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