backup
[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 <net_l2.h>
26 }
27
28 //#define CENTREX
29
30 #include "q931.h"
31 #include "ie.cpp"
32
33
34 /*
35  * constructor
36  */
37 Pdss1::Pdss1(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive) : PmISDN(type, mISDNport, portname, settings, channel, exclusive)
38 {
39         p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
40         p_m_d_ntmode = mISDNport->ntmode;
41         p_m_d_l3id = 0;
42         p_m_d_ces = -1;
43         p_m_d_queue = NULL;
44         p_m_d_notify_pending = NULL;
45         p_m_d_collect_cause = CAUSE_NOUSER;
46         p_m_d_collect_location = LOCATION_PRIVATE_LOCAL;
47
48         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);
49 }
50
51
52 /*
53  * destructor
54  */
55 Pdss1::~Pdss1()
56 {
57         /* remove queued message */
58         if (p_m_d_queue)
59                 message_free(p_m_d_queue);
60
61         if (p_m_d_notify_pending)
62                 message_free(p_m_d_notify_pending);
63
64         /* check how many processes are left */
65         if (p_m_d_ntmode == 1)
66         {
67                 if (p_m_mISDNport->nst.layer3->proc)
68                         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);
69         }
70 }
71
72
73 /*
74  * create layer 3 message
75  */
76 static msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
77 {
78         msg_t *dmsg;
79         Q931_info_t *qi;
80         iframe_t *frm;
81
82         if (!ntmode)
83                 size = sizeof(Q931_info_t)+2;
84
85         if (ntmode)
86         {
87                 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
88                 if (dmsg)
89                 {
90                         return(dmsg);
91                 }
92         } else
93         {
94                 dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
95                 if (dmsg)
96                 {
97                         memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
98                         frm = (iframe_t *)dmsg->data;
99                         frm->prim = prim;
100                         frm->dinfo = dinfo;
101                         qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
102                         qi->type = mt;
103                         return(dmsg);
104                 }
105         }
106
107         FATAL("Cannot allocate memory, system overloaded.\n");
108         exit(0); // make gcc happy
109 }
110
111 msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
112 {
113         msg_t *dmsg;
114
115         dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
116         if (dmsg)
117                 return(dmsg);
118
119         FATAL("Cannot allocate memory, system overloaded.\n");
120         exit(0); // make gcc happy
121 }
122
123 /*
124  * if we received a first reply to the setup message,
125  * we will check if we have now channel information 
126  * return: <0: error, call is released, -cause is given
127  *          0: ok, nothing to do
128  */
129 int Pdss1::received_first_reply_to_setup(unsigned long prim, int channel, int exclusive)
130 {
131         int ret;
132         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
133         RELEASE_COMPLETE_t *release_complete;
134         msg_t *dmsg;
135
136         /* correct exclusive to 0, if no explicit channel was given */
137         if (exclusive<0 || channel<=0)
138                 exclusive = 0;
139         
140         /* select scenario */
141         if (p_m_b_channel && p_m_b_exclusive)
142         {
143                 /*** we gave an exclusive channel (or if we are done) ***/
144
145                 /* if not first reply, we are done */
146                 if (p_state != PORT_STATE_OUT_SETUP)
147                         return(0);
148
149                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
150                 add_trace("channel", "request", "%d (forced)", p_m_b_channel);
151                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
152
153                 /* if give channel not accepted or not equal */
154                 if (channel!=-1 && p_m_b_channel!=channel)
155                 {
156                         add_trace("conclusion", NULL, "forced channel not accepted");
157                         end_trace();
158                         ret = -44;
159                         goto channelerror;
160                 }
161
162                 add_trace("conclusion", NULL, "channel was accepted");
163                 add_trace("connect", "channel", "%d", p_m_b_channel);
164                 end_trace();
165
166                 /* activate our exclusive channel */
167                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
168         } else
169         if (p_m_b_channel)
170         {
171                 /*** we gave a non-exclusive channel ***/
172
173                 /* if not first reply, we are done */
174                 if (p_state != PORT_STATE_OUT_SETUP)
175                         return(0);
176
177                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
178                 add_trace("channel", "request", "%d (suggest)", p_m_b_channel);
179                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
180
181                 /* if channel was accepted as given */
182                 if (channel==-1 || p_m_b_channel==channel)
183                 {
184                         add_trace("conclusion", NULL, "channel was accepted as given");
185                         add_trace("connect", "channel", "%d", p_m_b_channel);
186                         end_trace();
187                         p_m_b_exclusive = 1; // we are done
188                         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
189                         return(0);
190                 }
191
192                 /* if channel value is faulty */
193                 if (channel <= 0)
194                 {
195                         add_trace("conclusion", NULL, "illegal reply");
196                         end_trace();
197                         ret = -111; // protocol error
198                         goto channelerror;
199                 }
200
201                 /* if channel was not accepted, try to get it */
202                 ret = seize_bchannel(channel, 1); // exclusively
203                 add_trace("channel", "available", ret<0?"no":"yes");
204                 if (ret < 0)
205                 {
206                         add_trace("conclusion", NULL, "replied channel not available");
207                         end_trace();
208                         goto channelerror;
209                 }
210                 add_trace("conclusion", NULL, "replied channel accepted");
211                 add_trace("connect", "channel", "%d", p_m_b_channel);
212                 end_trace();
213
214                 /* activate channel given by remote */
215                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
216         } else
217         if (p_m_b_reserve)
218         {
219                 /*** we sent 'any channel acceptable' ***/
220
221                 /* if not first reply, we are done */
222                 if (p_state != PORT_STATE_OUT_SETUP)
223                         return(0);
224
225                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
226                 add_trace("channel", "request", "any");
227                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
228                 /* if no channel was replied */
229                 if (channel <= 0)
230                 {
231                         add_trace("conclusion", NULL, "no channel, protocol error");
232                         end_trace();
233                         ret = -111; // protocol error
234                         goto channelerror;
235                 }
236
237                 /* we will see, if our received channel is available */
238                 ret = seize_bchannel(channel, 1); // exclusively
239                 add_trace("channel", "available", ret<0?"no":"yes");
240                 if (ret < 0)
241                 {
242                         add_trace("conclusion", NULL, "replied channel not available");
243                         end_trace();
244                         goto channelerror;
245                 }
246                 add_trace("conclusion", NULL, "replied channel accepted");
247                 add_trace("connect", "channel", "%d", p_m_b_channel);
248                 end_trace();
249
250                 /* activate channel given by remote */
251                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
252         } else
253         {
254                 /*** we sent 'no channel available' ***/
255
256                 /* if not the first reply, but a connect, we are forced */
257                 if (prim==(CC_CONNECT | INDICATION) && p_state!=PORT_STATE_OUT_SETUP)
258                 {
259                         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (connect)", DIRECTION_NONE);
260                         add_trace("channel", "request", "no-channel");
261                         add_trace("channel", "reply", (channel>=0)?"%d%s":"(none)", channel, exclusive?" (forced)":"");
262                         if (channel > 0)
263                         {
264                                 goto use_from_connect;
265                         }
266                         ret = seize_bchannel(CHANNEL_ANY, 0); // any channel
267                         add_trace("channel", "available", ret<0?"no":"yes");
268                         if (ret < 0)
269                         {
270                                 add_trace("conclusion", NULL, "no channel available during call-waiting");
271                                 end_trace();
272                                 goto channelerror;
273                         }
274                         add_trace("conclusion", NULL, "using channel %d", p_m_b_channel);
275                         add_trace("connect", "channel", "%d", p_m_b_channel);
276                         end_trace();
277                         p_m_b_exclusive = 1; // we are done
278
279                         /* activate channel given by remote */
280                         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
281                         return(0);
282                 }
283                 
284                 /* if not first reply, we are done */
285                 if (p_state != PORT_STATE_OUT_SETUP)
286                         return(0);
287
288                 chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (first reply to setup)", DIRECTION_NONE);
289                 add_trace("channel", "request", "no-channel");
290                 add_trace("channel", "reply", (channel>=0)?"%d":"(none)", channel);
291                 /* if first reply has no channel, we are done */
292                 if (channel <= 0)
293                 {
294                         add_trace("conclusion", NULL, "no channel until connect");
295                         end_trace();
296                         return(0);
297                 }
298
299                 /* we will see, if our received channel is available */
300                 use_from_connect:
301                 ret = seize_bchannel(channel, exclusive);
302                 add_trace("channel", "available", ret<0?"no":"yes");
303                 if (ret < 0)
304                 {
305                         add_trace("conclusion", NULL, "replied channel not available");
306                         end_trace();
307                         goto channelerror;
308                 }
309                 add_trace("conclusion", NULL, "replied channel accepted");
310                 add_trace("connect", "channel", "%d", p_m_b_channel);
311                 end_trace();
312                 p_m_b_exclusive = 1; // we are done
313
314                 /* activate channel given by remote */
315                 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
316         }
317         return(0);
318
319         channelerror:
320         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
321         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
322         l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
323         enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
324         end_trace();
325         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
326         new_state(PORT_STATE_RELEASE);
327         p_m_delete = 1;
328         return(-34); /* to epoint: no channel available */
329 }
330
331
332 /*
333  * hunt bchannel for incomming setup or retrieve or resume
334  */
335 int Pdss1::hunt_bchannel(int channel, int exclusive)
336 {
337         struct select_channel *selchannel;
338         struct interface_port *ifport = p_m_mISDNport->ifport;
339         int i;
340
341         chan_trace_header(p_m_mISDNport, this, "CHANNEL SELECTION (setup)", DIRECTION_NONE);
342         if (exclusive<0)
343                 exclusive = 0;
344         if (channel == CHANNEL_NO)
345                 add_trace("channel", "request", "no-channel");
346         else
347                 add_trace("channel", "request", (channel>0)?"%d%s":"any", channel, exclusive?" (forced)":"");
348         if (channel==CHANNEL_NO && p_type==PORT_TYPE_DSS1_TE_IN)
349         {
350                 add_trace("conclusion", NULL, "incoming call-waiting not supported for TE-mode");
351                 end_trace();
352                 return(-6); // channel unacceptable
353         }
354         if (channel <= 0) /* not given, no channel, whatever.. */
355                 channel = CHANNEL_ANY; /* any channel */
356         if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) // of out chan..
357         {
358                 add_trace("channel", "reserved", "%d", p_m_mISDNport->b_reserved);
359                 add_trace("conclusion", NULL, "all channels are reserved");
360                 end_trace();
361                 return(-34); // no channel
362         }
363         if (channel == CHANNEL_ANY)
364                 goto get_from_list;
365         if (channel > 0)
366         {
367                 /* check for given channel in selection list */
368                 selchannel = ifport->in_channel;
369                 while(selchannel)
370                 {
371                         if (selchannel->channel == channel || selchannel->channel == CHANNEL_FREE)
372                                 break;
373                         selchannel = selchannel->next;
374                 }
375                 if (!selchannel)
376                         channel = 0;
377
378                 /* exclusive channel requests must be in the list */
379                 if (exclusive)
380                 {
381                         if (!channel)
382                         {
383                                 add_trace("conclusion", NULL, "exclusively requested channel not in list");
384                                 end_trace();
385                                 return(-6); // channel unacceptable
386                         }
387                         i = selchannel->channel-1-(selchannel->channel>=17);
388                         if (p_m_mISDNport->b_port[i] == NULL)
389                                 goto use_channel;
390                         add_trace("conclusion", NULL, "exclusively requested channel is busy");
391                         end_trace();
392                         return(-6); // channel unacceptable
393                 }
394
395                 /* requested channels in list will be used */
396                 if (channel)
397                 {
398                         i = selchannel->channel-1-(selchannel->channel>=17);
399                         if (p_m_mISDNport->b_port[i] == NULL)
400                                 goto use_channel;
401                 }
402
403                 /* if channel is not available or not in list, it must be searched */
404                 get_from_list:
405                 /* check for first free channel in list */
406                 channel = 0;
407                 selchannel = ifport->in_channel;
408                 while(selchannel)
409                 {
410                         switch(selchannel->channel)
411                         {
412                                 case CHANNEL_FREE: /* free channel */
413                                 add_trace("hunting", "channel", "free");
414                                 if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num)
415                                         break; /* all channel in use or reserverd */
416                                 /* find channel */
417                                 i = 0;
418                                 while(i < p_m_mISDNport->b_num)
419                                 {
420                                         if (p_m_mISDNport->b_port[i] == NULL)
421                                         {
422                                                 channel = i+1+(i>=15);
423                                                 break;
424                                         }
425                                         i++;
426                                 }
427                                 break;
428
429                                 default:
430                                 add_trace("hunting", "channel", "%d", selchannel->channel);
431                                 if (selchannel->channel<1 || selchannel->channel==16)
432                                         break; /* invalid channels */
433                                 i = selchannel->channel-1-(selchannel->channel>=17);
434                                 if (i >= p_m_mISDNport->b_num)
435                                         break; /* channel not in port */
436                                 if (p_m_mISDNport->b_port[i] == NULL)
437                                 {
438                                         channel = selchannel->channel;
439                                         break;
440                                 }
441                                 break;
442                         }
443                         if (channel)
444                                 break; /* found channel */
445                         selchannel = selchannel->next;
446                 }
447                 if (!channel)
448                 {
449                         add_trace("conclusion", NULL, "no channel available");
450                         end_trace();
451                         return(-6); // channel unacceptable
452                 }
453         }
454 use_channel:
455         add_trace("conclusion", NULL, "channel available");
456         add_trace("connect", "channel", "%d", channel);
457         end_trace();
458         return(channel);
459 }
460
461 /*
462  * handles all indications
463  */
464 /* CC_SETUP INDICATION */
465 void Pdss1::setup_ind(unsigned long prim, unsigned long dinfo, void *data)
466 {
467         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
468         SETUP_t *setup = (SETUP_t *)((unsigned long)data + headerlen);
469         int calling_type, calling_plan, calling_present, calling_screen;
470         int called_type, called_plan;
471         int redir_type, redir_plan, redir_present, redir_screen, redir_reason;
472         int hlc_coding, hlc_presentation, hlc_interpretation, hlc_hlc, hlc_exthlc;
473         int bearer_coding, bearer_capability, bearer_mode, bearer_rate, bearer_multi, bearer_user;
474         int exclusive, channel;
475         int ret;
476         msg_t *dmsg;
477         unsigned char keypad[32] = "";
478         unsigned char useruser[128];
479         int useruser_len = 0, useruser_protocol;
480         class Endpoint *epoint;
481         struct message *message;
482
483         /* callref from nt-lib */
484         if (p_m_d_ntmode)
485         {
486                 /* nt-library now gives us the id via CC_SETUP */
487                 if (dinfo&(~0xff) == 0xff00)
488                         FATAL("l3-stack gives us a process id 0xff00-0xffff\n");
489                 l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
490                 if (p_m_d_l3id)
491                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
492                 add_trace("callref", "new", "0x%x", dinfo);
493                 end_trace();
494                 if (p_m_d_l3id&(~0xff) == 0xff00)
495                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
496                 p_m_d_l3id = dinfo;
497                 p_m_d_ces = setup->ces;
498         }
499
500         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
501         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));
502         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));
503         dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)keypad, sizeof(keypad));
504 #ifdef CENTREX
505         /* te-mode: CNIP (calling name identification presentation) */
506         if (!p_m_d_ntmode)
507                 dec_facility_centrex(setup->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)p_callerinfo.name, sizeof(p_callerinfo.name));
508 #endif
509         dec_ie_useruser(setup->USER_USER, (Q931_info_t *)((unsigned long)data+headerlen), &useruser_protocol, useruser, &useruser_len);
510         dec_ie_complete(setup->COMPLETE, (Q931_info_t *)((unsigned long)data+headerlen), &p_dialinginfo.sending_complete);
511         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));
512         dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
513         dec_ie_hlc(setup->HLC, (Q931_info_t *)((unsigned long)data+headerlen), &hlc_coding, &hlc_interpretation, &hlc_presentation, &hlc_hlc, &hlc_exthlc);
514         dec_ie_bearer(setup->BEARER, (Q931_info_t *)((unsigned long)data+headerlen), &bearer_coding, &bearer_capability, &bearer_mode, &bearer_rate, &bearer_multi, &bearer_user);
515         end_trace();
516
517         /* if blocked, release call */
518         if (p_m_mISDNport->ifport->block)
519         {
520                 RELEASE_COMPLETE_t *release_complete;
521
522                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
523                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
524                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
525                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 27); /* temporary unavailable */
526                 add_trace("reason", NULL, "port blocked");
527                 end_trace();
528                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
529                 new_state(PORT_STATE_RELEASE);
530                 p_m_delete = 1;
531                 return;
532         }
533
534         /* caller info */
535         switch (calling_present)
536         {
537                 case 1:
538                 p_callerinfo.present = INFO_PRESENT_RESTRICTED;
539                 break;
540                 case 2:
541                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
542                 break;
543                 default:
544                 p_callerinfo.present = INFO_PRESENT_ALLOWED;
545                 break;
546         }
547         switch (calling_screen)
548         {
549                 case 0:
550                 p_callerinfo.screen = INFO_SCREEN_USER;
551                 break;
552                 default:
553                 p_callerinfo.screen = INFO_SCREEN_NETWORK;
554                 break;
555         }
556         switch (calling_type)
557         {
558                 case -1:
559                 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
560                 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
561                 p_callerinfo.screen = INFO_SCREEN_NETWORK;
562                 break;
563                 case 0x1:
564                 p_callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
565                 break;
566                 case 0x2:
567                 p_callerinfo.ntype = INFO_NTYPE_NATIONAL;
568                 break;
569                 case 0x4:
570                 p_callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
571                 break;
572                 default:
573                 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
574                 break;
575         }
576         p_callerinfo.isdn_port = p_m_portnum;
577         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
578
579         /* dialing information */
580         SCAT(p_dialinginfo.id, (char *)keypad);
581         switch (called_type)
582         {
583                 case 0x1:
584                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
585                 break;
586                 case 0x2:
587                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
588                 break;
589                 case 0x4:
590                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
591                 break;
592                 default:
593                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
594                 break;
595         }
596
597         /* redir info */
598         switch (redir_present)
599         {
600                 case 1:
601                 p_redirinfo.present = INFO_PRESENT_RESTRICTED;
602                 break;
603                 case 2:
604                 p_redirinfo.present = INFO_PRESENT_NOTAVAIL;
605                 break;
606                 default:
607                 p_redirinfo.present = INFO_PRESENT_ALLOWED;
608                 break;
609         }
610         switch (redir_screen)
611         {
612                 case 0:
613                 p_redirinfo.screen = INFO_SCREEN_USER;
614                 break;
615                 default:
616                 p_redirinfo.screen = INFO_SCREEN_NETWORK;
617                 break;
618         }
619         switch (redir_reason)
620         {
621                 case 1:
622                 p_redirinfo.reason = INFO_REDIR_BUSY;
623                 break;
624                 case 2:
625                 p_redirinfo.reason = INFO_REDIR_NORESPONSE;
626                 break;
627                 case 15:
628                 p_redirinfo.reason = INFO_REDIR_UNCONDITIONAL;
629                 break;
630                 case 10:
631                 p_redirinfo.reason = INFO_REDIR_CALLDEFLECT;
632                 break;
633                 case 9:
634                 p_redirinfo.reason = INFO_REDIR_OUTOFORDER;
635                 break;
636                 default:
637                 p_redirinfo.reason = INFO_REDIR_UNKNOWN;
638                 break;
639         }
640         switch (redir_type)
641         {
642                 case -1:
643                 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
644                 p_redirinfo.present = INFO_PRESENT_NULL; /* not redirecting */
645                 p_redirinfo.reason = INFO_REDIR_UNKNOWN;
646                 break;
647                 case 0x1:
648                 p_redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
649                 break;
650                 case 0x2:
651                 p_redirinfo.ntype = INFO_NTYPE_NATIONAL;
652                 break;
653                 case 0x4:
654                 p_redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
655                 break;
656                 default:
657                 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
658                 break;
659         }
660         p_redirinfo.isdn_port = p_m_portnum;
661
662         /* bearer capability */
663         switch (bearer_capability)
664         {
665                 case -1:
666                 p_capainfo.bearer_capa = INFO_BC_AUDIO;
667                 bearer_user = (options.law=='a')?3:2;
668                 break;
669                 default:
670                 p_capainfo.bearer_capa = bearer_capability;
671                 break;
672         }
673         switch (bearer_mode)
674         {
675                 case 2:
676                 p_capainfo.bearer_mode = INFO_BMODE_PACKET;
677                 break;
678                 default:
679                 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
680                 break;
681         }
682         switch (bearer_user)
683         {
684                 case -1:
685                 p_capainfo.bearer_info1 = INFO_INFO1_NONE;
686                 break;
687                 default:
688                 p_capainfo.bearer_info1 = bearer_user + 0x80;
689                 break;
690         }
691
692         /* hlc */
693         switch (hlc_hlc)
694         {
695                 case -1:
696                 p_capainfo.hlc = INFO_HLC_NONE;
697                 break;
698                 default:
699                 p_capainfo.hlc = hlc_hlc + 0x80;
700                 break;
701         }
702         switch (hlc_exthlc)
703         {
704                 case -1:
705                 p_capainfo.exthlc = INFO_HLC_NONE;
706                 break;
707                 default:
708                 p_capainfo.exthlc = hlc_exthlc + 0x80;
709                 break;
710         }
711
712         /* hunt channel */
713         ret = channel = hunt_bchannel(channel, exclusive);
714         if (ret < 0)
715                 goto no_channel;
716
717         /* open channel */
718         ret = seize_bchannel(channel, 1);
719         if (ret < 0)
720         {
721                 no_channel:
722                 RELEASE_COMPLETE_t *release_complete;
723
724                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
725                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
726                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
727                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
728                 end_trace();
729                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
730                 new_state(PORT_STATE_RELEASE);
731                 p_m_delete = 1;
732                 return;
733         }
734         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
735
736         /* create endpoint */
737         if (p_epointlist)
738                 FATAL("Incoming call but already got an endpoint.\n");
739         if (!(epoint = new Endpoint(p_serial, 0, 0)))
740                 FATAL("No memory for Endpoint instance\n");
741         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint)))
742                 FATAL("No memory for Endpoint Application instance\n");
743         epointlist_new(epoint->ep_serial);
744
745         /* send setup message to endpoit */
746         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
747         message->param.setup.isdn_port = p_m_portnum;
748         message->param.setup.port_type = p_type;
749         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
750         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
751         memcpy(&message->param.setup.redirinfo, &p_redirinfo, sizeof(struct redir_info));
752         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
753         memcpy(message->param.setup.useruser.data, &useruser, useruser_len);
754         message->param.setup.useruser.len = useruser_len;
755         message->param.setup.useruser.protocol = useruser_protocol;
756         message_put(message);
757
758         new_state(PORT_STATE_IN_SETUP);
759 }
760
761 /* CC_INFORMATION INDICATION */
762 void Pdss1::information_ind(unsigned long prim, unsigned long dinfo, void *data)
763 {
764         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
765         INFORMATION_t *information = (INFORMATION_t *)((unsigned long)data + headerlen);
766         int type, plan;
767         unsigned char keypad[32] = "";
768         struct message *message;
769
770         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
771         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));
772         dec_ie_keypad(information->KEYPAD, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)keypad, sizeof(keypad));
773         dec_ie_complete(information->COMPLETE, (Q931_info_t *)((unsigned long)data+headerlen), &p_dialinginfo.sending_complete);
774         end_trace();
775
776         SCAT(p_dialinginfo.id, (char *)keypad);
777         switch (type)
778         {
779                 case 0x1:
780                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
781                 break;
782                 case 0x2:
783                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
784                 break;
785                 case 0x4:
786                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
787                 break;
788                 default:
789                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
790                 break;
791         }
792         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
793         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
794         message_put(message);
795         /* reset overlap timeout */
796         new_state(p_state);
797 }
798
799 /* CC_SETUP_ACCNOWLEDGE INDICATION */
800 void Pdss1::setup_acknowledge_ind(unsigned long prim, unsigned long dinfo, void *data)
801 {
802         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
803         SETUP_ACKNOWLEDGE_t *setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)((unsigned long)data + headerlen);
804         int exclusive, channel;
805         int coding, location, progress;
806         int ret;
807         struct message *message;
808
809         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
810         dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
811         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
812         end_trace();
813
814         /* process channel */
815         ret = received_first_reply_to_setup(prim, exclusive, channel);
816         if (ret < 0)
817         {
818                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
819                 message->param.disconnectinfo.cause = -ret;
820                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
821                 message_put(message);
822                 new_state(PORT_STATE_RELEASE);
823                 p_m_delete = 1;
824                 return;
825         }
826
827         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_OVERLAP);
828         message_put(message);
829
830         new_state(PORT_STATE_OUT_OVERLAP);
831 }
832
833 /* CC_PROCEEDING INDICATION */
834 void Pdss1::proceeding_ind(unsigned long prim, unsigned long dinfo, void *data)
835 {
836         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
837         CALL_PROCEEDING_t *proceeding = (CALL_PROCEEDING_t *)((unsigned long)data + headerlen);
838         int exclusive, channel;
839         int coding, location, progress;
840         int ret;
841         struct message *message;
842         int notify = -1, type, plan, present;
843         char redir[32];
844
845         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
846         dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
847         dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
848         dec_ie_notify(NULL/*proceeding->NOTIFY*/, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
849         dec_ie_redir_dn(proceeding->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)redir, sizeof(redir));
850         end_trace();
851
852         ret = received_first_reply_to_setup(prim, exclusive, channel);
853         if (ret < 0)
854         {
855                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
856                 message->param.disconnectinfo.cause = -ret;
857                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
858                 message_put(message);
859                 new_state(PORT_STATE_RELEASE);
860                 p_m_delete = 1;
861                 return;
862         }
863         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
864         message_put(message);
865
866         new_state(PORT_STATE_OUT_PROCEEDING);
867         
868         if (notify >= 0)
869                 notify |= 0x80;
870         else
871                 notify = 0;
872         if (type >= 0 || notify)
873         {
874                 if (!notify && type >= 0)
875                         notify = 251;
876                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
877                 message->param.notifyinfo.notify = notify;
878                 SCPY(message->param.notifyinfo.id, redir);
879                 /* redirection number */
880                 switch (present)
881                 {
882                         case 1:
883                         message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
884                         break;
885                         case 2:
886                         message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
887                         break;
888                         default:
889                         message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
890                         break;
891                 }
892                 switch (type)
893                 {
894                         case -1:
895                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
896                         message->param.notifyinfo.present = INFO_PRESENT_NULL;
897                         break;
898                         case 1:
899                         message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
900                         break;
901                         case 2:
902                         message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
903                         break;
904                         case 4:
905                         message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
906                         break;
907                         default:
908                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
909                         break;
910                 }
911                 message->param.notifyinfo.isdn_port = p_m_portnum;
912                 message_put(message);
913         }
914 }
915
916 /* CC_ALERTING INDICATION */
917 void Pdss1::alerting_ind(unsigned long prim, unsigned long dinfo, void *data)
918 {
919         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
920         ALERTING_t *alerting = (ALERTING_t *)((unsigned long)data + headerlen);
921         int exclusive, channel;
922         int coding, location, progress;
923         int ret;
924         struct message *message;
925         int notify = -1, type, plan, present;
926         char redir[32];
927
928         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
929         dec_ie_channel_id(alerting->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
930         dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
931         dec_ie_notify(NULL/*alerting->NOTIFY*/, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
932         dec_ie_redir_dn(alerting->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)redir, sizeof(redir));
933         end_trace();
934
935         /* process channel */
936         ret = received_first_reply_to_setup(prim, exclusive, channel);
937         if (ret < 0)
938         {
939                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
940                 message->param.disconnectinfo.cause = -ret;
941                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
942                 message_put(message);
943                 new_state(PORT_STATE_RELEASE);
944                 p_m_delete = 1;
945                 return;
946         }
947         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
948         message_put(message);
949
950         new_state(PORT_STATE_OUT_ALERTING);
951
952         if (notify >= 0)
953                 notify |= 0x80;
954         else
955                 notify = 0;
956         if (type >= 0 || notify)
957         {
958                 if (!notify && type >= 0)
959                         notify = 251;
960                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
961                 message->param.notifyinfo.notify = notify;
962                 SCPY(message->param.notifyinfo.id, redir);
963                 switch (present)
964                 {
965                         case 1:
966                         message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
967                         break;
968                         case 2:
969                         message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
970                         break;
971                         default:
972                         message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
973                         break;
974                 }
975                 switch (type)
976                 {
977                         case -1:
978                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
979                         message->param.notifyinfo.present = INFO_PRESENT_NULL;
980                         break;
981                         case 1:
982                         message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
983                         break;
984                         case 2:
985                         message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
986                         break;
987                         case 4:
988                         message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
989                         break;
990                         default:
991                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
992                         break;
993                 }
994                 message->param.notifyinfo.isdn_port = p_m_portnum;
995                 message_put(message);
996         }
997 }
998
999 /* CC_CONNECT INDICATION */
1000 void Pdss1::connect_ind(unsigned long prim, unsigned long dinfo, void *data)
1001 {
1002         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1003         CONNECT_t *connect = (CONNECT_t *)((unsigned long)data + headerlen);
1004         int exclusive, channel;
1005         int type, plan, present, screen;
1006         int ret;
1007         msg_t *dmsg;
1008         struct message *message;
1009         int bchannel_before;
1010         CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
1011
1012         if (p_m_d_ntmode)
1013                 p_m_d_ces = connect->ces;
1014
1015         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1016         dec_ie_channel_id(connect->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1017         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));
1018 #ifdef CENTREX
1019         /* te-mode: CONP (connected name identification presentation) */
1020         if (!p_m_d_ntmode)
1021                 dec_facility_centrex(connect->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)p_connectinfo.name, sizeof(p_connectinfo.name));
1022 #endif
1023         end_trace();
1024
1025         /* select channel */
1026         bchannel_before = p_m_b_channel;
1027         ret = received_first_reply_to_setup(prim, exclusive, channel);
1028         if (ret < 0)
1029         {
1030                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1031                 message->param.disconnectinfo.cause = -ret;
1032                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1033                 message_put(message);
1034                 new_state(PORT_STATE_RELEASE);
1035                 p_m_delete = 1;
1036                 return;
1037         }
1038
1039         /* connect information */
1040         switch (present)
1041         {
1042                 case 1:
1043                 p_connectinfo.present = INFO_PRESENT_RESTRICTED;
1044                 break;
1045                 case 2:
1046                 p_connectinfo.present = INFO_PRESENT_NOTAVAIL;
1047                 break;
1048                 default:
1049                 p_connectinfo.present = INFO_PRESENT_ALLOWED;
1050                 break;
1051         }
1052         switch (screen)
1053         {
1054                 case 0:
1055                 p_connectinfo.screen = INFO_SCREEN_USER;
1056                 break;
1057                 default:
1058                 p_connectinfo.screen = INFO_SCREEN_NETWORK;
1059                 break;
1060         }
1061         switch (type)
1062         {
1063                 case 0x0:
1064                 p_connectinfo.present = INFO_PRESENT_NULL; /* no COLP info */
1065                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1066                 break;
1067                 case 0x1:
1068                 p_connectinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1069                 break;
1070                 case 0x2:
1071                 p_connectinfo.ntype = INFO_NTYPE_NATIONAL;
1072                 break;
1073                 case 0x4:
1074                 p_connectinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1075                 break;
1076                 default:
1077                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1078                 break;
1079         }
1080         p_connectinfo.isdn_port = p_m_portnum;
1081         SCPY(p_connectinfo.interface, p_m_mISDNport->ifport->interface->name);
1082
1083         /* only in nt-mode we send connect ack. in te-mode it is done by stack itself or optional */
1084         if (p_m_d_ntmode)
1085         {
1086                 /* send connect acknowledge */
1087                 dmsg = create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT, dinfo, sizeof(CONNECT_ACKNOWLEDGE_t), p_m_d_ntmode);
1088                 connect_acknowledge = (CONNECT_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1089                 l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | RESPONSE, DIRECTION_OUT);
1090                 /* if we had no bchannel before, we send it now */
1091                 if (!bchannel_before && p_m_b_channel)
1092                         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1093                 end_trace();
1094                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1095         }
1096         
1097         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1098         memcpy(&message->param.connectinfo, &p_connectinfo, sizeof(struct connect_info));
1099         message_put(message);
1100
1101         new_state(PORT_STATE_CONNECT);
1102 }
1103
1104 /* CC_DISCONNECT INDICATION */
1105 void Pdss1::disconnect_ind(unsigned long prim, unsigned long dinfo, void *data)
1106 {
1107         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1108         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1109         int location, cause;
1110         int coding, proglocation, progress;
1111         struct message *message;
1112
1113         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1114         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &proglocation, &progress);
1115         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1116         end_trace();
1117         if (location == LOCATION_PRIVATE_LOCAL)
1118                 location = LOCATION_PRIVATE_REMOTE;
1119
1120         if (cause < 0)
1121                 cause = 16;
1122
1123         /* release if we are remote sends us no tones */
1124         if (p_m_mISDNport->earlyb)
1125         {
1126                 RELEASE_t *release;
1127                 msg_t *dmsg;
1128
1129                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, dinfo, sizeof(RELEASE_t), p_m_d_ntmode);
1130                 release = (RELEASE_t *)(dmsg->data + headerlen);
1131                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
1132                 enc_ie_cause(&release->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16); /* normal */
1133                 add_trace("reason", NULL, "no remote patterns");
1134                 end_trace();
1135                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1136
1137                 /* sending release to endpoint */
1138                 while(p_epointlist)
1139                 {
1140                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1141                         message->param.disconnectinfo.cause = cause;
1142                         message->param.disconnectinfo.location = location;
1143                         message_put(message);
1144                         /* remove epoint */
1145                         free_epointlist(p_epointlist);
1146                 }
1147                 new_state(PORT_STATE_RELEASE);
1148                 p_m_delete = 1;
1149                 return;
1150         }
1151
1152         /* sending disconnect to active endpoint and release to inactive endpoints */
1153         if (ACTIVE_EPOINT(p_epointlist))
1154         {
1155                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
1156                 message->param.disconnectinfo.location = location;
1157                 message->param.disconnectinfo.cause = cause;
1158                 message_put(message);
1159         }
1160         while(INACTIVE_EPOINT(p_epointlist))
1161         {
1162                 message = message_create(p_serial, INACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1163                 message->param.disconnectinfo.location = location;
1164                 message->param.disconnectinfo.cause = cause;
1165                 message_put(message);
1166                 /* remove epoint */
1167                 free_epointid(INACTIVE_EPOINT(p_epointlist));
1168         }
1169         new_state(PORT_STATE_IN_DISCONNECT);
1170 }
1171
1172 /* CC_DISCONNECT INDICATION */
1173 void Pdss1::disconnect_ind_i(unsigned long prim, unsigned long dinfo, void *data)
1174 {
1175         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1176         DISCONNECT_t *disconnect = (DISCONNECT_t *)((unsigned long)data + headerlen);
1177         int location, cause;
1178
1179         /* cause */
1180         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1181         if (p_m_d_collect_cause > 0)
1182         {
1183                 add_trace("old-cause", "location", "%d", p_m_d_collect_location);
1184                 add_trace("old-cause", "value", "%d", p_m_d_collect_cause);
1185         }
1186         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1187         end_trace();
1188         if (location == LOCATION_PRIVATE_LOCAL)
1189                 location = LOCATION_PRIVATE_REMOTE;
1190
1191         /* collect cause */
1192         collect_cause(&p_m_d_collect_cause, &p_m_d_collect_location, cause, location);
1193         add_trace("new-cause", "location", "%d", p_m_d_collect_location);
1194         add_trace("new-cause", "value", "%d", p_m_d_collect_cause);
1195
1196 }
1197
1198 /* CC_RELEASE INDICATION */
1199 void Pdss1::release_ind(unsigned long prim, unsigned long dinfo, void *data)
1200 {
1201         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1202         RELEASE_t *release = (RELEASE_t *)((unsigned long)data + headerlen);
1203         msg_t *dmsg;
1204         int location, cause;
1205         struct message *message;
1206
1207         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1208         dec_ie_cause(release->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1209         end_trace();
1210         if (location == LOCATION_PRIVATE_LOCAL)
1211                 location = LOCATION_PRIVATE_REMOTE;
1212
1213         if (cause < 0)
1214                 cause = 16;
1215
1216         /* sending release to endpoint */
1217         while(p_epointlist)
1218         {
1219                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1220                 message->param.disconnectinfo.cause = cause;
1221                 message->param.disconnectinfo.location = location;
1222                 message_put(message);
1223                 /* remove epoint */
1224                 free_epointlist(p_epointlist);
1225         }
1226
1227         /* only in NT mode we must send release_complete, if we got a release confirm */
1228         if (prim == (CC_RELEASE | CONFIRM))
1229         {
1230                 /* sending release complete */
1231                 RELEASE_COMPLETE_t *release_complete;
1232
1233                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
1234                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
1235                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
1236                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 16);
1237                 end_trace();
1238                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1239         }
1240
1241         new_state(PORT_STATE_RELEASE);
1242         p_m_delete = 1;
1243 }
1244
1245 /* CC_RELEASE_COMPLETE INDICATION */
1246 void Pdss1::release_complete_ind(unsigned long prim, unsigned long dinfo, void *data)
1247 {
1248         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1249         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *)((unsigned long)data + headerlen);
1250         int location, cause;
1251         struct message *message;
1252
1253         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1254         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)((unsigned long)data+headerlen), &location, &cause);
1255         end_trace();
1256         if (location == LOCATION_PRIVATE_LOCAL)
1257                 location = LOCATION_PRIVATE_REMOTE;
1258
1259         if (cause < 0)
1260                 cause = 16;
1261
1262         /* sending release to endpoint */
1263         while(p_epointlist)
1264         {
1265                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1266                 message->param.disconnectinfo.cause = cause;
1267                 message->param.disconnectinfo.location = location;
1268                 message_put(message);
1269                 /* remove epoint */
1270                 free_epointlist(p_epointlist);
1271         }
1272
1273         new_state(PORT_STATE_RELEASE);
1274         p_m_delete = 1;
1275 }
1276
1277 /* T312 timeout  */
1278 void Pdss1::t312_timeout(unsigned long prim, unsigned long dinfo, void *data)
1279 {
1280         struct message *message;
1281
1282         // trace is done at message_isdn()
1283         
1284         /* sending release to endpoint */
1285         while(p_epointlist)
1286         {
1287                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1288                 message->param.disconnectinfo.cause = p_m_d_collect_cause;
1289                 message->param.disconnectinfo.location = p_m_d_collect_location;
1290                 message_put(message);
1291                 /* remove epoint */
1292                 free_epointlist(p_epointlist);
1293         }
1294
1295         new_state(PORT_STATE_RELEASE);
1296         p_m_delete = 1;
1297 }
1298
1299 /* CC_NOTIFY INDICATION */
1300 void Pdss1::notify_ind(unsigned long prim, unsigned long dinfo, void *data)
1301 {
1302         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1303         NOTIFY_t *notifying = (NOTIFY_t *)((unsigned long)data + headerlen);
1304         struct message *message;
1305         int notify, type, plan, present;
1306
1307         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1308         dec_ie_notify(notifying->NOTIFY, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
1309         dec_ie_redir_dn(notifying->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)message->param.notifyinfo.id, sizeof(message->param.notifyinfo.id));
1310         end_trace();
1311
1312         if (!ACTIVE_EPOINT(p_epointlist))
1313                 return;
1314         /* notification indicator */
1315         if (notify < 0)
1316                 return;
1317         notify |= 0x80;
1318         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1319         message->param.notifyinfo.notify = notify;
1320         /* redirection number */
1321         switch (present)
1322         {
1323                 case 1:
1324                 message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1325                 break;
1326                 case 2:
1327                 message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1328                 break;
1329                 default:
1330                 message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1331                 break;
1332         }
1333         switch (type)
1334         {
1335                 case -1:
1336                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1337                 message->param.notifyinfo.present = INFO_PRESENT_NULL;
1338                 break;
1339                 case 1:
1340                 message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1341                 break;
1342                 case 2:
1343                 message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1344                 break;
1345                 case 4:
1346                 message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1347                 break;
1348                 default:
1349                 message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1350                 break;
1351         }
1352         message->param.notifyinfo.isdn_port = p_m_portnum;
1353         message_put(message);
1354 }
1355
1356
1357 /* CC_HOLD INDICATION */
1358 void Pdss1::hold_ind(unsigned long prim, unsigned long dinfo, void *data)
1359 {
1360         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1361 //      HOLD_t *hold = (HOLD_t *)((unsigned long)data + headerlen);
1362         struct message *message;
1363         HOLD_REJECT_t *hold_reject;
1364         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
1365         msg_t *dmsg;
1366 //      class Endpoint *epoint;
1367
1368         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1369         end_trace();
1370
1371         if (!ACTIVE_EPOINT(p_epointlist) || p_m_hold)
1372         {
1373                 dmsg = create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT, dinfo, sizeof(HOLD_REJECT_t), p_m_d_ntmode);
1374                 hold_reject = (HOLD_REJECT_t *)(dmsg->data + headerlen);
1375                 l1l2l3_trace_header(p_m_mISDNport, this, CC_HOLD_REJECT | REQUEST, DIRECTION_OUT);
1376                 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 */
1377                 add_trace("reason", NULL, "no endpoint");
1378                 end_trace();
1379                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1380
1381                 return;
1382         }
1383
1384         /* notify the hold of call */
1385         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1386         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1387         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1388         message_put(message);
1389
1390         /* deactivate bchannel */
1391         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (hold)", DIRECTION_NONE);
1392         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1393         end_trace();
1394         drop_bchannel();
1395
1396         /* set hold state */
1397         p_m_hold = 1;
1398 #if 0
1399         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1400         if (epoint && p_m_d_ntmode)
1401         {
1402                 p_m_timeout = p_settings.tout_hold;
1403                 time(&p_m_timer);
1404         }
1405 #endif
1406
1407         /* acknowledge hold */
1408         dmsg = create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE, dinfo, sizeof(HOLD_ACKNOWLEDGE_t), p_m_d_ntmode);
1409         hold_acknowledge = (HOLD_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1410         l1l2l3_trace_header(p_m_mISDNport, this, CC_HOLD_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1411         end_trace();
1412         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1413 }
1414
1415
1416 /* CC_RETRIEVE INDICATION */
1417 void Pdss1::retrieve_ind(unsigned long prim, unsigned long dinfo, void *data)
1418 {
1419         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1420         RETRIEVE_t *retrieve = (RETRIEVE_t *)((unsigned long)data + headerlen);
1421         RETRIEVE_REJECT_t *retrieve_reject;
1422         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1423         struct message *message;
1424         int channel, exclusive, cause;
1425         msg_t *dmsg;
1426         int ret;
1427
1428         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1429         dec_ie_channel_id(retrieve->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1430         end_trace();
1431
1432         if (!p_m_hold)
1433         {
1434                 cause = 101; /* incompatible state */
1435                 reject:
1436
1437                 dmsg = create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT, dinfo, sizeof(RETRIEVE_REJECT_t), p_m_d_ntmode);
1438                 retrieve_reject = (RETRIEVE_REJECT_t *)(dmsg->data + headerlen);
1439                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RETRIEVE_REJECT | REQUEST, DIRECTION_OUT);
1440                 enc_ie_cause(&retrieve_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, cause);
1441                 end_trace();
1442                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1443
1444                 return;
1445         }
1446
1447         /* notify the retrieve of call */
1448         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1449         message->param.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1450         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1451         message_put(message);
1452
1453         /* hunt channel */
1454         ret = channel = hunt_bchannel(channel, exclusive);
1455         if (ret < 0)
1456                 goto no_channel;
1457
1458         /* open channel */
1459         ret = seize_bchannel(channel, 1);
1460         if (ret < 0)
1461         {
1462                 no_channel:
1463                 cause = -ret;
1464                 goto reject;
1465         }
1466         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
1467
1468         /* set hold state */
1469         p_m_hold = 0;
1470         p_m_timeout = 0;
1471
1472         /* acknowledge retrieve */
1473         dmsg = create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE, dinfo, sizeof(RETRIEVE_ACKNOWLEDGE_t), p_m_d_ntmode);
1474         retrieve_acknowledge = (RETRIEVE_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1475         l1l2l3_trace_header(p_m_mISDNport, this, CC_RETRIEVE_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1476         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1477         end_trace();
1478         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1479 }
1480
1481 /* CC_SUSPEND INDICATION */
1482 void Pdss1::suspend_ind(unsigned long prim, unsigned long dinfo, void *data)
1483 {
1484         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1485         SUSPEND_t *suspend = (SUSPEND_t *)((unsigned long)data + headerlen);
1486         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
1487         SUSPEND_REJECT_t *suspend_reject;
1488         struct message *message;
1489         class Endpoint *epoint;
1490         unsigned char callid[8];
1491         int len;
1492         msg_t *dmsg;
1493         int ret = -31; /* normal, unspecified */
1494
1495         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1496         dec_ie_call_id(suspend->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1497         end_trace();
1498
1499         if (!ACTIVE_EPOINT(p_epointlist))
1500         {
1501                 reject:
1502                 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, dinfo, sizeof(SUSPEND_REJECT_t), p_m_d_ntmode);
1503                 suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + headerlen);
1504                 l1l2l3_trace_header(p_m_mISDNport, this, CC_SUSPEND_REJECT | REQUEST, DIRECTION_OUT);
1505                 enc_ie_cause(&suspend_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1506                 end_trace();
1507                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1508
1509                 return;
1510         }
1511
1512         /* call id */
1513         if (len<0) len = 0;
1514
1515         /* check if call id is in use */
1516         epoint = epoint_first;
1517         while(epoint)
1518         {
1519                 if (epoint->ep_park)
1520                 {
1521                         if (epoint->ep_park_len == len)
1522                         if (!memcmp(epoint->ep_park_callid, callid, len))
1523                         {
1524                                 ret = -84; /* call id in use */
1525                                 goto reject;
1526                         }
1527                 }
1528                 epoint = epoint->next;
1529         }
1530
1531         /* notify the hold of call */
1532         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1533         message->param.notifyinfo.notify = INFO_NOTIFY_USER_SUSPENDED;
1534         message->param.notifyinfo.local = 1; /* call is held by supplementary service */
1535         message_put(message);
1536
1537         /* deactivate bchannel */
1538         chan_trace_header(p_m_mISDNport, this, "CHANNEL RELEASE (suspend)", DIRECTION_NONE);
1539         add_trace("disconnect", "channel", "%d", p_m_b_channel);
1540         end_trace();
1541         drop_bchannel();
1542
1543         /* sending suspend to endpoint */
1544         while (p_epointlist)
1545         {
1546                 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_SUSPEND);
1547                 memcpy(message->param.parkinfo.callid, callid, sizeof(message->param.parkinfo.callid));
1548                 message->param.parkinfo.len = len;
1549                 message_put(message);
1550                 /* remove epoint */
1551                 free_epointlist(p_epointlist);
1552         }
1553
1554         /* sending SUSPEND_ACKNOWLEDGE */
1555         dmsg = create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE, dinfo, sizeof(SUSPEND_ACKNOWLEDGE_t), p_m_d_ntmode);
1556         suspend_acknowledge = (SUSPEND_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1557         l1l2l3_trace_header(p_m_mISDNport, this, CC_SUSPEND_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1558         end_trace();
1559         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1560
1561         new_state(PORT_STATE_RELEASE);
1562         p_m_delete = 1;
1563 }
1564
1565 /* CC_RESUME INDICATION */
1566 void Pdss1::resume_ind(unsigned long prim, unsigned long dinfo, void *data)
1567 {
1568         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1569         RESUME_t *resume = (RESUME_t *)((unsigned long)data + headerlen);
1570         RESUME_REJECT_t *resume_reject;
1571         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
1572         unsigned char callid[8];
1573         int len;
1574         int channel, exclusive;
1575         msg_t *dmsg;
1576         class Endpoint *epoint;
1577         struct message *message;
1578         int ret;
1579
1580         /* callref from nt-lib */
1581         if (p_m_d_ntmode)
1582         {
1583                 /* nt-library now gives us the id via CC_RESUME */
1584                 if (dinfo&(~0xff) == 0xff00)
1585                         FATAL("l3-stack gives us a process id 0xff00-0xffff\n");
1586                 l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1587                 if (p_m_d_l3id)
1588                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1589                 add_trace("callref", "new", "0x%x", dinfo);
1590                 end_trace();
1591                 if (p_m_d_l3id&(~0xff) == 0xff00)
1592                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1593                 p_m_d_l3id = dinfo;
1594                 p_m_d_ces = resume->ces;
1595         }
1596
1597         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1598         dec_ie_call_id(resume->CALL_ID, (Q931_info_t *)((unsigned long)data+headerlen), callid, &len);
1599         end_trace();
1600
1601         /* if blocked, release call */
1602         if (p_m_mISDNport->ifport->block)
1603         {
1604                 ret = -27;
1605                 goto reject;
1606         }
1607
1608         /* call id */
1609         if (len<0) len = 0;
1610
1611         /* channel_id (no channel is possible in message) */
1612         exclusive = 0;
1613         channel = -1; /* any channel */
1614
1615         /* hunt channel */
1616         ret = channel = hunt_bchannel(channel, exclusive);
1617         if (ret < 0)
1618                 goto no_channel;
1619
1620         /* open channel */
1621         ret = seize_bchannel(channel, 1);
1622         if (ret < 0)
1623         {
1624                 no_channel:
1625                 reject:
1626                 dmsg = create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT, dinfo, sizeof(RESUME_REJECT_t), p_m_d_ntmode);
1627                 resume_reject = (RESUME_REJECT_t *)(dmsg->data + headerlen);
1628                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RESUME_REJECT | REQUEST, DIRECTION_OUT);
1629                 enc_ie_cause(&resume_reject->CAUSE, dmsg, (p_m_mISDNport->locally)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
1630                 if (ret == -27)
1631                         add_trace("reason", NULL, "port blocked");
1632                 end_trace();
1633                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1634                 new_state(PORT_STATE_RELEASE);
1635                 p_m_delete = 1;
1636                 return;
1637         }
1638         bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_ACTIVATE);
1639
1640         /* create endpoint */
1641         if (p_epointlist)
1642                 FATAL("Incoming resume but already got an endpoint.\n");
1643         ret = -85; /* no call suspended */
1644         epoint = epoint_first;
1645         while(epoint)
1646         {
1647                 if (epoint->ep_park)
1648                 {
1649                         ret = -83; /* suspended call exists, but this not */
1650                         if (epoint->ep_park_len == len)
1651                         if (!memcmp(epoint->ep_park_callid, callid, len))
1652                                 break;
1653                 }
1654                 epoint = epoint->next;
1655         }
1656         if (!epoint)
1657                 goto reject;
1658
1659         epointlist_new(epoint->ep_serial);
1660         if (!(epoint->portlist_new(p_serial, p_type, p_m_mISDNport->earlyb)))
1661                 FATAL("No memory for portlist\n");
1662         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RESUME);
1663         message_put(message);
1664
1665         /* notify the resume of call */
1666         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1667         message->param.notifyinfo.notify = INFO_NOTIFY_USER_RESUMED;
1668         message->param.notifyinfo.local = 1; /* call is retrieved by supplementary service */
1669         message_put(message);
1670
1671         /* sending RESUME_ACKNOWLEDGE */
1672         dmsg = create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE, dinfo, sizeof(RESUME_ACKNOWLEDGE_t), p_m_d_ntmode);
1673         resume_acknowledge = (RESUME_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
1674         l1l2l3_trace_header(p_m_mISDNport, this, CC_RESUME_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
1675         enc_ie_channel_id(&resume_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
1676         end_trace();
1677         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
1678
1679         new_state(PORT_STATE_CONNECT);
1680 }
1681
1682
1683 /* CC_FACILITY INDICATION */
1684 void Pdss1::facility_ind(unsigned long prim, unsigned long dinfo, void *data)
1685 {
1686         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1687         FACILITY_t *facility = (FACILITY_t *)((unsigned long)data + headerlen);
1688         unsigned char facil[256];
1689         int facil_len;
1690         struct message *message;
1691
1692         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1693         dec_ie_facility(facility->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), facil, &facil_len);
1694         end_trace();
1695
1696         /* facility */
1697         if (facil_len<=0)
1698                 return;
1699
1700         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_FACILITY);
1701         message->param.facilityinfo.len = facil_len;
1702         memcpy(message->param.facilityinfo.data, facil, facil_len);
1703         message_put(message);
1704 }
1705
1706
1707 /*
1708  * handler for isdn connections
1709  * incoming information are parsed and sent via message to the endpoint
1710  */
1711 void Pdss1::message_isdn(unsigned long prim, unsigned long dinfo, void *data)
1712 {
1713         int new_l3id;
1714         int timer_hex=0;
1715
1716         switch (prim)
1717         {
1718                 case CC_TIMEOUT | INDICATION:
1719                 if (p_m_d_ntmode)
1720                 {
1721                         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1722                         timer_hex = *((int *)(((char *)data)+headerlen));
1723                 }
1724                 if (timer_hex==0x312 && p_m_d_ntmode)
1725                 {
1726                         l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1727                         add_trace("timer", NULL, "%x", timer_hex);
1728                         end_trace();
1729                         t312_timeout(prim, dinfo, data);
1730                 }
1731                 break;
1732
1733                 case CC_SETUP | INDICATION:
1734                 if (p_state != PORT_STATE_IDLE)
1735                         break;
1736                 setup_ind(prim, dinfo, data);
1737                 break;
1738
1739                 case CC_SETUP | CONFIRM:
1740                 if (p_m_d_ntmode)
1741                 {
1742                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | INDICATION, DIRECTION_IN);
1743                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1744                         /* nt-library now gives us a new id via CC_SETUP_CONFIRM */
1745                         if ((p_m_d_l3id&0xff00) != 0xff00)
1746                                 PERROR("    strange setup-procid 0x%x\n", p_m_d_l3id);
1747                         p_m_d_l3id = *((int *)(((u_char *)data)+ mISDNUSER_HEAD_SIZE));
1748                         add_trace("callref", "new", "0x%x", p_m_d_l3id);
1749                         end_trace();
1750                 }
1751                 break;
1752
1753                 case CC_INFORMATION | INDICATION:
1754                 information_ind(prim, dinfo, data);
1755                 break;
1756
1757                 case CC_SETUP_ACKNOWLEDGE | INDICATION:
1758                 if (p_state != PORT_STATE_OUT_SETUP)
1759                 {
1760                         PERROR("Pdss1(%s) received setup_acknowledge, but we are not in outgoing setup state, IGNORING.\n", p_name);
1761                         break;
1762                 }
1763                 setup_acknowledge_ind(prim, dinfo, data);
1764                 break;
1765
1766                 case CC_PROCEEDING | INDICATION:
1767                 if (p_state != PORT_STATE_OUT_SETUP
1768                  && p_state != PORT_STATE_OUT_OVERLAP)
1769                 {
1770                         PERROR("Pdss1(%s) received proceeding, but we are not in outgoing setup OR overlap state, IGNORING.\n", p_name);
1771                         break;
1772                 }
1773                 proceeding_ind(prim, dinfo, data);
1774                 break;
1775
1776                 case CC_ALERTING | INDICATION:
1777                 if (p_state != PORT_STATE_OUT_SETUP
1778                  && p_state != PORT_STATE_OUT_OVERLAP
1779                  && p_state != PORT_STATE_OUT_PROCEEDING)
1780                 {
1781                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding state, IGNORING.\n", p_name);
1782                         break;
1783                 }
1784                 alerting_ind(prim, dinfo, data);
1785                 break;
1786
1787                 case CC_CONNECT | INDICATION:
1788                 if (p_state != PORT_STATE_OUT_SETUP
1789                  && p_state != PORT_STATE_OUT_OVERLAP
1790                  && p_state != PORT_STATE_OUT_PROCEEDING
1791                  && p_state != PORT_STATE_OUT_ALERTING)
1792                 {
1793                         PERROR("Pdss1(%s) received alerting, but we are not in outgoing setup OR overlap OR proceeding OR ALERTING state, IGNORING.\n", p_name);
1794                         break;
1795                 }
1796                 connect_ind(prim, dinfo, data);
1797                 if (p_m_d_notify_pending)
1798                 {
1799                         /* send pending notify message during connect */
1800                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1801                         message_free(p_m_d_notify_pending);
1802                         p_m_d_notify_pending = NULL;
1803                 }
1804                 break;
1805
1806                 case CC_CONNECT_ACKNOWLEDGE | INDICATION:
1807                 case CC_CONNECT | CONFIRM:
1808                 if (p_state == PORT_STATE_CONNECT_WAITING)
1809                         new_state(PORT_STATE_CONNECT);
1810                 if (p_m_d_notify_pending)
1811                 {
1812                         /* send pending notify message during connect-ack */
1813                         message_notify(ACTIVE_EPOINT(p_epointlist), p_m_d_notify_pending->type, &p_m_d_notify_pending->param);
1814                         message_free(p_m_d_notify_pending);
1815                         p_m_d_notify_pending = NULL;
1816                 }
1817                 break;
1818
1819                 case CC_DISCONNECT | INDICATION:
1820                 disconnect_ind(prim, dinfo, data);
1821                 break;
1822
1823                 case CC_RELEASE | CONFIRM:
1824                 case CC_RELEASE | INDICATION:
1825                 release_ind(prim, dinfo, data);
1826                 break;
1827
1828                 case CC_RELEASE_COMPLETE | INDICATION:
1829                 release_complete_ind(prim, dinfo, data);
1830                 break;
1831
1832                 case CC_RELEASE_COMPLETE | CONFIRM:
1833                 break;
1834
1835                 case CC_NOTIFY | INDICATION:
1836                 notify_ind(prim, dinfo, data);
1837                 break;
1838
1839                 case CC_HOLD | INDICATION:
1840                 hold_ind(prim, dinfo, data);
1841                 break;
1842
1843                 case CC_RETRIEVE | INDICATION:
1844                 retrieve_ind(prim, dinfo, data);
1845                 break;
1846
1847                 case CC_SUSPEND | INDICATION:
1848                 suspend_ind(prim, dinfo, data);
1849                 break;
1850
1851                 case CC_RESUME | INDICATION:
1852                 resume_ind(prim, dinfo, data);
1853                 break;
1854
1855                 case CC_FACILITY | INDICATION:
1856                 facility_ind(prim, dinfo, data);
1857                 break;
1858
1859                 case CC_RELEASE_CR | INDICATION:
1860                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | INDICATION, DIRECTION_IN);
1861                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
1862                 end_trace();
1863                 if (p_m_d_ntmode)
1864                 {
1865                         if ((p_m_d_l3id&0xff00) == 0xff00)
1866                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1867                 }
1868                 p_m_d_l3id = 0;
1869                 p_m_delete = 1;
1870                 /* sending release to endpoint in case we still have an endpoint
1871                  * NOTE: this only happens if the stack releases due to layer1
1872                  * or layer2 breakdown. otherwhise a release is received first.
1873                  */
1874                 while(p_epointlist)
1875                 {
1876                         struct message *message;
1877                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1878                         message->param.disconnectinfo.cause = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_cause:CAUSE_UNSPECIFIED;
1879                         message->param.disconnectinfo.location = (p_m_d_collect_cause!=CAUSE_NOUSER)?p_m_d_collect_location:LOCATION_PRIVATE_LOCAL;
1880                         message_put(message);
1881                         /* remove epoint */
1882                         free_epointlist(p_epointlist);
1883
1884                         new_state(PORT_STATE_RELEASE);
1885                 }
1886                 break;
1887
1888                 case CC_NEW_CR | INDICATION:
1889                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1890                 if (p_m_d_l3id)
1891                         add_trace("callref", "old", "0x%x", p_m_d_l3id);
1892                 if (p_m_d_ntmode)
1893                 {
1894                         new_l3id = *((int *)(((u_char *)data+mISDNUSER_HEAD_SIZE)));
1895                         if (((new_l3id&0xff00)!=0xff00) && ((p_m_d_l3id&0xff00)==0xff00))
1896                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
1897                 } else
1898                 {
1899                         new_l3id = dinfo;
1900                 }
1901                 p_m_d_l3id = new_l3id;
1902                 add_trace("callref", "new", "0x%x", p_m_d_l3id);
1903                 end_trace();
1904                 break;
1905
1906                 default:
1907                 l1l2l3_trace_header(p_m_mISDNport, this, prim, DIRECTION_IN);
1908                 add_trace("unhandled", "prim", "0x%x", prim);
1909                 end_trace();
1910         }
1911 }
1912
1913 void Pdss1::new_state(int state)
1914 {
1915         class Endpoint *epoint;
1916
1917         /* set timeout */
1918         epoint = find_epoint_id(ACTIVE_EPOINT(p_epointlist));
1919         if (epoint && p_m_d_ntmode)
1920         {
1921                 if (state == PORT_STATE_IN_OVERLAP)
1922                 {
1923                         p_m_timeout = p_settings.tout_dialing;
1924                         time(&p_m_timer);
1925                 }
1926                 if (state != p_state)
1927                 {
1928                         if (state == PORT_STATE_IN_SETUP
1929                          || state == PORT_STATE_IN_OVERLAP)
1930                         {
1931                                 p_m_timeout = p_settings.tout_setup;
1932                                 time(&p_m_timer);
1933                         }
1934                         if (state == PORT_STATE_IN_PROCEEDING
1935                          || state == PORT_STATE_OUT_PROCEEDING)
1936                         {
1937                                 p_m_timeout = p_settings.tout_proceeding;
1938                                 time(&p_m_timer);
1939                         }
1940                         if (state == PORT_STATE_IN_ALERTING
1941                          || state == PORT_STATE_OUT_ALERTING)
1942                         {
1943                                 p_m_timeout = p_settings.tout_alerting;
1944                                 time(&p_m_timer);
1945                         }
1946                         if (state == PORT_STATE_CONNECT
1947                          || state == PORT_STATE_CONNECT_WAITING)
1948                         {
1949                                 p_m_timeout = 0;
1950                         }
1951                         if (state == PORT_STATE_IN_DISCONNECT
1952                          || state == PORT_STATE_OUT_DISCONNECT)
1953                         {
1954                                 p_m_timeout = p_settings.tout_disconnect;
1955                                 time(&p_m_timer);
1956                         }
1957                 }
1958         }
1959         
1960         Port::new_state(state);
1961 }
1962
1963
1964 /*
1965  * handler
1966  */
1967 int Pdss1::handler(void)
1968 {
1969         int ret;
1970
1971         if ((ret = PmISDN::handler()))
1972                 return(ret);
1973
1974         /* handle destruction */
1975         if (p_m_delete && p_m_d_l3id==0)
1976         {
1977                 delete this;
1978                 return(-1);
1979         }
1980
1981         return(0);
1982 }
1983
1984
1985 /*
1986  * handles all messages from endpoint
1987  */
1988 /* MESSAGE_INFORMATION */
1989 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
1990 {
1991         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1992         INFORMATION_t *information;
1993         msg_t *dmsg;
1994
1995         if (param->information.id[0]) /* only if we have something to dial */
1996         {
1997                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
1998                 information = (INFORMATION_t *)(dmsg->data + headerlen);
1999                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2000                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.id);
2001                 end_trace();
2002                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2003         }
2004         new_state(p_state);
2005 }
2006
2007
2008 int newteid = 0;
2009
2010 /* MESSAGE_SETUP */
2011 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2012 {
2013         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2014         INFORMATION_t *information;
2015         SETUP_t *setup;
2016         msg_t *dmsg;
2017         int plan, type, screen, present, reason;
2018         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2019         int channel, exclusive;
2020         int i;
2021         struct epoint_list *epointlist;
2022
2023         /* release if port is blocked */
2024         if (p_m_mISDNport->ifport->block)
2025         {
2026                 struct message *message;
2027
2028                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2029                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2030                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2031                 message_put(message);
2032                 new_state(PORT_STATE_RELEASE);
2033                 p_m_delete = 1;
2034                 return;
2035         }
2036
2037         /* copy setup infos to port */
2038         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2039         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2040         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2041         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2042 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2043
2044         /* only display at connect state: this case happens if endpoint is in connected mode */
2045         if (p_state==PORT_STATE_CONNECT)
2046         {
2047                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2048                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2049                         return;
2050                 if (p_callerinfo.display[0])
2051                 {
2052                         /* sending information */
2053                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2054                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2055                         l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2056                         if (p_m_d_ntmode)
2057                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2058                         end_trace();
2059                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2060                         return;
2061                 }
2062         }
2063
2064         /* attach only if not already */
2065         epointlist = p_epointlist;
2066         while(epointlist)
2067         {
2068                 if (epointlist->epoint_id == epoint_id)
2069                         break;
2070                 epointlist = epointlist->next;
2071         }
2072         if (!epointlist)
2073                 epointlist_new(epoint_id);
2074
2075         /* get channel */
2076         exclusive = 0;
2077         if (p_m_b_channel)
2078         {
2079                 channel = p_m_b_channel;
2080                 exclusive = p_m_b_exclusive;
2081         } else
2082                 channel = CHANNEL_ANY;
2083         /* nt-port with no channel, not reserverd */
2084         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2085                 channel = CHANNEL_NO;
2086
2087         /* creating l3id */
2088         l1l2l3_trace_header(p_m_mISDNport, this, CC_NEW_CR | REQUEST, DIRECTION_OUT);
2089         if (p_m_d_ntmode)
2090         {
2091                 i = 0;
2092                 while(i < 0x100)
2093                 {
2094                         if (p_m_mISDNport->procids[i] == 0)
2095                                 break;
2096                         i++;
2097                 }
2098                 if (i == 0x100)
2099                 {
2100                         struct message *message;
2101
2102                         add_trace("callref", NULL, "no free id");
2103                         end_trace();
2104                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2105                         message->param.disconnectinfo.cause = 47;
2106                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2107                         message_put(message);
2108                         new_state(PORT_STATE_RELEASE);
2109                         p_m_delete = 1;
2110                         return;
2111                 }
2112                 p_m_mISDNport->procids[i] = 1;
2113                 p_m_d_l3id = 0xff00 | i;
2114         } else
2115         {
2116                 iframe_t ncr;
2117                 /* if we are in te-mode, we need to create a process first */
2118                 if (newteid++ > 0x7fff)
2119                         newteid = 0x0001;
2120                 p_m_d_l3id = (entity<<16) | newteid;
2121                 /* preparing message */
2122                 ncr.prim = CC_NEW_CR | REQUEST; 
2123                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2124                 ncr.dinfo = p_m_d_l3id;
2125                 ncr.len = 0;
2126                 /* send message */
2127                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2128 //              if (!dmsg)
2129 //                      goto nomem;
2130         }
2131         add_trace("callref", "new", "0x%x", p_m_d_l3id);
2132         end_trace();
2133
2134         /* preparing setup message */
2135         dmsg = create_l3msg(CC_SETUP | REQUEST, MT_SETUP, p_m_d_l3id, sizeof(SETUP_t), p_m_d_ntmode);
2136         setup = (SETUP_t *)(dmsg->data + headerlen);
2137         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP | REQUEST, DIRECTION_OUT);
2138         /* channel information */
2139         if (channel >= 0) /* it should */
2140         {
2141                 enc_ie_channel_id(&setup->CHANNEL_ID, dmsg, exclusive, channel);
2142         }
2143         /* caller information */
2144         plan = 1;
2145         switch (p_callerinfo.ntype)
2146         {
2147                 case INFO_NTYPE_INTERNATIONAL:
2148                 type = 0x1;
2149                 break;
2150                 case INFO_NTYPE_NATIONAL:
2151                 type = 0x2;
2152                 break;
2153                 case INFO_NTYPE_SUBSCRIBER:
2154                 type = 0x4;
2155                 break;
2156                 default: /* INFO_NTYPE_UNKNOWN */
2157                 type = 0x0;
2158                 break;
2159         }
2160         switch (p_callerinfo.screen)
2161         {
2162                 case INFO_SCREEN_USER:
2163                 screen = 0;
2164                 break;
2165                 default: /* INFO_SCREEN_NETWORK */
2166                 screen = 3;
2167                 break;
2168         }
2169         switch (p_callerinfo.present)
2170         {
2171                 case INFO_PRESENT_RESTRICTED:
2172                 present = 1;
2173                 break;
2174                 case INFO_PRESENT_NOTAVAIL:
2175                 present = 2;
2176                 break;
2177                 default: /* INFO_PRESENT_ALLOWED */
2178                 present = 0;
2179                 break;
2180         }
2181         if (type >= 0)
2182                 enc_ie_calling_pn(&setup->CALLING_PN, dmsg, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2183         /* dialing information */
2184         if (p_dialinginfo.id[0]) /* only if we have something to dial */
2185         {
2186                 enc_ie_called_pn(&setup->CALLED_PN, dmsg, 0, 1, (unsigned char *)p_dialinginfo.id);
2187         }
2188         /* sending complete */
2189         if (p_dialinginfo.sending_complete)
2190                 enc_ie_complete(&setup->COMPLETE, dmsg, 1);
2191         /* sending user-user */
2192         if (param->setup.useruser.len)
2193         {
2194                 enc_ie_useruser(&setup->USER_USER, dmsg, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2195         }
2196         /* redirecting number */
2197         plan = 1;
2198         switch (p_redirinfo.ntype)
2199         {
2200                 case INFO_NTYPE_INTERNATIONAL:
2201                 type = 0x1;
2202                 break;
2203                 case INFO_NTYPE_NATIONAL:
2204                 type = 0x2;
2205                 break;
2206                 case INFO_NTYPE_SUBSCRIBER:
2207                 type = 0x4;
2208                 break;
2209                 default: /* INFO_NTYPE_UNKNOWN */
2210                 type = 0x0;
2211                 break;
2212         }
2213         switch (p_redirinfo.screen)
2214         {
2215                 case INFO_SCREEN_USER:
2216                 screen = 0;
2217                 break;
2218                 default: /* INFO_SCREE_NETWORK */
2219                 screen = 3;
2220                 break;
2221         }
2222         switch (p_redirinfo.reason)
2223         {
2224                 case INFO_REDIR_BUSY:
2225                 reason = 1;
2226                 break;
2227                 case INFO_REDIR_NORESPONSE:
2228                 reason = 2;
2229                 break;
2230                 case INFO_REDIR_UNCONDITIONAL:
2231                 reason = 15;
2232                 break;
2233                 case INFO_REDIR_CALLDEFLECT:
2234                 reason = 10;
2235                 break;
2236                 case INFO_REDIR_OUTOFORDER:
2237                 reason = 9;
2238                 break;
2239                 default: /* INFO_REDIR_UNKNOWN */
2240                 reason = 0;
2241                 break;
2242         }
2243         switch (p_redirinfo.present)
2244         {
2245                 case INFO_PRESENT_NULL: /* no redir at all */
2246                 present = -1;
2247                 screen = -1;
2248                 reason = -1;
2249                 plan = -1;
2250                 type = -1;
2251                 break;
2252                 case INFO_PRESENT_RESTRICTED:
2253                 present = 1;
2254                 break;
2255                 case INFO_PRESENT_NOTAVAIL:
2256                 present = 2;
2257                 break;
2258                 default: /* INFO_PRESENT_ALLOWED */
2259                 present = 0;
2260                 break;
2261         }
2262         /* sending redirecting number only in ntmode */
2263         if (type >= 0 && p_m_d_ntmode)
2264                 enc_ie_redir_nr(&setup->REDIR_NR, dmsg, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2265         /* bearer capability */
2266 //printf("hlc=%d\n",p_capainfo.hlc);
2267         coding = 0;
2268         capability = p_capainfo.bearer_capa;
2269         mode = p_capainfo.bearer_mode;
2270         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2271         switch (p_capainfo.bearer_info1)
2272         {
2273                 case INFO_INFO1_NONE:
2274                 user = -1;
2275                 break;
2276                 default:
2277                 user = p_capainfo.bearer_info1 & 0x7f;
2278                 break;
2279         }
2280         enc_ie_bearer(&setup->BEARER, dmsg, coding, capability, mode, rate, -1, user);
2281         /* hlc */
2282         if (p_capainfo.hlc)
2283         {
2284                 coding = 0;
2285                 interpretation = 4;
2286                 presentation = 1;
2287                 hlc = p_capainfo.hlc & 0x7f;
2288                 exthlc = -1;
2289                 if (p_capainfo.exthlc)
2290                         exthlc = p_capainfo.exthlc & 0x7f;
2291                 enc_ie_hlc(&setup->HLC, dmsg, coding, interpretation, presentation, hlc, exthlc);
2292         }
2293
2294         /* display */
2295         if (p_callerinfo.display[0] && p_m_d_ntmode)
2296                 enc_ie_display(&setup->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2297 #ifdef CENTREX
2298         /* nt-mode: CNIP (calling name identification presentation) */
2299         if (p_callerinfo.name[0] && p_m_d_ntmode)
2300                 enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2301 #endif
2302         end_trace();
2303
2304         /* send setup message now */
2305         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2306
2307         new_state(PORT_STATE_OUT_SETUP);
2308 }
2309
2310 /* MESSAGE_FACILITY */
2311 void Pdss1::message_facility(unsigned long epoint_id, int message_id, union parameter *param)
2312 {
2313         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2314         FACILITY_t *facility;
2315         msg_t *dmsg;
2316
2317         /* facility will not be sent to external lines */
2318         if (!p_m_d_ntmode)
2319                 return;
2320
2321         /* sending facility */
2322         dmsg = create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, p_m_d_l3id, sizeof(FACILITY_t), p_m_d_ntmode);
2323         facility = (FACILITY_t *)(dmsg->data + headerlen);
2324         l1l2l3_trace_header(p_m_mISDNport, this, CC_FACILITY | REQUEST, DIRECTION_OUT);
2325         enc_ie_facility(&facility->FACILITY, dmsg, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2326         end_trace();
2327         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2328 }
2329
2330 /* MESSAGE_NOTIFY */
2331 void Pdss1::message_notify(unsigned long epoint_id, int message_id, union parameter *param)
2332 {
2333         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2334         INFORMATION_t *information;
2335         NOTIFY_t *notification;
2336         int notify;
2337         int plan, type = -1, present;
2338         msg_t *dmsg;
2339
2340         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2341                 notify = param->notifyinfo.notify & 0x7f;
2342         else
2343                 notify = -1;
2344         if (p_state != PORT_STATE_CONNECT)
2345         {
2346                 /* notify only allowed in active state */
2347                 notify = -1;
2348         }
2349         if (notify >= 0)
2350         {
2351                 plan = 1;
2352                 switch (param->notifyinfo.ntype)
2353                 {
2354                         case INFO_NTYPE_INTERNATIONAL:
2355                         type = 1;
2356                         break;
2357                         case INFO_NTYPE_NATIONAL:
2358                         type = 2;
2359                         break;
2360                         case INFO_NTYPE_SUBSCRIBER:
2361                         type = 4;
2362                         break;
2363                         default: /* INFO_NTYPE_UNKNOWN */
2364                         type = 0;
2365                         break;
2366                 }
2367                 switch (param->notifyinfo.present)
2368                 {
2369                         case INFO_PRESENT_NULL: /* no redir at all */
2370                         present = -1;
2371                         plan = -1;
2372                         type = -1;
2373                         break;
2374                         case INFO_PRESENT_RESTRICTED:
2375                         present = 1;
2376                         break;
2377                         case INFO_PRESENT_NOTAVAIL:
2378                         present = 2;
2379                         break;
2380                         default: /* INFO_PRESENT_ALLOWED */
2381                         present = 0;
2382                         break;
2383                 }
2384         }
2385
2386         if (notify<0 && !param->notifyinfo.display[0])
2387         {
2388                 /* nothing to notify, nothing to display */
2389                 return;
2390         }
2391
2392         if (notify >= 0)
2393         {
2394                 if (p_state!=PORT_STATE_CONNECT)
2395                 {
2396                         /* queue notification */
2397                         if (p_m_d_notify_pending)
2398                                 message_free(p_m_d_notify_pending);
2399                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2400                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2401                 } else
2402                 {
2403                         /* sending notification */
2404                         dmsg = create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, p_m_d_l3id, sizeof(NOTIFY_t), p_m_d_ntmode);
2405                         notification = (NOTIFY_t *)(dmsg->data + headerlen);
2406                         l1l2l3_trace_header(p_m_mISDNport, this, CC_NOTIFY | REQUEST, DIRECTION_OUT);
2407                         enc_ie_notify(&notification->NOTIFY, dmsg, notify);
2408                         /* sending redirection number only in ntmode */
2409                         if (type >= 0 && p_m_d_ntmode)
2410                                 enc_ie_redir_dn(&notification->REDIR_DN, dmsg, type, plan, present, (unsigned char *)param->notifyinfo.id);
2411                         if (param->notifyinfo.display[0] && p_m_d_ntmode)
2412                                 enc_ie_display(&notification->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2413                         end_trace();
2414                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2415                 }
2416         } else if (p_m_d_ntmode)
2417         {
2418                 /* sending information */
2419                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2420                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2421                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2422                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2423                 end_trace();
2424                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2425         }
2426 }
2427
2428 /* MESSAGE_OVERLAP */
2429 void Pdss1::message_overlap(unsigned long epoint_id, int message_id, union parameter *param)
2430 {
2431         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2432         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
2433         msg_t *dmsg;
2434
2435         /* sending setup_acknowledge */
2436         dmsg = create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, sizeof(SETUP_ACKNOWLEDGE_t), p_m_d_ntmode);
2437         setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2438         l1l2l3_trace_header(p_m_mISDNport, this, CC_SETUP_ACKNOWLEDGE | REQUEST, DIRECTION_OUT);
2439         /* channel information */
2440         if (p_state == PORT_STATE_IN_SETUP)
2441                 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2442         /* progress information */
2443         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2444          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2445          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2446         if (p_m_mISDNport->tones)
2447                 enc_ie_progress(&setup_acknowledge->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2448         end_trace();
2449         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2450
2451         new_state(PORT_STATE_IN_OVERLAP);
2452 }
2453
2454 /* MESSAGE_PROCEEDING */
2455 void Pdss1::message_proceeding(unsigned long epoint_id, int message_id, union parameter *param)
2456 {
2457         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2458         CALL_PROCEEDING_t *proceeding;
2459         msg_t *dmsg;
2460
2461         /* sending proceeding */
2462         dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2463         proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2464         l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2465         /* channel information */
2466         if (p_state == PORT_STATE_IN_SETUP)
2467                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2468         /* progress information */
2469         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2470          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2471          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2472         if (p_m_mISDNport->tones)
2473                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2474         end_trace();
2475         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2476
2477         new_state(PORT_STATE_IN_PROCEEDING);
2478 }
2479
2480 /* MESSAGE_ALERTING */
2481 void Pdss1::message_alerting(unsigned long epoint_id, int message_id, union parameter *param)
2482 {
2483         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2484         ALERTING_t *alerting;
2485         msg_t *dmsg;
2486
2487         /* NT-MODE in setup state we must send PROCEEDING first */
2488         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2489         {
2490                 CALL_PROCEEDING_t *proceeding;
2491
2492                 /* sending proceeding */
2493                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2494                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2495                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2496                 /* channel information */
2497                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2498                 /* progress information */
2499                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2500                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2501                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2502                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2503                 end_trace();
2504                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2505                 new_state(PORT_STATE_IN_PROCEEDING);
2506         }
2507
2508         /* sending alerting */
2509         dmsg = create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, p_m_d_l3id, sizeof(ALERTING_t), p_m_d_ntmode);
2510         alerting = (ALERTING_t *)(dmsg->data + headerlen);
2511         l1l2l3_trace_header(p_m_mISDNport, this, CC_ALERTING | REQUEST, DIRECTION_OUT);
2512         /* channel information */
2513         if (p_state == PORT_STATE_IN_SETUP)
2514                 enc_ie_channel_id(&alerting->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2515         /* progress information */
2516         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2517          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2518          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2519         if (p_m_mISDNport->tones)
2520                 enc_ie_progress(&alerting->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2521         end_trace();
2522         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2523
2524         new_state(PORT_STATE_IN_ALERTING);
2525 }
2526
2527 /* MESSAGE_CONNECT */
2528 void Pdss1::message_connect(unsigned long epoint_id, int message_id, union parameter *param)
2529 {
2530         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2531         INFORMATION_t *information;
2532         CONNECT_t *connect;
2533         int type, plan, present, screen;
2534         msg_t *dmsg;
2535         class Endpoint *epoint;
2536
2537         /* NT-MODE in setup state we must send PROCEEDING first */
2538         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2539         {
2540                 CALL_PROCEEDING_t *proceeding;
2541
2542                 /* sending proceeding */
2543                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2544                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2545                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2546                 /* channel information */
2547                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2548 //              /* progress information */
2549 //              if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2550 //               || p_capainfo.bearer_capa==INFO_BC_AUDIO
2551 //               || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2552 //              enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2553                 end_trace();
2554                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2555                 new_state(PORT_STATE_IN_PROCEEDING);
2556         }
2557
2558         /* copy connected information */
2559         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2560
2561         /* only display at connect state */
2562         if (p_state == PORT_STATE_CONNECT)
2563         if (p_connectinfo.display[0])
2564         {
2565                 /* sending information */
2566                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2567                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2568                 l1l2l3_trace_header(p_m_mISDNport, this, CC_INFORMATION | REQUEST, DIRECTION_OUT);
2569                 if (p_m_d_ntmode)
2570                         enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2571                 end_trace();
2572                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2573                 return;
2574         }
2575
2576         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)
2577         {
2578                 /* connect command only possible in setup, proceeding or alerting state */
2579                 return;
2580         }
2581
2582         /* preparing connect message */
2583         dmsg = create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, p_m_d_l3id, sizeof(CONNECT_t), p_m_d_ntmode);
2584         connect = (CONNECT_t *)(dmsg->data + headerlen);
2585         l1l2l3_trace_header(p_m_mISDNport, this, CC_CONNECT | REQUEST, DIRECTION_OUT);
2586         /* connect information */
2587         plan = 1;
2588         switch (p_connectinfo.ntype)
2589         {
2590                 case INFO_NTYPE_INTERNATIONAL:
2591                 type = 0x1;
2592                 break;
2593                 case INFO_NTYPE_NATIONAL:
2594                 type = 0x2;
2595                 break;
2596                 case INFO_NTYPE_SUBSCRIBER:
2597                 type = 0x4;
2598                 break;
2599                 default: /* INFO_NTYPE_UNKNOWN */
2600                 type = 0x0;
2601                 break;
2602         }
2603         switch (param->connectinfo.screen)
2604         {
2605                 case INFO_SCREEN_USER:
2606                 screen = 0;
2607                 break;
2608                 default: /* INFO_SCREE_NETWORK */
2609                 screen = 3;
2610                 break;
2611         }
2612         switch (p_connectinfo.present)
2613         {
2614                 case INFO_PRESENT_NULL: /* no colp at all */
2615                 present = -1;
2616                 screen = -1;
2617                 plan = -1;
2618                 type = -1;
2619                 break;
2620                 case INFO_PRESENT_RESTRICTED:
2621                 present = 1;
2622                 break;
2623                 case INFO_PRESENT_NOTAVAIL:
2624                 present = 2;
2625                 break;
2626                 default: /* INFO_PRESENT_ALLOWED */
2627                 present = 0;
2628                 break;
2629         }
2630         if (type >= 0)
2631                 enc_ie_connected_pn(&connect->CONNECT_PN, dmsg, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2632         /* display */
2633         if (p_connectinfo.display[0] && p_m_d_ntmode)
2634                 enc_ie_display(&connect->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2635 #ifdef CENTREX
2636         /* nt-mode: CONP (connected name identification presentation) */
2637         if (p_connectinfo.name[0] && p_m_d_ntmode)
2638                 enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2639 #endif
2640         /* date & time */
2641         if (p_m_d_ntmode)
2642         {
2643                 epoint = find_epoint_id(epoint_id);
2644                 enc_ie_date(&connect->DATE, dmsg, now, p_settings.no_seconds);
2645         }
2646         end_trace();
2647         /* finally send message */
2648         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2649
2650         if (p_m_d_ntmode)
2651                 new_state(PORT_STATE_CONNECT);
2652         else
2653                 new_state(PORT_STATE_CONNECT_WAITING);
2654         set_tone("", NULL);
2655 }
2656
2657 /* MESSAGE_DISCONNECT */
2658 void Pdss1::message_disconnect(unsigned long epoint_id, int message_id, union parameter *param)
2659 {
2660         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2661         DISCONNECT_t *disconnect;
2662         RELEASE_COMPLETE_t *release_complete;
2663         msg_t *dmsg;
2664         struct message *message;
2665         char *p = NULL;
2666
2667         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2668         if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->tones)
2669          || p_state==PORT_STATE_OUT_SETUP)
2670         {
2671                 /* sending release to endpoint */
2672                 while(p_epointlist)
2673                 {
2674                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2675                         message->param.disconnectinfo.cause = 16;
2676                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2677                         message_put(message);
2678                         /* remove epoint */
2679                         free_epointlist(p_epointlist);
2680                 }
2681                 /* sending release */
2682                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2683                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2684                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_COMPLETE | REQUEST, DIRECTION_OUT);
2685                 /* send cause */
2686                 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);
2687                 end_trace();
2688                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2689                 new_state(PORT_STATE_RELEASE);
2690                 p_m_delete = 1;
2691                 return;
2692         }
2693
2694         /* NT-MODE in setup state we must send PROCEEDING first */
2695         if (p_state==PORT_STATE_IN_SETUP)
2696         {
2697                 CALL_PROCEEDING_t *proceeding;
2698
2699                 /* sending proceeding */
2700                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2701                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2702                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2703                 /* channel information */
2704                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2705                 /* progress information */
2706                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2707                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2708                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2709                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2710                 end_trace();
2711                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2712                 new_state(PORT_STATE_IN_PROCEEDING);
2713         }
2714
2715         /* sending disconnect */
2716         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2717         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2718         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2719         /* progress information */
2720         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2721          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2722          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2723         if (p_m_mISDNport->tones)
2724                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2725         /* send cause */
2726         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);
2727         /* send display */
2728         if (param->disconnectinfo.display[0])
2729                 p = param->disconnectinfo.display;
2730         if (p) if (*p && p_m_d_ntmode)
2731                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2732         end_trace();
2733         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2734         new_state(PORT_STATE_OUT_DISCONNECT);
2735 }
2736
2737 /* MESSAGE_RELEASE */
2738 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
2739 {
2740         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2741         RELEASE_t *release;
2742         RELEASE_COMPLETE_t *release_complete;
2743         DISCONNECT_t *disconnect;
2744         msg_t *dmsg;
2745         class Endpoint *epoint;
2746         char *p = NULL;
2747
2748         /* if we have incoming disconnected, we may release */
2749         if (p_state==PORT_STATE_IN_DISCONNECT
2750          || p_state==PORT_STATE_OUT_DISCONNECT)
2751         {
2752                 /* sending release */
2753                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
2754                 release = (RELEASE_t *)(dmsg->data + headerlen);
2755                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2756                 /* send cause */
2757                 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);
2758                 end_trace();
2759                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2760                 new_state(PORT_STATE_RELEASE);
2761                 return;
2762
2763         }
2764         /* if we are on outgoing/incoming call setup, we may release complete */
2765         if (p_state==PORT_STATE_OUT_SETUP
2766          || p_state==PORT_STATE_IN_SETUP
2767 // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
2768          || p_state==PORT_STATE_OUT_PROCEEDING)
2769         {
2770                 /* sending release */
2771                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2772                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2773                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE | REQUEST, DIRECTION_OUT);
2774                 /* send cause */
2775                 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);
2776                 end_trace();
2777                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2778                 new_state(PORT_STATE_RELEASE);
2779
2780                 /* remove epoint */
2781                 free_epointid(epoint_id);
2782                 l1l2l3_trace_header(p_m_mISDNport, this, CC_RELEASE_CR | REQUEST, DIRECTION_OUT);
2783                 add_trace("callref", NULL, "0x%x", p_m_d_l3id);
2784                 end_trace();
2785                 if (p_m_d_ntmode)
2786                 {
2787                         if ((p_m_d_l3id&0xff00) == 0xff00)
2788                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2789                 }
2790                 p_m_d_l3id = 0;
2791                 p_m_delete = 1;
2792                 return;
2793         }
2794
2795         /* NT-MODE in setup state we must send PROCEEDING first */
2796         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2797         {
2798                 CALL_PROCEEDING_t *proceeding;
2799
2800                 /* sending proceeding */
2801                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2802                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2803                 l1l2l3_trace_header(p_m_mISDNport, this, CC_PROCEEDING | REQUEST, DIRECTION_OUT);
2804                 /* channel information */
2805                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2806                 /* progress information */
2807                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2808                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2809                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2810                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2811                 end_trace();
2812                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2813         }
2814
2815         /* sending disconnect */
2816         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2817         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2818         l1l2l3_trace_header(p_m_mISDNport, this, CC_DISCONNECT | REQUEST, DIRECTION_OUT);
2819         /* progress information */
2820         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2821          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2822          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2823         if (p_m_mISDNport->tones)
2824                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2825         /* send cause */
2826         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);
2827         /* send display */
2828         epoint = find_epoint_id(epoint_id);
2829         if (param->disconnectinfo.display[0])
2830                 p = param->disconnectinfo.display;
2831         if (p) if (*p && p_m_d_ntmode)
2832                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2833         end_trace();
2834         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2835         new_state(PORT_STATE_RELEASE);
2836         free_epointid(epoint_id);
2837 //      p_m_delete = 1;
2838 }
2839
2840
2841 /*
2842  * endpoint sends messages to the port
2843  */
2844 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
2845 {
2846         struct message *message;
2847
2848         if (PmISDN::message_epoint(epoint_id, message_id, param))
2849                 return(1);
2850
2851         switch(message_id)
2852         {
2853                 case MESSAGE_INFORMATION: /* overlap dialing */
2854                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2855                  && p_state!=PORT_STATE_OUT_OVERLAP
2856                  && p_state!=PORT_STATE_CONNECT
2857                  && p_state!=PORT_STATE_OUT_DISCONNECT
2858                  && p_state!=PORT_STATE_IN_DISCONNECT)
2859                 {
2860                         break;
2861                 }
2862                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2863                  && p_state!=PORT_STATE_OUT_OVERLAP
2864                  && p_state!=PORT_STATE_OUT_PROCEEDING
2865                  && p_state!=PORT_STATE_OUT_ALERTING
2866                  && p_state!=PORT_STATE_CONNECT
2867                  && p_state!=PORT_STATE_OUT_DISCONNECT
2868                  && p_state!=PORT_STATE_IN_DISCONNECT)
2869                 {
2870                         break;
2871                 }
2872                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
2873                  && p_state!=PORT_STATE_IN_OVERLAP
2874                  && p_state!=PORT_STATE_IN_PROCEEDING
2875                  && p_state!=PORT_STATE_IN_ALERTING
2876                  && p_state!=PORT_STATE_CONNECT
2877                  && p_state!=PORT_STATE_CONNECT_WAITING
2878                  && p_state!=PORT_STATE_OUT_DISCONNECT
2879                  && p_state!=PORT_STATE_IN_DISCONNECT)
2880                 {
2881                         break;
2882                 }
2883                 message_information(epoint_id, message_id, param);
2884                 break;
2885
2886                 case MESSAGE_SETUP: /* dial-out command received from epoint */
2887                 if (p_state!=PORT_STATE_IDLE
2888                  && p_state!=PORT_STATE_CONNECT)
2889                 {
2890                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
2891                         break;
2892                 }
2893                 if (p_epointlist && p_state==PORT_STATE_IDLE)
2894                         FATAL("Pdss1(%s): epoint pointer is set in idle state, how bad!!\n", p_name);
2895                 /* note: pri is a special case, because links must be up for pri */ 
2896                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
2897                 {
2898                         /* LAYER 1 is up, or we may send */
2899                         message_setup(epoint_id, message_id, param);
2900                 } else {
2901                         iframe_t act;
2902                         /* LAYER 1 id down, so we queue */
2903                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
2904                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
2905                         /* attach us */
2906                         if (!(epointlist_new(epoint_id)))
2907                                 FATAL("No memory for epointlist\n");
2908                         /* activate link */
2909                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
2910                         act.prim = PH_ACTIVATE | REQUEST; 
2911                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2912                         act.dinfo = 0;
2913                         act.len = 0;
2914                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
2915                         l1l2l3_trace_header(p_m_mISDNport, this, act.prim, DIRECTION_OUT);
2916                         end_trace();
2917 //                      /* set timeout */
2918 //                      p_m_mISDNport->l1timeout = now+3;
2919                 }
2920                 break;
2921
2922                 case MESSAGE_NOTIFY: /* display and notifications */
2923                 message_notify(epoint_id, message_id, param);
2924                 break;
2925
2926                 case MESSAGE_FACILITY: /* facility message */
2927                 message_facility(epoint_id, message_id, param);
2928                 break;
2929
2930                 case MESSAGE_OVERLAP: /* more information is needed */
2931                 if (p_state!=PORT_STATE_IN_SETUP)
2932                 {
2933                         break;
2934                 }
2935                 message_overlap(epoint_id, message_id, param);
2936                 break;
2937
2938                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
2939                 if (p_state!=PORT_STATE_IN_SETUP
2940                  && p_state!=PORT_STATE_IN_OVERLAP)
2941                 {
2942                         break;
2943                 }
2944                 message_proceeding(epoint_id, message_id, param);
2945                 break;
2946
2947                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
2948                 if (p_state!=PORT_STATE_IN_SETUP
2949                  && p_state!=PORT_STATE_IN_OVERLAP
2950                  && p_state!=PORT_STATE_IN_PROCEEDING)
2951                 {
2952                         break;
2953                 }
2954                 message_alerting(epoint_id, message_id, param);
2955                 break;
2956
2957                 case MESSAGE_CONNECT: /* call of endpoint is connected */
2958                 if (p_state!=PORT_STATE_IN_SETUP
2959                  && p_state!=PORT_STATE_IN_OVERLAP
2960                  && p_state!=PORT_STATE_IN_PROCEEDING
2961                  && p_state!=PORT_STATE_IN_ALERTING
2962                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
2963                 {
2964                         break;
2965                 }
2966                 message_connect(epoint_id, message_id, param);
2967                 break;
2968
2969                 case MESSAGE_DISCONNECT: /* call has been disconnected */
2970                 if (p_state!=PORT_STATE_IN_SETUP
2971                  && p_state!=PORT_STATE_IN_OVERLAP
2972                  && p_state!=PORT_STATE_IN_PROCEEDING
2973                  && p_state!=PORT_STATE_IN_ALERTING
2974                  && p_state!=PORT_STATE_OUT_SETUP
2975                  && p_state!=PORT_STATE_OUT_OVERLAP
2976                  && p_state!=PORT_STATE_OUT_PROCEEDING
2977                  && p_state!=PORT_STATE_OUT_ALERTING
2978                  && p_state!=PORT_STATE_CONNECT
2979                  && p_state!=PORT_STATE_CONNECT_WAITING)
2980                 {
2981                         break;
2982                 }
2983                 message_disconnect(epoint_id, message_id, param);
2984                 break;
2985
2986                 case MESSAGE_RELEASE: /* release isdn port */
2987                 if (p_state==PORT_STATE_RELEASE)
2988                 {
2989                         break;
2990                 }
2991                 message_release(epoint_id, message_id, param);
2992                 break;
2993
2994                 default:
2995                 PERROR("Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
2996         }
2997
2998         return(1);
2999 }
3000
3001
3002
3003 /*
3004  * data from isdn-stack (layer-3) to pbx (port class)
3005  */
3006 /* NOTE: nt mode use mISDNuser_head_t as header */
3007 int stack2manager_nt(void *dat, void *arg)
3008 {
3009         class Port *port;
3010         class Pdss1 *pdss1;
3011         manager_t *mgr = (manager_t *)dat;
3012         msg_t *msg = (msg_t *)arg;
3013         mISDNuser_head_t *hh;
3014         struct mISDNport *mISDNport;
3015         char name[32];
3016
3017         if (!msg || !mgr)
3018                 return(-EINVAL);
3019
3020         /* note: nst is the first data feld of mISDNport */
3021         mISDNport = (struct mISDNport *)mgr->nst;
3022
3023         hh = (mISDNuser_head_t *)msg->data;
3024         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3025
3026         /* find Port object of type ISDN */
3027         port = port_first;
3028         while(port)
3029         {
3030                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
3031                 {
3032                         pdss1 = (class Pdss1 *)port;
3033 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
3034                         /* check out correct stack */
3035                         if (pdss1->p_m_mISDNport == mISDNport)
3036                         /* check out correct id */
3037                         if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
3038                         {
3039                                 /* found port, the message belongs to */
3040                                 break;
3041                         }
3042                 }
3043                 port = port->next;
3044         }
3045         if (port)
3046         {
3047                 /* if process id is master process, but a child disconnects */
3048                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00 && hh->prim==(CC_DISCONNECT|INDICATION))
3049                 {
3050                         /* send special indication for child disconnect */
3051                         pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
3052                         free_msg(msg);
3053                         return(0);
3054                 }
3055                 /* if process id and layer 3 id matches */
3056                 if (hh->dinfo == pdss1->p_m_d_l3id)
3057                 {
3058                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3059                         free_msg(msg);
3060                         return(0);
3061                 }
3062         }
3063
3064         /* d-message */
3065         switch(hh->prim)
3066         {
3067                 case MGR_SHORTSTATUS | INDICATION:
3068                 case MGR_SHORTSTATUS | CONFIRM:
3069                 switch(hh->dinfo) {
3070                         case SSTATUS_L2_ESTABLISHED:
3071                         goto ss_estab;
3072                         case SSTATUS_L2_RELEASED:
3073                         goto ss_rel;
3074                 }
3075                 break;
3076
3077                 case DL_ESTABLISH | INDICATION:
3078                 case DL_ESTABLISH | CONFIRM:
3079                 ss_estab:
3080                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3081                 add_trace("tei", NULL, "%d", hh->dinfo);
3082                 end_trace();
3083                 if (mISDNport->ptp && hh->dinfo == 0)
3084                 {
3085                         if (mISDNport->l2establish)
3086                         {
3087                                 mISDNport->l2establish = 0;
3088                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
3089                         }
3090                         mISDNport->l2link = 1;
3091                         if (mISDNport->pri);
3092                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
3093                 }
3094                 break;
3095
3096                 case DL_RELEASE | INDICATION:
3097                 case DL_RELEASE | CONFIRM:
3098                 ss_rel:
3099                 l1l2l3_trace_header(mISDNport, NULL, hh->prim, DIRECTION_IN);
3100                 add_trace("tei", NULL, "%d", hh->dinfo);
3101                 end_trace();
3102                 if (mISDNport->ptp && hh->dinfo == 0)
3103                 {
3104                         mISDNport->l2link = 0;
3105                         time(&mISDNport->l2establish);
3106                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
3107                 }
3108                 break;
3109
3110                 case CC_SETUP | INDICATION:
3111                 /* creating port object */
3112                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3113                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3114
3115                         FATAL("Cannot create Port instance.\n");
3116                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3117                 break;
3118
3119                 case CC_RESUME | INDICATION:
3120                 /* creating port object */
3121                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3122                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3123                         FATAL("Cannot create Port instance.\n");
3124                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3125                 break;
3126
3127                 case CC_RELEASE_CR | INDICATION:
3128                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", hh->dinfo);
3129                 break;
3130
3131                 case CC_DISCONNECT | INDICATION:
3132
3133                 // fall throug
3134                 default:
3135                 PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3136                 return(-EINVAL);
3137         }
3138         free_msg(msg);
3139         return(0);
3140 }
3141
3142 /* NOTE: te mode use iframe_t as header */
3143 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
3144 {
3145         class Port *port;
3146         class Pdss1 *pdss1;
3147         iframe_t *frm;
3148         char name[32];
3149
3150         if (!msg || !mISDNport)
3151                 return(-EINVAL);
3152         frm = (iframe_t *)msg->data;
3153         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3154
3155         /* find Port object of type ISDN */
3156         port = port_first;
3157         while(port)
3158         {
3159                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
3160                 {
3161                         pdss1 = (class Pdss1 *)port;
3162                         /* check out correct stack */
3163                         if (pdss1->p_m_mISDNport == mISDNport)
3164                         /* check out correct id */
3165                         if (frm->dinfo == pdss1->p_m_d_l3id)
3166                         {
3167                                 /* found port, the message belongs to */
3168                                 break;
3169                         }
3170                 }
3171                 port = port->next;
3172         }
3173         if (port)
3174         {
3175                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3176                 free_msg(msg);
3177                 return(0);
3178         }
3179
3180         /* process new cr (before setup indication) */
3181 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
3182         if (frm->prim == (CC_NEW_CR | INDICATION))
3183         {
3184
3185                 /* creating port object */
3186                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3187                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_TE_IN, mISDNport, name, NULL, 0, 0)))
3188                         FATAL("Cannot create Port instance.\n");
3189                 /* l3id will be set from dinfo at message_isdn */
3190                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3191                 free_msg(msg);
3192                 return(0);
3193         }
3194
3195         if (frm->prim == (CC_RELEASE_CR | INDICATION))
3196         {
3197                 PERROR("unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
3198                 free_msg(msg);
3199                 return(0);
3200         }
3201         PERROR("unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3202         return(-EINVAL);
3203 }
3204
3205
3206 /*
3207  * sending message that were queued during L1 activation
3208  * or releasing port if link is down
3209  */
3210 void setup_queue(struct mISDNport *mISDNport, int link)
3211 {
3212         class Port *port;
3213         class Pdss1 *pdss1;
3214         struct message *message;
3215
3216         if (!mISDNport->ntmode)
3217                 return;
3218
3219         /* check all port objects for pending message */
3220         port = port_first;
3221         while(port)
3222         {
3223                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
3224                 {
3225                         pdss1 = (class Pdss1 *)port;
3226                         if (pdss1->p_m_mISDNport == mISDNport)
3227                         {
3228                                 if (pdss1->p_m_d_queue)
3229                                 {
3230                                         if (link)
3231                                         {
3232                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3233                                                 /* LAYER 1 is up, so we send */
3234                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
3235                                                 message_free(pdss1->p_m_d_queue);
3236                                                 pdss1->p_m_d_queue = NULL;
3237                                         } else
3238                                         {
3239                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3240                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
3241                                                 message->param.disconnectinfo.cause = 27;
3242                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3243                                                 message_put(message);
3244                                                 pdss1->new_state(PORT_STATE_RELEASE);
3245                                                 pdss1->p_m_delete = 1;
3246                                         }
3247                                 }
3248                         }
3249                 }
3250                 port = port->next;
3251         }
3252 }
3253
3254
3255
3256
3257