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