summaryrefslogtreecommitdiff
path: root/code/fe310/eos/event.c
blob: f7ee4eff699312749bea2b26eb5f19011a290d49 (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
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

#include "encoding.h"
#include "platform.h"

#include "net.h"
#include "msgq.h"
#include "event.h"

EOSMsgQ _eos_event_q;
static EOSMsgItem event_q_array[EOS_EVT_SIZE_Q];

static eos_evt_fptr_t evt_handler[EOS_EVT_MAX_EVT];
static uint16_t evt_handler_wrapper_acq = 0;
static uint16_t evt_handler_wrapper_en = 0;

void eos_evtq_init(void) {
    int i;
    
    for (i=0; i<EOS_EVT_MAX_EVT; i++) {
        evt_handler[i] = eos_evtq_bad_handler;
    }
    eos_msgq_init(&_eos_event_q, event_q_array, EOS_EVT_SIZE_Q);
}

int eos_evtq_push(unsigned char cmd, unsigned char *buffer, uint16_t len) {
    clear_csr(mstatus, MSTATUS_MIE);
    int ret = eos_msgq_push(&_eos_event_q, cmd, buffer, len);
    set_csr(mstatus, MSTATUS_MIE);
    return ret;
}

void eos_evtq_pop(unsigned char *cmd, unsigned char **buffer, uint16_t *len) {
    clear_csr(mstatus, MSTATUS_MIE);
    eos_msgq_pop(&_eos_event_q, cmd, buffer, len);
    set_csr(mstatus, MSTATUS_MIE);
}

void eos_evtq_bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) {
    write(1, "error\n", 6);
}

void eos_evtq_handler_wrapper(unsigned char cmd, unsigned char *buffer, uint16_t len, uint16_t *flags_acq, uint16_t flag, eos_evt_fptr_t f) {
    int ok = eos_net_acquire(*flags_acq & flag);
    if (ok) {
        f(cmd, buffer, len);
        eos_net_release();
        *flags_acq &= ~flag;
    } else {
        *flags_acq |= flag;
        eos_evtq_push(cmd, buffer, len);
    }
}

void eos_evtq_handle(unsigned char cmd, unsigned char *buffer, uint16_t len) {
    if (((cmd & EOS_EVT_MASK) >> 4) > EOS_EVT_MAX_EVT) {
        eos_evtq_bad_handler(cmd, buffer, len);
    } else {
        unsigned char idx = ((cmd & EOS_EVT_MASK) >> 4) - 1;
        uint16_t flag = (uint16_t)1 << idx;
        if (flag & evt_handler_wrapper_en) {
            eos_evtq_handler_wrapper(cmd, buffer, len, &evt_handler_wrapper_acq, flag, evt_handler[idx]);
        } else {
            evt_handler[idx](cmd, buffer, len);
        }
    }
}

void eos_evtq_loop(void) {
    unsigned char cmd;
    unsigned char *buffer;
    uint16_t len;
    int foo = 1;

    while(foo) {
        clear_csr(mstatus, MSTATUS_MIE);
        eos_msgq_pop(&_eos_event_q, &cmd, &buffer, &len);
        if (cmd) {
            set_csr(mstatus, MSTATUS_MIE);
            eos_evtq_handle(cmd, buffer, len);
            clear_csr(mstatus, MSTATUS_MIE);
        } else {
            asm volatile ("wfi");
        }
        set_csr(mstatus, MSTATUS_MIE);
    }
}

void eos_evtq_set_handler(unsigned char cmd, eos_evt_fptr_t handler, uint8_t flags) {
    if (flags & EOS_EVT_FLAG_WRAP) {
        uint16_t flag = (uint16_t)1 << (((cmd & EOS_EVT_MASK) >> 4) - 1);
        evt_handler_wrapper_en |= flag;
    }
    evt_handler[((cmd & EOS_EVT_MASK) >> 4) - 1] = handler;
}