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