Merge branch 'master' of ssh://jolly@www.mISDN.org/var/git/lcr
[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 != pid)
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         if (p_m_d_ntmode)
1184 #ifdef SOCKET_MISDN
1185                 p_m_d_ces = pid >> 16;
1186 #else
1187                 p_m_d_ces = connect->ces;
1188 #endif
1189
1190         l1l2l3_trace_header(p_m_mISDNport, this, L3_CONNECT_IND, DIRECTION_IN);
1191 #ifdef SOCKET_MISDN
1192         dec_ie_channel_id(l3m, &exclusive, &channel);
1193         dec_ie_connected_pn(l3m, &type, &plan, &present, &screen, (unsigned char *)p_connectinfo.id, sizeof(p_connectinfo.id));
1194         /* te-mode: CONP (connected name identification presentation) */
1195         if (!p_m_d_ntmode)
1196                 dec_facility_centrex(l3m, (unsigned char *)p_connectinfo.name, sizeof(p_connectinfo.name));
1197 #else
1198         dec_ie_channel_id(connect->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1199         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));
1200         /* te-mode: CONP (connected name identification presentation) */
1201         if (!p_m_d_ntmode)
1202                 dec_facility_centrex(connect->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)p_connectinfo.name, sizeof(p_connectinfo.name));
1203 #endif
1204         end_trace();
1205
1206         /* select channel */
1207         bchannel_before = p_m_b_channel;
1208 #ifdef SOCKET_MISDN
1209         ret = received_first_reply_to_setup(cmd, channel, exclusive);
1210 #else
1211         ret = received_first_reply_to_setup(prim, channel, exclusive);
1212 #endif
1213         if (ret < 0)
1214         {
1215                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1216                 message->param.disconnectinfo.cause = -ret;
1217                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1218                 message_put(message);
1219                 new_state(PORT_STATE_RELEASE);
1220                 p_m_delete = 1;
1221                 return;
1222         }
1223
1224         /* connect information */
1225         switch (present)
1226         {
1227                 case 1:
1228                 p_connectinfo.present = INFO_PRESENT_RESTRICTED;
1229                 break;
1230                 case 2:
1231                 p_connectinfo.present = INFO_PRESENT_NOTAVAIL;
1232                 break;
1233                 default:
1234                 p_connectinfo.present = INFO_PRESENT_ALLOWED;
1235                 break;
1236         }
1237         switch (screen)
1238         {
1239                 case 0:
1240                 p_connectinfo.screen = INFO_SCREEN_USER;
1241                 break;
1242                 default:
1243                 p_connectinfo.screen = INFO_SCREEN_NETWORK;
1244                 break;
1245         }
1246         switch (type)
1247         {
1248                 case 0x0:
1249                 p_connectinfo.present = INFO_PRESENT_NULL; /* no COLP info */
1250                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1251                 break;
1252                 case 0x1:
1253                 p_connectinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1254                 break;
1255                 case 0x2:
1256                 p_connectinfo.ntype = INFO_NTYPE_NATIONAL;
1257                 break;
1258                 case 0x4:
1259                 p_connectinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1260                 break;
1261                 default:
1262                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1263                 break;
1264         }
1265         p_connectinfo.isdn_port = p_m_portnum;
1266         SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
1267
1268         /* only in nt-mode we send connect ack. in te-mode it is done by stack itself or optional */
1269         if (p_m_d_ntmode)
1270         {
1271                 /* send connect acknowledge */
1272 #ifdef SOCKET_MISDN
1273                 l3m = create_l3msg();
1274 #else
1275                 dmsg = create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT, dinfo, sizeof(CONNECT_ACKNOWLEDGE_t), p_m_d_ntmode);
1276                 connect_acknowledge = (CONNECT_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1277 #endif
1278                 l1l2l3_trace_header(p_m_mISDNport, this, L3_CONNECT_RES, DIRECTION_OUT);
1279                 /* if we had no bchannel before, we send it now */
1280                 if (!bchannel_before && p_m_b_channel)
1281 #ifdef SOCKET_MISDN
1282                         enc_ie_channel_id(l3m, 1, p_m_b_channel);
1283 #else
1284                         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1285 #endif
1286                 end_trace();
1287 #ifdef SOCKET_MISDN
1288                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CONNECT, p_m_d_l3id, l3m);
1289 #else
1290                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1291 #endif
1292         }
1293         
1294         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1295         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
1296         message_put(message);
1297
1298         new_state(PORT_STATE_CONNECT);
1299 }
1300
1301 /* CC_DISCONNECT INDICATION */
1302 #ifdef SOCKET_MISDN
1303 void Pdss1::disconnect_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1304 {
1305 #else
1306 void Pdss1::disconnect_ind(unsigned long prim, unsigned long dinfo, void *data)
1307 {
1308         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1309         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1310 #endif
1311         int location, cause;
1312         int coding, proglocation, progress;
1313         struct lcr_msg *message;
1314
1315         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_IND, DIRECTION_IN);
1316 #ifdef SOCKET_MISDN
1317         dec_ie_progress(l3m, &coding, &proglocation, &progress);
1318         dec_ie_cause(l3m, &location, &cause);
1319 #else
1320         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &proglocation, &progress);
1321         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1322 #endif
1323         end_trace();
1324         if (location == LOCATION_PRIVATE_LOCAL)
1325                 location = LOCATION_PRIVATE_REMOTE;
1326
1327         if (cause < 0)
1328                 cause = 16;
1329
1330         /* release if remote sends us no tones */
1331         if (!p_m_mISDNport->earlyb)
1332         {
1333 #ifdef SOCKET_MISDN
1334                 l3_msg *l3m;
1335 #else
1336                 msg_t *dmsg;
1337                 RELEASE_t *release;
1338 #endif
1339
1340 #ifdef SOCKET_MISDN
1341                 l3m = create_l3msg();
1342 #else
1343                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, dinfo, sizeof(RELEASE_t), p_m_d_ntmode);
1344                 release = (RELEASE_t *)(dmsg->data + headerlen);
1345 #endif
1346                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
1347 #ifdef SOCKET_MISDN
1348                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16); /* normal */
1349 #else
1350                 enc_ie_cause(&release->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16); /* normal */
1351 #endif
1352                 add_trace("reason", NULL, "no remote patterns");
1353                 end_trace();
1354 #ifdef SOCKET_MISDN
1355                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
1356 #else
1357                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1358 #endif
1359
1360                 /* sending release to endpoint */
1361                 while(p_epointlist)
1362                 {
1363                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1364                         message->param.disconnectinfo.cause = cause;
1365                         message->param.disconnectinfo.location = location;
1366                         message_put(message);
1367                         /* remove epoint */
1368                         free_epointlist(p_epointlist);
1369                 }
1370                 new_state(PORT_STATE_RELEASE);
1371                 p_m_delete = 1;
1372                 return;
1373         }
1374
1375         /* sending disconnect to active endpoint and release to inactive endpoints */
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         if (location == LOCATION_PRIVATE_LOCAL)
1452                 location = LOCATION_PRIVATE_REMOTE;
1453
1454         if (cause < 0)
1455                 cause = 16;
1456
1457         /* sending release to endpoint */
1458         while(p_epointlist)
1459         {
1460                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1461                 message->param.disconnectinfo.cause = cause;
1462                 message->param.disconnectinfo.location = location;
1463                 message_put(message);
1464                 /* remove epoint */
1465                 free_epointlist(p_epointlist);
1466         }
1467
1468         /* only in NT mode we must send release_complete, if we got a release confirm */
1469 #ifdef SOCKET_MISDN
1470         if (cmd == MT_RELEASE)
1471 #else
1472         if (prim == (CC_RELEASE | CONFIRM))
1473 #endif
1474         {
1475                 /* sending release complete */
1476 #ifdef SOCKET_MISDN
1477                 l3m = create_l3msg();
1478 #else
1479                 RELEASE_COMPLETE_t *release_complete;
1480                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
1481                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
1482 #endif
1483                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
1484 #ifdef SOCKET_MISDN
1485                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16);
1486 #else
1487                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16);
1488 #endif
1489                 end_trace();
1490 #ifdef SOCKET_MISDN
1491                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
1492 #else
1493                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1494 #endif
1495         }
1496
1497         new_state(PORT_STATE_RELEASE);
1498         p_m_delete = 1;
1499 }
1500
1501 /* CC_RELEASE_COMPLETE INDICATION (a reject) */
1502 #ifdef SOCKET_MISDN
1503 void Pdss1::release_complete_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1504 {
1505 #else
1506 void Pdss1::release_complete_ind(unsigned long prim, unsigned long dinfo, void *data)
1507 {
1508         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1509         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *)((unsigned long)data + headerlen);
1510 #endif
1511         int location, cause;
1512         struct lcr_msg *message;
1513
1514         l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_IND, DIRECTION_IN);
1515 #ifdef SOCKET_MISDN
1516         dec_ie_cause(l3m, &location, &cause);
1517 #else
1518         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1519 #endif
1520         end_trace();
1521         if (location == LOCATION_PRIVATE_LOCAL)
1522                 location = LOCATION_PRIVATE_REMOTE;
1523
1524         if (cause < 0)
1525                 cause = 16;
1526
1527         /* sending release to endpoint */
1528         while(p_epointlist)
1529         {
1530                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1531                 message->param.disconnectinfo.cause = cause;
1532                 message->param.disconnectinfo.location = location;
1533                 message_put(message);
1534                 /* remove epoint */
1535                 free_epointlist(p_epointlist);
1536         }
1537
1538         new_state(PORT_STATE_RELEASE);
1539         p_m_delete = 1;
1540 }
1541
1542 /* T312 timeout  */
1543 #ifdef SOCKET_MISDN
1544 void Pdss1::t312_timeout_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1545 {
1546 #else
1547 void Pdss1::t312_timeout_ind(unsigned long prim, unsigned long dinfo, void *data)
1548 {
1549 #endif
1550         struct lcr_msg *message;
1551
1552         // trace is done at message_isdn()
1553         
1554         /* sending release to endpoint */
1555         while(p_epointlist)
1556         {
1557                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1558                 if (p_m_d_collect_cause)
1559                 {
1560                         message->param.disconnectinfo.cause = p_m_d_collect_cause;
1561                         message->param.disconnectinfo.location = p_m_d_collect_location;
1562                 } else
1563                 {
1564                         message->param.disconnectinfo.cause = CAUSE_NOUSER;
1565                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1566                 }
1567                 message_put(message);
1568                 /* remove epoint */
1569                 free_epointlist(p_epointlist);
1570         }
1571
1572         new_state(PORT_STATE_RELEASE);
1573         p_m_delete = 1;
1574 }
1575
1576 /* CC_NOTIFY INDICATION */
1577 #ifdef SOCKET_MISDN
1578 void Pdss1::notify_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1579 {
1580 #else
1581 void Pdss1::notify_ind(unsigned long prim, unsigned long dinfo, void *data)
1582 {
1583         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1584         NOTIFY_t *notifying = (NOTIFY_t *)((unsigned long)data + headerlen);
1585 #endif
1586         struct lcr_msg *message;
1587         int notify, type, plan, present;
1588         unsigned char notifyid[sizeof(message->param.notifyinfo.id)];
1589
1590         l1l2l3_trace_header(p_m_mISDNport, this, L3_NOTIFY_IND, DIRECTION_IN);
1591 #ifdef SOCKET_MISDN
1592         dec_ie_notify(l3m, &notify);
1593         dec_ie_redir_dn(l3m, &type, &plan, &present, notifyid, sizeof(notifyid));
1594 #else
1595         dec_ie_notify(notifying->NOTIFY, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
1596         dec_ie_redir_dn(notifying->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, notifyid, sizeof(notifyid));
1597 #endif
1598         end_trace();
1599
1600         if (!ACTIVE_EPOINT(p_epointlist))
1601                 return;
1602         /* notification indicator */
1603         if (notify < 0)
1604                 return;
1605         notify |= 0x80;
1606         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1607         message->param.notifyinfo.notify = notify;
1608         SCPY(message->param.notifyinfo.id, (char *)notifyid);
1609         /* redirection number */
1610         switch (present)
1611         {
1612                 case 1:
1613                 message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1614                 break;
1615                 case 2:
1616                 message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1617                 break;
1618                 default:
1619                 message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1620                 break;
1621         }
1622         switch (type)
1623         {
1624                 case -1:
1625                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1626                 message->param.notifyinfo.present = INFO_PRESENT_NULL;
1627                 break;
1628                 case 1:
1629                 message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1630                 break;
1631                 case 2:
1632                 message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1633                 break;
1634                 case 4:
1635                 message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1636                 break;
1637                 default:
1638                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1639                 break;
1640         }
1641         message->param.notifyinfo.isdn_port = p_m_portnum;
1642         message_put(message);
1643 }
1644
1645
1646 /* CC_HOLD INDICATION */
1647         struct lcr_msg *message;
1648 #ifdef SOCKET_MISDN
1649 void Pdss1::hold_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1650 {
1651 #else
1652 void Pdss1::hold_ind(unsigned long prim, unsigned long dinfo, void *data)
1653 {
1654         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1655         msg_t *dmsg;
1656 //      HOLD_t *hold = (HOLD_t *)((unsigned long)data + headerlen);
1657         HOLD_REJECT_t *hold_reject;
1658         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
1659 #endif
1660 //      class Endpoint *epoint;
1661
1662         l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_IND, DIRECTION_IN);
1663         end_trace();
1664
1665         if (!ACTIVE_EPOINT(p_epointlist) || p_m_hold)
1666         {
1667 #ifdef SOCKET_MISDN
1668                 l3m = create_l3msg();
1669 #else
1670                 dmsg = create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT, dinfo, sizeof(HOLD_REJECT_t), p_m_d_ntmode);
1671                 hold_reject = (HOLD_REJECT_t *)(dmsg->data + headerlen);
1672 #endif
1673                 l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_REJECT_REQ, DIRECTION_OUT);
1674 #ifdef SOCKET_MISDN
1675                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, p_m_hold?101:31); /* normal unspecified / incompatible state */
1676 #else
1677                 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 */
1678 #endif
1679                 add_trace("reason", NULL, "no endpoint");
1680                 end_trace();
1681 #ifdef SOCKET_MISDN
1682                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_HOLD_REJECT, p_m_d_l3id, l3m);
1683 #else
1684                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1685 #endif
1686
1687                 return;
1688         }
1689
1690         /* notify the hold of call */
1691         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1692         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1693         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1694         message_put(message);
1695
1696         /* deactivate bchannel */
1697         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (hold)", DIRECTION_NONE);
1698         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1699         end_trace();
1700         drop_bchannel();
1701
1702         /* set hold state */
1703         p_m_hold = 1;
1704 #if 0
1705         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1706         if (epoint && p_m_d_ntmode)
1707         {
1708                 p_m_timeout = p_settings.tout_hold;
1709                 time(&p_m_timer);
1710         }
1711 #endif
1712
1713         /* acknowledge hold */
1714 #ifdef SOCKET_MISDN
1715         l3m = create_l3msg();
1716 #else
1717         dmsg = create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE, dinfo, sizeof(HOLD_ACKNOWLEDGE_t), p_m_d_ntmode);
1718         hold_acknowledge = (HOLD_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1719 #endif
1720         l1l2l3_trace_header(p_m_mISDNport, this, L3_HOLD_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1721         end_trace();
1722 #ifdef SOCKET_MISDN
1723         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_HOLD_ACKNOWLEDGE, p_m_d_l3id, l3m);
1724 #else
1725         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1726 #endif
1727 }
1728
1729
1730 /* CC_RETRIEVE INDICATION */
1731 #ifdef SOCKET_MISDN
1732 void Pdss1::retrieve_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1733 {
1734 #else
1735 void Pdss1::retrieve_ind(unsigned long prim, unsigned long dinfo, void *data)
1736 {
1737         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1738         msg_t *dmsg;
1739         RETRIEVE_t *retrieve = (RETRIEVE_t *)((unsigned long)data + headerlen);
1740         RETRIEVE_REJECT_t *retrieve_reject;
1741         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1742 #endif
1743         struct lcr_msg *message;
1744         int channel, exclusive, cause;
1745         int ret;
1746
1747         l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_IND, DIRECTION_IN);
1748 #ifdef SOCKET_MISDN
1749         dec_ie_channel_id(l3m, &exclusive, &channel);
1750 #else
1751         dec_ie_channel_id(retrieve->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1752 #endif
1753         end_trace();
1754
1755         if (!p_m_hold)
1756         {
1757                 cause = 101; /* incompatible state */
1758                 reject:
1759
1760 #ifdef SOCKET_MISDN
1761                 l3m = create_l3msg();
1762 #else
1763                 dmsg = create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT, dinfo, sizeof(RETRIEVE_REJECT_t), p_m_d_ntmode);
1764                 retrieve_reject = (RETRIEVE_REJECT_t *)(dmsg->data + headerlen);
1765 #endif
1766                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_REJECT_REQ, DIRECTION_OUT);
1767 #ifdef SOCKET_MISDN
1768                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1769 #else
1770                 enc_ie_cause(&retrieve_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1771 #endif
1772                 end_trace();
1773 #ifdef SOCKET_MISDN
1774                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RETRIEVE_REJECT, p_m_d_l3id, l3m);
1775 #else
1776                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1777 #endif
1778
1779                 return;
1780         }
1781
1782         /* notify the retrieve of call */
1783         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1784         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1785         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1786         message_put(message);
1787
1788         /* hunt channel */
1789         ret = channel = hunt_bchannel(channel, exclusive);
1790         if (ret < 0)
1791                 goto no_channel;
1792
1793         /* open channel */
1794         ret = seize_bchannel(channel, 1);
1795         if (ret < 0)
1796         {
1797                 no_channel:
1798                 cause = -ret;
1799                 goto reject;
1800         }
1801         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1802
1803         /* set hold state */
1804         p_m_hold = 0;
1805         p_m_timeout = 0;
1806
1807         /* acknowledge retrieve */
1808 #ifdef SOCKET_MISDN
1809         l3m = create_l3msg();
1810 #else
1811         dmsg = create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE, dinfo, sizeof(RETRIEVE_ACKNOWLEDGE_t), p_m_d_ntmode);
1812         retrieve_acknowledge = (RETRIEVE_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1813 #endif
1814         l1l2l3_trace_header(p_m_mISDNport, this, L3_RETRIEVE_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1815 #ifdef SOCKET_MISDN
1816         enc_ie_channel_id(l3m, 1, p_m_b_channel);
1817 #else
1818         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1819 #endif
1820         end_trace();
1821 #ifdef SOCKET_MISDN
1822         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RETRIEVE_ACKNOWLEDGE, p_m_d_l3id, l3m);
1823 #else
1824         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1825 #endif
1826 }
1827
1828 /* CC_SUSPEND INDICATION */
1829 #ifdef SOCKET_MISDN
1830 void Pdss1::suspend_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1831 {
1832 #else
1833 void Pdss1::suspend_ind(unsigned long prim, unsigned long dinfo, void *data)
1834 {
1835         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1836         msg_t *dmsg;
1837         SUSPEND_t *suspend = (SUSPEND_t *)((unsigned long)data + headerlen);
1838         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
1839         SUSPEND_REJECT_t *suspend_reject;
1840 #endif
1841         struct lcr_msg *message;
1842         class Endpoint *epoint;
1843         unsigned char callid[8];
1844         int len;
1845         int ret = -31; /* normal, unspecified */
1846
1847         l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_IND, DIRECTION_IN);
1848 #ifdef SOCKET_MISDN
1849         dec_ie_call_id(l3m, callid, &len);
1850 #else
1851         dec_ie_call_id(suspend->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1852 #endif
1853         end_trace();
1854
1855         if (!ACTIVE_EPOINT(p_epointlist))
1856         {
1857                 reject:
1858 #ifdef SOCKET_MISDN
1859                 l3m = create_l3msg();
1860 #else
1861                 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, dinfo, sizeof(SUSPEND_REJECT_t), p_m_d_ntmode);
1862                 suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + headerlen);
1863 #endif
1864                 l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_REJECT_REQ, DIRECTION_OUT);
1865 #ifdef SOCKET_MISDN
1866                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1867 #else
1868                 enc_ie_cause(&suspend_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1869 #endif
1870                 end_trace();
1871 #ifdef SOCKET_MISDN
1872                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SUSPEND_REJECT, p_m_d_l3id, l3m);
1873 #else
1874                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1875 #endif
1876
1877                 return;
1878         }
1879
1880         /* call id */
1881         if (len<0) len = 0;
1882
1883         /* check if call id is in use */
1884         epoint = epoint_first;
1885         while(epoint)
1886         {
1887                 if (epoint->ep_park)
1888                 {
1889                         if (epoint->ep_park_len == len)
1890                         if (!memcmp(epoint->ep_park_callid, callid, len))
1891                         {
1892                                 ret = -84; /* call id in use */
1893                                 goto reject;
1894                         }
1895                 }
1896                 epoint = epoint->next;
1897         }
1898
1899         /* notify the hold of call */
1900         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1901         message->param.notifyinfo.notify = INFO_NOTIFY_USER_SUSPENDED;
1902         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1903         message_put(message);
1904
1905         /* deactivate bchannel */
1906         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (suspend)", DIRECTION_NONE);
1907         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1908         end_trace();
1909         drop_bchannel();
1910
1911         /* sending suspend to endpoint */
1912         while (p_epointlist)
1913         {
1914                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_SUSPEND);
1915                 memcpy(message->param.parkinfo.callid, callid, sizeof(message->param.parkinfo.callid));
1916                 message->param.parkinfo.len = len;
1917                 message_put(message);
1918                 /* remove epoint */
1919                 free_epointlist(p_epointlist);
1920         }
1921
1922         /* sending SUSPEND_ACKNOWLEDGE */
1923 #ifdef SOCKET_MISDN
1924         l3m = create_l3msg();
1925 #else
1926         dmsg = create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE, dinfo, sizeof(SUSPEND_ACKNOWLEDGE_t), p_m_d_ntmode);
1927         suspend_acknowledge = (SUSPEND_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1928 #endif
1929         l1l2l3_trace_header(p_m_mISDNport, this, L3_SUSPEND_ACKNOWLEDGE_REQ, DIRECTION_OUT);
1930         end_trace();
1931 #ifdef SOCKET_MISDN
1932         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_SUSPEND_ACKNOWLEDGE, p_m_d_l3id, l3m);
1933 #else
1934         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1935 #endif
1936
1937         new_state(PORT_STATE_RELEASE);
1938         p_m_delete = 1;
1939 }
1940
1941 /* CC_RESUME INDICATION */
1942 #ifdef SOCKET_MISDN
1943 void Pdss1::resume_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
1944 {
1945 #else
1946 void Pdss1::resume_ind(unsigned long prim, unsigned long dinfo, void *data)
1947 {
1948         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1949         msg_t *dmsg;
1950         RESUME_t *resume = (RESUME_t *)((unsigned long)data + headerlen);
1951         RESUME_REJECT_t *resume_reject;
1952         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
1953 #endif
1954         unsigned char callid[8];
1955         int len;
1956         int channel, exclusive;
1957         class Endpoint *epoint;
1958         struct lcr_msg *message;
1959         int ret;
1960
1961 #ifdef SOCKET_MISDN
1962         /* process given callref */
1963         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_IND, DIRECTION_IN);
1964         add_trace("callref", "new", "0x%x", pid);
1965         if (p_m_d_l3id != pid)
1966         {
1967                 /* release is case the ID is already in use */
1968                 add_trace("error", NULL, "callref already in use");
1969                 end_trace();
1970                 l3m = create_l3msg();
1971                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_REJECT_REQ, DIRECTION_OUT);
1972                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
1973                 add_trace("reason", NULL, "callref already in use");
1974                 end_trace();
1975                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_REJECT, pid, l3m);
1976                 new_state(PORT_STATE_RELEASE);
1977                 p_m_delete = 1;
1978                 return;
1979         }
1980         p_m_d_l3id = pid;
1981         p_m_d_ces = pid >> 16;
1982         end_trace();
1983 #else
1984         /* callref from nt-lib */
1985         if (p_m_d_ntmode)
1986         {
1987                 /* nt-library now gives us the id via CC_RESUME */
1988                 if (dinfo&(~0xff) == 0xff00)
1989                         FATAL("l3-stack gives us a process id 0xff00-0xffff\n");
1990                 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_IND, DIRECTION_IN);
1991                 if (p_m_d_l3id)
1992                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1993                 add_trace("callref", "new", "0x%x", dinfo);
1994                 end_trace();
1995                 if (p_m_d_l3id&(~0xff) == 0xff00)
1996                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1997                 p_m_d_l3id = dinfo;
1998                 p_m_d_ces = resume->ces;
1999         }
2000 #endif
2001
2002         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_IND, DIRECTION_IN);
2003 #ifdef SOCKET_MISDN
2004         dec_ie_call_id(l3m, callid, &len);
2005 #else
2006         dec_ie_call_id(resume->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
2007 #endif
2008         end_trace();
2009
2010         /* if blocked, release call */
2011         if (p_m_mISDNport->ifport->block)
2012         {
2013                 ret = -27;
2014                 goto reject;
2015         }
2016
2017         /* call id */
2018         if (len<0) len = 0;
2019
2020         /* channel_id (no channel is possible in message) */
2021         exclusive = 0;
2022         channel = -1; /* any channel */
2023
2024         /* hunt channel */
2025         ret = channel = hunt_bchannel(channel, exclusive);
2026         if (ret < 0)
2027                 goto no_channel;
2028
2029         /* open channel */
2030         ret = seize_bchannel(channel, 1);
2031         if (ret < 0)
2032         {
2033                 no_channel:
2034                 reject:
2035 #ifdef SOCKET_MISDN
2036                 l3m = create_l3msg();
2037 #else
2038                 dmsg = create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT, dinfo, sizeof(RESUME_REJECT_t), p_m_d_ntmode);
2039                 resume_reject = (RESUME_REJECT_t *)(dmsg->data + headerlen);
2040 #endif
2041                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_REJECT_REQ, DIRECTION_OUT);
2042 #ifdef SOCKET_MISDN
2043                 enc_ie_cause(l3m, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
2044 #else
2045                 enc_ie_cause(&resume_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
2046 #endif
2047                 if (ret == -27)
2048                         add_trace("reason", NULL, "port blocked");
2049                 end_trace();
2050 #ifdef SOCKET_MISDN
2051                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_REJECT, p_m_d_l3id, l3m);
2052 #else
2053                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2054 #endif
2055                 new_state(PORT_STATE_RELEASE);
2056                 p_m_delete = 1;
2057                 return;
2058         }
2059         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
2060
2061         /* create endpoint */
2062         if (p_epointlist)
2063                 FATAL("Incoming resume but already got an endpoint.\n");
2064         ret = -85; /* no call suspended */
2065         epoint = epoint_first;
2066         while(epoint)
2067         {
2068                 if (epoint->ep_park)
2069                 {
2070                         ret = -83; /* suspended call exists, but this not */
2071                         if (epoint->ep_park_len == len)
2072                         if (!memcmp(epoint->ep_park_callid, callid, len))
2073                                 break;
2074                 }
2075                 epoint = epoint->next;
2076         }
2077         if (!epoint)
2078                 goto reject;
2079
2080         epointlist_new(epoint->ep_serial);
2081         if (!(epoint->portlist_new(p_serial, p_type, p_m_mISDNport->earlyb)))
2082                 FATAL("No memory for portlist\n");
2083         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RESUME);
2084         message_put(message);
2085
2086         /* notify the resume of call */
2087         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
2088         message->param.notifyinfo.notify = INFO_NOTIFY_USER_RESUMED;
2089         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
2090         message_put(message);
2091
2092         /* sending RESUME_ACKNOWLEDGE */
2093 #ifdef SOCKET_MISDN
2094         l3m = create_l3msg();
2095 #else
2096         dmsg = create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE, dinfo, sizeof(RESUME_ACKNOWLEDGE_t), p_m_d_ntmode);
2097         resume_acknowledge = (RESUME_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2098 #endif
2099         l1l2l3_trace_header(p_m_mISDNport, this, L3_RESUME_ACKNOWLEDGE_REQ, DIRECTION_OUT);
2100 #ifdef SOCKET_MISDN
2101         enc_ie_channel_id(l3m, 1, p_m_b_channel);
2102 #else
2103         enc_ie_channel_id(&resume_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2104 #endif
2105         end_trace();
2106 #ifdef SOCKET_MISDN
2107         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RESUME_ACKNOWLEDGE, p_m_d_l3id, l3m);
2108 #else
2109         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2110 #endif
2111
2112         new_state(PORT_STATE_CONNECT);
2113 }
2114
2115
2116 /* CC_FACILITY INDICATION */
2117 #ifdef SOCKET_MISDN
2118 void Pdss1::facility_ind(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
2119 {
2120 #else
2121 void Pdss1::facility_ind(unsigned long prim, unsigned long dinfo, void *data)
2122 {
2123         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2124         FACILITY_t *facility = (FACILITY_t *)((unsigned long)data + headerlen);
2125 #endif
2126         unsigned char facil[256];
2127         int facil_len;
2128         struct lcr_msg *message;
2129
2130         l1l2l3_trace_header(p_m_mISDNport, this, L3_FACILITY_IND, DIRECTION_IN);
2131 #ifdef SOCKET_MISDN
2132         dec_ie_facility(l3m, facil, &facil_len);
2133 #else
2134         dec_ie_facility(facility->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), facil, &facil_len);
2135 #endif
2136         end_trace();
2137
2138         /* facility */
2139         if (facil_len<=0)
2140                 return;
2141
2142         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_FACILITY);
2143         message->param.facilityinfo.len = facil_len;
2144         memcpy(message->param.facilityinfo.data, facil, facil_len);
2145         message_put(message);
2146 }
2147
2148
2149 /*
2150  * handler for isdn connections
2151  * incoming information are parsed and sent via message to the endpoint
2152  */
2153 #ifdef SOCKET_MISDN
2154 void Pdss1::message_isdn(unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
2155 {
2156         int timer_hex=0;
2157
2158         switch (cmd)
2159         {
2160                 case MT_TIMEOUT:
2161                 if (!l3m->cause)
2162                 {
2163                         PERROR("Pdss1(%s) timeout without cause.\n", p_name);
2164                         break;
2165                 }
2166                 if (l3m->cause[1] != 5)
2167                 {
2168                         PERROR("Pdss1(%s) expecting timeout with timer diagnostic.\n", p_name);
2169                         break;
2170                 }
2171                 if (l3m->cause[4]=='3' && l3m->cause[5]=='1' && l3m->cause[6]=='2')
2172                 {
2173                         l1l2l3_trace_header(p_m_mISDNport, this, L3_TIMEOUT_IND, DIRECTION_IN);
2174                         add_trace("timer", NULL, "%x", timer_hex);
2175                         end_trace();
2176                         t312_timeout_ind(cmd, pid, l3m);
2177                 }
2178                 break;
2179
2180                 case MT_SETUP:
2181                 if (p_state != PORT_STATE_IDLE)
2182                         break;
2183                 setup_ind(cmd, pid, l3m);
2184                 break;
2185
2186                 case MT_INFORMATION:
2187                 information_ind(cmd, pid, l3m);
2188                 break;
2189
2190                 case MT_SETUP_ACKNOWLEDGE:
2191                 if (p_state != PORT_STATE_OUT_SETUP)
2192                 {
2193                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
2194                         break;
2195                 }
2196                 setup_acknowledge_ind(cmd, pid, l3m);
2197                 break;
2198
2199                 case MT_CALL_PROCEEDING:
2200                 if (p_state != PORT_STATE_OUT_SETUP
2201                  && p_state != PORT_STATE_OUT_OVERLAP)
2202                 {
2203                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
2204                         break;
2205                 }
2206                 proceeding_ind(cmd, pid, l3m);
2207                 break;
2208
2209                 case MT_ALERTING:
2210                 if (p_state != PORT_STATE_OUT_SETUP
2211                  && p_state != PORT_STATE_OUT_OVERLAP
2212                  && p_state != PORT_STATE_OUT_PROCEEDING)
2213                 {
2214                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
2215                         break;
2216                 }
2217                 alerting_ind(cmd, pid, l3m);
2218                 break;
2219
2220                 case MT_CONNECT:
2221                 if (p_state != PORT_STATE_OUT_SETUP
2222                  && p_state != PORT_STATE_OUT_OVERLAP
2223                  && p_state != PORT_STATE_OUT_PROCEEDING
2224                  && p_state != PORT_STATE_OUT_ALERTING)
2225                 {
2226                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
2227                         break;
2228                 }
2229                 connect_ind(cmd, pid, l3m);
2230                 if (p_m_d_notify_pending)
2231                 {
2232                         /* send pending notify message during connect */
2233                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2234                         message_free(p_m_d_notify_pending);
2235                         p_m_d_notify_pending = NULL;
2236                 }
2237                 break;
2238
2239                 case MT_CONNECT_ACKNOWLEDGE:
2240                 if (p_state == PORT_STATE_CONNECT_WAITING)
2241                         new_state(PORT_STATE_CONNECT);
2242                 if (p_m_d_notify_pending)
2243                 {
2244                         /* send pending notify message during connect-ack */
2245                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2246                         message_free(p_m_d_notify_pending);
2247                         p_m_d_notify_pending = NULL;
2248                 }
2249                 break;
2250
2251                 case MT_DISCONNECT:
2252                 disconnect_ind(cmd, pid, l3m);
2253                 break;
2254
2255                 case MT_RELEASE:
2256                 release_ind(cmd, pid, l3m);
2257                 break;
2258
2259                 case MT_RELEASE_COMPLETE:
2260                 release_complete_ind(cmd, pid, l3m);
2261                 break;
2262
2263                 case MT_NOTIFY:
2264                 notify_ind(cmd, pid, l3m);
2265                 break;
2266
2267                 case MT_HOLD:
2268                 hold_ind(cmd, pid, l3m);
2269                 break;
2270
2271                 case MT_RETRIEVE:
2272                 retrieve_ind(cmd, pid, l3m);
2273                 break;
2274
2275                 case MT_SUSPEND:
2276                 suspend_ind(cmd, pid, l3m);
2277                 break;
2278
2279                 case MT_RESUME:
2280                 resume_ind(cmd, pid, l3m);
2281                 break;
2282
2283                 case MT_FACILITY:
2284                 facility_ind(cmd, pid, l3m);
2285                 break;
2286
2287                 case MT_FREE:
2288                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_L3ID_IND, DIRECTION_IN);
2289                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
2290                 end_trace();
2291                 p_m_d_l3id = 0;
2292                 p_m_d_ces = -1;
2293                 p_m_delete = 1;
2294 //#warning remove me
2295 //PDEBUG(DEBUG_LOG, "JOLLY release cr %d\n", p_serial);
2296                 /* sending release to endpoint in case we still have an endpoint
2297                  * this is because we don't get any response if a release_complete is received (or a release in release state)
2298                  */
2299                 while(p_epointlist)
2300                 {
2301                         struct lcr_msg *message;
2302                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2303                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
2304                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
2305                         message_put(message);
2306                         /* remove epoint */
2307                         free_epointlist(p_epointlist);
2308
2309                         new_state(PORT_STATE_RELEASE);
2310                 }
2311                 break;
2312
2313                 default:
2314                 l1l2l3_trace_header(p_m_mISDNport, this, L3_UNKNOWN, DIRECTION_IN);
2315                 add_trace("unhandled", "cmd", "0x%x", cmd);
2316                 end_trace();
2317         }
2318 }
2319 #else
2320 void Pdss1::message_isdn(unsigned long prim, unsigned long dinfo, void *data)
2321 {
2322         int new_l3id;
2323         int timer_hex=0;
2324
2325         switch (prim)
2326         {
2327                 case CC_TIMEOUT | INDICATION:
2328                 if (p_m_d_ntmode)
2329                 {
2330                         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2331                         timer_hex = *((int *)(((char *)data)+headerlen));
2332                 }
2333                 if (timer_hex==0x312 && p_m_d_ntmode)
2334                 {
2335                         l1l2l3_trace_header(p_m_mISDNport, this, L3_TIMEOUT_IND, DIRECTION_IN);
2336                         add_trace("timer", NULL, "%x", timer_hex);
2337                         end_trace();
2338                         t312_timeout_ind(prim, dinfo, data);
2339                 }
2340                 break;
2341
2342                 case CC_SETUP | INDICATION:
2343                 if (p_state != PORT_STATE_IDLE)
2344                         break;
2345                 setup_ind(prim, dinfo, data);
2346                 break;
2347
2348                 case CC_SETUP | CONFIRM:
2349                 if (p_m_d_ntmode)
2350                 {
2351                         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_IND, DIRECTION_IN);
2352                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
2353                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
2354                         if ((p_m_d_l3id&0xff00) != 0xff00)
2355                                 PERROR("    strange setup-procid 0x%x\n", p_m_d_l3id);
2356                         p_m_d_l3id = *((int *)(((u_char *)data)+ mISDNUSER_HEAD_SIZE));
2357                         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2358                         end_trace();
2359                 }
2360                 break;
2361
2362                 case CC_INFORMATION | INDICATION:
2363                 information_ind(prim, dinfo, data);
2364                 break;
2365
2366                 case CC_SETUP_ACKNOWLEDGE | INDICATION:
2367                 if (p_state != PORT_STATE_OUT_SETUP)
2368                 {
2369                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
2370                         break;
2371                 }
2372                 setup_acknowledge_ind(prim, dinfo, data);
2373                 break;
2374
2375                 case CC_PROCEEDING | INDICATION:
2376                 if (p_state != PORT_STATE_OUT_SETUP
2377                  && p_state != PORT_STATE_OUT_OVERLAP)
2378                 {
2379                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
2380                         break;
2381                 }
2382                 proceeding_ind(prim, dinfo, data);
2383                 break;
2384
2385                 case CC_ALERTING | INDICATION:
2386                 if (p_state != PORT_STATE_OUT_SETUP
2387                  && p_state != PORT_STATE_OUT_OVERLAP
2388                  && p_state != PORT_STATE_OUT_PROCEEDING)
2389                 {
2390                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
2391                         break;
2392                 }
2393                 alerting_ind(prim, dinfo, data);
2394                 break;
2395
2396                 case CC_CONNECT | INDICATION:
2397                 if (p_state != PORT_STATE_OUT_SETUP
2398                  && p_state != PORT_STATE_OUT_OVERLAP
2399                  && p_state != PORT_STATE_OUT_PROCEEDING
2400                  && p_state != PORT_STATE_OUT_ALERTING)
2401                 {
2402                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
2403                         break;
2404                 }
2405                 connect_ind(prim, dinfo, data);
2406                 if (p_m_d_notify_pending)
2407                 {
2408                         /* send pending notify message during connect */
2409                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2410                         message_free(p_m_d_notify_pending);
2411                         p_m_d_notify_pending = NULL;
2412                 }
2413                 break;
2414
2415                 case CC_CONNECT_ACKNOWLEDGE | INDICATION:
2416                 case CC_CONNECT | CONFIRM:
2417                 if (p_state == PORT_STATE_CONNECT_WAITING)
2418                         new_state(PORT_STATE_CONNECT);
2419                 if (p_m_d_notify_pending)
2420                 {
2421                         /* send pending notify message during connect-ack */
2422                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
2423                         message_free(p_m_d_notify_pending);
2424                         p_m_d_notify_pending = NULL;
2425                 }
2426                 break;
2427
2428                 case CC_DISCONNECT | INDICATION:
2429                 disconnect_ind(prim, dinfo, data);
2430                 break;
2431
2432                 case CC_RELEASE | CONFIRM:
2433                 case CC_RELEASE | INDICATION:
2434                 release_ind(prim, dinfo, data);
2435                 break;
2436
2437                 case CC_RELEASE_COMPLETE | INDICATION:
2438                 release_complete_ind(prim, dinfo, data);
2439                 break;
2440
2441                 case CC_RELEASE_COMPLETE | CONFIRM:
2442                 break;
2443
2444                 case CC_NOTIFY | INDICATION:
2445                 notify_ind(prim, dinfo, data);
2446                 break;
2447
2448                 case CC_HOLD | INDICATION:
2449                 hold_ind(prim, dinfo, data);
2450                 break;
2451
2452                 case CC_RETRIEVE | INDICATION:
2453                 retrieve_ind(prim, dinfo, data);
2454                 break;
2455
2456                 case CC_SUSPEND | INDICATION:
2457                 suspend_ind(prim, dinfo, data);
2458                 break;
2459
2460                 case CC_RESUME | INDICATION:
2461                 resume_ind(prim, dinfo, data);
2462                 break;
2463
2464                 case CC_FACILITY | INDICATION:
2465                 facility_ind(prim, dinfo, data);
2466                 break;
2467
2468                 case CC_RELEASE_CR | INDICATION:
2469                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_CR_IND, DIRECTION_IN);
2470                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
2471                 end_trace();
2472                 if (p_m_d_ntmode)
2473                 {
2474                         if ((p_m_d_l3id&0xff00) == 0xff00)
2475                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2476                 }
2477                 p_m_d_l3id = 0;
2478                 p_m_d_ces = -1;
2479                 p_m_delete = 1;
2480 //#warning remove me
2481 //PDEBUG(DEBUG_LOG, "JOLLY release cr %d\n", p_serial);
2482                 /* sending release to endpoint in case we still have an endpoint
2483                  * this is because we don't get any response if a release_complete is received (or a release in release state)
2484                  */
2485                 while(p_epointlist)
2486                 {
2487                         struct lcr_msg *message;
2488                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2489                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
2490                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
2491                         message_put(message);
2492                         /* remove epoint */
2493                         free_epointlist(p_epointlist);
2494
2495                         new_state(PORT_STATE_RELEASE);
2496                 }
2497                 break;
2498
2499                 case CC_NEW_CR | INDICATION:
2500                 l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_IND, DIRECTION_IN);
2501                 if (p_m_d_l3id)
2502                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
2503                 if (p_m_d_ntmode)
2504                 {
2505                         new_l3id = *((int *)(((u_char *)data+mISDNUSER_HEAD_SIZE)));
2506                         if (((new_l3id&0xff00)!=0xff00) && ((p_m_d_l3id&0xff00)==0xff00))
2507                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2508                 } else
2509                 {
2510                         new_l3id = dinfo;
2511                 }
2512                 p_m_d_l3id = new_l3id;
2513                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
2514                 end_trace();
2515                 break;
2516
2517                 default:
2518                 l1l2l3_trace_header(p_m_mISDNport, this, L3_UNKNOWN, DIRECTION_IN);
2519                 add_trace("unhandled", "prim", "0x%x", prim);
2520                 end_trace();
2521         }
2522 }
2523 #endif
2524
2525 void Pdss1::new_state(int state)
2526 {
2527 //      class Endpoint *epoint;
2528
2529         /* set timeout */
2530         if (state == PORT_STATE_IN_OVERLAP)
2531         {
2532                 p_m_timeout = p_m_mISDNport->ifport->tout_dialing;
2533                 time(&p_m_timer);
2534         }
2535         if (state != p_state)
2536         {
2537                 if (state == PORT_STATE_IN_SETUP
2538                  || state == PORT_STATE_OUT_SETUP
2539                  || state == PORT_STATE_IN_OVERLAP
2540                  || state == PORT_STATE_OUT_OVERLAP)
2541                 {
2542                         p_m_timeout = p_m_mISDNport->ifport->tout_setup;
2543                         time(&p_m_timer);
2544                 }
2545                 if (state == PORT_STATE_IN_PROCEEDING
2546                  || state == PORT_STATE_OUT_PROCEEDING)
2547                 {
2548                         p_m_timeout = p_m_mISDNport->ifport->tout_proceeding;
2549                         time(&p_m_timer);
2550                 }
2551                 if (state == PORT_STATE_IN_ALERTING
2552                  || state == PORT_STATE_OUT_ALERTING)
2553                 {
2554                         p_m_timeout = p_m_mISDNport->ifport->tout_alerting;
2555                         time(&p_m_timer);
2556                 }
2557                 if (state == PORT_STATE_CONNECT
2558                  || state == PORT_STATE_CONNECT_WAITING)
2559                 {
2560                         p_m_timeout = 0;
2561                 }
2562                 if (state == PORT_STATE_IN_DISCONNECT
2563                  || state == PORT_STATE_OUT_DISCONNECT)
2564                 {
2565                         p_m_timeout = p_m_mISDNport->ifport->tout_disconnect;
2566                         time(&p_m_timer);
2567                 }
2568         }
2569         
2570         Port::new_state(state);
2571 }
2572
2573
2574 /*
2575  * handler
2576  */
2577 int Pdss1::handler(void)
2578 {
2579         int ret;
2580
2581         if ((ret = PmISDN::handler()))
2582                 return(ret);
2583
2584         /* handle destruction */
2585         if (p_m_delete && p_m_d_l3id==0)
2586         {
2587                 delete this;
2588                 return(-1);
2589         }
2590
2591         return(0);
2592 }
2593
2594
2595 /*
2596  * handles all messages from endpoint
2597  */
2598 /* MESSAGE_INFORMATION */
2599 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
2600 {
2601 #ifdef SOCKET_MISDN
2602         l3_msg *l3m;
2603 #else
2604         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2605         msg_t *dmsg;
2606         INFORMATION_t *information;
2607 #endif
2608
2609         if (param->information.id[0]) /* only if we have something to dial */
2610         {
2611 #ifdef SOCKET_MISDN
2612                 l3m = create_l3msg();
2613 #else
2614                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2615                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2616 #endif
2617                 l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2618 #ifdef SOCKET_MISDN
2619                 enc_ie_called_pn(l3m, 0, 1, (unsigned char *)param->information.id);
2620 #else
2621                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.id);
2622 #endif
2623                 end_trace();
2624 #ifdef SOCKET_MISDN
2625                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2626 #else
2627                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2628 #endif
2629         }
2630         new_state(p_state);
2631 }
2632
2633
2634 int newteid = 0;
2635
2636 /* MESSAGE_SETUP */
2637 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2638 {
2639 #ifdef SOCKET_MISDN
2640         l3_msg *l3m;
2641         int ret;
2642 #else
2643         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2644         msg_t *dmsg;
2645         INFORMATION_t *information;
2646         SETUP_t *setup;
2647         int i;
2648 #endif
2649         int plan, type, screen, present, reason;
2650         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2651         int channel, exclusive;
2652         struct epoint_list *epointlist;
2653
2654         /* release if port is blocked */
2655         if (p_m_mISDNport->ifport->block)
2656         {
2657                 struct lcr_msg *message;
2658
2659                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2660                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2661                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2662                 message_put(message);
2663                 new_state(PORT_STATE_RELEASE);
2664                 p_m_delete = 1;
2665                 return;
2666         }
2667
2668         /* copy setup infos to port */
2669         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2670         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2671         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2672         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2673 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2674         /* screen outgoing caller id */
2675         do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
2676
2677         /* only display at connect state: this case happens if endpoint is in connected mode */
2678         if (p_state==PORT_STATE_CONNECT)
2679         {
2680                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2681                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2682                         return;
2683                 if (p_callerinfo.display[0])
2684                 {
2685                         /* sending information */
2686 #ifdef SOCKET_MISDN
2687                         l3m = create_l3msg();
2688 #else
2689                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2690                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2691 #endif
2692                         l1l2l3_trace_header(p_m_mISDNport, this, L3_INFORMATION_REQ, DIRECTION_OUT);
2693                         if (p_m_d_ntmode)
2694 #ifdef SOCKET_MISDN
2695                                 enc_ie_display(l3m, (unsigned char *)p_callerinfo.display);
2696 #else
2697                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2698 #endif
2699                         end_trace();
2700 #ifdef SOCKET_MISDN
2701                         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_INFORMATION, p_m_d_l3id, l3m);
2702 #else
2703                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2704 #endif
2705                         return;
2706                 }
2707         }
2708
2709         /* attach only if not already */
2710         epointlist = p_epointlist;
2711         while(epointlist)
2712         {
2713                 if (epointlist->epoint_id == epoint_id)
2714                         break;
2715                 epointlist = epointlist->next;
2716         }
2717         if (!epointlist)
2718                 epointlist_new(epoint_id);
2719
2720         /* get channel */
2721         exclusive = 0;
2722         if (p_m_b_channel)
2723         {
2724                 channel = p_m_b_channel;
2725                 exclusive = p_m_b_exclusive;
2726         } else
2727                 channel = CHANNEL_ANY;
2728         /* nt-port with no channel, not reserverd */
2729         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2730                 channel = CHANNEL_NO;
2731
2732 #ifdef SOCKET_MISDN
2733         /* creating l3id */
2734         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_L3ID_REQ, DIRECTION_OUT);
2735         /* see MT_ASSIGN notes at do_layer3() */
2736         mt_assign_pid = 0;
2737         ret = p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_ASSIGN, 0, NULL);
2738         if (mt_assign_pid == 0 || ret < 0)
2739         {
2740                 struct lcr_msg *message;
2741
2742                 add_trace("callref", NULL, "no free id");
2743                 end_trace();
2744                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2745                 message->param.disconnectinfo.cause = 47;
2746                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2747                 message_put(message);
2748                 new_state(PORT_STATE_RELEASE);
2749                 p_m_delete = 1;
2750                 return;
2751         }
2752         p_m_d_l3id = mt_assign_pid;
2753         mt_assign_pid = ~0;
2754 #else
2755         /* creating l3id */
2756         l1l2l3_trace_header(p_m_mISDNport, this, L3_NEW_CR_REQ, DIRECTION_OUT);
2757         if (p_m_d_ntmode)
2758         {
2759                 i = 0;
2760                 while(i < 0x100)
2761                 {
2762                         if (p_m_mISDNport->procids[i] == 0)
2763                                 break;
2764                         i++;
2765                 }
2766                 if (i == 0x100)
2767                 {
2768                         struct lcr_msg *message;
2769
2770                         add_trace("callref", NULL, "no free id");
2771                         end_trace();
2772                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2773                         message->param.disconnectinfo.cause = 47;
2774                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2775                         message_put(message);
2776                         new_state(PORT_STATE_RELEASE);
2777                         p_m_delete = 1;
2778                         return;
2779                 }
2780                 p_m_mISDNport->procids[i] = 1;
2781                 p_m_d_l3id = 0xff00 | i;
2782         } else
2783         {
2784                 iframe_t ncr;
2785                 /* if we are in te-mode, we need to create a process first */
2786                 if (newteid++ > 0x7fff)
2787                         newteid = 0x0001;
2788                 p_m_d_l3id = (entity<<16) | newteid;
2789                 /* preparing message */
2790                 ncr.prim = CC_NEW_CR | REQUEST; 
2791                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2792                 ncr.dinfo = p_m_d_l3id;
2793                 ncr.len = 0;
2794                 /* send message */
2795                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2796 //              if (!dmsg)
2797 //                      goto nomem;
2798         }
2799         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2800 #endif
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                         message->param.disconnectinfo.cause = 16;
3569                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3570                         message_put(message);
3571                         /* remove epoint */
3572                         free_epointlist(p_epointlist);
3573                 }
3574                 /* sending release */
3575 #ifdef SOCKET_MISDN
3576                 l3m = create_l3msg();
3577 #else
3578                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
3579                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
3580 #endif
3581                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_COMPLETE_REQ, DIRECTION_OUT);
3582                 /* send cause */
3583 #ifdef SOCKET_MISDN
3584                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3585 #else
3586                 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);
3587 #endif
3588                 end_trace();
3589 #ifdef SOCKET_MISDN
3590                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
3591 #else
3592                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3593 #endif
3594                 new_state(PORT_STATE_RELEASE);
3595                 p_m_delete = 1;
3596                 return;
3597         }
3598
3599         /* workarround: NT-MODE in setup state we must send PROCEEDING first to make it work */
3600         if (p_state==PORT_STATE_IN_SETUP)
3601         {
3602                 /* sending proceeding */
3603 #ifdef SOCKET_MISDN
3604                 l3m = create_l3msg();
3605 #else
3606                 CALL_PROCEEDING_t *proceeding;
3607                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3608                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3609 #endif
3610                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3611                 /* channel information */
3612 #ifdef SOCKET_MISDN
3613                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3614 #else
3615                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3616 #endif
3617                 /* progress information */
3618                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3619                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
3620                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3621 #ifdef SOCKET_MISDN
3622                         enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3623 #else
3624                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3625 #endif
3626                 end_trace();
3627 #ifdef SOCKET_MISDN
3628                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3629 #else
3630                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3631 #endif
3632                 new_state(PORT_STATE_IN_PROCEEDING);
3633         }
3634
3635         /* sending disconnect */
3636 #ifdef SOCKET_MISDN
3637         l3m = create_l3msg();
3638 #else
3639         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
3640         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
3641 #endif
3642         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
3643         /* progress information */
3644         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3645          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3646          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3647         if (p_m_mISDNport->tones)
3648 #ifdef SOCKET_MISDN
3649                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3650 #else
3651                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3652 #endif
3653         /* send cause */
3654 #ifdef SOCKET_MISDN
3655         enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3656 #else
3657         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);
3658 #endif
3659         /* send display */
3660         if (param->disconnectinfo.display[0])
3661                 p = param->disconnectinfo.display;
3662         if (p) if (*p && p_m_d_ntmode)
3663 #ifdef SOCKET_MISDN
3664                 enc_ie_display(l3m, (unsigned char *)p);
3665 #else
3666                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
3667 #endif
3668         end_trace();
3669 #ifdef SOCKET_MISDN
3670         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
3671 #else
3672         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3673 #endif
3674         new_state(PORT_STATE_OUT_DISCONNECT);
3675 }
3676
3677 /* MESSAGE_RELEASE */
3678 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
3679 {
3680 #ifdef SOCKET_MISDN
3681         l3_msg *l3m;
3682 #else
3683         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
3684         msg_t *dmsg;
3685         RELEASE_t *release;
3686         RELEASE_COMPLETE_t *release_complete;
3687         DISCONNECT_t *disconnect;
3688 #endif
3689         class Endpoint *epoint;
3690         char *p = NULL;
3691
3692         /*
3693          * we may only release during incoming disconnect state.
3694          * this means that the endpoint doesnt require audio anymore
3695          */
3696         if (p_state == PORT_STATE_IN_DISCONNECT
3697          || p_state == PORT_STATE_OUT_DISCONNECT)
3698         {
3699                 /* sending release */
3700 #ifdef SOCKET_MISDN
3701                 l3m = create_l3msg();
3702 #else
3703                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
3704                 release = (RELEASE_t *)(dmsg->data + headerlen);
3705 #endif
3706                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
3707                 /* send cause */
3708 #ifdef SOCKET_MISDN
3709                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3710 #else
3711                 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);
3712 #endif
3713                 end_trace();
3714 #ifdef SOCKET_MISDN
3715                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE, p_m_d_l3id, l3m);
3716 #else
3717                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3718 #endif
3719                 new_state(PORT_STATE_RELEASE);
3720                 /* remove epoint */
3721                 free_epointid(epoint_id);
3722                 // wait for callref to be released
3723                 return;
3724
3725         }
3726         /*
3727          * if we are on incoming call setup, we may reject by sending a release_complete
3728          * also on outgoing call setup, we send a release complete, BUT this is not conform. (i don't know any other way)
3729          */
3730         if (p_state==PORT_STATE_IN_SETUP
3731          || p_state==PORT_STATE_OUT_SETUP)
3732 // // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
3733 //       || p_state==PORT_STATE_OUT_DISCO)
3734         {
3735 //#warning remove me
3736 //PDEBUG(DEBUG_LOG, "JOLLY sending release complete %d\n", p_serial);
3737                 /* sending release complete */
3738 #ifdef SOCKET_MISDN
3739                 l3m = create_l3msg();
3740 #else
3741                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
3742                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
3743 #endif
3744                 l1l2l3_trace_header(p_m_mISDNport, this, L3_RELEASE_REQ, DIRECTION_OUT);
3745                 /* send cause */
3746 #ifdef SOCKET_MISDN
3747                 enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3748 #else
3749                 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);
3750 #endif
3751                 end_trace();
3752 #ifdef SOCKET_MISDN
3753                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_RELEASE_COMPLETE, p_m_d_l3id, l3m);
3754 #else
3755                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3756 #endif
3757                 new_state(PORT_STATE_RELEASE);
3758                 /* remove epoint */
3759                 free_epointid(epoint_id);
3760                 // wait for callref to be released
3761                 return;
3762         }
3763
3764 #if 0
3765 wirklich erst proceeding?:
3766         /* NT-MODE in setup state we must send PROCEEDING first */
3767         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
3768         {
3769                 CALL_PROCEEDING_t *proceeding;
3770
3771                 /* sending proceeding */
3772 #ifdef SOCKET_MISDN
3773                 l3m = create_l3msg();
3774 #else
3775                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
3776                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
3777 #endif
3778                 l1l2l3_trace_header(p_m_mISDNport, this, L3_PROCEEDING_REQ, DIRECTION_OUT);
3779                 /* channel information */
3780 #ifdef SOCKET_MISDN
3781                 enc_ie_channel_id(l3m, 1, p_m_b_channel);
3782 #else
3783                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
3784 #endif
3785                 /* progress information */
3786                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3787                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
3788                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3789 #ifdef SOCKET_MISDN
3790                         enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3791 #else
3792                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3793 #endif
3794                 end_trace();
3795 #ifdef SOCKET_MISDN
3796                 p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_CALL_PROCEEDING, p_m_d_l3id, l3m);
3797 #else
3798                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3799 #endif
3800         }
3801 #endif
3802
3803         /* sending disconnect */
3804 #ifdef SOCKET_MISDN
3805         l3m = create_l3msg();
3806 #else
3807         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
3808         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
3809 #endif
3810         l1l2l3_trace_header(p_m_mISDNport, this, L3_DISCONNECT_REQ, DIRECTION_OUT);
3811         /* progress information */
3812         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
3813          || p_capainfo.bearer_capa==INFO_BC_AUDIO
3814          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
3815         if (p_m_mISDNport->tones)
3816 #ifdef SOCKET_MISDN
3817                 enc_ie_progress(l3m, 0, p_m_d_ntmode?1:5, 8);
3818 #else
3819                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
3820 #endif
3821         /* send cause */
3822 #ifdef SOCKET_MISDN
3823         enc_ie_cause(l3m, (p_m_mISDNport->locally && param->disconnectinfo.location==LOCATION_PRIVATE_LOCAL)?LOCATION_PRIVATE_LOCAL:param->disconnectinfo.location, param->disconnectinfo.cause);
3824 #else
3825         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);
3826 #endif
3827         /* send display */
3828         epoint = find_epoint_id(epoint_id);
3829         if (param->disconnectinfo.display[0])
3830                 p = param->disconnectinfo.display;
3831         if (p) if (*p && p_m_d_ntmode)
3832 #ifdef SOCKET_MISDN
3833                 enc_ie_display(l3m, (unsigned char *)p);
3834 #else
3835                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
3836 #endif
3837         end_trace();
3838 #ifdef SOCKET_MISDN
3839         p_m_mISDNport->ml3->to_layer3(p_m_mISDNport->ml3, MT_DISCONNECT, p_m_d_l3id, l3m);
3840 #else
3841         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
3842 #endif
3843         new_state(PORT_STATE_OUT_DISCONNECT);
3844         /* remove epoint */
3845         free_epointid(epoint_id);
3846         // wait for release and callref to be released
3847 //#warning remove me
3848 //PDEBUG(DEBUG_LOG, "JOLLY sending disconnect %d\n", p_serial);
3849 }
3850
3851
3852 /*
3853  * endpoint sends messages to the port
3854  */
3855 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
3856 {
3857         struct lcr_msg *message;
3858
3859         if (PmISDN::message_epoint(epoint_id, message_id, param))
3860                 return(1);
3861
3862         switch(message_id)
3863         {
3864                 case MESSAGE_INFORMATION: /* overlap dialing */
3865                 if (p_type==PORT_TYPE_DSS1_NT_OUT
3866                  && p_state!=PORT_STATE_OUT_OVERLAP
3867                  && p_state!=PORT_STATE_CONNECT
3868                  && p_state!=PORT_STATE_OUT_DISCONNECT
3869                  && p_state!=PORT_STATE_IN_DISCONNECT)
3870                 {
3871                         break;
3872                 }
3873                 if (p_type==PORT_TYPE_DSS1_TE_OUT
3874                  && p_state!=PORT_STATE_OUT_OVERLAP
3875                  && p_state!=PORT_STATE_OUT_PROCEEDING
3876                  && p_state!=PORT_STATE_OUT_ALERTING
3877                  && p_state!=PORT_STATE_CONNECT
3878                  && p_state!=PORT_STATE_OUT_DISCONNECT
3879                  && p_state!=PORT_STATE_IN_DISCONNECT)
3880                 {
3881                         break;
3882                 }
3883                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
3884                  && p_state!=PORT_STATE_IN_OVERLAP
3885                  && p_state!=PORT_STATE_IN_PROCEEDING
3886                  && p_state!=PORT_STATE_IN_ALERTING
3887                  && p_state!=PORT_STATE_CONNECT
3888                  && p_state!=PORT_STATE_CONNECT_WAITING
3889                  && p_state!=PORT_STATE_OUT_DISCONNECT
3890                  && p_state!=PORT_STATE_IN_DISCONNECT)
3891                 {
3892                         break;
3893                 }
3894                 message_information(epoint_id, message_id, param);
3895                 break;
3896
3897                 case MESSAGE_SETUP: /* dial-out command received from epoint */
3898                 if (p_state!=PORT_STATE_IDLE
3899                  && p_state!=PORT_STATE_CONNECT)
3900                 {
3901                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
3902                         break;
3903                 }
3904                 if (p_epointlist && p_state==PORT_STATE_IDLE)
3905                         FATAL("Pdss1(%s): epoint pointer is set in idle state, how bad!!\n", p_name);
3906 #ifdef SOCKET_MISDN
3907                 message_setup(epoint_id, message_id, param);
3908 #else
3909                 /* note: pri is a special case, because links must be up for pri */ 
3910                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
3911                 {
3912                         /* LAYER 1 is up, or we may send */
3913                         message_setup(epoint_id, message_id, param);
3914                 } else {
3915                         iframe_t act;
3916                         /* LAYER 1 id down, so we queue */
3917                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
3918                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
3919                         /* attach us */
3920                         if (!(epointlist_new(epoint_id)))
3921                                 FATAL("No memory for epointlist\n");
3922                         /* activate link */
3923                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
3924                         act.prim = PH_ACTIVATE | REQUEST; 
3925                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
3926                         act.dinfo = 0;
3927                         act.len = 0;
3928                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
3929                         l1l2l3_trace_header(p_m_mISDNport, this, L1_ACTIVATE_REQ, DIRECTION_OUT);
3930                         end_trace();
3931 //                      /* set timeout */
3932 //                      p_m_mISDNport->l1timeout = now+3;
3933                 }
3934 #endif
3935                 break;
3936
3937                 case MESSAGE_NOTIFY: /* display and notifications */
3938                 message_notify(epoint_id, message_id, param);
3939                 break;
3940
3941                 case MESSAGE_FACILITY: /* facility message */
3942                 message_facility(epoint_id, message_id, param);
3943                 break;
3944
3945                 case MESSAGE_OVERLAP: /* more information is needed */
3946                 if (p_state!=PORT_STATE_IN_SETUP)
3947                 {
3948                         break;
3949                 }
3950                 message_overlap(epoint_id, message_id, param);
3951                 break;
3952
3953                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
3954                 if (p_state!=PORT_STATE_IN_SETUP
3955                  && p_state!=PORT_STATE_IN_OVERLAP)
3956                 {
3957                         break;
3958                 }
3959                 message_proceeding(epoint_id, message_id, param);
3960                 break;
3961
3962                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
3963                 if (p_state!=PORT_STATE_IN_SETUP
3964                  && p_state!=PORT_STATE_IN_OVERLAP
3965                  && p_state!=PORT_STATE_IN_PROCEEDING)
3966                 {
3967                         break;
3968                 }
3969                 message_alerting(epoint_id, message_id, param);
3970                 break;
3971
3972                 case MESSAGE_CONNECT: /* call of endpoint is connected */
3973                 if (p_state!=PORT_STATE_IN_SETUP
3974                  && p_state!=PORT_STATE_IN_OVERLAP
3975                  && p_state!=PORT_STATE_IN_PROCEEDING
3976                  && p_state!=PORT_STATE_IN_ALERTING
3977                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
3978                 {
3979                         break;
3980                 }
3981                 message_connect(epoint_id, message_id, param);
3982                 break;
3983
3984                 case MESSAGE_DISCONNECT: /* call has been disconnected */
3985                 if (p_state!=PORT_STATE_IN_SETUP
3986                  && p_state!=PORT_STATE_IN_OVERLAP
3987                  && p_state!=PORT_STATE_IN_PROCEEDING
3988                  && p_state!=PORT_STATE_IN_ALERTING
3989                  && p_state!=PORT_STATE_OUT_SETUP
3990                  && p_state!=PORT_STATE_OUT_OVERLAP
3991                  && p_state!=PORT_STATE_OUT_PROCEEDING
3992                  && p_state!=PORT_STATE_OUT_ALERTING
3993                  && p_state!=PORT_STATE_CONNECT
3994                  && p_state!=PORT_STATE_CONNECT_WAITING)
3995                 {
3996                         break;
3997                 }
3998                 message_disconnect(epoint_id, message_id, param);
3999                 break;
4000
4001                 case MESSAGE_RELEASE: /* release isdn port */
4002                 if (p_state==PORT_STATE_RELEASE)
4003                 {
4004                         break;
4005                 }
4006                 message_release(epoint_id, message_id, param);
4007                 break;
4008
4009                 default:
4010                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
4011         }
4012
4013         return(1);
4014 }
4015
4016
4017
4018 /*
4019  * data from isdn-stack (layer-3) to pbx (port class)
4020  */
4021 #ifdef SOCKET_MISDN
4022 int stack2manager(struct mISDNport *mISDNport, unsigned int cmd, unsigned int pid, struct l3_msg *l3m)
4023 {
4024         class Port *port;
4025         class Pdss1 *pdss1;
4026         char name[32];
4027
4028         PDEBUG(DEBUG_ISDN, "cmd(0x%x) pid(0x%x)\n", cmd, pid);
4029
4030         if (pid == 0)
4031         {
4032                 PERROR("PID is 0. change it.... quick...\n");
4033                 return(-EINVAL);
4034         }
4035
4036         /* find Port object of type ISDN */
4037         port = port_first;
4038         while(port)
4039         {
4040                 /* are we ISDN ? */
4041                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4042                 {
4043                         pdss1 = (class Pdss1 *)port;
4044                         /* check out correct stack and id */
4045                         if (pdss1->p_m_mISDNport == mISDNport
4046                          && (pdss1->p_m_d_l3id & MISDN_PID_CRVAL_MASK) == (pid & MISDN_PID_CRVAL_MASK))
4047                         {
4048                                 /* found port, the message belongs to */
4049                                 break;
4050                         }
4051                 }
4052                 port = port->next;
4053         }
4054
4055         /* aktueller prozess */
4056         if (port)
4057         {
4058                 /* if process id is master process, but a child disconnects */
4059                 if (mISDNport->ntmode
4060                  && (pid & MISDN_PID_CRTYPE_MASK) != MISDN_PID_MASTER
4061                  && (pdss1->p_m_d_l3id & MISDN_PID_CRVAL_MASK) == MISDN_PID_MASTER)
4062                 {
4063                         if (cmd == MT_DISCONNECT)
4064                         {
4065                                 /* send special indication for child disconnect */
4066                                 pdss1->disconnect_ind_i(cmd, pid, l3m);
4067                                 return(0);
4068                         }
4069                         // ignoring other messages from child processes
4070                         return(0);
4071                 }
4072                 /* if process id and layer 3 id matches */
4073                 if (pid == pdss1->p_m_d_l3id)
4074                         pdss1->message_isdn(cmd, pid, l3m);
4075                 return(0);
4076         }
4077
4078         /* d-message */
4079         switch(cmd)
4080         {
4081                 case MT_SETUP:
4082                 /* creating port object */
4083                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4084                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4085
4086                         FATAL("Cannot create Port instance.\n");
4087                 pdss1->message_isdn(cmd, pid, l3m);
4088                 break;
4089
4090                 case MT_RESUME:
4091                 /* creating port object */
4092                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4093                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4094                         FATAL("Cannot create Port instance.\n");
4095                 pdss1->message_isdn(cmd, pid, l3m);
4096                 break;
4097
4098                 case MT_FREE:
4099                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", pid);
4100                 break;
4101
4102                 case MT_RELEASE_COMPLETE:
4103                 PERROR("must be ignored by stack, not sent to app\n");
4104                 break;
4105
4106                 case MT_FACILITY:
4107                 // facility als broadcast
4108                 break;
4109
4110                 default:
4111                 PERROR("unhandled message: cmd(0x%x) pid(0x%x)\n", cmd, pid);
4112                 port = port_first;
4113                 while(port)
4114                 {
4115                         if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4116                         {
4117                                 pdss1 = (class Pdss1 *)port;
4118                                 /* check out correct stack */
4119                                 if (pdss1->p_m_mISDNport == mISDNport)
4120                                 /* check out correct id */
4121                                 PERROR("unhandled message: pid=%x is not associated with port-dinfo=%x\n", pid, pdss1->p_m_d_l3id);
4122                         }
4123                         port = port->next;
4124                 }
4125                 return(-EINVAL);
4126         }
4127         return(0);
4128 }
4129 #else
4130 /* NOTE: nt mode use mISDNuser_head_t as header */
4131 int stack2manager_nt(void *dat, void *arg)
4132 {
4133         class Port *port;
4134         class Pdss1 *pdss1;
4135         manager_t *mgr = (manager_t *)dat;
4136         msg_t *msg = (msg_t *)arg;
4137         mISDNuser_head_t *hh;
4138         struct mISDNport *mISDNport;
4139         char name[32];
4140
4141         if (!msg || !mgr)
4142                 return(-EINVAL);
4143
4144         /* note: nst is the first data feld of mISDNport */
4145         mISDNport = (struct mISDNport *)mgr->nst;
4146
4147         hh = (mISDNuser_head_t *)msg->data;
4148         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
4149
4150         /* find Port object of type ISDN */
4151         port = port_first;
4152         while(port)
4153         {
4154                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4155                 {
4156                         pdss1 = (class Pdss1 *)port;
4157 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
4158                         /* check out correct stack */
4159                         if (pdss1->p_m_mISDNport == mISDNport)
4160                         /* check out correct id */
4161                         if ((pdss1->p_m_d_l3id&0x0000ff00) != 0x000ff00)
4162                         {
4163                                 /* a single process */
4164                                 if (hh->dinfo == pdss1->p_m_d_l3id)
4165                                 {
4166                                         /* found port, the message belongs to */
4167                                         break;
4168                                 }
4169                         } else
4170                         {
4171                                 /* a broadcast process */
4172                                 if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
4173                                 {
4174                                         /* found port, the message belongs to */
4175                                         break;
4176                                 }
4177                         }
4178                 }
4179                 port = port->next;
4180         }
4181         if (port)
4182         {
4183 //printf("%x %x\n", hh->dinfo, pdss1->p_m_d_l3id);
4184                 /* if process id is master process, but a child disconnects */
4185                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00)
4186                 {
4187                         if (hh->prim == (CC_DISCONNECT|INDICATION))
4188                         {
4189                                 /* send special indication for child disconnect */
4190                                 pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
4191                                 free_msg(msg);
4192                                 return(0);
4193                         }
4194                         // ignoring other messages from child processes
4195                         free_msg(msg);
4196                         return(0);
4197                 }
4198                 /* if process id and layer 3 id matches */
4199                 if (hh->dinfo == pdss1->p_m_d_l3id)
4200                 {
4201                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
4202                         free_msg(msg);
4203                         return(0);
4204                 }
4205         }
4206
4207         /* d-message */
4208         switch(hh->prim)
4209         {
4210                 case MGR_SHORTSTATUS | INDICATION:
4211                 case MGR_SHORTSTATUS | CONFIRM:
4212                 switch(hh->dinfo) {
4213                         case SSTATUS_L2_ESTABLISHED:
4214                         goto ss_estab;
4215                         case SSTATUS_L2_RELEASED:
4216                         goto ss_rel;
4217                 }
4218                 break;
4219
4220                 case DL_ESTABLISH | INDICATION:
4221                 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_IND, DIRECTION_IN);
4222                 goto ss_estab;
4223                 case DL_ESTABLISH | CONFIRM:
4224                 l1l2l3_trace_header(mISDNport, NULL, L2_ESTABLISH_CON, DIRECTION_IN);
4225                 ss_estab:
4226                 add_trace("tei", NULL, "%d", hh->dinfo);
4227                 end_trace();
4228                 if (mISDNport->ptp && hh->dinfo == 0)
4229                 {
4230                         if (mISDNport->l2establish)
4231                         {
4232                                 mISDNport->l2establish = 0;
4233                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
4234                         }
4235                         mISDNport->l2link = 1;
4236                         if (mISDNport->pri);
4237                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
4238                 }
4239                 break;
4240
4241                 case DL_RELEASE | INDICATION:
4242                 l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_IND, DIRECTION_IN);
4243                 goto ss_rel;
4244                 case DL_RELEASE | CONFIRM:
4245                 l1l2l3_trace_header(mISDNport, NULL, L2_RELEASE_CON, DIRECTION_IN);
4246                 ss_rel:
4247                 add_trace("tei", NULL, "%d", hh->dinfo);
4248                 end_trace();
4249                 if (mISDNport->ptp && hh->dinfo == 0)
4250                 {
4251                         mISDNport->l2link = 0;
4252                         time(&mISDNport->l2establish);
4253                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
4254                 }
4255                 break;
4256
4257                 case CC_SETUP | INDICATION:
4258                 /* creating port object */
4259                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4260                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4261
4262                         FATAL("Cannot create Port instance.\n");
4263                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
4264                 break;
4265
4266                 case CC_RESUME | INDICATION:
4267                 /* creating port object */
4268                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4269                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
4270                         FATAL("Cannot create Port instance.\n");
4271                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
4272                 break;
4273
4274                 case CC_RELEASE_CR | INDICATION:
4275                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", hh->dinfo);
4276                 break;
4277
4278                 case CC_RELEASE_COMPLETE | INDICATION:
4279                 break;
4280
4281                 case CC_FACILITY | INDICATION:
4282                 break;
4283
4284                 default:
4285                 PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
4286                 port = port_first;
4287                 while(port)
4288                 {
4289                         if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
4290                         {
4291                                 pdss1 = (class Pdss1 *)port;
4292         //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
4293                                 /* check out correct stack */
4294                                 if (pdss1->p_m_mISDNport == mISDNport)
4295                                 /* check out correct id */
4296                                 PERROR("unhandled message: dinfo=%x is not associated with port-dinfo=%x\n",hh->dinfo,pdss1->p_m_d_l3id);
4297                         }
4298                         port = port->next;
4299                 }
4300                 return(-EINVAL);
4301         }
4302         free_msg(msg);
4303         return(0);
4304 }
4305
4306 /* NOTE: te mode use iframe_t as header */
4307 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
4308 {
4309         class Port *port;
4310         class Pdss1 *pdss1;
4311         iframe_t *frm;
4312         char name[32];
4313
4314         if (!msg || !mISDNport)
4315                 return(-EINVAL);
4316         frm = (iframe_t *)msg->data;
4317         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
4318
4319         /* find Port object of type ISDN */
4320         port = port_first;
4321         while(port)
4322         {
4323                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
4324                 {
4325                         pdss1 = (class Pdss1 *)port;
4326                         /* check out correct stack */
4327                         if (pdss1->p_m_mISDNport == mISDNport)
4328                         /* check out correct id */
4329                         if (frm->dinfo == pdss1->p_m_d_l3id)
4330                         {
4331                                 /* found port, the message belongs to */
4332                                 break;
4333                         }
4334                 }
4335                 port = port->next;
4336         }
4337         if (port)
4338         {
4339                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
4340                 free_msg(msg);
4341                 return(0);
4342         }
4343
4344         /* process new cr (before setup indication) */
4345 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
4346         if (frm->prim == (CC_NEW_CR | INDICATION))
4347         {
4348
4349                 /* creating port object */
4350                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
4351                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_TE_IN, mISDNport, name, NULL, 0, 0)))
4352                         FATAL("Cannot create Port instance.\n");
4353                 /* l3id will be set from dinfo at message_isdn */
4354                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
4355                 free_msg(msg);
4356                 return(0);
4357         }
4358
4359         if (frm->prim == (CC_RELEASE_CR | INDICATION))
4360         {
4361                 PDEBUG(DEBUG_ISDN, "unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
4362                 free_msg(msg);
4363                 return(0);
4364         }
4365         PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
4366         return(-EINVAL);
4367 }
4368
4369 #endif // stacktomanager
4370
4371
4372 #ifndef SOCKET_MISDN
4373 /*
4374  * sending message that were queued during L1 activation
4375  * or releasing port if link is down
4376  */
4377 void setup_queue(struct mISDNport *mISDNport, int link)
4378 {
4379         class Port *port;
4380         class Pdss1 *pdss1;
4381         struct lcr_msg *message;
4382
4383         if (!mISDNport->ntmode)
4384                 return;
4385
4386         /* check all port objects for pending message */
4387         port = port_first;
4388         while(port)
4389         {
4390                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
4391                 {
4392                         pdss1 = (class Pdss1 *)port;
4393                         if (pdss1->p_m_mISDNport == mISDNport)
4394                         {
4395                                 if (pdss1->p_m_d_queue)
4396                                 {
4397                                         if (link)
4398                                         {
4399                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
4400                                                 /* LAYER 1 is up, so we send */
4401                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
4402                                                 message_free(pdss1->p_m_d_queue);
4403                                                 pdss1->p_m_d_queue = NULL;
4404                                         } else
4405                                         {
4406                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
4407                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
4408                                                 message->param.disconnectinfo.cause = 27;
4409                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
4410                                                 message_put(message);
4411                                                 pdss1->new_state(PORT_STATE_RELEASE);
4412                                                 pdss1->p_m_delete = 1;
4413                                         }
4414                                 }
4415                         }
4416                 }
4417                 port = port->next;
4418         }
4419 }
4420
4421 #endif
4422
4423
4424