diff options
Diffstat (limited to 'fw/fe310/eos/msgq.c')
| -rw-r--r-- | fw/fe310/eos/msgq.c | 94 |
1 files changed, 31 insertions, 63 deletions
diff --git a/fw/fe310/eos/msgq.c b/fw/fe310/eos/msgq.c index 2af3b14..8964046 100644 --- a/fw/fe310/eos/msgq.c +++ b/fw/fe310/eos/msgq.c @@ -3,13 +3,19 @@ #include <string.h> #include "eos.h" -#include "msgq.h" + +#include "event.h" #define IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) #define IDX_HALF ((uint8_t)1 << (sizeof(uint8_t) * 8 - 1)) #define IDX_LT(a,b) ((uint8_t)((uint8_t)(a) - (uint8_t)(b)) > IDX_HALF) #define IDX_LTE(a,b) ((uint8_t)((uint8_t)(b) - (uint8_t)(a)) < IDX_HALF) +void eos_msg_init(EOSMessage *msg, unsigned char *buffer, uint16_t size) { + msg->buffer = buffer; + msg->size = size; +} + void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->idx_r = 0; msgq->idx_w = 0; @@ -21,92 +27,54 @@ uint8_t eos_msgq_len(EOSMsgQ *msgq) { return (uint8_t)(msgq->idx_w - msgq->idx_r); } -int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len) { - return eos_msgq_push_widx(msgq, type, buffer, len, NULL); +int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, EOSMessage *msg, uint16_t len) { + return eos_msgq_push_widx(msgq, type, msg, len, NULL); } -int eos_msgq_push_widx(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len, uint8_t *_idx) { +int eos_msgq_push_widx(EOSMsgQ *msgq, unsigned char type, EOSMessage *msg, uint16_t len, uint8_t *_idx) { + uint8_t idx; + if ((uint8_t)(msgq->idx_w - msgq->idx_r) == msgq->size) return EOS_ERR_FULL; - uint8_t idx = IDX_MASK(msgq->idx_w, msgq->size); + idx = IDX_MASK(msgq->idx_w, msgq->size); msgq->array[idx].type = type; - msgq->array[idx].buffer = buffer; - msgq->array[idx].len = len; + if (msg) { + msgq->array[idx].buffer = msg->buffer; + msgq->array[idx].size = msg->size; + msgq->array[idx].len = len; + } else { + msgq->array[idx].buffer = NULL; + msgq->array[idx].size = 0; + msgq->array[idx].len = 0; + } msgq->idx_w++; if (_idx) *_idx = idx; + return EOS_OK; } -void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len) { - eos_msgq_pop_widx(msgq, type, buffer, len, NULL); +void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, EOSMessage *msg, uint16_t *len) { + eos_msgq_pop_widx(msgq, type, msg, len, NULL); } -void eos_msgq_pop_widx(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len, uint8_t *_idx) { +void eos_msgq_pop_widx(EOSMsgQ *msgq, unsigned char *type, EOSMessage *msg, uint16_t *len, uint8_t *_idx) { if (msgq->idx_r == msgq->idx_w) { *type = 0; - *buffer = NULL; + msg->buffer = NULL; + msg->size = 0; *len = 0; } else { uint8_t idx = IDX_MASK(msgq->idx_r, msgq->size); + *type = msgq->array[idx].type; - *buffer = msgq->array[idx].buffer; + msg->buffer = msgq->array[idx].buffer; + msg->size = msgq->array[idx].size; *len = msgq->array[idx].len; msgq->idx_r++; if (_idx) *_idx = idx; } } -int eos_msgq_find(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len) { - uint8_t i, j, idx; - unsigned char *_buffer; - uint16_t _len; - - if (msgq->idx_r == msgq->idx_w) { - if (buffer && len) { - *buffer = NULL; - *len = 0; - } - return EOS_ERR_NOTFOUND; - } - - idx = IDX_MASK(msgq->idx_r, msgq->size); - if (type == msgq->array[idx].type) { - _buffer = msgq->array[idx].buffer; - _len = msgq->array[idx].len; - if ((selector == NULL) || (sel_len == 0) || ((sel_len <= _len) && (memcmp(selector, _buffer, sel_len) == 0))) { - msgq->idx_r++; - if (buffer && len) { - *buffer = _buffer; - *len = _len; - } - return EOS_OK; - } - } - for (i = msgq->idx_r + 1; IDX_LT(i, msgq->idx_w); i++) { - idx = IDX_MASK(i, msgq->size); - if (type== msgq->array[idx].type) { - _buffer = msgq->array[idx].buffer; - _len = msgq->array[idx].len; - if ((selector == NULL) || (sel_len == 0) || ((sel_len <= _len) && (memcmp(selector, _buffer, sel_len) == 0))) { - for (j = i + 1; IDX_LT(j, msgq->idx_w); j++) { - msgq->array[IDX_MASK(j - 1, msgq->size)] = msgq->array[IDX_MASK(j, msgq->size)]; - } - msgq->idx_w--; - if (buffer && len) { - *buffer = _buffer; - *len = _len; - } - return EOS_OK; - } - } - } - if (buffer && len) { - *buffer = NULL; - *len = 0; - } - return EOS_ERR_NOTFOUND; -} - void eos_bufq_init(EOSBufQ *bufq, unsigned char **array, uint8_t size) { bufq->idx_r = 0; bufq->idx_w = 0; |
