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;
198 bchannel->rebuffer_usage = 0;
205 static void bchannel_activated(struct bchannel *bchannel)
209 sock = bchannel->b_sock;
211 /* set dsp features */
212 if (bchannel->b_txdata)
213 ph_control(sock, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata, bchannel->b_mode);
214 if (bchannel->b_delay)
215 ph_control(sock, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay, bchannel->b_mode);
216 if (bchannel->b_tx_dejitter)
217 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);
218 if (bchannel->b_tx_gain)
219 ph_control(sock, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain, bchannel->b_mode);
220 if (bchannel->b_rx_gain)
221 ph_control(sock, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain, bchannel->b_mode);
222 if (bchannel->b_pipeline[0])
223 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
224 if (bchannel->b_conf)
225 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
226 if (bchannel->b_echo)
227 ph_control(sock, DSP_ECHO_ON, 0, "DSP-ECHO", 1, bchannel->b_mode);
228 if (bchannel->b_tone)
229 ph_control(sock, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone, bchannel->b_mode);
230 if (bchannel->b_rxoff)
231 ph_control(sock, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1, bchannel->b_mode);
232 // if (bchannel->b_txmix)
233 // ph_control(sock, DSP_MIX_ON, 0, "DSP-MIX", 1, bchannel->b_mode);
234 if (bchannel->b_dtmf)
235 ph_control(sock, DTMF_TONE_START, 0, "DSP-DTMF", 1, bchannel->b_mode);
236 if (bchannel->b_bf_len)
237 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);
238 if (bchannel->b_conf)
239 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
241 bchannel->b_state = BSTATE_ACTIVE;
247 void bchannel_destroy(struct bchannel *bchannel)
249 if (bchannel->b_sock > -1)
251 close(bchannel->b_sock);
252 bchannel->b_sock = -1;
253 bchannel->rebuffer_usage = 0;
255 bchannel->b_state = BSTATE_IDLE;
260 * whenever we get audio data from bchannel, we process it here
262 static void bchannel_receive(struct bchannel *bchannel, unsigned char *buffer, int len)
264 struct mISDNhead *hh = (struct mISDNhead *)buffer;
265 unsigned char *data = buffer + MISDN_HEADER_LEN;
266 unsigned int cont = *((unsigned int *)data);
267 struct bchannel *remote_bchannel;
270 if (hh->prim == PH_CONTROL_IND)
274 CERROR(NULL, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
277 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
280 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
286 CERROR(NULL, NULL, "Blowfish crypt rejected.\n");
290 CDEBUG(NULL, NULL, "Blowfish crypt enabled.\n");
294 CDEBUG(NULL, NULL, "Unhandled bchannel control 0x%x.\n", cont);
298 if (hh->prim == PH_DATA_REQ)
300 if (!bchannel->b_txdata)
302 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
303 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
308 if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND)
310 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", hh->prim);
313 /* if call is bridged and in non-dsp mode */
315 && (bchannel->b_mode == 1 || bchannel->b_mode == 3)
317 && bchannel->call->bridge_call
318 && bchannel->call->bridge_call->bchannel)
320 remote_bchannel = bchannel->call->bridge_call->bchannel;
321 if (remote_bchannel->b_mode == 1 || remote_bchannel->b_mode == 3)
323 hh->prim = PH_DATA_REQ;
324 ret = sendto(remote_bchannel->b_sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
326 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
330 /* calls will not process any audio data unless
331 * the call is connected OR interface features audio during call setup.
334 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
335 if (bchannel->b_rxoff)
337 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
343 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
346 if (!bchannel->call->audiopath)
348 /* return, because we have no audio from port */
352 if (bchannel->call->pipe[1] < 0)
359 if (bchannel->call->rebuffer) {
360 int u = bchannel->rebuffer_usage;
361 unsigned char * b = bchannel->rebuffer;
362 unsigned char * d = data;
364 int fd = bchannel->call->pipe[1];
368 memcpy(b + u, d, 160 - u);
372 if (write(fd, b, 160) < 0) {
383 if (write(fd, d, 160) < 0) {
393 bchannel->rebuffer_usage = u + l;
395 len = write(bchannel->call->pipe[1], data, len);
404 close(bchannel->call->pipe[1]);
405 bchannel->call->pipe[1] = -1;
406 bchannel->rebuffer_usage = 0;
407 CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
412 * transmit data to bchannel
414 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
416 unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
417 struct mISDNhead *frm = (struct mISDNhead *)buff;
421 if (bchannel->b_state != BSTATE_ACTIVE)
423 if (len > 1024 || len < 1)
425 for (i = 0; i < len; i++)
426 *p++ = flip_bits[*data++];
427 switch(bchannel->b_mode)
431 frm->prim = DL_DATA_REQ;
435 frm->prim = PH_DATA_REQ;
439 ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
441 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
448 void bchannel_join(struct bchannel *bchannel, unsigned short id)
452 sock = bchannel->b_sock;
454 bchannel->b_conf = (id<<16) + bchannel_pid;
455 bchannel->b_rxoff = 1;
457 bchannel->b_conf = 0;
458 bchannel->b_rxoff = 0;
460 if (bchannel->b_state == BSTATE_ACTIVE)
462 ph_control(sock, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf, bchannel->b_mode);
463 ph_control(sock, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
471 void bchannel_dtmf(struct bchannel *bchannel, int on)
475 sock = bchannel->b_sock;
476 bchannel->b_dtmf = 1;
477 if (bchannel->b_state == BSTATE_ACTIVE)
478 ph_control(sock, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1, bchannel->b_mode);
485 void bchannel_blowfish(struct bchannel *bchannel, unsigned char *key, int len)
489 sock = bchannel->b_sock;
490 memcpy(bchannel->b_bf_key, key, len);
491 bchannel->b_bf_len = len;
492 if (bchannel->b_state == BSTATE_ACTIVE)
493 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);
500 void bchannel_pipeline(struct bchannel *bchannel, char *pipeline)
504 sock = bchannel->b_sock;
505 strncpy(bchannel->b_pipeline, pipeline, sizeof(bchannel->b_pipeline)-1);
506 if (bchannel->b_state == BSTATE_ACTIVE)
507 ph_control_block(sock, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
514 void bchannel_gain(struct bchannel *bchannel, int gain, int tx)
518 sock = bchannel->b_sock;
520 bchannel->b_tx_gain = gain;
522 bchannel->b_rx_gain = gain;
523 if (bchannel->b_state == BSTATE_ACTIVE)
524 ph_control(sock, (tx)?DSP_VOL_CHANGE_TX:DSP_VOL_CHANGE_RX, gain, (tx)?"DSP-TX_GAIN":"DSP-RX_GAIN", gain, bchannel->b_mode);
529 * main loop for processing messages from mISDN
531 int bchannel_handle(void)
534 struct bchannel *bchannel;
535 char buffer[2048+MISDN_HEADER_LEN];
536 struct mISDNhead *hh = (struct mISDNhead *)buffer;
538 /* process all bchannels */
539 bchannel = bchannel_first;
542 /* handle message from bchannel */
543 if (bchannel->b_sock > -1)
545 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
546 if (ret >= (int)MISDN_HEADER_LEN)
551 /* we don't care about confirms, we use rx data to sync tx */
555 /* we receive audio data, we respond to it AND we send tones */
560 bchannel_receive(bchannel, buffer, ret-MISDN_HEADER_LEN);
563 case PH_ACTIVATE_IND:
564 case DL_ESTABLISH_IND:
565 case PH_ACTIVATE_CNF:
566 case DL_ESTABLISH_CNF:
567 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
568 bchannel_activated(bchannel);
571 case PH_DEACTIVATE_IND:
573 case PH_DEACTIVATE_CNF:
575 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
576 // bchannel_deactivated(bchannel);
580 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);
584 if (ret < 0 && errno != EWOULDBLOCK)
585 CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
588 bchannel = bchannel->next;
591 /* if we received at least one b-frame, we will return 1 */
597 * bchannel channel handling
599 struct bchannel *bchannel_first = NULL;
600 struct bchannel *find_bchannel_handle(unsigned int handle)
602 struct bchannel *bchannel = bchannel_first;
606 if (bchannel->handle == handle)
608 bchannel = bchannel->next;
614 struct bchannel *find_bchannel_ref(unsigned int ref)
616 struct bchannel *bchannel = bchannel_first;
620 if (bchannel->ref == ref)
622 bchannel = bchannel->next;
628 struct bchannel *alloc_bchannel(unsigned int handle)
630 struct bchannel **bchannelp = &bchannel_first;
633 bchannelp = &((*bchannelp)->next);
635 *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
638 (*bchannelp)->handle = handle;
639 (*bchannelp)->b_state = BSTATE_IDLE;
640 (*bchannelp)->b_sock = -1;
645 void free_bchannel(struct bchannel *bchannel)
647 struct bchannel **temp = &bchannel_first;
651 if (*temp == bchannel)
653 *temp = (*temp)->next;
654 if (bchannel->b_sock > -1)
655 bchannel_destroy(bchannel);
658 if (bchannel->call->bchannel)
659 bchannel->call->bchannel = NULL;
664 temp = &((*temp)->next);