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