Correct some set-but-unused variable warnings in the mISDN library.
[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         int is_ptp;
504         enum FacFunction fac_type;
505
506         setup=(SETUP_t*)((msg->data+HEADER_LEN));
507
508         if (bc->channel == 0 || bc->channel == ANY_CHANNEL || bc->channel==-1)
509                 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 0, bc->channel, nt,bc);
510         else
511                 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
512
513         fac_type = bc->fac_out.Function;
514         if (fac_type != Fac_None) {
515                 enc_ie_facility(&setup->FACILITY, msg, &bc->fac_out, nt);
516         }
517
518         enc_ie_calling_pn(&setup->CALLING_PN, msg, bc->caller.number_type, bc->caller.number_plan,
519                 bc->caller.presentation, bc->caller.screening, bc->caller.number, nt, bc);
520
521         if (bc->dialed.number[0]) {
522                 enc_ie_called_pn(&setup->CALLED_PN, msg, bc->dialed.number_type, bc->dialed.number_plan, bc->dialed.number, nt, bc);
523         }
524
525         switch (bc->outgoing_colp) {
526         case 0:/* pass */
527         case 1:/* restricted */
528                 is_ptp = misdn_lib_is_ptp(bc->port);
529                 if (bc->redirecting.from.number[0]
530                         && ((!is_ptp && nt)
531                                 || (is_ptp
532 #if defined(AST_MISDN_ENHANCEMENTS)
533                                         /*
534                                          * There is no need to send out this ie when we are also sending
535                                          * a Fac_DivertingLegInformation2 as well.  The
536                                          * Fac_DivertingLegInformation2 supercedes the information in
537                                          * this ie.
538                                          */
539                                         && fac_type != Fac_DivertingLegInformation2
540 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
541                         ))) {
542 #if 1
543                         /* ETSI and Q.952 do not define the screening field */
544                         enc_ie_redir_nr(&setup->REDIR_NR, msg, bc->redirecting.from.number_type,
545                                 bc->redirecting.from.number_plan, bc->redirecting.from.presentation, 0,
546                                 bc->redirecting.reason, bc->redirecting.from.number, nt, bc);
547 #else
548                         /* Q.931 defines the screening field */
549                         enc_ie_redir_nr(&setup->REDIR_NR, msg, bc->redirecting.from.number_type,
550                                 bc->redirecting.from.number_plan, bc->redirecting.from.presentation,
551                                 bc->redirecting.from.screening, bc->redirecting.reason,
552                                 bc->redirecting.from.number, nt, bc);
553 #endif
554                 }
555                 break;
556         default:
557                 break;
558         }
559
560         if (bc->keypad[0]) {
561                 enc_ie_keypad(&setup->KEYPAD, msg, bc->keypad, nt,bc);
562         }
563
564
565
566         if (*bc->display) {
567                 enc_ie_display(&setup->DISPLAY, msg, bc->display, nt, bc);
568         } else if (nt && bc->caller.presentation == 0) {
569                 char display[sizeof(bc->display)];
570
571                 /* Presentation is allowed */
572                 build_display_str(display, sizeof(display), bc->display_setup, bc->caller.name, bc->caller.number);
573                 if (display[0]) {
574                         enc_ie_display(&setup->DISPLAY, msg, display, nt, bc);
575                 }
576         }
577
578         {
579                 int coding = 0;
580                 int capability;
581                 int mode = 0;   /* 2 for packet! */
582                 int user;
583                 int rate = 0x10;
584
585                 switch (bc->law) {
586                 case INFO_CODEC_ULAW: user=2;
587                         break;
588                 case INFO_CODEC_ALAW: user=3;
589                         break;
590                 default:
591                         user=3;
592                 }
593
594                 switch (bc->capability) {
595                 case INFO_CAPABILITY_SPEECH: capability = 0;
596                         break;
597                 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8;
598                         user=-1;
599                         mode=bc->mode;
600                         rate=bc->rate;
601                         break;
602                 case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9;
603                         user=-1;
604                         break;
605                 default:
606                         capability=bc->capability;
607                 }
608
609                 enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc);
610         }
611
612         if (bc->sending_complete) {
613                 enc_ie_complete(&setup->COMPLETE,msg, bc->sending_complete, nt, bc);
614         }
615
616         if (bc->uulen) {
617                 int  protocol=4;
618                 enc_ie_useruser(&setup->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
619                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
620         }
621
622 #if defined(AST_MISDN_ENHANCEMENTS)
623         extract_setup_Bc_Hlc_Llc(setup, nt, bc);
624 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
625
626 #ifdef DEBUG
627         printf("Building SETUP Msg\n");
628 #endif
629         return msg;
630 }
631
632 static void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
633 {
634         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
635         CONNECT_t *connect = (CONNECT_t *) (msg->data + HEADER_LEN);
636         int type;
637         int plan;
638         int pres;
639         int screen;
640
641         bc->ces = connect->ces;
642
643         dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
644
645         dec_ie_connected_pn(connect->CONNECT_PN, (Q931_info_t *) connect, &type, &plan,
646                 &pres, &screen, bc->connected.number, sizeof(bc->connected.number), nt, bc);
647         bc->connected.number_type = type;
648         bc->connected.number_plan = plan;
649         switch (pres) {
650         default:
651         case 0:
652                 bc->connected.presentation = 0; /* presentation allowed */
653                 break;
654         case 1:
655                 bc->connected.presentation = 1; /* presentation restricted */
656                 break;
657         case 2:
658                 bc->connected.presentation = 2; /* Number not available */
659                 break;
660         }
661         if (0 <= screen) {
662                 bc->connected.screening = screen;
663         } else {
664                 bc->connected.screening = 0;    /* Unscreened */
665         }
666
667         dec_ie_facility(connect->FACILITY, (Q931_info_t *) connect, &bc->fac_in, nt, bc);
668
669         /*
670                 cb_log(1,bc->port,"CONNETED PN: %s cpn_dialplan:%d\n", connected_pn, type);
671         */
672
673 #ifdef DEBUG
674         printf("Parsing CONNECT Msg\n");
675 #endif
676 }
677
678 static msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
679 {
680         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
681         CONNECT_t *connect;
682         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt);
683
684         cb_log(6,bc->port,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,nt);
685
686         connect=(CONNECT_t*)((msg->data+HEADER_LEN));
687
688         if (nt) {
689                 time_t now;
690                 time(&now);
691                 enc_ie_date(&connect->DATE, msg, now, nt,bc);
692         }
693
694         switch (bc->outgoing_colp) {
695         case 0:/* pass */
696         case 1:/* restricted */
697                 enc_ie_connected_pn(&connect->CONNECT_PN, msg, bc->connected.number_type,
698                         bc->connected.number_plan, bc->connected.presentation,
699                         bc->connected.screening, bc->connected.number, nt, bc);
700                 break;
701         default:
702                 break;
703         }
704
705         if (nt && bc->connected.presentation == 0) {
706                 char display[sizeof(bc->display)];
707
708                 /* Presentation is allowed */
709                 build_display_str(display, sizeof(display), bc->display_connected, bc->connected.name, bc->connected.number);
710                 if (display[0]) {
711                         enc_ie_display(&connect->DISPLAY, msg, display, nt, bc);
712                 }
713         }
714
715         if (bc->fac_out.Function != Fac_None) {
716                 enc_ie_facility(&connect->FACILITY, msg, &bc->fac_out, nt);
717         }
718
719 #ifdef DEBUG
720         printf("Building CONNECT Msg\n");
721 #endif
722         return msg;
723 }
724
725 static void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
726 {
727         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
728         SETUP_ACKNOWLEDGE_t *setup_acknowledge = (SETUP_ACKNOWLEDGE_t *) (msg->data + HEADER_LEN);
729
730         {
731                 int  exclusive, channel;
732                 dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
733
734
735                 set_channel(bc, channel);
736         }
737
738         dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
739
740         dec_ie_facility(setup_acknowledge->FACILITY, (Q931_info_t *) setup_acknowledge, &bc->fac_in, nt, bc);
741
742 #ifdef DEBUG
743         printf("Parsing SETUP_ACKNOWLEDGE Msg\n");
744 #endif
745
746
747 }
748
749 static msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
750 {
751         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
752         SETUP_ACKNOWLEDGE_t *setup_acknowledge;
753         msg_t *msg =(msg_t*)create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SETUP_ACKNOWLEDGE_t) ,nt);
754
755         setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
756
757         enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
758
759         if (nt)
760                 enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
761
762         if (bc->fac_out.Function != Fac_None) {
763                 enc_ie_facility(&setup_acknowledge->FACILITY, msg, &bc->fac_out, nt);
764         }
765
766 #ifdef DEBUG
767         printf("Building SETUP_ACKNOWLEDGE Msg\n");
768 #endif
769         return msg;
770 }
771
772 static void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
773 {
774 #ifdef DEBUG
775         printf("Parsing CONNECT_ACKNOWLEDGE Msg\n");
776 #endif
777
778
779 }
780
781 static msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
782 {
783         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
784         CONNECT_ACKNOWLEDGE_t *connect_acknowledge;
785         msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT,  bc?bc->l3_id:-1, sizeof(CONNECT_ACKNOWLEDGE_t) ,nt);
786
787         connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
788
789         enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
790
791 #ifdef DEBUG
792         printf("Building CONNECT_ACKNOWLEDGE Msg\n");
793 #endif
794         return msg;
795 }
796
797 static void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
798 {
799 #ifdef DEBUG
800         printf("Parsing USER_INFORMATION Msg\n");
801 #endif
802
803
804 }
805
806 static msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
807 {
808         msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION,  bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt);
809
810 #ifdef DEBUG
811         printf("Building USER_INFORMATION Msg\n");
812 #endif
813         return msg;
814 }
815
816 static void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
817 {
818 #ifdef DEBUG
819         printf("Parsing SUSPEND_REJECT Msg\n");
820 #endif
821
822
823 }
824
825 static msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
826 {
827         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT,  bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt);
828
829 #ifdef DEBUG
830         printf("Building SUSPEND_REJECT Msg\n");
831 #endif
832         return msg;
833 }
834
835 static void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
836 {
837 #ifdef DEBUG
838         printf("Parsing RESUME_REJECT Msg\n");
839 #endif
840
841
842 }
843
844 static msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
845 {
846         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT,  bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt);
847
848 #ifdef DEBUG
849         printf("Building RESUME_REJECT Msg\n");
850 #endif
851         return msg;
852 }
853
854 static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
855 {
856 #ifdef DEBUG
857         printf("Parsing HOLD Msg\n");
858 #endif
859
860
861 }
862
863 static msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
864 {
865         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD,  bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt);
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         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND,  bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt);
885
886 #ifdef DEBUG
887         printf("Building SUSPEND Msg\n");
888 #endif
889         return msg;
890 }
891
892 static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
893 {
894 #ifdef DEBUG
895         printf("Parsing RESUME Msg\n");
896 #endif
897
898
899 }
900
901 static msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
902 {
903         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME,  bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt);
904
905 #ifdef DEBUG
906         printf("Building RESUME Msg\n");
907 #endif
908         return msg;
909 }
910
911 static void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
912 {
913 #ifdef DEBUG
914         printf("Parsing HOLD_ACKNOWLEDGE Msg\n");
915 #endif
916
917
918 }
919
920 static msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
921 {
922         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt);
923
924 #ifdef DEBUG
925         printf("Building HOLD_ACKNOWLEDGE Msg\n");
926 #endif
927         return msg;
928 }
929
930 static void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
931 {
932 #ifdef DEBUG
933         printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n");
934 #endif
935
936
937 }
938
939 static msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
940 {
941         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt);
942
943 #ifdef DEBUG
944         printf("Building SUSPEND_ACKNOWLEDGE Msg\n");
945 #endif
946         return msg;
947 }
948
949 static void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
950 {
951 #ifdef DEBUG
952         printf("Parsing RESUME_ACKNOWLEDGE Msg\n");
953 #endif
954
955
956 }
957
958 static msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
959 {
960         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt);
961
962 #ifdef DEBUG
963         printf("Building RESUME_ACKNOWLEDGE Msg\n");
964 #endif
965         return msg;
966 }
967
968 static void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
969 {
970 #ifdef DEBUG
971         printf("Parsing HOLD_REJECT Msg\n");
972 #endif
973
974
975 }
976
977 static msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
978 {
979         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT,  bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt);
980
981 #ifdef DEBUG
982         printf("Building HOLD_REJECT Msg\n");
983 #endif
984         return msg;
985 }
986
987 static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
988 {
989 #ifdef DEBUG
990         printf("Parsing RETRIEVE Msg\n");
991 #endif
992
993
994 }
995
996 static msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
997 {
998         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt);
999
1000 #ifdef DEBUG
1001         printf("Building RETRIEVE Msg\n");
1002 #endif
1003         return msg;
1004 }
1005
1006 static void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1007 {
1008 #ifdef DEBUG
1009         printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n");
1010 #endif
1011
1012
1013 }
1014
1015 static msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1016 {
1017         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1018         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1019         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt);
1020
1021         retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
1022
1023         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
1024 #ifdef DEBUG
1025         printf("Building RETRIEVE_ACKNOWLEDGE Msg\n");
1026 #endif
1027         return msg;
1028 }
1029
1030 static void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1031 {
1032 #ifdef DEBUG
1033         printf("Parsing RETRIEVE_REJECT Msg\n");
1034 #endif
1035
1036
1037 }
1038
1039 static msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1040 {
1041         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT,  bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt);
1042
1043 #ifdef DEBUG
1044         printf("Building RETRIEVE_REJECT Msg\n");
1045 #endif
1046         return msg;
1047 }
1048
1049 static void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1050 {
1051         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1052         DISCONNECT_t *disconnect = (DISCONNECT_t *) (msg->data + HEADER_LEN);
1053         int location;
1054         int cause;
1055         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc);
1056         if (cause>0) bc->cause=cause;
1057
1058         dec_ie_facility(disconnect->FACILITY, (Q931_info_t *) disconnect, &bc->fac_in, nt, bc);
1059
1060         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
1061 #ifdef DEBUG
1062         printf("Parsing DISCONNECT Msg\n");
1063 #endif
1064
1065
1066 }
1067
1068 static msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1069 {
1070         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1071         DISCONNECT_t *disconnect;
1072         msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT,  bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt);
1073
1074         disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN));
1075
1076         enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
1077         if (nt) {
1078                 enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt ? 1 : 5, 8, nt, bc);
1079         }
1080
1081         if (bc->fac_out.Function != Fac_None) {
1082                 enc_ie_facility(&disconnect->FACILITY, msg, &bc->fac_out, nt);
1083         }
1084
1085         if (bc->uulen) {
1086                 int  protocol=4;
1087                 enc_ie_useruser(&disconnect->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1088                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1089         }
1090
1091 #ifdef DEBUG
1092         printf("Building DISCONNECT Msg\n");
1093 #endif
1094         return msg;
1095 }
1096
1097 static void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1098 {
1099         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1100         RESTART_t *restart = (RESTART_t *) (msg->data + HEADER_LEN);
1101
1102         struct misdn_stack *stack=get_stack_by_bc(bc);
1103
1104 #ifdef DEBUG
1105         printf("Parsing RESTART Msg\n");
1106 #endif
1107
1108         {
1109                 int  exclusive;
1110                 dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &bc->restart_channel, nt,bc);
1111                 cb_log(3, stack->port, "CC_RESTART Request on channel:%d on this port.\n", bc->restart_channel);
1112         }
1113
1114 }
1115
1116 static msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1117 {
1118         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1119         RESTART_t *restart;
1120         msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART,  bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt);
1121
1122         restart=(RESTART_t*)((msg->data+HEADER_LEN));
1123
1124 #ifdef DEBUG
1125         printf("Building RESTART Msg\n");
1126 #endif
1127
1128         if (bc->channel > 0) {
1129                 enc_ie_channel_id(&restart->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
1130                 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x80, nt, bc);
1131         } else {
1132                 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x87, nt, bc);
1133         }
1134
1135         cb_log(0,bc->port, "Restarting channel %d\n", bc->channel);
1136         return msg;
1137 }
1138
1139 static void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1140 {
1141         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1142         RELEASE_t *release = (RELEASE_t *) (msg->data + HEADER_LEN);
1143         int location;
1144         int cause;
1145
1146         dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc);
1147         if (cause>0) bc->cause=cause;
1148
1149         dec_ie_facility(release->FACILITY, (Q931_info_t *) release, &bc->fac_in, nt, bc);
1150
1151 #ifdef DEBUG
1152         printf("Parsing RELEASE Msg\n");
1153 #endif
1154
1155
1156 }
1157
1158 static msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1159 {
1160         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1161         RELEASE_t *release;
1162         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE,  bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt);
1163
1164         release=(RELEASE_t*)((msg->data+HEADER_LEN));
1165
1166         if (bc->out_cause>= 0)
1167                 enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1168
1169         if (bc->fac_out.Function != Fac_None) {
1170                 enc_ie_facility(&release->FACILITY, msg, &bc->fac_out, nt);
1171         }
1172
1173         if (bc->uulen) {
1174                 int  protocol=4;
1175                 enc_ie_useruser(&release->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1176                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1177         }
1178
1179 #ifdef DEBUG
1180         printf("Building RELEASE Msg\n");
1181 #endif
1182         return msg;
1183 }
1184
1185 static void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1186 {
1187         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1188         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *) (msg->data + HEADER_LEN);
1189         int location;
1190         int cause;
1191         iframe_t *frm = (iframe_t*) msg->data;
1192
1193         struct misdn_stack *stack=get_stack_by_bc(bc);
1194         mISDNuser_head_t *hh;
1195         hh=(mISDNuser_head_t*)msg->data;
1196
1197         /*hh=(mISDN_head_t*)msg->data;
1198         mISDN_head_t *hh;*/
1199
1200         if (nt) {
1201                 if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1202                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n");
1203                         return;
1204                 }
1205         } else {
1206                 if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1207                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n");
1208                         return;
1209                 }
1210         }
1211         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc);
1212         if (cause>0) bc->cause=cause;
1213
1214         dec_ie_facility(release_complete->FACILITY, (Q931_info_t *) release_complete, &bc->fac_in, nt, bc);
1215
1216 #ifdef DEBUG
1217         printf("Parsing RELEASE_COMPLETE Msg\n");
1218 #endif
1219 }
1220
1221 static msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1222 {
1223         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1224         RELEASE_COMPLETE_t *release_complete;
1225         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE,  bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt);
1226
1227         release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN));
1228
1229         enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1230
1231         if (bc->fac_out.Function != Fac_None) {
1232                 enc_ie_facility(&release_complete->FACILITY, msg, &bc->fac_out, nt);
1233         }
1234
1235         if (bc->uulen) {
1236                 int  protocol=4;
1237                 enc_ie_useruser(&release_complete->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1238                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1239         }
1240
1241 #ifdef DEBUG
1242         printf("Building RELEASE_COMPLETE Msg\n");
1243 #endif
1244         return msg;
1245 }
1246
1247 static void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1248 {
1249         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1250         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN);
1251         Q931_info_t *qi = (Q931_info_t*)(msg->data+HEADER_LEN);
1252         unsigned char *p = NULL;
1253 #if defined(AST_MISDN_ENHANCEMENTS)
1254         int description_code;
1255         int type;
1256         int plan;
1257         int present;
1258         char number[sizeof(bc->redirecting.to.number)];
1259 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1260
1261 #ifdef DEBUG
1262         printf("Parsing FACILITY Msg\n");
1263 #endif
1264
1265         bc->fac_in.Function = Fac_None;
1266
1267         if (!bc->nt) {
1268                 if (qi->QI_ELEMENT(facility))
1269                         p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
1270         } else {
1271                 p = facility->FACILITY;
1272         }
1273         if (!p)
1274                 return;
1275
1276         if (decodeFac(p, &bc->fac_in)) {
1277                 cb_log(3, bc->port, "Decoding facility ie failed! Unrecognized facility message?\n");
1278         }
1279
1280 #if defined(AST_MISDN_ENHANCEMENTS)
1281         dec_ie_notify(facility->NOTIFY, qi, &description_code, nt, bc);
1282         if (description_code < 0) {
1283                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1284         } else {
1285                 bc->notify_description_code = description_code;
1286         }
1287
1288         dec_ie_redir_dn(facility->REDIR_DN, qi, &type, &plan, &present, number, sizeof(number), nt, bc);
1289         if (0 <= type) {
1290                 bc->redirecting.to_changed = 1;
1291
1292                 bc->redirecting.to.number_type = type;
1293                 bc->redirecting.to.number_plan = plan;
1294                 switch (present) {
1295                 default:
1296                 case 0:
1297                         bc->redirecting.to.presentation = 0;    /* presentation allowed */
1298                         break;
1299                 case 1:
1300                         bc->redirecting.to.presentation = 1;    /* presentation restricted */
1301                         break;
1302                 case 2:
1303                         bc->redirecting.to.presentation = 2;    /* Number not available */
1304                         break;
1305                 }
1306                 bc->redirecting.to.screening = 0;       /* Unscreened */
1307                 strcpy(bc->redirecting.to.number, number);
1308         }
1309 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1310 }
1311
1312 static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1313 {
1314         int len;
1315         int HEADER_LEN;
1316         unsigned char *ie_fac;
1317         unsigned char fac_tmp[256];
1318         msg_t *msg;
1319         FACILITY_t *facility;
1320         Q931_info_t *qi;
1321
1322 #ifdef DEBUG
1323         printf("Building FACILITY Msg\n");
1324 #endif
1325
1326         len = encodeFac(fac_tmp, &(bc->fac_out));
1327         if (len <= 0) {
1328                 /*
1329                  * mISDN does not know how to build the requested facility structure
1330                  * Clear facility information
1331                  */
1332                 bc->fac_out.Function = Fac_None;
1333
1334 #if defined(AST_MISDN_ENHANCEMENTS)
1335                 /* Clear other one shot information. */
1336                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1337                 bc->redirecting.to_changed = 0;
1338 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1339                 return NULL;
1340         }
1341
1342         msg = (msg_t *) create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, bc ? bc->l3_id : -1, sizeof(FACILITY_t), nt);
1343         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1344         facility = (FACILITY_t *) (msg->data + HEADER_LEN);
1345
1346         ie_fac = msg_put(msg, len);
1347         if (bc->nt) {
1348                 facility->FACILITY = ie_fac + 1;
1349         } else {
1350                 qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
1351                 qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t);
1352         }
1353
1354         memcpy(ie_fac, fac_tmp, len);
1355
1356         /* Clear facility information */
1357         bc->fac_out.Function = Fac_None;
1358
1359         if (*bc->display) {
1360 #ifdef DEBUG
1361                 printf("Sending %s as Display\n", bc->display);
1362 #endif
1363                 enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
1364         }
1365
1366 #if defined(AST_MISDN_ENHANCEMENTS)
1367         if (bc->notify_description_code != mISDN_NOTIFY_CODE_INVALID) {
1368                 enc_ie_notify(&facility->NOTIFY, msg, bc->notify_description_code, nt, bc);
1369                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1370         }
1371
1372         if (bc->redirecting.to_changed) {
1373                 bc->redirecting.to_changed = 0;
1374                 switch (bc->outgoing_colp) {
1375                 case 0:/* pass */
1376                 case 1:/* restricted */
1377                         enc_ie_redir_dn(&facility->REDIR_DN, msg, bc->redirecting.to.number_type,
1378                                 bc->redirecting.to.number_plan, bc->redirecting.to.presentation,
1379                                 bc->redirecting.to.number, nt, bc);
1380                         break;
1381                 default:
1382                         break;
1383                 }
1384         }
1385 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1386
1387         return msg;
1388 }
1389
1390 #if defined(AST_MISDN_ENHANCEMENTS)
1391 /*!
1392  * \internal
1393  * \brief Parse a received REGISTER message
1394  *
1395  * \param msgs Search table entry that called us.
1396  * \param msg Received message contents
1397  * \param bc Associated B channel
1398  * \param nt TRUE if in NT mode.
1399  *
1400  * \return Nothing
1401  */
1402 static void parse_register(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1403 {
1404         int HEADER_LEN;
1405         REGISTER_t *reg;
1406
1407         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1408         reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1409
1410         /*
1411          * A facility ie is optional.
1412          * The peer may just be establishing a connection to send
1413          * messages later.
1414          */
1415         dec_ie_facility(reg->FACILITY, (Q931_info_t *) reg, &bc->fac_in, nt, bc);
1416 }
1417 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1418
1419 #if defined(AST_MISDN_ENHANCEMENTS)
1420 /*!
1421  * \internal
1422  * \brief Construct a REGISTER message
1423  *
1424  * \param msgs Search table entry that called us.
1425  * \param bc Associated B channel
1426  * \param nt TRUE if in NT mode.
1427  *
1428  * \return Allocated built message
1429  */
1430 static msg_t *build_register(struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1431 {
1432         int HEADER_LEN;
1433         REGISTER_t *reg;
1434         msg_t *msg;
1435
1436         msg = (msg_t *) create_l3msg(CC_REGISTER | REQUEST, MT_REGISTER,  bc ? bc->l3_id : -1, sizeof(REGISTER_t), nt);
1437         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1438         reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1439
1440         if (bc->fac_out.Function != Fac_None) {
1441                 enc_ie_facility(&reg->FACILITY, msg, &bc->fac_out, nt);
1442         }
1443
1444         return msg;
1445 }
1446 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1447
1448 static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1449 {
1450         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1451         NOTIFY_t *notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1452         int description_code;
1453         int type;
1454         int plan;
1455         int present;
1456         char number[sizeof(bc->redirecting.to.number)];
1457
1458 #ifdef DEBUG
1459         printf("Parsing NOTIFY Msg\n");
1460 #endif
1461
1462         dec_ie_notify(notify->NOTIFY, (Q931_info_t *) notify, &description_code, nt, bc);
1463         if (description_code < 0) {
1464                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1465         } else {
1466                 bc->notify_description_code = description_code;
1467         }
1468
1469         dec_ie_redir_dn(notify->REDIR_DN, (Q931_info_t *) notify, &type, &plan, &present, number, sizeof(number), nt, bc);
1470         if (0 <= type) {
1471                 bc->redirecting.to_changed = 1;
1472
1473                 bc->redirecting.to.number_type = type;
1474                 bc->redirecting.to.number_plan = plan;
1475                 switch (present) {
1476                 default:
1477                 case 0:
1478                         bc->redirecting.to.presentation = 0;    /* presentation allowed */
1479                         break;
1480                 case 1:
1481                         bc->redirecting.to.presentation = 1;    /* presentation restricted */
1482                         break;
1483                 case 2:
1484                         bc->redirecting.to.presentation = 2;    /* Number not available */
1485                         break;
1486                 }
1487                 bc->redirecting.to.screening = 0;       /* Unscreened */
1488                 strcpy(bc->redirecting.to.number, number);
1489         }
1490 }
1491
1492 static msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1493 {
1494         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1495         NOTIFY_t *notify;
1496         msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY,  bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt);
1497
1498 #ifdef DEBUG
1499         printf("Building NOTIFY Msg\n");
1500 #endif
1501
1502         notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1503
1504         enc_ie_notify(&notify->NOTIFY, msg, bc->notify_description_code, nt, bc);
1505         bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1506
1507         if (bc->redirecting.to_changed) {
1508                 bc->redirecting.to_changed = 0;
1509                 switch (bc->outgoing_colp) {
1510                 case 0:/* pass */
1511                 case 1:/* restricted */
1512                         enc_ie_redir_dn(&notify->REDIR_DN, msg, bc->redirecting.to.number_type,
1513                                 bc->redirecting.to.number_plan, bc->redirecting.to.presentation,
1514                                 bc->redirecting.to.number, nt, bc);
1515                         break;
1516                 default:
1517                         break;
1518                 }
1519         }
1520         return msg;
1521 }
1522
1523 static void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1524 {
1525 #ifdef DEBUG
1526         printf("Parsing STATUS_ENQUIRY Msg\n");
1527 #endif
1528 }
1529
1530 static msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1531 {
1532         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY,  bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt);
1533
1534 #ifdef DEBUG
1535         printf("Building STATUS_ENQUIRY Msg\n");
1536 #endif
1537         return msg;
1538 }
1539
1540 static void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1541 {
1542         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1543         INFORMATION_t *information = (INFORMATION_t *) (msg->data + HEADER_LEN);
1544         int type, plan;
1545
1546         dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *) information, &type, &plan, bc->info_dad, sizeof(bc->info_dad), nt, bc);
1547         dec_ie_keypad(information->KEYPAD, (Q931_info_t *) information, bc->keypad, sizeof(bc->keypad), nt, bc);
1548
1549 #ifdef DEBUG
1550         printf("Parsing INFORMATION Msg\n");
1551 #endif
1552 }
1553
1554 static msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1555 {
1556         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1557         INFORMATION_t *information;
1558         msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION,  bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt);
1559
1560         information=(INFORMATION_t*)((msg->data+HEADER_LEN));
1561
1562         enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
1563
1564         {
1565                 if (*bc->display) {
1566 #ifdef DEBUG
1567                         printf("Sending %s as Display\n", bc->display);
1568 #endif
1569                         enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
1570                 }
1571         }
1572
1573 #ifdef DEBUG
1574         printf("Building INFORMATION Msg\n");
1575 #endif
1576         return msg;
1577 }
1578
1579 static void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1580 {
1581         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1582         STATUS_t *status = (STATUS_t *) (msg->data + HEADER_LEN);
1583         int location;
1584         int cause;
1585
1586         dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc);
1587         if (cause>0) bc->cause=cause;
1588
1589 #ifdef DEBUG
1590         printf("Parsing STATUS Msg\n");
1591 #endif
1592 }
1593
1594 static msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1595 {
1596         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1597
1598 #ifdef DEBUG
1599         printf("Building STATUS Msg\n");
1600 #endif
1601         return msg;
1602 }
1603
1604 static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1605 {
1606 #ifdef DEBUG
1607         printf("Parsing STATUS Msg\n");
1608 #endif
1609 }
1610
1611 static msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1612 {
1613         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1614
1615 #ifdef DEBUG
1616         printf("Building STATUS Msg\n");
1617 #endif
1618         return msg;
1619 }
1620
1621
1622 /************************************/
1623
1624
1625
1626
1627 /** Msg Array **/
1628
1629 struct isdn_msg msgs_g[] = {
1630 /* *INDENT-OFF* */
1631         /* misdn_msg,               event,                      msg_parser,                 msg_builder,                info */
1632         { CC_PROCEEDING,            EVENT_PROCEEDING,           parse_proceeding,           build_proceeding,           "PROCEEDING" },
1633         { CC_ALERTING,              EVENT_ALERTING,             parse_alerting,             build_alerting,             "ALERTING" },
1634         { CC_PROGRESS,              EVENT_PROGRESS,             parse_progress,             build_progress,             "PROGRESS" },
1635         { CC_SETUP,                 EVENT_SETUP,                parse_setup,                build_setup,                "SETUP" },
1636 #if defined(AST_MISDN_ENHANCEMENTS)
1637         { CC_REGISTER,              EVENT_REGISTER,             parse_register,             build_register,             "REGISTER" },
1638 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1639         { CC_CONNECT,               EVENT_CONNECT,              parse_connect,              build_connect,              "CONNECT" },
1640         { CC_SETUP_ACKNOWLEDGE,     EVENT_SETUP_ACKNOWLEDGE,    parse_setup_acknowledge,    build_setup_acknowledge,    "SETUP_ACKNOWLEDGE" },
1641         { CC_CONNECT_ACKNOWLEDGE,   EVENT_CONNECT_ACKNOWLEDGE,  parse_connect_acknowledge,  build_connect_acknowledge,  "CONNECT_ACKNOWLEDGE " },
1642         { CC_USER_INFORMATION,      EVENT_USER_INFORMATION,     parse_user_information,     build_user_information,     "USER_INFORMATION" },
1643         { CC_SUSPEND_REJECT,        EVENT_SUSPEND_REJECT,       parse_suspend_reject,       build_suspend_reject,       "SUSPEND_REJECT" },
1644         { CC_RESUME_REJECT,         EVENT_RESUME_REJECT,        parse_resume_reject,        build_resume_reject,        "RESUME_REJECT" },
1645         { CC_HOLD,                  EVENT_HOLD,                 parse_hold,                 build_hold,                 "HOLD" },
1646         { CC_SUSPEND,               EVENT_SUSPEND,              parse_suspend,              build_suspend,              "SUSPEND" },
1647         { CC_RESUME,                EVENT_RESUME,               parse_resume,               build_resume,               "RESUME" },
1648         { CC_HOLD_ACKNOWLEDGE,      EVENT_HOLD_ACKNOWLEDGE,     parse_hold_acknowledge,     build_hold_acknowledge,     "HOLD_ACKNOWLEDGE" },
1649         { CC_SUSPEND_ACKNOWLEDGE,   EVENT_SUSPEND_ACKNOWLEDGE,  parse_suspend_acknowledge,  build_suspend_acknowledge,  "SUSPEND_ACKNOWLEDGE" },
1650         { CC_RESUME_ACKNOWLEDGE,    EVENT_RESUME_ACKNOWLEDGE,   parse_resume_acknowledge,   build_resume_acknowledge,   "RESUME_ACKNOWLEDGE" },
1651         { CC_HOLD_REJECT,           EVENT_HOLD_REJECT,          parse_hold_reject,          build_hold_reject,          "HOLD_REJECT" },
1652         { CC_RETRIEVE,              EVENT_RETRIEVE,             parse_retrieve,             build_retrieve,             "RETRIEVE" },
1653         { CC_RETRIEVE_ACKNOWLEDGE,  EVENT_RETRIEVE_ACKNOWLEDGE, parse_retrieve_acknowledge, build_retrieve_acknowledge, "RETRIEVE_ACKNOWLEDGE" },
1654         { CC_RETRIEVE_REJECT,       EVENT_RETRIEVE_REJECT,      parse_retrieve_reject,      build_retrieve_reject,      "RETRIEVE_REJECT" },
1655         { CC_DISCONNECT,            EVENT_DISCONNECT,           parse_disconnect,           build_disconnect,           "DISCONNECT" },
1656         { CC_RESTART,               EVENT_RESTART,              parse_restart,              build_restart,              "RESTART" },
1657         { CC_RELEASE,               EVENT_RELEASE,              parse_release,              build_release,              "RELEASE" },
1658         { CC_RELEASE_COMPLETE,      EVENT_RELEASE_COMPLETE,     parse_release_complete,     build_release_complete,     "RELEASE_COMPLETE" },
1659         { CC_FACILITY,              EVENT_FACILITY,             parse_facility,             build_facility,             "FACILITY" },
1660         { CC_NOTIFY,                EVENT_NOTIFY,               parse_notify,               build_notify,               "NOTIFY" },
1661         { CC_STATUS_ENQUIRY,        EVENT_STATUS_ENQUIRY,       parse_status_enquiry,       build_status_enquiry,       "STATUS_ENQUIRY" },
1662         { CC_INFORMATION,           EVENT_INFORMATION,          parse_information,          build_information,          "INFORMATION" },
1663         { CC_STATUS,                EVENT_STATUS,               parse_status,               build_status,               "STATUS" },
1664         { CC_TIMEOUT,               EVENT_TIMEOUT,              parse_timeout,              build_timeout,              "TIMEOUT" },
1665         { 0, 0, NULL, NULL, NULL }
1666 /* *INDENT-ON* */
1667 };
1668
1669 #define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
1670
1671 /** INTERFACE FCTS ***/
1672 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
1673 {
1674         int i;
1675
1676         if (nt){
1677                 mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
1678
1679                 for (i=0; i< msgs_max -1; i++) {
1680                         if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1681                 }
1682
1683         } else {
1684                 iframe_t *frm = (iframe_t*)msg->data;
1685
1686                 for (i=0; i< msgs_max -1; i++)
1687                         if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1688         }
1689
1690         return -1;
1691 }
1692
1693 int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
1694 {
1695         int i;
1696         for (i=0; i< msgs_max; i++)
1697                 if ( event == msgs[i].event) return i;
1698
1699         cb_log(10,0, "get_index: event not found!\n");
1700
1701         return -1;
1702 }
1703
1704 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
1705 {
1706         int i=isdn_msg_get_index(msgs, msg, nt);
1707         if(i>=0) return msgs[i].event;
1708         return EVENT_UNKNOWN;
1709 }
1710
1711 char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
1712 {
1713         int i=isdn_msg_get_index(msgs, msg, nt);
1714         if(i>=0) return msgs[i].info;
1715         return NULL;
1716 }
1717
1718
1719 char EVENT_CLEAN_INFO[] = "CLEAN_UP";
1720 char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
1721 char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
1722 char EVENT_NEW_BC_INFO[] = "NEW_BC";
1723 char EVENT_PORT_ALARM_INFO[] = "ALARM";
1724 char EVENT_NEW_CHANNEL_INFO[] = "NEW_CHANNEL";
1725 char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
1726 char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED";
1727 char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE";
1728 char EVENT_BCHAN_ERROR_INFO[] = "BCHAN_ERROR";
1729
1730 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
1731 {
1732         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1733
1734         if(i>=0) return msgs[i].info;
1735
1736         if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO;
1737         if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO;
1738         if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
1739         if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
1740         if (event == EVENT_NEW_CHANNEL) return EVENT_NEW_CHANNEL_INFO;
1741         if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
1742         if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO;
1743         if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO;
1744         if (event == EVENT_PORT_ALARM) return EVENT_PORT_ALARM_INFO;
1745         if (event == EVENT_BCHAN_ERROR) return EVENT_BCHAN_ERROR_INFO;
1746
1747         return NULL;
1748 }
1749
1750 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1751 {
1752         int i=isdn_msg_get_index(msgs, msg, nt);
1753         if(i<0) return -1;
1754
1755         msgs[i].msg_parser(msgs, msg, bc, nt);
1756         return 0;
1757 }
1758
1759 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
1760 {
1761         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1762         if(i<0) return NULL;
1763
1764         return  msgs[i].msg_builder(msgs, bc, nt);
1765 }