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