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