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(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
68 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
69 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
70 unsigned long *d = (unsigned long *)(buffer+MISDN_HEADER_LEN);
73 CDEBUG(NULL, NULL, "Sending PH_CONTROL %d,%d\n", c1, c2);
74 ctrl->prim = PH_CONTROL_REQ;
78 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
80 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
82 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
83 if (c1 == CMX_CONF_JOIN)
84 add_trace(trace_name, NULL, "0x%08x", trace_value);
86 add_trace(trace_name, NULL, "%d", trace_value);
91 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
93 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
94 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
95 unsigned long *d = (unsigned long *)(buffer+MISDN_HEADER_LEN);
98 CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %d\n", c1);
99 ctrl->prim = PH_CONTROL_REQ;
102 memcpy(d, c2, c2_len);
103 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
105 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
107 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
108 add_trace(trace_name, NULL, "%d", trace_value);
117 int bchannel_create(struct bchannel *bchannel)
120 unsigned long on = 1;
121 struct sockaddr_mISDN addr;
123 if (bchannel->b_sock)
125 CERROR(NULL, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
130 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
131 if (bchannel->b_sock < 0)
133 CERROR(NULL, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
137 /* set nonblocking io */
138 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
141 CERROR(NULL, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
142 close(bchannel->b_sock);
143 bchannel->b_sock = -1;
144 bchannel->handle = 0;
148 /* bind socket to bchannel */
149 addr.family = AF_ISDN;
150 addr.dev = (bchannel->handle>>8)-1;
151 addr.channel = bchannel->handle && 0xff;
152 ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
155 CERROR(NULL, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
156 close(bchannel->b_sock);
157 bchannel->b_sock = -1;
162 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
163 add_trace("channel", NULL, "%d", i+1+(i>=15));
164 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
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 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
183 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
185 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
187 bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
190 chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
191 add_trace("channel", NULL, "%d", i+1+(i>=15));
200 static void bchannel_activated(struct bchannel *bchannel)
204 handle = bchannel->b_sock;
206 /* set dsp features */
207 if (bchannel->b_txdata)
208 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
209 if (bchannel->b_delay)
210 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
211 if (bchannel->b_tx_dejitter)
212 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
213 if (bchannel->b_tx_gain)
214 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
215 if (bchannel->b_rx_gain)
216 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
217 if (bchannel->b_pipeline[0])
218 ph_control_block(handle, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
219 if (bchannel->b_conf)
220 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
221 if (bchannel->b_echo)
222 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
223 if (bchannel->b_tone)
224 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
225 if (bchannel->b_rxoff)
226 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
227 // if (bchannel->b_txmix)
228 // ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
229 if (bchannel->b_dtmf)
230 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
231 if (bchannel->b_crypt_len)
232 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
233 if (bchannel->b_conf)
234 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
236 bchannel->b_state = BSTATE_ACTIVE;
242 static void bchannel_destroy(struct bchannel *bchannel)
245 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
246 add_trace("channel", NULL, "%d", i+1+(i>=15));
247 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
250 if (bchannel->b_sock > -1)
252 close(bchannel->b_sock);
253 bchannel->b_sock = -1;
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 long prim, unsigned long dinfo, unsigned char *data, int len)
264 unsigned long cont = *((unsigned long *)data);
265 // unsigned char *data_temp;
266 // unsigned long length_temp;
270 if (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 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
281 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
284 if (bchannel->rx_dtmf)
285 bchannel->rx_dtmf(bchannel, cont & DTMF_TONE_MASK);
292 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
293 add_trace("DSP-CRYPT", NULL, "error");
300 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
301 add_trace("DSP-CRYPT", NULL, "ok");
308 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
309 add_trace("unknown", NULL, "0x%x", cont);
317 if (prim == PH_DATA_REQ)
319 if (!bchannel->b_txdata)
321 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
322 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
327 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
329 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", prim);
332 /* calls will not process any audio data unless
333 * the call is connected OR interface features audio during call setup.
336 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
337 if (bchannel->b_rxoff)
339 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
345 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
348 if (!bchannel->call->audiopath)
350 /* return, because we have no audio from port */
353 if (bchannel->call->pipe[1] > -1)
355 len = write(bchannel->call->pipe[1], data, len);
358 close(bchannel->call->pipe[1]);
359 bchannel->call->pipe[1] = -1;
360 CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
368 * transmit data to bchannel
370 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
372 unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
373 struct mISDNhead *frm = (struct mISDNhead *)buff;
377 if (bchannel->b_state != BSTATE_ACTIVE)
379 if (len > 1024 || len < 1)
381 for (i = 0; i < len; i++)
382 *p++ = flip_bits[*data++];
383 frm->prim = DL_DATA_REQ;
385 ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
387 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
394 void bchannel_join(struct bchannel *bchannel, unsigned short id)
398 handle = bchannel->b_sock;
400 bchannel->b_conf = (id<<16) + bchannel_pid;
401 bchannel->b_rxoff = 1;
403 bchannel->b_conf = 0;
404 bchannel->b_rxoff = 0;
406 if (bchannel->b_state == BSTATE_ACTIVE)
408 ph_control(handle, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
409 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
415 * main loop for processing messages from mISDN
417 int bchannel_handle(void)
420 struct bchannel *bchannel;
421 char buffer[2048+MISDN_HEADER_LEN];
422 struct mISDNhead *hh = (struct mISDNhead *)buffer;
424 /* process all bchannels */
425 bchannel = bchannel_first;
428 /* handle message from bchannel */
429 if (bchannel->b_sock > -1)
431 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
432 if (ret >= (int)MISDN_HEADER_LEN)
437 /* we don't care about confirms, we use rx data to sync tx */
441 /* we receive audio data, we respond to it AND we send tones */
446 bchannel_receive(bchannel, hh->prim, hh->id, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
449 case PH_ACTIVATE_IND:
450 case DL_ESTABLISH_IND:
451 case PH_ACTIVATE_CNF:
452 case DL_ESTABLISH_CNF:
453 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
454 bchannel_activated(bchannel);
457 case PH_DEACTIVATE_IND:
459 case PH_DEACTIVATE_CNF:
461 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
462 // bchannel_deactivated(bchannel);
466 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);
470 if (ret < 0 && errno != EWOULDBLOCK)
471 CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
474 bchannel = bchannel->next;
477 /* if we received at least one b-frame, we will return 1 */
483 * bchannel channel handling
485 struct bchannel *bchannel_first = NULL;
486 struct bchannel *find_bchannel_handle(unsigned long handle)
488 struct bchannel *bchannel = bchannel_first;
492 if (bchannel->handle == handle)
494 bchannel = bchannel->next;
500 struct bchannel *find_bchannel_ref(unsigned long ref)
502 struct bchannel *bchannel = bchannel_first;
506 if (bchannel->ref == ref)
508 bchannel = bchannel->next;
514 struct bchannel *alloc_bchannel(unsigned long handle)
516 struct bchannel **bchannelp = &bchannel_first;
519 bchannelp = &((*bchannelp)->next);
521 *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
524 (*bchannelp)->handle = handle;
525 (*bchannelp)->b_state = BSTATE_IDLE;
530 void free_bchannel(struct bchannel *bchannel)
532 struct bchannel **temp = &bchannel_first;
536 if (*temp == bchannel)
538 *temp = (*temp)->next;
539 if (bchannel->b_sock > -1)
540 bchannel_destroy(bchannel);
543 if (bchannel->call->bchannel)
544 bchannel->call->bchannel = NULL;
549 temp = &((*temp)->next);