b6cb00d85793ad6b24eee4afa7fe9f7f686a5e18
[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, int b_mode)
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         if (b_mode != 0 && b_mode != 2)
74                 return;
75
76         CDEBUG(NULL, NULL, "Sending PH_CONTROL %s %x,%x\n", trace_name, c1, c2);
77         ctrl->prim = PH_CONTROL_REQ;
78         ctrl->id = 0;
79         *d++ = c1;
80         *d++ = c2;
81         ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
82         if (ret < 0)
83                 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
84 }
85
86 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value, int b_mode)
87 {
88         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
89         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
90         unsigned long *d = (unsigned long *)(buffer+MISDN_HEADER_LEN);
91         int ret;
92
93         if (b_mode != 0 && b_mode != 2)
94                 return;
95
96         CDEBUG(NULL, NULL, "Sending PH_CONTROL (block) %s %x\n", trace_name, c1);
97         ctrl->prim = PH_CONTROL_REQ;
98         ctrl->id = 0;
99         *d++ = c1;
100         memcpy(d, c2, c2_len);
101         ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
102         if (ret < 0)
103                 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
104 }
105
106
107 /*
108  * create stack
109  */
110 int bchannel_create(struct bchannel *bchannel, int mode)
111 {
112         int ret;
113         unsigned long on = 1;
114         struct sockaddr_mISDN addr;
115
116         if (bchannel->b_sock)
117         {
118                 CERROR(NULL, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
119                 return(0);
120         }
121
122         /* open socket */
123         bchannel->b_mode = mode;
124         switch(bchannel->b_mode)
125         {
126                 case 0:
127                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
128                 break;
129                 case 1:
130                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
131                 break;
132                 case 2:
133                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSPHDLC);
134                 break;
135                 case 3:
136                 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_HDLC);
137                 break;
138         }
139         if (bchannel->b_sock < 0)
140         {
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);
142                 return(0);
143         }
144         
145         /* set nonblocking io */
146         ret = ioctl(bchannel->b_sock, FIONBIO, &on);
147         if (ret < 0)
148         {
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;
152                 return(0);
153         }
154
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));
160         if (ret < 0)
161         {
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;
165                 return(0);
166         }
167         return(1);
168 }
169
170
171 /*
172  * activate / deactivate request
173  */
174 void bchannel_activate(struct bchannel *bchannel, int activate)
175 {
176         struct mISDNhead act;
177         int ret;
178
179         /* activate bchannel */
180         CDEBUG(NULL, NULL, "%sActivating B-channel.\n", activate?"":"De-");
181         switch(bchannel->b_mode)
182         {
183                 case 0:
184                 case 2:
185                 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ; 
186                 break;
187                 case 1:
188                 case 3:
189                 act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ; 
190                 break;
191         }
192         act.id = 0;
193         ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
194         if (ret < 0)
195                 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
196
197         bchannel->b_state = (activate)?BSTATE_ACTIVATING:BSTATE_DEACTIVATING;
198 }
199
200
201 /*
202  * set features
203  */
204 static void bchannel_activated(struct bchannel *bchannel)
205 {
206         int handle;
207
208         handle = bchannel->b_sock;
209
210         /* set dsp features */
211         if (bchannel->b_txdata)
212                 ph_control(handle, (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(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay, bchannel->b_mode);
215         if (bchannel->b_tx_dejitter)
216                 ph_control(handle, (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(handle, 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(handle, 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(handle, 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(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
225         if (bchannel->b_echo)
226                 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1, bchannel->b_mode);
227         if (bchannel->b_tone)
228                 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone, bchannel->b_mode);
229         if (bchannel->b_rxoff)
230                 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1, bchannel->b_mode);
231 //      if (bchannel->b_txmix)
232 //              ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1, bchannel->b_mode);
233         if (bchannel->b_dtmf)
234                 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1, bchannel->b_mode);
235         if (bchannel->b_bf_len)
236                 ph_control_block(handle, 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(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
239
240         bchannel->b_state = BSTATE_ACTIVE;
241 }
242
243 /*
244  * destroy stack
245  */
246 void bchannel_destroy(struct bchannel *bchannel)
247 {
248         if (bchannel->b_sock > -1)
249         {
250                 close(bchannel->b_sock);
251                 bchannel->b_sock = -1;
252         }
253         bchannel->b_state = BSTATE_IDLE;
254 }
255
256
257 /*
258  * whenever we get audio data from bchannel, we process it here
259  */
260 static void bchannel_receive(struct bchannel *bchannel, unsigned char *buffer, int len)
261 {
262         struct mISDNhead *hh = (struct mISDNhead *)buffer;
263         unsigned char *data = buffer + MISDN_HEADER_LEN;
264         unsigned long cont = *((unsigned long *)data);
265         struct bchannel *remote_bchannel;
266         int ret;
267
268         if (hh->prim == PH_CONTROL_IND)
269         {
270                 if (len < 4)
271                 {
272                         CERROR(NULL, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
273                         return;
274                 }
275                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
276                 {
277                         if (bchannel->call)
278                                 lcr_in_dtmf(bchannel->call, cont & DTMF_TONE_MASK);
279                         return;
280                 }
281                 switch(cont)
282                 {
283                         case DSP_BF_REJECT:
284                         CERROR(NULL, NULL, "Blowfish crypt rejected.\n");
285                         break;
286
287                         case DSP_BF_ACCEPT:
288                         CDEBUG(NULL, NULL, "Blowfish crypt enabled.\n");
289                         break;
290
291                         default:
292                         CDEBUG(NULL, NULL, "Unhandled bchannel control 0x%x.\n", cont);
293                 }
294                 return;
295         }
296         if (hh->prim == PH_DATA_REQ)
297         {
298                 if (!bchannel->b_txdata)
299                 {
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");
302                         return;
303                 }
304                 return;
305         }
306         if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND)
307         {
308                 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", hh->prim);
309                 return;
310         }
311         /* if call is bridged and in non-dsp mode */
312         if (bchannel->b_conf
313          && (bchannel->b_mode == 1 || bchannel->b_mode == 3)
314          && bchannel->call
315          && bchannel->call->bridge_call
316          && bchannel->call->bridge_call->bchannel)
317         {
318                 remote_bchannel = bchannel->call->bridge_call->bchannel;
319                 if (remote_bchannel->b_mode == 1 || remote_bchannel->b_mode == 3)
320                 {
321                         hh->prim = PH_DATA_REQ;
322                         ret = sendto(remote_bchannel->b_sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
323                         if (ret < 0)
324                                 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
325                         return;
326                 }
327         }
328         /* calls will not process any audio data unless
329          * the call is connected OR interface features audio during call setup.
330          */
331
332         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
333         if (bchannel->b_rxoff)
334         {
335                 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
336                 return;
337         }
338
339         if (!bchannel->call)
340         {
341                 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
342                 return;
343         }
344         if (!bchannel->call->audiopath)
345         {
346                 /* return, because we have no audio from port */
347                 return;
348         }
349         if (bchannel->call->pipe[1] > -1)
350         {
351                 len = write(bchannel->call->pipe[1], data, len);
352                 if (len < 0)
353                 {
354                         close(bchannel->call->pipe[1]);
355                         bchannel->call->pipe[1] = -1;
356                         CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
357                         return;
358                 }
359         }
360 }
361
362
363 /*
364  * transmit data to bchannel
365  */
366 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
367 {
368         unsigned char buff[1024 + MISDN_HEADER_LEN], *p = buff + MISDN_HEADER_LEN;
369         struct mISDNhead *frm = (struct mISDNhead *)buff;
370         int ret;
371         int i;
372
373         if (bchannel->b_state != BSTATE_ACTIVE)
374                 return;
375         if (len > 1024 || len < 1)
376                 return;
377         for (i = 0; i < len; i++)
378                 *p++ = flip_bits[*data++];
379         switch(bchannel->b_mode)
380         {
381                 case 0:
382                 case 2:
383                 frm->prim = DL_DATA_REQ;
384                 break;
385                 case 1:
386                 case 3:
387                 frm->prim = PH_DATA_REQ;
388                 break;
389         }
390         frm->id = 0;
391         ret = sendto(bchannel->b_sock, buff, MISDN_HEADER_LEN+len, 0, NULL, 0);
392         if (ret < 0)
393                 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
394 }
395
396
397 /*
398  * join bchannel
399  */
400 void bchannel_join(struct bchannel *bchannel, unsigned short id)
401 {
402         int handle;
403
404         handle = bchannel->b_sock;
405         if (id) {
406                 bchannel->b_conf = (id<<16) + bchannel_pid;
407                 bchannel->b_rxoff = 1;
408         } else {
409                 bchannel->b_conf = 0;
410                 bchannel->b_rxoff = 0;
411         }
412         if (bchannel->b_state == BSTATE_ACTIVE)
413         {
414                 ph_control(handle, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf, bchannel->b_mode);
415                 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf, bchannel->b_mode);
416         }
417 }
418
419
420 /*
421  * dtmf bchannel
422  */
423 void bchannel_dtmf(struct bchannel *bchannel, int on)
424 {
425         int handle;
426
427         handle = bchannel->b_sock;
428         bchannel->b_dtmf = 1;
429         if (bchannel->b_state == BSTATE_ACTIVE)
430                 ph_control(handle, on?DTMF_TONE_START:DTMF_TONE_STOP, 0, "DSP-DTMF", 1, bchannel->b_mode);
431 }
432
433
434 /*
435  * blowfish bchannel
436  */
437 void bchannel_blowfish(struct bchannel *bchannel, unsigned char *key, int len)
438 {
439         int handle;
440
441         handle = 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(handle, DSP_BF_ENABLE_KEY, bchannel->b_bf_key, bchannel->b_bf_len, "DSP-CRYPT", bchannel->b_bf_len, bchannel->b_mode);
446 }
447
448
449 /*
450  * pipeline bchannel
451  */
452 void bchannel_pipeline(struct bchannel *bchannel, char *pipeline)
453 {
454         int handle;
455
456         handle = 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(handle, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0, bchannel->b_mode);
460 }
461
462
463 /*
464  * gain bchannel
465  */
466 void bchannel_gain(struct bchannel *bchannel, int gain, int tx)
467 {
468         int handle;
469
470         handle = bchannel->b_sock;
471         if (tx)
472                 bchannel->b_tx_gain = gain;
473         else
474                 bchannel->b_rx_gain = gain;
475         if (bchannel->b_state == BSTATE_ACTIVE)
476                 ph_control(handle, (tx)?DSP_VOL_CHANGE_TX:DSP_VOL_CHANGE_RX, gain, (tx)?"DSP-TX_GAIN":"DSP-RX_GAIN", gain, bchannel->b_mode);
477 }
478
479
480 /*
481  * main loop for processing messages from mISDN
482  */
483 int bchannel_handle(void)
484 {
485         int ret, work = 0;
486         struct bchannel *bchannel;
487         char buffer[2048+MISDN_HEADER_LEN];
488         struct mISDNhead *hh = (struct mISDNhead *)buffer;
489
490         /* process all bchannels */
491         bchannel = bchannel_first;
492         while(bchannel)
493         {
494                 /* handle message from bchannel */
495                 if (bchannel->b_sock > -1)
496                 {
497                         ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
498                         if (ret >= (int)MISDN_HEADER_LEN)
499                         {
500                                 work = 1;
501                                 switch(hh->prim)
502                                 {
503                                         /* we don't care about confirms, we use rx data to sync tx */
504                                         case PH_DATA_CNF:
505                                         break;
506
507                                         /* we receive audio data, we respond to it AND we send tones */
508                                         case PH_DATA_IND:
509                                         case PH_DATA_REQ:
510                                         case DL_DATA_IND:
511                                         case PH_CONTROL_IND:
512                                         bchannel_receive(bchannel, buffer, ret-MISDN_HEADER_LEN);
513                                         break;
514
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);
521                                         break;
522
523                                         case PH_DEACTIVATE_IND:
524                                         case DL_RELEASE_IND:
525                                         case PH_DEACTIVATE_CNF:
526                                         case DL_RELEASE_CNF:
527                                         CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
528 //                                      bchannel_deactivated(bchannel);
529                                         break;
530
531                                         default:
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);
533                                 }
534                         } else
535                         {
536                                 if (ret < 0 && errno != EWOULDBLOCK)
537                                         CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
538                         }
539                 }
540                 bchannel = bchannel->next;
541         }
542
543         /* if we received at least one b-frame, we will return 1 */
544         return(work);
545 }
546
547
548 /*
549  * bchannel channel handling
550  */
551 struct bchannel *bchannel_first = NULL;
552 struct bchannel *find_bchannel_handle(unsigned long handle)
553 {
554         struct bchannel *bchannel = bchannel_first;
555
556         while(bchannel)
557         {
558                 if (bchannel->handle == handle)
559                         break;
560                 bchannel = bchannel->next;
561         }
562         return(bchannel);
563 }
564
565 #if 0
566 struct bchannel *find_bchannel_ref(unsigned long ref)
567 {
568         struct bchannel *bchannel = bchannel_first;
569
570         while(bchannel)
571         {
572                 if (bchannel->ref == ref)
573                         break;
574                 bchannel = bchannel->next;
575         }
576         return(bchannel);
577 }
578 #endif
579
580 struct bchannel *alloc_bchannel(unsigned long handle)
581 {
582         struct bchannel **bchannelp = &bchannel_first;
583
584         while(*bchannelp)
585                 bchannelp = &((*bchannelp)->next);
586
587         *bchannelp = (struct bchannel *)calloc(1, sizeof(struct bchannel));
588         if (!*bchannelp)
589                 return(NULL);
590         (*bchannelp)->handle = handle;
591         (*bchannelp)->b_state = BSTATE_IDLE;
592                 
593         return(*bchannelp);
594 }
595
596 void free_bchannel(struct bchannel *bchannel)
597 {
598         struct bchannel **temp = &bchannel_first;
599
600         while(*temp)
601         {
602                 if (*temp == bchannel)
603                 {
604                         *temp = (*temp)->next;
605                         if (bchannel->b_sock > -1)
606                                 bchannel_destroy(bchannel);
607                         if (bchannel->call)
608                         {
609                                 if (bchannel->call->bchannel)
610                                         bchannel->call->bchannel = NULL;
611                         }
612                         free(bchannel);
613                         return;
614                 }
615                 temp = &((*temp)->next);
616         }
617 }
618
619