40d52040552807dc4500ebf63fea78e999aa4e21
[lcr.git] / mISDN.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN port abstraction for dss1 and sip                                   **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "main.h"
17 #include <unistd.h>
18 #include <poll.h>
19 #include <errno.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 extern "C" {
25 #include <net_l2.h>
26 }
27
28 #define ISDN_PID_L2_B_USER 0x420000ff
29 #define ISDN_PID_L3_B_USER 0x430000ff
30 #define ISDN_PID_L4_B_USER 0x440000ff
31
32 /* used for udevice */
33 int entity = 0;
34
35 /* noise randomizer */
36 unsigned char mISDN_rand[256];
37 int mISDN_rand_count = 0;
38
39 /* the device handler and port list */
40 int mISDNdevice = -1;
41
42 /* list of mISDN ports */
43 struct mISDNport *mISDNport_first;
44
45 /*
46  * constructor
47  */
48 PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive) : Port(type, portname, settings)
49 {
50         p_m_mISDNport = mISDNport;
51         p_m_portnum = mISDNport->portnum;
52         p_m_b_index = -1;
53         p_m_b_channel = 0;
54         p_m_b_exclusive = 0;
55         p_m_b_reserve = 0;
56         p_m_b_addr = 0;
57         p_m_b_stid = 0;
58         p_m_jittercheck = 0;
59         p_m_delete = 0;
60         p_m_hold = 0;
61         p_m_txvol = p_m_rxvol = 0;
62         p_m_conf = 0;
63 #warning set delay by routing parameter or interface config
64         p_m_delay = 0;
65         p_m_echo = 0;
66         p_m_tone = 0;
67         p_m_rxoff = 0;
68         p_m_txmix = 0;
69         p_m_txmix_on = 0;
70         p_m_nodata = 1; /* may be 1, because call always notifies us */
71         p_m_dtmf = !options.nodtmf;
72         p_m_timeout = 0;
73         p_m_timer = 0;
74 #warning denke auch an die andere seite. also das setup sollte dies weitertragen
75
76         p_m_crypt = 0;
77         p_m_crypt_listen = 0;
78         p_m_crypt_msg_loops = 0;
79         p_m_crypt_msg_loops = 0;
80         p_m_crypt_msg_len = 0;
81         p_m_crypt_msg[0] = '\0';
82         p_m_crypt_msg_current = 0;
83         p_m_crypt_key[0] = '\0';
84         p_m_crypt_key_len = 0;
85         p_m_crypt_listen = 0;
86         p_m_crypt_listen_state = 0;
87         p_m_crypt_listen_len = 0;
88         p_m_crypt_listen_msg[0] = '\0';
89         p_m_crypt_listen_crc = 0;
90
91         /* if any channel requested by constructor */
92         if (channel == CHANNEL_ANY)
93         {
94                 /* reserve channel */
95                 p_m_b_reserve = 1;
96                 mISDNport->b_reserved++;
97         }
98
99         } else
100         /* reserve channel */
101         if (channel) // only if constructor was called with a channel resevation
102                 seize_bchannel(channel, exclusive);
103
104         /* we increase the number of objects: */
105         mISDNport->use++;
106         PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, port #%d\n", portname, mISDNport->use, p_m_portnum);
107 }
108
109
110 /*
111  * destructor
112  */
113 PmISDN::~PmISDN()
114 {
115         struct message *message;
116
117         /* remove bchannel relation */
118         free_bchannel();
119
120         /* release epoint */
121         while (p_epointlist)
122         {
123                 PDEBUG(DEBUG_ISDN, "destroy mISDNPort(%s). endpoint still exists, releaseing.\n", p_name);
124                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
125                 message->param.disconnectinfo.cause = 16;
126                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
127                 message_put(message);
128                 /* remove from list */
129                 free_epointlist(p_epointlist);
130         }
131
132         /* we decrease the number of objects: */
133         p_m_mISDNport->use--;
134         PDEBUG(DEBUG_ISDN, "destroyed mISDNPort(%s). Currently %d objects\n", p_name, p_m_mISDNport->use);
135 }
136
137
138 /*
139  * send control information to the channel (dsp-module)
140  */
141 void ph_control(unsigned long b_addr, int c1, int c2)
142 {
143         unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
144         iframe_t *ctrl = (iframe_t *)buffer; 
145         unsigned long *d = (unsigned long *)&ctrl->data.p;
146
147         ctrl->prim = PH_CONTROL | REQUEST;
148         ctrl->addr = b_addr | FLG_MSG_DOWN;
149         ctrl->dinfo = 0;
150         ctrl->len = sizeof(unsigned long)*2;
151         *d++ = c1;
152         *d++ = c2;
153         mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
154 }
155
156 void ph_control_block(unsigned long b_addr, int c1, void *c2, int c2_len)
157 {
158         unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
159         iframe_t *ctrl = (iframe_t *)buffer;
160         unsigned long *d = (unsigned long *)&ctrl->data.p;
161
162         ctrl->prim = PH_CONTROL | REQUEST;
163         ctrl->addr = b_addr | FLG_MSG_DOWN;
164         ctrl->dinfo = 0;
165         ctrl->len = sizeof(unsigned long)*2;
166         *d++ = c1;
167         memcpy(d, c2, c2_len);
168         mISDN_write(mISDNdevice, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
169 }
170
171
172 /*
173  * activate / deactivate bchannel
174  */
175 static void bchannel_activate(struct mISDNport *mISDNport, int i)
176 {
177         iframe_t act;
178
179         /* we must activate if we are deactivated */
180         if (mISDNport->b_state[i] == B_STATE_IDLE)
181         {
182                 /* activate bchannel */
183                 PDEBUG(DEBUG_BCHANNEL, "activating bchannel (index %d), because currently idle (address 0x%x).\n", i, mISDNport->b_addr[i]);
184                 act.prim = DL_ESTABLISH | REQUEST; 
185                 act.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
186                 act.dinfo = 0;
187                 act.len = 0;
188                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
189                 mISDNport->b_state[i] = B_STATE_ACTIVATING;
190                 return;
191         }
192
193         /* if we are active, we configure our channel */
194         if (mISDNport->b_state[i] == B_STATE_ACTIVE)
195         {
196                 unsigned char buffer[mISDN_HEADER_LEN+ISDN_PRELOAD];
197                 iframe_t *pre = (iframe_t *)buffer; /* preload data */
198                 unsigned char *p = (unsigned char *)&pre->data.p;
199
200                 /* it is an error if this channel is not associated with a port object */
201                 if (!mISDNport->b_port[i])
202                 {
203                         PERROR("bchannel index i=%d not associated with a port object\n", i);
204                         return;
205                 }
206
207                 /* configure dsp features */
208                 if (mISDNport->b_port[i]->p_m_delay)
209                 {
210                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set delay to delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
211                         ph_control(mISDNport->b_addr[i], CMX_DELAY, mISDNport->b_port[i]->p_m_delay);
212                 }
213                 if (mISDNport->b_port[i]->p_m_txvol)
214                 {
215                         PDEBUG(DEBUG_BCHANNEL, "during activation, we change tx-volume to shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
216                         ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, mISDNport->b_port[i]->p_m_txvol);
217                 }
218                 if (mISDNport->b_port[i]->p_m_rxvol)
219                 {
220                         PDEBUG(DEBUG_BCHANNEL, "during activation, we change rx-volume to shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
221                         ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, mISDNport->b_port[i]->p_m_rxvol);
222                 }
223 //tone          if (mISDNport->b_port[i]->p_m_conf && !mISDNport->b_port[i]->p_m_tone)
224                 if (mISDNport->b_port[i]->p_m_conf)
225                 {
226                         PDEBUG(DEBUG_BCHANNEL, "during activation, we change conference to conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
227                         ph_control(mISDNport->b_addr[i], CMX_CONF_JOIN, mISDNport->b_port[i]->p_m_conf);
228                 }
229                 if (mISDNport->b_port[i]->p_m_echo)
230                 {
231                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set echo to echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
232                         ph_control(mISDNport->b_addr[i], CMX_ECHO_ON, 0);
233                 }
234                 if (mISDNport->b_port[i]->p_m_tone)
235                 {
236                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set tone to tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
237                         ph_control(mISDNport->b_addr[i], TONE_PATT_ON, mISDNport->b_port[i]->p_m_tone);
238                 }
239                 if (mISDNport->b_port[i]->p_m_rxoff)
240                 {
241                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set rxoff to rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
242                         ph_control(mISDNport->b_addr[i], CMX_RECEIVE_OFF, 0);
243                 }
244                 if (mISDNport->b_port[i]->p_m_txmix)
245                 {
246                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set txmix to txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
247                         ph_control(mISDNport->b_addr[i], CMX_MIX_ON, 0);
248                 }
249                 if (mISDNport->b_port[i]->p_m_dtmf)
250                 {
251                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set dtmf to dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
252                         ph_control(mISDNport->b_addr[i], DTMF_TONE_START, 0);
253                 }
254                 if (mISDNport->b_port[i]->p_m_crypt)
255                 {
256                         PDEBUG(DEBUG_BCHANNEL, "during activation, we set crypt to crypt=%d.\n", mISDNport->b_port[i]->p_m_crypt);
257                         ph_control_block(mISDNport->b_addr[i], BF_ENABLE_KEY, mISDNport->b_port[i]->p_m_crypt_key, mISDNport->b_port[i]->p_m_crypt_key_len);
258                 }
259
260                 /* preload tx-buffer */
261                 pre->len = mISDNport->b_port[i]->read_audio(p, ISDN_PRELOAD, 1);
262                 if (pre->len > 0)
263                 {
264                         PDEBUG(DEBUG_BCHANNEL, "port is activated, we fill our buffer (ISDN_PRELOAD = %d).\n", pre->len);
265                         /* flip bits */
266 #if 0
267                         q = p + pre->len;
268                         while(p != q)
269                                  *p++ = flip[*p];
270 #endif
271                         pre->prim = DL_DATA | REQUEST;
272                         pre->addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
273                         pre->dinfo = 0;
274                         mISDN_write(mISDNdevice, pre, mISDN_HEADER_LEN+pre->len, TIMEOUT_1SEC);
275                 }
276         }
277 }
278
279 static void bchannel_deactivate(struct mISDNport *mISDNport, int i)
280 {
281         iframe_t dact;
282
283         if (mISDNport->b_state[i] == B_STATE_ACTIVE)
284         {
285                 /* reset dsp features */
286                 if (mISDNport->b_port[i])
287                 {
288                         if (mISDNport->b_port[i]->p_m_delay)
289                         {
290                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset delay from delay=%d.\n", mISDNport->b_port[i]->p_m_delay);
291                                 ph_control(mISDNport->b_addr[i], CMX_JITTER, 0);
292                         }
293                         if (mISDNport->b_port[i]->p_m_txvol)
294                         {
295                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_txvol);
296                                 ph_control(mISDNport->b_addr[i], VOL_CHANGE_TX, 0);
297                         }
298                         if (mISDNport->b_port[i]->p_m_rxvol)
299                         {
300                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rx-volume from shift=%d.\n", mISDNport->b_port[i]->p_m_rxvol);
301                                 ph_control(mISDNport->b_addr[i], VOL_CHANGE_RX, 0);
302                         }
303                         if (mISDNport->b_port[i]->p_m_conf)
304                         {
305                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we release conference from conf=%d.\n", mISDNport->b_port[i]->p_m_conf);
306                                 ph_control(mISDNport->b_addr[i], CMX_CONF_SPLIT, 0);
307                         }
308                         if (mISDNport->b_port[i]->p_m_echo)
309                         {
310                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset echo from echo=%d.\n", mISDNport->b_port[i]->p_m_echo);
311                                 ph_control(mISDNport->b_addr[i], CMX_ECHO_OFF, 0);
312                         }
313                         if (mISDNport->b_port[i]->p_m_tone)
314                         {
315                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset tone from tone=%d.\n", mISDNport->b_port[i]->p_m_tone);
316                                 ph_control(mISDNport->b_addr[i], TONE_PATT_OFF, 0);
317                         }
318                         if (mISDNport->b_port[i]->p_m_rxoff)
319                         {
320                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset rxoff from rxoff=%d.\n", mISDNport->b_port[i]->p_m_rxoff);
321                                 ph_control(mISDNport->b_addr[i], CMX_RECEIVE_ON, 0);
322                         }
323                         if (mISDNport->b_port[i]->p_m_txmix)
324                         {
325                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset txmix from txmix=%d.\n", mISDNport->b_port[i]->p_m_txmix);
326                                 ph_control(mISDNport->b_addr[i], CMX_MIX_OFF, 0);
327                         }
328                         if (mISDNport->b_port[i]->p_m_dtmf)
329                         {
330                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset dtmf from dtmf=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
331                                 ph_control(mISDNport->b_addr[i], DTMF_TONE_STOP, 0);
332                         }
333                         if (mISDNport->b_port[i]->p_m_crypt)
334                         {
335                                 PDEBUG(DEBUG_BCHANNEL, "during deactivation, we reset crypt from crypt=%d.\n", mISDNport->b_port[i]->p_m_dtmf);
336                                 ph_control(mISDNport->b_addr[i], BF_DISABLE, 0);
337                         }
338                 }
339                 /* deactivate bchannel */
340                 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d), because currently active.\n", i);
341                 dact.prim = DL_RELEASE | REQUEST; 
342                 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
343                 dact.dinfo = 0;
344                 dact.len = 0;
345                 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
346                 mISDNport->b_state[i] = B_STATE_DEACTIVATING;
347                 return;
348         }
349 }
350
351
352 /*
353  * check for available channel and reserve+set it.
354  * give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
355  * give exclusiv flag
356  * returns -(cause value) or x = channel x or 0 = no channel
357  */
358 denke ans aktivieren und deaktivieren
359 int PmISDN::seize_bchannel(int channel, int exclusive)
360 {
361         int i;
362
363         /* the channel is what we have */
364         if (p_m_b_channel == channel)
365                 return(channel);
366
367         /* if channel already in use, release it */
368         if (p_m_b_channel)
369                 drop_bchannel();
370
371         /* if CHANNEL_NO */
372         if (channel==CHANNEL_NO || channel==0)
373                 return(0);
374         
375         /* is channel in range ? */
376         if (channel==16
377          || (channel>p_m_mISDNport->b_num && channel<16)
378          || ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
379                 return(-6); /* channel unacceptable */
380
381         /* request exclusive channel */
382         if (exclusive && channel>0)
383         {
384                 i = channel-1-(channel>16);
385                 if (p_m_mISDNport->b_port[i])
386                         return(-44); /* requested channel not available */
387                 goto seize;
388         }
389
390         /* ask for channel */
391         if (channel>0)
392         {
393                 i = channel-1-(channel>16);
394                 if (p_m_mISDNport->b_port[i] == NULL)
395                         goto seize;
396         }
397
398         /* search for channel */
399         i = 0;
400         while(i < p_m_mISDNport->b_num)
401         {
402                 if (!p_m_mISDNport->b_port[i])
403                 {
404                         channel = i+1+(i>=15);
405                         goto seize;
406                 }
407                 i++;
408         }
409         return(-34); /* no free channel */
410
411 seize:
412         /* link Port */
413         p_m_mISDNport->b_port[i] = this;
414         p_m_b_index = i;
415         p_m_b_channel = channel;
416         p_m_b_exclusive = exclusive;
417         p_m_b_stid = p_m_mISDNport->b_stid[i];
418         p_m_b_addr = p_m_mISDNport->b_addr[i];
419         p_m_jittercheck = 0;
420
421         /* reserve channel */
422         if (p_m_b_reserve == 0) // already reserved
423         {
424                 p_m_b_reserve = 1;
425                 mISDNport->b_reserved++;
426         }
427
428         PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
429
430         return(channel);
431 }
432
433 /*
434  * drop reserved channel and unset it.
435  */
436 void PmISDN::drop_bchannel(void)
437 {
438         /* unreserve channel */
439         if (p_m_b_reserve)
440                 p_m_mISDNport->b_reserved--;
441         p_m_b_reserve = 0;
442
443         /* if not in use */
444         if (!p_m_b_channel)
445                 return;
446
447         PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
448
449         p_m_mISDNport->b_port[p_m_b_index] = NULL;
450         p_m_b_index = -1;
451         p_m_b_channel = 0;
452         p_m_b_exclusive = 0;
453         p_m_b_addr = 0;
454         p_m_b_stid = 0;
455 }
456
457 /*
458  * handler
459  */
460 int PmISDN::handler(void)
461 {
462         struct message *message;
463
464         // NOTE: deletion is done by the child class
465
466         /* handle timeouts */
467         if (p_m_timeout)
468         {
469                 if (p_m_timer+p_m_timeout < now_d)
470                 {
471                         PDEBUG(DEBUG_ISDN, "(%s) timeout after %d seconds detected (state=%d).\n", p_name, p_m_timeout, p_state);
472                         p_m_timeout = 0;
473                         /* send timeout to endpoint */
474                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TIMEOUT);
475                         message->param.state = p_state;
476                         message_put(message);
477                 }
478                 return(1);
479         }
480         
481         return(0); /* nothing done */
482 }
483
484
485 /*
486  * whenever we get audio data from bchannel, we process it here
487  */
488 void PmISDN::bchannel_receive(iframe_t *frm)
489 {
490         unsigned char *data_temp;
491         unsigned long length_temp;
492         struct message *message;
493         struct timeval tv;
494         struct timezone tz;
495         long long jitter_now;
496         int newlen;
497         unsigned char *p;
498         int l;
499         unsigned long cont;
500 //      iframe_t rsp; /* response to possible indication */
501 #if 0
502 #warning BCHANNEL-DEBUG
503 {
504         // check if we are part of all ports */
505         class Port *port = port_first;
506         while(port)
507         {
508                 if (port==this)
509                         break;
510                 port=port->next;
511         }
512         if (!port)
513         {
514                 PERROR_RUNTIME("**************************************************\n");
515                 PERROR_RUNTIME("*** BCHANNEL-DEBUG: !this! is not in list of ports\n");
516                 PERROR_RUNTIME("**************************************************\n");
517                 return;
518         }
519 }
520 #endif
521
522
523         if (frm->prim == (PH_CONTROL | INDICATION))
524         {
525                 cont = *((unsigned long *)&frm->data.p);
526                 // PDEBUG(DEBUG_PORT, "PmISDN(%s) received a PH_CONTROL INDICATION 0x%x\n", p_name, cont);
527                 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
528                 {
529                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DTMF);
530                         message->param.dtmf = cont & DTMF_TONE_MASK;
531                         PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL  DTMF digit '%c'\n", p_name, message->param.dtmf);
532                         message_put(message);
533                 }
534                 switch(cont)
535                 {
536                         case BF_REJECT:
537                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
538                         message->param.crypt.type = CC_ERROR_IND;
539                         PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL  reject of blowfish.\n", p_name);
540                         message_put(message);
541                         break;
542
543                         case BF_ACCEPT:
544                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
545                         message->param.crypt.type = CC_ACTBF_CONF;
546                         PDEBUG(DEBUG_PORT, "PmISDN(%s) PH_CONTROL  accept of blowfish.\n", p_name);
547                         message_put(message);
548                         break;
549                 }
550                 return;
551         }
552
553         /* calls will not process any audio data unless
554          * the call is connected OR tones feature is enabled.
555          */
556         if (p_state!=PORT_STATE_CONNECT
557          && !p_m_mISDNport->is_tones)
558                 return;
559
560 #if 0
561         /* the bearer capability must be audio in order to send and receive
562          * audio prior or after connect.
563          */
564         if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
565                 return;
566 #endif
567
568         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
569         if (p_m_rxoff)
570         {
571                 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
572                 return;
573         }
574
575         /* randomize and listen to crypt message if enabled */
576         if (p_m_crypt_listen)
577         {
578                 /* the noisy randomizer */
579                 p = (unsigned char *)&frm->data.p;
580                 l = frm->len;
581                 while(l--)
582                         mISDN_rand[mISDN_rand_count & 0xff] += *p++;
583
584                 cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
585         }
586
587         /* prevent jitter */
588         gettimeofday(&tv, &tz);
589         jitter_now = tv.tv_sec;
590         jitter_now = jitter_now*8000 + tv.tv_usec/125;
591         if (p_m_jittercheck == 0)
592         {
593                 p_m_jittercheck = jitter_now;
594                 p_m_jitterdropped = 0;
595         } else
596                 p_m_jittercheck += frm->len;
597         if (p_m_jittercheck < jitter_now)
598         {
599 //              PERROR("jitter: ignoring slow data\n");
600                 p_m_jittercheck = jitter_now;
601         } else
602         if (p_m_jittercheck-ISDN_JITTERLIMIT > jitter_now)
603         {
604                 p_m_jitterdropped += frm->len;
605                 p_m_jittercheck -= frm->len;
606                 /* must exit here */
607                 return;
608         } else
609         if (p_m_jitterdropped)
610         {
611                 PERROR("jitter: dropping, caused by fast data: %lld\n", p_m_jitterdropped);
612                 p_m_jitterdropped = 0;
613         }
614
615         p = (unsigned char *)&frm->data.p;
616
617         /* send data to epoint */
618         if (ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
619         {
620 //printf("we are port %s and sending to epoint %d\n", p_m_cardname, p_epoint->serial);
621                 length_temp = frm->len;
622                 data_temp = p;
623                 while(length_temp)
624                 {
625                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
626                         message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
627                         memcpy(message->param.data.data, data_temp, message->param.data.len);
628                         message->param.data.compressed = 1;
629                         message->param.data.port_id = p_serial;
630                         message->param.data.port_type = p_type;
631                         message_put(message);
632                         if (length_temp <= sizeof(message->param.data.data))
633                                 break;
634                         data_temp += sizeof(message->param.data.data);
635                         length_temp -= sizeof(message->param.data.data);
636                 }
637         }
638         /* return the same number of tone data, as we recieved */
639         newlen = read_audio(p, frm->len, 1);
640         /* send tone data to isdn device only if we have data, otherwhise we send nothing */
641         if (newlen>0 && (p_tone_fh>=0 || p_tone_fetched || !p_m_nodata || p_m_crypt_msg_loops))
642         {
643 //printf("jolly: sending.... %d %d %d %d %d\n", newlen, p_tone_fh, p_tone_fetched, p_m_nodata, p_m_crypt_msg_loops);
644                 if (p_m_txmix_on)
645                 {
646                         p_m_txmix_on -= newlen;
647                         if (p_m_txmix_on <= 0)
648                         {
649                                 p_m_txmix_on = 0;
650                                 p_m_txmix = !p_m_nodata;
651                                 PDEBUG(DEBUG_BCHANNEL, "after sending message, we set txmix to txmix=%d.\n", p_m_txmix);
652                                 if (p_m_txmix)
653                                         ph_control(p_m_b_addr, CMX_MIX_ON, 0);
654                         }
655                 }
656                 if (p_m_crypt_msg_loops)
657                 {
658                         /* send pending message */
659                         int tosend;
660
661                         tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
662                         if (tosend > newlen)
663                                 tosend = newlen;
664                         memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
665                         p_m_crypt_msg_current += tosend;
666                         if (p_m_crypt_msg_current == p_m_crypt_msg_len)
667                         {
668                                 p_m_crypt_msg_current = 0;
669                                 p_m_crypt_msg_loops--;
670 // we need to disable rxmix some time after sending the loops...
671                                 if (!p_m_crypt_msg_loops && p_m_txmix)
672                                 {
673                                         p_m_txmix_on = 8000; /* one sec */
674                                 }
675                         }
676                 }
677                 frm->prim = frm->prim & 0xfffffffc | REQUEST; 
678                 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
679                 frm->len = newlen;
680                 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
681
682                 if (p_debug_nothingtosend)
683                 {
684                         p_debug_nothingtosend = 0;
685                         PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
686                 } 
687         } else
688         {
689                 if (!p_debug_nothingtosend)
690                 {
691                         p_debug_nothingtosend = 1;
692                         PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
693                 } 
694         }
695 #if 0
696         /* response to the data indication */
697         rsp.prim = frm->prim & 0xfffffffc | RESPONSE; 
698         rsp.addr = frm->addr & INST_ID_MASK | FLG_MSG_DOWN;
699         rsp.dinfo = frm->dinfo;
700         rsp.len = 0;
701         mISDN_write(mISDNdevice, &rsp, mISDN_HEADER_LEN+rsp.len, TIMEOUT_1SEC);
702 //PDEBUG(DEBUG_ISDN, "written %d bytes.\n", length);
703 #endif
704 }
705
706
707 /*
708  * set echotest
709  */
710 void PmISDN::set_echotest(int echo)
711 {
712         if (p_m_echo != echo)
713         {
714                 p_m_echo = echo;
715                 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
716                 if (p_m_b_channel)
717                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
718                                 ph_control(p_m_b_addr, p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
719         }
720 }
721
722 /*
723  * set tone
724  */
725 void PmISDN::set_tone(char *dir, char *tone)
726 {
727         int id;
728
729         if (!tone)
730                 tone = "";
731         PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
732         if (!tone[0])
733         {
734                 id = TONE_OFF;
735                 goto setdsp;
736         }
737
738         /* check if we NOT really have to use a dsp-tone */
739         if (!options.dsptones)
740         {
741                 nodsp:
742                 if (p_m_tone)
743                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
744                 {
745                         PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
746                         ph_control(p_m_b_addr, TONE_PATT_OFF, 0);
747                 }
748                 p_m_tone = 0;
749                 Port::set_tone(dir, tone);
750                 return;
751         }
752         if (p_tone_dir[0])
753                 goto nodsp;
754
755         /* now we USE dsp-tone, convert name */
756         else if (!strcmp(tone, "dialtone"))
757         {
758                 switch(options.dsptones) {
759                 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
760                 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
761                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
762                 }
763         } else if (!strcmp(tone, "dialpbx"))
764         {
765                 switch(options.dsptones) {
766                 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
767                 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
768                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
769                 }
770         } else if (!strcmp(tone, "ringing"))
771         {
772                 switch(options.dsptones) {
773                 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
774                 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
775                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
776                 }
777         } else if (!strcmp(tone, "ringpbx"))
778         {
779                 switch(options.dsptones) {
780                 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
781                 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
782                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
783                 }
784         } else if (!strcmp(tone, "busy"))
785         {
786                 busy:
787                 switch(options.dsptones) {
788                 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
789                 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
790                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
791                 }
792         } else if (!strcmp(tone, "release"))
793         {
794                 hangup:
795                 switch(options.dsptones) {
796                 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
797                 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
798                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
799                 }
800         } else if (!strcmp(tone, "cause_10"))
801                 goto hangup;
802         else if (!strcmp(tone, "cause_11"))
803                 goto busy;
804         else if (!strcmp(tone, "cause_22"))
805         {
806                 switch(options.dsptones) {
807                 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
808                 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
809                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
810                 }
811         } else if (!strncmp(tone, "cause_", 6))
812                 id = TONE_SPECIAL_INFO;
813         else
814                 id = TONE_OFF;
815
816         /* if we have a tone that is not supported by dsp */
817         if (id==TONE_OFF && tone[0])
818                 goto nodsp;
819
820         setdsp:
821         if (p_m_tone != id)
822         {
823                 /* set new tone */
824                 p_m_tone = id;
825                 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
826                 if (p_m_b_channel)
827                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
828                                 ph_control(p_m_b_addr, p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone);
829         }
830         /* turn user-space tones off in cases of no tone OR dsp tone */
831         Port::set_tone("",NULL);
832 }
833
834
835 /* MESSAGE_mISDNSIGNAL */
836 //extern struct message *dddebug;
837 void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union parameter *param)
838 {
839         switch(param->mISDNsignal.message)
840         {
841                 case mISDNSIGNAL_VOLUME:
842                 if (p_m_txvol != param->mISDNsignal.txvol)
843                 {
844                         p_m_txvol = param->mISDNsignal.txvol;
845                         PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
846                         if (p_m_b_channel)
847                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
848                                         ph_control(p_m_b_addr, VOL_CHANGE_TX, p_m_txvol);
849                 }
850                 if (p_m_rxvol != param->mISDNsignal.rxvol)
851                 {
852                         p_m_rxvol = param->mISDNsignal.rxvol;
853                         PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
854                         if (p_m_b_channel)
855                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
856                                         ph_control(p_m_b_addr, VOL_CHANGE_RX, p_m_rxvol);
857                 }
858                 break;
859
860                 case mISDNSIGNAL_CONF:
861 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
862 //tone          if (!p_m_tone && p_m_conf!=param->mISDNsignal.conf)
863                 if (p_m_conf != param->mISDNsignal.conf)
864                 {
865                         p_m_conf = param->mISDNsignal.conf;
866                         PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
867                         if (p_m_b_channel)
868                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
869                                         ph_control(p_m_b_addr, (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf);
870                 }
871                 /* we must set, even if currently tone forbids conf */
872                 p_m_conf = param->mISDNsignal.conf;
873 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
874                 break;
875
876                 case mISDNSIGNAL_NODATA:
877                 p_m_nodata = param->mISDNsignal.nodata;
878                 if (p_m_txmix == p_m_nodata) /* txmix != !nodata */
879                 {
880                         p_m_txmix = !p_m_nodata;
881                         PDEBUG(DEBUG_BCHANNEL, "we change mix mode to txmix=%d (nodata=%d).\n", p_m_txmix, p_m_nodata);
882                         if (p_m_b_channel)
883                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
884                                         ph_control(p_m_b_addr, p_m_txmix?CMX_MIX_ON:CMX_MIX_OFF, 0);
885                 }
886                 break;
887
888 #if 0
889                 case mISDNSIGNAL_RXOFF:
890                 if (p_m_rxoff != param->mISDNsignal.rxoff)
891                 {
892                         p_m_rxoff = param->mISDNsignal.rxoff;
893                         PDEBUG(DEBUG_BCHANNEL, "we change receive mode to rxoff=%d.\n", p_m_rxoff);
894                         if (p_m_b_channel)
895                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
896                                         ph_control(p_m_b_addr, p_m_rxoff?CMX_RECEIVE_OFF:CMX_RECEIVE_ON, 0);
897                 }
898                 break;
899
900                 case mISDNSIGNAL_DTMF:
901                 if (p_m_dtmf != param->mISDNsignal.dtmf)
902                 {
903                         p_m_dtmf = param->mISDNsignal.dtmf;
904                         PDEBUG(DEBUG_BCHANNEL, "we change dtmf mode to dtmf=%d.\n", p_m_dtmf);
905                         if (p_m_b_channel)
906                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
907                                         ph_control(p_m_b_addr, p_m_dtmf?DTMF_TONE_START:DTMF_TONE_STOP, 0);
908                 }
909                 break;
910
911 #endif
912                 default:
913                 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
914         }
915 }
916
917 /* MESSAGE_CRYPT */
918 void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parameter *param)
919 {
920         struct message *message;
921
922         switch(param->crypt.type)
923         {
924                 case CC_ACTBF_REQ:           /* activate blowfish */
925                 p_m_crypt = 1;
926                 p_m_crypt_key_len = param->crypt.len;
927                 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key))
928                 {
929                         PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
930                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
931                         message->param.crypt.type = CC_ERROR_IND;
932                         message_put(message);
933                         break;
934                 }
935                 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
936                 crypt_off:
937                 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
938                 if (p_m_b_channel)
939                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
940                                 ph_control_block(p_m_b_addr, p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len);
941                 break;
942
943                 case CC_DACT_REQ:            /* deactivate session encryption */
944                 p_m_crypt = 0;
945                 goto crypt_off;
946                 break;
947
948                 case CR_LISTEN_REQ:          /* start listening to messages */
949                 p_m_crypt_listen = 1;
950                 p_m_crypt_listen_state = 0;
951                 break;
952
953                 case CR_UNLISTEN_REQ:        /* stop listening to messages */
954                 p_m_crypt_listen = 0;
955                 break;
956
957                 case CR_MESSAGE_REQ:         /* send message */
958                 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
959                 if (!p_m_crypt_msg_len)
960                 {
961                         PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
962                         break;
963                 }
964                 p_m_crypt_msg_current = 0; /* reset */
965                 p_m_crypt_msg_loops = 3; /* enable */
966                 /* disable txmix, or we get corrupt data due to audio process */
967                 if (p_m_txmix)
968                 {
969                         PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
970                         ph_control(p_m_b_addr, CMX_MIX_OFF, 0);
971                 }
972                 break;
973
974                 default:
975                 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
976         }
977
978 }
979
980 /*
981  * endpoint sends messages to the port
982  */
983 int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
984 {
985         if (Port::message_epoint(epoint_id, message_id, param))
986                 return(1);
987
988         switch(message_id)
989         {
990                 case MESSAGE_mISDNSIGNAL: /* user command */
991                 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
992                 message_mISDNsignal(epoint_id, message_id, param);
993                 return(1);
994
995                 case MESSAGE_CRYPT: /* crypt control command */
996                 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
997                 message_crypt(epoint_id, message_id, param);
998                 return(1);
999         }
1000
1001         return(0);
1002 }
1003
1004
1005 /*
1006  * main loop for processing messages from mISDN device
1007  */
1008 int mISDN_handler(void)
1009 {
1010         int ret;
1011         msg_t *msg;
1012         iframe_t *frm;
1013         struct mISDNport *mISDNport;
1014         class PmISDN *isdnport;
1015         net_stack_t *nst;
1016         msg_t *dmsg;
1017         mISDNuser_head_t *hh;
1018         int i;
1019
1020         /* the que avoids loopbacks when replying to stack after receiving
1021          * from stack. */
1022         mISDNport = mISDNport_first;
1023         while(mISDNport)
1024         {
1025                 /* process turning off rx */
1026                 i = 0;
1027                 while(i < mISDNport->b_num)
1028                 {
1029                         if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
1030                         {
1031                                 isdnport=mISDNport->b_port[i];
1032                                 if (isdnport->p_tone_name[0] || isdnport->p_tone_fh>=0 || isdnport->p_tone_fetched || !isdnport->p_m_nodata || isdnport->p_m_crypt_msg_loops || isdnport->p_m_crypt_listen || isdnport->p_record)
1033                                 {
1034                                         /* rx IS required */
1035                                         if (isdnport->p_m_rxoff)
1036                                         {
1037                                                 /* turn on RX */
1038                                                 isdnport->p_m_rxoff = 0;
1039                                                 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
1040                                                 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_ON, 0);
1041                                         }
1042                                 } else
1043                                 {
1044                                         /* rx NOT required */
1045                                         if (!isdnport->p_m_rxoff)
1046                                         {
1047                                                 /* turn off RX */
1048                                                 isdnport->p_m_rxoff = 1;
1049                                                 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
1050                                                 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_OFF, 0);
1051                                         }
1052                                 }
1053                                 isdnport->p_m_jittercheck = 0; /* reset jitter detection check */
1054                         }
1055                         i++;
1056                 }
1057 #if 0
1058                 if (mISDNport->l1timeout && now>mISDNport->l1timeout)
1059                 {
1060                         PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
1061                         mISDNport->l1timeout = 0;
1062 #endif
1063
1064                 if (mISDNport->l2establish)
1065                 {
1066                         if (now-mISDNport->l2establish > 5)
1067                         {
1068                                 if (mISDNport->ntmode)
1069                                 {
1070                                         PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link NT portnum=%d.\n", mISDNport->portnum);
1071                                         time(&mISDNport->l2establish);
1072                                         /* establish */
1073                                         dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1074                                         if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1075                                                 free_msg(dmsg);
1076                                 } else {
1077                                         PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
1078                                         time(&mISDNport->l2establish);
1079                                         /* establish */
1080                                         iframe_t act;
1081                                         act.prim = DL_ESTABLISH | REQUEST; 
1082                                         act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
1083                                         act.dinfo = 0;
1084                                         act.len = 0;
1085                                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1086                                 }
1087                         }
1088                 }
1089                 if ((dmsg = msg_dequeue(&mISDNport->downqueue)))
1090                 {
1091                         if (mISDNport->ntmode)
1092                         {
1093                                 hh = (mISDNuser_head_t *)dmsg->data;
1094                                 PDEBUG(DEBUG_ISDN, "sending queued NT l3-down-message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, dmsg->len);
1095                                 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1096                                         free_msg(dmsg);
1097                         } else
1098                         {
1099                                 frm = (iframe_t *)dmsg->data;
1100                                 frm->addr = mISDNport->upper_id | FLG_MSG_DOWN;
1101                                 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
1102                                 PDEBUG(DEBUG_ISDN, "sending queued TE l3-down-message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, dmsg->len);
1103                                 mISDN_write(mISDNdevice, dmsg->data, dmsg->len, TIMEOUT_1SEC);
1104                                 free_msg(dmsg);
1105                         }
1106                         return(1);
1107                 }
1108                 mISDNport = mISDNport->next;
1109         } 
1110
1111         /* get message from kernel */
1112         if (!(msg = alloc_msg(MAX_MSG_SIZE)))
1113                 return(1);
1114         ret = mISDN_read(mISDNdevice, msg->data, MAX_MSG_SIZE, 0);
1115         if (ret < 0)
1116         {
1117                 free_msg(msg);
1118                 if (errno == EAGAIN)
1119                         return(0);
1120                 PERROR("FATAL ERROR: failed to do mISDN_read()\n");
1121                 exit(-1); 
1122         }
1123         if (!ret)
1124         {
1125                 free_msg(msg);
1126 //              printf("%s: ERROR: mISDN_read() returns nothing\n");
1127                 return(0);
1128         }
1129         msg->len = ret;
1130         frm = (iframe_t *)msg->data;
1131
1132         /* global prim */
1133         switch(frm->prim)
1134         {
1135                 case MGR_INITTIMER | CONFIRM:
1136                 case MGR_ADDTIMER | CONFIRM:
1137                 case MGR_DELTIMER | CONFIRM:
1138                 case MGR_REMOVETIMER | CONFIRM:
1139 //              if (options.deb & DEBUG_ISDN)
1140 //                      PDEBUG(DEBUG_ISDN, "timer-confirm\n");
1141                 free_msg(msg);
1142                 return(1);
1143         }
1144
1145         /* find the port */
1146         mISDNport = mISDNport_first;
1147         while(mISDNport)
1148         {
1149                 if ((frm->prim==(MGR_TIMER | INDICATION)) && mISDNport->ntmode)
1150                 {
1151                         itimer_t *it = mISDNport->nst.tlist;
1152
1153                         /* find timer */
1154                         while(it)
1155                         {
1156                                 if (it->id == (int)frm->addr)
1157                                         break;
1158                                 it = it->next;
1159                         }
1160                         if (it)
1161                         {
1162                                 mISDN_write_frame(mISDNdevice, msg->data, mISDNport->upper_id | FLG_MSG_DOWN,
1163                                         MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1164
1165                                 PDEBUG(DEBUG_ISDN, "timer-indication %s port %d it=%p\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, it);
1166                                 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1167                                 ret = it->function(it->data);
1168                                 break;
1169                         }
1170                         /* we will continue here because we have a timer for a different mISDNport */
1171                 }
1172 //printf("comparing frm->addr %x with upper_id %x\n", frm->addr, mISDNport->upper_id);
1173                 if ((frm->addr&STACK_ID_MASK) == (unsigned int)(mISDNport->upper_id&STACK_ID_MASK))
1174                 {
1175                         /* d-message */
1176                         switch(frm->prim)
1177                         {
1178                                 case MGR_SHORTSTATUS | INDICATION:
1179                                 case MGR_SHORTSTATUS | CONFIRM:
1180                                 switch(frm->dinfo) {
1181                                         case SSTATUS_L1_ACTIVATED:
1182                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1183                                         goto ss_act;
1184                                         case SSTATUS_L1_DEACTIVATED:
1185                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1186                                         goto ss_deact;
1187                                         case SSTATUS_L2_ESTABLISHED:
1188                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_ESTABLISHED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1189                                         goto ss_estab;
1190                                         case SSTATUS_L2_RELEASED:
1191                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_RELEASED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1192                                         goto ss_rel;
1193                                 }
1194                                 break;
1195
1196                                 case PH_ACTIVATE | CONFIRM:
1197                                 case PH_ACTIVATE | INDICATION:
1198                                 PDEBUG(DEBUG_ISDN, "Received PH_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1199                                 if (mISDNport->ntmode)
1200                                 {
1201                                         mISDNport->l1link = 1;
1202                                         setup_queue(mISDNport, 1);
1203                                         goto l1_msg;
1204                                 }
1205                                 ss_act:
1206                                 mISDNport->l1link = 1;
1207                                 setup_queue(mISDNport, 1);
1208                                 break;
1209
1210                                 case PH_DEACTIVATE | CONFIRM:
1211                                 case PH_DEACTIVATE | INDICATION:
1212                                 PDEBUG(DEBUG_ISDN, "Received PH_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1213                                 if (mISDNport->ntmode)
1214                                 {
1215                                         mISDNport->l1link = 0;
1216                                         setup_queue(mISDNport, 0);
1217                                         goto l1_msg;
1218                                 }
1219                                 ss_deact:
1220                                 mISDNport->l1link = 0;
1221                                 setup_queue(mISDNport, 0);
1222                                 break;
1223
1224                                 case PH_CONTROL | CONFIRM:
1225                                 case PH_CONTROL | INDICATION:
1226                                 PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1227                                 break;
1228
1229                                 case DL_ESTABLISH | INDICATION:
1230                                 case DL_ESTABLISH | CONFIRM:
1231 //                              PDEBUG(DEBUG_ISDN, "addr 0x%x established data link (DL) TE portnum=%d (DL_ESTABLISH)\n", frm->addr, mISDNport->portnum);
1232                                 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1233                                 ss_estab:
1234 //                              if (mISDNport->ntmode)
1235 //                                      break;
1236                                 if (mISDNport->l2establish)
1237                                 {
1238                                         mISDNport->l2establish = 0;
1239                                         PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1240                                 }
1241                                 mISDNport->l2link = 1;
1242                                 break;
1243
1244                                 case DL_RELEASE | INDICATION:
1245                                 case DL_RELEASE | CONFIRM:
1246 //                              PDEBUG(DEBUG_ISDN, "addr 0x%x released data link (DL) TE portnum=%d (DL_RELEASE)\n", frm->addr, mISDNport->portnum);
1247                                 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1248                                 ss_rel:
1249 //                              if (mISDNport->ntmode)
1250 //                                      break;
1251                                 mISDNport->l2link = 0;
1252                                 if (mISDNport->ptp)
1253                                 {
1254                                         time(&mISDNport->l2establish);
1255                                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
1256                                 }
1257                                 break;
1258
1259                                 default:
1260                                 l1_msg:
1261                                 PDEBUG(DEBUG_STACK, "GOT d-msg from %s port %d prim 0x%x dinfo 0x%x addr 0x%x\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, frm->prim, frm->dinfo, frm->addr);
1262                                 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM))
1263                                 {
1264                                         PERROR("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
1265                                 }
1266                                 /* d-message */
1267                                 if (mISDNport->ntmode)
1268                                 {
1269                                         /* l1-data enters the nt-mode library */
1270                                         nst = &mISDNport->nst;
1271                                         if (nst->l1_l2(nst, msg))
1272                                                 free_msg(msg);
1273                                         return(1);
1274                                 } else
1275                                 {
1276                                         /* l3-data is sent to pbx */
1277                                         if (stack2manager_te(mISDNport, msg))
1278                                                 free_msg(msg);
1279                                         return(1);
1280                                 }
1281                         }
1282                         break;
1283                 }
1284 //PDEBUG(DEBUG_ISDN, "flg:%d upper_id=%x addr=%x\n", (frm->addr&FLG_CHILD_STACK), (mISDNport->b_addr[0])&(~IF_CHILDMASK), (frm->addr)&(~IF_CHILDMASK));
1285                 /* check if child, and if parent stack match */
1286                 if ((frm->addr&FLG_CHILD_STACK) && (((unsigned int)(mISDNport->b_addr[0])&(~CHILD_ID_MASK)&STACK_ID_MASK) == ((frm->addr)&(~CHILD_ID_MASK)&STACK_ID_MASK)))
1287                 {
1288                         /* b-message */
1289                         switch(frm->prim)
1290                         {
1291                                 /* we don't care about confirms, we use rx data to sync tx */
1292                                 case PH_DATA | CONFIRM:
1293                                 case DL_DATA | CONFIRM:
1294                                 break;
1295
1296                                 /* we receive audio data, we respond to it AND we send tones */
1297                                 case PH_DATA | INDICATION:
1298                                 case DL_DATA | INDICATION:
1299                                 case PH_CONTROL | INDICATION:
1300                                 i = 0;
1301                                 while(i < mISDNport->b_num)
1302                                 {
1303                                         if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1304                                                 break;
1305                                         i++;
1306                                 }
1307                                 if (i == mISDNport->b_num)
1308                                 {
1309                                         PERROR("unhandled b-message (address 0x%x).\n", frm->addr);
1310                                         break;
1311                                 }
1312                                 if (mISDNport->b_port[i])
1313                                 {
1314 //PERROR("port sech: %s data\n", mISDNport->b_port[i]->p_name);
1315                                         mISDNport->b_port[i]->bchannel_receive(frm);
1316                                 } else
1317                                         PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (address 0x%x), ignoring.\n", frm->addr);
1318                                 break;
1319
1320                                 case PH_ACTIVATE | INDICATION:
1321                                 case DL_ESTABLISH | INDICATION:
1322                                 case PH_ACTIVATE | CONFIRM:
1323                                 case DL_ESTABLISH | CONFIRM:
1324                                 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
1325                                 i = 0;
1326                                 while(i < mISDNport->b_num)
1327                                 {
1328                                         if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1329                                                 break;
1330                                         i++;
1331                                 }
1332                                 if (i == mISDNport->b_num)
1333                                 {
1334                                         PERROR("unhandled b-establish (address 0x%x).\n", frm->addr);
1335                                         break;
1336                                 }
1337                                 mISDNport->b_state[i] = B_STATE_ACTIVE;
1338                                 if (!mISDNport->b_port[i])
1339                                         bchannel_deactivate(mISDNport, i);
1340                                 else
1341                                         bchannel_activate(mISDNport, i);
1342                                 break;
1343
1344                                 case PH_DEACTIVATE | INDICATION:
1345                                 case DL_RELEASE | INDICATION:
1346                                 case PH_DEACTIVATE | CONFIRM:
1347                                 case DL_RELEASE | CONFIRM:
1348                                 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
1349                                 i = 0;
1350                                 while(i < mISDNport->b_num)
1351                                 {
1352                                         if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1353                                                 break;
1354                                         i++;
1355                                 }
1356                                 if (i == mISDNport->b_num)
1357                                 {
1358                                         PERROR("unhandled b-release (address 0x%x).\n", frm->addr);
1359                                         break;
1360                                 }
1361                                 mISDNport->b_state[i] = B_STATE_IDLE;
1362                                 if (mISDNport->b_port[i])
1363                                         bchannel_activate(mISDNport, i);
1364                                 else
1365                                         bchannel_deactivate(mISDNport, i);
1366                                 break;
1367                         }
1368                         break;
1369                 }
1370
1371                 mISDNport = mISDNport->next;
1372         } 
1373         if (!mISDNport)
1374         {
1375                 if (frm->prim == (MGR_TIMER | INDICATION))
1376                         PERROR("unhandled timer indication message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1377                 else
1378                         PERROR("unhandled message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1379 //              PERROR("test: is_child: %x  of stack %x == %x (baddr %x frm %x)\n", (frm->addr&FLG_CHILD_STACK), ((unsigned int)(mISDNport_first->b_addr[0])&(~CHILD_ID_MASK)&STACK_ID_MASK), ((frm->addr)&(~CHILD_ID_MASK)&STACK_ID_MASK), mISDNport_first->b_addr[0], frm->addr);
1380         }
1381
1382         free_msg(msg);
1383         return(1);
1384 }
1385
1386
1387 /*
1388  * global function to add a new card (port)
1389  */
1390 struct mISDNport *mISDN_port_open(int port, int ptp, int ptmp)
1391 {
1392         int ret;
1393         unsigned char buff[1025];
1394         iframe_t *frm = (iframe_t *)buff;
1395         stack_info_t *stinf;
1396         struct mISDNport *mISDNport, **mISDNportp;
1397         int i, cnt;
1398         layer_info_t li;
1399 //      interface_info_t ii;
1400         net_stack_t *nst;
1401         manager_t *mgr;
1402         mISDN_pid_t pid;
1403         int pri = 0;
1404         int nt = 0;
1405         iframe_t dact;
1406
1407         /* open mISDNdevice if not already open */
1408         if (mISDNdevice < 0)
1409         {
1410                 ret = mISDN_open();
1411                 if (ret < 0)
1412                 {
1413                         PERROR("cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", ret, errno, strerror(errno));
1414                         return(NULL);
1415                 }
1416                 mISDNdevice = ret;
1417                 PDEBUG(DEBUG_ISDN, "mISDN device opened.\n");
1418
1419                 /* create entity for layer 3 TE-mode */
1420                 mISDN_write_frame(mISDNdevice, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1421                 ret = mISDN_read_frame(mISDNdevice, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
1422                 if (ret < (int)mISDN_HEADER_LEN)
1423                 {
1424                         noentity:
1425                         fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
1426                         exit(-1);
1427                 }
1428                 entity = frm->dinfo & 0xffff;
1429                 if (!entity)
1430                         goto noentity;
1431                 PDEBUG(DEBUG_ISDN, "our entity for l3-processes is %d.\n", entity);
1432         }
1433
1434         /* query port's requirements */
1435         cnt = mISDN_get_stack_count(mISDNdevice);
1436         if (cnt <= 0)
1437         {
1438                 PERROR("Found no card. Please be sure to load card drivers.\n");
1439                 return(NULL);
1440         }
1441         if (port>cnt || port<1)
1442         {
1443                 PERROR("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
1444                 return(NULL);
1445         }
1446         ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
1447         if (ret < 0)
1448         {
1449                 PERROR("Cannot get stack info for port %d (ret=%d)\n", port, ret);
1450                 return(NULL);
1451         }
1452         stinf = (stack_info_t *)&frm->data.p;
1453         switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1454         {
1455                 case ISDN_PID_L0_TE_S0:
1456                 PDEBUG(DEBUG_ISDN, "TE-mode BRI S/T interface line\n");
1457                 break;
1458                 case ISDN_PID_L0_NT_S0:
1459                 PDEBUG(DEBUG_ISDN, "NT-mode BRI S/T interface port\n");
1460                 nt = 1;
1461                 break;
1462                 case ISDN_PID_L0_TE_U:
1463                 PDEBUG(DEBUG_ISDN, "TE-mode BRI U   interface line\n");
1464                 break;
1465                 case ISDN_PID_L0_NT_U:
1466                 PDEBUG(DEBUG_ISDN, "NT-mode BRI U   interface port\n");
1467                 nt = 1;
1468                 break;
1469                 case ISDN_PID_L0_TE_UP2:
1470                 PDEBUG(DEBUG_ISDN, "TE-mode BRI Up2 interface line\n");
1471                 break;
1472                 case ISDN_PID_L0_NT_UP2:
1473                 PDEBUG(DEBUG_ISDN, "NT-mode BRI Up2 interface port\n");
1474                 nt = 1;
1475                 break;
1476                 case ISDN_PID_L0_TE_E1:
1477                 PDEBUG(DEBUG_ISDN, "TE-mode PRI E1  interface line\n");
1478                 pri = 1;
1479                 break;
1480                 case ISDN_PID_L0_NT_E1:
1481                 PDEBUG(DEBUG_ISDN, "LT-mode PRI E1  interface port\n");
1482                 pri = 1;
1483                 nt = 1;
1484                 break;
1485                 default:
1486                 PERROR("unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
1487                 return(NULL);
1488         }
1489         if (nt)
1490         {
1491                 /* NT */
1492                 if (stinf->pid.protocol[1] == 0)
1493                 {
1494                         PERROR("Given port %d: Missing layer 1 NT-mode protocol.\n", port);
1495                         return(NULL);
1496                 }
1497                 if (stinf->pid.protocol[2])
1498                 {
1499                         PERROR("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
1500                         return(NULL);
1501                 }
1502         } else
1503         {
1504                 /* TE */
1505                 if (stinf->pid.protocol[1] == 0)
1506                 {
1507                         PERROR("Given port %d: Missing layer 1 protocol.\n", port);
1508                         return(NULL);
1509                 }
1510                 if (stinf->pid.protocol[2] == 0)
1511                 {
1512                         PERROR("Given port %d: Missing layer 2 protocol.\n", port);
1513                         return(NULL);
1514                 }
1515                 if (stinf->pid.protocol[3] == 0)
1516                 {
1517                         PERROR("Given port %d: Missing layer 3 protocol.\n", port);
1518                         return(NULL);
1519                 } else
1520                 {
1521                         switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
1522                         {
1523                                 case ISDN_PID_L3_DSS1USER:
1524                                 break;
1525
1526                                 default:
1527                                 PERROR("Given port %d: own protocol 0x%08x", port,stinf->pid.protocol[3]);
1528                                 return(NULL);
1529                         }
1530                 }
1531                 if (stinf->pid.protocol[4])
1532                 {
1533                         PERROR("Given port %d: Layer 4 protocol not allowed.\n", port);
1534                         return(NULL);
1535                 }
1536         }
1537
1538         /* add mISDNport structure */
1539         mISDNportp = &mISDNport_first;
1540         while(*mISDNportp)
1541                 mISDNportp = &mISDNport->next;
1542         mISDNport = (struct mISDNport *)calloc(1, sizeof(struct mISDNport));
1543         if (!mISDNport)
1544         {
1545                 PERROR("Cannot alloc mISDNport structure\n");
1546                 return(NULL);
1547         }
1548         pmemuse++;
1549         memset(mISDNport, 0, sizeof(mISDNport));
1550         *mISDNportp = mISDNport;
1551
1552         /* allocate ressources of port */
1553         msg_queue_init(&mISDNport->downqueue);
1554 //      SCPY(mISDNport->name, "noname");
1555         mISDNport->portnum = port;
1556         mISDNport->ntmode = nt;
1557         mISDNport->pri = pri;
1558         mISDNport->d_stid = stinf->id;
1559         PDEBUG(DEBUG_ISDN, "d_stid = 0x%x.\n", mISDNport->d_stid);
1560         mISDNport->b_num = stinf->childcnt;
1561         PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1562         if ((stinf->pid.protocol[2]&ISDN_PID_L2_DF_PTP) || (nt&&ptp) || pri)
1563         {
1564                 PDEBUG(DEBUG_ISDN, "Port is point-to-point.\n");
1565                 mISDNport->ptp = ptp = 1;
1566                 if (ptmp && nt)
1567                 {
1568                         PDEBUG(DEBUG_ISDN, "Port is forced to point-to-multipoint.\n");
1569                         mISDNport->ptp = ptp = 0;
1570                 }
1571         }
1572         i = 0;
1573         while(i < stinf->childcnt)
1574         {
1575                 mISDNport->b_stid[i] = stinf->child[i];
1576                 PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
1577                 i++;
1578         }
1579         memset(&li, 0, sizeof(li));
1580         UCPY(&li.name[0], (nt)?"net l2":"pbx l4");
1581         li.object_id = -1;
1582         li.extentions = 0;
1583         li.pid.protocol[nt?2:4] = (nt)?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1584         li.pid.layermask = ISDN_LAYER((nt?2:4));
1585         li.st = mISDNport->d_stid;
1586         ret = mISDN_new_layer(mISDNdevice, &li);
1587         if (ret)
1588         {
1589                 PERROR("Cannot add layer %d of port %d (ret %d)\n", nt?2:4, port, ret);
1590                 closeport:
1591                 mISDNport_close(mISDNport);
1592                 return(NULL);
1593         }
1594         mISDNport->upper_id = li.id;
1595         ret = mISDN_register_layer(mISDNdevice, mISDNport->d_stid, mISDNport->upper_id);
1596         if (ret)
1597         {
1598                 PERROR("Cannot register layer %d of port %d\n", nt?2:4, port);
1599                 goto closeport;
1600         }
1601         mISDNport->lower_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?1:3); // id of lower layer (nt=1, te=3)
1602         if (mISDNport->lower_id < 0)
1603         {
1604                 PERROR("Cannot get layer(%d) id of port %d\n", nt?1:3, port);
1605                 goto closeport;
1606         }
1607         mISDNport->upper_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?2:4); // id of uppermost layer (nt=2, te=4)
1608         if (mISDNport->upper_id < 0)
1609         {
1610                 PERROR("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
1611                 goto closeport;
1612         }
1613         PDEBUG(DEBUG_ISDN, "Layer %d of port %d added.\n", nt?2:4, port);
1614
1615         /* if ntmode, establish L1 to send the tei removal during start */
1616         if (mISDNport->ntmode)
1617         {
1618                 iframe_t act;
1619                 /* L1 */
1620                 act.prim = PH_ACTIVATE | REQUEST; 
1621                 act.addr = mISDNport->upper_id | FLG_MSG_DOWN;
1622                 printf("UPPER ID 0x%x, addr 0x%x\n",mISDNport->upper_id, act.addr);
1623                 act.dinfo = 0;
1624                 act.len = 0;
1625                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1626                 usleep(10000); /* to be sure, that l1 is up */
1627         }
1628
1629         /* create nst (nt-mode only) */
1630         if (nt)
1631         {
1632                 mgr = &mISDNport->mgr;
1633                 nst = &mISDNport->nst;
1634
1635                 mgr->nst = nst;
1636                 nst->manager = mgr;
1637
1638                 nst->l3_manager = stack2manager_nt; /* messages from nt-mode */
1639                 nst->device = mISDNdevice;
1640                 nst->cardnr = port;
1641                 nst->d_stid = mISDNport->d_stid;
1642
1643                 nst->feature = FEATURE_NET_HOLD;
1644                 if (ptp)
1645                         nst->feature |= FEATURE_NET_PTP;
1646                 if (pri)
1647                         nst->feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1648 #if 0
1649                 i = 0;
1650                 while(i < mISDNport->b_num)
1651                 {
1652                         nst->b_stid[i] = mISDNport->b_stid[i];
1653                         i++;
1654                 }
1655 #endif
1656                 nst->l1_id = mISDNport->lower_id;
1657                 nst->l2_id = mISDNport->upper_id;
1658
1659                 /* phd */       
1660                 msg_queue_init(&nst->down_queue);
1661
1662                 Isdnl2Init(nst);
1663                 Isdnl3Init(nst);
1664         }
1665
1666         /* if te-mode, query state link */
1667         if (!mISDNport->ntmode)
1668         {
1669                 iframe_t act;
1670                 /* L2 */
1671                 PDEBUG(DEBUG_ISDN, "sending short status request for port %d.\n", port);
1672                 act.prim = MGR_SHORTSTATUS | REQUEST; 
1673                 act.addr = mISDNport->upper_id | MSG_BROADCAST;
1674                 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
1675                 act.len = 0;
1676                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1677         }
1678         /* if ptp AND te-mode, pull up the link */
1679         if (mISDNport->ptp && !mISDNport->ntmode)
1680         {
1681                 iframe_t act;
1682                 /* L2 */
1683                 act.prim = DL_ESTABLISH | REQUEST; 
1684                 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 4 | FLG_MSG_DOWN;
1685                 act.dinfo = 0;
1686                 act.len = 0;
1687                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1688         }
1689         /* if ptp AND nt-mode, pull up the link */
1690         if (mISDNport->ptp && mISDNport->ntmode)
1691         {
1692                 msg_t *dmsg;
1693                 /* L2 */
1694                 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1695                 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1696                         free_msg(dmsg);
1697         }
1698         /* initially, we assume that the link is down, exept for nt-ptmp */
1699         mISDNport->l2link = (mISDNport->ntmode && !mISDNport->ptp)?1:0;
1700
1701         PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
1702
1703         /* add all bchannel layers */
1704         i = 0;
1705         while(i < mISDNport->b_num)
1706         {
1707                 /* create new layer */
1708                 PDEBUG(DEBUG_BCHANNEL, "creating bchannel %d (index %d).\n" , i+1+(i>=15), i);
1709                 memset(&li, 0, sizeof(li));
1710                 memset(&pid, 0, sizeof(pid));
1711                 li.object_id = -1;
1712                 li.extentions = 0;
1713                 li.st = mISDNport->b_stid[i];
1714                 UCPY(li.name, "B L4");
1715                 li.pid.layermask = ISDN_LAYER((4));
1716                 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1717                 ret = mISDN_new_layer(mISDNdevice, &li);
1718                 if (ret)
1719                 {
1720                         failed_new_layer:
1721                         PERROR("mISDN_new_layer() failed to add bchannel %d (index %d)\n", i+1+(i>=15), i);
1722                         goto closeport;
1723                 }
1724                 mISDNport->b_addr[i] = li.id;
1725                 if (!li.id)
1726                 {
1727                         goto failed_new_layer;
1728                 }
1729                 PDEBUG(DEBUG_BCHANNEL, "new layer (b_addr=0x%x)\n", mISDNport->b_addr[i]);
1730
1731                 /* create new stack */
1732                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1733                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1734                 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1735                 pid.protocol[4] = ISDN_PID_L4_B_USER;
1736                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1737                 ret = mISDN_set_stack(mISDNdevice, mISDNport->b_stid[i], &pid);
1738                 if (ret)
1739                 {
1740                         stack_error:
1741                         PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel (index %d) stid=0x%x\n", ret, i, mISDNport->b_stid[i]);
1742                         mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i], MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1743                         mISDNport->b_addr[i] = 0;
1744 //                      mISDNport->b_addr_low[i] = 0;
1745                         goto closeport;
1746                 }
1747                 ret = mISDN_get_setstack_ind(mISDNdevice, mISDNport->b_addr[i]);
1748                 if (ret)
1749                         goto stack_error;
1750
1751                 /* get layer id */
1752                 mISDNport->b_addr[i] = mISDN_get_layerid(mISDNdevice, mISDNport->b_stid[i], 4);
1753                 if (!mISDNport->b_addr[i])
1754                         goto stack_error;
1755                 /* deactivate bchannel if already enabled due to crash */
1756                 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d) as a precaution.\n", i);
1757                 dact.prim = DL_RELEASE | REQUEST; 
1758                 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
1759                 dact.dinfo = 0;
1760                 dact.len = 0;
1761                 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
1762
1763                 i++;
1764         }
1765         PDEBUG(DEBUG_ISDN, "- port %d %s (%s) %d b-channels\n", mISDNport->portnum, (mISDNport->ntmode)?"NT-mode":"TE-mode", (mISDNport->ptp)?"Point-To-Point":"Multipoint", mISDNport->b_num);
1766         printlog("opening port %d %s (%s) %d b-channels\n", mISDNport->portnum, (mISDNport->ntmode)?"NT-mode":"TE-mode", (mISDNport->ptp)?"Point-To-Point":"Multipoint", mISDNport->b_num);
1767         return(mISDNport);
1768 }
1769
1770
1771 /*
1772  * function to free ALL cards (ports)
1773  */
1774 void mISDNport_close_all(void)
1775 {
1776         /* free all ports */
1777         while(mISDNport_first)
1778                 mISDNport_close(mISDNport_first);
1779 }
1780
1781 /*
1782  * free only one port
1783  */
1784 void mISDNport_close(struct mISDNport *mISDNport)
1785 {
1786         struct mISDNport **mISDNportp;
1787         class Port *port;
1788         classs PmISDN *pmISDN;
1789         net_stack_t *nst;
1790         unsigned char buf[32];
1791         int i;
1792
1793         /* remove all port instance that are linked to this mISDNport */
1794         port = port_first;
1795         while(port)
1796         {
1797                 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
1798                 {
1799                         pmISDN = (class PmISDN)*port;
1800                         if (pmISDN->p_m_mISDNport)
1801                         {
1802                                 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", pnISDN->p_name, mISDNport->portnum);
1803                                 delete pmISDN;
1804                         }
1805                 }
1806                 port = port->next;
1807         }
1808
1809         printlog("closing port %d\n", mISDNport->portnum);
1810
1811         /* free bchannels */
1812         i = 0;
1813         while(i < mISDNport->b_num)
1814         {
1815                 bchannel_deactivate(mISDNport, i);
1816                 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
1817                 if (mISDNport->b_stid[i])
1818                 {
1819                         mISDN_clear_stack(mISDNdevice, mISDNport->b_stid[i]);
1820                         if (mISDNport->b_addr[i])
1821                                 mISDN_write_frame(mISDNdevice, buf, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1822                 }
1823                 i++;
1824         }
1825
1826         /* free ressources of port */
1827         msg_queue_purge(&mISDNport->downqueue);
1828
1829         /* free stacks */
1830         if (mISDNport->ntmode)
1831         {
1832                 nst = &mISDNport->nst;
1833                 if (nst->manager) /* to see if initialized */
1834                 {
1835                         PDEBUG(DEBUG_STACK, "the following messages are ok: one L3 process always exists (broadcast process) and some L2 instances (broadcast + current telephone's instances)\n");
1836                         cleanup_Isdnl3(nst);
1837                         cleanup_Isdnl2(nst);
1838
1839                         /* phd */
1840                         msg_queue_purge(&nst->down_queue);
1841                         if (nst->phd_down_msg)
1842                                 free(nst->phd_down_msg);
1843                 }
1844         }
1845
1846         PDEBUG(DEBUG_BCHANNEL, "freeing d-stack.\n");
1847         if (mISDNport->d_stid)
1848         {
1849 //              mISDN_clear_stack(mISDNdevice, mISDNport->d_stid);
1850                 if (mISDNport->lower_id)
1851                         mISDN_write_frame(mISDNdevice, buf, mISDNport->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1852         }
1853
1854         /* remove from list */
1855         mISDNportp = &mISDNport_first;
1856         while(*mISDNportp)
1857         {
1858                 if (*mISDNportp == mISDNport)
1859                 {
1860                         *mISDNportp = (*mISDNportp)->next;
1861                         break;
1862                 }
1863                 mISDNportp = &((*mISDNportp)->next);
1864         }
1865
1866         if (!(*mISDNportp))
1867         {
1868                 PERROR("software error, mISDNport not in list\n");
1869                 exit(-1);
1870         }
1871         
1872         memset(mISDNport, 0, sizeof(struct mISDNport));
1873         free(mISDNport);
1874         pmemuse--;
1875
1876         /* close mISDNdevice, if no port */
1877         if (mISDNdevice>=0 && mISDNport_first==NULL)
1878         {
1879                 /* free entity */
1880                 mISDN_write_frame(mISDNdevice, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
1881                 /* close device */
1882                 mISDN_close(mISDNdevice);
1883                 mISDNdevice = -1;
1884                 PDEBUG(DEBUG_ISDN, "mISDN device closed.\n");
1885         }
1886 }
1887
1888
1889 /*
1890  * global function to show all available isdn ports
1891  */
1892 void mISDN_port_info(void)
1893 {
1894         int err;
1895         int i, ii, p;
1896         int useable, nt, pri;
1897         unsigned char buff[1025];
1898         iframe_t *frm = (iframe_t *)buff;
1899         stack_info_t *stinf;
1900         int device;
1901
1902         /* open mISDN */
1903         if ((device = mISDN_open()) < 0)
1904         {
1905                 fprintf(stderr, "cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", device, errno, strerror(errno));
1906                 exit(-1);
1907         }
1908
1909         /* get number of stacks */
1910         i = 1;
1911         ii = mISDN_get_stack_count(device);
1912         printf("\n");
1913         if (ii <= 0)
1914         {
1915                 printf("Found no card. Please be sure to load card drivers.\n");
1916         }
1917
1918         /* loop the number of cards and get their info */
1919         while(i <= ii)
1920         {
1921                 err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
1922                 if (err <= 0)
1923                 {
1924                         fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
1925                         break;
1926                 }
1927                 stinf = (stack_info_t *)&frm->data.p;
1928
1929                 nt = pri = 0;
1930                 useable = 1;
1931
1932                 /* output the port info */
1933                 printf("Port %2d: ", i);
1934                 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1935                 {
1936                         case ISDN_PID_L0_TE_S0:
1937                         printf("TE-mode BRI S/T interface line (for phone lines)");
1938 #if 0
1939                         if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
1940                                 printf(" HFC multiport card");
1941 #endif
1942                         break;
1943                         case ISDN_PID_L0_NT_S0:
1944                         nt = 1;
1945                         printf("NT-mode BRI S/T interface port (for phones)");
1946 #if 0
1947                         if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
1948                                 printf(" HFC multiport card");
1949 #endif
1950                         break;
1951                         case ISDN_PID_L0_TE_U:
1952                         printf("TE-mode BRI U   interface line");
1953                         break;
1954                         case ISDN_PID_L0_NT_U:
1955                         nt = 1;
1956                         printf("NT-mode BRI U   interface port");
1957                         break;
1958                         case ISDN_PID_L0_TE_UP2:
1959                         printf("TE-mode BRI Up2 interface line");
1960                         break;
1961                         case ISDN_PID_L0_NT_UP2:
1962                         nt = 1;
1963                         printf("NT-mode BRI Up2 interface port");
1964                         break;
1965                         case ISDN_PID_L0_TE_E1:
1966                         pri = 1;
1967                         printf("TE-mode PRI E1  interface line (for phone lines)");
1968 #if 0
1969                         if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
1970                                 printf(" HFC-E1 card");
1971 #endif
1972                         break;
1973                         case ISDN_PID_L0_NT_E1:
1974                         nt = 1;
1975                         pri = 1;
1976                         printf("NT-mode PRI E1  interface port (for phones)");
1977 #if 0
1978                         if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
1979                                 printf(" HFC-E1 card");
1980 #endif
1981                         break;
1982                         default:
1983                         useable = 0;
1984                         printf("unknown type 0x%08x",stinf->pid.protocol[0]);
1985                 }
1986                 printf("\n");
1987
1988                 if (nt)
1989                 {
1990                         if (stinf->pid.protocol[1] == 0)
1991                         {
1992                                 useable = 0;
1993                                 printf(" -> Missing layer 1 NT-mode protocol.\n");
1994                         }
1995                         p = 2;
1996                         while(p <= MAX_LAYER_NR) {
1997                                 if (stinf->pid.protocol[p])
1998                                 {
1999                                         useable = 0;
2000                                         printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
2001                                 }
2002                                 p++;
2003                         }
2004                         if (useable)
2005                         {
2006                                 if (pri)
2007                                         printf(" -> Interface is Point-To-Point (PRI).\n");
2008                                 else
2009                                         printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
2010                         }
2011                 } else
2012                 {
2013                         if (stinf->pid.protocol[1] == 0)
2014                         {
2015                                 useable = 0;
2016                                 printf(" -> Missing layer 1 protocol.\n");
2017                         }
2018                         if (stinf->pid.protocol[2] == 0)
2019                         {
2020                                 useable = 0;
2021                                 printf(" -> Missing layer 2 protocol.\n");
2022                         }
2023                         if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
2024                         {
2025                                 printf(" -> Interface is Poin-To-Point.\n");
2026                         }
2027                         if (stinf->pid.protocol[3] == 0)
2028                         {
2029                                 useable = 0;
2030                                 printf(" -> Missing layer 3 protocol.\n");
2031                         } else
2032                         {
2033                                 printf(" -> Protocol: ");
2034                                 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
2035                                 {
2036                                         case ISDN_PID_L3_DSS1USER:
2037                                         printf("DSS1 (Euro ISDN)");
2038                                         break;
2039
2040                                         default:
2041                                         useable = 0;
2042                                         printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
2043                                 }
2044                                 printf("\n");
2045                         }
2046                         p = 4;
2047                         while(p <= MAX_LAYER_NR) {
2048                                 if (stinf->pid.protocol[p])
2049                                 {
2050                                         useable = 0;
2051                                         printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
2052                                 }
2053                                 p++;
2054                         }
2055                 }
2056                 printf("  - %d B-channels\n", stinf->childcnt);
2057
2058                 if (!useable)
2059                         printf(" * Port NOT useable for PBX\n");
2060
2061                 printf("--------\n");
2062
2063                 i++;
2064         }
2065         printf("\n");
2066
2067         /* close mISDN */
2068         if ((err = mISDN_close(device)))
2069         {
2070                 fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));
2071                 exit(-1);
2072         }
2073 }
2074
2075