2 * Chan_Misdn -- Channel Driver for Asterisk
6 * Copyright (C) 2004, Christian Richter
8 * Christian Richter <crich@beronet.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
15 * \brief Interface to mISDN - message parser
16 * \author Christian Richter <crich@beronet.com>
21 #include "isdn_lib_intern.h"
30 * \brief Build the name, number, name/number display message string
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
40 static void build_display_str(char *display, size_t display_length, int display_format, const char *name, const char *number)
43 switch (display_format) {
49 snprintf(display, display_length, "%s", name);
53 snprintf(display, display_length, "%s", number);
57 if (name[0] || number[0]) {
58 snprintf(display, display_length, "\"%s\" <%s>", name, number);
66 * \brief Encode the Facility IE and put it into the message structure.
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.
75 static void enc_ie_facility(unsigned char **ntmode, msg_t *msg, struct FacParm *fac, int nt)
80 unsigned char buf[256];
82 len = encodeFac(buf, fac);
85 * mISDN does not know how to build the requested facility structure
86 * Clear facility information
88 fac->Function = Fac_None;
92 p = msg_put(msg, len);
96 qi = (Q931_info_t *) (msg->data + mISDN_HEADER_LEN);
97 qi->QI_ELEMENT(facility) = p - (unsigned char *) qi - sizeof(Q931_info_t);
102 /* Clear facility information */
103 fac->Function = Fac_None;
108 * \brief Decode the Facility IE.
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
118 static void dec_ie_facility(unsigned char *p, Q931_info_t *qi, struct FacParm *fac, int nt, struct misdn_bchannel *bc)
120 fac->Function = Fac_None;
124 if (qi->QI_ELEMENT(facility)) {
125 p = (unsigned char *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
132 if (decodeFac(p, fac)) {
133 cb_log(3, bc->port, "Decoding facility ie failed! Unrecognized facility message?\n");
139 static void set_channel(struct misdn_bchannel *bc, int channel)
142 cb_log(3,bc->port,"set_channel: bc->channel:%d channel:%d\n", bc->channel, channel);
150 /* ALERT: is that everytime true ? */
151 if (channel > 0 && bc->nt ) {
153 if (bc->channel && ( bc->channel != 0xff) ) {
154 cb_log(0,bc->port,"We already have a channel (%d)\n", bc->channel);
156 bc->channel = channel;
157 cb_event(EVENT_NEW_CHANNEL,bc,NULL);
161 if (channel > 0 && !bc->nt ) {
162 bc->channel = channel;
163 cb_event(EVENT_NEW_CHANNEL,bc,NULL);
167 static void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
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);
174 int exclusive, channel;
175 dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc);
177 set_channel(bc,channel);
181 dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
183 dec_ie_facility(proceeding->FACILITY, (Q931_info_t *) proceeding, &bc->fac_in, nt, bc);
185 /* dec_ie_redir_dn */
188 printf("Parsing PROCEEDING Msg\n");
191 static msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
197 proceeding=(CALL_PROCEEDING_t*)((msg->data+HEADER_LEN));
199 enc_ie_channel_id(&proceeding->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
202 enc_ie_progress(&proceeding->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
204 if (bc->fac_out.Function != Fac_None) {
205 enc_ie_facility(&proceeding->FACILITY, msg, &bc->fac_out, nt);
208 /* enc_ie_redir_dn */
211 printf("Building PROCEEDING Msg\n");
216 static void parse_alerting (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
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);
222 dec_ie_facility(alerting->FACILITY, (Q931_info_t *) alerting, &bc->fac_in, nt, bc);
224 /* dec_ie_redir_dn */
226 dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)alerting, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
229 printf("Parsing ALERTING Msg\n");
235 static msg_t *build_alerting (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
241 alerting=(ALERTING_t*)((msg->data+HEADER_LEN));
243 enc_ie_channel_id(&alerting->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
246 enc_ie_progress(&alerting->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
248 if (bc->fac_out.Function != Fac_None) {
249 enc_ie_facility(&alerting->FACILITY, msg, &bc->fac_out, nt);
252 /* enc_ie_redir_dn */
255 printf("Building ALERTING Msg\n");
261 static void parse_progress (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
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);
267 dec_ie_progress(progress->PROGRESS, (Q931_info_t *)progress, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
269 dec_ie_facility(progress->FACILITY, (Q931_info_t *) progress, &bc->fac_in, nt, bc);
272 printf("Parsing PROGRESS Msg\n");
276 static msg_t *build_progress (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
282 progress=(PROGRESS_t*)((msg->data+HEADER_LEN));
284 if (bc->fac_out.Function != Fac_None) {
285 enc_ie_facility(&progress->FACILITY, msg, &bc->fac_out, nt);
289 printf("Building PROGRESS Msg\n");
294 #if defined(AST_MISDN_ENHANCEMENTS)
297 * \brief Extract the SETUP message's BC, HLC, and LLC encoded ie contents.
299 * \param setup Indexed setup message contents
300 * \param nt TRUE if in NT mode.
301 * \param bc Associated B channel
305 static void extract_setup_Bc_Hlc_Llc(SETUP_t *setup, int nt, struct misdn_bchannel *bc)
310 qi = (Q931_info_t *) setup;
312 /* Extract Bearer Capability */
314 p = (__u8 *) setup->BEARER;
316 if (qi->QI_ELEMENT(bearer_capability)) {
317 p = (__u8 *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(bearer_capability) + 1;
322 if (!p || *p == 0 || sizeof(bc->setup_bc_hlc_llc.Bc.Contents) < *p) {
323 bc->setup_bc_hlc_llc.Bc.Length = 0;
325 bc->setup_bc_hlc_llc.Bc.Length = *p;
326 memcpy(bc->setup_bc_hlc_llc.Bc.Contents, p + 1, *p);
329 /* Extract Low Layer Compatibility */
331 p = (__u8 *) setup->LLC;
333 if (qi->QI_ELEMENT(llc)) {
334 p = (__u8 *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(llc) + 1;
339 if (!p || *p == 0 || sizeof(bc->setup_bc_hlc_llc.Llc.Contents) < *p) {
340 bc->setup_bc_hlc_llc.Llc.Length = 0;
342 bc->setup_bc_hlc_llc.Llc.Length = *p;
343 memcpy(bc->setup_bc_hlc_llc.Llc.Contents, p + 1, *p);
346 /* Extract High Layer Compatibility */
348 p = (__u8 *) setup->HLC;
350 if (qi->QI_ELEMENT(hlc)) {
351 p = (__u8 *) qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(hlc) + 1;
356 if (!p || *p == 0 || sizeof(bc->setup_bc_hlc_llc.Hlc.Contents) < *p) {
357 bc->setup_bc_hlc_llc.Hlc.Length = 0;
359 bc->setup_bc_hlc_llc.Hlc.Length = *p;
360 memcpy(bc->setup_bc_hlc_llc.Hlc.Contents, p + 1, *p);
363 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
365 static void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
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);
377 printf("Parsing SETUP Msg\n");
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;
386 bc->caller.presentation = 0; /* presentation allowed */
389 bc->caller.presentation = 1; /* presentation restricted */
392 bc->caller.presentation = 2; /* Number not available */
396 bc->caller.screening = screen;
398 bc->caller.screening = 0; /* Unscreened */
401 dec_ie_facility(setup->FACILITY, (Q931_info_t *) setup, &bc->fac_in, nt, bc);
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;
407 dec_ie_keypad(setup->KEYPAD, (Q931_info_t *) setup, bc->keypad, sizeof(bc->keypad), nt, bc);
409 dec_ie_complete(setup->COMPLETE, (Q931_info_t *) setup, &bc->sending_complete, nt, bc);
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;
417 bc->redirecting.from.presentation = 0; /* presentation allowed */
420 bc->redirecting.from.presentation = 1; /* presentation restricted */
423 bc->redirecting.from.presentation = 2; /* Number not available */
427 bc->redirecting.from.screening = screen;
429 bc->redirecting.from.screening = 0; /* Unscreened */
432 bc->redirecting.reason = reason;
434 bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
438 int coding, capability, mode, rate, multi, user, async, urate, stopbits, dbits, parity;
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;
444 case 0: bc->capability=INFO_CAPABILITY_SPEECH;
446 case 18: bc->capability=INFO_CAPABILITY_VIDEO;
448 case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
455 case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED;
463 bc->law=INFO_CODEC_ULAW;
466 bc->law=INFO_CODEC_ALAW;
469 bc->law=INFO_CODEC_ALAW;
473 bc->capability=capability;
476 int exclusive, channel;
477 dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)setup, &exclusive, &channel, nt,bc);
479 set_channel(bc,channel);
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);
487 cb_log(1,bc->port,"NO USERUESRINFO\n");
490 dec_ie_progress(setup->PROGRESS, (Q931_info_t *)setup, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
492 #if defined(AST_MISDN_ENHANCEMENTS)
493 extract_setup_Bc_Hlc_Llc(setup, nt, bc);
494 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
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)
500 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
502 msg_t *msg =(msg_t*)create_l3msg(CC_SETUP | REQUEST, MT_SETUP, bc?bc->l3_id:-1, sizeof(SETUP_t) ,nt);
504 enum FacFunction fac_type;
506 setup=(SETUP_t*)((msg->data+HEADER_LEN));
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);
511 enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
513 fac_type = bc->fac_out.Function;
514 if (fac_type != Fac_None) {
515 enc_ie_facility(&setup->FACILITY, msg, &bc->fac_out, nt);
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);
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);
525 switch (bc->outgoing_colp) {
527 case 1:/* restricted */
528 is_ptp = misdn_lib_is_ptp(bc->port);
529 if (bc->redirecting.from.number[0]
532 #if defined(AST_MISDN_ENHANCEMENTS)
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
539 && fac_type != Fac_DivertingLegInformation2
540 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
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);
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);
561 enc_ie_keypad(&setup->KEYPAD, msg, bc->keypad, nt,bc);
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)];
571 /* Presentation is allowed */
572 build_display_str(display, sizeof(display), bc->display_setup, bc->caller.name, bc->caller.number);
574 enc_ie_display(&setup->DISPLAY, msg, display, nt, bc);
581 int mode = 0; /* 2 for packet! */
586 case INFO_CODEC_ULAW: user=2;
588 case INFO_CODEC_ALAW: user=3;
594 switch (bc->capability) {
595 case INFO_CAPABILITY_SPEECH: capability = 0;
597 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8;
602 case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9;
606 capability=bc->capability;
609 enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc);
612 if (bc->sending_complete) {
613 enc_ie_complete(&setup->COMPLETE,msg, bc->sending_complete, nt, bc);
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);
622 #if defined(AST_MISDN_ENHANCEMENTS)
623 extract_setup_Bc_Hlc_Llc(setup, nt, bc);
624 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
627 printf("Building SETUP Msg\n");
632 static void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
634 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
635 CONNECT_t *connect = (CONNECT_t *) (msg->data + HEADER_LEN);
641 bc->ces = connect->ces;
643 dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
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;
652 bc->connected.presentation = 0; /* presentation allowed */
655 bc->connected.presentation = 1; /* presentation restricted */
658 bc->connected.presentation = 2; /* Number not available */
662 bc->connected.screening = screen;
664 bc->connected.screening = 0; /* Unscreened */
667 dec_ie_facility(connect->FACILITY, (Q931_info_t *) connect, &bc->fac_in, nt, bc);
670 cb_log(1,bc->port,"CONNETED PN: %s cpn_dialplan:%d\n", connected_pn, type);
674 printf("Parsing CONNECT Msg\n");
678 static msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
680 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
682 msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt);
684 cb_log(6,bc->port,"BUILD_CONNECT: bc:%p bc->l3id:%d, nt:%d\n",bc,bc->l3_id,nt);
686 connect=(CONNECT_t*)((msg->data+HEADER_LEN));
691 enc_ie_date(&connect->DATE, msg, now, nt,bc);
694 switch (bc->outgoing_colp) {
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);
705 if (nt && bc->connected.presentation == 0) {
706 char display[sizeof(bc->display)];
708 /* Presentation is allowed */
709 build_display_str(display, sizeof(display), bc->display_connected, bc->connected.name, bc->connected.number);
711 enc_ie_display(&connect->DISPLAY, msg, display, nt, bc);
715 if (bc->fac_out.Function != Fac_None) {
716 enc_ie_facility(&connect->FACILITY, msg, &bc->fac_out, nt);
720 printf("Building CONNECT Msg\n");
725 static void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
727 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
728 SETUP_ACKNOWLEDGE_t *setup_acknowledge = (SETUP_ACKNOWLEDGE_t *) (msg->data + HEADER_LEN);
731 int exclusive, channel;
732 dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc);
735 set_channel(bc, channel);
738 dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
740 dec_ie_facility(setup_acknowledge->FACILITY, (Q931_info_t *) setup_acknowledge, &bc->fac_in, nt, bc);
743 printf("Parsing SETUP_ACKNOWLEDGE Msg\n");
749 static msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
755 setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
757 enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc);
760 enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc);
762 if (bc->fac_out.Function != Fac_None) {
763 enc_ie_facility(&setup_acknowledge->FACILITY, msg, &bc->fac_out, nt);
767 printf("Building SETUP_ACKNOWLEDGE Msg\n");
772 static void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
775 printf("Parsing CONNECT_ACKNOWLEDGE Msg\n");
781 static msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
787 connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
789 enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
792 printf("Building CONNECT_ACKNOWLEDGE Msg\n");
797 static void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
800 printf("Parsing USER_INFORMATION Msg\n");
806 static msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
812 user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN));
815 printf("Building USER_INFORMATION Msg\n");
820 static void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
823 printf("Parsing SUSPEND_REJECT Msg\n");
829 static msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
835 suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN));
838 printf("Building SUSPEND_REJECT Msg\n");
843 static void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
846 printf("Parsing RESUME_REJECT Msg\n");
852 static msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
858 resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN));
861 printf("Building RESUME_REJECT Msg\n");
866 static void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
869 printf("Parsing HOLD Msg\n");
875 static msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
877 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
879 msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD, bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt);
881 hold=(HOLD_t*)((msg->data+HEADER_LEN));
884 printf("Building HOLD Msg\n");
889 static void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
892 printf("Parsing SUSPEND Msg\n");
898 static msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
900 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
902 msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND, bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt);
904 suspend=(SUSPEND_t*)((msg->data+HEADER_LEN));
907 printf("Building SUSPEND Msg\n");
912 static void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
915 printf("Parsing RESUME Msg\n");
921 static msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
923 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
925 msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME, bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt);
927 resume=(RESUME_t*)((msg->data+HEADER_LEN));
930 printf("Building RESUME Msg\n");
935 static void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
938 printf("Parsing HOLD_ACKNOWLEDGE Msg\n");
944 static msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
950 hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
953 printf("Building HOLD_ACKNOWLEDGE Msg\n");
958 static void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
961 printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n");
967 static msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
973 suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
976 printf("Building SUSPEND_ACKNOWLEDGE Msg\n");
981 static void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
984 printf("Parsing RESUME_ACKNOWLEDGE Msg\n");
990 static msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
996 resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
999 printf("Building RESUME_ACKNOWLEDGE Msg\n");
1004 static void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1007 printf("Parsing HOLD_REJECT Msg\n");
1013 static msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1019 hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN));
1022 printf("Building HOLD_REJECT Msg\n");
1027 static void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1030 printf("Parsing RETRIEVE Msg\n");
1036 static msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1042 retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN));
1045 printf("Building RETRIEVE Msg\n");
1050 static void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1053 printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n");
1059 static msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1065 retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN));
1067 enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc);
1069 printf("Building RETRIEVE_ACKNOWLEDGE Msg\n");
1074 static void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1077 printf("Parsing RETRIEVE_REJECT Msg\n");
1083 static msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1089 retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN));
1092 printf("Building RETRIEVE_REJECT Msg\n");
1097 static void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1099 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1100 DISCONNECT_t *disconnect = (DISCONNECT_t *) (msg->data + HEADER_LEN);
1103 dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &cause, nt,bc);
1104 if (cause>0) bc->cause=cause;
1106 dec_ie_facility(disconnect->FACILITY, (Q931_info_t *) disconnect, &bc->fac_in, nt, bc);
1108 dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc);
1110 printf("Parsing DISCONNECT Msg\n");
1116 static msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1122 disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN));
1124 enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc);
1126 enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt ? 1 : 5, 8, nt, bc);
1129 if (bc->fac_out.Function != Fac_None) {
1130 enc_ie_facility(&disconnect->FACILITY, msg, &bc->fac_out, nt);
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);
1140 printf("Building DISCONNECT Msg\n");
1145 static void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1147 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1148 RESTART_t *restart = (RESTART_t *) (msg->data + HEADER_LEN);
1150 struct misdn_stack *stack=get_stack_by_bc(bc);
1153 printf("Parsing RESTART Msg\n");
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);
1164 static msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1166 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1168 msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART, bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt);
1170 restart=(RESTART_t*)((msg->data+HEADER_LEN));
1173 printf("Building RESTART Msg\n");
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);
1180 enc_ie_restart_ind(&restart->RESTART_IND, msg, 0x87, nt, bc);
1183 cb_log(0,bc->port, "Restarting channel %d\n", bc->channel);
1187 static void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1189 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1190 RELEASE_t *release = (RELEASE_t *) (msg->data + HEADER_LEN);
1194 dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &cause, nt,bc);
1195 if (cause>0) bc->cause=cause;
1197 dec_ie_facility(release->FACILITY, (Q931_info_t *) release, &bc->fac_in, nt, bc);
1200 printf("Parsing RELEASE Msg\n");
1206 static msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1208 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1210 msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt);
1212 release=(RELEASE_t*)((msg->data+HEADER_LEN));
1214 if (bc->out_cause>= 0)
1215 enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1217 if (bc->fac_out.Function != Fac_None) {
1218 enc_ie_facility(&release->FACILITY, msg, &bc->fac_out, nt);
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);
1228 printf("Building RELEASE Msg\n");
1233 static void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1235 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1236 RELEASE_COMPLETE_t *release_complete = (RELEASE_COMPLETE_t *) (msg->data + HEADER_LEN);
1239 iframe_t *frm = (iframe_t*) msg->data;
1241 struct misdn_stack *stack=get_stack_by_bc(bc);
1242 mISDNuser_head_t *hh;
1243 hh=(mISDNuser_head_t*)msg->data;
1245 /*hh=(mISDN_head_t*)msg->data;
1249 if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1250 cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] \n");
1254 if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
1255 cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] \n");
1259 dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &cause, nt,bc);
1260 if (cause>0) bc->cause=cause;
1262 dec_ie_facility(release_complete->FACILITY, (Q931_info_t *) release_complete, &bc->fac_in, nt, bc);
1265 printf("Parsing RELEASE_COMPLETE Msg\n");
1269 static msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1275 release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN));
1277 enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc);
1279 if (bc->fac_out.Function != Fac_None) {
1280 enc_ie_facility(&release_complete->FACILITY, msg, &bc->fac_out, nt);
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);
1290 printf("Building RELEASE_COMPLETE Msg\n");
1295 static void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
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;
1306 char number[sizeof(bc->redirecting.to.number)];
1307 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1310 printf("Parsing FACILITY Msg\n");
1313 bc->fac_in.Function = Fac_None;
1316 if (qi->QI_ELEMENT(facility))
1317 p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->QI_ELEMENT(facility) + 1;
1319 p = facility->FACILITY;
1324 if (decodeFac(p, &bc->fac_in)) {
1325 cb_log(3, bc->port, "Decoding facility ie failed! Unrecognized facility message?\n");
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;
1333 bc->notify_description_code = description_code;
1336 dec_ie_redir_dn(facility->REDIR_DN, qi, &type, &plan, &present, number, sizeof(number), nt, bc);
1338 bc->redirecting.to_changed = 1;
1340 bc->redirecting.to.number_type = type;
1341 bc->redirecting.to.number_plan = plan;
1345 bc->redirecting.to.presentation = 0; /* presentation allowed */
1348 bc->redirecting.to.presentation = 1; /* presentation restricted */
1351 bc->redirecting.to.presentation = 2; /* Number not available */
1354 bc->redirecting.to.screening = 0; /* Unscreened */
1355 strcpy(bc->redirecting.to.number, number);
1357 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1360 static msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1364 unsigned char *ie_fac;
1365 unsigned char fac_tmp[256];
1367 FACILITY_t *facility;
1371 printf("Building FACILITY Msg\n");
1374 len = encodeFac(fac_tmp, &(bc->fac_out));
1377 * mISDN does not know how to build the requested facility structure
1378 * Clear facility information
1380 bc->fac_out.Function = Fac_None;
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) */
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);
1394 ie_fac = msg_put(msg, len);
1396 facility->FACILITY = ie_fac + 1;
1398 qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
1399 qi->QI_ELEMENT(facility) = ie_fac - (unsigned char *)qi - sizeof(Q931_info_t);
1402 memcpy(ie_fac, fac_tmp, len);
1404 /* Clear facility information */
1405 bc->fac_out.Function = Fac_None;
1409 printf("Sending %s as Display\n", bc->display);
1411 enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc);
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;
1420 if (bc->redirecting.to_changed) {
1421 bc->redirecting.to_changed = 0;
1422 switch (bc->outgoing_colp) {
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);
1433 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1438 #if defined(AST_MISDN_ENHANCEMENTS)
1441 * \brief Parse a received REGISTER message
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.
1450 static void parse_register(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1455 HEADER_LEN = nt ? mISDNUSER_HEAD_SIZE : mISDN_HEADER_LEN;
1456 reg = (REGISTER_t *) (msg->data + HEADER_LEN);
1459 * A facility ie is optional.
1460 * The peer may just be establishing a connection to send
1463 dec_ie_facility(reg->FACILITY, (Q931_info_t *) reg, &bc->fac_in, nt, bc);
1465 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1467 #if defined(AST_MISDN_ENHANCEMENTS)
1470 * \brief Construct a REGISTER message
1472 * \param msgs Search table entry that called us.
1473 * \param bc Associated B channel
1474 * \param nt TRUE if in NT mode.
1476 * \return Allocated built message
1478 static msg_t *build_register(struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1488 if (bc->fac_out.Function != Fac_None) {
1489 enc_ie_facility(®->FACILITY, msg, &bc->fac_out, nt);
1494 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1496 static void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
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;
1504 char number[sizeof(bc->redirecting.to.number)];
1507 printf("Parsing NOTIFY Msg\n");
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;
1514 bc->notify_description_code = description_code;
1517 dec_ie_redir_dn(notify->REDIR_DN, (Q931_info_t *) notify, &type, &plan, &present, number, sizeof(number), nt, bc);
1519 bc->redirecting.to_changed = 1;
1521 bc->redirecting.to.number_type = type;
1522 bc->redirecting.to.number_plan = plan;
1526 bc->redirecting.to.presentation = 0; /* presentation allowed */
1529 bc->redirecting.to.presentation = 1; /* presentation restricted */
1532 bc->redirecting.to.presentation = 2; /* Number not available */
1535 bc->redirecting.to.screening = 0; /* Unscreened */
1536 strcpy(bc->redirecting.to.number, number);
1540 static msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1542 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1544 msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt);
1547 printf("Building NOTIFY Msg\n");
1550 notify = (NOTIFY_t *) (msg->data + HEADER_LEN);
1552 enc_ie_notify(¬ify->NOTIFY, msg, bc->notify_description_code, nt, bc);
1553 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
1555 if (bc->redirecting.to_changed) {
1556 bc->redirecting.to_changed = 0;
1557 switch (bc->outgoing_colp) {
1559 case 1:/* restricted */
1560 enc_ie_redir_dn(¬ify->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);
1571 static void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1574 printf("Parsing STATUS_ENQUIRY Msg\n");
1578 static msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1584 status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN));
1587 printf("Building STATUS_ENQUIRY Msg\n");
1592 static void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1594 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1595 INFORMATION_t *information = (INFORMATION_t *) (msg->data + HEADER_LEN);
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);
1602 printf("Parsing INFORMATION Msg\n");
1606 static msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
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);
1612 information=(INFORMATION_t*)((msg->data+HEADER_LEN));
1614 enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc);
1619 printf("Sending %s as Display\n", bc->display);
1621 enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc);
1626 printf("Building INFORMATION Msg\n");
1631 static void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1633 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1634 STATUS_t *status = (STATUS_t *) (msg->data + HEADER_LEN);
1638 dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &cause, nt,bc);
1639 if (cause>0) bc->cause=cause;
1642 printf("Parsing STATUS Msg\n");
1646 static msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1648 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1650 msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS, bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1652 status=(STATUS_t*)((msg->data+HEADER_LEN));
1655 printf("Building STATUS Msg\n");
1660 static void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1663 printf("Parsing STATUS Msg\n");
1667 static msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
1669 int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
1671 msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS, bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt);
1673 status=(STATUS_t*)((msg->data+HEADER_LEN));
1676 printf("Building STATUS Msg\n");
1682 /************************************/
1689 struct isdn_msg msgs_g[] = {
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 }
1729 #define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg))
1731 /** INTERFACE FCTS ***/
1732 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt)
1737 mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data;
1739 for (i=0; i< msgs_max -1; i++) {
1740 if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1744 iframe_t *frm = (iframe_t*)msg->data;
1746 for (i=0; i< msgs_max -1; i++)
1747 if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i;
1753 int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt)
1756 for (i=0; i< msgs_max; i++)
1757 if ( event == msgs[i].event) return i;
1759 cb_log(10,0, "get_index: event not found!\n");
1764 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt)
1766 int i=isdn_msg_get_index(msgs, msg, nt);
1767 if(i>=0) return msgs[i].event;
1768 return EVENT_UNKNOWN;
1771 char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt)
1773 int i=isdn_msg_get_index(msgs, msg, nt);
1774 if(i>=0) return msgs[i].info;
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";
1790 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt)
1792 int i=isdn_msg_get_index_by_event(msgs, event, nt);
1794 if(i>=0) return msgs[i].info;
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;
1810 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt)
1812 int i=isdn_msg_get_index(msgs, msg, nt);
1815 msgs[i].msg_parser(msgs, msg, bc, nt);
1819 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt)
1821 int i=isdn_msg_get_index_by_event(msgs, event, nt);
1822 if(i<0) return NULL;
1824 return msgs[i].msg_builder(msgs, bc, nt);