forked from wdoekes/asterisk-chan-dongle
-
Notifications
You must be signed in to change notification settings - Fork 8
/
at_response.c
1991 lines (1708 loc) · 50.8 KB
/
at_response.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
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
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
bg <[email protected]>
Copyright (C) 2020 Max von Buelow <[email protected]>
*/
#include "ast_config.h"
#include <asterisk/logger.h> /* ast_debug() */
#include <asterisk/pbx.h> /* ast_pbx_start() */
#include "ast_compat.h" /* asterisk compatibility fixes */
#include "at_response.h"
#include "mutils.h" /* STRLEN() */
#include "at_queue.h"
#include "chan_quectel.h"
#include "at_parse.h"
#include "char_conv.h"
#include "manager.h"
#include "channel.h" /* channel_queue_hangup() channel_queue_control() */
#include "smsdb.h"
#include "error.h"
#define CCWA_STATUS_NOT_ACTIVE 0
#define CCWA_STATUS_ACTIVE 1
#define CLCC_CALL_TYPE_VOICE 0
#define CLCC_CALL_TYPE_DATA 1
#define CLCC_CALL_TYPE_FAX 2
static const at_response_t at_responses_list[] = {
AT_RESPONSES_TABLE(AT_RES_AS_STRUCTLIST)
/* The hackish way to define the duplicated responses in the meantime */
#define DEF_STR(str) str,STRLEN(str)
{ RES_CNUM, "+CNUM",DEF_STR("ERROR+CNUM:") },
{ RES_ERROR,"ERROR",DEF_STR("COMMAND NOT SUPPORT\r") },
#undef DEF_STR
};
EXPORT_DEF const at_responses_t at_responses = { at_responses_list, 2, ITEMS_OF(at_responses_list), RES_MIN, RES_MAX};
/*!
* \brief Get the string representation of the given AT response
* \param res -- the response to process
* \return a string describing the given response
*/
EXPORT_DEF const char* at_res2str (at_res_t res)
{
if((int)res >= at_responses.name_first && (int)res <= at_responses.name_last)
return at_responses.responses[res - at_responses.name_first].name;
return "UNDEFINED";
}
/*!
* \brief Handle OK response
* \param pvt -- pvt structure
* \retval 0 success
* \retval -1 error
*/
static int at_response_ok (struct pvt* pvt, at_res_t res)
{
const at_queue_task_t * task = at_queue_head_task (pvt);
const at_queue_cmd_t * ecmd = at_queue_task_cmd(task);
if(!ecmd)
{
ast_log (LOG_ERROR, "[%s] Received unexpected 'OK'\n", PVT_ID(pvt));
return 0;
}
if(ecmd->res == RES_OK || ecmd->res == RES_CMGR)
{
switch (ecmd->cmd)
{
case CMD_AT:
case CMD_AT_Z:
case CMD_AT_E:
case CMD_AT_U2DIAG:
case CMD_AT_CGMI:
case CMD_AT_CGMM:
case CMD_AT_CGMR:
case CMD_AT_CMEE:
case CMD_AT_CGSN:
case CMD_AT_CIMI:
case CMD_AT_CPIN:
case CMD_AT_CCWA_SET:
case CMD_AT_CCWA_STATUS:
case CMD_AT_CHLD_2:
case CMD_AT_CHLD_3:
case CMD_AT_CSCA:
case CMD_AT_CLCC:
case CMD_AT_CLIR:
ast_debug (3, "[%s] %s sent successfully\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
break;
case CMD_AT_COPS_INIT:
ast_debug (1, "[%s] Operator select parameters set\n", PVT_ID(pvt));
break;
case CMD_AT_CREG_INIT:
ast_debug (1, "[%s] registration info enabled\n", PVT_ID(pvt));
break;
case CMD_AT_CREG:
ast_debug (1, "[%s] registration query sent\n", PVT_ID(pvt));
break;
case CMD_AT_CNUM:
ast_debug (1, "[%s] Subscriber phone number query successed\n", PVT_ID(pvt));
break;
case CMD_AT_CVOICE:
ast_debug (1, "[%s] Quectel has voice support\n", PVT_ID(pvt));
pvt->has_voice = 1;
break;
/*
case CMD_AT_CLIP:
ast_debug (1, "[%s] Calling line indication disabled\n", PVT_ID(pvt));
break;
*/
case CMD_AT_CSSN:
ast_debug (1, "[%s] Supplementary Service Notification enabled successful\n", PVT_ID(pvt));
break;
case CMD_AT_CMGF:
ast_debug (1, "[%s] SMS operation mode set to PDU\n", PVT_ID(pvt));
break;
case CMD_AT_CSCS:
ast_debug (1, "[%s] UCS-2 text encoding enabled\n", PVT_ID(pvt));
pvt->use_ucs2_encoding = 1;
break;
case CMD_AT_CPMS:
ast_debug (1, "[%s] SMS storage location is established\n", PVT_ID(pvt));
break;
case CMD_AT_CNMI:
ast_debug (1, "[%s] SMS new message indication enabled\n", PVT_ID(pvt));
ast_debug (1, "[%s] Quectel has sms support\n", PVT_ID(pvt));
pvt->has_sms = 1;
if (!pvt->initialized)
{
pvt->timeout = DATA_READ_TIMEOUT;
pvt->initialized = 1;
ast_verb (3, "[%s] Quectel initialized and ready\n", PVT_ID(pvt));
manager_event_device_status(PVT_ID(pvt), "Initialize");
}
break;
case CMD_AT_D:
pvt->dialing = 1;
if (task->cpvt != &pvt->sys_chan) {
pvt->last_dialed_cpvt = task->cpvt;
}
/* fall through */
case CMD_AT_A:
case CMD_AT_CHLD_2x:
/* not work, ^CONN: appear before OK for CHLD_ANSWER
task->cpvt->answered = 1;
task->cpvt->needhangup = 1;
*/
CPVT_SET_FLAGS(task->cpvt, CALL_FLAG_NEED_HANGUP);
ast_debug (1, "[%s] %s sent successfully for call id %d\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd), task->cpvt->call_idx);
break;
case CMD_AT_CFUN:
/* in case of reset */
pvt->ring = 0;
pvt->dialing = 0;
pvt->cwaiting = 0;
break;
case CMD_AT_DDSETEX:
ast_debug (1, "[%s] %s sent successfully\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
if (!pvt->initialized)
{
pvt->timeout = DATA_READ_TIMEOUT;
pvt->initialized = 1;
ast_verb (3, "[%s] Quectel initialized and ready\n", PVT_ID(pvt));
manager_event_device_status(PVT_ID(pvt), "Initialize");
}
break;
case CMD_AT_CHUP:
case CMD_AT_CHLD_1x:
CPVT_RESET_FLAGS(task->cpvt, CALL_FLAG_NEED_HANGUP);
ast_debug (1, "[%s] Successful hangup for call idx %d\n", PVT_ID(pvt), task->cpvt->call_idx);
break;
case CMD_AT_CMGS:
ast_debug (1, "[%s] Sending sms message in progress\n", PVT_ID(pvt));
break;
case CMD_AT_SMSTEXT:
pvt->outgoing_sms = 0;
pvt_try_restate(pvt);
/* TODO: move to +CMGS: handler */
ast_verb (3, "[%s] Successfully sent SMS message %p\n", PVT_ID(pvt), task);
ast_log (LOG_NOTICE, "[%s] Successfully sent SMS message %p\n", PVT_ID(pvt), task);
break;
case CMD_AT_DTMF:
ast_debug (1, "[%s] DTMF sent successfully for call idx %d\n", PVT_ID(pvt), task->cpvt->call_idx);
break;
case CMD_AT_CUSD:
ast_verb (3, "[%s] Successfully sent USSD %p\n", PVT_ID(pvt), task);
ast_log (LOG_NOTICE, "[%s] Successfully sent USSD %p\n", PVT_ID(pvt), task);
break;
case CMD_AT_COPS:
ast_debug (1, "[%s] Provider query successfully\n", PVT_ID(pvt));
break;
case CMD_AT_CMGR:
ast_debug (1, "[%s] SMS message see later\n", PVT_ID(pvt));
at_retrieve_next_sms(&pvt->sys_chan, at_cmd_suppress_error_mode(ecmd->flags));
break;
case CMD_AT_CMGD:
ast_debug (1, "[%s] SMS message deleted successfully\n", PVT_ID(pvt));
break;
case CMD_AT_CSQ:
ast_debug (1, "[%s] Got signal strength result\n", PVT_ID(pvt));
break;
case CMD_AT_CLVL:
pvt->volume_sync_step++;
if(pvt->volume_sync_step == VOLUME_SYNC_DONE)
{
ast_debug (1, "[%s] Volume level synchronized\n", PVT_ID(pvt));
pvt->volume_sync_step = VOLUME_SYNC_BEGIN;
}
break;
case CMD_USER:
break;
default:
ast_log (LOG_ERROR, "[%s] Received 'OK' for unhandled command '%s'\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
break;
}
at_queue_handle_result (pvt, res);
}
else
{
ast_log (LOG_ERROR, "[%s] Received 'OK' when expecting '%s', ignoring\n", PVT_ID(pvt), at_res2str (ecmd->res));
}
return 0;
}
static void log_cmd_response_error(const struct pvt* pvt, const at_queue_cmd_t *ecmd, const char *fmt, ...)
{
va_list ap;
char tempbuff[512];
if (at_cmd_suppress_error_mode(ecmd->flags) == SUPPRESS_ERROR_ENABLED) {
if (DEBUG_ATLEAST(1)) {
ast_log(AST_LOG_DEBUG, "[%s] Command response error suppressed:\n", PVT_ID(pvt));
va_start(ap, fmt);
vsnprintf(tempbuff, 512, fmt, ap);
va_end(ap);
ast_log(AST_LOG_DEBUG, "%s", tempbuff);
}
return;
}
va_start(ap, fmt);
vsnprintf(tempbuff, 512, fmt, ap);
ast_log(LOG_ERROR, "%s", tempbuff);
va_end(ap);
}
/*!
* \brief Handle ERROR response
* \param pvt -- pvt structure
* \retval 0 success
* \retval -1 error
*/
static int at_response_error (struct pvt* pvt, at_res_t res)
{
const at_queue_task_t * task = at_queue_head_task(pvt);
const at_queue_cmd_t * ecmd = at_queue_task_cmd(task);
if (ecmd && (ecmd->res == RES_OK || ecmd->res == RES_CMGR || ecmd->res == RES_SMS_PROMPT))
{
switch (ecmd->cmd)
{
/* critical errors */
case CMD_AT:
case CMD_AT_Z:
case CMD_AT_E:
case CMD_AT_CLCC:
log_cmd_response_error(pvt, ecmd, "[%s] Command '%s' failed\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
/* mean disconnected from device */
goto e_return;
/* not critical errors */
case CMD_AT_U2DIAG:
case CMD_AT_CCWA_SET:
case CMD_AT_CCWA_STATUS:
case CMD_AT_CNUM:
log_cmd_response_error(pvt, ecmd, "[%s] Command '%s' failed\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
/* mean ignore error */
break;
case CMD_AT_CGMI:
log_cmd_response_error(pvt, ecmd, "[%s] Getting manufacturer info failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CGMM:
log_cmd_response_error(pvt, ecmd, "[%s] Getting model info failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CGMR:
log_cmd_response_error(pvt, ecmd, "[%s] Getting firmware info failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CMEE:
log_cmd_response_error(pvt, ecmd, "[%s] Setting error verbosity level failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CGSN:
log_cmd_response_error(pvt, ecmd, "[%s] Getting IMEI number failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CIMI:
log_cmd_response_error(pvt, ecmd, "[%s] Getting IMSI number failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CPIN:
log_cmd_response_error(pvt, ecmd, "[%s] Error checking PIN state\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_COPS_INIT:
log_cmd_response_error(pvt, ecmd, "[%s] Error setting operator select parameters\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CREG_INIT:
log_cmd_response_error(pvt, ecmd, "[%s] Error enabling registration info\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CREG:
ast_debug (1, "[%s] Error getting registration info\n", PVT_ID(pvt));
break;
case CMD_AT_CVOICE:
ast_debug (1, "[%s] Quectel has NO voice support\n", PVT_ID(pvt));
ast_log (LOG_WARNING, "[%s] Quectel has NO voice support\n", PVT_ID(pvt));
pvt->has_voice = 0;
if (!pvt->initialized)
{
/* continue initialization in other job at cmd CMD_AT_CMGF */
if (at_enqueue_initialization(task->cpvt, CMD_AT_CMGF))
{
log_cmd_response_error(pvt, ecmd, "[%s] Error schedule initialization commands\n", PVT_ID(pvt));
goto e_return;
}
}
break;
/*
case CMD_AT_CLIP:
log_cmd_response_error(pvt, ecmd, "[%s] Error enabling calling line indication\n", PVT_ID(pvt));
goto e_return;
*/
case CMD_AT_CSSN:
log_cmd_response_error(pvt, ecmd, "[%s] Error Supplementary Service Notification activation failed\n", PVT_ID(pvt));
goto e_return;
case CMD_AT_CMGF:
case CMD_AT_CPMS:
case CMD_AT_CNMI:
ast_debug (1, "[%s] Command '%s' failed\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
ast_debug (1, "[%s] No SMS support\n", PVT_ID(pvt));
pvt->has_sms = 0;
if (!pvt->initialized)
{
if (pvt->has_voice)
{
/* continue initialization in other job at cmd CMD_AT_CSQ */
if (at_enqueue_initialization(task->cpvt, CMD_AT_CSQ))
{
log_cmd_response_error(pvt, ecmd, "[%s] Error querying signal strength\n", PVT_ID(pvt));
goto e_return;
}
pvt->timeout = DATA_READ_TIMEOUT;
pvt->initialized = 1;
/* FIXME: say 'initialized and ready' but disconnect */
// ast_verb (3, "[%s] Quectel initialized and ready\n", PVT_ID(pvt));
}
goto e_return;
}
break;
case CMD_AT_CSCS:
ast_debug (1, "[%s] No UCS-2 encoding support\n", PVT_ID(pvt));
pvt->use_ucs2_encoding = 0;
break;
case CMD_AT_A:
case CMD_AT_CHLD_2x:
log_cmd_response_error(pvt, ecmd, "[%s] Answer failed for call idx %d\n", PVT_ID(pvt), task->cpvt->call_idx);
queue_hangup (task->cpvt->channel, 0);
break;
case CMD_AT_CHLD_3:
log_cmd_response_error(pvt, ecmd, "[%s] Can't begin conference call idx %d\n", PVT_ID(pvt), task->cpvt->call_idx);
queue_hangup(task->cpvt->channel, 0);
break;
case CMD_AT_CLIR:
log_cmd_response_error(pvt, ecmd, "[%s] Setting CLIR failed\n", PVT_ID(pvt));
break;
case CMD_AT_CHLD_2:
if (!CPVT_TEST_FLAG(task->cpvt, CALL_FLAG_HOLD_OTHER) ||
task->cpvt->state != CALL_STATE_INIT) {
break;
}
/* fall through */
case CMD_AT_D:
log_cmd_response_error(pvt, ecmd, "[%s] Dial failed\n", PVT_ID(pvt));
queue_control_channel (task->cpvt, AST_CONTROL_CONGESTION);
break;
case CMD_AT_DDSETEX:
log_cmd_response_error(pvt, ecmd, "[%s] AT^DDSETEX failed\n", PVT_ID(pvt));
break;
case CMD_AT_CHUP:
case CMD_AT_CHLD_1x:
log_cmd_response_error(pvt, ecmd, "[%s] Error sending hangup for call idx %d\n", PVT_ID(pvt), task->cpvt->call_idx);
break;
case CMD_AT_CMGR:
log_cmd_response_error(pvt, ecmd, "[%s] Error reading SMS message\n", PVT_ID(pvt));
at_retrieve_next_sms(&pvt->sys_chan, at_cmd_suppress_error_mode(ecmd->flags));
break;
case CMD_AT_CMGD:
log_cmd_response_error(pvt, ecmd, "[%s] Error deleting SMS message\n", PVT_ID(pvt));
break;
case CMD_AT_CMGS:
case CMD_AT_SMSTEXT:
pvt->outgoing_sms = 0;
pvt_try_restate(pvt);
{
char payload[SMSDB_PAYLOAD_MAX_LEN];
char dst[SMSDB_DST_MAX_LEN];
ssize_t payload_len = smsdb_outgoing_clear(task->uid, dst, payload);
if (payload_len >= 0) {
ast_verb (3, "[%s] Error payload: %.*s\n", PVT_ID(pvt), (int) payload_len, payload);
channel_var_t vars[] =
{
{ "SMS_REPORT_PAYLOAD", payload },
{ "SMS_REPORT_TS", "" },
{ "SMS_REPORT_DT", "" },
{ "SMS_REPORT_SUCCESS", "0" },
{ "SMS_REPORT_TYPE", "i" },
{ "SMS_REPORT", "" },
{ NULL, NULL },
};
start_local_channel(pvt, "report", dst, vars);
manager_event_report(PVT_ID(pvt), payload, payload_len, "", "", 0, 0, "");
}
}
ast_verb (3, "[%s] Error sending SMS message %p\n", PVT_ID(pvt), task);
log_cmd_response_error(pvt, ecmd, "[%s] Error sending SMS message %p %s\n", PVT_ID(pvt), task, at_cmd2str (ecmd->cmd));
break;
case CMD_AT_DTMF:
log_cmd_response_error(pvt, ecmd, "[%s] Error sending DTMF\n", PVT_ID(pvt));
break;
case CMD_AT_COPS:
ast_debug (1, "[%s] Could not get provider name\n", PVT_ID(pvt));
break;
case CMD_AT_CLVL:
ast_debug (1, "[%s] Audio level synchronization failed at step %d/%d\n", PVT_ID(pvt), pvt->volume_sync_step, VOLUME_SYNC_DONE-1);
pvt->volume_sync_step = VOLUME_SYNC_BEGIN;
break;
case CMD_AT_CUSD:
ast_verb (3, "[%s] Error sending USSD %p\n", PVT_ID(pvt), task);
log_cmd_response_error(pvt, ecmd, "[%s] Error sending USSD %p\n", PVT_ID(pvt), task);
break;
default:
log_cmd_response_error(pvt, ecmd, "[%s] Received 'ERROR' for unhandled command '%s'\n", PVT_ID(pvt), at_cmd2str (ecmd->cmd));
break;
}
at_queue_handle_result (pvt, res);
}
else if (ecmd)
{
switch (ecmd->cmd) {
case CMD_AT_CMGR:
at_retrieve_next_sms(&pvt->sys_chan, at_cmd_suppress_error_mode(ecmd->flags));
break;
default:
log_cmd_response_error(pvt, ecmd, "[%s] Received 'ERROR' when expecting '%s', ignoring\n", PVT_ID(pvt), at_res2str (ecmd->res));
break;
}
}
else
{
log_cmd_response_error(pvt, ecmd, "[%s] Received unexpected 'ERROR'\n", PVT_ID(pvt));
}
return 0;
e_return:
at_queue_handle_result (pvt, res);
return -1;
}
/*!
* \brief Handle ^RSSI response Here we get the signal strength.
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \param len -- string lenght
* \retval 0 success
* \retval -1 error
*/
static int at_response_rssi (struct pvt* pvt, const char* str)
{
int rssi = at_parse_rssi (str);
if (rssi == -1)
{
ast_debug (2, "[%s] Error parsing RSSI event '%s'\n", PVT_ID(pvt), str);
return -1;
}
pvt->rssi = rssi;
return 0;
}
/*!
* \brief Handle ^MODE response Here we get the link mode (GSM, UMTS, EDGE...).
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \param len -- string lenght
* \retval 0 success
* \retval -1 error
*/
static int at_response_mode (struct pvt* pvt, char* str, size_t len)
{
int mode;
int submode;
int rv = at_parse_mode (str, &mode, &submode);
if(rv)
{
ast_debug (2, "[%s] Error parsing MODE event '%.*s'\n", PVT_ID(pvt), (int) len, str);
}
else
{
pvt->linkmode = mode;
pvt->linksubmode = submode;
}
return rv;
}
static void request_clcc(struct pvt* pvt)
{
if (at_enqueue_clcc(&pvt->sys_chan))
{
ast_log(LOG_ERROR, "[%s] Error enqueue List Current Calls request\n", PVT_ID(pvt));
}
}
/*!
* \brief Handle ^ORIG response
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \retval 0 success
* \retval -1 error
*/
static int at_response_orig (struct pvt* pvt, const char* str)
{
int call_index;
int call_type;
struct cpvt * cpvt = pvt->last_dialed_cpvt;
pvt->last_dialed_cpvt = NULL;
if(!cpvt)
{
ast_log (LOG_ERROR, "[%s] ^ORIG '%s' for unknown ATD\n", PVT_ID(pvt), str);
return 0;
}
/*
* parse ORIG info in the following format:
* ^ORIG:<call_index>,<call_type>
*/
if (sscanf (str, "^ORIG:%d,%d", &call_index, &call_type) != 2)
{
ast_log (LOG_ERROR, "[%s] Error parsing ORIG event '%s'\n", PVT_ID(pvt), str);
return -1;
}
ast_debug (1, "[%s] ORIG Received call_index: %d call_type %d\n", PVT_ID(pvt), call_index, call_type);
if (call_type == CLCC_CALL_TYPE_VOICE)
{
if(call_index >= MIN_CALL_IDX && call_index <= MAX_CALL_IDX)
{
/* set REAL call idx */
/* WARNING if direction mismatch
cpvt->dir = CALL_DIR_OUTGOING;
*/
cpvt->call_idx = call_index;
change_channel_state(cpvt, CALL_STATE_DIALING, 0);
/* TODO: move to CONN ? */
if(pvt->volume_sync_step == VOLUME_SYNC_BEGIN)
{
pvt->volume_sync_step = VOLUME_SYNC_BEGIN;
if (at_enqueue_volsync(cpvt))
{
ast_log (LOG_ERROR, "[%s] Error synchronize audio level\n", PVT_ID(pvt));
}
else
pvt->volume_sync_step++;
}
request_clcc(pvt);
}
}
else
{
/* FIXME: and reset call if no-voice, bad call_index !
*/
ast_log (LOG_ERROR, "[%s] ORIG event for non-voice call type '%d' index %d\n", PVT_ID(pvt), call_type, call_index);
}
return 0;
}
#if 0
/*!
* \brief Handle ^CONF response
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \retval 0 success
* \retval -1 error
*/
static int at_response_conf (struct pvt* pvt, const char* str)
{
int call_index;
struct cpvt * cpvt;
/*
* parse CONF info in the following format:
* ^CONF: <call_index>
*/
if (sscanf (str, "^CONF:%d", &call_index) != 1)
{
ast_log (LOG_ERROR, "[%s] Error parsing CONF event '%s'\n", PVT_ID(pvt), str);
return -1;
}
ast_debug (1, "[%s] CONF Received call_index %d\n", PVT_ID(pvt), call_index);
cpvt = pvt_find_cpvt(pvt, call_index);
if(cpvt)
{
channel_change_state(cpvt, CALL_STATE_ALERTING, 0);
}
return 0;
}
#endif /* 0 */
/*!
* \brief Handle ^CEND response
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \param len -- string lenght
* \retval 0 success
* \retval -1 error
*/
static int at_response_cend (struct pvt * pvt, const char* str)
{
int call_index = 0;
int duration = 0;
int end_status = 0;
int cc_cause = 0;
struct cpvt * cpvt;
request_clcc(pvt);
/*
* parse CEND info in the following format:
* ^CEND:<call_index>,<duration>,<end_status>[,<cc_cause>]
*/
if (sscanf (str, "^CEND:%d,%d,%d,%d", &call_index, &duration, &end_status, &cc_cause) != 4)
{
ast_debug (1, "[%s] Could not parse all CEND parameters\n", PVT_ID(pvt));
}
ast_debug (1, "[%s] CEND: call_index %d duration %d end_status %d cc_cause %d Line disconnected\n"
, PVT_ID(pvt), call_index, duration, end_status, cc_cause);
cpvt = pvt_find_cpvt(pvt, call_index);
if (cpvt)
{
CPVT_RESET_FLAGS(cpvt, CALL_FLAG_NEED_HANGUP);
PVT_STAT(pvt, calls_duration[cpvt->dir]) += duration;
change_channel_state(cpvt, CALL_STATE_RELEASED, cc_cause);
manager_event_cend(PVT_ID(pvt), call_index, duration, end_status, cc_cause);
}
else
{
// ast_log (LOG_ERROR, "[%s] CEND event for unknown call idx '%d'\n", PVT_ID(pvt), call_index);
}
return 0;
}
/*!
* \brief Handle +CSCA response
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \param len -- string lenght
* \retval 0 success
* \retval -1 error
*/
static int at_response_csca (struct pvt* pvt, char* str)
{
char * csca;
if(at_parse_csca(str, &csca))
{
ast_debug (1, "[%s] Could not parse CSCA response '%s'\n", PVT_ID(pvt), str);
return -1;
}
ast_copy_string (pvt->sms_scenter, csca, sizeof (pvt->sms_scenter));
ast_debug (1, "[%s] CSCA: %s\n", PVT_ID(pvt), pvt->sms_scenter);
return 0;
}
/*!
* \brief Handle ^CONN response
* \param pvt -- pvt structure
* \retval 0 success
* \retval -1 error
*/
static int at_response_conn (struct pvt* pvt, const char* str)
{
int call_index;
int call_type;
struct cpvt * cpvt;
pvt->ring = 0;
pvt->dialing = 0;
pvt->cwaiting = 0;
request_clcc(pvt);
/*
* parse CONN info in the following format:
* ^CONN:<call_index>,<call_type>
*/
if (sscanf (str, "^CONN:%d,%d", &call_index, &call_type) != 2)
{
ast_log (LOG_ERROR, "[%s] Error parsing CONN event '%s'\n", PVT_ID(pvt), str);
return -1;
}
ast_debug (1, "[%s] CONN Received call_index %d call_type %d\n", PVT_ID(pvt), call_index, call_type);
if (call_type == CLCC_CALL_TYPE_VOICE)
{
cpvt = pvt_find_cpvt(pvt, call_index);
if(cpvt)
{
/* FIXME: delay until CLCC handle?
*/
PVT_STAT(pvt, calls_answered[cpvt->dir]) ++;
change_channel_state(cpvt, CALL_STATE_ACTIVE, 0);
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_CONFERENCE))
at_enqueue_conference(cpvt);
}
else
{
at_enqueue_hangup(&pvt->sys_chan, call_index);
ast_log (LOG_ERROR, "[%s] answered incoming call with not exists call idx %d, hanging up!\n", PVT_ID(pvt), call_index);
}
}
else
ast_log (LOG_ERROR, "[%s] answered not voice incoming call type '%d' idx %d, skipped\n", PVT_ID(pvt), call_type, call_index);
return 0;
}
static int start_pbx(struct pvt* pvt, const char * number, int call_idx, call_state_t state)
{
struct cpvt* cpvt;
/* TODO: pass also Subscriber number or other DID info for exten */
#if ASTERISK_VERSION_NUM >= 120000 /* 12+ */
struct ast_channel * channel = new_channel(
pvt, AST_STATE_RING, number, call_idx, CALL_DIR_INCOMING, state,
pvt->has_subscriber_number ? pvt->subscriber_number : CONF_SHARED(pvt, exten),
NULL, NULL);
#else /* 12- */
struct ast_channel * channel = new_channel(
pvt, AST_STATE_RING, number, call_idx, CALL_DIR_INCOMING, state,
pvt->has_subscriber_number ? pvt->subscriber_number : CONF_SHARED(pvt, exten),
NULL);
#endif /* ^12- */
if (!channel)
{
ast_log (LOG_ERROR, "[%s] Unable to allocate channel for incoming call\n", PVT_ID(pvt));
if (at_enqueue_hangup(&pvt->sys_chan, call_idx))
{
ast_log (LOG_ERROR, "[%s] Error sending AT+CHUP command\n", PVT_ID(pvt));
}
return -1;
}
cpvt = ast_channel_tech_pvt(channel);
// FIXME: not execute if channel_new() failed
CPVT_SET_FLAGS(cpvt, CALL_FLAG_NEED_HANGUP);
/* ast_pbx_start() usually failed if asterisk.conf minmemfree
* set too low, try drop buffer cache
* sync && echo 3 >/proc/sys/vm/drop_caches
*/
if (ast_pbx_start (channel))
{
ast_channel_tech_pvt_set(channel, NULL);
cpvt_free(cpvt);
ast_hangup (channel);
ast_log (LOG_ERROR, "[%s] Unable to start pbx on incoming call\n", PVT_ID(pvt));
// TODO: count fails and reset incoming when count reach limit ?
return -1;
}
return 0;
}
/*!
* \brief Handle +CLCC response
* \param pvt -- pvt structure
* \param str -- string containing response (null terminated)
* \param len -- string lenght
* \retval 0 success
* \retval -1 error
*/
static int at_response_clcc (struct pvt* pvt, char* str)
{
struct cpvt * cpvt;
unsigned call_idx, dir, state, mode, mpty, type;
unsigned all = 0;
unsigned held = 0;
char * number;
char *p;
if (pvt->initialized)
{
/* I think man is good until he proves the reverse */
AST_LIST_TRAVERSE(&pvt->chans, cpvt, entry)
{
CPVT_RESET_FLAGS(cpvt, CALL_FLAG_ALIVE);
}
for(;;)
{
p = strchr(str, '\r');
if(at_parse_clcc(str, &call_idx, &dir, &state, &mode, &mpty, &number, &type) == 0)
{
ast_debug (3, "[%s] CLCC callidx %u dir %u state %u mode %u mpty %u number %s type %u\n", PVT_ID(pvt), call_idx, dir, state, mode, mpty, number, type);
if(mode == CLCC_CALL_TYPE_VOICE && state <= CALL_STATE_WAITING)
{
cpvt = pvt_find_cpvt(pvt, call_idx);
if(cpvt)
{
/* cpvt alive */
CPVT_SET_FLAGS(cpvt, CALL_FLAG_ALIVE);
if(dir == cpvt->dir)
{
if(mpty)
CPVT_SET_FLAGS(cpvt, CALL_FLAG_MULTIPARTY);
else
CPVT_RESET_FLAGS(cpvt, CALL_FLAG_MULTIPARTY);
if(dir == CALL_DIR_INCOMING && (state == CALL_STATE_INCOMING || state == CALL_STATE_WAITING))
{
if(cpvt->channel)
{
/* FIXME: unprotected channel access */
int rings = ast_channel_rings(cpvt->channel);
rings += pvt->rings;
ast_channel_rings_set(cpvt->channel, rings);
pvt->rings = 0;
}
}
if(state != cpvt->state)
{
change_channel_state(cpvt, state, 0);
}
}
else
{
ast_log (LOG_ERROR, "[%s] CLCC call idx %d direction mismatch %d/%d\n", PVT_ID(pvt), cpvt->call_idx, dir, cpvt->dir);
}
}
else if(dir == CALL_DIR_INCOMING && (state == CALL_STATE_INCOMING || state == CALL_STATE_WAITING))
{
if(state == CALL_STATE_INCOMING)
PVT_STAT(pvt, in_calls) ++;
else
PVT_STAT(pvt, cw_calls) ++;
if(pvt_enabled(pvt))
{
/* TODO: give dialplan level user tool for checking device is voice enabled or not */
if(start_pbx(pvt, number, call_idx, state) == 0)
{
PVT_STAT(pvt, in_calls_handled) ++;
if(!pvt->has_voice)
ast_log (LOG_WARNING, "[%s] pbx started for device not voice capable\n", PVT_ID(pvt));
}
else
PVT_STAT(pvt, in_pbx_fails) ++;
}
}
all++;
switch(state)
{
case CALL_STATE_WAITING:
pvt->cwaiting = 1;
pvt->ring = 0;
pvt->dialing = 0;
break;
case CALL_STATE_ONHOLD:
held++;
break;
case CALL_STATE_DIALING:
case CALL_STATE_ALERTING:
pvt->dialing = 1;
pvt->cwaiting = 0;
pvt->ring = 0;
break;
case CALL_STATE_INCOMING:
pvt->ring = 1;
pvt->dialing = 0;
pvt->cwaiting = 0;
break;
default:;