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