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