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