3c7a8f33a625867f42d61bdad26bf859d4daa991
[asterisk/asterisk.git] / channels / misdn / isdn_msg_parser.c
1 /*
2  * Chan_Misdn -- Channel Driver for Asterisk
3  *
4  * Interface to mISDN
5  *
6  * Copyright (C) 2004, Christian Richter
7  *
8  * Christian Richter <crich@beronet.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 /*! \file
15  * \brief Interface to mISDN - message parser
16  * \author Christian Richter <crich@beronet.com>
17  */
18
19
20
21 #include "isdn_lib_intern.h"
22
23
24 #include "isdn_lib.h"
25
26 #include "ie.c"
27
28 /*!
29  * \internal
30  * \brief Build the name, number, name/number display message string
31  *
32  * \param display Display buffer to fill in
33  * \param display_length Length of the display buffer to fill in
34  * \param display_format Display format enumeration
35  * \param name Name string to use
36  * \param number Number string to use
37  *
38  * \return Nothing
39  */
40 static void build_display_str(char *display, size_t display_length, int display_format, const char *name, const char *number)
41 {
42         display[0] = 0;
43         switch (display_format) {
44         default:
45         case 0:         /* none */
46                 break;
47
48         case 1:         /* name */
49                 snprintf(display, display_length, "%s", name);
50                 break;
51
52         case 2:         /* number */
53                 snprintf(display, display_length, "%s", number);
54                 break;
55
56         case 3:         /* both */
57                 if (name[0] || number[0]) {
58                         snprintf(display, display_length, "\"%s\" <%s>", name, number);
59                 }
60                 break;
61         }
62 }
63
64 /*!
65  * \internal
66  * \brief Encode the Facility IE and put it into the message structure.
67  *
68  * \param ntmode Where the encoded facility was put when in NT mode.
69  * \param msg General message structure
70  * \param fac Data to encode into the facility ie.
71  * \param nt TRUE if in NT mode.
72  *
73  * \return Nothing
74  */
75 static void enc_ie_facility(unsigned char **ntmode, msg_t *msg, struct FacParm *fac, int nt)
76 {
77         int len;
78         Q931_info_t *qi;
79         unsigned char *p;
80         unsigned char buf[256];
81
82         len = encodeFac(buf, fac);
83         if (len <= 0) {
84                 /*
85                  * mISDN does not know how to build the requested facility structure
86                  * Clear facility information
87                  */
88                 fac->Function = Fac_None;
89                 return;
90         }
91
92         p = msg_put(msg, len);
93         if (nt) {
94                 *ntmode = p + 1;
95         } else {
96                 qi = (Q931_info_t *) (msg->data + mISDN_HEADER_LEN);
97                 qi->QI_ELEMENT(facility) = p - (unsigned char *) qi - sizeof(Q931_info_t);
98         }
99
100         memcpy(p, buf, len);
101
102         /* Clear facility information */
103         fac->Function = Fac_None;
104 }
105
106 /*!
107  * \internal
108  * \brief Decode the Facility IE.
109  *
110  * \param p Encoded facility ie data to decode. (NT mode)
111  * \param qi Encoded facility ie data to decode. (TE mode)
112  * \param fac Where to put the decoded facility ie data if it is available.
113  * \param nt TRUE if in NT mode.
114  * \param bc Associated B channel
115  *
116  * \return Nothing
117  */
118 static void dec_ie_facility(unsigned char *p, Q931_info_t *qi, struct FacParm *fac, int nt, struct misdn_bchannel *bc)
119 {
120         fac->Function = Fac_None;
121
122         if (!nt) {
123                 p = NULL;
124                 if (qi->QI_ELEMENT(facility)) {
125                         p = (unsigned char *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
126                 }
127         }
128         if (!p) {
129                 return;
130         }
131
132         if (decodeFac(p, fac)) {
133                 cb_log(3, bc->port, "Decoding facility ie failed! Unrecognized facility message?\n");
134         }
135 }
136
137
138
139 static void set_channel(struct misdn_bchannel *bc, int channel)
140 {
141
142         cb_log(3,bc->port,"set_channel: bc->channel:%d channel:%d\n", bc->channel, channel);
143
144
145         if (channel==0xff) {
146                 /* any channel */
147                 channel=-1;
148         }
149
150         /*  ALERT: is that everytime true ?  */
151         if (channel > 0 && bc->nt ) {
152
153                 if (bc->channel && ( bc->channel != 0xff) ) {
154                         cb_log(0,bc->port,"We already have a channel (%d)\n", bc->channel);
155                 } else {
156                         bc->channel = channel;
157                         cb_event(EVENT_NEW_CHANNEL,bc,NULL);
158                 }
159         }
160
161         if (channel > 0 && !bc->nt ) {
162                 bc->channel = channel;
163                 cb_event(EVENT_NEW_CHANNEL,bc,NULL);
164         }
165 }
166
167 static void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
168 {
169         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
170         CALL_PROCEEDING_t *proceeding = (CALL_PROCEEDING_t *) (msg->data + HEADER_LEN);
171         //struct misdn_stack *stack=get_stack_by_bc(bc);
172
173         {
174                 int  exclusive, channel;
175                 dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc);
176
177                 set_channel(bc,channel);
178
179         }
180
181         dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
182
183         dec_ie_facility(proceeding->FACILITY, (Q931_info_t *) proceeding, &bc->fac_in, nt, bc);
184
185         /* dec_ie_redir_dn */
186
187 #ifdef DEBUG
188         printf("Parsing PROCEEDING Msg\n");
189 #endif
190 }
191 static msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
192 {
193         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
194         CALL_PROCEEDING_t *proceeding;
195         msg_t *msg =(msg_t*)create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING,  bc?bc->l3_id:-1, sizeof(CALL_PROCEEDING_t) ,nt);
196
197         proceeding=(CALL_PROCEEDING_t*)((msg->data+HEADER_LEN));
198
199         enc_ie_channel_id(&proceeding->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
200
201         if (nt)
202                 enc_ie_progress(&proceeding->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
203
204         if (bc->fac_out.Function != Fac_None) {
205                 enc_ie_facility(&proceeding->FACILITY, msg, &bc->fac_out, nt);
206         }
207
208         /* enc_ie_redir_dn */
209
210 #ifdef DEBUG
211         printf("Building PROCEEDING Msg\n");
212 #endif
213         return msg;
214 }
215
216 static void parse_alerting (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
217 {
218         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
219         ALERTING_t *alerting = (ALERTING_t *) (msg->data + HEADER_LEN);
220         //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);
221
222         dec_ie_facility(alerting->FACILITY, (Q931_info_t *) alerting, &bc->fac_in, nt, bc);
223
224         /* dec_ie_redir_dn */
225
226         dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)alerting, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
227
228 #ifdef DEBUG
229         printf("Parsing ALERTING Msg\n");
230 #endif
231
232
233 }
234
235 static msg_t *build_alerting (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
236 {
237         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
238         ALERTING_t *alerting;
239         msg_t *msg =(msg_t*)create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING,  bc?bc->l3_id:-1, sizeof(ALERTING_t) ,nt);
240
241         alerting=(ALERTING_t*)((msg->data+HEADER_LEN));
242
243         enc_ie_channel_id(&alerting->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
244
245         if (nt)
246                 enc_ie_progress(&alerting->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
247
248         if (bc->fac_out.Function != Fac_None) {
249                 enc_ie_facility(&alerting->FACILITY, msg, &bc->fac_out, nt);
250         }
251
252         /* enc_ie_redir_dn */
253
254 #ifdef DEBUG
255         printf("Building ALERTING Msg\n");
256 #endif
257         return msg;
258 }
259
260
261 static void parse_progress (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
262 {
263         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
264         PROGRESS_t *progress = (PROGRESS_t *) (msg->data + HEADER_LEN);
265         //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN);
266
267         dec_ie_progress(progress->PROGRESS, (Q931_info_t *)progress, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
268
269         dec_ie_facility(progress->FACILITY, (Q931_info_t *) progress, &bc->fac_in, nt, bc);
270
271 #ifdef DEBUG
272         printf("Parsing PROGRESS Msg\n");
273 #endif
274 }
275
276 static msg_t *build_progress (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
277 {
278         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
279         PROGRESS_t *progress;
280         msg_t *msg =(msg_t*)create_l3msg(CC_PROGRESS | REQUEST, MT_PROGRESS,  bc?bc->l3_id:-1, sizeof(PROGRESS_t) ,nt);
281
282         progress=(PROGRESS_t*)((msg->data+HEADER_LEN));
283
284         if (bc->fac_out.Function != Fac_None) {
285                 enc_ie_facility(&progress->FACILITY, msg, &bc->fac_out, nt);
286         }
287
288 #ifdef DEBUG
289         printf("Building PROGRESS Msg\n");
290 #endif
291         return msg;
292 }
293
294 #if defined(AST_MISDN_ENHANCEMENTS)
295 /*!
296  * \internal
297  * \brief Extract the SETUP message's BC, HLC, and LLC encoded ie contents.
298  *
299  * \param setup Indexed setup message contents
300  * \param nt TRUE if in NT mode.
301  * \param bc Associated B channel
302  *
303  * \return Nothing
304  */
305 static void extract_setup_Bc_Hlc_Llc(SETUP_t *setup, int nt, struct misdn_bchannel *bc)
306 {
307         __u8 *p;
308         Q931_info_t *qi;
309
310         qi = (Q931_info_t *) setup;
311
312         /* Extract Bearer Capability */
313         if (nt) {
314                 p = (__u8 *) setup->BEARER;
315         } else {
316                 if (qi->QI_ELEMENT(bearer_capability)) {
317                         p = (__u8 *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(bearer_capability) + 1;
318                 } else {
319                         p = NULL;
320                 }
321         }
322         if (!p || *p == 0 || sizeof(bc->setup_bc_hlc_llc.Bc.Contents) < *p) {
323                 bc->setup_bc_hlc_llc.Bc.Length = 0;
324         } else {
325                 bc->setup_bc_hlc_llc.Bc.Length = *p;
326                 memcpy(bc->setup_bc_hlc_llc.Bc.Contents, p + 1, *p);
327         }
328
329         /* Extract Low Layer Compatibility */
330         if (nt) {
331                 p = (__u8 *) setup->LLC;
332         } else {
333                 if (qi->QI_ELEMENT(llc)) {
334                         p = (__u8 *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(llc) + 1;
335                 } else {
336                         p = NULL;
337                 }
338         }
339         if (!p || *p == 0 || sizeof(bc->setup_bc_hlc_llc.Llc.Contents) < *p) {
340                 bc->setup_bc_hlc_llc.Llc.Length = 0;
341         } else {
342                 bc->setup_bc_hlc_llc.Llc.Length = *p;
343                 memcpy(bc->setup_bc_hlc_llc.Llc.Contents, p + 1, *p);
344         }
345
346         /* Extract High Layer Compatibility */
347         if (nt) {
348                 p = (__u8 *) setup->HLC;
349         } else {
350                 if (qi->QI_ELEMENT(hlc)) {
351                         p = (__u8 *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(hlc) + 1;
352                 } else {
353                         p = NULL;
354                 }
355         }
356         if (!p || *p == 0 || sizeof(bc->setup_bc_hlc_llc.Hlc.Contents) < *p) {
357                 bc->setup_bc_hlc_llc.Hlc.Length = 0;
358         } else {
359                 bc->setup_bc_hlc_llc.Hlc.Length = *p;
360                 memcpy(bc->setup_bc_hlc_llc.Hlc.Contents, p + 1, *p);
361         }
362 }
363 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
364
365 static void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
366 {
367         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
368         SETUP_t *setup = (SETUP_t *) (msg->data + HEADER_LEN);
369         Q931_info_t *qi = (Q931_info_t *) (msg->data + HEADER_LEN);
370         int type;
371         int plan;
372         int present;
373         int screen;
374         int reason;
375
376 #ifdef DEBUG
377         printf("Parsing SETUP Msg\n");
378 #endif
379
380         dec_ie_calling_pn(setup->CALLING_PN, qi, &type, &plan, &present, &screen, bc->caller.number, sizeof(bc->caller.number), nt, bc);
381         bc->caller.number_type = type;
382         bc->caller.number_plan = plan;
383         switch (present) {
384         default:
385         case 0:
386                 bc->caller.presentation = 0;    /* presentation allowed */
387                 break;
388         case 1:
389                 bc->caller.presentation = 1;    /* presentation restricted */
390                 break;
391         case 2:
392                 bc->caller.presentation = 2;    /* Number not available */
393                 break;
394         }
395         if (0 <= screen) {
396                 bc->caller.screening = screen;
397         } else {
398                 bc->caller.screening = 0;       /* Unscreened */
399         }
400
401         dec_ie_facility(setup->FACILITY, (Q931_info_t *) setup, &bc->fac_in, nt, bc);
402
403         dec_ie_called_pn(setup->CALLED_PN, (Q931_info_t *) setup, &type, &plan, bc->dialed.number, sizeof(bc->dialed.number), nt, bc);
404         bc->dialed.number_type = type;
405         bc->dialed.number_plan = plan;
406
407         dec_ie_keypad(setup->KEYPAD, (Q931_info_t *) setup, bc->keypad, sizeof(bc->keypad), nt, bc);
408
409         dec_ie_complete(setup->COMPLETE, (Q931_info_t *) setup, &bc->sending_complete, nt, bc);
410
411         dec_ie_redir_nr(setup->REDIR_NR, (Q931_info_t *) setup, &type, &plan, &present, &screen, &reason, bc->redirecting.from.number, sizeof(bc->redirecting.from.number), nt, bc);
412         bc->redirecting.from.number_type = type;
413         bc->redirecting.from.number_plan = plan;
414         switch (present) {
415         default:
416         case 0:
417                 bc->redirecting.from.presentation = 0;  /* presentation allowed */
418                 break;
419         case 1:
420                 bc->redirecting.from.presentation = 1;  /* presentation restricted */
421                 break;
422         case 2:
423                 bc->redirecting.from.presentation = 2;  /* Number not available */
424                 break;
425         }
426         if (0 <= screen) {
427                 bc->redirecting.from.screening = screen;
428         } else {
429                 bc->redirecting.from.screening = 0;     /* Unscreened */
430         }
431         if (0 <= reason) {
432                 bc->redirecting.reason = reason;
433         } else {
434                 bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
435         }
436
437         {
438                 int  coding, capability, mode, rate, multi, user, async, urate, stopbits, dbits, parity;
439
440                 dec_ie_bearer(setup->BEARER, (Q931_info_t *)setup, &coding, &capability, &mode, &rate, &multi, &user, &async, &urate, &stopbits, &dbits, &parity, nt,bc);
441                 switch (capability) {
442                 case -1: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
443                         break;
444                 case 0: bc->capability=INFO_CAPABILITY_SPEECH;
445                         break;
446                 case 18: bc->capability=INFO_CAPABILITY_VIDEO;
447                         break;
448                 case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
449                         bc->user1 = user;
450                         bc->urate = urate;
451
452                         bc->rate = rate;
453                         bc->mode = mode;
454                         break;
455                 case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED;
456                         break;
457                 default:
458                         break;
459                 }
460
461                 switch(user) {
462                 case 2:
463                         bc->law=INFO_CODEC_ULAW;
464                         break;
465                 case 3:
466                         bc->law=INFO_CODEC_ALAW;
467                         break;
468                 default:
469                         bc->law=INFO_CODEC_ALAW;
470
471                 }
472
473                 bc->capability=capability;
474         }
475         {
476                 int  exclusive, channel;
477                 dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)setup, &exclusive, &channel, nt,bc);
478
479                 set_channel(bc,channel);
480         }
481
482         {
483                 int  protocol ;
484                 dec_ie_useruser(setup->USER_USER, (Q931_info_t *)setup, &protocol, bc->uu, &bc->uulen, nt,bc);
485                 if (bc->uulen) cb_log(1,bc->port,"USERUESRINFO:%s\n",bc->uu);
486                 else
487                 cb_log(1,bc->port,"NO USERUESRINFO\n");
488         }
489
490         dec_ie_progress(setup->PROGRESS, (Q931_info_t *)setup, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
491
492 #if defined(AST_MISDN_ENHANCEMENTS)
493         extract_setup_Bc_Hlc_Llc(setup, nt, bc);
494 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
495 }
496
497 #define ANY_CHANNEL 0xff /* IE attribute for 'any channel' */
498 static msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
499 {
500         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
501         SETUP_t *setup;
502         msg_t *msg =(msg_t*)create_l3msg(CC_SETUP | REQUEST, MT_SETUP,  bc?bc->l3_id:-1, sizeof(SETUP_t) ,nt);
503
504         setup=(SETUP_t*)((msg->data+HEADER_LEN));
505
506         if (bc->channel == 0 || bc->channel == ANY_CHANNEL || bc->channel==-1)
507                 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 0, bc->channel, nt,bc);
508         else
509                 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
510
511         if (bc->fac_out.Function != Fac_None) {
512                 enc_ie_facility(&setup->FACILITY, msg, &bc->fac_out, nt);
513         }
514
515         enc_ie_calling_pn(&setup->CALLING_PN, msg, bc->caller.number_type, bc->caller.number_plan,
516                 bc->caller.presentation, bc->caller.screening, bc->caller.number, nt, bc);
517
518         if (bc->dialed.number[0]) {
519                 enc_ie_called_pn(&setup->CALLED_PN, msg, bc->dialed.number_type, bc->dialed.number_plan, bc->dialed.number, nt, bc);
520         }
521
522         switch (bc->outgoing_colp) {
523         case 0:/* pass */
524         case 1:/* restricted */
525                 if (bc->redirecting.from.number[0]) {
526 #if 1
527                         /* ETSI and Q.952 do not define the screening field */
528                         enc_ie_redir_nr(&setup->REDIR_NR, msg, bc->redirecting.from.number_type,
529                                 bc->redirecting.from.number_plan, bc->redirecting.from.presentation, 0,
530                                 bc->redirecting.reason, bc->redirecting.from.number, nt, bc);
531 #else
532                         /* Q.931 defines the screening field */
533                         enc_ie_redir_nr(&setup->REDIR_NR, msg, bc->redirecting.from.number_type,
534                                 bc->redirecting.from.number_plan, bc->redirecting.from.presentation,
535                                 bc->redirecting.from.screening, bc->redirecting.reason,
536                                 bc->redirecting.from.number, nt, bc);
537 #endif
538                 }
539                 break;
540         default:
541                 break;
542         }
543
544         if (bc->keypad[0]) {
545                 enc_ie_keypad(&setup->KEYPAD, msg, bc->keypad, nt,bc);
546         }
547
548
549
550         if (*bc->display) {
551                 enc_ie_display(&setup->DISPLAY, msg, bc->display, nt, bc);
552         } else if (nt && bc->caller.presentation == 0) {
553                 char display[sizeof(bc->display)];
554
555                 /* Presentation is allowed */
556                 build_display_str(display, sizeof(display), bc->display_setup, bc->caller.name, bc->caller.number);
557                 if (display[0]) {
558                         enc_ie_display(&setup->DISPLAY, msg, display, nt, bc);
559                 }
560         }
561
562         {
563                 int coding = 0;
564                 int capability;
565                 int mode = 0;   /* 2 for packet! */
566                 int user;
567                 int rate = 0x10;
568
569                 switch (bc->law) {
570                 case INFO_CODEC_ULAW: user=2;
571                         break;
572                 case INFO_CODEC_ALAW: user=3;
573                         break;
574                 default:
575                         user=3;
576                 }
577
578                 switch (bc->capability) {
579                 case INFO_CAPABILITY_SPEECH: capability = 0;
580                         break;
581                 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8;
582                         user=-1;
583                         mode=bc->mode;
584                         rate=bc->rate;
585                         break;
586                 case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9;
587                         user=-1;
588                         break;
589                 default:
590                         capability=bc->capability;
591                 }
592
593                 enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc);
594         }
595
596         if (bc->sending_complete) {
597                 enc_ie_complete(&setup->COMPLETE,msg, bc->sending_complete, nt, bc);
598         }
599
600         if (bc->uulen) {
601                 int  protocol=4;
602                 enc_ie_useruser(&setup->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
603                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
604         }
605
606 #if defined(AST_MISDN_ENHANCEMENTS)
607         extract_setup_Bc_Hlc_Llc(setup, nt, bc);
608 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
609
610 #ifdef DEBUG
611         printf("Building SETUP Msg\n");
612 #endif
613         return msg;
614 }
615
616 static void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
617 {
618         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
619         CONNECT_t *connect = (CONNECT_t *) (msg->data + HEADER_LEN);
620         int type;
621         int plan;
622         int pres;
623         int screen;
624
625         bc->ces = connect->ces;
626
627         dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
628
629         dec_ie_connected_pn(connect->CONNECT_PN, (Q931_info_t *) connect, &type, &plan,
630                 &pres, &screen, bc->connected.number, sizeof(bc->connected.number), nt, bc);
631         bc->connected.number_type = type;
632         bc->connected.number_plan = plan;
633         switch (pres) {
634         default:
635         case 0:
636                 bc->connected.presentation = 0; /* presentation allowed */
637                 break;
638         case 1:
639                 bc->connected.presentation = 1; /* presentation restricted */
640                 break;
641         case 2:
642                 bc->connected.presentation = 2; /* Number not available */
643                 break;
644         }
645         if (0 <= screen) {
646                 bc->connected.screening = screen;
647         } else {
648                 bc->connected.screening = 0;    /* Unscreened */
649         }
650
651         dec_ie_facility(connect->FACILITY, (Q931_info_t *) connect, &bc->fac_in, nt, bc);
652
653         /*
654                 cb_log(1,bc->port,"CONNETED PN: %s cpn_dialplan:%d\n", connected_pn, type);
655         */
656
657 #ifdef DEBUG
658         printf("Parsing CONNECT Msg\n");
659 #endif
660 }
661
662 static msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
663 {
664         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
665         CONNECT_t *connect;
666         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt);
667
668         cb_log(6,bc->port,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,nt);
669
670         connect=(CONNECT_t*)((msg->data+HEADER_LEN));
671
672         if (nt) {
673                 time_t now;
674                 time(&now);
675                 enc_ie_date(&connect->DATE, msg, now, nt,bc);
676         }
677
678         switch (bc->outgoing_colp) {
679         case 0:/* pass */
680         case 1:/* restricted */
681                 enc_ie_connected_pn(&connect->CONNECT_PN, msg, bc->connected.number_type,
682                         bc->connected.number_plan, bc->connected.presentation,
683                         bc->connected.screening, bc->connected.number, nt, bc);
684                 break;
685         default:
686                 break;
687         }
688
689         if (nt && bc->connected.presentation == 0) {
690                 char display[sizeof(bc->display)];
691
692                 /* Presentation is allowed */
693                 build_display_str(display, sizeof(display), bc->display_connected, bc->connected.name, bc->connected.number);
694                 if (display[0]) {
695                         enc_ie_display(&connect->DISPLAY, msg, display, nt, bc);
696                 }
697         }
698
699         if (bc->fac_out.Function != Fac_None) {
700                 enc_ie_facility(&connect->FACILITY, msg, &bc->fac_out, nt);
701         }
702
703 #ifdef DEBUG
704         printf("Building CONNECT Msg\n");
705 #endif
706         return msg;
707 }
708
709 static void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
710 {
711         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
712         SETUP_ACKNOWLEDGE_t *setup_acknowledge = (SETUP_ACKNOWLEDGE_t *) (msg->data + HEADER_LEN);
713
714         {
715                 int  exclusive, channel;
716                 dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
717
718
719                 set_channel(bc, channel);
720         }
721
722         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
723
724         dec_ie_facility(setup_acknowledge->FACILITY, (Q931_info_t *) setup_acknowledge, &bc->fac_in, nt, bc);
725
726 #ifdef DEBUG
727         printf("Parsing SETUP_ACKNOWLEDGE Msg\n");
728 #endif
729
730
731 }
732
733 static msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
734 {
735         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
736         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
737         msg_t *msg =(msg_t*)create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SETUP_ACKNOWLEDGE_t) ,nt);
738
739         setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
740
741         enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
742
743         if (nt)
744                 enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
745
746         if (bc->fac_out.Function != Fac_None) {
747                 enc_ie_facility(&setup_acknowledge->FACILITY, msg, &bc->fac_out, nt);
748         }
749
750 #ifdef DEBUG
751         printf("Building SETUP_ACKNOWLEDGE Msg\n");
752 #endif
753         return msg;
754 }
755
756 static void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
757 {
758 #ifdef DEBUG
759         printf("Parsing CONNECT_ACKNOWLEDGE Msg\n");
760 #endif
761
762
763 }
764
765 static msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
766 {
767         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
768         CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
769         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_ACKNOWLEDGE_t) ,nt);
770
771         connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
772
773         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
774
775 #ifdef DEBUG
776         printf("Building CONNECT_ACKNOWLEDGE Msg\n");
777 #endif
778         return msg;
779 }
780
781 static void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
782 {
783 #ifdef DEBUG
784         printf("Parsing USER_INFORMATION Msg\n");
785 #endif
786
787
788 }
789
790 static msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
791 {
792         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
793         USER_INFORMATION_t *user_information;
794         msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION,  bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt);
795
796         user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN));
797
798 #ifdef DEBUG
799         printf("Building USER_INFORMATION Msg\n");
800 #endif
801         return msg;
802 }
803
804 static void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
805 {
806 #ifdef DEBUG
807         printf("Parsing SUSPEND_REJECT Msg\n");
808 #endif
809
810
811 }
812
813 static msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
814 {
815         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
816         SUSPEND_REJECT_t *suspend_reject;
817         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT,  bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt);
818
819         suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN));
820
821 #ifdef DEBUG
822         printf("Building SUSPEND_REJECT Msg\n");
823 #endif
824         return msg;
825 }
826
827 static void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
828 {
829 #ifdef DEBUG
830         printf("Parsing RESUME_REJECT Msg\n");
831 #endif
832
833
834 }
835
836 static msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
837 {
838         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
839         RESUME_REJECT_t *resume_reject;
840         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT,  bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt);
841
842         resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN));
843
844 #ifdef DEBUG
845         printf("Building RESUME_REJECT Msg\n");
846 #endif
847         return msg;
848 }
849
850 static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
851 {
852 #ifdef DEBUG
853         printf("Parsing HOLD Msg\n");
854 #endif
855
856
857 }
858
859 static msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
860 {
861         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
862         HOLD_t *hold;
863         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD,  bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt);
864
865         hold=(HOLD_t*)((msg->data+HEADER_LEN));
866
867 #ifdef DEBUG
868         printf("Building HOLD Msg\n");
869 #endif
870         return msg;
871 }
872
873 static void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
874 {
875 #ifdef DEBUG
876         printf("Parsing SUSPEND Msg\n");
877 #endif
878
879
880 }
881
882 static msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
883 {
884         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
885         SUSPEND_t *suspend;
886         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND,  bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt);
887
888         suspend=(SUSPEND_t*)((msg->data+HEADER_LEN));
889
890 #ifdef DEBUG
891         printf("Building SUSPEND Msg\n");
892 #endif
893         return msg;
894 }
895
896 static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
897 {
898 #ifdef DEBUG
899         printf("Parsing RESUME Msg\n");
900 #endif
901
902
903 }
904
905 static msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
906 {
907         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
908         RESUME_t *resume;
909         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME,  bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt);
910
911         resume=(RESUME_t*)((msg->data+HEADER_LEN));
912
913 #ifdef DEBUG
914         printf("Building RESUME Msg\n");
915 #endif
916         return msg;
917 }
918
919 static void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
920 {
921 #ifdef DEBUG
922         printf("Parsing HOLD_ACKNOWLEDGE Msg\n");
923 #endif
924
925
926 }
927
928 static msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
929 {
930         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
931         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
932         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt);
933
934         hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
935
936 #ifdef DEBUG
937         printf("Building HOLD_ACKNOWLEDGE Msg\n");
938 #endif
939         return msg;
940 }
941
942 static void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
943 {
944 #ifdef DEBUG
945         printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n");
946 #endif
947
948
949 }
950
951 static msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
952 {
953         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
954         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
955         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt);
956
957         suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
958
959 #ifdef DEBUG
960         printf("Building SUSPEND_ACKNOWLEDGE Msg\n");
961 #endif
962         return msg;
963 }
964
965 static void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
966 {
967 #ifdef DEBUG
968         printf("Parsing RESUME_ACKNOWLEDGE Msg\n");
969 #endif
970
971
972 }
973
974 static msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
975 {
976         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
977         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
978         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt);
979
980         resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
981
982 #ifdef DEBUG
983         printf("Building RESUME_ACKNOWLEDGE Msg\n");
984 #endif
985         return msg;
986 }
987
988 static void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
989 {
990 #ifdef DEBUG
991         printf("Parsing HOLD_REJECT Msg\n");
992 #endif
993
994
995 }
996
997 static msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
998 {
999         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1000         HOLD_REJECT_t *hold_reject;
1001         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT,  bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt);
1002
1003         hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN));
1004
1005 #ifdef DEBUG
1006         printf("Building HOLD_REJECT Msg\n");
1007 #endif
1008         return msg;
1009 }
1010
1011 static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1012 {
1013 #ifdef DEBUG
1014         printf("Parsing RETRIEVE Msg\n");
1015 #endif
1016
1017
1018 }
1019
1020 static msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1021 {
1022         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1023         RETRIEVE_t *retrieve;
1024         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt);
1025
1026         retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN));
1027
1028 #ifdef DEBUG
1029         printf("Building RETRIEVE Msg\n");
1030 #endif
1031         return msg;
1032 }
1033
1034 static void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1035 {
1036 #ifdef DEBUG
1037         printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n");
1038 #endif
1039
1040
1041 }
1042
1043 static msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1044 {
1045         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1046         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1047         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt);
1048
1049         retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
1050
1051         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
1052 #ifdef DEBUG
1053         printf("Building RETRIEVE_ACKNOWLEDGE Msg\n");
1054 #endif
1055         return msg;
1056 }
1057
1058 static void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1059 {
1060 #ifdef DEBUG
1061         printf("Parsing RETRIEVE_REJECT Msg\n");
1062 #endif
1063
1064
1065 }
1066
1067 static msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1068 {
1069         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1070         RETRIEVE_REJECT_t *retrieve_reject;
1071         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT,  bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt);
1072
1073         retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN));
1074
1075 #ifdef DEBUG
1076         printf("Building RETRIEVE_REJECT Msg\n");
1077 #endif
1078         return msg;
1079 }
1080
1081 static void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1082 {
1083         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1084         DISCONNECT_t *disconnect = (DISCONNECT_t *) (msg->data + HEADER_LEN);
1085         int location;
1086         int cause;
1087         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc);
1088         if (cause>0) bc->cause=cause;
1089
1090         dec_ie_facility(disconnect->FACILITY, (Q931_info_t *) disconnect, &bc->fac_in, nt, bc);
1091
1092         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
1093 #ifdef DEBUG
1094         printf("Parsing DISCONNECT Msg\n");
1095 #endif
1096
1097
1098 }
1099
1100 static msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1101 {
1102         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1103         DISCONNECT_t *disconnect;
1104         msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT,  bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt);
1105
1106         disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN));
1107
1108         enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
1109         if (nt) {
1110                 enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt ? 1 : 5, 8, nt, bc);
1111         }
1112
1113         if (bc->fac_out.Function != Fac_None) {
1114                 enc_ie_facility(&disconnect->FACILITY, msg, &bc->fac_out, nt);
1115         }
1116
1117         if (bc->uulen) {
1118                 int  protocol=4;
1119                 enc_ie_useruser(&disconnect->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1120                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1121         }
1122
1123 #ifdef DEBUG
1124         printf("Building DISCONNECT Msg\n");
1125 #endif
1126         return msg;
1127 }
1128
1129 static void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1130 {
1131         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1132         RESTART_t *restart = (RESTART_t *) (msg->data + HEADER_LEN);
1133
1134         struct misdn_stack *stack=get_stack_by_bc(bc);
1135
1136 #ifdef DEBUG
1137         printf("Parsing RESTART Msg\n");
1138 #endif
1139
1140         {
1141                 int  exclusive;
1142                 dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &bc->restart_channel, nt,bc);
1143                 cb_log(3, stack->port, "CC_RESTART Request on channel:%d on this port.\n", bc->restart_channel);
1144         }
1145
1146 }
1147
1148 static msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1149 {
1150         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1151         RESTART_t *restart;
1152         msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART,  bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt);
1153
1154         restart=(RESTART_t*)((msg->data+HEADER_LEN));
1155
1156 #ifdef DEBUG
1157         printf("Building RESTART Msg\n");
1158 #endif
1159
1160         if (bc->channel > 0) {
1161                 enc_ie_channel_id(&restart->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
1162                 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x80, nt, bc);
1163         } else {
1164                 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x87, nt, bc);
1165         }
1166
1167         cb_log(0,bc->port, "Restarting channel %d\n", bc->channel);
1168         return msg;
1169 }
1170
1171 static void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1172 {
1173         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1174         RELEASE_t *release = (RELEASE_t *) (msg->data + HEADER_LEN);
1175         int location;
1176         int cause;
1177
1178         dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc);
1179         if (cause>0) bc->cause=cause;
1180
1181         dec_ie_facility(release->FACILITY, (Q931_info_t *) release, &bc->fac_in, nt, bc);
1182
1183 #ifdef DEBUG
1184         printf("Parsing RELEASE Msg\n");
1185 #endif
1186
1187
1188 }
1189
1190 static msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1191 {
1192         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1193         RELEASE_t *release;
1194         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE,  bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt);
1195
1196         release=(RELEASE_t*)((msg->data+HEADER_LEN));
1197
1198         if (bc->out_cause>= 0)
1199                 enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1200
1201         if (bc->fac_out.Function != Fac_None) {
1202                 enc_ie_facility(&release->FACILITY, msg, &bc->fac_out, nt);
1203         }
1204
1205         if (bc->uulen) {
1206                 int  protocol=4;
1207                 enc_ie_useruser(&release->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1208                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1209         }
1210
1211 #ifdef DEBUG
1212         printf("Building RELEASE Msg\n");
1213 #endif
1214         return msg;
1215 }
1216
1217 static void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1218 {
1219         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1220         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *) (msg->data + HEADER_LEN);
1221         int location;
1222         int cause;
1223         iframe_t *frm = (iframe_t*) msg->data;
1224
1225         struct misdn_stack *stack=get_stack_by_bc(bc);
1226         mISDNuser_head_t *hh;
1227         hh=(mISDNuser_head_t*)msg->data;
1228
1229         /*hh=(mISDN_head_t*)msg->data;
1230         mISDN_head_t *hh;*/
1231
1232         if (nt) {
1233                 if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1234                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n");
1235                         return;
1236                 }
1237         } else {
1238                 if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1239                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n");
1240                         return;
1241                 }
1242         }
1243         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc);
1244         if (cause>0) bc->cause=cause;
1245
1246         dec_ie_facility(release_complete->FACILITY, (Q931_info_t *) release_complete, &bc->fac_in, nt, bc);
1247
1248 #ifdef DEBUG
1249         printf("Parsing RELEASE_COMPLETE Msg\n");
1250 #endif
1251 }
1252
1253 static msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1254 {
1255         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1256         RELEASE_COMPLETE_t *release_complete;
1257         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE,  bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt);
1258
1259         release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN));
1260
1261         enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1262
1263         if (bc->fac_out.Function != Fac_None) {
1264                 enc_ie_facility(&release_complete->FACILITY, msg, &bc->fac_out, nt);
1265         }
1266
1267         if (bc->uulen) {
1268                 int  protocol=4;
1269                 enc_ie_useruser(&release_complete->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1270                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1271         }
1272
1273 #ifdef DEBUG
1274         printf("Building RELEASE_COMPLETE Msg\n");
1275 #endif
1276         return msg;
1277 }
1278
1279 static void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1280 {
1281         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1282         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN);
1283         Q931_info_t *qi = (Q931_info_t*)(msg->data+HEADER_LEN);
1284         unsigned char *p = NULL;
1285 #if defined(AST_MISDN_ENHANCEMENTS)
1286         int description_code;
1287         int type;
1288         int plan;
1289         int present;
1290         char number[sizeof(bc->redirecting.to.number)];
1291 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1292
1293 #ifdef DEBUG
1294         printf("Parsing FACILITY Msg\n");
1295 #endif
1296
1297         bc->fac_in.Function = Fac_None;
1298
1299         if (!bc->nt) {
1300                 if (qi->QI_ELEMENT(facility))
1301                         p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
1302         } else {
1303                 p = facility->FACILITY;
1304         }
1305         if (!p)
1306                 return;
1307
1308         if (decodeFac(p, &bc->fac_in)) {
1309                 cb_log(3, bc->port, "Decoding facility ie failed! Unrecognized facility message?\n");
1310         }
1311
1312 #if defined(AST_MISDN_ENHANCEMENTS)
1313         dec_ie_notify(facility->NOTIFY, qi, &description_code, nt, bc);
1314         if (description_code < 0) {
1315                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1316         } else {
1317                 bc->notify_description_code = description_code;
1318         }
1319
1320         dec_ie_redir_dn(facility->REDIR_DN, qi, &type, &plan, &present, number, sizeof(number), nt, bc);
1321         if (0 <= type) {
1322                 bc->redirecting.to_changed = 1;
1323
1324                 bc->redirecting.to.number_type = type;
1325                 bc->redirecting.to.number_plan = plan;
1326                 switch (present) {
1327                 default:
1328                 case 0:
1329                         bc->redirecting.to.presentation = 0;    /* presentation allowed */
1330                         break;
1331                 case 1:
1332                         bc->redirecting.to.presentation = 1;    /* presentation restricted */
1333                         break;
1334                 case 2:
1335                         bc->redirecting.to.presentation = 2;    /* Number not available */
1336                         break;
1337                 }
1338                 bc->redirecting.to.screening = 0;       /* Unscreened */
1339                 strcpy(bc->redirecting.to.number, number);
1340         }
1341 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1342 }
1343
1344 static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1345 {
1346         int len;
1347         int HEADER_LEN;
1348         unsigned char *ie_fac;
1349         unsigned char fac_tmp[256];
1350         msg_t *msg;
1351         FACILITY_t *facility;
1352         Q931_info_t *qi;
1353
1354 #ifdef DEBUG
1355         printf("Building FACILITY Msg\n");
1356 #endif
1357
1358         len = encodeFac(fac_tmp, &(bc->fac_out));
1359         if (len <= 0) {
1360                 /*
1361                  * mISDN does not know how to build the requested facility structure
1362                  * Clear facility information
1363                  */
1364                 bc->fac_out.Function = Fac_None;
1365
1366 #if defined(AST_MISDN_ENHANCEMENTS)
1367                 /* Clear other one shot information. */
1368                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1369                 bc->redirecting.to_changed = 0;
1370 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1371                 return NULL;
1372         }
1373
1374         msg = (msg_t *) create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, bc ? bc->l3_id : -1, sizeof(FACILITY_t), nt);
1375         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1376         facility = (FACILITY_t *) (msg->data + HEADER_LEN);
1377
1378         ie_fac = msg_put(msg, len);
1379         if (bc->nt) {
1380                 facility->FACILITY = ie_fac + 1;
1381         } else {
1382                 qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
1383                 qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t);
1384         }
1385
1386         memcpy(ie_fac, fac_tmp, len);
1387
1388         /* Clear facility information */
1389         bc->fac_out.Function = Fac_None;
1390
1391         if (*bc->display) {
1392 #ifdef DEBUG
1393                 printf("Sending %s as Display\n", bc->display);
1394 #endif
1395                 enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
1396         }
1397
1398 #if defined(AST_MISDN_ENHANCEMENTS)
1399         if (bc->notify_description_code != mISDN_NOTIFY_CODE_INVALID) {
1400                 enc_ie_notify(&facility->NOTIFY, msg, bc->notify_description_code, nt, bc);
1401                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1402         }
1403
1404         if (bc->redirecting.to_changed) {
1405                 bc->redirecting.to_changed = 0;
1406                 switch (bc->outgoing_colp) {
1407                 case 0:/* pass */
1408                 case 1:/* restricted */
1409                         enc_ie_redir_dn(&facility->REDIR_DN, msg, bc->redirecting.to.number_type,
1410                                 bc->redirecting.to.number_plan, bc->redirecting.to.presentation,
1411                                 bc->redirecting.to.number, nt, bc);
1412                         break;
1413                 default:
1414                         break;
1415                 }
1416         }
1417 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1418
1419         return msg;
1420 }
1421
1422 #if defined(AST_MISDN_ENHANCEMENTS)
1423 /*!
1424  * \internal
1425  * \brief Parse a received REGISTER message
1426  *
1427  * \param msgs Search table entry that called us.
1428  * \param msg Received message contents
1429  * \param bc Associated B channel
1430  * \param nt TRUE if in NT mode.
1431  *
1432  * \return Nothing
1433  */
1434 static void parse_register(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1435 {
1436         int HEADER_LEN;
1437         REGISTER_t *reg;
1438
1439         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1440         reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1441
1442         /*
1443          * A facility ie is optional.
1444          * The peer may just be establishing a connection to send
1445          * messages later.
1446          */
1447         dec_ie_facility(reg->FACILITY, (Q931_info_t *) reg, &bc->fac_in, nt, bc);
1448 }
1449 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1450
1451 #if defined(AST_MISDN_ENHANCEMENTS)
1452 /*!
1453  * \internal
1454  * \brief Construct a REGISTER message
1455  *
1456  * \param msgs Search table entry that called us.
1457  * \param bc Associated B channel
1458  * \param nt TRUE if in NT mode.
1459  *
1460  * \return Allocated built message
1461  */
1462 static msg_t *build_register(struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1463 {
1464         int HEADER_LEN;
1465         REGISTER_t *reg;
1466         msg_t *msg;
1467
1468         msg = (msg_t *) create_l3msg(CC_REGISTER | REQUEST, MT_REGISTER,  bc ? bc->l3_id : -1, sizeof(REGISTER_t), nt);
1469         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1470         reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1471
1472         if (bc->fac_out.Function != Fac_None) {
1473                 enc_ie_facility(&reg->FACILITY, msg, &bc->fac_out, nt);
1474         }
1475
1476         return msg;
1477 }
1478 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1479
1480 static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1481 {
1482         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1483         NOTIFY_t *notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1484         int description_code;
1485         int type;
1486         int plan;
1487         int present;
1488         char number[sizeof(bc->redirecting.to.number)];
1489
1490 #ifdef DEBUG
1491         printf("Parsing NOTIFY Msg\n");
1492 #endif
1493
1494         dec_ie_notify(notify->NOTIFY, (Q931_info_t *) notify, &description_code, nt, bc);
1495         if (description_code < 0) {
1496                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1497         } else {
1498                 bc->notify_description_code = description_code;
1499         }
1500
1501         dec_ie_redir_dn(notify->REDIR_DN, (Q931_info_t *) notify, &type, &plan, &present, number, sizeof(number), nt, bc);
1502         if (0 <= type) {
1503                 bc->redirecting.to_changed = 1;
1504
1505                 bc->redirecting.to.number_type = type;
1506                 bc->redirecting.to.number_plan = plan;
1507                 switch (present) {
1508                 default:
1509                 case 0:
1510                         bc->redirecting.to.presentation = 0;    /* presentation allowed */
1511                         break;
1512                 case 1:
1513                         bc->redirecting.to.presentation = 1;    /* presentation restricted */
1514                         break;
1515                 case 2:
1516                         bc->redirecting.to.presentation = 2;    /* Number not available */
1517                         break;
1518                 }
1519                 bc->redirecting.to.screening = 0;       /* Unscreened */
1520                 strcpy(bc->redirecting.to.number, number);
1521         }
1522 }
1523
1524 static msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1525 {
1526         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1527         NOTIFY_t *notify;
1528         msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY,  bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt);
1529
1530 #ifdef DEBUG
1531         printf("Building NOTIFY Msg\n");
1532 #endif
1533
1534         notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1535
1536         enc_ie_notify(&notify->NOTIFY, msg, bc->notify_description_code, nt, bc);
1537         bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1538
1539         if (bc->redirecting.to_changed) {
1540                 bc->redirecting.to_changed = 0;
1541                 switch (bc->outgoing_colp) {
1542                 case 0:/* pass */
1543                 case 1:/* restricted */
1544                         enc_ie_redir_dn(&notify->REDIR_DN, msg, bc->redirecting.to.number_type,
1545                                 bc->redirecting.to.number_plan, bc->redirecting.to.presentation,
1546                                 bc->redirecting.to.number, nt, bc);
1547                         break;
1548                 default:
1549                         break;
1550                 }
1551         }
1552         return msg;
1553 }
1554
1555 static void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1556 {
1557 #ifdef DEBUG
1558         printf("Parsing STATUS_ENQUIRY Msg\n");
1559 #endif
1560 }
1561
1562 static msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1563 {
1564         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1565         STATUS_ENQUIRY_t *status_enquiry;
1566         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY,  bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt);
1567
1568         status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN));
1569
1570 #ifdef DEBUG
1571         printf("Building STATUS_ENQUIRY Msg\n");
1572 #endif
1573         return msg;
1574 }
1575
1576 static void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1577 {
1578         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1579         INFORMATION_t *information = (INFORMATION_t *) (msg->data + HEADER_LEN);
1580         int type, plan;
1581
1582         dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *) information, &type, &plan, bc->info_dad, sizeof(bc->info_dad), nt, bc);
1583         dec_ie_keypad(information->KEYPAD, (Q931_info_t *) information, bc->keypad, sizeof(bc->keypad), nt, bc);
1584
1585 #ifdef DEBUG
1586         printf("Parsing INFORMATION Msg\n");
1587 #endif
1588 }
1589
1590 static msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1591 {
1592         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1593         INFORMATION_t *information;
1594         msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION,  bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt);
1595
1596         information=(INFORMATION_t*)((msg->data+HEADER_LEN));
1597
1598         enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
1599
1600         {
1601                 if (*bc->display) {
1602 #ifdef DEBUG
1603                         printf("Sending %s as Display\n", bc->display);
1604 #endif
1605                         enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
1606                 }
1607         }
1608
1609 #ifdef DEBUG
1610         printf("Building INFORMATION Msg\n");
1611 #endif
1612         return msg;
1613 }
1614
1615 static void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1616 {
1617         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1618         STATUS_t *status = (STATUS_t *) (msg->data + HEADER_LEN);
1619         int location;
1620         int cause;
1621
1622         dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc);
1623         if (cause>0) bc->cause=cause;
1624
1625 #ifdef DEBUG
1626         printf("Parsing STATUS Msg\n");
1627 #endif
1628 }
1629
1630 static msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1631 {
1632         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1633         STATUS_t *status;
1634         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1635
1636         status=(STATUS_t*)((msg->data+HEADER_LEN));
1637
1638 #ifdef DEBUG
1639         printf("Building STATUS Msg\n");
1640 #endif
1641         return msg;
1642 }
1643
1644 static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1645 {
1646 #ifdef DEBUG
1647         printf("Parsing STATUS Msg\n");
1648 #endif
1649 }
1650
1651 static msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1652 {
1653         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1654         STATUS_t *status;
1655         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1656
1657         status=(STATUS_t*)((msg->data+HEADER_LEN));
1658
1659 #ifdef DEBUG
1660         printf("Building STATUS Msg\n");
1661 #endif
1662         return msg;
1663 }
1664
1665
1666 /************************************/
1667
1668
1669
1670
1671 /** Msg Array **/
1672
1673 struct isdn_msg msgs_g[] = {
1674 /* *INDENT-OFF* */
1675         /* misdn_msg,               event,                      msg_parser,                 msg_builder,                info */
1676         { CC_PROCEEDING,            EVENT_PROCEEDING,           parse_proceeding,           build_proceeding,           "PROCEEDING" },
1677         { CC_ALERTING,              EVENT_ALERTING,             parse_alerting,             build_alerting,             "ALERTING" },
1678         { CC_PROGRESS,              EVENT_PROGRESS,             parse_progress,             build_progress,             "PROGRESS" },
1679         { CC_SETUP,                 EVENT_SETUP,                parse_setup,                build_setup,                "SETUP" },
1680 #if defined(AST_MISDN_ENHANCEMENTS)
1681         { CC_REGISTER,              EVENT_REGISTER,             parse_register,             build_register,             "REGISTER" },
1682 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1683         { CC_CONNECT,               EVENT_CONNECT,              parse_connect,              build_connect,              "CONNECT" },
1684         { CC_SETUP_ACKNOWLEDGE,     EVENT_SETUP_ACKNOWLEDGE,    parse_setup_acknowledge,    build_setup_acknowledge,    "SETUP_ACKNOWLEDGE" },
1685         { CC_CONNECT_ACKNOWLEDGE,   EVENT_CONNECT_ACKNOWLEDGE,  parse_connect_acknowledge,  build_connect_acknowledge,  "CONNECT_ACKNOWLEDGE " },
1686         { CC_USER_INFORMATION,      EVENT_USER_INFORMATION,     parse_user_information,     build_user_information,     "USER_INFORMATION" },
1687         { CC_SUSPEND_REJECT,        EVENT_SUSPEND_REJECT,       parse_suspend_reject,       build_suspend_reject,       "SUSPEND_REJECT" },
1688         { CC_RESUME_REJECT,         EVENT_RESUME_REJECT,        parse_resume_reject,        build_resume_reject,        "RESUME_REJECT" },
1689         { CC_HOLD,                  EVENT_HOLD,                 parse_hold,                 build_hold,                 "HOLD" },
1690         { CC_SUSPEND,               EVENT_SUSPEND,              parse_suspend,              build_suspend,              "SUSPEND" },
1691         { CC_RESUME,                EVENT_RESUME,               parse_resume,               build_resume,               "RESUME" },
1692         { CC_HOLD_ACKNOWLEDGE,      EVENT_HOLD_ACKNOWLEDGE,     parse_hold_acknowledge,     build_hold_acknowledge,     "HOLD_ACKNOWLEDGE" },
1693         { CC_SUSPEND_ACKNOWLEDGE,   EVENT_SUSPEND_ACKNOWLEDGE,  parse_suspend_acknowledge,  build_suspend_acknowledge,  "SUSPEND_ACKNOWLEDGE" },
1694         { CC_RESUME_ACKNOWLEDGE,    EVENT_RESUME_ACKNOWLEDGE,   parse_resume_acknowledge,   build_resume_acknowledge,   "RESUME_ACKNOWLEDGE" },
1695         { CC_HOLD_REJECT,           EVENT_HOLD_REJECT,          parse_hold_reject,          build_hold_reject,          "HOLD_REJECT" },
1696         { CC_RETRIEVE,              EVENT_RETRIEVE,             parse_retrieve,             build_retrieve,             "RETRIEVE" },
1697         { CC_RETRIEVE_ACKNOWLEDGE,  EVENT_RETRIEVE_ACKNOWLEDGE, parse_retrieve_acknowledge, build_retrieve_acknowledge, "RETRIEVE_ACKNOWLEDGE" },
1698         { CC_RETRIEVE_REJECT,       EVENT_RETRIEVE_REJECT,      parse_retrieve_reject,      build_retrieve_reject,      "RETRIEVE_REJECT" },
1699         { CC_DISCONNECT,            EVENT_DISCONNECT,           parse_disconnect,           build_disconnect,           "DISCONNECT" },
1700         { CC_RESTART,               EVENT_RESTART,              parse_restart,              build_restart,              "RESTART" },
1701         { CC_RELEASE,               EVENT_RELEASE,              parse_release,              build_release,              "RELEASE" },
1702         { CC_RELEASE_COMPLETE,      EVENT_RELEASE_COMPLETE,     parse_release_complete,     build_release_complete,     "RELEASE_COMPLETE" },
1703         { CC_FACILITY,              EVENT_FACILITY,             parse_facility,             build_facility,             "FACILITY" },
1704         { CC_NOTIFY,                EVENT_NOTIFY,               parse_notify,               build_notify,               "NOTIFY" },
1705         { CC_STATUS_ENQUIRY,        EVENT_STATUS_ENQUIRY,       parse_status_enquiry,       build_status_enquiry,       "STATUS_ENQUIRY" },
1706         { CC_INFORMATION,           EVENT_INFORMATION,          parse_information,          build_information,          "INFORMATION" },
1707         { CC_STATUS,                EVENT_STATUS,               parse_status,               build_status,               "STATUS" },
1708         { CC_TIMEOUT,               EVENT_TIMEOUT,              parse_timeout,              build_timeout,              "TIMEOUT" },
1709         { 0, 0, NULL, NULL, NULL }
1710 /* *INDENT-ON* */
1711 };
1712
1713 #define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
1714
1715 /** INTERFACE FCTS ***/
1716 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
1717 {
1718         int i;
1719
1720         if (nt){
1721                 mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
1722
1723                 for (i=0; i< msgs_max -1; i++) {
1724                         if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1725                 }
1726
1727         } else {
1728                 iframe_t *frm = (iframe_t*)msg->data;
1729
1730                 for (i=0; i< msgs_max -1; i++)
1731                         if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1732         }
1733
1734         return -1;
1735 }
1736
1737 int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
1738 {
1739         int i;
1740         for (i=0; i< msgs_max; i++)
1741                 if ( event == msgs[i].event) return i;
1742
1743         cb_log(10,0, "get_index: event not found!\n");
1744
1745         return -1;
1746 }
1747
1748 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
1749 {
1750         int i=isdn_msg_get_index(msgs, msg, nt);
1751         if(i>=0) return msgs[i].event;
1752         return EVENT_UNKNOWN;
1753 }
1754
1755 char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
1756 {
1757         int i=isdn_msg_get_index(msgs, msg, nt);
1758         if(i>=0) return msgs[i].info;
1759         return NULL;
1760 }
1761
1762
1763 char EVENT_CLEAN_INFO[] = "CLEAN_UP";
1764 char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
1765 char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
1766 char EVENT_NEW_BC_INFO[] = "NEW_BC";
1767 char EVENT_PORT_ALARM_INFO[] = "ALARM";
1768 char EVENT_NEW_CHANNEL_INFO[] = "NEW_CHANNEL";
1769 char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
1770 char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED";
1771 char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE";
1772 char EVENT_BCHAN_ERROR_INFO[] = "BCHAN_ERROR";
1773
1774 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
1775 {
1776         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1777
1778         if(i>=0) return msgs[i].info;
1779
1780         if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO;
1781         if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO;
1782         if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
1783         if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
1784         if (event == EVENT_NEW_CHANNEL) return EVENT_NEW_CHANNEL_INFO;
1785         if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
1786         if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO;
1787         if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO;
1788         if (event == EVENT_PORT_ALARM) return EVENT_PORT_ALARM_INFO;
1789         if (event == EVENT_BCHAN_ERROR) return EVENT_BCHAN_ERROR_INFO;
1790
1791         return NULL;
1792 }
1793
1794 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1795 {
1796         int i=isdn_msg_get_index(msgs, msg, nt);
1797         if(i<0) return -1;
1798
1799         msgs[i].msg_parser(msgs, msg, bc, nt);
1800         return 0;
1801 }
1802
1803 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
1804 {
1805         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1806         if(i<0) return NULL;
1807
1808         return  msgs[i].msg_builder(msgs, bc, nt);
1809 }