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_EXTENSION:INFO_ITYPE_ISDN;
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", 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.id, sizeof(p_dialinginfo.id));
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.id, (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.id, sizeof(p_dialinginfo.id));
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.id, (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         /* only in nt-mode we send connect ack. in te-mode it is done by stack itself or optional */
1124         if (p_m_d_ntmode)
1125         {
1126                 /* send connect acknowledge */
1127                 dmsg = create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT, dinfo, sizeof(CONNECT_ACKNOWLEDGE_t), p_m_d_ntmode);
1128                 connect_acknowledge = (CONNECT_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1129                 l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | RESPONSE, DIRECTION_OUT);
1130                 /* if we had no bchannel before, we send it now */
1131                 if (!bchannel_before && p_m_b_channel)
1132                         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1133                 end_trace();
1134                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1135         }
1136         
1137         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1138         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
1139         message_put(message);
1140
1141         new_state(PORT_STATE_CONNECT);
1142 }
1143
1144 /* CC_DISCONNECT INDICATION */
1145 void Pdss1::disconnect_ind(unsigned long prim, unsigned long dinfo, void *data)
1146 {
1147         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1148         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1149         int location, cause;
1150         int coding, proglocation, progress;
1151         struct message *message;
1152
1153         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1154         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &proglocation, &progress);
1155         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1156         end_trace();
1157
1158         if (cause < 0)
1159                 cause = 16;
1160
1161         /* release if we are remote sends us no tones */
1162         if (p_m_mISDNport->earlyb)
1163         {
1164                 RELEASE_t *release;
1165                 msg_t *dmsg;
1166
1167                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, dinfo, sizeof(RELEASE_t), p_m_d_ntmode);
1168                 release = (RELEASE_t *)(dmsg->data + headerlen);
1169                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
1170                 enc_ie_cause(&release->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16); /* normal */
1171                 add_trace("reason", NULL, "no remote patterns");
1172                 end_trace();
1173                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1174
1175                 /* sending release to endpoint */
1176                 while(p_epointlist)
1177                 {
1178                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1179                         message->param.disconnectinfo.cause = cause;
1180                         message->param.disconnectinfo.location = location;
1181                         message_put(message);
1182                         /* remove epoint */
1183                         free_epointlist(p_epointlist);
1184                 }
1185                 new_state(PORT_STATE_RELEASE);
1186                 p_m_delete = 1;
1187                 return;
1188         }
1189
1190         /* sending disconnect to active endpoint and release to inactive endpoints */
1191         if (ACTIVE_EPOINT(p_epointlist))
1192         {
1193                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
1194                 message->param.disconnectinfo.location = location;
1195                 message->param.disconnectinfo.cause = cause;
1196                 message_put(message);
1197         }
1198         while(INACTIVE_EPOINT(p_epointlist))
1199         {
1200                 message = message_create(p_serial, INACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1201                 message->param.disconnectinfo.location = location;
1202                 message->param.disconnectinfo.cause = cause;
1203                 message_put(message);
1204                 /* remove epoint */
1205                 free_epointid(INACTIVE_EPOINT(p_epointlist));
1206         }
1207         new_state(PORT_STATE_IN_DISCONNECT);
1208 }
1209
1210 /* CC_DISCONNECT INDICATION */
1211 void Pdss1::disconnect_ind_i(unsigned long prim, unsigned long dinfo, void *data)
1212 {
1213         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1214         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1215         int location, cause;
1216
1217         /* cause */
1218         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1219         if (p_m_d_collect_cause > 0)
1220         {
1221                 add_trace("old-cause", "location", "%d", p_m_d_collect_location);
1222                 add_trace("old-cause", "value", "%d", p_m_d_collect_cause);
1223         }
1224         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1225         end_trace();
1226
1227         /* collect cause */
1228         if (cause == CAUSE_REJECTED) /* call rejected */
1229         {
1230                 p_m_d_collect_cause = CAUSE_REJECTED;
1231                 p_m_d_collect_location = location;
1232         } else
1233         if (cause==CAUSE_NORMAL && p_m_d_collect_cause!=CAUSE_REJECTED) /* reject via hangup */
1234         {
1235                 p_m_d_collect_cause = CAUSE_NORMAL;
1236                 p_m_d_collect_location = location;
1237         } else
1238         if (cause==CAUSE_BUSY && p_m_d_collect_cause!=CAUSE_REJECTED && p_m_d_collect_cause!=CAUSE_NORMAL) /* busy */
1239         {
1240                 p_m_d_collect_cause = CAUSE_BUSY;
1241                 p_m_d_collect_location = location;
1242         } else
1243         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 */
1244         {
1245                 p_m_d_collect_cause = CAUSE_OUTOFORDER;
1246                 p_m_d_collect_location = location;
1247         } else
1248         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 */
1249         {
1250                 p_m_d_collect_cause = cause;
1251                 p_m_d_collect_location = location;
1252         }
1253         add_trace("new-cause", "location", "%d", p_m_d_collect_location);
1254         add_trace("new-cause", "value", "%d", p_m_d_collect_cause);
1255
1256 }
1257
1258 /* CC_RELEASE INDICATION */
1259 void Pdss1::release_ind(unsigned long prim, unsigned long dinfo, void *data)
1260 {
1261         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1262         RELEASE_t *release = (RELEASE_t *)((unsigned long)data + headerlen);
1263         msg_t *dmsg;
1264         int location, cause;
1265         struct message *message;
1266
1267         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1268         dec_ie_cause(release->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1269         end_trace();
1270
1271         if (cause < 0)
1272                 cause = 16;
1273
1274         /* sending release to endpoint */
1275         while(p_epointlist)
1276         {
1277                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1278                 message->param.disconnectinfo.cause = cause;
1279                 message->param.disconnectinfo.location = location;
1280                 message_put(message);
1281                 /* remove epoint */
1282                 free_epointlist(p_epointlist);
1283         }
1284
1285         /* only in NT mode we must send release_complete, if we got a release confirm */
1286         if (prim == (CC_RELEASE | CONFIRM))
1287         {
1288                 /* sending release complete */
1289                 RELEASE_COMPLETE_t *release_complete;
1290
1291                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
1292                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
1293                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
1294                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16);
1295                 end_trace();
1296                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1297         }
1298
1299         new_state(PORT_STATE_RELEASE);
1300         p_m_delete = 1;
1301 }
1302
1303 /* CC_RELEASE_COMPLETE INDICATION */
1304 void Pdss1::release_complete_ind(unsigned long prim, unsigned long dinfo, void *data)
1305 {
1306         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1307         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *)((unsigned long)data + headerlen);
1308         int location, cause;
1309         struct message *message;
1310
1311         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1312         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1313         end_trace();
1314
1315         if (cause < 0)
1316                 cause = 16;
1317
1318         /* sending release to endpoint */
1319         while(p_epointlist)
1320         {
1321                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1322                 message->param.disconnectinfo.cause = cause;
1323                 message->param.disconnectinfo.location = location;
1324                 message_put(message);
1325                 /* remove epoint */
1326                 free_epointlist(p_epointlist);
1327         }
1328
1329         new_state(PORT_STATE_RELEASE);
1330         p_m_delete = 1;
1331 }
1332
1333 /* T312 timeout  */
1334 void Pdss1::t312_timeout(unsigned long prim, unsigned long dinfo, void *data)
1335 {
1336         struct message *message;
1337
1338         // trace is done at message_isdn()
1339         
1340         /* sending release to endpoint */
1341         while(p_epointlist)
1342         {
1343                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1344                 message->param.disconnectinfo.cause = p_m_d_collect_cause;
1345                 message->param.disconnectinfo.location = p_m_d_collect_location;
1346                 message_put(message);
1347                 /* remove epoint */
1348                 free_epointlist(p_epointlist);
1349         }
1350
1351         new_state(PORT_STATE_RELEASE);
1352         p_m_delete = 1;
1353 }
1354
1355 /* CC_NOTIFY INDICATION */
1356 void Pdss1::notify_ind(unsigned long prim, unsigned long dinfo, void *data)
1357 {
1358         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1359         NOTIFY_t *notifying = (NOTIFY_t *)((unsigned long)data + headerlen);
1360         struct message *message;
1361         int notify, type, plan, present;
1362
1363         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1364         dec_ie_notify(notifying->NOTIFY, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
1365         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));
1366         end_trace();
1367
1368         if (!ACTIVE_EPOINT(p_epointlist))
1369                 return;
1370         /* notification indicator */
1371         if (notify < 0)
1372                 return;
1373         notify |= 0x80;
1374         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1375         message->param.notifyinfo.notify = notify;
1376         /* redirection number */
1377         switch (present)
1378         {
1379                 case 1:
1380                 message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1381                 break;
1382                 case 2:
1383                 message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1384                 break;
1385                 default:
1386                 message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1387                 break;
1388         }
1389         switch (type)
1390         {
1391                 case -1:
1392                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1393                 message->param.notifyinfo.present = INFO_PRESENT_NULL;
1394                 break;
1395                 case 1:
1396                 message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1397                 break;
1398                 case 2:
1399                 message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1400                 break;
1401                 case 4:
1402                 message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1403                 break;
1404                 default:
1405                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1406                 break;
1407         }
1408         message->param.notifyinfo.isdn_port = p_m_portnum;
1409         message_put(message);
1410 }
1411
1412
1413 /* CC_HOLD INDICATION */
1414 void Pdss1::hold_ind(unsigned long prim, unsigned long dinfo, void *data)
1415 {
1416         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1417 //      HOLD_t *hold = (HOLD_t *)((unsigned long)data + headerlen);
1418         struct message *message;
1419         HOLD_REJECT_t *hold_reject;
1420         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
1421         msg_t *dmsg;
1422 //      class Endpoint *epoint;
1423
1424         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1425         end_trace();
1426
1427         if (!ACTIVE_EPOINT(p_epointlist) || p_m_hold)
1428         {
1429                 dmsg = create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT, dinfo, sizeof(HOLD_REJECT_t), p_m_d_ntmode);
1430                 hold_reject = (HOLD_REJECT_t *)(dmsg->data + headerlen);
1431                 l1l2l3_trace_header(p_m_mISDNport, this, CC_HOLD_REJECT | REQUEST, DIRECTION_OUT);
1432                 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 */
1433                 add_trace("reason", NULL, "no endpoint");
1434                 end_trace();
1435                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1436
1437                 return;
1438         }
1439
1440         /* notify the hold of call */
1441         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1442         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1443         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1444         message_put(message);
1445
1446         /* deactivate bchannel */
1447         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (hold)", DIRECTION_NONE);
1448         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1449         end_trace();
1450         drop_bchannel();
1451
1452         /* set hold state */
1453         p_m_hold = 1;
1454 #if 0
1455         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1456         if (epoint && p_m_d_ntmode)
1457         {
1458                 p_m_timeout = p_settings.tout_hold;
1459                 time(&p_m_timer);
1460         }
1461 #endif
1462
1463         /* acknowledge hold */
1464         dmsg = create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE, dinfo, sizeof(HOLD_ACKNOWLEDGE_t), p_m_d_ntmode);
1465         hold_acknowledge = (HOLD_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1466         l1l2l3_trace_header(p_m_mISDNport, this, CC_HOLD_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1467         end_trace();
1468         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1469 }
1470
1471
1472 /* CC_RETRIEVE INDICATION */
1473 void Pdss1::retrieve_ind(unsigned long prim, unsigned long dinfo, void *data)
1474 {
1475         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1476         RETRIEVE_t *retrieve = (RETRIEVE_t *)((unsigned long)data + headerlen);
1477         RETRIEVE_REJECT_t *retrieve_reject;
1478         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1479         struct message *message;
1480         int channel, exclusive, cause;
1481         msg_t *dmsg;
1482         int ret;
1483
1484         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1485         dec_ie_channel_id(retrieve->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1486         end_trace();
1487
1488         if (!p_m_hold)
1489         {
1490                 cause = 101; /* incompatible state */
1491                 reject:
1492
1493                 dmsg = create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT, dinfo, sizeof(RETRIEVE_REJECT_t), p_m_d_ntmode);
1494                 retrieve_reject = (RETRIEVE_REJECT_t *)(dmsg->data + headerlen);
1495                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RETRIEVE_REJECT | REQUEST, DIRECTION_OUT);
1496                 enc_ie_cause(&retrieve_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1497                 end_trace();
1498                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1499
1500                 return;
1501         }
1502
1503         /* notify the retrieve of call */
1504         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1505         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1506         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1507         message_put(message);
1508
1509         /* hunt channel */
1510         ret = channel = hunt_bchannel(channel, exclusive);
1511         if (ret < 0)
1512                 goto no_channel;
1513
1514         /* open channel */
1515         ret = seize_bchannel(channel, 1);
1516         if (ret < 0)
1517         {
1518                 no_channel:
1519                 cause = -ret;
1520                 goto reject;
1521         }
1522         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
1523
1524         /* set hold state */
1525         p_m_hold = 0;
1526         p_m_timeout = 0;
1527
1528         /* acknowledge retrieve */
1529         dmsg = create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE, dinfo, sizeof(RETRIEVE_ACKNOWLEDGE_t), p_m_d_ntmode);
1530         retrieve_acknowledge = (RETRIEVE_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1531         l1l2l3_trace_header(p_m_mISDNport, this, CC_RETRIEVE_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1532         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1533         end_trace();
1534         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1535 }
1536
1537 /* CC_SUSPEND INDICATION */
1538 void Pdss1::suspend_ind(unsigned long prim, unsigned long dinfo, void *data)
1539 {
1540         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1541         SUSPEND_t *suspend = (SUSPEND_t *)((unsigned long)data + headerlen);
1542         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
1543         SUSPEND_REJECT_t *suspend_reject;
1544         struct message *message;
1545         class Endpoint *epoint;
1546         unsigned char callid[8];
1547         int len;
1548         msg_t *dmsg;
1549         int ret = -31; /* normal, unspecified */
1550
1551         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1552         dec_ie_call_id(suspend->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1553         end_trace();
1554
1555         if (!ACTIVE_EPOINT(p_epointlist))
1556         {
1557                 reject:
1558                 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, dinfo, sizeof(SUSPEND_REJECT_t), p_m_d_ntmode);
1559                 suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + headerlen);
1560                 l1l2l3_trace_header(p_m_mISDNport, this, CC_SUSPEND_REJECT | REQUEST, DIRECTION_OUT);
1561                 enc_ie_cause(&suspend_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1562                 end_trace();
1563                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1564
1565                 return;
1566         }
1567
1568         /* call id */
1569         if (len<0) len = 0;
1570
1571         /* check if call id is in use */
1572         epoint = epoint_first;
1573         while(epoint)
1574         {
1575                 if (epoint->ep_park)
1576                 {
1577                         if (epoint->ep_park_len == len)
1578                         if (!memcmp(epoint->ep_park_callid, callid, len))
1579                         {
1580                                 ret = -84; /* call id in use */
1581                                 goto reject;
1582                         }
1583                 }
1584                 epoint = epoint->next;
1585         }
1586
1587         /* notify the hold of call */
1588         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1589         message->param.notifyinfo.notify = INFO_NOTIFY_USER_SUSPENDED;
1590         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1591         message_put(message);
1592
1593         /* deactivate bchannel */
1594         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (suspend)", DIRECTION_NONE);
1595         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1596         end_trace();
1597         drop_bchannel();
1598
1599         /* sending suspend to endpoint */
1600         while (p_epointlist)
1601         {
1602                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_SUSPEND);
1603                 memcpy(message->param.parkinfo.callid, callid, sizeof(message->param.parkinfo.callid));
1604                 message->param.parkinfo.len = len;
1605                 message_put(message);
1606                 /* remove epoint */
1607                 free_epointlist(p_epointlist);
1608         }
1609
1610         /* sending SUSPEND_ACKNOWLEDGE */
1611         dmsg = create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE, dinfo, sizeof(SUSPEND_ACKNOWLEDGE_t), p_m_d_ntmode);
1612         suspend_acknowledge = (SUSPEND_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1613         l1l2l3_trace_header(p_m_mISDNport, this, CC_SUSPEND_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1614         end_trace();
1615         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1616
1617         new_state(PORT_STATE_RELEASE);
1618         p_m_delete = 1;
1619 }
1620
1621 /* CC_RESUME INDICATION */
1622 void Pdss1::resume_ind(unsigned long prim, unsigned long dinfo, void *data)
1623 {
1624         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1625         RESUME_t *resume = (RESUME_t *)((unsigned long)data + headerlen);
1626         RESUME_REJECT_t *resume_reject;
1627         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
1628         unsigned char callid[8];
1629         int len;
1630         int channel, exclusive;
1631         msg_t *dmsg;
1632         class Endpoint *epoint;
1633         struct message *message;
1634         int ret;
1635
1636         /* callref from nt-lib */
1637         if (p_m_d_ntmode)
1638         {
1639                 /* nt-library now gives us the id via CC_RESUME */
1640                 if (dinfo&(~0xff) == 0xff00)
1641                 {
1642                         PERROR("fatal software error: l3-stack gives us a process id 0xff00-0xffff\n");
1643                         exit(-1);
1644                 }
1645                 l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1646                 if (p_m_d_l3id)
1647                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1648                 add_trace("callref", "new", "0x%x", dinfo);
1649                 end_trace();
1650                 if (p_m_d_l3id&(~0xff) == 0xff00)
1651                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1652                 p_m_d_l3id = dinfo;
1653                 p_m_d_ces = resume->ces;
1654         }
1655
1656         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1657         dec_ie_call_id(resume->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1658         end_trace();
1659
1660         /* if blocked, release call */
1661         if (p_m_mISDNport->ifport->block)
1662         {
1663                 ret = -27;
1664                 goto reject;
1665         }
1666
1667         /* call id */
1668         if (len<0) len = 0;
1669
1670         /* channel_id (no channel is possible in message) */
1671         exclusive = 0;
1672         channel = -1; /* any channel */
1673
1674         /* hunt channel */
1675         ret = channel = hunt_bchannel(channel, exclusive);
1676         if (ret < 0)
1677                 goto no_channel;
1678
1679         /* open channel */
1680         ret = seize_bchannel(channel, 1);
1681         if (ret < 0)
1682         {
1683                 no_channel:
1684                 reject:
1685                 dmsg = create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT, dinfo, sizeof(RESUME_REJECT_t), p_m_d_ntmode);
1686                 resume_reject = (RESUME_REJECT_t *)(dmsg->data + headerlen);
1687                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RESUME_REJECT | REQUEST, DIRECTION_OUT);
1688                 enc_ie_cause(&resume_reject->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1689                 if (ret == -27)
1690                         add_trace("reason", NULL, "port blocked");
1691                 end_trace();
1692                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1693                 new_state(PORT_STATE_RELEASE);
1694                 p_m_delete = 1;
1695                 return;
1696         }
1697         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
1698
1699         /* create endpoint */
1700         if (p_epointlist)
1701         {
1702                 PERROR("SOFTWARE ERROR: incoming resume but already got an endpoint, exitting...\n");
1703                 exit(-1);
1704         }
1705         ret = -85; /* no call suspended */
1706         epoint = epoint_first;
1707         while(epoint)
1708         {
1709                 if (epoint->ep_park)
1710                 {
1711                         ret = -83; /* suspended call exists, but this not */
1712                         if (epoint->ep_park_len == len)
1713                         if (!memcmp(epoint->ep_park_callid, callid, len))
1714                                 break;
1715                 }
1716                 epoint = epoint->next;
1717         }
1718         if (!epoint)
1719                 goto reject;
1720
1721         if (!(epointlist_new(epoint->ep_serial)))
1722         {
1723                 PERROR("no memory for epointlist\n");
1724                 exit(-1);
1725         }
1726         if (!(epoint->portlist_new(p_serial, p_type, p_m_mISDNport->earlyb)))
1727         {
1728                 PERROR("no memory for portlist\n");
1729                 exit(-1);
1730         }
1731         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RESUME);
1732         message_put(message);
1733
1734         /* notify the resume of call */
1735         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1736         message->param.notifyinfo.notify = INFO_NOTIFY_USER_RESUMED;
1737         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1738         message_put(message);
1739
1740         /* sending RESUME_ACKNOWLEDGE */
1741         dmsg = create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE, dinfo, sizeof(RESUME_ACKNOWLEDGE_t), p_m_d_ntmode);
1742         resume_acknowledge = (RESUME_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1743         l1l2l3_trace_header(p_m_mISDNport, this, CC_RESUME_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1744         enc_ie_channel_id(&resume_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1745         end_trace();
1746         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1747
1748         new_state(PORT_STATE_CONNECT);
1749 }
1750
1751
1752 /* CC_FACILITY INDICATION */
1753 void Pdss1::facility_ind(unsigned long prim, unsigned long dinfo, void *data)
1754 {
1755         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1756         FACILITY_t *facility = (FACILITY_t *)((unsigned long)data + headerlen);
1757         unsigned char facil[256];
1758         int facil_len;
1759         struct message *message;
1760
1761         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1762         dec_ie_facility(facility->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), facil, &facil_len);
1763         end_trace();
1764
1765         /* facility */
1766         if (facil_len<=0)
1767                 return;
1768
1769         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_FACILITY);
1770         message->param.facilityinfo.len = facil_len;
1771         memcpy(message->param.facilityinfo.data, facil, facil_len);
1772         message_put(message);
1773 }
1774
1775
1776 /*
1777  * handler for isdn connections
1778  * incoming information are parsed and sent via message to the endpoint
1779  */
1780 void Pdss1::message_isdn(unsigned long prim, unsigned long dinfo, void *data)
1781 {
1782         int new_l3id;
1783         int timer_hex=0;
1784
1785         switch (prim)
1786         {
1787                 case CC_TIMEOUT | INDICATION:
1788                 if (p_m_d_ntmode)
1789                 {
1790                         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1791                         timer_hex = *((int *)(((char *)data)+headerlen));
1792                 }
1793                 if (timer_hex==0x312 && p_m_d_ntmode)
1794                 {
1795                         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1796                         add_trace("timer", NULL, "%x", timer_hex);
1797                         end_trace();
1798                         t312_timeout(prim, dinfo, data);
1799                 }
1800                 break;
1801
1802                 case CC_SETUP | INDICATION:
1803                 if (p_state != PORT_STATE_IDLE)
1804                         break;
1805                 setup_ind(prim, dinfo, data);
1806                 break;
1807
1808                 case CC_SETUP | CONFIRM:
1809                 if (p_m_d_ntmode)
1810                 {
1811                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1812                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1813                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
1814                         if ((p_m_d_l3id&0xff00) != 0xff00)
1815                                 PERROR("    strange setup-procid 0x%x\n", p_m_d_l3id);
1816                         p_m_d_l3id = *((int *)(((u_char *)data)+ mISDNUSER_HEAD_SIZE));
1817                         add_trace("callref", "new", "0x%x", p_m_d_l3id);
1818                         end_trace();
1819                 }
1820                 break;
1821
1822                 case CC_INFORMATION | INDICATION:
1823                 information_ind(prim, dinfo, data);
1824                 break;
1825
1826                 case CC_SETUP_ACKNOWLEDGE | INDICATION:
1827                 if (p_state != PORT_STATE_OUT_SETUP)
1828                 {
1829                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
1830                         break;
1831                 }
1832                 setup_acknowledge_ind(prim, dinfo, data);
1833                 break;
1834
1835                 case CC_PROCEEDING | INDICATION:
1836                 if (p_state != PORT_STATE_OUT_SETUP
1837                  && p_state != PORT_STATE_OUT_OVERLAP)
1838                 {
1839                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
1840                         break;
1841                 }
1842                 proceeding_ind(prim, dinfo, data);
1843                 break;
1844
1845                 case CC_ALERTING | INDICATION:
1846                 if (p_state != PORT_STATE_OUT_SETUP
1847                  && p_state != PORT_STATE_OUT_OVERLAP
1848                  && p_state != PORT_STATE_OUT_PROCEEDING)
1849                 {
1850                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
1851                         break;
1852                 }
1853                 alerting_ind(prim, dinfo, data);
1854                 break;
1855
1856                 case CC_CONNECT | INDICATION:
1857                 if (p_state != PORT_STATE_OUT_SETUP
1858                  && p_state != PORT_STATE_OUT_OVERLAP
1859                  && p_state != PORT_STATE_OUT_PROCEEDING
1860                  && p_state != PORT_STATE_OUT_ALERTING)
1861                 {
1862                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
1863                         break;
1864                 }
1865                 connect_ind(prim, dinfo, data);
1866                 if (p_m_d_notify_pending)
1867                 {
1868                         /* send pending notify message during connect */
1869                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1870                         message_free(p_m_d_notify_pending);
1871                         p_m_d_notify_pending = NULL;
1872                 }
1873                 break;
1874
1875                 case CC_CONNECT_ACKNOWLEDGE | INDICATION:
1876                 case CC_CONNECT | CONFIRM:
1877                 if (p_state == PORT_STATE_CONNECT_WAITING)
1878                         new_state(PORT_STATE_CONNECT);
1879                 if (p_m_d_notify_pending)
1880                 {
1881                         /* send pending notify message during connect-ack */
1882                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1883                         message_free(p_m_d_notify_pending);
1884                         p_m_d_notify_pending = NULL;
1885                 }
1886                 break;
1887
1888                 case CC_DISCONNECT | INDICATION:
1889                 disconnect_ind(prim, dinfo, data);
1890                 break;
1891
1892                 case CC_RELEASE | CONFIRM:
1893                 case CC_RELEASE | INDICATION:
1894                 release_ind(prim, dinfo, data);
1895                 break;
1896
1897                 case CC_RELEASE_COMPLETE | INDICATION:
1898                 release_complete_ind(prim, dinfo, data);
1899                 break;
1900
1901                 case CC_RELEASE_COMPLETE | CONFIRM:
1902                 break;
1903
1904                 case CC_NOTIFY | INDICATION:
1905                 notify_ind(prim, dinfo, data);
1906                 break;
1907
1908                 case CC_HOLD | INDICATION:
1909                 hold_ind(prim, dinfo, data);
1910                 break;
1911
1912                 case CC_RETRIEVE | INDICATION:
1913                 retrieve_ind(prim, dinfo, data);
1914                 break;
1915
1916                 case CC_SUSPEND | INDICATION:
1917                 suspend_ind(prim, dinfo, data);
1918                 break;
1919
1920                 case CC_RESUME | INDICATION:
1921                 resume_ind(prim, dinfo, data);
1922                 break;
1923
1924                 case CC_FACILITY | INDICATION:
1925                 facility_ind(prim, dinfo, data);
1926                 break;
1927
1928                 case CC_RELEASE_CR | INDICATION:
1929                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | INDICATION, DIRECTION_IN);
1930                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
1931                 end_trace();
1932                 if (p_m_d_ntmode)
1933                 {
1934                         if ((p_m_d_l3id&0xff00) == 0xff00)
1935                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1936                 }
1937                 p_m_d_l3id = 0;
1938                 p_m_delete = 1;
1939                 /* sending release to endpoint in case we still have an endpoint
1940                  * NOTE: this only happens if the stack releases due to layer1
1941                  * or layer2 breakdown. otherwhise a release is received first.
1942                  */
1943                 while(p_epointlist)
1944                 {
1945                         struct message *message;
1946                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1947                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
1948                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
1949                         message_put(message);
1950                         /* remove epoint */
1951                         free_epointlist(p_epointlist);
1952
1953                         new_state(PORT_STATE_RELEASE);
1954                 }
1955                 break;
1956
1957                 case CC_NEW_CR | INDICATION:
1958                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1959                 if (p_m_d_l3id)
1960                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1961                 if (p_m_d_ntmode)
1962                 {
1963                         new_l3id = *((int *)(((u_char *)data+mISDNUSER_HEAD_SIZE)));
1964                         if (((new_l3id&0xff00)!=0xff00) && ((p_m_d_l3id&0xff00)==0xff00))
1965                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1966                 } else
1967                 {
1968                         new_l3id = dinfo;
1969                 }
1970                 p_m_d_l3id = new_l3id;
1971                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
1972                 end_trace();
1973                 break;
1974
1975                 default:
1976                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1977                 add_trace("unhandled", "prim", "0x%x", prim);
1978                 end_trace();
1979         }
1980 }
1981
1982 void Pdss1::new_state(int state)
1983 {
1984         class Endpoint *epoint;
1985
1986         /* set timeout */
1987         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1988         if (epoint && p_m_d_ntmode)
1989         {
1990                 if (state == PORT_STATE_IN_OVERLAP)
1991                 {
1992                         p_m_timeout = p_settings.tout_dialing;
1993                         time(&p_m_timer);
1994                 }
1995                 if (state != p_state)
1996                 {
1997                         if (state == PORT_STATE_IN_SETUP
1998                          || state == PORT_STATE_IN_OVERLAP)
1999                         {
2000                                 p_m_timeout = p_settings.tout_setup;
2001                                 time(&p_m_timer);
2002                         }
2003                         if (state == PORT_STATE_IN_PROCEEDING
2004                          || state == PORT_STATE_OUT_PROCEEDING)
2005                         {
2006                                 p_m_timeout = p_settings.tout_proceeding;
2007                                 time(&p_m_timer);
2008                         }
2009                         if (state == PORT_STATE_IN_ALERTING
2010                          || state == PORT_STATE_OUT_ALERTING)
2011                         {
2012                                 p_m_timeout = p_settings.tout_alerting;
2013                                 time(&p_m_timer);
2014                         }
2015                         if (state == PORT_STATE_CONNECT
2016                          || state == PORT_STATE_CONNECT_WAITING)
2017                         {
2018                                 p_m_timeout = 0;
2019                         }
2020                         if (state == PORT_STATE_IN_DISCONNECT
2021                          || state == PORT_STATE_OUT_DISCONNECT)
2022                         {
2023                                 p_m_timeout = p_settings.tout_disconnect;
2024                                 time(&p_m_timer);
2025                         }
2026                 }
2027         }
2028         
2029         Port::new_state(state);
2030 }
2031
2032
2033 /*
2034  * handler
2035  */
2036 int Pdss1::handler(void)
2037 {
2038         int ret;
2039
2040         if ((ret = PmISDN::handler()))
2041                 return(ret);
2042
2043         /* handle destruction */
2044         if (p_m_delete && p_m_d_l3id==0)
2045         {
2046                 delete this;
2047                 return(-1);
2048         }
2049
2050         return(0);
2051 }
2052
2053
2054 /*
2055  * handles all messages from endpoint
2056  */
2057 /* MESSAGE_INFORMATION */
2058 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
2059 {
2060         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2061         INFORMATION_t *information;
2062         msg_t *dmsg;
2063
2064         if (param->information.id[0]) /* only if we have something to dial */
2065         {
2066                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2067                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2068                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2069                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.id);
2070                 end_trace();
2071                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2072         }
2073         new_state(p_state);
2074 }
2075
2076
2077 int newteid = 0;
2078
2079 /* MESSAGE_SETUP */
2080 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2081 {
2082         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2083         INFORMATION_t *information;
2084         SETUP_t *setup;
2085         msg_t *dmsg;
2086         int plan, type, screen, present, reason;
2087         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2088         int channel, exclusive;
2089         int i;
2090         struct epoint_list *epointlist;
2091
2092         /* release if port is blocked */
2093         if (p_m_mISDNport->ifport->block)
2094         {
2095                 struct message *message;
2096
2097                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2098                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2099                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2100                 message_put(message);
2101                 new_state(PORT_STATE_RELEASE);
2102                 p_m_delete = 1;
2103                 return;
2104         }
2105
2106         /* copy setup infos to port */
2107         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2108         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2109         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2110         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2111 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2112
2113         /* only display at connect state: this case happens if endpoint is in connected mode */
2114         if (p_state==PORT_STATE_CONNECT)
2115         {
2116                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2117                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2118                         return;
2119                 if (p_callerinfo.display[0])
2120                 {
2121                         /* sending information */
2122                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2123                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2124                         l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2125                         if (p_m_d_ntmode)
2126                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2127                         end_trace();
2128                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2129                         return;
2130                 }
2131         }
2132
2133         /* attach only if not already */
2134         epointlist = p_epointlist;
2135         while(epointlist)
2136         {
2137                 if (epointlist->epoint_id == epoint_id)
2138                         break;
2139                 epointlist = epointlist->next;
2140         }
2141         if (!epointlist)
2142         {
2143                 if (!(epointlist_new(epoint_id)))
2144                 {
2145                         PERROR("no memory for epointlist\n");
2146                         exit(-1);
2147                 }
2148         }
2149
2150         /* get channel */
2151         exclusive = 0;
2152         if (p_m_b_channel)
2153         {
2154                 channel = p_m_b_channel;
2155                 exclusive = p_m_b_exclusive;
2156         } else
2157                 channel = CHANNEL_ANY;
2158         /* nt-port with no channel, not reserverd */
2159         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2160                 channel = CHANNEL_NO;
2161
2162         /* creating l3id */
2163         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | REQUEST, DIRECTION_OUT);
2164         if (p_m_d_ntmode)
2165         {
2166                 i = 0;
2167                 while(i < 0x100)
2168                 {
2169                         if (p_m_mISDNport->procids[i] == 0)
2170                                 break;
2171                         i++;
2172                 }
2173                 if (i == 0x100)
2174                 {
2175                         struct message *message;
2176
2177                         add_trace("callref", NULL, "no free id");
2178                         end_trace();
2179                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2180                         message->param.disconnectinfo.cause = 47;
2181                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2182                         message_put(message);
2183                         new_state(PORT_STATE_RELEASE);
2184                         p_m_delete = 1;
2185                         return;
2186                 }
2187                 p_m_mISDNport->procids[i] = 1;
2188                 p_m_d_l3id = 0xff00 | i;
2189         } else
2190         {
2191                 iframe_t ncr;
2192                 /* if we are in te-mode, we need to create a process first */
2193                 if (newteid++ > 0x7fff)
2194                         newteid = 0x0001;
2195                 p_m_d_l3id = (entity<<16) | newteid;
2196                 /* preparing message */
2197                 ncr.prim = CC_NEW_CR | REQUEST; 
2198                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2199                 ncr.dinfo = p_m_d_l3id;
2200                 ncr.len = 0;
2201                 /* send message */
2202                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2203 //              if (!dmsg)
2204 //                      goto nomem;
2205         }
2206         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2207         end_trace();
2208
2209         /* preparing setup message */
2210         dmsg = create_l3msg(CC_SETUP | REQUEST, MT_SETUP, p_m_d_l3id, sizeof(SETUP_t), p_m_d_ntmode);
2211         setup = (SETUP_t *)(dmsg->data + headerlen);
2212         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP | REQUEST, DIRECTION_OUT);
2213         /* channel information */
2214         if (channel >= 0) /* it should */
2215         {
2216                 enc_ie_channel_id(&setup->CHANNEL_ID, dmsg, exclusive, channel);
2217         }
2218         /* caller information */
2219         plan = 1;
2220         switch (p_callerinfo.ntype)
2221         {
2222                 case INFO_NTYPE_INTERNATIONAL:
2223                 type = 0x1;
2224                 break;
2225                 case INFO_NTYPE_NATIONAL:
2226                 type = 0x2;
2227                 break;
2228                 case INFO_NTYPE_SUBSCRIBER:
2229                 type = 0x4;
2230                 break;
2231                 default: /* INFO_NTYPE_UNKNOWN */
2232                 type = 0x0;
2233                 break;
2234         }
2235         switch (p_callerinfo.screen)
2236         {
2237                 case INFO_SCREEN_USER:
2238                 screen = 0;
2239                 break;
2240                 default: /* INFO_SCREEN_NETWORK */
2241                 screen = 3;
2242                 break;
2243         }
2244         switch (p_callerinfo.present)
2245         {
2246                 case INFO_PRESENT_RESTRICTED:
2247                 present = 1;
2248                 break;
2249                 case INFO_PRESENT_NOTAVAIL:
2250                 present = 2;
2251                 break;
2252                 default: /* INFO_PRESENT_ALLOWED */
2253                 present = 0;
2254                 break;
2255         }
2256         if (type >= 0)
2257                 enc_ie_calling_pn(&setup->CALLING_PN, dmsg, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2258         /* dialing information */
2259         if (p_dialinginfo.id[0]) /* only if we have something to dial */
2260         {
2261                 enc_ie_called_pn(&setup->CALLED_PN, dmsg, 0, 1, (unsigned char *)p_dialinginfo.id);
2262         }
2263         /* sending complete */
2264         if (p_dialinginfo.sending_complete)
2265                 enc_ie_complete(&setup->COMPLETE, dmsg, 1);
2266         /* sending user-user */
2267         if (param->setup.useruser.len)
2268         {
2269                 enc_ie_useruser(&setup->USER_USER, dmsg, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2270         }
2271         /* redirecting number */
2272         plan = 1;
2273         switch (p_redirinfo.ntype)
2274         {
2275                 case INFO_NTYPE_INTERNATIONAL:
2276                 type = 0x1;
2277                 break;
2278                 case INFO_NTYPE_NATIONAL:
2279                 type = 0x2;
2280                 break;
2281                 case INFO_NTYPE_SUBSCRIBER:
2282                 type = 0x4;
2283                 break;
2284                 default: /* INFO_NTYPE_UNKNOWN */
2285                 type = 0x0;
2286                 break;
2287         }
2288         switch (p_redirinfo.screen)
2289         {
2290                 case INFO_SCREEN_USER:
2291                 screen = 0;
2292                 break;
2293                 default: /* INFO_SCREE_NETWORK */
2294                 screen = 3;
2295                 break;
2296         }
2297         switch (p_redirinfo.reason)
2298         {
2299                 case INFO_REDIR_BUSY:
2300                 reason = 1;
2301                 break;
2302                 case INFO_REDIR_NORESPONSE:
2303                 reason = 2;
2304                 break;
2305                 case INFO_REDIR_UNCONDITIONAL:
2306                 reason = 15;
2307                 break;
2308                 case INFO_REDIR_CALLDEFLECT:
2309                 reason = 10;
2310                 break;
2311                 case INFO_REDIR_OUTOFORDER:
2312                 reason = 9;
2313                 break;
2314                 default: /* INFO_REDIR_UNKNOWN */
2315                 reason = 0;
2316                 break;
2317         }
2318         switch (p_redirinfo.present)
2319         {
2320                 case INFO_PRESENT_NULL: /* no redir at all */
2321                 present = -1;
2322                 screen = -1;
2323                 reason = -1;
2324                 plan = -1;
2325                 type = -1;
2326                 break;
2327                 case INFO_PRESENT_RESTRICTED:
2328                 present = 1;
2329                 break;
2330                 case INFO_PRESENT_NOTAVAIL:
2331                 present = 2;
2332                 break;
2333                 default: /* INFO_PRESENT_ALLOWED */
2334                 present = 0;
2335                 break;
2336         }
2337         /* sending redirecting number only in ntmode */
2338         if (type >= 0 && p_m_d_ntmode)
2339                 enc_ie_redir_nr(&setup->REDIR_NR, dmsg, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2340         /* bearer capability */
2341 //printf("hlc=%d\n",p_capainfo.hlc);
2342         coding = 0;
2343         capability = p_capainfo.bearer_capa;
2344         mode = p_capainfo.bearer_mode;
2345         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2346         switch (p_capainfo.bearer_info1)
2347         {
2348                 case INFO_INFO1_NONE:
2349                 user = -1;
2350                 break;
2351                 default:
2352                 user = p_capainfo.bearer_info1 & 0x7f;
2353                 break;
2354         }
2355         enc_ie_bearer(&setup->BEARER, dmsg, coding, capability, mode, rate, -1, user);
2356         /* hlc */
2357         if (p_capainfo.hlc)
2358         {
2359                 coding = 0;
2360                 interpretation = 4;
2361                 presentation = 1;
2362                 hlc = p_capainfo.hlc & 0x7f;
2363                 exthlc = -1;
2364                 if (p_capainfo.exthlc)
2365                         exthlc = p_capainfo.exthlc & 0x7f;
2366                 enc_ie_hlc(&setup->HLC, dmsg, coding, interpretation, presentation, hlc, exthlc);
2367         }
2368
2369         /* display */
2370         if (p_callerinfo.display[0] && p_m_d_ntmode)
2371                 enc_ie_display(&setup->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2372 #ifdef CENTREX
2373         /* nt-mode: CNIP (calling name identification presentation) */
2374         if (p_callerinfo.name[0] && p_m_d_ntmode)
2375                 enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2376 #endif
2377         end_trace();
2378
2379         /* send setup message now */
2380         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2381
2382         new_state(PORT_STATE_OUT_SETUP);
2383 }
2384
2385 /* MESSAGE_FACILITY */
2386 void Pdss1::message_facility(unsigned long epoint_id, int message_id, union parameter *param)
2387 {
2388         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2389         FACILITY_t *facility;
2390         msg_t *dmsg;
2391
2392         /* facility will not be sent to external lines */
2393         if (!p_m_d_ntmode)
2394                 return;
2395
2396         /* sending facility */
2397         dmsg = create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, p_m_d_l3id, sizeof(FACILITY_t), p_m_d_ntmode);
2398         facility = (FACILITY_t *)(dmsg->data + headerlen);
2399         l1l2l3_trace_header(p_m_mISDNport, this, CC_FACILITY | REQUEST, DIRECTION_OUT);
2400         enc_ie_facility(&facility->FACILITY, dmsg, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2401         end_trace();
2402         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2403 }
2404
2405 /* MESSAGE_NOTIFY */
2406 void Pdss1::message_notify(unsigned long epoint_id, int message_id, union parameter *param)
2407 {
2408         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2409         INFORMATION_t *information;
2410         NOTIFY_t *notification;
2411         int notify;
2412         int plan, type = -1, present;
2413         msg_t *dmsg;
2414
2415         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2416                 notify = param->notifyinfo.notify & 0x7f;
2417         else
2418                 notify = -1;
2419         if (p_state != PORT_STATE_CONNECT)
2420         {
2421                 /* notify only allowed in active state */
2422                 notify = -1;
2423         }
2424         if (notify >= 0)
2425         {
2426                 plan = 1;
2427                 switch (param->notifyinfo.ntype)
2428                 {
2429                         case INFO_NTYPE_INTERNATIONAL:
2430                         type = 1;
2431                         break;
2432                         case INFO_NTYPE_NATIONAL:
2433                         type = 2;
2434                         break;
2435                         case INFO_NTYPE_SUBSCRIBER:
2436                         type = 4;
2437                         break;
2438                         default: /* INFO_NTYPE_UNKNOWN */
2439                         type = 0;
2440                         break;
2441                 }
2442                 switch (param->notifyinfo.present)
2443                 {
2444                         case INFO_PRESENT_NULL: /* no redir at all */
2445                         present = -1;
2446                         plan = -1;
2447                         type = -1;
2448                         break;
2449                         case INFO_PRESENT_RESTRICTED:
2450                         present = 1;
2451                         break;
2452                         case INFO_PRESENT_NOTAVAIL:
2453                         present = 2;
2454                         break;
2455                         default: /* INFO_PRESENT_ALLOWED */
2456                         present = 0;
2457                         break;
2458                 }
2459         }
2460
2461         if (notify<0 && !param->notifyinfo.display[0])
2462         {
2463                 /* nothing to notify, nothing to display */
2464                 return;
2465         }
2466
2467         if (notify >= 0)
2468         {
2469                 if (p_state!=PORT_STATE_CONNECT)
2470                 {
2471                         /* queue notification */
2472                         if (p_m_d_notify_pending)
2473                                 message_free(p_m_d_notify_pending);
2474                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2475                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2476                 } else
2477                 {
2478                         /* sending notification */
2479                         dmsg = create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, p_m_d_l3id, sizeof(NOTIFY_t), p_m_d_ntmode);
2480                         notification = (NOTIFY_t *)(dmsg->data + headerlen);
2481                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NOTIFY | REQUEST, DIRECTION_OUT);
2482                         enc_ie_notify(&notification->NOTIFY, dmsg, notify);
2483                         /* sending redirection number only in ntmode */
2484                         if (type >= 0 && p_m_d_ntmode)
2485                                 enc_ie_redir_dn(&notification->REDIR_DN, dmsg, type, plan, present, (unsigned char *)param->notifyinfo.id);
2486                         if (param->notifyinfo.display[0] && p_m_d_ntmode)
2487                                 enc_ie_display(&notification->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2488                         end_trace();
2489                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2490                 }
2491         } else if (p_m_d_ntmode)
2492         {
2493                 /* sending information */
2494                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2495                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2496                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2497                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2498                 end_trace();
2499                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2500         }
2501 }
2502
2503 /* MESSAGE_OVERLAP */
2504 void Pdss1::message_overlap(unsigned long epoint_id, int message_id, union parameter *param)
2505 {
2506         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2507         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
2508         msg_t *dmsg;
2509
2510         /* sending setup_acknowledge */
2511         dmsg = create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, sizeof(SETUP_ACKNOWLEDGE_t), p_m_d_ntmode);
2512         setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2513         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
2514         /* channel information */
2515         if (p_state == PORT_STATE_IN_SETUP)
2516                 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2517         /* progress information */
2518         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2519          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2520          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2521         if (p_m_mISDNport->tones)
2522                 enc_ie_progress(&setup_acknowledge->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2523         end_trace();
2524         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2525
2526         new_state(PORT_STATE_IN_OVERLAP);
2527 }
2528
2529 /* MESSAGE_PROCEEDING */
2530 void Pdss1::message_proceeding(unsigned long epoint_id, int message_id, union parameter *param)
2531 {
2532         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2533         CALL_PROCEEDING_t *proceeding;
2534         msg_t *dmsg;
2535
2536         /* sending proceeding */
2537         dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2538         proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2539         l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2540         /* channel information */
2541         if (p_state == PORT_STATE_IN_SETUP)
2542                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2543         /* progress information */
2544         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2545          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2546          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2547         if (p_m_mISDNport->tones)
2548                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2549         end_trace();
2550         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2551
2552         new_state(PORT_STATE_IN_PROCEEDING);
2553 }
2554
2555 /* MESSAGE_ALERTING */
2556 void Pdss1::message_alerting(unsigned long epoint_id, int message_id, union parameter *param)
2557 {
2558         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2559         ALERTING_t *alerting;
2560         msg_t *dmsg;
2561
2562         /* NT-MODE in setup state we must send PROCEEDING first */
2563         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2564         {
2565                 CALL_PROCEEDING_t *proceeding;
2566
2567                 /* sending proceeding */
2568                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2569                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2570                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2571                 /* channel information */
2572                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2573                 /* progress information */
2574                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2575                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2576                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2577                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2578                 end_trace();
2579                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2580                 new_state(PORT_STATE_IN_PROCEEDING);
2581         }
2582
2583         /* sending alerting */
2584         dmsg = create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, p_m_d_l3id, sizeof(ALERTING_t), p_m_d_ntmode);
2585         alerting = (ALERTING_t *)(dmsg->data + headerlen);
2586         l1l2l3_trace_header(p_m_mISDNport, this, CC_ALERTING | REQUEST, DIRECTION_OUT);
2587         /* channel information */
2588         if (p_state == PORT_STATE_IN_SETUP)
2589                 enc_ie_channel_id(&alerting->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2590         /* progress information */
2591         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2592          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2593          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2594         if (p_m_mISDNport->tones)
2595                 enc_ie_progress(&alerting->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2596         end_trace();
2597         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2598
2599         new_state(PORT_STATE_IN_ALERTING);
2600 }
2601
2602 /* MESSAGE_CONNECT */
2603 void Pdss1::message_connect(unsigned long epoint_id, int message_id, union parameter *param)
2604 {
2605         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2606         INFORMATION_t *information;
2607         CONNECT_t *connect;
2608         int type, plan, present, screen;
2609         msg_t *dmsg;
2610         class Endpoint *epoint;
2611
2612         /* NT-MODE in setup state we must send PROCEEDING first */
2613         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2614         {
2615                 CALL_PROCEEDING_t *proceeding;
2616
2617                 /* sending proceeding */
2618                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2619                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2620                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2621                 /* channel information */
2622                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2623 //              /* progress information */
2624 //              if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2625 //               || p_capainfo.bearer_capa==INFO_BC_AUDIO
2626 //               || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2627 //              enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2628                 end_trace();
2629                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2630                 new_state(PORT_STATE_IN_PROCEEDING);
2631         }
2632
2633         /* copy connected information */
2634         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2635
2636         /* only display at connect state */
2637         if (p_state == PORT_STATE_CONNECT)
2638         if (p_connectinfo.display[0])
2639         {
2640                 /* sending information */
2641                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2642                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2643                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2644                 if (p_m_d_ntmode)
2645                         enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2646                 end_trace();
2647                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2648                 return;
2649         }
2650
2651         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)
2652         {
2653                 /* connect command only possible in setup, proceeding or alerting state */
2654                 return;
2655         }
2656
2657         /* preparing connect message */
2658         dmsg = create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, p_m_d_l3id, sizeof(CONNECT_t), p_m_d_ntmode);
2659         connect = (CONNECT_t *)(dmsg->data + headerlen);
2660         l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | REQUEST, DIRECTION_OUT);
2661         /* connect information */
2662         plan = 1;
2663         switch (p_connectinfo.ntype)
2664         {
2665                 case INFO_NTYPE_INTERNATIONAL:
2666                 type = 0x1;
2667                 break;
2668                 case INFO_NTYPE_NATIONAL:
2669                 type = 0x2;
2670                 break;
2671                 case INFO_NTYPE_SUBSCRIBER:
2672                 type = 0x4;
2673                 break;
2674                 default: /* INFO_NTYPE_UNKNOWN */
2675                 type = 0x0;
2676                 break;
2677         }
2678         switch (param->connectinfo.screen)
2679         {
2680                 case INFO_SCREEN_USER:
2681                 screen = 0;
2682                 break;
2683                 default: /* INFO_SCREE_NETWORK */
2684                 screen = 3;
2685                 break;
2686         }
2687         switch (p_connectinfo.present)
2688         {
2689                 case INFO_PRESENT_NULL: /* no colp at all */
2690                 present = -1;
2691                 screen = -1;
2692                 plan = -1;
2693                 type = -1;
2694                 break;
2695                 case INFO_PRESENT_RESTRICTED:
2696                 present = 1;
2697                 break;
2698                 case INFO_PRESENT_NOTAVAIL:
2699                 present = 2;
2700                 break;
2701                 default: /* INFO_PRESENT_ALLOWED */
2702                 present = 0;
2703                 break;
2704         }
2705         if (type >= 0)
2706                 enc_ie_connected_pn(&connect->CONNECT_PN, dmsg, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2707         /* display */
2708         if (p_connectinfo.display[0] && p_m_d_ntmode)
2709                 enc_ie_display(&connect->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2710 #ifdef CENTREX
2711         /* nt-mode: CONP (connected name identification presentation) */
2712         if (p_connectinfo.name[0] && p_m_d_ntmode)
2713                 enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2714 #endif
2715         /* date & time */
2716         if (p_m_d_ntmode)
2717         {
2718                 epoint = find_epoint_id(epoint_id);
2719                 enc_ie_date(&connect->DATE, dmsg, now, p_settings.no_seconds);
2720         }
2721         end_trace();
2722         /* finally send message */
2723         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2724
2725         if (p_m_d_ntmode)
2726                 new_state(PORT_STATE_CONNECT);
2727         else
2728                 new_state(PORT_STATE_CONNECT_WAITING);
2729         set_tone("", NULL);
2730 }
2731
2732 /* MESSAGE_DISCONNECT */
2733 void Pdss1::message_disconnect(unsigned long epoint_id, int message_id, union parameter *param)
2734 {
2735         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2736         DISCONNECT_t *disconnect;
2737         RELEASE_COMPLETE_t *release_complete;
2738         msg_t *dmsg;
2739         struct message *message;
2740         char *p = NULL;
2741
2742         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2743         if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->tones)
2744          || p_state==PORT_STATE_OUT_SETUP)
2745         {
2746                 /* sending release to endpoint */
2747                 while(p_epointlist)
2748                 {
2749                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2750                         message->param.disconnectinfo.cause = 16;
2751                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2752                         message_put(message);
2753                         /* remove epoint */
2754                         free_epointlist(p_epointlist);
2755                 }
2756                 /* sending release */
2757                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2758                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2759                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
2760                 /* send cause */
2761                 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);
2762                 end_trace();
2763                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2764                 new_state(PORT_STATE_RELEASE);
2765                 p_m_delete = 1;
2766                 return;
2767         }
2768
2769         /* NT-MODE in setup state we must send PROCEEDING first */
2770         if (p_state==PORT_STATE_IN_SETUP)
2771         {
2772                 CALL_PROCEEDING_t *proceeding;
2773
2774                 /* sending proceeding */
2775                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2776                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2777                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2778                 /* channel information */
2779                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2780                 /* progress information */
2781                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2782                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2783                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2784                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2785                 end_trace();
2786                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2787                 new_state(PORT_STATE_IN_PROCEEDING);
2788         }
2789
2790         /* sending disconnect */
2791         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2792         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2793         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2794         /* progress information */
2795         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2796          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2797          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2798         if (p_m_mISDNport->tones)
2799                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2800         /* send cause */
2801         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);
2802         /* send display */
2803         if (param->disconnectinfo.display[0])
2804                 p = param->disconnectinfo.display;
2805         if (p) if (*p && p_m_d_ntmode)
2806                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2807         end_trace();
2808         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2809         new_state(PORT_STATE_OUT_DISCONNECT);
2810 }
2811
2812 /* MESSAGE_RELEASE */
2813 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
2814 {
2815         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2816         RELEASE_t *release;
2817         RELEASE_COMPLETE_t *release_complete;
2818         DISCONNECT_t *disconnect;
2819         msg_t *dmsg;
2820         class Endpoint *epoint;
2821         char *p = NULL;
2822
2823         /* if we have incoming disconnected, we may release */
2824         if (p_state==PORT_STATE_IN_DISCONNECT
2825          || p_state==PORT_STATE_OUT_DISCONNECT)
2826         {
2827                 /* sending release */
2828                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
2829                 release = (RELEASE_t *)(dmsg->data + headerlen);
2830                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2831                 /* send cause */
2832                 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);
2833                 end_trace();
2834                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2835                 new_state(PORT_STATE_RELEASE);
2836                 return;
2837
2838         }
2839         /* if we are on outgoing/incoming call setup, we may release complete */
2840         if (p_state==PORT_STATE_OUT_SETUP
2841          || p_state==PORT_STATE_IN_SETUP
2842 // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
2843          || p_state==PORT_STATE_OUT_PROCEEDING)
2844         {
2845                 /* sending release */
2846                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2847                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2848                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2849                 /* send cause */
2850                 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);
2851                 end_trace();
2852                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2853                 new_state(PORT_STATE_RELEASE);
2854
2855                 /* remove epoint */
2856                 free_epointid(epoint_id);
2857                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | REQUEST, DIRECTION_OUT);
2858                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
2859                 end_trace();
2860                 if (p_m_d_ntmode)
2861                 {
2862                         if ((p_m_d_l3id&0xff00) == 0xff00)
2863                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2864                 }
2865                 p_m_d_l3id = 0;
2866                 p_m_delete = 1;
2867                 return;
2868         }
2869
2870         /* NT-MODE in setup state we must send PROCEEDING first */
2871         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2872         {
2873                 CALL_PROCEEDING_t *proceeding;
2874
2875                 /* sending proceeding */
2876                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2877                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2878                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2879                 /* channel information */
2880                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2881                 /* progress information */
2882                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2883                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2884                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2885                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2886                 end_trace();
2887                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2888         }
2889
2890         /* sending disconnect */
2891         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2892         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2893         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2894         /* progress information */
2895         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2896          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2897          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2898         if (p_m_mISDNport->tones)
2899                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2900         /* send cause */
2901         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);
2902         /* send display */
2903         epoint = find_epoint_id(epoint_id);
2904         if (param->disconnectinfo.display[0])
2905                 p = param->disconnectinfo.display;
2906         if (p) if (*p && p_m_d_ntmode)
2907                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2908         end_trace();
2909         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2910         new_state(PORT_STATE_RELEASE);
2911         free_epointid(epoint_id);
2912 //      p_m_delete = 1;
2913 }
2914
2915
2916 /*
2917  * endpoint sends messages to the port
2918  */
2919 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
2920 {
2921         struct message *message;
2922
2923         if (PmISDN::message_epoint(epoint_id, message_id, param))
2924                 return(1);
2925
2926         switch(message_id)
2927         {
2928                 case MESSAGE_INFORMATION: /* overlap dialing */
2929                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2930                  && p_state!=PORT_STATE_OUT_OVERLAP
2931                  && p_state!=PORT_STATE_CONNECT
2932                  && p_state!=PORT_STATE_OUT_DISCONNECT
2933                  && p_state!=PORT_STATE_IN_DISCONNECT)
2934                 {
2935                         break;
2936                 }
2937                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2938                  && p_state!=PORT_STATE_OUT_OVERLAP
2939                  && p_state!=PORT_STATE_OUT_PROCEEDING
2940                  && p_state!=PORT_STATE_OUT_ALERTING
2941                  && p_state!=PORT_STATE_CONNECT
2942                  && p_state!=PORT_STATE_OUT_DISCONNECT
2943                  && p_state!=PORT_STATE_IN_DISCONNECT)
2944                 {
2945                         break;
2946                 }
2947                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
2948                  && p_state!=PORT_STATE_IN_OVERLAP
2949                  && p_state!=PORT_STATE_IN_PROCEEDING
2950                  && p_state!=PORT_STATE_IN_ALERTING
2951                  && p_state!=PORT_STATE_CONNECT
2952                  && p_state!=PORT_STATE_CONNECT_WAITING
2953                  && p_state!=PORT_STATE_OUT_DISCONNECT
2954                  && p_state!=PORT_STATE_IN_DISCONNECT)
2955                 {
2956                         break;
2957                 }
2958                 message_information(epoint_id, message_id, param);
2959                 break;
2960
2961                 case MESSAGE_SETUP: /* dial-out command received from epoint */
2962                 if (p_state!=PORT_STATE_IDLE
2963                  && p_state!=PORT_STATE_CONNECT)
2964                 {
2965                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
2966                         break;
2967                 }
2968                 if (p_epointlist && p_state==PORT_STATE_IDLE)
2969                 {
2970                         PERROR("Pdss1(%s): software error: epoint pointer is set in idle state, how bad!! exitting.\n", p_name);
2971                         exit(-1);
2972                 }
2973                 /* note: pri is a special case, because links must be up for pri */ 
2974                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
2975                 {
2976                         /* LAYER 1 is up, or we may send */
2977                         message_setup(epoint_id, message_id, param);
2978                 } else {
2979                         iframe_t act;
2980                         /* LAYER 1 id down, so we queue */
2981                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
2982                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
2983                         /* attach us */
2984                         if (!(epointlist_new(epoint_id)))
2985                         {
2986                                 PERROR("no memory for epointlist\n");
2987                                 exit(-1);
2988                         }
2989                         /* activate link */
2990                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
2991                         act.prim = PH_ACTIVATE | REQUEST; 
2992                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2993                         act.dinfo = 0;
2994                         act.len = 0;
2995                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
2996                         l1l2l3_trace_header(p_m_mISDNport, this, act.prim, DIRECTION_OUT);
2997                         end_trace();
2998 //                      /* set timeout */
2999 //                      p_m_mISDNport->l1timeout = now+3;
3000                 }
3001                 break;
3002
3003                 case MESSAGE_NOTIFY: /* display and notifications */
3004                 message_notify(epoint_id, message_id, param);
3005                 break;
3006
3007                 case MESSAGE_FACILITY: /* facility message */
3008                 message_facility(epoint_id, message_id, param);
3009                 break;
3010
3011                 case MESSAGE_OVERLAP: /* more information is needed */
3012                 if (p_state!=PORT_STATE_IN_SETUP)
3013                 {
3014                         break;
3015                 }
3016                 message_overlap(epoint_id, message_id, param);
3017                 break;
3018
3019                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
3020                 if (p_state!=PORT_STATE_IN_SETUP
3021                  && p_state!=PORT_STATE_IN_OVERLAP)
3022                 {
3023                         break;
3024                 }
3025                 message_proceeding(epoint_id, message_id, param);
3026                 break;
3027
3028                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
3029                 if (p_state!=PORT_STATE_IN_SETUP
3030                  && p_state!=PORT_STATE_IN_OVERLAP
3031                  && p_state!=PORT_STATE_IN_PROCEEDING)
3032                 {
3033                         break;
3034                 }
3035                 message_alerting(epoint_id, message_id, param);
3036                 break;
3037
3038                 case MESSAGE_CONNECT: /* call of endpoint is connected */
3039                 if (p_state!=PORT_STATE_IN_SETUP
3040                  && p_state!=PORT_STATE_IN_OVERLAP
3041                  && p_state!=PORT_STATE_IN_PROCEEDING
3042                  && p_state!=PORT_STATE_IN_ALERTING
3043                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
3044                 {
3045                         break;
3046                 }
3047                 message_connect(epoint_id, message_id, param);
3048                 break;
3049
3050                 case MESSAGE_DISCONNECT: /* call has been disconnected */
3051                 if (p_state!=PORT_STATE_IN_SETUP
3052                  && p_state!=PORT_STATE_IN_OVERLAP
3053                  && p_state!=PORT_STATE_IN_PROCEEDING
3054                  && p_state!=PORT_STATE_IN_ALERTING
3055                  && p_state!=PORT_STATE_OUT_SETUP
3056                  && p_state!=PORT_STATE_OUT_OVERLAP
3057                  && p_state!=PORT_STATE_OUT_PROCEEDING
3058                  && p_state!=PORT_STATE_OUT_ALERTING
3059                  && p_state!=PORT_STATE_CONNECT
3060                  && p_state!=PORT_STATE_CONNECT_WAITING)
3061                 {
3062                         break;
3063                 }
3064                 message_disconnect(epoint_id, message_id, param);
3065                 break;
3066
3067                 case MESSAGE_RELEASE: /* release isdn port */
3068                 if (p_state==PORT_STATE_RELEASE)
3069                 {
3070                         break;
3071                 }
3072                 message_release(epoint_id, message_id, param);
3073                 break;
3074
3075                 default:
3076                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
3077         }
3078
3079         return(1);
3080 }
3081
3082
3083
3084 /*
3085  * data from isdn-stack (layer-3) to pbx (port class)
3086  */
3087 /* NOTE: nt mode use mISDNuser_head_t as header */
3088 int stack2manager_nt(void *dat, void *arg)
3089 {
3090         class Port *port;
3091         class Pdss1 *pdss1;
3092         manager_t *mgr = (manager_t *)dat;
3093         msg_t *msg = (msg_t *)arg;
3094         mISDNuser_head_t *hh;
3095         struct mISDNport *mISDNport;
3096         char name[32];
3097
3098         if (!msg || !mgr)
3099                 return(-EINVAL);
3100
3101         /* note: nst is the first data feld of mISDNport */
3102         mISDNport = (struct mISDNport *)mgr->nst;
3103
3104         hh = (mISDNuser_head_t *)msg->data;
3105         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3106
3107         /* find Port object of type ISDN */
3108         port = port_first;
3109         while(port)
3110         {
3111                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
3112                 {
3113                         pdss1 = (class Pdss1 *)port;
3114 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
3115                         /* check out correct stack */
3116                         if (pdss1->p_m_mISDNport == mISDNport)
3117                         /* check out correct id */
3118                         if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
3119                         {
3120                                 /* found port, the message belongs to */
3121                                 break;
3122                         }
3123                 }
3124                 port = port->next;
3125         }
3126         if (port)
3127         {
3128                 /* if process id is master process, but a child disconnects */
3129                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00 && hh->prim==(CC_DISCONNECT|INDICATION))
3130                 {
3131                         /* send special indication for child disconnect */
3132                         pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
3133                         free_msg(msg);
3134                         return(0);
3135                 }
3136                 /* if process id and layer 3 id matches */
3137                 if (hh->dinfo == pdss1->p_m_d_l3id)
3138                 {
3139                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3140                         free_msg(msg);
3141                         return(0);
3142                 }
3143         }
3144
3145         /* d-message */
3146         switch(hh->prim)
3147         {
3148                 case MGR_SHORTSTATUS | INDICATION:
3149                 case MGR_SHORTSTATUS | CONFIRM:
3150                 switch(hh->dinfo) {
3151                         case SSTATUS_L2_ESTABLISHED:
3152                         goto ss_estab;
3153                         case SSTATUS_L2_RELEASED:
3154                         goto ss_rel;
3155                 }
3156                 break;
3157
3158                 case DL_ESTABLISH | INDICATION:
3159                 case DL_ESTABLISH | CONFIRM:
3160                 ss_estab:
3161                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3162                 add_trace("tei", NULL, "%d", hh->dinfo);
3163                 end_trace();
3164                 if (mISDNport->ptp && hh->dinfo == 0)
3165                 {
3166                         if (mISDNport->l2establish)
3167                         {
3168                                 mISDNport->l2establish = 0;
3169                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
3170                         }
3171                         mISDNport->l2link = 1;
3172                         if (mISDNport->pri);
3173                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
3174                 }
3175                 break;
3176
3177                 case DL_RELEASE | INDICATION:
3178                 case DL_RELEASE | CONFIRM:
3179                 ss_rel:
3180                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3181                 add_trace("tei", NULL, "%d", hh->dinfo);
3182                 end_trace();
3183                 if (mISDNport->ptp && hh->dinfo == 0)
3184                 {
3185                         mISDNport->l2link = 0;
3186                         time(&mISDNport->l2establish);
3187                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
3188                 }
3189                 break;
3190
3191                 case CC_SETUP | INDICATION:
3192                 /* creating port object */
3193                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3194                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3195                 {
3196                         RELEASE_COMPLETE_t *release_complete;
3197                         msg_t *dmsg;
3198
3199                         PERROR("FATAL ERROR: cannot create port object.\n");
3200                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3201                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3202                         l1l2l3_trace_header(mISDNport, NULL, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
3203                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3204                         end_trace();
3205                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3206                         break;
3207                 }
3208                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3209                 break;
3210
3211                 case CC_RESUME | INDICATION:
3212                 /* creating port object */
3213                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3214                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3215                 {
3216                         SUSPEND_REJECT_t *suspend_reject;
3217                         msg_t *dmsg;
3218
3219                         PERROR("FATAL ERROR: cannot create port object.\n");
3220                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, hh->dinfo, sizeof(SUSPEND_REJECT_t), mISDNport->ntmode);
3221                         suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + mISDN_HEADER_LEN);
3222                         l1l2l3_trace_header(mISDNport, NULL, CC_SUSPEND_REJECT | REQUEST, DIRECTION_OUT);
3223                         enc_ie_cause_standalone(mISDNport->ntmode?&suspend_reject->CAUSE:NULL, dmsg, (mISDNport->ntmode)?1:0, 47);
3224                         end_trace();
3225                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3226                         break;
3227                 }
3228                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3229                 break;
3230
3231                 case CC_RELEASE_CR | INDICATION:
3232                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", hh->dinfo);
3233                 break;
3234
3235                 case CC_DISCONNECT | INDICATION:
3236
3237                 // fall throug
3238                 default:
3239                 PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3240                 return(-EINVAL);
3241         }
3242         free_msg(msg);
3243         return(0);
3244 }
3245
3246 /* NOTE: te mode use iframe_t as header */
3247 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
3248 {
3249         class Port *port;
3250         class Pdss1 *pdss1;
3251         iframe_t *frm;
3252         char name[32];
3253
3254         if (!msg || !mISDNport)
3255                 return(-EINVAL);
3256         frm = (iframe_t *)msg->data;
3257         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3258
3259         /* find Port object of type ISDN */
3260         port = port_first;
3261         while(port)
3262         {
3263                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
3264                 {
3265                         pdss1 = (class Pdss1 *)port;
3266                         /* check out correct stack */
3267                         if (pdss1->p_m_mISDNport == mISDNport)
3268                         /* check out correct id */
3269                         if (frm->dinfo == pdss1->p_m_d_l3id)
3270                         {
3271                                 /* found port, the message belongs to */
3272                                 break;
3273                         }
3274                 }
3275                 port = port->next;
3276         }
3277         if (port)
3278         {
3279                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3280                 free_msg(msg);
3281                 return(0);
3282         }
3283
3284         /* process new cr (before setup indication) */
3285 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
3286         if (frm->prim == (CC_NEW_CR | INDICATION))
3287         {
3288
3289                 /* creating port object */
3290                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3291                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_TE_IN, mISDNport, name, NULL, 0, 0)))
3292                 {
3293                         RELEASE_COMPLETE_t *release_complete;
3294                         msg_t *dmsg;
3295
3296                         PERROR("FATAL ERROR: cannot create port object.\n");
3297                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, frm->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3298                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3299                         l1l2l3_trace_header(mISDNport, NULL, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
3300                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3301                         end_trace();
3302                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3303                         free_msg(msg);
3304                         return(0);
3305                 }
3306                 /* l3id will be set from dinfo at message_isdn */
3307                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3308                 free_msg(msg);
3309                 return(0);
3310         }
3311
3312         if (frm->prim == (CC_RELEASE_CR | INDICATION))
3313         {
3314                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
3315                 free_msg(msg);
3316                 return(0);
3317         }
3318         PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3319         return(-EINVAL);
3320 }
3321
3322
3323 /*
3324  * sending message that were queued during L1 activation
3325  * or releasing port if link is down
3326  */
3327 void setup_queue(struct mISDNport *mISDNport, int link)
3328 {
3329         class Port *port;
3330         class Pdss1 *pdss1;
3331         struct message *message;
3332
3333         if (!mISDNport->ntmode)
3334                 return;
3335
3336         /* check all port objects for pending message */
3337         port = port_first;
3338         while(port)
3339         {
3340                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
3341                 {
3342                         pdss1 = (class Pdss1 *)port;
3343                         if (pdss1->p_m_mISDNport == mISDNport)
3344                         {
3345                                 if (pdss1->p_m_d_queue)
3346                                 {
3347                                         if (link)
3348                                         {
3349                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3350                                                 /* LAYER 1 is up, so we send */
3351                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
3352                                                 message_free(pdss1->p_m_d_queue);
3353                                                 pdss1->p_m_d_queue = NULL;
3354                                         } else
3355                                         {
3356                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3357                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
3358                                                 message->param.disconnectinfo.cause = 27;
3359                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3360                                                 message_put(message);
3361                                                 pdss1->new_state(PORT_STATE_RELEASE);
3362                                                 pdss1->p_m_delete = 1;
3363                                         }
3364                                 }
3365                         }
3366                 }
3367                 port = port->next;
3368         }
3369 }
3370
3371
3372
3373
3374