forked from colognechip/chan_capi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan_capi.c
8574 lines (6862 loc) · 207 KB
/
chan_capi.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) 2006-2013 Hans Petter Selasky. All rights reserved.
* Copyright (C) 2005 Cytronics & Melware, Armin Schindler
* Copyright (C) 2002-2005 Junghanns.NET GmbH, Klaus-Peter Junghanns
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* chan_capi.c - Common ISDN API 2.0 for Asterisk / OpenPBX
*
*/
#include "config.h"
#if (CC_AST_VERSION >= 0x10400)
#include <asterisk.h>
#endif
#include <asterisk/lock.h>
#include <asterisk/frame.h>
#include <asterisk/channel.h>
#ifndef CC_AST_HAVE_TECH_PVT
#include <asterisk/channel_pvt.h>
#endif
#include <asterisk/logger.h>
#include <asterisk/module.h>
#include <asterisk/pbx.h>
#include <asterisk/config.h>
#include <asterisk/options.h>
#include <asterisk/features.h>
#include <asterisk/utils.h>
#include <asterisk/cli.h>
#include <asterisk/causes.h>
#include <asterisk/ulaw.h>
#include <asterisk/alaw.h>
#ifndef CC_AST_NO_DEVICESTATE
#include <asterisk/devicestate.h>
#endif
#ifndef CC_AST_NO_FORMAT_COMPATIBILITY
#include <asterisk/format_compatibility.h>
#endif
#ifdef CC_AST_MOH_PRESENT
#include <asterisk/musiconhold.h>
#endif
#if (CC_AST_VERSION >= 0x10800)
#include <asterisk/callerid.h>
#endif
#include <sys/time.h>
#include <sys/signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <stdint.h>
#include <asterisk/dsp.h>
#include "xlaw.h"
#define CAPI_MAKE_TRANSLATOR
#define panic(...)
#include "chan_capi20.h"
#include "chan_capi.h"
#undef _XPTR
#define _XPTR(data) *(void **)(&(data))
/*
* local variables
*/
#define CHAN_CAPI_DESC "Common ISDN API 2.0 Driver " ASTERISKVERSION
#define CHAN_CAPI_APP "capiCommand"
#ifdef CC_AST_HAVE_TECH_PVT
static const char chan_capi_pbx_type[] = "CAPI";
extern struct ast_channel_tech chan_capi_tech;
#else
static char *chan_capi_pbx_type = "CAPI";
#endif
static const char * const config_file = "capi.conf";
#if (CC_AST_VERSION < 0x10400)
STANDARD_LOCAL_USER;
LOCAL_USER_DECL;
#else
static int unload_module(void);
#undef CC_AST_CUSTOM_FUNCTION
#define AST_MODULE "chan_capi"
#endif
#if (CC_AST_VERSION >= 0x10800)
#undef strlcpy
#undef strlcat
#define FRAME_SUBCLASS(x) x.integer
#define ast_channel_free ast_channel_release
#else
#define FRAME_SUBCLASS(x) x
#endif
/*
* LOCKING RULES
* =============
*
* This channel driver uses several locks. One must be
* careful not to reverse the locking order, which will
* lead to a so called deadlock. Here is the locking order
* that must be followed:
*
* struct call_desc *cd;
* struct cc_capi_application *p_app;
*
* 1. cc_mutex_lock(&do_periodic_lock);
*
* 2. cc_mutex_lock(&modlock); (See Asterisk source code)
*
* 3. cc_mutex_lock(&chlock); (See Asterisk source code)
*
* 4. cc_mutex_lock(&cd->pbx_chan->lock); **
*
* 5. cc_mutex_lock(&p_app->lock); ***
*
* 6. cc_mutex_lock(&capi_global_lock);
*
* 7. cc_mutex_lock(&capi_verbose_lock);
*
*
* ** the PBX will call the callback functions with
* this lock locked. This lock protects the
* structure pointed to by 'cd->pbx_chan'. Also note
* that calling some PBX functions will lock
* this lock!
*
* *** in case multiple CAPI applications should be locked
* in series, the CAPI application with the lowest
* memory address should be locked first.
*/
static ast_mutex_t do_periodic_lock;
static ast_mutex_t capi_global_lock;
static ast_mutex_t capi_verbose_lock;
static pthread_t periodic_thread;
static uint16_t chan_capi_load_level = 0;
static struct config_entry_global capi_global;
static struct cc_capi_application *capi_application[CAPI_MAX_APPLICATIONS];
static struct cc_capi_controller capi_controller[CAPI_MAX_CONTROLLERS];
static uint8_t capi_controller_used_mask[(CAPI_MAX_CONTROLLERS+7)/8];
static struct config_entry_iface *cep_root_ptr;
static struct config_entry_iface *cep_free_ptr;
static const char * const empty_string = "\0\0";
static const uint8_t sending_complete_struct[] = { 2, 1, 0 };
static const uint8_t sending_not_complete_struct[] = { 2, 0, 0 };
static uint8_t update_use_count = 0;
/* external prototypes */
#include "c20msg.h"
/*===========================================================================*
* ring buffer routines
*===========================================================================*/
static void
buf_init(struct ring_buffer *buffer)
{
memset(buffer, 0, sizeof(*buffer));
buffer->end_pos = sizeof(buffer->data);
buffer->last_byte = 0xFF; /* ISDN default */
buffer->bf_free_len = FIFO_BF_SIZE;
return;
}
static void
buf_write_block(struct ring_buffer *buffer,
const uint8_t *data, uint16_t len_data)
{
uint16_t len_max = buffer->end_pos - buffer->bf_write_pos;
if (len_data > buffer->bf_free_len) {
/* data overflow */
len_data = buffer->bf_free_len;
}
if (len_data > 0) {
buffer->last_byte = data[len_data-1];
}
/* update free data length */
buffer->bf_free_len -= len_data;
buffer->bf_used_len += len_data;
/* copy data */
if(len_data >= len_max) {
/* wrapped write */
memcpy(&(buffer->data[buffer->bf_write_pos]), data, len_max);
buffer->bf_write_pos = 0;
len_data -= len_max;
data += len_max;
}
memcpy(&buffer->data[buffer->bf_write_pos], data, len_data);
buffer->bf_write_pos += len_data;
return;
}
static void
buf_read_block(struct ring_buffer *buffer, void **p_ptr, uint16_t *p_len)
{
uint8_t temp[FIFO_BLOCK_SIZE];
uint16_t len;
if(buffer->bf_used_len < FIFO_BLOCK_SIZE) {
/* data underflow */
len = FIFO_BLOCK_SIZE - buffer->bf_used_len;
memset(temp, buffer->last_byte, len);
buf_write_block(buffer, temp, len);
}
*p_ptr = &(buffer->data[buffer->bf_read_pos]);
*p_len = FIFO_BLOCK_SIZE;
if(buffer->bf_used_len >= FIFO_BLOCK_SIZE) {
buffer->bf_used_len -= FIFO_BLOCK_SIZE;
buffer->bf_free_len += FIFO_BLOCK_SIZE;
}
buffer->bf_read_pos += FIFO_BLOCK_SIZE;
if (buffer->bf_read_pos >= buffer->end_pos) {
buffer->bf_read_pos -= buffer->end_pos;
}
return;
}
/*===========================================================================*
* various CAPI helper functions
*===========================================================================*/
#if (CAPI_OS_HINT == 0)
/*
* Copyright (c) 1998 Todd C. Miller <[email protected]>
*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
static size_t
strlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
static size_t
strlcat(dst, src, siz)
char *dst;
const char *src;
size_t siz;
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif
/* simple counter */
static uint32_t
capi_get_counter(void)
{
static uint32_t count;
uint32_t temp;
cc_mutex_lock(&capi_global_lock);
temp = count++;
cc_mutex_unlock(&capi_global_lock);
return temp;
}
/* check if a CAPI structure read, at the given offset, is valid */
static uint8_t
capi_get_valid(const void *ptr, uint16_t offset)
{
const uint8_t *data = (const uint8_t *)ptr;
uint16_t len;
if (data == NULL) {
return 0;
}
if (data[0] == 0xff) {
len = data[1] | (data[2] << 8);
} else {
len = data[0];
}
return ((offset < len) ? 1 : 0);
}
/* read a byte from a CAPI structure */
static uint8_t
capi_get_1(const void *ptr, uint16_t offset)
{
const uint8_t *data = (const uint8_t *)ptr;
uint16_t len;
if (data == NULL) {
return 0;
}
if (data[0] == 0xff) {
len = data[1] | (data[2] << 8);
data += 2;
} else {
len = data[0];
data += 1;
}
return ((offset < len) ? data[offset] : 0);
}
/* read a word from a CAPI structure */
static uint16_t
capi_get_2(const void *ptr, uint16_t offset)
{
return (capi_get_1(ptr,offset)|(capi_get_1(ptr,offset+1) << 8));
}
/* read a dword from a CAPI structure */
static uint32_t
capi_get_4(const void *ptr, uint16_t offset)
{
return (capi_get_2(ptr,offset)|(capi_get_2(ptr,offset+2) << 16));
}
/* convert a CAPI structure into a zero terminated string */
static void
capi_get_multi_1(const void *src, uint16_t offset,
void *dst, uint16_t max_len)
{
const uint8_t *data = (const uint8_t *)src;
uint16_t len;
if (max_len == 0) {
return;
}
/* reserve one byte for
* the terminating zero:
*/
max_len--;
if (data == NULL) {
len = 0;
} else {
if (data[0] == 0xff) {
len = data[1] | (data[2] << 8);
data += 2;
} else {
len = data[0];
data += 1;
}
}
if (offset >= len) {
((uint8_t *)dst)[0] = 0;
return;
}
len -= offset;
data += offset;
if (len > max_len) {
len = max_len;
}
memcpy(dst, data, len);
((uint8_t *)dst)[len] = '\0';
return;
}
/* write a byte to a CAPI structure */
static void
capi_put_1(void *ptr, uint16_t offset, uint8_t value)
{
uint8_t *data = (uint8_t *)ptr;
uint16_t len;
if (data == NULL) {
return;
}
if (data[0] == 0xff) {
len = data[1] | (data[2] << 8);
data += 2;
} else {
len = data[0];
data += 1;
}
if(offset < len) {
data[offset] = value;
}
return;
}
/* write a word to a CAPI structure */
static void
capi_put_2(void *ptr, uint16_t offset, uint16_t value)
{
capi_put_1(ptr, offset, value);
capi_put_1(ptr, offset+1, value >> 8);
return;
}
/* write a dword to a CAPI structure */
static void
capi_put_4(void *ptr, uint16_t offset, uint32_t value)
{
capi_put_2(ptr, offset, value);
capi_put_2(ptr, offset+2, value >> 16);
return;
}
/* build a CAPI structure */
static void
capi_build_struct(void *dst, uint16_t max_len,
const void *src1, uint16_t len1,
const void *src2, uint16_t len2,
const void *src3, uint16_t len3)
{
uint32_t temp = (len1 + len2 + len3);
uint8_t *dst_ptr = (uint8_t *)dst;
if (max_len < 3) {
/* just forget it */
return;
}
max_len -= 3;
if (temp >= max_len) {
/* truncate */
temp = max_len;
}
max_len += 3;
if (temp >= 0xFF) {
dst_ptr[0] = 0xFF;
dst_ptr[1] = (temp & 0xFF);
dst_ptr[2] = (temp >> 8);
dst_ptr += 3;
max_len -= 3;
} else {
dst_ptr[0] = temp;
dst_ptr += 1;
max_len -= 1;
}
if (len1) {
#undef min
#define min(a,b) ((a) < (b) ? (a) : (b))
temp = min(max_len, len1);
memcpy(dst_ptr, src1, temp);
dst_ptr += temp;
max_len -= temp;
}
if (len2) {
temp = min(max_len, len2);
memcpy(dst_ptr, src2, temp);
dst_ptr += temp;
max_len -= temp;
}
if (len3) {
temp = min(max_len, len3);
memcpy(dst_ptr, src3, temp);
dst_ptr += temp;
max_len -= temp;
}
return;
}
/* copy sound and do sound conversion */
static void
capi_copy_sound(const void *_src, void *_dst,
uint16_t len, const uint8_t *p_table)
{
const uint8_t *src = (const uint8_t *)(_src);
uint8_t *dst = (uint8_t *)(_dst);
if (p_table) {
if (src == dst) {
while(len--) {
*dst = p_table[*dst];
dst++;
}
} else {
while(len--) {
*dst = p_table[*src];
dst++;
src++;
}
}
} else {
if(src != dst) {
memcpy(dst, src,len);
}
}
return;
}
/* command to string function */
static const char *
capi_command_to_string(uint16_t wCmd)
{
enum { lowest_value = CAPI_P_MIN,
end_value = CAPI_P_MAX,
range = end_value - lowest_value,
};
#undef CHAN_CAPI_COMMAND_DESC
#define CHAN_CAPI_COMMAND_DESC(n, ENUM, value) \
[CAPI_P_REQ(ENUM)-(n)] = #ENUM "_REQ", \
[CAPI_P_CONF(ENUM)-(n)] = #ENUM "_CONF", \
[CAPI_P_IND(ENUM)-(n)] = #ENUM "_IND", \
[CAPI_P_RESP(ENUM)-(n)] = #ENUM "_RESP",
static const char * const table[range] = {
CAPI_COMMANDS(CHAN_CAPI_COMMAND_DESC, lowest_value)
};
wCmd -= lowest_value;
if (wCmd >= range) {
goto error;
}
if (table[wCmd] == NULL) {
goto error;
}
return table[wCmd];
error:
return "UNDEFINED";
}
/* show the text for a CAPI message info value */
static void
capi_show_info(uint16_t info)
{
const char *p;
if (info == 0x0000) {
/* no error, do nothing */
return;
}
if (!(p = capi_info_string((uint32_t)info))) {
/* message not available */
return;
}
cc_verbose(3, 0, VERBOSE_PREFIX_4 "CAPI INFO "
"0x%04x: %s\n", info, p);
return;
}
/* show error in confirmation */
static void
capi_show_conf_error(struct call_desc *cd, uint32_t PLCI,
uint16_t wInfo, uint16_t wCmd)
{
const char *name = chan_capi_pbx_type;
if (cd && cd->cep) {
name = cd->cep->name;
}
if ((wInfo == 0x2002) && cd) {
cd_verbose(cd, 1, 1, 3, "0x%04x (wrong state) "
"Command=%s,0x%04x\n",
wInfo, capi_command_to_string(wCmd), wCmd);
} else {
cc_log(LOG_WARNING, "%s: conf_error 0x%04x "
"PLCI=0x%08x Command=%s,0x%04x\n",
name, wInfo, PLCI, capi_command_to_string(wCmd), wCmd);
}
return;
}
/*===========================================================================*
* call descriptor and configuration management functions
*===========================================================================*/
/* prototypes */
static void
cd_free(struct call_desc *cd, uint8_t hangup_what);
static uint8_t
cd_set_cep(struct call_desc *cd, struct config_entry_iface *cep);
static void
cd_root_shrink(struct call_desc *cd);
static void *
capi_do_monitor(void *data);
static uint16_t
capi_send_disconnect_req(struct call_desc *cd);
static uint16_t
capi_send_connect_resp(struct call_desc *cd, uint16_t wReject,
const uint16_t *p_bprot);
static uint16_t
capi_send_connect_b3_req(struct call_desc *cd);
static void
capi_handle_cmsg(struct cc_capi_application *p_app, _cmsg *CMSG);
static uint16_t
chan_capi_cmd_progress(struct call_desc *cd, struct call_desc *cd_unknown,
char *param);
static uint16_t
chan_capi_cmd_retrieve(struct call_desc *cd, struct call_desc *cd_unknown,
char *param);
static uint16_t
chan_capi_cmd_hold(struct call_desc *cd, struct call_desc *cd_unknown,
char *param);
#ifndef CC_AST_HAVE_TECH_PVT
static void
chan_capi_fill_pvt(struct ast_channel *pbx_chan);
#endif
static uint16_t
chan_capi_fill_controller_info(struct cc_capi_application *p_app,
const uint16_t controller_unit);
static struct call_desc *
cd_by_pbx_chan(struct ast_channel *pbx_chan);
static int
cd_send_pbx_frame(struct call_desc *cd, int frametype, int subclass,
const void *data, int len);
static void
capi_usleep(uint32_t us);
static void
capi_application_usleep(struct cc_capi_application *p_app, uint32_t us);
static int
chan_capi_scan_config(struct ast_config *cfg, int do_global);
static uint16_t
chan_capi_post_init(struct cc_capi_application *p_app);
#define CD_IS_UNUSED(cd) \
(((cd)->msg_num == 0x0000) && \
((cd)->msg_plci == 0x0000) && \
((cd)->state == 0x00))
#define CD_NO_HANGUP(cd) \
(((cd)->hangup_chan == NULL) && \
((cd)->free_chan == NULL))
/* initialize sound conversion tables */
static void
cep_init_convert_tables(struct config_entry_iface *cep)
{
const int dp = 256; /* decimal point */
const int gain_max = ((dp*256)-1);
int capability;
int rx_gain = (int)(cep->rx_gain * 256.0);
int tx_gain = (int)(cep->tx_gain * 256.0);
int n = 0;
int x = 0;
cc_mutex_lock(&capi_global_lock);
capability = capi_global.capability;
cc_mutex_unlock(&capi_global_lock);
if (rx_gain > gain_max) {
rx_gain = gain_max;
}
if (tx_gain > gain_max) {
tx_gain = gain_max;
}
if (rx_gain < -gain_max) {
rx_gain = -gain_max;
}
if (tx_gain < -gain_max) {
tx_gain = -gain_max;
}
if (rx_gain != dp) {
for (n = 0; n < 256; n++) {
if (capability == AST_FORMAT_ULAW) {
x = (capi_ulaw_to_signed[n] * rx_gain) / dp;
} else {
x = (capi_alaw_to_signed[n] * rx_gain) / dp;
}
if (capability == AST_FORMAT_ULAW) {
cep->rx_convert[n] = capi_reverse_bits[capi_signed_to_ulaw(x)];
} else {
cep->rx_convert[n] = capi_reverse_bits[capi_signed_to_alaw(x)];
}
}
} else {
for (n = 0; n < 256; n++) {
cep->rx_convert[n] = capi_reverse_bits[n];
}
}
if (tx_gain != dp) {
for (n = 0; n < 256; n++) {
if (capability == AST_FORMAT_ULAW) {
x = (capi_ulaw_to_signed[capi_reverse_bits[n]] * tx_gain) / dp;
} else {
x = (capi_alaw_to_signed[capi_reverse_bits[n]] * tx_gain) / dp;
}
if (capability == AST_FORMAT_ULAW) {
cep->tx_convert[n] = capi_signed_to_ulaw(x);
} else {
cep->tx_convert[n] = capi_signed_to_alaw(x);
}
}
} else {
for (n = 0; n < 256; n++) {
cep->tx_convert[n] = capi_reverse_bits[n];
}
}
return;
}
static void
cep_root_prepend(struct config_entry_iface *cep)
{
cc_mutex_lock(&capi_global_lock);
cep->next = cep_root_ptr;
cep_root_ptr = cep;
cc_mutex_unlock(&capi_global_lock);
return;
}
static struct config_entry_iface *
cep_alloc(const char *name)
{
struct config_entry_iface *cep;
struct config_entry_iface **cep_p;
if (name == NULL) {
return NULL;
}
cc_mutex_lock(&capi_global_lock);
cep = cep_free_ptr;
cep_p = &cep_free_ptr;
while (cep) {
if (strcmp(cep->name, name) == 0) {
/* remove entry from linked list */
cep_p[0] = cep->next;
cep->next = NULL;
break;
}
cep_p = &cep->next;
cep = cep->next;
}
cc_mutex_unlock(&capi_global_lock);
if (cep == NULL) {
cep = malloc(sizeof(*cep));
if (cep) {
memset(cep, 0, sizeof(*cep));
strlcpy(cep->name, name, sizeof(cep->name));
}
} else {
memset(&cep->dummy_zero_start[0], 0,
&cep->dummy_zero_end[0] -
&cep->dummy_zero_start[0]);
}
return cep;
}
static void
cep_unload(void)
{
struct config_entry_iface **cep_p;
struct config_entry_iface *cep;
cc_mutex_lock(&capi_global_lock);
cep = cep_root_ptr;
while (cep) {
/* If there are active channels
* the counters won't reach zero,
* therefore max is subtracted instead:
*/
cep->b_channels_curr -= cep->b_channels_max;
cep->b_channels_max = 0;
cep->d_channels_curr -= cep->d_channels_max;
cep->d_channels_max = 0;
cep = cep->next;
}
cep = cep_free_ptr;
cep_p = &cep_free_ptr;
while (cep) {
cep_p = &cep->next;
cep = cep->next;
}
/* move all configuration
* entries over to free
* list:
*/
cep_p[0] = cep_root_ptr;
cep_root_ptr = NULL;
cc_mutex_unlock(&capi_global_lock);
}
static struct config_entry_iface *
cep_root_acquire(void)
{
struct config_entry_iface *cep;
cc_mutex_lock(&capi_global_lock);
cep = cep_root_ptr;
return cep;
}
static void
cep_root_release(void)
{
/* currently does nothing: eventually this function
* should do some cleanup, update config ...
*/
cc_mutex_unlock(&capi_global_lock);
return;
}
static void
cep_queue_last(struct config_entry_iface *cep_last)