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 #include <asterisk/frame.h>
31 #include "extension.h"
33 #include "lcrsocket.h"
40 #ifndef ISDN_PID_L4_B_USER
41 #define ISDN_PID_L4_B_USER 0x440000ff
54 int bchannel_initialize(void)
59 void bchannel_deinitialize(void)
64 * send control information to the channel (dsp-module)
66 static void ph_control(int sock, unsigned int c1, unsigned int c2, char *trace_name, int trace_value, int b_mode)
68 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
69 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
70 unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
73 if (b_mode != 0 && b_mode != 2)
76 CDEBUG(NULL, NULL, "Sending PH_CONTROL %s %x,%x\n", trace_name, c1, c2);
77 ctrl->prim = PH_CONTROL_REQ;
81 ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
83 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
86 static void ph_control_block(int sock, unsigned int c1, void *c2, int c2_len, char *trace_name, int trace_value, int b_mode)
88 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
89 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
90 unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
93 if (b_mode != 0 && b_mode != 2)
96 CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %s %x\n", trace_name, c1);
97 ctrl->prim = PH_CONTROL_REQ;
100 memcpy(d, c2, c2_len);
101 ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
103 CERROR(NULL, NULL, "Failed to send to socket %d\n", sock);
110 int bchannel_create(struct bchannel *bchannel, int mode)
114 struct sockaddr_mISDN addr;
116 if (bchannel->b_sock > -1)
118 CERROR(NULL, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
123 bchannel->b_mode = mode;
124 switch(bchannel->b_mode)
127 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
130 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
133 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSPHDLC);
136 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_HDLC);
139 if (bchannel->b_sock < 0)
141 CERROR(NULL, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
145 /* set nonblocking io */
146 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
149 CERROR(NULL, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
150 close(bchannel->b_sock);
151 bchannel->b_sock = -1;
155 /* bind socket to bchannel */
156 addr.family = AF_ISDN;
157 addr.dev = (bchannel->handle>>8)-1;
158 addr.channel = bchannel->handle & 0xff;
159 ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
162 CERROR(NULL, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. (port %d, channel %d) Did you load mISDNdsp.ko?\n", bchannel->handle, addr.dev + 1, addr.channel);
163 close(bchannel->b_sock);
164 bchannel->b_sock = -1;
172 * activate / deactivate request
174 void bchannel_activate(struct bchannel *bchannel, int activate)
176 struct mISDNhead act;
179 /* activate bchannel */
180 CDEBUG(NULL, NULL, "%sActivating B-channel.\n", activate?"":"De-");
181 switch(bchannel->b_mode)
185 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
189 act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ;
193 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
195 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
197 bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
204 static void bchannel_activated(struct bchannel *bchannel)
208 sock = bchannel->b_sock;
210 /* set dsp features */
211 if (bchannel->b_txdata)
212 ph_control(sock, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata, bchannel->b_mode);
213 if (bchannel->b_delay)
214 ph_control(sock, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay, bchannel->b_mode);
215 if (bchannel->b_tx_dejitter)
216 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);
217 if (bchannel->b_tx_gain)
218 ph_control(sock, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain, bchannel->b_mode);
219 if (bchannel->b_rx_gain)
220 ph_control(sock, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain, bchannel->b_mode);
221 if (bchannel->b_pipeline[0])
222 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
223 if (bchannel->b_conf)
224 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
225 if (bchannel->b_echo)
226 ph_control(sock, DSP_ECHO_ON, 0, "DSP-ECHO", 1, bchannel->b_mode);
227 if (bchannel->b_tone)
228 ph_control(sock, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone, bchannel->b_mode);
229 if (bchannel->b_rxoff)
230 ph_control(sock, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1, bchannel->b_mode);
231 // if (bchannel->b_txmix)
232 // ph_control(sock, DSP_MIX_ON, 0, "DSP-MIX", 1, bchannel->b_mode);
233 if (bchannel->b_dtmf)
234 ph_control(sock, DTMF_TONE_START, 0, "DSP-DTMF", 1, bchannel->b_mode);
235 if (bchannel->b_bf_len)
236 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);
237 if (bchannel->b_conf)
238 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
240 bchannel->b_state = BSTATE_ACTIVE;
246 void bchannel_destroy(struct bchannel *bchannel)
248 if (bchannel->b_sock > -1)
250 close(bchannel->b_sock);
251 bchannel->b_sock = -1;
253 bchannel->b_state = BSTATE_IDLE;
258 * whenever we get audio data from bchannel, we process it here
260 static void bchannel_receive(struct bchannel *bchannel, unsigned char *buffer, int len)
262 struct mISDNhead *hh = (struct mISDNhead *)buffer;
263 unsigned char *data = buffer + MISDN_HEADER_LEN;
264 unsigned int cont = *((unsigned int *)data);
265 struct bchannel *remote_bchannel;
268 if (hh->prim == PH_CONTROL_IND)
272 CERROR(NULL, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
275 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
278 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
284 CERROR(NULL, NULL, "Blowfish crypt rejected.\n");
288 CDEBUG(NULL, NULL, "Blowfish crypt enabled.\n");
292 CDEBUG(NULL, NULL, "Unhandled bchannel control 0x%x.\n", cont);
296 if (hh->prim == PH_DATA_REQ)
298 if (!bchannel->b_txdata)
300 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
301 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
306 if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND)
308 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", hh->prim);
311 /* if call is bridged and in non-dsp mode */
313 && (bchannel->b_mode == 1 || bchannel->b_mode == 3)
315 && bchannel->call->bridge_call
316 && bchannel->call->bridge_call->bchannel)
318 remote_bchannel = bchannel->call->bridge_call->bchannel;
319 if (remote_bchannel->b_mode == 1 || remote_bchannel->b_mode == 3)
321 hh->prim = PH_DATA_REQ;
322 ret = sendto(remote_bchannel->b_sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
324 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
328 /* calls will not process any audio data unless
329 * the call is connected OR interface features audio during call setup.
332 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
333 if (bchannel->b_rxoff)
335 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
341 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
344 if (!bchannel->call->audiopath)
346 /* return, because we have no audio from port */
349 if (bchannel->call->pipe[1] > -1)
351 len = write(bchannel->call->pipe[1], data, len);
354 close(bchannel->call->pipe[1]);
355 bchannel->call->pipe[1] = -1;
356 CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
364 * transmit data to bchannel
366 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
368 unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
369 struct mISDNhead *frm = (struct mISDNhead *)buff;
373 if (bchannel->b_state != BSTATE_ACTIVE)
375 if (len > 1024 || len < 1)
377 for (i = 0; i < len; i++)
378 *p++ = flip_bits[*data++];
379 switch(bchannel->b_mode)
383 frm->prim = DL_DATA_REQ;
387 frm->prim = PH_DATA_REQ;
391 ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
393 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
400 void bchannel_join(struct bchannel *bchannel, unsigned short id)
404 sock = bchannel->b_sock;
406 bchannel->b_conf = (id<<16) + bchannel_pid;
407 bchannel->b_rxoff = 1;
409 bchannel->b_conf = 0;
410 bchannel->b_rxoff = 0;
412 if (bchannel->b_state == BSTATE_ACTIVE)
414 ph_control(sock, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf, bchannel->b_mode);
415 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
423 void bchannel_dtmf(struct bchannel *bchannel, int on)
427 sock = bchannel->b_sock;
428 bchannel->b_dtmf = 1;
429 if (bchannel->b_state == BSTATE_ACTIVE)
430 ph_control(sock, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1, bchannel->b_mode);
437 void bchannel_blowfish(struct bchannel *bchannel, unsigned char *key, int len)
441 sock = bchannel->b_sock;
442 memcpy(bchannel->b_bf_key, key, len);
443 bchannel->b_bf_len = len;
444 if (bchannel->b_state == BSTATE_ACTIVE)
445 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);
452 void bchannel_pipeline(struct bchannel *bchannel, char *pipeline)
456 sock = bchannel->b_sock;
457 strncpy(bchannel->b_pipeline, pipeline, sizeof(bchannel->b_pipeline)-1);
458 if (bchannel->b_state == BSTATE_ACTIVE)
459 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
466 void bchannel_gain(struct bchannel *bchannel, int gain, int tx)
470 sock = bchannel->b_sock;
472 bchannel->b_tx_gain = gain;
474 bchannel->b_rx_gain = gain;
475 if (bchannel->b_state == BSTATE_ACTIVE)
476 ph_control(sock, (tx)?DSP_VOL_CHANGE_TX:DSP_VOL_CHANGE_RX, gain, (tx)?"DSP-TX_GAIN":"DSP-RX_GAIN", gain, bchannel->b_mode);
481 * main loop for processing messages from mISDN
483 int bchannel_handle(void)
486 struct bchannel *bchannel;
487 char buffer[2048+MISDN_HEADER_LEN];
488 struct mISDNhead *hh = (struct mISDNhead *)buffer;
490 /* process all bchannels */
491 bchannel = bchannel_first;
494 /* handle message from bchannel */
495 if (bchannel->b_sock > -1)
497 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
498 if (ret >= (int)MISDN_HEADER_LEN)
503 /* we don't care about confirms, we use rx data to sync tx */
507 /* we receive audio data, we respond to it AND we send tones */
512 bchannel_receive(bchannel, buffer, ret-MISDN_HEADER_LEN);
515 case PH_ACTIVATE_IND:
516 case DL_ESTABLISH_IND:
517 case PH_ACTIVATE_CNF:
518 case DL_ESTABLISH_CNF:
519 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
520 bchannel_activated(bchannel);
523 case PH_DEACTIVATE_IND:
525 case PH_DEACTIVATE_CNF:
527 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
528 // bchannel_deactivated(bchannel);
532 CERROR(NULL, NULL, "child message not handled: prim(0x%x) socket(%d) data len(%d)\n", hh->prim, bchannel->b_sock, ret - MISDN_HEADER_LEN);
536 if (ret < 0 && errno != EWOULDBLOCK)
537 CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
540 bchannel = bchannel->next;
543 /* if we received at least one b-frame, we will return 1 */
549 * bchannel channel handling
551 struct bchannel *bchannel_first = NULL;
552 struct bchannel *find_bchannel_handle(unsigned int handle)
554 struct bchannel *bchannel = bchannel_first;
558 if (bchannel->handle == handle)
560 bchannel = bchannel->next;
566 struct bchannel *find_bchannel_ref(unsigned int ref)
568 struct bchannel *bchannel = bchannel_first;
572 if (bchannel->ref == ref)
574 bchannel = bchannel->next;
580 struct bchannel *alloc_bchannel(unsigned int handle)
582 struct bchannel **bchannelp = &bchannel_first;
585 bchannelp = &((*bchannelp)->next);
587 *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
590 (*bchannelp)->handle = handle;
591 (*bchannelp)->b_state = BSTATE_IDLE;
592 (*bchannelp)->b_sock = -1;
597 void free_bchannel(struct bchannel *bchannel)
599 struct bchannel **temp = &bchannel_first;
603 if (*temp == bchannel)
605 *temp = (*temp)->next;
606 if (bchannel->b_sock > -1)
607 bchannel_destroy(bchannel);
610 if (bchannel->call->bchannel)
611 bchannel->call->bchannel = NULL;
616 temp = &((*temp)->next);