Restructured tones_dir, added tones_dir to interface.conf
[lcr.git] / dss1.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** mISDN dss1                                                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13 #include "myisdn.h"
14 // socket mISDN
15 //#include <sys/socket.h>
16 extern "C" {
17 }
18 #include <q931.h>
19 extern unsigned int mt_assign_pid;
20
21 #include "ie.cpp"
22
23 /*
24  * constructor
25  */
26 Pdss1::Pdss1(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : PmISDN(type, mISDNport, portname, settings, channel, exclusive, mode)
27 {
28         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
29         p_m_d_ntmode = mISDNport->ntmode;
30         p_m_d_tespecial = mISDNport->tespecial;
31         p_m_d_l3id = 0;
32         p_m_d_ces = -1;
33         p_m_d_queue[0] = '\0';
34         p_m_d_notify_pending = NULL;
35         p_m_d_collect_cause = 0;
36         p_m_d_collect_location = 0;
37
38         PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, %s%s port #%d\n", portname, mISDNport->use, (mISDNport->ntmode)?"NT":"TE", (mISDNport->tespecial)?" (special)":"", p_m_portnum);
39 }
40
41
42 /*
43  * destructor
44  */
45 Pdss1::~Pdss1()
46 {
47         /* remove queued message */
48         if (p_m_d_notify_pending)
49                 message_free(p_m_d_notify_pending);
50 }
51
52
53 /*
54  * create layer 3 message
55  */
56 static struct l3_msg *create_l3msg(void)
57 {
58         struct l3_msg *l3m;
59
60         l3m = alloc_l3_msg();
61         if (l3m)
62                 return(l3m);
63
64         FATAL("Cannot allocate memory, system overloaded.\n");
65         exit(0); // make gcc happy
66 }
67
68 /*
69  * if we received a first reply to the setup message,
70  * we will check if we have now channel information 
71  * return: <0: error, call is released, -cause is given
72  *          0: ok, nothing to do
73  */
74 int Pdss1::received_first_reply_to_setup(unsigned int cmd, int channel, int exclusive)
75 {
76         int ret;
77         l3_msg *l3m;
78
79         /* correct exclusive to 0, if no explicit channel was given */
80         if (exclusive<0 || channel<=0)
81                 exclusive = 0;
82         
83         /* select scenario */
84         if (p_m_b_channel && p_m_b_exclusive) {
85                 /*** we gave an exclusive channel (or if we are done) ***/
86
87                 /* if not first reply, we are done */
88                 if (p_state != PORT_STATE_OUT_SETUP)
89                         return(0);
90
91                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
92                 add_trace("channel", "request", "%d (forced)", p_m_b_channel);
93                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
94
95                 /* if give channel not accepted or not equal */
96                 if (channel!=-1 && p_m_b_channel!=channel) {
97                         add_trace("conclusion", NULL, "forced channel not accepted");
98                         end_trace();
99                         ret = -44;
100                         goto channelerror;
101                 }
102
103                 add_trace("conclusion", NULL, "channel was accepted");
104                 add_trace("connect", "channel", "%d", p_m_b_channel);
105                 end_trace();
106
107                 /* activate our exclusive channel */
108                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
109         } else
110         if (p_m_b_channel) {
111                 /*** we gave a non-exclusive channel ***/
112
113                 /* if not first reply, we are done */
114                 if (p_state != PORT_STATE_OUT_SETUP)
115                         return(0);
116
117                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
118                 add_trace("channel", "request", "%d (suggest)", p_m_b_channel);
119                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
120
121                 /* if channel was accepted as given */
122                 if (channel==-1 || p_m_b_channel==channel) {
123                         add_trace("conclusion", NULL, "channel was accepted as given");
124                         add_trace("connect", "channel", "%d", p_m_b_channel);
125                         end_trace();
126                         p_m_b_exclusive = 1; // we are done
127                         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
128                         return(0);
129                 }
130
131                 /* if channel value is faulty */
132                 if (channel <= 0) {
133                         add_trace("conclusion", NULL, "illegal reply");
134                         end_trace();
135                         ret = -111; // protocol error
136                         goto channelerror;
137                 }
138
139                 /* if channel was not accepted, try to get it */
140                 ret = seize_bchannel(channel, 1); // exclusively
141                 add_trace("channel", "available", ret<0?"no":"yes");
142                 if (ret < 0) {
143                         add_trace("conclusion", NULL, "replied channel not available");
144                         end_trace();
145                         goto channelerror;
146                 }
147                 add_trace("conclusion", NULL, "replied channel accepted");
148                 add_trace("connect", "channel", "%d", p_m_b_channel);
149                 end_trace();
150
151                 /* activate channel given by remote */
152                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
153         } else
154         if (p_m_b_reserve) {
155                 /*** we sent 'any channel acceptable' ***/
156
157                 /* if not first reply, we are done */
158                 if (p_state != PORT_STATE_OUT_SETUP)
159                         return(0);
160
161                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
162                 add_trace("channel", "request", "any");
163                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
164                 /* if no channel was replied */
165                 if (channel <= 0) {
166                         add_trace("conclusion", NULL, "no channel, protocol error");
167                         end_trace();
168                         ret = -111; // protocol error
169                         goto channelerror;
170                 }
171
172                 /* we will see, if our received channel is available */
173                 ret = seize_bchannel(channel, 1); // exclusively
174                 add_trace("channel", "available", ret<0?"no":"yes");
175                 if (ret < 0) {
176                         add_trace("conclusion", NULL, "replied channel not available");
177                         end_trace();
178                         goto channelerror;
179                 }
180                 add_trace("conclusion", NULL, "replied channel accepted");
181                 add_trace("connect", "channel", "%d", p_m_b_channel);
182                 end_trace();
183
184                 /* activate channel given by remote */
185                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
186         } else {
187                 /*** we sent 'no channel available' ***/
188
189                 /* if not the first reply, but a connect, we are forced */
190                 if (cmd==MT_CONNECT && p_state!=PORT_STATE_OUT_SETUP) {
191                         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (connect)", DIRECTION_NONE);
192                         add_trace("channel", "request", "no-channel");
193                         add_trace("channel", "reply", (channel>=0)?"%d%s":"(none)", channel, exclusive?" (forced)":"");
194                         if (channel > 0) {
195                                 goto use_from_connect;
196                         }
197                         ret = seize_bchannel(CHANNEL_ANY, 0); // any channel
198                         add_trace("channel", "available", ret<0?"no":"yes");
199                         if (ret < 0) {
200                                 add_trace("conclusion", NULL, "no channel available during call-waiting");
201                                 end_trace();
202                                 goto channelerror;
203                         }
204                         add_trace("conclusion", NULL, "using channel %d", p_m_b_channel);
205                         add_trace("connect", "channel", "%d", p_m_b_channel);
206                         end_trace();
207                         p_m_b_exclusive = 1; // we are done
208
209                         /* activate channel given by remote */
210                         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
211                         return(0);
212                 }
213                 
214                 /* if not first reply, we are done */
215                 if (p_state != PORT_STATE_OUT_SETUP)
216                         return(0);
217
218                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
219                 add_trace("channel", "request", "no-channel");
220                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
221                 /* if first reply has no channel, we are done */
222                 if (channel <= 0) {
223                         add_trace("conclusion", NULL, "no channel until connect");
224                         end_trace();
225                         return(0);
226                 }
227
228                 /* we will see, if our received channel is available */
229                 use_from_connect:
230                 ret = seize_bchannel(channel, exclusive);
231                 add_trace("channel", "available", ret<0?"no":"yes");
232                 if (ret < 0) {
233                         add_trace("conclusion", NULL, "replied channel not available");
234                         end_trace();
235                         goto channelerror;
236                 }
237                 add_trace("conclusion", NULL, "replied channel accepted");
238                 add_trace("connect", "channel", "%d", p_m_b_channel);
239                 end_trace();
240                 p_m_b_exclusive = 1; // we are done
241
242                 /* activate channel given by remote */
243                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
244         }
245         return(0);
246
247         channelerror:
248         /*
249          * NOTE: we send MT_RELEASE_COMPLETE to "REJECT" the channel
250          * in response to the setup reply
251          */
252         l3m = create_l3msg();
253         l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
254         enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
255         end_trace();
256         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
257         new_state(PORT_STATE_RELEASE);
258         p_m_delete = 1;
259         return(-34); /* to epoint: no channel available */
260 }
261
262
263 /*
264  * hunt bchannel for incoming setup or retrieve or resume
265  */
266 int Pdss1::hunt_bchannel(int channel, int exclusive)
267 {
268         struct select_channel *selchannel;
269         struct interface_port *ifport = p_m_mISDNport->ifport;
270         int i;
271
272         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
273         if (exclusive<0)
274                 exclusive = 0;
275         if (channel == CHANNEL_NO)
276                 add_trace("channel", "request", "no-channel");
277         else
278                 add_trace("channel", "request", (channel>0)?"%d%s":"any", channel, exclusive?" (forced)":"");
279         if (channel==CHANNEL_NO && p_type==PORT_TYPE_DSS1_TE_IN) {
280                 add_trace("conclusion", NULL, "incoming call-waiting not supported for TE-mode");
281                 end_trace();
282                 return(-6); // channel unacceptable
283         }
284         if (channel <= 0) /* not given, no channel, whatever.. */
285                 channel = CHANNEL_ANY; /* any channel */
286         add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
287         if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) { // of out chan..
288                 add_trace("conclusion", NULL, "all channels are reserved");
289                 end_trace();
290                 return(-34); // no channel
291         }
292         if (channel == CHANNEL_ANY)
293                 goto get_from_list;
294         if (channel > 0) {
295                 /* check for given channel in selection list */
296                 selchannel = ifport->in_channel;
297                 while(selchannel) {
298                         if (selchannel->channel == channel || selchannel->channel == CHANNEL_FREE)
299                                 break;
300                         selchannel = selchannel->next;
301                 }
302                 if (!selchannel)
303                         channel = 0;
304
305                 /* exclusive channel requests must be in the list */
306                 if (exclusive) {
307                         /* no exclusive channel */
308                         if (!channel) {
309                                 add_trace("conclusion", NULL, "exclusively requested channel not in list");
310                                 end_trace();
311                                 return(-6); // channel unacceptable
312                         }
313                         /* get index for channel */
314                         i = channel-1-(channel>=17);
315                         if (i < 0 || i >= p_m_mISDNport->b_num || channel == 16) {
316                                 add_trace("conclusion", NULL, "exclusively requested channel outside interface range");
317                                 end_trace();
318                                 return(-6); // channel unacceptable
319                         }
320                         /* check if busy */
321                         if (p_m_mISDNport->b_port[i] == NULL)
322                                 goto use_channel;
323                         add_trace("conclusion", NULL, "exclusively requested channel is busy");
324                         end_trace();
325                         return(-6); // channel unacceptable
326                 }
327
328                 /* requested channels in list will be used */
329                 if (channel) {
330                         /* get index for channel */
331                         i = channel-1-(channel>=17);
332                         if (i < 0 || i >= p_m_mISDNport->b_num || channel == 16) {
333                                 add_trace("info", NULL, "requested channel %d outside interface range", channel);
334                         } else /* if inside range (else) check if available */
335                         if (p_m_mISDNport->b_port[i] == NULL)
336                                 goto use_channel;
337                 }
338
339                 /* if channel is not available or not in list, it must be searched */
340                 get_from_list:
341                 /* check for first free channel in list */
342                 channel = 0;
343                 selchannel = ifport->in_channel;
344                 while(selchannel) {
345                         switch(selchannel->channel) {
346                                 case CHANNEL_FREE: /* free channel */
347                                 add_trace("hunting", "channel", "free");
348                                 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num)
349                                         break; /* all channel in use or reserverd */
350                                 /* find channel */
351                                 i = 0;
352                                 while(i < p_m_mISDNport->b_num) {
353                                         if (p_m_mISDNport->b_port[i] == NULL) {
354                                                 channel = i+1+(i>=15);
355                                                 break;
356                                         }
357                                         i++;
358                                 }
359                                 break;
360
361                                 default:
362                                 add_trace("hunting", "channel", "%d", selchannel->channel);
363                                 if (selchannel->channel<1 || selchannel->channel==16)
364                                         break; /* invalid channels */
365                                 i = selchannel->channel-1-(selchannel->channel>=17);
366                                 if (i >= p_m_mISDNport->b_num)
367                                         break; /* channel not in port */
368                                 if (p_m_mISDNport->b_port[i] == NULL) {
369                                         channel = selchannel->channel;
370                                         break;
371                                 }
372                                 break;
373                         }
374                         if (channel)
375                                 break; /* found channel */
376                         selchannel = selchannel->next;
377                 }
378                 if (!channel) {
379                         add_trace("conclusion", NULL, "no channel available");
380                         end_trace();
381                         return(-6); // channel unacceptable
382                 }
383         }
384 use_channel:
385         add_trace("conclusion", NULL, "channel available");
386         add_trace("connect", "channel", "%d", channel);
387         end_trace();
388         return(channel);
389 }
390
391 /*
392  * handles all indications
393  */
394 /* CC_SETUP INDICATION */
395 void Pdss1::setup_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
396 {
397         int calling_type, calling_plan, calling_present, calling_screen;
398         int calling_type2, calling_plan2, calling_present2, calling_screen2;
399         int called_type, called_plan;
400         int redir_type, redir_plan, redir_present, redir_screen, redir_reason;
401         int hlc_coding, hlc_presentation, hlc_interpretation, hlc_hlc, hlc_exthlc;
402         int bearer_coding, bearer_capability, bearer_mode, bearer_rate, bearer_multi, bearer_user;
403         int exclusive, channel;
404         int ret;
405         unsigned char keypad[32] = "";
406         unsigned char useruser[128];
407         int useruser_len = 0, useruser_protocol;
408         class Endpoint *epoint;
409         struct lcr_msg *message;
410
411         /* process given callref */
412         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
413         add_trace("callref", "new", "0x%x", pid);
414         if (p_m_d_l3id) {
415                 /* release in case the ID is already in use */
416                 add_trace("error", NULL, "callref already in use");
417                 end_trace();
418                 l3m = create_l3msg();
419                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
420                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
421                 add_trace("reason", NULL, "callref already in use");
422                 end_trace();
423                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, pid, l3m);
424                 new_state(PORT_STATE_RELEASE);
425                 p_m_delete = 1;
426                 return;
427         }
428         p_m_d_l3id = pid;
429         p_m_d_ces = pid >> 16;
430         end_trace();
431
432         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_IND, DIRECTION_IN);
433         dec_ie_calling_pn(l3m, &calling_type, &calling_plan, &calling_present, &calling_screen, (unsigned char *)p_callerinfo.id, sizeof(p_callerinfo.id), &calling_type2, &calling_plan2, &calling_present2, &calling_screen2, (unsigned char *)p_callerinfo.id2, sizeof(p_callerinfo.id2));
434         dec_ie_called_pn(l3m, &called_type, &called_plan, (unsigned char *)p_dialinginfo.id, sizeof(p_dialinginfo.id));
435         dec_ie_keypad(l3m, (unsigned char *)keypad, sizeof(keypad));
436         /* te-mode: CNIP (calling name identification presentation) */
437         dec_facility_centrex(l3m, (unsigned char *)p_callerinfo.name, sizeof(p_callerinfo.name));
438         dec_ie_useruser(l3m, &useruser_protocol, useruser, &useruser_len);
439         dec_ie_complete(l3m, &p_dialinginfo.sending_complete);
440         dec_ie_redir_nr(l3m, &redir_type, &redir_plan, &redir_present, &redir_screen, &redir_reason, (unsigned char *)p_redirinfo.id, sizeof(p_redirinfo.id));
441         dec_ie_channel_id(l3m, &exclusive, &channel);
442         dec_ie_hlc(l3m, &hlc_coding, &hlc_interpretation, &hlc_presentation, &hlc_hlc, &hlc_exthlc);
443         dec_ie_bearer(l3m, &bearer_coding, &bearer_capability, &bearer_mode, &bearer_rate, &bearer_multi, &bearer_user);
444         dec_ie_display(l3m, (unsigned char *)p_dialinginfo.display, sizeof(p_dialinginfo.display));
445         end_trace();
446
447         /* if blocked, release call with MT_RELEASE_COMPLETE */
448         if (p_m_mISDNport->ifport->block) {
449                 l3m = create_l3msg();
450                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
451                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 27); /* temporary unavailable */
452                 add_trace("reason", NULL, "port blocked");
453                 end_trace();
454                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
455                 new_state(PORT_STATE_RELEASE);
456                 p_m_delete = 1;
457                 return;
458         }
459
460         /* caller info */
461         switch (calling_present) {
462                 case 1:
463                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
464                 break;
465                 case 2:
466                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
467                 break;
468                 default:
469                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
470                 break;
471         }
472         switch (calling_screen) {
473                 case 0:
474                 p_callerinfo.screen = INFO_SCREEN_USER;
475                 break;
476                 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         /* screen outgoing caller id */
1906         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
1907         do_screen(1, p_callerinfo.id2, sizeof(p_callerinfo.id2), &p_callerinfo.ntype2, &p_callerinfo.present2, p_m_mISDNport->ifport->interface);
1908
1909         /* only display at connect state: this case happens if endpoint is in connected mode */
1910         if (p_state==PORT_STATE_CONNECT) {
1911                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
1912                  && p_type!=PORT_TYPE_DSS1_NT_IN)
1913                         return;
1914                 if (p_callerinfo.display[0]) {
1915                         /* sending information */
1916                         l3m = create_l3msg();
1917                         l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
1918                         if (p_m_d_ntmode || p_m_d_tespecial)
1919                                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
1920                         end_trace();
1921                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
1922                         return;
1923                 }
1924         }
1925
1926         /* attach only if not already */
1927         epointlist = p_epointlist;
1928         while(epointlist) {
1929                 if (epointlist->epoint_id == epoint_id)
1930                         break;
1931                 epointlist = epointlist->next;
1932         }
1933         if (!epointlist)
1934                 epointlist_new(epoint_id);
1935
1936         /* get channel */
1937         exclusive = 0;
1938         if (p_m_b_channel) {
1939                 channel = p_m_b_channel;
1940                 exclusive = p_m_b_exclusive;
1941         } else
1942                 channel = CHANNEL_ANY;
1943         /* nt-port with no channel, not reserverd */
1944         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
1945                 channel = CHANNEL_NO;
1946
1947         /* creating l3id */
1948         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
1949         /* see MT_ASSIGN notes at do_layer3() */
1950         mt_assign_pid = 0;
1951         ret = p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ASSIGN, 0, NULL);
1952         if (mt_assign_pid == 0 || ret < 0) {
1953                 struct lcr_msg *message;
1954
1955                 add_trace("callref", NULL, "no free id");
1956                 end_trace();
1957                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1958                 message->param.disconnectinfo.cause = 47;
1959                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1960                 message_put(message);
1961                 new_state(PORT_STATE_RELEASE);
1962                 p_m_delete = 1;
1963                 return;
1964         }
1965         p_m_d_l3id = mt_assign_pid;
1966         mt_assign_pid = ~0;
1967         add_trace("callref", "new", "0x%x", p_m_d_l3id);
1968         end_trace();
1969
1970         /* preparing setup message */
1971         l3m = create_l3msg();
1972         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_REQ, DIRECTION_OUT);
1973         /* channel information */
1974         if (channel >= 0) /* it should */
1975                 enc_ie_channel_id(l3m, exclusive, channel);
1976         /* caller information */
1977         plan = 1;
1978         switch (p_callerinfo.ntype) {
1979                 case INFO_NTYPE_UNKNOWN:
1980                 type = 0x0;
1981                 break;
1982                 case INFO_NTYPE_INTERNATIONAL:
1983                 type = 0x1;
1984                 break;
1985                 case INFO_NTYPE_NATIONAL:
1986                 type = 0x2;
1987                 break;
1988                 case INFO_NTYPE_SUBSCRIBER:
1989                 type = 0x4;
1990                 break;
1991                 default: /* INFO_NTYPE_NOTPRESENT */
1992                 type = -1;
1993                 break;
1994         }
1995         switch (p_callerinfo.screen) {
1996                 case INFO_SCREEN_USER:
1997                 screen = 0;
1998                 break;
1999                 default: /* INFO_SCREEN_NETWORK */
2000                 screen = 3;
2001                 break;
2002         }
2003         switch (p_callerinfo.present) {
2004                 case INFO_PRESENT_ALLOWED:
2005                 present = 0;
2006                 break;
2007                 case INFO_PRESENT_RESTRICTED:
2008                 present = 1;
2009                 break;
2010                 default: /* INFO_PRESENT_NOTAVAIL */
2011                 present = 2;
2012                 break;
2013         }
2014         /* caller information 2 */
2015         plan2 = 1;
2016         switch (p_callerinfo.ntype2) {
2017                 case INFO_NTYPE_UNKNOWN:
2018                 type2 = 0x0;
2019                 break;
2020                 case INFO_NTYPE_INTERNATIONAL:
2021                 type2 = 0x1;
2022                 break;
2023                 case INFO_NTYPE_NATIONAL:
2024                 type2 = 0x2;
2025                 break;
2026                 case INFO_NTYPE_SUBSCRIBER:
2027                 type2 = 0x4;
2028                 break;
2029                 default: /* INFO_NTYPE_NOTPRESENT */
2030                 type2 = -1;
2031                 break;
2032         }
2033         switch (p_callerinfo.screen2) {
2034                 case INFO_SCREEN_USER:
2035                 screen2 = 0;
2036                 break;
2037                 default: /* INFO_SCREEN_NETWORK */
2038                 screen2 = 3;
2039                 break;
2040         }
2041         switch (p_callerinfo.present2) {
2042                 case INFO_PRESENT_ALLOWED:
2043                 present2 = 0;
2044                 break;
2045                 case INFO_PRESENT_RESTRICTED:
2046                 present2 = 1;
2047                 break;
2048                 default: /* INFO_PRESENT_NOTAVAIL */
2049                 present2 = 2;
2050                 break;
2051         }
2052         if (type >= 0)
2053                 enc_ie_calling_pn(l3m, type, plan, present, screen, (unsigned char *)p_callerinfo.id, type2, plan2, present2, screen2, (unsigned char *)p_callerinfo.id2);
2054         /* dialing information */
2055         if (p_dialinginfo.id[0]) { /* only if we have something to dial */
2056                 if (max > (int)strlen(p_dialinginfo.id) || max == 0)
2057                         max = (int)strlen(p_dialinginfo.id);
2058                 enc_ie_called_pn(l3m, 0, 1, (unsigned char *)p_dialinginfo.id, max);
2059                 SCPY(p_m_d_queue, p_dialinginfo.id + max);
2060         }
2061         /* sending complete */
2062         if (p_dialinginfo.sending_complete)
2063                 enc_ie_complete(l3m, 1);
2064         /* sending user-user */
2065         if (param->setup.useruser.len) {
2066                 enc_ie_useruser(l3m, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2067         }
2068         /* redirecting number */
2069         plan = 1;
2070         switch (p_redirinfo.ntype) {
2071                 case INFO_NTYPE_UNKNOWN:
2072                 type = 0x0;
2073                 break;
2074                 case INFO_NTYPE_INTERNATIONAL:
2075                 type = 0x1;
2076                 break;
2077                 case INFO_NTYPE_NATIONAL:
2078                 type = 0x2;
2079                 break;
2080                 case INFO_NTYPE_SUBSCRIBER:
2081                 type = 0x4;
2082                 break;
2083                 default: /* INFO_NTYPE_NOTPRESENT */
2084                 type = -1;
2085                 break;
2086         }
2087         switch (p_redirinfo.screen) {
2088                 case INFO_SCREEN_USER:
2089                 screen = 0;
2090                 break;
2091                 default: /* INFO_SCREE_NETWORK */
2092                 screen = 3;
2093                 break;
2094         }
2095         switch (p_redirinfo.reason) {
2096                 case INFO_REDIR_BUSY:
2097                 reason = 1;
2098                 break;
2099                 case INFO_REDIR_NORESPONSE:
2100                 reason = 2;
2101                 break;
2102                 case INFO_REDIR_UNCONDITIONAL:
2103                 reason = 15;
2104                 break;
2105                 case INFO_REDIR_CALLDEFLECT:
2106                 reason = 10;
2107                 break;
2108                 case INFO_REDIR_OUTOFORDER:
2109                 reason = 9;
2110                 break;
2111                 default: /* INFO_REDIR_UNKNOWN */
2112                 reason = 0;
2113                 break;
2114         }
2115         switch (p_redirinfo.present) {
2116                 case INFO_PRESENT_ALLOWED:
2117                 present = 0;
2118                 break;
2119                 case INFO_PRESENT_RESTRICTED:
2120                 present = 1;
2121                 break;
2122                 default: /* INFO_PRESENT_NOTAVAIL */
2123                 present = 2;
2124                 break;
2125         }
2126         /* sending redirecting number only in ntmode */
2127         if (type >= 0 && (p_m_d_ntmode || p_m_d_tespecial))
2128                 enc_ie_redir_nr(l3m, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2129         /* bearer capability */
2130 //printf("hlc=%d\n",p_capainfo.hlc);
2131         coding = 0;
2132         capability = p_capainfo.bearer_capa;
2133         mode = p_capainfo.bearer_mode;
2134         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2135         switch (p_capainfo.bearer_info1) {
2136                 case INFO_INFO1_NONE:
2137                 user = -1;
2138                 break;
2139                 default:
2140                 user = p_capainfo.bearer_info1 & 0x7f;
2141                 break;
2142         }
2143         enc_ie_bearer(l3m, coding, capability, mode, rate, -1, user);
2144         /* hlc */
2145         if (p_capainfo.hlc) {
2146                 coding = 0;
2147                 interpretation = 4;
2148                 presentation = 1;
2149                 hlc = p_capainfo.hlc & 0x7f;
2150                 exthlc = -1;
2151                 if (p_capainfo.exthlc)
2152                         exthlc = p_capainfo.exthlc & 0x7f;
2153                 enc_ie_hlc(l3m, coding, interpretation, presentation, hlc, exthlc);
2154         }
2155
2156         /* display */
2157         if (p_callerinfo.display[0] && (p_m_d_ntmode || p_m_d_tespecial))
2158                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
2159         /* nt-mode: CNIP (calling name identification presentation) */
2160 //      if (p_callerinfo.name[0] && (p_m_d_ntmode || p_m_d_tespecial))
2161 //              enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2162         end_trace();
2163
2164         /* send setup message now */
2165         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SETUP, p_m_d_l3id, l3m);
2166
2167         new_state(PORT_STATE_OUT_SETUP);
2168 }
2169
2170 /* MESSAGE_FACILITY */
2171 void Pdss1::message_facility(unsigned int epoint_id, int message_id, union parameter *param)
2172 {
2173         l3_msg *l3m;
2174
2175         /* facility will not be sent to external lines */
2176         if (!p_m_d_ntmode && !p_m_d_tespecial)
2177                 return;
2178
2179         /* sending facility */
2180         l3m = create_l3msg();
2181         l1l2l3_trace_header(p_m_mISDNport, this, L3_FACILITY_REQ, DIRECTION_OUT);
2182         enc_ie_facility(l3m, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2183         end_trace();
2184         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_FACILITY, p_m_d_l3id, l3m);
2185 }
2186
2187 /* MESSAGE_NOTIFY */
2188 void Pdss1::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
2189 {
2190         l3_msg *l3m;
2191         int notify;
2192         int plan = 0, type = -1, present = 0;
2193
2194         printf("if = %d\n", param->notifyinfo.notify);
2195         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2196                 notify = param->notifyinfo.notify & 0x7f;
2197         else
2198                 notify = -1;
2199         if (notify >= 0) {
2200                 plan = 1;
2201                 switch (param->notifyinfo.ntype) {
2202                         case INFO_NTYPE_UNKNOWN:
2203                         type = 0;
2204                         break;
2205                         case INFO_NTYPE_INTERNATIONAL:
2206                         type = 1;
2207                         break;
2208                         case INFO_NTYPE_NATIONAL:
2209                         type = 2;
2210                         break;
2211                         case INFO_NTYPE_SUBSCRIBER:
2212                         type = 4;
2213                         break;
2214                         default: /* INFO_NTYPE_UNKNOWN */
2215                         type = -1;
2216                         break;
2217                 }
2218                 switch (param->notifyinfo.present) {
2219                         case INFO_PRESENT_ALLOWED:
2220                         present = 0;
2221                         break;
2222                         case INFO_PRESENT_RESTRICTED:
2223                         present = 1;
2224                         break;
2225                         default: /* INFO_PRESENT_NOTAVAIL */
2226                         present = 2;
2227                         break;
2228                 }
2229         }
2230
2231         if (notify<0 && !param->notifyinfo.display[0]) {
2232                 /* nothing to notify, nothing to display */
2233                 return;
2234         }
2235
2236         if (notify >= 0) {
2237                 if (p_state!=PORT_STATE_CONNECT && p_state!=PORT_STATE_IN_PROCEEDING && p_state!=PORT_STATE_IN_ALERTING) {
2238                         /* queue notification */
2239                         if (p_m_d_notify_pending)
2240                                 message_free(p_m_d_notify_pending);
2241                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2242                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2243                 } else {
2244                         /* sending notification */
2245                         l3m = create_l3msg();
2246                         l1l2l3_trace_header(p_m_mISDNport, this, L3_NOTIFY_REQ, DIRECTION_OUT);
2247                         enc_ie_notify(l3m, notify);
2248                         /* sending redirection number only in ntmode */
2249                         if (type >= 0 && (p_m_d_ntmode || p_m_d_tespecial))
2250                                 enc_ie_redir_dn(l3m, type, plan, present, (unsigned char *)param->notifyinfo.id);
2251                         if (param->notifyinfo.display[0] && (p_m_d_ntmode || p_m_d_tespecial))
2252                                 enc_ie_display(l3m, (unsigned char *)param->notifyinfo.display);
2253                         end_trace();
2254                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_NOTIFY, p_m_d_l3id, l3m);
2255                 }
2256         } else if (p_m_d_ntmode || p_m_d_tespecial) {
2257                 /* sending information */
2258                 l3m = create_l3msg();
2259                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2260                 enc_ie_display(l3m, (unsigned char *)param->notifyinfo.display);
2261                 end_trace();
2262                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2263         }
2264 }
2265
2266 /* MESSAGE_OVERLAP */
2267 void Pdss1::message_overlap(unsigned int epoint_id, int message_id, union parameter *param)
2268 {
2269         l3_msg *l3m;
2270
2271         /* in case of sending complete, we proceed */
2272         if (p_dialinginfo.sending_complete) {
2273                 PDEBUG(DEBUG_ISDN, "sending proceeding instead of setup_acknowledge, because address is complete.\n");
2274                 message_proceeding(epoint_id, message_id, param);
2275                 return;
2276         }
2277
2278         /* sending setup_acknowledge */
2279         l3m = create_l3msg();
2280         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_ACKNOWLEDGE_REQ, DIRECTION_OUT);
2281         /* channel information */
2282         if (p_state == PORT_STATE_IN_SETUP)
2283                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2284         /* progress information */
2285         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2286          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2287          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2288         if (p_m_mISDNport->tones)
2289                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2290         end_trace();
2291         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, l3m);
2292
2293         new_state(PORT_STATE_IN_OVERLAP);
2294 }
2295
2296 /* MESSAGE_PROCEEDING */
2297 void Pdss1::message_proceeding(unsigned int epoint_id, int message_id, union parameter *param)
2298 {
2299         l3_msg *l3m;
2300
2301         /* sending proceeding */
2302         l3m = create_l3msg();
2303         l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2304         /* channel information */
2305         if (p_state == PORT_STATE_IN_SETUP)
2306                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2307         /* progress information */
2308         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2309          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2310          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2311         if (p_m_mISDNport->tones)
2312                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2313         end_trace();
2314         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2315
2316         new_state(PORT_STATE_IN_PROCEEDING);
2317 }
2318
2319 /* MESSAGE_ALERTING */
2320 void Pdss1::message_alerting(unsigned int epoint_id, int message_id, union parameter *param)
2321 {
2322         l3_msg *l3m;
2323
2324         /* NT-MODE in setup state we must send PROCEEDING first */
2325         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP) {
2326                 /* sending proceeding */
2327                 l3m = create_l3msg();
2328                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2329                 /* channel information */
2330                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2331                 /* progress information */
2332                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2333                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2334                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2335                 if (p_m_mISDNport->tones)
2336                         enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2337                 end_trace();
2338                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2339                 new_state(PORT_STATE_IN_PROCEEDING);
2340         }
2341
2342         /* sending alerting */
2343         l3m = create_l3msg();
2344         l1l2l3_trace_header(p_m_mISDNport, this, L3_ALERTING_REQ, DIRECTION_OUT);
2345         /* channel information */
2346         if (p_state == PORT_STATE_IN_SETUP)
2347                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2348         /* progress information */
2349         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2350          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2351          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2352         if (p_m_mISDNport->tones)
2353                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2354         end_trace();
2355         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ALERTING, p_m_d_l3id, l3m);
2356
2357         new_state(PORT_STATE_IN_ALERTING);
2358 }
2359
2360 /* MESSAGE_CONNECT */
2361 void Pdss1::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
2362 {
2363         l3_msg *l3m;
2364         int type, plan, present, screen;
2365         class Endpoint *epoint;
2366
2367         /* NT-MODE in setup state we must send PROCEEDING first */
2368         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP) {
2369                 /* sending proceeding */
2370                 l3m = create_l3msg();
2371                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2372                 /* channel information */
2373                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2374                 end_trace();
2375                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2376                 new_state(PORT_STATE_IN_PROCEEDING);
2377         }
2378
2379         /* copy connected information */
2380         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2381         /* screen outgoing caller id */
2382         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
2383
2384         /* only display at connect state */
2385         if (p_state == PORT_STATE_CONNECT)
2386         if (p_connectinfo.display[0]) {
2387                 /* sending information */
2388                 l3m = create_l3msg();
2389                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2390                 if (p_m_d_ntmode || p_m_d_tespecial)
2391                         enc_ie_display(l3m, (unsigned char *)p_connectinfo.display);
2392                 end_trace();
2393                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2394                 return;
2395         }
2396
2397         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) {
2398                 /* connect command only possible in setup, proceeding or alerting state */
2399                 return;
2400         }
2401
2402         /* preparing connect message */
2403         l3m = create_l3msg();
2404         l1l2l3_trace_header(p_m_mISDNport, this, L3_CONNECT_REQ, DIRECTION_OUT);
2405         /* connect information */
2406         plan = 1;
2407         switch (p_connectinfo.ntype) {
2408                 case INFO_NTYPE_UNKNOWN:
2409                 type = 0x0;
2410                 break;
2411                 case INFO_NTYPE_INTERNATIONAL:
2412                 type = 0x1;
2413                 break;
2414                 case INFO_NTYPE_NATIONAL:
2415                 type = 0x2;
2416                 break;
2417                 case INFO_NTYPE_SUBSCRIBER:
2418                 type = 0x4;
2419                 break;
2420                 default: /* INFO_NTYPE_NOTPRESENT */
2421                 type = -1;
2422                 break;
2423         }
2424         switch (param->connectinfo.screen) {
2425                 case INFO_SCREEN_USER:
2426                 screen = 0;
2427                 break;
2428                 default: /* INFO_SCREE_NETWORK */
2429                 screen = 3;
2430                 break;
2431         }
2432         switch (p_connectinfo.present) {
2433                 case INFO_PRESENT_ALLOWED:
2434                 present = 0;
2435                 break;
2436                 case INFO_PRESENT_RESTRICTED:
2437                 present = 1;
2438                 break;
2439                 default: /* INFO_PRESENT_NOTAVAIL */
2440                 present = 2;
2441                 break;
2442         }
2443         if (type >= 0)
2444                 enc_ie_connected_pn(l3m, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2445         /* display */
2446         if (p_connectinfo.display[0] && (p_m_d_ntmode || p_m_d_tespecial))
2447                 enc_ie_display(l3m, (unsigned char *)p_connectinfo.display);
2448         /* nt-mode: CONP (connected name identification presentation) */
2449 //      if (p_connectinfo.name[0] && (p_m_d_ntmode || p_m_d_tespecial))
2450 //              enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2451         /* date & time */
2452         if (p_m_d_ntmode || p_m_d_tespecial) {
2453                 epoint = find_epoint_id(epoint_id);
2454                 enc_ie_date(l3m, now, p_settings.no_seconds);
2455         }
2456         end_trace();
2457         /* finally send message */
2458         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CONNECT, p_m_d_l3id, l3m);
2459
2460         if (p_m_d_ntmode)
2461                 new_state(PORT_STATE_CONNECT);
2462         else
2463                 new_state(PORT_STATE_CONNECT_WAITING);
2464         set_tone("", NULL);
2465 }
2466
2467 /* MESSAGE_DISCONNECT */
2468 void Pdss1::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
2469 {
2470         l3_msg *l3m;
2471         struct lcr_msg *message;
2472         char *p = NULL;
2473
2474         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2475 //      if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->tones)
2476 if (/*   ||*/ p_state==PORT_STATE_OUT_SETUP) {
2477                 /* sending release to endpoint */
2478                 while(p_epointlist) {
2479                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2480                         memcpy(&message->param, param, sizeof(union parameter));
2481                         message_put(message);
2482                         /* remove epoint */
2483                         free_epointlist(p_epointlist);
2484                 }
2485                 /* sending release */
2486                 l3m = create_l3msg();
2487                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
2488                 /* send cause */
2489                 enc_ie_cause(l3m, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
2490                 end_trace();
2491                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
2492                 new_state(PORT_STATE_RELEASE);
2493                 p_m_delete = 1;
2494                 return;
2495         }
2496
2497         /* workarround: NT-MODE in setup state we must send PROCEEDING first to make it work */
2498         if (p_state==PORT_STATE_IN_SETUP) {
2499                 /* sending proceeding */
2500                 l3m = create_l3msg();
2501                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2502                 /* channel information */
2503                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2504                 /* progress information */
2505                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2506                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2507                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2508                 if (p_m_mISDNport->tones)
2509                         enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2510                 end_trace();
2511                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2512                 new_state(PORT_STATE_IN_PROCEEDING);
2513         }
2514
2515         /* sending disconnect */
2516         l3m = create_l3msg();
2517         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
2518         /* progress information */
2519         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2520          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2521          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2522         if (p_m_mISDNport->tones)
2523                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2524         /* send cause */
2525         enc_ie_cause(l3m, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
2526         /* send display */
2527         if (param->disconnectinfo.display[0])
2528                 p = param->disconnectinfo.display;
2529         if (p) if (*p && (p_m_d_ntmode || p_m_d_tespecial))
2530                 enc_ie_display(l3m, (unsigned char *)p);
2531         end_trace();
2532         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
2533         new_state(PORT_STATE_OUT_DISCONNECT);
2534 }
2535
2536 /* MESSAGE_RELEASE */
2537 void Pdss1::message_release(unsigned int epoint_id, int message_id, union parameter *param)
2538 {
2539         l3_msg *l3m;
2540         class Endpoint *epoint;
2541         char *p = NULL;
2542
2543         /*
2544          * we may only release during incoming disconnect state.
2545          * this means that the endpoint doesnt require audio anymore
2546          */
2547         if (p_state == PORT_STATE_IN_DISCONNECT
2548          || p_state == PORT_STATE_OUT_DISCONNECT) {
2549                 /* sending release */
2550                 l3m = create_l3msg();
2551                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
2552                 /* send cause */
2553                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2554                 end_trace();
2555                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
2556                 new_state(PORT_STATE_RELEASE);
2557                 /* remove epoint */
2558                 free_epointid(epoint_id);
2559                 // wait for callref to be released
2560                 return;
2561
2562         }
2563         /*
2564          * if we are on incoming call setup, we may reject by sending a release_complete
2565          * also on outgoing call setup, we send a release complete, BUT this is not conform. (i don't know any other way)
2566          */
2567         if (p_state==PORT_STATE_IN_SETUP
2568          || p_state==PORT_STATE_OUT_SETUP) {
2569 //#warning remove me
2570 //PDEBUG(DEBUG_LOG, "JOLLY sending release complete %d\n", p_serial);
2571                 /* sending release complete */
2572                 l3m = create_l3msg();
2573                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
2574                 /* send cause */
2575                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2576                 end_trace();
2577                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
2578                 new_state(PORT_STATE_RELEASE);
2579                 /* remove epoint */
2580                 free_epointid(epoint_id);
2581                 // wait for callref to be released
2582                 return;
2583         }
2584
2585 #if 0
2586 wirklich erst proceeding?:
2587         /* NT-MODE in setup state we must send PROCEEDING first */
2588         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP) {
2589                 CALL_PROCEEDING_t *proceeding;
2590
2591                 /* sending proceeding */
2592                 l3m = create_l3msg();
2593                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
2594                 /* channel information */
2595                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
2596                 /* progress information */
2597                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2598                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2599                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2600                 if (p_m_mISDNport->tones)
2601                         enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2602                 end_trace();
2603                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
2604         }
2605 #endif
2606
2607         /* sending disconnect */
2608         l3m = create_l3msg();
2609         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
2610         /* progress information */
2611         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2612          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2613          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2614         if (p_m_mISDNport->tones)
2615                 enc_ie_progress(l3m, 0, (p_m_d_ntmode)?1:5, 8);
2616         /* send cause */
2617         enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
2618         /* send display */
2619         epoint = find_epoint_id(epoint_id);
2620         if (param->disconnectinfo.display[0])
2621                 p = param->disconnectinfo.display;
2622         if (p) if (*p && (p_m_d_ntmode || p_m_d_tespecial))
2623                 enc_ie_display(l3m, (unsigned char *)p);
2624         end_trace();
2625         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
2626         new_state(PORT_STATE_OUT_DISCONNECT);
2627         /* remove epoint */
2628         free_epointid(epoint_id);
2629         // wait for release and callref to be released
2630 //#warning remove me
2631 //PDEBUG(DEBUG_LOG, "JOLLY sending disconnect %d\n", p_serial);
2632 }
2633
2634
2635 /*
2636  * endpoint sends messages to the port
2637  */
2638 int Pdss1::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
2639 {
2640         if (PmISDN::message_epoint(epoint_id, message_id, param))
2641                 return(1);
2642
2643         switch(message_id) {
2644                 case MESSAGE_INFORMATION: /* overlap dialing */
2645                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2646                  && p_state!=PORT_STATE_OUT_OVERLAP
2647                  && p_state!=PORT_STATE_CONNECT
2648                  && p_state!=PORT_STATE_OUT_DISCONNECT
2649                  && p_state!=PORT_STATE_IN_DISCONNECT) {
2650                         break;
2651                 }
2652                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2653                  && p_state!=PORT_STATE_OUT_OVERLAP
2654                  && p_state!=PORT_STATE_OUT_PROCEEDING
2655                  && p_state!=PORT_STATE_OUT_ALERTING
2656                  && p_state!=PORT_STATE_CONNECT
2657                  && p_state!=PORT_STATE_OUT_DISCONNECT
2658                  && p_state!=PORT_STATE_IN_DISCONNECT) {
2659                         break;
2660                 }
2661                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
2662                  && p_state!=PORT_STATE_IN_OVERLAP
2663                  && p_state!=PORT_STATE_IN_PROCEEDING
2664                  && p_state!=PORT_STATE_IN_ALERTING
2665                  && p_state!=PORT_STATE_CONNECT
2666                  && p_state!=PORT_STATE_CONNECT_WAITING
2667                  && p_state!=PORT_STATE_OUT_DISCONNECT
2668                  && p_state!=PORT_STATE_IN_DISCONNECT) {
2669                         break;
2670                 }
2671                 message_information(epoint_id, message_id, param);
2672                 break;
2673
2674                 case MESSAGE_SETUP: /* dial-out command received from epoint */
2675                 if (p_state!=PORT_STATE_IDLE
2676                  && p_state!=PORT_STATE_CONNECT) {
2677                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
2678                         break;
2679                 }
2680                 if (p_epointlist && p_state==PORT_STATE_IDLE)
2681                         FATAL("Pdss1(%s): epoint pointer is set in idle state, how bad!!\n", p_name);
2682                 message_setup(epoint_id, message_id, param);
2683                 break;
2684
2685                 case MESSAGE_NOTIFY: /* display and notifications */
2686                 message_notify(epoint_id, message_id, param);
2687                 break;
2688
2689                 case MESSAGE_FACILITY: /* facility message */
2690                 message_facility(epoint_id, message_id, param);
2691                 break;
2692
2693                 case MESSAGE_OVERLAP: /* more information is needed */
2694                 if (p_state!=PORT_STATE_IN_SETUP) {
2695                         break;
2696                 }
2697                 message_overlap(epoint_id, message_id, param);
2698                 break;
2699
2700                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
2701                 if (p_state!=PORT_STATE_IN_SETUP
2702                  && p_state!=PORT_STATE_IN_OVERLAP) {
2703                         break;
2704                 }
2705                 message_proceeding(epoint_id, message_id, param);
2706                 if (p_m_d_notify_pending) {
2707                         /* send pending notify message during connect */
2708                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2709                         message_free(p_m_d_notify_pending);
2710                         p_m_d_notify_pending = NULL;
2711                 }
2712                 break;
2713
2714                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
2715                 if (p_state!=PORT_STATE_IN_SETUP
2716                  && p_state!=PORT_STATE_IN_OVERLAP
2717                  && p_state!=PORT_STATE_IN_PROCEEDING) {
2718                         break;
2719                 }
2720                 message_alerting(epoint_id, message_id, param);
2721                 if (p_m_d_notify_pending) {
2722                         /* send pending notify message during connect */
2723                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2724                         message_free(p_m_d_notify_pending);
2725                         p_m_d_notify_pending = NULL;
2726                 }
2727                 break;
2728
2729                 case MESSAGE_CONNECT: /* call of endpoint is connected */
2730                 if (p_state!=PORT_STATE_IN_SETUP
2731                  && p_state!=PORT_STATE_IN_OVERLAP
2732                  && p_state!=PORT_STATE_IN_PROCEEDING
2733                  && p_state!=PORT_STATE_IN_ALERTING
2734                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode)) {
2735                         break;
2736                 }
2737                 message_connect(epoint_id, message_id, param);
2738                 if (p_m_d_notify_pending) {
2739                         /* send pending notify message during connect */
2740                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2741                         message_free(p_m_d_notify_pending);
2742                         p_m_d_notify_pending = NULL;
2743                 }
2744                 break;
2745
2746                 case MESSAGE_DISCONNECT: /* call has been disconnected */
2747                 if (p_state!=PORT_STATE_IN_SETUP
2748                  && p_state!=PORT_STATE_IN_OVERLAP
2749                  && p_state!=PORT_STATE_IN_PROCEEDING
2750                  && p_state!=PORT_STATE_IN_ALERTING
2751                  && p_state!=PORT_STATE_OUT_SETUP
2752                  && p_state!=PORT_STATE_OUT_OVERLAP
2753                  && p_state!=PORT_STATE_OUT_PROCEEDING
2754                  && p_state!=PORT_STATE_OUT_ALERTING
2755                  && p_state!=PORT_STATE_CONNECT
2756                  && p_state!=PORT_STATE_CONNECT_WAITING) {
2757                         break;
2758                 }
2759                 message_disconnect(epoint_id, message_id, param);
2760                 break;
2761
2762                 case MESSAGE_RELEASE: /* release isdn port */
2763                 if (p_state==PORT_STATE_RELEASE) {
2764                         break;
2765                 }
2766                 message_release(epoint_id, message_id, param);
2767                 break;
2768
2769                 default:
2770                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message_id);
2771         }
2772
2773         return(1);
2774 }
2775
2776
2777
2778 /*
2779  * data from isdn-stack (layer-3) to pbx (port class)
2780  */
2781 int stack2manager(struct mISDNport *mISDNport, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
2782 {
2783         class Port *port;
2784         class Pdss1 *pdss1;
2785         char name[32];
2786
2787         PDEBUG(DEBUG_ISDN, "cmd(0x%x) pid(0x%x)\n", cmd, pid);
2788
2789         if (pid == 0) {
2790                 PDEBUG(DEBUG_ISDN, "ignoring dummy process from phone.\n");
2791                 return(0);
2792         }
2793
2794         /* find Port object of type ISDN */
2795         port = port_first;
2796         while(port) {
2797                 /* are we ISDN ? */
2798                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1) {
2799                         pdss1 = (class Pdss1 *)port;
2800                         /* check out correct stack and id */
2801                         if (pdss1->p_m_mISDNport == mISDNport) {
2802                                 if (pdss1->p_m_d_l3id & MISDN_PID_CR_FLAG) {
2803                                         /* local callref, so match value only */
2804                                         if ((pdss1->p_m_d_l3id & MISDN_PID_CRVAL_MASK) == (pid & MISDN_PID_CRVAL_MASK))
2805                                                 break; // found
2806                                 } else {
2807                                         /* remote callref, ref + channel id */
2808                                         if (pdss1->p_m_d_l3id == pid)
2809                                                 break; // found
2810                                 }
2811                         }
2812                 }
2813                 port = port->next;
2814         }
2815
2816         /* aktueller prozess */
2817         if (port) {
2818                 if (cmd == MT_ASSIGN) {
2819                         /* stack gives us new layer 3 id (during connect) */
2820                         l1l2l3_trace_header(mISDNport, pdss1, L3_NEW_L3ID_IND, DIRECTION_IN);
2821                         add_trace("callref", "old", "0x%x", pdss1->p_m_d_l3id);
2822                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
2823                         if ((pdss1->p_m_d_l3id&MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER)
2824                                 PERROR("    strange setup-procid 0x%x\n", pdss1->p_m_d_l3id);
2825                         pdss1->p_m_d_l3id = pid;
2826                         if (port->p_state == PORT_STATE_CONNECT)
2827                                 pdss1->p_m_d_ces = pid >> 16;
2828                         add_trace("callref", "new", "0x%x", pdss1->p_m_d_l3id);
2829                         end_trace();
2830                         return(0);
2831                 }
2832                 /* if process id is master process, but a child disconnects */
2833                 if (mISDNport->ntmode
2834                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
2835                  && (pdss1->p_m_d_l3id & MISDN_PID_CRTYPE_MASK) == MISDN_PID_MASTER) {
2836                         if (cmd == MT_DISCONNECT
2837                          || cmd == MT_RELEASE) {
2838                                 /* send special indication for child disconnect */
2839                                 pdss1->disconnect_ind_i(cmd, pid, l3m);
2840                                 return(0);
2841                         }
2842                         if (cmd == MT_RELEASE_COMPLETE)
2843                                 return(0);
2844                 }
2845                 /* if we have child pid and got different child pid message, ignore */
2846                 if (mISDNport->ntmode
2847                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
2848                  && (pdss1->p_m_d_l3id & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
2849                  && pid != pdss1->p_m_d_l3id)
2850                         return(0);
2851
2852                 /* process message */
2853                 pdss1->message_isdn(cmd, pid, l3m);
2854                 return(0);
2855         }
2856
2857         /* d-message */
2858         switch(cmd) {
2859                 case MT_SETUP:
2860                 /* creating port object, transparent until setup with hdlc */
2861                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
2862                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
2863
2864                         FATAL("Cannot create Port instance.\n");
2865                 pdss1->message_isdn(cmd, pid, l3m);
2866                 break;
2867
2868                 case MT_RESUME:
2869                 /* creating port object, transparent until setup with hdlc */
2870                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
2871                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0, B_MODE_TRANSPARENT)))
2872                         FATAL("Cannot create Port instance.\n");
2873                 pdss1->message_isdn(cmd, pid, l3m);
2874                 break;
2875
2876                 case MT_FREE:
2877                 PDEBUG(DEBUG_ISDN, "unused call ref released (l3id=0x%x)\n", pid);
2878                 break;
2879
2880                 case MT_RELEASE_COMPLETE:
2881                 PERROR("must be ignored by stack, not sent to app\n");
2882                 break;
2883
2884                 case MT_FACILITY:
2885                 // facility als broadcast
2886                 break;
2887
2888                 default:
2889                 PERROR("unhandled message: cmd(0x%x) pid(0x%x)\n", cmd, pid);
2890                 port = port_first;
2891                 while(port) {
2892                         if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT) {
2893                                 pdss1 = (class Pdss1 *)port;
2894                                 /* check out correct stack */
2895                                 if (pdss1->p_m_mISDNport == mISDNport)
2896                                 /* check out correct id */
2897                                 PERROR("unhandled message: pid=%x is not associated with port-dinfo=%x\n", pid, pdss1->p_m_d_l3id);
2898                         }
2899                         port = port->next;
2900                 }
2901                 return(-EINVAL);
2902         }
2903         return(0);
2904 }
2905
2906
2907
2908
2909