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