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