1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN channel handlin for remote application **
10 \*****************************************************************************/
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
23 #include <netinet/udp.h>
24 #include <netinet/in.h>
26 #include <sys/socket.h>
28 #include <mISDNuser/isdn_net.h>
29 #include <mISDNuser/net_l3.h>
32 #include "extension.h"
34 #include "lcrsocket.h"
41 #ifndef ISDN_PID_L4_B_USER
42 #define ISDN_PID_L4_B_USER 0x440000ff
55 int bchannel_initialize(void)
60 void bchannel_deinitialize(void)
64 int bchannel_entity = 0; /* used for udevice */
65 int bchannel_device = -1; /* the device handler and port list */
67 int bchannel_initialize(void)
69 unsigned char buff[1025];
70 iframe_t *frm = (iframe_t *)buff;
73 /* open mISDNdevice if not already open */
74 if (bchannel_device < 0)
79 CERROR("cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", ret, errno, strerror(errno));
82 bchannel_device = ret;
83 CDEBUG("mISDN device opened.\n");
85 /* create entity for layer 3 TE-mode */
86 mISDN_write_frame(bchannel_device, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
87 ret = mISDN_read_frame(bchannel_device, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
88 if (ret < (int)mISDN_HEADER_LEN)
91 CERROR("Cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
94 bchannel_entity = frm->dinfo & 0xffff;
101 void bchannel_deinitialize(void)
103 unsigned char buff[1025];
105 if (bchannel_device >= 0)
108 mISDN_write_frame(bchannel_device, buff, 0, MGR_DELENTITY | REQUEST, bchannel_entity, 0, NULL, TIMEOUT_1SEC);
110 mISDN_close(bchannel_device);
111 bchannel_device = -1;
117 * send control information to the channel (dsp-module)
119 static void ph_control(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
122 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
123 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
124 unsigned long *d = buffer+MISDN_HEADER_LEN;
127 ctrl->prim = PH_CONTROL_REQ;
131 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
133 CERROR("Failed to send to socket %d\n", handle);
135 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
136 iframe_t *ctrl = (iframe_t *)buffer;
137 unsigned long *d = (unsigned long *)&ctrl->data.p;
139 ctrl->prim = PH_CONTROL | REQUEST;
140 ctrl->addr = handle | FLG_MSG_DOWN;
142 ctrl->len = sizeof(int)*2;
145 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
148 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
149 if (c1 == CMX_CONF_JOIN)
150 add_trace(trace_name, NULL, "0x%08x", trace_value);
152 add_trace(trace_name, NULL, "%d", trace_value);
157 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
160 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
161 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
162 unsigned long *d = buffer+MISDN_HEADER_LEN;
165 ctrl->prim = PH_CONTROL_REQ;
168 memcpy(d, c2, c2_len);
169 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
171 CERROR("Failed to send to socket %d\n", handle);
173 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
174 iframe_t *ctrl = (iframe_t *)buffer;
175 unsigned long *d = (unsigned long *)&ctrl->data.p;
177 ctrl->prim = PH_CONTROL | REQUEST;
178 ctrl->addr = handle | FLG_MSG_DOWN;
180 ctrl->len = sizeof(int)+c2_len;
182 memcpy(d, c2, c2_len);
183 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
186 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
187 add_trace(trace_name, NULL, "%d", trace_value);
196 int bchannel_create(struct bchannel *bchannel)
198 unsigned char buff[1024];
201 unsigned long on = 1;
202 struct sockadd_mISDN addr;
204 if (bchannel->b_sock)
206 CERROR("Error: Socket already created for handle 0x%x\n", bchannel->handle);
210 /* default tx_dejitter */
211 bchannel->b_tx_dejitter = 1;
214 channel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
215 if (bchannel->b_sock < 0)
217 CERROR("Error: Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
221 /* set nonblocking io */
222 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
225 CERROR("Error: Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
226 close(bchannel->b_sock);
227 channel->b_sock = -1;
231 /* bind socket to bchannel */
232 addr.family = AF_ISDN;
233 addr.dev = (bchannel->handle>>8)-1;
234 addr.channel = bchannel->handle && 0xff;
235 ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
238 CERROR("Error: Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
239 close(bchannel->b_sock);
240 channel->b_sock = -1;
245 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
246 add_trace("channel", NULL, "%d", i+1+(i>=15));
247 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
254 if (bchannel->b_stid)
256 CERROR("Error: stack already created for address 0x%lx\n", bchannel->b_stid);
260 if (bchannel->b_addr)
262 CERROR("Error: stack already created for address 0x%lx\n", bchannel->b_addr);
266 /* create new layer */
267 CDEBUG("creating new layer for stid 0x%lx.\n" , bchannel->handle);
268 memset(&li, 0, sizeof(li));
269 memset(&pid, 0, sizeof(pid));
272 li.st = bchannel->handle;
273 strcpy(li.name, "B L4");
274 li.pid.layermask = ISDN_LAYER((4));
275 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
276 ret = mISDN_new_layer(bchannel_device, &li);
280 CERROR("mISDN_new_layer() failed to add bchannel for stid 0x%lx.\n", bchannel->handle);
285 goto failed_new_layer;
287 bchannel->b_stid = bchannel->handle;
288 bchannel->b_addr = li.id;
289 CDEBUG("new layer (b_addr=0x%x)\n", bchannel->b_addr);
291 /* create new stack */
292 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
293 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
294 pid.protocol[3] = ISDN_PID_L3_B_DSP;
295 pid.protocol[4] = ISDN_PID_L4_B_USER;
296 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
297 ret = mISDN_set_stack(bchannel_device, bchannel->b_stid, &pid);
301 CERROR("mISDN_set_stack() failed (ret=%d) to add bchannel stid=0x%lx\n", ret, bchannel->b_stid);
302 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
305 ret = mISDN_get_setstack_ind(bchannel_device, bchannel->b_addr);
310 bchannel->b_addr = mISDN_get_layerid(bchannel_device, bchannel->b_stid, 4);
311 if (!bchannel->b_addr)
314 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create stack", DIRECTION_OUT);
315 add_trace("channel", NULL, "%d", i+1+(i>=15));
316 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
317 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
325 bchannel->b_stid = 0;
326 bchannel->b_addr = 0;
332 * activate / deactivate request
334 void bchannel_activate(struct bchannel *bchannel, int activate)
337 struct mISDNhead act;
340 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
342 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
344 CERROR("Failed to send to socket %d\n", bchannel->b_sock);
348 /* activate bchannel */
349 act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST;
350 act.addr = bchannel->b_addr | FLG_MSG_DOWN;
353 mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
356 bchannel->b_state = BSTATE_ACTIVATING;
359 chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
360 add_trace("channel", NULL, "%d", i+1+(i>=15));
369 static void bchannel_activated(struct bchannel *bchannel)
374 handle = bchannel->b_sock;
376 unsigned long handle;
378 handle = bchannel->b_addr;
381 /* set dsp features */
382 if (bchannel->b_txdata)
383 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
384 if (bchannel->b_delay)
385 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
386 if (bchannel->b_tx_dejitter)
387 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
388 if (bchannel->b_tx_gain)
389 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
390 if (bchannel->b_rx_gain)
391 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
392 if (bchannel->b_pipeline[0])
393 ph_control_block(handle, PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
394 if (bchannel->b_conf)
395 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
396 if (bchannel->b_echo)
397 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
398 if (bchannel->b_tone)
399 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
400 if (bchannel->b_rxoff)
401 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
402 // if (bchannel->b_txmix)
403 // ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
404 if (bchannel->b_dtmf)
405 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
406 if (bchannel->b_crypt_len)
407 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
408 if (bchannel->b_conf)
409 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
411 bchannel->b_state = BSTATE_ACTIVE;
417 static void bchannel_destroy(struct bchannel *bchannel)
421 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
422 add_trace("channel", NULL, "%d", i+1+(i>=15));
423 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
426 if (bchannel->b_sock > -1)
428 close(bchannel->b_sock);
429 bchannel->b_sock = -1;
432 unsigned char buff[1024];
435 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
436 add_trace("channel", NULL, "%d", i+1+(i>=15));
437 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
438 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
441 /* remove our stack only if set */
442 if (bchannel->b_addr)
444 CDEBUG("free stack (b_addr=0x%x)\n", bchannel->b_addr);
445 mISDN_clear_stack(bchannel_device, bchannel->b_stid);
446 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
447 bchannel->b_stid = 0;
448 bchannel->b_addr = 0;
451 bchannel->b_state = BSTATE_IDLE;
456 * whenever we get audio data from bchannel, we process it here
458 static void bchannel_receive(struct bchannel *bchannel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
460 unsigned long cont = *((unsigned long *)data);
461 // unsigned char *data_temp;
462 // unsigned long length_temp;
466 if (prim == (PH_CONTROL | INDICATION))
470 CERROR("SHORT READ OF PH_CONTROL INDICATION\n");
473 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
476 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
477 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
480 if (bchannel->rx_dtmf)
481 bchannel->rx_dtmf(bchannel, cont & DTMF_TONE_MASK);
488 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
489 add_trace("DSP-CRYPT", NULL, "error");
496 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
497 add_trace("DSP-CRYPT", NULL, "ok");
504 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
505 add_trace("unknown", NULL, "0x%x", cont);
513 if (prim == (PH_SIGNAL | INDICATION))
518 if (!bchannel->b_txdata)
520 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
521 CDEBUG("PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
528 chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
529 add_trace("unknown", NULL, "0x%x", frm->dinfo);
537 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
539 CERROR("Bchannel received unknown primitve: 0x%lx\n", prim);
542 /* calls will not process any audio data unless
543 * the call is connected OR interface features audio during call setup.
546 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
547 if (bchannel->b_rxoff)
549 CDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
555 CDEBUG("PmISDN(%s) ignoring data, because no call associated with bchannel\n", p_name);
558 if (!bchannel->call->audiopath)
560 /* return, because we have no audio from port */
563 len = write(bchannel->call->pipe[1], data, len);
566 CDEBUG("PmISDN(%s) broken pipe on bchannel pipe\n", p_name);
573 * transmit data to bchannel
575 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
577 unsigned char buff[1025];
578 iframe_t *frm = (iframe_t *)buff;
580 if (bchannel->b_state != BSTATE_ACTIVE)
583 frm->prim = DL_DATA_REQ;
585 ret = sendto(bchannel->b_sock, data, len, 0, NULL, 0);
587 CERROR("Failed to send to socket %d\n", bchannel->b_sock);
589 frm->prim = DL_DATA | REQUEST;
590 frm->addr = bchannel->b_addr | FLG_MSG_DOWN;
594 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
602 void bchannel_join(struct bchannel *bchannel, unsigned short id)
607 handle = bchannel->b_sock;
609 unsigned long handle;
611 handle = bchannel->b_addr;
614 bchannel->b_conf = (id<<16) + bchannel_pid;
615 bchannel->b_rxoff = 1;
617 bchannel->b_conf = 0;
618 bchannel->b_rxoff = 0;
620 if (bchannel->b_state == BSTATE_ACTIVE)
622 ph_control(handle, DSP_RX_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
623 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
629 * main loop for processing messages from mISDN
632 int bchannel_handle(void)
635 struct bchannel *bchannel;
637 char buffer[2048+MISDN_HEADER_LEN];
638 struct mISDNhead *hh = (struct mISDNhead *)buffer;
640 /* process all bchannels */
641 bchannel = bchannel_first;
644 /* handle message from bchannel */
645 if (bchannel->b_sock > -1)
647 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
648 if (ret >= MISDN_HEADER_LEN)
653 /* we don't care about confirms, we use rx data to sync tx */
658 /* we receive audio data, we respond to it AND we send tones */
662 case PH_CONTROL | INDICATION:
663 bchannel_receive(bchannel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
666 case PH_ACTIVATE_IND:
667 case DL_ESTABLISH_IND:
668 case PH_ACTIVATE_CONF:
669 case DL_ESTABLISH_CONF:
670 CDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
671 bchannel_activated(bchannel);
674 case PH_DEACTIVATE_IND:
676 case PH_DEACTIVATE_CONF:
677 case DL_RELEASE_CONF:
678 CDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
679 // bchannel_deactivated(bchannel);
683 CERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, bchannel->b_sock, msg->len);
687 if (ret < 0 && errno != EWOULDBLOCK)
688 CERROR("Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
691 bchannel = bchannel->next;
694 /* if we received at least one b-frame, we will return 1 */
698 int bchannel_handle(void)
700 struct bchannel *bchannel;
702 unsigned char buffer[2048];
705 /* no device, no read */
706 if (bchannel_device < 0)
709 /* get message from kernel */
710 len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
715 CERROR("Failed to do mISDN_read()\n");
720 // printf("%s: ERROR: mISDN_read() returns nothing\n");
723 frm = (iframe_t *)buffer;
728 case MGR_DELLAYER | CONFIRM:
729 case MGR_INITTIMER | CONFIRM:
730 case MGR_ADDTIMER | CONFIRM:
731 case MGR_DELTIMER | CONFIRM:
732 case MGR_REMOVETIMER | CONFIRM:
736 /* find the mISDNport that belongs to the stack */
737 bchannel = bchannel_first;
740 if (frm->addr == bchannel->b_addr)
742 bchannel = bchannel->next;
746 CERROR("message belongs to no bchannel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
753 /* we don't care about confirms, we use rx data to sync tx */
754 case PH_DATA | CONFIRM:
755 case DL_DATA | CONFIRM:
758 /* we receive audio data, we respond to it AND we send tones */
759 case PH_DATA | INDICATION:
760 case DL_DATA | INDICATION:
761 case PH_CONTROL | INDICATION:
762 case PH_SIGNAL | INDICATION:
763 bchannel_receive(bchannel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
766 case PH_ACTIVATE | INDICATION:
767 case DL_ESTABLISH | INDICATION:
768 case PH_ACTIVATE | CONFIRM:
769 case DL_ESTABLISH | CONFIRM:
770 CDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
771 bchannel_activated(bchannel);
774 case PH_DEACTIVATE | INDICATION:
775 case DL_RELEASE | INDICATION:
776 case PH_DEACTIVATE | CONFIRM:
777 case DL_RELEASE | CONFIRM:
778 CDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
779 // bchannel_deactivated(bchannel);
783 CERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
793 * bchannel channel handling
795 struct bchannel *bchannel_first = NULL;
796 struct bchannel *find_bchannel_handle(unsigned long handle)
798 struct bchannel *bchannel = bchannel_first;
802 if (bchannel->handle == handle)
804 bchannel = bchannel->next;
810 struct bchannel *find_bchannel_ref(unsigned long ref)
812 struct bchannel *bchannel = bchannel_first;
816 if (bchannel->ref == ref)
818 bchannel = bchannel->next;
824 struct bchannel *alloc_bchannel(unsigned long handle)
826 struct bchannel **bchannelp = &bchannel_first;
829 bchannelp = &((*bchannelp)->next);
831 *bchannelp = (struct bchannel *)malloc(sizeof(struct bchannel));
834 (*bchannelp)->handle = handle;
835 (*bchannelp)->b_state = BSTATE_IDLE;
840 void free_bchannel(struct bchannel *bchannel)
842 struct bchannel **temp = &bchannel_first;
846 if (*temp == bchannel)
848 *temp = (*temp)->next;
850 if (bchannel->b_sock > -1)
852 if (bchannel->b_stid)
854 bchannel_destroy(bchannel);
857 if (bchannel->call->bchannel)
858 bchannel->call->bchannel = NULL;
863 temp = &((*temp)->next);