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