forked from thomasklingbeil/chan_datacard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchan_datacard.c
4836 lines (4198 loc) · 124 KB
/
chan_datacard.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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
* Copyright (C) 2009 Artem Makhutov
*
* Artem Makhutov <[email protected]>
*
* chan_datacard is based on chan_mobile by Digium
*
* Mark Spencer <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief UMTS Voice Datacard channel driver
*
* \author Artem Makhutov <[email protected]>
* \author Dave Bowerman <[email protected]>
*
* \ingroup channel_drivers
*/
#include <asterisk.h>
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 947 $")
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/termios.h>
#include <signal.h>
#include <asterisk/lock.h>
#include <asterisk/channel.h>
#include <asterisk/config.h>
#include <asterisk/logger.h>
#include <asterisk/module.h>
#include <asterisk/pbx.h>
#include <asterisk/options.h>
#include <asterisk/utils.h>
#include <asterisk/linkedlists.h>
#include <asterisk/cli.h>
#include <asterisk/devicestate.h>
#include <asterisk/causes.h>
#include <asterisk/dsp.h>
#include <asterisk/app.h>
#include <asterisk/manager.h>
#include <asterisk/io.h>
/*! Global jitterbuffer configuration - by default, jb is disabled */
static struct ast_jb_conf default_jbconf = {
.flags = 0,
.max_size = -1,
.resync_threshold = -1,
.impl = ""
};
static struct ast_jb_conf global_jbconf;
#include "char_conv.h"
#define DC_CONFIG "datacard.conf"
#define DEVICE_FRAME_SIZE 320
#define DEVICE_FRAME_FORMAT AST_FORMAT_SLINEAR
#define CHANNEL_FRAME_SIZE 320
static int prefformat = DEVICE_FRAME_FORMAT;
static int discovery_interval = 60; /* The device discovery interval, default 60 seconds. */
static pthread_t discovery_thread = AST_PTHREADT_NULL; /* The discovery thread */
AST_MUTEX_DEFINE_STATIC(unload_mutex);
static int unloading_flag = 0;
static inline int check_unloading();
static inline void set_unloading();
struct msg_queue_entry;
struct dc_pvt {
struct ast_channel *owner; /* Channel we belong to, possibly NULL */
struct ast_frame fr; /* "null" frame */
ast_mutex_t lock; /*!< pvt lock */
/*! queue for messages we are expecting */
AST_LIST_HEAD_NOLOCK(msg_queue, msg_queue_entry) msg_queue;
char id[31]; /* The id from datacard.conf */
int group; /* group number for group dialling */
char context[AST_MAX_CONTEXT]; /* the context for incoming calls */
struct dc_pvt *pvt; /*!< pvt pvt */
char io_buf[CHANNEL_FRAME_SIZE + AST_FRIENDLY_OFFSET];
struct ast_smoother *smoother; /* our smoother, for making 320 byte frames */
char audio_tty_str[256];
char data_tty_str[256];
int audio_socket; /* audio socket descriptor */
int data_socket; /* rfcomm socket descriptor */
pthread_t monitor_thread; /* monitor thread handle */
int timeout; /*!< used to set the timeout for rfcomm data (may be used in the future) */
unsigned int has_sms:1;
unsigned int has_voice:1;
unsigned int do_alignment_detection:1;
unsigned int alignment_detection_triggered:1;
short alignment_samples[4];
int alignment_count;
struct ast_dsp *dsp;
int hangupcause;
int initialized:1; /*!< whether a service level connection exists or not */
int rssi;
int linkmode;
int linksubmode;
int rxgain;
int txgain;
char provider_name[32];
char manufacturer[32];
char model[32];
char firmware[32];
char imei[17];
int sms_storage_position;
unsigned int auto_delete_sms:1;
unsigned int use_ucs2_encoding:1;
/* flags */
unsigned int outgoing:1; /*!< outgoing call */
unsigned int incoming:1; /*!< incoming call */
unsigned int outgoing_sms:1; /*!< outgoing sms */
unsigned int incoming_sms:1; /*!< outgoing sms */
unsigned int needchup:1; /*!< we need to send a chup */
unsigned int needring:1; /*!< we need to send a RING */
unsigned int answered:1; /*!< we sent/recieved an answer */
unsigned int connected:1; /*!< do we have an rfcomm connection to a device */
unsigned int volume_synchronized:1; /*!< we have synchronized the volume */
unsigned int group_last_used:1; /*!< mark the last used device */
AST_LIST_ENTRY(dc_pvt) entry;
};
static AST_RWLIST_HEAD_STATIC(devices, dc_pvt);
static void inline rfcomm_append_buf(char **buf, size_t count, size_t *in_count, char c);
static int rfcomm_read_and_expect_char(int data_socket, char *result, char expected);
static int rfcomm_read_and_append_char(int data_socket, char **buf, size_t count, size_t *in_count, char *result, char expected);
static int rfcomm_read_until_crlf(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_until_ok(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_until_cusd_end(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_sms_prompt(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_result(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_command(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_cmgr(int data_socket, char **buf, size_t count, size_t *in_count);
static int rfcomm_read_cusd(int data_socket, char **buf, size_t count, size_t *in_count);
static int handle_response_ok(struct dc_pvt *pvt, char *buf);
static int handle_response_error(struct dc_pvt *pvt, char *buf);
static int handle_response_clip(struct dc_pvt *pvt, char *buf);
static int handle_response_ring(struct dc_pvt *pvt, char *buf);
static int handle_response_cmti(struct dc_pvt *pvt, char *buf);
static int handle_response_cmgr(struct dc_pvt *pvt, char *buf);
static int handle_response_cusd(struct dc_pvt *pvt, char *buf);
static int handle_response_busy(struct dc_pvt *pvt);
static int handle_response_no_dialtone(struct dc_pvt *pvt, char *buf);
static int handle_response_no_carrier(struct dc_pvt *pvt, char *buf);
static int handle_response_conn(struct dc_pvt *pvt, char *buf);
static int handle_response_orig(struct dc_pvt *pvt, char *buf);
static int handle_response_cssi(struct dc_pvt *pvt, char *buf);
static int handle_response_cssu(struct dc_pvt *pvt, char *buf);
static int handle_response_cpin(struct dc_pvt *pvt, char *buf);
static int handle_response_smmemfull(struct dc_pvt *pvt, char *buf);
static int handle_response_rssi(struct dc_pvt *pvt, char *buf);
static int handle_response_cops(struct dc_pvt *pvt, char *buf);
static int handle_response_mode(struct dc_pvt *pvt, char *buf);
static int handle_response_cgmi(struct dc_pvt *pvt, char *buf);
static int handle_response_cgmm(struct dc_pvt *pvt, char *buf);
static int handle_response_cgmr(struct dc_pvt *pvt, char *buf);
static int handle_response_cgsn(struct dc_pvt *pvt, char *buf);
static int handle_sms_prompt(struct dc_pvt *pvt, char *buf);
/* Manager stuff */
static int dc_manager_show_devices(struct mansession *s, const struct message *m);
static int dc_manager_send_cusd(struct mansession *s, const struct message *m);
static int dc_manager_send_sms(struct mansession *s, const struct message *m);
static char *dc_send_manager_event_new_cusd(struct dc_pvt *pvt, char *message);
static char *dc_send_manager_event_new_sms(struct dc_pvt *pvt, char *from_number, char *message);
static char *manager_show_devices_desc =
"Description: Lists Datacard devices in text format with details on current status.\n"
"\n"
"DatacardShowDevicesComplete.\n"
"Variables:\n"
" ActionID: <id> Action ID for this transaction. Will be returned.\n";
static char *manager_send_cusd_desc =
"Description: Send a cusd message to a datacard.\n"
"\n"
"Variables: (Names marked with * are required)\n"
" ActionID: <id> Action ID for this transaction. Will be returned.\n"
" *Device: <id> The datacard to which the cusd code will be send.\n"
" *CUSD: <code> The cusd code that will be send to the device.\n";
static char *manager_send_sms_desc =
"Description: Send a sms message from a datacard.\n"
"\n"
"Variables: (Names marked with * are required)\n"
" ActionID: <id> Action ID for this transaction. Will be returned.\n"
" *Device: <id> The datacard to which the cusd code will be send.\n"
" *Number: <number> The phone number to which the sms will be send.\n"
" *Message: <message> The sms message that will be send.\n";
/* CLI stuff */
static char *handle_cli_dc_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
static char *handle_cli_dc_rfcomm(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
static char *handle_cli_dc_cusd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
static struct ast_cli_entry dc_cli[] = {
AST_CLI_DEFINE(handle_cli_dc_show_devices, "Show Datacard devices"),
AST_CLI_DEFINE(handle_cli_dc_rfcomm, "Send commands to the rfcomm port for debugging"),
AST_CLI_DEFINE(handle_cli_dc_cusd, "Send CUSD commands to the datacard"),
};
/* App stuff */
static char *app_dcstatus = "DatacardStatus";
static char *dcstatus_synopsis = "DatacardStatus(Device,Variable)";
static char *dcstatus_desc =
"DatacardStatus(Device,Variable)\n"
" Device - Id of device from datacard.conf\n"
" Variable - Variable to store status in will be 1-3.\n"
" In order, Disconnected, Connected & Free, Connected & Busy.\n";
static char *app_dcsendsms = "DatacardSendSMS";
static char *dcsendsms_synopsis = "DatacardSendSMS(Device,Dest,Message)";
static char *dcsendsms_desc =
"DatacardSendSms(Device,Dest,Message)\n"
" Device - Id of device from datacard.conf\n"
" Dest - destination\n"
" Message - text of the message\n";
static struct ast_channel *dc_new(int state, struct dc_pvt *pvt, char *cid_num);
static struct ast_channel *dc_request(const char *type, int format, void *data, int *cause);
static int dc_call(struct ast_channel *ast, char *dest, int timeout);
static int dc_hangup(struct ast_channel *ast);
static int dc_answer(struct ast_channel *ast);
static int dc_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static struct ast_frame *dc_audio_read(struct ast_channel *ast);
static int dc_audio_write(struct ast_channel *ast, struct ast_frame *frame);
static int dc_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static int dc_devicestate(void *data);
static void do_alignment_detection(struct dc_pvt *pvt, char *buf, int buflen);
static int dc_queue_control(struct dc_pvt *pvt, enum ast_control_frame_type control);
static int dc_queue_hangup(struct dc_pvt *pvt);
static int dc_ast_hangup(struct dc_pvt *pvt);
static int dc_data_connect(char *data_tty_str);
static int dc_audio_connect(char *audio_tty_str);
static int dc_get_device_status(int fd);
static int disconnect_datacard(struct dc_pvt *pvt);
static int opentty(char *iface);
static int rfcomm_write(int data_socket, char *buf);
static int rfcomm_write_full(int data_socket, char *buf, size_t count);
static int rfcomm_wait(int data_socket, int *ms);
static ssize_t rfcomm_read(int data_socket, char *buf, size_t count);
static int audio_write(int s, char *buf, int len);
static char *dc_parse_clip(struct dc_pvt *pvt, char *buf);
static char *dc_parse_cops(struct dc_pvt *pvt, char *buf);
static int dc_parse_cmti(struct dc_pvt *pvt, char *buf);
static int dc_parse_cmgr(struct dc_pvt *pvt, char *buf, char **from_number, char **text);
static char *dc_parse_cusd(struct dc_pvt *pvt, char *buf);
static int dc_parse_cpin(struct dc_pvt *pvt, char *buf);
static int dc_parse_rssi(struct dc_pvt *pvt, char *buf);
static int dc_parse_linkmode(struct dc_pvt *pvt, char *buf);
static int dc_parse_linksubmode(struct dc_pvt *pvt, char *buf);
static int dc_send_cpin_test(struct dc_pvt *pvt);
static int dc_send_clip(struct dc_pvt *pvt, int status);
static int dc_send_ddsetex(struct dc_pvt *pvt);
static int dc_send_cvoice_test(struct dc_pvt *pvt);
static int dc_send_cssn(struct dc_pvt *pvt, int cssi, int cssu);
static int dc_send_ate0(struct dc_pvt *pvt);
static int dc_send_atz(struct dc_pvt *pvt);
static int dc_send_dtmf(struct dc_pvt *pvt, char digit);
static int dc_send_cmgf(struct dc_pvt *pvt, int mode);
static int dc_send_cnmi(struct dc_pvt *pvt);
static int dc_send_cmgr(struct dc_pvt *pvt, int index);
static int dc_send_cmgd(struct dc_pvt *pvt, int index);
static int dc_send_cmgs(struct dc_pvt *pvt, char *number);
static int dc_send_sms_text(struct dc_pvt *pvt, char *message);
static int dc_send_chup(struct dc_pvt *pvt);
static int dc_send_atd(struct dc_pvt *pvt, const char *number);
static int dc_send_ata(struct dc_pvt *pvt);
static int dc_send_cusd(struct dc_pvt *pvt, char *code);
static int dc_send_clvl(struct dc_pvt *pvt, int level);
static int dc_send_cops_init(struct dc_pvt *pvt,int mode, int format);
static int dc_send_cops(struct dc_pvt *pvt);
static int dc_send_creg_init(struct dc_pvt *pvt, int level);
static int dc_send_creg(struct dc_pvt *pvt);
static int dc_send_cgmi(struct dc_pvt *pvt);
static int dc_send_cgmm(struct dc_pvt *pvt);
static int dc_send_cgmr(struct dc_pvt *pvt);
static int dc_send_cgsn(struct dc_pvt *pvt);
static int dc_send_cscs(struct dc_pvt *pvt, const char *encoding);
/*
* Hayes AT command helpers
*/
typedef enum {
/* errors */
AT_PARSE_ERROR = -2,
AT_READ_ERROR = -1,
AT_UNKNOWN = 0,
/* at responses */
AT_OK,
AT_ERROR,
AT_RING,
AT_CLIP,
AT_CMTI,
AT_CMGR,
AT_CMGD,
AT_SMS_PROMPT,
AT_CMS_ERROR,
/* at commands */
AT_A,
AT_Z,
AT_D,
AT_E,
AT_DDSETEX,
AT_CVOICE,
AT_CONN,
AT_CEND,
AT_CONF,
AT_ORIG,
AT_SMMEMFULL,
AT_RSSI,
AT_BOOT,
AT_CSSN,
AT_CSSI,
AT_CSSU,
AT_CHUP,
AT_CKPD,
AT_CMGS,
AT_VGM,
AT_VGS,
AT_VTS,
AT_DTMF,
AT_CMGF,
AT_CNMI,
AT_CUSD,
AT_BUSY,
AT_NO_DIALTONE,
AT_NO_CARRIER,
AT_CPIN,
AT_COPS_INIT,
AT_COPS,
AT_CREG_INIT,
AT_CREG,
AT_MODE,
AT_I,
AT_CGMI,
AT_CGMM,
AT_CGMR,
AT_CGSN,
AT_CLVL,
AT_CPMS,
AT_SIMST,
AT_SRVST,
AT_CSCS,
} at_message_t;
static int at_match_prefix(char *buf, char *prefix);
static at_message_t at_read_full(int data_socket, char *buf, size_t count);
static inline const char *at_msg2str(at_message_t msg);
struct msg_queue_entry {
at_message_t expected;
at_message_t response_to;
void *data;
AST_LIST_ENTRY(msg_queue_entry) entry;
};
static int msg_queue_push(struct dc_pvt *pvt, at_message_t expect, at_message_t response_to);
static int msg_queue_push_data(struct dc_pvt *pvt, at_message_t expect, at_message_t response_to, void *data);
static struct msg_queue_entry *msg_queue_pop(struct dc_pvt *pvt);
static void msg_queue_free_and_pop(struct dc_pvt *pvt);
static void msg_queue_flush(struct dc_pvt *pvt);
static struct msg_queue_entry *msg_queue_head(struct dc_pvt *pvt);
/*
* channel stuff
*/
static const struct ast_channel_tech dc_tech = {
.type = "Datacard",
.description = "Datacard Channel Driver",
.capabilities = AST_FORMAT_SLINEAR,
.requester = dc_request,
.call = dc_call,
.hangup = dc_hangup,
.answer = dc_answer,
.send_digit_end = dc_digit_end,
.read = dc_audio_read,
.write = dc_audio_write,
.fixup = dc_fixup,
.devicestate = dc_devicestate
};
/* CLI Commands implementation */
static char *handle_cli_dc_show_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
struct dc_pvt *pvt;
char group[6];
char rssi[6];
char linkmode[6];
char linksubmode[6];
#define FORMAT1 "%-15.15s %-6.6s %-9.9s %-5.5s %-5.5s %-5.5s %-5.5s %-5.5s %-7.7s %-15.15s %-12.12s %-10.10s %-17.17s %-17.17s\n"
switch (cmd) {
case CLI_INIT:
e->command = "datacard show devices";
e->usage =
"Usage: datacard show devices\n"
" Shows the state of Datacard devices.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3)
return CLI_SHOWUSAGE;
ast_cli(a->fd, FORMAT1, "ID", "Group", "Connected", "State", "Voice", "SMS", "RSSI", "Mode", "Submode", "Provider Name", "Manufacturer", "Model", "Firmware", "IMEI");
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
ast_mutex_lock(&pvt->lock);
snprintf(group, sizeof(group), "%d", pvt->group);
snprintf(rssi, sizeof(rssi), "%d", pvt->rssi);
snprintf(linkmode, sizeof(linkmode), "%d", pvt->linkmode);
snprintf(linksubmode, sizeof(linksubmode), "%d", pvt->linksubmode);
ast_cli(a->fd, FORMAT1,
pvt->id,
group,
pvt->connected ? "Yes" : "No",
(!pvt->connected) ? "None" : (pvt->outgoing || pvt->incoming) ? "Busy" : (pvt->outgoing_sms || pvt->incoming_sms) ? "SMS" : "Free",
(pvt->has_voice) ? "Yes" : "No",
(pvt->has_sms) ? "Yes" : "No",
rssi,
linkmode,
linksubmode,
pvt->provider_name,
pvt->manufacturer,
pvt->model,
pvt->firmware,
pvt->imei
);
ast_mutex_unlock(&pvt->lock);
}
AST_RWLIST_UNLOCK(&devices);
#undef FORMAT1
return CLI_SUCCESS;
}
static int dc_manager_show_devices(struct mansession *s, const struct message *m)
{
struct dc_pvt *pvt;
int count = 0;
const char *id = astman_get_header(m, "ActionID");
char idtext[256] = "";
if (!ast_strlen_zero(id))
snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
astman_send_listack(s, m, "Device status list will follow", "start");
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
ast_mutex_lock(&pvt->lock);
astman_append(s,"Event: DatacardDeviceEntry\r\n%s", idtext);
astman_append(s,"Device: %s\r\n", pvt->id);
astman_append(s,"Group: %d\r\n", pvt->group);
astman_append(s,"Connected: %s\r\n", pvt->connected ? "Yes" : "No");
astman_append(s,"State: %s\r\n", (!pvt->connected) ? "None" : (pvt->outgoing || pvt->incoming) ? "Busy" : (pvt->outgoing_sms || pvt->incoming_sms) ? "SMS" : "Free");
astman_append(s,"Voice: %s\r\n", (pvt->has_voice) ? "Yes" : "No");
astman_append(s,"SMS: %s\r\n", (pvt->has_sms) ? "Yes" : "No");
astman_append(s,"RSSI: %d\r\n", pvt->rssi);
astman_append(s,"Mode: %d\r\n", pvt->linkmode);
astman_append(s,"Submode: %d\r\n", pvt->linksubmode);
astman_append(s,"ProviderName: %s\r\n", pvt->provider_name);
astman_append(s,"Manufacturer: %s\r\n", pvt->manufacturer);
astman_append(s,"Model: %s\r\n", pvt->model);
astman_append(s,"Firmware: %s\r\n", pvt->firmware);
astman_append(s,"IMEI: %s\r\n", pvt->imei);
astman_append(s,"\r\n");
count++;
ast_mutex_unlock(&pvt->lock);
}
AST_RWLIST_UNLOCK(&devices);
astman_append(s,
"Event: DatacardShowDevicesComplete\r\n%s"
"EventList: Complete\r\n"
"ListItems: %d\r\n"
"\r\n",
idtext,
count);
return 0;
}
static char *handle_cli_dc_rfcomm(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
char buf[128];
struct dc_pvt *pvt = NULL;
switch (cmd) {
case CLI_INIT:
e->command = "datacard rfcomm";
e->usage =
"Usage: datacard rfcomm <device ID> <command>\n"
" Send <command> to the rfcomm port on the device\n"
" with the specified <device ID>.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 4)
return CLI_SHOWUSAGE;
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (!strcmp(pvt->id, a->argv[2]))
break;
}
AST_RWLIST_UNLOCK(&devices);
if (!pvt) {
ast_cli(a->fd, "Device %s not found.\n", a->argv[2]);
goto e_return;
}
ast_mutex_lock(&pvt->lock);
if (!pvt->connected) {
ast_cli(a->fd, "Device %s not connected.\n", a->argv[2]);
goto e_unlock_pvt;
}
snprintf(buf, sizeof(buf), "%s\r", a->argv[3]);
rfcomm_write(pvt->data_socket, buf);
msg_queue_push(pvt, AT_OK, AT_UNKNOWN);
e_unlock_pvt:
ast_mutex_unlock(&pvt->lock);
e_return:
return CLI_SUCCESS;
}
static char *handle_cli_dc_cusd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
char *cusd_buf = NULL;
struct dc_pvt *pvt = NULL;
switch (cmd) {
case CLI_INIT:
e->command = "datacard cusd";
e->usage =
"Usage: datacard cusd <device ID> <command>\n"
" Send cusd <command> to the datacard\n"
" with the specified <device ID>.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 4)
return CLI_SHOWUSAGE;
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (!strcmp(pvt->id, a->argv[2]))
break;
}
AST_RWLIST_UNLOCK(&devices);
if (!pvt) {
ast_cli(a->fd, "Device %s not found.\n", a->argv[2]);
goto e_return;
}
ast_mutex_lock(&pvt->lock);
if (!pvt->connected) {
ast_cli(a->fd, "Device %s not connected.\n", a->argv[2]);
goto e_unlock_pvt;
}
cusd_buf = ast_strdup(a->argv[3]);
if (dc_send_cusd(pvt, cusd_buf) || msg_queue_push(pvt, AT_OK, AT_CUSD)) {
ast_log(LOG_ERROR, "[%s] problem sending CUSD command.\n", pvt->id);
goto e_unlock_pvt;
}
e_unlock_pvt:
ast_free(cusd_buf);
ast_mutex_unlock(&pvt->lock);
e_return:
return CLI_SUCCESS;
}
static int dc_manager_send_cusd(struct mansession *s, const struct message *m)
{
char *cusd_buf = NULL;
char idtext[256] = "";
struct dc_pvt *pvt = NULL;
const char *id = astman_get_header(m, "ActionID");
const char *device = astman_get_header(m, "Device");
const char *cusd = astman_get_header(m, "CUSD");
if (ast_strlen_zero(device)) {
astman_send_error(s, m, "Device not specified");
return 0;
}
if (ast_strlen_zero(cusd)) {
astman_send_error(s, m, "CUSD not specified");
return 0;
}
if (!ast_strlen_zero(id))
snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (!strcmp(pvt->id, device))
break;
}
AST_RWLIST_UNLOCK(&devices);
if (!pvt) {
char buf[256];
snprintf(buf, sizeof(buf), "Device %s not found.", device);
astman_send_error(s, m, buf);
goto e_return;
}
ast_mutex_lock(&pvt->lock);
if (!pvt->connected) {
char buf[256];
snprintf(buf, sizeof(buf), "Device %s not connected.", device);
astman_send_error(s, m, buf);
goto e_unlock_pvt;
}
cusd_buf = ast_strdup(cusd);
if (dc_send_cusd(pvt, cusd_buf) || msg_queue_push(pvt, AT_OK, AT_CUSD)) {
ast_log(LOG_ERROR, "[%s] problem sending CUSD command.\n", pvt->id);
goto e_unlock_pvt;
}
astman_send_ack(s, m, "CUSD code send successful");
e_unlock_pvt:
ast_free(cusd_buf);
ast_mutex_unlock(&pvt->lock);
e_return:
return 0;
}
static int dc_manager_send_sms(struct mansession *s, const struct message *m)
{
char *number_buf;
char *message_buf;
char idtext[256] = "";
struct dc_pvt *pvt = NULL;
const char *id = astman_get_header(m, "ActionID");
const char *device = astman_get_header(m, "Device");
const char *number = astman_get_header(m, "Number");
const char *message = astman_get_header(m, "Message");
if (ast_strlen_zero(device)) {
astman_send_error(s, m, "Device not specified");
return 0;
}
if (ast_strlen_zero(number)) {
astman_send_error(s, m, "Number not specified");
return 0;
}
if (ast_strlen_zero(message)) {
astman_send_error(s, m, "Message not specified");
return 0;
}
if (!ast_strlen_zero(id))
snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (!strcmp(pvt->id, device))
break;
}
AST_RWLIST_UNLOCK(&devices);
if (!pvt) {
char buf[256];
snprintf(buf, sizeof(buf), "Device %s not found -- SMS will not be sent.", device);
astman_send_error(s, m, buf);
goto e_return;
}
ast_mutex_lock(&pvt->lock);
if (!pvt->connected) {
char buf[256];
snprintf(buf, sizeof(buf), "Device %s not connected -- SMS will not be sent.", device);
astman_send_error(s, m, buf);
goto e_unlock_pvt;
}
if (!pvt->has_sms) {
ast_log(LOG_ERROR,"Device %s doesn't handle SMS -- SMS will not be sent.\n", device);
goto e_unlock_pvt;
}
number_buf = ast_strdup(number);
message_buf = ast_strdup(message);
if (dc_send_cmgs(pvt, number_buf) || msg_queue_push_data(pvt, AT_SMS_PROMPT, AT_CMGS, message_buf)) {
ast_log(LOG_ERROR, "[%s] problem sending SMS message\n", pvt->id);
goto e_free_vars;
}
astman_send_ack(s, m, "SMS send successful");
ast_mutex_unlock(&pvt->lock);
return 0;
e_free_vars:
ast_free(number_buf);
ast_free(message_buf);
e_unlock_pvt:
ast_mutex_unlock(&pvt->lock);
e_return:
return 0;
}
/*
Dialplan applications implementation
*/
static int dc_status_exec(struct ast_channel *ast, void *data)
{
struct dc_pvt *pvt;
char *parse;
int stat;
char status[2];
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(device);
AST_APP_ARG(variable);
);
if (ast_strlen_zero(data))
return -1;
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(args.device) || ast_strlen_zero(args.variable))
return -1;
stat = 1;
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (!strcmp(pvt->id, args.device))
break;
}
AST_RWLIST_UNLOCK(&devices);
if (pvt) {
ast_mutex_lock(&pvt->lock);
if (pvt->connected)
stat = 2;
if (pvt->owner)
stat = 3;
ast_mutex_unlock(&pvt->lock);
}
snprintf(status, sizeof(status), "%d", stat);
pbx_builtin_setvar_helper(ast, args.variable, status);
return 0;
}
static int dc_sendsms_exec(struct ast_channel *ast, void *data)
{
struct dc_pvt *pvt;
char *parse, *message;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(device);
AST_APP_ARG(dest);
AST_APP_ARG(message);
);
if (ast_strlen_zero(data))
return -1;
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(args.device)) {
ast_log(LOG_ERROR,"NULL device for message -- SMS will not be sent.\n");
return -1;
}
if (ast_strlen_zero(args.dest)) {
ast_log(LOG_ERROR,"NULL destination for message -- SMS will not be sent.\n");
return -1;
}
if (ast_strlen_zero(args.message)) {
ast_log(LOG_ERROR,"NULL Message to be sent -- SMS will not be sent.\n");
return -1;
}
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (!strcmp(pvt->id, args.device))
break;
}
AST_RWLIST_UNLOCK(&devices);
if (!pvt) {
ast_log(LOG_ERROR,"Datacard %s wasn't found in the list -- SMS will not be sent.\n", args.device);
goto e_return;
}
ast_mutex_lock(&pvt->lock);
if (!pvt->connected) {
ast_log(LOG_ERROR,"Datacard %s wasn't connected -- SMS will not be sent.\n", args.device);
goto e_unlock_pvt;
}
if (!pvt->has_sms) {
ast_log(LOG_ERROR,"Datacard %s doesn't handle SMS -- SMS will not be sent.\n", args.device);
goto e_unlock_pvt;
}
message = ast_strdup(args.message);
if (dc_send_cmgs(pvt, args.dest)
|| msg_queue_push_data(pvt, AT_SMS_PROMPT, AT_CMGS, message)) {
ast_log(LOG_ERROR, "[%s] problem sending SMS message\n", pvt->id);
goto e_free_message;
}
ast_mutex_unlock(&pvt->lock);
return 0;
e_free_message:
ast_free(message);
e_unlock_pvt:
ast_mutex_unlock(&pvt->lock);
e_return:
return -1;
}
/*
Channel Driver callbacks
*/
static struct ast_channel *dc_new(int state, struct dc_pvt *pvt, char *cid_num)
{
struct ast_channel *chn;
pvt->answered = 0;
pvt->alignment_count = 0;
pvt->alignment_detection_triggered = 0;
ast_smoother_reset(pvt->smoother, DEVICE_FRAME_SIZE);
ast_dsp_digitreset(pvt->dsp);
chn = ast_channel_alloc(1, state, cid_num, pvt->id, 0, 0, pvt->context, 0, "Datacard/%s-%04lx", pvt->id, ast_random() & 0xffff);
if (!chn) {
goto e_return;
}
chn->tech = &dc_tech;
chn->nativeformats = prefformat;
chn->rawreadformat = prefformat;
chn->rawwriteformat = prefformat;
chn->writeformat = prefformat;
chn->readformat = prefformat;
chn->tech_pvt = pvt;
ast_jb_configure(chn, &global_jbconf);
if (state == AST_STATE_RING)
chn->rings = 1;
ast_string_field_set(chn, language, "en");
pvt->owner = chn;
if (pvt->audio_socket != -1) {
ast_channel_set_fd(chn, 0, pvt->audio_socket);
}
return chn;
e_return:
return NULL;
}
static struct ast_channel *dc_request(const char *type, int format, void *data, int *cause)
{
struct ast_channel *chn = NULL;
struct dc_pvt *pvt;
struct dc_pvt *device_list[256];
char *dest_dev = NULL;
char *dest_num = NULL;
int oldformat, group = -1;
int loop_count = 0;
int loop_count2 = 0;
int last_used_device = 0;
int i = 0;
for (i=0;i<256;i++) {
device_list[i] = NULL;
}
if (!data) {
ast_log(LOG_WARNING, "Channel requested with no data\n");
*cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
return NULL;
}
oldformat = format;
format &= (AST_FORMAT_SLINEAR);
if (!format) {
ast_log(LOG_WARNING, "Asked to get a channel of unsupported format '%d'\n", oldformat);
*cause = AST_CAUSE_FACILITY_NOT_IMPLEMENTED;
return NULL;
}
dest_dev = ast_strdupa((char *)data);
dest_num = strchr(dest_dev, '/');
if (dest_num)
*dest_num++ = 0x00;
/* Find requested device and make sure it's connected. */
if (((dest_dev[0] == 'g') || (dest_dev[0] == 'G')) && ((dest_dev[1] >= '0') && (dest_dev[1] <= '9'))) {
group = atoi(&dest_dev[1]);
AST_RWLIST_RDLOCK(&devices);
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (group > -1 && pvt->group == group && pvt->connected && !pvt->owner) {
break;
}
}
AST_RWLIST_UNLOCK(&devices);
}
else if (((dest_dev[0] == 'r') || (dest_dev[0] == 'R')) && ((dest_dev[1] >= '0') && (dest_dev[1] <= '9'))) {
group = atoi(&dest_dev[1]);
AST_RWLIST_RDLOCK(&devices);
/* Generate a list of all availible devices */
AST_RWLIST_TRAVERSE(&devices, pvt, entry) {
if (group > -1 && pvt->group == group) {