summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/msgq.c
blob: 89640461e70b863a79a082a5e628c79f66df152e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "eos.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;
    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) {
    return eos_msgq_push_widx(msgq, type, msg, len, NULL);
}

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;

    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++;
    if (_idx) *_idx = idx;

    return EOS_OK;
}

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, EOSMessage *msg, uint16_t *len, uint8_t *_idx) {
    if (msgq->idx_r == msgq->idx_w) {
        *type = 0;
        msg->buffer = NULL;
        msg->size = 0;
        *len = 0;
    } else {
        uint8_t idx = IDX_MASK(msgq->idx_r, msgq->size);

        *type = msgq->array[idx].type;
        msg->buffer = msgq->array[idx].buffer;
        msg->size = msgq->array[idx].size;
        *len = msgq->array[idx].len;
        msgq->idx_r++;
        if (_idx) *_idx = idx;
    }
}

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)];
}