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