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