-
Notifications
You must be signed in to change notification settings - Fork 89
/
chan_dongle.c
1757 lines (1512 loc) · 39.7 KB
/
chan_dongle.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
/*
* chan_dongle
*
* Copyright (C) 2011
* bg <[email protected]>
* http://www.e1550.mobi
*
* chan_dongle is based on chan_datacard by
*
* Artem Makhutov <[email protected]>
* http://www.makhutov.org
*
* Dmitry Vagin <[email protected]>
*
* Copyright (C) 2011
* bg <[email protected]>
* http://www.e1550.mobi
* chan_datacard is based on chan_mobile by Digium
* (Mark Spencer <[email protected]>)
*
* 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 Dongle channel driver
*
* \author Artem Makhutov <[email protected]>
* \author Dave Bowerman <[email protected]>
* \author Dmitry Vagin <[email protected]>
* \author bg <[email protected]>
*
* \ingroup channel_drivers
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <asterisk.h>
ASTERISK_FILE_VERSION(__FILE__, "$Rev: " PACKAGE_REVISION " $")
#include <asterisk/stringfields.h> /* AST_DECLARE_STRING_FIELDS for asterisk/manager.h */
#include <asterisk/manager.h>
#include <asterisk/dsp.h>
#include <asterisk/callerid.h>
#include <asterisk/module.h> /* AST_MODULE_LOAD_DECLINE ... */
#include <asterisk/timing.h> /* ast_timer_open() ast_timer_fd() */
#include <sys/stat.h> /* S_IRUSR | S_IRGRP | S_IROTH */
#include <termios.h> /* struct termios tcgetattr() tcsetattr() */
#include <pthread.h> /* pthread_t pthread_kill() pthread_join() */
#include <fcntl.h> /* O_RDWR O_NOCTTY */
#include <signal.h> /* SIGURG */
#include "chan_dongle.h"
#include "at_response.h" /* at_res_t */
#include "at_queue.h" /* struct at_queue_task_cmd at_queue_head_cmd() */
#include "at_command.h" /* at_cmd2str() */
#include "mutils.h" /* ITEMS_OF() */
#include "at_read.h"
#include "cli.h"
#include "app.h"
#include "manager.h"
#include "channel.h" /* channel_queue_hangup() */
#include "dc_config.h" /* dc_uconfig_fill() dc_gconfig_fill() dc_sconfig_fill() */
#include "pdiscovery.h" /* pdiscovery_lookup() pdiscovery_init() pdiscovery_fini() */
EXPORT_DEF const char * const dev_state_strs[4] = { "stop", "restart", "remove", "start" };
EXPORT_DEF public_state_t * gpublic;
static int public_state_init(struct public_state * state);
/*!
* Get status of the dongle. It might happen that the device disappears
* (e.g. due to a USB unplug).
*
* \return 0 if device seems ok, non-0 if it seems not available
*/
static int port_status (int fd)
{
struct termios t;
if (fd < 0)
{
return -1;
}
return tcgetattr (fd, &t);
}
#/* return length of lockname */
static int lock_build(const char * devname, char * buf, unsigned length)
{
const char * basename;
char resolved_path[PATH_MAX];
/* follow symlinks */
if(realpath(devname, resolved_path) != NULL)
devname = resolved_path;
basename = strrchr(devname, '/');
if(basename)
basename++;
else
basename = devname;
/* NOTE: use system system wide lock directory */
return snprintf(buf, length, "/var/lock/LOCK..%s", basename);
}
#/* return 0 on error */
static int lock_create(const char * lockfile)
{
int fd;
int len = 0;
char pidb[21];
fd = open(lockfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH);
if(fd >= 0)
{
/* NOTE: bg: i assume next open reuse same fd - not thread-safe */
len = snprintf(pidb, sizeof(pidb), "%d %d", getpid(), fd);
len = write(fd, pidb, len);
close(fd);
}
return len;
}
#/* return pid of owner, 0 if free */
EXPORT_DEF int lock_try(const char * devname, char ** lockname)
{
int fd;
int len;
int pid = 0;
int assigned;
int fd2;
char name[1024];
char buffer[65];
lock_build(devname, name, sizeof(name));
/* FIXME: rise conditions: some time between lock check and got lock */
fd = open(name, O_RDONLY);
if(fd >= 0)
{
len = read(fd, buffer, sizeof(buffer) - 1);
if(len > 0)
{
buffer[len] = 0;
assigned = sscanf(buffer, "%d %d", &len, &fd2);
if(assigned > 0 && kill(len, 0) == 0)
{
if(len == getpid() && assigned > 1)
{
if(port_status(fd2) == 0)
pid = len;
}
else
pid = len;
}
}
close(fd);
}
if(pid == 0)
{
unlink(name);
lock_create(name);
*lockname = ast_strdup(name);
}
return pid;
}
#/* */
EXPORT_DEF void closetty(int fd, char ** lockfname)
{
close(fd);
/* remove lock */
unlink(*lockfname);
ast_free(*lockfname);
*lockfname = NULL;
}
EXPORT_DEF int opentty (const char* dev, char ** lockfile)
{
int flags;
int pid;
int fd;
struct termios term_attr;
char buf[40];
pid = lock_try(dev, lockfile);
if(pid != 0)
{
ast_log (LOG_WARNING, "%s already used by process %d\n", dev, pid);
return -1;
}
fd = open (dev, O_RDWR | O_NOCTTY);
if (fd < 0)
{
flags = errno;
closetty(fd, lockfile);
snprintf(buf, sizeof(buf), "Open Failed\r\nErrorCode: %d", flags);
manager_event_message_raw("DonglePortFail", dev, buf);
ast_log (LOG_WARNING, "unable to open %s: %s\n", dev, strerror(flags));
return -1;
}
flags = fcntl(fd, F_GETFD);
if(flags == -1 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
{
flags = errno;
closetty(fd, lockfile);
ast_log (LOG_WARNING, "fcntl(F_GETFD/F_SETFD) failed for %s: %s\n", dev, strerror(flags));
return -1;
}
if (tcgetattr (fd, &term_attr) != 0)
{
flags = errno;
closetty(fd, lockfile);
ast_log (LOG_WARNING, "tcgetattr() failed for %s: %s\n", dev, strerror(flags));
return -1;
}
term_attr.c_cflag = B115200 | CS8 | CREAD | CRTSCTS;
term_attr.c_iflag = 0;
term_attr.c_oflag = 0;
term_attr.c_lflag = 0;
term_attr.c_cc[VMIN] = 1;
term_attr.c_cc[VTIME] = 0;
if (tcsetattr (fd, TCSAFLUSH, &term_attr) != 0)
{
ast_log (LOG_WARNING, "tcsetattr(TCSAFLUSH) failed for %s: %s\n", dev, strerror(errno));
}
return fd;
}
#/* phone monitor thread pvt cleanup */
static void disconnect_dongle (struct pvt* pvt)
{
struct cpvt * cpvt, * next;
if (!PVT_NO_CHANS(pvt))
{
ast_debug (1, "[%s] Dongle disconnecting, hanging up channels\n", PVT_ID(pvt));
for(cpvt = pvt->chans.first; cpvt; cpvt = next)
{
next = cpvt->entry.next;
at_hangup_immediality(cpvt);
CPVT_RESET_FLAGS(cpvt, CALL_FLAG_NEED_HANGUP);
change_channel_state(cpvt, CALL_STATE_RELEASED, 0);
}
}
at_queue_flush(pvt);
pvt->last_dialed_cpvt = NULL;
closetty (pvt->audio_fd, &pvt->alock);
closetty (pvt->data_fd, &pvt->dlock);
pvt->data_fd = -1;
pvt->audio_fd = -1;
if(pvt->dsp)
ast_dsp_digitreset(pvt->dsp);
pvt_on_remove_last_channel(pvt);
/* pvt->a_write_rb */
pvt->dtmf_digit = 0;
pvt->rings = 0;
// else
{
/* unaffected in case of restart */
pvt->use_ucs2_encoding = 0;
pvt->cusd_use_7bit_encoding = 0;
pvt->cusd_use_ucs2_decoding = 1;
pvt->gsm_reg_status = -1;
pvt->rssi = 0;
pvt->linkmode = 0;
pvt->linksubmode = 0;
ast_copy_string (pvt->provider_name, "NONE", sizeof (pvt->provider_name));
pvt->manufacturer[0] = '\0';
pvt->model[0] = '\0';
pvt->firmware[0] = '\0';
pvt->imei[0] = '\0';
pvt->imsi[0] = '\0';
pvt->has_subscriber_number = 0;
ast_copy_string (pvt->subscriber_number, "Unknown", sizeof (pvt->subscriber_number));
pvt->location_area_code[0] = '\0';
pvt->cell_id[0] = '\0';
pvt->sms_scenter[0] = '\0';
pvt->gsm_registered = 0;
pvt->has_sms = 0;
pvt->has_voice = 0;
pvt->has_call_waiting = 0;
pvt->use_pdu = 0;
}
pvt->connected = 0;
pvt->initialized = 0;
pvt->use_pdu = 0;
pvt->has_call_waiting = 0;
/* FIXME: LOST real device state */
pvt->dialing = 0;
pvt->ring = 0;
pvt->cwaiting = 0;
pvt->outgoing_sms = 0;
pvt->incoming_sms = 0;
pvt->volume_sync_step = VOLUME_SYNC_BEGIN;
pvt->current_state = DEV_STATE_STOPPED;
/* clear statictics */
memset(&pvt->stat, 0, sizeof(pvt->stat));
ast_copy_string (PVT_STATE(pvt, data_tty), CONF_UNIQ(pvt, data_tty), sizeof (PVT_STATE(pvt, data_tty)));
ast_copy_string (PVT_STATE(pvt, audio_tty), CONF_UNIQ(pvt, audio_tty), sizeof (PVT_STATE(pvt, audio_tty)));
ast_verb (3, "[%s] Dongle has disconnected\n", PVT_ID(pvt));
manager_event_device_status(PVT_ID(pvt), "Disconnect");
}
/* anybody wrote some to device before me, and not read results, clean pending results here */
#/* */
EXPORT_DEF void clean_read_data(const char * devname, int fd)
{
char buf[2*1024];
struct ringbuffer rb;
int iovcnt;
int t;
rb_init (&rb, buf, sizeof (buf));
for (t = 0; at_wait(fd, &t); t = 0)
{
iovcnt = at_read (fd, devname, &rb);
ast_debug (4, "[%s] drop %u bytes of pending data before initialization\n", devname, (unsigned)rb_used(&rb));
/* drop readed */
rb_init (&rb, buf, sizeof (buf));
if (iovcnt == 0)
break;
}
}
/*!
* \brief Check if the module is unloading.
* \retval 0 not unloading
* \retval 1 unloading
*/
static void* do_monitor_phone (void* data)
{
struct pvt* pvt = (struct pvt*) data;
at_res_t at_res;
const struct at_queue_cmd * ecmd;
int t;
char buf[2*1024];
struct ringbuffer rb;
struct iovec iov[2];
int iovcnt;
char dev[sizeof(PVT_ID(pvt))];
int fd;
int read_result = 0;
pvt->timeout = DATA_READ_TIMEOUT;
rb_init (&rb, buf, sizeof (buf));
ast_mutex_lock (&pvt->lock);
/* 4 reduce locking time make copy of this readonly fields */
fd = pvt->data_fd;
ast_copy_string(dev, PVT_ID(pvt), sizeof(dev));
clean_read_data(dev, fd);
/* schedule dongle initilization */
if (at_enque_initialization (&pvt->sys_chan, CMD_AT))
{
ast_log (LOG_ERROR, "[%s] Error adding initialization commands to queue\n", dev);
goto e_cleanup;
}
ast_mutex_unlock (&pvt->lock);
while (1)
{
ast_mutex_lock (&pvt->lock);
if (port_status (pvt->data_fd) || port_status (pvt->audio_fd))
{
ast_log (LOG_ERROR, "[%s] Lost connection to Dongle\n", dev);
goto e_cleanup;
}
if(pvt->terminate_monitor)
{
ast_log (LOG_NOTICE, "[%s] stopping by %s request\n", dev, dev_state2str(pvt->desired_state));
goto e_restart;
}
t = at_queue_timeout(pvt);
if(t < 0)
t = pvt->timeout;
ast_mutex_unlock (&pvt->lock);
if (!at_wait (fd, &t))
{
ast_mutex_lock (&pvt->lock);
ecmd = at_queue_head_cmd (pvt);
if(ecmd)
{
ast_log (LOG_ERROR, "[%s] timedout while waiting '%s' in response to '%s'\n", dev, at_res2str (ecmd->res), at_cmd2str (ecmd->cmd));
goto e_cleanup;
}
at_enque_ping(&pvt->sys_chan);
ast_mutex_unlock (&pvt->lock);
continue;
}
/* FIXME: access to device not locked */
iovcnt = at_read (fd, dev, &rb);
if (iovcnt < 0)
{
break;
}
PVT_STAT(pvt, d_read_bytes) += iovcnt;
while ((iovcnt = at_read_result_iov (dev, &read_result, &rb, iov)) > 0)
{
at_res = at_read_result_classification (&rb, iov[0].iov_len + iov[1].iov_len);
ast_mutex_lock (&pvt->lock);
PVT_STAT(pvt, at_responces) ++;
if (at_response (pvt, iov, iovcnt, at_res) || at_queue_run(pvt))
{
goto e_cleanup;
}
ast_mutex_unlock (&pvt->lock);
}
}
ast_mutex_lock (&pvt->lock);
e_cleanup:
if (!pvt->initialized)
{
// TODO: send monitor event
ast_verb (3, "[%s] Error initializing Dongle\n", dev);
}
/* it real, unsolicited disconnect */
pvt->terminate_monitor = 0;
e_restart:
disconnect_dongle (pvt);
// pvt->monitor_running = 0;
ast_mutex_unlock (&pvt->lock);
/* TODO: wakeup discovery thread after some delay */
return NULL;
}
static inline int start_monitor (struct pvt * pvt)
{
if (ast_pthread_create_background (&pvt->monitor_thread, NULL, do_monitor_phone, pvt) < 0)
{
pvt->monitor_thread = AST_PTHREADT_NULL;
return 0;
}
return 1;
}
#/* */
static void pvt_stop(struct pvt * pvt)
{
pthread_t id;
if(pvt->monitor_thread != AST_PTHREADT_NULL)
{
pvt->terminate_monitor = 1;
pthread_kill (pvt->monitor_thread, SIGURG);
id = pvt->monitor_thread;
ast_mutex_unlock (&pvt->lock);
pthread_join (id, NULL);
ast_mutex_lock (&pvt->lock);
pvt->terminate_monitor = 0;
pvt->monitor_thread = AST_PTHREADT_NULL;
}
// pvt->current_state = DEV_STATE_STOPPED;
}
#/* called with pvt lock hold */
static int pvt_discovery(struct pvt * pvt)
{
char devname[DEVNAMELEN];
char imei[IMEI_SIZE+1];
char imsi[IMSI_SIZE+1];
int resolved;
if(CONF_UNIQ(pvt, data_tty)[0] == 0 && CONF_UNIQ(pvt, audio_tty)[0] == 0) {
char * data_tty;
char * audio_tty;
ast_copy_string(devname, PVT_ID(pvt), sizeof(devname));
ast_copy_string(imei, CONF_UNIQ(pvt, imei), sizeof(imei));
ast_copy_string(imsi, CONF_UNIQ(pvt, imsi), sizeof(imsi));
ast_debug(3, "[%s] Trying ports discovery for%s%s%s%s\n",
PVT_ID(pvt),
imei[0] == 0 ? "" : " IMEI ",
imei,
imsi[0] == 0 ? "" : " IMSI ",
imsi
);
ast_mutex_unlock (&pvt->lock);
//sleep(10); // test
resolved = pdiscovery_lookup(devname, imei, imsi, &data_tty, &audio_tty);
ast_mutex_lock (&pvt->lock);
if(resolved) {
ast_copy_string (PVT_STATE(pvt, data_tty), data_tty, sizeof (PVT_STATE(pvt, data_tty)));
ast_copy_string (PVT_STATE(pvt, audio_tty), audio_tty, sizeof (PVT_STATE(pvt, audio_tty)));
ast_free(audio_tty);
ast_free(data_tty);
ast_verb (3, "[%s]%s%s%s%s found on data_tty=%s audio_tty=%s\n",
PVT_ID(pvt),
imei[0] == 0 ? "" : " IMEI ",
imei,
imsi[0] == 0 ? "" : " IMSI ",
imsi,
PVT_STATE(pvt, data_tty),
PVT_STATE(pvt, audio_tty)
);
} else {
ast_debug(3, "[%s] Not found ports for%s%s%s%s\n",
PVT_ID(pvt),
imei[0] == 0 ? "" : " IMEI ",
imei,
imsi[0] == 0 ? "" : " IMSI ",
imsi
);
}
} else {
ast_copy_string (PVT_STATE(pvt, data_tty), CONF_UNIQ(pvt, data_tty), sizeof (PVT_STATE(pvt, data_tty)));
ast_copy_string (PVT_STATE(pvt, audio_tty), CONF_UNIQ(pvt, audio_tty), sizeof (PVT_STATE(pvt, audio_tty)));
resolved = 1;
}
return ! resolved;
}
#/* */
static void pvt_start(struct pvt * pvt)
{
/* prevent start_monitor() multiple times and on turned off devices */
if (!pvt->connected && pvt->desired_state == DEV_STATE_STARTED)
// && (pvt->monitor_thread == AST_PTHREADT_NULL || (pthread_kill(pvt->monitor_thread, 0) != 0 && errno == ESRCH)))
{
pvt_stop(pvt);
if(pvt_discovery(pvt))
return;
ast_verb (3, "[%s] Trying to connect on %s...\n", PVT_ID(pvt), PVT_STATE(pvt, data_tty));
pvt->data_fd = opentty(PVT_STATE(pvt, data_tty), &pvt->dlock);
if (pvt->data_fd >= 0)
{
// TODO: delay until device activate voice call or at pvt_on_create_1st_channel()
pvt->audio_fd = opentty(PVT_STATE(pvt, audio_tty), &pvt->alock);
if (pvt->audio_fd >= 0)
{
if (start_monitor (pvt))
{
pvt->connected = 1;
pvt->current_state = DEV_STATE_STARTED;
manager_event_device_status(PVT_ID(pvt), "Connect");
ast_verb (3, "[%s] Dongle has connected, initializing...\n", PVT_ID(pvt));
return;
}
closetty(pvt->audio_fd, &pvt->alock);
}
closetty(pvt->data_fd, &pvt->dlock);
}
}
}
#/* */
static void pvt_free(struct pvt * pvt)
{
at_queue_flush(pvt);
if(pvt->dsp)
ast_dsp_free(pvt->dsp);
ast_mutex_unlock(&pvt->lock);
ast_free(pvt);
}
#/* */
static void pvt_destroy(struct pvt * pvt)
{
ast_mutex_lock(&pvt->lock);
pvt_stop(pvt);
pvt_free(pvt);
}
static void * do_discovery(void * arg)
{
struct public_state * state = (struct public_state *) arg;
struct pvt * pvt;
while(state->unloading_flag == 0)
{
/* read lock for avoid deadlock when IMEI/IMSI discovery */
AST_RWLIST_RDLOCK(&state->devices);
AST_RWLIST_TRAVERSE(&state->devices, pvt, entry)
{
ast_mutex_lock (&pvt->lock);
pvt->must_remove = 0;
if(pvt->restart_time == RESTATE_TIME_NOW && pvt->desired_state != pvt->current_state)
{
switch(pvt->desired_state)
{
case DEV_STATE_RESTARTED:
pvt_stop(pvt);
/* passthru */
pvt->desired_state = DEV_STATE_STARTED;
case DEV_STATE_STARTED:
pvt_start(pvt);
break;
case DEV_STATE_REMOVED:
pvt_stop(pvt);
pvt->must_remove = 1;
break;
case DEV_STATE_STOPPED:
pvt_stop(pvt);
}
}
ast_mutex_unlock (&pvt->lock);
}
AST_RWLIST_UNLOCK (&state->devices);
/* actual device removal here for avoid long (discovery) time write lock on device list in loop above */
AST_RWLIST_WRLOCK(&state->devices);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&state->devices, pvt, entry)
{
ast_mutex_lock(&pvt->lock);
if(pvt->must_remove)
{
AST_RWLIST_REMOVE_CURRENT(entry);
pvt_free(pvt);
} else
ast_mutex_unlock(&pvt->lock);
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&state->devices);
/* Go to sleep (only if we are not unloading) */
if (state->unloading_flag == 0)
{
sleep(SCONF_GLOBAL(state, discovery_interval));
}
}
return NULL;
}
#/* */
static int discovery_restart(public_state_t * state)
{
if(state->discovery_thread == AST_PTHREADT_STOP)
return 0;
ast_mutex_lock(&state->discovery_lock);
if (state->discovery_thread == pthread_self()) {
ast_mutex_unlock(&state->discovery_lock);
ast_log(LOG_WARNING, "Cannot kill myself\n");
return -1;
}
if (state->discovery_thread != AST_PTHREADT_NULL) {
/* Wake up the thread */
pthread_kill(state->discovery_thread, SIGURG);
} else {
/* Start a new monitor */
if (ast_pthread_create_background(&state->discovery_thread, NULL, do_discovery, state) < 0) {
ast_mutex_unlock(&state->discovery_lock);
ast_log(LOG_ERROR, "Unable to start discovery thread\n");
return -1;
}
}
ast_mutex_unlock(&state->discovery_lock);
return 0;
}
#/* */
static void discovery_stop(public_state_t * state)
{
/* signal for discovery unloading */
state->unloading_flag = 1;
ast_mutex_lock(&state->discovery_lock);
if (state->discovery_thread && (state->discovery_thread != AST_PTHREADT_STOP) && (state->discovery_thread != AST_PTHREADT_NULL)) {
// pthread_cancel(state->discovery_thread);
pthread_kill(state->discovery_thread, SIGURG);
pthread_join(state->discovery_thread, NULL);
}
state->discovery_thread = AST_PTHREADT_STOP;
ast_mutex_unlock(&state->discovery_lock);
}
#/* */
EXPORT_DEF void pvt_on_create_1st_channel(struct pvt* pvt)
{
mixb_init (&pvt->a_write_mixb, pvt->a_write_buf, sizeof (pvt->a_write_buf));
// rb_init (&pvt->a_write_rb, pvt->a_write_buf, sizeof (pvt->a_write_buf));
if(!pvt->a_timer)
pvt->a_timer = ast_timer_open ();
/* FIXME: do on each channel switch */
if(pvt->dsp)
ast_dsp_digitreset (pvt->dsp);
pvt->dtmf_digit = 0;
pvt->dtmf_begin_time.tv_sec = 0;
pvt->dtmf_begin_time.tv_usec = 0;
pvt->dtmf_end_time.tv_sec = 0;
pvt->dtmf_end_time.tv_usec = 0;
manager_event_device_status(PVT_ID(pvt), "Used");
}
#/* */
EXPORT_DEF void pvt_on_remove_last_channel(struct pvt* pvt)
{
if (pvt->a_timer)
{
ast_timer_close(pvt->a_timer);
pvt->a_timer = NULL;
}
manager_event_device_status(PVT_ID(pvt), "Free");
}
#define SET_BIT(dw_array,bitno) do { (dw_array)[(bitno) >> 5] |= 1 << ((bitno) & 31) ; } while(0)
#define TEST_BIT(dw_array,bitno) ((dw_array)[(bitno) >> 5] & 1 << ((bitno) & 31))
#/* */
EXPORT_DEF int pvt_get_pseudo_call_idx(const struct pvt * pvt)
{
struct cpvt * cpvt;
int * bits;
int dwords = ((MAX_CALL_IDX + sizeof(*bits) - 1) / sizeof(*bits));
bits = alloca(dwords * sizeof(*bits));
memset(bits, 0, dwords * sizeof(*bits));
AST_LIST_TRAVERSE(&pvt->chans, cpvt, entry) {
SET_BIT(bits, cpvt->call_idx);
}
for(dwords = 1; dwords <= MAX_CALL_IDX; dwords++)
{
if(!TEST_BIT(bits, dwords))
return dwords;
}
return 0;
}
#undef TEST_BIT
#undef SET_BIT
#/* */
static int is_dial_possible2(const struct pvt * pvt, int opts, const struct cpvt * ignore_cpvt)
{
struct cpvt * cpvt;
int hold = 0;
int active = 0;
// FIXME: allow HOLD states for CONFERENCE
int use_call_waiting = opts & CALL_FLAG_HOLD_OTHER;
if(pvt->ring || pvt->cwaiting || pvt->dialing)
return 0;
AST_LIST_TRAVERSE(&pvt->chans, cpvt, entry) {
switch(cpvt->state)
{
case CALL_STATE_INIT:
if(cpvt != ignore_cpvt)
return 0;
break;
case CALL_STATE_DIALING:
case CALL_STATE_ALERTING:
case CALL_STATE_INCOMING:
case CALL_STATE_WAITING:
return 0;
case CALL_STATE_ACTIVE:
if(hold || !use_call_waiting)
return 0;
active++;
break;
case CALL_STATE_ONHOLD:
if(active || !use_call_waiting)
return 0;
hold++;
break;
case CALL_STATE_RELEASED:
;
}
}
return 1;
}
#/* */
EXPORT_DEF int is_dial_possible(const struct pvt * pvt, int opts)
{
return is_dial_possible2(pvt, opts, NULL);
}
#/* */
EXPORT_DECL int pvt_enabled(const struct pvt * pvt)
{
return pvt->current_state == DEV_STATE_STARTED && (pvt->desired_state == pvt->current_state || pvt->restart_time == RESTATE_TIME_CONVENIENT);
}
#/* */
EXPORT_DEF int ready4voice_call(const struct pvt* pvt, const struct cpvt * current_cpvt, int opts)
{
if(!pvt->connected
||
!pvt->initialized
||
!pvt->has_voice
||
!pvt->gsm_registered
||
!pvt_enabled(pvt)
)
return 0;
return is_dial_possible2(pvt, opts, current_cpvt);
}
#/* */
static int can_dial(struct pvt* pvt, int opts, const struct ast_channel * requestor)
{
/* not allow hold requester channel :) */
/* FIXME: requestor may be just proxy/masquerade for real channel */
// use ast_bridged_channel(chan) ?
// use requestor->tech->get_base_channel() ?
if((opts & CALL_FLAG_HOLD_OTHER) == CALL_FLAG_HOLD_OTHER && channels_loop(pvt, requestor))
return 0;
return ready4voice_call(pvt, NULL, opts);
}
#/* return locked pvt or NULL */
EXPORT_DEF struct pvt * find_device_ex(struct public_state * state, const char * name)
{
struct pvt * pvt;
AST_RWLIST_RDLOCK(&state->devices);
AST_RWLIST_TRAVERSE(&state->devices, pvt, entry)
{
ast_mutex_lock (&pvt->lock);
if (!strcmp (PVT_ID(pvt), name))
{
break;
}
ast_mutex_unlock (&pvt->lock);
}
AST_RWLIST_UNLOCK(&state->devices);
return pvt;
}
#/* return locked pvt or NULL */
EXPORT_DEF struct pvt * find_device_ext (const char * name, const char ** reason)
{
char * res = "";
struct pvt * pvt = find_device(name);
if(pvt)
{
if(!pvt_enabled(pvt))
{
ast_mutex_unlock (&pvt->lock);
res = "device disabled";
pvt = NULL;
}
}
else
res = "no such device";
if(reason)
*reason = res;
return pvt;
}
#/* like find_device but for resource spec; return locked! pvt or NULL */
EXPORT_DEF struct pvt * find_device_by_resource_ex(struct public_state * state, const char * resource, int opts, const struct ast_channel * requestor, int * exists)
{
int group;
size_t i;
size_t j;
size_t c;
size_t last_used;
struct pvt * pvt;
struct pvt * found = NULL;
*exists = 0;
/* Find requested device and make sure it's connected and initialized. */
AST_RWLIST_RDLOCK(&state->devices);
if (((resource[0] == 'g') || (resource[0] == 'G')) && ((resource[1] >= '0') && (resource[1] <= '9')))
{
errno = 0;
group = (int) strtol (&resource[1], (char**) NULL, 10);
if (errno != EINVAL)
{
AST_RWLIST_TRAVERSE(&state->devices, pvt, entry)
{
ast_mutex_lock (&pvt->lock);
if (CONF_SHARED(pvt, group) == group)
{
*exists = 1;
if(can_dial(pvt, opts, requestor))
{
found = pvt;
break;
}
}
ast_mutex_unlock (&pvt->lock);
}
}
}
else if (((resource[0] == 'r') || (resource[0] == 'R')) && ((resource[1] >= '0') && (resource[1] <= '9')))
{
errno = 0;
group = (int) strtol (&resource[1], (char**) NULL, 10);
if (errno != EINVAL)
{
ast_mutex_lock(&state->round_robin_mtx);
/* Generate a list of all availible devices */
j = ITEMS_OF (state->round_robin);
c = 0; last_used = 0;
AST_RWLIST_TRAVERSE(&state->devices, pvt, entry)
{
ast_mutex_lock (&pvt->lock);
if (CONF_SHARED(pvt, group) == group)
{
state->round_robin[c] = pvt;
if (pvt->group_last_used == 1)
{
pvt->group_last_used = 0;
last_used = c;
}
c++;
if (c == j)
{
ast_mutex_unlock (&pvt->lock);
break;
}
}
ast_mutex_unlock (&pvt->lock);
}
/* Search for a availible device starting at the last used device */
for (i = 0, j = last_used + 1; i < c; i++, j++)
{