Adds screening of redirecting number
[lcr.git] / bchannel.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN channel handlin for remote application                              **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <poll.h>
17 #include <errno.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <netinet/udp.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <mISDN/mISDNif.h>
27
28 #include <mISDN/mISDNcompat.h>
29 int __af_isdn = MISDN_AF_ISDN;
30 #include <mISDN/q931.h>
31
32 #define HAVE_ATTRIBUTE_always_inline 1
33 #define HAVE_ARPA_INET_H 1
34 #define HAVE_TIMERSUB 1
35
36 #include <asterisk/autoconfig.h>
37 #include <asterisk/compiler.h>
38 #include <asterisk/frame.h>
39
40 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
41 /* #define LCR_FOR_CALLWEAVER */
42
43 #ifdef LCR_FOR_CALLWEAVER
44 #include <asterisk/phone_no_utils.h>
45 #include <asterisk/logger.h>
46 #include <asterisk/module.h>
47 #include <asterisk/channel.h>
48 #endif
49
50 #include "extension.h"
51 #include "message.h"
52 #include "lcrsocket.h"
53 #include "cause.h"
54 #include "select.h"
55 #include "bchannel.h"
56 #include "chan_lcr.h"
57 #include "callerid.h"
58 #include "options.h"
59
60
61 #ifndef ISDN_PID_L4_B_USER
62 #define ISDN_PID_L4_B_USER 0x440000ff
63 #endif
64
65 pid_t   bchannel_pid;
66
67 enum {
68         BSTATE_IDLE,
69         BSTATE_ACTIVATING,
70         BSTATE_ACTIVE,
71         BSTATE_DEACTIVATING,
72 };
73
74 static void bchannel_send_queue(struct bchannel *bchannel);
75
76 int bchannel_initialize(void)
77 {
78         return 0;
79 }
80
81 void bchannel_deinitialize(void)
82 {
83 }
84
85 /*
86  * send control information to the channel (dsp-module)
87  */
88 static void ph_control(int sock, unsigned int c1, unsigned int c2, char *trace_name, int trace_value, int b_mode)
89 {
90         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
91         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
92         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
93         int ret;
94
95         if (b_mode != 0 && b_mode != 2)
96                 return;
97
98         CDEBUG(NULL, NULL, "Sending PH_CONTROL %s %x,%x\n", trace_name, c1, c2);
99         ctrl->prim = PH_CONTROL_REQ;
100         ctrl->id = 0;
101         *d++ = c1;
102         *d++ = c2;
103         ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
104         if (ret < 0)
105                 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
106 }
107
108 static void ph_control_block(int sock, unsigned int c1, void *c2, int c2_len, char *trace_name, int trace_value, int b_mode)
109 {
110         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
111         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
112         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
113         int ret;
114
115         if (b_mode != 0 && b_mode != 2)
116                 return;
117
118         CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %s %x\n", trace_name, c1);
119         ctrl->prim = PH_CONTROL_REQ;
120         ctrl->id = 0;
121         *d++ = c1;
122         memcpy(d, c2, c2_len);
123         ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
124         if (ret < 0)
125                 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
126 }
127
128 static int bchannel_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index);
129
130 /*
131  * create stack
132  */
133 int bchannel_create(struct bchannel *bchannel, int mode, int queue)
134 {
135         int ret;
136         struct sockaddr_mISDN addr;
137
138         if (bchannel->b_sock > -1) {
139                 CERROR(bchannel->call, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
140                 return 0;
141         }
142
143         /* open socket */
144         bchannel->b_mode = (mode & 3);
145         switch(bchannel->b_mode) {
146                 case 0:
147                 CDEBUG(bchannel->call, NULL, "Open DSP audio\n");
148                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
149                 break;
150                 case 1:
151                 CDEBUG(bchannel->call, NULL, "Open audio\n");
152                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
153                 break;
154                 case 2:
155                 CDEBUG(bchannel->call, NULL, "Open DSP HDLC\n");
156                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSPHDLC);
157                 break;
158                 case 3:
159                 CDEBUG(bchannel->call, NULL, "Open HDLC\n");
160                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_HDLC);
161                 break;
162         }
163         if (bchannel->b_sock < 0) {
164                 CERROR(bchannel->call, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDN_dsp.ko?\n", bchannel->handle);
165                 return 0;
166         }
167
168         /* register fd */
169         memset(&bchannel->lcr_fd, 0, sizeof(bchannel->lcr_fd));
170         bchannel->lcr_fd.fd = bchannel->b_sock;
171         register_fd(&bchannel->lcr_fd, LCR_FD_READ | LCR_FD_EXCEPT, bchannel_handle, bchannel, 0);
172
173         /* bind socket to bchannel */
174         addr.family = AF_ISDN;
175         addr.dev = (bchannel->handle>>8);
176         addr.channel = bchannel->handle & 0xff;
177         ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
178         if (ret < 0) {
179                 CERROR(bchannel->call, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. (port %d, channel %d) Did you load mISDN_dsp.ko?\n", bchannel->handle, addr.dev, addr.channel);
180                 unregister_fd(&bchannel->lcr_fd);
181                 close(bchannel->b_sock);
182                 bchannel->b_sock = -1;
183                 return 0;
184         }
185
186         /* queue */
187         if (bchannel->b_mode == 1 && queue) {
188                 bchannel->nodsp_queue_out = 0;
189                 bchannel->nodsp_queue_in = queue * 8;
190                 if (bchannel->nodsp_queue_in > QUEUE_BUFFER_MAX-1)
191                         bchannel->nodsp_queue_in = QUEUE_BUFFER_MAX-1;
192                 bchannel->nodsp_queue = bchannel->nodsp_queue_in; /* store initial load */
193                 memset(&bchannel->nodsp_queue_buffer, (options.law=='a')?0x2a:0xff, QUEUE_BUFFER_SIZE);
194                 bchannel->queue_sent = 0;
195         } else
196                 bchannel->nodsp_queue = 0;
197                                                                                  
198         return 1;
199 }
200
201
202 /*
203  * activate / deactivate request
204  */
205 void bchannel_activate(struct bchannel *bchannel, int activate)
206 {
207         struct mISDNhead act;
208         int ret;
209
210         /* activate bchannel */
211         CDEBUG(bchannel->call, NULL, "%sActivating B-channel.\n", activate?"":"De-");
212         switch(bchannel->b_mode) {
213                 case 0:
214                 case 2:
215                 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ; 
216                 break;
217                 case 1:
218                 case 3:
219                 act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ; 
220                 break;
221         }
222         act.id = 0;
223         ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
224         if (ret < 0)
225                 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
226
227         bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
228 }
229
230
231 /*
232  * set features
233  */
234 static void bchannel_activated(struct bchannel *bchannel)
235 {
236         int sock;
237
238         sock = bchannel->b_sock;
239
240         /* set dsp features */
241         if (bchannel->b_txdata)
242                 ph_control(sock, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata, bchannel->b_mode);
243         if (bchannel->b_delay && bchannel->b_mode == 0)
244                 ph_control(sock, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay, bchannel->b_mode);
245         if (bchannel->b_tx_dejitter && bchannel->b_mode == 0)
246                 ph_control(sock, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter, bchannel->b_mode);
247         if (bchannel->b_tx_gain && bchannel->b_mode == 0)
248                 ph_control(sock, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain, bchannel->b_mode);
249         if (bchannel->b_rx_gain && bchannel->b_mode == 0)
250                 ph_control(sock, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain, bchannel->b_mode);
251         if (bchannel->b_pipeline[0] && bchannel->b_mode == 0)
252                 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
253         if (bchannel->b_conf)
254                 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
255         if (bchannel->b_echo)
256                 ph_control(sock, DSP_ECHO_ON, 0, "DSP-ECHO", 1, bchannel->b_mode);
257         if (bchannel->b_tone && bchannel->b_mode == 0)
258                 ph_control(sock, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone, bchannel->b_mode);
259         if (bchannel->b_rxoff)
260                 ph_control(sock, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1, bchannel->b_mode);
261 //      if (bchannel->b_txmix && bchannel->b_mode == 0)
262 //              ph_control(sock, DSP_MIX_ON, 0, "DSP-MIX", 1, bchannel->b_mode);
263         if (bchannel->b_dtmf && bchannel->b_mode == 0)
264                 ph_control(sock, DTMF_TONE_START, 0, "DSP-DTMF", 1, bchannel->b_mode);
265         if (bchannel->b_bf_len && bchannel->b_mode == 0)
266                 ph_control_block(sock, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
267         if (bchannel->b_conf)
268                 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
269
270         bchannel->b_state = BSTATE_ACTIVE;
271 }
272
273 /*
274  * destroy stack
275  */
276 void bchannel_destroy(struct bchannel *bchannel)
277 {
278         if (bchannel->b_sock > -1) {
279                 unregister_fd(&bchannel->lcr_fd);
280                 close(bchannel->b_sock);
281                 bchannel->b_sock = -1;
282         }
283         bchannel->b_state = BSTATE_IDLE;
284 }
285
286
287 /*
288  * whenever we get audio data from bchannel, we process it here
289  */
290 static void bchannel_receive(struct bchannel *bchannel, unsigned char *buffer, int len)
291 {
292         struct mISDNhead *hh = (struct mISDNhead *)buffer;
293         unsigned char *data = buffer + MISDN_HEADER_LEN;
294         unsigned int cont = *((unsigned int *)data);
295         unsigned char *d;
296         int i;
297         struct bchannel *remote_bchannel;
298         int ret;
299
300         if (hh->prim == PH_CONTROL_IND) {
301                 /* non dsp -> ignore ph_control */
302                 if (bchannel->b_mode == 1 || bchannel->b_mode == 3)
303                         return;
304                 if (len < 4) {
305                         CERROR(bchannel->call, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
306                         return;
307                 }
308                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL) {
309                         if (bchannel->call)
310                                 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
311                         return;
312                 }
313                 switch(cont) {
314                         case DSP_BF_REJECT:
315                         CERROR(bchannel->call, NULL, "Blowfish crypt rejected.\n");
316                         break;
317
318                         case DSP_BF_ACCEPT:
319                         CDEBUG(bchannel->call, NULL, "Blowfish crypt enabled.\n");
320                         break;
321
322                         default:
323                         CDEBUG(bchannel->call, NULL, "Unhandled bchannel control 0x%x.\n", cont);
324                 }
325                 return;
326         }
327         if (hh->prim == PH_DATA_REQ) {
328                 if (!bchannel->b_txdata) {
329                         /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
330                         CDEBUG(bchannel->call, NULL, "ignoring tx data, because 'txdata' is turned off\n");
331                         return;
332                 }
333                 return;
334         }
335         if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND) {
336                 CERROR(bchannel->call, NULL, "Bchannel received unknown primitve: 0x%lx\n", hh->prim);
337                 return;
338         }
339         /* if calls are bridged, but not via dsp (no b_conf), forward here */
340         if (!bchannel->b_conf
341          && bchannel->call
342          && bchannel->call->bridge_call
343          && bchannel->call->bridge_call->bchannel) {
344                 remote_bchannel = bchannel->call->bridge_call->bchannel;
345 #if 0
346                 int i = 0;
347                 char string[4096] = "";
348                 while(i < len) {
349                         sprintf(string+strlen(string), " %02x", data[i]);
350                         i++;
351                 }
352                 CDEBUG(remote_bchannel->call, NULL, "Forwarding packet%s\n", string);
353 #endif
354                 hh->prim = PH_DATA_REQ;
355                 hh->id = 0;
356                 ret = sendto(remote_bchannel->b_sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
357                 if (ret < 0)
358                         CERROR(remote_bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
359                 return;
360         }
361         /* calls will not process any audio data unless
362          * the call is connected OR interface features audio during call setup.
363          */
364
365         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
366         if (bchannel->b_rxoff) {
367                 CDEBUG(bchannel->call, NULL, "ignoring data, because rx is turned off\n");
368                 return;
369         }
370
371         if (!bchannel->call) {
372                 CDEBUG(bchannel->call, NULL, "ignoring data, because no call associated with bchannel\n");
373                 return;
374         }
375         if (!bchannel->call->audiopath) {
376                 /* return, because we have no audio from port */
377                 return;
378         }
379
380         if (bchannel->call->pipe[1] < 0) {
381                 /* nobody there */
382                 return;
383         }
384
385         /* if no hdlc */
386         if (bchannel->b_mode == 0 || bchannel->b_mode == 1) {
387                 d = data;
388                 for (i = 0; i < len; i++) {
389                         *d = flip_bits[*d];
390                         d++;
391                 }
392         }
393
394         
395         len = write(bchannel->call->pipe[1], data, len);
396         if (len < 0)
397                 goto errout;
398
399         return;
400  errout:
401         close(bchannel->call->pipe[1]);
402         bchannel->call->pipe[1] = -1;
403         CDEBUG(bchannel->call, NULL, "broken pipe on bchannel pipe\n");
404 }
405
406
407 /*
408  * transmit data to bchannel
409  */
410 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
411 {
412         unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
413         struct mISDNhead *frm = (struct mISDNhead *)buff;
414         int ret;
415         int i;
416         int space, in;
417
418         if (bchannel->b_state != BSTATE_ACTIVE)
419                 return;
420         if (len > 1024 || len < 1)
421                 return;
422         if (data) {
423                 switch(bchannel->b_mode) {
424                 case 0:
425                         for (i = 0; i < len; i++)
426                                 *p++ = flip_bits[*data++];
427                         frm->prim = DL_DATA_REQ;
428                         break;
429                 case 1:
430                         for (i = 0; i < len; i++)
431                                 *p++ = flip_bits[*data++];
432                         frm->prim = PH_DATA_REQ;
433                         break;
434                 case 2:
435                         memcpy(p, data, len);
436                         frm->prim = DL_DATA_REQ;
437                         break;
438                 case 3:
439                         memcpy(p, data, len);
440                         frm->prim = PH_DATA_REQ;
441                         break;
442                 }
443         } else
444                 memset(p, flip_bits[(options.law=='a')?0x2a:0xff], len);
445         frm->id = 0;
446 #ifdef SEAMLESS_TEST
447         unsigned char test_tone[8] = {0x2a, 0x24, 0xb4, 0x24, 0x2a, 0x25, 0xb5, 0x25};
448         p = buff + MISDN_HEADER_LEN;
449         for (i = 0; i < len; i++)
450                 *p++ = test_tone[(bchannel->test + i) & 7];
451         bchannel->test = (bchannel->test + len) & 7;
452 #endif
453         if (bchannel->nodsp_queue) {
454                 space = (bchannel->nodsp_queue_out - bchannel->nodsp_queue_in - 1) & (QUEUE_BUFFER_SIZE - 1);
455                 if (len > space) {
456                         CERROR(bchannel->call, NULL, "Queue buffer overflow, space is %d, len is %d.\n", space, len);
457                         return;
458                 }
459                 p = buff + MISDN_HEADER_LEN;
460                 in = bchannel->nodsp_queue_in;
461                 for (i = 0; i < len; i++) {
462                         bchannel->nodsp_queue_buffer[in] = *p++;
463                         in = (in + 1) & (QUEUE_BUFFER_SIZE - 1);
464                 }
465                 bchannel->nodsp_queue_in = in;
466                 if (bchannel->queue_sent == 0) /* if there is no pending data */
467                         bchannel_send_queue(bchannel);
468                 return;
469         }
470         ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
471         if (ret < 0)
472                 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
473 }
474
475
476 /*
477  * in case of a send queue, we send from that queue rather directly
478  */
479 static void bchannel_send_queue(struct bchannel *bchannel)
480 {
481         unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
482         struct mISDNhead *frm = (struct mISDNhead *)buff;
483         int ret;
484         int i;
485         int len, out;
486
487         len = (bchannel->nodsp_queue_in - bchannel->nodsp_queue_out) & (QUEUE_BUFFER_SIZE - 1);
488         if (len == 0)
489                 return; /* mISDN driver received all load */
490 #if 0
491         printf("%4d:(%s|%s)\n", bchannel->nodsp_queue_out,
492                 "----------------------------------------------------------------"+64-len/(8192/64),
493                 "                                                                "+len/(8192/64));
494 #endif
495         if (len > 1024)
496                 len = 1024;
497         frm->prim = PH_DATA_REQ;
498         frm->id = 0;
499         out = bchannel->nodsp_queue_out;
500         for (i = 0; i < len; i++) {
501                 *p++ = bchannel->nodsp_queue_buffer[out];
502                 out = (out + 1) & (QUEUE_BUFFER_SIZE - 1);
503         }
504         bchannel->nodsp_queue_out = out;
505         ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
506         if (ret < 0)
507                 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
508         else
509                 bchannel->queue_sent = 1;
510 }
511
512
513 /*
514  * join bchannel
515  */
516 void bchannel_join(struct bchannel *bchannel, unsigned short id)
517 {
518         int sock;
519
520         sock = bchannel->b_sock;
521         if (id) {
522                 bchannel->b_conf = (id<<16) + bchannel_pid;
523                 bchannel->b_rxoff = 1;
524         } else {
525                 bchannel->b_conf = 0;
526                 bchannel->b_rxoff = 0;
527         }
528         if (bchannel->b_state == BSTATE_ACTIVE) {
529                 ph_control(sock, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf, bchannel->b_mode);
530                 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
531         }
532 }
533
534
535 /*
536  * dtmf bchannel
537  */
538 void bchannel_dtmf(struct bchannel *bchannel, int on)
539 {
540         int sock;
541
542         sock = bchannel->b_sock;
543         bchannel->b_dtmf = on;
544         if (bchannel->b_state == BSTATE_ACTIVE && bchannel->b_mode == 0)
545                 ph_control(sock, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1, bchannel->b_mode);
546 }
547
548
549 /*
550  * blowfish bchannel
551  */
552 void bchannel_blowfish(struct bchannel *bchannel, unsigned char *key, int len)
553 {
554         int sock;
555
556         sock = bchannel->b_sock;
557         memcpy(bchannel->b_bf_key, key, len);
558         bchannel->b_bf_len = len;
559         if (bchannel->b_state == BSTATE_ACTIVE)
560                 ph_control_block(sock, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
561 }
562
563
564 /*
565  * pipeline bchannel
566  */
567 void bchannel_pipeline(struct bchannel *bchannel, char *pipeline)
568 {
569         int sock;
570
571         sock = bchannel->b_sock;
572         strncpy(bchannel->b_pipeline, pipeline, sizeof(bchannel->b_pipeline)-1);
573         if (bchannel->b_state == BSTATE_ACTIVE)
574                 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
575 }
576
577
578 /*
579  * gain bchannel
580  */
581 void bchannel_gain(struct bchannel *bchannel, int gain, int tx)
582 {
583         int sock;
584
585         sock = bchannel->b_sock;
586         if (tx)
587                 bchannel->b_tx_gain = gain;
588         else
589                 bchannel->b_rx_gain = gain;
590         if (bchannel->b_state == BSTATE_ACTIVE && bchannel->b_mode == 0)
591                 ph_control(sock, (tx)?DSP_VOL_CHANGE_TX:DSP_VOL_CHANGE_RX, gain, (tx)?"DSP-TX_GAIN":"DSP-RX_GAIN", gain, bchannel->b_mode);
592 }
593
594
595 /*
596  * main loop for processing messages from mISDN
597  */
598 static int bchannel_handle(struct lcr_fd *fd, unsigned int what, void *instance, int index)
599 {
600         struct bchannel *bchannel = (struct bchannel *)instance;
601         int ret;
602         unsigned char buffer[2048+MISDN_HEADER_LEN];
603         struct mISDNhead *hh = (struct mISDNhead *)buffer;
604
605         ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
606         if (ret >= (int)MISDN_HEADER_LEN) {
607                 switch(hh->prim) {
608                         /* after a confim, we can send more from queue */
609                         case PH_DATA_CNF:
610                         if (bchannel->nodsp_queue) {
611                                 bchannel->queue_sent = 0;
612                                 bchannel_send_queue(bchannel);
613                         }
614                         break;
615
616                         /* we receive audio data, we respond to it AND we send tones */
617                         case PH_DATA_IND:
618                         case PH_DATA_REQ:
619                         case DL_DATA_IND:
620                         case PH_CONTROL_IND:
621                         bchannel_receive(bchannel, buffer, ret-MISDN_HEADER_LEN);
622                         break;
623
624                         case PH_ACTIVATE_IND:
625                         case DL_ESTABLISH_IND:
626                         case PH_ACTIVATE_CNF:
627                         case DL_ESTABLISH_CNF:
628                         CDEBUG(bchannel->call, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
629                         bchannel_activated(bchannel);
630                         break;
631
632                         case PH_DEACTIVATE_IND:
633                         case DL_RELEASE_IND:
634                         case PH_DEACTIVATE_CNF:
635                         case DL_RELEASE_CNF:
636                         CDEBUG(bchannel->call, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
637 //                                      bchannel_deactivated(bchannel);
638                         break;
639
640                         default:
641                         CERROR(bchannel->call, NULL, "child message not handled: prim(0x%x) socket(%d) data len(%d)\n", hh->prim, bchannel->b_sock, ret - MISDN_HEADER_LEN);
642                 }
643         } else {
644 //              if (ret < 0 && errno != EWOULDBLOCK)
645                 CERROR(bchannel->call, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
646         }
647
648         /* if we received at least one b-frame, we will return 1 */
649         return 0;
650 }
651
652
653 /*
654  * bchannel channel handling
655  */
656 struct bchannel *bchannel_first = NULL;
657 struct bchannel *find_bchannel_handle(unsigned int handle)
658 {
659         struct bchannel *bchannel = bchannel_first;
660
661         while(bchannel) {
662                 if (bchannel->handle == handle)
663                         break;
664                 bchannel = bchannel->next;
665         }
666         return bchannel;
667 }
668
669 #if 0
670 struct bchannel *find_bchannel_ref(unsigned int ref)
671 {
672         struct bchannel *bchannel = bchannel_first;
673
674         while(bchannel) {
675                 if (bchannel->ref == ref)
676                         break;
677                 bchannel = bchannel->next;
678         }
679         return bchannel;
680 }
681 #endif
682
683 struct bchannel *alloc_bchannel(unsigned int handle)
684 {
685         struct bchannel **bchannelp = &bchannel_first;
686
687         while(*bchannelp)
688                 bchannelp = &((*bchannelp)->next);
689
690         *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
691         if (!*bchannelp)
692                 return NULL;
693         (*bchannelp)->handle = handle;
694         (*bchannelp)->b_state = BSTATE_IDLE;
695         (*bchannelp)->b_sock = -1;
696                 
697         return *bchannelp;
698 }
699
700 void free_bchannel(struct bchannel *bchannel)
701 {
702         struct bchannel **temp = &bchannel_first;
703
704         while(*temp) {
705                 if (*temp == bchannel) {
706                         *temp = (*temp)->next;
707                         if (bchannel->b_sock > -1)
708                                 bchannel_destroy(bchannel);
709                         if (bchannel->call) {
710                                 if (bchannel->call->bchannel)
711                                         bchannel->call->bchannel = NULL;
712                         }
713                         free(bchannel);
714                         return;
715                 }
716                 temp = &((*temp)->next);
717         }
718 }
719
720