-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathat_interpreter.c
More file actions
5659 lines (5148 loc) · 163 KB
/
at_interpreter.c
File metadata and controls
5659 lines (5148 loc) · 163 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SpanDSP - a series of DSP components for telephony
*
* at_interpreter.c - AT command interpreter to V.251, V.252, V.253, T.31 and the 3GPP specs.
*
* Written by Steve Underwood <steveu@coppice.org>
*
* Special thanks to Lee Howard <faxguy@howardsilvan.com>
* for his great work debugging and polishing this code.
*
* Copyright (C) 2004, 2005, 2006 Steve Underwood
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*! \file */
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#if defined(__sun)
#define __EXTENSIONS__
#endif
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <memory.h>
#include <string.h>
#include <ctype.h>
#if defined(HAVE_STDBOOL_H)
#include <stdbool.h>
#else
#include "spandsp/stdbool.h"
#endif
#include <assert.h>
#include "spandsp/telephony.h"
#include "spandsp/alloc.h"
#include "spandsp/logging.h"
#include "spandsp/queue.h"
#include "spandsp/power_meter.h"
#include "spandsp/complex.h"
#include "spandsp/tone_generate.h"
#include "spandsp/async.h"
#include "spandsp/hdlc.h"
#include "spandsp/fsk.h"
#include "spandsp/super_tone_rx.h"
#include "spandsp/fax_modems.h"
#include "spandsp/at_interpreter.h"
#include "spandsp/private/logging.h"
#include "spandsp/private/at_interpreter.h"
#define MANUFACTURER "www.soft-switch.org"
#define SERIAL_NUMBER "42"
#define GLOBAL_OBJECT_IDENTITY "42"
enum
{
ASCII_RESULT_CODES = 1,
NUMERIC_RESULT_CODES,
NO_RESULT_CODES
};
static at_profile_t profiles[3] =
{
{
#if defined(_MSC_VER) || defined(__sunos) || defined(__solaris) || defined(__sun)
/*.echo =*/ true,
/*.verbose =*/ true,
/*.result_code_format =*/ ASCII_RESULT_CODES,
/*.pulse_dial =*/ false,
/*.double_escape =*/ false,
/*.adaptive_receive =*/ false,
/*.s_regs[100] =*/ {0, 0, 0, '\r', '\n', '\b', 1, 60, 5, 0, 0}
#else
.echo = true,
.verbose = true,
.result_code_format = ASCII_RESULT_CODES,
.pulse_dial = false,
.double_escape = false,
.adaptive_receive = false,
.s_regs[0] = 0,
.s_regs[3] = '\r',
.s_regs[4] = '\n',
.s_regs[5] = '\b',
.s_regs[6] = 1,
.s_regs[7] = 60,
.s_regs[8] = 5,
.s_regs[10] = 0
#endif
}
};
typedef const char *(*at_cmd_service_t)(at_state_t *s, const char *cmd);
static const char *manufacturer = MANUFACTURER;
static const char *model = PACKAGE;
static const char *revision = VERSION;
#define ETX 0x03
#define DLE 0x10
#define SUB 0x1A
static const char *at_response_codes[] =
{
"OK",
"CONNECT",
"RING",
"NO CARRIER",
"ERROR",
"???",
"NO DIALTONE",
"BUSY",
"NO ANSWER",
"+FCERROR",
"+FRH:3"
};
SPAN_DECLARE(const char *) at_call_state_to_str(int state)
{
switch (state)
{
case AT_CALL_EVENT_ALERTING:
return "Alerting";
case AT_CALL_EVENT_CONNECTED:
return "Connected";
case AT_CALL_EVENT_ANSWERED:
return "Answered";
case AT_CALL_EVENT_BUSY:
return "Busy";
case AT_CALL_EVENT_NO_DIALTONE:
return "No dialtone";
case AT_CALL_EVENT_NO_ANSWER:
return "No answer";
case AT_CALL_EVENT_HANGUP:
return "Hangup";
}
/*endswitch*/
return "???";
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(const char *) at_modem_control_to_str(int state)
{
switch (state)
{
case AT_MODEM_CONTROL_CALL:
return "Call";
case AT_MODEM_CONTROL_ANSWER:
return "Answer";
case AT_MODEM_CONTROL_HANGUP:
return "Hangup";
case AT_MODEM_CONTROL_OFFHOOK:
return "Off hook";
case AT_MODEM_CONTROL_ONHOOK:
return "On hook";
case AT_MODEM_CONTROL_DTR:
return "DTR";
case AT_MODEM_CONTROL_RTS:
return "RTS";
case AT_MODEM_CONTROL_CTS:
return "CTS";
case AT_MODEM_CONTROL_CAR:
return "CAR";
case AT_MODEM_CONTROL_RNG:
return "RNG";
case AT_MODEM_CONTROL_DSR:
return "DSR";
case AT_MODEM_CONTROL_SETID:
return "Set ID";
case AT_MODEM_CONTROL_RESTART:
return "Restart";
case AT_MODEM_CONTROL_DTE_TIMEOUT:
return "DTE timeout";
}
/*endswitch*/
return "???";
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_set_at_rx_mode(at_state_t *s, int new_mode)
{
/* The use of a DTE timeout is mode dependent. Set the timeout appropriately in
the modem. */
switch (new_mode)
{
case AT_MODE_HDLC:
case AT_MODE_STUFFED:
at_modem_control(s, s->dte_inactivity_timeout*1000, (void *) (intptr_t) s->dte_inactivity_timeout);
break;
default:
at_modem_control(s, AT_MODEM_CONTROL_DTE_TIMEOUT, NULL);
break;
}
s->at_rx_mode = new_mode;
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_put_response(at_state_t *s, const char *t)
{
uint8_t buf[3];
if (!s) return;
buf[0] = s->p.s_regs[3];
buf[1] = s->p.s_regs[4];
buf[2] = '\0';
if (s->p.result_code_format == ASCII_RESULT_CODES)
s->at_tx_handler(s->at_tx_user_data, buf, 2);
s->at_tx_handler(s->at_tx_user_data, (uint8_t *) t, strlen(t));
s->at_tx_handler(s->at_tx_user_data, buf, 2);
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_put_numeric_response(at_state_t *s, int val)
{
char buf[20];
snprintf(buf, sizeof(buf), "%d", val);
at_put_response(s, buf);
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_put_response_code(at_state_t *s, int code)
{
uint8_t buf[20];
span_log(&s->logging, SPAN_LOG_FLOW, "Sending AT response code %s\n", at_response_codes[code]);
switch (s->p.result_code_format)
{
case ASCII_RESULT_CODES:
at_put_response(s, at_response_codes[code]);
break;
case NUMERIC_RESULT_CODES:
snprintf((char *) buf, sizeof(buf), "%d%c", code, s->p.s_regs[3]);
s->at_tx_handler(s->at_tx_user_data, buf, strlen((char *) buf));
break;
default:
/* No result codes */
break;
}
}
/*- End of function --------------------------------------------------------*/
static int answer_call(at_state_t *s)
{
if (at_modem_control(s, AT_MODEM_CONTROL_ANSWER, NULL) < 0)
return false;
/* Answering should now be in progress. No AT response should be
issued at this point. */
s->do_hangup = false;
return true;
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_call_event(at_state_t *s, int event)
{
span_log(&s->logging, SPAN_LOG_FLOW, "Call event %d received\n", event);
switch (event)
{
case AT_CALL_EVENT_ALERTING:
at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 1);
if (s->display_call_info && !s->call_info_displayed)
at_display_call_info(s);
at_put_response_code(s, AT_RESPONSE_CODE_RING);
if ((++s->rings_indicated) >= s->p.s_regs[0] && s->p.s_regs[0])
{
/* The modem is set to auto-answer now */
answer_call(s);
}
break;
case AT_CALL_EVENT_ANSWERED:
at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 0);
if (s->fclass_mode == 0)
{
/* Normal data modem connection */
at_set_at_rx_mode(s, AT_MODE_CONNECTED);
/* TODO: */
}
else
{
/* FAX modem connection */
at_set_at_rx_mode(s, AT_MODE_DELIVERY);
at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) FAX_MODEM_CED_TONE_TX);
}
break;
case AT_CALL_EVENT_CONNECTED:
span_log(&s->logging, SPAN_LOG_FLOW, "Dial call - connected. FCLASS=%d\n", s->fclass_mode);
at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 0);
if (s->fclass_mode == 0)
{
/* Normal data modem connection */
at_set_at_rx_mode(s, AT_MODE_CONNECTED);
/* TODO: */
}
else
{
if (s->command_dial)
{
at_put_response_code(s, AT_RESPONSE_CODE_OK);
at_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
}
else
{
/* FAX modem connection */
at_set_at_rx_mode(s, AT_MODE_DELIVERY);
if (s->silent_dial)
at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) FAX_MODEM_NOCNG_TONE_TX);
else
at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) FAX_MODEM_CNG_TONE_TX);
s->dte_is_waiting = true;
}
}
break;
case AT_CALL_EVENT_BUSY:
at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
at_put_response_code(s, AT_RESPONSE_CODE_BUSY);
break;
case AT_CALL_EVENT_NO_DIALTONE:
at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
at_put_response_code(s, AT_RESPONSE_CODE_NO_DIALTONE);
break;
case AT_CALL_EVENT_NO_ANSWER:
at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
at_put_response_code(s, AT_RESPONSE_CODE_NO_ANSWER);
break;
case AT_CALL_EVENT_HANGUP:
span_log(&s->logging, SPAN_LOG_FLOW, "Hangup... at_rx_mode %d\n", s->at_rx_mode);
if (s->dte_is_waiting)
{
if (s->ok_is_pending)
{
at_put_response_code(s, AT_RESPONSE_CODE_OK);
s->ok_is_pending = false;
}
else
{
at_put_response_code(s, AT_RESPONSE_CODE_NO_CARRIER);
}
s->dte_is_waiting = false;
at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
}
else if (s->fclass_mode && s->rx_signal_present)
{
s->rx_data[s->rx_data_bytes++] = DLE;
s->rx_data[s->rx_data_bytes++] = ETX;
s->at_tx_handler(s->at_tx_user_data, s->rx_data, s->rx_data_bytes);
s->rx_data_bytes = 0;
}
if (s->at_rx_mode != AT_MODE_OFFHOOK_COMMAND && s->at_rx_mode != AT_MODE_ONHOOK_COMMAND)
at_put_response_code(s, AT_RESPONSE_CODE_NO_CARRIER);
s->rx_signal_present = false;
at_modem_control(s, AT_MODEM_CONTROL_ONHOOK, NULL);
at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 0);
at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
break;
default:
span_log(&s->logging, SPAN_LOG_WARNING, "Invalid call event %d received.\n", event);
break;
}
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_reset_call_info(at_state_t *s)
{
at_call_id_t *call_id;
at_call_id_t *next;
for (call_id = s->call_id; call_id; call_id = next)
{
next = call_id->next;
span_free(call_id);
}
s->call_id = NULL;
s->rings_indicated = 0;
s->call_info_displayed = false;
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_set_call_info(at_state_t *s, char const *id, char const *value)
{
at_call_id_t *new_call_id;
at_call_id_t *call_id;
/* TODO: We should really not merely ignore a failure to allocate */
if ((new_call_id = (at_call_id_t *) span_alloc(sizeof(*new_call_id))) == NULL)
return;
call_id = s->call_id;
/* If these strdups fail its pretty harmless. We just appear to not
have the relevant field. */
new_call_id->id = (id) ? strdup(id) : NULL;
new_call_id->value = (value) ? strdup(value) : NULL;
new_call_id->next = NULL;
if (call_id)
{
while (call_id->next)
call_id = call_id->next;
call_id->next = new_call_id;
}
else
{
s->call_id = new_call_id;
}
}
/*- End of function --------------------------------------------------------*/
SPAN_DECLARE(void) at_display_call_info(at_state_t *s)
{
char buf[132 + 1];
at_call_id_t *call_id = s->call_id;
while (call_id)
{
snprintf(buf,
sizeof(buf),
"%s=%s",
(call_id->id) ? call_id->id : "NULL",
(call_id->value) ? call_id->value : "<NONE>");
at_put_response(s, buf);
call_id = call_id->next;
}
s->call_info_displayed = true;
}
/*- End of function --------------------------------------------------------*/
static int parse_num(const char **s, int max_value)
{
int i;
/* The spec. says no digits is valid, and should be treated as zero. */
i = 0;
while (isdigit((int) **s))
{
i = i*10 + ((**s) - '0');
(*s)++;
}
if (i > max_value)
i = -1;
return i;
}
/*- End of function --------------------------------------------------------*/
static int parse_hex_num(const char **s, int max_value)
{
int i;
/* The spec. says a hex value is always 2 digits, and the alpha digits are
upper case. */
if (isdigit((int) **s))
i = **s - '0';
else if (**s >= 'A' && **s <= 'F')
i = **s - 'A';
else
return -1;
(*s)++;
if (isdigit((int) **s))
i = (i << 4) | (**s - '0');
else if (**s >= 'A' && **s <= 'F')
i = (i << 4) | (**s - 'A');
else
return -1;
(*s)++;
if (i > max_value)
i = -1;
return i;
}
/*- End of function --------------------------------------------------------*/
static int match_element(const char **variant, const char *variants)
{
int i;
size_t len;
char const *s;
char const *t;
s = variants;
for (i = 0; *s; i++)
{
if ((t = strchr(s, ',')))
len = t - s;
else
len = strlen(s);
if (len == (int) strlen(*variant) && memcmp(*variant, s, len) == 0)
{
*variant += len;
return i;
}
s += len;
if (*s == ',')
s++;
}
return -1;
}
/*- End of function --------------------------------------------------------*/
static int parse_out(at_state_t *s, const char **t, int *target, int max_value, const char *prefix, const char *def)
{
char buf[100];
int val;
switch (*(*t)++)
{
case '=':
switch (**t)
{
case '?':
/* Show possible values */
(*t)++;
snprintf(buf, sizeof(buf), "%s%s", (prefix) ? prefix : "", def);
at_put_response(s, buf);
break;
default:
/* Set value */
if ((val = parse_num(t, max_value)) < 0)
return false;
if (target)
*target = val;
break;
}
break;
case '?':
/* Show current value */
val = (target) ? *target : 0;
snprintf(buf, sizeof(buf), "%s%d", (prefix) ? prefix : "", val);
at_put_response(s, buf);
break;
default:
return false;
}
return true;
}
/*- End of function --------------------------------------------------------*/
static int parse_2_out(at_state_t *s, const char **t, int *target1, int max_value1, int *target2, int max_value2, const char *prefix, const char *def)
{
char buf[100];
int val1;
int val2;
switch (*(*t)++)
{
case '=':
switch (**t)
{
case '?':
/* Show possible values */
(*t)++;
snprintf(buf, sizeof(buf), "%s%s", (prefix) ? prefix : "", def);
at_put_response(s, buf);
break;
default:
/* Set value */
if ((val1 = parse_num(t, max_value1)) < 0)
return false;
if (target1)
*target1 = val1;
if (**t == ',')
{
(*t)++;
if ((val2 = parse_num(t, max_value2)) < 0)
return false;
if (target2)
*target2 = val2;
}
break;
}
break;
case '?':
/* Show current value */
val1 = (target1) ? *target1 : 0;
val2 = (target2) ? *target2 : 0;
snprintf(buf, sizeof(buf), "%s%d,%d", (prefix) ? prefix : "", val1, val2);
at_put_response(s, buf);
break;
default:
return false;
}
return true;
}
/*- End of function --------------------------------------------------------*/
static int parse_n_out(at_state_t *s,
const char **t,
int *targets[],
const int max_values[],
int entries,
const char *prefix,
const char *def)
{
char buf[100];
int val;
int len;
int i;
switch (*(*t)++)
{
case '=':
switch (**t)
{
case '?':
/* Show possible values */
(*t)++;
snprintf(buf, sizeof(buf), "%s%s", (prefix) ? prefix : "", def);
at_put_response(s, buf);
break;
default:
/* Set value */
for (i = 0; i < entries; i++)
{
if ((val = parse_num(t, max_values[i])) < 0)
return false;
if (targets[i])
*targets[i] = val;
if (**t != ',')
break;
(*t)++;
}
break;
}
break;
case '?':
/* Show current value */
len = snprintf(buf, sizeof(buf), "%s", (prefix) ? prefix : "");
for (i = 0; i < entries; i++)
{
if (i > 0)
len += snprintf(&buf[len], sizeof(buf) - len, ",");
val = (targets[i]) ? *targets[i] : 0;
len += snprintf(&buf[len], sizeof(buf) - len, "%d", val);
}
at_put_response(s, buf);
break;
default:
return false;
}
return true;
}
/*- End of function --------------------------------------------------------*/
static int parse_hex_out(at_state_t *s, const char **t, int *target, int max_value, const char *prefix, const char *def)
{
char buf[100];
int val;
switch (*(*t)++)
{
case '=':
switch (**t)
{
case '?':
/* Show possible values */
(*t)++;
snprintf(buf, sizeof(buf), "%s%s", (prefix) ? prefix : "", def);
at_put_response(s, buf);
break;
default:
/* Set value */
if ((val = parse_hex_num(t, max_value)) < 0)
return false;
if (target)
*target = val;
break;
}
break;
case '?':
/* Show current value */
val = (target) ? *target : 0;
snprintf(buf, sizeof(buf), "%s%02X", (prefix) ? prefix : "", val);
at_put_response(s, buf);
break;
default:
return false;
}
return true;
}
/*- End of function --------------------------------------------------------*/
static int parse_string_list_out(at_state_t *s, const char **t, int *target, int max_value, const char *prefix, const char *def)
{
char buf[100];
int val;
size_t len;
char *tmp;
switch (*(*t)++)
{
case '=':
switch (**t)
{
case '?':
/* Show possible values */
(*t)++;
snprintf(buf, sizeof(buf), "%s%s", (prefix) ? prefix : "", def);
at_put_response(s, buf);
break;
default:
/* Set value */
if ((val = match_element(t, def)) < 0)
return false;
if (target)
*target = val;
break;
}
break;
case '?':
/* Show current index value from def */
val = (target) ? *target : 0;
while (val-- && (def = strchr(def, ',')))
def++;
if (def)
{
if ((tmp = strchr(def, ',')))
len = tmp - def;
else
len = strlen(def);
snprintf(buf, sizeof(buf), "%s%.*s", (prefix) ? prefix : "", (int) len, def);
}
else
{
buf[0] = '\0';
}
at_put_response(s, buf);
break;
default:
return false;
}
return true;
}
/*- End of function --------------------------------------------------------*/
static int parse_string_out(at_state_t *s, const char **t, char **target, const char *prefix)
{
char buf[100];
switch (*(*t)++)
{
case '=':
switch (**t)
{
case '?':
/* Show possible values */
(*t)++;
snprintf(buf, sizeof(buf), "%s", (prefix) ? prefix : "");
at_put_response(s, buf);
break;
default:
/* Set value */
if (*target)
span_free(*target);
/* If this strdup fails, it should be harmless */
*target = strdup(*t);
break;
}
break;
case '?':
/* Show current index value */
at_put_response(s, (*target) ? *target : "");
break;
default:
return false;
}
while (**t)
(*t)++;
return true;
}
/*- End of function --------------------------------------------------------*/
static const char *s_reg_handler(at_state_t *s, const char *t, int reg)
{
int val;
int b;
char buf[4];
/* Set or get an S register */
switch (*t++)
{
case '=':
switch (*t)
{
case '?':
t++;
snprintf(buf, sizeof(buf), "%3.3d", 0);
at_put_response(s, buf);
break;
default:
if ((val = parse_num(&t, 255)) < 0)
return NULL;
s->p.s_regs[reg] = (uint8_t) val;
break;
}
break;
case '?':
snprintf(buf, sizeof(buf), "%3.3d", s->p.s_regs[reg]);
at_put_response(s, buf);
break;
case '.':
if ((b = parse_num(&t, 7)) < 0)
return NULL;
switch (*t++)
{
case '=':
switch (*t)
{
case '?':
t++;
at_put_numeric_response(s, 0);
break;
default:
if ((val = parse_num(&t, 1)) < 0)
return NULL;
if (val)
s->p.s_regs[reg] |= (1 << b);
else
s->p.s_regs[reg] &= ~(1 << b);
break;
}
break;
case '?':
at_put_numeric_response(s, (int) ((s->p.s_regs[reg] >> b) & 1));
break;
default:
return NULL;
}
break;
default:
return NULL;
}
return t;
}
/*- End of function --------------------------------------------------------*/
static int process_class1_cmd(at_state_t *s, const char **t)
{
int val;
int operation;
int direction;
int result;
const char *allowed;
direction = (*(*t + 2) == 'T');
operation = *(*t + 3);
/* Step past the "+Fxx" */
*t += 4;
switch (operation)
{
case 'S':
allowed = "0-255";
break;
case 'H':
allowed = "3";
break;
default:
allowed = "24,48,72,73,74,96,97,98,121,122,145,146";
break;
}
val = -1;
if (!parse_out(s, t, &val, 255, NULL, allowed))
return true;
if (val < 0)
{
/* It was just a query */
return true;
}
/* All class 1 FAX commands are supposed to give an ERROR response, if the phone
is on-hook. */
if (s->at_rx_mode == AT_MODE_ONHOOK_COMMAND)
return false;
result = true;
if (s->class1_handler)
result = s->class1_handler(s->class1_user_data, direction, operation, val);
switch (result)
{
case 0:
/* Inhibit an immediate response. (These commands should not be part of a multi-command entry.) */
*t = (const char *) -1;
return true;
case -1:
return false;
}
return true;
}
/*- End of function --------------------------------------------------------*/
static const char *at_cmd_dummy(at_state_t *s, const char *t)
{
/* Dummy routine to absorb delimiting characters from a command string */
return t + 1;
}
/*- End of function --------------------------------------------------------*/
static const char *at_cmd_A(at_state_t *s, const char *t)
{
/* V.250 6.3.5 - Answer (abortable) */
if (!answer_call(s))
return NULL;
return (const char *) -1;
}
/*- End of function --------------------------------------------------------*/
static const char *at_cmd_D(at_state_t *s, const char *t)
{
char *u;
char num[100 + 1];
char ch;
/* V.250 6.3.1 - Dial (abortable) */
at_reset_call_info(s);
s->do_hangup = false;
s->silent_dial = false;
s->command_dial = false;
t += 1;
/* There are a numbers of options in a dial command string.
Many are completely irrelevant in this application. */
u = num;
for ( ; (ch = *t); t++)
{
if (isdigit((int) ch))
{
/* V.250 6.3.1.1 Basic digit set */
*u++ = ch;
}
else
{
switch (ch)
{
case 'A':
case 'B':
case 'C':
case 'D':
case '*':
case '#':
/* V.250 6.3.1.1 Full DTMF repertoire */
if (!s->p.pulse_dial)
*u++ = ch;
break;
case ' ':
case '-':
/* Ignore spaces and dashes */
/* This is not a standards based thing. It just improves
compatibility with some other modems. */
break;
case '+':
/* V.250 6.3.1.1 International access code */
case ',':
/* V.250 6.3.1.2 Pause */
/* Pass these through to the application to handle. */
*u++ = ch;
break;
case 'T':
/* V.250 6.3.1.3 Tone dial */
s->p.pulse_dial = false;
break;
case 'P':
/* V.250 6.3.1.4 Pulse dial */
s->p.pulse_dial = true;
break;
case '!':
/* V.250 6.3.1.5 Hook flash, register recall */
/* TODO: */
break;
case 'W':
/* V.250 6.3.1.6 Wait for dial tone */
/* TODO: */
break;
case '@':
/* V.250 6.3.1.7 Wait for quiet answer */
s->silent_dial = true;
break;
case 'S':
/* V.250 6.3.1.8 Invoke stored string */
/* S=<location> */
/* TODO: */
break;
case 'G':
case 'g':
/* GSM07.07 6.2 - Control the CUG supplementary service for this call */
/* Uses index and info values set with command +CCUG. See +CCUG */
/* TODO: */