1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN channel handlin for remote application **
10 \*****************************************************************************/
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
26 #include <netinet/udp.h>
27 #include <netinet/in.h>
29 #include <sys/socket.h>
33 #include <linux/mISDNif.h>
37 #ifndef ISDN_PID_L4_B_USER
38 #define ISDN_PID_L4_B_USER 0x440000ff
41 #define PERROR(arg...) fprintf(stderr, ##arg)
42 #define PDEBUG(arg...) while(0)
54 int bchannel_initialize(void)
59 void bchannel_deinitialize(void)
63 int bchannel_entity = 0; /* used for udevice */
64 int bchannel_device = -1; /* the device handler and port list */
66 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 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));
82 bchannel_device = ret;
83 PDEBUG("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 PERROR("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 PERROR("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 PERROR("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 *channel)
198 unsigned char buff[1024];
201 unsigned long on = 1;
202 struct sockadd_mISDN addr;
206 PERROR("Error: Socket already created for handle 0x%x\n", channel->handle);
211 channel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
212 if (channel->b_sock < 0)
214 PERROR("Error: Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", channel->handle);
218 /* set nonblocking io */
219 ret = ioctl(channel->b_sock, FIONBIO, &on);
222 PERROR("Error: Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", channel->handle);
223 close(channel->b_sock);
224 channel->b_sock = -1;
228 /* bind socket to bchannel */
229 addr.family = AF_ISDN;
230 addr.dev = (channel->handle>>8)-1;
231 addr.channel = channel->handle && 0xff;
232 ret = bind(channel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
235 PERROR("Error: Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", channel->handle);
236 close(channel->b_sock);
237 channel->b_sock = -1;
242 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
243 add_trace("channel", NULL, "%d", i+1+(i>=15));
244 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
253 PERROR("Error: stack already created for address 0x%x\n", channel->b_stid);
259 PERROR("Error: stack already created for address 0x%x\n", channel->b_addr);
263 /* create new layer */
264 PDEBUG("creating new layer for stid 0x%x.\n" , channel->handle);
265 memset(&li, 0, sizeof(li));
266 memset(&pid, 0, sizeof(pid));
269 li.st = channel->handle;
270 strcpy(li.name, "B L4");
271 li.pid.layermask = ISDN_LAYER((4));
272 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
273 ret = mISDN_new_layer(bchannel_device, &li);
277 PERROR("mISDN_new_layer() failed to add bchannel for stid 0x%x.\n", channel->handle);
282 goto failed_new_layer;
284 channel->b_stid = channel->handle;
285 channel->b_addr = li.id;
286 PDEBUG("new layer (b_addr=0x%x)\n", channel->b_addr);
288 /* create new stack */
289 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
290 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
291 pid.protocol[3] = ISDN_PID_L3_B_DSP;
292 pid.protocol[4] = ISDN_PID_L4_B_USER;
293 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
294 ret = mISDN_set_stack(bchannel_device, channel->b_stid, &pid);
298 PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel stid=0x%x\n", ret, channel->b_stid);
299 mISDN_write_frame(bchannel_device, buff, channel->b_addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
302 ret = mISDN_get_setstack_ind(bchannel_device, channel->b_addr);
307 channel->b_addr = mISDN_get_layerid(bchannel_device, channel->b_stid, 4);
308 if (!channel->b_addr)
311 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create stack", DIRECTION_OUT);
312 add_trace("channel", NULL, "%d", i+1+(i>=15));
313 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
314 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
329 * activate / deactivate request
331 void bchannel_activate(struct bchannel *channel, int activate)
334 struct mISDNhead act;
337 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
339 ret = sendto(channel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
341 PERROR("Failed to send to socket %d\n", channel->b_sock);
345 /* activate bchannel */
346 act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST;
347 act.addr = channel->b_addr | FLG_MSG_DOWN;
350 mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
353 channel->b_state = BSTATE_ACTIVATING;
356 chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
357 add_trace("channel", NULL, "%d", i+1+(i>=15));
366 static void bchannel_activated(struct bchannel *channel)
371 handle = channel->b_sock;
373 unsigned long handle;
375 handle = channel->b_addr;
378 /* set dsp features */
379 if (channel->b_txdata)
380 ph_control(handle, (channel->b_txdata)?CMX_TXDATA_ON:CMX_TXDATA_OFF, 0, "DSP-TXDATA", channel->b_txdata);
381 if (channel->b_delay)
382 ph_control(handle, CMX_DELAY, channel->b_delay, "DSP-DELAY", channel->b_delay);
383 if (channel->b_tx_dejitter)
384 ph_control(handle, (channel->b_tx_dejitter)?CMX_TX_DEJITTER:CMX_TX_DEJ_OFF, 0, "DSP-DELAY", channel->b_tx_dejitter);
385 if (channel->b_tx_gain)
386 ph_control(handle, VOL_CHANGE_TX, channel->b_tx_gain, "DSP-TX_GAIN", channel->b_tx_gain);
387 if (channel->b_rx_gain)
388 ph_control(handle, VOL_CHANGE_RX, channel->b_rx_gain, "DSP-RX_GAIN", channel->b_rx_gain);
389 if (channel->b_pipeline[0])
390 ph_control_block(handle, PIPELINE_CFG, channel->b_pipeline, strlen(channel->b_pipeline)+1, "DSP-PIPELINE", 0);
392 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
394 ph_control(handle, CMX_ECHO_ON, 0, "DSP-ECHO", 1);
396 ph_control(handle, TONE_PATT_ON, channel->b_tone, "DSP-TONE", channel->b_tone);
397 if (channel->b_rxoff)
398 ph_control(handle, CMX_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
399 // if (channel->b_txmix)
400 // ph_control(handle, CMX_MIX_ON, 0, "DSP-MIX", 1);
402 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
403 if (channel->b_crypt_len)
404 ph_control_block(handle, BF_ENABLE_KEY, channel->b_crypt_key, channel->b_crypt_len, "DSP-CRYPT", channel->b_crypt_len);
406 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
408 channel->b_state = BSTATE_ACTIVE;
414 static void bchannel_destroy(struct bchannel *channel)
418 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
419 add_trace("channel", NULL, "%d", i+1+(i>=15));
420 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
423 if (channel->b_sock > -1)
425 close(channel->b_sock);
426 channel->b_sock = -1;
429 unsigned char buff[1024];
432 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
433 add_trace("channel", NULL, "%d", i+1+(i>=15));
434 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
435 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
438 /* remove our stack only if set */
441 PDEBUG("free stack (b_addr=0x%x)\n", channel->b_addr);
442 mISDN_clear_stack(bchannel_device, channel->b_stid);
443 mISDN_write_frame(bchannel_device, buff, channel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
448 channel->b_state = BSTATE_IDLE;
453 * whenever we get audio data from bchannel, we process it here
455 static void bchannel_receive(struct bchannel *channel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
457 unsigned long cont = *((unsigned long *)data);
458 unsigned char *data_temp;
459 unsigned long length_temp;
463 if (prim == (PH_CONTROL | INDICATION))
467 PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
470 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
473 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
474 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
477 if (channel->rx_dtmf)
478 channel->rx_dtmf(channel, cont & DTMF_TONE_MASK);
485 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
486 add_trace("DSP-CRYPT", NULL, "error");
493 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
494 add_trace("DSP-CRYPT", NULL, "ok");
501 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
502 add_trace("unknown", NULL, "0x%x", cont);
510 if (prim == (PH_SIGNAL | INDICATION))
515 if (!channel->b_txdata)
517 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
518 PDEBUG("PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
525 chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
526 add_trace("unknown", NULL, "0x%x", frm->dinfo);
534 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
536 PERROR("Bchannel received unknown primitve: 0x%x\n", prim);
539 /* calls will not process any audio data unless
540 * the call is connected OR interface features audio during call setup.
543 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
544 if (channel->b_rxoff)
546 PDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
550 if (channel->rx_data)
551 channel->rx_data(channel, data, len);
556 * transmit data to bchannel
558 void bchannel_transmit(struct bchannel *channel, unsigned char *data, int len)
560 unsigned char buff[1025];
561 iframe_t *frm = (iframe_t *)buff;
563 if (channel->b_state != BSTATE_ACTIVE)
566 frm->prim = DL_DATA_REQ;
568 ret = sendto(channel->b_sock, data, len, 0, NULL, 0);
570 PERROR("Failed to send to socket %d\n", channel->b_sock);
572 frm->prim = DL_DATA | REQUEST;
573 frm->addr = channel->b_addr | FLG_MSG_DOWN;
577 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
585 void bchannel_join(struct bchannel *channel, unsigned short id)
590 handle = channel->b_sock;
592 unsigned long handle;
594 handle = channel->b_addr;
597 channel->b_conf = (id<<16) + bchannel_pid;
600 if (channel->b_state == BSTATE_ACTIVE)
601 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
606 * main loop for processing messages from mISDN
609 int bchannel_handle(void)
612 struct bchannel *channel;
614 char buffer[2048+MISDN_HEADER_LEN];
615 struct mISDNhead *hh = (struct mISDNhead *)buffer;
617 /* process all bchannels */
618 channel = bchannel_first;
621 /* handle message from bchannel */
622 if (channel->b_sock > -1)
624 ret = recv(channel->b_sock, buffer, sizeof(buffer), 0);
625 if (ret >= MISDN_HEADER_LEN)
630 /* we don't care about confirms, we use rx data to sync tx */
635 /* we receive audio data, we respond to it AND we send tones */
639 case PH_CONTROL | INDICATION:
640 bchannel_receive(channel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
643 case PH_ACTIVATE_IND:
644 case DL_ESTABLISH_IND:
645 case PH_ACTIVATE_CONF:
646 case DL_ESTABLISH_CONF:
647 PDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", channel->b_sock);
648 bchannel_activated(channel);
651 case PH_DEACTIVATE_IND:
653 case PH_DEACTIVATE_CONF:
654 case DL_RELEASE_CONF:
655 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", channel->b_sock);
656 // bchannel_deactivated(channel);
660 PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, channel->b_sock, msg->len);
664 if (ret < 0 && errno != EWOULDBLOCK)
665 PERROR("Read from socket %d failed with return code %d\n", channel->b_sock, ret);
668 channel = channel->next;
671 /* if we received at least one b-frame, we will return 1 */
675 int bchannel_handle(void)
678 struct bchannel *channel;
680 unsigned char buffer[2048];
681 struct mISDNhead *hh = (struct mISDNhead *)buffer;
684 /* no device, no read */
685 if (bchannel_device < 0)
688 /* get message from kernel */
689 len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
694 PERROR("Failed to do mISDN_read()\n");
699 // printf("%s: ERROR: mISDN_read() returns nothing\n");
702 frm = (iframe_t *)buffer;
707 case MGR_DELLAYER | CONFIRM:
708 case MGR_INITTIMER | CONFIRM:
709 case MGR_ADDTIMER | CONFIRM:
710 case MGR_DELTIMER | CONFIRM:
711 case MGR_REMOVETIMER | CONFIRM:
715 /* find the mISDNport that belongs to the stack */
716 channel = bchannel_first;
719 if (frm->addr == channel->b_addr)
721 channel = channel->next;
725 PERROR("message belongs to no channel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
732 /* we don't care about confirms, we use rx data to sync tx */
733 case PH_DATA | CONFIRM:
734 case DL_DATA | CONFIRM:
737 /* we receive audio data, we respond to it AND we send tones */
738 case PH_DATA | INDICATION:
739 case DL_DATA | INDICATION:
740 case PH_CONTROL | INDICATION:
741 case PH_SIGNAL | INDICATION:
742 bchannel_receive(channel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
745 case PH_ACTIVATE | INDICATION:
746 case DL_ESTABLISH | INDICATION:
747 case PH_ACTIVATE | CONFIRM:
748 case DL_ESTABLISH | CONFIRM:
749 PDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
750 bchannel_activated(channel);
753 case PH_DEACTIVATE | INDICATION:
754 case DL_RELEASE | INDICATION:
755 case PH_DEACTIVATE | CONFIRM:
756 case DL_RELEASE | CONFIRM:
757 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
758 // bchannel_deactivated(channel);
762 PERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
772 * bchannel channel handling
774 struct bchannel *bchannel_first = NULL;
775 struct bchannel *find_bchannel_handle(unsigned long handle)
777 struct bchannel *channel = bchannel_first;
781 if (channel->handle == handle)
783 channel = channel->next;
788 struct bchannel *find_bchannel_ref(unsigned long ref)
790 struct bchannel *channel = bchannel_first;
794 if (channel->ref == ref)
796 channel = channel->next;
801 struct bchannel *alloc_bchannel(unsigned long handle)
803 struct bchannel **channelp = &bchannel_first;
806 channelp = &((*channelp)->next);
808 *channelp = (struct bchannel *)malloc(sizeof(struct bchannel));
811 (*channelp)->handle = handle;
812 (*channelp)->b_state = BSTATE_IDLE;
817 void free_bchannel(struct bchannel *channel)
819 struct bchannel **temp = &bchannel_first;
823 if (*temp == channel)
825 *temp = (*temp)->next;
827 if (channel->b_sock > -1)
831 bchannel_destroy(channel);
834 if (channel->call->channel)
835 channel->call->channel = NULL;
837 if (channel->bridge_channel)
839 if (channel->bridge_channel->bridge_channel)
840 channel->bridge_channel->bridge_channel = NULL;
845 temp = &((*temp)->next);