chan_lcr work
[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         /* open socket */
214         channel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
215         if (bchannel->b_sock < 0)
216         {
217                 PERROR("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                 PERROR("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                 PERROR("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                 PERROR("Error: stack already created for address 0x%lx\n", bchannel->b_stid);
257                 return(0);
258         }
259
260         if (bchannel->b_addr)
261         {
262                 PERROR("Error: stack already created for address 0x%lx\n", bchannel->b_addr);
263                 return(0);
264         }
265
266         /* create new layer */
267         PDEBUG("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                 PERROR("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         PDEBUG("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                 PERROR("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                 PERROR("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)?CMX_TXDATA_ON:CMX_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
384         if (bchannel->b_delay)
385                 ph_control(handle, CMX_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
386         if (bchannel->b_tx_dejitter)
387                 ph_control(handle, (bchannel->b_tx_dejitter)?CMX_TX_DEJITTER:CMX_TX_DEJ_OFF, 0, "DSP-DELAY", bchannel->b_tx_dejitter);
388         if (bchannel->b_tx_gain)
389                 ph_control(handle, VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
390         if (bchannel->b_rx_gain)
391                 ph_control(handle, 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, CMX_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
396         if (bchannel->b_echo)
397                 ph_control(handle, CMX_ECHO_ON, 0, "DSP-ECHO", 1);
398         if (bchannel->b_tone)
399                 ph_control(handle, TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
400         if (bchannel->b_rxoff)
401                 ph_control(handle, CMX_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
402 //      if (bchannel->b_txmix)
403 //              ph_control(handle, CMX_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, 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, CMX_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                 PDEBUG("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                         PERROR("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                                 PDEBUG("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                 PERROR("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                 PDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
550                 return;
551         }
552
553         if (bchannel->rx_data)
554                 bchannel->rx_data(bchannel, data, len);
555 }
556
557
558 /*
559  * transmit data to bchannel
560  */
561 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
562 {
563         unsigned char buff[1025];
564         iframe_t *frm = (iframe_t *)buff;
565
566         if (bchannel->b_state != BSTATE_ACTIVE)
567                 return;
568 #ifdef SOCKET_MISDN
569         frm->prim = DL_DATA_REQ;
570         frm->id = 0;
571         ret = sendto(bchannel->b_sock, data, len, 0, NULL, 0);
572         if (!ret)
573                 PERROR("Failed to send to socket %d\n", bchannel->b_sock);
574 #else
575         frm->prim = DL_DATA | REQUEST; 
576         frm->addr = bchannel->b_addr | FLG_MSG_DOWN;
577         frm->dinfo = 0;
578         frm->len = len;
579         if (frm->len)
580                 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
581 #endif
582 }
583
584
585 /*
586  * join bchannel
587  */
588 void bchannel_join(struct bchannel *bchannel, unsigned short id)
589 {
590 #ifdef SOCKET_MISDN
591         int handle;
592
593         handle = bchannel->b_sock;
594 #else
595         unsigned long handle;
596
597         handle = bchannel->b_addr;
598 #endif
599         if (id)
600                 bchannel->b_conf = (id<<16) + bchannel_pid;
601         else
602                 bchannel->b_conf = 0;
603         if (bchannel->b_state == BSTATE_ACTIVE)
604                 ph_control(handle, CMX_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
605 }
606
607
608 /*
609  * main loop for processing messages from mISDN
610  */
611 #ifdef SOCKET_MISDN
612 int bchannel_handle(void)
613 {
614         int ret, work = 0;
615         struct bchannel *bchannel;
616         int i;
617         char buffer[2048+MISDN_HEADER_LEN];
618         struct mISDNhead *hh = (struct mISDNhead *)buffer;
619
620         /* process all bchannels */
621         bchannel = bchannel_first;
622         while(bchannel)
623         {
624                 /* handle message from bchannel */
625                 if (bchannel->b_sock > -1)
626                 {
627                         ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
628                         if (ret >= MISDN_HEADER_LEN)
629                         {
630                                 work = 1;
631                                 switch(hh->prim)
632                                 {
633                                         /* we don't care about confirms, we use rx data to sync tx */
634                                         case PH_DATA_CONF:
635                                         case DL_DATA_CONF:
636                                         break;
637
638                                         /* we receive audio data, we respond to it AND we send tones */
639                                         case PH_DATA_IND:
640                                         case DL_DATA_IND:
641                                         case PH_SIGNAL_IND:
642                                         case PH_CONTROL | INDICATION:
643                                         bchannel_receive(bchannel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
644                                         break;
645
646                                         case PH_ACTIVATE_IND:
647                                         case DL_ESTABLISH_IND:
648                                         case PH_ACTIVATE_CONF:
649                                         case DL_ESTABLISH_CONF:
650                                         PDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
651                                         bchannel_activated(bchannel);
652                                         break;
653
654                                         case PH_DEACTIVATE_IND:
655                                         case DL_RELEASE_IND:
656                                         case PH_DEACTIVATE_CONF:
657                                         case DL_RELEASE_CONF:
658                                         PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
659 //                                      bchannel_deactivated(bchannel);
660                                         break;
661
662                                         default:
663                                         PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, bchannel->b_sock, msg->len);
664                                 }
665                         } else
666                         {
667                                 if (ret < 0 && errno != EWOULDBLOCK)
668                                         PERROR("Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
669                         }
670                 }
671                 bchannel = bchannel->next;
672         }
673
674         /* if we received at least one b-frame, we will return 1 */
675         return(work);
676 }
677 #else
678 int bchannel_handle(void)
679 {
680         struct bchannel *bchannel;
681         iframe_t *frm;
682         unsigned char buffer[2048];
683         int len;
684
685         /* no device, no read */
686         if (bchannel_device < 0)
687                 return(0);
688
689         /* get message from kernel */
690         len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
691         if (len < 0)
692         {
693                 if (errno == EAGAIN)
694                         return(0);
695                 PERROR("Failed to do mISDN_read()\n");
696                 return(0);
697         }
698         if (!len)
699         {
700 //              printf("%s: ERROR: mISDN_read() returns nothing\n");
701                 return(0);
702         }
703         frm = (iframe_t *)buffer;
704
705         /* global prim */
706         switch(frm->prim)
707         {
708                 case MGR_DELLAYER | CONFIRM:
709                 case MGR_INITTIMER | CONFIRM:
710                 case MGR_ADDTIMER | CONFIRM:
711                 case MGR_DELTIMER | CONFIRM:
712                 case MGR_REMOVETIMER | CONFIRM:
713                 return(1);
714         }
715
716         /* find the mISDNport that belongs to the stack */
717         bchannel = bchannel_first;
718         while(bchannel)
719         {
720                 if (frm->addr == bchannel->b_addr)
721                         break;
722                 bchannel = bchannel->next;
723         } 
724         if (!bchannel)
725         {
726                 PERROR("message belongs to no bchannel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
727                 goto out;
728         }
729
730         /* b-message */
731         switch(frm->prim)
732         {
733                 /* we don't care about confirms, we use rx data to sync tx */
734                 case PH_DATA | CONFIRM:
735                 case DL_DATA | CONFIRM:
736                 break;
737
738                 /* we receive audio data, we respond to it AND we send tones */
739                 case PH_DATA | INDICATION:
740                 case DL_DATA | INDICATION:
741                 case PH_CONTROL | INDICATION:
742                 case PH_SIGNAL | INDICATION:
743                 bchannel_receive(bchannel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
744                 break;
745
746                 case PH_ACTIVATE | INDICATION:
747                 case DL_ESTABLISH | INDICATION:
748                 case PH_ACTIVATE | CONFIRM:
749                 case DL_ESTABLISH | CONFIRM:
750                 PDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
751                 bchannel_activated(bchannel);
752                 break;
753
754                 case PH_DEACTIVATE | INDICATION:
755                 case DL_RELEASE | INDICATION:
756                 case PH_DEACTIVATE | CONFIRM:
757                 case DL_RELEASE | CONFIRM:
758                 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
759 //              bchannel_deactivated(bchannel);
760                 break;
761
762                 default:
763                 PERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
764         }
765
766         out:
767         return(1);
768 }
769 #endif
770
771
772 /*
773  * bchannel channel handling
774  */
775 struct bchannel *bchannel_first = NULL;
776 struct bchannel *find_bchannel_handle(unsigned long handle)
777 {
778         struct bchannel *bchannel = bchannel_first;
779
780         while(bchannel)
781         {
782                 if (bchannel->handle == handle)
783                         break;
784                 bchannel = bchannel->next;
785         }
786         return(bchannel);
787 }
788
789 #if 0
790 struct bchannel *find_bchannel_ref(unsigned long ref)
791 {
792         struct bchannel *bchannel = bchannel_first;
793
794         while(bchannel)
795         {
796                 if (bchannel->ref == ref)
797                         break;
798                 bchannel = bchannel->next;
799         }
800         return(bchannel);
801 }
802 #endif
803
804 struct bchannel *alloc_bchannel(unsigned long handle)
805 {
806         struct bchannel **bchannelp = &bchannel_first;
807
808         while(*bchannelp)
809                 bchannelp = &((*bchannelp)->next);
810
811         *bchannelp = (struct bchannel *)malloc(sizeof(struct bchannel));
812         if (!*bchannelp)
813                 return(NULL);
814         (*bchannelp)->handle = handle;
815         (*bchannelp)->b_state = BSTATE_IDLE;
816                 
817         return(*bchannelp);
818 }
819
820 void free_bchannel(struct bchannel *bchannel)
821 {
822         struct bchannel **temp = &bchannel_first;
823
824         while(*temp)
825         {
826                 if (*temp == bchannel)
827                 {
828                         *temp = (*temp)->next;
829 #ifdef SOCKET_MISDN
830                         if (bchannel->b_sock > -1)
831 #else
832                         if (bchannel->b_stid)
833 #endif
834                                 bchannel_destroy(bchannel);
835                         if (bchannel->call)
836                         {
837                                 if (bchannel->call->bchannel)
838                                         bchannel->call->bchannel = NULL;
839                         }
840                         free(bchannel);
841                         return;
842                 }
843                 temp = &((*temp)->next);
844         }
845 }
846
847