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
|
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_log.h>
#include "eos.h"
#include "msgq.h"
#include "net.h"
#include "cell.h"
#define CELL_SIZE_QUEUE 2
static const char *TAG = "EOS CELL";
static uint8_t cell_mode;
static EOSBufQ cell_buf_q;
static unsigned char *cell_bufq_array[CELL_SIZE_QUEUE];
static SemaphoreHandle_t mutex;
static QueueHandle_t cell_queue;
static void _cell_handler(unsigned char _mtype, unsigned char *buffer, uint16_t buf_len) {
uint8_t mtype;
if (buf_len < 1) return;
mtype = buffer[0];
switch (mtype & EOS_CELL_MTYPE_MASK) {
case EOS_CELL_MTYPE_DEV: {
switch (mtype & ~EOS_CELL_MTYPE_MASK) {
case EOS_CELL_MTYPE_STATUS: {
size_t rv;
rv = eos_modem_get_status(buffer + 1);
eos_net_reply(EOS_NET_MTYPE_CELL, buffer, rv + 1);
break;
}
case EOS_CELL_MTYPE_RESET: {
eos_modem_reset();
break;
}
case EOS_CELL_MTYPE_UART_TAKE: {
uint8_t mode;
mode = eos_modem_get_mode();
if (mode == EOS_CELL_UART_MODE_NONE) break;
eos_modem_set_mode(EOS_CELL_UART_MODE_RELAY);
cell_mode = mode;
break;
}
case EOS_CELL_MTYPE_UART_GIVE: {
eos_modem_atinit();
eos_modem_set_mode(cell_mode);
break;
}
case EOS_CELL_MTYPE_UART_DATA: {
if (eos_modem_get_mode() == EOS_CELL_UART_MODE_RELAY) eos_modem_write(buffer + 1, buf_len - 1);
break;
}
}
break;
}
case EOS_CELL_MTYPE_VOICE: {
eos_cell_voice_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len);
break;
}
case EOS_CELL_MTYPE_SMS: {
eos_cell_sms_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len);
break;
}
case EOS_CELL_MTYPE_USSD: {
eos_cell_ussd_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len);
break;
}
case EOS_CELL_MTYPE_PDP: {
eos_cell_pdp_handler(mtype & ~EOS_CELL_MTYPE_MASK, buffer, buf_len);
break;
}
}
}
static void cell_handler_task(void *pvParameters) {
EOSMsgItem mi;
while (1) {
if (xQueueReceive(cell_queue, &mi, portMAX_DELAY)) {
_cell_handler(mi.type, mi.buffer, mi.len);
xSemaphoreTake(mutex, portMAX_DELAY);
eos_bufq_push(&cell_buf_q, mi.buffer);
xSemaphoreGive(mutex);
}
}
vTaskDelete(NULL);
}
static void cell_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
EOSMsgItem mi;
unsigned char *buf;
xSemaphoreTake(mutex, portMAX_DELAY);
buf = eos_bufq_pop(&cell_buf_q);
xSemaphoreGive(mutex);
if (buf == NULL) {
ESP_LOGE(TAG, "Cell message NOT handled: %2x", type);
return;
}
memcpy(buf, buffer, len);
mi.type = type;
mi.buffer = buf;
mi.len = len;
xQueueSend(cell_queue, &mi, portMAX_DELAY);
}
void eos_cell_init(void) {
int i;
eos_bufq_init(&cell_buf_q, cell_bufq_array, CELL_SIZE_QUEUE);
for (i=0; i<CELL_SIZE_QUEUE; i++) {
eos_bufq_push(&cell_buf_q, malloc(EOS_NET_SIZE_BUF));
}
mutex = xSemaphoreCreateBinary();
xSemaphoreGive(mutex);
cell_queue = xQueueCreate(CELL_SIZE_QUEUE, sizeof(EOSMsgItem));
xTaskCreate(cell_handler_task, "cell_handler", EOS_TASK_SSIZE_CELL, NULL, EOS_TASK_PRIORITY_CELL, NULL);
eos_net_set_handler(EOS_NET_MTYPE_CELL, cell_handler);
}
|