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
45 #define PERROR(arg...) fprintf(stderr, ##arg)
46 #define PDEBUG(arg...) while(0)
58 int bchannel_initialize(void)
63 void bchannel_deinitialize(void)
67 int bchannel_entity = 0; /* used for udevice */
68 int bchannel_device = -1; /* the device handler and port list */
70 int bchannel_initialize(void)
72 unsigned char buff[1025];
73 iframe_t *frm = (iframe_t *)buff;
76 /* open mISDNdevice if not already open */
77 if (bchannel_device < 0)
82 PERROR("cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", ret, errno, strerror(errno));
85 bchannel_device = ret;
86 PDEBUG("mISDN device opened.\n");
88 /* create entity for layer 3 TE-mode */
89 mISDN_write_frame(bchannel_device, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
90 ret = mISDN_read_frame(bchannel_device, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
91 if (ret < (int)mISDN_HEADER_LEN)
94 PERROR("Cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
97 bchannel_entity = frm->dinfo & 0xffff;
104 void bchannel_deinitialize(void)
106 unsigned char buff[1025];
108 if (bchannel_device >= 0)
111 mISDN_write_frame(bchannel_device, buff, 0, MGR_DELENTITY | REQUEST, bchannel_entity, 0, NULL, TIMEOUT_1SEC);
113 mISDN_close(bchannel_device);
114 bchannel_device = -1;
120 * send control information to the channel (dsp-module)
122 static void ph_control(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
125 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
126 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
127 unsigned long *d = buffer+MISDN_HEADER_LEN;
130 ctrl->prim = PH_CONTROL_REQ;
134 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
136 PERROR("Failed to send to socket %d\n", handle);
138 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
139 iframe_t *ctrl = (iframe_t *)buffer;
140 unsigned long *d = (unsigned long *)&ctrl->data.p;
142 ctrl->prim = PH_CONTROL | REQUEST;
143 ctrl->addr = handle | FLG_MSG_DOWN;
145 ctrl->len = sizeof(int)*2;
148 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
151 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
152 if (c1 == CMX_CONF_JOIN)
153 add_trace(trace_name, NULL, "0x%08x", trace_value);
155 add_trace(trace_name, NULL, "%d", trace_value);
160 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
163 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
164 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
165 unsigned long *d = buffer+MISDN_HEADER_LEN;
168 ctrl->prim = PH_CONTROL_REQ;
171 memcpy(d, c2, c2_len);
172 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
174 PERROR("Failed to send to socket %d\n", handle);
176 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
177 iframe_t *ctrl = (iframe_t *)buffer;
178 unsigned long *d = (unsigned long *)&ctrl->data.p;
180 ctrl->prim = PH_CONTROL | REQUEST;
181 ctrl->addr = handle | FLG_MSG_DOWN;
183 ctrl->len = sizeof(int)+c2_len;
185 memcpy(d, c2, c2_len);
186 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
189 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
190 add_trace(trace_name, NULL, "%d", trace_value);
199 int bchannel_create(struct bchannel *bchannel)
201 unsigned char buff[1024];
204 unsigned long on = 1;
205 struct sockadd_mISDN addr;
207 if (bchannel->b_sock)
209 PERROR("Error: Socket already created for handle 0x%x\n", bchannel->handle);
213 /* default tx_dejitter */
214 bchannel->b_tx_dejitter = 1;
217 channel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
218 if (bchannel->b_sock < 0)
220 PERROR("Error: Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
224 /* set nonblocking io */
225 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
228 PERROR("Error: Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
229 close(bchannel->b_sock);
230 channel->b_sock = -1;
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));
241 PERROR("Error: 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 channel->b_sock = -1;
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]);
257 if (bchannel->b_stid)
259 PERROR("Error: stack already created for address 0x%lx\n", bchannel->b_stid);
263 if (bchannel->b_addr)
265 PERROR("Error: stack already created for address 0x%lx\n", bchannel->b_addr);
269 /* create new layer */
270 PDEBUG("creating new layer for stid 0x%lx.\n" , bchannel->handle);
271 memset(&li, 0, sizeof(li));
272 memset(&pid, 0, sizeof(pid));
275 li.st = bchannel->handle;
276 strcpy(li.name, "B L4");
277 li.pid.layermask = ISDN_LAYER((4));
278 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
279 ret = mISDN_new_layer(bchannel_device, &li);
283 PERROR("mISDN_new_layer() failed to add bchannel for stid 0x%lx.\n", bchannel->handle);
288 goto failed_new_layer;
290 bchannel->b_stid = bchannel->handle;
291 bchannel->b_addr = li.id;
292 PDEBUG("new layer (b_addr=0x%x)\n", bchannel->b_addr);
294 /* create new stack */
295 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
296 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
297 pid.protocol[3] = ISDN_PID_L3_B_DSP;
298 pid.protocol[4] = ISDN_PID_L4_B_USER;
299 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
300 ret = mISDN_set_stack(bchannel_device, bchannel->b_stid, &pid);
304 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel stid=0x%lx\n", ret, bchannel->b_stid);
305 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
308 ret = mISDN_get_setstack_ind(bchannel_device, bchannel->b_addr);
313 bchannel->b_addr = mISDN_get_layerid(bchannel_device, bchannel->b_stid, 4);
314 if (!bchannel->b_addr)
317 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create stack", DIRECTION_OUT);
318 add_trace("channel", NULL, "%d", i+1+(i>=15));
319 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
320 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
328 bchannel->b_stid = 0;
329 bchannel->b_addr = 0;
335 * activate / deactivate request
337 void bchannel_activate(struct bchannel *bchannel, int activate)
340 struct mISDNhead act;
343 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
345 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
347 PERROR("Failed to send to socket %d\n", bchannel->b_sock);
351 /* activate bchannel */
352 act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST;
353 act.addr = bchannel->b_addr | FLG_MSG_DOWN;
356 mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
359 bchannel->b_state = BSTATE_ACTIVATING;
362 chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
363 add_trace("channel", NULL, "%d", i+1+(i>=15));
372 static void bchannel_activated(struct bchannel *bchannel)
377 handle = bchannel->b_sock;
379 unsigned long handle;
381 handle = bchannel->b_addr;
384 /* set dsp features */
385 if (bchannel->b_txdata)
386 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
387 if (bchannel->b_delay)
388 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
389 if (bchannel->b_tx_dejitter)
390 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
391 if (bchannel->b_tx_gain)
392 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
393 if (bchannel->b_rx_gain)
394 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
395 if (bchannel->b_pipeline[0])
396 ph_control_block(handle, PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
397 if (bchannel->b_conf)
398 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
399 if (bchannel->b_echo)
400 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
401 if (bchannel->b_tone)
402 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
403 if (bchannel->b_rxoff)
404 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
405 // if (bchannel->b_txmix)
406 // ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
407 if (bchannel->b_dtmf)
408 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
409 if (bchannel->b_crypt_len)
410 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
411 if (bchannel->b_conf)
412 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
414 bchannel->b_state = BSTATE_ACTIVE;
420 static void bchannel_destroy(struct bchannel *bchannel)
424 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
425 add_trace("channel", NULL, "%d", i+1+(i>=15));
426 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
429 if (bchannel->b_sock > -1)
431 close(bchannel->b_sock);
432 bchannel->b_sock = -1;
435 unsigned char buff[1024];
438 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
439 add_trace("channel", NULL, "%d", i+1+(i>=15));
440 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
441 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
444 /* remove our stack only if set */
445 if (bchannel->b_addr)
447 PDEBUG("free stack (b_addr=0x%x)\n", bchannel->b_addr);
448 mISDN_clear_stack(bchannel_device, bchannel->b_stid);
449 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
450 bchannel->b_stid = 0;
451 bchannel->b_addr = 0;
454 bchannel->b_state = BSTATE_IDLE;
459 * whenever we get audio data from bchannel, we process it here
461 static void bchannel_receive(struct bchannel *bchannel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
463 unsigned long cont = *((unsigned long *)data);
464 // unsigned char *data_temp;
465 // unsigned long length_temp;
469 if (prim == (PH_CONTROL | INDICATION))
473 PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
476 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
479 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
480 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
483 if (bchannel->rx_dtmf)
484 bchannel->rx_dtmf(bchannel, cont & DTMF_TONE_MASK);
491 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
492 add_trace("DSP-CRYPT", NULL, "error");
499 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
500 add_trace("DSP-CRYPT", NULL, "ok");
507 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
508 add_trace("unknown", NULL, "0x%x", cont);
516 if (prim == (PH_SIGNAL | INDICATION))
521 if (!bchannel->b_txdata)
523 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
524 PDEBUG("PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
531 chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
532 add_trace("unknown", NULL, "0x%x", frm->dinfo);
540 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
542 PERROR("Bchannel received unknown primitve: 0x%lx\n", prim);
545 /* calls will not process any audio data unless
546 * the call is connected OR interface features audio during call setup.
549 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
550 if (bchannel->b_rxoff)
552 PDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
558 PDEBUG("PmISDN(%s) ignoring data, because no call associated with bchannel\n", p_name);
561 len = write(bchannel->call->pipe[1], data, len);
564 PDEBUG("PmISDN(%s) broken pipe on bchannel pipe\n", p_name);
571 * transmit data to bchannel
573 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
575 unsigned char buff[1025];
576 iframe_t *frm = (iframe_t *)buff;
578 if (bchannel->b_state != BSTATE_ACTIVE)
581 frm->prim = DL_DATA_REQ;
583 ret = sendto(bchannel->b_sock, data, len, 0, NULL, 0);
585 PERROR("Failed to send to socket %d\n", bchannel->b_sock);
587 frm->prim = DL_DATA | REQUEST;
588 frm->addr = bchannel->b_addr | FLG_MSG_DOWN;
592 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
600 void bchannel_join(struct bchannel *bchannel, unsigned short id)
605 handle = bchannel->b_sock;
607 unsigned long handle;
609 handle = bchannel->b_addr;
612 bchannel->b_conf = (id<<16) + bchannel_pid;
613 bchannel->b_rxoff = 1;
615 bchannel->b_conf = 0;
616 bchannel->b_rxoff = 0;
618 if (bchannel->b_state == BSTATE_ACTIVE)
620 ph_control(handle, DSP_RX_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
621 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
627 * main loop for processing messages from mISDN
630 int bchannel_handle(void)
633 struct bchannel *bchannel;
635 char buffer[2048+MISDN_HEADER_LEN];
636 struct mISDNhead *hh = (struct mISDNhead *)buffer;
638 /* process all bchannels */
639 bchannel = bchannel_first;
642 /* handle message from bchannel */
643 if (bchannel->b_sock > -1)
645 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
646 if (ret >= MISDN_HEADER_LEN)
651 /* we don't care about confirms, we use rx data to sync tx */
656 /* we receive audio data, we respond to it AND we send tones */
660 case PH_CONTROL | INDICATION:
661 bchannel_receive(bchannel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
664 case PH_ACTIVATE_IND:
665 case DL_ESTABLISH_IND:
666 case PH_ACTIVATE_CONF:
667 case DL_ESTABLISH_CONF:
668 PDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
669 bchannel_activated(bchannel);
672 case PH_DEACTIVATE_IND:
674 case PH_DEACTIVATE_CONF:
675 case DL_RELEASE_CONF:
676 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
677 // bchannel_deactivated(bchannel);
681 PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, bchannel->b_sock, msg->len);
685 if (ret < 0 && errno != EWOULDBLOCK)
686 PERROR("Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
689 bchannel = bchannel->next;
692 /* if we received at least one b-frame, we will return 1 */
696 int bchannel_handle(void)
698 struct bchannel *bchannel;
700 unsigned char buffer[2048];
703 /* no device, no read */
704 if (bchannel_device < 0)
707 /* get message from kernel */
708 len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
713 PERROR("Failed to do mISDN_read()\n");
718 // printf("%s: ERROR: mISDN_read() returns nothing\n");
721 frm = (iframe_t *)buffer;
726 case MGR_DELLAYER | CONFIRM:
727 case MGR_INITTIMER | CONFIRM:
728 case MGR_ADDTIMER | CONFIRM:
729 case MGR_DELTIMER | CONFIRM:
730 case MGR_REMOVETIMER | CONFIRM:
734 /* find the mISDNport that belongs to the stack */
735 bchannel = bchannel_first;
738 if (frm->addr == bchannel->b_addr)
740 bchannel = bchannel->next;
744 PERROR("message belongs to no bchannel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
751 /* we don't care about confirms, we use rx data to sync tx */
752 case PH_DATA | CONFIRM:
753 case DL_DATA | CONFIRM:
756 /* we receive audio data, we respond to it AND we send tones */
757 case PH_DATA | INDICATION:
758 case DL_DATA | INDICATION:
759 case PH_CONTROL | INDICATION:
760 case PH_SIGNAL | INDICATION:
761 bchannel_receive(bchannel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
764 case PH_ACTIVATE | INDICATION:
765 case DL_ESTABLISH | INDICATION:
766 case PH_ACTIVATE | CONFIRM:
767 case DL_ESTABLISH | CONFIRM:
768 PDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
769 bchannel_activated(bchannel);
772 case PH_DEACTIVATE | INDICATION:
773 case DL_RELEASE | INDICATION:
774 case PH_DEACTIVATE | CONFIRM:
775 case DL_RELEASE | CONFIRM:
776 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
777 // bchannel_deactivated(bchannel);
781 PERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
791 * bchannel channel handling
793 struct bchannel *bchannel_first = NULL;
794 struct bchannel *find_bchannel_handle(unsigned long handle)
796 struct bchannel *bchannel = bchannel_first;
800 if (bchannel->handle == handle)
802 bchannel = bchannel->next;
808 struct bchannel *find_bchannel_ref(unsigned long ref)
810 struct bchannel *bchannel = bchannel_first;
814 if (bchannel->ref == ref)
816 bchannel = bchannel->next;
822 struct bchannel *alloc_bchannel(unsigned long handle)
824 struct bchannel **bchannelp = &bchannel_first;
827 bchannelp = &((*bchannelp)->next);
829 *bchannelp = (struct bchannel *)malloc(sizeof(struct bchannel));
832 (*bchannelp)->handle = handle;
833 (*bchannelp)->b_state = BSTATE_IDLE;
838 void free_bchannel(struct bchannel *bchannel)
840 struct bchannel **temp = &bchannel_first;
844 if (*temp == bchannel)
846 *temp = (*temp)->next;
848 if (bchannel->b_sock > -1)
850 if (bchannel->b_stid)
852 bchannel_destroy(bchannel);
855 if (bchannel->call->bchannel)
856 bchannel->call->bchannel = NULL;
861 temp = &((*temp)->next);