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