work on 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 #ifdef SOCKET_MISDN
23 #include <netinet/udp.h>
24 #include <netinet/in.h>
25 #include <netdb.h>
26 #include <sys/socket.h>
27 #else
28 #include <mISDNuser/isdn_net.h>
29 #include <mISDNuser/net_l3.h>
30 #endif
31
32 #include "extension.h"
33 #include "message.h"
34 #include "lcrsocket.h"
35 #include "cause.h"
36 #include "bchannel.h"
37 #include "chan_lcr.h"
38 #include "callerid.h"
39
40
41 #ifndef ISDN_PID_L4_B_USER
42 #define ISDN_PID_L4_B_USER 0x440000ff
43 #endif
44
45 #define PERROR(arg...) fprintf(stderr, ##arg)
46 #define PDEBUG(arg...) while(0)
47
48 pid_t   bchannel_pid;
49
50 enum {
51         BSTATE_IDLE,
52         BSTATE_ACTIVATING,
53         BSTATE_ACTIVE,
54 };
55
56 #ifdef SOCKET_MISDN
57
58 int bchannel_initialize(void)
59 {
60         return(0);
61 }
62
63 void bchannel_deinitialize(void)
64 {
65 }
66 #else
67 int bchannel_entity = 0; /* used for udevice */
68 int bchannel_device = -1; /* the device handler and port list */
69
70 int bchannel_initialize(void)
71 {
72         unsigned char buff[1025];
73         iframe_t *frm = (iframe_t *)buff;
74         int ret;
75
76         /* open mISDNdevice if not already open */
77         if (bchannel_device < 0)
78         {
79                 ret = mISDN_open();
80                 if (ret < 0)
81                 {
82                         PERROR("cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", ret, errno, strerror(errno));
83                         return(-1);
84                 }
85                 bchannel_device = ret;
86                 PDEBUG("mISDN device opened.\n");
87
88                 /* create entity for layer 3 TE-mode */
89                 mISDN_write_frame(bchannel_device, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
90                 ret = mISDN_read_frame(bchannel_device, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
91                 if (ret < (int)mISDN_HEADER_LEN)
92                 {
93                         noentity:
94                         PERROR("Cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
95                         return(-1);
96                 }
97                 bchannel_entity = frm->dinfo & 0xffff;
98                 if (!bchannel_entity)
99                         goto noentity;
100         }
101         return(0);
102 }
103
104 void bchannel_deinitialize(void)
105 {
106         unsigned char buff[1025];
107
108         if (bchannel_device >= 0)
109         {
110                 /* free entity */
111                 mISDN_write_frame(bchannel_device, buff, 0, MGR_DELENTITY | REQUEST, bchannel_entity, 0, NULL, TIMEOUT_1SEC);
112                 /* close device */
113                 mISDN_close(bchannel_device);
114                 bchannel_device = -1;
115         }
116 }
117 #endif
118
119 /*
120  * send control information to the channel (dsp-module)
121  */
122 static void ph_control(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
123 {
124 #ifdef SOCKET_MISDN
125         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
126         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
127         unsigned long *d = buffer+MISDN_HEADER_LEN;
128         int ret;
129
130         ctrl->prim = PH_CONTROL_REQ;
131         ctrl->id = 0;
132         *d++ = c1;
133         *d++ = c2;
134         ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
135         if (!ret)
136                 PERROR("Failed to send to socket %d\n", handle);
137 #else
138         unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
139         iframe_t *ctrl = (iframe_t *)buffer; 
140         unsigned long *d = (unsigned long *)&ctrl->data.p;
141
142         ctrl->prim = PH_CONTROL | REQUEST;
143         ctrl->addr = handle | FLG_MSG_DOWN;
144         ctrl->dinfo = 0;
145         ctrl->len = sizeof(int)*2;
146         *d++ = c1;
147         *d++ = c2;
148         mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
149 #endif
150 #if 0
151         chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
152         if (c1 == CMX_CONF_JOIN)
153                 add_trace(trace_name, NULL, "0x%08x", trace_value);
154         else
155                 add_trace(trace_name, NULL, "%d", trace_value);
156         end_trace();
157 #endif
158 }
159
160 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
161 {
162 #ifdef SOCKET_MISDN
163         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
164         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
165         unsigned long *d = buffer+MISDN_HEADER_LEN;
166         int ret;
167
168         ctrl->prim = PH_CONTROL_REQ;
169         ctrl->id = 0;
170         *d++ = c1;
171         memcpy(d, c2, c2_len);
172         ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
173         if (!ret)
174                 PERROR("Failed to send to socket %d\n", handle);
175 #else
176         unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
177         iframe_t *ctrl = (iframe_t *)buffer;
178         unsigned long *d = (unsigned long *)&ctrl->data.p;
179
180         ctrl->prim = PH_CONTROL | REQUEST;
181         ctrl->addr = handle | FLG_MSG_DOWN;
182         ctrl->dinfo = 0;
183         ctrl->len = sizeof(int)+c2_len;
184         *d++ = c1;
185         memcpy(d, c2, c2_len);
186         mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
187 #endif
188 #if 0
189         chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
190         add_trace(trace_name, NULL, "%d", trace_value);
191         end_trace();
192 #endif
193 }
194
195
196 /*
197  * create stack
198  */
199 int bchannel_create(struct bchannel *bchannel)
200 {
201         unsigned char buff[1024];
202         int ret;
203 #ifdef SOCKET_MISDN
204         unsigned long on = 1;
205         struct sockadd_mISDN addr;
206
207         if (bchannel->b_sock)
208         {
209                 PERROR("Error: Socket already created for handle 0x%x\n", bchannel->handle);
210                 return(0);
211         }
212
213         /* default tx_dejitter */
214         bchannel->b_tx_dejitter = 1;
215
216         /* open socket */
217         channel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
218         if (bchannel->b_sock < 0)
219         {
220                 PERROR("Error: Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
221                 return(0);
222         }
223         
224         /* set nonblocking io */
225         ret = ioctl(bchannel->b_sock, FIONBIO, &on);
226         if (ret < 0)
227         {
228                 PERROR("Error: Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
229                 close(bchannel->b_sock);
230                 channel->b_sock = -1;
231                 return(0);
232         }
233
234         /* bind socket to bchannel */
235         addr.family = AF_ISDN;
236         addr.dev = (bchannel->handle>>8)-1;
237         addr.channel = bchannel->handle && 0xff;
238         ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
239         if (ret < 0)
240         {
241                 PERROR("Error: Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
242                 close(bchannel->b_sock);
243                 channel->b_sock = -1;
244                 return(0);
245         }
246
247 #if 0
248         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
249         add_trace("channel", NULL, "%d", i+1+(i>=15));
250         add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
251         end_trace();
252 #endif
253 #else
254         layer_info_t li;
255         mISDN_pid_t pid;
256
257         if (bchannel->b_stid)
258         {
259                 PERROR("Error: stack already created for address 0x%lx\n", bchannel->b_stid);
260                 return(0);
261         }
262
263         if (bchannel->b_addr)
264         {
265                 PERROR("Error: stack already created for address 0x%lx\n", bchannel->b_addr);
266                 return(0);
267         }
268
269         /* create new layer */
270         PDEBUG("creating new layer for stid 0x%lx.\n" , bchannel->handle);
271         memset(&li, 0, sizeof(li));
272         memset(&pid, 0, sizeof(pid));
273         li.object_id = -1;
274         li.extentions = 0;
275         li.st = bchannel->handle;
276         strcpy(li.name, "B L4");
277         li.pid.layermask = ISDN_LAYER((4));
278         li.pid.protocol[4] = ISDN_PID_L4_B_USER;
279         ret = mISDN_new_layer(bchannel_device, &li);
280         if (ret)
281         {
282                 failed_new_layer:
283                 PERROR("mISDN_new_layer() failed to add bchannel for stid 0x%lx.\n", bchannel->handle);
284                 goto failed;
285         }
286         if (!li.id)
287         {
288                 goto failed_new_layer;
289         }
290         bchannel->b_stid = bchannel->handle;
291         bchannel->b_addr = li.id;
292         PDEBUG("new layer (b_addr=0x%x)\n", bchannel->b_addr);
293
294         /* create new stack */
295         pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
296         pid.protocol[2] = ISDN_PID_L2_B_TRANS;
297         pid.protocol[3] = ISDN_PID_L3_B_DSP;
298         pid.protocol[4] = ISDN_PID_L4_B_USER;
299         pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
300         ret = mISDN_set_stack(bchannel_device, bchannel->b_stid, &pid);
301         if (ret)
302         {
303                 stack_error:
304                 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel stid=0x%lx\n", ret, bchannel->b_stid);
305                 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
306                 goto failed;
307         }
308         ret = mISDN_get_setstack_ind(bchannel_device, bchannel->b_addr);
309         if (ret)
310                 goto stack_error;
311
312         /* get layer id */
313         bchannel->b_addr = mISDN_get_layerid(bchannel_device, bchannel->b_stid, 4);
314         if (!bchannel->b_addr)
315                 goto stack_error;
316 #if 0
317         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create stack", DIRECTION_OUT);
318         add_trace("channel", NULL, "%d", i+1+(i>=15));
319         add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
320         add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
321         end_trace();
322 #endif
323 #endif
324
325         return(1);
326
327 failed:
328         bchannel->b_stid = 0;
329         bchannel->b_addr = 0;
330         return(0);
331 }
332
333
334 /*
335  * activate / deactivate request
336  */
337 void bchannel_activate(struct bchannel *bchannel, int activate)
338 {
339 #ifdef SOCKET_MISDN
340         struct mISDNhead act;
341         int ret;
342
343         act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ; 
344         act.id = 0;
345         ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
346         if (!ret)
347                 PERROR("Failed to send to socket %d\n", bchannel->b_sock);
348 #else
349         iframe_t act;
350
351         /* activate bchannel */
352         act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST; 
353         act.addr = bchannel->b_addr | FLG_MSG_DOWN;
354         act.dinfo = 0;
355         act.len = 0;
356         mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
357 #endif
358
359         bchannel->b_state = BSTATE_ACTIVATING;
360 #if 0
361         /* trace */
362         chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
363         add_trace("channel", NULL, "%d", i+1+(i>=15));
364         end_trace();
365 #endif
366 }
367
368
369 /*
370  * set features
371  */
372 static void bchannel_activated(struct bchannel *bchannel)
373 {
374 #ifdef SOCKET_MISDN
375         int handle;
376
377         handle = bchannel->b_sock;
378 #else
379         unsigned long handle;
380
381         handle = bchannel->b_addr;
382 #endif
383
384         /* set dsp features */
385         if (bchannel->b_txdata)
386                 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
387         if (bchannel->b_delay)
388                 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
389         if (bchannel->b_tx_dejitter)
390                 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
391         if (bchannel->b_tx_gain)
392                 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
393         if (bchannel->b_rx_gain)
394                 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
395         if (bchannel->b_pipeline[0])
396                 ph_control_block(handle, PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
397         if (bchannel->b_conf)
398                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
399         if (bchannel->b_echo)
400                 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
401         if (bchannel->b_tone)
402                 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
403         if (bchannel->b_rxoff)
404                 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
405 //      if (bchannel->b_txmix)
406 //              ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
407         if (bchannel->b_dtmf)
408                 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
409         if (bchannel->b_crypt_len)
410                 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
411         if (bchannel->b_conf)
412                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
413
414         bchannel->b_state = BSTATE_ACTIVE;
415 }
416
417 /*
418  * destroy stack
419  */
420 static void bchannel_destroy(struct bchannel *bchannel)
421 {
422 #ifdef SOCKET_MISDN
423 #if 0
424         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
425         add_trace("channel", NULL, "%d", i+1+(i>=15));
426         add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
427         end_trace();
428 #endif
429         if (bchannel->b_sock > -1)
430         {
431                 close(bchannel->b_sock);
432                 bchannel->b_sock = -1;
433         }
434 #else
435         unsigned char buff[1024];
436
437 #if 0
438         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
439         add_trace("channel", NULL, "%d", i+1+(i>=15));
440         add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
441         add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
442         end_trace();
443 #endif
444         /* remove our stack only if set */
445         if (bchannel->b_addr)
446         {
447                 PDEBUG("free stack (b_addr=0x%x)\n", bchannel->b_addr);
448                 mISDN_clear_stack(bchannel_device, bchannel->b_stid);
449                 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
450                 bchannel->b_stid = 0;
451                 bchannel->b_addr = 0;
452         }
453 #endif
454         bchannel->b_state = BSTATE_IDLE;
455 }
456
457
458 /*
459  * whenever we get audio data from bchannel, we process it here
460  */
461 static void bchannel_receive(struct bchannel *bchannel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
462 {
463         unsigned long cont = *((unsigned long *)data);
464 //      unsigned char *data_temp;
465 //      unsigned long length_temp;
466 //      unsigned char *p;
467 //      int l;
468
469         if (prim == (PH_CONTROL | INDICATION))
470         {
471                 if (len < 4)
472                 {
473                         PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
474                         return;
475                 }
476                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
477                 {
478 #if 0
479                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
480                         add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
481                         end_trace();
482 #endif
483                         if (bchannel->rx_dtmf)
484                                 bchannel->rx_dtmf(bchannel, cont & DTMF_TONE_MASK);
485                         return;
486                 }
487                 switch(cont)
488                 {
489                         case BF_REJECT:
490 #if 0
491                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
492                         add_trace("DSP-CRYPT", NULL, "error");
493                         end_trace();
494 #endif
495                         break;
496
497                         case BF_ACCEPT:
498 #if 0
499                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
500                         add_trace("DSP-CRYPT", NULL, "ok");
501                         end_trace();
502 #endif
503                         break;
504
505                         default:
506 #if 0
507                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
508                         add_trace("unknown", NULL, "0x%x", cont);
509                         end_trace();
510 #else
511                         ;
512 #endif
513                 }
514                 return;
515         }
516         if (prim == (PH_SIGNAL | INDICATION))
517         {
518                 switch(dinfo)
519                 {
520                         case CMX_TX_DATA:
521                         if (!bchannel->b_txdata)
522                         {
523                                 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
524                                 PDEBUG("PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
525                                 return;
526                         }
527                         break;
528
529                         default:
530 #if 0
531                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
532                         add_trace("unknown", NULL, "0x%x", frm->dinfo);
533                         end_trace();
534 #else
535                         ;
536 #endif
537                 }
538                 return;
539         }
540         if (prim != PH_DATA_IND && prim != DL_DATA_IND)
541         {
542                 PERROR("Bchannel received unknown primitve: 0x%lx\n", prim);
543                 return;
544         }
545         /* calls will not process any audio data unless
546          * the call is connected OR interface features audio during call setup.
547          */
548
549         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
550         if (bchannel->b_rxoff)
551         {
552                 PDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
553                 return;
554         }
555
556         if (!bchannel->call)
557         {
558                 PDEBUG("PmISDN(%s) ignoring data, because no call associated with bchannel\n", p_name);
559                 return;
560         }
561         len = write(bchannel->call->pipe[1], data, len);
562         if (len < 0)
563         {
564                 PDEBUG("PmISDN(%s) broken pipe on bchannel pipe\n", p_name);
565                 return;
566         }
567 }
568
569
570 /*
571  * transmit data to bchannel
572  */
573 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
574 {
575         unsigned char buff[1025];
576         iframe_t *frm = (iframe_t *)buff;
577
578         if (bchannel->b_state != BSTATE_ACTIVE)
579                 return;
580 #ifdef SOCKET_MISDN
581         frm->prim = DL_DATA_REQ;
582         frm->id = 0;
583         ret = sendto(bchannel->b_sock, data, len, 0, NULL, 0);
584         if (!ret)
585                 PERROR("Failed to send to socket %d\n", bchannel->b_sock);
586 #else
587         frm->prim = DL_DATA | REQUEST; 
588         frm->addr = bchannel->b_addr | FLG_MSG_DOWN;
589         frm->dinfo = 0;
590         frm->len = len;
591         if (frm->len)
592                 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
593 #endif
594 }
595
596
597 /*
598  * join bchannel
599  */
600 void bchannel_join(struct bchannel *bchannel, unsigned short id)
601 {
602 #ifdef SOCKET_MISDN
603         int handle;
604
605         handle = bchannel->b_sock;
606 #else
607         unsigned long handle;
608
609         handle = bchannel->b_addr;
610 #endif
611         if (id) {
612                 bchannel->b_conf = (id<<16) + bchannel_pid;
613                 bchannel->b_rxoff = 1;
614         } else {
615                 bchannel->b_conf = 0;
616                 bchannel->b_rxoff = 0;
617         }
618         if (bchannel->b_state == BSTATE_ACTIVE)
619         {
620                 ph_control(handle, DSP_RX_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
621                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
622         }
623 }
624
625
626 /*
627  * main loop for processing messages from mISDN
628  */
629 #ifdef SOCKET_MISDN
630 int bchannel_handle(void)
631 {
632         int ret, work = 0;
633         struct bchannel *bchannel;
634         int i;
635         char buffer[2048+MISDN_HEADER_LEN];
636         struct mISDNhead *hh = (struct mISDNhead *)buffer;
637
638         /* process all bchannels */
639         bchannel = bchannel_first;
640         while(bchannel)
641         {
642                 /* handle message from bchannel */
643                 if (bchannel->b_sock > -1)
644                 {
645                         ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
646                         if (ret >= MISDN_HEADER_LEN)
647                         {
648                                 work = 1;
649                                 switch(hh->prim)
650                                 {
651                                         /* we don't care about confirms, we use rx data to sync tx */
652                                         case PH_DATA_CONF:
653                                         case DL_DATA_CONF:
654                                         break;
655
656                                         /* we receive audio data, we respond to it AND we send tones */
657                                         case PH_DATA_IND:
658                                         case DL_DATA_IND:
659                                         case PH_SIGNAL_IND:
660                                         case PH_CONTROL | INDICATION:
661                                         bchannel_receive(bchannel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
662                                         break;
663
664                                         case PH_ACTIVATE_IND:
665                                         case DL_ESTABLISH_IND:
666                                         case PH_ACTIVATE_CONF:
667                                         case DL_ESTABLISH_CONF:
668                                         PDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
669                                         bchannel_activated(bchannel);
670                                         break;
671
672                                         case PH_DEACTIVATE_IND:
673                                         case DL_RELEASE_IND:
674                                         case PH_DEACTIVATE_CONF:
675                                         case DL_RELEASE_CONF:
676                                         PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
677 //                                      bchannel_deactivated(bchannel);
678                                         break;
679
680                                         default:
681                                         PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, bchannel->b_sock, msg->len);
682                                 }
683                         } else
684                         {
685                                 if (ret < 0 && errno != EWOULDBLOCK)
686                                         PERROR("Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
687                         }
688                 }
689                 bchannel = bchannel->next;
690         }
691
692         /* if we received at least one b-frame, we will return 1 */
693         return(work);
694 }
695 #else
696 int bchannel_handle(void)
697 {
698         struct bchannel *bchannel;
699         iframe_t *frm;
700         unsigned char buffer[2048];
701         int len;
702
703         /* no device, no read */
704         if (bchannel_device < 0)
705                 return(0);
706
707         /* get message from kernel */
708         len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
709         if (len < 0)
710         {
711                 if (errno == EAGAIN)
712                         return(0);
713                 PERROR("Failed to do mISDN_read()\n");
714                 return(0);
715         }
716         if (!len)
717         {
718 //              printf("%s: ERROR: mISDN_read() returns nothing\n");
719                 return(0);
720         }
721         frm = (iframe_t *)buffer;
722
723         /* global prim */
724         switch(frm->prim)
725         {
726                 case MGR_DELLAYER | CONFIRM:
727                 case MGR_INITTIMER | CONFIRM:
728                 case MGR_ADDTIMER | CONFIRM:
729                 case MGR_DELTIMER | CONFIRM:
730                 case MGR_REMOVETIMER | CONFIRM:
731                 return(1);
732         }
733
734         /* find the mISDNport that belongs to the stack */
735         bchannel = bchannel_first;
736         while(bchannel)
737         {
738                 if (frm->addr == bchannel->b_addr)
739                         break;
740                 bchannel = bchannel->next;
741         } 
742         if (!bchannel)
743         {
744                 PERROR("message belongs to no bchannel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
745                 goto out;
746         }
747
748         /* b-message */
749         switch(frm->prim)
750         {
751                 /* we don't care about confirms, we use rx data to sync tx */
752                 case PH_DATA | CONFIRM:
753                 case DL_DATA | CONFIRM:
754                 break;
755
756                 /* we receive audio data, we respond to it AND we send tones */
757                 case PH_DATA | INDICATION:
758                 case DL_DATA | INDICATION:
759                 case PH_CONTROL | INDICATION:
760                 case PH_SIGNAL | INDICATION:
761                 bchannel_receive(bchannel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
762                 break;
763
764                 case PH_ACTIVATE | INDICATION:
765                 case DL_ESTABLISH | INDICATION:
766                 case PH_ACTIVATE | CONFIRM:
767                 case DL_ESTABLISH | CONFIRM:
768                 PDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
769                 bchannel_activated(bchannel);
770                 break;
771
772                 case PH_DEACTIVATE | INDICATION:
773                 case DL_RELEASE | INDICATION:
774                 case PH_DEACTIVATE | CONFIRM:
775                 case DL_RELEASE | CONFIRM:
776                 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
777 //              bchannel_deactivated(bchannel);
778                 break;
779
780                 default:
781                 PERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
782         }
783
784         out:
785         return(1);
786 }
787 #endif
788
789
790 /*
791  * bchannel channel handling
792  */
793 struct bchannel *bchannel_first = NULL;
794 struct bchannel *find_bchannel_handle(unsigned long handle)
795 {
796         struct bchannel *bchannel = bchannel_first;
797
798         while(bchannel)
799         {
800                 if (bchannel->handle == handle)
801                         break;
802                 bchannel = bchannel->next;
803         }
804         return(bchannel);
805 }
806
807 #if 0
808 struct bchannel *find_bchannel_ref(unsigned long ref)
809 {
810         struct bchannel *bchannel = bchannel_first;
811
812         while(bchannel)
813         {
814                 if (bchannel->ref == ref)
815                         break;
816                 bchannel = bchannel->next;
817         }
818         return(bchannel);
819 }
820 #endif
821
822 struct bchannel *alloc_bchannel(unsigned long handle)
823 {
824         struct bchannel **bchannelp = &bchannel_first;
825
826         while(*bchannelp)
827                 bchannelp = &((*bchannelp)->next);
828
829         *bchannelp = (struct bchannel *)malloc(sizeof(struct bchannel));
830         if (!*bchannelp)
831                 return(NULL);
832         (*bchannelp)->handle = handle;
833         (*bchannelp)->b_state = BSTATE_IDLE;
834                 
835         return(*bchannelp);
836 }
837
838 void free_bchannel(struct bchannel *bchannel)
839 {
840         struct bchannel **temp = &bchannel_first;
841
842         while(*temp)
843         {
844                 if (*temp == bchannel)
845                 {
846                         *temp = (*temp)->next;
847 #ifdef SOCKET_MISDN
848                         if (bchannel->b_sock > -1)
849 #else
850                         if (bchannel->b_stid)
851 #endif
852                                 bchannel_destroy(bchannel);
853                         if (bchannel->call)
854                         {
855                                 if (bchannel->call->bchannel)
856                                         bchannel->call->bchannel = NULL;
857                         }
858                         free(bchannel);
859                         return;
860                 }
861                 temp = &((*temp)->next);
862         }
863 }
864
865