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)
53 int bchannel_socket = -1;
55 int bchannel_initialize(void)
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)
61 PERROR("Cannot open mISDN due to %s. (Does your Kernel support socket based mISDN?)\n", strerror(errno));
65 mISDN_debug_init(global_debug, NULL, NULL, NULL);
67 bchannel_pid = get_pid();
70 init_layer3(4); // buffer of 4
75 void bchannel_deinitialize(void)
81 if (bchannel_socket > -1)
82 close(bchannel_socket);
85 int bchannel_entity = 0; /* used for udevice */
86 int bchannel_device = -1; /* the device handler and port list */
88 int bchannel_initialize(void)
91 unsigned char buff[1025];
92 iframe_t *frm = (iframe_t *)buff;
95 /* open mISDNdevice if not already open */
96 if (bchannel_device < 0)
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));
104 bchannel_device = ret;
105 PDEBUG("mISDN device opened.\n");
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)
113 PERROR("Cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
116 bchannel_entity = frm->dinfo & 0xffff;
117 if (!bchannel_entity)
123 void bchannel_deinitialize(void)
125 unsigned char buff[1025];
127 if (bchannel_device >= 0)
130 mISDN_write_frame(bchannel_device, buff, 0, MGR_DELENTITY | REQUEST, bchannel_entity, 0, NULL, TIMEOUT_1SEC);
132 mISDN_close(bchannel_device);
133 bchannel_device = -1;
139 * send control information to the channel (dsp-module)
141 static void ph_control(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
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;
149 ctrl->prim = PH_CONTROL_REQ;
153 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
155 PERROR("Failed to send to socket %d\n", handle);
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;
161 ctrl->prim = PH_CONTROL | REQUEST;
162 ctrl->addr = handle | FLG_MSG_DOWN;
164 ctrl->len = sizeof(int)*2;
167 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
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);
174 add_trace(trace_name, NULL, "%d", trace_value);
179 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
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;
187 ctrl->prim = PH_CONTROL_REQ;
190 memcpy(d, c2, c2_len);
191 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
193 PERROR("Failed to send to socket %d\n", handle);
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;
199 ctrl->prim = PH_CONTROL | REQUEST;
200 ctrl->addr = handle | FLG_MSG_DOWN;
202 ctrl->len = sizeof(int)+c2_len;
204 memcpy(d, c2, c2_len);
205 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
208 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
209 add_trace(trace_name, NULL, "%d", trace_value);
218 int bchannel_create(struct bchannel *channel)
220 unsigned char buff[1024];
223 unsigned long on = 1;
224 struct sockadd_mISDN addr;
228 PERROR("Error: Socket already created for handle %d\n", channel->handle);
233 channel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
234 if (channel->b_sock < 0)
236 PERROR("Error: Failed to open bchannel-socket for handle %d with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", channel->handle);
240 /* set nonblocking io */
241 ret = ioctl(channel->b_sock, FIONBIO, &on);
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;
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));
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;
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]);
275 PERROR("Error: stack already created for address 0x%x\n", channel->b_stid);
281 PERROR("Error: stack already created for address 0x%x\n", channel->b_addr);
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));
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);
299 PERROR("mISDN_new_layer() failed to add bchannel for stid 0x%x.\n", channel->handle);
304 goto failed_new_layer;
306 channel->b_stid = channel->handle;
307 channel->b_addr = li.id;
308 PDEBUG("new layer (b_addr=0x%x)\n", channel->b_addr);
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);
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);
324 ret = mISDN_get_setstack_ind(bchannel_device, channel->b_addr);
329 channel->b_addr = mISDN_get_layerid(bchannel_device, channel->b_stid, 4);
330 if (!channel->b_addr)
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]);
351 * activate / deactivate request
353 void bchannel_activate(struct bchannel *channel, int activate)
356 struct mISDNhead act;
359 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
361 ret = sendto(channel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
363 PERROR("Failed to send to socket %d\n", channel->b_sock);
367 /* activate bchannel */
368 act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST;
369 act.addr = channel->b_addr | FLG_MSG_DOWN;
372 mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
375 channel->b_state = BSTATE_ACTIVATING;
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));
388 static void bchannel_activated(struct bchannel *channel)
393 handle = channel->b_sock;
395 unsigned long handle;
397 handle = channel->b_addr;
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);
414 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
416 ph_control(handle, CMX_ECHO_ON, 0, "DSP-ECHO", 1);
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);
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);
428 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
430 channel->b_state = BSTATE_ACTIVE;
436 static void bchannel_destroy(struct bchannel *channel)
440 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
441 add_trace("channel", NULL, "%d", i+1+(i>=15));
442 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
445 if (channel->b_sock > -1)
447 close(channel->b_sock);
448 channel->b_sock = -1;
451 unsigned char buff[1024];
454 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
455 add_trace("channel", NULL, "%d", i+1+(i>=15));
456 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
457 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
460 /* remove our stack only if set */
463 PDEBUG("free stack (b_addr=0x%x)\n", channel->b_addr);
464 mISDN_clear_stack(bchannel_device, channel->b_stid);
465 mISDN_write_frame(bchannel_device, buff, channel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
470 channel->b_state = BSTATE_IDLE;
475 * whenever we get audio data from bchannel, we process it here
477 static void bchannel_receive(struct bchannel *channel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
479 unsigned long cont = *((unsigned long *)data);
480 unsigned char *data_temp;
481 unsigned long length_temp;
485 if (prim == (PH_CONTROL | INDICATION))
489 PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
492 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
495 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
496 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
499 if (channel->rx_dtmf)
500 channel->rx_dtmf(channel, cont & DTMF_TONE_MASK);
507 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
508 add_trace("DSP-CRYPT", NULL, "error");
515 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
516 add_trace("DSP-CRYPT", NULL, "ok");
523 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
524 add_trace("unknown", NULL, "0x%x", cont);
532 if (prim == (PH_SIGNAL | INDICATION))
537 if (!channel->b_txdata)
539 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
540 PDEBUG("PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
547 chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
548 add_trace("unknown", NULL, "0x%x", frm->dinfo);
556 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
558 PERROR("Bchannel received unknown primitve: 0x%x\n", prim);
561 /* calls will not process any audio data unless
562 * the call is connected OR interface features audio during call setup.
565 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
566 if (channel->b_rxoff)
568 PDEBUG("PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
572 if (channel->rx_data)
573 channel->rx_data(channel, data, len);
578 * transmit data to bchannel
580 void bchannel_transmit(struct bchannel *channel, unsigned char *data, int len)
582 unsigned char buff[1025];
583 iframe_t *frm = (iframe_t *)buff;
585 if (channel->b_state != BSTATE_ACTIVE)
588 frm->prim = DL_DATA_REQ;
590 ret = sendto(channel->b_sock, data, len, 0, NULL, 0);
592 PERROR("Failed to send to socket %d\n", channel->b_sock);
594 frm->prim = DL_DATA | REQUEST;
595 frm->addr = channel->b_addr | FLG_MSG_DOWN;
599 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
607 void bchannel_join(struct bchannel *channel, unsigned short id)
612 handle = channel->b_sock;
614 unsigned long handle;
616 handle = channel->b_addr;
619 channel->b_conf = (id<<16) + bchannel_pid;
622 if (channel->b_state == BSTATE_ACTIVE)
623 ph_control(handle, CMX_CONF_JOIN, channel->b_conf, "DSP-CONF", channel->b_conf);
628 * main loop for processing messages from mISDN
631 int bchannel_handle(void)
634 struct bchannel *channel;
636 char buffer[2048+MISDN_HEADER_LEN];
637 struct mISDNhead *hh = (struct mISDNhead *)buffer;
639 /* process all bchannels */
640 channel = bchannel_first;
643 /* handle message from bchannel */
644 if (channel->b_sock > -1)
646 ret = recv(channel->b_sock, buffer, sizeof(buffer), 0);
647 if (ret >= MISDN_HEADER_LEN)
652 /* we don't care about confirms, we use rx data to sync tx */
657 /* we receive audio data, we respond to it AND we send tones */
661 case PH_CONTROL | INDICATION:
662 bchannel_receive(channel, hh->prim, hh->dinfo, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
665 case PH_ACTIVATE_IND:
666 case DL_ESTABLISH_IND:
667 case PH_ACTIVATE_CONF:
668 case DL_ESTABLISH_CONF:
669 PDEBUG("DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", channel->b_sock);
670 bchannel_activated(channel);
673 case PH_DEACTIVATE_IND:
675 case PH_DEACTIVATE_CONF:
676 case DL_RELEASE_CONF:
677 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", channel->b_sock);
678 // bchannel_deactivated(channel);
682 PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, channel->b_sock, msg->len);
686 if (ret < 0 && errno != EWOULDBLOCK)
687 PERROR("Read from socket %d failed with return code %d\n", channel->b_sock, ret);
690 channel = channel->next;
693 /* if we received at least one b-frame, we will return 1 */
697 int bchannel_handle(void)
700 struct bchannel *channel;
702 unsigned char buffer[2048];
703 struct mISDNhead *hh = (struct mISDNhead *)buffer;
706 /* no device, no read */
707 if (bchannel_device < 0)
710 /* get message from kernel */
711 len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
716 PERROR("Failed to do mISDN_read()\n");
721 // printf("%s: ERROR: mISDN_read() returns nothing\n");
724 frm = (iframe_t *)buffer;
729 case MGR_DELLAYER | CONFIRM:
730 case MGR_INITTIMER | CONFIRM:
731 case MGR_ADDTIMER | CONFIRM:
732 case MGR_DELTIMER | CONFIRM:
733 case MGR_REMOVETIMER | CONFIRM:
737 /* find the mISDNport that belongs to the stack */
738 channel = bchannel_first;
741 if (frm->addr == channel->b_addr)
743 channel = channel->next;
747 PERROR("message belongs to no channel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
754 /* we don't care about confirms, we use rx data to sync tx */
755 case PH_DATA | CONFIRM:
756 case DL_DATA | CONFIRM:
759 /* we receive audio data, we respond to it AND we send tones */
760 case PH_DATA | INDICATION:
761 case DL_DATA | INDICATION:
762 case PH_CONTROL | INDICATION:
763 case PH_SIGNAL | INDICATION:
764 bchannel_receive(channel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
767 case PH_ACTIVATE | INDICATION:
768 case DL_ESTABLISH | INDICATION:
769 case PH_ACTIVATE | CONFIRM:
770 case DL_ESTABLISH | CONFIRM:
771 PDEBUG( "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
772 bchannel_activated(channel);
775 case PH_DEACTIVATE | INDICATION:
776 case DL_RELEASE | INDICATION:
777 case PH_DEACTIVATE | CONFIRM:
778 case DL_RELEASE | CONFIRM:
779 PDEBUG("DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
780 // bchannel_deactivated(channel);
784 PERROR("message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
794 * bchannel channel handling
796 struct bchannel *bchannel_first = NULL;
797 struct bchannel *find_bchannel_handle(unsigned long handle)
799 struct bchannel *channel = bchannel_first;
803 if (channel->handle == handle)
805 channel = channel->next;
810 struct bchannel *find_bchannel_ref(unsigned long ref)
812 struct bchannel *channel = bchannel_first;
816 if (channel->ref == ref)
818 channel = channel->next;
823 struct bchannel *alloc_bchannel(unsigned long handle)
825 struct bchannel **channelp = &bchannel_first;
828 channelp = &((*channelp)->next);
830 *channelp = (struct bchannel *)malloc(sizeof(struct bchannel));
833 (*channelp)->handle = handle;
834 (*channelp)->b_state = BSTATE_IDLE;
839 void free_bchannel(struct bchannel *channel)
841 struct bchannel **temp = &bchannel_first;
845 if (*temp == channel)
847 *temp = (*temp)->next;
849 if (channel->b_sock > -1)
853 bchannel_destroy(channel);
857 temp = &((*temp)->next);