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