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