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