work on chan_lcr: bridging works, interface selection possible
[lcr.git] / bchannel.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN channel handlin for remote application                              **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <poll.h>
17 #include <errno.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <netinet/udp.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <mISDNif.h>
27
28 #include <asterisk/frame.h>
29
30
31 #include "extension.h"
32 #include "message.h"
33 #include "lcrsocket.h"
34 #include "cause.h"
35 #include "bchannel.h"
36 #include "chan_lcr.h"
37 #include "callerid.h"
38
39
40 #ifndef ISDN_PID_L4_B_USER
41 #define ISDN_PID_L4_B_USER 0x440000ff
42 #endif
43
44 pid_t   bchannel_pid;
45
46 enum {
47         BSTATE_IDLE,
48         BSTATE_ACTIVATING,
49         BSTATE_ACTIVE,
50         BSTATE_DEACTIVATING,
51 };
52
53
54 int bchannel_initialize(void)
55 {
56         return(0);
57 }
58
59 void bchannel_deinitialize(void)
60 {
61 }
62
63 /*
64  * send control information to the channel (dsp-module)
65  */
66 static void ph_control(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
67 {
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);
71         int ret;
72
73         CDEBUG(NULL, NULL, "Sending PH_CONTROL %s %x,%x\n", trace_name, c1, c2);
74         ctrl->prim = PH_CONTROL_REQ;
75         ctrl->id = 0;
76         *d++ = c1;
77         *d++ = c2;
78         ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
79         if (ret < 0)
80                 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
81 }
82
83 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
84 {
85         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
86         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
87         unsigned long *d = (unsigned long *)(buffer+MISDN_HEADER_LEN);
88         int ret;
89
90         CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %s %x\n", trace_name, c1);
91         ctrl->prim = PH_CONTROL_REQ;
92         ctrl->id = 0;
93         *d++ = c1;
94         memcpy(d, c2, c2_len);
95         ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
96         if (ret < 0)
97                 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
98 }
99
100
101 /*
102  * create stack
103  */
104 int bchannel_create(struct bchannel *bchannel)
105 {
106         int ret;
107         unsigned long on = 1;
108         struct sockaddr_mISDN addr;
109
110         if (bchannel->b_sock)
111         {
112                 CERROR(NULL, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
113                 return(0);
114         }
115
116         /* open socket */
117         bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
118         if (bchannel->b_sock < 0)
119         {
120                 CERROR(NULL, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
121                 return(0);
122         }
123         
124         /* set nonblocking io */
125         ret = ioctl(bchannel->b_sock, FIONBIO, &on);
126         if (ret < 0)
127         {
128                 CERROR(NULL, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
129                 close(bchannel->b_sock);
130                 bchannel->b_sock = -1;
131                 return(0);
132         }
133
134         /* bind socket to bchannel */
135         addr.family = AF_ISDN;
136         addr.dev = (bchannel->handle>>8)-1;
137         addr.channel = bchannel->handle & 0xff;
138         ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
139         if (ret < 0)
140         {
141                 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);
142                 close(bchannel->b_sock);
143                 bchannel->b_sock = -1;
144                 return(0);
145         }
146
147 #if 0
148         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
149         add_trace("channel", NULL, "%d", i+1+(i>=15));
150         add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
151         end_trace();
152 #endif
153         return(1);
154 }
155
156
157 /*
158  * activate / deactivate request
159  */
160 void bchannel_activate(struct bchannel *bchannel, int activate)
161 {
162         struct mISDNhead act;
163         int ret;
164
165         /* activate bchannel */
166         CDEBUG(NULL, NULL, "%sActivating B-channel.\n", activate?"":"De-");
167         act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ; 
168         act.id = 0;
169         ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
170         if (ret < 0)
171                 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
172
173         bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
174 #if 0
175         /* trace */
176         chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
177         add_trace("channel", NULL, "%d", i+1+(i>=15));
178         end_trace();
179 #endif
180 }
181
182
183 /*
184  * set features
185  */
186 static void bchannel_activated(struct bchannel *bchannel)
187 {
188         int handle;
189
190         handle = bchannel->b_sock;
191
192         /* set dsp features */
193         if (bchannel->b_txdata)
194                 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
195         if (bchannel->b_delay)
196                 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
197         if (bchannel->b_tx_dejitter)
198                 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
199         if (bchannel->b_tx_gain)
200                 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
201         if (bchannel->b_rx_gain)
202                 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
203         if (bchannel->b_pipeline[0])
204                 ph_control_block(handle, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
205         if (bchannel->b_conf)
206                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
207         if (bchannel->b_echo)
208                 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
209         if (bchannel->b_tone)
210                 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
211         if (bchannel->b_rxoff)
212                 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
213 //      if (bchannel->b_txmix)
214 //              ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
215         if (bchannel->b_dtmf)
216                 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
217         if (bchannel->b_crypt_len)
218                 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
219         if (bchannel->b_conf)
220                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
221
222         bchannel->b_state = BSTATE_ACTIVE;
223 }
224
225 /*
226  * destroy stack
227  */
228 static void bchannel_destroy(struct bchannel *bchannel)
229 {
230 #if 0
231         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
232         add_trace("channel", NULL, "%d", i+1+(i>=15));
233         add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
234         end_trace();
235 #endif
236         if (bchannel->b_sock > -1)
237         {
238                 close(bchannel->b_sock);
239                 bchannel->b_sock = -1;
240         }
241         bchannel->b_state = BSTATE_IDLE;
242 }
243
244
245 /*
246  * whenever we get audio data from bchannel, we process it here
247  */
248 static void bchannel_receive(struct bchannel *bchannel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
249 {
250         unsigned long cont = *((unsigned long *)data);
251 //      unsigned char *data_temp;
252 //      unsigned long length_temp;
253 //      unsigned char *p;
254 //      int l;
255
256         if (prim == PH_CONTROL_IND)
257         {
258                 if (len < 4)
259                 {
260                         CERROR(NULL, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
261                         return;
262                 }
263                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
264                 {
265 #if 0
266                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
267                         add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
268                         end_trace();
269 #endif
270                         if (bchannel->call)
271                                 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
272                         return;
273                 }
274                 switch(cont)
275                 {
276                         case DSP_BF_REJECT:
277 #if 0
278                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
279                         add_trace("DSP-CRYPT", NULL, "error");
280                         end_trace();
281 #endif
282                         break;
283
284                         case DSP_BF_ACCEPT:
285 #if 0
286                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
287                         add_trace("DSP-CRYPT", NULL, "ok");
288                         end_trace();
289 #endif
290                         break;
291
292                         default:
293 #if 0
294                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
295                         add_trace("unknown", NULL, "0x%x", cont);
296                         end_trace();
297 #else
298                         ;
299 #endif
300                 }
301                 return;
302         }
303         if (prim == PH_DATA_REQ)
304         {
305                 if (!bchannel->b_txdata)
306                 {
307                         /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
308                         CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
309                         return;
310                 }
311                 return;
312         }
313         if (prim != PH_DATA_IND && prim != DL_DATA_IND)
314         {
315                 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", prim);
316                 return;
317         }
318         /* calls will not process any audio data unless
319          * the call is connected OR interface features audio during call setup.
320          */
321
322         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
323         if (bchannel->b_rxoff)
324         {
325                 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
326                 return;
327         }
328
329         if (!bchannel->call)
330         {
331                 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
332                 return;
333         }
334         if (!bchannel->call->audiopath)
335         {
336                 /* return, because we have no audio from port */
337                 return;
338         }
339         if (bchannel->call->pipe[1] > -1)
340         {
341                 len = write(bchannel->call->pipe[1], data, len);
342                 if (len < 0)
343                 {
344                         close(bchannel->call->pipe[1]);
345                         bchannel->call->pipe[1] = -1;
346                         CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
347                         return;
348                 }
349         }
350 }
351
352
353 /*
354  * transmit data to bchannel
355  */
356 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
357 {
358         unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
359         struct mISDNhead *frm = (struct mISDNhead *)buff;
360         int ret;
361         int i;
362
363         if (bchannel->b_state != BSTATE_ACTIVE)
364                 return;
365         if (len > 1024 || len < 1)
366                 return;
367         for (i = 0; i < len; i++)
368                 *p++ = flip_bits[*data++];
369         frm->prim = DL_DATA_REQ;
370         frm->id = 0;
371         ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
372         if (ret < 0)
373                 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
374 }
375
376
377 /*
378  * join bchannel
379  */
380 void bchannel_join(struct bchannel *bchannel, unsigned short id)
381 {
382         int handle;
383
384         handle = bchannel->b_sock;
385         if (id) {
386                 bchannel->b_conf = (id<<16) + bchannel_pid;
387                 bchannel->b_rxoff = 1;
388         } else {
389                 bchannel->b_conf = 0;
390                 bchannel->b_rxoff = 0;
391         }
392         if (bchannel->b_state == BSTATE_ACTIVE)
393         {
394                 ph_control(handle, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
395                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
396         }
397 }
398
399
400 /*
401  * dtmf bchannel
402  */
403 void bchannel_dtmf(struct bchannel *bchannel, int on)
404 {
405         int handle;
406
407         handle = bchannel->b_sock;
408         bchannel->b_dtmf = 1;
409         if (bchannel->b_state == BSTATE_ACTIVE)
410                 ph_control(handle, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1);
411 }
412
413
414 /*
415  * main loop for processing messages from mISDN
416  */
417 int bchannel_handle(void)
418 {
419         int ret, work = 0;
420         struct bchannel *bchannel;
421         char buffer[2048+MISDN_HEADER_LEN];
422         struct mISDNhead *hh = (struct mISDNhead *)buffer;
423
424         /* process all bchannels */
425         bchannel = bchannel_first;
426         while(bchannel)
427         {
428                 /* handle message from bchannel */
429                 if (bchannel->b_sock > -1)
430                 {
431                         ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
432                         if (ret >= (int)MISDN_HEADER_LEN)
433                         {
434                                 work = 1;
435                                 switch(hh->prim)
436                                 {
437                                         /* we don't care about confirms, we use rx data to sync tx */
438                                         case PH_DATA_CNF:
439                                         break;
440
441                                         /* we receive audio data, we respond to it AND we send tones */
442                                         case PH_DATA_IND:
443                                         case PH_DATA_REQ:
444                                         case DL_DATA_IND:
445                                         case PH_CONTROL_IND:
446                                         bchannel_receive(bchannel, hh->prim, hh->id, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
447                                         break;
448
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);
455                                         break;
456
457                                         case PH_DEACTIVATE_IND:
458                                         case DL_RELEASE_IND:
459                                         case PH_DEACTIVATE_CNF:
460                                         case DL_RELEASE_CNF:
461                                         CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
462 //                                      bchannel_deactivated(bchannel);
463                                         break;
464
465                                         default:
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);
467                                 }
468                         } else
469                         {
470                                 if (ret < 0 && errno != EWOULDBLOCK)
471                                         CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
472                         }
473                 }
474                 bchannel = bchannel->next;
475         }
476
477         /* if we received at least one b-frame, we will return 1 */
478         return(work);
479 }
480
481
482 /*
483  * bchannel channel handling
484  */
485 struct bchannel *bchannel_first = NULL;
486 struct bchannel *find_bchannel_handle(unsigned long handle)
487 {
488         struct bchannel *bchannel = bchannel_first;
489
490         while(bchannel)
491         {
492                 if (bchannel->handle == handle)
493                         break;
494                 bchannel = bchannel->next;
495         }
496         return(bchannel);
497 }
498
499 #if 0
500 struct bchannel *find_bchannel_ref(unsigned long ref)
501 {
502         struct bchannel *bchannel = bchannel_first;
503
504         while(bchannel)
505         {
506                 if (bchannel->ref == ref)
507                         break;
508                 bchannel = bchannel->next;
509         }
510         return(bchannel);
511 }
512 #endif
513
514 struct bchannel *alloc_bchannel(unsigned long handle)
515 {
516         struct bchannel **bchannelp = &bchannel_first;
517
518         while(*bchannelp)
519                 bchannelp = &((*bchannelp)->next);
520
521         *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
522         if (!*bchannelp)
523                 return(NULL);
524         (*bchannelp)->handle = handle;
525         (*bchannelp)->b_state = BSTATE_IDLE;
526                 
527         return(*bchannelp);
528 }
529
530 void free_bchannel(struct bchannel *bchannel)
531 {
532         struct bchannel **temp = &bchannel_first;
533
534         while(*temp)
535         {
536                 if (*temp == bchannel)
537                 {
538                         *temp = (*temp)->next;
539                         if (bchannel->b_sock > -1)
540                                 bchannel_destroy(bchannel);
541                         if (bchannel->call)
542                         {
543                                 if (bchannel->call->bchannel)
544                                         bchannel->call->bchannel = NULL;
545                         }
546                         free(bchannel);
547                         return;
548                 }
549                 temp = &((*temp)->next);
550         }
551 }
552
553