#include #include "eos.h" #include "msgq.h" #define IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) void eos_msg_init(EOSMessage *msg, unsigned char *buffer, uint16_t size) { msg->buffer = buffer; msg->size = size; msg->flags = 0; } void eos_msg_set_flags(EOSMessage *msg, uint8_t flags) { msg->flags |= flags; } void eos_msg_clear_flags(EOSMessage *msg, uint8_t flags) { msg->flags &= ~flags; } uint8_t eos_msg_flags(EOSMessage *msg) { return msg->flags; } void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->idx_r = 0; msgq->idx_w = 0; msgq->size = size; msgq->array = array; } 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, EOSMessage *msg, uint16_t len) { uint8_t idx; if ((uint8_t)(msgq->idx_w - msgq->idx_r) == msgq->size) return EOS_ERR_FULL; idx = IDX_MASK(msgq->idx_w, msgq->size); msgq->array[idx].type = type; 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++; return EOS_OK; } void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, EOSMessage *msg, uint16_t *len) { if (msgq->idx_r == msgq->idx_w) { *type = 0; eos_msg_init(msg, NULL, 0); *len = 0; } else { uint8_t idx = IDX_MASK(msgq->idx_r, msgq->size); *type = msgq->array[idx].type; eos_msg_init(msg, msgq->array[idx].buffer, msgq->array[idx].size); *len = msgq->array[idx].len; msgq->idx_r++; } } void eos_bufq_init(EOSBufQ *bufq, unsigned char **array, uint8_t size) { bufq->idx_r = 0; bufq->idx_w = 0; bufq->size = size; bufq->array = array; } uint8_t eos_bufq_len(EOSBufQ *bufq) { return (uint8_t)(bufq->idx_w - bufq->idx_r); } int eos_bufq_push(EOSBufQ *bufq, unsigned char *buffer) { if ((uint8_t)(bufq->idx_w - bufq->idx_r) == bufq->size) return EOS_ERR_FULL; bufq->array[IDX_MASK(bufq->idx_w++, bufq->size)] = buffer; return EOS_OK; } unsigned char *eos_bufq_pop(EOSBufQ *bufq) { if (bufq->idx_r == bufq->idx_w) return NULL; return bufq->array[IDX_MASK(bufq->idx_r++, bufq->size)]; }