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