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