5bb09d10c66de4da5f1c53dd7f41878611e99d8b
[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         /* external incoming calls will not process any audio data unless
554          * the call is connected OR inbandpattern feature is enabled.
555          */
556         if (p_type==PORT_TYPE_DSS1_TE_IN
557          && p_state!=PORT_STATE_CONNECT
558          && !options.inbandpattern)
559                 return;
560
561 #if 0
562         /* the bearer capability must be audio in order to send and receive
563          * audio prior or after connect.
564          */
565         if (!(p_bearerinfo.capability&CLASS_CAPABILITY_AUDIO) && p_state!=PORT_STATE_CONNECT)
566                 return;
567 #endif
568
569         /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
570         if (p_m_rxoff)
571         {
572                 PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) ignoring data, because rx is turned off\n", p_name);
573                 return;
574         }
575
576         /* randomize and listen to crypt message if enabled */
577         if (p_m_crypt_listen)
578         {
579                 /* the noisy randomizer */
580                 p = (unsigned char *)&frm->data.p;
581                 l = frm->len;
582                 while(l--)
583                         mISDN_rand[mISDN_rand_count & 0xff] += *p++;
584
585                 cryptman_listen_bch((unsigned char *)&frm->data.p, frm->len);
586         }
587
588         /* prevent jitter */
589         gettimeofday(&tv, &tz);
590         jitter_now = tv.tv_sec;
591         jitter_now = jitter_now*8000 + tv.tv_usec/125;
592         if (p_m_jittercheck == 0)
593         {
594                 p_m_jittercheck = jitter_now;
595                 p_m_jitterdropped = 0;
596         } else
597                 p_m_jittercheck += frm->len;
598         if (p_m_jittercheck < jitter_now)
599         {
600 //              PERROR("jitter: ignoring slow data\n");
601                 p_m_jittercheck = jitter_now;
602         } else
603         if (p_m_jittercheck-ISDN_JITTERLIMIT > jitter_now)
604         {
605                 p_m_jitterdropped += frm->len;
606                 p_m_jittercheck -= frm->len;
607                 /* must exit here */
608                 return;
609         } else
610         if (p_m_jitterdropped)
611         {
612                 PERROR("jitter: dropping, caused by fast data: %lld\n", p_m_jitterdropped);
613                 p_m_jitterdropped = 0;
614         }
615
616         p = (unsigned char *)&frm->data.p;
617
618         /* send data to epoint */
619         if (ACTIVE_EPOINT(p_epointlist)) /* only if we have an epoint object */
620         {
621 //printf("we are port %s and sending to epoint %d\n", p_m_cardname, p_epoint->serial);
622                 length_temp = frm->len;
623                 data_temp = p;
624                 while(length_temp)
625                 {
626                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
627                         message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
628                         memcpy(message->param.data.data, data_temp, message->param.data.len);
629                         message->param.data.compressed = 1;
630                         message->param.data.port_id = p_serial;
631                         message->param.data.port_type = p_type;
632                         message_put(message);
633                         if (length_temp <= sizeof(message->param.data.data))
634                                 break;
635                         data_temp += sizeof(message->param.data.data);
636                         length_temp -= sizeof(message->param.data.data);
637                 }
638         }
639         /* return the same number of tone data, as we recieved */
640         newlen = read_audio(p, frm->len, 1);
641         /* send tone data to isdn device only if we have data, otherwhise we send nothing */
642         if (newlen>0 && (p_tone_fh>=0 || p_tone_fetched || !p_m_nodata || p_m_crypt_msg_loops))
643         {
644 //printf("jolly: sending.... %d %d %d %d %d\n", newlen, p_tone_fh, p_tone_fetched, p_m_nodata, p_m_crypt_msg_loops);
645                 if (p_m_txmix_on)
646                 {
647                         p_m_txmix_on -= newlen;
648                         if (p_m_txmix_on <= 0)
649                         {
650                                 p_m_txmix_on = 0;
651                                 p_m_txmix = !p_m_nodata;
652                                 PDEBUG(DEBUG_BCHANNEL, "after sending message, we set txmix to txmix=%d.\n", p_m_txmix);
653                                 if (p_m_txmix)
654                                         ph_control(p_m_b_addr, CMX_MIX_ON, 0);
655                         }
656                 }
657                 if (p_m_crypt_msg_loops)
658                 {
659                         /* send pending message */
660                         int tosend;
661
662                         tosend = p_m_crypt_msg_len - p_m_crypt_msg_current;
663                         if (tosend > newlen)
664                                 tosend = newlen;
665                         memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, tosend);
666                         p_m_crypt_msg_current += tosend;
667                         if (p_m_crypt_msg_current == p_m_crypt_msg_len)
668                         {
669                                 p_m_crypt_msg_current = 0;
670                                 p_m_crypt_msg_loops--;
671 // we need to disable rxmix some time after sending the loops...
672                                 if (!p_m_crypt_msg_loops && p_m_txmix)
673                                 {
674                                         p_m_txmix_on = 8000; /* one sec */
675                                 }
676                         }
677                 }
678                 frm->prim = frm->prim & 0xfffffffc | REQUEST; 
679                 frm->addr = p_m_b_addr | FLG_MSG_DOWN;
680                 frm->len = newlen;
681                 mISDN_write(mISDNdevice, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
682
683                 if (p_debug_nothingtosend)
684                 {
685                         p_debug_nothingtosend = 0;
686                         PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) start sending, because we have tones and/or remote audio.\n", p_name);
687                 } 
688         } else
689         {
690                 if (!p_debug_nothingtosend)
691                 {
692                         p_debug_nothingtosend = 1;
693                         PDEBUG((DEBUG_PORT | DEBUG_BCHANNEL), "PmISDN(%s) stop sending, because we have only silence.\n", p_name);
694                 } 
695         }
696 #if 0
697         /* response to the data indication */
698         rsp.prim = frm->prim & 0xfffffffc | RESPONSE; 
699         rsp.addr = frm->addr & INST_ID_MASK | FLG_MSG_DOWN;
700         rsp.dinfo = frm->dinfo;
701         rsp.len = 0;
702         mISDN_write(mISDNdevice, &rsp, mISDN_HEADER_LEN+rsp.len, TIMEOUT_1SEC);
703 //PDEBUG(DEBUG_ISDN, "written %d bytes.\n", length);
704 #endif
705 }
706
707
708 /*
709  * set echotest
710  */
711 void PmISDN::set_echotest(int echo)
712 {
713         if (p_m_echo != echo)
714         {
715                 p_m_echo = echo;
716                 PDEBUG(DEBUG_ISDN, "we set echo to echo=%d.\n", p_m_echo);
717                 if (p_m_b_channel)
718                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
719                                 ph_control(p_m_b_addr, p_m_echo?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
720         }
721 }
722
723 /*
724  * set tone
725  */
726 void PmISDN::set_tone(char *dir, char *tone)
727 {
728         int id;
729
730         if (!tone)
731                 tone = "";
732         PDEBUG(DEBUG_ISDN, "isdn port now plays tone:'%s'.\n", tone);
733         if (!tone[0])
734         {
735                 id = TONE_OFF;
736                 goto setdsp;
737         }
738
739         /* check if we NOT really have to use a dsp-tone */
740         if (!options.dsptones)
741         {
742                 nodsp:
743                 if (p_m_tone)
744                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
745                 {
746                         PDEBUG(DEBUG_ISDN, "we reset tone from id=%d to OFF.\n", p_m_tone);
747                         ph_control(p_m_b_addr, TONE_PATT_OFF, 0);
748                 }
749                 p_m_tone = 0;
750                 Port::set_tone(dir, tone);
751                 return;
752         }
753         if (p_tone_dir[0])
754                 goto nodsp;
755
756         /* now we USE dsp-tone, convert name */
757         else if (!strcmp(tone, "dialtone"))
758         {
759                 switch(options.dsptones) {
760                 case DSP_AMERICAN: id = TONE_AMERICAN_DIALTONE; break;
761                 case DSP_GERMAN: id = TONE_GERMAN_DIALTONE; break;
762                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALTONE; break;
763                 }
764         } else if (!strcmp(tone, "dialpbx"))
765         {
766                 switch(options.dsptones) {
767                 case DSP_AMERICAN: id = TONE_AMERICAN_DIALPBX; break;
768                 case DSP_GERMAN: id = TONE_GERMAN_DIALPBX; break;
769                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDDIALPBX; break;
770                 }
771         } else if (!strcmp(tone, "ringing"))
772         {
773                 switch(options.dsptones) {
774                 case DSP_AMERICAN: id = TONE_AMERICAN_RINGING; break;
775                 case DSP_GERMAN: id = TONE_GERMAN_RINGING; break;
776                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGING; break;
777                 }
778         } else if (!strcmp(tone, "ringpbx"))
779         {
780                 switch(options.dsptones) {
781                 case DSP_AMERICAN: id = TONE_AMERICAN_RINGPBX; break;
782                 case DSP_GERMAN: id = TONE_GERMAN_RINGPBX; break;
783                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDRINGPBX; break;
784                 }
785         } else if (!strcmp(tone, "busy"))
786         {
787                 busy:
788                 switch(options.dsptones) {
789                 case DSP_AMERICAN: id = TONE_AMERICAN_BUSY; break;
790                 case DSP_GERMAN: id = TONE_GERMAN_BUSY; break;
791                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
792                 }
793         } else if (!strcmp(tone, "release"))
794         {
795                 hangup:
796                 switch(options.dsptones) {
797                 case DSP_AMERICAN: id = TONE_AMERICAN_HANGUP; break;
798                 case DSP_GERMAN: id = TONE_GERMAN_HANGUP; break;
799                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDHANGUP; break;
800                 }
801         } else if (!strcmp(tone, "cause_10"))
802                 goto hangup;
803         else if (!strcmp(tone, "cause_11"))
804                 goto busy;
805         else if (!strcmp(tone, "cause_22"))
806         {
807                 switch(options.dsptones) {
808                 case DSP_AMERICAN: id = TONE_SPECIAL_INFO; break;
809                 case DSP_GERMAN: id = TONE_GERMAN_GASSENBESETZT; break;
810                 case DSP_OLDGERMAN: id = TONE_GERMAN_OLDBUSY; break;
811                 }
812         } else if (!strncmp(tone, "cause_", 6))
813                 id = TONE_SPECIAL_INFO;
814         else
815                 id = TONE_OFF;
816
817         /* if we have a tone that is not supported by dsp */
818         if (id==TONE_OFF && tone[0])
819                 goto nodsp;
820
821         setdsp:
822         if (p_m_tone != id)
823         {
824                 /* set new tone */
825                 p_m_tone = id;
826                 PDEBUG(DEBUG_ISDN, "we set tone to id=%d.\n", p_m_tone);
827                 if (p_m_b_channel)
828                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
829                                 ph_control(p_m_b_addr, p_m_tone?TONE_PATT_ON:TONE_PATT_OFF, p_m_tone);
830         }
831         /* turn user-space tones off in cases of no tone OR dsp tone */
832         Port::set_tone("",NULL);
833 }
834
835
836 /* MESSAGE_mISDNSIGNAL */
837 //extern struct message *dddebug;
838 void PmISDN::message_mISDNsignal(unsigned long epoint_id, int message_id, union parameter *param)
839 {
840         switch(param->mISDNsignal.message)
841         {
842                 case mISDNSIGNAL_VOLUME:
843                 if (p_m_txvol != param->mISDNsignal.txvol)
844                 {
845                         p_m_txvol = param->mISDNsignal.txvol;
846                         PDEBUG(DEBUG_BCHANNEL, "we change tx-volume to shift=%d.\n", p_m_txvol);
847                         if (p_m_b_channel)
848                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
849                                         ph_control(p_m_b_addr, VOL_CHANGE_TX, p_m_txvol);
850                 }
851                 if (p_m_rxvol != param->mISDNsignal.rxvol)
852                 {
853                         p_m_rxvol = param->mISDNsignal.rxvol;
854                         PDEBUG(DEBUG_BCHANNEL, "we change rx-volume to shift=%d.\n", p_m_rxvol);
855                         if (p_m_b_channel)
856                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
857                                         ph_control(p_m_b_addr, VOL_CHANGE_RX, p_m_rxvol);
858                 }
859                 break;
860
861                 case mISDNSIGNAL_CONF:
862 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
863 //tone          if (!p_m_tone && p_m_conf!=param->mISDNsignal.conf)
864                 if (p_m_conf != param->mISDNsignal.conf)
865                 {
866                         p_m_conf = param->mISDNsignal.conf;
867                         PDEBUG(DEBUG_BCHANNEL, "we change conference to conf=%d.\n", p_m_conf);
868                         if (p_m_b_channel)
869                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
870                                         ph_control(p_m_b_addr, (p_m_conf)?CMX_CONF_JOIN:CMX_CONF_SPLIT, p_m_conf);
871                 }
872                 /* we must set, even if currently tone forbids conf */
873                 p_m_conf = param->mISDNsignal.conf;
874 //if (dddebug) PDEBUG(DEBUG_ISDN, "dddebug = %d\n", dddebug->type);
875                 break;
876
877                 case mISDNSIGNAL_NODATA:
878                 p_m_nodata = param->mISDNsignal.nodata;
879                 if (p_m_txmix == p_m_nodata) /* txmix != !nodata */
880                 {
881                         p_m_txmix = !p_m_nodata;
882                         PDEBUG(DEBUG_BCHANNEL, "we change mix mode to txmix=%d (nodata=%d).\n", p_m_txmix, p_m_nodata);
883                         if (p_m_b_channel)
884                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
885                                         ph_control(p_m_b_addr, p_m_txmix?CMX_MIX_ON:CMX_MIX_OFF, 0);
886                 }
887                 break;
888
889 #if 0
890                 case mISDNSIGNAL_RXOFF:
891                 if (p_m_rxoff != param->mISDNsignal.rxoff)
892                 {
893                         p_m_rxoff = param->mISDNsignal.rxoff;
894                         PDEBUG(DEBUG_BCHANNEL, "we change receive mode to rxoff=%d.\n", p_m_rxoff);
895                         if (p_m_b_channel)
896                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
897                                         ph_control(p_m_b_addr, p_m_rxoff?CMX_RECEIVE_OFF:CMX_RECEIVE_ON, 0);
898                 }
899                 break;
900
901                 case mISDNSIGNAL_DTMF:
902                 if (p_m_dtmf != param->mISDNsignal.dtmf)
903                 {
904                         p_m_dtmf = param->mISDNsignal.dtmf;
905                         PDEBUG(DEBUG_BCHANNEL, "we change dtmf mode to dtmf=%d.\n", p_m_dtmf);
906                         if (p_m_b_channel)
907                                 if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
908                                         ph_control(p_m_b_addr, p_m_dtmf?DTMF_TONE_START:DTMF_TONE_STOP, 0);
909                 }
910                 break;
911
912 #endif
913                 default:
914                 PERROR("PmISDN(%s) unsupported signal message %d.\n", p_name, param->mISDNsignal.message);
915         }
916 }
917
918 /* MESSAGE_CRYPT */
919 void PmISDN::message_crypt(unsigned long epoint_id, int message_id, union parameter *param)
920 {
921         struct message *message;
922
923         switch(param->crypt.type)
924         {
925                 case CC_ACTBF_REQ:           /* activate blowfish */
926                 p_m_crypt = 1;
927                 p_m_crypt_key_len = param->crypt.len;
928                 if (p_m_crypt_key_len > (int)sizeof(p_m_crypt_key))
929                 {
930                         PERROR("PmISDN(%s) key too long %d > %d\n", p_name, p_m_crypt_key_len, sizeof(p_m_crypt_key));
931                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CRYPT);
932                         message->param.crypt.type = CC_ERROR_IND;
933                         message_put(message);
934                         break;
935                 }
936                 memcpy(p_m_crypt_key, param->crypt.data, p_m_crypt_key_len);
937                 crypt_off:
938                 PDEBUG(DEBUG_BCHANNEL, "we set encryption to crypt=%d. (0 means OFF)\n", p_m_crypt);
939                 if (p_m_b_channel)
940                         if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
941                                 ph_control_block(p_m_b_addr, p_m_crypt?BF_ENABLE_KEY:BF_DISABLE, p_m_crypt_key, p_m_crypt_key_len);
942                 break;
943
944                 case CC_DACT_REQ:            /* deactivate session encryption */
945                 p_m_crypt = 0;
946                 goto crypt_off;
947                 break;
948
949                 case CR_LISTEN_REQ:          /* start listening to messages */
950                 p_m_crypt_listen = 1;
951                 p_m_crypt_listen_state = 0;
952                 break;
953
954                 case CR_UNLISTEN_REQ:        /* stop listening to messages */
955                 p_m_crypt_listen = 0;
956                 break;
957
958                 case CR_MESSAGE_REQ:         /* send message */
959                 p_m_crypt_msg_len = cryptman_encode_bch(param->crypt.data, param->crypt.len, p_m_crypt_msg, sizeof(p_m_crypt_msg));
960                 if (!p_m_crypt_msg_len)
961                 {
962                         PERROR("PmISDN(%s) message too long %d > %d\n", p_name, param->crypt.len-1, sizeof(p_m_crypt_msg));
963                         break;
964                 }
965                 p_m_crypt_msg_current = 0; /* reset */
966                 p_m_crypt_msg_loops = 3; /* enable */
967                 /* disable txmix, or we get corrupt data due to audio process */
968                 if (p_m_txmix)
969                 {
970                         PDEBUG(DEBUG_BCHANNEL, "for sending CR_MESSAGE_REQ, we reset txmix from txmix=%d.\n", p_m_txmix);
971                         ph_control(p_m_b_addr, CMX_MIX_OFF, 0);
972                 }
973                 break;
974
975                 default:
976                 PERROR("PmISDN(%s) unknown crypt message %d\n", p_name, param->crypt.type);
977         }
978
979 }
980
981 /*
982  * endpoint sends messages to the port
983  */
984 int PmISDN::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
985 {
986         if (Port::message_epoint(epoint_id, message_id, param))
987                 return(1);
988
989         switch(message_id)
990         {
991                 case MESSAGE_mISDNSIGNAL: /* user command */
992                 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received special ISDN SIGNAL %d.\n", p_name, param->mISDNsignal.message);
993                 message_mISDNsignal(epoint_id, message_id, param);
994                 return(1);
995
996                 case MESSAGE_CRYPT: /* crypt control command */
997                 PDEBUG(DEBUG_ISDN, "PmISDN(%s) received encryption command '%d'.\n", p_name, param->crypt.type);
998                 message_crypt(epoint_id, message_id, param);
999                 return(1);
1000         }
1001
1002         return(0);
1003 }
1004
1005
1006 /*
1007  * main loop for processing messages from mISDN device
1008  */
1009 int mISDN_handler(void)
1010 {
1011         int ret;
1012         msg_t *msg;
1013         iframe_t *frm;
1014         struct mISDNport *mISDNport;
1015         class PmISDN *isdnport;
1016         net_stack_t *nst;
1017         msg_t *dmsg;
1018         mISDNuser_head_t *hh;
1019         int i;
1020
1021         /* the que avoids loopbacks when replying to stack after receiving
1022          * from stack. */
1023         mISDNport = mISDNport_first;
1024         while(mISDNport)
1025         {
1026                 /* process turning off rx */
1027                 i = 0;
1028                 while(i < mISDNport->b_num)
1029                 {
1030                         if (mISDNport->b_port[i] && mISDNport->b_state[i] == B_STATE_ACTIVE)
1031                         {
1032                                 isdnport=mISDNport->b_port[i];
1033                                 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)
1034                                 {
1035                                         /* rx IS required */
1036                                         if (isdnport->p_m_rxoff)
1037                                         {
1038                                                 /* turn on RX */
1039                                                 isdnport->p_m_rxoff = 0;
1040                                                 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is required, so we turn them on\n");
1041                                                 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_ON, 0);
1042                                         }
1043                                 } else
1044                                 {
1045                                         /* rx NOT required */
1046                                         if (!isdnport->p_m_rxoff)
1047                                         {
1048                                                 /* turn off RX */
1049                                                 isdnport->p_m_rxoff = 1;
1050                                                 PDEBUG(DEBUG_BCHANNEL, "%s: receive data is not required, so we turn them off\n");
1051                                                 ph_control(isdnport->p_m_b_addr, CMX_RECEIVE_OFF, 0);
1052                                         }
1053                                 }
1054                                 isdnport->p_m_jittercheck = 0; /* reset jitter detection check */
1055                         }
1056                         i++;
1057                 }
1058 #if 0
1059                 if (mISDNport->l1timeout && now>mISDNport->l1timeout)
1060                 {
1061                         PDEBUG(DEBUG_ISDN, "the L1 establish timer expired, we release all pending messages.\n", mISDNport->portnum);
1062                         mISDNport->l1timeout = 0;
1063 #endif
1064
1065                 if (mISDNport->l2establish)
1066                 {
1067                         if (now-mISDNport->l2establish > 5)
1068                         {
1069                                 if (mISDNport->ntmode)
1070                                 {
1071                                         PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link NT portnum=%d.\n", mISDNport->portnum);
1072                                         time(&mISDNport->l2establish);
1073                                         /* establish */
1074                                         dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1075                                         if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1076                                                 free_msg(dmsg);
1077                                 } else {
1078                                         PDEBUG(DEBUG_ISDN, "the L2 establish timer expired, we try to establish the link TE portnum=%d.\n", mISDNport->portnum);
1079                                         time(&mISDNport->l2establish);
1080                                         /* establish */
1081                                         iframe_t act;
1082                                         act.prim = DL_ESTABLISH | REQUEST; 
1083                                         act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
1084                                         act.dinfo = 0;
1085                                         act.len = 0;
1086                                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1087                                 }
1088                         }
1089                 }
1090                 if ((dmsg = msg_dequeue(&mISDNport->downqueue)))
1091                 {
1092                         if (mISDNport->ntmode)
1093                         {
1094                                 hh = (mISDNuser_head_t *)dmsg->data;
1095                                 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);
1096                                 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1097                                         free_msg(dmsg);
1098                         } else
1099                         {
1100                                 frm = (iframe_t *)dmsg->data;
1101                                 frm->addr = mISDNport->upper_id | FLG_MSG_DOWN;
1102                                 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
1103                                 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);
1104                                 mISDN_write(mISDNdevice, dmsg->data, dmsg->len, TIMEOUT_1SEC);
1105                                 free_msg(dmsg);
1106                         }
1107                         return(1);
1108                 }
1109                 mISDNport = mISDNport->next;
1110         } 
1111
1112         /* get message from kernel */
1113         if (!(msg = alloc_msg(MAX_MSG_SIZE)))
1114                 return(1);
1115         ret = mISDN_read(mISDNdevice, msg->data, MAX_MSG_SIZE, 0);
1116         if (ret < 0)
1117         {
1118                 free_msg(msg);
1119                 if (errno == EAGAIN)
1120                         return(0);
1121                 PERROR("FATAL ERROR: failed to do mISDN_read()\n");
1122                 exit(-1); 
1123         }
1124         if (!ret)
1125         {
1126                 free_msg(msg);
1127 //              printf("%s: ERROR: mISDN_read() returns nothing\n");
1128                 return(0);
1129         }
1130         msg->len = ret;
1131         frm = (iframe_t *)msg->data;
1132
1133         /* global prim */
1134         switch(frm->prim)
1135         {
1136                 case MGR_INITTIMER | CONFIRM:
1137                 case MGR_ADDTIMER | CONFIRM:
1138                 case MGR_DELTIMER | CONFIRM:
1139                 case MGR_REMOVETIMER | CONFIRM:
1140 //              if (options.deb & DEBUG_ISDN)
1141 //                      PDEBUG(DEBUG_ISDN, "timer-confirm\n");
1142                 free_msg(msg);
1143                 return(1);
1144         }
1145
1146         /* find the port */
1147         mISDNport = mISDNport_first;
1148         while(mISDNport)
1149         {
1150                 if ((frm->prim==(MGR_TIMER | INDICATION)) && mISDNport->ntmode)
1151                 {
1152                         itimer_t *it = mISDNport->nst.tlist;
1153
1154                         /* find timer */
1155                         while(it)
1156                         {
1157                                 if (it->id == (int)frm->addr)
1158                                         break;
1159                                 it = it->next;
1160                         }
1161                         if (it)
1162                         {
1163                                 mISDN_write_frame(mISDNdevice, msg->data, mISDNport->upper_id | FLG_MSG_DOWN,
1164                                         MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1165
1166                                 PDEBUG(DEBUG_ISDN, "timer-indication %s port %d it=%p\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, it);
1167                                 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1168                                 ret = it->function(it->data);
1169                                 break;
1170                         }
1171                         /* we will continue here because we have a timer for a different mISDNport */
1172                 }
1173 //printf("comparing frm->addr %x with upper_id %x\n", frm->addr, mISDNport->upper_id);
1174                 if ((frm->addr&STACK_ID_MASK) == (unsigned int)(mISDNport->upper_id&STACK_ID_MASK))
1175                 {
1176                         /* d-message */
1177                         switch(frm->prim)
1178                         {
1179                                 case MGR_SHORTSTATUS | INDICATION:
1180                                 case MGR_SHORTSTATUS | CONFIRM:
1181                                 switch(frm->dinfo) {
1182                                         case SSTATUS_L1_ACTIVATED:
1183                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1184                                         goto ss_act;
1185                                         case SSTATUS_L1_DEACTIVATED:
1186                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L1_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1187                                         goto ss_deact;
1188                                         case SSTATUS_L2_ESTABLISHED:
1189                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_ESTABLISHED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1190                                         goto ss_estab;
1191                                         case SSTATUS_L2_RELEASED:
1192                                         PDEBUG(DEBUG_ISDN, "Received SSTATUS_L2_RELEASED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1193                                         goto ss_rel;
1194                                 }
1195                                 break;
1196
1197                                 case PH_ACTIVATE | CONFIRM:
1198                                 case PH_ACTIVATE | INDICATION:
1199                                 PDEBUG(DEBUG_ISDN, "Received PH_ACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1200                                 if (mISDNport->ntmode)
1201                                 {
1202                                         mISDNport->l1link = 1;
1203                                         setup_queue(mISDNport, 1);
1204                                         goto l1_msg;
1205                                 }
1206                                 ss_act:
1207                                 mISDNport->l1link = 1;
1208                                 setup_queue(mISDNport, 1);
1209                                 break;
1210
1211                                 case PH_DEACTIVATE | CONFIRM:
1212                                 case PH_DEACTIVATE | INDICATION:
1213                                 PDEBUG(DEBUG_ISDN, "Received PH_DEACTIVATED for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1214                                 if (mISDNport->ntmode)
1215                                 {
1216                                         mISDNport->l1link = 0;
1217                                         setup_queue(mISDNport, 0);
1218                                         goto l1_msg;
1219                                 }
1220                                 ss_deact:
1221                                 mISDNport->l1link = 0;
1222                                 setup_queue(mISDNport, 0);
1223                                 break;
1224
1225                                 case PH_CONTROL | CONFIRM:
1226                                 case PH_CONTROL | INDICATION:
1227                                 PDEBUG(DEBUG_ISDN, "Received PH_CONTROL for port %d (%s).\n", mISDNport->portnum, mISDNport->ifport->interface->name);
1228                                 break;
1229
1230                                 case DL_ESTABLISH | INDICATION:
1231                                 case DL_ESTABLISH | CONFIRM:
1232 //                              PDEBUG(DEBUG_ISDN, "addr 0x%x established data link (DL) TE portnum=%d (DL_ESTABLISH)\n", frm->addr, mISDNport->portnum);
1233                                 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1234                                 ss_estab:
1235 //                              if (mISDNport->ntmode)
1236 //                                      break;
1237                                 if (mISDNport->l2establish)
1238                                 {
1239                                         mISDNport->l2establish = 0;
1240                                         PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
1241                                 }
1242                                 mISDNport->l2link = 1;
1243                                 break;
1244
1245                                 case DL_RELEASE | INDICATION:
1246                                 case DL_RELEASE | CONFIRM:
1247 //                              PDEBUG(DEBUG_ISDN, "addr 0x%x released data link (DL) TE portnum=%d (DL_RELEASE)\n", frm->addr, mISDNport->portnum);
1248                                 if (!mISDNport->ntmode) break; /* !!!!!!!!!!!!!!!! */
1249                                 ss_rel:
1250 //                              if (mISDNport->ntmode)
1251 //                                      break;
1252                                 mISDNport->l2link = 0;
1253                                 if (mISDNport->ptp)
1254                                 {
1255                                         time(&mISDNport->l2establish);
1256                                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
1257                                 }
1258                                 break;
1259
1260                                 default:
1261                                 l1_msg:
1262                                 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);
1263                                 if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM))
1264                                 {
1265                                         PERROR("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
1266                                 }
1267                                 /* d-message */
1268                                 if (mISDNport->ntmode)
1269                                 {
1270                                         /* l1-data enters the nt-mode library */
1271                                         nst = &mISDNport->nst;
1272                                         if (nst->l1_l2(nst, msg))
1273                                                 free_msg(msg);
1274                                         return(1);
1275                                 } else
1276                                 {
1277                                         /* l3-data is sent to pbx */
1278                                         if (stack2manager_te(mISDNport, msg))
1279                                                 free_msg(msg);
1280                                         return(1);
1281                                 }
1282                         }
1283                         break;
1284                 }
1285 //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));
1286                 /* check if child, and if parent stack match */
1287                 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)))
1288                 {
1289                         /* b-message */
1290                         switch(frm->prim)
1291                         {
1292                                 /* we don't care about confirms, we use rx data to sync tx */
1293                                 case PH_DATA | CONFIRM:
1294                                 case DL_DATA | CONFIRM:
1295                                 break;
1296
1297                                 /* we receive audio data, we respond to it AND we send tones */
1298                                 case PH_DATA | INDICATION:
1299                                 case DL_DATA | INDICATION:
1300                                 case PH_CONTROL | INDICATION:
1301                                 i = 0;
1302                                 while(i < mISDNport->b_num)
1303                                 {
1304                                         if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1305                                                 break;
1306                                         i++;
1307                                 }
1308                                 if (i == mISDNport->b_num)
1309                                 {
1310                                         PERROR("unhandled b-message (address 0x%x).\n", frm->addr);
1311                                         break;
1312                                 }
1313                                 if (mISDNport->b_port[i])
1314                                 {
1315 //PERROR("port sech: %s data\n", mISDNport->b_port[i]->p_name);
1316                                         mISDNport->b_port[i]->bchannel_receive(frm);
1317                                 } else
1318                                         PDEBUG(DEBUG_BCHANNEL, "b-channel is not associated to an ISDNPort (address 0x%x), ignoring.\n", frm->addr);
1319                                 break;
1320
1321                                 case PH_ACTIVATE | INDICATION:
1322                                 case DL_ESTABLISH | INDICATION:
1323                                 case PH_ACTIVATE | CONFIRM:
1324                                 case DL_ESTABLISH | CONFIRM:
1325                                 PDEBUG(DEBUG_BCHANNEL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
1326                                 i = 0;
1327                                 while(i < mISDNport->b_num)
1328                                 {
1329                                         if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1330                                                 break;
1331                                         i++;
1332                                 }
1333                                 if (i == mISDNport->b_num)
1334                                 {
1335                                         PERROR("unhandled b-establish (address 0x%x).\n", frm->addr);
1336                                         break;
1337                                 }
1338                                 mISDNport->b_state[i] = B_STATE_ACTIVE;
1339                                 if (!mISDNport->b_port[i])
1340                                         bchannel_deactivate(mISDNport, i);
1341                                 else
1342                                         bchannel_activate(mISDNport, i);
1343                                 break;
1344
1345                                 case PH_DEACTIVATE | INDICATION:
1346                                 case DL_RELEASE | INDICATION:
1347                                 case PH_DEACTIVATE | CONFIRM:
1348                                 case DL_RELEASE | CONFIRM:
1349                                 PDEBUG(DEBUG_BCHANNEL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
1350                                 i = 0;
1351                                 while(i < mISDNport->b_num)
1352                                 {
1353                                         if ((unsigned int)(mISDNport->b_addr[i]&STACK_ID_MASK) == (frm->addr&STACK_ID_MASK))
1354                                                 break;
1355                                         i++;
1356                                 }
1357                                 if (i == mISDNport->b_num)
1358                                 {
1359                                         PERROR("unhandled b-release (address 0x%x).\n", frm->addr);
1360                                         break;
1361                                 }
1362                                 mISDNport->b_state[i] = B_STATE_IDLE;
1363                                 if (mISDNport->b_port[i])
1364                                         bchannel_activate(mISDNport, i);
1365                                 else
1366                                         bchannel_deactivate(mISDNport, i);
1367                                 break;
1368                         }
1369                         break;
1370                 }
1371
1372                 mISDNport = mISDNport->next;
1373         } 
1374         if (!mISDNport)
1375         {
1376                 if (frm->prim == (MGR_TIMER | INDICATION))
1377                         PERROR("unhandled timer indication message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1378                 else
1379                         PERROR("unhandled message: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, msg->len);
1380 //              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);
1381         }
1382
1383         free_msg(msg);
1384         return(1);
1385 }
1386
1387
1388 /*
1389  * global function to add a new card (port)
1390  */
1391 struct mISDNport *mISDN_port_open(int port, int ptp)
1392 {
1393         int ret;
1394         unsigned char buff[1025];
1395         iframe_t *frm = (iframe_t *)buff;
1396         stack_info_t *stinf;
1397         struct mISDNport *mISDNport, **mISDNportp;
1398         int i, cnt;
1399         layer_info_t li;
1400 //      interface_info_t ii;
1401         net_stack_t *nst;
1402         manager_t *mgr;
1403         mISDN_pid_t pid;
1404         int pri = 0;
1405         int nt = 0;
1406         iframe_t dact;
1407
1408         /* open mISDNdevice if not already open */
1409         if (mISDNdevice < 0)
1410         {
1411                 ret = mISDN_open();
1412                 if (ret < 0)
1413                 {
1414                         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));
1415                         return(NULL);
1416                 }
1417                 mISDNdevice = ret;
1418                 PDEBUG(DEBUG_ISDN, "mISDN device opened.\n");
1419
1420                 /* create entity for layer 3 TE-mode */
1421                 mISDN_write_frame(mISDNdevice, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1422                 ret = mISDN_read_frame(mISDNdevice, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
1423                 if (ret < (int)mISDN_HEADER_LEN)
1424                 {
1425                         noentity:
1426                         fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
1427                         exit(-1);
1428                 }
1429                 entity = frm->dinfo & 0xffff;
1430                 if (!entity)
1431                         goto noentity;
1432                 PDEBUG(DEBUG_ISDN, "our entity for l3-processes is %d.\n", entity);
1433         }
1434
1435         /* query port's requirements */
1436         cnt = mISDN_get_stack_count(mISDNdevice);
1437         if (cnt <= 0)
1438         {
1439                 PERROR("Found no card. Please be sure to load card drivers.\n");
1440                 return(NULL);
1441         }
1442         if (port>cnt || port<1)
1443         {
1444                 PERROR("Port (%d) given at 'ports' (options.conf) is out of existing port range (%d-%d)\n", port, 1, cnt);
1445                 return(NULL);
1446         }
1447         ret = mISDN_get_stack_info(mISDNdevice, port, buff, sizeof(buff));
1448         if (ret < 0)
1449         {
1450                 PERROR("Cannot get stack info for port %d (ret=%d)\n", port, ret);
1451                 return(NULL);
1452         }
1453         stinf = (stack_info_t *)&frm->data.p;
1454         switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1455         {
1456                 case ISDN_PID_L0_TE_S0:
1457                 PDEBUG(DEBUG_ISDN, "TE-mode BRI S/T interface line\n");
1458                 break;
1459                 case ISDN_PID_L0_NT_S0:
1460                 PDEBUG(DEBUG_ISDN, "NT-mode BRI S/T interface port\n");
1461                 nt = 1;
1462                 break;
1463                 case ISDN_PID_L0_TE_U:
1464                 PDEBUG(DEBUG_ISDN, "TE-mode BRI U   interface line\n");
1465                 break;
1466                 case ISDN_PID_L0_NT_U:
1467                 PDEBUG(DEBUG_ISDN, "NT-mode BRI U   interface port\n");
1468                 nt = 1;
1469                 break;
1470                 case ISDN_PID_L0_TE_UP2:
1471                 PDEBUG(DEBUG_ISDN, "TE-mode BRI Up2 interface line\n");
1472                 break;
1473                 case ISDN_PID_L0_NT_UP2:
1474                 PDEBUG(DEBUG_ISDN, "NT-mode BRI Up2 interface port\n");
1475                 nt = 1;
1476                 break;
1477                 case ISDN_PID_L0_TE_E1:
1478                 PDEBUG(DEBUG_ISDN, "TE-mode PRI E1  interface line\n");
1479                 pri = 1;
1480                 break;
1481                 case ISDN_PID_L0_NT_E1:
1482                 PDEBUG(DEBUG_ISDN, "LT-mode PRI E1  interface port\n");
1483                 pri = 1;
1484                 nt = 1;
1485                 break;
1486                 default:
1487                 PERROR("unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]);
1488                 return(NULL);
1489         }
1490         if (nt)
1491         {
1492                 /* NT */
1493                 if (stinf->pid.protocol[1] == 0)
1494                 {
1495                         PERROR("Given port %d: Missing layer 1 NT-mode protocol.\n", port);
1496                         return(NULL);
1497                 }
1498                 if (stinf->pid.protocol[2])
1499                 {
1500                         PERROR("Given port %d: Layer 2 protocol 0x%08x is detected, but not allowed for NT lib.\n", port, stinf->pid.protocol[2]);
1501                         return(NULL);
1502                 }
1503         } else
1504         {
1505                 /* TE */
1506                 if (stinf->pid.protocol[1] == 0)
1507                 {
1508                         PERROR("Given port %d: Missing layer 1 protocol.\n", port);
1509                         return(NULL);
1510                 }
1511                 if (stinf->pid.protocol[2] == 0)
1512                 {
1513                         PERROR("Given port %d: Missing layer 2 protocol.\n", port);
1514                         return(NULL);
1515                 }
1516                 if (stinf->pid.protocol[3] == 0)
1517                 {
1518                         PERROR("Given port %d: Missing layer 3 protocol.\n", port);
1519                         return(NULL);
1520                 } else
1521                 {
1522                         switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
1523                         {
1524                                 case ISDN_PID_L3_DSS1USER:
1525                                 break;
1526
1527                                 default:
1528                                 PERROR("Given port %d: own protocol 0x%08x", port,stinf->pid.protocol[3]);
1529                                 return(NULL);
1530                         }
1531                 }
1532                 if (stinf->pid.protocol[4])
1533                 {
1534                         PERROR("Given port %d: Layer 4 protocol not allowed.\n", port);
1535                         return(NULL);
1536                 }
1537         }
1538
1539         /* add mISDNport structure */
1540         mISDNportp = &mISDNport_first;
1541         while(*mISDNportp)
1542                 mISDNportp = &mISDNport->next;
1543         mISDNport = (struct mISDNport *)calloc(1, sizeof(struct mISDNport));
1544         if (!mISDNport)
1545         {
1546                 PERROR("Cannot alloc mISDNport structure\n");
1547                 return(NULL);
1548         }
1549         pmemuse++;
1550         memset(mISDNport, 0, sizeof(mISDNport));
1551         *mISDNportp = mISDNport;
1552
1553         /* allocate ressources of port */
1554         msg_queue_init(&mISDNport->downqueue);
1555 //      SCPY(mISDNport->name, "noname");
1556         mISDNport->portnum = port;
1557         mISDNport->ntmode = nt;
1558         mISDNport->pri = pri;
1559         mISDNport->d_stid = stinf->id;
1560         PDEBUG(DEBUG_ISDN, "d_stid = 0x%x.\n", mISDNport->d_stid);
1561         mISDNport->b_num = stinf->childcnt;
1562         PDEBUG(DEBUG_ISDN, "Port has %d b-channels.\n", mISDNport->b_num);
1563         if ((stinf->pid.protocol[2]&ISDN_PID_L2_DF_PTP) || (nt&&ptp) || pri)
1564         {
1565                 PDEBUG(DEBUG_ISDN, "Port is point-to-point.\n");
1566                 mISDNport->ptp = ptp = 1;
1567         }
1568         i = 0;
1569         while(i < stinf->childcnt)
1570         {
1571                 mISDNport->b_stid[i] = stinf->child[i];
1572                 PDEBUG(DEBUG_ISDN, "b_stid[%d] = 0x%x.\n", i, mISDNport->b_stid[i]);
1573                 i++;
1574         }
1575         memset(&li, 0, sizeof(li));
1576         UCPY(&li.name[0], (nt)?"net l2":"pbx l4");
1577         li.object_id = -1;
1578         li.extentions = 0;
1579         li.pid.protocol[nt?2:4] = (nt)?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1580         li.pid.layermask = ISDN_LAYER((nt?2:4));
1581         li.st = mISDNport->d_stid;
1582         ret = mISDN_new_layer(mISDNdevice, &li);
1583         if (ret)
1584         {
1585                 PERROR("Cannot add layer %d of port %d (ret %d)\n", nt?2:4, port, ret);
1586                 closeport:
1587                 mISDNport_close(mISDNport);
1588                 return(NULL);
1589         }
1590         mISDNport->upper_id = li.id;
1591         ret = mISDN_register_layer(mISDNdevice, mISDNport->d_stid, mISDNport->upper_id);
1592         if (ret)
1593         {
1594                 PERROR("Cannot register layer %d of port %d\n", nt?2:4, port);
1595                 goto closeport;
1596         }
1597         mISDNport->lower_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?1:3); // id of lower layer (nt=1, te=3)
1598         if (mISDNport->lower_id < 0)
1599         {
1600                 PERROR("Cannot get layer(%d) id of port %d\n", nt?1:3, port);
1601                 goto closeport;
1602         }
1603         mISDNport->upper_id = mISDN_get_layerid(mISDNdevice, mISDNport->d_stid, nt?2:4); // id of uppermost layer (nt=2, te=4)
1604         if (mISDNport->upper_id < 0)
1605         {
1606                 PERROR("Cannot get layer(%d) id of port %d\n", nt?2:4, port);
1607                 goto closeport;
1608         }
1609         PDEBUG(DEBUG_ISDN, "Layer %d of port %d added.\n", nt?2:4, port);
1610
1611         /* if ntmode, establish L1 to send the tei removal during start */
1612         if (mISDNport->ntmode)
1613         {
1614                 iframe_t act;
1615                 /* L1 */
1616                 act.prim = PH_ACTIVATE | REQUEST; 
1617                 act.addr = mISDNport->upper_id | FLG_MSG_DOWN;
1618                 printf("UPPER ID 0x%x, addr 0x%x\n",mISDNport->upper_id, act.addr);
1619                 act.dinfo = 0;
1620                 act.len = 0;
1621                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1622                 usleep(10000); /* to be sure, that l1 is up */
1623         }
1624
1625         /* create nst (nt-mode only) */
1626         if (nt)
1627         {
1628                 mgr = &mISDNport->mgr;
1629                 nst = &mISDNport->nst;
1630
1631                 mgr->nst = nst;
1632                 nst->manager = mgr;
1633
1634                 nst->l3_manager = stack2manager_nt; /* messages from nt-mode */
1635                 nst->device = mISDNdevice;
1636                 nst->cardnr = port;
1637                 nst->d_stid = mISDNport->d_stid;
1638
1639                 nst->feature = FEATURE_NET_HOLD;
1640                 if (ptp)
1641                         nst->feature |= FEATURE_NET_PTP;
1642                 if (pri)
1643                         nst->feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1644 #if 0
1645                 i = 0;
1646                 while(i < mISDNport->b_num)
1647                 {
1648                         nst->b_stid[i] = mISDNport->b_stid[i];
1649                         i++;
1650                 }
1651 #endif
1652                 nst->l1_id = mISDNport->lower_id;
1653                 nst->l2_id = mISDNport->upper_id;
1654
1655                 /* phd */       
1656                 msg_queue_init(&nst->down_queue);
1657
1658                 Isdnl2Init(nst);
1659                 Isdnl3Init(nst);
1660         }
1661
1662         /* if te-mode, query state link */
1663         if (!mISDNport->ntmode)
1664         {
1665                 iframe_t act;
1666                 /* L2 */
1667                 PDEBUG(DEBUG_ISDN, "sending short status request for port %d.\n", port);
1668                 act.prim = MGR_SHORTSTATUS | REQUEST; 
1669                 act.addr = mISDNport->upper_id | MSG_BROADCAST;
1670                 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
1671                 act.len = 0;
1672                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1673         }
1674         /* if ptp AND te-mode, pull up the link */
1675         if (mISDNport->ptp && !mISDNport->ntmode)
1676         {
1677                 iframe_t act;
1678                 /* L2 */
1679                 act.prim = DL_ESTABLISH | REQUEST; 
1680                 act.addr = (mISDNport->upper_id & ~LAYER_ID_MASK) | 4 | FLG_MSG_DOWN;
1681                 act.dinfo = 0;
1682                 act.len = 0;
1683                 mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
1684         }
1685         /* if ptp AND nt-mode, pull up the link */
1686         if (mISDNport->ptp && mISDNport->ntmode)
1687         {
1688                 msg_t *dmsg;
1689                 /* L2 */
1690                 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
1691                 if (mISDNport->nst.manager_l3(&mISDNport->nst, dmsg))
1692                         free_msg(dmsg);
1693         }
1694         /* initially, we assume that the link is down, exept for nt-ptmp */
1695         mISDNport->l2link = (mISDNport->ntmode && !mISDNport->ptp)?1:0;
1696
1697         PDEBUG(DEBUG_BCHANNEL, "using 'mISDN_dsp.o' module\n");
1698
1699         /* add all bchannel layers */
1700         i = 0;
1701         while(i < mISDNport->b_num)
1702         {
1703                 /* create new layer */
1704                 PDEBUG(DEBUG_BCHANNEL, "creating bchannel %d (index %d).\n" , i+1+(i>=15), i);
1705                 memset(&li, 0, sizeof(li));
1706                 memset(&pid, 0, sizeof(pid));
1707                 li.object_id = -1;
1708                 li.extentions = 0;
1709                 li.st = mISDNport->b_stid[i];
1710                 UCPY(li.name, "B L4");
1711                 li.pid.layermask = ISDN_LAYER((4));
1712                 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1713                 ret = mISDN_new_layer(mISDNdevice, &li);
1714                 if (ret)
1715                 {
1716                         failed_new_layer:
1717                         PERROR("mISDN_new_layer() failed to add bchannel %d (index %d)\n", i+1+(i>=15), i);
1718                         goto closeport;
1719                 }
1720                 mISDNport->b_addr[i] = li.id;
1721                 if (!li.id)
1722                 {
1723                         goto failed_new_layer;
1724                 }
1725                 PDEBUG(DEBUG_BCHANNEL, "new layer (b_addr=0x%x)\n", mISDNport->b_addr[i]);
1726
1727                 /* create new stack */
1728                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1729                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1730                 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1731                 pid.protocol[4] = ISDN_PID_L4_B_USER;
1732                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1733                 ret = mISDN_set_stack(mISDNdevice, mISDNport->b_stid[i], &pid);
1734                 if (ret)
1735                 {
1736                         stack_error:
1737                         PERROR("mISDN_set_stack() failed (ret=%d) to add bchannel (index %d) stid=0x%x\n", ret, i, mISDNport->b_stid[i]);
1738                         mISDN_write_frame(mISDNdevice, buff, mISDNport->b_addr[i], MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1739                         mISDNport->b_addr[i] = 0;
1740 //                      mISDNport->b_addr_low[i] = 0;
1741                         goto closeport;
1742                 }
1743                 ret = mISDN_get_setstack_ind(mISDNdevice, mISDNport->b_addr[i]);
1744                 if (ret)
1745                         goto stack_error;
1746
1747                 /* get layer id */
1748                 mISDNport->b_addr[i] = mISDN_get_layerid(mISDNdevice, mISDNport->b_stid[i], 4);
1749                 if (!mISDNport->b_addr[i])
1750                         goto stack_error;
1751                 /* deactivate bchannel if already enabled due to crash */
1752                 PDEBUG(DEBUG_BCHANNEL, "deactivating bchannel (index %d) as a precaution.\n", i);
1753                 dact.prim = DL_RELEASE | REQUEST; 
1754                 dact.addr = mISDNport->b_addr[i] | FLG_MSG_DOWN;
1755                 dact.dinfo = 0;
1756                 dact.len = 0;
1757                 mISDN_write(mISDNdevice, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
1758
1759                 i++;
1760         }
1761         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);
1762         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);
1763         return(mISDNport);
1764 }
1765
1766
1767 /*
1768  * function to free ALL cards (ports)
1769  */
1770 void mISDNport_close_all(void)
1771 {
1772         /* free all ports */
1773         while(mISDNport_first)
1774                 mISDNport_close(mISDNport_first);
1775 }
1776
1777 /*
1778  * free only one port
1779  */
1780 void mISDNport_close(struct mISDNport *mISDNport)
1781 {
1782         struct mISDNport **mISDNportp;
1783         class Port *port;
1784         classs PmISDN *pmISDN;
1785         net_stack_t *nst;
1786         unsigned char buf[32];
1787         int i;
1788
1789         /* remove all port instance that are linked to this mISDNport */
1790         port = port_first;
1791         while(port)
1792         {
1793                 if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
1794                 {
1795                         pmISDN = (class PmISDN)*port;
1796                         if (pmISDN->p_m_mISDNport)
1797                         {
1798                                 PDEBUG(DEBUG_ISDN, "port %s uses mISDNport %d, destroying it.\n", pnISDN->p_name, mISDNport->portnum);
1799                                 delete pmISDN;
1800                         }
1801                 }
1802                 port = port->next;
1803         }
1804
1805         printlog("closing port %d\n", mISDNport->portnum);
1806
1807         /* free bchannels */
1808         i = 0;
1809         while(i < mISDNport->b_num)
1810         {
1811                 bchannel_deactivate(mISDNport, i);
1812                 PDEBUG(DEBUG_BCHANNEL, "freeing %s port %d bchannel (index %d).\n", (mISDNport->ntmode)?"NT":"TE", mISDNport->portnum, i);
1813                 if (mISDNport->b_stid[i])
1814                 {
1815                         mISDN_clear_stack(mISDNdevice, mISDNport->b_stid[i]);
1816                         if (mISDNport->b_addr[i])
1817                                 mISDN_write_frame(mISDNdevice, buf, mISDNport->b_addr[i] | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1818                 }
1819                 i++;
1820         }
1821
1822         /* free ressources of port */
1823         msg_queue_purge(&mISDNport->downqueue);
1824
1825         /* free stacks */
1826         if (mISDNport->ntmode)
1827         {
1828                 nst = &mISDNport->nst;
1829                 if (nst->manager) /* to see if initialized */
1830                 {
1831                         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");
1832                         cleanup_Isdnl3(nst);
1833                         cleanup_Isdnl2(nst);
1834
1835                         /* phd */
1836                         msg_queue_purge(&nst->down_queue);
1837                         if (nst->phd_down_msg)
1838                                 free(nst->phd_down_msg);
1839                 }
1840         }
1841
1842         PDEBUG(DEBUG_BCHANNEL, "freeing d-stack.\n");
1843         if (mISDNport->d_stid)
1844         {
1845 //              mISDN_clear_stack(mISDNdevice, mISDNport->d_stid);
1846                 if (mISDNport->lower_id)
1847                         mISDN_write_frame(mISDNdevice, buf, mISDNport->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1848         }
1849
1850         /* remove from list */
1851         mISDNportp = &mISDNport_first;
1852         while(*mISDNportp)
1853         {
1854                 if (*mISDNportp == mISDNport)
1855                 {
1856                         *mISDNportp = (*mISDNportp)->next;
1857                         break;
1858                 }
1859                 mISDNportp = &((*mISDNportp)->next);
1860         }
1861
1862         if (!(*mISDNportp))
1863         {
1864                 PERROR("software error, mISDNport not in list\n");
1865                 exit(-1);
1866         }
1867         
1868         memset(mISDNport, 0, sizeof(struct mISDNport));
1869         free(mISDNport);
1870         pmemuse--;
1871
1872         /* close mISDNdevice, if no port */
1873         if (mISDNdevice>=0 && mISDNport_first==NULL)
1874         {
1875                 /* free entity */
1876                 mISDN_write_frame(mISDNdevice, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
1877                 /* close device */
1878                 mISDN_close(mISDNdevice);
1879                 mISDNdevice = -1;
1880                 PDEBUG(DEBUG_ISDN, "mISDN device closed.\n");
1881         }
1882 }
1883
1884
1885 /*
1886  * global function to show all available isdn ports
1887  */
1888 void mISDN_port_info(void)
1889 {
1890         int err;
1891         int i, ii, p;
1892         int useable, nt, pri;
1893         unsigned char buff[1025];
1894         iframe_t *frm = (iframe_t *)buff;
1895         stack_info_t *stinf;
1896         int device;
1897
1898         /* open mISDN */
1899         if ((device = mISDN_open()) < 0)
1900         {
1901                 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));
1902                 exit(-1);
1903         }
1904
1905         /* get number of stacks */
1906         i = 1;
1907         ii = mISDN_get_stack_count(device);
1908         printf("\n");
1909         if (ii <= 0)
1910         {
1911                 printf("Found no card. Please be sure to load card drivers.\n");
1912         }
1913
1914         /* loop the number of cards and get their info */
1915         while(i <= ii)
1916         {
1917                 err = mISDN_get_stack_info(device, i, buff, sizeof(buff));
1918                 if (err <= 0)
1919                 {
1920                         fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err);
1921                         break;
1922                 }
1923                 stinf = (stack_info_t *)&frm->data.p;
1924
1925                 nt = pri = 0;
1926                 useable = 1;
1927
1928                 /* output the port info */
1929                 printf("Port %2d: ", i);
1930                 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK)
1931                 {
1932                         case ISDN_PID_L0_TE_S0:
1933                         printf("TE-mode BRI S/T interface line (for phone lines)");
1934 #if 0
1935                         if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK)
1936                                 printf(" HFC multiport card");
1937 #endif
1938                         break;
1939                         case ISDN_PID_L0_NT_S0:
1940                         nt = 1;
1941                         printf("NT-mode BRI S/T interface port (for phones)");
1942 #if 0
1943                         if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK)
1944                                 printf(" HFC multiport card");
1945 #endif
1946                         break;
1947                         case ISDN_PID_L0_TE_U:
1948                         printf("TE-mode BRI U   interface line");
1949                         break;
1950                         case ISDN_PID_L0_NT_U:
1951                         nt = 1;
1952                         printf("NT-mode BRI U   interface port");
1953                         break;
1954                         case ISDN_PID_L0_TE_UP2:
1955                         printf("TE-mode BRI Up2 interface line");
1956                         break;
1957                         case ISDN_PID_L0_NT_UP2:
1958                         nt = 1;
1959                         printf("NT-mode BRI Up2 interface port");
1960                         break;
1961                         case ISDN_PID_L0_TE_E1:
1962                         pri = 1;
1963                         printf("TE-mode PRI E1  interface line (for phone lines)");
1964 #if 0
1965                         if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK)
1966                                 printf(" HFC-E1 card");
1967 #endif
1968                         break;
1969                         case ISDN_PID_L0_NT_E1:
1970                         nt = 1;
1971                         pri = 1;
1972                         printf("NT-mode PRI E1  interface port (for phones)");
1973 #if 0
1974                         if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK)
1975                                 printf(" HFC-E1 card");
1976 #endif
1977                         break;
1978                         default:
1979                         useable = 0;
1980                         printf("unknown type 0x%08x",stinf->pid.protocol[0]);
1981                 }
1982                 printf("\n");
1983
1984                 if (nt)
1985                 {
1986                         if (stinf->pid.protocol[1] == 0)
1987                         {
1988                                 useable = 0;
1989                                 printf(" -> Missing layer 1 NT-mode protocol.\n");
1990                         }
1991                         p = 2;
1992                         while(p <= MAX_LAYER_NR) {
1993                                 if (stinf->pid.protocol[p])
1994                                 {
1995                                         useable = 0;
1996                                         printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]);
1997                                 }
1998                                 p++;
1999                         }
2000                         if (useable)
2001                         {
2002                                 if (pri)
2003                                         printf(" -> Interface is Point-To-Point (PRI).\n");
2004                                 else
2005                                         printf(" -> Interface can be Poin-To-Point/Multipoint.\n");
2006                         }
2007                 } else
2008                 {
2009                         if (stinf->pid.protocol[1] == 0)
2010                         {
2011                                 useable = 0;
2012                                 printf(" -> Missing layer 1 protocol.\n");
2013                         }
2014                         if (stinf->pid.protocol[2] == 0)
2015                         {
2016                                 useable = 0;
2017                                 printf(" -> Missing layer 2 protocol.\n");
2018                         }
2019                         if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP)
2020                         {
2021                                 printf(" -> Interface is Poin-To-Point.\n");
2022                         }
2023                         if (stinf->pid.protocol[3] == 0)
2024                         {
2025                                 useable = 0;
2026                                 printf(" -> Missing layer 3 protocol.\n");
2027                         } else
2028                         {
2029                                 printf(" -> Protocol: ");
2030                                 switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK)
2031                                 {
2032                                         case ISDN_PID_L3_DSS1USER:
2033                                         printf("DSS1 (Euro ISDN)");
2034                                         break;
2035
2036                                         default:
2037                                         useable = 0;
2038                                         printf("unknown protocol 0x%08x",stinf->pid.protocol[3]);
2039                                 }
2040                                 printf("\n");
2041                         }
2042                         p = 4;
2043                         while(p <= MAX_LAYER_NR) {
2044                                 if (stinf->pid.protocol[p])
2045                                 {
2046                                         useable = 0;
2047                                         printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]);
2048                                 }
2049                                 p++;
2050                         }
2051                 }
2052                 printf("  - %d B-channels\n", stinf->childcnt);
2053
2054                 if (!useable)
2055                         printf(" * Port NOT useable for PBX\n");
2056
2057                 printf("--------\n");
2058
2059                 i++;
2060         }
2061         printf("\n");
2062
2063         /* close mISDN */
2064         if ((err = mISDN_close(device)))
2065         {
2066                 fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err));
2067                 exit(-1);
2068         }
2069 }
2070
2071