summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/net/cell.c
blob: c1feb0ab1aef75ddf4ea8f1525b02d782e7c3ed1 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

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

#include "cell.h"

static eos_evt_handler_t evt_handler[EOS_CELL_MAX_MTYPE];

static void cell_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
    unsigned char mtype;
    unsigned char idx;

    if ((buffer == NULL) || (len < 1)) {
        eos_net_bad_handler(type, buffer, len);
        return;
    }

    mtype = buffer[0];
    idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;
    if ((idx < EOS_CELL_MAX_MTYPE) && evt_handler[idx]) {
        evt_handler[idx](mtype & ~EOS_CELL_MTYPE_MASK, buffer, len);
    } else {
        eos_net_bad_handler(type, buffer, len);
    }
}

void eos_cell_init(void) {
    int i;

    for (i=0; i<EOS_CELL_MAX_MTYPE; i++) {
        evt_handler[i] = NULL;
    }
    eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handle_msg);
}

void eos_cell_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
    unsigned char idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;

    if (idx < EOS_CELL_MAX_MTYPE) evt_handler[idx] = handler;
}

eos_evt_handler_t eos_cell_get_handler(unsigned char mtype) {
    unsigned char idx = (mtype & EOS_CELL_MTYPE_MASK) >> 4;

    if (idx < EOS_CELL_MAX_MTYPE) return evt_handler[idx];
    return NULL;
}

int eos_cell_send_buffer(unsigned char *buffer, uint16_t buf_len, uint16_t offset, int sync) {
    buffer -= offset;
    return eos_net_send_async(EOS_NET_MTYPE_CELL, buffer, buf_len + offset, 1);
}

int eos_cell_status_parse(unsigned char *buffer, uint16_t len, uint8_t *status, uint8_t *connected) {
    if (len < 2) return EOS_ERR_SIZE;

    if (buffer[0] != (EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_STATUS)) return EOS_ERR_NET;
    buffer++;
    len -= 1;

    *status = buffer[0];
    buffer++;
    len -= 1;

    if (*status == EOS_CELL_STATUS_PPP) {
        if (len < 1) return EOS_ERR_SIZE;
        if (connected) *connected = buffer[0];
        buffer++;
        len--;
    }
    return EOS_OK;
}

int eos_cell_status(uint8_t *status, uint8_t *connected, unsigned char *buffer) {
    unsigned char type;
    uint16_t len;
    int do_release;
    int rv;

    do_release = 0;
    if (buffer == NULL) {
        buffer = eos_net_alloc();
        do_release = 1;
    }

    type = EOS_NET_MTYPE_CELL;
    len = 1;
    buffer[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_STATUS;

    rv = eos_net_xchg(&type, buffer, &len);
    if (rv) goto cell_status_fin;

    if (type != EOS_NET_MTYPE_CELL) {
        rv = EOS_ERR_NET;
        goto cell_status_fin;
    }

    rv = eos_cell_status_parse(buffer, len, status, connected);

cell_status_fin:
    if (do_release) eos_net_free(buffer, 1);
    return rv;
}

int eos_cell_uart_take(unsigned char *buffer, int sync) {
    int async;

    if (buffer == NULL) {
        buffer = eos_net_alloc();
        async = 1;
    } else {
        async = !sync;
    }
    buffer[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_TAKE;
    return _eos_net_send(EOS_NET_MTYPE_CELL, buffer, 1, async, 1);
}

int eos_cell_uart_give(unsigned char *buffer, int sync) {
    int async;

    if (buffer == NULL) {
        buffer = eos_net_alloc();
        async = 1;
    } else {
        async = !sync;
    }
    buffer[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_GIVE;
    return _eos_net_send(EOS_NET_MTYPE_CELL, buffer, 1, async, 1);
}

unsigned char *eos_cell_uart_data_buffer(uint16_t *offset) {
    unsigned char *buffer;

    buffer = eos_net_alloc();
    buffer[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_DATA;
    *offset = 1;
    return buffer + *offset;
}

int eos_cell_voice_dial(char *num, unsigned char *buffer, int sync) {
    int async;
    size_t num_len;

    num_len = strlen(num);
    if (num_len > EOS_CELL_MAX_DIAL_STR) return EOS_ERR_SIZE;

    if (buffer == NULL) {
        buffer = eos_net_alloc();
        async = 1;
    } else {
        async = !sync;
    }
    buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_DIAL;
    strcpy(buffer + 1, num);
    return _eos_net_send(EOS_NET_MTYPE_CELL, buffer, 1 + num_len, async, 1);
}

int eos_cell_voice_answer(unsigned char *buffer, int sync) {
    int async;

    if (buffer == NULL) {
        buffer = eos_net_alloc();
        async = 1;
    } else {
        async = !sync;
    }
    buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_ANSWER;
    return _eos_net_send(EOS_NET_MTYPE_CELL, buffer, 1, async, 1);
}

int eos_cell_voice_hangup(unsigned char *buffer, int sync) {
    int async;

    if (buffer == NULL) {
        buffer = eos_net_alloc();
        async = 1;
    } else {
        async = !sync;
    }
    buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_HANGUP;
    return _eos_net_send(EOS_NET_MTYPE_CELL, buffer, 1, async, 1);
}

unsigned char *eos_cell_voice_pcm_buffer(uint16_t *offset) {
    unsigned char *buffer;

    buffer = eos_net_alloc();
    buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_PCM;
    *offset = 1;
    return buffer + *offset;
}