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