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