summaryrefslogtreecommitdiff
path: root/code/fe310/eos/sock.c
blob: ab0fc59740e267cbe6ea46496d2785c373b1c82c (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
101
102
103
104
105
106
107
108
109
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "eos.h"
#include "event.h"
#include "net.h"

#include "sock.h"

static eos_sock_fptr_t sock_handler[EOS_SOCK_MAX_SOCK];
static uint16_t sock_handler_flags_buf_free = 0;
static uint16_t sock_handler_flags_buf_acq = 0;

static void sock_handler_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
    if ((buffer == NULL) || (len < 2)) {
        eos_evtq_bad_handler(type, buffer, len);
        eos_net_free(buffer, 0);
        return;
    }

    if (buffer[0] == EOS_SOCK_MTYPE_PKT) {
        uint8_t sock = buffer[1];
        if (sock && (sock <= EOS_SOCK_MAX_SOCK)) {
            sock--;
        } else {
            eos_evtq_bad_handler(type, buffer, len);
            eos_net_free(buffer, 0);
            return;
        }
        uint16_t buf_free = ((uint16_t)1 << sock) & sock_handler_flags_buf_free;
        uint16_t buf_acq = ((uint16_t)1 << sock) & sock_handler_flags_buf_acq;
        if (buf_free) {
            eos_net_free(buffer, buf_acq);
            buffer = NULL;
            len = 0;
        }

        sock_handler[sock](buffer, len);

        if (buf_free && buf_acq) eos_net_release();
    }
}

void eos_sock_init(void) {
    eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handler_evt, 0);
}

int eos_sock_open_udp(void) {
    unsigned char type = EOS_SOCK_MTYPE_OPEN_DGRAM;
    unsigned char *buffer = eos_net_alloc();
    uint16_t buf_size;
    int rv, sock;

    buffer[0] = type;
    rv = eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 1, 0);
    if (rv) return rv;

    eos_evtq_get(EOS_NET_MTYPE_SOCK, &type, 1, &buffer, &buf_size);
    if (buf_size < 2) {
        eos_net_free(buffer, 0);
        return EOS_ERR_NET;
    }

    sock = buffer[1];
    eos_net_free(buffer, 1);

    if (sock == 0) return EOS_ERR_NET;
    return sock;
}

void eos_sock_close(int sock) {
    unsigned char *buffer = eos_net_alloc();
    buffer[0] = EOS_SOCK_MTYPE_CLOSE;
    buffer[1] = sock;
    eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, 1);
}

int eos_sock_sendto(int sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr) {
    unsigned char type = EOS_NET_MTYPE_SOCK;

    buffer[0] = EOS_SOCK_MTYPE_PKT;
    buffer[1] = sock;
    memcpy(buffer+2, addr->host, sizeof(addr->host));
    memcpy(buffer+2+sizeof(addr->host), &addr->port, sizeof(addr->port));
    return eos_net_send(type, buffer, size, more);
}

void eos_sock_getfrom(unsigned char *buffer, EOSNetAddr *addr) {
    memcpy(addr->host, buffer+2, sizeof(addr->host));
    memcpy(&addr->port, buffer+2+sizeof(addr->host), sizeof(addr->port));
}


void eos_sock_set_handler(int sock, eos_sock_fptr_t handler, uint8_t flags) {
    if (sock && (sock <= EOS_SOCK_MAX_SOCK)) {
        sock--;
    } else {
        return;
    }
    if (flags) {
        uint16_t flag = (uint16_t)1 << sock;
        if (flags & EOS_NET_FLAG_BUF_FREE) sock_handler_flags_buf_free |= flag;
        if (flags & EOS_NET_FLAG_BUF_ACQ) sock_handler_flags_buf_acq |= flag;
    }
    sock_handler[sock] = handler;
}