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