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