lcr work (soon done :)
[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
1323         if (cause < 0)
1324                 cause = 16;
1325
1326         /* release if remote sends us no tones */
1327         if (!p_m_mISDNport->earlyb)
1328         {
1329 #ifdef SOCKET_MISDN
1330                 l3_msg *l3m;
1331 #else
1332                 msg_t *dmsg;
1333                 RELEASE_t *release;
1334 #endif
1335
1336 #ifdef SOCKET_MISDN
1337                 l3m = create_l3msg();
1338 #else
1339                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, dinfo, sizeof(RELEASE_t), p_m_d_ntmode);
1340                 release = (RELEASE_t *)(dmsg->data + headerlen);
1341 #endif
1342                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
1343 #ifdef SOCKET_MISDN
1344                 enc_ie_cause(l3m, location, cause); /* normal */
1345 #else
1346                 enc_ie_cause(&release->CAUSE, dmsg, location, cause); /* normal */
1347 #endif
1348                 add_trace("reason", NULL, "no remote patterns");
1349                 end_trace();
1350 #ifdef SOCKET_MISDN
1351                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
1352 #else
1353                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1354 #endif
1355
1356                 /* sending release to endpoint */
1357                 if (location == LOCATION_PRIVATE_LOCAL)
1358                         location = LOCATION_PRIVATE_REMOTE;
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 (location == LOCATION_PRIVATE_LOCAL)
1375                 location = LOCATION_PRIVATE_REMOTE;
1376         if (ACTIVE_EPOINT(p_epointlist))
1377         {
1378                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
1379                 message->param.disconnectinfo.location = location;
1380                 message->param.disconnectinfo.cause = cause;
1381                 message_put(message);
1382         }
1383         while(INACTIVE_EPOINT(p_epointlist))
1384         {
1385                 message = message_create(p_serial, INACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1386                 message->param.disconnectinfo.location = location;
1387                 message->param.disconnectinfo.cause = cause;
1388                 message_put(message);
1389                 /* remove epoint */
1390                 free_epointid(INACTIVE_EPOINT(p_epointlist));
1391         }
1392         new_state(PORT_STATE_IN_DISCONNECT);
1393 }
1394
1395 /* CC_DISCONNECT INDICATION */
1396 #ifdef SOCKET_MISDN
1397 void Pdss1::disconnect_ind_i(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1398 {
1399 #else
1400 void Pdss1::disconnect_ind_i(unsigned long prim, unsigned long dinfo, void *data)
1401 {
1402         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1403         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1404 #endif
1405         int location, cause;
1406
1407         /* cause */
1408         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_IND, DIRECTION_IN);
1409         if (p_m_d_collect_cause > 0)
1410         {
1411                 add_trace("old-cause", "location", "%d", p_m_d_collect_location);
1412                 add_trace("old-cause", "value", "%d", p_m_d_collect_cause);
1413         }
1414 #ifdef SOCKET_MISDN
1415         dec_ie_cause(l3m, &location, &cause);
1416 #else
1417         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1418 #endif
1419         if (location == LOCATION_PRIVATE_LOCAL)
1420                 location = LOCATION_PRIVATE_REMOTE;
1421
1422         /* collect cause */
1423         collect_cause(&p_m_d_collect_cause, &p_m_d_collect_location, cause, location);
1424         add_trace("new-cause", "location", "%d", p_m_d_collect_location);
1425         add_trace("new-cause", "value", "%d", p_m_d_collect_cause);
1426         end_trace();
1427
1428 }
1429
1430 /* CC_RELEASE INDICATION */
1431 #ifdef SOCKET_MISDN
1432 void Pdss1::release_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1433 {
1434 #else
1435 void Pdss1::release_ind(unsigned long prim, unsigned long dinfo, void *data)
1436 {
1437         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1438         msg_t *dmsg;
1439         RELEASE_t *release = (RELEASE_t *)((unsigned long)data + headerlen);
1440 #endif
1441         int location, cause;
1442         struct lcr_msg *message;
1443
1444         l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_IND, DIRECTION_IN);
1445 #ifdef SOCKET_MISDN
1446         dec_ie_cause(l3m, &location, &cause);
1447 #else
1448         dec_ie_cause(release->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1449 #endif
1450         end_trace();
1451
1452         if (cause < 0)
1453                 cause = 16;
1454
1455 #ifndef SOCKET_MISDN
1456         /* only in NT mode we must send release_complete, if we got a release confirm */
1457         if (prim == (CC_RELEASE | CONFIRM))
1458         {
1459                 /* sending release complete */
1460                 RELEASE_COMPLETE_t *release_complete;
1461                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
1462                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
1463                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
1464                 enc_ie_cause(&release_complete->CAUSE, dmsg, location, cause);
1465                 end_trace();
1466                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1467         }
1468 #endif
1469
1470         /* sending release to endpoint */
1471         if (location == LOCATION_PRIVATE_LOCAL)
1472                 location = LOCATION_PRIVATE_REMOTE;
1473         while(p_epointlist)
1474         {
1475                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1476                 message->param.disconnectinfo.cause = cause;
1477                 message->param.disconnectinfo.location = location;
1478                 message_put(message);
1479                 /* remove epoint */
1480                 free_epointlist(p_epointlist);
1481         }
1482
1483         new_state(PORT_STATE_RELEASE);
1484         p_m_delete = 1;
1485 }
1486
1487 /* CC_RELEASE_COMPLETE INDICATION (a reject) */
1488 #ifdef SOCKET_MISDN
1489 void Pdss1::release_complete_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1490 {
1491 #else
1492 void Pdss1::release_complete_ind(unsigned long prim, unsigned long dinfo, void *data)
1493 {
1494         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1495         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *)((unsigned long)data + headerlen);
1496 #endif
1497         int location, cause;
1498         struct lcr_msg *message;
1499         
1500         l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_IND, DIRECTION_IN);
1501         /* in case layer 2 is down during setup, we send cause 27 loc 5 */
1502         if (p_state == PORT_STATE_OUT_SETUP && !p_m_mISDNport->l1link)
1503         {
1504                 cause = 27;
1505                 location = 5;
1506         } else
1507         {
1508 #ifdef SOCKET_MISDN
1509                 dec_ie_cause(l3m, &location, &cause);
1510 #else
1511                 dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1512 #endif
1513                 add_trace("layer 1", NULL, (p_m_mISDNport->l1link)?"up":"down");
1514         }
1515         end_trace();
1516         if (location == LOCATION_PRIVATE_LOCAL)
1517                 location = LOCATION_PRIVATE_REMOTE;
1518
1519         if (cause < 0)
1520                 cause = 16;
1521
1522         /* sending release to endpoint */
1523         while(p_epointlist)
1524         {
1525                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1526                 message->param.disconnectinfo.cause = cause;
1527                 message->param.disconnectinfo.location = location;
1528                 message_put(message);
1529                 /* remove epoint */
1530                 free_epointlist(p_epointlist);
1531         }
1532
1533         new_state(PORT_STATE_RELEASE);
1534         p_m_delete = 1;
1535 }
1536
1537 /* T312 timeout  */
1538 #ifdef SOCKET_MISDN
1539 void Pdss1::t312_timeout_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1540 {
1541         // not required, release is performed with MT_FREE
1542 }
1543 #else
1544 void Pdss1::t312_timeout_ind(unsigned long prim, unsigned long dinfo, void *data)
1545 {
1546         struct lcr_msg *message;
1547         // trace is done at message_isdn()
1548         
1549         /* sending release to endpoint */
1550         while(p_epointlist)
1551         {
1552                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1553                 if (p_m_d_collect_cause)
1554                 {
1555                         message->param.disconnectinfo.cause = p_m_d_collect_cause;
1556                         message->param.disconnectinfo.location = p_m_d_collect_location;
1557                 } else
1558                 {
1559                         message->param.disconnectinfo.cause = CAUSE_NOUSER;
1560                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1561                 }
1562                 message_put(message);
1563                 /* remove epoint */
1564                 free_epointlist(p_epointlist);
1565         }
1566
1567         new_state(PORT_STATE_RELEASE);
1568         p_m_delete = 1;
1569 }
1570 #endif
1571
1572 /* CC_NOTIFY INDICATION */
1573 #ifdef SOCKET_MISDN
1574 void Pdss1::notify_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1575 {
1576 #else
1577 void Pdss1::notify_ind(unsigned long prim, unsigned long dinfo, void *data)
1578 {
1579         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1580         NOTIFY_t *notifying = (NOTIFY_t *)((unsigned long)data + headerlen);
1581 #endif
1582         struct lcr_msg *message;
1583         int notify, type, plan, present;
1584         unsigned char notifyid[sizeof(message->param.notifyinfo.id)];
1585
1586         l1l2l3_trace_header(p_m_mISDNport, this, L3_NOTIFY_IND, DIRECTION_IN);
1587 #ifdef SOCKET_MISDN
1588         dec_ie_notify(l3m, &notify);
1589         dec_ie_redir_dn(l3m, &type, &plan, &present, notifyid, sizeof(notifyid));
1590 #else
1591         dec_ie_notify(notifying->NOTIFY, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
1592         dec_ie_redir_dn(notifying->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, notifyid, sizeof(notifyid));
1593 #endif
1594         end_trace();
1595
1596         if (!ACTIVE_EPOINT(p_epointlist))
1597                 return;
1598         /* notification indicator */
1599         if (notify < 0)
1600                 return;
1601         notify |= 0x80;
1602         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1603         message->param.notifyinfo.notify = notify;
1604         SCPY(message->param.notifyinfo.id, (char *)notifyid);
1605         /* redirection number */
1606         switch (present)
1607         {
1608                 case 1:
1609                 message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1610                 break;
1611                 case 2:
1612                 message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1613                 break;
1614                 default:
1615                 message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1616                 break;
1617         }
1618         switch (type)
1619         {
1620                 case -1:
1621                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1622                 message->param.notifyinfo.present = INFO_PRESENT_NULL;
1623                 break;
1624                 case 1:
1625                 message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1626                 break;
1627                 case 2:
1628                 message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1629                 break;
1630                 case 4:
1631                 message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1632                 break;
1633                 default:
1634                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1635                 break;
1636         }
1637         message->param.notifyinfo.isdn_port = p_m_portnum;
1638         message_put(message);
1639 }
1640
1641
1642 /* CC_HOLD INDICATION */
1643         struct lcr_msg *message;
1644 #ifdef SOCKET_MISDN
1645 void Pdss1::hold_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1646 {
1647 #else
1648 void Pdss1::hold_ind(unsigned long prim, unsigned long dinfo, void *data)
1649 {
1650         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1651         msg_t *dmsg;
1652 //      HOLD_t *hold = (HOLD_t *)((unsigned long)data + headerlen);
1653         HOLD_REJECT_t *hold_reject;
1654         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
1655 #endif
1656 //      class Endpoint *epoint;
1657
1658         l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_IND, DIRECTION_IN);
1659         end_trace();
1660
1661         if (!ACTIVE_EPOINT(p_epointlist) || p_m_hold)
1662         {
1663 #ifdef SOCKET_MISDN
1664                 l3m = create_l3msg();
1665 #else
1666                 dmsg = create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT, dinfo, sizeof(HOLD_REJECT_t), p_m_d_ntmode);
1667                 hold_reject = (HOLD_REJECT_t *)(dmsg->data + headerlen);
1668 #endif
1669                 l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_REJECT_REQ, DIRECTION_OUT);
1670 #ifdef SOCKET_MISDN
1671                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, p_m_hold?101:31); /* normal unspecified / incompatible state */
1672 #else
1673                 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 */
1674 #endif
1675                 add_trace("reason", NULL, "no endpoint");
1676                 end_trace();
1677 #ifdef SOCKET_MISDN
1678                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_HOLD_REJECT, p_m_d_l3id, l3m);
1679 #else
1680                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1681 #endif
1682
1683                 return;
1684         }
1685
1686         /* notify the hold of call */
1687         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1688         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1689         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1690         message_put(message);
1691
1692         /* deactivate bchannel */
1693         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (hold)", DIRECTION_NONE);
1694         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1695         end_trace();
1696         drop_bchannel();
1697
1698         /* set hold state */
1699         p_m_hold = 1;
1700 #if 0
1701         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1702         if (epoint && p_m_d_ntmode)
1703         {
1704                 p_m_timeout = p_settings.tout_hold;
1705                 time(&p_m_timer);
1706         }
1707 #endif
1708
1709         /* acknowledge hold */
1710 #ifdef SOCKET_MISDN
1711         l3m = create_l3msg();
1712 #else
1713         dmsg = create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE, dinfo, sizeof(HOLD_ACKNOWLEDGE_t), p_m_d_ntmode);
1714         hold_acknowledge = (HOLD_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1715 #endif
1716         l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1717         end_trace();
1718 #ifdef SOCKET_MISDN
1719         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_HOLD_ACKNOWLEDGE, p_m_d_l3id, l3m);
1720 #else
1721         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1722 #endif
1723 }
1724
1725
1726 /* CC_RETRIEVE INDICATION */
1727 #ifdef SOCKET_MISDN
1728 void Pdss1::retrieve_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1729 {
1730 #else
1731 void Pdss1::retrieve_ind(unsigned long prim, unsigned long dinfo, void *data)
1732 {
1733         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1734         msg_t *dmsg;
1735         RETRIEVE_t *retrieve = (RETRIEVE_t *)((unsigned long)data + headerlen);
1736         RETRIEVE_REJECT_t *retrieve_reject;
1737         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1738 #endif
1739         struct lcr_msg *message;
1740         int channel, exclusive, cause;
1741         int ret;
1742
1743         l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_IND, DIRECTION_IN);
1744 #ifdef SOCKET_MISDN
1745         dec_ie_channel_id(l3m, &exclusive, &channel);
1746 #else
1747         dec_ie_channel_id(retrieve->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1748 #endif
1749         end_trace();
1750
1751         if (!p_m_hold)
1752         {
1753                 cause = 101; /* incompatible state */
1754                 reject:
1755
1756 #ifdef SOCKET_MISDN
1757                 l3m = create_l3msg();
1758 #else
1759                 dmsg = create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT, dinfo, sizeof(RETRIEVE_REJECT_t), p_m_d_ntmode);
1760                 retrieve_reject = (RETRIEVE_REJECT_t *)(dmsg->data + headerlen);
1761 #endif
1762                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_REJECT_REQ, DIRECTION_OUT);
1763 #ifdef SOCKET_MISDN
1764                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1765 #else
1766                 enc_ie_cause(&retrieve_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1767 #endif
1768                 end_trace();
1769 #ifdef SOCKET_MISDN
1770                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RETRIEVE_REJECT, p_m_d_l3id, l3m);
1771 #else
1772                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1773 #endif
1774
1775                 return;
1776         }
1777
1778         /* notify the retrieve of call */
1779         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1780         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1781         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1782         message_put(message);
1783
1784         /* hunt channel */
1785         ret = channel = hunt_bchannel(channel, exclusive);
1786         if (ret < 0)
1787                 goto no_channel;
1788
1789         /* open channel */
1790         ret = seize_bchannel(channel, 1);
1791         if (ret < 0)
1792         {
1793                 no_channel:
1794                 cause = -ret;
1795                 goto reject;
1796         }
1797         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1798
1799         /* set hold state */
1800         p_m_hold = 0;
1801         p_m_timeout = 0;
1802
1803         /* acknowledge retrieve */
1804 #ifdef SOCKET_MISDN
1805         l3m = create_l3msg();
1806 #else
1807         dmsg = create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE, dinfo, sizeof(RETRIEVE_ACKNOWLEDGE_t), p_m_d_ntmode);
1808         retrieve_acknowledge = (RETRIEVE_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1809 #endif
1810         l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1811 #ifdef SOCKET_MISDN
1812         enc_ie_channel_id(l3m, 1, p_m_b_channel);
1813 #else
1814         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1815 #endif
1816         end_trace();
1817 #ifdef SOCKET_MISDN
1818         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RETRIEVE_ACKNOWLEDGE, p_m_d_l3id, l3m);
1819 #else
1820         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1821 #endif
1822 }
1823
1824 /* CC_SUSPEND INDICATION */
1825 #ifdef SOCKET_MISDN
1826 void Pdss1::suspend_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1827 {
1828 #else
1829 void Pdss1::suspend_ind(unsigned long prim, unsigned long dinfo, void *data)
1830 {
1831         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1832         msg_t *dmsg;
1833         SUSPEND_t *suspend = (SUSPEND_t *)((unsigned long)data + headerlen);
1834         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
1835         SUSPEND_REJECT_t *suspend_reject;
1836 #endif
1837         struct lcr_msg *message;
1838         class Endpoint *epoint;
1839         unsigned char callid[8];
1840         int len;
1841         int ret = -31; /* normal, unspecified */
1842
1843         l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_IND, DIRECTION_IN);
1844 #ifdef SOCKET_MISDN
1845         dec_ie_call_id(l3m, callid, &len);
1846 #else
1847         dec_ie_call_id(suspend->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1848 #endif
1849         end_trace();
1850
1851         if (!ACTIVE_EPOINT(p_epointlist))
1852         {
1853                 reject:
1854 #ifdef SOCKET_MISDN
1855                 l3m = create_l3msg();
1856 #else
1857                 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, dinfo, sizeof(SUSPEND_REJECT_t), p_m_d_ntmode);
1858                 suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + headerlen);
1859 #endif
1860                 l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_REJECT_REQ, DIRECTION_OUT);
1861 #ifdef SOCKET_MISDN
1862                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1863 #else
1864                 enc_ie_cause(&suspend_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1865 #endif
1866                 end_trace();
1867 #ifdef SOCKET_MISDN
1868                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SUSPEND_REJECT, p_m_d_l3id, l3m);
1869 #else
1870                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1871 #endif
1872
1873                 return;
1874         }
1875
1876         /* call id */
1877         if (len<0) len = 0;
1878
1879         /* check if call id is in use */
1880         epoint = epoint_first;
1881         while(epoint)
1882         {
1883                 if (epoint->ep_park)
1884                 {
1885                         if (epoint->ep_park_len == len)
1886                         if (!memcmp(epoint->ep_park_callid, callid, len))
1887                         {
1888                                 ret = -84; /* call id in use */
1889                                 goto reject;
1890                         }
1891                 }
1892                 epoint = epoint->next;
1893         }
1894
1895         /* notify the hold of call */
1896         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1897         message->param.notifyinfo.notify = INFO_NOTIFY_USER_SUSPENDED;
1898         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1899         message_put(message);
1900
1901         /* deactivate bchannel */
1902         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (suspend)", DIRECTION_NONE);
1903         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1904         end_trace();
1905         drop_bchannel();
1906
1907         /* sending suspend to endpoint */
1908         while (p_epointlist)
1909         {
1910                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_SUSPEND);
1911                 memcpy(message->param.parkinfo.callid, callid, sizeof(message->param.parkinfo.callid));
1912                 message->param.parkinfo.len = len;
1913                 message_put(message);
1914                 /* remove epoint */
1915                 free_epointlist(p_epointlist);
1916         }
1917
1918         /* sending SUSPEND_ACKNOWLEDGE */
1919 #ifdef SOCKET_MISDN
1920         l3m = create_l3msg();
1921 #else
1922         dmsg = create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE, dinfo, sizeof(SUSPEND_ACKNOWLEDGE_t), p_m_d_ntmode);
1923         suspend_acknowledge = (SUSPEND_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1924 #endif
1925         l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1926         end_trace();
1927 #ifdef SOCKET_MISDN
1928         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SUSPEND_ACKNOWLEDGE, p_m_d_l3id, l3m);
1929 #else
1930         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1931 #endif
1932
1933         new_state(PORT_STATE_RELEASE);
1934         p_m_delete = 1;
1935 }
1936
1937 /* CC_RESUME INDICATION */
1938 #ifdef SOCKET_MISDN
1939 void Pdss1::resume_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1940 {
1941 #else
1942 void Pdss1::resume_ind(unsigned long prim, unsigned long dinfo, void *data)
1943 {
1944         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1945         msg_t *dmsg;
1946         RESUME_t *resume = (RESUME_t *)((unsigned long)data + headerlen);
1947         RESUME_REJECT_t *resume_reject;
1948         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
1949 #endif
1950         unsigned char callid[8];
1951         int len;
1952         int channel, exclusive;
1953         class Endpoint *epoint;
1954         struct lcr_msg *message;
1955         int ret;
1956
1957 #ifdef SOCKET_MISDN
1958         /* process given callref */
1959         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
1960         add_trace("callref", "new", "0x%x", pid);
1961         if (p_m_d_l3id)
1962         {
1963                 /* release is case the ID is already in use */
1964                 add_trace("error", NULL, "callref already in use");
1965                 end_trace();
1966                 l3m = create_l3msg();
1967                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_REJECT_REQ, DIRECTION_OUT);
1968                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
1969                 add_trace("reason", NULL, "callref already in use");
1970                 end_trace();
1971                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_REJECT, pid, l3m);
1972                 new_state(PORT_STATE_RELEASE);
1973                 p_m_delete = 1;
1974                 return;
1975         }
1976         p_m_d_l3id = pid;
1977         p_m_d_ces = pid >> 16;
1978         end_trace();
1979 #else
1980         /* callref from nt-lib */
1981         if (p_m_d_ntmode)
1982         {
1983                 /* nt-library now gives us the id via CC_RESUME */
1984                 if (dinfo&(~0xff) == 0xff00)
1985                         FATAL("l3-stack gives us a process id 0xff00-0xffff\n");
1986                 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_IND, DIRECTION_IN);
1987                 if (p_m_d_l3id)
1988                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1989                 add_trace("callref", "new", "0x%x", dinfo);
1990                 end_trace();
1991                 if (p_m_d_l3id&(~0xff) == 0xff00)
1992                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1993                 p_m_d_l3id = dinfo;
1994                 p_m_d_ces = resume->ces;
1995         }
1996 #endif
1997
1998         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_IND, DIRECTION_IN);
1999 #ifdef SOCKET_MISDN
2000         dec_ie_call_id(l3m, callid, &len);
2001 #else
2002         dec_ie_call_id(resume->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
2003 #endif
2004         end_trace();
2005
2006         /* if blocked, release call */
2007         if (p_m_mISDNport->ifport->block)
2008         {
2009                 ret = -27;
2010                 goto reject;
2011         }
2012
2013         /* call id */
2014         if (len<0) len = 0;
2015
2016         /* channel_id (no channel is possible in message) */
2017         exclusive = 0;
2018         channel = -1; /* any channel */
2019
2020         /* hunt channel */
2021         ret = channel = hunt_bchannel(channel, exclusive);
2022         if (ret < 0)
2023                 goto no_channel;
2024
2025         /* open channel */
2026         ret = seize_bchannel(channel, 1);
2027         if (ret < 0)
2028         {
2029                 no_channel:
2030                 reject:
2031 #ifdef SOCKET_MISDN
2032                 l3m = create_l3msg();
2033 #else
2034                 dmsg = create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT, dinfo, sizeof(RESUME_REJECT_t), p_m_d_ntmode);
2035                 resume_reject = (RESUME_REJECT_t *)(dmsg->data + headerlen);
2036 #endif
2037                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_REJECT_REQ, DIRECTION_OUT);
2038 #ifdef SOCKET_MISDN
2039                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
2040 #else
2041                 enc_ie_cause(&resume_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
2042 #endif
2043                 if (ret == -27)
2044                         add_trace("reason", NULL, "port blocked");
2045                 end_trace();
2046 #ifdef SOCKET_MISDN
2047                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_REJECT, p_m_d_l3id, l3m);
2048 #else
2049                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2050 #endif
2051                 new_state(PORT_STATE_RELEASE);
2052                 p_m_delete = 1;
2053                 return;
2054         }
2055         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
2056
2057         /* create endpoint */
2058         if (p_epointlist)
2059                 FATAL("Incoming resume but already got an endpoint.\n");
2060         ret = -85; /* no call suspended */
2061         epoint = epoint_first;
2062         while(epoint)
2063         {
2064                 if (epoint->ep_park)
2065                 {
2066                         ret = -83; /* suspended call exists, but this not */
2067                         if (epoint->ep_park_len == len)
2068                         if (!memcmp(epoint->ep_park_callid, callid, len))
2069                                 break;
2070                 }
2071                 epoint = epoint->next;
2072         }
2073         if (!epoint)
2074                 goto reject;
2075
2076         epointlist_new(epoint->ep_serial);
2077         if (!(epoint->portlist_new(p_serial, p_type, p_m_mISDNport->earlyb)))
2078                 FATAL("No memory for portlist\n");
2079         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RESUME);
2080         message_put(message);
2081
2082         /* notify the resume of call */
2083         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
2084         message->param.notifyinfo.notify = INFO_NOTIFY_USER_RESUMED;
2085         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
2086         message_put(message);
2087
2088         /* sending RESUME_ACKNOWLEDGE */
2089 #ifdef SOCKET_MISDN
2090         l3m = create_l3msg();
2091 #else
2092         dmsg = create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE, dinfo, sizeof(RESUME_ACKNOWLEDGE_t), p_m_d_ntmode);
2093         resume_acknowledge = (RESUME_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2094 #endif
2095         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_ACKNOWLEDGE_REQ, DIRECTION_OUT);
2096 #ifdef SOCKET_MISDN
2097         enc_ie_channel_id(l3m, 1, p_m_b_channel);
2098 #else
2099         enc_ie_channel_id(&resume_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2100 #endif
2101         end_trace();
2102 #ifdef SOCKET_MISDN
2103         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_ACKNOWLEDGE, p_m_d_l3id, l3m);
2104 #else
2105         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2106 #endif
2107
2108         new_state(PORT_STATE_CONNECT);
2109 }
2110
2111
2112 /* CC_FACILITY INDICATION */
2113 #ifdef SOCKET_MISDN
2114 void Pdss1::facility_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
2115 {
2116 #else
2117 void Pdss1::facility_ind(unsigned long prim, unsigned long dinfo, void *data)
2118 {
2119         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2120         FACILITY_t *facility = (FACILITY_t *)((unsigned long)data + headerlen);
2121 #endif
2122         unsigned char facil[256];
2123         int facil_len;
2124         struct lcr_msg *message;
2125
2126         l1l2l3_trace_header(p_m_mISDNport, this, L3_FACILITY_IND, DIRECTION_IN);
2127 #ifdef SOCKET_MISDN
2128         dec_ie_facility(l3m, facil, &facil_len);
2129 #else
2130         dec_ie_facility(facility->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), facil, &facil_len);
2131 #endif
2132         end_trace();
2133
2134         /* facility */
2135         if (facil_len<=0)
2136                 return;
2137
2138         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_FACILITY);
2139         message->param.facilityinfo.len = facil_len;
2140         memcpy(message->param.facilityinfo.data, facil, facil_len);
2141         message_put(message);
2142 }
2143
2144
2145 /*
2146  * handler for isdn connections
2147  * incoming information are parsed and sent via message to the endpoint
2148  */
2149 #ifdef SOCKET_MISDN
2150 void Pdss1::message_isdn(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
2151 {
2152         int timer = 0;
2153
2154         switch (cmd)
2155         {
2156                 case MT_TIMEOUT:
2157                 if (!l3m->cause)
2158                 {
2159                         PERROR("Pdss1(%s) timeout without cause.\n", p_name);
2160                         break;
2161                 }
2162                 if (l3m->cause[0] != 5)
2163                 {
2164                         PERROR("Pdss1(%s) expecting timeout with timer diagnostic. (got len=%d)\n", p_name, l3m->cause[0]);
2165                         break;
2166                 }
2167                 timer = (l3m->cause[3]-'0')*100;
2168                 timer += (l3m->cause[4]-'0')*10;
2169                 timer += (l3m->cause[5]-'0');
2170                 l1l2l3_trace_header(p_m_mISDNport, this, L3_TIMEOUT_IND, DIRECTION_IN);
2171                 add_trace("timer", NULL, "%d", timer);
2172                 end_trace();
2173                 if (timer == 312)
2174                         t312_timeout_ind(cmd, pid, l3m);
2175                 break;
2176
2177                 case MT_SETUP:
2178                 if (p_state != PORT_STATE_IDLE)
2179                         break;
2180                 setup_ind(cmd, pid, l3m);
2181                 break;
2182
2183                 case MT_INFORMATION:
2184                 information_ind(cmd, pid, l3m);
2185                 break;
2186
2187                 case MT_SETUP_ACKNOWLEDGE:
2188                 if (p_state != PORT_STATE_OUT_SETUP)
2189                 {
2190                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
2191                         break;
2192                 }
2193                 setup_acknowledge_ind(cmd, pid, l3m);
2194                 break;
2195
2196                 case MT_CALL_PROCEEDING:
2197                 if (p_state != PORT_STATE_OUT_SETUP
2198                  && p_state != PORT_STATE_OUT_OVERLAP)
2199                 {
2200                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
2201                         break;
2202                 }
2203                 proceeding_ind(cmd, pid, l3m);
2204                 break;
2205
2206                 case MT_ALERTING:
2207                 if (p_state != PORT_STATE_OUT_SETUP
2208                  && p_state != PORT_STATE_OUT_OVERLAP
2209                  && p_state != PORT_STATE_OUT_PROCEEDING)
2210                 {
2211                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
2212                         break;
2213                 }
2214                 alerting_ind(cmd, pid, l3m);
2215                 break;
2216
2217                 case MT_CONNECT:
2218                 if (p_state != PORT_STATE_OUT_SETUP
2219                  && p_state != PORT_STATE_OUT_OVERLAP
2220                  && p_state != PORT_STATE_OUT_PROCEEDING
2221                  && p_state != PORT_STATE_OUT_ALERTING)
2222                 {
2223                         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);
2224                         break;
2225                 }
2226                 connect_ind(cmd, pid, l3m);
2227                 if (p_m_d_notify_pending)
2228                 {
2229                         /* send pending notify message during connect */
2230                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2231                         message_free(p_m_d_notify_pending);
2232                         p_m_d_notify_pending = NULL;
2233                 }
2234                 break;
2235
2236                 case MT_CONNECT_ACKNOWLEDGE:
2237                 if (p_state == PORT_STATE_CONNECT_WAITING)
2238                         new_state(PORT_STATE_CONNECT);
2239                 if (p_m_d_notify_pending)
2240                 {
2241                         /* send pending notify message during connect-ack */
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_DISCONNECT:
2249                 disconnect_ind(cmd, pid, l3m);
2250                 break;
2251
2252                 case MT_RELEASE:
2253                 release_ind(cmd, pid, l3m);
2254                 break;
2255
2256                 case MT_RELEASE_COMPLETE:
2257                 release_complete_ind(cmd, pid, l3m);
2258                 break;
2259
2260                 case MT_NOTIFY:
2261                 notify_ind(cmd, pid, l3m);
2262                 break;
2263
2264                 case MT_HOLD:
2265                 hold_ind(cmd, pid, l3m);
2266                 break;
2267
2268                 case MT_RETRIEVE:
2269                 retrieve_ind(cmd, pid, l3m);
2270                 break;
2271
2272                 case MT_SUSPEND:
2273                 suspend_ind(cmd, pid, l3m);
2274                 break;
2275
2276                 case MT_RESUME:
2277                 resume_ind(cmd, pid, l3m);
2278                 break;
2279
2280                 case MT_FACILITY:
2281                 facility_ind(cmd, pid, l3m);
2282                 break;
2283
2284                 case MT_FREE:
2285                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_L3ID_IND, DIRECTION_IN);
2286                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
2287                 end_trace();
2288                 p_m_d_l3id = 0;
2289                 p_m_d_ces = -1;
2290                 p_m_delete = 1;
2291                 /* sending release to endpoint in case we still have an endpoint
2292                  * this is because we don't get any response if a release_complete is received (or a release in release state)
2293                  */
2294                 while(p_epointlist) // only if not already released
2295                 {
2296                         struct lcr_msg *message;
2297                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2298                         if (p_m_d_collect_cause)
2299                         {
2300                                 message->param.disconnectinfo.cause = p_m_d_collect_cause;
2301                                 message->param.disconnectinfo.location = p_m_d_collect_location;
2302                         } else
2303                         {
2304                                 message->param.disconnectinfo.cause = CAUSE_NOUSER;
2305                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2306                         }
2307                         message_put(message);
2308                         /* remove epoint */
2309                         free_epointlist(p_epointlist);
2310
2311                         new_state(PORT_STATE_RELEASE);
2312                 }
2313                 break;
2314
2315                 default:
2316                 l1l2l3_trace_header(p_m_mISDNport, this, L3_UNKNOWN, DIRECTION_IN);
2317                 add_trace("unhandled", "cmd", "0x%x", cmd);
2318                 end_trace();
2319         }
2320 }
2321 #else
2322 void Pdss1::message_isdn(unsigned long prim, unsigned long dinfo, void *data)
2323 {
2324         int new_l3id;
2325         int timer_hex=0;
2326
2327         switch (prim)
2328         {
2329                 case CC_TIMEOUT | INDICATION:
2330                 if (p_m_d_ntmode)
2331                 {
2332                         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2333                         timer_hex = *((int *)(((char *)data)+headerlen));
2334                 }
2335                 if (timer_hex==0x312 && p_m_d_ntmode)
2336                 {
2337                         l1l2l3_trace_header(p_m_mISDNport, this, L3_TIMEOUT_IND, DIRECTION_IN);
2338                         add_trace("timer", NULL, "%x", timer_hex);
2339                         end_trace();
2340                         t312_timeout_ind(prim, dinfo, data);
2341                 }
2342                 break;
2343
2344                 case CC_SETUP | INDICATION:
2345                 if (p_state != PORT_STATE_IDLE)
2346                         break;
2347                 setup_ind(prim, dinfo, data);
2348                 break;
2349
2350                 case CC_SETUP | CONFIRM:
2351                 if (p_m_d_ntmode)
2352                 {
2353                         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_IND, DIRECTION_IN);
2354                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
2355                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
2356                         if ((p_m_d_l3id&0xff00) != 0xff00)
2357                                 PERROR("    strange setup-procid 0x%x\n", p_m_d_l3id);
2358                         p_m_d_l3id = *((int *)(((u_char *)data)+ mISDNUSER_HEAD_SIZE));
2359                         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2360                         end_trace();
2361                 }
2362                 break;
2363
2364                 case CC_INFORMATION | INDICATION:
2365                 information_ind(prim, dinfo, data);
2366                 break;
2367
2368                 case CC_SETUP_ACKNOWLEDGE | INDICATION:
2369                 if (p_state != PORT_STATE_OUT_SETUP)
2370                 {
2371                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
2372                         break;
2373                 }
2374                 setup_acknowledge_ind(prim, dinfo, data);
2375                 break;
2376
2377                 case CC_PROCEEDING | INDICATION:
2378                 if (p_state != PORT_STATE_OUT_SETUP
2379                  && p_state != PORT_STATE_OUT_OVERLAP)
2380                 {
2381                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
2382                         break;
2383                 }
2384                 proceeding_ind(prim, dinfo, data);
2385                 break;
2386
2387                 case CC_ALERTING | INDICATION:
2388                 if (p_state != PORT_STATE_OUT_SETUP
2389                  && p_state != PORT_STATE_OUT_OVERLAP
2390                  && p_state != PORT_STATE_OUT_PROCEEDING)
2391                 {
2392                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
2393                         break;
2394                 }
2395                 alerting_ind(prim, dinfo, data);
2396                 break;
2397
2398                 case CC_CONNECT | INDICATION:
2399                 if (p_state != PORT_STATE_OUT_SETUP
2400                  && p_state != PORT_STATE_OUT_OVERLAP
2401                  && p_state != PORT_STATE_OUT_PROCEEDING
2402                  && p_state != PORT_STATE_OUT_ALERTING)
2403                 {
2404                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
2405                         break;
2406                 }
2407                 connect_ind(prim, dinfo, data);
2408                 if (p_m_d_notify_pending)
2409                 {
2410                         /* send pending notify message during connect */
2411                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2412                         message_free(p_m_d_notify_pending);
2413                         p_m_d_notify_pending = NULL;
2414                 }
2415                 break;
2416
2417                 case CC_CONNECT_ACKNOWLEDGE | INDICATION:
2418                 case CC_CONNECT | CONFIRM:
2419                 if (p_state == PORT_STATE_CONNECT_WAITING)
2420                         new_state(PORT_STATE_CONNECT);
2421                 if (p_m_d_notify_pending)
2422                 {
2423                         /* send pending notify message during connect-ack */
2424                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2425                         message_free(p_m_d_notify_pending);
2426                         p_m_d_notify_pending = NULL;
2427                 }
2428                 break;
2429
2430                 case CC_DISCONNECT | INDICATION:
2431                 disconnect_ind(prim, dinfo, data);
2432                 break;
2433
2434                 case CC_RELEASE | CONFIRM:
2435                 case CC_RELEASE | INDICATION:
2436                 release_ind(prim, dinfo, data);
2437                 break;
2438
2439                 case CC_RELEASE_COMPLETE | INDICATION:
2440                 release_complete_ind(prim, dinfo, data);
2441                 break;
2442
2443                 case CC_RELEASE_COMPLETE | CONFIRM:
2444                 break;
2445
2446                 case CC_NOTIFY | INDICATION:
2447                 notify_ind(prim, dinfo, data);
2448                 break;
2449
2450                 case CC_HOLD | INDICATION:
2451                 hold_ind(prim, dinfo, data);
2452                 break;
2453
2454                 case CC_RETRIEVE | INDICATION:
2455                 retrieve_ind(prim, dinfo, data);
2456                 break;
2457
2458                 case CC_SUSPEND | INDICATION:
2459                 suspend_ind(prim, dinfo, data);
2460                 break;
2461
2462                 case CC_RESUME | INDICATION:
2463                 resume_ind(prim, dinfo, data);
2464                 break;
2465
2466                 case CC_FACILITY | INDICATION:
2467                 facility_ind(prim, dinfo, data);
2468                 break;
2469
2470                 case CC_RELEASE_CR | INDICATION:
2471                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_CR_IND, DIRECTION_IN);
2472                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
2473                 end_trace();
2474                 if (p_m_d_ntmode)
2475                 {
2476                         if ((p_m_d_l3id&0xff00) == 0xff00)
2477                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2478                 }
2479                 p_m_d_l3id = 0;
2480                 p_m_d_ces = -1;
2481                 p_m_delete = 1;
2482 //#warning remove me
2483 //PDEBUG(DEBUG_LOG, "JOLLY release cr %d\n", p_serial);
2484                 /* sending release to endpoint in case we still have an endpoint
2485                  * this is because we don't get any response if a release_complete is received (or a release in release state)
2486                  */
2487                 while(p_epointlist)
2488                 {
2489                         struct lcr_msg *message;
2490                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2491                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
2492                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
2493                         message_put(message);
2494                         /* remove epoint */
2495                         free_epointlist(p_epointlist);
2496
2497                         new_state(PORT_STATE_RELEASE);
2498                 }
2499                 break;
2500
2501                 case CC_NEW_CR | INDICATION:
2502                 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_IND, DIRECTION_IN);
2503                 if (p_m_d_l3id)
2504                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
2505                 if (p_m_d_ntmode)
2506                 {
2507                         new_l3id = *((int *)(((u_char *)data+mISDNUSER_HEAD_SIZE)));
2508                         if (((new_l3id&0xff00)!=0xff00) && ((p_m_d_l3id&0xff00)==0xff00))
2509                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2510                 } else
2511                 {
2512                         new_l3id = dinfo;
2513                 }
2514                 p_m_d_l3id = new_l3id;
2515                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
2516                 end_trace();
2517                 break;
2518
2519                 default:
2520                 l1l2l3_trace_header(p_m_mISDNport, this, L3_UNKNOWN, DIRECTION_IN);
2521                 add_trace("unhandled", "prim", "0x%x", prim);
2522                 end_trace();
2523         }
2524 }
2525 #endif
2526
2527 void Pdss1::new_state(int state)
2528 {
2529 //      class Endpoint *epoint;
2530
2531         /* set timeout */
2532         if (state == PORT_STATE_IN_OVERLAP)
2533         {
2534                 p_m_timeout = p_m_mISDNport->ifport->tout_dialing;
2535                 time(&p_m_timer);
2536         }
2537         if (state != p_state)
2538         {
2539                 if (state == PORT_STATE_IN_SETUP
2540                  || state == PORT_STATE_OUT_SETUP
2541                  || state == PORT_STATE_IN_OVERLAP
2542                  || state == PORT_STATE_OUT_OVERLAP)
2543                 {
2544                         p_m_timeout = p_m_mISDNport->ifport->tout_setup;
2545                         time(&p_m_timer);
2546                 }
2547                 if (state == PORT_STATE_IN_PROCEEDING
2548                  || state == PORT_STATE_OUT_PROCEEDING)
2549                 {
2550                         p_m_timeout = p_m_mISDNport->ifport->tout_proceeding;
2551                         time(&p_m_timer);
2552                 }
2553                 if (state == PORT_STATE_IN_ALERTING
2554                  || state == PORT_STATE_OUT_ALERTING)
2555                 {
2556                         p_m_timeout = p_m_mISDNport->ifport->tout_alerting;
2557                         time(&p_m_timer);
2558                 }
2559                 if (state == PORT_STATE_CONNECT
2560                  || state == PORT_STATE_CONNECT_WAITING)
2561                 {
2562                         p_m_timeout = 0;
2563                 }
2564                 if (state == PORT_STATE_IN_DISCONNECT
2565                  || state == PORT_STATE_OUT_DISCONNECT)
2566                 {
2567                         p_m_timeout = p_m_mISDNport->ifport->tout_disconnect;
2568                         time(&p_m_timer);
2569                 }
2570         }
2571         
2572         Port::new_state(state);
2573 }
2574
2575
2576 /*
2577  * handler
2578  */
2579 int Pdss1::handler(void)
2580 {
2581         int ret;
2582
2583         if ((ret = PmISDN::handler()))
2584                 return(ret);
2585
2586         /* handle destruction */
2587         if (p_m_delete && p_m_d_l3id==0)
2588         {
2589                 delete this;
2590                 return(-1);
2591         }
2592
2593         return(0);
2594 }
2595
2596
2597 /*
2598  * handles all messages from endpoint
2599  */
2600 /* MESSAGE_INFORMATION */
2601 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
2602 {
2603 #ifdef SOCKET_MISDN
2604         l3_msg *l3m;
2605 #else
2606         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2607         msg_t *dmsg;
2608         INFORMATION_t *information;
2609 #endif
2610
2611         if (param->information.id[0]) /* only if we have something to dial */
2612         {
2613 #ifdef SOCKET_MISDN
2614                 l3m = create_l3msg();
2615 #else
2616                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2617                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2618 #endif
2619                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2620 #ifdef SOCKET_MISDN
2621                 enc_ie_called_pn(l3m, 0, 1, (unsigned char *)param->information.id);
2622 #else
2623                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.id);
2624 #endif
2625                 end_trace();
2626 #ifdef SOCKET_MISDN
2627                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2628 #else
2629                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2630 #endif
2631         }
2632         new_state(p_state);
2633 }
2634
2635
2636 int newteid = 0;
2637
2638 /* MESSAGE_SETUP */
2639 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2640 {
2641 #ifdef SOCKET_MISDN
2642         l3_msg *l3m;
2643         int ret;
2644 #else
2645         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2646         msg_t *dmsg;
2647         INFORMATION_t *information;
2648         SETUP_t *setup;
2649         int i;
2650 #endif
2651         int plan, type, screen, present, reason;
2652         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2653         int channel, exclusive;
2654         struct epoint_list *epointlist;
2655
2656         /* release if port is blocked */
2657         if (p_m_mISDNport->ifport->block)
2658         {
2659                 struct lcr_msg *message;
2660
2661                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2662                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2663                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2664                 message_put(message);
2665                 new_state(PORT_STATE_RELEASE);
2666                 p_m_delete = 1;
2667                 return;
2668         }
2669
2670         /* copy setup infos to port */
2671         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2672         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2673         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2674         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2675 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2676         /* screen outgoing caller id */
2677         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
2678
2679         /* only display at connect state: this case happens if endpoint is in connected mode */
2680         if (p_state==PORT_STATE_CONNECT)
2681         {
2682                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2683                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2684                         return;
2685                 if (p_callerinfo.display[0])
2686                 {
2687                         /* sending information */
2688 #ifdef SOCKET_MISDN
2689                         l3m = create_l3msg();
2690 #else
2691                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2692                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2693 #endif
2694                         l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2695                         if (p_m_d_ntmode)
2696 #ifdef SOCKET_MISDN
2697                                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
2698 #else
2699                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2700 #endif
2701                         end_trace();
2702 #ifdef SOCKET_MISDN
2703                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2704 #else
2705                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2706 #endif
2707                         return;
2708                 }
2709         }
2710
2711         /* attach only if not already */
2712         epointlist = p_epointlist;
2713         while(epointlist)
2714         {
2715                 if (epointlist->epoint_id == epoint_id)
2716                         break;
2717                 epointlist = epointlist->next;
2718         }
2719         if (!epointlist)
2720                 epointlist_new(epoint_id);
2721
2722         /* get channel */
2723         exclusive = 0;
2724         if (p_m_b_channel)
2725         {
2726                 channel = p_m_b_channel;
2727                 exclusive = p_m_b_exclusive;
2728         } else
2729                 channel = CHANNEL_ANY;
2730         /* nt-port with no channel, not reserverd */
2731         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2732                 channel = CHANNEL_NO;
2733
2734 #ifdef SOCKET_MISDN
2735         /* creating l3id */
2736         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
2737         /* see MT_ASSIGN notes at do_layer3() */
2738         mt_assign_pid = 0;
2739         ret = p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ASSIGN, 0, NULL);
2740         if (mt_assign_pid == 0 || ret < 0)
2741         {
2742                 struct lcr_msg *message;
2743
2744                 add_trace("callref", NULL, "no free id");
2745                 end_trace();
2746                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2747                 message->param.disconnectinfo.cause = 47;
2748                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2749                 message_put(message);
2750                 new_state(PORT_STATE_RELEASE);
2751                 p_m_delete = 1;
2752                 return;
2753         }
2754         p_m_d_l3id = mt_assign_pid;
2755         mt_assign_pid = ~0;
2756 #else
2757         /* creating l3id */
2758         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_REQ, DIRECTION_OUT);
2759         if (p_m_d_ntmode)
2760         {
2761                 i = 0;
2762                 while(i < 0x100)
2763                 {
2764                         if (p_m_mISDNport->procids[i] == 0)
2765                                 break;
2766                         i++;
2767                 }
2768                 if (i == 0x100)
2769                 {
2770                         struct lcr_msg *message;
2771
2772                         add_trace("callref", NULL, "no free id");
2773                         end_trace();
2774                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2775                         message->param.disconnectinfo.cause = 47;
2776                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2777                         message_put(message);
2778                         new_state(PORT_STATE_RELEASE);
2779                         p_m_delete = 1;
2780                         return;
2781                 }
2782                 p_m_mISDNport->procids[i] = 1;
2783                 p_m_d_l3id = 0xff00 | i;
2784         } else
2785         {
2786                 iframe_t ncr;
2787                 /* if we are in te-mode, we need to create a process first */
2788                 if (newteid++ > 0x7fff)
2789                         newteid = 0x0001;
2790                 p_m_d_l3id = (entity<<16) | newteid;
2791                 /* preparing message */
2792                 ncr.prim = CC_NEW_CR | REQUEST; 
2793                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2794                 ncr.dinfo = p_m_d_l3id;
2795                 ncr.len = 0;
2796                 /* send message */
2797                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2798         }
2799 #endif
2800         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2801         end_trace();
2802
2803         /* preparing setup message */
2804 #ifdef SOCKET_MISDN
2805         l3m = create_l3msg();
2806 #else
2807         dmsg = create_l3msg(CC_SETUP | REQUEST, MT_SETUP, p_m_d_l3id, sizeof(SETUP_t), p_m_d_ntmode);
2808         setup = (SETUP_t *)(dmsg->data + headerlen);
2809 #endif
2810         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_REQ, DIRECTION_OUT);
2811         /* channel information */
2812         if (channel >= 0) /* it should */
2813         {
2814 #ifdef SOCKET_MISDN
2815                 enc_ie_channel_id(l3m, exclusive, channel);
2816 #else
2817                 enc_ie_channel_id(&setup->CHANNEL_ID, dmsg, exclusive, channel);
2818 #endif
2819         }
2820         /* caller information */
2821         plan = 1;
2822         switch (p_callerinfo.ntype)
2823         {
2824                 case INFO_NTYPE_INTERNATIONAL:
2825                 type = 0x1;
2826                 break;
2827                 case INFO_NTYPE_NATIONAL:
2828                 type = 0x2;
2829                 break;
2830                 case INFO_NTYPE_SUBSCRIBER:
2831                 type = 0x4;
2832                 break;
2833                 default: /* INFO_NTYPE_UNKNOWN */
2834                 type = 0x0;
2835                 break;
2836         }
2837         switch (p_callerinfo.screen)
2838         {
2839                 case INFO_SCREEN_USER:
2840                 screen = 0;
2841                 break;
2842                 default: /* INFO_SCREEN_NETWORK */
2843                 screen = 3;
2844                 break;
2845         }
2846         switch (p_callerinfo.present)
2847         {
2848                 case INFO_PRESENT_RESTRICTED:
2849                 present = 1;
2850                 break;
2851                 case INFO_PRESENT_NOTAVAIL:
2852                 present = 2;
2853                 break;
2854                 default: /* INFO_PRESENT_ALLOWED */
2855                 present = 0;
2856                 break;
2857         }
2858         if (type >= 0)
2859 #ifdef SOCKET_MISDN
2860                 enc_ie_calling_pn(l3m, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2861 #else
2862                 enc_ie_calling_pn(&setup->CALLING_PN, dmsg, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2863 #endif
2864         /* dialing information */
2865         if (p_dialinginfo.id[0]) /* only if we have something to dial */
2866         {
2867 #ifdef SOCKET_MISDN
2868                 enc_ie_called_pn(l3m, 0, 1, (unsigned char *)p_dialinginfo.id);
2869 #else
2870                 enc_ie_called_pn(&setup->CALLED_PN, dmsg, 0, 1, (unsigned char *)p_dialinginfo.id);
2871 #endif
2872         }
2873         /* sending complete */
2874         if (p_dialinginfo.sending_complete)
2875 #ifdef SOCKET_MISDN
2876                 enc_ie_complete(l3m, 1);
2877 #else
2878                 enc_ie_complete(&setup->COMPLETE, dmsg, 1);
2879 #endif
2880         /* sending user-user */
2881         if (param->setup.useruser.len)
2882         {
2883 #ifdef SOCKET_MISDN
2884                 enc_ie_useruser(l3m, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2885 #else
2886                 enc_ie_useruser(&setup->USER_USER, dmsg, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2887 #endif
2888         }
2889         /* redirecting number */
2890         plan = 1;
2891         switch (p_redirinfo.ntype)
2892         {
2893                 case INFO_NTYPE_INTERNATIONAL:
2894                 type = 0x1;
2895                 break;
2896                 case INFO_NTYPE_NATIONAL:
2897                 type = 0x2;
2898                 break;
2899                 case INFO_NTYPE_SUBSCRIBER:
2900                 type = 0x4;
2901                 break;
2902                 default: /* INFO_NTYPE_UNKNOWN */
2903                 type = 0x0;
2904                 break;
2905         }
2906         switch (p_redirinfo.screen)
2907         {
2908                 case INFO_SCREEN_USER:
2909                 screen = 0;
2910                 break;
2911                 default: /* INFO_SCREE_NETWORK */
2912                 screen = 3;
2913                 break;
2914         }
2915         switch (p_redirinfo.reason)
2916         {
2917                 case INFO_REDIR_BUSY:
2918                 reason = 1;
2919                 break;
2920                 case INFO_REDIR_NORESPONSE:
2921                 reason = 2;
2922                 break;
2923                 case INFO_REDIR_UNCONDITIONAL:
2924                 reason = 15;
2925                 break;
2926                 case INFO_REDIR_CALLDEFLECT:
2927                 reason = 10;
2928                 break;
2929                 case INFO_REDIR_OUTOFORDER:
2930                 reason = 9;
2931                 break;
2932                 default: /* INFO_REDIR_UNKNOWN */
2933                 reason = 0;
2934                 break;
2935         }
2936         switch (p_redirinfo.present)
2937         {
2938                 case INFO_PRESENT_NULL: /* no redir at all */
2939                 present = -1;
2940                 screen = -1;
2941                 reason = -1;
2942                 plan = -1;
2943                 type = -1;
2944                 break;
2945                 case INFO_PRESENT_RESTRICTED:
2946                 present = 1;
2947                 break;
2948                 case INFO_PRESENT_NOTAVAIL:
2949                 present = 2;
2950                 break;
2951                 default: /* INFO_PRESENT_ALLOWED */
2952                 present = 0;
2953                 break;
2954         }
2955         /* sending redirecting number only in ntmode */
2956         if (type >= 0 && p_m_d_ntmode)
2957 #ifdef SOCKET_MISDN
2958                 enc_ie_redir_nr(l3m, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2959 #else
2960                 enc_ie_redir_nr(&setup->REDIR_NR, dmsg, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2961 #endif
2962         /* bearer capability */
2963 //printf("hlc=%d\n",p_capainfo.hlc);
2964         coding = 0;
2965         capability = p_capainfo.bearer_capa;
2966         mode = p_capainfo.bearer_mode;
2967         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2968         switch (p_capainfo.bearer_info1)
2969         {
2970                 case INFO_INFO1_NONE:
2971                 user = -1;
2972                 break;
2973                 default:
2974                 user = p_capainfo.bearer_info1 & 0x7f;
2975                 break;
2976         }
2977 #ifdef SOCKET_MISDN
2978         enc_ie_bearer(l3m, coding, capability, mode, rate, -1, user);
2979 #else
2980         enc_ie_bearer(&setup->BEARER, dmsg, coding, capability, mode, rate, -1, user);
2981 #endif
2982         /* hlc */
2983         if (p_capainfo.hlc)
2984         {
2985                 coding = 0;
2986                 interpretation = 4;
2987                 presentation = 1;
2988                 hlc = p_capainfo.hlc & 0x7f;
2989                 exthlc = -1;
2990                 if (p_capainfo.exthlc)
2991                         exthlc = p_capainfo.exthlc & 0x7f;
2992 #ifdef SOCKET_MISDN
2993                 enc_ie_hlc(l3m, coding, interpretation, presentation, hlc, exthlc);
2994 #else
2995                 enc_ie_hlc(&setup->HLC, dmsg, coding, interpretation, presentation, hlc, exthlc);
2996 #endif
2997         }
2998
2999         /* display */
3000         if (p_callerinfo.display[0] && p_m_d_ntmode)
3001 #ifdef SOCKET_MISDN
3002                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
3003 #else
3004                 enc_ie_display(&setup->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
3005 #endif
3006         /* nt-mode: CNIP (calling name identification presentation) */
3007 //      if (p_callerinfo.name[0] && p_m_d_ntmode)
3008 //              enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
3009         end_trace();
3010
3011         /* send setup message now */
3012 #ifdef SOCKET_MISDN
3013         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SETUP, p_m_d_l3id, l3m);
3014 #else
3015         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3016 #endif
3017
3018         new_state(PORT_STATE_OUT_SETUP);
3019 }
3020
3021 /* MESSAGE_FACILITY */
3022 void Pdss1::message_facility(unsigned long epoint_id, int message_id, union parameter *param)
3023 {
3024 #ifdef SOCKET_MISDN
3025         l3_msg *l3m;
3026 #else
3027         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3028         msg_t *dmsg;
3029         FACILITY_t *facility;
3030 #endif
3031
3032         /* facility will not be sent to external lines */
3033         if (!p_m_d_ntmode)
3034                 return;
3035
3036         /* sending facility */
3037 #ifdef SOCKET_MISDN
3038         l3m = create_l3msg();
3039 #else
3040         dmsg = create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, p_m_d_l3id, sizeof(FACILITY_t), p_m_d_ntmode);
3041         facility = (FACILITY_t *)(dmsg->data + headerlen);
3042 #endif
3043         l1l2l3_trace_header(p_m_mISDNport, this, L3_FACILITY_REQ, DIRECTION_OUT);
3044 #ifdef SOCKET_MISDN
3045         enc_ie_facility(l3m, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
3046 #else
3047         enc_ie_facility(&facility->FACILITY, dmsg, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
3048 #endif
3049         end_trace();
3050 #ifdef SOCKET_MISDN
3051         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_FACILITY, p_m_d_l3id, l3m);
3052 #else
3053         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3054 #endif
3055 }
3056
3057 /* MESSAGE_NOTIFY */
3058 void Pdss1::message_notify(unsigned long epoint_id, int message_id, union parameter *param)
3059 {
3060 #ifdef SOCKET_MISDN
3061         l3_msg *l3m;
3062 #else
3063         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3064         msg_t *dmsg;
3065         INFORMATION_t *information;
3066         NOTIFY_t *notification;
3067 #endif
3068         int notify;
3069         int plan, type = -1, present;
3070
3071         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
3072                 notify = param->notifyinfo.notify & 0x7f;
3073         else
3074                 notify = -1;
3075         if (p_state != PORT_STATE_CONNECT)
3076         {
3077                 /* notify only allowed in active state */
3078                 notify = -1;
3079         }
3080         if (notify >= 0)
3081         {
3082                 plan = 1;
3083                 switch (param->notifyinfo.ntype)
3084                 {
3085                         case INFO_NTYPE_INTERNATIONAL:
3086                         type = 1;
3087                         break;
3088                         case INFO_NTYPE_NATIONAL:
3089                         type = 2;
3090                         break;
3091                         case INFO_NTYPE_SUBSCRIBER:
3092                         type = 4;
3093                         break;
3094                         default: /* INFO_NTYPE_UNKNOWN */
3095                         type = 0;
3096                         break;
3097                 }
3098                 switch (param->notifyinfo.present)
3099                 {
3100                         case INFO_PRESENT_NULL: /* no redir at all */
3101                         present = -1;
3102                         plan = -1;
3103                         type = -1;
3104                         break;
3105                         case INFO_PRESENT_RESTRICTED:
3106                         present = 1;
3107                         break;
3108                         case INFO_PRESENT_NOTAVAIL:
3109                         present = 2;
3110                         break;
3111                         default: /* INFO_PRESENT_ALLOWED */
3112                         present = 0;
3113                         break;
3114                 }
3115         }
3116
3117         if (notify<0 && !param->notifyinfo.display[0])
3118         {
3119                 /* nothing to notify, nothing to display */
3120                 return;
3121         }
3122
3123         if (notify >= 0)
3124         {
3125                 if (p_state!=PORT_STATE_CONNECT)
3126                 {
3127                         /* queue notification */
3128                         if (p_m_d_notify_pending)
3129                                 message_free(p_m_d_notify_pending);
3130                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
3131                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
3132                 } else
3133                 {
3134                         /* sending notification */
3135 #ifdef SOCKET_MISDN
3136                         l3m = create_l3msg();
3137 #else
3138                         dmsg = create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, p_m_d_l3id, sizeof(NOTIFY_t), p_m_d_ntmode);
3139                         notification = (NOTIFY_t *)(dmsg->data + headerlen);
3140 #endif
3141                         l1l2l3_trace_header(p_m_mISDNport, this, L3_NOTIFY_REQ, DIRECTION_OUT);
3142 #ifdef SOCKET_MISDN
3143                         enc_ie_notify(l3m, notify);
3144 #else
3145                         enc_ie_notify(&notification->NOTIFY, dmsg, notify);
3146 #endif
3147                         /* sending redirection number only in ntmode */
3148                         if (type >= 0 && p_m_d_ntmode)
3149 #ifdef SOCKET_MISDN
3150                                 enc_ie_redir_dn(l3m, type, plan, present, (unsigned char *)param->notifyinfo.id);
3151 #else
3152                                 enc_ie_redir_dn(&notification->REDIR_DN, dmsg, type, plan, present, (unsigned char *)param->notifyinfo.id);
3153 #endif
3154                         if (param->notifyinfo.display[0] && p_m_d_ntmode)
3155 #ifdef SOCKET_MISDN
3156                                 enc_ie_display(l3m, (unsigned char *)param->notifyinfo.display);
3157 #else
3158                                 enc_ie_display(&notification->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
3159 #endif
3160                         end_trace();
3161 #ifdef SOCKET_MISDN
3162                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_NOTIFY, p_m_d_l3id, l3m);
3163 #else
3164                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3165 #endif
3166                 }
3167         } else if (p_m_d_ntmode)
3168         {
3169                 /* sending information */
3170 #ifdef SOCKET_MISDN
3171                 l3m = create_l3msg();
3172 #else
3173                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
3174                 information = (INFORMATION_t *)(dmsg->data + headerlen);
3175 #endif
3176                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
3177 #ifdef SOCKET_MISDN
3178                 enc_ie_display(l3m, (unsigned char *)param->notifyinfo.display);
3179 #else
3180                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
3181 #endif
3182                 end_trace();
3183 #ifdef SOCKET_MISDN
3184                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
3185 #else
3186                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3187 #endif
3188         }
3189 }
3190
3191 /* MESSAGE_OVERLAP */
3192 void Pdss1::message_overlap(unsigned long epoint_id, int message_id, union parameter *param)
3193 {
3194 #ifdef SOCKET_MISDN
3195         l3_msg *l3m;
3196 #else
3197         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3198         msg_t *dmsg;
3199         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
3200 #endif
3201
3202         /* sending setup_acknowledge */
3203 #ifdef SOCKET_MISDN
3204         l3m = create_l3msg();
3205 #else
3206         dmsg = create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, sizeof(SETUP_ACKNOWLEDGE_t), p_m_d_ntmode);
3207         setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
3208 #endif
3209         l1l2l3_trace_header(p_m_mISDNport, this, L3_SETUP_ACKNOWLEDGE_REQ, DIRECTION_OUT);
3210         /* channel information */
3211         if (p_state == PORT_STATE_IN_SETUP)
3212 #ifdef SOCKET_MISDN
3213                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3214 #else
3215                 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3216 #endif
3217         /* progress information */
3218         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3219          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3220          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3221         if (p_m_mISDNport->tones)
3222 #ifdef SOCKET_MISDN
3223                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3224 #else
3225                 enc_ie_progress(&setup_acknowledge->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3226 #endif
3227         end_trace();
3228 #ifdef SOCKET_MISDN
3229         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, l3m);
3230 #else
3231         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3232 #endif
3233
3234         new_state(PORT_STATE_IN_OVERLAP);
3235 }
3236
3237 /* MESSAGE_PROCEEDING */
3238 void Pdss1::message_proceeding(unsigned long epoint_id, int message_id, union parameter *param)
3239 {
3240 #ifdef SOCKET_MISDN
3241         l3_msg *l3m;
3242 #else
3243         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3244         msg_t *dmsg;
3245         CALL_PROCEEDING_t *proceeding;
3246 #endif
3247
3248         /* sending proceeding */
3249 #ifdef SOCKET_MISDN
3250         l3m = create_l3msg();
3251 #else
3252         dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3253         proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3254 #endif
3255         l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3256         /* channel information */
3257         if (p_state == PORT_STATE_IN_SETUP)
3258 #ifdef SOCKET_MISDN
3259                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3260 #else
3261                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3262 #endif
3263         /* progress information */
3264         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3265          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3266          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3267         if (p_m_mISDNport->tones)
3268 #ifdef SOCKET_MISDN
3269                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3270 #else
3271                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3272 #endif
3273         end_trace();
3274 #ifdef SOCKET_MISDN
3275         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3276 #else
3277         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3278 #endif
3279
3280         new_state(PORT_STATE_IN_PROCEEDING);
3281 }
3282
3283 /* MESSAGE_ALERTING */
3284 void Pdss1::message_alerting(unsigned long epoint_id, int message_id, union parameter *param)
3285 {
3286 #ifdef SOCKET_MISDN
3287         l3_msg *l3m;
3288 #else
3289         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3290         msg_t *dmsg;
3291         ALERTING_t *alerting;
3292 #endif
3293
3294         /* NT-MODE in setup state we must send PROCEEDING first */
3295         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
3296         {
3297                 /* sending proceeding */
3298 #ifdef SOCKET_MISDN
3299                 l3m = create_l3msg();
3300 #else
3301                 CALL_PROCEEDING_t *proceeding;
3302                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3303                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3304 #endif
3305                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3306                 /* channel information */
3307 #ifdef SOCKET_MISDN
3308                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3309 #else
3310                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3311 #endif
3312                 /* progress information */
3313                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3314                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
3315                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3316 #ifdef SOCKET_MISDN
3317                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3318 #else
3319                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3320 #endif
3321                 end_trace();
3322 #ifdef SOCKET_MISDN
3323                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3324 #else
3325                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3326 #endif
3327                 new_state(PORT_STATE_IN_PROCEEDING);
3328         }
3329
3330         /* sending alerting */
3331 #ifdef SOCKET_MISDN
3332         l3m = create_l3msg();
3333 #else
3334         dmsg = create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, p_m_d_l3id, sizeof(ALERTING_t), p_m_d_ntmode);
3335         alerting = (ALERTING_t *)(dmsg->data + headerlen);
3336 #endif
3337         l1l2l3_trace_header(p_m_mISDNport, this, L3_ALERTING_REQ, DIRECTION_OUT);
3338         /* channel information */
3339         if (p_state == PORT_STATE_IN_SETUP)
3340 #ifdef SOCKET_MISDN
3341                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3342 #else
3343                 enc_ie_channel_id(&alerting->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3344 #endif
3345         /* progress information */
3346         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3347          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3348          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3349         if (p_m_mISDNport->tones)
3350 #ifdef SOCKET_MISDN
3351                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3352 #else
3353                 enc_ie_progress(&alerting->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3354 #endif
3355         end_trace();
3356 #ifdef SOCKET_MISDN
3357         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ALERTING, p_m_d_l3id, l3m);
3358 #else
3359         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3360 #endif
3361
3362         new_state(PORT_STATE_IN_ALERTING);
3363 }
3364
3365 /* MESSAGE_CONNECT */
3366 void Pdss1::message_connect(unsigned long epoint_id, int message_id, union parameter *param)
3367 {
3368 #ifdef SOCKET_MISDN
3369         l3_msg *l3m;
3370 #else
3371         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3372         msg_t *dmsg;
3373         INFORMATION_t *information;
3374         CONNECT_t *connect;
3375 #endif
3376         int type, plan, present, screen;
3377         class Endpoint *epoint;
3378
3379         /* NT-MODE in setup state we must send PROCEEDING first */
3380         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
3381         {
3382                 /* sending proceeding */
3383 #ifdef SOCKET_MISDN
3384                 l3m = create_l3msg();
3385 #else
3386                 CALL_PROCEEDING_t *proceeding;
3387                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3388                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3389 #endif
3390                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3391                 /* channel information */
3392 #ifdef SOCKET_MISDN
3393                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3394 #else
3395                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3396 #endif
3397 //              /* progress information */
3398 //              if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3399 //               || p_capainfo.bearer_capa==INFO_BC_AUDIO
3400 //               || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3401 #ifdef SOCKET_MISDN
3402 //              enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3403 #else
3404 //              enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3405 #endif
3406                 end_trace();
3407 #ifdef SOCKET_MISDN
3408                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3409 #else
3410                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3411 #endif
3412                 new_state(PORT_STATE_IN_PROCEEDING);
3413         }
3414
3415         /* copy connected information */
3416         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
3417         /* screen outgoing caller id */
3418         do_screen(1, p_connectinfo.id, sizeof(p_connectinfo.id), &p_connectinfo.ntype, &p_connectinfo.present, p_m_mISDNport->ifport->interface);
3419
3420         /* only display at connect state */
3421         if (p_state == PORT_STATE_CONNECT)
3422         if (p_connectinfo.display[0])
3423         {
3424                 /* sending information */
3425 #ifdef SOCKET_MISDN
3426                 l3m = create_l3msg();
3427 #else
3428                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
3429                 information = (INFORMATION_t *)(dmsg->data + headerlen);
3430 #endif
3431                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
3432                 if (p_m_d_ntmode)
3433 #ifdef SOCKET_MISDN
3434                         enc_ie_display(l3m, (unsigned char *)p_connectinfo.display);
3435 #else
3436                         enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
3437 #endif
3438                 end_trace();
3439 #ifdef SOCKET_MISDN
3440                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
3441 #else
3442                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3443 #endif
3444                 return;
3445         }
3446
3447         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)
3448         {
3449                 /* connect command only possible in setup, proceeding or alerting state */
3450                 return;
3451         }
3452
3453         /* preparing connect message */
3454 #ifdef SOCKET_MISDN
3455         l3m = create_l3msg();
3456 #else
3457         dmsg = create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, p_m_d_l3id, sizeof(CONNECT_t), p_m_d_ntmode);
3458         connect = (CONNECT_t *)(dmsg->data + headerlen);
3459 #endif
3460         l1l2l3_trace_header(p_m_mISDNport, this, L3_CONNECT_REQ, DIRECTION_OUT);
3461         /* connect information */
3462         plan = 1;
3463         switch (p_connectinfo.ntype)
3464         {
3465                 case INFO_NTYPE_INTERNATIONAL:
3466                 type = 0x1;
3467                 break;
3468                 case INFO_NTYPE_NATIONAL:
3469                 type = 0x2;
3470                 break;
3471                 case INFO_NTYPE_SUBSCRIBER:
3472                 type = 0x4;
3473                 break;
3474                 default: /* INFO_NTYPE_UNKNOWN */
3475                 type = 0x0;
3476                 break;
3477         }
3478         switch (param->connectinfo.screen)
3479         {
3480                 case INFO_SCREEN_USER:
3481                 screen = 0;
3482                 break;
3483                 default: /* INFO_SCREE_NETWORK */
3484                 screen = 3;
3485                 break;
3486         }
3487         switch (p_connectinfo.present)
3488         {
3489                 case INFO_PRESENT_NULL: /* no colp at all */
3490                 present = -1;
3491                 screen = -1;
3492                 plan = -1;
3493                 type = -1;
3494                 break;
3495                 case INFO_PRESENT_RESTRICTED:
3496                 present = 1;
3497                 break;
3498                 case INFO_PRESENT_NOTAVAIL:
3499                 present = 2;
3500                 break;
3501                 default: /* INFO_PRESENT_ALLOWED */
3502                 present = 0;
3503                 break;
3504         }
3505         if (type >= 0)
3506 #ifdef SOCKET_MISDN
3507                 enc_ie_connected_pn(l3m, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
3508 #else
3509                 enc_ie_connected_pn(&connect->CONNECT_PN, dmsg, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
3510 #endif
3511         /* display */
3512         if (p_connectinfo.display[0] && p_m_d_ntmode)
3513 #ifdef SOCKET_MISDN
3514                 enc_ie_display(l3m, (unsigned char *)p_connectinfo.display);
3515 #else
3516                 enc_ie_display(&connect->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
3517 #endif
3518         /* nt-mode: CONP (connected name identification presentation) */
3519 //      if (p_connectinfo.name[0] && p_m_d_ntmode)
3520 //              enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
3521         /* date & time */
3522         if (p_m_d_ntmode)
3523         {
3524                 epoint = find_epoint_id(epoint_id);
3525 #ifdef SOCKET_MISDN
3526                 enc_ie_date(l3m, now, p_settings.no_seconds);
3527 #else
3528                 enc_ie_date(&connect->DATE, dmsg, now, p_settings.no_seconds);
3529 #endif
3530         }
3531         end_trace();
3532         /* finally send message */
3533 #ifdef SOCKET_MISDN
3534         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CONNECT, p_m_d_l3id, l3m);
3535 #else
3536         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3537 #endif
3538
3539         if (p_m_d_ntmode)
3540                 new_state(PORT_STATE_CONNECT);
3541         else
3542                 new_state(PORT_STATE_CONNECT_WAITING);
3543         set_tone("", NULL);
3544 }
3545
3546 /* MESSAGE_DISCONNECT */
3547 void Pdss1::message_disconnect(unsigned long epoint_id, int message_id, union parameter *param)
3548 {
3549 #ifdef SOCKET_MISDN
3550         l3_msg *l3m;
3551 #else
3552         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3553         msg_t *dmsg;
3554         DISCONNECT_t *disconnect;
3555         RELEASE_COMPLETE_t *release_complete;
3556 #endif
3557         struct lcr_msg *message;
3558         char *p = NULL;
3559
3560         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
3561 //      if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->tones)
3562 if (/*   ||*/ p_state==PORT_STATE_OUT_SETUP)
3563         {
3564                 /* sending release to endpoint */
3565                 while(p_epointlist)
3566                 {
3567                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
3568                         memcpy(&message->param, param, sizeof(union parameter));
3569                         message_put(message);
3570                         /* remove epoint */
3571                         free_epointlist(p_epointlist);
3572                 }
3573                 /* sending release */
3574 #ifdef SOCKET_MISDN
3575                 l3m = create_l3msg();
3576 #else
3577                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
3578                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
3579 #endif
3580                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
3581                 /* send cause */
3582 #ifdef SOCKET_MISDN
3583                 enc_ie_cause(l3m, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
3584 #else
3585                 enc_ie_cause(&release_complete->CAUSE, dmsg, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
3586 #endif
3587                 end_trace();
3588 #ifdef SOCKET_MISDN
3589                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
3590 #else
3591                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3592 #endif
3593                 new_state(PORT_STATE_RELEASE);
3594                 p_m_delete = 1;
3595                 return;
3596         }
3597
3598         /* workarround: NT-MODE in setup state we must send PROCEEDING first to make it work */
3599         if (p_state==PORT_STATE_IN_SETUP)
3600         {
3601                 /* sending proceeding */
3602 #ifdef SOCKET_MISDN
3603                 l3m = create_l3msg();
3604 #else
3605                 CALL_PROCEEDING_t *proceeding;
3606                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3607                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3608 #endif
3609                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3610                 /* channel information */
3611 #ifdef SOCKET_MISDN
3612                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3613 #else
3614                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3615 #endif
3616                 /* progress information */
3617                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3618                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
3619                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3620 #ifdef SOCKET_MISDN
3621                         enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3622 #else
3623                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3624 #endif
3625                 end_trace();
3626 #ifdef SOCKET_MISDN
3627                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3628 #else
3629                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3630 #endif
3631                 new_state(PORT_STATE_IN_PROCEEDING);
3632         }
3633
3634         /* sending disconnect */
3635 #ifdef SOCKET_MISDN
3636         l3m = create_l3msg();
3637 #else
3638         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
3639         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
3640 #endif
3641         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
3642         /* progress information */
3643         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3644          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3645          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3646         if (p_m_mISDNport->tones)
3647 #ifdef SOCKET_MISDN
3648                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3649 #else
3650                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3651 #endif
3652         /* send cause */
3653 #ifdef SOCKET_MISDN
3654         enc_ie_cause(l3m, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
3655 #else
3656         enc_ie_cause(&disconnect->CAUSE, dmsg, (!p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_REMOTE:param->disconnectinfo.location, param->disconnectinfo.cause);
3657 #endif
3658         /* send display */
3659         if (param->disconnectinfo.display[0])
3660                 p = param->disconnectinfo.display;
3661         if (p) if (*p && p_m_d_ntmode)
3662 #ifdef SOCKET_MISDN
3663                 enc_ie_display(l3m, (unsigned char *)p);
3664 #else
3665                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
3666 #endif
3667         end_trace();
3668 #ifdef SOCKET_MISDN
3669         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
3670 #else
3671         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3672 #endif
3673         new_state(PORT_STATE_OUT_DISCONNECT);
3674 }
3675
3676 /* MESSAGE_RELEASE */
3677 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
3678 {
3679 #ifdef SOCKET_MISDN
3680         l3_msg *l3m;
3681 #else
3682         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3683         msg_t *dmsg;
3684         RELEASE_t *release;
3685         RELEASE_COMPLETE_t *release_complete;
3686         DISCONNECT_t *disconnect;
3687 #endif
3688         class Endpoint *epoint;
3689         char *p = NULL;
3690
3691         /*
3692          * we may only release during incoming disconnect state.
3693          * this means that the endpoint doesnt require audio anymore
3694          */
3695         if (p_state == PORT_STATE_IN_DISCONNECT
3696          || p_state == PORT_STATE_OUT_DISCONNECT)
3697         {
3698                 /* sending release */
3699 #ifdef SOCKET_MISDN
3700                 l3m = create_l3msg();
3701 #else
3702                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
3703                 release = (RELEASE_t *)(dmsg->data + headerlen);
3704 #endif
3705                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
3706                 /* send cause */
3707 #ifdef SOCKET_MISDN
3708                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3709 #else
3710                 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);
3711 #endif
3712                 end_trace();
3713 #ifdef SOCKET_MISDN
3714                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
3715 #else
3716                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3717 #endif
3718                 new_state(PORT_STATE_RELEASE);
3719                 /* remove epoint */
3720                 free_epointid(epoint_id);
3721                 // wait for callref to be released
3722                 return;
3723
3724         }
3725         /*
3726          * if we are on incoming call setup, we may reject by sending a release_complete
3727          * also on outgoing call setup, we send a release complete, BUT this is not conform. (i don't know any other way)
3728          */
3729         if (p_state==PORT_STATE_IN_SETUP
3730          || p_state==PORT_STATE_OUT_SETUP)
3731 // // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
3732 //       || p_state==PORT_STATE_OUT_DISCO)
3733         {
3734 //#warning remove me
3735 //PDEBUG(DEBUG_LOG, "JOLLY sending release complete %d\n", p_serial);
3736                 /* sending release complete */
3737 #ifdef SOCKET_MISDN
3738                 l3m = create_l3msg();
3739 #else
3740                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
3741                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
3742 #endif
3743                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
3744                 /* send cause */
3745 #ifdef SOCKET_MISDN
3746                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3747 #else
3748                 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);
3749 #endif
3750                 end_trace();
3751 #ifdef SOCKET_MISDN
3752                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
3753 #else
3754                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3755 #endif
3756                 new_state(PORT_STATE_RELEASE);
3757                 /* remove epoint */
3758                 free_epointid(epoint_id);
3759                 // wait for callref to be released
3760                 return;
3761         }
3762
3763 #if 0
3764 wirklich erst proceeding?:
3765         /* NT-MODE in setup state we must send PROCEEDING first */
3766         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
3767         {
3768                 CALL_PROCEEDING_t *proceeding;
3769
3770                 /* sending proceeding */
3771 #ifdef SOCKET_MISDN
3772                 l3m = create_l3msg();
3773 #else
3774                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3775                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3776 #endif
3777                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3778                 /* channel information */
3779 #ifdef SOCKET_MISDN
3780                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3781 #else
3782                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3783 #endif
3784                 /* progress information */
3785                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3786                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
3787                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3788 #ifdef SOCKET_MISDN
3789                         enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3790 #else
3791                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3792 #endif
3793                 end_trace();
3794 #ifdef SOCKET_MISDN
3795                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3796 #else
3797                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3798 #endif
3799         }
3800 #endif
3801
3802         /* sending disconnect */
3803 #ifdef SOCKET_MISDN
3804         l3m = create_l3msg();
3805 #else
3806         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
3807         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
3808 #endif
3809         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
3810         /* progress information */
3811         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3812          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3813          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3814         if (p_m_mISDNport->tones)
3815 #ifdef SOCKET_MISDN
3816                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3817 #else
3818                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3819 #endif
3820         /* send cause */
3821 #ifdef SOCKET_MISDN
3822         enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3823 #else
3824         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);
3825 #endif
3826         /* send display */
3827         epoint = find_epoint_id(epoint_id);
3828         if (param->disconnectinfo.display[0])
3829                 p = param->disconnectinfo.display;
3830         if (p) if (*p && p_m_d_ntmode)
3831 #ifdef SOCKET_MISDN
3832                 enc_ie_display(l3m, (unsigned char *)p);
3833 #else
3834                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
3835 #endif
3836         end_trace();
3837 #ifdef SOCKET_MISDN
3838         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
3839 #else
3840         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3841 #endif
3842         new_state(PORT_STATE_OUT_DISCONNECT);
3843         /* remove epoint */
3844         free_epointid(epoint_id);
3845         // wait for release and callref to be released
3846 //#warning remove me
3847 //PDEBUG(DEBUG_LOG, "JOLLY sending disconnect %d\n", p_serial);
3848 }
3849
3850
3851 /*
3852  * endpoint sends messages to the port
3853  */
3854 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
3855 {
3856         struct lcr_msg *message;
3857
3858         if (PmISDN::message_epoint(epoint_id, message_id, param))
3859                 return(1);
3860
3861         switch(message_id)
3862         {
3863                 case MESSAGE_INFORMATION: /* overlap dialing */
3864                 if (p_type==PORT_TYPE_DSS1_NT_OUT
3865                  && p_state!=PORT_STATE_OUT_OVERLAP
3866                  && p_state!=PORT_STATE_CONNECT
3867                  && p_state!=PORT_STATE_OUT_DISCONNECT
3868                  && p_state!=PORT_STATE_IN_DISCONNECT)
3869                 {
3870                         break;
3871                 }
3872                 if (p_type==PORT_TYPE_DSS1_TE_OUT
3873                  && p_state!=PORT_STATE_OUT_OVERLAP
3874                  && p_state!=PORT_STATE_OUT_PROCEEDING
3875                  && p_state!=PORT_STATE_OUT_ALERTING
3876                  && p_state!=PORT_STATE_CONNECT
3877                  && p_state!=PORT_STATE_OUT_DISCONNECT
3878                  && p_state!=PORT_STATE_IN_DISCONNECT)
3879                 {
3880                         break;
3881                 }
3882                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
3883                  && p_state!=PORT_STATE_IN_OVERLAP
3884                  && p_state!=PORT_STATE_IN_PROCEEDING
3885                  && p_state!=PORT_STATE_IN_ALERTING
3886                  && p_state!=PORT_STATE_CONNECT
3887                  && p_state!=PORT_STATE_CONNECT_WAITING
3888                  && p_state!=PORT_STATE_OUT_DISCONNECT
3889                  && p_state!=PORT_STATE_IN_DISCONNECT)
3890                 {
3891                         break;
3892                 }
3893                 message_information(epoint_id, message_id, param);
3894                 break;
3895
3896                 case MESSAGE_SETUP: /* dial-out command received from epoint */
3897                 if (p_state!=PORT_STATE_IDLE
3898                  && p_state!=PORT_STATE_CONNECT)
3899                 {
3900                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
3901                         break;
3902                 }
3903                 if (p_epointlist && p_state==PORT_STATE_IDLE)
3904                         FATAL("Pdss1(%s): epoint pointer is set in idle state, how bad!!\n", p_name);
3905 #ifdef SOCKET_MISDN
3906                 message_setup(epoint_id, message_id, param);
3907 #else
3908                 /* note: pri is a special case, because links must be up for pri */ 
3909                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
3910                 {
3911                         /* LAYER 1 is up, or we may send */
3912                         message_setup(epoint_id, message_id, param);
3913                 } else {
3914                         iframe_t act;
3915                         /* LAYER 1 id down, so we queue */
3916                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
3917                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
3918                         /* attach us */
3919                         if (!(epointlist_new(epoint_id)))
3920                                 FATAL("No memory for epointlist\n");
3921                         /* activate link */
3922                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
3923                         act.prim = PH_ACTIVATE | REQUEST; 
3924                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
3925                         act.dinfo = 0;
3926                         act.len = 0;
3927                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
3928                         l1l2l3_trace_header(p_m_mISDNport, this, L1_ACTIVATE_REQ, DIRECTION_OUT);
3929                         end_trace();
3930 //                      /* set timeout */
3931 //                      p_m_mISDNport->l1timeout = now+3;
3932                 }
3933 #endif
3934                 break;
3935
3936                 case MESSAGE_NOTIFY: /* display and notifications */
3937                 message_notify(epoint_id, message_id, param);
3938                 break;
3939
3940                 case MESSAGE_FACILITY: /* facility message */
3941                 message_facility(epoint_id, message_id, param);
3942                 break;
3943
3944                 case MESSAGE_OVERLAP: /* more information is needed */
3945                 if (p_state!=PORT_STATE_IN_SETUP)
3946                 {
3947                         break;
3948                 }
3949                 message_overlap(epoint_id, message_id, param);
3950                 break;
3951
3952                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
3953                 if (p_state!=PORT_STATE_IN_SETUP
3954                  && p_state!=PORT_STATE_IN_OVERLAP)
3955                 {
3956                         break;
3957                 }
3958                 message_proceeding(epoint_id, message_id, param);
3959                 break;
3960
3961                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
3962                 if (p_state!=PORT_STATE_IN_SETUP
3963                  && p_state!=PORT_STATE_IN_OVERLAP
3964                  && p_state!=PORT_STATE_IN_PROCEEDING)
3965                 {
3966                         break;
3967                 }
3968                 message_alerting(epoint_id, message_id, param);
3969                 break;
3970
3971                 case MESSAGE_CONNECT: /* call of endpoint is connected */
3972                 if (p_state!=PORT_STATE_IN_SETUP
3973                  && p_state!=PORT_STATE_IN_OVERLAP
3974                  && p_state!=PORT_STATE_IN_PROCEEDING
3975                  && p_state!=PORT_STATE_IN_ALERTING
3976                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
3977                 {
3978                         break;
3979                 }
3980                 message_connect(epoint_id, message_id, param);
3981                 break;
3982
3983                 case MESSAGE_DISCONNECT: /* call has been disconnected */
3984                 if (p_state!=PORT_STATE_IN_SETUP
3985                  && p_state!=PORT_STATE_IN_OVERLAP
3986                  && p_state!=PORT_STATE_IN_PROCEEDING
3987                  && p_state!=PORT_STATE_IN_ALERTING
3988                  && p_state!=PORT_STATE_OUT_SETUP
3989                  && p_state!=PORT_STATE_OUT_OVERLAP
3990                  && p_state!=PORT_STATE_OUT_PROCEEDING
3991                  && p_state!=PORT_STATE_OUT_ALERTING
3992                  && p_state!=PORT_STATE_CONNECT
3993                  && p_state!=PORT_STATE_CONNECT_WAITING)
3994                 {
3995                         break;
3996                 }
3997                 message_disconnect(epoint_id, message_id, param);
3998                 break;
3999
4000                 case MESSAGE_RELEASE: /* release isdn port */
4001                 if (p_state==PORT_STATE_RELEASE)
4002                 {
4003                         break;
4004                 }
4005                 message_release(epoint_id, message_id, param);
4006                 break;
4007
4008                 default:
4009                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
4010         }
4011
4012         return(1);
4013 }
4014
4015
4016
4017 /*
4018  * data from isdn-stack (layer-3) to pbx (port class)
4019  */
4020 #ifdef SOCKET_MISDN
4021 int stack2manager(struct mISDNport *mISDNport, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
4022 {
4023         class Port *port;
4024         class Pdss1 *pdss1;
4025         char name[32];
4026
4027         PDEBUG(DEBUG_ISDN, "cmd(0x%x) pid(0x%x)\n", cmd, pid);
4028
4029         if (pid == 0)
4030         {
4031                 PDEBUG(DEBUG_ISDN, "ignoring dummy process from phone.\n");
4032                 return(0);
4033         }
4034
4035         /* find Port object of type ISDN */
4036         port = port_first;
4037         while(port)
4038         {
4039                 /* are we ISDN ? */
4040                 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
4041                 {
4042                         pdss1 = (class Pdss1 *)port;
4043                         /* check out correct stack and id */
4044                         if (pdss1->p_m_mISDNport == mISDNport)
4045                         {
4046                                 if (pdss1->p_m_d_l3id & MISDN_PID_CR_FLAG)
4047                                 {
4048                                         /* local callref, so match value only */
4049                                         if ((pdss1->p_m_d_l3id & MISDN_PID_CRVAL_MASK) == (pid & MISDN_PID_CRVAL_MASK))
4050                                                 break; // found
4051                                 } else
4052                                 {
4053                                         /* remote callref, ref + channel id */
4054                                         if (pdss1->p_m_d_l3id == pid)
4055                                                 break; // found
4056                                 }
4057                         }
4058                 }
4059                 port = port->next;
4060         }
4061
4062         /* aktueller prozess */
4063         if (port)
4064         {
4065                 if (cmd == MT_ASSIGN)
4066                 {
4067                         /* stack gives us new layer 3 id (during connect) */
4068                         l1l2l3_trace_header(mISDNport, pdss1, L3_NEW_L3ID_IND, DIRECTION_IN);
4069                         add_trace("callref", "old", "0x%x", pdss1->p_m_d_l3id);
4070                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
4071                         if ((pdss1->p_m_d_l3id&MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER)
4072                                 PERROR("    strange setup-procid 0x%x\n", pdss1->p_m_d_l3id);
4073                         pdss1->p_m_d_l3id = pid;
4074                         if (port->p_state == PORT_STATE_CONNECT)
4075                                 pdss1->p_m_d_ces = pid >> 16;
4076                         add_trace("callref", "new", "0x%x", pdss1->p_m_d_l3id);
4077                         end_trace();
4078                         return(0);
4079                 }
4080                 /* if process id is master process, but a child disconnects */
4081                 if (mISDNport->ntmode
4082                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
4083                  && (pdss1->p_m_d_l3id & MISDN_PID_CRTYPE_MASK) == MISDN_PID_MASTER)
4084                 {
4085                         if (cmd == MT_DISCONNECT
4086                          || cmd == MT_RELEASE)
4087                         {
4088                                 /* send special indication for child disconnect */
4089                                 pdss1->disconnect_ind_i(cmd, pid, l3m);
4090                                 return(0);
4091                         }
4092                         if (cmd == MT_RELEASE_COMPLETE)
4093                                 return(0);
4094                 }
4095                 /* if we have child pid and got different child pid message, ignore */
4096                 if (mISDNport->ntmode
4097                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
4098                  && (pdss1->p_m_d_l3id & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
4099                  && pid != pdss1->p_m_d_l3id)
4100                         return(0);
4101
4102                 /* process message */
4103                 pdss1->message_isdn(cmd, pid, l3m);
4104                 return(0);
4105         }
4106
4107         /* d-message */
4108         switch(cmd)
4109         {
4110                 case MT_SETUP:
4111                 /* creating port object */
4112                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4113                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4114
4115                         FATAL("Cannot create Port instance.\n");
4116                 pdss1->message_isdn(cmd, pid, l3m);
4117                 break;
4118
4119                 case MT_RESUME:
4120                 /* creating port object */
4121                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4122                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4123                         FATAL("Cannot create Port instance.\n");
4124                 pdss1->message_isdn(cmd, pid, l3m);
4125                 break;
4126
4127                 case MT_FREE:
4128                 PDEBUG(DEBUG_ISDN, "unused call ref released (l3id=0x%x)\n", pid);
4129                 break;
4130
4131                 case MT_RELEASE_COMPLETE:
4132                 PERROR("must be ignored by stack, not sent to app\n");
4133                 break;
4134
4135                 case MT_FACILITY:
4136                 // facility als broadcast
4137                 break;
4138
4139                 default:
4140                 PERROR("unhandled message: cmd(0x%x) pid(0x%x)\n", cmd, pid);
4141                 port = port_first;
4142                 while(port)
4143                 {
4144                         if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4145                         {
4146                                 pdss1 = (class Pdss1 *)port;
4147                                 /* check out correct stack */
4148                                 if (pdss1->p_m_mISDNport == mISDNport)
4149                                 /* check out correct id */
4150                                 PERROR("unhandled message: pid=%x is not associated with port-dinfo=%x\n", pid, pdss1->p_m_d_l3id);
4151                         }
4152                         port = port->next;
4153                 }
4154                 return(-EINVAL);
4155         }
4156         return(0);
4157 }
4158 #else
4159 /* NOTE: nt mode use mISDNuser_head_t as header */
4160 int stack2manager_nt(void *dat, void *arg)
4161 {
4162         class Port *port;
4163         class Pdss1 *pdss1;
4164         manager_t *mgr = (manager_t *)dat;
4165         msg_t *msg = (msg_t *)arg;
4166         mISDNuser_head_t *hh;
4167         struct mISDNport *mISDNport;
4168         char name[32];
4169
4170         if (!msg || !mgr)
4171                 return(-EINVAL);
4172
4173         /* note: nst is the first data feld of mISDNport */
4174         mISDNport = (struct mISDNport *)mgr->nst;
4175
4176         hh = (mISDNuser_head_t *)msg->data;
4177         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
4178
4179         /* find Port object of type ISDN */
4180         port = port_first;
4181         while(port)
4182         {
4183                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4184                 {
4185                         pdss1 = (class Pdss1 *)port;
4186 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
4187                         /* check out correct stack */
4188                         if (pdss1->p_m_mISDNport == mISDNport)
4189                         /* check out correct id */
4190                         if ((pdss1->p_m_d_l3id&0x0000ff00) != 0x000ff00)
4191                         {
4192                                 /* a single process */
4193                                 if (hh->dinfo == pdss1->p_m_d_l3id)
4194                                 {
4195                                         /* found port, the message belongs to */
4196                                         break;
4197                                 }
4198                         } else
4199                         {
4200                                 /* a broadcast process */
4201                                 if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
4202                                 {
4203                                         /* found port, the message belongs to */
4204                                         break;
4205                                 }
4206                         }
4207                 }
4208                 port = port->next;
4209         }
4210         if (port)
4211         {
4212 //printf("%x %x\n", hh->dinfo, pdss1->p_m_d_l3id);
4213                 /* if process id is master process, but a child disconnects */
4214                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00)
4215                 {
4216                         if (hh->prim == (CC_DISCONNECT|INDICATION))
4217                         {
4218                                 /* send special indication for child disconnect */
4219                                 pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
4220                                 free_msg(msg);
4221                                 return(0);
4222                         }
4223                         // ignoring other messages from child processes
4224                         free_msg(msg);
4225                         return(0);
4226                 }
4227                 /* if process id and layer 3 id matches */
4228                 if (hh->dinfo == pdss1->p_m_d_l3id)
4229                 {
4230                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
4231                         free_msg(msg);
4232                         return(0);
4233                 }
4234         }
4235
4236         /* d-message */
4237         switch(hh->prim)
4238         {
4239                 case MGR_SHORTSTATUS | INDICATION:
4240                 case MGR_SHORTSTATUS | CONFIRM:
4241                 switch(hh->dinfo) {
4242                         case SSTATUS_L2_ESTABLISHED:
4243                         goto ss_estab;
4244                         case SSTATUS_L2_RELEASED:
4245                         goto ss_rel;
4246                 }
4247                 break;
4248
4249                 case DL_ESTABLISH | INDICATION:
4250                 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_IND, DIRECTION_IN);
4251                 goto ss_estab;
4252                 case DL_ESTABLISH | CONFIRM:
4253                 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_CON, DIRECTION_IN);
4254                 ss_estab:
4255                 add_trace("tei", NULL, "%d", hh->dinfo);
4256                 end_trace();
4257                 if (mISDNport->ptp && hh->dinfo == 0)
4258                 {
4259                         if (mISDNport->l2establish)
4260                         {
4261                                 mISDNport->l2establish = 0;
4262                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
4263                         }
4264                         mISDNport->l2link = 1;
4265                         if (mISDNport->pri);
4266                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
4267                 }
4268                 break;
4269
4270                 case DL_RELEASE | INDICATION:
4271                 l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_IND, DIRECTION_IN);
4272                 goto ss_rel;
4273                 case DL_RELEASE | CONFIRM:
4274                 l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_CON, DIRECTION_IN);
4275                 ss_rel:
4276                 add_trace("tei", NULL, "%d", hh->dinfo);
4277                 end_trace();
4278                 if (mISDNport->ptp && hh->dinfo == 0)
4279                 {
4280                         mISDNport->l2link = 0;
4281                         time(&mISDNport->l2establish);
4282                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
4283                 }
4284                 break;
4285
4286                 case CC_SETUP | INDICATION:
4287                 /* creating port object */
4288                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4289                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4290
4291                         FATAL("Cannot create Port instance.\n");
4292                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
4293                 break;
4294
4295                 case CC_RESUME | INDICATION:
4296                 /* creating port object */
4297                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4298                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4299                         FATAL("Cannot create Port instance.\n");
4300                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
4301                 break;
4302
4303                 case CC_RELEASE_CR | INDICATION:
4304                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", hh->dinfo);
4305                 break;
4306
4307                 case CC_RELEASE_COMPLETE | INDICATION:
4308                 break;
4309
4310                 case CC_FACILITY | INDICATION:
4311                 break;
4312
4313                 default:
4314                 PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
4315                 port = port_first;
4316                 while(port)
4317                 {
4318                         if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4319                         {
4320                                 pdss1 = (class Pdss1 *)port;
4321         //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
4322                                 /* check out correct stack */
4323                                 if (pdss1->p_m_mISDNport == mISDNport)
4324                                 /* check out correct id */
4325                                 PERROR("unhandled message: dinfo=%x is not associated with port-dinfo=%x\n",hh->dinfo,pdss1->p_m_d_l3id);
4326                         }
4327                         port = port->next;
4328                 }
4329                 return(-EINVAL);
4330         }
4331         free_msg(msg);
4332         return(0);
4333 }
4334
4335 /* NOTE: te mode use iframe_t as header */
4336 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
4337 {
4338         class Port *port;
4339         class Pdss1 *pdss1;
4340         iframe_t *frm;
4341         char name[32];
4342
4343         if (!msg || !mISDNport)
4344                 return(-EINVAL);
4345         frm = (iframe_t *)msg->data;
4346         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
4347
4348         /* find Port object of type ISDN */
4349         port = port_first;
4350         while(port)
4351         {
4352                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
4353                 {
4354                         pdss1 = (class Pdss1 *)port;
4355                         /* check out correct stack */
4356                         if (pdss1->p_m_mISDNport == mISDNport)
4357                         /* check out correct id */
4358                         if (frm->dinfo == pdss1->p_m_d_l3id)
4359                         {
4360                                 /* found port, the message belongs to */
4361                                 break;
4362                         }
4363                 }
4364                 port = port->next;
4365         }
4366         if (port)
4367         {
4368                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
4369                 free_msg(msg);
4370                 return(0);
4371         }
4372
4373         /* process new cr (before setup indication) */
4374 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
4375         if (frm->prim == (CC_NEW_CR | INDICATION))
4376         {
4377
4378                 /* creating port object */
4379                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4380                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_TE_IN, mISDNport, name, NULL, 0, 0)))
4381                         FATAL("Cannot create Port instance.\n");
4382                 /* l3id will be set from dinfo at message_isdn */
4383                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
4384                 free_msg(msg);
4385                 return(0);
4386         }
4387
4388         if (frm->prim == (CC_RELEASE_CR | INDICATION))
4389         {
4390                 PDEBUG(DEBUG_ISDN, "unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
4391                 free_msg(msg);
4392                 return(0);
4393         }
4394         PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
4395         return(-EINVAL);
4396 }
4397
4398 #endif // stacktomanager
4399
4400
4401 #ifndef SOCKET_MISDN
4402 /*
4403  * sending message that were queued during L1 activation
4404  * or releasing port if link is down
4405  */
4406 void setup_queue(struct mISDNport *mISDNport, int link)
4407 {
4408         class Port *port;
4409         class Pdss1 *pdss1;
4410         struct lcr_msg *message;
4411
4412         if (!mISDNport->ntmode)
4413                 return;
4414
4415         /* check all port objects for pending message */
4416         port = port_first;
4417         while(port)
4418         {
4419                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
4420                 {
4421                         pdss1 = (class Pdss1 *)port;
4422                         if (pdss1->p_m_mISDNport == mISDNport)
4423                         {
4424                                 if (pdss1->p_m_d_queue)
4425                                 {
4426                                         if (link)
4427                                         {
4428                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
4429                                                 /* LAYER 1 is up, so we send */
4430                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
4431                                                 message_free(pdss1->p_m_d_queue);
4432                                                 pdss1->p_m_d_queue = NULL;
4433                                         } else
4434                                         {
4435                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
4436                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
4437                                                 message->param.disconnectinfo.cause = 27;
4438                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
4439                                                 message_put(message);
4440                                                 pdss1->new_state(PORT_STATE_RELEASE);
4441                                                 pdss1->p_m_delete = 1;
4442                                         }
4443                                 }
4444                         }
4445                 }
4446                 port = port->next;
4447         }
4448 }
4449
4450 #endif
4451
4452
4453