forked from t4rd15/asterisk-chan-quectel
-
Notifications
You must be signed in to change notification settings - Fork 34
/
cli.c
900 lines (769 loc) · 26.4 KB
/
cli.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
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
bg <[email protected]>
*/
#include "ast_config.h"
#include <asterisk.h>
#include <asterisk/cli.h> /* struct ast_cli_entry; struct ast_cli_args */
#include <asterisk/callerid.h> /* ast_describe_caller_presentation() */
#include "ast_compat.h" /* asterisk compatibility fixes */
#include "cli.h"
#include "chan_quectel.h" /* devices */
#include "helpers.h" /* ITEMS_OF() send_ccwa_set() send_reset() send_sms() send_ussd() */
#include "pdiscovery.h" /* pdiscovery_list_begin() pdiscovery_list_next() pdiscovery_list_end() */
#include "error.h"
static const char * restate2str_msg(restate_time_t when);
static char* complete_device (const char* word, int state)
{
struct pvt* pvt;
char* res = NULL;
int which = 0;
int wordlen = strlen (word);
AST_RWLIST_RDLOCK (&gpublic->devices);
AST_RWLIST_TRAVERSE (&gpublic->devices, pvt, entry)
{
if (!strncasecmp (PVT_ID(pvt), word, wordlen) && ++which > state)
{
res = ast_strdup (PVT_ID(pvt));
break;
}
}
AST_RWLIST_UNLOCK (&gpublic->devices);
return res;
}
static char* cli_show_devices (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
struct pvt* pvt;
#define FORMAT1 "%-12.12s %-5.5s %-10.10s %-4.4s %-4.4s %-7.7s %-14.14s %-10.10s %-17.17s %-16.16s %-16.16s %-14.14s\n"
#define FORMAT2 "%-12.12s %-5d %-10.10s %-4d %-4d %-7d %-14.14s %-10.10s %-17.17s %-16.16s %-16.16s %-14.14s\n"
switch (cmd)
{
case CLI_INIT:
e->command = "quectel show devices";
e->usage = "Usage: quectel show devices\n"
" Shows the state of Quectel devices.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3)
{
return CLI_SHOWUSAGE;
}
ast_cli (a->fd, FORMAT1, "ID", "Group", "State", "RSSI", "Mode", "Submode", "Provider Name", "Model", "Firmware", "IMEI", "IMSI", "Number");
AST_RWLIST_RDLOCK (&gpublic->devices);
AST_RWLIST_TRAVERSE (&gpublic->devices, pvt, entry)
{
ast_mutex_lock (&pvt->lock);
ast_cli (a->fd, FORMAT2,
PVT_ID(pvt),
CONF_SHARED(pvt, group),
pvt_str_state(pvt),
pvt->rssi,
pvt->linkmode,
pvt->linksubmode,
pvt->provider_name,
pvt->model,
pvt->firmware,
pvt->imei,
pvt->imsi,
pvt->subscriber_number
);
ast_mutex_unlock (&pvt->lock);
}
AST_RWLIST_UNLOCK (&gpublic->devices);
#undef FORMAT1
#undef FORMAT2
return CLI_SUCCESS;
}
static char* cli_show_device_settings (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
struct pvt* pvt;
switch (cmd)
{
case CLI_INIT:
e->command = "quectel show device settings";
e->usage = "Usage: quectel show device settings <device>\n"
" Shows the settings of Quectel device.\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 4)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc != 5)
{
return CLI_SHOWUSAGE;
}
pvt = find_device (a->argv[4]);
if (pvt)
{
ast_cli (a->fd, "------------- Settings ------------\n");
ast_cli (a->fd, " Device : %s\n", PVT_ID(pvt));
if (strcmp(CONF_UNIQ(pvt, quec_uac),"1") == 0) ast_cli (a->fd, " Audio UAC : %s\n", CONF_UNIQ(pvt, alsadev));
else ast_cli (a->fd, " Audio : %s\n", CONF_UNIQ(pvt, audio_tty));
ast_cli (a->fd, " Data : %s\n", CONF_UNIQ(pvt, data_tty));
ast_cli (a->fd, " IMEI : %s\n", CONF_UNIQ(pvt, imei));
ast_cli (a->fd, " IMSI : %s\n", CONF_UNIQ(pvt, imsi));
ast_cli (a->fd, " Channel Language : %s\n", CONF_SHARED(pvt, language));
ast_cli (a->fd, " Context : %s\n", CONF_SHARED(pvt, context));
ast_cli (a->fd, " Exten : %s\n", CONF_SHARED(pvt, exten));
ast_cli (a->fd, " Group : %d\n", CONF_SHARED(pvt, group));
ast_cli (a->fd, " RX gain : %d\n", CONF_SHARED(pvt, rxgain));
ast_cli (a->fd, " TX gain : %d\n", CONF_SHARED(pvt, txgain));
ast_cli (a->fd, " U2Diag : %d\n", CONF_SHARED(pvt, u2diag));
ast_cli (a->fd, " Use CallingPres : %s\n", CONF_SHARED(pvt, usecallingpres) ? "Yes" : "No");
ast_cli (a->fd, " Default CallingPres : %s\n", CONF_SHARED(pvt, callingpres) < 0 ? "<Not set>" : ast_describe_caller_presentation (CONF_SHARED(pvt, callingpres)));
ast_cli (a->fd, " Auto delete SMS : %s\n", CONF_SHARED(pvt, autodeletesms) ? "Yes" : "No");
ast_cli (a->fd, " Disable SMS : %s\n", CONF_SHARED(pvt, disablesms) ? "Yes" : "No");
ast_cli (a->fd, " Reset Quectel : %s\n", CONF_SHARED(pvt, resetquectel) ? "Yes" : "No");
ast_cli (a->fd, " Call Waiting : %s\n", dc_cw_setting2str(CONF_SHARED(pvt, callwaiting)));
ast_cli (a->fd, " DTMF : %s\n", dc_dtmf_setting2str(CONF_SHARED(pvt, dtmf)));
ast_cli (a->fd, " Minimal DTMF Gap : %d\n", CONF_SHARED(pvt, mindtmfgap));
ast_cli (a->fd, " Minimal DTMF Duration : %d\n", CONF_SHARED(pvt, mindtmfduration));
ast_cli (a->fd, " Minimal DTMF Interval : %d\n", CONF_SHARED(pvt, mindtmfinterval));
ast_cli (a->fd, " Initial device state : %s\n\n", dev_state2str(CONF_SHARED(pvt, initstate)));
ast_mutex_unlock (&pvt->lock);
}
else
{
ast_cli (a->fd, "Device %s not found\n", a->argv[4]);
}
return CLI_SUCCESS;
}
static char* cli_show_device_state (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
struct pvt* pvt;
struct ast_str * statebuf;
char buf[40];
switch (cmd)
{
case CLI_INIT:
e->command = "quectel show device state";
e->usage = "Usage: quectel show device state <device>\n"
" Shows the state of Quectel device.\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 4)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc != 5)
{
return CLI_SHOWUSAGE;
}
pvt = find_device (a->argv[4]);
if (pvt)
{
statebuf = pvt_str_state_ex(pvt);
ast_cli (a->fd, "-------------- Status -------------\n");
ast_cli (a->fd, " Device : %s\n", PVT_ID(pvt));
ast_cli (a->fd, " State : %s\n", ast_str_buffer(statebuf));
if (strcmp(CONF_UNIQ(pvt, quec_uac),"1") == 0) ast_cli (a->fd, " Audio UAC : %s\n", CONF_UNIQ(pvt, alsadev));
else ast_cli (a->fd, " Audio : %s\n", PVT_STATE(pvt, audio_tty));
ast_cli (a->fd, " Data : %s\n", PVT_STATE(pvt, data_tty));
ast_cli (a->fd, " Voice : %s\n", (pvt->has_voice) ? "Yes" : "No");
ast_cli (a->fd, " SMS : %s\n", (pvt->has_sms) ? "Yes" : "No");
ast_cli (a->fd, " Manufacturer : %s\n", pvt->manufacturer);
ast_cli (a->fd, " Model : %s\n", pvt->model);
ast_cli (a->fd, " Firmware : %s\n", pvt->firmware);
ast_cli (a->fd, " IMEI : %s\n", pvt->imei);
ast_cli (a->fd, " IMSI : %s\n", pvt->imsi);
ast_cli (a->fd, " GSM Registration Status : %s\n", GSM_regstate2str(pvt->gsm_reg_status));
ast_cli (a->fd, " RSSI : %d, %s\n", pvt->rssi, rssi2dBm(pvt->rssi, buf, sizeof(buf)));
ast_cli (a->fd, " Mode : %s\n", sys_mode2str(pvt->linkmode));
ast_cli (a->fd, " Submode : %s\n", sys_submode2str(pvt->linksubmode));
ast_cli (a->fd, " Provider Name : %s\n", pvt->provider_name);
ast_cli (a->fd, " Location area code : %s\n", pvt->location_area_code);
ast_cli (a->fd, " Cell ID : %s\n", pvt->cell_id);
ast_cli (a->fd, " Subscriber Number : %s\n", pvt->subscriber_number);
ast_cli (a->fd, " SMS Service Center : %s\n", pvt->sms_scenter);
ast_cli (a->fd, " Use UCS-2 encoding : %s\n", pvt->use_ucs2_encoding ? "Yes" : "No");
ast_cli (a->fd, " Tasks in queue : %u\n", PVT_STATE(pvt, at_tasks));
ast_cli (a->fd, " Commands in queue : %u\n", PVT_STATE(pvt, at_cmds));
ast_cli (a->fd, " Call Waiting : %s\n", pvt->has_call_waiting ? "Enabled" : "Disabled" );
ast_cli (a->fd, " Current device state : %s\n", dev_state2str(pvt->current_state) );
ast_cli (a->fd, " Desired device state : %s\n", dev_state2str(pvt->desired_state) );
ast_cli (a->fd, " When change state : %s\n", restate2str_msg(pvt->restart_time) );
ast_cli (a->fd, " Calls/Channels : %u\n", PVT_STATE(pvt, chansno));
ast_cli (a->fd, " Active : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_ACTIVE]));
ast_cli (a->fd, " Held : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_ONHOLD]));
ast_cli (a->fd, " Dialing : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_DIALING]));
ast_cli (a->fd, " Alerting : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_ALERTING]));
ast_cli (a->fd, " Incoming : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_INCOMING]));
ast_cli (a->fd, " Waiting : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_WAITING]));
ast_cli (a->fd, " Releasing : %u\n", PVT_STATE(pvt, chan_count[CALL_STATE_RELEASED]));
ast_cli (a->fd, " Initializing : %u\n\n", PVT_STATE(pvt, chan_count[CALL_STATE_INIT]));
/* TODO: show call waiting network setting and local config value */
ast_mutex_unlock (&pvt->lock);
ast_free(statebuf);
}
else
{
ast_cli (a->fd, "Device %s not found\n", a->argv[4]);
}
return CLI_SUCCESS;
}
#/* */
static int32_t getACD(uint32_t calls, uint32_t duration)
{
int32_t acd;
if(calls) {
acd = duration / calls;
} else {
acd = -1;
}
return acd;
}
#/* */
static int32_t getASR(uint32_t total, uint32_t handled)
{
int32_t asr;
if(total) {
asr = handled * 100 / total;
} else {
asr = -1;
}
return asr;
}
static char* cli_show_device_statistics (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
struct pvt * pvt;
switch (cmd)
{
case CLI_INIT:
e->command = "quectel show device statistics";
e->usage = "Usage: quectel show device statistics <device>\n"
" Shows the statistics of Quectel device.\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 4)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc != 5)
{
return CLI_SHOWUSAGE;
}
pvt = find_device (a->argv[4]);
if (pvt)
{
ast_cli (a->fd, "-------------- Statistics -------------\n");
ast_cli (a->fd, " Device : %s\n", PVT_ID(pvt));
ast_cli (a->fd, " Queue tasks : %u\n", PVT_STAT(pvt, at_tasks));
ast_cli (a->fd, " Queue commands : %u\n", PVT_STAT(pvt, at_cmds));
ast_cli (a->fd, " Responses : %u\n", PVT_STAT(pvt, at_responses));
ast_cli (a->fd, " Bytes of read responses : %u\n", PVT_STAT(pvt, d_read_bytes));
ast_cli (a->fd, " Bytes of written commands : %u\n", PVT_STAT(pvt, d_write_bytes));
ast_cli (a->fd, " Bytes of read audio : %llu\n", (unsigned long long int)PVT_STAT(pvt, a_read_bytes));
ast_cli (a->fd, " Bytes of written audio : %llu\n", (unsigned long long int)PVT_STAT(pvt, a_write_bytes));
ast_cli (a->fd, " Readed frames : %u\n", PVT_STAT(pvt, read_frames));
ast_cli (a->fd, " Readed short frames : %u\n", PVT_STAT(pvt, read_sframes));
ast_cli (a->fd, " Wrote frames : %u\n", PVT_STAT(pvt, write_frames));
ast_cli (a->fd, " Wrote short frames : %u\n", PVT_STAT(pvt, write_tframes));
ast_cli (a->fd, " Wrote silence frames : %u\n", PVT_STAT(pvt, write_sframes));
ast_cli (a->fd, " Write buffer overflow bytes : %llu\n", (unsigned long long int)PVT_STAT(pvt, write_rb_overflow_bytes));
ast_cli (a->fd, " Write buffer overflow count : %u\n", PVT_STAT(pvt, write_rb_overflow));
ast_cli (a->fd, " Incoming calls : %u\n", PVT_STAT(pvt, in_calls));
ast_cli (a->fd, " Waiting calls : %u\n", PVT_STAT(pvt, cw_calls));
ast_cli (a->fd, " Handled input calls : %u\n", PVT_STAT(pvt, in_calls_handled));
ast_cli (a->fd, " Fails to PBX run : %u\n", PVT_STAT(pvt, in_pbx_fails));
ast_cli (a->fd, " Attempts to outgoing calls : %u\n", PVT_STAT(pvt, out_calls));
ast_cli (a->fd, " Answered outgoing calls : %u\n", PVT_STAT(pvt, calls_answered[CALL_DIR_OUTGOING]));
ast_cli (a->fd, " Answered incoming calls : %u\n", PVT_STAT(pvt, calls_answered[CALL_DIR_INCOMING]));
ast_cli (a->fd, " Seconds of outgoing calls : %u\n", PVT_STAT(pvt, calls_duration[CALL_DIR_OUTGOING]));
ast_cli (a->fd, " Seconds of incoming calls : %u\n", PVT_STAT(pvt, calls_duration[CALL_DIR_INCOMING]));
ast_cli (a->fd, " ACD for incoming calls : %d\n", getACD(PVT_STAT(pvt, calls_answered[CALL_DIR_INCOMING]), PVT_STAT(pvt, calls_duration[CALL_DIR_INCOMING])));
ast_cli (a->fd, " ACD for outgoing calls : %d\n", getACD(PVT_STAT(pvt, calls_answered[CALL_DIR_OUTGOING]), PVT_STAT(pvt, calls_duration[CALL_DIR_OUTGOING])));
/*
ast_cli (a->fd, " ACD : %d\n",
getACD(
PVT_STAT(pvt, calls_answered[CALL_DIR_OUTGOING])
+ PVT_STAT(pvt, calls_answered[CALL_DIR_INCOMING]),
PVT_STAT(pvt, calls_duration[CALL_DIR_OUTGOING])
+ PVT_STAT(pvt, calls_duration[CALL_DIR_INCOMING])
)
);
*/
ast_cli (a->fd, " ASR for incoming calls : %d\n", getASR(PVT_STAT(pvt, in_calls) + PVT_STAT(pvt, cw_calls), PVT_STAT(pvt, calls_answered[CALL_DIR_INCOMING])) );
ast_cli (a->fd, " ASR for outgoing calls : %d\n\n", getASR(PVT_STAT(pvt, out_calls), PVT_STAT(pvt, calls_answered[CALL_DIR_OUTGOING])));
/*
ast_cli (a->fd, " ASR : %d\n\n",
getASR(
PVT_STAT(pvt, out_calls)
+ PVT_STAT(pvt, in_calls)
+ PVT_STAT(pvt, cw_calls),
PVT_STAT(pvt, calls_answered[CALL_DIR_OUTGOING])
+ PVT_STAT(pvt, calls_answered[CALL_DIR_INCOMING])
)
);
*/
ast_mutex_unlock (&pvt->lock);
}
else
{
ast_cli (a->fd, "Device %s not found\n", a->argv[4]);
}
return CLI_SUCCESS;
}
static char* cli_show_version (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
switch (cmd)
{
case CLI_INIT:
e->command = "quectel show version";
e->usage = "Usage: quectel show version\n"
" Shows the version of module.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3)
{
return CLI_SHOWUSAGE;
}
ast_cli (a->fd, "\n%s: %s, Version %s, Revision %s\nProject Home: %s\nBug Reporting: %s\n\n", AST_MODULE, MODULE_DESCRIPTION, MODULE_VERSION, PACKAGE_REVISION, MODULE_URL, MODULE_BUGREPORT);
return CLI_SUCCESS;
}
static char* cli_cmd (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
const char * msg;
switch (cmd)
{
case CLI_INIT:
e->command = "quectel cmd";
e->usage = "Usage: quectel cmd <device> <command>\n"
" Send <command> to the rfcomm port on the device\n"
" with the specified <device>.\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 2)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc != 4)
{
return CLI_SHOWUSAGE;
}
int res = send_at_command(a->argv[2], a->argv[3]);
ast_cli (a->fd, "[%s] '%s' %s\n", a->argv[2], a->argv[3], res < 0 ? error2str(chan_quectel_err) : "AT command queued");
return CLI_SUCCESS;
}
static char* cli_ussd (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
switch (cmd)
{
case CLI_INIT:
e->command = "quectel ussd";
e->usage =
"Usage: quectel ussd <device> <command>\n"
" Send ussd <command> to the quectel\n"
" with the specified <device>.\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 2)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc != 4)
{
return CLI_SHOWUSAGE;
}
int res = send_ussd(a->argv[2], a->argv[3]);
ast_cli(a->fd, "[%s] %s\n", a->argv[2], res < 0 ? error2str(chan_quectel_err) : "USSD queued for send");
return CLI_SUCCESS;
}
static char* cli_sms (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
struct ast_str * buf;
int i;
switch (cmd)
{
case CLI_INIT:
e->command = "quectel sms";
e->usage =
"Usage: quectel sms <device> <number> <message>\n"
" Send a SMS to <number> with the <message> from <device>\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 2)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc < 5)
{
return CLI_SHOWUSAGE;
}
buf = ast_str_create (160 * 255);
for (i = 4; i < a->argc; i++)
{
if (i < (a->argc - 1))
{
ast_str_append (&buf, 0, "%s ", a->argv[i]);
}
else
{
ast_str_append (&buf, 0, "%s", a->argv[i]);
}
}
int res = send_sms(a->argv[2], a->argv[3], ast_str_buffer(buf), 0, "1", "UNKNOWN", 8);
ast_free (buf);
ast_cli(a->fd, "[%s] %s\n", a->argv[2], res < 0 ? error2str(chan_quectel_err) : "SMS queued for send");
return CLI_SUCCESS;
}
#if ASTERISK_VERSION_NUM >= 10800 /* 1.8+ */
typedef const char * const * ast_cli_complete2_t;
#else /* 1.8- */
typedef char * const * ast_cli_complete2_t;
#endif /* ^1.8- */
static char* cli_ccwa_set (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
static const char * const choices[] = { "enable", "disable", NULL };
call_waiting_t enable;
switch (cmd)
{
case CLI_INIT:
e->command = "quectel callwaiting";
e->usage =
"Usage: quectel callwaiting disable|enable <device>\n"
" Disable/Enable Call-Waiting on <device>\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 2)
{
return ast_cli_complete(a->word, (ast_cli_complete2_t)choices, a->n);
}
if (a->pos == 3)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc < 4)
{
return CLI_SHOWUSAGE;
}
if (strcasecmp("disable", a->argv[2]) == 0)
enable = CALL_WAITING_DISALLOWED;
else if (strcasecmp("enable", a->argv[2]) == 0)
enable = CALL_WAITING_ALLOWED;
else
return CLI_SHOWUSAGE;
int res = send_ccwa_set(a->argv[3], enable);
ast_cli(a->fd, "[%s] %s\n", a->argv[3], res < 0 ? error2str(chan_quectel_err) : "Call-Waiting commands queued for execute");
return CLI_SUCCESS;
}
static char* cli_reset (struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
switch (cmd)
{
case CLI_INIT:
e->command = "quectel reset";
e->usage =
"Usage: quectel reset <device>\n"
" Reset quectel <device>\n";
return NULL;
case CLI_GENERATE:
if (a->pos == 2)
{
return complete_device (a->word, a->n);
}
return NULL;
}
if (a->argc != 3)
{
return CLI_SHOWUSAGE;
}
int res = send_reset(a->argv[2]);
ast_cli(a->fd, "[%s] %s\n", a->argv[2], res < 0 ? error2str(chan_quectel_err) : "Reset command queued for execute");
return CLI_SUCCESS;
}
static const char * const a_choices[] = { "now", "gracefully", "when", NULL };
static const char * const a_choices2[] = { "convenient", NULL };
static const char * restate2str_msg(restate_time_t when)
{
static const char * const choices[] = { "now", "gracefully", "when convenient" };
return enum2str(when, choices, ITEMS_OF(choices));
}
#/* */
static char* cli_restart_event(struct ast_cli_entry* e, int cmd, struct ast_cli_args* a, dev_state_t event)
{
static char * const cmds[] = {
"quectel stop",
"quectel restart",
"quectel remove",
};
static const char * const usage[] = {
"Usage: quectel stop < now | gracefully | when convenient > <device>\n"
" Stop quectel <device>\n",
"Usage: quectel restart < now | gracefully | when convenient > <device>\n"
" Restart quectel <device>\n",
"Usage: quectel remove < now | gracefully | when convenient > <device>\n"
" Remove quectel <device>\n",
};
const char * device = NULL;
int res;
int i;
switch (cmd)
{
case CLI_INIT:
e->command = cmds[event];
e->usage = usage[event];
return NULL;
case CLI_GENERATE:
switch(a->pos)
{
case 2:
return ast_cli_complete(a->word, (ast_cli_complete2_t)a_choices, a->n);
case 3:
if(strcasecmp(a->argv[2], "when") == 0)
return ast_cli_complete(a->word, (ast_cli_complete2_t)a_choices2, a->n);
return complete_device(a->word, a->n);
break;
case 4:
if(strcasecmp(a->argv[2], "when") == 0 && strcasecmp(a->argv[3], "convenient") == 0)
return complete_device(a->word, a->n);
}
return NULL;
}
if(a->argc != 4 && a->argc != 5)
{
return CLI_SHOWUSAGE;
}
for(i = 0; a_choices[i]; i++)
{
if(strcasecmp(a->argv[2], a_choices[i]) == 0)
{
if(i == RESTATE_TIME_CONVENIENT)
{
if(a->argc == 5 && strcasecmp(a->argv[3], a_choices2[0]) == 0)
{
device = a->argv[4];
}
}
else if(a->argc == 4)
{
device = a->argv[3];
}
if(device)
{
res = schedule_restart_event(event, i, device);
ast_cli(a->fd, "[%s] %s\n", device, res < 0 ? error2str(chan_quectel_err) : dev_state2str_msg(event));
return CLI_SUCCESS;
}
break;
}
}
return CLI_SHOWUSAGE;
}
#/* */
static char* cli_stop(struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
return cli_restart_event(e, cmd, a, DEV_STATE_STOPPED);
}
#/* */
static char* cli_restart(struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
return cli_restart_event(e, cmd, a, DEV_STATE_RESTARTED);
}
#/* */
static char * cli_remove(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
return cli_restart_event(e, cmd, a, DEV_STATE_REMOVED);
}
#/* */
static char* cli_start(struct ast_cli_entry* e, int cmd, struct ast_cli_args* a)
{
switch (cmd)
{
case CLI_INIT:
e->command = "quectel start";
e->usage = "Usage: quectel start <device>\n"
" Start quectel <device>\n";
return NULL;
case CLI_GENERATE:
if(a->pos == 2)
return complete_device(a->word, a->n);
return NULL;
}
if (a->argc != 3)
{
return CLI_SHOWUSAGE;
}
int res = schedule_restart_event(DEV_STATE_STARTED, RESTATE_TIME_NOW, a->argv[2]);
ast_cli(a->fd, "[%s] %s\n", a->argv[2], res < 0 ? error2str(chan_quectel_err) : dev_state2str_msg(DEV_STATE_STARTED));
return CLI_SUCCESS;
}
static char * cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
int ok = 0;
int i;
switch (cmd)
{
case CLI_INIT:
e->command = "quectel reload";
e->usage = "Usage: quectel reload < now | gracefully | when convenient >\n"
" Reloads the chan_quectel configuration\n";
return NULL;
case CLI_GENERATE:
switch(a->pos)
{
case 2:
return ast_cli_complete(a->word, (ast_cli_complete2_t)a_choices, a->n);
case 3:
if(strcasecmp(a->argv[2], "when") == 0)
return ast_cli_complete(a->word, (ast_cli_complete2_t)a_choices2, a->n);
}
return NULL;
}
if (a->argc != 3 && a->argc != 4)
{
return CLI_SHOWUSAGE;
}
for(i = 0; a_choices[i]; i++)
{
if(strcasecmp(a->argv[2], a_choices[i]) == 0)
{
if(i == RESTATE_TIME_CONVENIENT)
{
if(a->argc == 4 && strcasecmp(a->argv[3], a_choices2[0]) == 0)
{
ok = 1;
}
}
else if(a->argc == 3)
{
ok = 1;
}
if(ok)
{
pvt_reload(i);
return CLI_SUCCESS;
}
break;
}
}
return CLI_SHOWUSAGE;
}
#/* */
static char * cli_discovery(struct ast_cli_entry * e, int cmd, struct ast_cli_args * a)
{
const struct pdiscovery_cache_item * item;
const struct pdiscovery_result * res;
struct pvt * pvt;
const char * imei;
const char * imsi;
int imeilen;
int imsilen;
switch (cmd) {
case CLI_INIT:
e->command = "quectel discovery";
e->usage = "Usage: quectel discovery\n"
" Discovery devices and create config\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 2) {
return CLI_SHOWUSAGE;
}
AST_RWLIST_RDLOCK(&gpublic->devices);
for(res = pdiscovery_list_begin(&item); res; res = pdiscovery_list_next(&item)) {
AST_RWLIST_TRAVERSE (&gpublic->devices, pvt, entry) {
if(strcmp(PVT_STATE(pvt, data_tty), res->ports.ports[INTERFACE_TYPE_DATA]) == 0) {
break;
}
}
if(pvt) {
/*
ast_cli(a->fd, "; existing device\n");
ast_cli(a->fd, "[%s](defaults)\n", PVT_ID(pvt));
if(CONF_UNIQ(pvt, audio_tty)[0])
ast_cli(a->fd, "audio=%s\n", CONF_UNIQ(pvt, audio_tty));
else
ast_cli(a->fd, ";audio=%s\n", PVT_STATE(pvt, audio_tty));
if(CONF_UNIQ(pvt, data_tty)[0])
ast_cli(a->fd, "data=%s\n", CONF_UNIQ(pvt, data_tty));
else
ast_cli(a->fd, ";data=%s\n", PVT_STATE(pvt, data_tty));
if(CONF_UNIQ(pvt, imei)[0])
ast_cli(a->fd, "imei=%s\n", CONF_UNIQ(pvt, imei));
else
ast_cli(a->fd, ";imei=%s\n", pvt->imei);
if(CONF_UNIQ(pvt, imsi)[0])
ast_cli(a->fd, "imsi=%s\n\n", CONF_UNIQ(pvt, imsi));
else
ast_cli(a->fd, ";imsi=%s\n\n", pvt->imsi);
*/
} else {
imei = S_OR(res->imei, "");
imsi = S_OR(res->imsi, "");
imeilen = strlen(imei);
imsilen = strlen(imsi);
ast_cli(a->fd, "; discovered device\n");
ast_cli(a->fd, "[dc_%s_%s](defaults)\n", imei + imeilen - MIN(imeilen,4), imsi + imsilen - MIN(imsilen,4));
ast_cli(a->fd, ";audio=%s\n", res->ports.ports[INTERFACE_TYPE_VOICE]);
ast_cli(a->fd, ";data=%s\n", res->ports.ports[INTERFACE_TYPE_DATA]);
ast_cli(a->fd, "imei=%s\n", imei);
ast_cli(a->fd, "imsi=%s\n\n", imsi);
}
}
pdiscovery_list_end();
AST_RWLIST_UNLOCK(&gpublic->devices);
return CLI_SUCCESS;
}
static struct ast_cli_entry cli[] = {
AST_CLI_DEFINE (cli_show_devices, "Show Quectel devices state"),
AST_CLI_DEFINE (cli_show_device_settings,"Show Quectel device settings"),
AST_CLI_DEFINE (cli_show_device_state, "Show Quectel device state"),
AST_CLI_DEFINE (cli_show_device_statistics,"Show Quectel device statistics"),
AST_CLI_DEFINE (cli_show_version, "Show module version"),
AST_CLI_DEFINE (cli_cmd, "Send commands to port for debugging"),
AST_CLI_DEFINE (cli_ussd, "Send USSD commands to the quectel"),
AST_CLI_DEFINE (cli_sms, "Send SMS from the quectel"),
AST_CLI_DEFINE (cli_ccwa_set, "Enable/Disable Call-Waiting on the quectel"),
AST_CLI_DEFINE (cli_reset, "Reset quectel now"),
AST_CLI_DEFINE (cli_stop, "Stop quectel"),
AST_CLI_DEFINE (cli_restart, "Restart quectel"),
AST_CLI_DEFINE (cli_remove, "Remove quectel"),
AST_CLI_DEFINE (cli_reload, "Reload quectel"),
AST_CLI_DEFINE (cli_start, "Start quectel"),
AST_CLI_DEFINE (cli_discovery, "Discovery devices and create config"),
};
#/* */
EXPORT_DEF void cli_register()
{
ast_cli_register_multiple (cli, ITEMS_OF(cli));
}
#/* */
EXPORT_DEF void cli_unregister()
{
ast_cli_unregister_multiple (cli, ITEMS_OF(cli));
}
/*
static char * ast_str_truncate2(struct ast_str *buf, ssize_t len)
{
if (len < 0) {
buf->__AST_STR_USED += ((ssize_t) abs(len)) > ((ssize_t) buf->__AST_STR_USED) ? (ssize_t)-buf->__AST_STR_USED : (ssize_t)len;
} else {
buf->__AST_STR_USED = len;
}
buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
return buf->__AST_STR_STR;
}
*/