-
Notifications
You must be signed in to change notification settings - Fork 319
/
at_parse.c
632 lines (546 loc) · 13.1 KB
/
at_parse.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
bg <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include "memmem.h"
#include <stdio.h> /* NULL */
#include <errno.h> /* errno */
#include <stdlib.h> /* strtol */
#include "at_parse.h"
#include "mutils.h" /* ITEMS_OF() */
#include "chan_dongle.h"
#include "pdu.h" /* pdu_parse() */
#/* */
static unsigned mark_line(char * line, const char * delimiters, char * pointers[])
{
unsigned found = 0;
for(; line[0] && delimiters[found]; line++)
{
if(line[0] == delimiters[found])
{
pointers[found] = line;
found++;
}
}
return found;
}
/*!
* \brief Parse a CNUM response
* \param str -- string to parse (null terminated)
* @note str will be modified when the CNUM message is parsed
* \return NULL on error (parse error) or a pointer to the subscriber number
*/
EXPORT_DEF char * at_parse_cnum (char* str)
{
/*
* parse CNUM response in the following format:
* +CNUM: <name>,<number>,<type>
* example
* +CNUM: "Subscriber Number","+79139131234",145
* +CNUM: "Subscriber Number","",145
* +CNUM: "Subscriber Number",,145
*/
char delimiters[] = ":,,";
char * marks[STRLEN(delimiters)];
/* parse URC only here */
if(mark_line(str, delimiters, marks) == ITEMS_OF(marks))
{
marks[1]++;
if(marks[1][0] == '"')
marks[1]++;
if(marks[2][-1] == '"')
marks[2]--;
marks[2][0] = 0;
return marks[1];
}
return NULL;
}
/*!
* \brief Parse a COPS response
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* @note str will be modified when the COPS message is parsed
* \return NULL on error (parse error) or a pointer to the provider name
*/
EXPORT_DEF char* at_parse_cops (char* str)
{
/*
* parse COPS response in the following format:
* +COPS: <mode>[,<format>,<oper>,<?>]
*
* example
* +COPS: 0,0,"TELE2",0
*/
char delimiters[] = ":,,,";
char * marks[STRLEN(delimiters)];
/* parse URC only here */
if(mark_line(str, delimiters, marks) == ITEMS_OF(marks))
{
marks[2]++;
if(marks[2][0] == '"')
marks[2]++;
if(marks[3][-1] == '"')
marks[3]--;
marks[3][0] = 0;
return marks[2];
}
return NULL;
}
/*!
* \brief Parse a CREG response
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* \param gsm_reg -- a pointer to a int
* \param gsm_reg_status -- a pointer to a int
* \param lac -- a pointer to a char pointer which will store the location area code in hex format
* \param ci -- a pointer to a char pointer which will store the cell id in hex format
* @note str will be modified when the CREG message is parsed
* \retval 0 success
* \retval -1 parse error
*/
EXPORT_DEF int at_parse_creg (char* str, unsigned len, int* gsm_reg, int* gsm_reg_status, char** lac, char** ci)
{
unsigned i;
int state;
char* p1 = NULL;
char* p2 = NULL;
char* p3 = NULL;
char* p4 = NULL;
*gsm_reg = 0;
*gsm_reg_status = -1;
*lac = NULL;
*ci = NULL;
/*
* parse CREG response in the following format:
* +CREG: [<p1>,]<p2>[,<p3>,<p4>]
*/
for (i = 0, state = 0; i < len && state < 8; i++)
{
switch (state)
{
case 0:
if (str[i] == ':')
{
state++;
}
break;
case 1:
if (str[i] != ' ')
{
p1 = &str[i];
state++;
}
/* fall through */
case 2:
if (str[i] == ',')
{
str[i] = '\0';
state++;
}
break;
case 3:
if (str[i] != ' ')
{
p2 = &str[i];
state++;
}
/* fall through */
case 4:
if (str[i] == ',')
{
str[i] = '\0';
state++;
}
break;
case 5:
if (str[i] != ' ')
{
p3 = &str[i];
state++;
}
/* fall through */
case 6:
if (str[i] == ',')
{
str[i] = '\0';
state++;
}
break;
case 7:
if (str[i] != ' ')
{
p4 = &str[i];
state++;
}
break;
}
}
if (state < 2)
{
return -1;
}
if ((p2 && !p3 && !p4) || (p2 && p3 && p4))
{
p1 = p2;
}
if (p1)
{
errno = 0;
*gsm_reg_status = (int) strtol (p1, (char**) NULL, 10);
if (*gsm_reg_status == 0 && errno == EINVAL)
{
*gsm_reg_status = -1;
return -1;
}
if (*gsm_reg_status == 1 || *gsm_reg_status == 5)
{
*gsm_reg = 1;
}
}
if (p2 && p3 && !p4)
{
*lac = p2;
*ci = p3;
}
else if (p3 && p4)
{
*lac = p3;
*ci = p4;
}
return 0;
}
/*!
* \brief Parse a CMTI notification
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* @note str will be modified when the CMTI message is parsed
* \return -1 on error (parse error) or the index of the new sms message
*/
EXPORT_DEF int at_parse_cmti (const char* str)
{
int index;
/*
* parse cmti info in the following format:
* +CMTI: <mem>,<index>
*/
return sscanf (str, "+CMTI: %*[^,],%u", &index) == 1 ? index : -1;
}
static const char * parse_cmgr_text(char ** str, size_t len, char * oa, size_t oa_len, str_encoding_t * oa_enc, char ** msg, str_encoding_t * msg_enc)
{
/*
* parse cmgr info in the following TEXT format:
* +CMGR: "<msg status>","+123456789",,timestamp<CR><LF>
* <message text><CR><LF><CR><LF>
* OK<CR><LF>
* or
* +CMGR: "<msg status>","002B....",,timestamp<CR><LF>
* <message text><CR><LF><CR><LF>
* OK<CR><LF>
*/
char delimiters[] = ",,,\n";
char * marks[STRLEN(delimiters)];
size_t length;
unsigned count = mark_line(*str, delimiters, marks);
if(count == ITEMS_OF(marks))
{
/* unquote number */
marks[0]++;
if(marks[0][0] == '"')
marks[0]++;
if(marks[1][-1] == '"')
marks[1]--;
length = marks[1] - marks[0] + 1;
if(oa_len < length)
return "Not enought space for store number";
*oa_enc = get_encoding(RECODE_DECODE, marks[0], length - 1);
marks[1][0] = 0;
memcpy(oa, marks[0], length);
*msg = marks[3] + 1;
length = len - (*msg - *str);
*msg_enc = get_encoding(RECODE_DECODE, *msg, length);
return NULL;
}
else if(count > 0)
*str = marks[count - 1];
return "Can't parse +CMGR response text";
}
static const char* parse_cmgr_pdu(char** str, attribute_unused size_t len, char* oa, size_t oa_len, str_encoding_t* oa_enc, char** msg, str_encoding_t* msg_enc)
{
/*
* parse cmgr info in the following PDU format
* +CMGR: message_status,[address_text],TPDU_length<CR><LF>
* SMSC_number_and_TPDU<CR><LF><CR><LF>
* OK<CR><LF>
*
* sample
* +CMGR: 1,,31
* 07911234567890F3040B911234556780F20008012150220040210C041F04400438043204350442<CR><LF><CR><LF>
* OK<CR><LF>
*/
char delimiters[] = ",,\n";
char * marks[STRLEN(delimiters)];
char * end;
size_t tpdu_length;
if(mark_line(*str, delimiters, marks) == ITEMS_OF(marks))
{
tpdu_length = strtol(marks[1] + 1, &end, 10);
if(tpdu_length <= 0 || end[0] != '\r')
return "Invalid TPDU length in CMGR PDU status line";
*str = marks[2] + 1;
return pdu_parse(str, tpdu_length, oa, oa_len, oa_enc, msg, msg_enc);
}
return "Can't parse +CMGR response";
}
/*!
* \brief Parse a CMGR message
* \param str -- pointer to pointer of string to parse (null terminated)
* \param len -- string lenght
* \param number -- a pointer to a char pointer which will store the from number
* \param text -- a pointer to a char pointer which will store the message text
* @note str will be modified when the CMGR message is parsed
* \retval 0 success
* \retval -1 parse error
*/
EXPORT_DEF const char * at_parse_cmgr(char ** str, size_t len, char * oa, size_t oa_len, str_encoding_t * oa_enc, char ** msg, str_encoding_t * msg_enc)
{
const char* rv = "Can't parse +CMGR response line";
/* skip "+CMGR:" */
*str += 6;
len -= 6;
/* skip leading spaces */
while(len > 0 && str[0][0] == ' ')
{
(*str)++;
len--;
}
if(len > 0)
{
/* check PDU or TEXT mode */
const char* (*fptr)(char** str, size_t len, char* num, size_t num_len, str_encoding_t * oa_enc, char** msg, str_encoding_t * msg_enc);
fptr = str[0][0] == '"' ? parse_cmgr_text : parse_cmgr_pdu;
rv = (*fptr)(str, len, oa, oa_len, oa_enc, msg, msg_enc);
}
return rv;
}
/*!
* \brief Parse a CUSD answer
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* @note str will be modified when the CUSD string is parsed
* \retval 0 success
* \retval -1 parse error
*/
EXPORT_DEF int at_parse_cusd (char* str, int * type, char** cusd, int * dcs)
{
/*
* parse cusd message in the following format:
* +CUSD: <m>,[<str>,<dcs>]
*
* examples
* +CUSD: 5
* +CUSD: 0,"100,00 EURO, valid till 01.01.2010, you are using tariff "Mega Tariff". More informations *111#.",15
*/
char delimiters[] = ":,,";
char * marks[STRLEN(delimiters)];
unsigned count;
*type = -1;
*cusd = "";
*dcs = -1;
count = mark_line(str, delimiters, marks);
// 0, 1, 2, 3
if(count > 0)
{
if(sscanf(marks[0] + 1, "%u", type) == 1)
{
if(count > 1)
{
marks[1]++;
if(marks[1][0] == '"')
marks[1]++;
*cusd = marks[1];
if(count > 2) {
sscanf(marks[2] + 1, "%u", dcs);
if(marks[2][-1] == '"')
marks[2]--;
marks[2][0] = 0;
} else {
int len = strlen(*cusd);
if(len > 0 && (*cusd)[len - 1] == '"')
(*cusd)[len-1] = 0;
}
}
return 0;
}
}
return -1;
}
/*!
* \brief Parse a CPIN notification
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* \return 2 if PUK required
* \return 1 if PIN required
* \return 0 if no PIN required
* \return -1 on error (parse error) or card lock
*/
EXPORT_DEF int at_parse_cpin (char* str, size_t len)
{
static const struct {
const char * value;
unsigned length;
} resp[] = {
{ "READY", 5 },
{ "SIM PIN", 7 },
{ "SIM PUK", 7 },
};
unsigned idx;
for(idx = 0; idx < ITEMS_OF(resp); idx++)
{
if(memmem (str, len, resp[idx].value, resp[idx].length) != NULL)
return idx;
}
return -1;
}
/*!
* \brief Parse +CSQ response
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* \retval 0 success
* \retval -1 error
*/
EXPORT_DEF int at_parse_csq (const char* str, int* rssi)
{
/*
* parse +CSQ response in the following format:
* +CSQ: <RSSI>,<BER>
*/
return sscanf (str, "+CSQ:%2d,", rssi) == 1 ? 0 : -1;
}
/*!
* \brief Parse a ^RSSI notification
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* \return -1 on error (parse error) or the rssi value
*/
EXPORT_DEF int at_parse_rssi (const char* str)
{
int rssi = -1;
/*
* parse RSSI info in the following format:
* ^RSSI:<rssi>
*/
sscanf (str, "^RSSI:%d", &rssi);
return rssi;
}
/*!
* \brief Parse a ^MODE notification (link mode)
* \param str -- string to parse (null terminated)
* \param len -- string lenght
* \return -1 on error (parse error) or the the link mode value
*/
EXPORT_DEF int at_parse_mode (char * str, int * mode, int * submode)
{
/*
* parse RSSI info in the following format:
* ^MODE:<mode>,<submode>
*/
return sscanf (str, "^MODE:%d,%d", mode, submode) == 2 ? 0 : -1;
}
#/* */
EXPORT_DEF int at_parse_csca(char* str, char ** csca)
{
/*
* parse CSCA info in the following format:
* +CSCA: <SCA>,<TOSCA>
* +CSCA: "+79139131234",145
* +CSCA: "",145
*/
char delimiters[] = "\"\"";
char * marks[STRLEN(delimiters)];
if(mark_line(str, delimiters, marks) == ITEMS_OF(marks))
{
*csca = marks[0] + 1;
marks[1][0] = 0;
return 0;
}
return -1;
}
#/* */
EXPORT_DEF int at_parse_clcc(char* str, unsigned * call_idx, unsigned * dir, unsigned * state, unsigned * mode, unsigned * mpty, char ** number, unsigned * toa)
{
/*
* +CLCC:<id1>,<dir>,<stat>,<mode>,<mpty>[,<number>,<type>[,<alpha>[,<priority>]]]\r\n
* ...
* +CLCC:<id1>,<dir>,<stat>,<mode>,<mpty>[,<number>,<type>[,<alpha>[,<priority>]]]\r\n
* examples
* +CLCC: 1,1,4,0,0,"",145
* +CLCC: 1,1,4,0,0,"+79139131234",145
* +CLCC: 1,1,4,0,0,"0079139131234",145
* +CLCC: 1,1,4,0,0,"+7913913ABCA",145
*/
char delimiters[] = ":,,,,,,";
char * marks[STRLEN(delimiters)];
*call_idx = 0;
*dir = 0;
*state = 0;
*mode = 0;
*mpty = 0;
*number = "";
*toa = 0;
if(mark_line(str, delimiters, marks) == ITEMS_OF(marks))
{
if( sscanf(marks[0] + 1, "%u", call_idx) == 1
&&
sscanf(marks[1] + 1, "%u", dir) == 1
&&
sscanf(marks[2] + 1, "%u", state) == 1
&&
sscanf(marks[3] + 1, "%u", mode) == 1
&&
sscanf(marks[4] + 1, "%u", mpty) == 1
&&
sscanf(marks[6] + 1, "%u", toa) == 1)
{
marks[5]++;
if(marks[5][0] == '"')
marks[5]++;
if(marks[6][-1] == '"')
marks[6]--;
*number = marks[5];
marks[6][0] = 0;
return 0;
}
}
return -1;
}
#/* */
EXPORT_DEF int at_parse_ccwa(char* str, unsigned * class)
{
/*
* CCWA may be in form:
* in response of AT+CCWA=?
* +CCWA: (0,1)
* in response of AT+CCWA=?
* +CCWA: <n>
* in response of "AT+CCWA=[<n>[,<mode>[,<class>]]]"
* +CCWA: <status>,<class1>
*
* unsolicited result code
* +CCWA: <number>,<type>,<class>,[<alpha>][,<CLI validity>[,<subaddr>,<satype>[,<priority>]]]
*/
char delimiters[] = ":,,";
char * marks[STRLEN(delimiters)];
/* parse URC only here */
if(mark_line(str, delimiters, marks) == ITEMS_OF(marks))
{
if(sscanf(marks[2] + 1, "%u", class) == 1)
return 0;
}
return -1;
}