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                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1789                 if (p_m_d_ntmode)
1790                 {
1791                         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1792                         timer_hex = *((int *)(((char *)data)+headerlen));
1793                 }
1794                 if (timer_hex)
1795                         add_trace("timer", NULL, "%x", timer_hex);
1796                 end_trace();
1797                 if (timer_hex==0x312 && p_m_d_ntmode)
1798                         t312_timeout(prim, dinfo, data);
1799                 break;
1800
1801                 case CC_SETUP | INDICATION:
1802                 if (p_state != PORT_STATE_IDLE)
1803                         break;
1804                 setup_ind(prim, dinfo, data);
1805                 break;
1806
1807                 case CC_SETUP | CONFIRM:
1808                 if (p_m_d_ntmode)
1809                 {
1810                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1811                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1812                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
1813                         if ((p_m_d_l3id&0xff00) != 0xff00)
1814                                 PERROR("    strange setup-procid 0x%x\n", p_m_d_l3id);
1815                         p_m_d_l3id = *((int *)(((u_char *)data)+ mISDNUSER_HEAD_SIZE));
1816                         add_trace("callref", "new", "0x%x", p_m_d_l3id);
1817                         end_trace();
1818                 }
1819                 break;
1820
1821                 case CC_INFORMATION | INDICATION:
1822                 information_ind(prim, dinfo, data);
1823                 break;
1824
1825                 case CC_SETUP_ACKNOWLEDGE | INDICATION:
1826                 if (p_state != PORT_STATE_OUT_SETUP)
1827                 {
1828                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
1829                         break;
1830                 }
1831                 setup_acknowledge_ind(prim, dinfo, data);
1832                 break;
1833
1834                 case CC_PROCEEDING | INDICATION:
1835                 if (p_state != PORT_STATE_OUT_SETUP
1836                  && p_state != PORT_STATE_OUT_OVERLAP)
1837                 {
1838                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
1839                         break;
1840                 }
1841                 proceeding_ind(prim, dinfo, data);
1842                 break;
1843
1844                 case CC_ALERTING | INDICATION:
1845                 if (p_state != PORT_STATE_OUT_SETUP
1846                  && p_state != PORT_STATE_OUT_OVERLAP
1847                  && p_state != PORT_STATE_OUT_PROCEEDING)
1848                 {
1849                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
1850                         break;
1851                 }
1852                 alerting_ind(prim, dinfo, data);
1853                 break;
1854
1855                 case CC_CONNECT | INDICATION:
1856                 if (p_state != PORT_STATE_OUT_SETUP
1857                  && p_state != PORT_STATE_OUT_OVERLAP
1858                  && p_state != PORT_STATE_OUT_PROCEEDING
1859                  && p_state != PORT_STATE_OUT_ALERTING)
1860                 {
1861                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
1862                         break;
1863                 }
1864                 connect_ind(prim, dinfo, data);
1865                 if (p_m_d_notify_pending)
1866                 {
1867                         /* send pending notify message during connect */
1868                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1869                         message_free(p_m_d_notify_pending);
1870                         p_m_d_notify_pending = NULL;
1871                 }
1872                 break;
1873
1874                 case CC_CONNECT_ACKNOWLEDGE | INDICATION:
1875                 case CC_CONNECT | CONFIRM:
1876                 if (p_state == PORT_STATE_CONNECT_WAITING)
1877                         new_state(PORT_STATE_CONNECT);
1878                 if (p_m_d_notify_pending)
1879                 {
1880                         /* send pending notify message during connect-ack */
1881                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1882                         message_free(p_m_d_notify_pending);
1883                         p_m_d_notify_pending = NULL;
1884                 }
1885                 break;
1886
1887                 case CC_DISCONNECT | INDICATION:
1888                 disconnect_ind(prim, dinfo, data);
1889                 break;
1890
1891                 case CC_RELEASE | CONFIRM:
1892                 case CC_RELEASE | INDICATION:
1893                 release_ind(prim, dinfo, data);
1894                 break;
1895
1896                 case CC_RELEASE_COMPLETE | INDICATION:
1897                 release_complete_ind(prim, dinfo, data);
1898                 break;
1899
1900                 case CC_RELEASE_COMPLETE | CONFIRM:
1901                 break;
1902
1903                 case CC_NOTIFY | INDICATION:
1904                 notify_ind(prim, dinfo, data);
1905                 break;
1906
1907                 case CC_HOLD | INDICATION:
1908                 hold_ind(prim, dinfo, data);
1909                 break;
1910
1911                 case CC_RETRIEVE | INDICATION:
1912                 retrieve_ind(prim, dinfo, data);
1913                 break;
1914
1915                 case CC_SUSPEND | INDICATION:
1916                 suspend_ind(prim, dinfo, data);
1917                 break;
1918
1919                 case CC_RESUME | INDICATION:
1920                 resume_ind(prim, dinfo, data);
1921                 break;
1922
1923                 case CC_FACILITY | INDICATION:
1924                 facility_ind(prim, dinfo, data);
1925                 break;
1926
1927                 case CC_RELEASE_CR | INDICATION:
1928                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | INDICATION, DIRECTION_IN);
1929                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
1930                 end_trace();
1931                 if (p_m_d_ntmode)
1932                 {
1933                         if ((p_m_d_l3id&0xff00) == 0xff00)
1934                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1935                 }
1936                 p_m_d_l3id = 0;
1937                 p_m_delete = 1;
1938                 /* sending release to endpoint in case we still have an endpoint
1939                  * NOTE: this only happens if the stack releases due to layer1
1940                  * or layer2 breakdown. otherwhise a release is received first.
1941                  */
1942                 while(p_epointlist)
1943                 {
1944                         struct message *message;
1945                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1946                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
1947                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
1948                         message_put(message);
1949                         /* remove epoint */
1950                         free_epointlist(p_epointlist);
1951
1952                         new_state(PORT_STATE_RELEASE);
1953                 }
1954                 break;
1955
1956                 case CC_NEW_CR | INDICATION:
1957                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1958                 if (p_m_d_l3id)
1959                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1960                 if (p_m_d_ntmode)
1961                 {
1962                         new_l3id = *((int *)(((u_char *)data+mISDNUSER_HEAD_SIZE)));
1963                         if (((new_l3id&0xff00)!=0xff00) && ((p_m_d_l3id&0xff00)==0xff00))
1964                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1965                 } else
1966                 {
1967                         new_l3id = dinfo;
1968                 }
1969                 p_m_d_l3id = new_l3id;
1970                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
1971                 end_trace();
1972                 break;
1973
1974                 default:
1975                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1976                 add_trace("unhandled", "prim", "0x%x", prim);
1977                 end_trace();
1978         }
1979 }
1980
1981 void Pdss1::new_state(int state)
1982 {
1983         class Endpoint *epoint;
1984
1985         /* set timeout */
1986         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1987         if (epoint && p_m_d_ntmode)
1988         {
1989                 if (state == PORT_STATE_IN_OVERLAP)
1990                 {
1991                         p_m_timeout = p_settings.tout_dialing;
1992                         time(&p_m_timer);
1993                 }
1994                 if (state != p_state)
1995                 {
1996                         if (state == PORT_STATE_IN_SETUP
1997                          || state == PORT_STATE_IN_OVERLAP)
1998                         {
1999                                 p_m_timeout = p_settings.tout_setup;
2000                                 time(&p_m_timer);
2001                         }
2002                         if (state == PORT_STATE_IN_PROCEEDING
2003                          || state == PORT_STATE_OUT_PROCEEDING)
2004                         {
2005                                 p_m_timeout = p_settings.tout_proceeding;
2006                                 time(&p_m_timer);
2007                         }
2008                         if (state == PORT_STATE_IN_ALERTING
2009                          || state == PORT_STATE_OUT_ALERTING)
2010                         {
2011                                 p_m_timeout = p_settings.tout_alerting;
2012                                 time(&p_m_timer);
2013                         }
2014                         if (state == PORT_STATE_CONNECT
2015                          || state == PORT_STATE_CONNECT_WAITING)
2016                         {
2017                                 p_m_timeout = 0;
2018                         }
2019                         if (state == PORT_STATE_IN_DISCONNECT
2020                          || state == PORT_STATE_OUT_DISCONNECT)
2021                         {
2022                                 p_m_timeout = p_settings.tout_disconnect;
2023                                 time(&p_m_timer);
2024                         }
2025                 }
2026         }
2027         
2028         Port::new_state(state);
2029 }
2030
2031
2032 /*
2033  * handler
2034  */
2035 int Pdss1::handler(void)
2036 {
2037         int ret;
2038
2039         if ((ret = PmISDN::handler()))
2040                 return(ret);
2041
2042         /* handle destruction */
2043         if (p_m_delete && p_m_d_l3id==0)
2044         {
2045                 delete this;
2046                 return(-1);
2047         }
2048
2049         return(0);
2050 }
2051
2052
2053 /*
2054  * handles all messages from endpoint
2055  */
2056 /* MESSAGE_INFORMATION */
2057 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
2058 {
2059         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2060         INFORMATION_t *information;
2061         msg_t *dmsg;
2062
2063         if (param->information.id[0]) /* only if we have something to dial */
2064         {
2065                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2066                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2067                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2068                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.id);
2069                 end_trace();
2070                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2071         }
2072         new_state(p_state);
2073 }
2074
2075
2076 int newteid = 0;
2077
2078 /* MESSAGE_SETUP */
2079 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2080 {
2081         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2082         INFORMATION_t *information;
2083         SETUP_t *setup;
2084         msg_t *dmsg;
2085         int plan, type, screen, present, reason;
2086         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2087         int channel, exclusive;
2088         int i;
2089         struct epoint_list *epointlist;
2090
2091         /* release if port is blocked */
2092         if (p_m_mISDNport->ifport->block)
2093         {
2094                 struct message *message;
2095
2096                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2097                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2098                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2099                 message_put(message);
2100                 new_state(PORT_STATE_RELEASE);
2101                 p_m_delete = 1;
2102                 return;
2103         }
2104
2105         /* copy setup infos to port */
2106         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2107         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2108         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2109         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2110 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2111
2112         /* only display at connect state: this case happens if endpoint is in connected mode */
2113         if (p_state==PORT_STATE_CONNECT)
2114         {
2115                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2116                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2117                         return;
2118                 if (p_callerinfo.display[0])
2119                 {
2120                         /* sending information */
2121                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2122                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2123                         l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2124                         if (p_m_d_ntmode)
2125                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2126                         end_trace();
2127                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2128                         return;
2129                 }
2130         }
2131
2132         /* attach only if not already */
2133         epointlist = p_epointlist;
2134         while(epointlist)
2135         {
2136                 if (epointlist->epoint_id == epoint_id)
2137                         break;
2138                 epointlist = epointlist->next;
2139         }
2140         if (!epointlist)
2141         {
2142                 if (!(epointlist_new(epoint_id)))
2143                 {
2144                         PERROR("no memory for epointlist\n");
2145                         exit(-1);
2146                 }
2147         }
2148
2149         /* get channel */
2150         exclusive = 0;
2151         if (p_m_b_channel)
2152         {
2153                 channel = p_m_b_channel;
2154                 exclusive = p_m_b_exclusive;
2155         } else
2156                 channel = CHANNEL_ANY;
2157         /* nt-port with no channel, not reserverd */
2158         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2159                 channel = CHANNEL_NO;
2160
2161         /* creating l3id */
2162         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | REQUEST, DIRECTION_OUT);
2163         if (p_m_d_ntmode)
2164         {
2165                 i = 0;
2166                 while(i < 0x100)
2167                 {
2168                         if (p_m_mISDNport->procids[i] == 0)
2169                                 break;
2170                         i++;
2171                 }
2172                 if (i == 0x100)
2173                 {
2174                         struct message *message;
2175
2176                         add_trace("callref", NULL, "no free id");
2177                         end_trace();
2178                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2179                         message->param.disconnectinfo.cause = 47;
2180                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2181                         message_put(message);
2182                         new_state(PORT_STATE_RELEASE);
2183                         p_m_delete = 1;
2184                         return;
2185                 }
2186                 p_m_mISDNport->procids[i] = 1;
2187                 p_m_d_l3id = 0xff00 | i;
2188         } else
2189         {
2190                 iframe_t ncr;
2191                 /* if we are in te-mode, we need to create a process first */
2192                 if (newteid++ > 0x7fff)
2193                         newteid = 0x0001;
2194                 p_m_d_l3id = (entity<<16) | newteid;
2195                 /* preparing message */
2196                 ncr.prim = CC_NEW_CR | REQUEST; 
2197                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2198                 ncr.dinfo = p_m_d_l3id;
2199                 ncr.len = 0;
2200                 /* send message */
2201                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2202 //              if (!dmsg)
2203 //                      goto nomem;
2204         }
2205         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2206         end_trace();
2207
2208         /* preparing setup message */
2209         dmsg = create_l3msg(CC_SETUP | REQUEST, MT_SETUP, p_m_d_l3id, sizeof(SETUP_t), p_m_d_ntmode);
2210         setup = (SETUP_t *)(dmsg->data + headerlen);
2211         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP | REQUEST, DIRECTION_OUT);
2212         /* channel information */
2213         if (channel >= 0) /* it should */
2214         {
2215                 enc_ie_channel_id(&setup->CHANNEL_ID, dmsg, exclusive, channel);
2216         }
2217         /* caller information */
2218         plan = 1;
2219         switch (p_callerinfo.ntype)
2220         {
2221                 case INFO_NTYPE_INTERNATIONAL:
2222                 type = 0x1;
2223                 break;
2224                 case INFO_NTYPE_NATIONAL:
2225                 type = 0x2;
2226                 break;
2227                 case INFO_NTYPE_SUBSCRIBER:
2228                 type = 0x4;
2229                 break;
2230                 default: /* INFO_NTYPE_UNKNOWN */
2231                 type = 0x0;
2232                 break;
2233         }
2234         switch (p_callerinfo.screen)
2235         {
2236                 case INFO_SCREEN_USER:
2237                 screen = 0;
2238                 break;
2239                 default: /* INFO_SCREEN_NETWORK */
2240                 screen = 3;
2241                 break;
2242         }
2243         switch (p_callerinfo.present)
2244         {
2245                 case INFO_PRESENT_RESTRICTED:
2246                 present = 1;
2247                 break;
2248                 case INFO_PRESENT_NOTAVAIL:
2249                 present = 2;
2250                 break;
2251                 default: /* INFO_PRESENT_ALLOWED */
2252                 present = 0;
2253                 break;
2254         }
2255         if (type >= 0)
2256                 enc_ie_calling_pn(&setup->CALLING_PN, dmsg, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2257         /* dialing information */
2258         if (p_dialinginfo.id[0]) /* only if we have something to dial */
2259         {
2260                 enc_ie_called_pn(&setup->CALLED_PN, dmsg, 0, 1, (unsigned char *)p_dialinginfo.id);
2261         }
2262         /* sending complete */
2263         if (p_dialinginfo.sending_complete)
2264                 enc_ie_complete(&setup->COMPLETE, dmsg, 1);
2265         /* sending user-user */
2266         if (param->setup.useruser.len)
2267         {
2268                 enc_ie_useruser(&setup->USER_USER, dmsg, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2269         }
2270         /* redirecting number */
2271         plan = 1;
2272         switch (p_redirinfo.ntype)
2273         {
2274                 case INFO_NTYPE_INTERNATIONAL:
2275                 type = 0x1;
2276                 break;
2277                 case INFO_NTYPE_NATIONAL:
2278                 type = 0x2;
2279                 break;
2280                 case INFO_NTYPE_SUBSCRIBER:
2281                 type = 0x4;
2282                 break;
2283                 default: /* INFO_NTYPE_UNKNOWN */
2284                 type = 0x0;
2285                 break;
2286         }
2287         switch (p_redirinfo.screen)
2288         {
2289                 case INFO_SCREEN_USER:
2290                 screen = 0;
2291                 break;
2292                 default: /* INFO_SCREE_NETWORK */
2293                 screen = 3;
2294                 break;
2295         }
2296         switch (p_redirinfo.reason)
2297         {
2298                 case INFO_REDIR_BUSY:
2299                 reason = 1;
2300                 break;
2301                 case INFO_REDIR_NORESPONSE:
2302                 reason = 2;
2303                 break;
2304                 case INFO_REDIR_UNCONDITIONAL:
2305                 reason = 15;
2306                 break;
2307                 case INFO_REDIR_CALLDEFLECT:
2308                 reason = 10;
2309                 break;
2310                 case INFO_REDIR_OUTOFORDER:
2311                 reason = 9;
2312                 break;
2313                 default: /* INFO_REDIR_UNKNOWN */
2314                 reason = 0;
2315                 break;
2316         }
2317         switch (p_redirinfo.present)
2318         {
2319                 case INFO_PRESENT_NULL: /* no redir at all */
2320                 present = -1;
2321                 screen = -1;
2322                 reason = -1;
2323                 plan = -1;
2324                 type = -1;
2325                 break;
2326                 case INFO_PRESENT_RESTRICTED:
2327                 present = 1;
2328                 break;
2329                 case INFO_PRESENT_NOTAVAIL:
2330                 present = 2;
2331                 break;
2332                 default: /* INFO_PRESENT_ALLOWED */
2333                 present = 0;
2334                 break;
2335         }
2336         /* sending redirecting number only in ntmode */
2337         if (type >= 0 && p_m_d_ntmode)
2338                 enc_ie_redir_nr(&setup->REDIR_NR, dmsg, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2339         /* bearer capability */
2340 //printf("hlc=%d\n",p_capainfo.hlc);
2341         coding = 0;
2342         capability = p_capainfo.bearer_capa;
2343         mode = p_capainfo.bearer_mode;
2344         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2345         switch (p_capainfo.bearer_info1)
2346         {
2347                 case INFO_INFO1_NONE:
2348                 user = -1;
2349                 break;
2350                 default:
2351                 user = p_capainfo.bearer_info1 & 0x7f;
2352                 break;
2353         }
2354         enc_ie_bearer(&setup->BEARER, dmsg, coding, capability, mode, rate, -1, user);
2355         /* hlc */
2356         if (p_capainfo.hlc)
2357         {
2358                 coding = 0;
2359                 interpretation = 4;
2360                 presentation = 1;
2361                 hlc = p_capainfo.hlc & 0x7f;
2362                 exthlc = -1;
2363                 if (p_capainfo.exthlc)
2364                         exthlc = p_capainfo.exthlc & 0x7f;
2365                 enc_ie_hlc(&setup->HLC, dmsg, coding, interpretation, presentation, hlc, exthlc);
2366         }
2367
2368         /* display */
2369         if (p_callerinfo.display[0] && p_m_d_ntmode)
2370                 enc_ie_display(&setup->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2371 #ifdef CENTREX
2372         /* nt-mode: CNIP (calling name identification presentation) */
2373         if (p_callerinfo.name[0] && p_m_d_ntmode)
2374                 enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2375 #endif
2376         end_trace();
2377
2378         /* send setup message now */
2379         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2380
2381         new_state(PORT_STATE_OUT_SETUP);
2382 }
2383
2384 /* MESSAGE_FACILITY */
2385 void Pdss1::message_facility(unsigned long epoint_id, int message_id, union parameter *param)
2386 {
2387         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2388         FACILITY_t *facility;
2389         msg_t *dmsg;
2390
2391         /* facility will not be sent to external lines */
2392         if (!p_m_d_ntmode)
2393                 return;
2394
2395         /* sending facility */
2396         dmsg = create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, p_m_d_l3id, sizeof(FACILITY_t), p_m_d_ntmode);
2397         facility = (FACILITY_t *)(dmsg->data + headerlen);
2398         l1l2l3_trace_header(p_m_mISDNport, this, CC_FACILITY | REQUEST, DIRECTION_OUT);
2399         enc_ie_facility(&facility->FACILITY, dmsg, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2400         end_trace();
2401         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2402 }
2403
2404 /* MESSAGE_NOTIFY */
2405 void Pdss1::message_notify(unsigned long epoint_id, int message_id, union parameter *param)
2406 {
2407         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2408         INFORMATION_t *information;
2409         NOTIFY_t *notification;
2410         int notify;
2411         int plan, type = -1, present;
2412         msg_t *dmsg;
2413
2414         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2415                 notify = param->notifyinfo.notify & 0x7f;
2416         else
2417                 notify = -1;
2418         if (p_state != PORT_STATE_CONNECT)
2419         {
2420                 /* notify only allowed in active state */
2421                 notify = -1;
2422         }
2423         if (notify >= 0)
2424         {
2425                 plan = 1;
2426                 switch (param->notifyinfo.ntype)
2427                 {
2428                         case INFO_NTYPE_INTERNATIONAL:
2429                         type = 1;
2430                         break;
2431                         case INFO_NTYPE_NATIONAL:
2432                         type = 2;
2433                         break;
2434                         case INFO_NTYPE_SUBSCRIBER:
2435                         type = 4;
2436                         break;
2437                         default: /* INFO_NTYPE_UNKNOWN */
2438                         type = 0;
2439                         break;
2440                 }
2441                 switch (param->notifyinfo.present)
2442                 {
2443                         case INFO_PRESENT_NULL: /* no redir at all */
2444                         present = -1;
2445                         plan = -1;
2446                         type = -1;
2447                         break;
2448                         case INFO_PRESENT_RESTRICTED:
2449                         present = 1;
2450                         break;
2451                         case INFO_PRESENT_NOTAVAIL:
2452                         present = 2;
2453                         break;
2454                         default: /* INFO_PRESENT_ALLOWED */
2455                         present = 0;
2456                         break;
2457                 }
2458         }
2459
2460         if (notify<0 && !param->notifyinfo.display[0])
2461         {
2462                 /* nothing to notify, nothing to display */
2463                 return;
2464         }
2465
2466         if (notify >= 0)
2467         {
2468                 if (p_state!=PORT_STATE_CONNECT)
2469                 {
2470                         /* queue notification */
2471                         if (p_m_d_notify_pending)
2472                                 message_free(p_m_d_notify_pending);
2473                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2474                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2475                 } else
2476                 {
2477                         /* sending notification */
2478                         dmsg = create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, p_m_d_l3id, sizeof(NOTIFY_t), p_m_d_ntmode);
2479                         notification = (NOTIFY_t *)(dmsg->data + headerlen);
2480                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NOTIFY | REQUEST, DIRECTION_OUT);
2481                         enc_ie_notify(&notification->NOTIFY, dmsg, notify);
2482                         /* sending redirection number only in ntmode */
2483                         if (type >= 0 && p_m_d_ntmode)
2484                                 enc_ie_redir_dn(&notification->REDIR_DN, dmsg, type, plan, present, (unsigned char *)param->notifyinfo.id);
2485                         if (param->notifyinfo.display[0] && p_m_d_ntmode)
2486                                 enc_ie_display(&notification->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2487                         end_trace();
2488                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2489                 }
2490         } else if (p_m_d_ntmode)
2491         {
2492                 /* sending information */
2493                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2494                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2495                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2496                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2497                 end_trace();
2498                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2499         }
2500 }
2501
2502 /* MESSAGE_OVERLAP */
2503 void Pdss1::message_overlap(unsigned long epoint_id, int message_id, union parameter *param)
2504 {
2505         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2506         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
2507         msg_t *dmsg;
2508
2509         /* sending setup_acknowledge */
2510         dmsg = create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, sizeof(SETUP_ACKNOWLEDGE_t), p_m_d_ntmode);
2511         setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2512         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
2513         /* channel information */
2514         if (p_state == PORT_STATE_IN_SETUP)
2515                 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2516         /* progress information */
2517         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2518          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2519          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2520         if (p_m_mISDNport->tones)
2521                 enc_ie_progress(&setup_acknowledge->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2522         end_trace();
2523         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2524
2525         new_state(PORT_STATE_IN_OVERLAP);
2526 }
2527
2528 /* MESSAGE_PROCEEDING */
2529 void Pdss1::message_proceeding(unsigned long epoint_id, int message_id, union parameter *param)
2530 {
2531         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2532         CALL_PROCEEDING_t *proceeding;
2533         msg_t *dmsg;
2534
2535         /* sending proceeding */
2536         dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2537         proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2538         l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2539         /* channel information */
2540         if (p_state == PORT_STATE_IN_SETUP)
2541                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2542         /* progress information */
2543         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2544          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2545          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2546         if (p_m_mISDNport->tones)
2547                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2548         end_trace();
2549         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2550
2551         new_state(PORT_STATE_IN_PROCEEDING);
2552 }
2553
2554 /* MESSAGE_ALERTING */
2555 void Pdss1::message_alerting(unsigned long epoint_id, int message_id, union parameter *param)
2556 {
2557         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2558         ALERTING_t *alerting;
2559         msg_t *dmsg;
2560
2561         /* NT-MODE in setup state we must send PROCEEDING first */
2562         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2563         {
2564                 CALL_PROCEEDING_t *proceeding;
2565
2566                 /* sending proceeding */
2567                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2568                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2569                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2570                 /* channel information */
2571                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2572                 /* progress information */
2573                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2574                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2575                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2576                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2577                 end_trace();
2578                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2579                 new_state(PORT_STATE_IN_PROCEEDING);
2580         }
2581
2582         /* sending alerting */
2583         dmsg = create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, p_m_d_l3id, sizeof(ALERTING_t), p_m_d_ntmode);
2584         alerting = (ALERTING_t *)(dmsg->data + headerlen);
2585         l1l2l3_trace_header(p_m_mISDNport, this, CC_ALERTING | REQUEST, DIRECTION_OUT);
2586         /* channel information */
2587         if (p_state == PORT_STATE_IN_SETUP)
2588                 enc_ie_channel_id(&alerting->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2589         /* progress information */
2590         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2591          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2592          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2593         if (p_m_mISDNport->tones)
2594                 enc_ie_progress(&alerting->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2595         end_trace();
2596         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2597
2598         new_state(PORT_STATE_IN_ALERTING);
2599 }
2600
2601 /* MESSAGE_CONNECT */
2602 void Pdss1::message_connect(unsigned long epoint_id, int message_id, union parameter *param)
2603 {
2604         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2605         INFORMATION_t *information;
2606         CONNECT_t *connect;
2607         int type, plan, present, screen;
2608         msg_t *dmsg;
2609         class Endpoint *epoint;
2610
2611         /* NT-MODE in setup state we must send PROCEEDING first */
2612         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2613         {
2614                 CALL_PROCEEDING_t *proceeding;
2615
2616                 /* sending proceeding */
2617                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2618                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2619                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2620                 /* channel information */
2621                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2622 //              /* progress information */
2623 //              if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2624 //               || p_capainfo.bearer_capa==INFO_BC_AUDIO
2625 //               || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2626 //              enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2627                 end_trace();
2628                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2629                 new_state(PORT_STATE_IN_PROCEEDING);
2630         }
2631
2632         /* copy connected information */
2633         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2634
2635         /* only display at connect state */
2636         if (p_state == PORT_STATE_CONNECT)
2637         if (p_connectinfo.display[0])
2638         {
2639                 /* sending information */
2640                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2641                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2642                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2643                 if (p_m_d_ntmode)
2644                         enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2645                 end_trace();
2646                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2647                 return;
2648         }
2649
2650         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)
2651         {
2652                 /* connect command only possible in setup, proceeding or alerting state */
2653                 return;
2654         }
2655
2656         /* preparing connect message */
2657         dmsg = create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, p_m_d_l3id, sizeof(CONNECT_t), p_m_d_ntmode);
2658         connect = (CONNECT_t *)(dmsg->data + headerlen);
2659         l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | REQUEST, DIRECTION_OUT);
2660         /* connect information */
2661         plan = 1;
2662         switch (p_connectinfo.ntype)
2663         {
2664                 case INFO_NTYPE_INTERNATIONAL:
2665                 type = 0x1;
2666                 break;
2667                 case INFO_NTYPE_NATIONAL:
2668                 type = 0x2;
2669                 break;
2670                 case INFO_NTYPE_SUBSCRIBER:
2671                 type = 0x4;
2672                 break;
2673                 default: /* INFO_NTYPE_UNKNOWN */
2674                 type = 0x0;
2675                 break;
2676         }
2677         switch (param->connectinfo.screen)
2678         {
2679                 case INFO_SCREEN_USER:
2680                 screen = 0;
2681                 break;
2682                 default: /* INFO_SCREE_NETWORK */
2683                 screen = 3;
2684                 break;
2685         }
2686         switch (p_connectinfo.present)
2687         {
2688                 case INFO_PRESENT_NULL: /* no colp at all */
2689                 present = -1;
2690                 screen = -1;
2691                 plan = -1;
2692                 type = -1;
2693                 break;
2694                 case INFO_PRESENT_RESTRICTED:
2695                 present = 1;
2696                 break;
2697                 case INFO_PRESENT_NOTAVAIL:
2698                 present = 2;
2699                 break;
2700                 default: /* INFO_PRESENT_ALLOWED */
2701                 present = 0;
2702                 break;
2703         }
2704         if (type >= 0)
2705                 enc_ie_connected_pn(&connect->CONNECT_PN, dmsg, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2706         /* display */
2707         if (p_connectinfo.display[0] && p_m_d_ntmode)
2708                 enc_ie_display(&connect->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2709 #ifdef CENTREX
2710         /* nt-mode: CONP (connected name identification presentation) */
2711         if (p_connectinfo.name[0] && p_m_d_ntmode)
2712                 enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2713 #endif
2714         /* date & time */
2715         if (p_m_d_ntmode)
2716         {
2717                 epoint = find_epoint_id(epoint_id);
2718                 enc_ie_date(&connect->DATE, dmsg, now, p_settings.no_seconds);
2719         }
2720         end_trace();
2721         /* finally send message */
2722         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2723
2724         if (p_m_d_ntmode)
2725                 new_state(PORT_STATE_CONNECT);
2726         else
2727                 new_state(PORT_STATE_CONNECT_WAITING);
2728         set_tone("", NULL);
2729 }
2730
2731 /* MESSAGE_DISCONNECT */
2732 void Pdss1::message_disconnect(unsigned long epoint_id, int message_id, union parameter *param)
2733 {
2734         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2735         DISCONNECT_t *disconnect;
2736         RELEASE_COMPLETE_t *release_complete;
2737         msg_t *dmsg;
2738         struct message *message;
2739         char *p = NULL;
2740
2741         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2742         if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->tones)
2743          || p_state==PORT_STATE_OUT_SETUP)
2744         {
2745                 /* sending release to endpoint */
2746                 while(p_epointlist)
2747                 {
2748                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2749                         message->param.disconnectinfo.cause = 16;
2750                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2751                         message_put(message);
2752                         /* remove epoint */
2753                         free_epointlist(p_epointlist);
2754                 }
2755                 /* sending release */
2756                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2757                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2758                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
2759                 /* send cause */
2760                 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);
2761                 end_trace();
2762                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2763                 new_state(PORT_STATE_RELEASE);
2764                 p_m_delete = 1;
2765                 return;
2766         }
2767
2768         /* NT-MODE in setup state we must send PROCEEDING first */
2769         if (p_state==PORT_STATE_IN_SETUP)
2770         {
2771                 CALL_PROCEEDING_t *proceeding;
2772
2773                 /* sending proceeding */
2774                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2775                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2776                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2777                 /* channel information */
2778                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2779                 /* progress information */
2780                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2781                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2782                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2783                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2784                 end_trace();
2785                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2786                 new_state(PORT_STATE_IN_PROCEEDING);
2787         }
2788
2789         /* sending disconnect */
2790         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2791         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2792         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2793         /* progress information */
2794         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2795          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2796          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2797         if (p_m_mISDNport->tones)
2798                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2799         /* send cause */
2800         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);
2801         /* send display */
2802         if (param->disconnectinfo.display[0])
2803                 p = param->disconnectinfo.display;
2804         if (p) if (*p && p_m_d_ntmode)
2805                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2806         end_trace();
2807         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2808         new_state(PORT_STATE_OUT_DISCONNECT);
2809 }
2810
2811 /* MESSAGE_RELEASE */
2812 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
2813 {
2814         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2815         RELEASE_t *release;
2816         RELEASE_COMPLETE_t *release_complete;
2817         DISCONNECT_t *disconnect;
2818         msg_t *dmsg;
2819         class Endpoint *epoint;
2820         char *p = NULL;
2821
2822         /* if we have incoming disconnected, we may release */
2823         if (p_state==PORT_STATE_IN_DISCONNECT
2824          || p_state==PORT_STATE_OUT_DISCONNECT)
2825         {
2826                 /* sending release */
2827                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
2828                 release = (RELEASE_t *)(dmsg->data + headerlen);
2829                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2830                 /* send cause */
2831                 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);
2832                 end_trace();
2833                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2834                 new_state(PORT_STATE_RELEASE);
2835                 /* remove epoint */
2836                 remove_endpoint:
2837                 free_epointid(epoint_id);
2838                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | REQUEST, DIRECTION_OUT);
2839                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
2840                 end_trace();
2841                 if (p_m_d_ntmode)
2842                 {
2843                         if ((p_m_d_l3id&0xff00) == 0xff00)
2844                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2845                 }
2846                 p_m_d_l3id = 0;
2847                 p_m_delete = 1;
2848                 return;
2849         }
2850         /* if we are on outgoing/incoming call setup, we may release complete */
2851         if (p_state==PORT_STATE_OUT_SETUP
2852          || p_state==PORT_STATE_IN_SETUP
2853 // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
2854          || p_state==PORT_STATE_OUT_PROCEEDING)
2855         {
2856                 /* sending release */
2857                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2858                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2859                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2860                 /* send cause */
2861                 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);
2862                 end_trace();
2863                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2864                 new_state(PORT_STATE_RELEASE);
2865                 goto remove_endpoint;
2866         }
2867
2868         /* NT-MODE in setup state we must send PROCEEDING first */
2869         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2870         {
2871                 CALL_PROCEEDING_t *proceeding;
2872
2873                 /* sending proceeding */
2874                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2875                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2876                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2877                 /* channel information */
2878                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2879                 /* progress information */
2880                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2881                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2882                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2883                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2884                 end_trace();
2885                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2886         }
2887
2888         /* sending disconnect */
2889         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2890         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2891         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2892         /* progress information */
2893         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2894          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2895          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2896         if (p_m_mISDNport->tones)
2897                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2898         /* send cause */
2899         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);
2900         /* send display */
2901         epoint = find_epoint_id(epoint_id);
2902         if (param->disconnectinfo.display[0])
2903                 p = param->disconnectinfo.display;
2904         if (p) if (*p && p_m_d_ntmode)
2905                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2906         end_trace();
2907         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2908         new_state(PORT_STATE_RELEASE);
2909         free_epointid(epoint_id);
2910 //      p_m_delete = 1;
2911 }
2912
2913
2914 /*
2915  * endpoint sends messages to the port
2916  */
2917 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
2918 {
2919         struct message *message;
2920
2921         if (PmISDN::message_epoint(epoint_id, message_id, param))
2922                 return(1);
2923
2924         switch(message_id)
2925         {
2926                 case MESSAGE_INFORMATION: /* overlap dialing */
2927                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2928                  && p_state!=PORT_STATE_OUT_OVERLAP
2929                  && p_state!=PORT_STATE_CONNECT
2930                  && p_state!=PORT_STATE_OUT_DISCONNECT
2931                  && p_state!=PORT_STATE_IN_DISCONNECT)
2932                 {
2933                         break;
2934                 }
2935                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2936                  && p_state!=PORT_STATE_OUT_OVERLAP
2937                  && p_state!=PORT_STATE_OUT_PROCEEDING
2938                  && p_state!=PORT_STATE_OUT_ALERTING
2939                  && p_state!=PORT_STATE_CONNECT
2940                  && p_state!=PORT_STATE_OUT_DISCONNECT
2941                  && p_state!=PORT_STATE_IN_DISCONNECT)
2942                 {
2943                         break;
2944                 }
2945                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
2946                  && p_state!=PORT_STATE_IN_OVERLAP
2947                  && p_state!=PORT_STATE_IN_PROCEEDING
2948                  && p_state!=PORT_STATE_IN_ALERTING
2949                  && p_state!=PORT_STATE_CONNECT
2950                  && p_state!=PORT_STATE_CONNECT_WAITING
2951                  && p_state!=PORT_STATE_OUT_DISCONNECT
2952                  && p_state!=PORT_STATE_IN_DISCONNECT)
2953                 {
2954                         break;
2955                 }
2956                 message_information(epoint_id, message_id, param);
2957                 break;
2958
2959                 case MESSAGE_SETUP: /* dial-out command received from epoint */
2960                 if (p_state!=PORT_STATE_IDLE
2961                  && p_state!=PORT_STATE_CONNECT)
2962                 {
2963                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
2964                         break;
2965                 }
2966                 if (p_epointlist && p_state==PORT_STATE_IDLE)
2967                 {
2968                         PERROR("Pdss1(%s): software error: epoint pointer is set in idle state, how bad!! exitting.\n", p_name);
2969                         exit(-1);
2970                 }
2971                 /* note: pri is a special case, because links must be up for pri */ 
2972                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
2973                 {
2974                         /* LAYER 1 is up, or we may send */
2975                         message_setup(epoint_id, message_id, param);
2976                 } else {
2977                         iframe_t act;
2978                         /* LAYER 1 id down, so we queue */
2979                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
2980                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
2981                         /* attach us */
2982                         if (!(epointlist_new(epoint_id)))
2983                         {
2984                                 PERROR("no memory for epointlist\n");
2985                                 exit(-1);
2986                         }
2987                         /* activate link */
2988                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
2989                         act.prim = PH_ACTIVATE | REQUEST; 
2990                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2991                         act.dinfo = 0;
2992                         act.len = 0;
2993                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
2994                         l1l2l3_trace_header(p_m_mISDNport, this, act.prim, DIRECTION_OUT);
2995                         end_trace();
2996 //                      /* set timeout */
2997 //                      p_m_mISDNport->l1timeout = now+3;
2998                 }
2999                 break;
3000
3001                 case MESSAGE_NOTIFY: /* display and notifications */
3002                 message_notify(epoint_id, message_id, param);
3003                 break;
3004
3005                 case MESSAGE_FACILITY: /* facility message */
3006                 message_facility(epoint_id, message_id, param);
3007                 break;
3008
3009                 case MESSAGE_OVERLAP: /* more information is needed */
3010                 if (p_state!=PORT_STATE_IN_SETUP)
3011                 {
3012                         break;
3013                 }
3014                 message_overlap(epoint_id, message_id, param);
3015                 break;
3016
3017                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
3018                 if (p_state!=PORT_STATE_IN_SETUP
3019                  && p_state!=PORT_STATE_IN_OVERLAP)
3020                 {
3021                         break;
3022                 }
3023                 message_proceeding(epoint_id, message_id, param);
3024                 break;
3025
3026                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
3027                 if (p_state!=PORT_STATE_IN_SETUP
3028                  && p_state!=PORT_STATE_IN_OVERLAP
3029                  && p_state!=PORT_STATE_IN_PROCEEDING)
3030                 {
3031                         break;
3032                 }
3033                 message_alerting(epoint_id, message_id, param);
3034                 break;
3035
3036                 case MESSAGE_CONNECT: /* call of endpoint is connected */
3037                 if (p_state!=PORT_STATE_IN_SETUP
3038                  && p_state!=PORT_STATE_IN_OVERLAP
3039                  && p_state!=PORT_STATE_IN_PROCEEDING
3040                  && p_state!=PORT_STATE_IN_ALERTING
3041                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
3042                 {
3043                         break;
3044                 }
3045                 message_connect(epoint_id, message_id, param);
3046                 break;
3047
3048                 case MESSAGE_DISCONNECT: /* call has been disconnected */
3049                 if (p_state!=PORT_STATE_IN_SETUP
3050                  && p_state!=PORT_STATE_IN_OVERLAP
3051                  && p_state!=PORT_STATE_IN_PROCEEDING
3052                  && p_state!=PORT_STATE_IN_ALERTING
3053                  && p_state!=PORT_STATE_OUT_SETUP
3054                  && p_state!=PORT_STATE_OUT_OVERLAP
3055                  && p_state!=PORT_STATE_OUT_PROCEEDING
3056                  && p_state!=PORT_STATE_OUT_ALERTING
3057                  && p_state!=PORT_STATE_CONNECT
3058                  && p_state!=PORT_STATE_CONNECT_WAITING)
3059                 {
3060                         break;
3061                 }
3062                 message_disconnect(epoint_id, message_id, param);
3063                 break;
3064
3065                 case MESSAGE_RELEASE: /* release isdn port */
3066                 if (p_state==PORT_STATE_RELEASE)
3067                 {
3068                         break;
3069                 }
3070                 message_release(epoint_id, message_id, param);
3071                 break;
3072
3073                 default:
3074                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
3075         }
3076
3077         return(1);
3078 }
3079
3080
3081
3082 /*
3083  * data from isdn-stack (layer-3) to pbx (port class)
3084  */
3085 /* NOTE: nt mode use mISDNuser_head_t as header */
3086 int stack2manager_nt(void *dat, void *arg)
3087 {
3088         class Port *port;
3089         class Pdss1 *pdss1;
3090         manager_t *mgr = (manager_t *)dat;
3091         msg_t *msg = (msg_t *)arg;
3092         mISDNuser_head_t *hh;
3093         struct mISDNport *mISDNport;
3094         char name[32];
3095
3096         if (!msg || !mgr)
3097                 return(-EINVAL);
3098
3099         /* note: nst is the first data feld of mISDNport */
3100         mISDNport = (struct mISDNport *)mgr->nst;
3101
3102         hh = (mISDNuser_head_t *)msg->data;
3103         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3104
3105         /* find Port object of type ISDN */
3106         port = port_first;
3107         while(port)
3108         {
3109                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
3110                 {
3111                         pdss1 = (class Pdss1 *)port;
3112 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
3113                         /* check out correct stack */
3114                         if (pdss1->p_m_mISDNport == mISDNport)
3115                         /* check out correct id */
3116                         if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
3117                         {
3118                                 /* found port, the message belongs to */
3119                                 break;
3120                         }
3121                 }
3122                 port = port->next;
3123         }
3124         if (port)
3125         {
3126                 /* if process id is master process, but a child disconnects */
3127                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00 && hh->prim==(CC_DISCONNECT|INDICATION))
3128                 {
3129                         /* send special indication for child disconnect */
3130                         pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
3131                         free_msg(msg);
3132                         return(0);
3133                 }
3134                 /* if process id and layer 3 id matches */
3135                 if (hh->dinfo == pdss1->p_m_d_l3id)
3136                 {
3137                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3138                         free_msg(msg);
3139                         return(0);
3140                 }
3141         }
3142
3143         /* d-message */
3144         switch(hh->prim)
3145         {
3146                 case MGR_SHORTSTATUS | INDICATION:
3147                 case MGR_SHORTSTATUS | CONFIRM:
3148                 switch(hh->dinfo) {
3149                         case SSTATUS_L2_ESTABLISHED:
3150                         goto ss_estab;
3151                         case SSTATUS_L2_RELEASED:
3152                         goto ss_rel;
3153                 }
3154                 break;
3155
3156                 case DL_ESTABLISH | INDICATION:
3157                 case DL_ESTABLISH | CONFIRM:
3158                 ss_estab:
3159                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3160                 add_trace("tei", NULL, "%d", hh->dinfo);
3161                 end_trace();
3162                 if (mISDNport->ptp && hh->dinfo == 0)
3163                 {
3164                         if (mISDNport->l2establish)
3165                         {
3166                                 mISDNport->l2establish = 0;
3167                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
3168                         }
3169                         mISDNport->l2link = 1;
3170                         if (mISDNport->pri);
3171                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
3172                 }
3173                 break;
3174
3175                 case DL_RELEASE | INDICATION:
3176                 case DL_RELEASE | CONFIRM:
3177                 ss_rel:
3178                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3179                 add_trace("tei", NULL, "%d", hh->dinfo);
3180                 end_trace();
3181                 if (mISDNport->ptp && hh->dinfo == 0)
3182                 {
3183                         mISDNport->l2link = 0;
3184                         time(&mISDNport->l2establish);
3185                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
3186                 }
3187                 break;
3188
3189                 case CC_SETUP | INDICATION:
3190                 /* creating port object */
3191                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3192                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3193                 {
3194                         RELEASE_COMPLETE_t *release_complete;
3195                         msg_t *dmsg;
3196
3197                         PERROR("FATAL ERROR: cannot create port object.\n");
3198                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3199                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3200                         l1l2l3_trace_header(mISDNport, NULL, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
3201                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3202                         end_trace();
3203                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3204                         break;
3205                 }
3206                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3207                 break;
3208
3209                 case CC_RESUME | INDICATION:
3210                 /* creating port object */
3211                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3212                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3213                 {
3214                         SUSPEND_REJECT_t *suspend_reject;
3215                         msg_t *dmsg;
3216
3217                         PERROR("FATAL ERROR: cannot create port object.\n");
3218                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, hh->dinfo, sizeof(SUSPEND_REJECT_t), mISDNport->ntmode);
3219                         suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + mISDN_HEADER_LEN);
3220                         l1l2l3_trace_header(mISDNport, NULL, CC_SUSPEND_REJECT | REQUEST, DIRECTION_OUT);
3221                         enc_ie_cause_standalone(mISDNport->ntmode?&suspend_reject->CAUSE:NULL, dmsg, (mISDNport->ntmode)?1:0, 47);
3222                         end_trace();
3223                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3224                         break;
3225                 }
3226                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3227                 break;
3228
3229                 case CC_RELEASE_CR | INDICATION:
3230                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", hh->dinfo);
3231                 break;
3232
3233                 case CC_DISCONNECT | INDICATION:
3234
3235                 // fall throug
3236                 default:
3237                 PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3238                 return(-EINVAL);
3239         }
3240         free_msg(msg);
3241         return(0);
3242 }
3243
3244 /* NOTE: te mode use iframe_t as header */
3245 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
3246 {
3247         class Port *port;
3248         class Pdss1 *pdss1;
3249         iframe_t *frm;
3250         char name[32];
3251
3252         if (!msg || !mISDNport)
3253                 return(-EINVAL);
3254         frm = (iframe_t *)msg->data;
3255         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3256
3257         /* find Port object of type ISDN */
3258         port = port_first;
3259         while(port)
3260         {
3261                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
3262                 {
3263                         pdss1 = (class Pdss1 *)port;
3264                         /* check out correct stack */
3265                         if (pdss1->p_m_mISDNport == mISDNport)
3266                         /* check out correct id */
3267                         if (frm->dinfo == pdss1->p_m_d_l3id)
3268                         {
3269                                 /* found port, the message belongs to */
3270                                 break;
3271                         }
3272                 }
3273                 port = port->next;
3274         }
3275         if (port)
3276         {
3277                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3278                 free_msg(msg);
3279                 return(0);
3280         }
3281
3282         /* process new cr (before setup indication) */
3283 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
3284         if (frm->prim == (CC_NEW_CR | INDICATION))
3285         {
3286
3287                 /* creating port object */
3288                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3289                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3290                 {
3291                         RELEASE_COMPLETE_t *release_complete;
3292                         msg_t *dmsg;
3293
3294                         PERROR("FATAL ERROR: cannot create port object.\n");
3295                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, frm->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3296                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3297                         l1l2l3_trace_header(mISDNport, NULL, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
3298                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3299                         end_trace();
3300                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3301                         free_msg(msg);
3302                         return(0);
3303                 }
3304                 /* l3id will be set from dinfo at message_isdn */
3305                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3306                 free_msg(msg);
3307                 return(0);
3308         }
3309
3310         if (frm->prim == (CC_RELEASE_CR | INDICATION))
3311         {
3312                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
3313                 free_msg(msg);
3314                 return(0);
3315         }
3316         PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3317         return(-EINVAL);
3318 }
3319
3320
3321 /*
3322  * sending message that were queued during L1 activation
3323  * or releasing port if link is down
3324  */
3325 void setup_queue(struct mISDNport *mISDNport, int link)
3326 {
3327         class Port *port;
3328         class Pdss1 *pdss1;
3329         struct message *message;
3330
3331         if (!mISDNport->ntmode)
3332                 return;
3333
3334         /* check all port objects for pending message */
3335         port = port_first;
3336         while(port)
3337         {
3338                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
3339                 {
3340                         pdss1 = (class Pdss1 *)port;
3341                         if (pdss1->p_m_mISDNport == mISDNport)
3342                         {
3343                                 if (pdss1->p_m_d_queue)
3344                                 {
3345                                         if (link)
3346                                         {
3347                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3348                                                 /* LAYER 1 is up, so we send */
3349                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
3350                                                 message_free(pdss1->p_m_d_queue);
3351                                                 pdss1->p_m_d_queue = NULL;
3352                                         } else
3353                                         {
3354                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3355                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
3356                                                 message->param.disconnectinfo.cause = 27;
3357                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3358                                                 message_put(message);
3359                                                 pdss1->new_state(PORT_STATE_RELEASE);
3360                                                 pdss1->p_m_delete = 1;
3361                                         }
3362                                 }
3363                         }
3364                 }
3365                 port = port->next;
3366         }
3367 }
3368
3369
3370
3371
3372