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