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