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