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