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