backup
[lcr.git] / dss1.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN dss1                                                                **
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 CENTREX
29
30 #include "q931.h"
31 #include "ie.cpp"
32
33
34 /*
35  * constructor
36  */
37 Pdss1::Pdss1(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive) : PmISDN(type, mISDNport, portname, settings, channel, exclusive)
38 {
39         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN:INFO_ITYPE_ISDN_EXTENSION;
40         p_m_d_ntmode = mISDNport->ntmode;
41         p_m_d_l3id = 0;
42         p_m_d_ces = -1;
43         p_m_d_queue = NULL;
44         p_m_d_notify_pending = NULL;
45         p_m_d_collect_cause = CAUSE_NOUSER;
46         p_m_d_collect_location = LOCATION_PRIVATE_LOCAL;
47
48         PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, %s port #%d\n", portname, mISDNport->use, (mISDNport->ntmode)?"NT":"TE", p_m_portnum);
49 }
50
51
52 /*
53  * destructor
54  */
55 Pdss1::~Pdss1()
56 {
57         /* remove queued message */
58         if (p_m_d_queue)
59                 message_free(p_m_d_queue);
60
61         if (p_m_d_notify_pending)
62                 message_free(p_m_d_notify_pending);
63
64         /* check how many processes are left */
65         if (p_m_d_ntmode == 1)
66         {
67                 if (p_m_mISDNport->nst.layer3->proc)
68                         PDEBUG(DEBUG_ISDN, "destroyed mISDNPort(%s). WARNING: There is still a layer 3 process left. Ignore this, if currently are other calls. This message is not an error!\n", p_name);
69         }
70 }
71
72
73 /*
74  * create layer 3 message
75  */
76 static msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
77 {
78         int i = 0;
79         msg_t *dmsg;
80         Q931_info_t *qi;
81         iframe_t *frm;
82
83         if (!ntmode)
84                 size = sizeof(Q931_info_t)+2;
85
86         while(i < 10)
87         {
88                 if (ntmode)
89                 {
90                         dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
91                         if (dmsg)
92                         {
93                                 return(dmsg);
94                         }
95                 } else
96                 {
97                         dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
98                         if (dmsg)
99                         {
100                                 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
101                                 frm = (iframe_t *)dmsg->data;
102                                 frm->prim = prim;
103                                 frm->dinfo = dinfo;
104                                 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
105                                 qi->type = mt;
106                                 return(dmsg);
107                         }
108                 }
109
110                 if (!i)
111                         PERROR("cannot allocate memory, trying again...\n");
112                 i++;
113                 usleep(50000);
114         }
115         PERROR("cannot allocate memory, system overloaded.\n");
116         exit(-1);
117 }
118
119 msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
120 {
121         int i = 0;
122         msg_t *dmsg;
123
124         while(i < 10)
125         {
126                 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
127                 if (dmsg)
128                         return(dmsg);
129
130                 if (!i)
131                         PERROR("cannot allocate memory, trying again...\n");
132                 i++;
133                 usleep(50000);
134         }
135         PERROR("cannot allocate memory, system overloaded.\n");
136         exit(-1);
137 }
138
139 /*
140  * if we received a first reply to the setup message,
141  * we will check if we have now channel information 
142  * return: <0: error, call is released, -cause is given
143  *          0: ok, nothing to do
144  */
145 int Pdss1::received_first_reply_to_setup(unsigned long prim, int channel, int exclusive)
146 {
147         int ret;
148         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
149         RELEASE_COMPLETE_t *release_complete;
150         msg_t *dmsg;
151
152         /* correct exclusive to 0, if no explicit channel was given */
153         if (exclusive<0 || channel<=0)
154                 exclusive = 0;
155         
156         /* select scenario */
157         if (p_m_b_channel && p_m_b_exclusive)
158         {
159                 /*** we gave an exclusive channel (or if we are done) ***/
160
161                 /* if not first reply, we are done */
162                 if (p_state != PORT_STATE_OUT_SETUP)
163                         return(0);
164
165                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
166                 add_trace("channel", "request", "%d (forced)", p_m_b_channel);
167                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
168
169                 /* if give channel not accepted or not equal */
170                 if (channel!=-1 && p_m_b_channel!=channel)
171                 {
172                         add_trace("conclusion", NULL, "forced channel not accepted");
173                         end_trace();
174                         ret = -44;
175                         goto channelerror;
176                 }
177
178                 add_trace("conclusion", NULL, "channel was accepted");
179                 add_trace("connect", "channel", "%d", p_m_b_channel);
180                 end_trace();
181
182                 /* activate our exclusive channel */
183                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
184         } else
185         if (p_m_b_channel)
186         {
187                 /*** we gave a non-exclusive channel ***/
188
189                 /* if not first reply, we are done */
190                 if (p_state != PORT_STATE_OUT_SETUP)
191                         return(0);
192
193                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
194                 add_trace("channel", "request", "%d (suggest)", p_m_b_channel);
195                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
196
197                 /* if channel was accepted as given */
198                 if (channel==-1 || p_m_b_channel==channel)
199                 {
200                         add_trace("conclusion", NULL, "channel was accepted as given");
201                         add_trace("connect", "channel", "%d", p_m_b_channel);
202                         end_trace();
203                         p_m_b_exclusive = 1; // we are done
204                         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
205                         return(0);
206                 }
207
208                 /* if channel value is faulty */
209                 if (channel <= 0)
210                 {
211                         add_trace("conclusion", NULL, "illegal reply");
212                         end_trace();
213                         ret = -111; // protocol error
214                         goto channelerror;
215                 }
216
217                 /* if channel was not accepted, try to get it */
218                 ret = seize_bchannel(channel, 1); // exclusively
219                 add_trace("channel", "available", ret<0?"no":"yes");
220                 if (ret < 0)
221                 {
222                         add_trace("conclusion", NULL, "replied channel not available");
223                         end_trace();
224                         goto channelerror;
225                 }
226                 add_trace("conclusion", NULL, "replied channel accepted");
227                 add_trace("connect", "channel", "%d", p_m_b_channel);
228                 end_trace();
229
230                 /* activate channel given by remote */
231                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
232         } else
233         if (p_m_b_reserve)
234         {
235                 /*** we sent 'any channel acceptable' ***/
236
237                 /* if not first reply, we are done */
238                 if (p_state != PORT_STATE_OUT_SETUP)
239                         return(0);
240
241                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
242                 add_trace("channel", "request", "any");
243                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
244                 /* if no channel was replied */
245                 if (channel <= 0)
246                 {
247                         add_trace("conclusion", NULL, "no channel, protocol error");
248                         end_trace();
249                         ret = -111; // protocol error
250                         goto channelerror;
251                 }
252
253                 /* we will see, if our received channel is available */
254                 ret = seize_bchannel(channel, 1); // exclusively
255                 add_trace("channel", "available", ret<0?"no":"yes");
256                 if (ret < 0)
257                 {
258                         add_trace("conclusion", NULL, "replied channel not available");
259                         end_trace();
260                         goto channelerror;
261                 }
262                 add_trace("conclusion", NULL, "replied channel accepted");
263                 add_trace("connect", "channel", "%d", p_m_b_channel);
264                 end_trace();
265
266                 /* activate channel given by remote */
267                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
268         } else
269         {
270                 /*** we sent 'no channel available' ***/
271
272                 /* if not the first reply, but a connect, we are forced */
273                 if (prim==(CC_CONNECT | INDICATION) && p_state!=PORT_STATE_OUT_SETUP)
274                 {
275                         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (connect)", DIRECTION_NONE);
276                         add_trace("channel", "request", "no-channel");
277                         add_trace("channel", "reply", (channel>=0)?"%d%s":"(none)", channel, exclusive?" (forced)":"");
278                         if (channel > 0)
279                         {
280                                 goto use_from_connect;
281                         }
282                         ret = seize_bchannel(CHANNEL_ANY, 0); // any channel
283                         add_trace("channel", "available", ret<0?"no":"yes");
284                         if (ret < 0)
285                         {
286                                 add_trace("conclusion", NULL, "no channel available during call-waiting");
287                                 end_trace();
288                                 goto channelerror;
289                         }
290                         add_trace("conclusion", NULL, "using channel %d", p_m_b_channel);
291                         add_trace("connect", "channel", "%d", p_m_b_channel);
292                         end_trace();
293                         p_m_b_exclusive = 1; // we are done
294
295                         /* activate channel given by remote */
296                         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
297                         return(0);
298                 }
299                 
300                 /* if not first reply, we are done */
301                 if (p_state != PORT_STATE_OUT_SETUP)
302                         return(0);
303
304                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
305                 add_trace("channel", "request", "no-channel");
306                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
307                 /* if first reply has no channel, we are done */
308                 if (channel <= 0)
309                 {
310                         add_trace("conclusion", NULL, "no channel until connect");
311                         end_trace();
312                         return(0);
313                 }
314
315                 /* we will see, if our received channel is available */
316                 use_from_connect:
317                 ret = seize_bchannel(channel, exclusive);
318                 add_trace("channel", "available", ret<0?"no":"yes");
319                 if (ret < 0)
320                 {
321                         add_trace("conclusion", NULL, "replied channel not available");
322                         end_trace();
323                         goto channelerror;
324                 }
325                 add_trace("conclusion", NULL, "replied channel accepted");
326                 add_trace("connect", "channel", "%d", p_m_b_channel);
327                 end_trace();
328                 p_m_b_exclusive = 1; // we are done
329
330                 /* activate channel given by remote */
331                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
332         }
333         return(0);
334
335         channelerror:
336         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
337         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
338         l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
339         enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
340         end_trace();
341         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
342         new_state(PORT_STATE_RELEASE);
343         p_m_delete = 1;
344         return(-34); /* to epoint: no channel available */
345 }
346
347
348 /*
349  * hunt bchannel for incomming setup or retrieve or resume
350  */
351 int Pdss1::hunt_bchannel(int channel, int exclusive)
352 {
353         struct select_channel *selchannel;
354         struct interface_port *ifport = p_m_mISDNport->ifport;
355         int i;
356
357         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
358         if (exclusive<0)
359                 exclusive = 0;
360         if (channel == CHANNEL_NO)
361                 add_trace("channel", "request", "no-channel");
362         else
363                 add_trace("channel", "request", (channel>0)?"%d%s":"any", channel, exclusive?" (forced)":"");
364         if (channel==CHANNEL_NO && p_type==PORT_TYPE_DSS1_TE_IN)
365         {
366                 add_trace("conclusion", NULL, "incoming call-waiting not supported for TE-mode");
367                 end_trace();
368                 return(-6); // channel unacceptable
369         }
370         if (channel <= 0) /* not given, no channel, whatever.. */
371                 channel = CHANNEL_ANY; /* any channel */
372         if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) // of out chan..
373         {
374                 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
375                 add_trace("conclusion", NULL, "all channels are reserved");
376                 end_trace();
377                 return(-34); // no channel
378         }
379         if (channel == CHANNEL_ANY)
380                 goto get_from_list;
381         if (channel > 0)
382         {
383                 /* check for given channel in selection list */
384                 selchannel = ifport->in_channel;
385                 while(selchannel)
386                 {
387                         if (selchannel->channel == channel || selchannel->channel == CHANNEL_FREE)
388                                 break;
389                         selchannel = selchannel->next;
390                 }
391                 if (!selchannel)
392                         channel = 0;
393
394                 /* exclusive channel requests must be in the list */
395                 if (exclusive)
396                 {
397                         if (!channel)
398                         {
399                                 add_trace("conclusion", NULL, "exclusively requested channel not in list");
400                                 end_trace();
401                                 return(-6); // channel unacceptable
402                         }
403                         i = selchannel->channel-1-(selchannel->channel>=17);
404                         if (p_m_mISDNport->b_port[i] == NULL)
405                                 goto use_channel;
406                         add_trace("conclusion", NULL, "exclusively requested channel is busy");
407                         end_trace();
408                         return(-6); // channel unacceptable
409                 }
410
411                 /* requested channels in list will be used */
412                 if (channel)
413                 {
414                         i = selchannel->channel-1-(selchannel->channel>=17);
415                         if (p_m_mISDNport->b_port[i] == NULL)
416                                 goto use_channel;
417                 }
418
419                 /* if channel is not available or not in list, it must be searched */
420                 get_from_list:
421                 /* check for first free channel in list */
422                 channel = 0;
423                 selchannel = ifport->in_channel;
424                 while(selchannel)
425                 {
426                         switch(selchannel->channel)
427                         {
428                                 case CHANNEL_FREE: /* free channel */
429                                 add_trace("hunting", "channel", "free");
430                                 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num)
431                                         break; /* all channel in use or reserverd */
432                                 /* find channel */
433                                 i = 0;
434                                 while(i < p_m_mISDNport->b_num)
435                                 {
436                                         if (p_m_mISDNport->b_port[i] == NULL)
437                                         {
438                                                 channel = i+1+(i>=15);
439                                                 break;
440                                         }
441                                         i++;
442                                 }
443                                 break;
444
445                                 default:
446                                 add_trace("hunting", "channel", "%d", selchannel->channel);
447                                 if (selchannel->channel<1 || selchannel->channel==16)
448                                         break; /* invalid channels */
449                                 i = selchannel->channel-1-(selchannel->channel>=17);
450                                 if (i >= p_m_mISDNport->b_num)
451                                         break; /* channel not in port */
452                                 if (p_m_mISDNport->b_port[i] == NULL)
453                                 {
454                                         channel = selchannel->channel;
455                                         break;
456                                 }
457                                 break;
458                         }
459                         if (channel)
460                                 break; /* found channel */
461                         selchannel = selchannel->next;
462                 }
463                 if (!channel)
464                 {
465                         add_trace("conclusion", NULL, "no channel available");
466                         end_trace();
467                         return(-6); // channel unacceptable
468                 }
469         }
470 use_channel:
471         add_trace("conclusion", NULL, "channel available");
472         add_trace("connect", "channel", "%d", p_m_b_channel);
473         end_trace();
474         return(channel);
475 }
476
477 /*
478  * handles all indications
479  */
480 /* CC_SETUP INDICATION */
481 void Pdss1::setup_ind(unsigned long prim, unsigned long dinfo, void *data)
482 {
483         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
484         SETUP_t *setup = (SETUP_t *)((unsigned long)data + headerlen);
485         int calling_type, calling_plan, calling_present, calling_screen;
486         int called_type, called_plan;
487         int redir_type, redir_plan, redir_present, redir_screen, redir_reason;
488         int hlc_coding, hlc_presentation, hlc_interpretation, hlc_hlc, hlc_exthlc;
489         int bearer_coding, bearer_capability, bearer_mode, bearer_rate, bearer_multi, bearer_user;
490         int exclusive, channel;
491         int ret;
492         msg_t *dmsg;
493         unsigned char keypad[32] = "";
494         unsigned char useruser[128];
495         int useruser_len = 0, useruser_protocol;
496         class Endpoint *epoint;
497         struct message *message;
498
499         /* callref from nt-lib */
500         if (p_m_d_ntmode)
501         {
502                 /* nt-library now gives us the id via CC_SETUP */
503                 if (dinfo&(~0xff) == 0xff00)
504                 {
505                         PERROR("fatal software error: l3-stack gives us a process id 0xff00-0xffff\n");
506                         exit(-1);
507                 }
508                 l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
509                 if (p_m_d_l3id)
510                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
511                 add_trace("callref", "new", "0x%x", dinfo);
512                 end_trace();
513                 if (p_m_d_l3id&(~0xff) == 0xff00)
514                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
515                 p_m_d_l3id = dinfo;
516                 p_m_d_ces = setup->ces;
517         }
518
519         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
520         dec_ie_calling_pn(setup->CALLING_PN, (Q931_info_t *)((unsigned long)data+headerlen), &calling_type, &calling_plan, &calling_present, &calling_screen, (unsigned char *)p_callerinfo.id, sizeof(p_callerinfo.id));
521         dec_ie_called_pn(setup->CALLED_PN, (Q931_info_t *)((unsigned long)data+headerlen), &called_type, &called_plan, (unsigned char *)p_dialinginfo.number, sizeof(p_dialinginfo.number));
522         dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)keypad, sizeof(keypad));
523 #ifdef CENTREX
524         /* te-mode: CNIP (calling name identification presentation) */
525         if (!p_m_d_ntmode)
526                 dec_facility_centrex(setup->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)p_callerinfo.name, sizeof(p_callerinfo.name));
527 #endif
528         dec_ie_useruser(setup->USER_USER, (Q931_info_t *)((unsigned long)data+headerlen), &useruser_protocol, useruser, &useruser_len);
529         dec_ie_complete(setup->COMPLETE, (Q931_info_t *)((unsigned long)data+headerlen), &p_dialinginfo.sending_complete);
530         dec_ie_redir_nr(setup->REDIR_NR, (Q931_info_t *)((unsigned long)data+headerlen), &redir_type, &redir_plan, &redir_present, &redir_screen, &redir_reason, (unsigned char *)p_redirinfo.id, sizeof(p_redirinfo.id));
531         dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
532         dec_ie_hlc(setup->HLC, (Q931_info_t *)((unsigned long)data+headerlen), &hlc_coding, &hlc_interpretation, &hlc_presentation, &hlc_hlc, &hlc_exthlc);
533         dec_ie_bearer(setup->BEARER, (Q931_info_t *)((unsigned long)data+headerlen), &bearer_coding, &bearer_capability, &bearer_mode, &bearer_rate, &bearer_multi, &bearer_user);
534         end_trace();
535
536         /* if blocked, release call */
537         if (p_m_mISDNport->ifport->block)
538         {
539                 RELEASE_COMPLETE_t *release_complete;
540
541                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
542                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
543                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
544                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 27); /* temporary unavailable */
545                 add_trace("reason", NULL, "port blocked");
546                 end_trace();
547                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
548                 new_state(PORT_STATE_RELEASE);
549                 p_m_delete = 1;
550                 return;
551         }
552
553         /* caller info */
554         switch (calling_present)
555         {
556                 case 1:
557                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
558                 break;
559                 case 2:
560                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
561                 break;
562                 default:
563                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
564                 break;
565         }
566         switch (calling_screen)
567         {
568                 case 0:
569                 p_callerinfo.screen = INFO_SCREEN_USER;
570                 break;
571                 default:
572                 p_callerinfo.screen = INFO_SCREEN_NETWORK;
573                 break;
574         }
575         switch (calling_type)
576         {
577                 case -1:
578                 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
579                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
580                 p_callerinfo.screen = INFO_SCREEN_NETWORK;
581                 break;
582                 case 0x1:
583                 p_callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
584                 break;
585                 case 0x2:
586                 p_callerinfo.ntype = INFO_NTYPE_NATIONAL;
587                 break;
588                 case 0x4:
589                 p_callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
590                 break;
591                 default:
592                 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
593                 break;
594         }
595         p_callerinfo.isdn_port = p_m_portnum;
596         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
597
598         /* dialing information */
599         SCAT(p_dialinginfo.number, (char *)keypad);
600         switch (called_type)
601         {
602                 case 0x1:
603                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
604                 break;
605                 case 0x2:
606                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
607                 break;
608                 case 0x4:
609                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
610                 break;
611                 default:
612                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
613                 break;
614         }
615
616         /* redir info */
617         switch (redir_present)
618         {
619                 case 1:
620                 p_redirinfo.present = INFO_PRESENT_RESTRICTED;
621                 break;
622                 case 2:
623                 p_redirinfo.present = INFO_PRESENT_NOTAVAIL;
624                 break;
625                 default:
626                 p_redirinfo.present = INFO_PRESENT_ALLOWED;
627                 break;
628         }
629         switch (redir_screen)
630         {
631                 case 0:
632                 p_redirinfo.screen = INFO_SCREEN_USER;
633                 break;
634                 default:
635                 p_redirinfo.screen = INFO_SCREEN_NETWORK;
636                 break;
637         }
638         switch (redir_reason)
639         {
640                 case 1:
641                 p_redirinfo.reason = INFO_REDIR_BUSY;
642                 break;
643                 case 2:
644                 p_redirinfo.reason = INFO_REDIR_NORESPONSE;
645                 break;
646                 case 15:
647                 p_redirinfo.reason = INFO_REDIR_UNCONDITIONAL;
648                 break;
649                 case 10:
650                 p_redirinfo.reason = INFO_REDIR_CALLDEFLECT;
651                 break;
652                 case 9:
653                 p_redirinfo.reason = INFO_REDIR_OUTOFORDER;
654                 break;
655                 default:
656                 p_redirinfo.reason = INFO_REDIR_UNKNOWN;
657                 break;
658         }
659         switch (redir_type)
660         {
661                 case -1:
662                 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
663                 p_redirinfo.present = INFO_PRESENT_NULL; /* not redirecting */
664                 p_redirinfo.reason = INFO_REDIR_UNKNOWN;
665                 break;
666                 case 0x1:
667                 p_redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
668                 break;
669                 case 0x2:
670                 p_redirinfo.ntype = INFO_NTYPE_NATIONAL;
671                 break;
672                 case 0x4:
673                 p_redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
674                 break;
675                 default:
676                 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
677                 break;
678         }
679         p_redirinfo.isdn_port = p_m_portnum;
680
681         /* bearer capability */
682         switch (bearer_capability)
683         {
684                 case -1:
685                 p_capainfo.bearer_capa = INFO_BC_AUDIO;
686                 bearer_user = (options.law=='a')?3:2;
687                 break;
688                 default:
689                 p_capainfo.bearer_capa = bearer_capability;
690                 break;
691         }
692         switch (bearer_mode)
693         {
694                 case 2:
695                 p_capainfo.bearer_mode = INFO_BMODE_PACKET;
696                 break;
697                 default:
698                 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
699                 break;
700         }
701         switch (bearer_user)
702         {
703                 case -1:
704                 p_capainfo.bearer_info1 = INFO_INFO1_NONE;
705                 break;
706                 default:
707                 p_capainfo.bearer_info1 = bearer_user + 0x80;
708                 break;
709         }
710
711         /* hlc */
712         switch (hlc_hlc)
713         {
714                 case -1:
715                 p_capainfo.hlc = INFO_HLC_NONE;
716                 break;
717                 default:
718                 p_capainfo.hlc = hlc_hlc + 0x80;
719                 break;
720         }
721         switch (hlc_exthlc)
722         {
723                 case -1:
724                 p_capainfo.exthlc = INFO_HLC_NONE;
725                 break;
726                 default:
727                 p_capainfo.exthlc = hlc_exthlc + 0x80;
728                 break;
729         }
730
731         /* hunt channel */
732         ret = channel = hunt_bchannel(channel, exclusive);
733         if (ret < 0)
734                 goto no_channel;
735
736         /* open channel */
737         ret = seize_bchannel(channel, 1);
738         if (ret < 0)
739         {
740                 no_channel:
741                 RELEASE_COMPLETE_t *release_complete;
742
743                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
744                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
745                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
746                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
747                 end_trace();
748                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
749                 new_state(PORT_STATE_RELEASE);
750                 p_m_delete = 1;
751                 return;
752         }
753         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
754
755         /* create endpoint */
756         if (p_epointlist)
757         {
758                 PERROR("SOFTWARE ERROR: incoming call but already got an endpoint, exitting...\n");
759                 exit(-1);
760         }
761         if (!(epoint = new Endpoint(p_serial, 0)))
762         {
763                 RELEASE_COMPLETE_t *release_complete;
764
765                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
766                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
767                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
768                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 41); /* temporary failure */
769                 end_trace();
770                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
771                 new_state(PORT_STATE_RELEASE);
772                 p_m_delete = 1;
773                 return;
774         }
775         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint)))
776         {
777                 PERROR("no memory for application\n");
778                 exit(-1);
779         }
780         if (!(epointlist_new(epoint->ep_serial)))
781         {
782                 PERROR("no memory for epointlist\n");
783                 exit(-1);
784         }
785         /* send setup message to endpoit */
786         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
787         message->param.setup.isdn_port = p_m_portnum;
788         message->param.setup.port_type = p_type;
789         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
790         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
791         memcpy(&message->param.setup.redirinfo, &p_redirinfo, sizeof(struct redir_info));
792         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
793         memcpy(message->param.setup.useruser.data, &useruser, useruser_len);
794         message->param.setup.useruser.len = useruser_len;
795         message->param.setup.useruser.protocol = useruser_protocol;
796         message_put(message);
797
798         new_state(PORT_STATE_IN_SETUP);
799 }
800
801 /* CC_INFORMATION INDICATION */
802 void Pdss1::information_ind(unsigned long prim, unsigned long dinfo, void *data)
803 {
804         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
805         INFORMATION_t *information = (INFORMATION_t *)((unsigned long)data + headerlen);
806         int type, plan;
807         unsigned char keypad[32] = "";
808         struct message *message;
809
810         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
811         dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, (unsigned char *)p_dialinginfo.number, sizeof(p_dialinginfo.number));
812         dec_ie_keypad(information->KEYPAD, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)keypad, sizeof(keypad));
813         dec_ie_complete(information->COMPLETE, (Q931_info_t *)((unsigned long)data+headerlen), &p_dialinginfo.sending_complete);
814         end_trace();
815
816         SCAT(p_dialinginfo.number, (char *)keypad);
817         switch (type)
818         {
819                 case 0x1:
820                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
821                 break;
822                 case 0x2:
823                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
824                 break;
825                 case 0x4:
826                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
827                 break;
828                 default:
829                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
830                 break;
831         }
832         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
833         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
834         message_put(message);
835         /* reset overlap timeout */
836         new_state(p_state);
837 }
838
839 /* CC_SETUP_ACCNOWLEDGE INDICATION */
840 void Pdss1::setup_acknowledge_ind(unsigned long prim, unsigned long dinfo, void *data)
841 {
842         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
843         SETUP_ACKNOWLEDGE_t *setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)((unsigned long)data + headerlen);
844         int exclusive, channel;
845         int coding, location, progress;
846         int ret;
847         struct message *message;
848
849         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
850         dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
851         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
852         end_trace();
853
854         /* process channel */
855         ret = received_first_reply_to_setup(prim, exclusive, channel);
856         if (ret < 0)
857         {
858                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
859                 message->param.disconnectinfo.cause = -ret;
860                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
861                 message_put(message);
862                 new_state(PORT_STATE_RELEASE);
863                 p_m_delete = 1;
864                 return;
865         }
866
867         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_OVERLAP);
868         message_put(message);
869
870         new_state(PORT_STATE_OUT_OVERLAP);
871 }
872
873 /* CC_PROCEEDING INDICATION */
874 void Pdss1::proceeding_ind(unsigned long prim, unsigned long dinfo, void *data)
875 {
876         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
877         CALL_PROCEEDING_t *proceeding = (CALL_PROCEEDING_t *)((unsigned long)data + headerlen);
878         int exclusive, channel;
879         int coding, location, progress;
880         int ret;
881         struct message *message;
882         int notify = -1, type, plan, present;
883         char redir[32];
884
885         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
886         dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
887         dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
888         dec_ie_notify(NULL/*proceeding->NOTIFY*/, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
889         dec_ie_redir_dn(proceeding->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)redir, sizeof(redir));
890         end_trace();
891
892         ret = received_first_reply_to_setup(prim, exclusive, channel);
893         if (ret < 0)
894         {
895                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
896                 message->param.disconnectinfo.cause = -ret;
897                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
898                 message_put(message);
899                 new_state(PORT_STATE_RELEASE);
900                 p_m_delete = 1;
901                 return;
902         }
903         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
904         message_put(message);
905
906         new_state(PORT_STATE_OUT_PROCEEDING);
907         
908         if (notify >= 0)
909                 notify |= 0x80;
910         else
911                 notify = 0;
912         if (type >= 0 || notify)
913         {
914                 if (!notify && type >= 0)
915                         notify = 251;
916                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
917                 message->param.notifyinfo.notify = notify;
918                 SCPY(message->param.notifyinfo.id, redir);
919                 /* redirection number */
920                 switch (present)
921                 {
922                         case 1:
923                         message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
924                         break;
925                         case 2:
926                         message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
927                         break;
928                         default:
929                         message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
930                         break;
931                 }
932                 switch (type)
933                 {
934                         case -1:
935                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
936                         message->param.notifyinfo.present = INFO_PRESENT_NULL;
937                         break;
938                         case 1:
939                         message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
940                         break;
941                         case 2:
942                         message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
943                         break;
944                         case 4:
945                         message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
946                         break;
947                         default:
948                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
949                         break;
950                 }
951                 message->param.notifyinfo.isdn_port = p_m_portnum;
952                 message_put(message);
953         }
954 }
955
956 /* CC_ALERTING INDICATION */
957 void Pdss1::alerting_ind(unsigned long prim, unsigned long dinfo, void *data)
958 {
959         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
960         ALERTING_t *alerting = (ALERTING_t *)((unsigned long)data + headerlen);
961         int exclusive, channel;
962         int coding, location, progress;
963         int ret;
964         struct message *message;
965         int notify = -1, type, plan, present;
966         char redir[32];
967
968         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
969         dec_ie_channel_id(alerting->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
970         dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
971         dec_ie_notify(NULL/*alerting->NOTIFY*/, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
972         dec_ie_redir_dn(alerting->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)redir, sizeof(redir));
973         end_trace();
974
975         /* process channel */
976         ret = received_first_reply_to_setup(prim, exclusive, channel);
977         if (ret < 0)
978         {
979                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
980                 message->param.disconnectinfo.cause = -ret;
981                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
982                 message_put(message);
983                 new_state(PORT_STATE_RELEASE);
984                 p_m_delete = 1;
985                 return;
986         }
987         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
988         message_put(message);
989
990         new_state(PORT_STATE_OUT_ALERTING);
991
992         if (notify >= 0)
993                 notify |= 0x80;
994         else
995                 notify = 0;
996         if (type >= 0 || notify)
997         {
998                 if (!notify && type >= 0)
999                         notify = 251;
1000                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1001                 message->param.notifyinfo.notify = notify;
1002                 SCPY(message->param.notifyinfo.id, redir);
1003                 switch (present)
1004                 {
1005                         case 1:
1006                         message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1007                         break;
1008                         case 2:
1009                         message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1010                         break;
1011                         default:
1012                         message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1013                         break;
1014                 }
1015                 switch (type)
1016                 {
1017                         case -1:
1018                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1019                         message->param.notifyinfo.present = INFO_PRESENT_NULL;
1020                         break;
1021                         case 1:
1022                         message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1023                         break;
1024                         case 2:
1025                         message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1026                         break;
1027                         case 4:
1028                         message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1029                         break;
1030                         default:
1031                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1032                         break;
1033                 }
1034                 message->param.notifyinfo.isdn_port = p_m_portnum;
1035                 message_put(message);
1036         }
1037 }
1038
1039 /* CC_CONNECT INDICATION */
1040 void Pdss1::connect_ind(unsigned long prim, unsigned long dinfo, void *data)
1041 {
1042         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1043         CONNECT_t *connect = (CONNECT_t *)((unsigned long)data + headerlen);
1044         int exclusive, channel;
1045         int type, plan, present, screen;
1046         int ret;
1047         msg_t *dmsg;
1048         struct message *message;
1049         int bchannel_before;
1050         CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
1051
1052         if (p_m_d_ntmode)
1053                 p_m_d_ces = connect->ces;
1054
1055         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1056         dec_ie_channel_id(connect->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1057         dec_ie_connected_pn(connect->CONNECT_PN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, &screen, (unsigned char *)p_connectinfo.id, sizeof(p_connectinfo.id));
1058 #ifdef CENTREX
1059         /* te-mode: CONP (connected name identification presentation) */
1060         if (!p_m_d_ntmode)
1061                 dec_facility_centrex(connect->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)p_connectinfo.name, sizeof(p_connectinfo.name));
1062 #endif
1063         end_trace();
1064
1065         /* select channel */
1066         bchannel_before = p_m_b_channel;
1067         ret = received_first_reply_to_setup(prim, exclusive, channel);
1068         if (ret < 0)
1069         {
1070                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1071                 message->param.disconnectinfo.cause = -ret;
1072                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1073                 message_put(message);
1074                 new_state(PORT_STATE_RELEASE);
1075                 p_m_delete = 1;
1076                 return;
1077         }
1078
1079         /* connect information */
1080         switch (present)
1081         {
1082                 case 1:
1083                 p_connectinfo.present = INFO_PRESENT_RESTRICTED;
1084                 break;
1085                 case 2:
1086                 p_connectinfo.present = INFO_PRESENT_NOTAVAIL;
1087                 break;
1088                 default:
1089                 p_connectinfo.present = INFO_PRESENT_ALLOWED;
1090                 break;
1091         }
1092         switch (screen)
1093         {
1094                 case 0:
1095                 p_connectinfo.screen = INFO_SCREEN_USER;
1096                 break;
1097                 default:
1098                 p_connectinfo.screen = INFO_SCREEN_NETWORK;
1099                 break;
1100         }
1101         switch (type)
1102         {
1103                 case 0x0:
1104                 p_connectinfo.present = INFO_PRESENT_NULL; /* no COLP info */
1105                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1106                 break;
1107                 case 0x1:
1108                 p_connectinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1109                 break;
1110                 case 0x2:
1111                 p_connectinfo.ntype = INFO_NTYPE_NATIONAL;
1112                 break;
1113                 case 0x4:
1114                 p_connectinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1115                 break;
1116                 default:
1117                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1118                 break;
1119         }
1120         p_connectinfo.isdn_port = p_m_portnum;
1121         SCPY(p_connectinfo.interfaces, p_m_mISDNport->ifport->interface->name);
1122
1123         /* send connect acknowledge */
1124         dmsg = create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT, dinfo, sizeof(CONNECT_ACKNOWLEDGE_t), p_m_d_ntmode);
1125         connect_acknowledge = (CONNECT_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1126         l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | RESPONSE, DIRECTION_OUT);
1127         /* if we had no bchannel before, we send it now */
1128         if (!bchannel_before && p_m_b_channel)
1129                 enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1130         end_trace();
1131         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1132
1133         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1134         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
1135         message_put(message);
1136
1137         new_state(PORT_STATE_CONNECT);
1138 }
1139
1140 /* CC_DISCONNECT INDICATION */
1141 void Pdss1::disconnect_ind(unsigned long prim, unsigned long dinfo, void *data)
1142 {
1143         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1144         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1145         int location, cause;
1146         int coding, proglocation, progress;
1147         struct message *message;
1148
1149         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1150         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &proglocation, &progress);
1151         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1152         end_trace();
1153
1154         if (cause < 0)
1155                 cause = 16;
1156
1157         /* release if we are remote sends us no tones */
1158         if (p_m_mISDNport->is_earlyb)
1159         {
1160                 RELEASE_t *release;
1161                 msg_t *dmsg;
1162
1163                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, dinfo, sizeof(RELEASE_t), p_m_d_ntmode);
1164                 release = (RELEASE_t *)(dmsg->data + headerlen);
1165                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
1166                 enc_ie_cause(&release->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16); /* normal */
1167                 add_trace("reason", NULL, "no remote patterns");
1168                 end_trace();
1169                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1170
1171                 /* sending release to endpoint */
1172                 while(p_epointlist)
1173                 {
1174                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1175                         message->param.disconnectinfo.cause = cause;
1176                         message->param.disconnectinfo.location = location;
1177                         message_put(message);
1178                         /* remove epoint */
1179                         free_epointlist(p_epointlist);
1180                 }
1181                 new_state(PORT_STATE_RELEASE);
1182                 p_m_delete = 1;
1183                 return;
1184         }
1185
1186         /* sending disconnect to active endpoint and release to inactive endpoints */
1187         if (ACTIVE_EPOINT(p_epointlist))
1188         {
1189                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
1190                 message->param.disconnectinfo.location = location;
1191                 message->param.disconnectinfo.cause = cause;
1192                 message_put(message);
1193         }
1194         while(INACTIVE_EPOINT(p_epointlist))
1195         {
1196                 message = message_create(p_serial, INACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1197                 message->param.disconnectinfo.location = location;
1198                 message->param.disconnectinfo.cause = cause;
1199                 message_put(message);
1200                 /* remove epoint */
1201                 free_epointid(INACTIVE_EPOINT(p_epointlist));
1202         }
1203         new_state(PORT_STATE_IN_DISCONNECT);
1204 }
1205
1206 /* CC_DISCONNECT INDICATION */
1207 void Pdss1::disconnect_ind_i(unsigned long prim, unsigned long dinfo, void *data)
1208 {
1209         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1210         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1211         int location, cause;
1212
1213         /* cause */
1214         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1215         if (p_m_d_collect_cause > 0)
1216         {
1217                 add_trace("old-cause", "location", "%d", p_m_d_collect_location);
1218                 add_trace("old-cause", "value", "%d", p_m_d_collect_cause);
1219         }
1220         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1221         end_trace();
1222
1223         /* collect cause */
1224         if (cause == CAUSE_REJECTED) /* call rejected */
1225         {
1226                 p_m_d_collect_cause = CAUSE_REJECTED;
1227                 p_m_d_collect_location = location;
1228         } else
1229         if (cause==CAUSE_NORMAL && p_m_d_collect_cause!=CAUSE_REJECTED) /* reject via hangup */
1230         {
1231                 p_m_d_collect_cause = CAUSE_NORMAL;
1232                 p_m_d_collect_location = location;
1233         } else
1234         if (cause==CAUSE_BUSY && p_m_d_collect_cause!=CAUSE_REJECTED && p_m_d_collect_cause!=CAUSE_NORMAL) /* busy */
1235         {
1236                 p_m_d_collect_cause = CAUSE_BUSY;
1237                 p_m_d_collect_location = location;
1238         } else
1239         if (cause==CAUSE_OUTOFORDER && p_m_d_collect_cause!=CAUSE_BUSY && p_m_d_collect_cause!=CAUSE_REJECTED && p_m_d_collect_cause!=CAUSE_NORMAL) /* no L1 */
1240         {
1241                 p_m_d_collect_cause = CAUSE_OUTOFORDER;
1242                 p_m_d_collect_location = location;
1243         } else
1244         if (cause!=0 && cause!=CAUSE_NOUSER && p_m_d_collect_cause!=CAUSE_OUTOFORDER && p_m_d_collect_cause!=CAUSE_BUSY && p_m_d_collect_cause!=CAUSE_REJECTED && p_m_d_collect_cause!=CAUSE_NORMAL) /* anything if cause exists and not 18 */
1245         {
1246                 p_m_d_collect_cause = cause;
1247                 p_m_d_collect_location = location;
1248         }
1249         add_trace("new-cause", "location", "%d", p_m_d_collect_location);
1250         add_trace("new-cause", "value", "%d", p_m_d_collect_cause);
1251
1252 }
1253
1254 /* CC_RELEASE INDICATION */
1255 void Pdss1::release_ind(unsigned long prim, unsigned long dinfo, void *data)
1256 {
1257         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1258         RELEASE_t *release = (RELEASE_t *)((unsigned long)data + headerlen);
1259         msg_t *dmsg;
1260         int location, cause;
1261         struct message *message;
1262
1263         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1264         dec_ie_cause(release->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1265         end_trace();
1266
1267         if (cause < 0)
1268                 cause = 16;
1269
1270         /* sending release to endpoint */
1271         while(p_epointlist)
1272         {
1273                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1274                 message->param.disconnectinfo.cause = cause;
1275                 message->param.disconnectinfo.location = location;
1276                 message_put(message);
1277                 /* remove epoint */
1278                 free_epointlist(p_epointlist);
1279         }
1280
1281         /* only in NT mode we must send release_complete, if we got a release confirm */
1282         if (prim == (CC_RELEASE | CONFIRM))
1283         {
1284                 /* sending release complete */
1285                 RELEASE_COMPLETE_t *release_complete;
1286
1287                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
1288                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
1289                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
1290                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16);
1291                 end_trace();
1292                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1293         }
1294
1295         new_state(PORT_STATE_RELEASE);
1296         p_m_delete = 1;
1297 }
1298
1299 /* CC_RELEASE_COMPLETE INDICATION */
1300 void Pdss1::release_complete_ind(unsigned long prim, unsigned long dinfo, void *data)
1301 {
1302         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1303         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *)((unsigned long)data + headerlen);
1304         int location, cause;
1305         struct message *message;
1306
1307         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1308         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1309         end_trace();
1310
1311         if (cause < 0)
1312                 cause = 16;
1313
1314         /* sending release to endpoint */
1315         while(p_epointlist)
1316         {
1317                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1318                 message->param.disconnectinfo.cause = cause;
1319                 message->param.disconnectinfo.location = location;
1320                 message_put(message);
1321                 /* remove epoint */
1322                 free_epointlist(p_epointlist);
1323         }
1324
1325         new_state(PORT_STATE_RELEASE);
1326         p_m_delete = 1;
1327 }
1328
1329 /* T312 timeout  */
1330 void Pdss1::t312_timeout(unsigned long prim, unsigned long dinfo, void *data)
1331 {
1332         struct message *message;
1333
1334         // trace is done at message_isdn()
1335         
1336         /* sending release to endpoint */
1337         while(p_epointlist)
1338         {
1339                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1340                 message->param.disconnectinfo.cause = p_m_d_collect_cause;
1341                 message->param.disconnectinfo.location = p_m_d_collect_location;
1342                 message_put(message);
1343                 /* remove epoint */
1344                 free_epointlist(p_epointlist);
1345         }
1346
1347         new_state(PORT_STATE_RELEASE);
1348         p_m_delete = 1;
1349 }
1350
1351 /* CC_NOTIFY INDICATION */
1352 void Pdss1::notify_ind(unsigned long prim, unsigned long dinfo, void *data)
1353 {
1354         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1355         NOTIFY_t *notifying = (NOTIFY_t *)((unsigned long)data + headerlen);
1356         struct message *message;
1357         int notify, type, plan, present;
1358
1359         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1360         dec_ie_notify(notifying->NOTIFY, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
1361         dec_ie_redir_dn(notifying->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)message->param.notifyinfo.id, sizeof(message->param.notifyinfo.id));
1362         end_trace();
1363
1364         if (!ACTIVE_EPOINT(p_epointlist))
1365                 return;
1366         /* notification indicator */
1367         if (notify < 0)
1368                 return;
1369         notify |= 0x80;
1370         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1371         message->param.notifyinfo.notify = notify;
1372         /* redirection number */
1373         switch (present)
1374         {
1375                 case 1:
1376                 message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1377                 break;
1378                 case 2:
1379                 message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1380                 break;
1381                 default:
1382                 message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1383                 break;
1384         }
1385         switch (type)
1386         {
1387                 case -1:
1388                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1389                 message->param.notifyinfo.present = INFO_PRESENT_NULL;
1390                 break;
1391                 case 1:
1392                 message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1393                 break;
1394                 case 2:
1395                 message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1396                 break;
1397                 case 4:
1398                 message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1399                 break;
1400                 default:
1401                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1402                 break;
1403         }
1404         message->param.notifyinfo.isdn_port = p_m_portnum;
1405         message_put(message);
1406 }
1407
1408
1409 /* CC_HOLD INDICATION */
1410 void Pdss1::hold_ind(unsigned long prim, unsigned long dinfo, void *data)
1411 {
1412         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1413 //      HOLD_t *hold = (HOLD_t *)((unsigned long)data + headerlen);
1414         struct message *message;
1415         HOLD_REJECT_t *hold_reject;
1416         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
1417         msg_t *dmsg;
1418 //      class Endpoint *epoint;
1419
1420         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1421         end_trace();
1422
1423         if (!ACTIVE_EPOINT(p_epointlist) || p_m_hold)
1424         {
1425                 dmsg = create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT, dinfo, sizeof(HOLD_REJECT_t), p_m_d_ntmode);
1426                 hold_reject = (HOLD_REJECT_t *)(dmsg->data + headerlen);
1427                 l1l2l3_trace_header(p_m_mISDNport, this, CC_HOLD_REJECT | REQUEST, DIRECTION_OUT);
1428                 enc_ie_cause(&hold_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, p_m_hold?101:31); /* normal unspecified / incompatible state */
1429                 add_trace("reason", NULL, "no endpoint");
1430                 end_trace();
1431                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1432
1433                 return;
1434         }
1435
1436         /* notify the hold of call */
1437         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1438         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1439         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1440         message_put(message);
1441
1442         /* deactivate bchannel */
1443         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (hold)", DIRECTION_NONE);
1444         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1445         end_trace();
1446         drop_bchannel();
1447
1448         /* set hold state */
1449         p_m_hold = 1;
1450 #if 0
1451         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1452         if (epoint && p_m_d_ntmode)
1453         {
1454                 p_m_timeout = p_settings.tout_hold;
1455                 time(&p_m_timer);
1456         }
1457 #endif
1458
1459         /* acknowledge hold */
1460         dmsg = create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE, dinfo, sizeof(HOLD_ACKNOWLEDGE_t), p_m_d_ntmode);
1461         hold_acknowledge = (HOLD_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1462         l1l2l3_trace_header(p_m_mISDNport, this, CC_HOLD_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1463         end_trace();
1464         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1465 }
1466
1467
1468 /* CC_RETRIEVE INDICATION */
1469 void Pdss1::retrieve_ind(unsigned long prim, unsigned long dinfo, void *data)
1470 {
1471         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1472         RETRIEVE_t *retrieve = (RETRIEVE_t *)((unsigned long)data + headerlen);
1473         RETRIEVE_REJECT_t *retrieve_reject;
1474         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1475         struct message *message;
1476         int channel, exclusive, cause;
1477         msg_t *dmsg;
1478         int ret;
1479
1480         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1481         dec_ie_channel_id(retrieve->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1482         end_trace();
1483
1484         if (!p_m_hold)
1485         {
1486                 cause = 101; /* incompatible state */
1487                 reject:
1488
1489                 dmsg = create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT, dinfo, sizeof(RETRIEVE_REJECT_t), p_m_d_ntmode);
1490                 retrieve_reject = (RETRIEVE_REJECT_t *)(dmsg->data + headerlen);
1491                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RETRIEVE_REJECT | REQUEST, DIRECTION_OUT);
1492                 enc_ie_cause(&retrieve_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1493                 end_trace();
1494                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1495
1496                 return;
1497         }
1498
1499         /* notify the retrieve of call */
1500         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1501         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1502         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1503         message_put(message);
1504
1505         /* hunt channel */
1506         ret = channel = hunt_bchannel(channel, exclusive);
1507         if (ret < 0)
1508                 goto no_channel;
1509
1510         /* open channel */
1511         ret = seize_bchannel(channel, 1);
1512         if (ret < 0)
1513         {
1514                 no_channel:
1515                 cause = -ret;
1516                 goto reject;
1517         }
1518         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
1519
1520         /* set hold state */
1521         p_m_hold = 0;
1522         p_m_timeout = 0;
1523
1524         /* acknowledge retrieve */
1525         dmsg = create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE, dinfo, sizeof(RETRIEVE_ACKNOWLEDGE_t), p_m_d_ntmode);
1526         retrieve_acknowledge = (RETRIEVE_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1527         l1l2l3_trace_header(p_m_mISDNport, this, CC_RETRIEVE_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1528         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1529         end_trace();
1530         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1531 }
1532
1533 /* CC_SUSPEND INDICATION */
1534 void Pdss1::suspend_ind(unsigned long prim, unsigned long dinfo, void *data)
1535 {
1536         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1537         SUSPEND_t *suspend = (SUSPEND_t *)((unsigned long)data + headerlen);
1538         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
1539         SUSPEND_REJECT_t *suspend_reject;
1540         struct message *message;
1541         class Endpoint *epoint;
1542         unsigned char callid[8];
1543         int len;
1544         msg_t *dmsg;
1545         int ret = -31; /* normal, unspecified */
1546
1547         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1548         dec_ie_call_id(suspend->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1549         end_trace();
1550
1551         if (!ACTIVE_EPOINT(p_epointlist))
1552         {
1553                 reject:
1554                 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, dinfo, sizeof(SUSPEND_REJECT_t), p_m_d_ntmode);
1555                 suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + headerlen);
1556                 l1l2l3_trace_header(p_m_mISDNport, this, CC_SUSPEND_REJECT | REQUEST, DIRECTION_OUT);
1557                 enc_ie_cause(&suspend_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1558                 end_trace();
1559                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1560
1561                 return;
1562         }
1563
1564         /* call id */
1565         if (len<0) len = 0;
1566
1567         /* check if call id is in use */
1568         epoint = epoint_first;
1569         while(epoint)
1570         {
1571                 if (epoint->ep_park)
1572                 {
1573                         if (epoint->ep_park_len == len)
1574                         if (!memcmp(epoint->ep_park_callid, callid, len))
1575                         {
1576                                 ret = -84; /* call id in use */
1577                                 goto reject;
1578                         }
1579                 }
1580                 epoint = epoint->next;
1581         }
1582
1583         /* notify the hold of call */
1584         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1585         message->param.notifyinfo.notify = INFO_NOTIFY_USER_SUSPENDED;
1586         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1587         message_put(message);
1588
1589         /* deactivate bchannel */
1590         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (suspend)", DIRECTION_NONE);
1591         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1592         end_trace();
1593         drop_bchannel();
1594
1595         /* sending suspend to endpoint */
1596         while (p_epointlist)
1597         {
1598                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_SUSPEND);
1599                 memcpy(message->param.parkinfo.callid, callid, sizeof(message->param.parkinfo.callid));
1600                 message->param.parkinfo.len = len;
1601                 message_put(message);
1602                 /* remove epoint */
1603                 free_epointlist(p_epointlist);
1604         }
1605
1606         /* sending SUSPEND_ACKNOWLEDGE */
1607         dmsg = create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE, dinfo, sizeof(SUSPEND_ACKNOWLEDGE_t), p_m_d_ntmode);
1608         suspend_acknowledge = (SUSPEND_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1609         l1l2l3_trace_header(p_m_mISDNport, this, CC_SUSPEND_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1610         end_trace();
1611         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1612
1613         new_state(PORT_STATE_RELEASE);
1614         p_m_delete = 1;
1615 }
1616
1617 /* CC_RESUME INDICATION */
1618 void Pdss1::resume_ind(unsigned long prim, unsigned long dinfo, void *data)
1619 {
1620         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1621         RESUME_t *resume = (RESUME_t *)((unsigned long)data + headerlen);
1622         RESUME_REJECT_t *resume_reject;
1623         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
1624         unsigned char callid[8];
1625         int len;
1626         int channel, exclusive;
1627         msg_t *dmsg;
1628         class Endpoint *epoint;
1629         struct message *message;
1630         int ret;
1631
1632         /* callref from nt-lib */
1633         if (p_m_d_ntmode)
1634         {
1635                 /* nt-library now gives us the id via CC_RESUME */
1636                 if (dinfo&(~0xff) == 0xff00)
1637                 {
1638                         PERROR("fatal software error: l3-stack gives us a process id 0xff00-0xffff\n");
1639                         exit(-1);
1640                 }
1641                 l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1642                 if (p_m_d_l3id)
1643                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1644                 add_trace("callref", "new", "0x%x", dinfo);
1645                 end_trace();
1646                 if (p_m_d_l3id&(~0xff) == 0xff00)
1647                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1648                 p_m_d_l3id = dinfo;
1649                 p_m_d_ces = resume->ces;
1650         }
1651
1652         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1653         dec_ie_call_id(resume->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1654         end_trace();
1655
1656         /* if blocked, release call */
1657         if (p_m_mISDNport->ifport->block)
1658         {
1659                 ret = -27;
1660                 goto reject;
1661         }
1662
1663         /* call id */
1664         if (len<0) len = 0;
1665
1666         /* channel_id (no channel is possible in message) */
1667         exclusive = 0;
1668         channel = -1; /* any channel */
1669
1670         /* hunt channel */
1671         ret = channel = hunt_bchannel(channel, exclusive);
1672         if (ret < 0)
1673                 goto no_channel;
1674
1675         /* open channel */
1676         ret = seize_bchannel(channel, 1);
1677         if (ret < 0)
1678         {
1679                 no_channel:
1680                 reject:
1681                 dmsg = create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT, dinfo, sizeof(RESUME_REJECT_t), p_m_d_ntmode);
1682                 resume_reject = (RESUME_REJECT_t *)(dmsg->data + headerlen);
1683                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RESUME_REJECT | REQUEST, DIRECTION_OUT);
1684                 enc_ie_cause(&resume_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1685                 if (ret == -27)
1686                         add_trace("reason", NULL, "port blocked");
1687                 end_trace();
1688                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1689                 new_state(PORT_STATE_RELEASE);
1690                 p_m_delete = 1;
1691                 return;
1692         }
1693         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
1694
1695         /* create endpoint */
1696         if (p_epointlist)
1697         {
1698                 PERROR("SOFTWARE ERROR: incoming resume but already got an endpoint, exitting...\n");
1699                 exit(-1);
1700         }
1701         ret = -85; /* no call suspended */
1702         epoint = epoint_first;
1703         while(epoint)
1704         {
1705                 if (epoint->ep_park)
1706                 {
1707                         ret = -83; /* suspended call exists, but this not */
1708                         if (epoint->ep_park_len == len)
1709                         if (!memcmp(epoint->ep_park_callid, callid, len))
1710                                 break;
1711                 }
1712                 epoint = epoint->next;
1713         }
1714         if (!epoint)
1715                 goto reject;
1716
1717         if (!(epointlist_new(epoint->ep_serial)))
1718         {
1719                 PERROR("no memory for epointlist\n");
1720                 exit(-1);
1721         }
1722         if (!(epoint->portlist_new(p_serial, p_type, p_m_mISDNport->is_earlyb)))
1723         {
1724                 PERROR("no memory for portlist\n");
1725                 exit(-1);
1726         }
1727         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RESUME);
1728         message_put(message);
1729
1730         /* notify the resume of call */
1731         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1732         message->param.notifyinfo.notify = INFO_NOTIFY_USER_RESUMED;
1733         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1734         message_put(message);
1735
1736         /* sending RESUME_ACKNOWLEDGE */
1737         dmsg = create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE, dinfo, sizeof(RESUME_ACKNOWLEDGE_t), p_m_d_ntmode);
1738         resume_acknowledge = (RESUME_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1739         l1l2l3_trace_header(p_m_mISDNport, this, CC_RESUME_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1740         enc_ie_channel_id(&resume_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1741         end_trace();
1742         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1743
1744         new_state(PORT_STATE_CONNECT);
1745 }
1746
1747
1748 /* CC_FACILITY INDICATION */
1749 void Pdss1::facility_ind(unsigned long prim, unsigned long dinfo, void *data)
1750 {
1751         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1752         FACILITY_t *facility = (FACILITY_t *)((unsigned long)data + headerlen);
1753         unsigned char facil[256];
1754         int facil_len;
1755         struct message *message;
1756
1757         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1758         dec_ie_facility(facility->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), facil, &facil_len);
1759         end_trace();
1760
1761         /* facility */
1762         if (facil_len<=0)
1763                 return;
1764
1765         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_FACILITY);
1766         message->param.facilityinfo.len = facil_len;
1767         memcpy(message->param.facilityinfo.data, facil, facil_len);
1768         message_put(message);
1769 }
1770
1771
1772 /*
1773  * handler for isdn connections
1774  * incoming information are parsed and sent via message to the endpoint
1775  */
1776 void Pdss1::message_isdn(unsigned long prim, unsigned long dinfo, void *data)
1777 {
1778         int new_l3id;
1779         int timer_hex=0;
1780
1781         switch (prim)
1782         {
1783                 case CC_TIMEOUT | INDICATION:
1784                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1785                 if (p_m_d_ntmode)
1786                 {
1787                         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1788                         timer_hex = *((int *)(((char *)data)+headerlen));
1789                 }
1790                 if (timer_hex)
1791                         add_trace("timer", NULL, "%x", timer_hex);
1792                 end_trace();
1793                 if (timer_hex==0x312 && p_m_d_ntmode)
1794                         t312_timeout(prim, dinfo, data);
1795                 break;
1796
1797                 case CC_SETUP | INDICATION:
1798                 if (p_state != PORT_STATE_IDLE)
1799                         break;
1800                 setup_ind(prim, dinfo, data);
1801                 break;
1802
1803                 case CC_SETUP | CONFIRM:
1804                 if (p_m_d_ntmode)
1805                 {
1806                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1807                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1808                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
1809                         if ((p_m_d_l3id&0xff00) != 0xff00)
1810                                 PERROR("    strange setup-procid 0x%x\n", p_m_d_l3id);
1811                         p_m_d_l3id = *((int *)(((u_char *)data)+ mISDNUSER_HEAD_SIZE));
1812                         add_trace("callref", "new", "0x%x", p_m_d_l3id);
1813                         end_trace();
1814                 }
1815                 break;
1816
1817                 case CC_INFORMATION | INDICATION:
1818                 information_ind(prim, dinfo, data);
1819                 break;
1820
1821                 case CC_SETUP_ACKNOWLEDGE | INDICATION:
1822                 if (p_state != PORT_STATE_OUT_SETUP)
1823                 {
1824                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
1825                         break;
1826                 }
1827                 setup_acknowledge_ind(prim, dinfo, data);
1828                 break;
1829
1830                 case CC_PROCEEDING | INDICATION:
1831                 if (p_state != PORT_STATE_OUT_SETUP
1832                  && p_state != PORT_STATE_OUT_OVERLAP)
1833                 {
1834                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
1835                         break;
1836                 }
1837                 proceeding_ind(prim, dinfo, data);
1838                 break;
1839
1840                 case CC_ALERTING | INDICATION:
1841                 if (p_state != PORT_STATE_OUT_SETUP
1842                  && p_state != PORT_STATE_OUT_OVERLAP
1843                  && p_state != PORT_STATE_OUT_PROCEEDING)
1844                 {
1845                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
1846                         break;
1847                 }
1848                 alerting_ind(prim, dinfo, data);
1849                 break;
1850
1851                 case CC_CONNECT | INDICATION:
1852                 if (p_state != PORT_STATE_OUT_SETUP
1853                  && p_state != PORT_STATE_OUT_OVERLAP
1854                  && p_state != PORT_STATE_OUT_PROCEEDING
1855                  && p_state != PORT_STATE_OUT_ALERTING)
1856                 {
1857                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
1858                         break;
1859                 }
1860                 connect_ind(prim, dinfo, data);
1861                 if (p_m_d_notify_pending)
1862                 {
1863                         /* send pending notify message during connect */
1864                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1865                         message_free(p_m_d_notify_pending);
1866                         p_m_d_notify_pending = NULL;
1867                 }
1868                 break;
1869
1870                 case CC_CONNECT_ACKNOWLEDGE | INDICATION:
1871                 case CC_CONNECT | CONFIRM:
1872                 if (p_state == PORT_STATE_CONNECT_WAITING)
1873                         new_state(PORT_STATE_CONNECT);
1874                 if (p_m_d_notify_pending)
1875                 {
1876                         /* send pending notify message during connect-ack */
1877                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1878                         message_free(p_m_d_notify_pending);
1879                         p_m_d_notify_pending = NULL;
1880                 }
1881                 break;
1882
1883                 case CC_DISCONNECT | INDICATION:
1884                 disconnect_ind(prim, dinfo, data);
1885                 break;
1886
1887                 case CC_RELEASE | CONFIRM:
1888                 case CC_RELEASE | INDICATION:
1889                 release_ind(prim, dinfo, data);
1890                 break;
1891
1892                 case CC_RELEASE_COMPLETE | INDICATION:
1893                 release_complete_ind(prim, dinfo, data);
1894                 break;
1895
1896                 case CC_RELEASE_COMPLETE | CONFIRM:
1897                 break;
1898
1899                 case CC_NOTIFY | INDICATION:
1900                 notify_ind(prim, dinfo, data);
1901                 break;
1902
1903                 case CC_HOLD | INDICATION:
1904                 hold_ind(prim, dinfo, data);
1905                 break;
1906
1907                 case CC_RETRIEVE | INDICATION:
1908                 retrieve_ind(prim, dinfo, data);
1909                 break;
1910
1911                 case CC_SUSPEND | INDICATION:
1912                 suspend_ind(prim, dinfo, data);
1913                 break;
1914
1915                 case CC_RESUME | INDICATION:
1916                 resume_ind(prim, dinfo, data);
1917                 break;
1918
1919                 case CC_FACILITY | INDICATION:
1920                 facility_ind(prim, dinfo, data);
1921                 break;
1922
1923                 case CC_RELEASE_CR | INDICATION:
1924                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | INDICATION, DIRECTION_IN);
1925                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
1926                 end_trace();
1927                 if (p_m_d_ntmode)
1928                 {
1929                         if ((p_m_d_l3id&0xff00) == 0xff00)
1930                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1931                 }
1932                 p_m_d_l3id = 0;
1933                 p_m_delete = 1;
1934                 /* sending release to endpoint in case we still have an endpoint
1935                  * NOTE: this only happens if the stack releases due to layer1
1936                  * or layer2 breakdown. otherwhise a release is received first.
1937                  */
1938                 while(p_epointlist)
1939                 {
1940                         struct message *message;
1941                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1942                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
1943                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
1944                         message_put(message);
1945                         /* remove epoint */
1946                         free_epointlist(p_epointlist);
1947
1948                         new_state(PORT_STATE_RELEASE);
1949                 }
1950                 break;
1951
1952                 case CC_NEW_CR | INDICATION:
1953                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1954                 if (p_m_d_l3id)
1955                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1956                 if (p_m_d_ntmode)
1957                 {
1958                         new_l3id = *((int *)(((u_char *)data+mISDNUSER_HEAD_SIZE)));
1959                         if (((new_l3id&0xff00)!=0xff00) && ((p_m_d_l3id&0xff00)==0xff00))
1960                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1961                 } else
1962                 {
1963                         new_l3id = dinfo;
1964                 }
1965                 p_m_d_l3id = new_l3id;
1966                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
1967                 end_trace();
1968                 break;
1969
1970                 default:
1971                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1972                 add_trace("unhandled", "prim", "0x%x", prim);
1973                 end_trace();
1974         }
1975 }
1976
1977 void Pdss1::new_state(int state)
1978 {
1979         class Endpoint *epoint;
1980
1981         /* set timeout */
1982         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1983         if (epoint && p_m_d_ntmode)
1984         {
1985                 if (state == PORT_STATE_IN_OVERLAP)
1986                 {
1987                         p_m_timeout = p_settings.tout_dialing;
1988                         time(&p_m_timer);
1989                 }
1990                 if (state != p_state)
1991                 {
1992                         if (state == PORT_STATE_IN_SETUP
1993                          || state == PORT_STATE_IN_OVERLAP)
1994                         {
1995                                 p_m_timeout = p_settings.tout_setup;
1996                                 time(&p_m_timer);
1997                         }
1998                         if (state == PORT_STATE_IN_PROCEEDING
1999                          || state == PORT_STATE_OUT_PROCEEDING)
2000                         {
2001                                 p_m_timeout = p_settings.tout_proceeding;
2002                                 time(&p_m_timer);
2003                         }
2004                         if (state == PORT_STATE_IN_ALERTING
2005                          || state == PORT_STATE_OUT_ALERTING)
2006                         {
2007                                 p_m_timeout = p_settings.tout_alerting;
2008                                 time(&p_m_timer);
2009                         }
2010                         if (state == PORT_STATE_CONNECT
2011                          || state == PORT_STATE_CONNECT_WAITING)
2012                         {
2013                                 p_m_timeout = 0;
2014                         }
2015                         if (state == PORT_STATE_IN_DISCONNECT
2016                          || state == PORT_STATE_OUT_DISCONNECT)
2017                         {
2018                                 p_m_timeout = p_settings.tout_disconnect;
2019                                 time(&p_m_timer);
2020                         }
2021                 }
2022         }
2023         
2024         Port::new_state(state);
2025 }
2026
2027
2028 /*
2029  * handler
2030  */
2031 int Pdss1::handler(void)
2032 {
2033         int ret;
2034
2035         if ((ret = Port::handler()))
2036                 return(ret);
2037
2038         /* handle destruction */
2039         if (p_m_delete && p_m_d_l3id==0)
2040         {
2041                 delete this;
2042                 return(-1);
2043         }
2044
2045         return(0);
2046 }
2047
2048
2049 /*
2050  * handles all messages from endpoint
2051  */
2052 /* MESSAGE_INFORMATION */
2053 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
2054 {
2055         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2056         INFORMATION_t *information;
2057         msg_t *dmsg;
2058
2059         if (param->information.number[0]) /* only if we have something to dial */
2060         {
2061                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2062                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2063                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2064                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.number);
2065                 end_trace();
2066                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2067         }
2068         new_state(p_state);
2069 }
2070
2071
2072 int newteid = 0;
2073
2074 /* MESSAGE_SETUP */
2075 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2076 {
2077         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2078         INFORMATION_t *information;
2079         SETUP_t *setup;
2080         msg_t *dmsg;
2081         int plan, type, screen, present, reason;
2082         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2083         int channel, exclusive;
2084         int i;
2085         struct epoint_list *epointlist;
2086
2087         /* release if port is blocked */
2088         if (p_m_mISDNport->ifport->block)
2089         {
2090                 struct message *message;
2091
2092                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2093                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2094                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2095                 message_put(message);
2096                 new_state(PORT_STATE_RELEASE);
2097                 p_m_delete = 1;
2098                 return;
2099         }
2100
2101         /* copy setup infos to port */
2102         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2103         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2104         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2105         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2106 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2107
2108         /* only display at connect state: this case happens if endpoint is in connected mode */
2109         if (p_state==PORT_STATE_CONNECT)
2110         {
2111                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2112                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2113                         return;
2114                 if (p_callerinfo.display[0])
2115                 {
2116                         /* sending information */
2117                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2118                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2119                         l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2120                         if (p_m_d_ntmode)
2121                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2122                         end_trace();
2123                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2124                         return;
2125                 }
2126         }
2127
2128         /* attach only if not already */
2129         epointlist = p_epointlist;
2130         while(epointlist)
2131         {
2132                 if (epointlist->epoint_id == epoint_id)
2133                         break;
2134                 epointlist = epointlist->next;
2135         }
2136         if (!epointlist)
2137         {
2138                 if (!(epointlist_new(epoint_id)))
2139                 {
2140                         PERROR("no memory for epointlist\n");
2141                         exit(-1);
2142                 }
2143         }
2144
2145         /* get channel */
2146         exclusive = 0;
2147         if (p_m_b_channel)
2148         {
2149                 channel = p_m_b_channel;
2150                 exclusive = p_m_b_exclusive;
2151         } else
2152                 channel = CHANNEL_ANY;
2153         /* nt-port with no channel, not reserverd */
2154         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2155                 channel = CHANNEL_NO;
2156
2157         /* creating l3id */
2158         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | REQUEST, DIRECTION_OUT);
2159         if (p_m_d_ntmode)
2160         {
2161                 i = 0;
2162                 while(i < 0x100)
2163                 {
2164                         if (p_m_mISDNport->procids[i] == 0)
2165                                 break;
2166                         i++;
2167                 }
2168                 if (i == 0x100)
2169                 {
2170                         struct message *message;
2171
2172                         add_trace("callref", NULL, "no free id");
2173                         end_trace();
2174                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2175                         message->param.disconnectinfo.cause = 47;
2176                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2177                         message_put(message);
2178                         new_state(PORT_STATE_RELEASE);
2179                         p_m_delete = 1;
2180                         return;
2181                 }
2182                 p_m_mISDNport->procids[i] = 1;
2183                 p_m_d_l3id = 0xff00 | i;
2184         } else
2185         {
2186                 iframe_t ncr;
2187                 /* if we are in te-mode, we need to create a process first */
2188                 if (newteid++ > 0x7fff)
2189                         newteid = 0x0001;
2190                 p_m_d_l3id = (entity<<16) | newteid;
2191                 /* preparing message */
2192                 ncr.prim = CC_NEW_CR | REQUEST; 
2193                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2194                 ncr.dinfo = p_m_d_l3id;
2195                 ncr.len = 0;
2196                 /* send message */
2197                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2198 //              if (!dmsg)
2199 //                      goto nomem;
2200         }
2201         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2202         end_trace();
2203
2204         /* preparing setup message */
2205         dmsg = create_l3msg(CC_SETUP | REQUEST, MT_SETUP, p_m_d_l3id, sizeof(SETUP_t), p_m_d_ntmode);
2206         setup = (SETUP_t *)(dmsg->data + headerlen);
2207         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP | REQUEST, DIRECTION_OUT);
2208         /* channel information */
2209         if (channel >= 0) /* it should */
2210         {
2211                 enc_ie_channel_id(&setup->CHANNEL_ID, dmsg, exclusive, channel);
2212         }
2213         /* caller information */
2214         plan = 1;
2215         switch (p_callerinfo.ntype)
2216         {
2217                 case INFO_NTYPE_INTERNATIONAL:
2218                 type = 0x1;
2219                 break;
2220                 case INFO_NTYPE_NATIONAL:
2221                 type = 0x2;
2222                 break;
2223                 case INFO_NTYPE_SUBSCRIBER:
2224                 type = 0x4;
2225                 break;
2226                 default: /* INFO_NTYPE_UNKNOWN */
2227                 type = 0x0;
2228                 break;
2229         }
2230         switch (p_callerinfo.screen)
2231         {
2232                 case INFO_SCREEN_USER:
2233                 screen = 0;
2234                 break;
2235                 default: /* INFO_SCREEN_NETWORK */
2236                 screen = 3;
2237                 break;
2238         }
2239         switch (p_callerinfo.present)
2240         {
2241                 case INFO_PRESENT_RESTRICTED:
2242                 present = 1;
2243                 break;
2244                 case INFO_PRESENT_NOTAVAIL:
2245                 present = 2;
2246                 break;
2247                 default: /* INFO_PRESENT_ALLOWED */
2248                 present = 0;
2249                 break;
2250         }
2251         if (type >= 0)
2252                 enc_ie_calling_pn(&setup->CALLING_PN, dmsg, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2253         /* dialing information */
2254         if (p_dialinginfo.number[0]) /* only if we have something to dial */
2255         {
2256                 enc_ie_called_pn(&setup->CALLED_PN, dmsg, 0, 1, (unsigned char *)p_dialinginfo.number);
2257         }
2258         /* sending complete */
2259         if (p_dialinginfo.sending_complete)
2260                 enc_ie_complete(&setup->COMPLETE, dmsg, 1);
2261         /* sending user-user */
2262         if (param->setup.useruser.len)
2263         {
2264                 enc_ie_useruser(&setup->USER_USER, dmsg, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2265         }
2266         /* redirecting number */
2267         plan = 1;
2268         switch (p_redirinfo.ntype)
2269         {
2270                 case INFO_NTYPE_INTERNATIONAL:
2271                 type = 0x1;
2272                 break;
2273                 case INFO_NTYPE_NATIONAL:
2274                 type = 0x2;
2275                 break;
2276                 case INFO_NTYPE_SUBSCRIBER:
2277                 type = 0x4;
2278                 break;
2279                 default: /* INFO_NTYPE_UNKNOWN */
2280                 type = 0x0;
2281                 break;
2282         }
2283         switch (p_redirinfo.screen)
2284         {
2285                 case INFO_SCREEN_USER:
2286                 screen = 0;
2287                 break;
2288                 default: /* INFO_SCREE_NETWORK */
2289                 screen = 3;
2290                 break;
2291         }
2292         switch (p_redirinfo.reason)
2293         {
2294                 case INFO_REDIR_BUSY:
2295                 reason = 1;
2296                 break;
2297                 case INFO_REDIR_NORESPONSE:
2298                 reason = 2;
2299                 break;
2300                 case INFO_REDIR_UNCONDITIONAL:
2301                 reason = 15;
2302                 break;
2303                 case INFO_REDIR_CALLDEFLECT:
2304                 reason = 10;
2305                 break;
2306                 case INFO_REDIR_OUTOFORDER:
2307                 reason = 9;
2308                 break;
2309                 default: /* INFO_REDIR_UNKNOWN */
2310                 reason = 0;
2311                 break;
2312         }
2313         switch (p_redirinfo.present)
2314         {
2315                 case INFO_PRESENT_NULL: /* no redir at all */
2316                 present = -1;
2317                 screen = -1;
2318                 reason = -1;
2319                 plan = -1;
2320                 type = -1;
2321                 break;
2322                 case INFO_PRESENT_RESTRICTED:
2323                 present = 1;
2324                 break;
2325                 case INFO_PRESENT_NOTAVAIL:
2326                 present = 2;
2327                 break;
2328                 default: /* INFO_PRESENT_ALLOWED */
2329                 present = 0;
2330                 break;
2331         }
2332         /* sending redirecting number only in ntmode */
2333         if (type >= 0 && p_m_d_ntmode)
2334                 enc_ie_redir_nr(&setup->REDIR_NR, dmsg, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2335         /* bearer capability */
2336 //printf("hlc=%d\n",p_capainfo.hlc);
2337         coding = 0;
2338         capability = p_capainfo.bearer_capa;
2339         mode = p_capainfo.bearer_mode;
2340         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2341         switch (p_capainfo.bearer_info1)
2342         {
2343                 case INFO_INFO1_NONE:
2344                 user = -1;
2345                 break;
2346                 default:
2347                 user = p_capainfo.bearer_info1 & 0x7f;
2348                 break;
2349         }
2350         enc_ie_bearer(&setup->BEARER, dmsg, coding, capability, mode, rate, -1, user);
2351         /* hlc */
2352         if (p_capainfo.hlc)
2353         {
2354                 coding = 0;
2355                 interpretation = 4;
2356                 presentation = 1;
2357                 hlc = p_capainfo.hlc & 0x7f;
2358                 exthlc = -1;
2359                 if (p_capainfo.exthlc)
2360                         exthlc = p_capainfo.exthlc & 0x7f;
2361                 enc_ie_hlc(&setup->HLC, dmsg, coding, interpretation, presentation, hlc, exthlc);
2362         }
2363
2364         /* display */
2365         if (p_callerinfo.display[0] && p_m_d_ntmode)
2366                 enc_ie_display(&setup->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2367 #ifdef CENTREX
2368         /* nt-mode: CNIP (calling name identification presentation) */
2369         if (p_callerinfo.name[0] && p_m_d_ntmode)
2370                 enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2371 #endif
2372         end_trace();
2373
2374         /* send setup message now */
2375         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2376
2377         new_state(PORT_STATE_OUT_SETUP);
2378 }
2379
2380 /* MESSAGE_FACILITY */
2381 void Pdss1::message_facility(unsigned long epoint_id, int message_id, union parameter *param)
2382 {
2383         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2384         FACILITY_t *facility;
2385         msg_t *dmsg;
2386
2387         /* facility will not be sent to external lines */
2388         if (!p_m_d_ntmode)
2389                 return;
2390
2391         /* sending facility */
2392         dmsg = create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, p_m_d_l3id, sizeof(FACILITY_t), p_m_d_ntmode);
2393         facility = (FACILITY_t *)(dmsg->data + headerlen);
2394         l1l2l3_trace_header(p_m_mISDNport, this, CC_FACILITY | REQUEST, DIRECTION_OUT);
2395         enc_ie_facility(&facility->FACILITY, dmsg, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2396         end_trace();
2397         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2398 }
2399
2400 /* MESSAGE_NOTIFY */
2401 void Pdss1::message_notify(unsigned long epoint_id, int message_id, union parameter *param)
2402 {
2403         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2404         INFORMATION_t *information;
2405         NOTIFY_t *notification;
2406         int notify;
2407         int plan, type = -1, present;
2408         msg_t *dmsg;
2409
2410         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2411                 notify = param->notifyinfo.notify & 0x7f;
2412         else
2413                 notify = -1;
2414         if (p_state != PORT_STATE_CONNECT)
2415         {
2416                 /* notify only allowed in active state */
2417                 notify = -1;
2418         }
2419         if (notify >= 0)
2420         {
2421                 plan = 1;
2422                 switch (param->notifyinfo.ntype)
2423                 {
2424                         case INFO_NTYPE_INTERNATIONAL:
2425                         type = 1;
2426                         break;
2427                         case INFO_NTYPE_NATIONAL:
2428                         type = 2;
2429                         break;
2430                         case INFO_NTYPE_SUBSCRIBER:
2431                         type = 4;
2432                         break;
2433                         default: /* INFO_NTYPE_UNKNOWN */
2434                         type = 0;
2435                         break;
2436                 }
2437                 switch (param->notifyinfo.present)
2438                 {
2439                         case INFO_PRESENT_NULL: /* no redir at all */
2440                         present = -1;
2441                         plan = -1;
2442                         type = -1;
2443                         break;
2444                         case INFO_PRESENT_RESTRICTED:
2445                         present = 1;
2446                         break;
2447                         case INFO_PRESENT_NOTAVAIL:
2448                         present = 2;
2449                         break;
2450                         default: /* INFO_PRESENT_ALLOWED */
2451                         present = 0;
2452                         break;
2453                 }
2454         }
2455
2456         if (notify<0 && !param->notifyinfo.display[0])
2457         {
2458                 /* nothing to notify, nothing to display */
2459                 return;
2460         }
2461
2462         if (notify >= 0)
2463         {
2464                 if (p_state!=PORT_STATE_CONNECT)
2465                 {
2466                         /* queue notification */
2467                         if (p_m_d_notify_pending)
2468                                 message_free(p_m_d_notify_pending);
2469                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2470                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2471                 } else
2472                 {
2473                         /* sending notification */
2474                         dmsg = create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, p_m_d_l3id, sizeof(NOTIFY_t), p_m_d_ntmode);
2475                         notification = (NOTIFY_t *)(dmsg->data + headerlen);
2476                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NOTIFY | REQUEST, DIRECTION_OUT);
2477                         enc_ie_notify(&notification->NOTIFY, dmsg, notify);
2478                         /* sending redirection number only in ntmode */
2479                         if (type >= 0 && p_m_d_ntmode)
2480                                 enc_ie_redir_dn(&notification->REDIR_DN, dmsg, type, plan, present, (unsigned char *)param->notifyinfo.id);
2481                         if (param->notifyinfo.display[0] && p_m_d_ntmode)
2482                                 enc_ie_display(&notification->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2483                         end_trace();
2484                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2485                 }
2486         } else if (p_m_d_ntmode)
2487         {
2488                 /* sending information */
2489                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2490                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2491                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2492                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2493                 end_trace();
2494                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2495         }
2496 }
2497
2498 /* MESSAGE_OVERLAP */
2499 void Pdss1::message_overlap(unsigned long epoint_id, int message_id, union parameter *param)
2500 {
2501         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2502         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
2503         msg_t *dmsg;
2504
2505         /* sending setup_acknowledge */
2506         dmsg = create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, sizeof(SETUP_ACKNOWLEDGE_t), p_m_d_ntmode);
2507         setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2508         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
2509         /* channel information */
2510         if (p_state == PORT_STATE_IN_SETUP)
2511                 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2512         /* progress information */
2513         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2514          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2515          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2516         if (p_m_mISDNport->is_tones)
2517                 enc_ie_progress(&setup_acknowledge->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2518         end_trace();
2519         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2520
2521         new_state(PORT_STATE_IN_OVERLAP);
2522 }
2523
2524 /* MESSAGE_PROCEEDING */
2525 void Pdss1::message_proceeding(unsigned long epoint_id, int message_id, union parameter *param)
2526 {
2527         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2528         CALL_PROCEEDING_t *proceeding;
2529         msg_t *dmsg;
2530
2531         /* sending proceeding */
2532         dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2533         proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2534         l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2535         /* channel information */
2536         if (p_state == PORT_STATE_IN_SETUP)
2537                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2538         /* progress information */
2539         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2540          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2541          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2542         if (p_m_mISDNport->is_tones)
2543                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2544         end_trace();
2545         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2546
2547         new_state(PORT_STATE_IN_PROCEEDING);
2548 }
2549
2550 /* MESSAGE_ALERTING */
2551 void Pdss1::message_alerting(unsigned long epoint_id, int message_id, union parameter *param)
2552 {
2553         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2554         ALERTING_t *alerting;
2555         msg_t *dmsg;
2556
2557         /* NT-MODE in setup state we must send PROCEEDING first */
2558         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2559         {
2560                 CALL_PROCEEDING_t *proceeding;
2561
2562                 /* sending proceeding */
2563                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2564                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2565                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2566                 /* channel information */
2567                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2568                 /* progress information */
2569                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2570                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2571                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2572                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2573                 end_trace();
2574                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2575                 new_state(PORT_STATE_IN_PROCEEDING);
2576         }
2577
2578         /* sending alerting */
2579         dmsg = create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, p_m_d_l3id, sizeof(ALERTING_t), p_m_d_ntmode);
2580         alerting = (ALERTING_t *)(dmsg->data + headerlen);
2581         l1l2l3_trace_header(p_m_mISDNport, this, CC_ALERTING | REQUEST, DIRECTION_OUT);
2582         /* channel information */
2583         if (p_state == PORT_STATE_IN_SETUP)
2584                 enc_ie_channel_id(&alerting->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2585         /* progress information */
2586         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2587          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2588          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2589         if (p_m_mISDNport->is_tones)
2590                 enc_ie_progress(&alerting->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2591         end_trace();
2592         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2593
2594         new_state(PORT_STATE_IN_ALERTING);
2595 }
2596
2597 /* MESSAGE_CONNECT */
2598 void Pdss1::message_connect(unsigned long epoint_id, int message_id, union parameter *param)
2599 {
2600         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2601         INFORMATION_t *information;
2602         CONNECT_t *connect;
2603         int type, plan, present, screen;
2604         msg_t *dmsg;
2605         class Endpoint *epoint;
2606
2607         /* NT-MODE in setup state we must send PROCEEDING first */
2608         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2609         {
2610                 CALL_PROCEEDING_t *proceeding;
2611
2612                 /* sending proceeding */
2613                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2614                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2615                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2616                 /* channel information */
2617                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2618 //              /* progress information */
2619 //              if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2620 //               || p_capainfo.bearer_capa==INFO_BC_AUDIO
2621 //               || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2622 //              enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2623                 end_trace();
2624                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2625                 new_state(PORT_STATE_IN_PROCEEDING);
2626         }
2627
2628         /* copy connected information */
2629         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2630
2631         /* only display at connect state */
2632         if (p_state == PORT_STATE_CONNECT)
2633         if (p_connectinfo.display[0])
2634         {
2635                 /* sending information */
2636                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2637                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2638                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2639                 if (p_m_d_ntmode)
2640                         enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2641                 end_trace();
2642                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2643                 return;
2644         }
2645
2646         if (p_state!=PORT_STATE_IN_SETUP && p_state!=PORT_STATE_IN_OVERLAP && p_state!=PORT_STATE_IN_PROCEEDING && p_state!=PORT_STATE_IN_ALERTING)
2647         {
2648                 /* connect command only possible in setup, proceeding or alerting state */
2649                 return;
2650         }
2651
2652         /* preparing connect message */
2653         dmsg = create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, p_m_d_l3id, sizeof(CONNECT_t), p_m_d_ntmode);
2654         connect = (CONNECT_t *)(dmsg->data + headerlen);
2655         l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | REQUEST, DIRECTION_OUT);
2656         /* connect information */
2657         plan = 1;
2658         switch (p_connectinfo.ntype)
2659         {
2660                 case INFO_NTYPE_INTERNATIONAL:
2661                 type = 0x1;
2662                 break;
2663                 case INFO_NTYPE_NATIONAL:
2664                 type = 0x2;
2665                 break;
2666                 case INFO_NTYPE_SUBSCRIBER:
2667                 type = 0x4;
2668                 break;
2669                 default: /* INFO_NTYPE_UNKNOWN */
2670                 type = 0x0;
2671                 break;
2672         }
2673         switch (param->connectinfo.screen)
2674         {
2675                 case INFO_SCREEN_USER:
2676                 screen = 0;
2677                 break;
2678                 default: /* INFO_SCREE_NETWORK */
2679                 screen = 3;
2680                 break;
2681         }
2682         switch (p_connectinfo.present)
2683         {
2684                 case INFO_PRESENT_NULL: /* no colp at all */
2685                 present = -1;
2686                 screen = -1;
2687                 plan = -1;
2688                 type = -1;
2689                 break;
2690                 case INFO_PRESENT_RESTRICTED:
2691                 present = 1;
2692                 break;
2693                 case INFO_PRESENT_NOTAVAIL:
2694                 present = 2;
2695                 break;
2696                 default: /* INFO_PRESENT_ALLOWED */
2697                 present = 0;
2698                 break;
2699         }
2700         if (type >= 0)
2701                 enc_ie_connected_pn(&connect->CONNECT_PN, dmsg, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2702         /* display */
2703         if (p_connectinfo.display[0] && p_m_d_ntmode)
2704                 enc_ie_display(&connect->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2705 #ifdef CENTREX
2706         /* nt-mode: CONP (connected name identification presentation) */
2707         if (p_connectinfo.name[0] && p_m_d_ntmode)
2708                 enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2709 #endif
2710         /* date & time */
2711         if (p_m_d_ntmode)
2712         {
2713                 epoint = find_epoint_id(epoint_id);
2714                 enc_ie_date(&connect->DATE, dmsg, now, p_settings.no_seconds);
2715         }
2716         end_trace();
2717         /* finally send message */
2718         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2719
2720         if (p_m_d_ntmode)
2721                 new_state(PORT_STATE_CONNECT);
2722         else
2723                 new_state(PORT_STATE_CONNECT_WAITING);
2724         set_tone("", NULL);
2725 }
2726
2727 /* MESSAGE_DISCONNECT */
2728 void Pdss1::message_disconnect(unsigned long epoint_id, int message_id, union parameter *param)
2729 {
2730         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2731         DISCONNECT_t *disconnect;
2732         RELEASE_COMPLETE_t *release_complete;
2733         msg_t *dmsg;
2734         struct message *message;
2735         char *p = NULL;
2736
2737         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2738         if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->is_tones)
2739          || p_state==PORT_STATE_OUT_SETUP)
2740         {
2741                 /* sending release to endpoint */
2742                 while(p_epointlist)
2743                 {
2744                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2745                         message->param.disconnectinfo.cause = 16;
2746                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2747                         message_put(message);
2748                         /* remove epoint */
2749                         free_epointlist(p_epointlist);
2750                 }
2751                 /* sending release */
2752                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2753                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2754                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
2755                 /* send cause */
2756                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2757                 end_trace();
2758                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2759                 new_state(PORT_STATE_RELEASE);
2760                 p_m_delete = 1;
2761                 return;
2762         }
2763
2764         /* NT-MODE in setup state we must send PROCEEDING first */
2765         if (p_state==PORT_STATE_IN_SETUP)
2766         {
2767                 CALL_PROCEEDING_t *proceeding;
2768
2769                 /* sending proceeding */
2770                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2771                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2772                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2773                 /* channel information */
2774                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2775                 /* progress information */
2776                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2777                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2778                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2779                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2780                 end_trace();
2781                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2782                 new_state(PORT_STATE_IN_PROCEEDING);
2783         }
2784
2785         /* sending disconnect */
2786         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2787         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2788         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2789         /* progress information */
2790         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2791          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2792          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2793         if (p_m_mISDNport->is_tones)
2794                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2795         /* send cause */
2796         enc_ie_cause(&disconnect->CAUSE, dmsg, (p_m_d_ntmode && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2797         /* send display */
2798         if (param->disconnectinfo.display[0])
2799                 p = param->disconnectinfo.display;
2800         if (p) if (*p && p_m_d_ntmode)
2801                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2802         end_trace();
2803         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2804         new_state(PORT_STATE_OUT_DISCONNECT);
2805 }
2806
2807 /* MESSAGE_RELEASE */
2808 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
2809 {
2810         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2811         RELEASE_t *release;
2812         RELEASE_COMPLETE_t *release_complete;
2813         DISCONNECT_t *disconnect;
2814         msg_t *dmsg;
2815         class Endpoint *epoint;
2816         char *p = NULL;
2817
2818         /* if we have incoming disconnected, we may release */
2819         if (p_state==PORT_STATE_IN_DISCONNECT
2820          || p_state==PORT_STATE_OUT_DISCONNECT)
2821         {
2822                 /* sending release */
2823                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
2824                 release = (RELEASE_t *)(dmsg->data + headerlen);
2825                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2826                 /* send cause */
2827                 enc_ie_cause(&release->CAUSE, dmsg, (p_m_d_ntmode && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2828                 end_trace();
2829                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2830                 new_state(PORT_STATE_RELEASE);
2831                 /* remove epoint */
2832                 remove_endpoint:
2833                 free_epointid(epoint_id);
2834                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | REQUEST, DIRECTION_OUT);
2835                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
2836                 end_trace();
2837                 if (p_m_d_ntmode)
2838                 {
2839                         if ((p_m_d_l3id&0xff00) == 0xff00)
2840                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2841                 }
2842                 p_m_d_l3id = 0;
2843                 p_m_delete = 1;
2844                 return;
2845         }
2846         /* if we are on outgoing/incoming call setup, we may release complete */
2847         if (p_state==PORT_STATE_OUT_SETUP
2848          || p_state==PORT_STATE_IN_SETUP
2849 // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
2850          || p_state==PORT_STATE_OUT_PROCEEDING)
2851         {
2852                 /* sending release */
2853                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2854                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2855                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2856                 /* send cause */
2857                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2858                 end_trace();
2859                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2860                 new_state(PORT_STATE_RELEASE);
2861                 goto remove_endpoint;
2862         }
2863
2864         /* NT-MODE in setup state we must send PROCEEDING first */
2865         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2866         {
2867                 CALL_PROCEEDING_t *proceeding;
2868
2869                 /* sending proceeding */
2870                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2871                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2872                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2873                 /* channel information */
2874                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2875                 /* progress information */
2876                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2877                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2878                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2879                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2880                 end_trace();
2881                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2882         }
2883
2884         /* sending disconnect */
2885         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2886         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2887         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2888         /* progress information */
2889         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2890          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2891          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2892         if (p_m_mISDNport->is_tones)
2893                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2894         /* send cause */
2895         enc_ie_cause(&disconnect->CAUSE, dmsg, (p_m_d_ntmode && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2896         /* send display */
2897         epoint = find_epoint_id(epoint_id);
2898         if (param->disconnectinfo.display[0])
2899                 p = param->disconnectinfo.display;
2900         if (p) if (*p && p_m_d_ntmode)
2901                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2902         end_trace();
2903         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2904         new_state(PORT_STATE_RELEASE);
2905         free_epointid(epoint_id);
2906 //      p_m_delete = 1;
2907 }
2908
2909
2910 /*
2911  * endpoint sends messages to the port
2912  */
2913 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
2914 {
2915         struct message *message;
2916
2917         if (PmISDN::message_epoint(epoint_id, message_id, param))
2918                 return(1);
2919
2920         switch(message_id)
2921         {
2922                 case MESSAGE_INFORMATION: /* overlap dialing */
2923                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2924                  && p_state!=PORT_STATE_OUT_OVERLAP
2925                  && p_state!=PORT_STATE_CONNECT
2926                  && p_state!=PORT_STATE_OUT_DISCONNECT
2927                  && p_state!=PORT_STATE_IN_DISCONNECT)
2928                 {
2929                         break;
2930                 }
2931                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2932                  && p_state!=PORT_STATE_OUT_OVERLAP
2933                  && p_state!=PORT_STATE_OUT_PROCEEDING
2934                  && p_state!=PORT_STATE_OUT_ALERTING
2935                  && p_state!=PORT_STATE_CONNECT
2936                  && p_state!=PORT_STATE_OUT_DISCONNECT
2937                  && p_state!=PORT_STATE_IN_DISCONNECT)
2938                 {
2939                         break;
2940                 }
2941                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
2942                  && p_state!=PORT_STATE_IN_OVERLAP
2943                  && p_state!=PORT_STATE_IN_PROCEEDING
2944                  && p_state!=PORT_STATE_IN_ALERTING
2945                  && p_state!=PORT_STATE_CONNECT
2946                  && p_state!=PORT_STATE_CONNECT_WAITING
2947                  && p_state!=PORT_STATE_OUT_DISCONNECT
2948                  && p_state!=PORT_STATE_IN_DISCONNECT)
2949                 {
2950                         break;
2951                 }
2952                 message_information(epoint_id, message_id, param);
2953                 break;
2954
2955                 case MESSAGE_SETUP: /* dial-out command received from epoint */
2956                 if (p_state!=PORT_STATE_IDLE
2957                  && p_state!=PORT_STATE_CONNECT)
2958                 {
2959                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
2960                         break;
2961                 }
2962                 if (p_epointlist && p_state==PORT_STATE_IDLE)
2963                 {
2964                         PERROR("Pdss1(%s): software error: epoint pointer is set in idle state, how bad!! exitting.\n", p_name);
2965                         exit(-1);
2966                 }
2967                 /* note: pri is a special case, because links must be up for pri */ 
2968                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
2969                 {
2970                         /* LAYER 1 is up, or we may send */
2971                         message_setup(epoint_id, message_id, param);
2972                 } else {
2973                         iframe_t act;
2974                         /* LAYER 1 id down, so we queue */
2975                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
2976                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
2977                         /* attach us */
2978                         if (!(epointlist_new(epoint_id)))
2979                         {
2980                                 PERROR("no memory for epointlist\n");
2981                                 exit(-1);
2982                         }
2983                         /* activate link */
2984                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
2985                         act.prim = PH_ACTIVATE | REQUEST; 
2986                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2987                         act.dinfo = 0;
2988                         act.len = 0;
2989                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
2990                         l1l2l3_trace_header(p_m_mISDNport, this, act.prim, DIRECTION_OUT);
2991                         end_trace();
2992 //                      /* set timeout */
2993 //                      p_m_mISDNport->l1timeout = now+3;
2994                 }
2995                 break;
2996
2997                 case MESSAGE_NOTIFY: /* display and notifications */
2998                 message_notify(epoint_id, message_id, param);
2999                 break;
3000
3001                 case MESSAGE_FACILITY: /* facility message */
3002                 message_facility(epoint_id, message_id, param);
3003                 break;
3004
3005                 case MESSAGE_OVERLAP: /* more information is needed */
3006                 if (p_state!=PORT_STATE_IN_SETUP)
3007                 {
3008                         break;
3009                 }
3010                 message_overlap(epoint_id, message_id, param);
3011                 break;
3012
3013                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
3014                 if (p_state!=PORT_STATE_IN_SETUP
3015                  && p_state!=PORT_STATE_IN_OVERLAP)
3016                 {
3017                         break;
3018                 }
3019                 message_proceeding(epoint_id, message_id, param);
3020                 break;
3021
3022                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
3023                 if (p_state!=PORT_STATE_IN_SETUP
3024                  && p_state!=PORT_STATE_IN_OVERLAP
3025                  && p_state!=PORT_STATE_IN_PROCEEDING)
3026                 {
3027                         break;
3028                 }
3029                 message_alerting(epoint_id, message_id, param);
3030                 break;
3031
3032                 case MESSAGE_CONNECT: /* call of endpoint is connected */
3033                 if (p_state!=PORT_STATE_IN_SETUP
3034                  && p_state!=PORT_STATE_IN_OVERLAP
3035                  && p_state!=PORT_STATE_IN_PROCEEDING
3036                  && p_state!=PORT_STATE_IN_ALERTING
3037                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
3038                 {
3039                         break;
3040                 }
3041                 message_connect(epoint_id, message_id, param);
3042                 break;
3043
3044                 case MESSAGE_DISCONNECT: /* call has been disconnected */
3045                 if (p_state!=PORT_STATE_IN_SETUP
3046                  && p_state!=PORT_STATE_IN_OVERLAP
3047                  && p_state!=PORT_STATE_IN_PROCEEDING
3048                  && p_state!=PORT_STATE_IN_ALERTING
3049                  && p_state!=PORT_STATE_OUT_SETUP
3050                  && p_state!=PORT_STATE_OUT_OVERLAP
3051                  && p_state!=PORT_STATE_OUT_PROCEEDING
3052                  && p_state!=PORT_STATE_OUT_ALERTING
3053                  && p_state!=PORT_STATE_CONNECT
3054                  && p_state!=PORT_STATE_CONNECT_WAITING)
3055                 {
3056                         break;
3057                 }
3058                 message_disconnect(epoint_id, message_id, param);
3059                 break;
3060
3061                 case MESSAGE_RELEASE: /* release isdn port */
3062                 if (p_state==PORT_STATE_RELEASE)
3063                 {
3064                         break;
3065                 }
3066                 message_release(epoint_id, message_id, param);
3067                 break;
3068
3069                 default:
3070                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
3071         }
3072
3073         return(1);
3074 }
3075
3076
3077
3078 /*
3079  * data from isdn-stack (layer-3) to pbx (port class)
3080  */
3081 /* NOTE: nt mode use mISDNuser_head_t as header */
3082 int stack2manager_nt(void *dat, void *arg)
3083 {
3084         class Port *port;
3085         class Pdss1 *pdss1;
3086         manager_t *mgr = (manager_t *)dat;
3087         msg_t *msg = (msg_t *)arg;
3088         mISDNuser_head_t *hh;
3089         struct mISDNport *mISDNport;
3090         char name[32];
3091
3092         if (!msg || !mgr)
3093                 return(-EINVAL);
3094
3095         /* note: nst is the first data feld of mISDNport */
3096         mISDNport = (struct mISDNport *)mgr->nst;
3097
3098         hh = (mISDNuser_head_t *)msg->data;
3099         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3100
3101         /* find Port object of type ISDN */
3102         port = port_first;
3103         while(port)
3104         {
3105                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
3106                 {
3107                         pdss1 = (class Pdss1 *)port;
3108 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
3109                         /* check out correct stack */
3110                         if (pdss1->p_m_mISDNport == mISDNport)
3111                         /* check out correct id */
3112                         if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
3113                         {
3114                                 /* found port, the message belongs to */
3115                                 break;
3116                         }
3117                 }
3118                 port = port->next;
3119         }
3120         if (port)
3121         {
3122                 /* if process id is master process, but a child disconnects */
3123                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00 && hh->prim==(CC_DISCONNECT|INDICATION))
3124                 {
3125                         /* send special indication for child disconnect */
3126                         pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
3127                         free_msg(msg);
3128                         return(0);
3129                 }
3130                 /* if process id and layer 3 id matches */
3131                 if (hh->dinfo == pdss1->p_m_d_l3id)
3132                 {
3133                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3134                         free_msg(msg);
3135                         return(0);
3136                 }
3137         }
3138
3139         /* d-message */
3140         switch(hh->prim)
3141         {
3142                 case MGR_SHORTSTATUS | INDICATION:
3143                 case MGR_SHORTSTATUS | CONFIRM:
3144                 switch(hh->dinfo) {
3145                         case SSTATUS_L2_ESTABLISHED:
3146                         goto ss_estab;
3147                         case SSTATUS_L2_RELEASED:
3148                         goto ss_rel;
3149                 }
3150                 break;
3151
3152                 case DL_ESTABLISH | INDICATION:
3153                 case DL_ESTABLISH | CONFIRM:
3154                 ss_estab:
3155                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3156                 add_trace("tei", NULL, "%d", hh->dinfo);
3157                 end_trace();
3158                 if (mISDNport->ptp && hh->dinfo == 0)
3159                 {
3160                         if (mISDNport->l2establish)
3161                         {
3162                                 mISDNport->l2establish = 0;
3163                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
3164                         }
3165                         mISDNport->l2link = 1;
3166                         if (mISDNport->pri);
3167                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
3168                 }
3169                 break;
3170
3171                 case DL_RELEASE | INDICATION:
3172                 case DL_RELEASE | CONFIRM:
3173                 ss_rel:
3174                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3175                 add_trace("tei", NULL, "%d", hh->dinfo);
3176                 end_trace();
3177                 if (mISDNport->ptp && hh->dinfo == 0)
3178                 {
3179                         mISDNport->l2link = 0;
3180                         time(&mISDNport->l2establish);
3181                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
3182                 }
3183                 break;
3184
3185                 case CC_SETUP | INDICATION:
3186                 /* creating port object */
3187                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3188                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3189                 {
3190                         RELEASE_COMPLETE_t *release_complete;
3191                         msg_t *dmsg;
3192
3193                         PERROR("FATAL ERROR: cannot create port object.\n");
3194                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3195                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3196                         l1l2l3_trace_header(mISDNport, NULL, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
3197                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3198                         end_trace();
3199                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3200                         break;
3201                 }
3202                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3203                 break;
3204
3205                 case CC_RESUME | INDICATION:
3206                 /* creating port object */
3207                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3208                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3209                 {
3210                         SUSPEND_REJECT_t *suspend_reject;
3211                         msg_t *dmsg;
3212
3213                         PERROR("FATAL ERROR: cannot create port object.\n");
3214                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, hh->dinfo, sizeof(SUSPEND_REJECT_t), mISDNport->ntmode);
3215                         suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + mISDN_HEADER_LEN);
3216                         l1l2l3_trace_header(mISDNport, NULL, CC_SUSPEND_REJECT | REQUEST, DIRECTION_OUT);
3217                         enc_ie_cause_standalone(mISDNport->ntmode?&suspend_reject->CAUSE:NULL, dmsg, (mISDNport->ntmode)?1:0, 47);
3218                         end_trace();
3219                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3220                         break;
3221                 }
3222                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3223                 break;
3224
3225                 case CC_RELEASE_CR | INDICATION:
3226                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", hh->dinfo);
3227                 break;
3228
3229                 case CC_DISCONNECT | INDICATION:
3230
3231                 // fall throug
3232                 default:
3233                 PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3234                 return(-EINVAL);
3235         }
3236         free_msg(msg);
3237         return(0);
3238 }
3239
3240 /* NOTE: te mode use iframe_t as header */
3241 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
3242 {
3243         class Port *port;
3244         class Pdss1 *pdss1;
3245         iframe_t *frm;
3246         char name[32];
3247
3248         if (!msg || !mISDNport)
3249                 return(-EINVAL);
3250         frm = (iframe_t *)msg->data;
3251         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3252
3253         /* find Port object of type ISDN */
3254         port = port_first;
3255         while(port)
3256         {
3257                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
3258                 {
3259                         pdss1 = (class Pdss1 *)port;
3260                         /* check out correct stack */
3261                         if (pdss1->p_m_mISDNport == mISDNport)
3262                         /* check out correct id */
3263                         if (frm->dinfo == pdss1->p_m_d_l3id)
3264                         {
3265                                 /* found port, the message belongs to */
3266                                 break;
3267                         }
3268                 }
3269                 port = port->next;
3270         }
3271         if (port)
3272         {
3273                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3274                 free_msg(msg);
3275                 return(0);
3276         }
3277
3278         /* process new cr (before setup indication) */
3279 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
3280         if (frm->prim == (CC_NEW_CR | INDICATION))
3281         {
3282
3283                 /* creating port object */
3284                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3285                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3286                 {
3287                         RELEASE_COMPLETE_t *release_complete;
3288                         msg_t *dmsg;
3289
3290                         PERROR("FATAL ERROR: cannot create port object.\n");
3291                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, frm->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3292                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3293                         l1l2l3_trace_header(mISDNport, NULL, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
3294                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3295                         end_trace();
3296                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3297                         free_msg(msg);
3298                         return(0);
3299                 }
3300                 /* l3id will be set from dinfo at message_isdn */
3301                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3302                 free_msg(msg);
3303                 return(0);
3304         }
3305
3306         if (frm->prim == (CC_RELEASE_CR | INDICATION))
3307         {
3308                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
3309                 free_msg(msg);
3310                 return(0);
3311         }
3312         PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3313         return(-EINVAL);
3314 }
3315
3316
3317 /*
3318  * sending message that were queued during L1 activation
3319  * or releasing port if link is down
3320  */
3321 void setup_queue(struct mISDNport *mISDNport, int link)
3322 {
3323         class Port *port;
3324         class Pdss1 *pdss1;
3325         struct message *message;
3326
3327         if (!mISDNport->ntmode)
3328                 return;
3329
3330         /* check all port objects for pending message */
3331         port = port_first;
3332         while(port)
3333         {
3334                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
3335                 {
3336                         pdss1 = (class Pdss1 *)port;
3337                         if (pdss1->p_m_mISDNport == mISDNport)
3338                         {
3339                                 if (pdss1->p_m_d_queue)
3340                                 {
3341                                         if (link)
3342                                         {
3343                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3344                                                 /* LAYER 1 is up, so we send */
3345                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
3346                                                 message_free(pdss1->p_m_d_queue);
3347                                                 pdss1->p_m_d_queue = NULL;
3348                                         } else
3349                                         {
3350                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3351                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
3352                                                 message->param.disconnectinfo.cause = 27;
3353                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3354                                                 message_put(message);
3355                                                 pdss1->new_state(PORT_STATE_RELEASE);
3356                                                 pdss1->p_m_delete = 1;
3357                                         }
3358                                 }
3359                         }
3360                 }
3361                 port = port->next;
3362         }
3363 }
3364
3365
3366
3367
3368