-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathat_read.c
273 lines (230 loc) · 6.94 KB
/
at_read.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
Copyright (C) 2010 - 2011
bg <[email protected]>
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* vasprintf() in asterisk/utils.h */
#endif /* #ifndef _GNU_SOURCE */
#include "ast_config.h"
#include <sys/types.h>
#include <errno.h>
#include <asterisk/channel.h> /* ast_waitfor_n_fd() */
#include <asterisk/logger.h> /* ast_debug() */
#include "chan_quectel.h"
#include "at_read.h"
#include "ringbuffer.h"
/*!
* \brief Wait for activity on an socket
* \param fd -- file descriptor
* \param ms -- pointer to an int containing a timeout in ms
* \return 0 on timeout and the socket fd (non-zero) otherwise
* \retval 0 timeout
*/
EXPORT_DEF int at_wait (int fd, int* ms)
{
int exception, outfd;
outfd = ast_waitfor_n_fd (&fd, 1, ms, &exception);
if (outfd < 0)
{
outfd = 0;
}
return outfd;
}
#/* return number of bytes read */
EXPORT_DEF ssize_t at_read (int fd, const char * dev, struct ringbuffer* rb)
{
struct iovec iov[2];
int iovcnt;
ssize_t n = -1;
/* TODO: read until major error */
iovcnt = rb_write_iov (rb, iov);
if (iovcnt > 0)
{
n = readv (fd, iov, iovcnt);
if (n < 0)
{
if (errno != EINTR && errno != EAGAIN)
{
ast_debug (1, "[%s] readv() error: %d\n", dev, errno);
return n;
}
return 0;
}
else if (n > 0)
{
rb_write_upd (rb, n);
ast_debug (5, "[%s] receive %zu byte, used %zu, free %zu, read %zu, write %zu\n",
dev, n, rb_used (rb), rb_free (rb), rb->read, rb->write);
iovcnt = rb_read_all_iov (rb, iov);
if (iovcnt > 0)
{
if (iovcnt == 2)
{
ast_debug (5, "[%s] [%.*s%.*s]\n", dev,
(int) iov[0].iov_len, (char*) iov[0].iov_base,
(int) iov[1].iov_len, (char*) iov[1].iov_base);
}
else
{
ast_debug (5, "[%s] [%.*s]\n", dev,
(int) iov[0].iov_len, (char*) iov[0].iov_base);
}
}
}
}
else
ast_log (LOG_ERROR, "[%s] at cmd receive buffer overflow\n", dev);
return n;
}
EXPORT_DEF int at_read_result_iov (struct pvt *pvt, int * read_result, struct ringbuffer* rb, struct iovec iov[2])
{
char dev[sizeof(PVT_ID(pvt))];
ast_copy_string(dev, PVT_ID(pvt), sizeof(dev));
int iovcnt = 0;
int res;
size_t s;
s = rb_used (rb);
ast_verb (100, "[%s] %s used=%d read_result=%d str=\"%.*s\"\n", dev, __func__, s, *read_result, s, (char*)rb->buffer + rb->read);
if (s > 0)
{
ast_verb (100, "[%s] %s: len=%4$d input=\"%.*s\"\n", dev, __func__, s, *read_result, (char*)rb->buffer + rb->read);
if (*read_result == 0)
{
res = rb_memcmp (rb, "\r\n", 2);
if (rb_memcmp (rb, "\n", 1) == 0)
{
rb_read_upd (rb, 1);
*read_result = 1;
return at_read_result_iov (pvt, read_result, rb, iov);
}
else if (res == 0)
{
rb_read_upd (rb, 2);
*read_result = 1;
return at_read_result_iov (pvt, read_result, rb, iov);
}
else if (res > 0)
{
if (rb_memcmp (rb, "\n", 1) == 0)
{
rb_read_upd (rb, 1);
return at_read_result_iov (pvt, read_result, rb, iov);
}
if (rb_read_until_char_iov (rb, iov, '\r') > 0)
{
s = iov[0].iov_len + iov[1].iov_len + 1;
}
rb_read_upd (rb, s);
return at_read_result_iov (pvt, read_result, rb, iov);
}
return 0;
}
else
{
ast_verb (100, "[%s] %s: Comparing ..\n", dev, __func__);
if (rb_memcmp (rb, "+CSSI:", 6) == 0)
{
iovcnt = rb_read_n_iov (rb, iov, 8);
if (iovcnt > 0)
{
*read_result = 0;
}
return iovcnt;
}
else if (rb_memcmp (rb, "\r\n+CSSU:", 8) == 0 || rb_memcmp (rb, "\r\n+CMS ERROR:", 13) == 0 || rb_memcmp (rb, "\r\n+CMGS:", 8) == 0 || rb_memcmp (rb, "\r\nOK", 4) == 0)
{
ast_verb (100, "[%s] %s: OK matched\n", dev, __func__);
rb_read_upd (rb, 2);
return at_read_result_iov (pvt, read_result, rb, iov);
}
else if (rb_memcmp (rb, "> ", 2) == 0)
{
*read_result = 0;
return rb_read_n_iov (rb, iov, 2);
}
else if (rb_memcmp (rb, "+CMT:", 5) == 0 || (rb_memcmp (rb, "CONNECT", 7) == 0 && pvt->connect_length > 0))
{
char *endptr;
if(rb_memcmp (rb, "+CMT:", 5) == 0) {
int len = strtol(memchr(rb->buffer + rb->read, ',', rb->used) + 1, &endptr, 10);
s = (size_t)memchr(rb->buffer + rb->read, '\n', rb->used) - (size_t)(rb->buffer + rb->read) + (len+8)*2 + 2;
ast_verb (100, "[%s] %s: +CMT matched. read %d bytes (len=%d)\n", dev, __func__, s, len);
}
else if(rb_memcmp (rb, "CONNECT", 7) == 0) {
s = (size_t)memchr(rb->buffer + rb->read, '\n', rb->used) - (size_t)(rb->buffer + rb->read) + pvt->connect_length + 1;
ast_verb (100, "[%s] %s: CONNECT matched. read %d bytes (%d)\n", dev, __func__, s, pvt->connect_length);
}
else {
ast_verb (100, "[%s] %s: none matched. read %d bytes (%d)\n", dev, __func__, s);
}
iovcnt = rb_read_n_iov (rb, iov, s);
if (iovcnt > 0)
{
*read_result = 0;
}
ast_verb (100, "[%s] %s: +CMT/CONNECT return %d bytes (read_result=%d)\n", dev, __func__, iov[0].iov_len + iov[1].iov_len + 1, *read_result);
return iovcnt;
}
else if (rb_memcmp (rb, "+CMGR:", 6) == 0 || rb_memcmp (rb, "+CNUM:", 6) == 0 || rb_memcmp (rb, "ERROR+CNUM:", 11) == 0 || rb_memcmp (rb, "+CLCC:", 6) == 0)
{
iovcnt = rb_read_until_mem_iov (rb, iov, "\n\r\nOK\r\n", 7);
if (iovcnt > 0)
{
*read_result = 0;
}
return iovcnt;
}
else
{
ast_verb (100, "[%s] %s: General matched (rb=0x%02x,iov_len=0x%02x)\n", dev, __func__, rb, rb->size - rb->read);
iovcnt = rb_read_until_mem_iov (rb, iov, "\r\n", 2);
ast_verb (100, "[%s] %s: rb_read_until_mem_iov returned (rb=0x%02x,iov_len=0x%02x, iovcnt=%d)\n", dev, __func__, rb, rb->size - rb->read, iovcnt);
ast_verb (100, "[%s] %s: \"%.*s\"\n", dev, __func__, iov[0].iov_len + iov[1].iov_len + 1, (char*)rb->buffer + rb->read);
if (iovcnt > 0)
{
*read_result = 0;
s = iov[0].iov_len + iov[1].iov_len + 1;
return rb_read_n_iov (rb, iov, s);
}
}
}
}
return 0;
}
EXPORT_DEF at_res_t at_read_result_classification (struct ringbuffer * rb, size_t len)
{
at_res_t at_res = RES_UNKNOWN;
unsigned idx;
ast_verb (100, "%s: at_responses .ids_first=%d .ids=%d .name_first=%d .name=%d", __func__, at_responses.ids_first, at_responses.ids, at_responses.name_first, at_responses.name_last);
for(idx = at_responses.ids_first; idx < at_responses.ids; idx++)
{
if (rb_memcmp (rb, at_responses.responses[idx].id, at_responses.responses[idx].idlen) == 0)
{
at_res = at_responses.responses[idx].res;
break;
}
}
ast_verb (100, "%s: %d (%s) matched (len=%d)", __func__, at_res, at_res2str (at_res), len);
switch (at_res)
{
case RES_SMS_PROMPT:
len = 2;
break;
case RES_CMGR:
len += 7;
break;
case RES_CSSI:
len = 8;
break;
default:
len += 1;
break;
}
rb_read_upd (rb, len);
ast_verb (100, "%s: receive result at_res=%d (%s) str=\"%.*s\"\n", __func__, at_res, at_res2str (at_res), len, (char*)rb->buffer + rb->read);
return at_res;
}