585ae82ed85602e37ffeec825786f9c381d3e830
[lcr.git] / mISDN.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN port abstraction for dss1                                           **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13 #include "myisdn.h"
14 #include <mISDN/q931.h>
15
16 #undef offsetof
17 #ifdef __compiler_offsetof
18 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
19 #else
20 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
21 #endif
22
23 #ifndef container_of
24 #define container_of(ptr, type, member) ({                      \
25         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
26         (type *)( (char *)__mptr - offsetof(type,member) );})
27 #endif
28
29 // timeouts if activating/deactivating response from mISDN got lost
30 #define B_TIMER_ACTIVATING 1 // seconds
31 #define B_TIMER_DEACTIVATING 1 // seconds
32
33 /* list of mISDN ports */
34 struct mISDNport *mISDNport_first;
35
36 /* noise randomizer */
37 unsigned char mISDN_rand[256];
38 int mISDN_rand_count = 0;
39
40 #ifdef OLD_MT_ASSIGN
41 unsigned int mt_assign_pid = ~0;
42 #endif
43
44 int mISDNsocket = -1;
45 static int upqueue_pipe[2];
46 static struct lcr_fd upqueue_fd;
47 int upqueue_avail = 0;
48
49 static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, int i);
50 static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i);
51
52 static int my_mISDNlib_debug(const char *file, int line, const char *func, int level, const char *fmt, va_list va)
53 {
54         int ret = 0;
55
56         if (debug_fp > 0)
57                 ret = vfprintf(debug_fp, fmt, va);
58         return ret;
59 }
60
61 static struct mi_ext_fn_s myfn;
62
63 int mISDN_initialize(void)
64 {
65         char filename[256];
66         int ver;
67
68         /* try to open raw socket to check kernel */
69         mISDNsocket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
70         if (mISDNsocket < 0) {
71                 fprintf(stderr, "Cannot open mISDN due to '%s'. (Does your Kernel support socket based mISDN? Protocol family is %d.)\n", strerror(errno), PF_ISDN);
72                 return(-1);
73         }
74
75         /* init mlayer3 */
76         // set debug printout function
77         myfn.prt_debug = my_mISDNlib_debug;
78
79         ver = init_layer3(4, &myfn); // buffer of 4
80
81         /* open debug, if enabled and not only stack debugging */
82         if (options.deb) {
83                 SPRINT(filename, "%s/debug.log", LOG_DIR);
84                 debug_fp = fopen(filename, "a");
85         }
86
87         if (options.deb & DEBUG_STACK)
88                 mISDN_set_debug_level(0xfffffeff);
89         else
90                 mISDN_set_debug_level(0);
91
92         if (pipe(upqueue_pipe) < 0)
93                 FATAL("Failed to open pipe\n");
94         memset(&upqueue_fd, 0, sizeof(upqueue_fd));
95         upqueue_fd.fd = upqueue_pipe[0];
96         register_fd(&upqueue_fd, LCR_FD_READ, mISDN_upqueue, NULL, 0);
97
98         return(0);
99 }
100
101 void mISDN_deinitialize(void)
102 {
103         cleanup_layer3();
104
105         if (debug_fp)
106                 fclose(debug_fp);
107         debug_fp = NULL;
108
109         if (mISDNsocket > -1)
110                 close(mISDNsocket);
111
112         if (upqueue_fd.inuse) {
113                 unregister_fd(&upqueue_fd);
114                 close(upqueue_pipe[0]);
115                 close(upqueue_pipe[1]);
116         }
117         upqueue_avail = 0;
118 }
119
120 int load_timer(struct lcr_timer *timer, void *instance, int index);
121
122 /*
123  * constructor
124  */
125 PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : Port(type, portname, settings)
126 {
127         p_m_mISDNport = mISDNport;
128         p_m_portnum = mISDNport->portnum;
129         p_m_b_index = -1;
130         p_m_b_channel = 0;
131         p_m_b_exclusive = 0;
132         p_m_b_reserve = 0;
133         p_m_b_mode = mode;
134         p_m_hold = 0;
135         p_m_tx_gain = mISDNport->ifport->interface->tx_gain;
136         p_m_rx_gain = mISDNport->ifport->interface->rx_gain;
137         p_m_conf = 0;
138         p_m_mute = 0;
139         p_m_txdata = 0;
140         p_m_delay = 0;
141         p_m_tx_dejitter = 0;
142         p_m_echo = 0;
143         p_m_tone = 0;
144         p_m_rxoff = 0;
145         p_m_inband_send_on = 0;
146         p_m_inband_receive_on = 0;
147         p_m_dtmf = !mISDNport->ifport->nodtmf;
148         memset(&p_m_timeout, 0, sizeof(p_m_timeout));
149         add_timer(&p_m_timeout, mISDN_timeout, this, 0);
150         SCPY(p_m_pipeline, mISDNport->ifport->interface->pipeline);
151         
152         /* audio */
153         memset(&p_m_loadtimer, 0, sizeof(p_m_loadtimer));
154         add_timer(&p_m_loadtimer, load_timer, this, 0);
155         p_m_load = 0;
156         p_m_last_tv_sec = 0;
157
158         /* crypt */
159         p_m_crypt = 0;
160         p_m_crypt_listen = 0;
161         p_m_crypt_msg_loops = 0;
162         p_m_crypt_msg_loops = 0;
163         p_m_crypt_msg_len = 0;
164         p_m_crypt_msg[0] = '\0';
165         p_m_crypt_msg_current = 0;
166         p_m_crypt_key_len = 0;
167         p_m_crypt_listen = 0;
168         p_m_crypt_listen_state = 0;
169         p_m_crypt_listen_len = 0;
170         p_m_crypt_listen_msg[0] = '\0';
171         p_m_crypt_listen_crc = 0;
172         if (mISDNport->ifport->interface->bf_len >= 4 && mISDNport->ifport->interface->bf_len <= 56) {
173                 memcpy(p_m_crypt_key, mISDNport->ifport->interface->bf_key, p_m_crypt_key_len);
174                 p_m_crypt_key_len = mISDNport->ifport->interface->bf_len;
175                 p_m_crypt = 1;
176         }
177
178         /* if any channel requested by constructor */
179         if (channel == CHANNEL_ANY) {
180                 /* reserve channel */
181                 p_m_b_reserve = 1;
182                 mISDNport->b_reserved++;
183         }
184
185         /* reserve channel */
186         if (channel > 0) // only if constructor was called with a channel resevation
187                 seize_bchannel(channel, exclusive);
188
189         /* we increase the number of objects: */
190         mISDNport->use++;
191         PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, port #%d\n", portname, mISDNport->use, p_m_portnum);
192 //inband_receive_on();
193 }
194
195
196 /*
197  * destructor
198  */
199 PmISDN::~PmISDN()
200 {
201         struct lcr_msg *message;
202
203         del_timer(&p_m_timeout);
204         del_timer(&p_m_loadtimer);
205
206         /* remove bchannel relation */
207         drop_bchannel();
208
209         /* release epoint */
210         while (p_epointlist) {
211                 PDEBUG(DEBUG_ISDN, "destroy mISDNPort(%s). endpoint still exists, releaseing.\n", p_name);
212                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
213                 message->param.disconnectinfo.cause = 16;
214                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
215                 message_put(message);
216                 /* remove from list */
217                 free_epointlist(p_epointlist);
218         }
219
220         /* we decrease the number of objects: */
221         p_m_mISDNport->use--;
222         PDEBUG(DEBUG_ISDN, "destroyed mISDNPort(%s). Currently %d objects\n", p_name, p_m_mISDNport->use);
223 }
224
225
226 /*
227  * trace
228  */
229 void chan_trace_header(struct mISDNport *mISDNport, class PmISDN *port, const char *msgtext, int direction)
230 {
231         /* init trace with given values */
232         start_trace(mISDNport?mISDNport->portnum:-1,
233                     (mISDNport)?((mISDNport->ifport)?mISDNport->ifport->interface:NULL):NULL,
234                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
235                     port?port->p_dialinginfo.id:NULL,
236                     direction,
237                     CATEGORY_CH,
238                     port?port->p_serial:0,
239                     msgtext);
240 }
241
242
243 /*
244  * layer trace header
245  */
246 static struct isdn_message {
247         const char *name;
248         unsigned int value;
249 } isdn_message[] = {
250         {"PH_ACTIVATE", L1_ACTIVATE_REQ},
251         {"PH_DEACTIVATE", L1_DEACTIVATE_REQ},
252         {"DL_ESTABLISH", L2_ESTABLISH_REQ},
253         {"DL_RELEASE", L2_RELEASE_REQ},
254         {"UNKNOWN", L3_UNKNOWN_REQ},
255         {"MT_TIMEOUT", L3_TIMEOUT_REQ},
256         {"MT_SETUP", L3_SETUP_REQ},
257         {"MT_SETUP_ACK", L3_SETUP_ACKNOWLEDGE_REQ},
258         {"MT_PROCEEDING", L3_PROCEEDING_REQ},
259         {"MT_ALERTING", L3_ALERTING_REQ},
260         {"MT_CONNECT", L3_CONNECT_REQ},
261         {"MT_CONNECT_ACK", L3_CONNECT_ACKNOWLEDGE_REQ},
262         {"MT_DISCONNECT", L3_DISCONNECT_REQ},
263         {"MT_RELEASE", L3_RELEASE_REQ},
264         {"MT_RELEASE_COMP", L3_RELEASE_COMPLETE_REQ},
265         {"MT_INFORMATION", L3_INFORMATION_REQ},
266         {"MT_PROGRESS", L3_PROGRESS_REQ},
267         {"MT_NOTIFY", L3_NOTIFY_REQ},
268         {"MT_SUSPEND", L3_SUSPEND_REQ},
269         {"MT_SUSPEND_ACK", L3_SUSPEND_ACKNOWLEDGE_REQ},
270         {"MT_SUSPEND_REJ", L3_SUSPEND_REJECT_REQ},
271         {"MT_RESUME", L3_RESUME_REQ},
272         {"MT_RESUME_ACK", L3_RESUME_ACKNOWLEDGE_REQ},
273         {"MT_RESUME_REJ", L3_RESUME_REJECT_REQ},
274         {"MT_HOLD", L3_HOLD_REQ},
275         {"MT_HOLD_ACK", L3_HOLD_ACKNOWLEDGE_REQ},
276         {"MT_HOLD_REJ", L3_HOLD_REJECT_REQ},
277         {"MT_RETRIEVE", L3_RETRIEVE_REQ},
278         {"MT_RETRIEVE_ACK", L3_RETRIEVE_ACKNOWLEDGE_REQ},
279         {"MT_RETRIEVE_REJ", L3_RETRIEVE_REJECT_REQ},
280         {"MT_FACILITY", L3_FACILITY_REQ},
281         {"MT_STATUS", L3_STATUS_REQ},
282         {"MT_RESTART", L3_RESTART_REQ},
283         {"MT_NEW_L3ID", L3_NEW_L3ID_REQ},
284         {"MT_RELEASE_L3ID", L3_RELEASE_L3ID_REQ},
285         {NULL, 0},
286 };
287 static const char *isdn_prim[4] = {
288         " REQUEST",
289         " CONFIRM",
290         " INDICATION",
291         " RESPONSE",
292 };
293 void l1l2l3_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg, int direction)
294 {
295         int i;
296         char msgtext[64];
297
298         SCPY(msgtext, "<<UNKNOWN MESSAGE>>");
299         /* select message and primitive text */
300         i = 0;
301         while(isdn_message[i].name) {
302 //              if (msg == L3_NOTIFY_REQ) printf("val = %x %s\n", isdn_message[i].value, isdn_message[i].name);
303                 if (isdn_message[i].value == (msg&0xffffff00)) {
304                         SCPY(msgtext, isdn_message[i].name);
305                         break;
306                 }
307                 i++;
308         }
309         SCAT(msgtext, isdn_prim[msg&0x00000003]);
310
311         /* add direction */
312         if (direction && (msg&0xffffff00)!=L3_NEW_L3ID_REQ && (msg&0xffffff00)!=L3_RELEASE_L3ID_REQ) {
313                 if (mISDNport) {
314                         if (mISDNport->ntmode) {
315                                 if (direction == DIRECTION_OUT)
316                                         SCAT(msgtext, " N->U");
317                                 else
318                                         SCAT(msgtext, " N<-U");
319                         } else {
320                                 if (direction == DIRECTION_OUT)
321                                         SCAT(msgtext, " U->N");
322                                 else
323                                         SCAT(msgtext, " U<-N");
324                         }
325                 }
326         }
327
328         /* init trace with given values */
329         start_trace(mISDNport?mISDNport->portnum:-1,
330                     mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
331                     port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
332                     port?port->p_dialinginfo.id:NULL,
333                     direction,
334                     CATEGORY_CH,
335                     port?port->p_serial:0,
336                     msgtext);
337 }
338
339
340 /*
341  * send control information to the channel (dsp-module)
342  */
343 void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, int sock, unsigned int c1, unsigned int c2, const char *trace_name, int trace_value)
344 {
345         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
346         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
347         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
348         int ret;
349
350         if (sock < 0)
351                 return;
352
353         ctrl->prim = PH_CONTROL_REQ;
354         ctrl->id = 0;
355         *d++ = c1;
356         *d++ = c2;
357         ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
358         if (ret <= 0)
359                 PERROR("Failed to send to socket %d\n", sock);
360         chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
361         if (c1 == DSP_CONF_JOIN)
362                 add_trace(trace_name, NULL, "0x%08x", trace_value);
363         else
364                 add_trace(trace_name, NULL, "%d", trace_value);
365         end_trace();
366 }
367
368 void ph_control_block(struct mISDNport *mISDNport, class PmISDN *isdnport, int sock, unsigned int c1, void *c2, int c2_len, const char *trace_name, int trace_value)
369 {
370         unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
371         struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
372         unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
373         int ret;
374
375         if (sock < 0)
376                 return;
377
378         ctrl->prim = PH_CONTROL_REQ;
379         ctrl->id = 0;
380         *d++ = c1;
381         memcpy(d, c2, c2_len);
382         ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
383         if (ret <= 0)
384                 PERROR("Failed to send to socket %d\n", sock);
385         chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
386         add_trace(trace_name, NULL, "%d", trace_value);
387         end_trace();
388 }
389
390 static int b_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int i);
391
392 /*
393  * subfunction for bchannel_event
394  * create stack
395  */
396 static int _bchannel_create(struct mISDNport *mISDNport, int i)
397 {
398         int ret;
399         struct sockaddr_mISDN addr;
400
401         if (mISDNport->b_sock[i].inuse) {
402                 PERROR("Error: Socket already created for index %d\n", i);
403                 return(0);
404         }
405
406         /* open socket */
407 //#warning testing without DSP
408 //      mISDNport->b_sock[i].fd = socket(PF_ISDN, SOCK_DGRAM, (mISDNport->b_mode[i]==B_MODE_HDLC)?ISDN_P_B_HDLC:ISDN_P_B_RAW);
409         mISDNport->b_sock[i].fd = socket(PF_ISDN, SOCK_DGRAM, (mISDNport->b_mode[i]==B_MODE_HDLC)?ISDN_P_B_L2DSPHDLC:ISDN_P_B_L2DSP);
410         if (mISDNport->b_sock[i].fd < 0) {
411                 PERROR("Error: Failed to open bchannel-socket for index %d with mISDN-DSP layer. Did you load mISDN_dsp.ko?\n", i);
412                 return(0);
413         }
414
415         /* register callback for read */
416         register_fd(&mISDNport->b_sock[i], LCR_FD_READ, b_sock_callback, mISDNport, i);
417         
418         /* bind socket to bchannel */
419         addr.family = AF_ISDN;
420         addr.dev = mISDNport->portnum;
421         addr.channel = i+1+(i>=15);
422         ret = bind(mISDNport->b_sock[i].fd, (struct sockaddr *)&addr, sizeof(addr));
423         if (ret < 0) {
424                 PERROR("Error: Failed to bind bchannel-socket for index %d with mISDN-DSP layer (errno=%d). Did you load mISDN_dsp.ko?\n", i, errno);
425                 close(mISDNport->b_sock[i].fd);
426                 unregister_fd(&mISDNport->b_sock[i]);
427                 return(0);
428         }
429
430         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
431         add_trace("channel", NULL, "%d", i+1+(i>=15));
432         add_trace("socket", NULL, "%d", mISDNport->b_sock[i].fd);
433         end_trace();
434
435         return(1);
436 }
437
438
439 /*
440  * subfunction for bchannel_event
441  * activate / deactivate request
442  */
443 static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate, int timeout)
444 {
445         struct mISDNhead act;
446         int ret;
447
448         if (!mISDNport->b_sock[i].inuse)
449                 return;
450         act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ; 
451         act.id = 0;
452         ret = sendto(mISDNport->b_sock[i].fd, &act, MISDN_HEADER_LEN, 0, NULL, 0);
453         if (ret <= 0)
454                 PERROR("Failed to send to socket %d\n", mISDNport->b_sock[i].fd);
455
456         /* trace */
457         chan_trace_header(mISDNport, mISDNport->b_port[i], activate ? "BCHANNEL activate" : "BCHANNEL deactivate", DIRECTION_OUT);
458         add_trace("channel", NULL, "%d", i+1+(i>=15));
459         if (timeout)
460                 add_trace("event", NULL, "timeout recovery");
461         end_trace();
462 }
463
464
465 /*
466  * subfunction for bchannel_event
467  * set features
468  */
469 static void _bchannel_configure(struct mISDNport *mISDNport, int i)
470 {
471         struct PmISDN *port;
472         int handle, mode;
473
474         if (!mISDNport->b_sock[i].inuse)
475                 return;
476         handle = mISDNport->b_sock[i].fd;
477         port = mISDNport->b_port[i];
478         mode = mISDNport->b_mode[i];
479         if (!port) {
480                 PERROR("bchannel index i=%d not associated with a port object\n", i);
481                 return;
482         }
483
484         /* set dsp features */
485         if (port->p_m_txdata)
486                 ph_control(mISDNport, port, handle, (port->p_m_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", port->p_m_txdata);
487         if (port->p_m_delay && mode == B_MODE_TRANSPARENT)
488                 ph_control(mISDNport, port, handle, DSP_DELAY, port->p_m_delay, "DSP-DELAY", port->p_m_delay);
489         if (port->p_m_tx_dejitter && mode == B_MODE_TRANSPARENT)
490                 ph_control(mISDNport, port, handle, DSP_TX_DEJITTER, port->p_m_tx_dejitter, "DSP-TX_DEJITTER", port->p_m_tx_dejitter);
491         if (port->p_m_tx_gain && mode == B_MODE_TRANSPARENT)
492                 ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_TX, port->p_m_tx_gain, "DSP-TX_GAIN", port->p_m_tx_gain);
493         if (port->p_m_rx_gain && mode == B_MODE_TRANSPARENT)
494                 ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_RX, port->p_m_rx_gain, "DSP-RX_GAIN", port->p_m_rx_gain);
495         if (port->p_m_pipeline[0] && mode == B_MODE_TRANSPARENT)
496                 ph_control_block(mISDNport, port, handle, DSP_PIPELINE_CFG, port->p_m_pipeline, strlen(port->p_m_pipeline)+1, "DSP-PIPELINE", 0);
497         if (port->p_m_conf && !port->p_m_mute)
498                 ph_control(mISDNport, port, handle, DSP_CONF_JOIN, port->p_m_conf, "DSP-CONF", port->p_m_conf);
499         if (port->p_m_echo)
500                 ph_control(mISDNport, port, handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
501         if (port->p_m_tone && mode == B_MODE_TRANSPARENT)
502                 ph_control(mISDNport, port, handle, DSP_TONE_PATT_ON, port->p_m_tone, "DSP-TONE", port->p_m_tone);
503         if (port->p_m_rxoff)
504                 ph_control(mISDNport, port, handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
505 //      if (port->p_m_txmix && mode == B_MODE_TRANSPARENT)
506 //              ph_control(mISDNport, port, handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
507         if (port->p_m_dtmf && mode == B_MODE_TRANSPARENT)
508                 ph_control(mISDNport, port, handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
509         if (port->p_m_crypt && mode == B_MODE_TRANSPARENT)
510                 ph_control_block(mISDNport, port, handle, DSP_BF_ENABLE_KEY, port->p_m_crypt_key, port->p_m_crypt_key_len, "DSP-CRYPT", port->p_m_crypt_key_len);
511 }
512
513
514 void PmISDN::set_conf(int oldconf, int newconf)
515 {
516                 if (oldconf != newconf) {
517                         PDEBUG(DEBUG_BCHANNEL, "we change conference from conf=%d to conf=%d.\n", oldconf, newconf);
518                         if (p_m_b_index > -1)
519                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
520                                 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, (newconf)?DSP_CONF_JOIN:DSP_CONF_SPLIT, newconf, "DSP-CONF", newconf);
521                 } else
522                         PDEBUG(DEBUG_BCHANNEL, "we already have conf=%d.\n", newconf);
523 }
524
525
526 /*
527  * subfunction for bchannel_event
528  * destroy stack
529  */
530 static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
531 {
532         if (!mISDNport->b_sock[i].inuse)
533                 return;
534         chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
535         add_trace("channel", NULL, "%d", i+1+(i>=15));
536         add_trace("socket", NULL, "%d", mISDNport->b_sock[i].fd);
537         end_trace();
538         close(mISDNport->b_sock[i].fd);
539         unregister_fd(&mISDNport->b_sock[i]);
540 }
541
542
543 /*
544 bchannel procedure
545 ------------------
546
547 A bchannel goes through the following states in this order:
548
549 - B_STATE_IDLE
550 No one is using the bchannel.
551 It is available and not linked to Port class, nor reserved.
552
553 - B_STATE_ACTIVATING
554 The bchannel stack is created and an activation request is sent.
555 It MAY be linked to Port class, but already unlinked due to Port class removal.
556
557 - B_STATE_ACTIVE
558 The bchannel is active and cofigured to the Port class needs.
559 Also it is linked to a Port class, otherwhise it would be deactivated.
560
561 - B_STATE_DEACTIVATING
562 The bchannel is in deactivating state, due to deactivation request.
563 It may be linked to a Port class, that likes to reactivate it.
564
565 - B_STATE_IDLE
566 See above.
567 After deactivating bchannel, and if not used, the bchannel becomes idle again.
568
569 A bchannel can have the following events:
570
571 - B_EVENT_USE
572 A bchannel is required by a Port class.
573
574 - B_EVENT_ACTIVATED
575 The bchannel beomes active.
576
577 - B_EVENT_DROP
578 The bchannel is not required by Port class anymore
579
580 - B_EVENT_DEACTIVATED
581 The bchannel becomes inactive.
582
583 All actions taken on these events depend on the current bchannel's state and if it is linked to a Port class.
584
585 */
586
587 /*
588  * process bchannel events
589  * - mISDNport is a pointer to the port's structure
590  * - i is the index of the bchannel
591  * - event is the B_EVENT_* value
592  * - port is the PmISDN class pointer
593  */
594 void bchannel_event(struct mISDNport *mISDNport, int i, int event)
595 {
596         class PmISDN *b_port = mISDNport->b_port[i];
597         int state = mISDNport->b_state[i];
598         int timer = -1; // no change
599         int p_m_tx_gain = 0;
600         int p_m_rx_gain = 0;
601         char *p_m_pipeline = NULL;
602         unsigned char *p_m_crypt_key = NULL;
603         int p_m_crypt_key_len = 0;
604         int p_m_crypt_key_type = 0;
605
606         if (b_port) {
607                 p_m_tx_gain = b_port->p_m_tx_gain;
608                 p_m_rx_gain = b_port->p_m_rx_gain;
609                 p_m_pipeline = b_port->p_m_pipeline;
610                 p_m_crypt_key = b_port->p_m_crypt_key;
611                 p_m_crypt_key_len = b_port->p_m_crypt_key_len;
612                 p_m_crypt_key_type = /*b_port->p_m_crypt_key_type*/1;
613         }
614
615         switch(event) {
616                 case B_EVENT_USE:
617                 /* port must be linked in order to allow activation */
618                 if (!b_port)
619                         FATAL("bchannel must be linked to a Port class\n");
620                 switch(state) {
621                         case B_STATE_IDLE:
622                         /* create stack and send activation request */
623                         if (_bchannel_create(mISDNport, i)) {
624                                 _bchannel_activate(mISDNport, i, 1, 0);
625                                 state = B_STATE_ACTIVATING;
626                                 timer = B_TIMER_ACTIVATING;
627                         }
628                         break;
629
630                         case B_STATE_ACTIVATING:
631                         /* do nothing, because it is already activating */
632                         break;
633
634                         default:
635                         /* problems that might ocurr:
636                          * B_EVENT_USE is received when channel already in use.
637                          */
638                         PERROR("Illegal event %d at state %d, please correct.\n", event, state);
639                 }
640                 break;
641
642
643                 case B_EVENT_ACTIVATED:
644                 timer = 0;
645                 switch(state) {
646                         case B_STATE_ACTIVATING:
647                         if (b_port) {
648                                 /* bchannel is active and used by Port class, so we configure bchannel */
649                                 _bchannel_configure(mISDNport, i);
650                                 state = B_STATE_ACTIVE;
651                                 b_port->p_m_load = 0;
652                         } else {
653                                 /* bchannel is active, but not used anymore (or has wrong stack config), so we deactivate */
654                                 _bchannel_activate(mISDNport, i, 0, 0);
655                                 state = B_STATE_DEACTIVATING;
656                                 timer = B_TIMER_DEACTIVATING;
657                         }
658                         break;
659
660                         default:
661                         PERROR("Illegal event %d at state %d, please correct.\n", event, state);
662                 }
663                 break;
664
665                 case B_EVENT_DROP:
666                 if (!b_port)
667                         FATAL("bchannel must be linked to a Port class\n");
668                 switch(state) {
669                         case B_STATE_IDLE:
670                         /* bchannel is idle due to an error, so we do nothing */
671                         break;
672
673                         case B_STATE_ACTIVATING:
674                         /* do nothing because we must wait until bchanenl is active before deactivating */
675                         break;
676
677                         case B_STATE_ACTIVE:
678                         /* bchannel is active, so we deactivate */
679                         _bchannel_activate(mISDNport, i, 0, 0);
680                         state = B_STATE_DEACTIVATING;
681                         timer = B_TIMER_DEACTIVATING;
682                         break;
683
684                         case B_STATE_DEACTIVATING:
685                         /* we may have taken an already deactivating bchannel, but do not require it anymore, so we do nothing */
686                         break;
687
688                         default:
689                         PERROR("Illegal event %d at state %d, please correct.\n", event, state);
690                 }
691                 break;
692
693                 case B_EVENT_DEACTIVATED:
694                 timer = 0;
695                 switch(state) {
696                         case B_STATE_IDLE:
697                         /* ignore due to deactivation confirm after unloading */
698                         break;
699
700                         case B_STATE_DEACTIVATING:
701                         _bchannel_destroy(mISDNport, i);
702                         state = B_STATE_IDLE;
703                         if (b_port) {
704                                 /* bchannel is now deactivate, but is requied by Port class, so we reactivate */
705                                 if (_bchannel_create(mISDNport, i)) {
706                                         _bchannel_activate(mISDNport, i, 1, 0);
707                                         state = B_STATE_ACTIVATING;
708                                         timer = B_TIMER_ACTIVATING;
709                                 }
710                         }
711                         break;
712
713                         default:
714                         PERROR("Illegal event %d at state %d, please correct.\n", event, state);
715                 }
716                 break;
717
718                 case B_EVENT_TIMEOUT:
719                 timer = 0;
720                 switch(state) {
721                         case B_STATE_IDLE:
722                         /* ignore due to deactivation confirm after unloading */
723                         break;
724
725                         case B_STATE_ACTIVATING:
726                         _bchannel_activate(mISDNport, i, 1, 1);
727                         timer = B_TIMER_ACTIVATING;
728                         break;
729
730                         case B_STATE_DEACTIVATING:
731                         _bchannel_activate(mISDNport, i, 0, 1);
732                         timer = B_TIMER_DEACTIVATING;
733                         break;
734
735                         default:
736                         PERROR("Illegal event %d at state %d, please correct.\n", event, state);
737                 }
738                 break;
739
740                 default:
741                 PERROR("Illegal event %d, please correct.\n", event);
742         }
743
744         mISDNport->b_state[i] = state;
745         if (timer == 0)
746                 unsched_timer(&mISDNport->b_timer[i]);
747         else if (timer > 0)
748                 schedule_timer(&mISDNport->b_timer[i], timer, 0);
749 }
750
751
752
753
754 /*
755  * check for available channel and reserve+set it.
756  * give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
757  * give exclusiv flag
758  * returns -(cause value) or x = channel x or 0 = no channel
759  * NOTE: no activation is done here
760  */
761 int PmISDN::seize_bchannel(int channel, int exclusive)
762 {
763         int i;
764
765         /* the channel is what we have */
766         if (p_m_b_channel == channel)
767                 return(channel);
768
769         /* if channel already in use, release it */
770         if (p_m_b_channel)
771                 drop_bchannel();
772
773         /* if CHANNEL_NO */
774         if (channel==CHANNEL_NO || channel==0)
775                 return(0);
776         
777         /* is channel in range ? */
778         if (channel==16
779          || (channel>p_m_mISDNport->b_num && channel<16)
780          || ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
781                 return(-6); /* channel unacceptable */
782
783         /* request exclusive channel */
784         if (exclusive && channel>0) {
785                 i = channel-1-(channel>16);
786                 if (p_m_mISDNport->b_port[i])
787                         return(-44); /* requested channel not available */
788                 goto seize;
789         }
790
791         /* ask for channel */
792         if (channel>0) {
793                 i = channel-1-(channel>16);
794                 if (p_m_mISDNport->b_port[i] == NULL)
795                         goto seize;
796         }
797
798         /* search for channel */
799         i = 0;
800         while(i < p_m_mISDNport->b_num) {
801                 if (!p_m_mISDNport->b_port[i]) {
802                         channel = i+1+(i>=15);
803                         goto seize;
804                 }
805                 i++;
806         }
807         return(-34); /* no free channel */
808
809 seize:
810         PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
811
812         /* link Port, set parameters */
813         p_m_mISDNport->b_port[i] = this;
814         p_m_b_index = i;
815         p_m_b_channel = channel;
816         p_m_b_exclusive = exclusive;
817         p_m_mISDNport->b_mode[i] = p_m_b_mode;
818
819         /* reserve channel */
820         if (!p_m_b_reserve) {
821                 p_m_b_reserve = 1;
822                 p_m_mISDNport->b_reserved++;
823         }
824
825         return(channel);
826 }
827
828 /*
829  * drop reserved channel and unset it.
830  * deactivation is also done
831  */
832 void PmISDN::drop_bchannel(void)
833 {
834         /* unreserve channel */
835         if (p_m_b_reserve)
836                 p_m_mISDNport->b_reserved--;
837         p_m_b_reserve = 0;
838
839         /* if not in use */
840         if (p_m_b_index < 0)
841                 return;
842         if (!p_m_b_channel)
843                 return;
844
845         PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
846
847         if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_IDLE)
848                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_DROP);
849         p_m_mISDNport->b_port[p_m_b_index] = NULL;
850         p_m_mISDNport->b_mode[p_m_b_index] = 0;
851         p_m_b_index = -1;
852         p_m_b_channel = 0;
853         p_m_b_exclusive = 0;
854 }
855
856
857 /*
858  * handler
859
860 audio transmission procedure:
861 -----------------------------
862
863 * priority
864 three sources of audio transmission:
865 - crypto-data high priority
866 - tones high priority (also high)
867 - remote-data low priority
868
869 * elapsed
870 a variable that temporarily shows the number of samples elapsed since last transmission process.
871 p_m_last_tv_* is used to store that last timestamp. this is used to calculate the time elapsed.
872
873 * load
874 a variable that is increased whenever data is transmitted.
875 it is decreased while time elapses. it stores the number of samples that
876 are currently loaded to dsp module.
877 since clock in dsp module is the same clock for user space process, these 
878 times have no skew.
879
880 * levels
881 there are two levels:
882 ISDN_LOAD will give the load that have to be kept in dsp.
883 ISDN_MAXLOAD will give the maximum load before dropping.
884
885 * procedure for low priority data
886 see txfromup() for procedure
887 in short: remote data is ignored during high priority tones
888
889 * procedure for high priority data
890 whenever load is below ISDN_LOAD, load is filled up to ISDN_LOAD
891 if no more data is available, load becomes empty again.
892
893 'load' variable:
894 0                    ISDN_LOAD           ISDN_MAXLOAD
895 +--------------------+----------------------+
896 |                    |                      |
897 +--------------------+----------------------+
898
899 on empty load or on load below ISDN_LOAD, the load is inceased to ISDN_LOAD:
900 0                    ISDN_LOAD           ISDN_MAXLOAD
901 +--------------------+----------------------+
902 |TTTTTTTTTTTTTTTTTTTT|                      |
903 +--------------------+----------------------+
904
905 on empty load, remote-audio causes the load with the remote audio to be increased to ISDN_LOAD.
906 0                    ISDN_LOAD           ISDN_MAXLOAD
907 +--------------------+----------------------+
908 |TTTTTTTTTTTTTTTTTTTTRRRRR                  |
909 +--------------------+----------------------+
910
911  */
912 void PmISDN::update_load(void)
913 {
914         /* don't trigger load event if: */
915         if (!p_tone_name[0] && !p_m_crypt_msg_loops && !p_m_inband_send_on)
916                 return;
917
918         /* don't trigger load event if event already active */
919         if (p_m_loadtimer.active)
920                 return;
921
922         schedule_timer(&p_m_loadtimer, 0, 0); /* no delay the first time */
923 }
924
925 int load_timer(struct lcr_timer *timer, void *instance, int index)
926 {
927         class PmISDN *isdnport = (class PmISDN *)instance;
928
929         isdnport->load_tx();
930
931         return 0;
932 }
933
934 void PmISDN::load_tx(void)
935 {
936         int elapsed = 0;
937         int ret;
938         struct timeval current_time;
939
940         /* get elapsed */
941         gettimeofday(&current_time, NULL);
942         if (p_m_last_tv_sec) {
943                 elapsed = 8000 * (current_time.tv_sec - p_m_last_tv_sec)
944                         + 8 * (current_time.tv_usec/1000 - p_m_last_tv_msec);
945         }
946         /* set clock of last process! */
947         p_m_last_tv_sec = current_time.tv_sec;
948         p_m_last_tv_msec = current_time.tv_usec/1000;
949
950         /* process only if we have samples and we are active */
951         if (elapsed && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE) {
952                 /* update load */
953                 if (elapsed < p_m_load)
954                         p_m_load -= elapsed;
955                 else
956                         p_m_load = 0;
957
958                 /* to send data, tone must be on */
959                 if ((p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on) /* what tones? */
960                  && (p_m_load < ISDN_LOAD) /* not too much load? */
961                  && (p_state==PORT_STATE_CONNECT || p_m_mISDNport->tones || p_m_inband_send_on)) { /* connected or inband-tones? */
962                         int tosend = ISDN_LOAD - p_m_load, length; 
963                         unsigned char buf[MISDN_HEADER_LEN+tosend];
964                         struct mISDNhead *frm = (struct mISDNhead *)buf;
965                         unsigned char *p = buf+MISDN_HEADER_LEN;
966
967                         /* copy inband signalling (e.g. used by ss5) */
968                         if (p_m_inband_send_on && tosend) {
969                                 tosend -= inband_send(p, tosend);
970                         }
971
972                         /* copy crypto loops */
973                         while (p_m_crypt_msg_loops && tosend) {
974                                 /* how much do we have to send */
975                                 length = p_m_crypt_msg_len - p_m_crypt_msg_current;
976
977                                 /* clip tosend */
978                                 if (length > tosend)
979                                         length = tosend;
980
981                                 /* copy message (part) to buffer */
982                                 memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, length);
983
984                                 /* new position */
985                                 p_m_crypt_msg_current += length;
986                                 if (p_m_crypt_msg_current == p_m_crypt_msg_len) {
987                                         /* next loop */
988                                         p_m_crypt_msg_current = 0;
989                                         p_m_crypt_msg_loops--;
990                                         if (!p_m_crypt_msg_loops)
991                                                 update_rxoff();
992 //                                      puts("eine loop weniger");
993                                 }
994
995                                 /* new length */
996                                 tosend -= length;
997                         }
998
999                         /* copy tones */
1000                         if (p_tone_name[0] && tosend) {
1001                                 tosend -= read_audio(p, tosend);
1002                         }
1003
1004                         /* send data */
1005                         if (ISDN_LOAD - p_m_load - tosend > 0) {
1006                                 frm->prim = PH_DATA_REQ;
1007                                 frm->id = 0;
1008                                 ret = sendto(p_m_mISDNport->b_sock[p_m_b_index].fd, buf, MISDN_HEADER_LEN+ISDN_LOAD-p_m_load-tosend, 0, NULL, 0);
1009                                 if (ret <= 0)
1010                                         PERROR("Failed to send to socket %d (samples = %d)\n", p_m_mISDNport->b_sock[p_m_b_index].fd, ISDN_LOAD-p_m_load-tosend);
1011                                 p_m_load += ISDN_LOAD - p_m_load - tosend;
1012                         }
1013                 }
1014         }
1015
1016         if (p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on || p_m_load) {
1017                 schedule_timer(&p_m_loadtimer, 0, PORT_TRANSMIT * 125);
1018         }
1019 }
1020
1021 /* handle timeouts */
1022 static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i)
1023 {
1024         class PmISDN *isdnport = (class PmISDN *)instance;
1025         struct lcr_msg *message;
1026
1027         PDEBUG(DEBUG_ISDN, "(%s) timeout after %d seconds detected (state=%d).\n", isdnport->p_name, isdnport->p_m_timeout.timeout.tv_sec, isdnport->p_state);
1028         /* send timeout to endpoint */
1029         message = message_create(isdnport->p_serial, ACTIVE_EPOINT(isdnport->p_epointlist), PORT_TO_EPOINT, MESSAGE_TIMEOUT);
1030         message->param.state = isdnport->p_state;
1031         message_put(message);
1032
1033         return 0;
1034 }
1035         
1036
1037 /*
1038  * whenever we get audio data from bchannel, we process it here
1039  */
1040 void PmISDN::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
1041 {
1042         unsigned int cont = *((unsigned int *)data);
1043         struct lcr_msg *message;
1044         unsigned char *p;
1045         int l;
1046
1047         if (hh->prim == PH_CONTROL_IND) {
1048                 if (len < 4) {
1049                         PERROR("SHORT READ OF PH_CONTROL INDICATION\n");
1050                         return;
1051                 }
1052                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL) {
1053                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1054                         add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
1055                         if (!p_m_dtmf)
1056                                 add_trace("info", NULL, "DTMF is disabled");
1057                         end_trace();
1058                         if (!p_m_dtmf)
1059                                 return;
1060                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
1061                         message->param.dtmf = cont & DTMF_TONE_MASK;
1062                         PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION  DTMF digit '%c'\n", p_name, message->param.dtmf);
1063                         message_put(message);
1064                         return;
1065                 }
1066                 switch(cont) {
1067                         case DSP_BF_REJECT:
1068                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1069                         add_trace("DSP-CRYPT", NULL, "error");
1070                         end_trace();
1071                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
1072                         message->param.crypt.type = CC_ERROR_IND;
1073                         PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION  reject of blowfish.\n", p_name);
1074                         message_put(message);
1075                         break;
1076
1077                         case DSP_BF_ACCEPT:
1078                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1079                         add_trace("DSP-CRYPT", NULL, "ok");
1080                         end_trace();
1081                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
1082                         message->param.crypt.type = CC_ACTBF_CONF;
1083                         PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL INDICATION  accept of blowfish.\n", p_name);
1084                         message_put(message);
1085                         break;
1086
1087                         default:
1088                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1089                         add_trace("unknown", NULL, "0x%x", cont);
1090                         end_trace();
1091                 }
1092                 return;
1093         }
1094         if (hh->prim == PH_CONTROL_IND) {
1095                 switch(hh->id) {
1096                         default:
1097                         chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
1098                         add_trace("unknown", NULL, "0x%x", hh->id);
1099                         end_trace();
1100                 }
1101                 return;
1102         }
1103         if (hh->prim == PH_DATA_REQ || hh->prim == DL_DATA_REQ) {
1104                 if (!p_m_txdata) {
1105                         /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
1106                         PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring tx data, because 'txdata' is turned off\n", p_name);
1107                         return;
1108                 }
1109                 /* see below (same condition) */
1110                 if (p_state!=PORT_STATE_CONNECT
1111                          && !p_m_mISDNport->tones)
1112                         return;
1113 //              printf(".");fflush(stdout);return;
1114                 if (p_record)
1115                         record(data, len, 1); // from up
1116                 return;
1117         }
1118         if (hh->prim != PH_DATA_IND && hh->prim != DL_DATA_IND) {
1119                 PERROR("Bchannel received unknown primitve: 0x%x\n", hh->prim);
1120                 return;
1121         }
1122
1123         /* inband is processed */
1124         if (p_m_inband_receive_on)
1125                 inband_receive(data, len);
1126
1127         /* send to remote, if bridged */
1128         bridge_tx(data, len);
1129
1130         /* calls will not process any audio data unless
1131          * the call is connected OR tones feature is enabled.
1132          */
1133 #ifndef DEBUG_COREBRIDGE
1134         if (p_state!=PORT_STATE_CONNECT
1135          && !p_m_mISDNport->tones)
1136                 return;
1137 #endif
1138
1139 #if 0
1140         /* the bearer capability must be audio in order to send and receive
1141          * audio prior or after connect.
1142          */
1143         if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
1144                 return;
1145 #endif
1146
1147         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
1148         if (p_m_rxoff) {
1149                 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
1150                 return;
1151         }
1152
1153         /* record data */
1154         if (p_record)
1155                 record(data, len, 0); // from down
1156
1157         /* randomize and listen to crypt message if enabled */
1158         if (p_m_crypt_listen) {
1159                 /* the noisy randomizer */
1160                 p = data;
1161                 l = len;
1162                 while(l--)
1163                         mISDN_rand[mISDN_rand_count & 0xff] += *p++;
1164
1165                 cryptman_listen_bch(data, len);
1166         }
1167 }
1168
1169
1170 /*
1171  * set echotest
1172  */
1173 void PmISDN::set_echotest(int echo)
1174 {
1175         if (p_m_echo != echo) {
1176                 p_m_echo = echo;
1177                 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
1178                 if (p_m_b_channel)
1179                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1180                                 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_echo?DSP_ECHO_ON:DSP_ECHO_OFF, 0, "DSP-ECHO", p_m_echo);
1181         }
1182 }
1183
1184 /*
1185  * set tone
1186  */
1187 void PmISDN::set_tone(const char *dir, const char *tone)
1188 {
1189         int id = TONE_OFF;
1190         int dsp = DSP_NONE;
1191
1192         /* if no directory is given (by extension), we use interface.conf or options.conf */
1193         if (!dir || !dir[0]) {
1194                 if (p_m_mISDNport->ifport->tones_dir[0])
1195                         dir = p_m_mISDNport->ifport->tones_dir;
1196                 else if (options.tones_dir[0])
1197                         dir = options.tones_dir;
1198         }
1199
1200         if (!tone)
1201                 tone = "";
1202         PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
1203         if (!tone[0]) {
1204                 id = TONE_OFF;
1205                 goto setdsp;
1206         }
1207
1208         /* check for dsp tones */
1209         if (!strcmp(dir, "american"))
1210                 dsp = DSP_AMERICAN;
1211         if (!strcmp(dir, "german"))
1212                 dsp = DSP_GERMAN;
1213         if (!strcmp(dir, "oldgerman"))
1214                 dsp = DSP_OLDGERMAN;
1215
1216         /* check if we NOT really have to use a dsp-tone */
1217         if (dsp == DSP_NONE) {
1218                 nodsp:
1219                 if (p_m_tone)
1220                 if (p_m_b_index > -1)
1221                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT) {
1222                         PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
1223                         ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TONE_PATT_OFF, 0, "DSP-TONE", 0);
1224                 }
1225                 p_m_tone = 0;
1226                 Port::set_tone(dir, tone);
1227                 return;
1228         }
1229
1230         /* now we USE dsp-tone, convert name */
1231         if (!strcmp(tone, "dialtone")) {
1232                 switch(dsp) {
1233                 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
1234                 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
1235                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
1236                 }
1237         } else if (!strcmp(tone, "dialpbx")) {
1238                 switch(dsp) {
1239                 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
1240                 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
1241                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
1242                 }
1243         } else if (!strcmp(tone, "ringing")) {
1244                 switch(dsp) {
1245                 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
1246                 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
1247                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
1248                 }
1249         } else if (!strcmp(tone, "ringpbx")) {
1250                 switch(dsp) {
1251                 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
1252                 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
1253                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
1254                 }
1255         } else if (!strcmp(tone, "busy")) {
1256                 busy:
1257                 switch(dsp) {
1258                 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
1259                 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
1260                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
1261                 }
1262         } else if (!strcmp(tone, "release")) {
1263                 hangup:
1264                 switch(dsp) {
1265                 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
1266                 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
1267                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
1268                 }
1269         } else if (!strcmp(tone, "cause_10"))
1270                 goto hangup;
1271         else if (!strcmp(tone, "cause_11"))
1272                 goto busy;
1273         else if (!strcmp(tone, "cause_22")) {
1274                 switch(dsp) {
1275                 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
1276                 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
1277                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
1278                 }
1279         } else if (!strncmp(tone, "cause_", 6))
1280                 id = TONE_SPECIAL_INFO;
1281         else
1282                 id = TONE_OFF;
1283
1284         /* if we have a tone that is not supported by dsp */
1285         if (id==TONE_OFF && tone[0])
1286                 goto nodsp;
1287
1288         setdsp:
1289         if (p_m_tone != id) {
1290                 /* set new tone */
1291                 p_m_tone = id;
1292                 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
1293                 if (p_m_b_index > -1)
1294                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1295                         ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_tone?DSP_TONE_PATT_ON:DSP_TONE_PATT_OFF, p_m_tone, "DSP-TONE", p_m_tone);
1296         }
1297         /* turn user-space tones off in cases of no tone OR dsp tone */
1298         Port::set_tone("",NULL);
1299 }
1300
1301
1302 /* MESSAGE_mISDNSIGNAL */
1303 //extern struct lcr_msg *dddebug;
1304 void PmISDN::message_mISDNsignal(unsigned int epoint_id, int message_id, union parameter *param)
1305 {
1306         int oldconf, newconf;
1307         switch(param->mISDNsignal.message) {
1308                 case mISDNSIGNAL_VOLUME:
1309                 if (p_m_tx_gain != param->mISDNsignal.tx_gain) {
1310                         p_m_tx_gain = param->mISDNsignal.tx_gain;
1311                         PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_tx_gain);
1312                         if (p_m_b_index > -1)
1313                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1314                                 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_VOL_CHANGE_TX, p_m_tx_gain, "DSP-TX_GAIN", p_m_tx_gain);
1315                 } else
1316                         PDEBUG(DEBUG_BCHANNEL, "we already have tx-volume shift=%d.\n", p_m_rx_gain);
1317                 if (p_m_rx_gain != param->mISDNsignal.rx_gain) {
1318                         p_m_rx_gain = param->mISDNsignal.rx_gain;
1319                         PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rx_gain);
1320                         if (p_m_b_index > -1)
1321                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1322                                 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_VOL_CHANGE_RX, p_m_rx_gain, "DSP-RX_GAIN", p_m_rx_gain);
1323                 } else
1324                         PDEBUG(DEBUG_BCHANNEL, "we already have rx-volume shift=%d.\n", p_m_rx_gain);
1325                 break;
1326
1327                 case mISDNSIGNAL_CONF:
1328                 oldconf = p_m_mute?0:p_m_conf;
1329                 p_m_conf = param->mISDNsignal.conf;
1330                 newconf = p_m_mute?0:p_m_conf;
1331                 set_conf(oldconf, newconf);
1332                 break;
1333
1334                 case mISDNSIGNAL_DELAY:
1335                 if (p_m_delay != param->mISDNsignal.delay) {
1336                         p_m_delay = param->mISDNsignal.delay;
1337                         PDEBUG(DEBUG_BCHANNEL, "we change delay mode to delay=%d.\n", p_m_delay);
1338                         if (p_m_b_index > -1)
1339                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1340                                 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_delay?DSP_DELAY:DSP_JITTER, p_m_delay, "DSP-DELAY", p_m_delay);
1341                 } else
1342                         PDEBUG(DEBUG_BCHANNEL, "we already have delay=%d.\n", p_m_delay);
1343                 break;
1344
1345                 default:
1346                 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
1347         }
1348 }
1349
1350 /* MESSAGE_CRYPT */
1351 void PmISDN::message_crypt(unsigned int epoint_id, int message_id, union parameter *param)
1352 {
1353         struct lcr_msg *message;
1354
1355         switch(param->crypt.type) {
1356                 case CC_ACTBF_REQ:           /* activate blowfish */
1357                 p_m_crypt = 1;
1358                 p_m_crypt_key_len = param->crypt.len;
1359                 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key)) {
1360                         PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
1361                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
1362                         message->param.crypt.type = CC_ERROR_IND;
1363                         message_put(message);
1364                         break;
1365                 }
1366                 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
1367                 crypt_off:
1368                 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
1369                 if (p_m_b_index > -1)
1370                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1371                         ph_control_block(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, p_m_crypt?DSP_BF_ENABLE_KEY:DSP_BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len, "DSP-CRYPT", p_m_crypt_key_len);
1372                 break;
1373
1374                 case CC_DACT_REQ:            /* deactivate session encryption */
1375                 p_m_crypt = 0;
1376                 goto crypt_off;
1377                 break;
1378
1379                 case CR_LISTEN_REQ:          /* start listening to messages */
1380                 p_m_crypt_listen = 1;
1381                 update_rxoff();
1382                 p_m_crypt_listen_state = 0;
1383                 break;
1384
1385                 case CR_UNLISTEN_REQ:        /* stop listening to messages */
1386                 p_m_crypt_listen = 0;
1387                 update_rxoff();
1388                 break;
1389
1390                 case CR_MESSAGE_REQ:         /* send message */
1391                 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
1392                 if (!p_m_crypt_msg_len) {
1393                         PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
1394                         break;
1395                 }
1396                 p_m_crypt_msg_current = 0; /* reset */
1397                 p_m_crypt_msg_loops = 6; /* enable */
1398                 update_rxoff();
1399                 update_load();
1400 #if 0
1401                 /* disable txmix, or we get corrupt data due to audio process */
1402                 if (p_m_txmix && p_m_b_index>=0 && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT) {
1403                         PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
1404                         ph_control(p_m_mISDNport, this, p_mISDNport->b_sock[p_m_b_index].fd, DSP_MIX_OFF, 0, "DSP-TXMIX", 0);
1405                 }
1406 #endif
1407                 break;
1408
1409                 default:
1410                 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
1411         }
1412
1413 }
1414
1415 /*
1416  * endpoint sends messages to the port
1417  */
1418 int PmISDN::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1419 {
1420         if (Port::message_epoint(epoint_id, message_id, param)) {
1421                 if (message_id == MESSAGE_BRIDGE)
1422                         update_rxoff();
1423                 return 1;
1424         }
1425
1426         switch(message_id) {
1427                 case MESSAGE_mISDNSIGNAL: /* user command */
1428                 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
1429                 message_mISDNsignal(epoint_id, message_id, param);
1430                 return 1;
1431
1432                 case MESSAGE_CRYPT: /* crypt control command */
1433                 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
1434                 message_crypt(epoint_id, message_id, param);
1435                 return 1;
1436         }
1437
1438         return 0;
1439 }
1440
1441 void PmISDN::update_rxoff(void)
1442 {
1443         int tx_dejitter = 0;
1444
1445         /* call bridges in user space OR crypto OR recording */
1446         if (p_bridge || p_m_crypt_msg_loops || p_m_crypt_listen || p_record || p_m_inband_receive_on) {
1447                 /* rx IS required */
1448                 if (p_m_rxoff) {
1449                         /* turn on RX */
1450                         p_m_rxoff = 0;
1451                         PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n", __FUNCTION__);
1452                         if (p_m_b_index > -1)
1453                                 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1454                                         ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_RECEIVE_ON, 0, "DSP-RXOFF", 0);
1455                 }
1456         } else {
1457                 /* rx NOT required */
1458                 if (!p_m_rxoff) {
1459                         /* turn off RX */
1460                         p_m_rxoff = 1;
1461                         PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n", __FUNCTION__);
1462                         if (p_m_b_index > -1)
1463                                 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1464                                         ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
1465                 }
1466         }
1467         /* recording */
1468         if (p_record) {
1469                 /* txdata IS required */
1470                 if (!p_m_txdata) {
1471                         /* turn on RX */
1472                         p_m_txdata = 1;
1473                         PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is required, so we turn them on\n", __FUNCTION__);
1474                         if (p_m_b_index > -1)
1475                                 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1476                                         ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TXDATA_ON, 0, "DSP-TXDATA", 1);
1477                 }
1478         } else {
1479                 /* txdata NOT required */
1480                 if (p_m_txdata) {
1481                         /* turn off RX */
1482                         p_m_txdata = 0;
1483                         PDEBUG(DEBUG_BCHANNEL, "%s: transmit data is not required, so we turn them off\n", __FUNCTION__);
1484                         if (p_m_b_index > -1)
1485                                 if (p_m_mISDNport->b_port[p_m_b_index] && p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
1486                                         ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TXDATA_OFF, 0, "DSP-TXDATA", 0);
1487                 }
1488         }
1489         /* dejitter on bridge */
1490         if (p_bridge)
1491                 tx_dejitter = 1;
1492         if (p_m_tx_dejitter != tx_dejitter) {
1493                 p_m_tx_dejitter = tx_dejitter;
1494                 PDEBUG(DEBUG_BCHANNEL, "we change dejitter mode to delay=%d.\n", p_m_tx_dejitter);
1495                 if (p_m_b_index > -1)
1496                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE && p_m_mISDNport->b_mode[p_m_b_index] == B_MODE_TRANSPARENT)
1497                                 ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, DSP_TX_DEJITTER, p_m_tx_dejitter, "DSP-TX_DEJITTER", p_m_tx_dejitter);
1498         }
1499 }
1500
1501 static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, int i)
1502 {
1503         struct mISDNport *mISDNport;
1504         struct mbuffer *mb;
1505         struct l3_msg *l3m;
1506         char byte;
1507         int ret;
1508
1509         /* unset global semaphore */
1510         upqueue_avail = 0;
1511         // with a very small incident, upqueue_avail may be set by mISDN thread and
1512         // another byte may be sent to the pipe, which causes a call to this function
1513         // again with nothing in the upqueue. this is no problem.
1514         ret = read(fd->fd, &byte, 1);
1515
1516         /* process all ports */
1517         mISDNport = mISDNport_first;
1518         while(mISDNport) {
1519                 /* handle queued up-messages (d-channel) */
1520                 while ((mb = mdequeue(&mISDNport->upqueue))) {
1521                         l3m = &mb->l3;
1522                         switch(l3m->type) {
1523                                 case MPH_ACTIVATE_IND:
1524                                 if (mISDNport->l1link != 1) {
1525                                         l1l2l3_trace_header(mISDNport, NULL, L1_ACTIVATE_IND, DIRECTION_IN);
1526                                         end_trace();
1527                                         mISDNport->l1link = 1;
1528                                 }
1529                                 break;
1530         
1531                                 case MPH_DEACTIVATE_IND:
1532                                 if (mISDNport->l1link != 0) {
1533                                         l1l2l3_trace_header(mISDNport, NULL, L1_DEACTIVATE_IND, DIRECTION_IN);
1534                                         end_trace();
1535                                         mISDNport->l1link = 0;
1536                                 }
1537                                 break;
1538
1539                                 case MPH_INFORMATION_IND:
1540                                 PDEBUG(DEBUG_ISDN, "Received MPH_INFORMATION_IND for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1541                                 switch (l3m->pid) {
1542                                         case L1_SIGNAL_LOS_ON:
1543                                         mISDNport->los = 1;
1544                                         break;
1545                                         case L1_SIGNAL_LOS_OFF:
1546                                         mISDNport->los = 0;
1547                                         break;
1548                                         case L1_SIGNAL_AIS_ON:
1549                                         mISDNport->ais = 1;
1550                                         break;
1551                                         case L1_SIGNAL_AIS_OFF:
1552                                         mISDNport->ais = 0;
1553                                         break;
1554                                         case L1_SIGNAL_RDI_ON:
1555                                         mISDNport->rdi = 1;
1556                                         break;
1557                                         case L1_SIGNAL_RDI_OFF:
1558                                         mISDNport->rdi = 0;
1559                                         break;
1560                                         case L1_SIGNAL_SLIP_TX:
1561                                         mISDNport->slip_tx++;
1562                                         break;
1563                                         case L1_SIGNAL_SLIP_RX:
1564                                         mISDNport->slip_rx++;
1565                                         break;
1566                                 }
1567                                 break;
1568
1569                                 case MT_L2ESTABLISH:
1570                                 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_IND, DIRECTION_IN);
1571                                 add_trace("tei", NULL, "%d", l3m->pid);
1572                                 end_trace();
1573                                 mISDNport->l2link = 1;
1574                                 if (l3m->pid < 128)
1575                                         mISDNport->l2mask[l3m->pid >> 3] |= (1 << (l3m->pid & 7));
1576                                 if ((!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
1577                                         if (mISDNport->l2establish.active) {
1578                                                 unsched_timer(&mISDNport->l2establish);
1579                                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1580                                         }
1581                                 }
1582                                 break;
1583
1584                                 case MT_L2RELEASE:
1585                                 if (l3m->pid < 128)
1586                                         mISDNport->l2mask[l3m->pid >> 3] &= ~(1 << (l3m->pid & 7));
1587                                 if (!mISDNport->l2establish.active) {
1588                                         l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_IND, DIRECTION_IN);
1589                                         add_trace("tei", NULL, "%d", l3m->pid);
1590                                         end_trace();
1591                                         /* down if not nt-ptmp */ 
1592                                         if (!mISDNport->ntmode || mISDNport->ptp)
1593                                                 mISDNport->l2link = 0;
1594                                 }
1595                                 if ((!mISDNport->ntmode || mISDNport->ptp) && l3m->pid < 127) {
1596                                         if (!mISDNport->l2establish.active && mISDNport->l2hold) {
1597                                                 PDEBUG(DEBUG_ISDN, "set timer and establish.\n");
1598                                                 schedule_timer(&mISDNport->l2establish, 5, 0);
1599                                                 mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
1600                                         }
1601                                 }
1602                                 break;
1603
1604                                 default:
1605                                 /* l3-data is sent to LCR */
1606                                 stack2manager(mISDNport, l3m->type, l3m->pid, l3m);
1607                         }
1608                         /* free message */
1609                         free_l3_msg(l3m);
1610                 }
1611                 mISDNport = mISDNport->next;
1612         }
1613         return 0;
1614 }
1615
1616 /* l2 establish timer fires */
1617 static int l2establish_timeout(struct lcr_timer *timer, void *instance, int i)
1618 {
1619         struct mISDNport *mISDNport = (struct mISDNport *)instance;
1620
1621         if (mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
1622                 PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link portnum=%d.\n", mISDNport->portnum);
1623                 mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
1624                 schedule_timer(&mISDNport->l2establish, 5, 0); /* 5 seconds */
1625         }
1626
1627         return 0;
1628 }
1629
1630 /* handle frames from bchannel */
1631 static int b_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int i)
1632 {
1633         struct mISDNport *mISDNport = (struct mISDNport *)instance;
1634         unsigned char buffer[2048+MISDN_HEADER_LEN];
1635         struct mISDNhead *hh = (struct mISDNhead *)buffer;
1636         int ret;
1637
1638         ret = recv(fd->fd, buffer, sizeof(buffer), 0);
1639         if (ret < 0) {
1640                 PERROR("read error frame, errno %d\n", errno);
1641                 return 0;
1642         }
1643         if (ret < (int)MISDN_HEADER_LEN) {
1644                 PERROR("read short frame, got %d, expected %d\n", ret, (int)MISDN_HEADER_LEN);
1645                 return 0;
1646         }
1647         switch(hh->prim) {
1648                 /* we don't care about confirms, we use rx data to sync tx */
1649                 case PH_DATA_CNF:
1650                 break;
1651
1652                 /* we receive audio data, we respond to it AND we send tones */
1653                 case PH_DATA_IND:
1654                 case DL_DATA_IND:
1655                 case PH_DATA_REQ:
1656                 case DL_DATA_REQ:
1657                 case PH_CONTROL_IND:
1658                 if (mISDNport->b_port[i])
1659                         mISDNport->b_port[i]->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
1660                 else
1661                         PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (socket %d), ignoring.\n", fd->fd);
1662                 break;
1663
1664                 case PH_ACTIVATE_IND:
1665                 case DL_ESTABLISH_IND:
1666                 case PH_ACTIVATE_CNF:
1667                 case DL_ESTABLISH_CNF:
1668                 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", fd->fd);
1669                 bchannel_event(mISDNport, i, B_EVENT_ACTIVATED);
1670                 break;
1671
1672                 case PH_DEACTIVATE_IND:
1673                 case DL_RELEASE_IND:
1674                 case PH_DEACTIVATE_CNF:
1675                 case DL_RELEASE_CNF:
1676                 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", fd->fd);
1677                 bchannel_event(mISDNport, i, B_EVENT_DEACTIVATED);
1678                 break;
1679
1680                 default:
1681                 PERROR("child message not handled: prim(0x%x) socket(%d) msg->len(%d)\n", hh->prim, fd->fd, ret-MISDN_HEADER_LEN);
1682         }
1683
1684         return 0;
1685 }
1686
1687 /* process timer events for bchannel handling */
1688 static int b_timer_timeout(struct lcr_timer *timer, void *instance, int i)
1689 {
1690         struct mISDNport *mISDNport = (struct mISDNport *)instance;
1691
1692         bchannel_event(mISDNport, i, B_EVENT_TIMEOUT);
1693
1694         return 0;
1695 }
1696
1697
1698 int do_layer3(struct mlayer3 *ml3, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1699 {
1700         /* IMPORTAINT:
1701          *
1702          * l3m must be queued, except for MT_ASSIGN
1703          *
1704          */
1705         struct mISDNport *mISDNport = (struct mISDNport *)ml3->priv;
1706         struct mbuffer *mb;
1707
1708 #ifdef OLD_MT_ASSIGN
1709         /* special MT_ASSIGN handling:
1710          *
1711          * if we request a PID from mlayer, we always do it while lcr is locked.
1712          * therefore we must check the MT_ASSIGN reply first before we lock.
1713          * this is because the MT_ASSIGN reply is received with the requesting
1714          * process, not by the mlayer thread!
1715          * this means, that the reply is sent during call of the request.
1716          * we must check if we get a reply and we know that we lcr is currently
1717          * locked.
1718          */
1719         if (cmd==MT_ASSIGN && (pid&MISDN_PID_CR_FLAG) && (pid>>16)==MISDN_CES_MASTER) {
1720                 /* let's do some checking if someone changes stack behaviour */
1721                 if (mt_assign_pid != 0)
1722                         FATAL("someone played with the mISDNuser stack. MT_ASSIGN not currently expected.\n");
1723                 mt_assign_pid = pid;
1724                 return(0);
1725         }
1726 #endif
1727         /* queue message, create, if required */
1728         if (!l3m) {
1729                 l3m = alloc_l3_msg();
1730                 if (!l3m)
1731                         FATAL("No memory for layer 3 message\n");
1732         }
1733         mb = container_of(l3m, struct mbuffer, l3);
1734         l3m->type = cmd;
1735         l3m->pid = pid;
1736         mqueue_tail(&mISDNport->upqueue, mb);
1737         if (!upqueue_avail) {
1738                 // multiple threads may cause multiple calls of this section, but this
1739                 // results only in multiple processing of the upqueue read.
1740                 // this is no problem.
1741                 upqueue_avail = 1;
1742                 char byte = 0;
1743                 int ret;
1744                 ret = write(upqueue_pipe[1], &byte, 1);
1745         }
1746         return 0;
1747 }
1748
1749 int mISDN_getportbyname(int sock, int cnt, char *portname)
1750 {
1751         struct mISDN_devinfo devinfo;
1752         int port = 0, ret;
1753
1754         /* resolve name */
1755         while (port < cnt) {
1756                 devinfo.id = port;
1757                 ret = ioctl(sock, IMGETDEVINFO, &devinfo);
1758                 if (ret < 0)
1759                         return ret;
1760                 if (!strcasecmp(devinfo.name, portname))
1761                         break;
1762                 port++;
1763         }
1764         if (port == cnt)
1765                 return -EIO;
1766
1767         return (port);
1768 }
1769
1770 /*
1771  * global function to add a new card (port)
1772  */
1773 struct mISDNport *mISDNport_open(struct interface_port *ifport)
1774 {
1775         int ret;
1776         struct mISDNport *mISDNport, **mISDNportp;
1777         int port = ifport->portnum;
1778         int ptp = ifport->ptp;
1779         int force_nt = ifport->nt;
1780         int l1hold = ifport->l1hold;
1781         int l2hold = ifport->l2hold;
1782         int ss5 = ifport->ss5;
1783         int i, cnt;
1784         int pri, bri, pots;
1785         int nt, te;
1786 //      struct mlayer3 *ml3;
1787         struct mISDN_devinfo devinfo;
1788         unsigned int protocol, prop;
1789
1790         /* check port counts */
1791         ret = ioctl(mISDNsocket, IMGETCOUNT, &cnt);
1792         if (ret < 0) {
1793                 fprintf(stderr, "Cannot get number of mISDN devices. (ioctl IMGETCOUNT failed ret=%d)\n", ret);
1794                 return(NULL);
1795         }
1796
1797         if (cnt <= 0) {
1798                 PERROR_RUNTIME("Found no card. Please be sure to load card drivers.\n");
1799                 return(NULL);
1800         }
1801         if (port < 0) {
1802                 port = mISDN_getportbyname(mISDNsocket, cnt, ifport->portname);
1803                 if (port < 0) {
1804                         PERROR_RUNTIME("Port name '%s' not found, use 'misdn_info' tool to list all existing ports.\n", ifport->portname);
1805                         return(NULL);
1806                 }
1807                 // note: 'port' has still the port number
1808         }
1809         if (port>cnt || port<0) {
1810                 PERROR_RUNTIME("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 0, cnt);
1811                 return(NULL);
1812         }
1813
1814         /* get port attributes */
1815         pri = bri = pots = nt = te = 0;
1816         devinfo.id = port;
1817         ret = ioctl(mISDNsocket, IMGETDEVINFO, &devinfo);
1818         if (ret < 0) {
1819                 PERROR_RUNTIME("Cannot get device information for port %d. (ioctl IMGETDEVINFO failed ret=%d)\n", port, ret);
1820                 return(NULL);
1821         }
1822         if (devinfo.Dprotocols & (1 << ISDN_P_TE_S0)) {
1823                 bri = 1;
1824                 te = 1;
1825         }
1826         if (devinfo.Dprotocols & (1 << ISDN_P_NT_S0)) {
1827                 bri = 1;
1828                 nt = 1;
1829         }
1830         if (devinfo.Dprotocols & (1 << ISDN_P_TE_E1)) {
1831                 pri = 1;
1832                 te = 1;
1833         }
1834         if (devinfo.Dprotocols & (1 << ISDN_P_NT_E1)) {
1835                 pri = 1;
1836                 nt = 1;
1837         }
1838 #ifdef ISDN_P_FXS
1839         if (devinfo.Dprotocols & (1 << ISDN_P_FXS)) {
1840                 pots = 1;
1841                 te = 1;
1842         }
1843 #endif
1844 #ifdef ISDN_P_FXO
1845         if (devinfo.Dprotocols & (1 << ISDN_P_FXO)) {
1846                 pots = 1;
1847                 nt = 1;
1848         }
1849 #endif
1850         if (force_nt && !nt) {
1851                 PERROR_RUNTIME("Port %d does not support NT-mode\n", port);
1852                 return(NULL);
1853         }
1854         if (bri && pri) {
1855                 PERROR_RUNTIME("Port %d supports BRI and PRI?? What kind of controller is that?. (Can't use this!)\n", port);
1856                 return(NULL);
1857         }
1858         if (pots && !bri && !pri) {
1859                 PERROR_RUNTIME("Port %d supports POTS, LCR does not!\n", port);
1860                 return(NULL);
1861         }
1862         if (!bri && !pri) {
1863                 PERROR_RUNTIME("Port %d does not support BRI nor PRI!\n", port);
1864                 return(NULL);
1865         }
1866         if (!nt && !te) {
1867                 PERROR_RUNTIME("Port %d does not support NT-mode nor TE-mode!\n", port);
1868                 return(NULL);
1869         }
1870         /* set NT by turning off TE */
1871         if (force_nt && nt)
1872                 te = 0;
1873         /* if TE an NT is supported (and not forced to NT), turn off NT */
1874         if (te && nt)
1875                 nt = 0;
1876
1877         /* check for double use of port */
1878         if (nt) {
1879                 mISDNport = mISDNport_first;
1880                 while(mISDNport) {
1881                         if (mISDNport->portnum == port)
1882                                 break;
1883                         mISDNport = mISDNport->next;
1884                 }
1885                 if (mISDNport) {
1886                         PERROR_RUNTIME("Port %d already in use by LCR. You can't use a NT port multiple times.\n", port);
1887                         return(NULL);
1888                 }
1889         }
1890
1891         /* check for continous channelmap with no bchannel on slot 16 */
1892         if (test_channelmap(0, devinfo.channelmap)) {
1893                 PERROR_RUNTIME("Port %d provides channel 0, but we cannot access it!\n", port);
1894                 return(NULL);
1895         }
1896         i = 1;
1897         while(i < (int)devinfo.nrbchan + 1) {
1898                 if (i == 16) {
1899                         if (test_channelmap(i, devinfo.channelmap)) {
1900                                 PERROR("Port %d provides bchannel 16. Pleas upgrade mISDN, if this port is mISDN loopback interface.\n", port);
1901                                 return(NULL);
1902                         }
1903                 } else {
1904                         if (!test_channelmap(i, devinfo.channelmap)) {
1905                                 PERROR_RUNTIME("Port %d has no channel on slot %d!\n", port, i);
1906                                 return(NULL);
1907                         }
1908                 }
1909                 i++;
1910         }
1911
1912         /* add mISDNport structure */
1913         mISDNportp = &mISDNport_first;
1914         while(*mISDNportp)
1915                 mISDNportp = &((*mISDNportp)->next);
1916         mISDNport = (struct mISDNport *)MALLOC(sizeof(struct mISDNport));
1917         add_timer(&mISDNport->l2establish, l2establish_timeout, mISDNport, 0);
1918         if (ss5) {
1919                 /* ss5 link is always active */
1920                 mISDNport->l1link = 1;
1921                 mISDNport->l2link = 1;
1922         } else {
1923                 mISDNport->l1link = -1;
1924                 mISDNport->l2link = -1;
1925         }
1926         pmemuse++;
1927         *mISDNportp = mISDNport;
1928
1929         /* if pri, must set PTP */
1930         if (pri)
1931                 ptp = 1;
1932
1933         /* set ss5 params */
1934         if (ss5) {
1935                 /* try to keep interface enabled */
1936                 l1hold = 1;
1937                 l2hold = 0;
1938         }
1939         /* set l2hold */
1940         switch (l2hold) {
1941                 case -1: // off
1942                 l2hold = 0;
1943                 break;
1944                 case 1: // on
1945                 l2hold = 1;
1946                 break;
1947                 default:
1948                 if (ptp)
1949                         l2hold = 1;
1950                 else
1951                         l2hold = 0;
1952                 break;
1953         }
1954                 
1955         /* allocate ressources of port */
1956         protocol = (nt)?L3_PROTOCOL_DSS1_NET:L3_PROTOCOL_DSS1_USER;
1957         prop = (1 << MISDN_FLG_L2_CLEAN);
1958         if (ptp) // ptp forced
1959                prop |= (1 << MISDN_FLG_PTP);
1960         if (nt) // supports hold/retrieve on nt-mode
1961                prop |= (1 << MISDN_FLG_NET_HOLD);
1962         if (l1hold) // supports layer 1 hold
1963                prop |= (1 << MISDN_FLG_L1_HOLD);
1964         if (l2hold) // supports layer 2 hold
1965                prop |= (1 << MISDN_FLG_L2_HOLD);
1966         /* open layer 3 and init upqueue */
1967         /* queue must be initializes, because l3-thread may send messages during open_layer3() */
1968         mqueue_init(&mISDNport->upqueue);
1969         mISDNport->ml3 = open_layer3(port, protocol, prop , do_layer3, mISDNport);
1970         if (!mISDNport->ml3) {
1971                 mqueue_purge(&mISDNport->upqueue);
1972                 PERROR_RUNTIME("open_layer3() failed for port %d\n", port);
1973                 start_trace(port,
1974                         ifport->interface,
1975                         NULL,
1976                         NULL,
1977                         DIRECTION_NONE,
1978                         CATEGORY_CH,
1979                         0,
1980                         "PORT (open failed)");
1981                 end_trace();
1982                 mISDNport_close(mISDNport);
1983                 return(NULL);
1984         }
1985
1986         SCPY(mISDNport->name, devinfo.name);
1987         mISDNport->b_num = devinfo.nrbchan;
1988         mISDNport->portnum = port;
1989         mISDNport->ntmode = nt;
1990         mISDNport->tespecial = ifport->tespecial;
1991         mISDNport->pri = pri;
1992         mISDNport->ptp = ptp;
1993         mISDNport->l1hold = l1hold;
1994         mISDNport->l2hold = l2hold;
1995         mISDNport->ss5 = ss5;
1996         PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1997         i = 0;
1998         while(i < mISDNport->b_num) {
1999                 mISDNport->b_state[i] = B_STATE_IDLE;
2000                 add_timer(&mISDNport->b_timer[i], b_timer_timeout, mISDNport, i);
2001                 i++;
2002         }
2003
2004         /* if ptp, pull up the link */
2005         if (mISDNport->l2hold && (mISDNport->ptp || !mISDNport->ntmode)) {
2006                 mISDNport->ml3->to_layer3(mISDNport->ml3, MT_L2ESTABLISH, 0, NULL);
2007                 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_REQ, DIRECTION_OUT);
2008                 add_trace("tei", NULL, "%d", 0);
2009                 end_trace();
2010                 schedule_timer(&mISDNport->l2establish, 5, 0); /* 5 seconds */
2011         }
2012
2013         /* for nt-mode ptmp the link is always up */
2014         if (mISDNport->ntmode && !mISDNport->ptp)
2015                 mISDNport->l2link = 1;
2016
2017         PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
2018
2019         start_trace(mISDNport->portnum,
2020                     ifport->interface,
2021                     NULL,
2022                     NULL,
2023                     DIRECTION_NONE,
2024                     CATEGORY_CH,
2025                     0,
2026                     "PORT (open)");
2027         add_trace("mode", NULL, (mISDNport->ntmode)?"network":"terminal");
2028         add_trace("channels", NULL, "%d", mISDNport->b_num);
2029         if (mISDNport->ss5)
2030                 add_trace("ccitt#5", NULL, "enabled");
2031         end_trace();
2032
2033         return(mISDNport);
2034 }
2035
2036
2037 /*
2038  * load static port instances, if required by mISDNport
2039  */
2040 void mISDNport_static(struct mISDNport *mISDNport)
2041 {
2042         int i;
2043
2044         i = 0;
2045         while(i < mISDNport->b_num) {
2046 #ifdef WITH_SS5
2047                 if (mISDNport->ss5)
2048                         ss5_create_channel(mISDNport, i);
2049 #endif
2050                 i++;
2051         }
2052 }
2053
2054
2055 /*
2056  * function to free ALL cards (ports)
2057  */
2058 void mISDNport_close_all(void)
2059 {
2060         /* free all ports */
2061         while(mISDNport_first)
2062                 mISDNport_close(mISDNport_first);
2063 }
2064
2065 /*
2066  * free only one port
2067  */
2068 void mISDNport_close(struct mISDNport *mISDNport)
2069 {
2070         struct mISDNport **mISDNportp;
2071         class Port *port;
2072         class PmISDN *isdnport;
2073         int i;
2074
2075         /* remove all port instance that are linked to this mISDNport */
2076         again:
2077         port = port_first;
2078         while(port) {
2079                 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN) {
2080                         isdnport = (class PmISDN *)port;
2081                         if (isdnport->p_m_mISDNport && isdnport->p_m_mISDNport == mISDNport) {
2082                                 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", isdnport->p_name, mISDNport->portnum);
2083                                 delete isdnport;
2084                                 goto again;
2085                         }
2086                 }
2087                 port = port->next;
2088         }
2089
2090         /* only if we are already part of interface */
2091         if (mISDNport->ifport) {
2092                 start_trace(mISDNport->portnum,
2093                             mISDNport->ifport->interface,
2094                             NULL,
2095                             NULL,
2096                             DIRECTION_NONE,
2097                             CATEGORY_CH,
2098                             0,
2099                             "PORT (close)");
2100                 end_trace();
2101         }
2102
2103         /* free bchannels */
2104         i = 0;
2105         while(i < mISDNport->b_num) {
2106                 if (mISDNport->b_sock[i].inuse) {
2107                         _bchannel_destroy(mISDNport, i);
2108                         PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
2109                 }
2110                 if (mISDNport->b_timer[i].inuse) {
2111                         del_timer(&mISDNport->b_timer[i]);
2112                 }
2113                 i++;
2114         }
2115         del_timer(&mISDNport->l2establish);
2116
2117         /* close layer 3, if open */
2118         if (mISDNport->ml3) {
2119                 close_layer3(mISDNport->ml3);
2120         }
2121
2122         /* purge upqueue */
2123         mqueue_purge(&mISDNport->upqueue);
2124
2125         /* remove from list */
2126         mISDNportp = &mISDNport_first;
2127         while(*mISDNportp) {
2128                 if (*mISDNportp == mISDNport) {
2129                         *mISDNportp = (*mISDNportp)->next;
2130                         mISDNportp = NULL;
2131                         break;
2132                 }
2133                 mISDNportp = &((*mISDNportp)->next);
2134         }
2135
2136         if (mISDNportp)
2137                 FATAL("mISDNport not in list\n");
2138         
2139         FREE(mISDNport, sizeof(struct mISDNport));
2140         pmemuse--;
2141
2142 }
2143
2144
2145 /*
2146  * enque data from upper buffer
2147  */
2148 int PmISDN::bridge_rx(unsigned char *data, int length)
2149 {
2150         unsigned char buf[MISDN_HEADER_LEN+((length>ISDN_LOAD)?length:ISDN_LOAD)];
2151         struct mISDNhead *hh = (struct mISDNhead *)buf;
2152         int ret;
2153
2154         if (p_m_b_index < 0)
2155                 return -EIO;
2156         if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_ACTIVE)
2157                 return -EINVAL;
2158
2159         /* check if high priority tones exist
2160          * ignore data in this case
2161          */
2162         if (p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on)
2163                 return -EBUSY;
2164
2165         /* preload procedure
2166          * if transmit buffer in DSP module is empty,
2167          * preload it to DSP_LOAD to prevent jitter gaps.
2168          */
2169         if (p_m_load == 0 && ISDN_LOAD > 0) {
2170                 hh->prim = PH_DATA_REQ; 
2171                 hh->id = 0;
2172                 memset(buf+MISDN_HEADER_LEN, silence, ISDN_LOAD);
2173                 ret = sendto(p_m_mISDNport->b_sock[p_m_b_index].fd, buf, MISDN_HEADER_LEN+ISDN_LOAD, 0, NULL, 0);
2174                 if (ret <= 0)
2175                         PERROR("Failed to send to socket %d\n", p_m_mISDNport->b_sock[p_m_b_index].fd);
2176                 p_m_load += ISDN_LOAD;
2177                 schedule_timer(&p_m_loadtimer, 0, PORT_TRANSMIT * 125);
2178         }
2179
2180         /* drop if load would exceed ISDN_MAXLOAD
2181          * this keeps the delay not too high
2182          */
2183         if (p_m_load+length > ISDN_MAXLOAD)
2184                 return -EINVAL;
2185
2186         /* make and send frame */
2187         hh->prim = PH_DATA_REQ;
2188         hh->id = 0;
2189         memcpy(buf+MISDN_HEADER_LEN, data, length);
2190         ret = sendto(p_m_mISDNport->b_sock[p_m_b_index].fd, buf, MISDN_HEADER_LEN+length, 0, NULL, 0);
2191         if (ret <= 0)
2192                 PERROR("Failed to send to socket %d\n", p_m_mISDNport->b_sock[p_m_b_index].fd);
2193         p_m_load += length;
2194
2195         return 0;
2196 }
2197
2198 int PmISDN::inband_send(unsigned char *buffer, int len)
2199 {
2200         PERROR("this function must be derived to function!\n");
2201         return 0;
2202 }
2203
2204 void PmISDN::inband_send_on(void)
2205 {
2206         PDEBUG(DEBUG_PORT, "turning inband signalling send on.\n");
2207         p_m_inband_send_on = 1;
2208         /* trigger inband transmit */
2209         update_load();
2210 }
2211
2212 void PmISDN::inband_send_off(void)
2213 {
2214         PDEBUG(DEBUG_PORT, "turning inband signalling send off.\n");
2215         p_m_inband_send_on = 0;
2216 }
2217
2218 void PmISDN::inband_receive(unsigned char *buffer, int len)
2219 {
2220 //
2221 //      if (len >= SS5_DECODER_NPOINTS)
2222 //              ss5_decode(buffer, SS5_DECODER_NPOINTS);
2223         PERROR("this function must be derived to function!\n");
2224 }
2225
2226 void PmISDN::inband_receive_on(void)
2227 {
2228         /* this must work during constructor, see ss5.cpp */
2229         PDEBUG(DEBUG_PORT, "turning inband signalling receive on.\n");
2230         p_m_inband_receive_on = 1;
2231         update_rxoff();
2232 }
2233
2234 void PmISDN::inband_receive_off(void)
2235 {
2236         PDEBUG(DEBUG_PORT, "turning inband signalling receive off.\n");
2237         p_m_inband_receive_on = 0;
2238         update_rxoff();
2239 }
2240
2241 void PmISDN::mute_on(void)
2242 {
2243         if (p_m_mute)
2244                 return;
2245         PDEBUG(DEBUG_PORT, "turning mute on.\n");
2246         p_m_mute = 1;
2247         set_conf(p_m_conf, 0);
2248 }
2249
2250 void PmISDN::mute_off(void)
2251 {
2252         if (!p_m_mute)
2253                 return;
2254         PDEBUG(DEBUG_PORT, "turning mute off.\n");
2255         p_m_mute = 0;
2256         set_conf(0, p_m_conf);
2257 }
2258
2259