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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_timer.h>
#include <esp_log.h>
#include "eos.h"
#include "net.h"
#include "at_cmd.h"
#include "cell.h"
static const char *TAG = "EOS CELL VOICE";
extern char *at_cmd_buf;
void eos_cell_voice_handler(unsigned char _mtype, EOSMessage *msg, uint16_t len) {
unsigned char mtype;
unsigned char *buffer = msg->buffer;
int rv;
if (len < 1) return;
mtype = buffer[0] & ~EOS_CELL_MTYPE_MASK;
buffer++;
len--;
switch (mtype) {
case EOS_CELL_MTYPE_VOICE_DIAL: {
if (len > EOS_CELL_SIZE_PHNUM) break;
rv = snprintf(at_cmd_buf, AT_SIZE_CMD_BUF, "ATD%.*s;\r", len, buffer);
if ((rv < 0) || (rv >= AT_SIZE_CMD_BUF)) break;
rv = eos_modem_take(1000);
if (rv) break;
at_cmd(at_cmd_buf);
rv = at_expect("^OK", "^(ERROR|\\+CME ERROR: [0-9]+)", 1000);
eos_modem_give();
eos_cell_pcm_start();
break;
}
case EOS_CELL_MTYPE_VOICE_ANSWER: {
rv = eos_modem_take(1000);
if (rv) break;
at_cmd("ATA\r");
// Response will be picked up by urc handler
eos_modem_give();
eos_cell_pcm_start();
break;
}
case EOS_CELL_MTYPE_VOICE_HANGUP: {
eos_cell_pcm_stop();
rv = eos_modem_take(1000);
if (rv) break;
at_cmd("AT+CHUP\r");
// Response will be picked up by urc handler
eos_modem_give();
break;
}
case EOS_CELL_MTYPE_VOICE_PCM: {
eos_cell_pcm_push(buffer, len);
break;
}
}
}
static void ring_handler(char *urc, regmatch_t m[]) {
EOSMessage msg;
uint16_t len;
char *buffer;
regmatch_t match[2];
int rv = EOS_OK;
eos_net_alloc(&msg);
if (msg.size < EOS_CELL_SIZE_PHNUM + 1) {
eos_net_free(&msg);
return;
}
msg.buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_RING;
len = 1;
rv = at_expect_match("^\\+CLIP: \"(\\+?[0-9]+)\"", NULL, &buffer, match, 2, REG_EXTENDED, 1000);
if (!rv) {
regoff_t num_len;
num_len = match[1].rm_eo - match[1].rm_so;
if (num_len > EOS_CELL_SIZE_PHNUM) {
eos_net_free(&msg);
return;
}
memcpy(msg.buffer + 1, buffer + match[1].rm_so, num_len);
len += num_len;
}
rv = eos_net_send(EOS_NET_MTYPE_CELL, &msg, len);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
}
static void busy_handler(char *urc, regmatch_t m[]) {
EOSMessage msg;
int rv;
eos_cell_pcm_stop();
eos_net_alloc(&msg);
if (msg.size < 1) {
eos_net_free(&msg);
return;
}
msg.buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_BUSY;
rv = eos_net_send(EOS_NET_MTYPE_CELL, &msg, 1);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
}
static void miss_handler(char *urc, regmatch_t m[]) {
EOSMessage msg;
int rv;
eos_cell_pcm_stop();
eos_net_alloc(&msg);
if (msg.size < 1) {
eos_net_free(&msg);
return;
}
msg.buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_MISS;
rv = eos_net_send(EOS_NET_MTYPE_CELL, &msg, 1);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
}
static void call_begin_handler(char *urc, regmatch_t m[]) {
EOSMessage msg;
int rv;
eos_net_alloc(&msg);
if (msg.size < 1) {
eos_net_free(&msg);
return;
}
vTaskDelay(100 / portTICK_PERIOD_MS);
at_cmd("AT+CECH=0x0000\r");
at_expect("^OK", "^ERROR", 1000);
at_cmd("AT+CECDT=0x0000\r");
at_expect("^OK", "^ERROR", 1000);
at_cmd("AT+CECWB=0x0000\r");
at_expect("^OK", "^ERROR", 1000);
at_cmd("AT+CNSLIM=0x0000\r");
at_expect("^OK", "^ERROR", 1000);
/*
at_cmd("AT+CECRX=0\r");
at_expect("^OK", "^ERROR", 1000);
*/
msg.buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_BEGIN;
rv = eos_net_send(EOS_NET_MTYPE_CELL, &msg, 1);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
}
static void call_end_handler(char *urc, regmatch_t m[]) {
EOSMessage msg;
int duration = 0;
int rv;
eos_cell_pcm_stop();
eos_net_alloc(&msg);
if (msg.size < 5) {
eos_net_free(&msg);
return;
}
sscanf(urc + m[1].rm_so, "%6d", &duration);
msg.buffer[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_END;
msg.buffer[1] = duration >> 24;
msg.buffer[2] = duration >> 16;
msg.buffer[3] = duration >> 8;
msg.buffer[4] = duration;
rv = eos_net_send(EOS_NET_MTYPE_CELL, &msg, 5);
if (rv) ESP_LOGE(TAG, "NET SEND ERR:%d", rv);
}
void eos_cell_voice_init(void) {
int rv;
rv = at_urc_insert("^RING", ring_handler, REG_EXTENDED);
assert(rv == EOS_OK);
rv = at_urc_insert("^BUSY", busy_handler, REG_EXTENDED);
assert(rv == EOS_OK);
rv = at_urc_insert("^NO CARRIER", miss_handler, REG_EXTENDED);
assert(rv == EOS_OK);
rv = at_urc_insert("^MISSED.CALL: [^ ]+ (\\+?[0-9]+)$", miss_handler, REG_EXTENDED);
assert(rv == EOS_OK);
rv = at_urc_insert("^VOICE CALL: BEGIN", call_begin_handler, REG_EXTENDED);
assert(rv == EOS_OK);
rv = at_urc_insert("^VOICE CALL: END: ([0-9]{6}$)$", call_end_handler, REG_EXTENDED);
assert(rv == EOS_OK);
}
|