work on asterisk channel driver
[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 MISDN_SOCKET
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 %d\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 %d 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 %d 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(di->bchan, (struct sockaddr *)&addr, sizeof(addr));
255         if (ret < 0)
256         {
257                 PERROR("Error: Failed to bind bchannel-socket for handle %d 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
428         channel->b_state = BSTATE_ACTIVE;
429 }
430
431 /*
432  * destroy stack
433  */
434 static void bchannel_destroy(struct bchannel *channel)
435 {
436 #ifdef SOCKET_MISDN
437 #if 0
438         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
439         add_trace("channel", NULL, "%d", i+1+(i>=15));
440         add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
441         end_trace();
442 #endif
443         if (channel->b_sock > -1)
444         {
445                 close(channel->b_sock);
446                 channel->b_sock = -1;
447         }
448 #else
449         unsigned char buff[1024];
450
451 #if 0
452         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
453         add_trace("channel", NULL, "%d", i+1+(i>=15));
454         add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
455         add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
456         end_trace();
457 #endif
458         /* remove our stack only if set */
459         if (channel->b_addr)
460         {
461                 PDEBUG("free stack (b_addr=0x%x)\n", channel->b_addr);
462                 mISDN_clear_stack(bchannel_device, channel->b_stid);
463                 mISDN_write_frame(bchannel_device, buff, channel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
464                 channel->b_stid = 0;
465                 channel->b_addr = 0;
466         }
467 #endif
468         channel->b_state = BSTATE_IDLE;
469 }
470
471
472 /*
473  * whenever we get audio data from bchannel, we process it here
474  */
475 static void bchannel_receive(struct bchannel *channel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
476 {
477         unsigned long cont = *((unsigned long *)data);
478         unsigned char *data_temp;
479         unsigned long length_temp;
480         unsigned char *p;
481         int l;
482
483         if (prim == (PH_CONTROL | INDICATION))
484         {
485                 if (len < 4)
486                 {
487                         PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
488                         return;
489                 }
490                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
491                 {
492 #if 0
493                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
494                         add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
495                         end_trace();
496 #endif
497                         if (channel->rx_dtmf)
498                                 channel->rx_dtmf(channel, cont & DTMF_TONE_MASK);
499                         return;
500                 }
501                 switch(cont)
502                 {
503                         case BF_REJECT:
504 #if 0
505                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
506                         add_trace("DSP-CRYPT", NULL, "error");
507                         end_trace();
508 #endif
509                         break;
510
511                         case BF_ACCEPT:
512 #if 0
513                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
514                         add_trace("DSP-CRYPT", NULL, "ok");
515                         end_trace();
516 #endif
517                         break;
518
519                         default:
520 #if 0
521                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
522                         add_trace("unknown", NULL, "0x%x", cont);
523                         end_trace();
524 #else
525                         ;
526 #endif
527                 }
528                 return;
529         }
530         if (prim == (PH_SIGNAL | INDICATION))
531         {
532                 switch(dinfo)
533                 {
534                         case CMX_TX_DATA:
535                         if (!channel->b_txdata)
536                         {
537                                 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
538                                 PDEBUG("PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
539                                 return;
540                         }
541                         break;
542
543                         default:
544 #if 0
545                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
546                         add_trace("unknown", NULL, "0x%x", frm->dinfo);
547                         end_trace();
548 #else
549                         ;
550 #endif
551                 }
552                 return;
553         }
554         if (prim != PH_DATA_IND && prim != DL_DATA_IND)
555         {
556                 PERROR("Bchannel received unknown primitve: 0x%x\n", prim);
557                 return;
558         }
559         /* calls will not process any audio data unless
560          * the call is connected OR interface features audio during call setup.
561          */
562
563         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
564         if (channel->b_rxoff)
565         {
566                 PDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
567                 return;
568         }
569
570         if (channel->rx_data)
571                 channel->rx_data(channel, data, len);
572 }
573
574
575 /*
576  * transmit data to bchannel
577  */
578 void bchannel_transmit(struct bchannel *channel, unsigned char *data, int len)
579 {
580         unsigned char buff[1025];
581         iframe_t *frm = (iframe_t *)buff;
582
583         if (channel->b_state != BSTATE_ACTIVE)
584                 return;
585 #ifdef SOCKET_MISDN
586         frm->prim = DL_DATA_REQ;
587         frm->id = 0;
588         ret = sendto(channel->b_sock, data, len, 0, NULL, 0);
589         if (!ret)
590                 PERROR("Failed to send to socket %d\n", channel->b_sock);
591 #else
592         frm->prim = DL_DATA | REQUEST; 
593         frm->addr = channel->b_addr | FLG_MSG_DOWN;
594         frm->dinfo = 0;
595         frm->len = len;
596         if (frm->len)
597                 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
598 #endif
599 }
600
601
602 /*
603  * join bchannel
604  */
605 void bchannel_join(struct bchannel *channel, unsigned short id)
606 {
607 #ifdef SOCKET_MISDN
608         int handle;
609
610         handle = channel->b_sock;
611 #else
612         unsigned long handle;
613
614         handle = channel->b_addr;
615 #endif
616         if (id)
617                 channel->b_conf = (id<<16) + bchannel_pid;
618         else
619                 channel->b_conf = 0;
620         if (channel->b_state == BSTATE_ACTIVE)
621                 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
622 }
623
624
625 /*
626  * main loop for processing messages from mISDN
627  */
628 #ifdef SOCKET_MISDN
629 int bchannel_handle(void)
630 {
631         int ret, work = 0;
632         struct bchannel *channel;
633         int i;
634         char buffer[2048+MISDN_HEADER_LEN];
635         struct mISDNhead *hh = (struct mISDNhead *)buffer;
636
637         /* process all bchannels */
638         channel = bchannel_first;
639         while(channel)
640         {
641                 /* handle message from bchannel */
642                 if (channel->b_sock > -1)
643                 {
644                         ret = recv(channel->b_sock, buffer, sizeof(buffer), 0);
645                         if (ret >= MISDN_HEADER_LEN)
646                         {
647                                 work = 1;
648                                 switch(hh->prim)
649                                 {
650                                         /* we don't care about confirms, we use rx data to sync tx */
651                                         case PH_DATA_CONF:
652                                         case DL_DATA_CONF:
653                                         break;
654
655                                         /* we receive audio data, we respond to it AND we send tones */
656                                         case PH_DATA_IND:
657                                         case DL_DATA_IND:
658                                         case PH_SIGNAL_IND:
659                                         case PH_CONTROL | INDICATION:
660                                         bchannel_receive(channel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
661                                         break;
662
663                                         case PH_ACTIVATE_IND:
664                                         case DL_ESTABLISH_IND:
665                                         case PH_ACTIVATE_CONF:
666                                         case DL_ESTABLISH_CONF:
667                                         PDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", channel->b_sock);
668                                         bchannel_activated(channel);
669                                         break;
670
671                                         case PH_DEACTIVATE_IND:
672                                         case DL_RELEASE_IND:
673                                         case PH_DEACTIVATE_CONF:
674                                         case DL_RELEASE_CONF:
675                                         PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", channel->b_sock);
676 //                                      bchannel_deactivated(channel);
677                                         break;
678
679                                         default:
680                                         PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, channel->b_sock, msg->len);
681                                 }
682                         } else
683                         {
684                                 if (ret < 0 && errno != EWOULDBLOCK)
685                                         PERROR("Read from socket %d failed with return code %d\n", channel->b_sock, ret);
686                         }
687                 }
688                 channel = channel->next;
689         }
690
691         /* if we received at least one b-frame, we will return 1 */
692         return(work);
693 }
694 #else
695 int bchannel_handle(void)
696 {
697         int i;
698         struct bchannel *channel;
699         iframe_t *frm;
700         unsigned char buffer[2048];
701         struct mISDNhead *hh = (struct mISDNhead *)buffer;
702         int len;
703
704         /* no device, no read */
705         if (bchannel_device < 0)
706                 return(0);
707
708         /* get message from kernel */
709         len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
710         if (len < 0)
711         {
712                 if (errno == EAGAIN)
713                         return(0);
714                 PERROR("Failed to do mISDN_read()\n");
715                 return(0);
716         }
717         if (!len)
718         {
719 //              printf("%s: ERROR: mISDN_read() returns nothing\n");
720                 return(0);
721         }
722         frm = (iframe_t *)buffer;
723
724         /* global prim */
725         switch(frm->prim)
726         {
727                 case MGR_DELLAYER | CONFIRM:
728                 case MGR_INITTIMER | CONFIRM:
729                 case MGR_ADDTIMER | CONFIRM:
730                 case MGR_DELTIMER | CONFIRM:
731                 case MGR_REMOVETIMER | CONFIRM:
732                 return(1);
733         }
734
735         /* find the mISDNport that belongs to the stack */
736         channel = bchannel_first;
737         while(channel)
738         {
739                 if (frm->addr == channel->b_addr)
740                         break;
741                 channel = channel->next;
742         } 
743         if (!channel)
744         {
745                 PERROR("message belongs to no channel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
746                 goto out;
747         }
748
749         /* b-message */
750         switch(frm->prim)
751         {
752                 /* we don't care about confirms, we use rx data to sync tx */
753                 case PH_DATA | CONFIRM:
754                 case DL_DATA | CONFIRM:
755                 break;
756
757                 /* we receive audio data, we respond to it AND we send tones */
758                 case PH_DATA | INDICATION:
759                 case DL_DATA | INDICATION:
760                 case PH_CONTROL | INDICATION:
761                 case PH_SIGNAL | INDICATION:
762                 bchannel_receive(channel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
763                 break;
764
765                 case PH_ACTIVATE | INDICATION:
766                 case DL_ESTABLISH | INDICATION:
767                 case PH_ACTIVATE | CONFIRM:
768                 case DL_ESTABLISH | CONFIRM:
769                 PDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
770                 bchannel_activated(channel);
771                 break;
772
773                 case PH_DEACTIVATE | INDICATION:
774                 case DL_RELEASE | INDICATION:
775                 case PH_DEACTIVATE | CONFIRM:
776                 case DL_RELEASE | CONFIRM:
777                 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
778 //              bchannel_deactivated(channel);
779                 break;
780
781                 default:
782                 PERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
783         }
784
785         out:
786         return(1);
787 }
788 #endif
789
790
791 /*
792  * bchannel channel handling
793  */
794 struct bchannel *bchannel_first = NULL;
795 struct bchannel *find_bchannel_handle(unsigned long handle)
796 {
797         struct bchannel *channel = bchannel_first;
798
799         while(channel)
800         {
801                 if (channel->handle == handle)
802                         break;
803                 channel = channel->next;
804         }
805         return(channel);
806 }
807
808 struct bchannel *find_bchannel_ref(unsigned long ref)
809 {
810         struct bchannel *channel = bchannel_first;
811
812         while(channel)
813         {
814                 if (channel->ref == ref)
815                         break;
816                 channel = channel->next;
817         }
818         return(channel);
819 }
820
821 struct bchannel *alloc_bchannel(unsigned long handle)
822 {
823         struct bchannel **channelp = &bchannel_first;
824
825         while(*channelp)
826                 channelp = &((*channelp)->next);
827
828         *channelp = (struct bchannel *)malloc(sizeof(struct bchannel));
829         if (!*channelp)
830                 return(NULL);
831         (*channelp)->handle = handle;
832         (*channelp)->b_state = BSTATE_IDLE;
833                 
834         return(*channelp);
835 }
836
837 void free_bchannel(struct bchannel *channel)
838 {
839         struct bchannel **temp = &bchannel_first;
840
841         while(*temp)
842         {
843                 if (*temp == channel)
844                 {
845                         *temp = (*temp)->next;
846 #ifdef SOCKET_MISDN
847                         if (channel->b_sock > -1)
848 #else
849                         if (channel->b_stid)
850 #endif
851                                 bchannel_destroy(channel);
852                         free(channel);
853                         return;
854                 }
855                 temp = &((*temp)->next);
856         }
857 }
858
859