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