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>
22 #include <netinet/udp.h>
23 #include <netinet/in.h>
25 #include <sys/socket.h>
28 #define AF_COMPATIBILITY_FUNC 1
29 #define MISDN_OLD_AF_COMPATIBILITY 1
30 #include <compat_af_isdn.h>
32 #define HAVE_ATTRIBUTE_always_inline 1
33 #define HAVE_ARPA_INET_H 1
34 #define HAVE_TIMERSUB 1
36 #include <asterisk/compiler.h>
37 #include <asterisk/frame.h>
40 #include "extension.h"
42 #include "lcrsocket.h"
49 #ifndef ISDN_PID_L4_B_USER
50 #define ISDN_PID_L4_B_USER 0x440000ff
63 int bchannel_initialize(void)
70 void bchannel_deinitialize(void)
75 * send control information to the channel (dsp-module)
77 static void ph_control(int sock, unsigned int c1, unsigned int c2, char *trace_name, int trace_value, int b_mode)
79 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
80 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
81 unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
84 if (b_mode != 0 && b_mode != 2)
87 CDEBUG(NULL, NULL, "Sending PH_CONTROL %s %x,%x\n", trace_name, c1, c2);
88 ctrl->prim = PH_CONTROL_REQ;
92 ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
94 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
97 static void ph_control_block(int sock, unsigned int c1, void *c2, int c2_len, char *trace_name, int trace_value, int b_mode)
99 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
100 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
101 unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
104 if (b_mode != 0 && b_mode != 2)
107 CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %s %x\n", trace_name, c1);
108 ctrl->prim = PH_CONTROL_REQ;
111 memcpy(d, c2, c2_len);
112 ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
114 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
121 int bchannel_create(struct bchannel *bchannel, int mode)
125 struct sockaddr_mISDN addr;
127 if (bchannel->b_sock > -1)
129 CERROR(bchannel->call, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
134 bchannel->b_mode = mode;
135 switch(bchannel->b_mode)
138 CDEBUG(bchannel->call, NULL, "Open DSP audio\n");
139 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
142 CDEBUG(bchannel->call, NULL, "Open audio\n");
143 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
146 CDEBUG(bchannel->call, NULL, "Open DSP HDLC\n");
147 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSPHDLC);
150 CDEBUG(bchannel->call, NULL, "Open HDLC\n");
151 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_HDLC);
154 if (bchannel->b_sock < 0)
156 CERROR(bchannel->call, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDN_dsp.ko?\n", bchannel->handle);
160 /* set nonblocking io */
161 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
164 CERROR(bchannel->call, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
165 close(bchannel->b_sock);
166 bchannel->b_sock = -1;
170 /* bind socket to bchannel */
171 addr.family = AF_ISDN;
172 addr.dev = (bchannel->handle>>8);
173 addr.channel = bchannel->handle & 0xff;
174 ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
177 CERROR(bchannel->call, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. (port %d, channel %d) Did you load mISDN_dsp.ko?\n", bchannel->handle, addr.dev, addr.channel);
178 close(bchannel->b_sock);
179 bchannel->b_sock = -1;
187 * activate / deactivate request
189 void bchannel_activate(struct bchannel *bchannel, int activate)
191 struct mISDNhead act;
194 /* activate bchannel */
195 CDEBUG(bchannel->call, NULL, "%sActivating B-channel.\n", activate?"":"De-");
196 switch(bchannel->b_mode)
200 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
204 act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ;
208 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
210 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
212 bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
219 static void bchannel_activated(struct bchannel *bchannel)
223 sock = bchannel->b_sock;
225 /* set dsp features */
226 if (bchannel->b_txdata)
227 ph_control(sock, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata, bchannel->b_mode);
228 if (bchannel->b_delay && bchannel->b_mode == 0)
229 ph_control(sock, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay, bchannel->b_mode);
230 if (bchannel->b_tx_dejitter && bchannel->b_mode == 0)
231 ph_control(sock, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter, bchannel->b_mode);
232 if (bchannel->b_tx_gain && bchannel->b_mode == 0)
233 ph_control(sock, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain, bchannel->b_mode);
234 if (bchannel->b_rx_gain && bchannel->b_mode == 0)
235 ph_control(sock, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain, bchannel->b_mode);
236 if (bchannel->b_pipeline[0] && bchannel->b_mode == 0)
237 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
238 if (bchannel->b_conf)
239 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
240 if (bchannel->b_echo)
241 ph_control(sock, DSP_ECHO_ON, 0, "DSP-ECHO", 1, bchannel->b_mode);
242 if (bchannel->b_tone && bchannel->b_mode == 0)
243 ph_control(sock, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone, bchannel->b_mode);
244 if (bchannel->b_rxoff)
245 ph_control(sock, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1, bchannel->b_mode);
246 // if (bchannel->b_txmix && bchannel->b_mode == 0)
247 // ph_control(sock, DSP_MIX_ON, 0, "DSP-MIX", 1, bchannel->b_mode);
248 if (bchannel->b_dtmf && bchannel->b_mode == 0)
249 ph_control(sock, DTMF_TONE_START, 0, "DSP-DTMF", 1, bchannel->b_mode);
250 if (bchannel->b_bf_len && bchannel->b_mode == 0)
251 ph_control_block(sock, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
252 if (bchannel->b_conf)
253 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
255 bchannel->b_state = BSTATE_ACTIVE;
261 void bchannel_destroy(struct bchannel *bchannel)
263 if (bchannel->b_sock > -1)
265 close(bchannel->b_sock);
266 bchannel->b_sock = -1;
268 bchannel->b_state = BSTATE_IDLE;
273 * whenever we get audio data from bchannel, we process it here
275 static void bchannel_receive(struct bchannel *bchannel, unsigned char *buffer, int len)
277 struct mISDNhead *hh = (struct mISDNhead *)buffer;
278 unsigned char *data = buffer + MISDN_HEADER_LEN;
279 unsigned int cont = *((unsigned int *)data);
282 struct bchannel *remote_bchannel;
285 if (hh->prim == PH_CONTROL_IND)
287 /* non dsp -> ignore ph_control */
288 if (bchannel->b_mode == 1 || bchannel->b_mode == 3)
292 CERROR(bchannel->call, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
295 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
298 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
304 CERROR(bchannel->call, NULL, "Blowfish crypt rejected.\n");
308 CDEBUG(bchannel->call, NULL, "Blowfish crypt enabled.\n");
312 CDEBUG(bchannel->call, NULL, "Unhandled bchannel control 0x%x.\n", cont);
316 if (hh->prim == PH_DATA_REQ)
318 if (!bchannel->b_txdata)
320 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
321 CDEBUG(bchannel->call, NULL, "ignoring tx data, because 'txdata' is turned off\n");
326 if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND)
328 CERROR(bchannel->call, NULL, "Bchannel received unknown primitve: 0x%lx\n", hh->prim);
331 /* if calls are bridged, but not via dsp (no b_conf), forward here */
332 if (!bchannel->b_conf
334 && bchannel->call->bridge_call
335 && bchannel->call->bridge_call->bchannel) {
336 remote_bchannel = bchannel->call->bridge_call->bchannel;
339 char string[4096] = "";
341 sprintf(string+strlen(string), " %02x", data[i]);
344 CDEBUG(remote_bchannel->call, NULL, "Forwarding packet%s\n", string);
346 hh->prim = PH_DATA_REQ;
348 ret = sendto(remote_bchannel->b_sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
350 CERROR(remote_bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
353 /* calls will not process any audio data unless
354 * the call is connected OR interface features audio during call setup.
357 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
358 if (bchannel->b_rxoff)
360 CDEBUG(bchannel->call, NULL, "ignoring data, because rx is turned off\n");
366 CDEBUG(bchannel->call, NULL, "ignoring data, because no call associated with bchannel\n");
369 if (!bchannel->call->audiopath)
371 /* return, because we have no audio from port */
375 if (bchannel->call->pipe[1] < 0)
382 if (bchannel->b_mode == 0 || bchannel->b_mode == 1)
385 for (i = 0; i < len; i++) {
392 len = write(bchannel->call->pipe[1], data, len);
398 close(bchannel->call->pipe[1]);
399 bchannel->call->pipe[1] = -1;
400 CDEBUG(bchannel->call, NULL, "broken pipe on bchannel pipe\n");
405 * transmit data to bchannel
407 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
409 unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
410 struct mISDNhead *frm = (struct mISDNhead *)buff;
414 if (bchannel->b_state != BSTATE_ACTIVE)
416 if (len > 1024 || len < 1)
418 switch(bchannel->b_mode)
421 for (i = 0; i < len; i++)
422 *p++ = flip_bits[*data++];
423 frm->prim = DL_DATA_REQ;
426 for (i = 0; i < len; i++)
427 *p++ = flip_bits[*data++];
428 frm->prim = PH_DATA_REQ;
431 memcpy(p, data, len);
432 frm->prim = DL_DATA_REQ;
435 memcpy(p, data, len);
436 frm->prim = PH_DATA_REQ;
440 ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
442 CERROR(bchannel->call, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
449 void bchannel_join(struct bchannel *bchannel, unsigned short id)
453 sock = bchannel->b_sock;
455 bchannel->b_conf = (id<<16) + bchannel_pid;
456 bchannel->b_rxoff = 1;
458 bchannel->b_conf = 0;
459 bchannel->b_rxoff = 0;
461 if (bchannel->b_state == BSTATE_ACTIVE)
463 ph_control(sock, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf, bchannel->b_mode);
464 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
472 void bchannel_dtmf(struct bchannel *bchannel, int on)
476 sock = bchannel->b_sock;
477 bchannel->b_dtmf = 1;
478 if (bchannel->b_state == BSTATE_ACTIVE && bchannel->b_mode == 0)
479 ph_control(sock, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1, bchannel->b_mode);
486 void bchannel_blowfish(struct bchannel *bchannel, unsigned char *key, int len)
490 sock = bchannel->b_sock;
491 memcpy(bchannel->b_bf_key, key, len);
492 bchannel->b_bf_len = len;
493 if (bchannel->b_state == BSTATE_ACTIVE)
494 ph_control_block(sock, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
501 void bchannel_pipeline(struct bchannel *bchannel, char *pipeline)
505 sock = bchannel->b_sock;
506 strncpy(bchannel->b_pipeline, pipeline, sizeof(bchannel->b_pipeline)-1);
507 if (bchannel->b_state == BSTATE_ACTIVE)
508 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
515 void bchannel_gain(struct bchannel *bchannel, int gain, int tx)
519 sock = bchannel->b_sock;
521 bchannel->b_tx_gain = gain;
523 bchannel->b_rx_gain = gain;
524 if (bchannel->b_state == BSTATE_ACTIVE && bchannel->b_mode == 0)
525 ph_control(sock, (tx)?DSP_VOL_CHANGE_TX:DSP_VOL_CHANGE_RX, gain, (tx)?"DSP-TX_GAIN":"DSP-RX_GAIN", gain, bchannel->b_mode);
530 * main loop for processing messages from mISDN
532 int bchannel_handle(void)
535 struct bchannel *bchannel;
536 unsigned char buffer[2048+MISDN_HEADER_LEN];
537 struct mISDNhead *hh = (struct mISDNhead *)buffer;
539 /* process all bchannels */
540 bchannel = bchannel_first;
543 /* handle message from bchannel */
544 if (bchannel->b_sock > -1)
546 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
547 if (ret >= (int)MISDN_HEADER_LEN)
552 /* we don't care about confirms, we use rx data to sync tx */
556 /* we receive audio data, we respond to it AND we send tones */
561 bchannel_receive(bchannel, buffer, ret-MISDN_HEADER_LEN);
564 case PH_ACTIVATE_IND:
565 case DL_ESTABLISH_IND:
566 case PH_ACTIVATE_CNF:
567 case DL_ESTABLISH_CNF:
568 CDEBUG(bchannel->call, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
569 bchannel_activated(bchannel);
572 case PH_DEACTIVATE_IND:
574 case PH_DEACTIVATE_CNF:
576 CDEBUG(bchannel->call, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
577 // bchannel_deactivated(bchannel);
581 CERROR(bchannel->call, NULL, "child message not handled: prim(0x%x) socket(%d) data len(%d)\n", hh->prim, bchannel->b_sock, ret - MISDN_HEADER_LEN);
585 if (ret < 0 && errno != EWOULDBLOCK)
586 CERROR(bchannel->call, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
589 bchannel = bchannel->next;
592 /* if we received at least one b-frame, we will return 1 */
598 * bchannel channel handling
600 struct bchannel *bchannel_first = NULL;
601 struct bchannel *find_bchannel_handle(unsigned int handle)
603 struct bchannel *bchannel = bchannel_first;
607 if (bchannel->handle == handle)
609 bchannel = bchannel->next;
615 struct bchannel *find_bchannel_ref(unsigned int ref)
617 struct bchannel *bchannel = bchannel_first;
621 if (bchannel->ref == ref)
623 bchannel = bchannel->next;
629 struct bchannel *alloc_bchannel(unsigned int handle)
631 struct bchannel **bchannelp = &bchannel_first;
634 bchannelp = &((*bchannelp)->next);
636 *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
639 (*bchannelp)->handle = handle;
640 (*bchannelp)->b_state = BSTATE_IDLE;
641 (*bchannelp)->b_sock = -1;
646 void free_bchannel(struct bchannel *bchannel)
648 struct bchannel **temp = &bchannel_first;
652 if (*temp == bchannel)
654 *temp = (*temp)->next;
655 if (bchannel->b_sock > -1)
656 bchannel_destroy(bchannel);
659 if (bchannel->call->bchannel)
660 bchannel->call->bchannel = NULL;
665 temp = &((*temp)->next);