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