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