Added GSM network support.
[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         SCAT(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
1070         /* release if remote sends us no tones */
1071         if (!p_m_mISDNport->earlyb) {
1072                 l3_msg *l3m;
1073
1074                 l3m = create_l3msg();
1075                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
1076                 enc_ie_cause(l3m, location, cause); /* normal */
1077                 add_trace("reason", NULL, "no remote patterns");
1078                 end_trace();
1079                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
1080
1081                 /* sending release to endpoint */
1082                 if (location == LOCATION_PRIVATE_LOCAL)
1083                         location = LOCATION_PRIVATE_REMOTE;
1084                 while(p_epointlist) {
1085                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1086                         message->param.disconnectinfo.cause = cause;
1087                         message->param.disconnectinfo.location = location;
1088                         SCAT(message->param.disconnectinfo.display, (char *)display);
1089                         message_put(message);
1090                         /* remove epoint */
1091                         free_epointlist(p_epointlist);
1092                 }
1093                 new_state(PORT_STATE_RELEASE);
1094                 p_m_delete = 1;
1095                 return;
1096         }
1097
1098         /* sending disconnect to active endpoint and release to inactive endpoints */
1099         if (location == LOCATION_PRIVATE_LOCAL)
1100                 location = LOCATION_PRIVATE_REMOTE;
1101         if (ACTIVE_EPOINT(p_epointlist)) {
1102                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
1103                 message->param.disconnectinfo.location = location;
1104                 message->param.disconnectinfo.cause = cause;
1105                 SCAT(message->param.disconnectinfo.display, (char *)display);
1106                 message_put(message);
1107         }
1108         while(INACTIVE_EPOINT(p_epointlist)) {
1109                 message = message_create(p_serial, INACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1110                 message->param.disconnectinfo.location = location;
1111                 message->param.disconnectinfo.cause = cause;
1112                 SCAT(message->param.disconnectinfo.display, (char *)display);
1113                 message_put(message);
1114                 /* remove epoint */
1115                 free_epointid(INACTIVE_EPOINT(p_epointlist));
1116         }
1117         new_state(PORT_STATE_IN_DISCONNECT);
1118 }
1119
1120 /* CC_DISCONNECT INDICATION */
1121 void Pdss1::disconnect_ind_i(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1122 {
1123         int location, cause;
1124
1125         /* cause */
1126         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_IND, DIRECTION_IN);
1127         if (p_m_d_collect_cause > 0) {
1128                 add_trace("old-cause", "location", "%d", p_m_d_collect_location);
1129                 add_trace("old-cause", "value", "%d", p_m_d_collect_cause);
1130         }
1131         dec_ie_cause(l3m, &location, &cause);
1132         if (location == LOCATION_PRIVATE_LOCAL)
1133                 location = LOCATION_PRIVATE_REMOTE;
1134
1135         /* collect cause */
1136         collect_cause(&p_m_d_collect_cause, &p_m_d_collect_location, cause, location);
1137         add_trace("new-cause", "location", "%d", p_m_d_collect_location);
1138         add_trace("new-cause", "value", "%d", p_m_d_collect_cause);
1139         end_trace();
1140
1141 }
1142
1143 /* CC_RELEASE INDICATION */
1144 void Pdss1::release_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1145 {
1146         int location, cause;
1147         struct lcr_msg *message;
1148         unsigned char display[128] = "";
1149
1150         l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_IND, DIRECTION_IN);
1151         dec_ie_cause(l3m, &location, &cause);
1152         dec_ie_display(l3m, (unsigned char *)display, sizeof(display));
1153         end_trace();
1154
1155         if (cause < 0)
1156                 cause = 16;
1157
1158         /* sending release to endpoint */
1159         if (location == LOCATION_PRIVATE_LOCAL)
1160                 location = LOCATION_PRIVATE_REMOTE;
1161         while(p_epointlist) {
1162                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1163                 message->param.disconnectinfo.cause = cause;
1164                 message->param.disconnectinfo.location = location;
1165                 SCAT(message->param.disconnectinfo.display, (char *)display);
1166                 message_put(message);
1167                 /* remove epoint */
1168                 free_epointlist(p_epointlist);
1169         }
1170
1171         new_state(PORT_STATE_RELEASE);
1172         p_m_delete = 1;
1173 }
1174
1175 /* CC_RESTART INDICATION */
1176 void Pdss1::restart_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1177 {
1178         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESTART_IND, DIRECTION_IN);
1179         end_trace();
1180
1181         // L3 process is not toucht. (not even by network stack)
1182 }
1183
1184 /* CC_RELEASE_COMPLETE INDICATION (a reject) */
1185 void Pdss1::release_complete_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1186 {
1187         int location, cause;
1188         struct lcr_msg *message;
1189         
1190         l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_IND, DIRECTION_IN);
1191         /* in case layer 2 is down during setup, we send cause 27 loc 5 */
1192         if (p_state == PORT_STATE_OUT_SETUP && p_m_mISDNport->l1link == 0) {
1193                 cause = 27;
1194                 location = 5;
1195         } else {
1196                 dec_ie_cause(l3m, &location, &cause);
1197                 if (p_m_mISDNport->l1link < 0)
1198                         add_trace("layer 1", NULL, "unknown");
1199                 else
1200                         add_trace("layer 1", NULL, (p_m_mISDNport->l1link)?"up":"down");
1201         }
1202         end_trace();
1203         if (location == LOCATION_PRIVATE_LOCAL)
1204                 location = LOCATION_PRIVATE_REMOTE;
1205
1206         if (cause < 0)
1207                 cause = 16;
1208
1209         /* sending release to endpoint */
1210         while(p_epointlist) {
1211                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1212                 message->param.disconnectinfo.cause = cause;
1213                 message->param.disconnectinfo.location = location;
1214                 message_put(message);
1215                 /* remove epoint */
1216                 free_epointlist(p_epointlist);
1217         }
1218
1219         new_state(PORT_STATE_RELEASE);
1220         p_m_delete = 1;
1221 }
1222
1223 /* T312 timeout  */
1224 void Pdss1::t312_timeout_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1225 {
1226         // not required, release is performed with MT_FREE
1227 }
1228
1229 /* CC_NOTIFY INDICATION */
1230 void Pdss1::notify_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1231 {
1232         struct lcr_msg *message;
1233         int notify, type, plan, present;
1234         unsigned char notifyid[sizeof(message->param.notifyinfo.id)];
1235         unsigned char display[128] = "";
1236
1237         l1l2l3_trace_header(p_m_mISDNport, this, L3_NOTIFY_IND, DIRECTION_IN);
1238         dec_ie_notify(l3m, &notify);
1239         dec_ie_redir_dn(l3m, &type, &plan, &present, notifyid, sizeof(notifyid));
1240         dec_ie_display(l3m, (unsigned char *)display, sizeof(display));
1241         end_trace();
1242
1243         if (!ACTIVE_EPOINT(p_epointlist))
1244                 return;
1245         /* notification indicator */
1246         if (notify < 0)
1247                 return;
1248         notify |= 0x80;
1249         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1250         message->param.notifyinfo.notify = notify;
1251         SCPY(message->param.notifyinfo.id, (char *)notifyid);
1252         /* redirection number */
1253         switch (present) {
1254                 case 1:
1255                 message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1256                 break;
1257                 case 2:
1258                 message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1259                 break;
1260                 default:
1261                 message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1262                 break;
1263         }
1264         switch (type) {
1265                 case -1:
1266                 message->param.notifyinfo.ntype = INFO_NTYPE_NOTPRESENT;
1267                 break;
1268                 case 1:
1269                 message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1270                 break;
1271                 case 2:
1272                 message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1273                 break;
1274                 case 4:
1275                 message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1276                 break;
1277                 default:
1278                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1279                 break;
1280         }
1281         SCAT(message->param.notifyinfo.display, (char *)display);
1282         message->param.notifyinfo.isdn_port = p_m_portnum;
1283         message_put(message);
1284 }
1285
1286
1287 /* CC_HOLD INDICATION */
1288         struct lcr_msg *message;
1289 void Pdss1::hold_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1290 {
1291 //      class Endpoint *epoint;
1292
1293         l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_IND, DIRECTION_IN);
1294         end_trace();
1295
1296         if (!ACTIVE_EPOINT(p_epointlist) || p_m_hold) {
1297                 l3m = create_l3msg();
1298                 l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_REJECT_REQ, DIRECTION_OUT);
1299                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, p_m_hold?101:31); /* normal unspecified / incompatible state */
1300                 add_trace("reason", NULL, "no endpoint");
1301                 end_trace();
1302                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_HOLD_REJECT, p_m_d_l3id, l3m);
1303
1304                 return;
1305         }
1306
1307         /* notify the hold of call */
1308         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1309         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1310         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1311         message_put(message);
1312
1313         /* deactivate bchannel */
1314         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (hold)", DIRECTION_NONE);
1315         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1316         end_trace();
1317         drop_bchannel();
1318
1319         /* set hold state */
1320         p_m_hold = 1;
1321 #if 0
1322         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1323         if (epoint && p_m_d_ntmode) {
1324                 p_m_timeout = p_settings.tout_hold;
1325                 time(&p_m_timer);
1326         }
1327 #endif
1328
1329         /* acknowledge hold */
1330         l3m = create_l3msg();
1331         l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1332         end_trace();
1333         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_HOLD_ACKNOWLEDGE, p_m_d_l3id, l3m);
1334 }
1335
1336
1337 /* CC_RETRIEVE INDICATION */
1338 void Pdss1::retrieve_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1339 {
1340         struct lcr_msg *message;
1341         int channel, exclusive, cause;
1342         int ret;
1343
1344         l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_IND, DIRECTION_IN);
1345         dec_ie_channel_id(l3m, &exclusive, &channel);
1346         end_trace();
1347
1348         if (!p_m_hold) {
1349                 cause = 101; /* incompatible state */
1350                 reject:
1351
1352                 l3m = create_l3msg();
1353                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_REJECT_REQ, DIRECTION_OUT);
1354                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1355                 end_trace();
1356                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RETRIEVE_REJECT, p_m_d_l3id, l3m);
1357
1358                 return;
1359         }
1360
1361         /* notify the retrieve of call */
1362         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1363         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1364         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1365         message_put(message);
1366
1367         /* hunt channel */
1368         ret = channel = hunt_bchannel(channel, exclusive);
1369         if (ret < 0)
1370                 goto no_channel;
1371
1372         /* open channel */
1373         ret = seize_bchannel(channel, 1);
1374         if (ret < 0) {
1375                 no_channel:
1376                 cause = -ret;
1377                 goto reject;
1378         }
1379         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1380
1381         /* set hold state */
1382         p_m_hold = 0;
1383         p_m_timeout = 0;
1384
1385         /* acknowledge retrieve */
1386         l3m = create_l3msg();
1387         l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1388         enc_ie_channel_id(l3m, 1, p_m_b_channel);
1389         end_trace();
1390         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RETRIEVE_ACKNOWLEDGE, p_m_d_l3id, l3m);
1391 }
1392
1393 /* CC_SUSPEND INDICATION */
1394 void Pdss1::suspend_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1395 {
1396         struct lcr_msg *message;
1397         class Endpoint *epoint;
1398         unsigned char callid[8];
1399         int len;
1400         int ret = -31; /* normal, unspecified */
1401
1402         l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_IND, DIRECTION_IN);
1403         dec_ie_call_id(l3m, callid, &len);
1404         end_trace();
1405
1406         if (!ACTIVE_EPOINT(p_epointlist)) {
1407                 reject:
1408                 l3m = create_l3msg();
1409                 l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_REJECT_REQ, DIRECTION_OUT);
1410                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1411                 end_trace();
1412                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SUSPEND_REJECT, p_m_d_l3id, l3m);
1413                 return;
1414         }
1415
1416         /* call id */
1417         if (len<0) len = 0;
1418
1419         /* check if call id is in use */
1420         epoint = epoint_first;
1421         while(epoint) {
1422                 if (epoint->ep_park) {
1423                         if (epoint->ep_park_len == len)
1424                         if (!memcmp(epoint->ep_park_callid, callid, len)) {
1425                                 ret = -84; /* call id in use */
1426                                 goto reject;
1427                         }
1428                 }
1429                 epoint = epoint->next;
1430         }
1431
1432         /* notify the hold of call */
1433         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1434         message->param.notifyinfo.notify = INFO_NOTIFY_USER_SUSPENDED;
1435         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1436         message_put(message);
1437
1438         /* deactivate bchannel */
1439         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (suspend)", DIRECTION_NONE);
1440         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1441         end_trace();
1442         drop_bchannel();
1443
1444         /* sending suspend to endpoint */
1445         while (p_epointlist) {
1446                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_SUSPEND);
1447                 memcpy(message->param.parkinfo.callid, callid, sizeof(message->param.parkinfo.callid));
1448                 message->param.parkinfo.len = len;
1449                 message_put(message);
1450                 /* remove epoint */
1451                 free_epointlist(p_epointlist);
1452         }
1453
1454         /* sending SUSPEND_ACKNOWLEDGE */
1455         l3m = create_l3msg();
1456         l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1457         end_trace();
1458         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SUSPEND_ACKNOWLEDGE, p_m_d_l3id, l3m);
1459
1460         new_state(PORT_STATE_RELEASE);
1461         p_m_delete = 1;
1462 }
1463
1464 /* CC_RESUME INDICATION */
1465 void Pdss1::resume_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1466 {
1467         unsigned char callid[8];
1468         int len;
1469         int channel, exclusive;
1470         class Endpoint *epoint;
1471         struct lcr_msg *message;
1472         int ret;
1473
1474         /* process given callref */
1475         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
1476         add_trace("callref", "new", "0x%x", pid);
1477         if (p_m_d_l3id) {
1478                 /* release is case the ID is already in use */
1479                 add_trace("error", NULL, "callref already in use");
1480                 end_trace();
1481                 l3m = create_l3msg();
1482                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_REJECT_REQ, DIRECTION_OUT);
1483                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
1484                 add_trace("reason", NULL, "callref already in use");
1485                 end_trace();
1486                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_REJECT, pid, l3m);
1487                 new_state(PORT_STATE_RELEASE);
1488                 p_m_delete = 1;
1489                 return;
1490         }
1491         p_m_d_l3id = pid;
1492         p_m_d_ces = pid >> 16;
1493         end_trace();
1494
1495         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_IND, DIRECTION_IN);
1496         dec_ie_call_id(l3m, callid, &len);
1497         end_trace();
1498
1499         /* if blocked, release call */
1500         if (p_m_mISDNport->ifport->block) {
1501                 ret = -27;
1502                 goto reject;
1503         }
1504
1505         /* call id */
1506         if (len<0) len = 0;
1507
1508         /* channel_id (no channel is possible in message) */
1509         exclusive = 0;
1510         channel = -1; /* any channel */
1511
1512         /* hunt channel */
1513         ret = channel = hunt_bchannel(channel, exclusive);
1514         if (ret < 0)
1515                 goto no_channel;
1516
1517 // mode (if hdlc parked) to be done. never mind, this is almost never requested
1518         /* open channel */
1519         ret = seize_bchannel(channel, 1);
1520         if (ret < 0) {
1521                 no_channel:
1522                 reject:
1523                 l3m = create_l3msg();
1524                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_REJECT_REQ, DIRECTION_OUT);
1525                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1526                 if (ret == -27)
1527                         add_trace("reason", NULL, "port blocked");
1528                 end_trace();
1529                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_REJECT, p_m_d_l3id, l3m);
1530                 new_state(PORT_STATE_RELEASE);
1531                 p_m_delete = 1;
1532                 return;
1533         }
1534         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1535
1536         /* create endpoint */
1537         if (p_epointlist)
1538                 FATAL("Incoming resume but already got an endpoint.\n");
1539         ret = -85; /* no call suspended */
1540         epoint = epoint_first;
1541         while(epoint) {
1542                 if (epoint->ep_park) {
1543                         ret = -83; /* suspended call exists, but this not */
1544                         if (epoint->ep_park_len == len)
1545                         if (!memcmp(epoint->ep_park_callid, callid, len))
1546                                 break;
1547                 }
1548                 epoint = epoint->next;
1549         }
1550         if (!epoint)
1551                 goto reject;
1552
1553         epointlist_new(epoint->ep_serial);
1554         if (!(epoint->portlist_new(p_serial, p_type, p_m_mISDNport->earlyb)))
1555                 FATAL("No memory for portlist\n");
1556         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RESUME);
1557         message_put(message);
1558
1559         /* notify the resume of call */
1560         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1561         message->param.notifyinfo.notify = INFO_NOTIFY_USER_RESUMED;
1562         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1563         message_put(message);
1564
1565         /* sending RESUME_ACKNOWLEDGE */
1566         l3m = create_l3msg();
1567         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1568         enc_ie_channel_id(l3m, 1, p_m_b_channel);
1569         end_trace();
1570         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_ACKNOWLEDGE, p_m_d_l3id, l3m);
1571
1572         new_state(PORT_STATE_CONNECT);
1573 }
1574
1575
1576 /* CC_FACILITY INDICATION */
1577 void Pdss1::facility_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1578 {
1579         unsigned char facil[256];
1580         int facil_len;
1581         struct lcr_msg *message;
1582
1583         l1l2l3_trace_header(p_m_mISDNport, this, L3_FACILITY_IND, DIRECTION_IN);
1584         dec_ie_facility(l3m, facil, &facil_len);
1585         end_trace();
1586
1587         /* facility */
1588         if (facil_len<=0)
1589                 return;
1590
1591         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_FACILITY);
1592         message->param.facilityinfo.len = facil_len;
1593         memcpy(message->param.facilityinfo.data, facil, facil_len);
1594         message_put(message);
1595 }
1596
1597
1598 /* CC_PROGRESS INDICATION */
1599 void Pdss1::progress_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1600 {
1601         int coding, location, progress;
1602
1603         l1l2l3_trace_header(p_m_mISDNport, this, L3_PROGRESS_IND, DIRECTION_IN);
1604         dec_ie_progress(l3m, &coding, &location, &progress);
1605         end_trace();
1606 }
1607
1608
1609 /*
1610  * handler for isdn connections
1611  * incoming information are parsed and sent via message to the endpoint
1612  */
1613 void Pdss1::message_isdn(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1614 {
1615         int timer = 0;
1616
1617         switch (cmd) {
1618                 case MT_TIMEOUT:
1619                 if (!l3m->cause) {
1620                         PERROR("Pdss1(%s) timeout without cause.\n", p_name);
1621                         break;
1622                 }
1623                 if (l3m->cause[0] != 5) {
1624                         PERROR("Pdss1(%s) expecting timeout with timer diagnostic. (got len=%d)\n", p_name, l3m->cause[0]);
1625                         break;
1626                 }
1627                 timer = (l3m->cause[3]-'0')*100;
1628                 timer += (l3m->cause[4]-'0')*10;
1629                 timer += (l3m->cause[5]-'0');
1630                 l1l2l3_trace_header(p_m_mISDNport, this, L3_TIMEOUT_IND, DIRECTION_IN);
1631                 add_trace("timer", NULL, "%d", timer);
1632                 end_trace();
1633                 if (timer == 312)
1634                         t312_timeout_ind(cmd, pid, l3m);
1635                 break;
1636
1637                 case MT_SETUP:
1638                 if (p_state != PORT_STATE_IDLE)
1639                         break;
1640                 setup_ind(cmd, pid, l3m);
1641                 break;
1642
1643                 case MT_INFORMATION:
1644                 information_ind(cmd, pid, l3m);
1645                 break;
1646
1647                 case MT_SETUP_ACKNOWLEDGE:
1648                 if (p_state != PORT_STATE_OUT_SETUP) {
1649                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
1650                         break;
1651                 }
1652                 setup_acknowledge_ind(cmd, pid, l3m);
1653                 break;
1654
1655                 case MT_CALL_PROCEEDING:
1656                 if (p_state != PORT_STATE_OUT_SETUP
1657                  && p_state != PORT_STATE_OUT_OVERLAP) {
1658                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
1659                         break;
1660                 }
1661                 proceeding_ind(cmd, pid, l3m);
1662                 break;
1663
1664                 case MT_ALERTING:
1665                 if (p_state != PORT_STATE_OUT_SETUP
1666                  && p_state != PORT_STATE_OUT_OVERLAP
1667                  && p_state != PORT_STATE_OUT_PROCEEDING) {
1668                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
1669                         break;
1670                 }
1671                 alerting_ind(cmd, pid, l3m);
1672                 break;
1673
1674                 case MT_CONNECT:
1675                 if (p_state != PORT_STATE_OUT_SETUP
1676                  && p_state != PORT_STATE_OUT_OVERLAP
1677                  && p_state != PORT_STATE_OUT_PROCEEDING
1678                  && p_state != PORT_STATE_OUT_ALERTING) {
1679                         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);
1680                         break;
1681                 }
1682                 connect_ind(cmd, pid, l3m);
1683                 if (p_m_d_notify_pending) {
1684                         /* send pending notify message during connect */
1685                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1686                         message_free(p_m_d_notify_pending);
1687                         p_m_d_notify_pending = NULL;
1688                 }
1689                 break;
1690
1691                 case MT_CONNECT_ACKNOWLEDGE:
1692                 if (p_state == PORT_STATE_CONNECT_WAITING)
1693                         new_state(PORT_STATE_CONNECT);
1694                 if (p_m_d_notify_pending) {
1695                         /* send pending notify message during connect-ack */
1696                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1697                         message_free(p_m_d_notify_pending);
1698                         p_m_d_notify_pending = NULL;
1699                 }
1700                 break;
1701
1702                 case MT_DISCONNECT:
1703                 disconnect_ind(cmd, pid, l3m);
1704                 break;
1705
1706                 case MT_RELEASE:
1707                 release_ind(cmd, pid, l3m);
1708                 break;
1709
1710                 case MT_RELEASE_COMPLETE:
1711                 release_complete_ind(cmd, pid, l3m);
1712                 break;
1713
1714                 case MT_RESTART:
1715                 restart_ind(cmd, pid, l3m);
1716                 break;
1717
1718                 case MT_NOTIFY:
1719                 notify_ind(cmd, pid, l3m);
1720                 break;
1721
1722                 case MT_HOLD:
1723                 hold_ind(cmd, pid, l3m);
1724                 break;
1725
1726                 case MT_RETRIEVE:
1727                 retrieve_ind(cmd, pid, l3m);
1728                 break;
1729
1730                 case MT_SUSPEND:
1731                 suspend_ind(cmd, pid, l3m);
1732                 break;
1733
1734                 case MT_RESUME:
1735                 resume_ind(cmd, pid, l3m);
1736                 break;
1737
1738                 case MT_FACILITY:
1739                 facility_ind(cmd, pid, l3m);
1740                 break;
1741
1742                 case MT_PROGRESS:
1743                 progress_ind(cmd, pid, l3m);
1744                 break;
1745
1746                 case MT_FREE:
1747                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_L3ID_IND, DIRECTION_IN);
1748                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
1749                 end_trace();
1750                 p_m_d_l3id = 0;
1751                 p_m_d_ces = -1;
1752                 p_m_delete = 1;
1753                 /* sending release to endpoint in case we still have an endpoint
1754                  * this is because we don't get any response if a release_complete is received (or a release in release state)
1755                  */
1756                 while(p_epointlist) { // only if not already released
1757                         struct lcr_msg *message;
1758                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1759                         if (p_m_d_collect_cause) {
1760                                 message->param.disconnectinfo.cause = p_m_d_collect_cause;
1761                                 message->param.disconnectinfo.location = p_m_d_collect_location;
1762                         } else {
1763                                 message->param.disconnectinfo.cause = CAUSE_NOUSER;
1764                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1765                         }
1766                         message_put(message);
1767                         /* remove epoint */
1768                         free_epointlist(p_epointlist);
1769
1770                         new_state(PORT_STATE_RELEASE);
1771                 }
1772                 break;
1773
1774                 default:
1775                 l1l2l3_trace_header(p_m_mISDNport, this, L3_UNKNOWN_IND, DIRECTION_IN);
1776                 add_trace("unhandled", "cmd", "0x%x", cmd);
1777                 end_trace();
1778         }
1779 }
1780
1781 void Pdss1::new_state(int state)
1782 {
1783 //      class Endpoint *epoint;
1784
1785         /* set timeout */
1786         if (state == PORT_STATE_IN_OVERLAP) {
1787                 p_m_timeout = p_m_mISDNport->ifport->tout_dialing;
1788                 time(&p_m_timer);
1789         }
1790         if (state != p_state) {
1791                 if (state == PORT_STATE_IN_SETUP
1792                  || state == PORT_STATE_OUT_SETUP
1793                  || state == PORT_STATE_IN_OVERLAP
1794                  || state == PORT_STATE_OUT_OVERLAP) {
1795                         p_m_timeout = p_m_mISDNport->ifport->tout_setup;
1796                         time(&p_m_timer);
1797                 }
1798                 if (state == PORT_STATE_IN_PROCEEDING
1799                  || state == PORT_STATE_OUT_PROCEEDING) {
1800                         p_m_timeout = p_m_mISDNport->ifport->tout_proceeding;
1801                         time(&p_m_timer);
1802                 }
1803                 if (state == PORT_STATE_IN_ALERTING
1804                  || state == PORT_STATE_OUT_ALERTING) {
1805                         p_m_timeout = p_m_mISDNport->ifport->tout_alerting;
1806                         time(&p_m_timer);
1807                 }
1808                 if (state == PORT_STATE_CONNECT
1809                  || state == PORT_STATE_CONNECT_WAITING) {
1810                         p_m_timeout = 0;
1811                 }
1812                 if (state == PORT_STATE_IN_DISCONNECT
1813                  || state == PORT_STATE_OUT_DISCONNECT) {
1814                         p_m_timeout = p_m_mISDNport->ifport->tout_disconnect;
1815                         time(&p_m_timer);
1816                 }
1817         }
1818         
1819         Port::new_state(state);
1820 }
1821
1822
1823 /*
1824  * handler
1825  */
1826 int Pdss1::handler(void)
1827 {
1828         int ret;
1829
1830         if ((ret = PmISDN::handler()))
1831                 return(ret);
1832
1833         /* handle destruction */
1834         if (p_m_delete && p_m_d_l3id==0) {
1835                 delete this;
1836                 return(-1);
1837         }
1838
1839         return(0);
1840 }
1841
1842
1843 /*
1844  * handles all messages from endpoint
1845  */
1846 /* MESSAGE_INFORMATION */
1847 void Pdss1::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1848 {
1849         l3_msg *l3m;
1850         char *display = param->information.display;
1851         char *number = param->information.id;
1852         int max = p_m_mISDNport->ifport->dialmax;
1853
1854         while (number[0]) { /* as long we have something to dial */
1855                 if (max > (int)strlen(number) || max == 0)
1856                         max = (int)strlen(number);
1857       
1858                 l3m = create_l3msg();
1859                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
1860                 enc_ie_called_pn(l3m, 0, 1, (unsigned char *)number, max);
1861                 if ((p_m_d_ntmode || p_m_d_tespecial) && display[0]) {
1862                         enc_ie_display(l3m, (unsigned char *)display);
1863                         display = "";
1864                 }
1865                 end_trace();
1866                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
1867                 number += max;
1868         }
1869         new_state(p_state);
1870 }
1871
1872
1873 int newteid = 0;
1874
1875 /* MESSAGE_SETUP */
1876 void Pdss1::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1877 {
1878         l3_msg *l3m;
1879         int ret;
1880         int plan, type, screen, present, reason;
1881         int plan2, type2, screen2, present2;
1882         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
1883         int channel, exclusive;
1884         struct epoint_list *epointlist;
1885         int max = p_m_mISDNport->ifport->dialmax;
1886
1887         /* release if port is blocked */
1888         if (p_m_mISDNport->ifport->block) {
1889                 struct lcr_msg *message;
1890
1891                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1892                 message->param.disconnectinfo.cause = 27; // temp. unavail.
1893                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1894                 message_put(message);
1895                 new_state(PORT_STATE_RELEASE);
1896                 p_m_delete = 1;
1897                 return;
1898         }
1899
1900         /* copy setup infos to port */
1901         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
1902         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
1903         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
1904         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
1905 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
1906         /* screen outgoing caller id */
1907         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1908         do_screen(1, p_callerinfo.id2, sizeof(p_callerinfo.id2), &p_callerinfo.ntype2, &p_callerinfo.present2, p_m_mISDNport->ifport->interface);
1909
1910         /* only display at connect state: this case happens if endpoint is in connected mode */
1911         if (p_state==PORT_STATE_CONNECT) {
1912                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
1913                  && p_type!=PORT_TYPE_DSS1_NT_IN)
1914                         return;
1915                 if (p_callerinfo.display[0]) {
1916                         /* sending information */
1917                         l3m = create_l3msg();
1918                         l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
1919                         if (p_m_d_ntmode || p_m_d_tespecial)
1920                                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
1921                         end_trace();
1922                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
1923                         return;
1924                 }
1925         }
1926
1927         /* attach only if not already */
1928         epointlist = p_epointlist;
1929         while(epointlist) {
1930                 if (epointlist->epoint_id == epoint_id)
1931                         break;
1932                 epointlist = epointlist->next;
1933         }
1934         if (!epointlist)
1935                 epointlist_new(epoint_id);
1936
1937         /* get channel */
1938         exclusive = 0;
1939         if (p_m_b_channel) {
1940                 channel = p_m_b_channel;
1941                 exclusive = p_m_b_exclusive;
1942         } else
1943                 channel = CHANNEL_ANY;
1944         /* nt-port with no channel, not reserverd */
1945         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
1946                 channel = CHANNEL_NO;
1947
1948         /* creating l3id */
1949         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1950         /* see MT_ASSIGN notes at do_layer3() */
1951         mt_assign_pid = 0;
1952         ret = p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ASSIGN, 0, NULL);
1953         if (mt_assign_pid == 0 || ret < 0) {
1954                 struct lcr_msg *message;
1955
1956                 add_trace("callref", NULL, "no free id");
1957                 end_trace();
1958                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1959                 message->param.disconnectinfo.cause = 47;
1960                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1961                 message_put(message);
1962                 new_state(PORT_STATE_RELEASE);
1963                 p_m_delete = 1;
1964                 return;
1965         }
1966         p_m_d_l3id = mt_assign_pid;
1967         mt_assign_pid = ~0;
1968         add_trace("callref", "new", "0x%x", p_m_d_l3id);
1969         end_trace();
1970
1971         /* preparing setup message */
1972         l3m = create_l3msg();
1973         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_REQ, DIRECTION_OUT);
1974         /* channel information */
1975         if (channel >= 0) /* it should */
1976                 enc_ie_channel_id(l3m, exclusive, channel);
1977         /* caller information */
1978         plan = 1;
1979         switch (p_callerinfo.ntype) {
1980                 case INFO_NTYPE_UNKNOWN:
1981                 type = 0x0;
1982                 break;
1983                 case INFO_NTYPE_INTERNATIONAL:
1984                 type = 0x1;
1985                 break;
1986                 case INFO_NTYPE_NATIONAL:
1987                 type = 0x2;
1988                 break;
1989                 case INFO_NTYPE_SUBSCRIBER:
1990                 type = 0x4;
1991                 break;
1992                 default: /* INFO_NTYPE_NOTPRESENT */
1993                 type = -1;
1994                 break;
1995         }
1996         switch (p_callerinfo.screen) {
1997                 case INFO_SCREEN_USER:
1998                 screen = 0;
1999                 break;
2000                 default: /* INFO_SCREEN_NETWORK */
2001                 screen = 3;
2002                 break;
2003         }
2004         switch (p_callerinfo.present) {
2005                 case INFO_PRESENT_ALLOWED:
2006                 present = 0;
2007                 break;
2008                 case INFO_PRESENT_RESTRICTED:
2009                 present = 1;
2010                 break;
2011                 default: /* INFO_PRESENT_NOTAVAIL */
2012                 present = 2;
2013                 break;
2014         }
2015         /* caller information 2 */
2016         plan2 = 1;
2017         switch (p_callerinfo.ntype2) {
2018                 case INFO_NTYPE_UNKNOWN:
2019                 type2 = 0x0;
2020                 break;
2021                 case INFO_NTYPE_INTERNATIONAL:
2022                 type2 = 0x1;
2023                 break;
2024                 case INFO_NTYPE_NATIONAL:
2025                 type2 = 0x2;
2026                 break;
2027                 case INFO_NTYPE_SUBSCRIBER:
2028                 type2 = 0x4;
2029                 break;
2030                 default: /* INFO_NTYPE_NOTPRESENT */
2031                 type2 = -1;
2032                 break;
2033         }
2034         switch (p_callerinfo.screen2) {
2035                 case INFO_SCREEN_USER:
2036                 screen2 = 0;
2037                 break;
2038                 default: /* INFO_SCREEN_NETWORK */
2039                 screen2 = 3;
2040                 break;
2041         }
2042         switch (p_callerinfo.present2) {
2043                 case INFO_PRESENT_ALLOWED:
2044                 present2 = 0;
2045                 break;
2046                 case INFO_PRESENT_RESTRICTED:
2047                 present2 = 1;
2048                 break;
2049                 default: /* INFO_PRESENT_NOTAVAIL */
2050                 present2 = 2;
2051                 break;
2052         }
2053         if (type >= 0)
2054                 enc_ie_calling_pn(l3m, type, plan, present, screen, (unsigned char *)p_callerinfo.id, type2, plan2, present2, screen2, (unsigned char *)p_callerinfo.id2);
2055         /* dialing information */
2056         if (p_dialinginfo.id[0]) { /* only if we have something to dial */
2057                 if (max > (int)strlen(p_dialinginfo.id) || max == 0)
2058                         max = (int)strlen(p_dialinginfo.id);
2059                 enc_ie_called_pn(l3m, 0, 1, (unsigned char *)p_dialinginfo.id, max);
2060                 SCPY(p_m_d_queue, p_dialinginfo.id + max);
2061         }
2062         /* sending complete */
2063         if (p_dialinginfo.sending_complete)
2064                 enc_ie_complete(l3m, 1);
2065         /* sending user-user */
2066         if (param->setup.useruser.len) {
2067                 enc_ie_useruser(l3m, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2068         }
2069         /* redirecting number */
2070         plan = 1;
2071         switch (p_redirinfo.ntype) {
2072                 case INFO_NTYPE_UNKNOWN:
2073                 type = 0x0;
2074                 break;
2075                 case INFO_NTYPE_INTERNATIONAL:
2076                 type = 0x1;
2077                 break;
2078                 case INFO_NTYPE_NATIONAL:
2079                 type = 0x2;
2080                 break;
2081                 case INFO_NTYPE_SUBSCRIBER:
2082                 type = 0x4;
2083                 break;
2084                 default: /* INFO_NTYPE_NOTPRESENT */
2085                 type = -1;
2086                 break;
2087         }
2088         switch (p_redirinfo.screen) {
2089                 case INFO_SCREEN_USER:
2090                 screen = 0;
2091                 break;
2092                 default: /* INFO_SCREE_NETWORK */
2093                 screen = 3;
2094                 break;
2095         }
2096         switch (p_redirinfo.reason) {
2097                 case INFO_REDIR_BUSY:
2098                 reason = 1;
2099                 break;
2100                 case INFO_REDIR_NORESPONSE:
2101                 reason = 2;
2102                 break;
2103                 case INFO_REDIR_UNCONDITIONAL:
2104                 reason = 15;
2105                 break;
2106                 case INFO_REDIR_CALLDEFLECT:
2107                 reason = 10;
2108                 break;
2109                 case INFO_REDIR_OUTOFORDER:
2110                 reason = 9;
2111                 break;
2112                 default: /* INFO_REDIR_UNKNOWN */
2113                 reason = 0;
2114                 break;
2115         }
2116         switch (p_redirinfo.present) {
2117                 case INFO_PRESENT_ALLOWED:
2118                 present = 0;
2119                 break;
2120                 case INFO_PRESENT_RESTRICTED:
2121                 present = 1;
2122                 break;
2123                 default: /* INFO_PRESENT_NOTAVAIL */
2124                 present = 2;
2125                 break;
2126         }
2127         /* sending redirecting number only in ntmode */
2128         if (type >= 0 && (p_m_d_ntmode || p_m_d_tespecial))
2129                 enc_ie_redir_nr(l3m, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2130         /* bearer capability */
2131 //printf("hlc=%d\n",p_capainfo.hlc);
2132         coding = 0;
2133         capability = p_capainfo.bearer_capa;
2134         mode = p_capainfo.bearer_mode;
2135         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2136         switch (p_capainfo.bearer_info1) {
2137                 case INFO_INFO1_NONE:
2138                 user = -1;
2139                 break;
2140                 default:
2141                 user = p_capainfo.bearer_info1 & 0x7f;
2142                 break;
2143         }
2144         enc_ie_bearer(l3m, coding, capability, mode, rate, -1, user);
2145         /* hlc */
2146         if (p_capainfo.hlc) {
2147                 coding = 0;
2148                 interpretation = 4;
2149                 presentation = 1;
2150                 hlc = p_capainfo.hlc & 0x7f;
2151                 exthlc = -1;
2152                 if (p_capainfo.exthlc)
2153                         exthlc = p_capainfo.exthlc & 0x7f;
2154                 enc_ie_hlc(l3m, coding, interpretation, presentation, hlc, exthlc);
2155         }
2156
2157         /* display */
2158         if (p_callerinfo.display[0] && (p_m_d_ntmode || p_m_d_tespecial))
2159                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
2160         /* nt-mode: CNIP (calling name identification presentation) */
2161 //      if (p_callerinfo.name[0] && (p_m_d_ntmode || p_m_d_tespecial))
2162 //              enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2163         end_trace();
2164
2165         /* send setup message now */
2166         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SETUP, p_m_d_l3id, l3m);
2167
2168         new_state(PORT_STATE_OUT_SETUP);
2169 }
2170
2171 /* MESSAGE_FACILITY */
2172 void Pdss1::message_facility(unsigned int epoint_id, int message_id, union parameter *param)
2173 {
2174         l3_msg *l3m;
2175
2176         /* facility will not be sent to external lines */
2177         if (!p_m_d_ntmode && !p_m_d_tespecial)
2178                 return;
2179
2180         /* sending facility */
2181         l3m = create_l3msg();
2182         l1l2l3_trace_header(p_m_mISDNport, this, L3_FACILITY_REQ, DIRECTION_OUT);
2183         enc_ie_facility(l3m, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2184         end_trace();
2185         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_FACILITY, p_m_d_l3id, l3m);
2186 }
2187
2188 /* MESSAGE_NOTIFY */
2189 void Pdss1::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
2190 {
2191         l3_msg *l3m;
2192         int notify;
2193         int plan = 0, type = -1, present = 0;
2194
2195         printf("if = %d\n", param->notifyinfo.notify);
2196         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2197                 notify = param->notifyinfo.notify & 0x7f;
2198         else
2199                 notify = -1;
2200         if (notify >= 0) {
2201                 plan = 1;
2202                 switch (param->notifyinfo.ntype) {
2203                         case INFO_NTYPE_UNKNOWN:
2204                         type = 0;
2205                         break;
2206                         case INFO_NTYPE_INTERNATIONAL:
2207                         type = 1;
2208                         break;
2209                         case INFO_NTYPE_NATIONAL:
2210                         type = 2;
2211                         break;
2212                         case INFO_NTYPE_SUBSCRIBER:
2213                         type = 4;
2214                         break;
2215                         default: /* INFO_NTYPE_UNKNOWN */
2216                         type = -1;
2217                         break;
2218                 }
2219                 switch (param->notifyinfo.present) {
2220                         case INFO_PRESENT_ALLOWED:
2221                         present = 0;
2222                         break;
2223                         case INFO_PRESENT_RESTRICTED:
2224                         present = 1;
2225                         break;
2226                         default: /* INFO_PRESENT_NOTAVAIL */
2227                         present = 2;
2228                         break;
2229                 }
2230         }
2231
2232         if (notify<0 && !param->notifyinfo.display[0]) {
2233                 /* nothing to notify, nothing to display */
2234                 return;
2235         }
2236
2237         if (notify >= 0) {
2238                 if (p_state!=PORT_STATE_CONNECT && p_state!=PORT_STATE_IN_PROCEEDING && p_state!=PORT_STATE_IN_ALERTING) {
2239                         /* queue notification */
2240                         if (p_m_d_notify_pending)
2241                                 message_free(p_m_d_notify_pending);
2242                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2243                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2244                 } else {
2245                         /* sending notification */
2246                         l3m = create_l3msg();
2247                         l1l2l3_trace_header(p_m_mISDNport, this, L3_NOTIFY_REQ, DIRECTION_OUT);
2248                         enc_ie_notify(l3m, notify);
2249                         /* sending redirection number only in ntmode */
2250                         if (type >= 0 && (p_m_d_ntmode || p_m_d_tespecial))
2251                                 enc_ie_redir_dn(l3m, type, plan, present, (unsigned char *)param->notifyinfo.id);
2252                         if (param->notifyinfo.display[0] && (p_m_d_ntmode || p_m_d_tespecial))
2253                                 enc_ie_display(l3m, (unsigned char *)param->notifyinfo.display);
2254                         end_trace();
2255                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_NOTIFY, p_m_d_l3id, l3m);
2256                 }
2257         } else if (p_m_d_ntmode || p_m_d_tespecial) {
2258                 /* sending information */
2259                 l3m = create_l3msg();
2260                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2261                 enc_ie_display(l3m, (unsigned char *)param->notifyinfo.display);
2262                 end_trace();
2263                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2264         }
2265 }
2266
2267 /* MESSAGE_OVERLAP */
2268 void Pdss1::message_overlap(unsigned int epoint_id, int message_id, union parameter *param)
2269 {
2270         l3_msg *l3m;
2271
2272         /* in case of sending complete, we proceed */
2273         if (p_dialinginfo.sending_complete) {
2274                 PDEBUG(DEBUG_ISDN, "sending proceeding instead of setup_acknowledge, because address is complete.\n");
2275                 message_proceeding(epoint_id, message_id, param);
2276                 return;
2277         }
2278
2279         /* sending setup_acknowledge */
2280         l3m = create_l3msg();
2281         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_ACKNOWLEDGE_REQ, DIRECTION_OUT);
2282         /* channel information */
2283         if (p_state == PORT_STATE_IN_SETUP)
2284                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2285         /* progress information */
2286         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2287          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2288          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2289         if (p_m_mISDNport->tones)
2290                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2291         end_trace();
2292         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, l3m);
2293
2294         new_state(PORT_STATE_IN_OVERLAP);
2295 }
2296
2297 /* MESSAGE_PROCEEDING */
2298 void Pdss1::message_proceeding(unsigned int epoint_id, int message_id, union parameter *param)
2299 {
2300         l3_msg *l3m;
2301
2302         /* sending proceeding */
2303         l3m = create_l3msg();
2304         l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2305         /* channel information */
2306         if (p_state == PORT_STATE_IN_SETUP)
2307                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2308         /* progress information */
2309         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2310          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2311          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2312         if (p_m_mISDNport->tones)
2313                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2314         end_trace();
2315         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2316
2317         new_state(PORT_STATE_IN_PROCEEDING);
2318 }
2319
2320 /* MESSAGE_ALERTING */
2321 void Pdss1::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
2322 {
2323         l3_msg *l3m;
2324
2325         /* NT-MODE in setup state we must send PROCEEDING first */
2326         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP) {
2327                 /* sending proceeding */
2328                 l3m = create_l3msg();
2329                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2330                 /* channel information */
2331                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2332                 /* progress information */
2333                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2334                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2335                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2336                 if (p_m_mISDNport->tones)
2337                         enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2338                 end_trace();
2339                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2340                 new_state(PORT_STATE_IN_PROCEEDING);
2341         }
2342
2343         /* sending alerting */
2344         l3m = create_l3msg();
2345         l1l2l3_trace_header(p_m_mISDNport, this, L3_ALERTING_REQ, DIRECTION_OUT);
2346         /* channel information */
2347         if (p_state == PORT_STATE_IN_SETUP)
2348                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2349         /* progress information */
2350         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2351          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2352          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2353         if (p_m_mISDNport->tones)
2354                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2355         end_trace();
2356         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ALERTING, p_m_d_l3id, l3m);
2357
2358         new_state(PORT_STATE_IN_ALERTING);
2359 }
2360
2361 /* MESSAGE_CONNECT */
2362 void Pdss1::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
2363 {
2364         l3_msg *l3m;
2365         int type, plan, present, screen;
2366         class Endpoint *epoint;
2367
2368         /* NT-MODE in setup state we must send PROCEEDING first */
2369         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP) {
2370                 /* sending proceeding */
2371                 l3m = create_l3msg();
2372                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2373                 /* channel information */
2374                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2375                 end_trace();
2376                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2377                 new_state(PORT_STATE_IN_PROCEEDING);
2378         }
2379
2380         /* copy connected information */
2381         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2382         /* screen outgoing caller id */
2383         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
2384
2385         /* only display at connect state */
2386         if (p_state == PORT_STATE_CONNECT)
2387         if (p_connectinfo.display[0]) {
2388                 /* sending information */
2389                 l3m = create_l3msg();
2390                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2391                 if (p_m_d_ntmode || p_m_d_tespecial)
2392                         enc_ie_display(l3m, (unsigned char *)p_connectinfo.display);
2393                 end_trace();
2394                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2395                 return;
2396         }
2397
2398         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) {
2399                 /* connect command only possible in setup, proceeding or alerting state */
2400                 return;
2401         }
2402
2403         /* preparing connect message */
2404         l3m = create_l3msg();
2405         l1l2l3_trace_header(p_m_mISDNport, this, L3_CONNECT_REQ, DIRECTION_OUT);
2406         /* connect information */
2407         plan = 1;
2408         switch (p_connectinfo.ntype) {
2409                 case INFO_NTYPE_UNKNOWN:
2410                 type = 0x0;
2411                 break;
2412                 case INFO_NTYPE_INTERNATIONAL:
2413                 type = 0x1;
2414                 break;
2415                 case INFO_NTYPE_NATIONAL:
2416                 type = 0x2;
2417                 break;
2418                 case INFO_NTYPE_SUBSCRIBER:
2419                 type = 0x4;
2420                 break;
2421                 default: /* INFO_NTYPE_NOTPRESENT */
2422                 type = -1;
2423                 break;
2424         }
2425         switch (param->connectinfo.screen) {
2426                 case INFO_SCREEN_USER:
2427                 screen = 0;
2428                 break;
2429                 default: /* INFO_SCREE_NETWORK */
2430                 screen = 3;
2431                 break;
2432         }
2433         switch (p_connectinfo.present) {
2434                 case INFO_PRESENT_ALLOWED:
2435                 present = 0;
2436                 break;
2437                 case INFO_PRESENT_RESTRICTED:
2438                 present = 1;
2439                 break;
2440                 default: /* INFO_PRESENT_NOTAVAIL */
2441                 present = 2;
2442                 break;
2443         }
2444         if (type >= 0)
2445                 enc_ie_connected_pn(l3m, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2446         /* display */
2447         if (p_connectinfo.display[0] && (p_m_d_ntmode || p_m_d_tespecial))
2448                 enc_ie_display(l3m, (unsigned char *)p_connectinfo.display);
2449         /* nt-mode: CONP (connected name identification presentation) */
2450 //      if (p_connectinfo.name[0] && (p_m_d_ntmode || p_m_d_tespecial))
2451 //              enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2452         /* date & time */
2453         if (p_m_d_ntmode || p_m_d_tespecial) {
2454                 epoint = find_epoint_id(epoint_id);
2455                 enc_ie_date(l3m, now, p_settings.no_seconds);
2456         }
2457         end_trace();
2458         /* finally send message */
2459         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CONNECT, p_m_d_l3id, l3m);
2460
2461         if (p_m_d_ntmode)
2462                 new_state(PORT_STATE_CONNECT);
2463         else
2464                 new_state(PORT_STATE_CONNECT_WAITING);
2465         set_tone("", NULL);
2466 }
2467
2468 /* MESSAGE_DISCONNECT */
2469 void Pdss1::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
2470 {
2471         l3_msg *l3m;
2472         struct lcr_msg *message;
2473         char *p = NULL;
2474
2475         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2476 //      if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->tones)
2477 if (/*   ||*/ p_state==PORT_STATE_OUT_SETUP) {
2478                 /* sending release to endpoint */
2479                 while(p_epointlist) {
2480                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2481                         memcpy(&message->param, param, sizeof(union parameter));
2482                         message_put(message);
2483                         /* remove epoint */
2484                         free_epointlist(p_epointlist);
2485                 }
2486                 /* sending release */
2487                 l3m = create_l3msg();
2488                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
2489                 /* send cause */
2490                 enc_ie_cause(l3m, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
2491                 end_trace();
2492                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
2493                 new_state(PORT_STATE_RELEASE);
2494                 p_m_delete = 1;
2495                 return;
2496         }
2497
2498         /* workarround: NT-MODE in setup state we must send PROCEEDING first to make it work */
2499         if (p_state==PORT_STATE_IN_SETUP) {
2500                 /* sending proceeding */
2501                 l3m = create_l3msg();
2502                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2503                 /* channel information */
2504                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2505                 /* progress information */
2506                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2507                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2508                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2509                 if (p_m_mISDNport->tones)
2510                         enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2511                 end_trace();
2512                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2513                 new_state(PORT_STATE_IN_PROCEEDING);
2514         }
2515
2516         /* sending disconnect */
2517         l3m = create_l3msg();
2518         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
2519         /* progress information */
2520         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2521          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2522          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2523         if (p_m_mISDNport->tones)
2524                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2525         /* send cause */
2526         enc_ie_cause(l3m, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
2527         /* send display */
2528         if (param->disconnectinfo.display[0])
2529                 p = param->disconnectinfo.display;
2530         if (p) if (*p && (p_m_d_ntmode || p_m_d_tespecial))
2531                 enc_ie_display(l3m, (unsigned char *)p);
2532         end_trace();
2533         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
2534         new_state(PORT_STATE_OUT_DISCONNECT);
2535 }
2536
2537 /* MESSAGE_RELEASE */
2538 void Pdss1::message_release(unsigned int epoint_id, int message_id, union parameter *param)
2539 {
2540         l3_msg *l3m;
2541         class Endpoint *epoint;
2542         char *p = NULL;
2543
2544         /*
2545          * we may only release during incoming disconnect state.
2546          * this means that the endpoint doesnt require audio anymore
2547          */
2548         if (p_state == PORT_STATE_IN_DISCONNECT
2549          || p_state == PORT_STATE_OUT_DISCONNECT) {
2550                 /* sending release */
2551                 l3m = create_l3msg();
2552                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
2553                 /* send cause */
2554                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2555                 end_trace();
2556                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
2557                 new_state(PORT_STATE_RELEASE);
2558                 /* remove epoint */
2559                 free_epointid(epoint_id);
2560                 // wait for callref to be released
2561                 return;
2562
2563         }
2564         /*
2565          * if we are on incoming call setup, we may reject by sending a release_complete
2566          * also on outgoing call setup, we send a release complete, BUT this is not conform. (i don't know any other way)
2567          */
2568         if (p_state==PORT_STATE_IN_SETUP
2569          || p_state==PORT_STATE_OUT_SETUP) {
2570 //#warning remove me
2571 //PDEBUG(DEBUG_LOG, "JOLLY sending release complete %d\n", p_serial);
2572                 /* sending release complete */
2573                 l3m = create_l3msg();
2574                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
2575                 /* send cause */
2576                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2577                 end_trace();
2578                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
2579                 new_state(PORT_STATE_RELEASE);
2580                 /* remove epoint */
2581                 free_epointid(epoint_id);
2582                 // wait for callref to be released
2583                 return;
2584         }
2585
2586 #if 0
2587 wirklich erst proceeding?:
2588         /* NT-MODE in setup state we must send PROCEEDING first */
2589         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP) {
2590                 CALL_PROCEEDING_t *proceeding;
2591
2592                 /* sending proceeding */
2593                 l3m = create_l3msg();
2594                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2595                 /* channel information */
2596                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2597                 /* progress information */
2598                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2599                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2600                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2601                 if (p_m_mISDNport->tones)
2602                         enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2603                 end_trace();
2604                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2605         }
2606 #endif
2607
2608         /* sending disconnect */
2609         l3m = create_l3msg();
2610         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
2611         /* progress information */
2612         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2613          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2614          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2615         if (p_m_mISDNport->tones)
2616                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2617         /* send cause */
2618         enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2619         /* send display */
2620         epoint = find_epoint_id(epoint_id);
2621         if (param->disconnectinfo.display[0])
2622                 p = param->disconnectinfo.display;
2623         if (p) if (*p && (p_m_d_ntmode || p_m_d_tespecial))
2624                 enc_ie_display(l3m, (unsigned char *)p);
2625         end_trace();
2626         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
2627         new_state(PORT_STATE_OUT_DISCONNECT);
2628         /* remove epoint */
2629         free_epointid(epoint_id);
2630         // wait for release and callref to be released
2631 //#warning remove me
2632 //PDEBUG(DEBUG_LOG, "JOLLY sending disconnect %d\n", p_serial);
2633 }
2634
2635
2636 /*
2637  * endpoint sends messages to the port
2638  */
2639 int Pdss1::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
2640 {
2641         if (PmISDN::message_epoint(epoint_id, message_id, param))
2642                 return(1);
2643
2644         switch(message_id) {
2645                 case MESSAGE_INFORMATION: /* overlap dialing */
2646                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2647                  && p_state!=PORT_STATE_OUT_OVERLAP
2648                  && p_state!=PORT_STATE_CONNECT
2649                  && p_state!=PORT_STATE_OUT_DISCONNECT
2650                  && p_state!=PORT_STATE_IN_DISCONNECT) {
2651                         break;
2652                 }
2653                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2654                  && p_state!=PORT_STATE_OUT_OVERLAP
2655                  && p_state!=PORT_STATE_OUT_PROCEEDING
2656                  && p_state!=PORT_STATE_OUT_ALERTING
2657                  && p_state!=PORT_STATE_CONNECT
2658                  && p_state!=PORT_STATE_OUT_DISCONNECT
2659                  && p_state!=PORT_STATE_IN_DISCONNECT) {
2660                         break;
2661                 }
2662                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
2663                  && p_state!=PORT_STATE_IN_OVERLAP
2664                  && p_state!=PORT_STATE_IN_PROCEEDING
2665                  && p_state!=PORT_STATE_IN_ALERTING
2666                  && p_state!=PORT_STATE_CONNECT
2667                  && p_state!=PORT_STATE_CONNECT_WAITING
2668                  && p_state!=PORT_STATE_OUT_DISCONNECT
2669                  && p_state!=PORT_STATE_IN_DISCONNECT) {
2670                         break;
2671                 }
2672                 message_information(epoint_id, message_id, param);
2673                 break;
2674
2675                 case MESSAGE_SETUP: /* dial-out command received from epoint */
2676                 if (p_state!=PORT_STATE_IDLE
2677                  && p_state!=PORT_STATE_CONNECT) {
2678                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
2679                         break;
2680                 }
2681                 if (p_epointlist && p_state==PORT_STATE_IDLE)
2682                         FATAL("Pdss1(%s): epoint pointer is set in idle state, how bad!!\n", p_name);
2683                 message_setup(epoint_id, message_id, param);
2684                 break;
2685
2686                 case MESSAGE_NOTIFY: /* display and notifications */
2687                 message_notify(epoint_id, message_id, param);
2688                 break;
2689
2690                 case MESSAGE_FACILITY: /* facility message */
2691                 message_facility(epoint_id, message_id, param);
2692                 break;
2693
2694                 case MESSAGE_OVERLAP: /* more information is needed */
2695                 if (p_state!=PORT_STATE_IN_SETUP) {
2696                         break;
2697                 }
2698                 message_overlap(epoint_id, message_id, param);
2699                 break;
2700
2701                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
2702                 if (p_state!=PORT_STATE_IN_SETUP
2703                  && p_state!=PORT_STATE_IN_OVERLAP) {
2704                         break;
2705                 }
2706                 message_proceeding(epoint_id, message_id, param);
2707                 if (p_m_d_notify_pending) {
2708                         /* send pending notify message during connect */
2709                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2710                         message_free(p_m_d_notify_pending);
2711                         p_m_d_notify_pending = NULL;
2712                 }
2713                 break;
2714
2715                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
2716                 if (p_state!=PORT_STATE_IN_SETUP
2717                  && p_state!=PORT_STATE_IN_OVERLAP
2718                  && p_state!=PORT_STATE_IN_PROCEEDING) {
2719                         break;
2720                 }
2721                 message_alerting(epoint_id, message_id, param);
2722                 if (p_m_d_notify_pending) {
2723                         /* send pending notify message during connect */
2724                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2725                         message_free(p_m_d_notify_pending);
2726                         p_m_d_notify_pending = NULL;
2727                 }
2728                 break;
2729
2730                 case MESSAGE_CONNECT: /* call of endpoint is connected */
2731                 if (p_state!=PORT_STATE_IN_SETUP
2732                  && p_state!=PORT_STATE_IN_OVERLAP
2733                  && p_state!=PORT_STATE_IN_PROCEEDING
2734                  && p_state!=PORT_STATE_IN_ALERTING
2735                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode)) {
2736                         break;
2737                 }
2738                 message_connect(epoint_id, message_id, param);
2739                 if (p_m_d_notify_pending) {
2740                         /* send pending notify message during connect */
2741                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2742                         message_free(p_m_d_notify_pending);
2743                         p_m_d_notify_pending = NULL;
2744                 }
2745                 break;
2746
2747                 case MESSAGE_DISCONNECT: /* call has been disconnected */
2748                 if (p_state!=PORT_STATE_IN_SETUP
2749                  && p_state!=PORT_STATE_IN_OVERLAP
2750                  && p_state!=PORT_STATE_IN_PROCEEDING
2751                  && p_state!=PORT_STATE_IN_ALERTING
2752                  && p_state!=PORT_STATE_OUT_SETUP
2753                  && p_state!=PORT_STATE_OUT_OVERLAP
2754                  && p_state!=PORT_STATE_OUT_PROCEEDING
2755                  && p_state!=PORT_STATE_OUT_ALERTING
2756                  && p_state!=PORT_STATE_CONNECT
2757                  && p_state!=PORT_STATE_CONNECT_WAITING) {
2758                         break;
2759                 }
2760                 message_disconnect(epoint_id, message_id, param);
2761                 break;
2762
2763                 case MESSAGE_RELEASE: /* release isdn port */
2764                 if (p_state==PORT_STATE_RELEASE) {
2765                         break;
2766                 }
2767                 message_release(epoint_id, message_id, param);
2768                 break;
2769
2770                 default:
2771                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message_id);
2772         }
2773
2774         return(1);
2775 }
2776
2777
2778
2779 /*
2780  * data from isdn-stack (layer-3) to pbx (port class)
2781  */
2782 int stack2manager(struct mISDNport *mISDNport, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
2783 {
2784         class Port *port;
2785         class Pdss1 *pdss1;
2786         char name[32];
2787
2788         PDEBUG(DEBUG_ISDN, "cmd(0x%x) pid(0x%x)\n", cmd, pid);
2789
2790         if (pid == 0) {
2791                 PDEBUG(DEBUG_ISDN, "ignoring dummy process from phone.\n");
2792                 return(0);
2793         }
2794
2795         /* find Port object of type ISDN */
2796         port = port_first;
2797         while(port) {
2798                 /* are we ISDN ? */
2799                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1) {
2800                         pdss1 = (class Pdss1 *)port;
2801                         /* check out correct stack and id */
2802                         if (pdss1->p_m_mISDNport == mISDNport) {
2803                                 if (pdss1->p_m_d_l3id & MISDN_PID_CR_FLAG) {
2804                                         /* local callref, so match value only */
2805                                         if ((pdss1->p_m_d_l3id & MISDN_PID_CRVAL_MASK) == (pid & MISDN_PID_CRVAL_MASK))
2806                                                 break; // found
2807                                 } else {
2808                                         /* remote callref, ref + channel id */
2809                                         if (pdss1->p_m_d_l3id == pid)
2810                                                 break; // found
2811                                 }
2812                         }
2813                 }
2814                 port = port->next;
2815         }
2816
2817         /* aktueller prozess */
2818         if (port) {
2819                 if (cmd == MT_ASSIGN) {
2820                         /* stack gives us new layer 3 id (during connect) */
2821                         l1l2l3_trace_header(mISDNport, pdss1, L3_NEW_L3ID_IND, DIRECTION_IN);
2822                         add_trace("callref", "old", "0x%x", pdss1->p_m_d_l3id);
2823                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
2824                         if ((pdss1->p_m_d_l3id&MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER)
2825                                 PERROR("    strange setup-procid 0x%x\n", pdss1->p_m_d_l3id);
2826                         pdss1->p_m_d_l3id = pid;
2827                         if (port->p_state == PORT_STATE_CONNECT)
2828                                 pdss1->p_m_d_ces = pid >> 16;
2829                         add_trace("callref", "new", "0x%x", pdss1->p_m_d_l3id);
2830                         end_trace();
2831                         return(0);
2832                 }
2833                 /* if process id is master process, but a child disconnects */
2834                 if (mISDNport->ntmode
2835                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
2836                  && (pdss1->p_m_d_l3id & MISDN_PID_CRTYPE_MASK) == MISDN_PID_MASTER) {
2837                         if (cmd == MT_DISCONNECT
2838                          || cmd == MT_RELEASE) {
2839                                 /* send special indication for child disconnect */
2840                                 pdss1->disconnect_ind_i(cmd, pid, l3m);
2841                                 return(0);
2842                         }
2843                         if (cmd == MT_RELEASE_COMPLETE)
2844                                 return(0);
2845                 }
2846                 /* if we have child pid and got different child pid message, ignore */
2847                 if (mISDNport->ntmode
2848                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
2849                  && (pdss1->p_m_d_l3id & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
2850                  && pid != pdss1->p_m_d_l3id)
2851                         return(0);
2852
2853                 /* process message */
2854                 pdss1->message_isdn(cmd, pid, l3m);
2855                 return(0);
2856         }
2857
2858         /* d-message */
2859         switch(cmd) {
2860                 case MT_SETUP:
2861                 /* creating port object, transparent until setup with hdlc */
2862                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
2863                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
2864
2865                         FATAL("Cannot create Port instance.\n");
2866                 pdss1->message_isdn(cmd, pid, l3m);
2867                 break;
2868
2869                 case MT_RESUME:
2870                 /* creating port object, transparent until setup with hdlc */
2871                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
2872                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
2873                         FATAL("Cannot create Port instance.\n");
2874                 pdss1->message_isdn(cmd, pid, l3m);
2875                 break;
2876
2877                 case MT_FREE:
2878                 PDEBUG(DEBUG_ISDN, "unused call ref released (l3id=0x%x)\n", pid);
2879                 break;
2880
2881                 case MT_RELEASE_COMPLETE:
2882                 PERROR("must be ignored by stack, not sent to app\n");
2883                 break;
2884
2885                 case MT_FACILITY:
2886                 // facility als broadcast
2887                 break;
2888
2889                 default:
2890                 PERROR("unhandled message: cmd(0x%x) pid(0x%x)\n", cmd, pid);
2891                 port = port_first;
2892                 while(port) {
2893                         if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT) {
2894                                 pdss1 = (class Pdss1 *)port;
2895                                 /* check out correct stack */
2896                                 if (pdss1->p_m_mISDNport == mISDNport)
2897                                 /* check out correct id */
2898                                 PERROR("unhandled message: pid=%x is not associated with port-dinfo=%x\n", pid, pdss1->p_m_d_l3id);
2899                         }
2900                         port = port->next;
2901                 }
2902                 return(-EINVAL);
2903         }
2904         return(0);
2905 }
2906
2907
2908
2909
2910