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