Merged revisions 296582 via svnmerge from
[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         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
809         USER_INFORMATION_t *user_information;
810         msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION,  bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt);
811
812         user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN));
813
814 #ifdef DEBUG
815         printf("Building USER_INFORMATION Msg\n");
816 #endif
817         return msg;
818 }
819
820 static void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
821 {
822 #ifdef DEBUG
823         printf("Parsing SUSPEND_REJECT Msg\n");
824 #endif
825
826
827 }
828
829 static msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
830 {
831         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
832         SUSPEND_REJECT_t *suspend_reject;
833         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT,  bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt);
834
835         suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN));
836
837 #ifdef DEBUG
838         printf("Building SUSPEND_REJECT Msg\n");
839 #endif
840         return msg;
841 }
842
843 static void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
844 {
845 #ifdef DEBUG
846         printf("Parsing RESUME_REJECT Msg\n");
847 #endif
848
849
850 }
851
852 static msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
853 {
854         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
855         RESUME_REJECT_t *resume_reject;
856         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT,  bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt);
857
858         resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN));
859
860 #ifdef DEBUG
861         printf("Building RESUME_REJECT Msg\n");
862 #endif
863         return msg;
864 }
865
866 static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
867 {
868 #ifdef DEBUG
869         printf("Parsing HOLD Msg\n");
870 #endif
871
872
873 }
874
875 static msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
876 {
877         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
878         HOLD_t *hold;
879         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD,  bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt);
880
881         hold=(HOLD_t*)((msg->data+HEADER_LEN));
882
883 #ifdef DEBUG
884         printf("Building HOLD Msg\n");
885 #endif
886         return msg;
887 }
888
889 static void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
890 {
891 #ifdef DEBUG
892         printf("Parsing SUSPEND Msg\n");
893 #endif
894
895
896 }
897
898 static msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
899 {
900         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
901         SUSPEND_t *suspend;
902         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND,  bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt);
903
904         suspend=(SUSPEND_t*)((msg->data+HEADER_LEN));
905
906 #ifdef DEBUG
907         printf("Building SUSPEND Msg\n");
908 #endif
909         return msg;
910 }
911
912 static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
913 {
914 #ifdef DEBUG
915         printf("Parsing RESUME Msg\n");
916 #endif
917
918
919 }
920
921 static msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
922 {
923         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
924         RESUME_t *resume;
925         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME,  bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt);
926
927         resume=(RESUME_t*)((msg->data+HEADER_LEN));
928
929 #ifdef DEBUG
930         printf("Building RESUME Msg\n");
931 #endif
932         return msg;
933 }
934
935 static void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
936 {
937 #ifdef DEBUG
938         printf("Parsing HOLD_ACKNOWLEDGE Msg\n");
939 #endif
940
941
942 }
943
944 static msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
945 {
946         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
947         HOLD_ACKNOWLEDGE_t *hold_acknowledge;
948         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt);
949
950         hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
951
952 #ifdef DEBUG
953         printf("Building HOLD_ACKNOWLEDGE Msg\n");
954 #endif
955         return msg;
956 }
957
958 static void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
959 {
960 #ifdef DEBUG
961         printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n");
962 #endif
963
964
965 }
966
967 static msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
968 {
969         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
970         SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge;
971         msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt);
972
973         suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
974
975 #ifdef DEBUG
976         printf("Building SUSPEND_ACKNOWLEDGE Msg\n");
977 #endif
978         return msg;
979 }
980
981 static void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
982 {
983 #ifdef DEBUG
984         printf("Parsing RESUME_ACKNOWLEDGE Msg\n");
985 #endif
986
987
988 }
989
990 static msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
991 {
992         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
993         RESUME_ACKNOWLEDGE_t *resume_acknowledge;
994         msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt);
995
996         resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
997
998 #ifdef DEBUG
999         printf("Building RESUME_ACKNOWLEDGE Msg\n");
1000 #endif
1001         return msg;
1002 }
1003
1004 static void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1005 {
1006 #ifdef DEBUG
1007         printf("Parsing HOLD_REJECT Msg\n");
1008 #endif
1009
1010
1011 }
1012
1013 static msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1014 {
1015         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1016         HOLD_REJECT_t *hold_reject;
1017         msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT,  bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt);
1018
1019         hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN));
1020
1021 #ifdef DEBUG
1022         printf("Building HOLD_REJECT Msg\n");
1023 #endif
1024         return msg;
1025 }
1026
1027 static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1028 {
1029 #ifdef DEBUG
1030         printf("Parsing RETRIEVE Msg\n");
1031 #endif
1032
1033
1034 }
1035
1036 static msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1037 {
1038         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1039         RETRIEVE_t *retrieve;
1040         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt);
1041
1042         retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN));
1043
1044 #ifdef DEBUG
1045         printf("Building RETRIEVE Msg\n");
1046 #endif
1047         return msg;
1048 }
1049
1050 static void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1051 {
1052 #ifdef DEBUG
1053         printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n");
1054 #endif
1055
1056
1057 }
1058
1059 static msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1060 {
1061         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1062         RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge;
1063         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE,  bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt);
1064
1065         retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
1066
1067         enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
1068 #ifdef DEBUG
1069         printf("Building RETRIEVE_ACKNOWLEDGE Msg\n");
1070 #endif
1071         return msg;
1072 }
1073
1074 static void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1075 {
1076 #ifdef DEBUG
1077         printf("Parsing RETRIEVE_REJECT Msg\n");
1078 #endif
1079
1080
1081 }
1082
1083 static msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1084 {
1085         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1086         RETRIEVE_REJECT_t *retrieve_reject;
1087         msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT,  bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt);
1088
1089         retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN));
1090
1091 #ifdef DEBUG
1092         printf("Building RETRIEVE_REJECT Msg\n");
1093 #endif
1094         return msg;
1095 }
1096
1097 static void parse_disconnect (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         DISCONNECT_t *disconnect = (DISCONNECT_t *) (msg->data + HEADER_LEN);
1101         int location;
1102         int cause;
1103         dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc);
1104         if (cause>0) bc->cause=cause;
1105
1106         dec_ie_facility(disconnect->FACILITY, (Q931_info_t *) disconnect, &bc->fac_in, nt, bc);
1107
1108         dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
1109 #ifdef DEBUG
1110         printf("Parsing DISCONNECT Msg\n");
1111 #endif
1112
1113
1114 }
1115
1116 static msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1117 {
1118         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1119         DISCONNECT_t *disconnect;
1120         msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT,  bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt);
1121
1122         disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN));
1123
1124         enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
1125         if (nt) {
1126                 enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt ? 1 : 5, 8, nt, bc);
1127         }
1128
1129         if (bc->fac_out.Function != Fac_None) {
1130                 enc_ie_facility(&disconnect->FACILITY, msg, &bc->fac_out, nt);
1131         }
1132
1133         if (bc->uulen) {
1134                 int  protocol=4;
1135                 enc_ie_useruser(&disconnect->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1136                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1137         }
1138
1139 #ifdef DEBUG
1140         printf("Building DISCONNECT Msg\n");
1141 #endif
1142         return msg;
1143 }
1144
1145 static void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1146 {
1147         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1148         RESTART_t *restart = (RESTART_t *) (msg->data + HEADER_LEN);
1149
1150         struct misdn_stack *stack=get_stack_by_bc(bc);
1151
1152 #ifdef DEBUG
1153         printf("Parsing RESTART Msg\n");
1154 #endif
1155
1156         {
1157                 int  exclusive;
1158                 dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &bc->restart_channel, nt,bc);
1159                 cb_log(3, stack->port, "CC_RESTART Request on channel:%d on this port.\n", bc->restart_channel);
1160         }
1161
1162 }
1163
1164 static msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1165 {
1166         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1167         RESTART_t *restart;
1168         msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART,  bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt);
1169
1170         restart=(RESTART_t*)((msg->data+HEADER_LEN));
1171
1172 #ifdef DEBUG
1173         printf("Building RESTART Msg\n");
1174 #endif
1175
1176         if (bc->channel > 0) {
1177                 enc_ie_channel_id(&restart->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
1178                 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x80, nt, bc);
1179         } else {
1180                 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x87, nt, bc);
1181         }
1182
1183         cb_log(0,bc->port, "Restarting channel %d\n", bc->channel);
1184         return msg;
1185 }
1186
1187 static void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1188 {
1189         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1190         RELEASE_t *release = (RELEASE_t *) (msg->data + HEADER_LEN);
1191         int location;
1192         int cause;
1193
1194         dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc);
1195         if (cause>0) bc->cause=cause;
1196
1197         dec_ie_facility(release->FACILITY, (Q931_info_t *) release, &bc->fac_in, nt, bc);
1198
1199 #ifdef DEBUG
1200         printf("Parsing RELEASE Msg\n");
1201 #endif
1202
1203
1204 }
1205
1206 static msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1207 {
1208         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1209         RELEASE_t *release;
1210         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE,  bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt);
1211
1212         release=(RELEASE_t*)((msg->data+HEADER_LEN));
1213
1214         if (bc->out_cause>= 0)
1215                 enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1216
1217         if (bc->fac_out.Function != Fac_None) {
1218                 enc_ie_facility(&release->FACILITY, msg, &bc->fac_out, nt);
1219         }
1220
1221         if (bc->uulen) {
1222                 int  protocol=4;
1223                 enc_ie_useruser(&release->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1224                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1225         }
1226
1227 #ifdef DEBUG
1228         printf("Building RELEASE Msg\n");
1229 #endif
1230         return msg;
1231 }
1232
1233 static void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1234 {
1235         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1236         RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *) (msg->data + HEADER_LEN);
1237         int location;
1238         int cause;
1239         iframe_t *frm = (iframe_t*) msg->data;
1240
1241         struct misdn_stack *stack=get_stack_by_bc(bc);
1242         mISDNuser_head_t *hh;
1243         hh=(mISDNuser_head_t*)msg->data;
1244
1245         /*hh=(mISDN_head_t*)msg->data;
1246         mISDN_head_t *hh;*/
1247
1248         if (nt) {
1249                 if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1250                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n");
1251                         return;
1252                 }
1253         } else {
1254                 if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1255                         cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n");
1256                         return;
1257                 }
1258         }
1259         dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc);
1260         if (cause>0) bc->cause=cause;
1261
1262         dec_ie_facility(release_complete->FACILITY, (Q931_info_t *) release_complete, &bc->fac_in, nt, bc);
1263
1264 #ifdef DEBUG
1265         printf("Parsing RELEASE_COMPLETE Msg\n");
1266 #endif
1267 }
1268
1269 static msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1270 {
1271         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1272         RELEASE_COMPLETE_t *release_complete;
1273         msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE,  bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt);
1274
1275         release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN));
1276
1277         enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1278
1279         if (bc->fac_out.Function != Fac_None) {
1280                 enc_ie_facility(&release_complete->FACILITY, msg, &bc->fac_out, nt);
1281         }
1282
1283         if (bc->uulen) {
1284                 int  protocol=4;
1285                 enc_ie_useruser(&release_complete->USER_USER, msg, protocol, bc->uu, bc->uulen, nt,bc);
1286                 cb_log(1,bc->port,"ENCODING USERUESRINFO:%s\n",bc->uu);
1287         }
1288
1289 #ifdef DEBUG
1290         printf("Building RELEASE_COMPLETE Msg\n");
1291 #endif
1292         return msg;
1293 }
1294
1295 static void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1296 {
1297         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1298         FACILITY_t *facility = (FACILITY_t*)(msg->data+HEADER_LEN);
1299         Q931_info_t *qi = (Q931_info_t*)(msg->data+HEADER_LEN);
1300         unsigned char *p = NULL;
1301 #if defined(AST_MISDN_ENHANCEMENTS)
1302         int description_code;
1303         int type;
1304         int plan;
1305         int present;
1306         char number[sizeof(bc->redirecting.to.number)];
1307 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1308
1309 #ifdef DEBUG
1310         printf("Parsing FACILITY Msg\n");
1311 #endif
1312
1313         bc->fac_in.Function = Fac_None;
1314
1315         if (!bc->nt) {
1316                 if (qi->QI_ELEMENT(facility))
1317                         p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
1318         } else {
1319                 p = facility->FACILITY;
1320         }
1321         if (!p)
1322                 return;
1323
1324         if (decodeFac(p, &bc->fac_in)) {
1325                 cb_log(3, bc->port, "Decoding facility ie failed! Unrecognized facility message?\n");
1326         }
1327
1328 #if defined(AST_MISDN_ENHANCEMENTS)
1329         dec_ie_notify(facility->NOTIFY, qi, &description_code, nt, bc);
1330         if (description_code < 0) {
1331                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1332         } else {
1333                 bc->notify_description_code = description_code;
1334         }
1335
1336         dec_ie_redir_dn(facility->REDIR_DN, qi, &type, &plan, &present, number, sizeof(number), nt, bc);
1337         if (0 <= type) {
1338                 bc->redirecting.to_changed = 1;
1339
1340                 bc->redirecting.to.number_type = type;
1341                 bc->redirecting.to.number_plan = plan;
1342                 switch (present) {
1343                 default:
1344                 case 0:
1345                         bc->redirecting.to.presentation = 0;    /* presentation allowed */
1346                         break;
1347                 case 1:
1348                         bc->redirecting.to.presentation = 1;    /* presentation restricted */
1349                         break;
1350                 case 2:
1351                         bc->redirecting.to.presentation = 2;    /* Number not available */
1352                         break;
1353                 }
1354                 bc->redirecting.to.screening = 0;       /* Unscreened */
1355                 strcpy(bc->redirecting.to.number, number);
1356         }
1357 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1358 }
1359
1360 static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1361 {
1362         int len;
1363         int HEADER_LEN;
1364         unsigned char *ie_fac;
1365         unsigned char fac_tmp[256];
1366         msg_t *msg;
1367         FACILITY_t *facility;
1368         Q931_info_t *qi;
1369
1370 #ifdef DEBUG
1371         printf("Building FACILITY Msg\n");
1372 #endif
1373
1374         len = encodeFac(fac_tmp, &(bc->fac_out));
1375         if (len <= 0) {
1376                 /*
1377                  * mISDN does not know how to build the requested facility structure
1378                  * Clear facility information
1379                  */
1380                 bc->fac_out.Function = Fac_None;
1381
1382 #if defined(AST_MISDN_ENHANCEMENTS)
1383                 /* Clear other one shot information. */
1384                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1385                 bc->redirecting.to_changed = 0;
1386 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1387                 return NULL;
1388         }
1389
1390         msg = (msg_t *) create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, bc ? bc->l3_id : -1, sizeof(FACILITY_t), nt);
1391         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1392         facility = (FACILITY_t *) (msg->data + HEADER_LEN);
1393
1394         ie_fac = msg_put(msg, len);
1395         if (bc->nt) {
1396                 facility->FACILITY = ie_fac + 1;
1397         } else {
1398                 qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
1399                 qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t);
1400         }
1401
1402         memcpy(ie_fac, fac_tmp, len);
1403
1404         /* Clear facility information */
1405         bc->fac_out.Function = Fac_None;
1406
1407         if (*bc->display) {
1408 #ifdef DEBUG
1409                 printf("Sending %s as Display\n", bc->display);
1410 #endif
1411                 enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
1412         }
1413
1414 #if defined(AST_MISDN_ENHANCEMENTS)
1415         if (bc->notify_description_code != mISDN_NOTIFY_CODE_INVALID) {
1416                 enc_ie_notify(&facility->NOTIFY, msg, bc->notify_description_code, nt, bc);
1417                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1418         }
1419
1420         if (bc->redirecting.to_changed) {
1421                 bc->redirecting.to_changed = 0;
1422                 switch (bc->outgoing_colp) {
1423                 case 0:/* pass */
1424                 case 1:/* restricted */
1425                         enc_ie_redir_dn(&facility->REDIR_DN, msg, bc->redirecting.to.number_type,
1426                                 bc->redirecting.to.number_plan, bc->redirecting.to.presentation,
1427                                 bc->redirecting.to.number, nt, bc);
1428                         break;
1429                 default:
1430                         break;
1431                 }
1432         }
1433 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1434
1435         return msg;
1436 }
1437
1438 #if defined(AST_MISDN_ENHANCEMENTS)
1439 /*!
1440  * \internal
1441  * \brief Parse a received REGISTER message
1442  *
1443  * \param msgs Search table entry that called us.
1444  * \param msg Received message contents
1445  * \param bc Associated B channel
1446  * \param nt TRUE if in NT mode.
1447  *
1448  * \return Nothing
1449  */
1450 static void parse_register(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1451 {
1452         int HEADER_LEN;
1453         REGISTER_t *reg;
1454
1455         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1456         reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1457
1458         /*
1459          * A facility ie is optional.
1460          * The peer may just be establishing a connection to send
1461          * messages later.
1462          */
1463         dec_ie_facility(reg->FACILITY, (Q931_info_t *) reg, &bc->fac_in, nt, bc);
1464 }
1465 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1466
1467 #if defined(AST_MISDN_ENHANCEMENTS)
1468 /*!
1469  * \internal
1470  * \brief Construct a REGISTER message
1471  *
1472  * \param msgs Search table entry that called us.
1473  * \param bc Associated B channel
1474  * \param nt TRUE if in NT mode.
1475  *
1476  * \return Allocated built message
1477  */
1478 static msg_t *build_register(struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1479 {
1480         int HEADER_LEN;
1481         REGISTER_t *reg;
1482         msg_t *msg;
1483
1484         msg = (msg_t *) create_l3msg(CC_REGISTER | REQUEST, MT_REGISTER,  bc ? bc->l3_id : -1, sizeof(REGISTER_t), nt);
1485         HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1486         reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1487
1488         if (bc->fac_out.Function != Fac_None) {
1489                 enc_ie_facility(&reg->FACILITY, msg, &bc->fac_out, nt);
1490         }
1491
1492         return msg;
1493 }
1494 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1495
1496 static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1497 {
1498         int HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1499         NOTIFY_t *notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1500         int description_code;
1501         int type;
1502         int plan;
1503         int present;
1504         char number[sizeof(bc->redirecting.to.number)];
1505
1506 #ifdef DEBUG
1507         printf("Parsing NOTIFY Msg\n");
1508 #endif
1509
1510         dec_ie_notify(notify->NOTIFY, (Q931_info_t *) notify, &description_code, nt, bc);
1511         if (description_code < 0) {
1512                 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1513         } else {
1514                 bc->notify_description_code = description_code;
1515         }
1516
1517         dec_ie_redir_dn(notify->REDIR_DN, (Q931_info_t *) notify, &type, &plan, &present, number, sizeof(number), nt, bc);
1518         if (0 <= type) {
1519                 bc->redirecting.to_changed = 1;
1520
1521                 bc->redirecting.to.number_type = type;
1522                 bc->redirecting.to.number_plan = plan;
1523                 switch (present) {
1524                 default:
1525                 case 0:
1526                         bc->redirecting.to.presentation = 0;    /* presentation allowed */
1527                         break;
1528                 case 1:
1529                         bc->redirecting.to.presentation = 1;    /* presentation restricted */
1530                         break;
1531                 case 2:
1532                         bc->redirecting.to.presentation = 2;    /* Number not available */
1533                         break;
1534                 }
1535                 bc->redirecting.to.screening = 0;       /* Unscreened */
1536                 strcpy(bc->redirecting.to.number, number);
1537         }
1538 }
1539
1540 static msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1541 {
1542         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1543         NOTIFY_t *notify;
1544         msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY,  bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt);
1545
1546 #ifdef DEBUG
1547         printf("Building NOTIFY Msg\n");
1548 #endif
1549
1550         notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1551
1552         enc_ie_notify(&notify->NOTIFY, msg, bc->notify_description_code, nt, bc);
1553         bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1554
1555         if (bc->redirecting.to_changed) {
1556                 bc->redirecting.to_changed = 0;
1557                 switch (bc->outgoing_colp) {
1558                 case 0:/* pass */
1559                 case 1:/* restricted */
1560                         enc_ie_redir_dn(&notify->REDIR_DN, msg, bc->redirecting.to.number_type,
1561                                 bc->redirecting.to.number_plan, bc->redirecting.to.presentation,
1562                                 bc->redirecting.to.number, nt, bc);
1563                         break;
1564                 default:
1565                         break;
1566                 }
1567         }
1568         return msg;
1569 }
1570
1571 static void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1572 {
1573 #ifdef DEBUG
1574         printf("Parsing STATUS_ENQUIRY Msg\n");
1575 #endif
1576 }
1577
1578 static msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1579 {
1580         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1581         STATUS_ENQUIRY_t *status_enquiry;
1582         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY,  bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt);
1583
1584         status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN));
1585
1586 #ifdef DEBUG
1587         printf("Building STATUS_ENQUIRY Msg\n");
1588 #endif
1589         return msg;
1590 }
1591
1592 static void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1593 {
1594         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1595         INFORMATION_t *information = (INFORMATION_t *) (msg->data + HEADER_LEN);
1596         int type, plan;
1597
1598         dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *) information, &type, &plan, bc->info_dad, sizeof(bc->info_dad), nt, bc);
1599         dec_ie_keypad(information->KEYPAD, (Q931_info_t *) information, bc->keypad, sizeof(bc->keypad), nt, bc);
1600
1601 #ifdef DEBUG
1602         printf("Parsing INFORMATION Msg\n");
1603 #endif
1604 }
1605
1606 static msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1607 {
1608         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1609         INFORMATION_t *information;
1610         msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION,  bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt);
1611
1612         information=(INFORMATION_t*)((msg->data+HEADER_LEN));
1613
1614         enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
1615
1616         {
1617                 if (*bc->display) {
1618 #ifdef DEBUG
1619                         printf("Sending %s as Display\n", bc->display);
1620 #endif
1621                         enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
1622                 }
1623         }
1624
1625 #ifdef DEBUG
1626         printf("Building INFORMATION Msg\n");
1627 #endif
1628         return msg;
1629 }
1630
1631 static void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1632 {
1633         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1634         STATUS_t *status = (STATUS_t *) (msg->data + HEADER_LEN);
1635         int location;
1636         int cause;
1637
1638         dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc);
1639         if (cause>0) bc->cause=cause;
1640
1641 #ifdef DEBUG
1642         printf("Parsing STATUS Msg\n");
1643 #endif
1644 }
1645
1646 static msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1647 {
1648         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1649         STATUS_t *status;
1650         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1651
1652         status=(STATUS_t*)((msg->data+HEADER_LEN));
1653
1654 #ifdef DEBUG
1655         printf("Building STATUS Msg\n");
1656 #endif
1657         return msg;
1658 }
1659
1660 static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1661 {
1662 #ifdef DEBUG
1663         printf("Parsing STATUS Msg\n");
1664 #endif
1665 }
1666
1667 static msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1668 {
1669         int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1670         STATUS_t *status;
1671         msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS,  bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1672
1673         status=(STATUS_t*)((msg->data+HEADER_LEN));
1674
1675 #ifdef DEBUG
1676         printf("Building STATUS Msg\n");
1677 #endif
1678         return msg;
1679 }
1680
1681
1682 /************************************/
1683
1684
1685
1686
1687 /** Msg Array **/
1688
1689 struct isdn_msg msgs_g[] = {
1690 /* *INDENT-OFF* */
1691         /* misdn_msg,               event,                      msg_parser,                 msg_builder,                info */
1692         { CC_PROCEEDING,            EVENT_PROCEEDING,           parse_proceeding,           build_proceeding,           "PROCEEDING" },
1693         { CC_ALERTING,              EVENT_ALERTING,             parse_alerting,             build_alerting,             "ALERTING" },
1694         { CC_PROGRESS,              EVENT_PROGRESS,             parse_progress,             build_progress,             "PROGRESS" },
1695         { CC_SETUP,                 EVENT_SETUP,                parse_setup,                build_setup,                "SETUP" },
1696 #if defined(AST_MISDN_ENHANCEMENTS)
1697         { CC_REGISTER,              EVENT_REGISTER,             parse_register,             build_register,             "REGISTER" },
1698 #endif  /* defined(AST_MISDN_ENHANCEMENTS) */
1699         { CC_CONNECT,               EVENT_CONNECT,              parse_connect,              build_connect,              "CONNECT" },
1700         { CC_SETUP_ACKNOWLEDGE,     EVENT_SETUP_ACKNOWLEDGE,    parse_setup_acknowledge,    build_setup_acknowledge,    "SETUP_ACKNOWLEDGE" },
1701         { CC_CONNECT_ACKNOWLEDGE,   EVENT_CONNECT_ACKNOWLEDGE,  parse_connect_acknowledge,  build_connect_acknowledge,  "CONNECT_ACKNOWLEDGE " },
1702         { CC_USER_INFORMATION,      EVENT_USER_INFORMATION,     parse_user_information,     build_user_information,     "USER_INFORMATION" },
1703         { CC_SUSPEND_REJECT,        EVENT_SUSPEND_REJECT,       parse_suspend_reject,       build_suspend_reject,       "SUSPEND_REJECT" },
1704         { CC_RESUME_REJECT,         EVENT_RESUME_REJECT,        parse_resume_reject,        build_resume_reject,        "RESUME_REJECT" },
1705         { CC_HOLD,                  EVENT_HOLD,                 parse_hold,                 build_hold,                 "HOLD" },
1706         { CC_SUSPEND,               EVENT_SUSPEND,              parse_suspend,              build_suspend,              "SUSPEND" },
1707         { CC_RESUME,                EVENT_RESUME,               parse_resume,               build_resume,               "RESUME" },
1708         { CC_HOLD_ACKNOWLEDGE,      EVENT_HOLD_ACKNOWLEDGE,     parse_hold_acknowledge,     build_hold_acknowledge,     "HOLD_ACKNOWLEDGE" },
1709         { CC_SUSPEND_ACKNOWLEDGE,   EVENT_SUSPEND_ACKNOWLEDGE,  parse_suspend_acknowledge,  build_suspend_acknowledge,  "SUSPEND_ACKNOWLEDGE" },
1710         { CC_RESUME_ACKNOWLEDGE,    EVENT_RESUME_ACKNOWLEDGE,   parse_resume_acknowledge,   build_resume_acknowledge,   "RESUME_ACKNOWLEDGE" },
1711         { CC_HOLD_REJECT,           EVENT_HOLD_REJECT,          parse_hold_reject,          build_hold_reject,          "HOLD_REJECT" },
1712         { CC_RETRIEVE,              EVENT_RETRIEVE,             parse_retrieve,             build_retrieve,             "RETRIEVE" },
1713         { CC_RETRIEVE_ACKNOWLEDGE,  EVENT_RETRIEVE_ACKNOWLEDGE, parse_retrieve_acknowledge, build_retrieve_acknowledge, "RETRIEVE_ACKNOWLEDGE" },
1714         { CC_RETRIEVE_REJECT,       EVENT_RETRIEVE_REJECT,      parse_retrieve_reject,      build_retrieve_reject,      "RETRIEVE_REJECT" },
1715         { CC_DISCONNECT,            EVENT_DISCONNECT,           parse_disconnect,           build_disconnect,           "DISCONNECT" },
1716         { CC_RESTART,               EVENT_RESTART,              parse_restart,              build_restart,              "RESTART" },
1717         { CC_RELEASE,               EVENT_RELEASE,              parse_release,              build_release,              "RELEASE" },
1718         { CC_RELEASE_COMPLETE,      EVENT_RELEASE_COMPLETE,     parse_release_complete,     build_release_complete,     "RELEASE_COMPLETE" },
1719         { CC_FACILITY,              EVENT_FACILITY,             parse_facility,             build_facility,             "FACILITY" },
1720         { CC_NOTIFY,                EVENT_NOTIFY,               parse_notify,               build_notify,               "NOTIFY" },
1721         { CC_STATUS_ENQUIRY,        EVENT_STATUS_ENQUIRY,       parse_status_enquiry,       build_status_enquiry,       "STATUS_ENQUIRY" },
1722         { CC_INFORMATION,           EVENT_INFORMATION,          parse_information,          build_information,          "INFORMATION" },
1723         { CC_STATUS,                EVENT_STATUS,               parse_status,               build_status,               "STATUS" },
1724         { CC_TIMEOUT,               EVENT_TIMEOUT,              parse_timeout,              build_timeout,              "TIMEOUT" },
1725         { 0, 0, NULL, NULL, NULL }
1726 /* *INDENT-ON* */
1727 };
1728
1729 #define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
1730
1731 /** INTERFACE FCTS ***/
1732 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
1733 {
1734         int i;
1735
1736         if (nt){
1737                 mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
1738
1739                 for (i=0; i< msgs_max -1; i++) {
1740                         if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1741                 }
1742
1743         } else {
1744                 iframe_t *frm = (iframe_t*)msg->data;
1745
1746                 for (i=0; i< msgs_max -1; i++)
1747                         if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1748         }
1749
1750         return -1;
1751 }
1752
1753 int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
1754 {
1755         int i;
1756         for (i=0; i< msgs_max; i++)
1757                 if ( event == msgs[i].event) return i;
1758
1759         cb_log(10,0, "get_index: event not found!\n");
1760
1761         return -1;
1762 }
1763
1764 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
1765 {
1766         int i=isdn_msg_get_index(msgs, msg, nt);
1767         if(i>=0) return msgs[i].event;
1768         return EVENT_UNKNOWN;
1769 }
1770
1771 char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
1772 {
1773         int i=isdn_msg_get_index(msgs, msg, nt);
1774         if(i>=0) return msgs[i].info;
1775         return NULL;
1776 }
1777
1778
1779 char EVENT_CLEAN_INFO[] = "CLEAN_UP";
1780 char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE";
1781 char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID";
1782 char EVENT_NEW_BC_INFO[] = "NEW_BC";
1783 char EVENT_PORT_ALARM_INFO[] = "ALARM";
1784 char EVENT_NEW_CHANNEL_INFO[] = "NEW_CHANNEL";
1785 char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA";
1786 char EVENT_BCHAN_ACTIVATED_INFO[] = "BCHAN_ACTIVATED";
1787 char EVENT_TONE_GENERATE_INFO[] = "TONE_GENERATE";
1788 char EVENT_BCHAN_ERROR_INFO[] = "BCHAN_ERROR";
1789
1790 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
1791 {
1792         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1793
1794         if(i>=0) return msgs[i].info;
1795
1796         if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO;
1797         if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO;
1798         if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO;
1799         if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO;
1800         if (event == EVENT_NEW_CHANNEL) return EVENT_NEW_CHANNEL_INFO;
1801         if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO;
1802         if (event == EVENT_BCHAN_ACTIVATED) return EVENT_BCHAN_ACTIVATED_INFO;
1803         if (event == EVENT_TONE_GENERATE) return EVENT_TONE_GENERATE_INFO;
1804         if (event == EVENT_PORT_ALARM) return EVENT_PORT_ALARM_INFO;
1805         if (event == EVENT_BCHAN_ERROR) return EVENT_BCHAN_ERROR_INFO;
1806
1807         return NULL;
1808 }
1809
1810 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1811 {
1812         int i=isdn_msg_get_index(msgs, msg, nt);
1813         if(i<0) return -1;
1814
1815         msgs[i].msg_parser(msgs, msg, bc, nt);
1816         return 0;
1817 }
1818
1819 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
1820 {
1821         int i=isdn_msg_get_index_by_event(msgs, event, nt);
1822         if(i<0) return NULL;
1823
1824         return  msgs[i].msg_builder(msgs, bc, nt);
1825 }