cbfc82238941272c0e871e24dea6df45d35fac69
[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         /* dialing information */
463         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));
464         dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)keypad, sizeof(keypad));
465         SCAT(p_dialinginfo.number, (char *)keypad);
466         switch (type)
467         {
468                 case 0x1:
469                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
470                 break;
471                 case 0x2:
472                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
473                 break;
474                 case 0x4:
475                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
476                 break;
477                 default:
478                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
479                 break;
480         }
481 #ifdef CENTREX
482         /* te-mode: CNIP (calling name identification presentation) */
483         if (!p_m_d_ntmode)
484                 dec_facility_centrex(setup->FACILITY, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)p_callerinfo.name, sizeof(p_callerinfo.name));
485 #endif
486
487         /* uus */ 
488         dec_ie_useruser(setup->USER_USER, (Q931_info_t *)((unsigned long)data+headerlen), &useruser_protocol, useruser, &useruser_len);
489
490         /* sending complete */
491         dec_ie_complete(setup->COMPLETE, (Q931_info_t *)((unsigned long)data+headerlen), &p_dialinginfo.sending_complete);
492         /* redirecting number */
493         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));
494         switch (present)
495         {
496                 case 1:
497                 p_redirinfo.present = INFO_PRESENT_RESTRICTED;
498                 break;
499                 case 2:
500                 p_redirinfo.present = INFO_PRESENT_NOTAVAIL;
501                 break;
502                 default:
503                 p_redirinfo.present = INFO_PRESENT_ALLOWED;
504                 break;
505         }
506         switch (screen)
507         {
508                 case 0:
509                 p_redirinfo.screen = INFO_SCREEN_USER;
510                 break;
511                 default:
512                 p_redirinfo.screen = INFO_SCREEN_NETWORK;
513                 break;
514         }
515         switch (reason)
516         {
517                 case 1:
518                 p_redirinfo.reason = INFO_REDIR_BUSY;
519                 break;
520                 case 2:
521                 p_redirinfo.reason = INFO_REDIR_NORESPONSE;
522                 break;
523                 case 15:
524                 p_redirinfo.reason = INFO_REDIR_UNCONDITIONAL;
525                 break;
526                 case 10:
527                 p_redirinfo.reason = INFO_REDIR_CALLDEFLECT;
528                 break;
529                 case 9:
530                 p_redirinfo.reason = INFO_REDIR_OUTOFORDER;
531                 break;
532                 default:
533                 p_redirinfo.reason = INFO_REDIR_UNKNOWN;
534                 break;
535         }
536         switch (type)
537         {
538                 case -1:
539                 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
540                 p_redirinfo.present = INFO_PRESENT_NULL; /* not redirecting */
541                 p_redirinfo.reason = INFO_REDIR_UNKNOWN;
542                 break;
543                 case 0x1:
544                 p_redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
545                 break;
546                 case 0x2:
547                 p_redirinfo.ntype = INFO_NTYPE_NATIONAL;
548                 break;
549                 case 0x4:
550                 p_redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
551                 break;
552                 default:
553                 p_redirinfo.ntype = INFO_NTYPE_UNKNOWN;
554                 break;
555         }
556         p_redirinfo.isdn_port = p_m_portnum;
557         /* bearer capability */
558         dec_ie_bearer(setup->BEARER, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &capability, &mode, &rate, &multi, &user);
559         switch (capability)
560         {
561                 case -1:
562                 p_capainfo.bearer_capa = INFO_BC_AUDIO;
563                 user = (options.law=='a')?3:2;
564                 break;
565                 default:
566                 p_capainfo.bearer_capa = capability;
567                 break;
568         }
569         switch (mode)
570         {
571                 case 2:
572                 p_capainfo.bearer_mode = INFO_BMODE_PACKET;
573                 break;
574                 default:
575                 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
576                 break;
577         }
578         switch (user)
579         {
580                 case -1:
581                 p_capainfo.bearer_info1 = INFO_INFO1_NONE;
582                 break;
583                 default:
584                 p_capainfo.bearer_info1 = user + 0x80;
585                 break;
586         }
587
588         /* hlc */
589         dec_ie_hlc(setup->HLC, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &interpretation, &presentation, &hlc, &exthlc);
590         switch (hlc)
591         {
592                 case -1:
593                 p_capainfo.hlc = INFO_HLC_NONE;
594                 break;
595                 default:
596                 p_capainfo.hlc = hlc + 0x80;
597                 break;
598         }
599         switch (exthlc)
600         {
601                 case -1:
602                 p_capainfo.exthlc = INFO_HLC_NONE;
603                 break;
604                 default:
605                 p_capainfo.exthlc = exthlc + 0x80;
606                 break;
607         }
608
609         /* channel_id */
610         dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
611         if (exclusive<0)
612                 exclusive = 0;
613         if (channel==CHANNEL_NO && p_type==PORT_TYPE_DSS1_TE_IN)
614                 PDEBUG(DEBUG_BCHANNEL, "- no channel is given by the network, causing to fail, since CW is not possible for external lines\n");
615         if (channel <= 0) /* not given, no channel, whatever.. */
616                 channel = CHANNEL_ANY; /* any channel */
617         if (p_m_mISDNport->b_reserved >= p_m_mISDNport->b_num) // of out chan..
618         {
619                 printlog("---  port#%d all channels are used/reserved.\n", ifport->portnum);
620                 ret = -34; // no channel
621                 goto no_channel;
622         }
623         if (channel == CHANNE_ANY)
624         {
625                 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");
626                 /* check for any channel form selection list */
627                 channel = 0;
628                 selchannel = ifport->channel_in;
629                 while(selchannel)
630                 {
631                         switch(selchannel->channel)
632                         {
633                                 case CHANNEL_FREE: /* free channel */
634                                 if (mISDNport->b_inuse >= mISDNport->b_num)
635                                         break; /* all channel in use or reserverd */
636                                 /* find channel */
637                                 i = 0;
638                                 while(i < mISDNport->b_num)
639                                 {
640                                         if (mISDNport->b_port[i] == NULL)
641                                         {
642                                                 channel = i+1+(i>=15);
643                                                 printlog("---  port#%d no channel given, so selecting free channel %d\n", ifport->portnum, channel);
644                                                 break;
645                                         }
646                                         i++;
647                                 }
648                                 break;
649
650                                 default:
651                                 if (selchannel->channel<1 || selchannel->channel==16)
652                                         break; /* invalid channels */
653                                 i = selchannel->channel-1-(selchannel->channel>=17);
654                                 if (i >= mISDNport->b_num)
655                                         break; /* channel not in port */
656                                 if (mISDNport->b_port[i] == NULL)
657                                 {
658                                         channel = selchannel->channel;
659                                         printlog("---  port#%d no channel given, so selecting channel %d from list\n", ifport->portnum, channel);
660                                         break;
661                                 }
662                                 break;
663                         }
664                         if (channel)
665                                 break; /* found channel */
666                         selchannel = selchannel->next;
667                 }
668                 if (!channel)
669                 {
670                         printlog("---  port#%d no channel found.\n", ifport->portnum);
671                         ret = -34; // no channel
672                         goto no_channel;
673                 }
674                 goto use_channel;
675         }
676         if (channel > 0)
677         {
678                 /* check for given channel in selection list */
679                 selchannel = ifport->channel_in;
680                 while(selchannel)
681                 {
682                         if (selchannel->channel == channel || selchannel->channel == CHANNEL_FREE)
683                                 break;
684                         selchannel = selchannel->next;
685                 }
686                 if (!selchannel)
687                         channel = 0;
688
689                 /* exclusive channel requests must be in the list */
690                 if (exclusive)
691                 {
692                         if (!channel)
693                         {
694                                 PDEBUG(DEBUG_BCHANNEL, "- exclusive channel %d is selected, but not in list of incomming channel.\n", channel);
695                                 printlog("---  port#%d channel %d given exclusively, it is accepted.\n", ifport->portnum, channel);
696                                 ret = 6; // unacceptable
697                                 goto no_channel;
698                         }
699                         PDEBUG(DEBUG_BCHANNEL, "- exclusive channel %d is selected, as in list of incomming channels.\n", channel);
700                         i = selchannel->channel-1-(selchannel->channel>=17);
701                         if (mISDNport->b_port[i] == NULL)
702                         {
703                                 printlog("---  port#%d channel %d given exclusively, it is accepted and free.\n", ifport->portnum, channel);
704                                 goto use_channel;
705                         }
706                         printlog("---  port#%d channel %d given exclusively, it is accepted, but busy.\n", ifport->portnum, channel);
707                         ret = 6; // unacceptable
708                         goto no_channel;
709                 }
710
711                 /* requested channels in list will be used */
712                 if (channel)
713                 {
714                         PDEBUG(DEBUG_BCHANNEL, "- channel %d given, found in list.\n", channel);
715                         i = selchannel->channel-1-(selchannel->channel>=17);
716                         if (mISDNport->b_port[i] == NULL)
717                         {
718                                 printlog("---  port#%d channel %d given, it is accepted and free.\n", ifport->portnum, channel);
719                                 goto use_channel;
720                         }
721                 }
722
723                 /* if channel is not available or not in list, it must be searched */
724                 PDEBUG(DEBUG_BCHANNEL, "- channel %d given, but not in list of incomming channels.\n", channel);
725
726                 /* check for first free channel in list */
727                 channel = 0;
728                 selchannel = ifport->channel_in;
729                 while(selchannel)
730                 {
731                         switch(selchannel->channel)
732                         {
733                                 case CHANNEL_FREE: /* free channel */
734                                 if (mISDNport->b_inuse >= mISDNport->b_num)
735                                         break; /* all channel in use or reserverd */
736                                 /* find channel */
737                                 i = 0;
738                                 while(i < mISDNport->b_num)
739                                 {
740                                         if (mISDNport->b_port[i] == NULL)
741                                         {
742                                                 channel = i+1+(i>=15);
743                                                 printlog("---  port#%d requested channel was not in list, so using free channel %d from list.\n", ifport->portnum, channel);
744                                                 break;
745                                         }
746                                         i++;
747                                 }
748                                 break;
749
750                                 default:
751                                 if (selchannel->channel<1 || selchannel->channel==16)
752                                         break; /* invalid channels */
753                                 i = selchannel->channel-1-(selchannel->channel>=17);
754                                 if (i >= mISDNport->b_num)
755                                         break; /* channel not in port */
756                                 if (mISDNport->b_port[i] == NULL)
757                                 {
758                                         channel = selchannel->channel;
759                                         printlog("---  port#%d requested channel was not in list, so using free channel %d from list.\n", ifport->portnum, channel);
760                                         break;
761                                 }
762                                 break;
763                         }
764                         if (channel)
765                                 break; /* found channel */
766                         selchannel = selchannel->next;
767                 }
768                 if (!channel)
769                 {
770                         printlog("---  port#%d no channel found.\n", ifport->portnum);
771                         ret = 6; // unacceptable
772                         goto no_channel;
773                 }
774         }
775
776         /* open channel */
777         use_channel:
778         ret = seize_bchannel(channel, 1);
779         if (ret < 0)
780         {
781                 no_channel:
782                 PDEBUG(DEBUG_BCHANNEL, "- channel is not available (cause=%d), so we send a release_complete.\n", -ret);
783                 RELEASE_COMPLETE_t *release_complete;
784
785                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
786                 isdn_show_send_message(CC_RELEASE_COMPLETE | REQUEST, dmsg);
787                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
788                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, -ret);
789                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
790                 new_state(PORT_STATE_RELEASE);
791                 p_m_delete = 1;
792                 return;
793         }
794         PDEBUG(DEBUG_BCHANNEL, "- channel is available, we open channel %d.\n", ret);
795         bchannel_activate(p_m_mISDNport, p_m_b_index);
796
797         /* create endpoint */
798         if (p_epointlist)
799         {
800                 PERROR("SOFTWARE ERROR: incoming call but already got an endpoint, exitting...\n");
801                 exit(-1);
802         }
803         if (!(epoint = new Endpoint(p_serial, 0)))
804         {
805                 RELEASE_COMPLETE_t *release_complete;
806
807                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, dinfo, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
808                 isdn_show_send_message(CC_RELEASE_COMPLETE | REQUEST, dmsg);
809                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
810                 enc_ie_cause(&release_complete->CAUSE, dmsg, (p_m_d_ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 41); /* temporary failure */
811                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
812                 new_state(PORT_STATE_RELEASE);
813                 p_m_delete = 1;
814                 return;
815         }
816         if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint)))
817         {
818                 PERROR("no memory for application\n");
819                 exit(-1);
820         }
821         if (!(epointlist_new(epoint->ep_serial)))
822         {
823                 PERROR("no memory for epointlist\n");
824                 exit(-1);
825         }
826         if (p_m_d_ntmode)
827         {
828                 /* nt-library now gives us the id via CC_SETUP */
829                 if (dinfo&(~0xff) == 0xff00)
830                 {
831                         PERROR("fatal software error: l3-stack gives us a process id 0xff00-0xffff\n");
832                         exit(-1);
833                 }
834                 printisdn("    l3id 0x%x changes to 0x%x\n", p_m_d_l3id, dinfo);
835                 if (p_m_d_l3id&(~0xff) == 0xff00)
836                         p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
837                 p_m_d_l3id = dinfo;
838                 p_m_d_ces = setup->ces;
839                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) nt-mode gives us new l3id via setup ind: 0x%x\n", p_name, p_m_d_l3id);
840         }
841
842         /* send setup message to endpoit */
843         PDEBUG(DEBUG_ISDN, "Pdss1(%s) setup: %s->%s\n", p_name, p_callerinfo.id, p_dialinginfo.number);
844         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
845         message->param.setup.isdn_port = p_m_portnum;
846         message->param.setup.port_type = p_type;
847         p_callerinfo.isdn_port = p_m_portnum;
848         SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);;
849         memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
850         memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
851         memcpy(&message->param.setup.redirinfo, &p_redirinfo, sizeof(struct redir_info));
852         memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
853         memcpy(message->param.setup.useruser.data, &useruser, useruser_len);
854         message->param.setup.useruser.len = useruser_len;
855         message->param.setup.useruser.protocol = useruser_protocol;
856         message_put(message);
857
858         new_state(PORT_STATE_IN_SETUP);
859 }
860
861 /* CC_INFORMATION INDICATION */
862 void Pdss1::information_ind(unsigned long prim, unsigned long dinfo, void *data)
863 {
864         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
865         INFORMATION_t *information = (INFORMATION_t *)((unsigned long)data + headerlen);
866         int type, plan;
867         unsigned char keypad[32] = "";
868         struct message *message;
869
870         /* dialing information */
871         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));
872         dec_ie_keypad(information->KEYPAD, (Q931_info_t *)((unsigned long)data+headerlen), (unsigned char *)keypad, sizeof(keypad));
873         SCAT(p_dialinginfo.number, (char *)keypad);
874         switch (type)
875         {
876                 case 0x1:
877                 p_dialinginfo.ntype = INFO_NTYPE_INTERNATIONAL;
878                 break;
879                 case 0x2:
880                 p_dialinginfo.ntype = INFO_NTYPE_NATIONAL;
881                 break;
882                 case 0x4:
883                 p_dialinginfo.ntype = INFO_NTYPE_SUBSCRIBER;
884                 break;
885                 default:
886                 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
887                 break;
888         }
889         /* sending complete */
890         dec_ie_complete(information->COMPLETE, (Q931_info_t *)((unsigned long)data+headerlen), &p_dialinginfo.sending_complete);;
891         PDEBUG(DEBUG_ISDN, "Pdss1(%s) more digits: %s\n", p_name,p_dialinginfo.number);
892         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
893         memcpy(&message->param.information, &p_dialinginfo, sizeof(struct dialing_info));
894         message_put(message);
895         /* reset overlap timeout */
896         new_state(p_state);
897 }
898
899 /* CC_SETUP_ACCNOWLEDGE INDICATION */
900 void Pdss1::setup_acknowledge_ind(unsigned long prim, unsigned long dinfo, void *data)
901 {
902         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
903         SETUP_ACKNOWLEDGE_t *setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)((unsigned long)data + headerlen);
904         int exclusive, channel;
905         int coding, location, progress;
906         int ret;
907         struct message *message;
908
909         /* channel_id */
910         dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
911         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
912         ret = received_first_reply_to_setup(prim, exclusive, channel);
913         if (ret < 0)
914         {
915                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
916                 message->param.disconnectinfo.cause = -ret;
917                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
918                 message_put(message);
919                 new_state(PORT_STATE_RELEASE);
920                 p_m_delete = 1;
921                 return;
922         }
923         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_OVERLAP);
924         message_put(message);
925
926         new_state(PORT_STATE_OUT_OVERLAP);
927 }
928
929 /* CC_PROCEEDING INDICATION */
930 void Pdss1::proceeding_ind(unsigned long prim, unsigned long dinfo, void *data)
931 {
932         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
933         CALL_PROCEEDING_t *proceeding = (CALL_PROCEEDING_t *)((unsigned long)data + headerlen);
934         int exclusive, channel;
935         int coding, location, progress;
936         int ret;
937         struct message *message;
938         int notify = -1, type, plan, present;
939         char redir[32];
940
941         /* channel id */
942         dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
943         dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
944         ret = received_first_reply_to_setup(prim, exclusive, channel);
945         if (ret < 0)
946         {
947                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
948                 message->param.disconnectinfo.cause = -ret;
949                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
950                 message_put(message);
951                 new_state(PORT_STATE_RELEASE);
952                 p_m_delete = 1;
953                 return;
954         }
955         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
956         message_put(message);
957
958         new_state(PORT_STATE_OUT_PROCEEDING);
959         
960         dec_ie_notify(NULL/*proceeding->NOTIFY*/, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
961         if (notify >= 0)
962                 notify |= 0x80;
963         else
964                 notify = 0;
965         dec_ie_redir_dn(proceeding->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)redir, sizeof(redir));
966         if (type >= 0 || notify)
967         {
968                 if (!notify && type >= 0)
969                         notify = 251;
970                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
971                 message->param.notifyinfo.notify = notify;
972                 SCPY(message->param.notifyinfo.id, redir);
973                 /* redirection number */
974                 switch (present)
975                 {
976                         case 1:
977                         message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
978                         break;
979                         case 2:
980                         message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
981                         break;
982                         default:
983                         message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
984                         break;
985                 }
986                 switch (type)
987                 {
988                         case -1:
989                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
990                         message->param.notifyinfo.present = INFO_PRESENT_NULL;
991                         break;
992                         case 1:
993                         message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
994                         break;
995                         case 2:
996                         message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
997                         break;
998                         case 4:
999                         message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1000                         break;
1001                         default:
1002                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1003                         break;
1004                 }
1005                 message->param.notifyinfo.isdn_port = p_m_portnum;
1006                 message_put(message);
1007         }
1008 }
1009
1010 /* CC_ALERTING INDICATION */
1011 void Pdss1::alerting_ind(unsigned long prim, unsigned long dinfo, void *data)
1012 {
1013         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1014         ALERTING_t *alerting = (ALERTING_t *)((unsigned long)data + headerlen);
1015         int exclusive, channel;
1016         int coding, location, progress;
1017         int ret;
1018         struct message *message;
1019         int notify = -1, type, plan, present;
1020         char redir[32];
1021
1022         /* channel id */
1023         dec_ie_channel_id(alerting->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1024         dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)((unsigned long)data+headerlen), &coding, &location, &progress);
1025         ret = received_first_reply_to_setup(prim, exclusive, channel);
1026         if (ret < 0)
1027         {
1028                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1029                 message->param.disconnectinfo.cause = -ret;
1030                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1031                 message_put(message);
1032                 new_state(PORT_STATE_RELEASE);
1033                 p_m_delete = 1;
1034                 return;
1035         }
1036         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1037         message_put(message);
1038
1039         new_state(PORT_STATE_OUT_ALERTING);
1040
1041         dec_ie_notify(NULL/*alerting->NOTIFY*/, (Q931_info_t *)((unsigned long)data+headerlen), &notify);
1042         if (notify >= 0)
1043                 notify |= 0x80;
1044         else
1045                 notify = 0;
1046         dec_ie_redir_dn(alerting->REDIR_DN, (Q931_info_t *)((unsigned long)data+headerlen), &type, &plan, &present, (unsigned char *)redir, sizeof(redir));
1047         if (type >= 0 || notify)
1048         {
1049                 if (!notify && type >= 0)
1050                         notify = 251;
1051                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_NOTIFY);
1052                 message->param.notifyinfo.notify = notify;
1053                 SCPY(message->param.notifyinfo.id, redir);
1054                 switch (present)
1055                 {
1056                         case 1:
1057                         message->param.notifyinfo.present = INFO_PRESENT_RESTRICTED;
1058                         break;
1059                         case 2:
1060                         message->param.notifyinfo.present = INFO_PRESENT_NOTAVAIL;
1061                         break;
1062                         default:
1063                         message->param.notifyinfo.present = INFO_PRESENT_ALLOWED;
1064                         break;
1065                 }
1066                 switch (type)
1067                 {
1068                         case -1:
1069                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1070                         message->param.notifyinfo.present = INFO_PRESENT_NULL;
1071                         break;
1072                         case 1:
1073                         message->param.notifyinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1074                         break;
1075                         case 2:
1076                         message->param.notifyinfo.ntype = INFO_NTYPE_NATIONAL;
1077                         break;
1078                         case 4:
1079                         message->param.notifyinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1080                         break;
1081                         default:
1082                         message->param.notifyinfo.ntype = INFO_NTYPE_UNKNOWN;
1083                         break;
1084                 }
1085                 message->param.notifyinfo.isdn_port = p_m_portnum;
1086                 message_put(message);
1087         }
1088 }
1089
1090 /* CC_CONNECT INDICATION */
1091 void Pdss1::connect_ind(unsigned long prim, unsigned long dinfo, void *data)
1092 {
1093         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1094         CONNECT_t *connect = (CONNECT_t *)((unsigned long)data + headerlen);
1095         int exclusive, channel;
1096         int type, plan, present, screen;
1097         int ret;
1098         msg_t *dmsg;
1099         struct message *message;
1100         int bchannel_before;
1101
1102         if (p_m_d_ntmode)
1103                 p_m_d_ces = connect->ces;
1104
1105         /* NOTE: we do not check the connected channel, since we
1106          * ready sent a channel to the remote side
1107          */
1108         /* channel id */
1109         dec_ie_channel_id(connect->CHANNEL_ID, (Q931_info_t *)((unsigned long)data+headerlen), &exclusive, &channel);
1110         bchannel_before = p_m_b_channel;
1111         ret = received_first_reply_to_setup(prim, exclusive, channel);
1112         if (ret < 0)
1113         {
1114                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1115                 message->param.disconnectinfo.cause = -ret;
1116                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1117                 message_put(message);
1118                 new_state(PORT_STATE_RELEASE);
1119                 p_m_delete = 1;
1120                 return;
1121         }
1122         /* connect information */
1123         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));
1124         switch (present)
1125         {
1126                 case 1:
1127                 p_connectinfo.present = INFO_PRESENT_RESTRICTED;
1128                 break;
1129                 case 2:
1130                 p_connectinfo.present = INFO_PRESENT_NOTAVAIL;
1131                 break;
1132                 default:
1133                 p_connectinfo.present = INFO_PRESENT_ALLOWED;
1134                 break;
1135         }
1136         switch (screen)
1137         {
1138                 case 0:
1139                 p_connectinfo.screen = INFO_SCREEN_USER;
1140                 break;
1141                 default:
1142                 p_connectinfo.screen = INFO_SCREEN_NETWORK;
1143                 break;
1144         }
1145         switch (type)
1146         {
1147                 case 0x0:
1148                 p_connectinfo.present = INFO_PRESENT_NULL; /* no COLP info */
1149                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1150                 break;
1151                 case 0x1:
1152                 p_connectinfo.ntype = INFO_NTYPE_INTERNATIONAL;
1153                 break;
1154                 case 0x2:
1155                 p_connectinfo.ntype = INFO_NTYPE_NATIONAL;
1156                 break;
1157                 case 0x4:
1158                 p_connectinfo.ntype = INFO_NTYPE_SUBSCRIBER;
1159                 break;
1160                 default:
1161                 p_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1162                 break;
1163         }
1164         p_connectinfo.isdn_port = p_m_portnum;
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         /* handle destruction */
2104         if (p_m_delete && p_m_d_l3id==0)
2105         {
2106                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) doing pending release.\n", p_name);
2107                 delete this;
2108                 return(-1);
2109         }
2110
2111         return(PmISDN::handler());
2112 }
2113
2114
2115 /*
2116  * handles all messages from endpoint
2117  */
2118 /* MESSAGE_INFORMATION */
2119 void Pdss1::message_information(unsigned long epoint_id, int message_id, union parameter *param)
2120 {
2121         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2122         INFORMATION_t *information;
2123         msg_t *dmsg;
2124
2125         if (param->information.number[0]) /* only if we have something to dial */
2126         {
2127                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2128                 isdn_show_send_message(CC_INFORMATION | REQUEST, dmsg);
2129                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2130                 enc_ie_called_pn(&information->CALLED_PN, dmsg, 0, 1, (unsigned char *)param->information.number);
2131                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2132         }
2133         new_state(p_state);
2134 }
2135
2136
2137 int newteid = 0;
2138
2139 /* MESSAGE_SETUP */
2140 void Pdss1::message_setup(unsigned long epoint_id, int message_id, union parameter *param)
2141 {
2142         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2143         INFORMATION_t *information;
2144         SETUP_t *setup;
2145         msg_t *dmsg;
2146         int plan, type, screen, present, reason;
2147         int capability, mode, rate, coding, user, presentation, interpretation, hlc, exthlc;
2148         int channel, exclusive;
2149         int i;
2150         struct epoint_list *epointlist;
2151
2152         /* release if port is blocked */
2153         if (p_m_mISDNport->ifport->block)
2154         {
2155                 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2156                 message->param.disconnectinfo.cause = 27; // temp. unavail.
2157                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2158                 message_put(message);
2159                 new_state(PORT_STATE_RELEASE);
2160                 p_m_delete = 1;
2161                 return;
2162         }
2163
2164         /* copy setup infos to port */
2165         memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
2166         memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
2167         memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
2168         memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
2169 //              SCPY(&p_m_tones_dir, param->setup.ext.tones_dir);
2170
2171         /* only display at connect state: this case happens if endpoint is in connected mode */
2172         if (p_state==PORT_STATE_CONNECT)
2173         {
2174                 if (p_type!=PORT_TYPE_DSS1_NT_OUT
2175                  && p_type!=PORT_TYPE_DSS1_NT_IN)
2176                         return;
2177                 if (p_callerinfo.display[0])
2178                 {
2179                         /* sending information */
2180                         dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2181                         isdn_show_send_message(CC_INFORMATION | REQUEST, dmsg);
2182                         information = (INFORMATION_t *)(dmsg->data + headerlen);
2183                         if (p_m_d_ntmode)
2184                                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2185                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2186                         return;
2187                 }
2188         }
2189
2190         /* attach only if not already */
2191         epointlist = p_epointlist;
2192         while(epointlist)
2193         {
2194                 if (epointlist->epoint_id == epoint_id)
2195                         break;
2196                 epointlist = epointlist->next;
2197         }
2198         if (!epointlist)
2199         {
2200                 if (!(epointlist_new(epoint_id)))
2201                 {
2202                         PERROR("no memory for epointlist\n");
2203                         exit(-1);
2204                 }
2205         }
2206
2207         /* get channel */
2208         exclusive = 0;
2209         if (p_m_b_channel)
2210         {
2211                 channel = p_m_b_channel;
2212                 exclusive = p_m_b_exclusive;
2213         } else
2214                 channel = CHANNEL_ANY;
2215         /* nt-port with no channel, not reserverd */
2216         if (!p_m_b_channel && !p_m_b_reserve && p_type==PORT_TYPE_DSS1_NT_OUT)
2217                 channel = CHANNEL_NO;
2218
2219         /* creating l3id */
2220         if (p_m_d_ntmode)
2221         {
2222                 i = 0;
2223                 while(i < 0x100)
2224                 {
2225                         if (p_m_mISDNport->procids[i] == 0)
2226                                 break;
2227                         i++;
2228                 }
2229                 if (i == 0x100)
2230                 {
2231                         struct message *message;
2232
2233                         printisdn("    no free process id\n");
2234                         PDEBUG(DEBUG_ISDN, "no more free process ID for port.\n");
2235                         message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2236                         message->param.disconnectinfo.cause = 47;
2237                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2238                         message_put(message);
2239                         new_state(PORT_STATE_RELEASE);
2240                         p_m_delete = 1;
2241                         return;
2242                 }
2243                 p_m_mISDNport->procids[i] = 1;
2244                 printisdn("    procid 0x%x allocated\n", i);
2245                 p_m_d_l3id = 0xff00 | i;
2246                 printisdn("    l3id is now 0x%x\n", p_m_d_l3id);
2247         } else
2248         {
2249                 iframe_t ncr;
2250                 /* if we are in te-mode, we need to create a process first */
2251                 if (newteid++ > 0x7fff)
2252                         newteid = 0x0001;
2253                 p_m_d_l3id = (entity<<16) | newteid;
2254                 /* preparing message */
2255                 ncr.prim = CC_NEW_CR | REQUEST; 
2256                 ncr.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
2257                 ncr.dinfo = p_m_d_l3id;
2258                 ncr.len = 0;
2259                 isdn_show_send_message(CC_NEW_CR | REQUEST, NULL);
2260                 /* send message */
2261                 mISDN_write(mISDNdevice, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
2262 //              if (!dmsg)
2263 //                      goto nomem;
2264         }
2265
2266         /* preparing setup message */
2267         dmsg = create_l3msg(CC_SETUP | REQUEST, MT_SETUP, p_m_d_l3id, sizeof(SETUP_t), p_m_d_ntmode);
2268         isdn_show_send_message(CC_SETUP | REQUEST, dmsg);
2269         setup = (SETUP_t *)(dmsg->data + headerlen);
2270         /* channel information */
2271         if (channel >= 0) /* it should */
2272         {
2273                 enc_ie_channel_id(&setup->CHANNEL_ID, dmsg, exclusive, channel);
2274         }
2275         /* caller information */
2276         plan = 1;
2277         switch (p_callerinfo.ntype)
2278         {
2279                 case INFO_NTYPE_INTERNATIONAL:
2280                 type = 0x1;
2281                 break;
2282                 case INFO_NTYPE_NATIONAL:
2283                 type = 0x2;
2284                 break;
2285                 case INFO_NTYPE_SUBSCRIBER:
2286                 type = 0x4;
2287                 break;
2288                 default: /* INFO_NTYPE_UNKNOWN */
2289                 type = 0x0;
2290                 break;
2291         }
2292         switch (p_callerinfo.screen)
2293         {
2294                 case INFO_SCREEN_USER:
2295                 screen = 0;
2296                 break;
2297                 default: /* INFO_SCREEN_NETWORK */
2298                 screen = 3;
2299                 break;
2300         }
2301         switch (p_callerinfo.present)
2302         {
2303                 case INFO_PRESENT_RESTRICTED:
2304                 present = 1;
2305                 break;
2306                 case INFO_PRESENT_NOTAVAIL:
2307                 present = 2;
2308                 break;
2309                 default: /* INFO_PRESENT_ALLOWED */
2310                 present = 0;
2311                 break;
2312         }
2313         if (type >= 0)
2314                 enc_ie_calling_pn(&setup->CALLING_PN, dmsg, type, plan, present, screen, (unsigned char *)p_callerinfo.id);
2315         /* dialing information */
2316         if (p_dialinginfo.number[0]) /* only if we have something to dial */
2317         {
2318                 enc_ie_called_pn(&setup->CALLED_PN, dmsg, 0, 1, (unsigned char *)p_dialinginfo.number);
2319         }
2320         /* sending complete */
2321         if (p_dialinginfo.sending_complete)
2322                 enc_ie_complete(&setup->COMPLETE, dmsg, 1);
2323         /* sending user-user */
2324         if (param->setup.useruser.len)
2325         {
2326                 enc_ie_useruser(&setup->USER_USER, dmsg, param->setup.useruser.protocol, param->setup.useruser.data, param->setup.useruser.len);
2327         }
2328         /* redirecting number */
2329         plan = 1;
2330         switch (p_redirinfo.ntype)
2331         {
2332                 case INFO_NTYPE_INTERNATIONAL:
2333                 type = 0x1;
2334                 break;
2335                 case INFO_NTYPE_NATIONAL:
2336                 type = 0x2;
2337                 break;
2338                 case INFO_NTYPE_SUBSCRIBER:
2339                 type = 0x4;
2340                 break;
2341                 default: /* INFO_NTYPE_UNKNOWN */
2342                 type = 0x0;
2343                 break;
2344         }
2345         switch (p_redirinfo.screen)
2346         {
2347                 case INFO_SCREEN_USER:
2348                 screen = 0;
2349                 break;
2350                 default: /* INFO_SCREE_NETWORK */
2351                 screen = 3;
2352                 break;
2353         }
2354         switch (p_redirinfo.reason)
2355         {
2356                 case INFO_REDIR_BUSY:
2357                 reason = 1;
2358                 break;
2359                 case INFO_REDIR_NORESPONSE:
2360                 reason = 2;
2361                 break;
2362                 case INFO_REDIR_UNCONDITIONAL:
2363                 reason = 15;
2364                 break;
2365                 case INFO_REDIR_CALLDEFLECT:
2366                 reason = 10;
2367                 break;
2368                 case INFO_REDIR_OUTOFORDER:
2369                 reason = 9;
2370                 break;
2371                 default: /* INFO_REDIR_UNKNOWN */
2372                 reason = 0;
2373                 break;
2374         }
2375         switch (p_redirinfo.present)
2376         {
2377                 case INFO_PRESENT_NULL: /* no redir at all */
2378                 present = -1;
2379                 screen = -1;
2380                 reason = -1;
2381                 plan = -1;
2382                 type = -1;
2383                 break;
2384                 case INFO_PRESENT_RESTRICTED:
2385                 present = 1;
2386                 break;
2387                 case INFO_PRESENT_NOTAVAIL:
2388                 present = 2;
2389                 break;
2390                 default: /* INFO_PRESENT_ALLOWED */
2391                 present = 0;
2392                 break;
2393         }
2394         /* sending redirecting number only in ntmode */
2395         if (type >= 0 && p_m_d_ntmode)
2396                 enc_ie_redir_nr(&setup->REDIR_NR, dmsg, type, plan, present, screen, reason, (unsigned char *)p_redirinfo.id);
2397         /* bearer capability */
2398 //printf("hlc=%d\n",p_capainfo.hlc);
2399         coding = 0;
2400         capability = p_capainfo.bearer_capa;
2401         mode = p_capainfo.bearer_mode;
2402         rate = (mode==INFO_BMODE_CIRCUIT)?0x10:0x00;
2403         switch (p_capainfo.bearer_info1)
2404         {
2405                 case INFO_INFO1_NONE:
2406                 user = -1;
2407                 break;
2408                 default:
2409                 user = p_capainfo.bearer_info1 & 0x7f;
2410                 break;
2411         }
2412         enc_ie_bearer(&setup->BEARER, dmsg, coding, capability, mode, rate, -1, user);
2413         /* hlc */
2414         if (p_capainfo.hlc)
2415         {
2416                 coding = 0;
2417                 interpretation = 4;
2418                 presentation = 1;
2419                 hlc = p_capainfo.hlc & 0x7f;
2420                 exthlc = -1;
2421                 if (p_capainfo.exthlc)
2422                         exthlc = p_capainfo.exthlc & 0x7f;
2423                 enc_ie_hlc(&setup->HLC, dmsg, coding, interpretation, presentation, hlc, exthlc);
2424         }
2425
2426         /* display */
2427         if (p_callerinfo.display[0] && p_m_d_ntmode)
2428                 enc_ie_display(&setup->DISPLAY, dmsg, (unsigned char *)p_callerinfo.display);
2429 #ifdef CENTREX
2430         /* nt-mode: CNIP (calling name identification presentation) */
2431         if (p_callerinfo.name[0] && p_m_d_ntmode)
2432                 enc_facility_centrex(&setup->FACILITY, dmsg, (unsigned char *)p_callerinfo.name, 1);
2433 #endif
2434
2435         /* send setup message now */
2436         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2437
2438         new_state(PORT_STATE_OUT_SETUP);
2439 }
2440
2441 /* MESSAGE_FACILITY */
2442 void Pdss1::message_facility(unsigned long epoint_id, int message_id, union parameter *param)
2443 {
2444         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2445         FACILITY_t *facility;
2446         msg_t *dmsg;
2447
2448         /* facility will not be sent to external lines */
2449         if (!p_m_d_ntmode)
2450                 return;
2451
2452         PDEBUG(DEBUG_ISDN, "Pdss1(%s) sending facility message\n", p_name);
2453
2454         /* sending facility */
2455         dmsg = create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, p_m_d_l3id, sizeof(FACILITY_t), p_m_d_ntmode);
2456         isdn_show_send_message(CC_FACILITY | REQUEST, dmsg);
2457         facility = (FACILITY_t *)(dmsg->data + headerlen);
2458         enc_ie_facility(&facility->FACILITY, dmsg, (unsigned char *)param->facilityinfo.data, param->facilityinfo.len);
2459         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2460 }
2461
2462 /* MESSAGE_NOTIFY */
2463 void Pdss1::message_notify(unsigned long epoint_id, int message_id, union parameter *param)
2464 {
2465         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2466         INFORMATION_t *information;
2467         NOTIFY_t *notification;
2468         int notify;
2469         int plan, type = -1, present;
2470         msg_t *dmsg;
2471
2472         PDEBUG(DEBUG_ISDN, "Pdss1(%s) sending information message\n", p_name);
2473         if (param->notifyinfo.notify>INFO_NOTIFY_NONE)
2474                 notify = param->notifyinfo.notify & 0x7f;
2475         else
2476                 notify = -1;
2477         if (p_state != PORT_STATE_CONNECT)
2478         {
2479                 /* notify only allowed in active state */
2480                 notify = -1;
2481         }
2482         if (notify >= 0)
2483         {
2484                 plan = 1;
2485                 switch (param->notifyinfo.ntype)
2486                 {
2487                         case INFO_NTYPE_INTERNATIONAL:
2488                         type = 1;
2489                         break;
2490                         case INFO_NTYPE_NATIONAL:
2491                         type = 2;
2492                         break;
2493                         case INFO_NTYPE_SUBSCRIBER:
2494                         type = 4;
2495                         break;
2496                         default: /* INFO_NTYPE_UNKNOWN */
2497                         type = 0;
2498                         break;
2499                 }
2500                 switch (param->notifyinfo.present)
2501                 {
2502                         case INFO_PRESENT_NULL: /* no redir at all */
2503                         present = -1;
2504                         plan = -1;
2505                         type = -1;
2506                         break;
2507                         case INFO_PRESENT_RESTRICTED:
2508                         present = 1;
2509                         break;
2510                         case INFO_PRESENT_NOTAVAIL:
2511                         present = 2;
2512                         break;
2513                         default: /* INFO_PRESENT_ALLOWED */
2514                         present = 0;
2515                         break;
2516                 }
2517         }
2518
2519         if (notify<0 && !param->notifyinfo.display[0])
2520         {
2521                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) nothing to notify, nothing to display\n", p_name);
2522                 return;
2523         }
2524
2525         if (notify >= 0)
2526         {
2527                 if (p_state!=PORT_STATE_CONNECT)
2528                 {
2529                         /* queue notification */
2530                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) queueing notification because isdn port is not active state.\n", p_name);
2531                         if (p_m_d_notify_pending)
2532                                 message_free(p_m_d_notify_pending);
2533                         p_m_d_notify_pending = message_create(ACTIVE_EPOINT(p_epointlist), p_serial, EPOINT_TO_PORT, message_id);
2534                         memcpy(&p_m_d_notify_pending->param, param, sizeof(union parameter));
2535                 } else
2536                 {
2537                         /* sending notification */
2538                         dmsg = create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, p_m_d_l3id, sizeof(NOTIFY_t), p_m_d_ntmode);
2539                         isdn_show_send_message(CC_NOTIFY | REQUEST, dmsg);
2540                         notification = (NOTIFY_t *)(dmsg->data + headerlen);
2541                         enc_ie_notify(&notification->NOTIFY, dmsg, notify);
2542                         /* sending redirection number only in ntmode */
2543                         if (type >= 0 && p_m_d_ntmode)
2544                                 enc_ie_redir_dn(&notification->REDIR_DN, dmsg, type, plan, present, (unsigned char *)param->notifyinfo.id);
2545                         if (param->notifyinfo.display[0] && p_m_d_ntmode)
2546                                 enc_ie_display(&notification->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2547                         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2548                 }
2549         } else if (p_m_d_ntmode)
2550         {
2551                 /* sending information */
2552                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2553                 isdn_show_send_message(CC_INFORMATION | REQUEST, dmsg);
2554                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2555                 enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)param->notifyinfo.display);
2556                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2557         }
2558 }
2559
2560 /* MESSAGE_OVERLAP */
2561 void Pdss1::message_overlap(unsigned long epoint_id, int message_id, union parameter *param)
2562 {
2563         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2564         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
2565         msg_t *dmsg;
2566
2567         /* sending setup_acknowledge */
2568         dmsg = create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, p_m_d_l3id, sizeof(SETUP_ACKNOWLEDGE_t), p_m_d_ntmode);
2569         isdn_show_send_message(CC_SETUP_ACKNOWLEDGE | REQUEST, dmsg);
2570         setup_acknowledge = (SETUP_ACKNOWLEDGE_t *)(dmsg->data + headerlen);
2571         /* channel information */
2572         if (p_state == PORT_STATE_IN_SETUP)
2573                 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2574         /* progress information */
2575         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2576          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2577          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2578         if (p_m_mISDNport->is_tones)
2579                 enc_ie_progress(&setup_acknowledge->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2580         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2581
2582         new_state(PORT_STATE_IN_OVERLAP);
2583 }
2584
2585 /* MESSAGE_PROCEEDING */
2586 void Pdss1::message_proceeding(unsigned long epoint_id, int message_id, union parameter *param)
2587 {
2588         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2589         CALL_PROCEEDING_t *proceeding;
2590         msg_t *dmsg;
2591
2592         /* sending proceeding */
2593         dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2594         isdn_show_send_message(CC_PROCEEDING | REQUEST, dmsg);
2595         proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2596         /* channel information */
2597         if (p_state == PORT_STATE_IN_SETUP)
2598                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2599         /* progress information */
2600         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2601          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2602          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2603         if (p_m_mISDNport->is_tones)
2604                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2605         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2606
2607         new_state(PORT_STATE_IN_PROCEEDING);
2608 }
2609
2610 /* MESSAGE_ALERTING */
2611 void Pdss1::message_alerting(unsigned long epoint_id, int message_id, union parameter *param)
2612 {
2613         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2614         ALERTING_t *alerting;
2615         msg_t *dmsg;
2616
2617         /* NT-MODE in setup state we must send PROCEEDING first */
2618         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2619         {
2620                 CALL_PROCEEDING_t *proceeding;
2621
2622                 /* sending proceeding */
2623                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2624                 isdn_show_send_message(CC_PROCEEDING | REQUEST, dmsg);
2625                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2626                 /* channel information */
2627                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2628                 /* progress information */
2629                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2630                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2631                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2632                 enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2633                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2634                 new_state(PORT_STATE_IN_PROCEEDING);
2635         }
2636
2637         /* sending alerting */
2638         dmsg = create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, p_m_d_l3id, sizeof(ALERTING_t), p_m_d_ntmode);
2639         isdn_show_send_message(CC_ALERTING | REQUEST, dmsg);
2640         alerting = (ALERTING_t *)(dmsg->data + headerlen);
2641         /* channel information */
2642         if (p_state == PORT_STATE_IN_SETUP)
2643                 enc_ie_channel_id(&alerting->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2644         /* progress information */
2645         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2646          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2647          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2648         if (p_m_mISDNport->is_tones)
2649                 enc_ie_progress(&alerting->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2650         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2651
2652         new_state(PORT_STATE_IN_ALERTING);
2653 }
2654
2655 /* MESSAGE_CONNECT */
2656 void Pdss1::message_connect(unsigned long epoint_id, int message_id, union parameter *param)
2657 {
2658         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2659         INFORMATION_t *information;
2660         CONNECT_t *connect;
2661         int type, plan, present, screen;
2662         msg_t *dmsg;
2663         class Endpoint *epoint;
2664
2665         /* NT-MODE in setup state we must send PROCEEDING first */
2666         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2667         {
2668                 CALL_PROCEEDING_t *proceeding;
2669
2670                 /* sending proceeding */
2671                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2672                 isdn_show_send_message(CC_PROCEEDING | REQUEST, dmsg);
2673                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2674                 /* channel information */
2675                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2676 //              /* progress information */
2677 //              if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2678 //               || p_capainfo.bearer_capa==INFO_BC_AUDIO
2679 //               || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2680 //              enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2681                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2682                 new_state(PORT_STATE_IN_PROCEEDING);
2683         }
2684
2685         /* copy connected information */
2686         memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));
2687
2688         /* only display at connect state */
2689         if (p_state == PORT_STATE_CONNECT)
2690         if (p_connectinfo.display[0])
2691         {
2692                 /* sending information */
2693                 dmsg = create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, p_m_d_l3id, sizeof(INFORMATION_t), p_m_d_ntmode);
2694                 isdn_show_send_message(CC_INFORMATION | REQUEST, dmsg);
2695                 information = (INFORMATION_t *)(dmsg->data + headerlen);
2696                 if (p_m_d_ntmode)
2697                         enc_ie_display(&information->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2698                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2699                 return;
2700         }
2701
2702         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)
2703         {
2704                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) connect command only possible in setup, proceeding or alerting state.\n", p_name);
2705                 return;
2706         }
2707
2708         /* preparing connect message */
2709         dmsg = create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, p_m_d_l3id, sizeof(CONNECT_t), p_m_d_ntmode);
2710         isdn_show_send_message(CC_CONNECT | REQUEST, dmsg);
2711         connect = (CONNECT_t *)(dmsg->data + headerlen);
2712         /* connect information */
2713         plan = 1;
2714         switch (p_connectinfo.ntype)
2715         {
2716                 case INFO_NTYPE_INTERNATIONAL:
2717                 type = 0x1;
2718                 break;
2719                 case INFO_NTYPE_NATIONAL:
2720                 type = 0x2;
2721                 break;
2722                 case INFO_NTYPE_SUBSCRIBER:
2723                 type = 0x4;
2724                 break;
2725                 default: /* INFO_NTYPE_UNKNOWN */
2726                 type = 0x0;
2727                 break;
2728         }
2729         switch (param->connectinfo.screen)
2730         {
2731                 case INFO_SCREEN_USER:
2732                 screen = 0;
2733                 break;
2734                 default: /* INFO_SCREE_NETWORK */
2735                 screen = 3;
2736                 break;
2737         }
2738         switch (p_connectinfo.present)
2739         {
2740                 case INFO_PRESENT_NULL: /* no colp at all */
2741                 present = -1;
2742                 screen = -1;
2743                 plan = -1;
2744                 type = -1;
2745                 break;
2746                 case INFO_PRESENT_RESTRICTED:
2747                 present = 1;
2748                 break;
2749                 case INFO_PRESENT_NOTAVAIL:
2750                 present = 2;
2751                 break;
2752                 default: /* INFO_PRESENT_ALLOWED */
2753                 present = 0;
2754                 break;
2755         }
2756         if (type >= 0)
2757                 enc_ie_connected_pn(&connect->CONNECT_PN, dmsg, type, plan, present, screen, (unsigned char *)p_connectinfo.id);
2758         /* display */
2759         if (p_connectinfo.display[0] && p_m_d_ntmode)
2760                 enc_ie_display(&connect->DISPLAY, dmsg, (unsigned char *)p_connectinfo.display);
2761 #ifdef CENTREX
2762         /* nt-mode: CONP (connected name identification presentation) */
2763         if (p_connectinfo.name[0] && p_m_d_ntmode)
2764                 enc_facility_centrex(&connect->FACILITY, dmsg, (unsigned char *)p_connectinfo.name, 0);
2765 #endif
2766         /* date & time */
2767         if (p_m_d_ntmode)
2768         {
2769                 epoint = find_epoint_id(epoint_id);
2770                 enc_ie_date(&connect->DATE, dmsg, now, p_settings.no_seconds);
2771         }
2772         /* finally send message */
2773         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2774
2775         if (p_m_d_ntmode)
2776                 new_state(PORT_STATE_CONNECT);
2777         else
2778                 new_state(PORT_STATE_CONNECT_WAITING);
2779         set_tone("", NULL);
2780 }
2781
2782 /* MESSAGE_DISCONNECT */
2783 void Pdss1::message_disconnect(unsigned long epoint_id, int message_id, union parameter *param)
2784 {
2785         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2786         DISCONNECT_t *disconnect;
2787         RELEASE_COMPLETE_t *release_complete;
2788         msg_t *dmsg;
2789         struct message *message;
2790         char *p = NULL;
2791
2792         /* we reject during incoming setup when we have no tones. also if we are in outgoing setup state */
2793         if ((p_state==PORT_STATE_IN_SETUP && !p_m_mISDNport->is_tones)
2794          || p_state==PORT_STATE_OUT_SETUP)
2795         {
2796                 /* sending release to endpoint */
2797                 while(p_epointlist)
2798                 {
2799                         message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2800                         message->param.disconnectinfo.cause = 16;
2801                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2802                         message_put(message);
2803                         /* remove epoint */
2804                         free_epointlist(p_epointlist);
2805                 }
2806                 /* sending release */
2807                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2808                 isdn_show_send_message(CC_RELEASE_COMPLETE | REQUEST, dmsg);
2809                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2810                 /* send cause */
2811                 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);
2812                 new_state(PORT_STATE_RELEASE);
2813                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2814                 p_m_delete = 1;
2815                 return;
2816         }
2817
2818         /* NT-MODE in setup state we must send PROCEEDING first */
2819         if (p_state==PORT_STATE_IN_SETUP)
2820         {
2821                 CALL_PROCEEDING_t *proceeding;
2822
2823                 /* sending proceeding */
2824                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2825                 isdn_show_send_message(CC_PROCEEDING | REQUEST, dmsg);
2826                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2827                 /* channel information */
2828                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2829                 /* progress information */
2830                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2831                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2832                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2833                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2834                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2835                 new_state(PORT_STATE_IN_PROCEEDING);
2836         }
2837
2838         /* sending disconnect */
2839         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2840         isdn_show_send_message(CC_DISCONNECT | REQUEST, dmsg);
2841         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2842         /* progress information */
2843         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2844          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2845          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2846         if (p_m_mISDNport->is_tones)
2847                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2848         /* send cause */
2849         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);
2850         /* send display */
2851         if (param->disconnectinfo.display[0])
2852                 p = param->disconnectinfo.display;
2853         if (p) if (*p && p_m_d_ntmode)
2854                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2855         new_state(PORT_STATE_OUT_DISCONNECT);
2856         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2857 }
2858
2859 /* MESSAGE_RELEASE */
2860 void Pdss1::message_release(unsigned long epoint_id, int message_id, union parameter *param)
2861 {
2862         int headerlen = (p_m_d_ntmode)?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
2863         RELEASE_t *release;
2864         RELEASE_COMPLETE_t *release_complete;
2865         DISCONNECT_t *disconnect;
2866         msg_t *dmsg;
2867         class Endpoint *epoint;
2868         char *p = NULL;
2869
2870         /* if we have incoming disconnected, we may release */
2871         if (p_state==PORT_STATE_IN_DISCONNECT
2872          || p_state==PORT_STATE_OUT_DISCONNECT)
2873         {
2874                 /* sending release */
2875                 dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
2876                 isdn_show_send_message(CC_RELEASE | REQUEST, dmsg);
2877                 release = (RELEASE_t *)(dmsg->data + headerlen);
2878                 /* send cause */
2879                 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);
2880                 new_state(PORT_STATE_RELEASE);
2881                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2882                 /* remove epoint */
2883                 remove_endpoint:
2884                 free_epointid(epoint_id);
2885                 if (p_m_d_ntmode)
2886                 {
2887                         if ((p_m_d_l3id&0xff00) == 0xff00)
2888                         {
2889                                 p_m_mISDNport->procids[p_m_d_l3id&0xff] = 0;
2890                                 printisdn("    procid 0x%x freed\n", p_m_d_l3id&0xff);
2891                         }
2892                 }
2893                 p_m_d_l3id = 0;
2894                 p_m_delete = 1;
2895                 return;
2896         }
2897         /* if we are on outgoing/incoming call setup, we may release complete */
2898         if (p_state==PORT_STATE_OUT_SETUP
2899          || p_state==PORT_STATE_IN_SETUP
2900 // NOTE: a bug in mISDNuser (see disconnect_req_out !!!)
2901          || p_state==PORT_STATE_OUT_PROCEEDING)
2902         {
2903                 /* sending release */
2904                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, p_m_d_l3id, sizeof(RELEASE_COMPLETE_t), p_m_d_ntmode);
2905                 isdn_show_send_message(CC_RELEASE_COMPLETE | REQUEST, dmsg);
2906                 release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + headerlen);
2907                 /* send cause */
2908                 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);
2909                 new_state(PORT_STATE_RELEASE);
2910                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2911                 goto remove_endpoint;
2912         }
2913
2914         /* NT-MODE in setup state we must send PROCEEDING first */
2915         if (p_m_d_ntmode && p_state==PORT_STATE_IN_SETUP)
2916         {
2917                 CALL_PROCEEDING_t *proceeding;
2918
2919                 /* sending proceeding */
2920                 dmsg = create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, p_m_d_l3id, sizeof(CALL_PROCEEDING_t), p_m_d_ntmode);
2921                 proceeding = (CALL_PROCEEDING_t *)(dmsg->data + headerlen);
2922                 /* channel information */
2923                 enc_ie_channel_id(&proceeding->CHANNEL_ID, dmsg, 1, p_m_b_channel);
2924                 /* progress information */
2925                 if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2926                  || p_capainfo.bearer_capa==INFO_BC_AUDIO
2927                  || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2928                         enc_ie_progress(&proceeding->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2929                 msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2930         }
2931
2932         /* sending disconnect */
2933         dmsg = create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, p_m_d_l3id, sizeof(DISCONNECT_t), p_m_d_ntmode);
2934         isdn_show_send_message(CC_DISCONNECT | REQUEST, dmsg);
2935         disconnect = (DISCONNECT_t *)(dmsg->data + headerlen);
2936         /* progress information */
2937         if (p_capainfo.bearer_capa==INFO_BC_SPEECH
2938          || p_capainfo.bearer_capa==INFO_BC_AUDIO
2939          || p_capainfo.bearer_capa==INFO_BC_DATAUNRESTRICTED_TONES)
2940         if (p_m_mISDNport->is_tones)
2941                 enc_ie_progress(&disconnect->PROGRESS, dmsg, 0, p_m_d_ntmode?1:5, 8);
2942         /* send cause */
2943         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);
2944         /* send display */
2945         epoint = find_epoint_id(epoint_id);
2946         if (param->disconnectinfo.display[0])
2947                 p = param->disconnectinfo.display;
2948         if (p) if (*p && p_m_d_ntmode)
2949                 enc_ie_display(&disconnect->DISPLAY, dmsg, (unsigned char *)p);
2950 #if 0
2951         /* sending release */
2952         dmsg = create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, p_m_d_l3id, sizeof(RELEASE_t), p_m_d_ntmode);
2953         isdn_show_send_message(CC_RELEASE | REQUEST, dmsg);
2954         release = (RELEASE_t *)(dmsg->data + headerlen);
2955         /* send cause */
2956         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);
2957 #endif
2958         new_state(PORT_STATE_RELEASE);
2959         msg_queue_tail(&p_m_mISDNport->downqueue, dmsg);
2960         free_epointid(epoint_id);
2961 //      p_m_delete = 1;
2962 }
2963
2964
2965 /*
2966  * endpoint sends messages to the port
2967  */
2968 int Pdss1::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
2969 {
2970         struct message *message;
2971
2972         if (PmISDN::message_epoint(epoint_id, message_id, param))
2973                 return(1);
2974
2975         switch(message_id)
2976         {
2977                 case MESSAGE_INFORMATION: /* overlap dialing */
2978                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) received dialing info '%s'.\n", p_name, param->information.number);
2979                 if (p_type==PORT_TYPE_DSS1_NT_OUT
2980                  && p_state!=PORT_STATE_OUT_OVERLAP
2981                  && p_state!=PORT_STATE_CONNECT
2982                  && p_state!=PORT_STATE_OUT_DISCONNECT
2983                  && p_state!=PORT_STATE_IN_DISCONNECT)
2984                 {
2985                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) (internal outgoing) ignoring information, invalid state.\n", p_name);
2986                         break;
2987                 }
2988                 if (p_type==PORT_TYPE_DSS1_TE_OUT
2989                  && p_state!=PORT_STATE_OUT_OVERLAP
2990                  && p_state!=PORT_STATE_OUT_PROCEEDING
2991                  && p_state!=PORT_STATE_OUT_ALERTING
2992                  && p_state!=PORT_STATE_CONNECT
2993                  && p_state!=PORT_STATE_OUT_DISCONNECT
2994                  && p_state!=PORT_STATE_IN_DISCONNECT)
2995                 {
2996                         if (options.deb & DEBUG_ISDN)
2997                                 PERROR("Pdss1(%s) (external outgoing) ignoring information, invalid state.\n", p_name);
2998                         break;
2999                 }
3000                 if ((p_type==PORT_TYPE_DSS1_NT_IN || p_type==PORT_TYPE_DSS1_TE_IN)
3001                  && p_state!=PORT_STATE_IN_OVERLAP
3002                  && p_state!=PORT_STATE_IN_PROCEEDING
3003                  && p_state!=PORT_STATE_IN_ALERTING
3004                  && p_state!=PORT_STATE_CONNECT
3005                  && p_state!=PORT_STATE_CONNECT_WAITING
3006                  && p_state!=PORT_STATE_OUT_DISCONNECT
3007                  && p_state!=PORT_STATE_IN_DISCONNECT)
3008                 {
3009                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) (incoming) ignoring information, invalid state.\n", p_name);
3010                         break;
3011                 }
3012                 message_information(epoint_id, message_id, param);
3013                 break;
3014
3015                 case MESSAGE_SETUP: /* dial-out command received from epoint */
3016                 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);
3017                 if (p_state!=PORT_STATE_IDLE
3018                  && p_state!=PORT_STATE_CONNECT)
3019                 {
3020                         PERROR("Pdss1(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
3021                         break;
3022                 }
3023                 if (p_epointlist && p_state==PORT_STATE_IDLE)
3024                 {
3025                         PERROR("Pdss1(%s): software error: epoint pointer is set in idle state, how bad!! exitting.\n", p_name);
3026                         exit(-1);
3027                 }
3028                 /* note: pri is a special case, because links must be up for pri */ 
3029                 if (p_m_mISDNport->l1link || p_m_mISDNport->pri || !p_m_mISDNport->ntmode || p_state!=PORT_STATE_IDLE)
3030                 {
3031                         /* LAYER 1 is up, or we may send */
3032                         message_setup(epoint_id, message_id, param);
3033                 } else {
3034                         iframe_t act;
3035                         /* LAYER 1 id down, so we queue */
3036                         p_m_d_queue = message_create(epoint_id, p_serial, EPOINT_TO_PORT, message_id);
3037                         memcpy(&p_m_d_queue->param, param, sizeof(union parameter));
3038                         /* attach us */
3039                         if (!(epointlist_new(epoint_id)))
3040                         {
3041                                 PERROR("no memory for epointlist\n");
3042                                 exit(-1);
3043                         }
3044                         /* activate link */
3045                         PDEBUG(DEBUG_ISDN, "the L1 is down, we try to establish the link NT portnum=%d (%s).\n", p_m_mISDNport->portnum, p_name);
3046                         act.prim = PH_ACTIVATE | REQUEST; 
3047                         act.addr = p_m_mISDNport->upper_id | FLG_MSG_DOWN;
3048                         act.dinfo = 0;
3049                         act.len = 0;
3050                         mISDN_write(mISDNdevice, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
3051 //                      /* set timeout */
3052 //                      p_m_mISDNport->l1timeout = now+3;
3053                 }
3054                 break;
3055
3056                 case MESSAGE_NOTIFY: /* display and notifications */
3057                 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);
3058                 message_notify(epoint_id, message_id, param);
3059                 break;
3060
3061                 case MESSAGE_FACILITY: /* facility message */
3062                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received facility.\n", p_name, p_callerinfo.id);
3063                 message_facility(epoint_id, message_id, param);
3064                 break;
3065
3066                 case MESSAGE_OVERLAP: /* more information is needed */
3067                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received setup acknowledge\n", p_name, p_callerinfo.id);
3068                 if (p_state!=PORT_STATE_IN_SETUP)
3069                 {
3070                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) ignoring setup acknowledge because isdn port is not incoming setup state.\n", p_name);
3071                         break;
3072                 }
3073                 message_overlap(epoint_id, message_id, param);
3074                 break;
3075
3076                 case MESSAGE_PROCEEDING: /* call of endpoint is proceeding */
3077                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received proceeding\n", p_name, p_callerinfo.id);
3078                 if (p_state!=PORT_STATE_IN_SETUP
3079                  && p_state!=PORT_STATE_IN_OVERLAP)
3080                 {
3081                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) ignoring: proceeding command not possible in current state.\n", p_name);
3082                         break;
3083                 }
3084                 message_proceeding(epoint_id, message_id, param);
3085                 break;
3086
3087                 case MESSAGE_ALERTING: /* call of endpoint is ringing */
3088                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received alerting\n", p_name, p_callerinfo.id);
3089                 if (p_state!=PORT_STATE_IN_SETUP
3090                  && p_state!=PORT_STATE_IN_OVERLAP
3091                  && p_state!=PORT_STATE_IN_PROCEEDING)
3092                 {
3093                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) ignoring: alerting command not possible in current state.\n", p_name);
3094                         break;
3095                 }
3096                 message_alerting(epoint_id, message_id, param);
3097                 break;
3098
3099                 case MESSAGE_CONNECT: /* call of endpoint is connected */
3100                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received connect\n", p_name, p_callerinfo.id);
3101                 if (p_state!=PORT_STATE_IN_SETUP
3102                  && p_state!=PORT_STATE_IN_OVERLAP
3103                  && p_state!=PORT_STATE_IN_PROCEEDING
3104                  && p_state!=PORT_STATE_IN_ALERTING
3105                  && !(p_state==PORT_STATE_CONNECT && p_m_d_ntmode))
3106                 {
3107                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) ignoring: connect command not possible in current state.\n", p_name);
3108                         break;
3109                 }
3110                 message_connect(epoint_id, message_id, param);
3111                 break;
3112
3113                 case MESSAGE_DISCONNECT: /* call has been disconnected */
3114                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received disconnect cause=%d\n", p_name, p_callerinfo.id, param->disconnectinfo.cause);
3115                 if (p_state!=PORT_STATE_IN_SETUP
3116                  && p_state!=PORT_STATE_IN_OVERLAP
3117                  && p_state!=PORT_STATE_IN_PROCEEDING
3118                  && p_state!=PORT_STATE_IN_ALERTING
3119                  && p_state!=PORT_STATE_OUT_SETUP
3120                  && p_state!=PORT_STATE_OUT_OVERLAP
3121                  && p_state!=PORT_STATE_OUT_PROCEEDING
3122                  && p_state!=PORT_STATE_OUT_ALERTING
3123                  && p_state!=PORT_STATE_CONNECT
3124                  && p_state!=PORT_STATE_CONNECT_WAITING)
3125                 {
3126                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) ignoring: disconnect command not possible in current state.\n", p_name);
3127                         break;
3128                 }
3129                 message_disconnect(epoint_id, message_id, param);
3130                 break;
3131
3132                 case MESSAGE_RELEASE: /* release isdn port */
3133                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received release cause=%d\n", p_name, p_callerinfo.id, param->disconnectinfo.cause);
3134                 if (p_state==PORT_STATE_RELEASE)
3135                 {
3136                         PDEBUG(DEBUG_ISDN, "Pdss1(%s) ignoring: release command not possible in RELEASE state.\n", p_name);
3137                         break;
3138                 }
3139                 message_release(epoint_id, message_id, param);
3140                 break;
3141
3142                 default:
3143                 PDEBUG(DEBUG_ISDN, "Pdss1(%s) isdn port with (caller id %s) received a wrong message: %d\n", p_name, p_callerinfo.id, message);
3144         }
3145
3146         return(1);
3147 }
3148
3149
3150
3151 /*
3152  * data from isdn-stack (layer-3) to pbx (port class)
3153  */
3154 /* NOTE: nt mode use mISDNuser_head_t as header */
3155 int stack2manager_nt(void *dat, void *arg)
3156 {
3157         class Port *port;
3158         class Pdss1 *pdss1;
3159         manager_t *mgr = (manager_t *)dat;
3160         msg_t *msg = (msg_t *)arg;
3161         mISDNuser_head_t *hh;
3162         struct mISDNport *mISDNport;
3163         char name[32];
3164
3165         if (!msg || !mgr)
3166                 return(-EINVAL);
3167
3168         /* note: nst is the first data feld of mISDNport */
3169         mISDNport = (struct mISDNport *)mgr->nst;
3170
3171         hh = (mISDNuser_head_t *)msg->data;
3172         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", hh->prim, hh->dinfo, msg->len);
3173
3174         /* find Port object of type ISDN */
3175         port = port_first;
3176         while(port)
3177         {
3178                 if (port->p_type == PORT_TYPE_DSS1_NT_IN || port->p_type == PORT_TYPE_DSS1_NT_OUT)
3179                 {
3180                         pdss1 = (class Pdss1 *)port;
3181 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", hh->dinfo, pdss1->p_m_d_l3id);
3182                         /* check out correct stack */
3183                         if (pdss1->p_m_mISDNport == mISDNport)
3184                         /* check out correct id */
3185                         if ((hh->dinfo&0xffff0000) == (pdss1->p_m_d_l3id&0xffff0000))
3186                         {
3187                                 /* found port, the message belongs to */
3188                                 break;
3189                         }
3190                 }
3191                 port = port->next;
3192         }
3193         if (port)
3194         {
3195                 /* if process id is master process, but a child disconnects */
3196                 if ((hh->dinfo&0x0000ff00)!=0x0000ff00 && (pdss1->p_m_d_l3id&0x0000ff00)==0x0000ff00 && hh->prim==(CC_DISCONNECT|INDICATION))
3197                 {
3198                         /* send special indication for child disconnect */
3199                         pdss1->disconnect_ind_i(hh->prim, hh->dinfo, msg->data);
3200                         free_msg(msg);
3201                         return(0);
3202                 }
3203                 /* if process id and layer 3 id matches */
3204                 if (hh->dinfo == pdss1->p_m_d_l3id)
3205                 {
3206                         pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3207                         free_msg(msg);
3208                         return(0);
3209                 }
3210         }
3211
3212         /* d-message */
3213         switch(hh->prim)
3214         {
3215                 case MGR_SHORTSTATUS | INDICATION:
3216                 case MGR_SHORTSTATUS | CONFIRM:
3217                 switch(hh->dinfo) {
3218                         case SSTATUS_L2_ESTABLISHED:
3219                         goto ss_estab;
3220                         case SSTATUS_L2_RELEASED:
3221                         goto ss_rel;
3222                 }
3223                 break;
3224
3225                 case DL_ESTABLISH | INDICATION:
3226                 case DL_ESTABLISH | CONFIRM:
3227                 ss_estab:
3228                 PDEBUG(DEBUG_ISDN, "establish data link (DL) NT portnum=%d TEI=%d\n", mISDNport->portnum, hh->dinfo);
3229                 if (mISDNport->ptp && hh->dinfo == 0)
3230                 {
3231                         if (mISDNport->l2establish)
3232                         {
3233                                 mISDNport->l2establish = 0;
3234                                 PDEBUG(DEBUG_ISDN, "the link became active before l2establish timer expiry.\n");
3235                         }
3236                         mISDNport->l2link = 1;
3237                         if (mISDNport->pri);
3238                                 mISDNport->l1link = 1; /* this is a hack, we also assume L1 to be active */
3239                 }
3240                 break;
3241
3242                 case DL_RELEASE | INDICATION:
3243                 case DL_RELEASE | CONFIRM:
3244                 ss_rel:
3245                 PDEBUG(DEBUG_ISDN, "release data link (DL) NT portnum=%d TEI=%d\n", mISDNport->portnum, hh->dinfo);
3246                 if (mISDNport->ptp && hh->dinfo == 0)
3247                 {
3248                         mISDNport->l2link = 0;
3249                         time(&mISDNport->l2establish);
3250                         PDEBUG(DEBUG_ISDN, "because we are ptp, we set a l2establish timer.\n");
3251                 }
3252                 break;
3253
3254                 case CC_SETUP | INDICATION:
3255                 /* creating port object */
3256                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3257                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL, 0, 0)))
3258                 {
3259                         RELEASE_COMPLETE_t *release_complete;
3260                         msg_t *dmsg;
3261
3262                         fprintf(stderr, "FATAL ERROR: cannot create port object.\n");
3263                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3264                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3265                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3266                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3267                         break;
3268                 }
3269                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3270                 break;
3271
3272                 case CC_RESUME | INDICATION:
3273                 /* creating port object */
3274                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3275                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_NT_IN, mISDNport, name, NULL)))
3276                 {
3277                         SUSPEND_REJECT_t *suspend_reject;
3278                         msg_t *dmsg;
3279
3280                         fprintf(stderr, "FATAL ERROR: cannot create port object.\n");
3281                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, hh->dinfo, sizeof(SUSPEND_REJECT_t), mISDNport->ntmode);
3282                         suspend_reject = (SUSPEND_REJECT_t *)(dmsg->data + mISDN_HEADER_LEN);
3283                         enc_ie_cause_standalone(mISDNport->ntmode?&suspend_reject->CAUSE:NULL, dmsg, (mISDNport->ntmode)?1:0, 47);
3284                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3285                         break;
3286                 }
3287                 pdss1->message_isdn(hh->prim, hh->dinfo, msg->data);
3288                 break;
3289
3290                 case CC_RELEASE_CR | INDICATION:
3291                 PDEBUG(DEBUG_ISDN, "%s: unhandled message from stack: call ref released (l3id=0x%x)\n", __FUNCTION__, hh->dinfo);
3292                 break;
3293
3294                 case CC_DISCONNECT | INDICATION:
3295
3296                 // fall throug
3297                 default:
3298                 PDEBUG(DEBUG_ISDN, "%s: unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", __FUNCTION__, hh->prim, hh->dinfo, msg->len);
3299                 return(-EINVAL);
3300         }
3301         free_msg(msg);
3302         return(0);
3303 }
3304
3305 /* NOTE: te mode use iframe_t as header */
3306 int stack2manager_te(struct mISDNport *mISDNport, msg_t *msg)
3307 {
3308         class Port *port;
3309         class Pdss1 *pdss1;
3310         iframe_t *frm;
3311         char name[32];
3312
3313         if (!msg || !mISDNport)
3314                 return(-EINVAL);
3315         frm = (iframe_t *)msg->data;
3316         PDEBUG(DEBUG_ISDN, "prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3317
3318         /* find Port object of type ISDN */
3319         port = port_first;
3320         while(port)
3321         {
3322                 if (port->p_type == PORT_TYPE_DSS1_TE_IN || port->p_type == PORT_TYPE_DSS1_TE_OUT)
3323                 {
3324                         pdss1 = (class Pdss1 *)port;
3325 //PDEBUG(DEBUG_ISDN, "comparing dinfo = 0x%x with l3id 0x%x\n", frm->dinfo, pdss1->p_m_d_l3id);
3326                         /* check out correct stack */
3327                         if (pdss1->p_m_mISDNport == mISDNport)
3328                         /* check out correct id */
3329                         if (frm->dinfo == pdss1->p_m_d_l3id)
3330                         {
3331                                 /* found port, the message belongs to */
3332                                 break;
3333                         }
3334                 }
3335                 port = port->next;
3336         }
3337         if (port)
3338         {
3339                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3340                 free_msg(msg);
3341                 return(0);
3342         }
3343
3344         /* process new cr (before setup indication) */
3345 //printf("prim = 0x%x, looking for 0x%x\n",frm->prim, (CC_NEW_CR | INDICATION));
3346         if (frm->prim == (CC_NEW_CR | INDICATION))
3347         {
3348
3349                 /* creating port object */
3350                 SPRINT(name, "%s-%d-in", mISDNport->ifport->interface->name, mISDNport->portnum);
3351                 if (!(pdss1 = new Pdss1(PORT_TYPE_DSS1_TE_IN, mISDNport, name, NULL)))
3352                 {
3353                         RELEASE_COMPLETE_t *release_complete;
3354                         msg_t *dmsg;
3355
3356                         fprintf(stderr, "FATAL ERROR: cannot create port object.\n");
3357                         dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, frm->dinfo, sizeof(RELEASE_COMPLETE_t), mISDNport->ntmode);
3358                         release_complete = (RELEASE_COMPLETE_t *)(dmsg->data + mISDN_HEADER_LEN);
3359                         enc_ie_cause_standalone(mISDNport->ntmode?&release_complete->CAUSE:NULL, dmsg, (mISDNport->ntmode)?LOCATION_PRIVATE_LOCAL:LOCATION_PRIVATE_REMOTE, 47);
3360                         msg_queue_tail(&mISDNport->downqueue, dmsg);
3361                         free_msg(msg);
3362                         return(0);
3363                 }
3364                 /* l3id will be set from dinfo at message_isdn */
3365                 pdss1->message_isdn(frm->prim, frm->dinfo, msg->data);
3366                 free_msg(msg);
3367                 return(0);
3368         }
3369
3370         if (frm->prim == (CC_RELEASE_CR | INDICATION))
3371         {
3372                 PDEBUG(DEBUG_ISDN, "unhandled message from stack: call ref released (l3id=0x%x)\n", frm->dinfo);
3373                 free_msg(msg);
3374                 return(0);
3375         }
3376         PDEBUG(DEBUG_ISDN, "unhandled message: prim(0x%x) dinfo(0x%x) msg->len(%d)\n", frm->prim, frm->dinfo, msg->len);
3377         return(-EINVAL);
3378 }
3379
3380
3381 /*
3382  * sending message that were queued during L1 activation
3383  * or releasing port if link is down
3384  */
3385 void setup_queue(struct mISDNport *mISDNport, int link)
3386 {
3387         class Port *port;
3388         class Pdss1 *pdss1;
3389         struct message *message;
3390
3391         if (!mISDNport->ntmode)
3392                 return;
3393
3394         /* check all port objects for pending message */
3395         port = port_first;
3396         while(port)
3397         {
3398                 if ((port->p_type&PORT_CLASS_mISDN_MASK) == PORT_CLASS_mISDN_DSS1)
3399                 {
3400                         pdss1 = (class Pdss1 *)port;
3401                         if (pdss1->p_m_mISDNport == mISDNport)
3402                         {
3403                                 if (pdss1->p_m_d_queue)
3404                                 {
3405                                         if (link)
3406                                         {
3407                                                 PDEBUG(DEBUG_ISDN, "the L1 became active, so we send queued message for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3408                                                 /* LAYER 1 is up, so we send */
3409                                                 pdss1->message_setup(pdss1->p_m_d_queue->id_from, pdss1->p_m_d_queue->type, &pdss1->p_m_d_queue->param);
3410                                                 message_free(pdss1->p_m_d_queue);
3411                                                 pdss1->p_m_d_queue = NULL;
3412                                         } else
3413                                         {
3414                                                 PDEBUG(DEBUG_ISDN, "the L1 became NOT active, so we release port for portnum=%d (%s).\n", mISDNport->portnum, pdss1->p_name);
3415                                                 message = message_create(pdss1->p_serial, pdss1->p_m_d_queue->id_from, PORT_TO_EPOINT, MESSAGE_RELEASE);
3416                                                 message->param.disconnectinfo.cause = 27;
3417                                                 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3418                                                 message_put(message);
3419                                                 pdss1->new_state(PORT_STATE_RELEASE);
3420                                                 pdss1->p_m_delete = 1;
3421                                         }
3422                                 }
3423                         }
3424                 }
3425                 port = port->next;
3426         }
3427 }
3428
3429
3430
3431
3432