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