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