2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2009, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief PRI signaling module
23 * \author Matthew Fredrickson <creslin@digium.com>
27 <support_level>core</support_level>
38 #include "asterisk/utils.h"
39 #include "asterisk/options.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/app.h"
42 #include "asterisk/file.h"
43 #include "asterisk/callerid.h"
44 #include "asterisk/say.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/astdb.h"
47 #include "asterisk/causes.h"
48 #include "asterisk/musiconhold.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/transcap.h"
51 #include "asterisk/features.h"
52 #include "asterisk/aoc.h"
55 #ifndef PRI_EVENT_FACILITY
56 #error "Upgrade your libpri"
63 /* define this to send PRI user-user information elements */
64 #undef SUPPORT_USERUSER
67 * Define to make always pick a channel if allowed. Useful for
68 * testing channel shifting.
70 //#define ALWAYS_PICK_CHANNEL 1
73 * Define to force a RESTART on a channel that returns a cause
74 * code of PRI_CAUSE_REQUESTED_CHAN_UNAVAIL(44). If the cause
75 * is because of a stuck channel on the peer and the channel is
76 * always the next channel we pick for an outgoing call then
79 #define FORCE_RESTART_UNAVAIL_CHANS 1
81 #if defined(HAVE_PRI_CCSS)
82 struct sig_pri_cc_agent_prv {
83 /*! Asterisk span D channel control structure. */
84 struct sig_pri_span *pri;
85 /*! CC id value to use with libpri. -1 if invalid. */
87 /*! TRUE if CC has been requested and we are waiting for the response. */
88 unsigned char cc_request_response_pending;
91 struct sig_pri_cc_monitor_instance {
92 /*! \brief Asterisk span D channel control structure. */
93 struct sig_pri_span *pri;
94 /*! CC id value to use with libpri. (-1 if already canceled). */
96 /*! CC core id value. */
98 /*! Device name(Channel name less sequence number) */
102 /*! Upper level agent/monitor type name. */
103 static const char *sig_pri_cc_type_name;
104 /*! Container of sig_pri monitor instances. */
105 static struct ao2_container *sig_pri_cc_monitors;
106 #endif /* defined(HAVE_PRI_CCSS) */
108 static int pri_matchdigittimeout = 3000;
110 static int pri_gendigittimeout = 8000;
112 #define DCHAN_NOTINALARM (1 << 0)
113 #define DCHAN_UP (1 << 1)
115 /* Defines to help decode the encoded event channel id. */
116 #define PRI_CHANNEL(p) ((p) & 0xff)
117 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
118 #define PRI_EXPLICIT (1 << 16)
119 #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
120 #define PRI_HELD_CALL (1 << 18)
123 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
125 static int pri_active_dchan_index(struct sig_pri_span *pri);
127 static const char *sig_pri_call_level2str(enum sig_pri_call_level level)
130 case SIG_PRI_CALL_LEVEL_IDLE:
132 case SIG_PRI_CALL_LEVEL_SETUP:
134 case SIG_PRI_CALL_LEVEL_OVERLAP:
136 case SIG_PRI_CALL_LEVEL_PROCEEDING:
138 case SIG_PRI_CALL_LEVEL_ALERTING:
140 case SIG_PRI_CALL_LEVEL_DEFER_DIAL:
142 case SIG_PRI_CALL_LEVEL_CONNECT:
148 static inline void pri_rel(struct sig_pri_span *pri)
150 ast_mutex_unlock(&pri->lock);
153 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
155 int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
156 ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
157 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
162 static void sig_pri_handle_dchan_exception(struct sig_pri_span *pri, int index)
164 if (sig_pri_callbacks.handle_dchan_exception) {
165 sig_pri_callbacks.handle_dchan_exception(pri, index);
169 static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
171 if (sig_pri_callbacks.set_dialing) {
172 sig_pri_callbacks.set_dialing(p->chan_pvt, is_dialing);
176 static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
178 p->digital = is_digital;
179 if (sig_pri_callbacks.set_digital) {
180 sig_pri_callbacks.set_digital(p->chan_pvt, is_digital);
184 static void sig_pri_set_outgoing(struct sig_pri_chan *p, int is_outgoing)
186 p->outgoing = is_outgoing;
187 if (sig_pri_callbacks.set_outgoing) {
188 sig_pri_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
192 void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
194 if (sig_pri_is_alarm_ignored(p->pri)) {
195 /* Always set not in alarm */
200 * Clear the channel restart state when the channel alarm
201 * changes to prevent the state from getting stuck when the link
204 p->resetting = SIG_PRI_RESET_IDLE;
206 p->inalarm = in_alarm;
207 if (sig_pri_callbacks.set_alarm) {
208 sig_pri_callbacks.set_alarm(p->chan_pvt, in_alarm);
212 static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
214 if (sig_pri_callbacks.get_orig_dialstring) {
215 return sig_pri_callbacks.get_orig_dialstring(p->chan_pvt);
217 ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
221 #if defined(HAVE_PRI_CCSS)
222 static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
224 if (sig_pri_callbacks.make_cc_dialstring) {
225 sig_pri_callbacks.make_cc_dialstring(p->chan_pvt, buf, buf_size);
227 ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
231 #endif /* defined(HAVE_PRI_CCSS) */
233 static void sig_pri_dial_digits(struct sig_pri_chan *p, const char *dial_string)
235 if (sig_pri_callbacks.dial_digits) {
236 sig_pri_callbacks.dial_digits(p->chan_pvt, dial_string);
242 * \brief Reevaluate the PRI span device state.
245 * \param pri PRI span control structure.
249 * \note Assumes the pri->lock is already obtained.
251 static void sig_pri_span_devstate_changed(struct sig_pri_span *pri)
253 if (sig_pri_callbacks.update_span_devstate) {
254 sig_pri_callbacks.update_span_devstate(pri);
260 * \brief Set the caller id information in the parent module.
263 * \param p sig_pri channel structure.
267 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
269 struct ast_party_caller caller;
271 if (sig_pri_callbacks.set_callerid) {
272 ast_party_caller_init(&caller);
274 caller.id.name.str = p->cid_name;
275 caller.id.name.presentation = p->callingpres;
276 caller.id.name.valid = 1;
278 caller.id.number.str = p->cid_num;
279 caller.id.number.plan = p->cid_ton;
280 caller.id.number.presentation = p->callingpres;
281 caller.id.number.valid = 1;
283 if (!ast_strlen_zero(p->cid_subaddr)) {
284 caller.id.subaddress.valid = 1;
285 //caller.id.subaddress.type = 0;/* nsap */
286 //caller.id.subaddress.odd_even_indicator = 0;
287 caller.id.subaddress.str = p->cid_subaddr;
289 caller.id.tag = p->user_tag;
291 caller.ani.number.str = p->cid_ani;
292 //caller.ani.number.plan = p->xxx;
293 //caller.ani.number.presentation = p->xxx;
294 caller.ani.number.valid = 1;
296 caller.ani2 = p->cid_ani2;
297 sig_pri_callbacks.set_callerid(p->chan_pvt, &caller);
303 * \brief Set the Dialed Number Identifier.
306 * \param p sig_pri channel structure.
307 * \param dnid Dialed Number Identifier string.
311 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
313 if (sig_pri_callbacks.set_dnid) {
314 sig_pri_callbacks.set_dnid(p->chan_pvt, dnid);
320 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
323 * \param p sig_pri channel structure.
324 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
328 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
330 if (sig_pri_callbacks.set_rdnis) {
331 sig_pri_callbacks.set_rdnis(p->chan_pvt, rdnis);
335 static void sig_pri_unlock_private(struct sig_pri_chan *p)
337 if (sig_pri_callbacks.unlock_private) {
338 sig_pri_callbacks.unlock_private(p->chan_pvt);
342 static void sig_pri_lock_private(struct sig_pri_chan *p)
344 if (sig_pri_callbacks.lock_private) {
345 sig_pri_callbacks.lock_private(p->chan_pvt);
349 static void sig_pri_deadlock_avoidance_private(struct sig_pri_chan *p)
351 if (sig_pri_callbacks.deadlock_avoidance_private) {
352 sig_pri_callbacks.deadlock_avoidance_private(p->chan_pvt);
354 /* Fallback to the old way if callback not present. */
355 sig_pri_unlock_private(p);
357 sig_pri_lock_private(p);
361 static void pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
363 /* Grab the lock first */
364 while (ast_mutex_trylock(&pri->lock)) {
366 sig_pri_deadlock_avoidance_private(p);
368 /* Then break the poll */
369 if (pri->master != AST_PTHREADT_NULL) {
370 pthread_kill(pri->master, SIGURG);
376 * \brief Convert PRI redirecting reason to asterisk version.
379 * \param pri_reason PRI redirecting reason.
381 * \return Equivalent asterisk redirecting reason value.
383 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
385 enum AST_REDIRECTING_REASON ast_reason;
387 switch (pri_reason) {
388 case PRI_REDIR_FORWARD_ON_BUSY:
389 ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
391 case PRI_REDIR_FORWARD_ON_NO_REPLY:
392 ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
394 case PRI_REDIR_DEFLECTION:
395 ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
397 case PRI_REDIR_UNCONDITIONAL:
398 ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
400 case PRI_REDIR_UNKNOWN:
402 ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
411 * \brief Convert asterisk redirecting reason to PRI version.
414 * \param ast_reason Asterisk redirecting reason.
416 * \return Equivalent PRI redirecting reason value.
418 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
422 switch (ast_reason) {
423 case AST_REDIRECTING_REASON_USER_BUSY:
424 pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
426 case AST_REDIRECTING_REASON_NO_ANSWER:
427 pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
429 case AST_REDIRECTING_REASON_UNCONDITIONAL:
430 pri_reason = PRI_REDIR_UNCONDITIONAL;
432 case AST_REDIRECTING_REASON_DEFLECTION:
433 pri_reason = PRI_REDIR_DEFLECTION;
435 case AST_REDIRECTING_REASON_UNKNOWN:
437 pri_reason = PRI_REDIR_UNKNOWN;
446 * \brief Convert PRI number presentation to asterisk version.
449 * \param pri_presentation PRI number presentation.
451 * \return Equivalent asterisk number presentation value.
453 static int pri_to_ast_presentation(int pri_presentation)
455 int ast_presentation;
457 switch (pri_presentation) {
458 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED:
459 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED;
461 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
462 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
464 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
465 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
467 case PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER:
468 ast_presentation = AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER;
471 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED:
472 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
474 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
475 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
477 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
478 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
480 case PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER:
481 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER;
484 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED:
485 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
486 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
487 case PRI_PRES_UNAVAILABLE | PRI_PRES_NETWORK_NUMBER:
488 ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
492 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
496 return ast_presentation;
501 * \brief Convert asterisk number presentation to PRI version.
504 * \param ast_presentation Asterisk number presentation.
506 * \return Equivalent PRI number presentation value.
508 static int ast_to_pri_presentation(int ast_presentation)
510 int pri_presentation;
512 switch (ast_presentation) {
513 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED:
514 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
516 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
517 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
519 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
520 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
522 case AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER:
523 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER;
526 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED:
527 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
529 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
530 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
532 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
533 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
535 case AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER:
536 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER;
539 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_UNSCREENED:
540 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_PASSED_SCREEN:
541 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_FAILED_SCREEN:
542 case AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER:
543 pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
547 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
551 return pri_presentation;
556 * \brief Convert PRI name char_set to asterisk version.
559 * \param pri_char_set PRI name char_set.
561 * \return Equivalent asterisk name char_set value.
563 static enum AST_PARTY_CHAR_SET pri_to_ast_char_set(int pri_char_set)
565 enum AST_PARTY_CHAR_SET ast_char_set;
567 switch (pri_char_set) {
569 case PRI_CHAR_SET_UNKNOWN:
570 ast_char_set = AST_PARTY_CHAR_SET_UNKNOWN;
572 case PRI_CHAR_SET_ISO8859_1:
573 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_1;
575 case PRI_CHAR_SET_WITHDRAWN:
576 ast_char_set = AST_PARTY_CHAR_SET_WITHDRAWN;
578 case PRI_CHAR_SET_ISO8859_2:
579 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_2;
581 case PRI_CHAR_SET_ISO8859_3:
582 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_3;
584 case PRI_CHAR_SET_ISO8859_4:
585 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_4;
587 case PRI_CHAR_SET_ISO8859_5:
588 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_5;
590 case PRI_CHAR_SET_ISO8859_7:
591 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_7;
593 case PRI_CHAR_SET_ISO10646_BMPSTRING:
594 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_BMPSTRING;
596 case PRI_CHAR_SET_ISO10646_UTF_8STRING:
597 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING;
606 * \brief Convert asterisk name char_set to PRI version.
609 * \param ast_char_set Asterisk name char_set.
611 * \return Equivalent PRI name char_set value.
613 static int ast_to_pri_char_set(enum AST_PARTY_CHAR_SET ast_char_set)
617 switch (ast_char_set) {
619 case AST_PARTY_CHAR_SET_UNKNOWN:
620 pri_char_set = PRI_CHAR_SET_UNKNOWN;
622 case AST_PARTY_CHAR_SET_ISO8859_1:
623 pri_char_set = PRI_CHAR_SET_ISO8859_1;
625 case AST_PARTY_CHAR_SET_WITHDRAWN:
626 pri_char_set = PRI_CHAR_SET_WITHDRAWN;
628 case AST_PARTY_CHAR_SET_ISO8859_2:
629 pri_char_set = PRI_CHAR_SET_ISO8859_2;
631 case AST_PARTY_CHAR_SET_ISO8859_3:
632 pri_char_set = PRI_CHAR_SET_ISO8859_3;
634 case AST_PARTY_CHAR_SET_ISO8859_4:
635 pri_char_set = PRI_CHAR_SET_ISO8859_4;
637 case AST_PARTY_CHAR_SET_ISO8859_5:
638 pri_char_set = PRI_CHAR_SET_ISO8859_5;
640 case AST_PARTY_CHAR_SET_ISO8859_7:
641 pri_char_set = PRI_CHAR_SET_ISO8859_7;
643 case AST_PARTY_CHAR_SET_ISO10646_BMPSTRING:
644 pri_char_set = PRI_CHAR_SET_ISO10646_BMPSTRING;
646 case AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING:
647 pri_char_set = PRI_CHAR_SET_ISO10646_UTF_8STRING;
654 #if defined(HAVE_PRI_SUBADDR)
657 * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
660 * \param ast_subaddress Asterisk party subaddress structure.
661 * \param pri_subaddress PRI party subaddress structure.
666 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
671 if (ast_subaddress->str) {
672 ast_free(ast_subaddress->str);
674 if (pri_subaddress->length <= 0) {
675 ast_party_subaddress_init(ast_subaddress);
679 if (!pri_subaddress->type) {
681 ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
684 if (!(cnum = ast_malloc(2 * pri_subaddress->length + 1))) {
685 ast_party_subaddress_init(ast_subaddress);
690 len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
691 for (x = 0; x < len; ++x) {
692 ptr += sprintf(ptr, "%02x", pri_subaddress->data[x]);
695 if (pri_subaddress->odd_even_indicator) {
697 sprintf(ptr, "%01x", (pri_subaddress->data[len]) >> 4);
700 sprintf(ptr, "%02x", pri_subaddress->data[len]);
702 ast_subaddress->str = cnum;
704 ast_subaddress->type = pri_subaddress->type;
705 ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
706 ast_subaddress->valid = 1;
708 #endif /* defined(HAVE_PRI_SUBADDR) */
710 #if defined(HAVE_PRI_SUBADDR)
711 static unsigned char ast_pri_pack_hex_char(char c)
717 } else if (c < ('9' + 1)) {
719 } else if (c < 'A') {
721 } else if (c < ('F' + 1)) {
723 } else if (c < 'a') {
725 } else if (c < ('f' + 1)) {
732 #endif /* defined(HAVE_PRI_SUBADDR) */
734 #if defined(HAVE_PRI_SUBADDR)
737 * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
738 * \details left justified, with 0 padding if odd length.
741 * \param dst pointer to packed byte array.
742 * \param src pointer to null terminated hexadecimal string.
743 * \param maxlen destination array size.
745 * \return Length of byte array
747 * \note The dst is not an ASCIIz string.
748 * \note The src is an ASCIIz hex string.
750 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
753 int len = strlen(src);
755 if (len > (2 * maxlen)) {
759 res = len / 2 + len % 2;
762 *dst = ast_pri_pack_hex_char(*src) << 4;
764 *dst |= ast_pri_pack_hex_char(*src);
768 if (len) { /* 1 left */
769 *dst = ast_pri_pack_hex_char(*src) << 4;
773 #endif /* defined(HAVE_PRI_SUBADDR) */
775 #if defined(HAVE_PRI_SUBADDR)
778 * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
781 * \param pri_subaddress PRI party subaddress structure.
782 * \param ast_subaddress Asterisk party subaddress structure.
786 * \note Assumes that pri_subaddress has been previously memset to zero.
788 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
790 if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
791 pri_subaddress->type = ast_subaddress->type;
792 if (!ast_subaddress->type) {
794 ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
795 sizeof(pri_subaddress->data));
796 pri_subaddress->length = strlen((char *) pri_subaddress->data);
797 pri_subaddress->odd_even_indicator = 0;
798 pri_subaddress->valid = 1;
800 /* 2 = User Specified */
802 * Copy HexString to packed HexData,
803 * if odd length then right pad trailing byte with 0
805 int length = ast_pri_pack_hex_string(pri_subaddress->data,
806 ast_subaddress->str, sizeof(pri_subaddress->data));
808 pri_subaddress->length = length; /* packed data length */
810 length = strlen(ast_subaddress->str);
811 if (length > 2 * sizeof(pri_subaddress->data)) {
812 pri_subaddress->odd_even_indicator = 0;
814 pri_subaddress->odd_even_indicator = (length & 1);
816 pri_subaddress->valid = 1;
820 #endif /* defined(HAVE_PRI_SUBADDR) */
824 * \brief Fill in the PRI party name from the given asterisk party name.
827 * \param pri_name PRI party name structure.
828 * \param ast_name Asterisk party name structure.
832 * \note Assumes that pri_name has been previously memset to zero.
834 static void sig_pri_party_name_from_ast(struct pri_party_name *pri_name, const struct ast_party_name *ast_name)
836 if (!ast_name->valid) {
840 pri_name->presentation = ast_to_pri_presentation(ast_name->presentation);
841 pri_name->char_set = ast_to_pri_char_set(ast_name->char_set);
842 if (!ast_strlen_zero(ast_name->str)) {
843 ast_copy_string(pri_name->str, ast_name->str, sizeof(pri_name->str));
849 * \brief Fill in the PRI party number from the given asterisk party number.
852 * \param pri_number PRI party number structure.
853 * \param ast_number Asterisk party number structure.
857 * \note Assumes that pri_number has been previously memset to zero.
859 static void sig_pri_party_number_from_ast(struct pri_party_number *pri_number, const struct ast_party_number *ast_number)
861 if (!ast_number->valid) {
864 pri_number->valid = 1;
865 pri_number->presentation = ast_to_pri_presentation(ast_number->presentation);
866 pri_number->plan = ast_number->plan;
867 if (!ast_strlen_zero(ast_number->str)) {
868 ast_copy_string(pri_number->str, ast_number->str, sizeof(pri_number->str));
874 * \brief Fill in the PRI party id from the given asterisk party id.
877 * \param pri_id PRI party id structure.
878 * \param ast_id Asterisk party id structure.
882 * \note Assumes that pri_id has been previously memset to zero.
884 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
886 sig_pri_party_name_from_ast(&pri_id->name, &ast_id->name);
887 sig_pri_party_number_from_ast(&pri_id->number, &ast_id->number);
888 #if defined(HAVE_PRI_SUBADDR)
889 sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
890 #endif /* defined(HAVE_PRI_SUBADDR) */
895 * \brief Update the PRI redirecting information for the current call.
898 * \param pvt sig_pri private channel structure.
899 * \param ast Asterisk channel
903 * \note Assumes that the PRI lock is already obtained.
905 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
907 struct pri_party_redirecting pri_redirecting;
908 const struct ast_party_redirecting *ast_redirecting;
909 struct ast_party_id redirecting_from = ast_channel_redirecting_effective_from(ast);
910 struct ast_party_id redirecting_to = ast_channel_redirecting_effective_to(ast);
911 struct ast_party_id redirecting_orig = ast_channel_redirecting_effective_orig(ast);
913 memset(&pri_redirecting, 0, sizeof(pri_redirecting));
914 ast_redirecting = ast_channel_redirecting(ast);
915 sig_pri_party_id_from_ast(&pri_redirecting.from, &redirecting_from);
916 sig_pri_party_id_from_ast(&pri_redirecting.to, &redirecting_to);
917 sig_pri_party_id_from_ast(&pri_redirecting.orig_called, &redirecting_orig);
918 pri_redirecting.count = ast_redirecting->count;
919 pri_redirecting.orig_reason = ast_to_pri_reason(ast_redirecting->orig_reason.code);
920 pri_redirecting.reason = ast_to_pri_reason(ast_redirecting->reason.code);
922 pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
927 * \brief Reset DTMF detector.
930 * \param p sig_pri channel structure.
934 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
936 if (sig_pri_callbacks.dsp_reset_and_flush_digits) {
937 sig_pri_callbacks.dsp_reset_and_flush_digits(p->chan_pvt);
941 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
943 if (sig_pri_callbacks.set_echocanceller) {
944 return sig_pri_callbacks.set_echocanceller(p->chan_pvt, enable);
950 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
952 if (sig_pri_callbacks.fixup_chans) {
953 sig_pri_callbacks.fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
957 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
959 if (sig_pri_callbacks.play_tone) {
960 return sig_pri_callbacks.play_tone(p->chan_pvt, tone);
966 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
968 struct ast_channel *c;
970 if (sig_pri_callbacks.new_ast_channel) {
971 c = sig_pri_callbacks.new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
982 p->alreadyhungup = 0;
983 ast_channel_transfercapability_set(c, transfercapability);
984 pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY",
985 ast_transfercapability2str(transfercapability));
986 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
987 sig_pri_set_digital(p, 1);
990 ast_mutex_lock(&p->pri->lock);
991 sig_pri_span_devstate_changed(p->pri);
992 ast_mutex_unlock(&p->pri->lock);
1000 * \brief Open the PRI channel media path.
1003 * \param p Channel private control structure.
1007 static void sig_pri_open_media(struct sig_pri_chan *p)
1009 if (p->no_b_channel) {
1013 if (sig_pri_callbacks.open_media) {
1014 sig_pri_callbacks.open_media(p->chan_pvt);
1020 * \brief Post an AMI B channel association event.
1023 * \param p Channel private control structure.
1025 * \note Assumes the private and owner are locked.
1029 static void sig_pri_ami_channel_event(struct sig_pri_chan *p)
1031 if (sig_pri_callbacks.ami_channel_event) {
1032 sig_pri_callbacks.ami_channel_event(p->chan_pvt, p->owner);
1036 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability)
1038 struct ast_channel *ast;
1040 ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1042 sig_pri_set_outgoing(p, 1);
1043 ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1045 sig_pri_set_outgoing(p, 0);
1050 int pri_is_up(struct sig_pri_span *pri)
1053 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1054 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1060 static const char *pri_order(int level)
1070 return "Quaternary";
1076 /* Returns index of the active dchan */
1077 static int pri_active_dchan_index(struct sig_pri_span *pri)
1081 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1082 if ((pri->dchans[x] == pri->pri))
1086 ast_log(LOG_WARNING, "No active dchan found!\n");
1090 static void pri_find_dchan(struct sig_pri_span *pri)
1098 for (idx = 0; idx < SIG_PRI_NUM_DCHANS; ++idx) {
1099 if (!pri->dchans[idx]) {
1100 /* No more D channels defined on the span. */
1103 if (pri->dchans[idx] == old) {
1106 if (newslot < 0 && pri->dchanavail[idx] == DCHAN_AVAILABLE) {
1110 /* At this point, idx is a count of how many D-channels are defined on the span. */
1113 /* We have several D-channels defined on the span. (NFAS PRI setup) */
1115 /* No D-channels available. Default to the primary D-channel. */
1118 if (!pri->no_d_channels) {
1119 pri->no_d_channels = 1;
1120 if (old && oldslot != newslot) {
1121 ast_log(LOG_WARNING,
1122 "Span %d: No D-channels up! Switching selected D-channel from %s to %s.\n",
1123 pri->span, pri_order(oldslot), pri_order(newslot));
1125 ast_log(LOG_WARNING, "Span %d: No D-channels up!\n", pri->span);
1129 pri->no_d_channels = 0;
1131 if (old && oldslot != newslot) {
1133 "Switching selected D-channel from %s (fd %d) to %s (fd %d)!\n",
1134 pri_order(oldslot), pri->fds[oldslot],
1135 pri_order(newslot), pri->fds[newslot]);
1139 /* The only D-channel is not up. */
1142 if (!pri->no_d_channels) {
1143 pri->no_d_channels = 1;
1146 * This is annoying to see on non-persistent layer 2
1147 * connections. Let's not complain in that case.
1149 if (pri->sig != SIG_BRI_PTMP) {
1150 ast_log(LOG_WARNING, "Span %d: D-channel is down!\n", pri->span);
1154 pri->no_d_channels = 0;
1157 pri->pri = pri->dchans[newslot];
1162 * \brief Determine if a private channel structure is in use.
1165 * \param pvt Channel to determine if in use.
1167 * \return TRUE if the channel is in use.
1169 static int sig_pri_is_chan_in_use(struct sig_pri_chan *pvt)
1171 return pvt->owner || pvt->call || pvt->allocated || pvt->inalarm
1172 || pvt->resetting != SIG_PRI_RESET_IDLE;
1176 * \brief Determine if a private channel structure is available.
1179 * \param pvt Channel to determine if available.
1181 * \return TRUE if the channel is available.
1183 int sig_pri_is_chan_available(struct sig_pri_chan *pvt)
1185 return !sig_pri_is_chan_in_use(pvt)
1186 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1187 /* And not out-of-service */
1188 && !pvt->service_status
1189 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1195 * \brief Obtain the sig_pri owner channel lock if the owner exists.
1198 * \param pri PRI span control structure.
1199 * \param chanpos Channel position in the span.
1201 * \note Assumes the pri->lock is already obtained.
1202 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1206 static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
1209 if (!pri->pvts[chanpos]->owner) {
1210 /* There is no owner lock to get. */
1213 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
1214 /* We got the lock */
1218 /* Avoid deadlock */
1219 sig_pri_unlock_private(pri->pvts[chanpos]);
1220 DEADLOCK_AVOIDANCE(&pri->lock);
1221 sig_pri_lock_private(pri->pvts[chanpos]);
1227 * \brief Queue the given frame onto the owner channel.
1230 * \param pri PRI span control structure.
1231 * \param chanpos Channel position in the span.
1232 * \param frame Frame to queue onto the owner channel.
1234 * \note Assumes the pri->lock is already obtained.
1235 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1239 static void pri_queue_frame(struct sig_pri_span *pri, int chanpos, struct ast_frame *frame)
1241 sig_pri_lock_owner(pri, chanpos);
1242 if (pri->pvts[chanpos]->owner) {
1243 ast_queue_frame(pri->pvts[chanpos]->owner, frame);
1244 ast_channel_unlock(pri->pvts[chanpos]->owner);
1250 * \brief Queue a control frame of the specified subclass onto the owner channel.
1253 * \param pri PRI span control structure.
1254 * \param chanpos Channel position in the span.
1255 * \param subclass Control frame subclass to queue onto the owner channel.
1257 * \note Assumes the pri->lock is already obtained.
1258 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1262 static void pri_queue_control(struct sig_pri_span *pri, int chanpos, int subclass)
1264 struct ast_frame f = {AST_FRAME_CONTROL, };
1265 struct sig_pri_chan *p = pri->pvts[chanpos];
1267 if (sig_pri_callbacks.queue_control) {
1268 sig_pri_callbacks.queue_control(p->chan_pvt, subclass);
1271 f.subclass.integer = subclass;
1272 pri_queue_frame(pri, chanpos, &f);
1277 * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
1280 * \param pri PRI span control structure.
1281 * \param chanpos Channel position in the span.
1282 * \param cause String describing the cause to be placed into the frame.
1284 * \note Assumes the pri->lock is already obtained.
1285 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1289 static void pri_queue_pvt_cause_data(struct sig_pri_span *pri, int chanpos, const char *cause, int ast_cause)
1291 struct ast_channel *chan;
1292 struct ast_control_pvt_cause_code *cause_code;
1294 sig_pri_lock_owner(pri, chanpos);
1295 chan = pri->pvts[chanpos]->owner;
1297 int datalen = sizeof(*cause_code) + strlen(cause);
1298 cause_code = ast_alloca(datalen);
1299 cause_code->ast_cause = ast_cause;
1300 ast_copy_string(cause_code->chan_name, ast_channel_name(chan), AST_CHANNEL_NAME);
1301 ast_copy_string(cause_code->code, cause, datalen + 1 - sizeof(*cause_code));
1302 ast_queue_control_data(chan, AST_CONTROL_PVT_CAUSE_CODE, cause_code, datalen);
1303 ast_channel_hangupcause_hash_set(chan, cause_code, datalen);
1304 ast_channel_unlock(chan);
1310 * \brief Find the channel associated with the libpri call.
1313 * \param pri PRI span control structure.
1314 * \param call LibPRI opaque call pointer to find.
1316 * \note Assumes the pri->lock is already obtained.
1318 * \retval array-index into private pointer array on success.
1319 * \retval -1 on error.
1321 static int pri_find_principle_by_call(struct sig_pri_span *pri, q931_call *call)
1326 /* Cannot find a call without a call. */
1329 for (idx = 0; idx < pri->numchans; ++idx) {
1330 if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
1331 /* Found the principle */
1340 * \brief Kill the call.
1343 * \param pri PRI span control structure.
1344 * \param call LibPRI opaque call pointer to find.
1345 * \param cause Reason call was killed.
1347 * \note Assumes the pvt->pri->lock is already obtained.
1351 static void sig_pri_kill_call(struct sig_pri_span *pri, q931_call *call, int cause)
1355 chanpos = pri_find_principle_by_call(pri, call);
1357 pri_hangup(pri->pri, call, cause);
1360 sig_pri_lock_private(pri->pvts[chanpos]);
1361 if (!pri->pvts[chanpos]->owner) {
1362 pri_hangup(pri->pri, call, cause);
1363 pri->pvts[chanpos]->call = NULL;
1364 sig_pri_unlock_private(pri->pvts[chanpos]);
1365 sig_pri_span_devstate_changed(pri);
1368 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, cause);
1369 pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
1370 sig_pri_unlock_private(pri->pvts[chanpos]);
1375 * \brief Find the private structure for the libpri call.
1377 * \param pri PRI span control structure.
1378 * \param channel LibPRI encoded channel ID.
1379 * \param call LibPRI opaque call pointer.
1381 * \note Assumes the pri->lock is already obtained.
1383 * \retval array-index into private pointer array on success.
1384 * \retval -1 on error.
1386 static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1394 /* Channel is not picked yet. */
1398 prioffset = PRI_CHANNEL(channel);
1399 if (!prioffset || (channel & PRI_HELD_CALL)) {
1400 /* Find the call waiting call or held call. */
1401 return pri_find_principle_by_call(pri, call);
1404 span = PRI_SPAN(channel);
1405 if (!(channel & PRI_EXPLICIT)) {
1408 index = pri_active_dchan_index(pri);
1412 span = pri->dchan_logical_span[index];
1416 for (x = 0; x < pri->numchans; x++) {
1418 && pri->pvts[x]->prioffset == prioffset
1419 && pri->pvts[x]->logicalspan == span
1420 && !pri->pvts[x]->no_b_channel) {
1431 * \brief Fixup the private structure associated with the libpri call.
1433 * \param pri PRI span control structure.
1434 * \param principle Array-index into private array to move call to if not already there.
1435 * \param call LibPRI opaque call pointer to find if need to move call.
1437 * \note Assumes the pri->lock is already obtained.
1439 * \retval principle on success.
1440 * \retval -1 on error.
1442 static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
1446 if (principle < 0 || pri->numchans <= principle) {
1454 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
1455 /* Call is already on the specified principle. */
1459 /* Find the old principle location. */
1460 for (x = 0; x < pri->numchans; x++) {
1461 struct sig_pri_chan *new_chan;
1462 struct sig_pri_chan *old_chan;
1464 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
1468 /* Found our call */
1469 new_chan = pri->pvts[principle];
1470 old_chan = pri->pvts[x];
1472 /* Get locks to safely move to the new private structure. */
1473 sig_pri_lock_private(old_chan);
1474 sig_pri_lock_owner(pri, x);
1475 sig_pri_lock_private(new_chan);
1477 ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
1478 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1479 old_chan->channel, new_chan->channel);
1480 if (!sig_pri_is_chan_available(new_chan)) {
1481 ast_log(LOG_WARNING,
1482 "Can't move call (%s) from channel %d to %d. It is already in use.\n",
1483 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1484 old_chan->channel, new_chan->channel);
1485 sig_pri_unlock_private(new_chan);
1486 if (old_chan->owner) {
1487 ast_channel_unlock(old_chan->owner);
1489 sig_pri_unlock_private(old_chan);
1493 sig_pri_fixup_chans(old_chan, new_chan);
1495 /* Fix it all up now */
1496 new_chan->owner = old_chan->owner;
1497 old_chan->owner = NULL;
1499 new_chan->call = old_chan->call;
1500 old_chan->call = NULL;
1502 /* Transfer flags from the old channel. */
1503 #if defined(HAVE_PRI_AOC_EVENTS)
1504 new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
1505 new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
1506 new_chan->holding_aoce = old_chan->holding_aoce;
1507 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1508 new_chan->alreadyhungup = old_chan->alreadyhungup;
1509 new_chan->isidlecall = old_chan->isidlecall;
1510 new_chan->progress = old_chan->progress;
1511 new_chan->allocated = old_chan->allocated;
1512 new_chan->outgoing = old_chan->outgoing;
1513 new_chan->digital = old_chan->digital;
1514 #if defined(HAVE_PRI_CALL_WAITING)
1515 new_chan->is_call_waiting = old_chan->is_call_waiting;
1516 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1518 #if defined(HAVE_PRI_AOC_EVENTS)
1519 old_chan->aoc_s_request_invoke_id_valid = 0;
1520 old_chan->waiting_for_aoce = 0;
1521 old_chan->holding_aoce = 0;
1522 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1523 old_chan->alreadyhungup = 0;
1524 old_chan->isidlecall = 0;
1525 old_chan->progress = 0;
1526 old_chan->allocated = 0;
1527 old_chan->outgoing = 0;
1528 old_chan->digital = 0;
1529 #if defined(HAVE_PRI_CALL_WAITING)
1530 old_chan->is_call_waiting = 0;
1531 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1533 /* More stuff to transfer to the new channel. */
1534 new_chan->call_level = old_chan->call_level;
1535 old_chan->call_level = SIG_PRI_CALL_LEVEL_IDLE;
1536 #if defined(HAVE_PRI_REVERSE_CHARGE)
1537 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
1538 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
1539 #if defined(HAVE_PRI_SETUP_KEYPAD)
1540 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
1541 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
1542 strcpy(new_chan->deferred_digits, old_chan->deferred_digits);
1543 strcpy(new_chan->moh_suggested, old_chan->moh_suggested);
1544 new_chan->moh_state = old_chan->moh_state;
1545 old_chan->moh_state = SIG_PRI_MOH_STATE_IDLE;
1547 #if defined(HAVE_PRI_AOC_EVENTS)
1548 new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
1549 new_chan->aoc_e = old_chan->aoc_e;
1550 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1551 strcpy(new_chan->user_tag, old_chan->user_tag);
1553 if (new_chan->no_b_channel) {
1554 /* Copy the real channel configuration to the no B channel interface. */
1555 new_chan->hidecallerid = old_chan->hidecallerid;
1556 new_chan->hidecalleridname = old_chan->hidecalleridname;
1557 new_chan->immediate = old_chan->immediate;
1558 new_chan->priexclusive = old_chan->priexclusive;
1559 new_chan->priindication_oob = old_chan->priindication_oob;
1560 new_chan->use_callerid = old_chan->use_callerid;
1561 new_chan->use_callingpres = old_chan->use_callingpres;
1562 new_chan->stripmsd = old_chan->stripmsd;
1563 strcpy(new_chan->context, old_chan->context);
1564 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1566 /* Become a member of the old channel span/trunk-group. */
1567 new_chan->logicalspan = old_chan->logicalspan;
1568 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1569 } else if (old_chan->no_b_channel) {
1571 * We are transitioning from a held/call-waiting channel to a
1572 * real channel so we need to make sure that the media path is
1573 * open. (Needed especially if the channel is natively
1576 sig_pri_open_media(new_chan);
1579 if (new_chan->owner) {
1580 sig_pri_ami_channel_event(new_chan);
1583 sig_pri_unlock_private(old_chan);
1584 if (new_chan->owner) {
1585 ast_channel_unlock(new_chan->owner);
1587 sig_pri_unlock_private(new_chan);
1591 ast_verb(3, "Call specified, but not found.\n");
1597 * \brief Find and fixup the private structure associated with the libpri call.
1599 * \param pri PRI span control structure.
1600 * \param channel LibPRI encoded channel ID.
1601 * \param call LibPRI opaque call pointer.
1604 * This is a combination of pri_find_principle() and pri_fixup_principle()
1605 * to reduce code redundancy and to make handling several PRI_EVENT_xxx's
1606 * consistent for the current architecture.
1608 * \note Assumes the pri->lock is already obtained.
1610 * \retval array-index into private pointer array on success.
1611 * \retval -1 on error.
1613 static int pri_find_fixup_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1617 chanpos = pri_find_principle(pri, channel, call);
1619 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is unconfigured.\n",
1620 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1621 sig_pri_kill_call(pri, call, PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST);
1624 chanpos = pri_fixup_principle(pri, chanpos, call);
1626 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is not available.\n",
1627 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1629 * Using Q.931 section 5.2.3.1 b) as the reason for picking
1630 * PRI_CAUSE_CHANNEL_UNACCEPTABLE. Receiving a
1631 * PRI_CAUSE_REQUESTED_CHAN_UNAVAIL would cause us to restart
1632 * that channel (which is not specified by Q.931) and kill some
1633 * other call which would be bad.
1635 sig_pri_kill_call(pri, call, PRI_CAUSE_CHANNEL_UNACCEPTABLE);
1641 static char * redirectingreason2str(int redirectingreason)
1643 switch (redirectingreason) {
1651 return "UNCONDITIONAL";
1653 return "NOREDIRECT";
1657 static char *dialplan2str(int dialplan)
1659 if (dialplan == -1) {
1660 return("Dynamically set dialplan in ISDN");
1662 return (pri_plan2str(dialplan));
1667 * \brief Apply numbering plan prefix to the given number.
1669 * \param buf Buffer to put number into.
1670 * \param size Size of given buffer.
1671 * \param pri PRI span control structure.
1672 * \param number Number to apply numbering plan.
1673 * \param plan Numbering plan to apply.
1677 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1680 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1681 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1683 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1684 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1686 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1687 snprintf(buf, size, "%s%s", pri->localprefix, number);
1689 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1690 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1692 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1693 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1695 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1696 snprintf(buf, size, "%s", number);
1703 * \brief Apply numbering plan prefix to the given number if the number exists.
1705 * \param buf Buffer to put number into.
1706 * \param size Size of given buffer.
1707 * \param pri PRI span control structure.
1708 * \param number Number to apply numbering plan.
1709 * \param plan Numbering plan to apply.
1713 static void apply_plan_to_existing_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1715 /* Make sure a number exists so the prefix isn't placed on an empty string. */
1716 if (ast_strlen_zero(number)) {
1722 apply_plan_to_number(buf, size, pri, number, plan);
1727 * \brief Restart the next channel we think is idle on the span.
1729 * \param pri PRI span control structure.
1731 * \note Assumes the pri->lock is already obtained.
1735 static void pri_check_restart(struct sig_pri_span *pri)
1737 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1739 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1741 for (++pri->resetpos; pri->resetpos < pri->numchans; ++pri->resetpos) {
1742 if (!pri->pvts[pri->resetpos]
1743 || pri->pvts[pri->resetpos]->no_b_channel
1744 || sig_pri_is_chan_in_use(pri->pvts[pri->resetpos])) {
1747 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1748 why = pri->pvts[pri->resetpos]->service_status;
1751 "Span %d: channel %d out-of-service (reason: %s), not sending RESTART\n",
1752 pri->span, pri->pvts[pri->resetpos]->channel,
1753 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1756 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1759 if (pri->resetpos < pri->numchans) {
1760 /* Mark the channel as resetting and restart it */
1761 pri->pvts[pri->resetpos]->resetting = SIG_PRI_RESET_ACTIVE;
1762 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1765 time(&pri->lastreset);
1766 sig_pri_span_devstate_changed(pri);
1770 #if defined(HAVE_PRI_CALL_WAITING)
1773 * \brief Init the private channel configuration using the span controller.
1776 * \param pvt Channel to init the configuration.
1777 * \param pri PRI span control structure.
1779 * \note Assumes the pri->lock is already obtained.
1783 static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
1785 pvt->stripmsd = pri->ch_cfg.stripmsd;
1786 pvt->hidecallerid = pri->ch_cfg.hidecallerid;
1787 pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
1788 pvt->immediate = pri->ch_cfg.immediate;
1789 pvt->priexclusive = pri->ch_cfg.priexclusive;
1790 pvt->priindication_oob = pri->ch_cfg.priindication_oob;
1791 pvt->use_callerid = pri->ch_cfg.use_callerid;
1792 pvt->use_callingpres = pri->ch_cfg.use_callingpres;
1793 ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
1794 ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
1796 if (sig_pri_callbacks.init_config) {
1797 sig_pri_callbacks.init_config(pvt->chan_pvt, pri);
1800 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1804 * \brief Find an empty B-channel interface to use.
1806 * \param pri PRI span control structure.
1807 * \param backwards TRUE if the search starts from higher channels.
1809 * \note Assumes the pri->lock is already obtained.
1811 * \retval array-index into private pointer array on success.
1812 * \retval -1 on error.
1814 static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
1822 if (backwards && (x < 0))
1824 if (!backwards && (x >= pri->numchans))
1827 && !pri->pvts[x]->no_b_channel
1828 && sig_pri_is_chan_available(pri->pvts[x])) {
1829 ast_debug(1, "Found empty available channel %d/%d\n",
1830 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1841 #if defined(HAVE_PRI_CALL_HOLD)
1844 * \brief Find or create an empty no-B-channel interface to use.
1847 * \param pri PRI span control structure.
1849 * \note Assumes the pri->lock is already obtained.
1851 * \retval array-index into private pointer array on success.
1852 * \retval -1 on error.
1854 static int pri_find_empty_nobch(struct sig_pri_span *pri)
1858 for (idx = 0; idx < pri->numchans; ++idx) {
1860 && pri->pvts[idx]->no_b_channel
1861 && sig_pri_is_chan_available(pri->pvts[idx])) {
1862 ast_debug(1, "Found empty available no B channel interface\n");
1867 /* Need to create a new interface. */
1868 if (sig_pri_callbacks.new_nobch_intf) {
1869 idx = sig_pri_callbacks.new_nobch_intf(pri);
1875 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1877 static void *do_idle_thread(void *v_pvt)
1879 struct sig_pri_chan *pvt = v_pvt;
1880 struct ast_channel *chan = pvt->owner;
1881 struct ast_frame *f;
1883 /* Wait up to 30 seconds for an answer */
1884 int timeout_ms = 30000;
1886 struct timeval start;
1887 struct ast_callid *callid;
1889 if ((callid = ast_channel_callid(chan))) {
1890 ast_callid_threadassoc_add(callid);
1891 callid = ast_callid_unref(callid);
1894 ast_verb(3, "Initiating idle call on channel %s\n", ast_channel_name(chan));
1895 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
1896 if (ast_call(chan, ex, 0)) {
1897 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", ast_channel_name(chan), ex);
1901 start = ast_tvnow();
1902 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1903 if (ast_waitfor(chan, ms) <= 0) {
1912 if (f->frametype == AST_FRAME_CONTROL) {
1913 switch (f->subclass.integer) {
1914 case AST_CONTROL_ANSWER:
1915 /* Launch the PBX */
1916 ast_channel_exten_set(chan, pvt->pri->idleext);
1917 ast_channel_context_set(chan, pvt->pri->idlecontext);
1918 ast_channel_priority_set(chan, 1);
1919 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", ast_channel_name(chan), ast_channel_exten(chan), ast_channel_context(chan));
1921 /* It's already hungup, return immediately */
1923 case AST_CONTROL_BUSY:
1924 ast_verb(4, "Idle channel '%s' busy, waiting...\n", ast_channel_name(chan));
1926 case AST_CONTROL_CONGESTION:
1927 ast_verb(4, "Idle channel '%s' congested, waiting...\n", ast_channel_name(chan));
1933 /* Hangup the channel since nothing happend */
1938 static void *pri_ss_thread(void *data)
1940 struct sig_pri_chan *p = data;
1941 struct ast_channel *chan = p->owner;
1942 char exten[AST_MAX_EXTENSION];
1946 struct ast_callid *callid;
1949 /* We lost the owner before we could get started. */
1953 if ((callid = ast_channel_callid(chan))) {
1954 ast_callid_threadassoc_add(callid);
1955 ast_callid_unref(callid);
1959 * In the bizarre case where the channel has become a zombie before we
1960 * even get started here, abort safely.
1962 if (!ast_channel_tech_pvt(chan)) {
1963 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
1968 ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
1970 sig_pri_dsp_reset_and_flush_digits(p);
1972 /* Now loop looking for an extension */
1973 ast_copy_string(exten, p->exten, sizeof(exten));
1974 len = strlen(exten);
1976 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
1977 if (len && !ast_ignore_pattern(ast_channel_context(chan), exten))
1978 sig_pri_play_tone(p, -1);
1980 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
1981 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num))
1982 timeout = pri_matchdigittimeout;
1984 timeout = pri_gendigittimeout;
1985 res = ast_waitfordigit(chan, timeout);
1987 ast_debug(1, "waitfordigit returned < 0...\n");
1996 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
1997 if (ast_strlen_zero(exten)) {
1998 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
2002 ast_free(ast_channel_dialed(chan)->number.str);
2003 ast_channel_dialed(chan)->number.str = ast_strdup(exten);
2005 if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
2007 * Update the user tag for party id's from this device for this call
2008 * now that we have a complete MSN from the network.
2010 snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
2012 ast_free(ast_channel_caller(chan)->id.tag);
2013 ast_channel_caller(chan)->id.tag = ast_strdup(p->user_tag);
2016 sig_pri_play_tone(p, -1);
2017 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2018 /* Start the real PBX */
2019 ast_channel_exten_set(chan, exten);
2020 sig_pri_dsp_reset_and_flush_digits(p);
2021 #if defined(ISSUE_16789)
2023 * Conditionaled out this code to effectively revert the Mantis
2024 * issue 16789 change. It breaks overlap dialing through
2025 * Asterisk. There is not enough information available at this
2026 * point to know if dialing is complete. The
2027 * ast_exists_extension(), ast_matchmore_extension(), and
2028 * ast_canmatch_extension() calls are not adequate to detect a
2029 * dial through extension pattern of "_9!".
2031 * Workaround is to use the dialplan Proceeding() application
2032 * early on non-dial through extensions.
2034 if ((p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2035 && !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2036 sig_pri_lock_private(p);
2038 pri_grab(p, p->pri);
2039 if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
2040 p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
2042 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
2045 sig_pri_unlock_private(p);
2047 #endif /* defined(ISSUE_16789) */
2049 sig_pri_set_echocanceller(p, 1);
2050 ast_setstate(chan, AST_STATE_RING);
2051 res = ast_pbx_run(chan);
2053 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
2056 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, ast_channel_context(chan));
2057 ast_channel_hangupcause_set(chan, AST_CAUSE_UNALLOCATED);
2060 /* Since we send release complete here, we won't get one */
2062 ast_mutex_lock(&p->pri->lock);
2063 sig_pri_span_devstate_changed(p->pri);
2064 ast_mutex_unlock(&p->pri->lock);
2069 void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
2071 pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
2072 if (!before_start_pri) {
2073 pri_find_dchan(pri);
2077 void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
2079 pri->dchanavail[index] |= DCHAN_NOTINALARM;
2080 if (!before_start_pri)
2081 pri_restart(pri->dchans[index]);
2086 * \brief Convert libpri party name into asterisk party name.
2089 * \param ast_name Asterisk party name structure to fill. Must already be set initialized.
2090 * \param pri_name libpri party name structure containing source information.
2092 * \note The filled in ast_name structure needs to be destroyed by
2093 * ast_party_name_free() when it is no longer needed.
2097 static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
2099 ast_name->str = ast_strdup(pri_name->str);
2100 ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
2101 ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
2102 ast_name->valid = 1;
2107 * \brief Convert libpri party number into asterisk party number.
2110 * \param ast_number Asterisk party number structure to fill. Must already be set initialized.
2111 * \param pri_number libpri party number structure containing source information.
2112 * \param pri PRI span control structure.
2114 * \note The filled in ast_number structure needs to be destroyed by
2115 * ast_party_number_free() when it is no longer needed.
2119 static void sig_pri_party_number_convert(struct ast_party_number *ast_number, const struct pri_party_number *pri_number, struct sig_pri_span *pri)
2121 char number[AST_MAX_EXTENSION];
2123 apply_plan_to_existing_number(number, sizeof(number), pri, pri_number->str,
2125 ast_number->str = ast_strdup(number);
2126 ast_number->plan = pri_number->plan;
2127 ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
2128 ast_number->valid = 1;
2133 * \brief Convert libpri party id into asterisk party id.
2136 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
2137 * \param pri_id libpri party id structure containing source information.
2138 * \param pri PRI span control structure.
2140 * \note The filled in ast_id structure needs to be destroyed by
2141 * ast_party_id_free() when it is no longer needed.
2145 static void sig_pri_party_id_convert(struct ast_party_id *ast_id, const struct pri_party_id *pri_id, struct sig_pri_span *pri)
2147 if (pri_id->name.valid) {
2148 sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
2150 if (pri_id->number.valid) {
2151 sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
2153 #if defined(HAVE_PRI_SUBADDR)
2154 if (pri_id->subaddress.valid) {
2155 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
2157 #endif /* defined(HAVE_PRI_SUBADDR) */
2162 * \brief Convert libpri redirecting information into asterisk redirecting information.
2165 * \param ast_redirecting Asterisk redirecting structure to fill.
2166 * \param pri_redirecting libpri redirecting structure containing source information.
2167 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
2168 * \param pri PRI span control structure.
2170 * \note The filled in ast_redirecting structure needs to be destroyed by
2171 * ast_party_redirecting_free() when it is no longer needed.
2175 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
2176 const struct pri_party_redirecting *pri_redirecting,
2177 const struct ast_party_redirecting *ast_guide,
2178 struct sig_pri_span *pri)
2180 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
2182 sig_pri_party_id_convert(&ast_redirecting->orig, &pri_redirecting->orig_called, pri);
2183 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
2184 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
2185 ast_redirecting->count = pri_redirecting->count;
2186 ast_redirecting->reason.code = pri_to_ast_reason(pri_redirecting->reason);
2187 ast_redirecting->orig_reason.code = pri_to_ast_reason(pri_redirecting->orig_reason);
2192 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
2195 * \param msn_patterns Comma separated list of MSN patterns to match.
2196 * \param exten Extension to match in the MSN list.
2198 * \retval 1 if matches.
2199 * \retval 0 if no match.
2201 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
2207 msn_list = ast_strdupa(msn_patterns);
2210 pattern = strtok_r(msn_list, ",", &list_tail);
2212 pattern = ast_strip(pattern);
2213 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
2214 /* Extension matched the pattern. */
2217 pattern = strtok_r(NULL, ",", &list_tail);
2219 /* Did not match any pattern in the list. */
2223 #if defined(HAVE_PRI_MCID)
2226 * \brief Append the given party id to the event string.
2229 * \param msg Event message string being built.
2230 * \param prefix Prefix to add to the party id lines.
2231 * \param party Party information to encode.
2235 static void sig_pri_event_party_id(struct ast_str **msg, const char *prefix, struct ast_party_id *party)
2239 /* Combined party presentation */
2240 pres = ast_party_id_presentation(party);
2241 ast_str_append(msg, 0, "%sPres: %d (%s)\r\n", prefix, pres,
2242 ast_describe_caller_presentation(pres));
2245 ast_str_append(msg, 0, "%sNumValid: %d\r\n", prefix,
2246 (unsigned) party->number.valid);
2247 ast_str_append(msg, 0, "%sNum: %s\r\n", prefix,
2248 S_COR(party->number.valid, party->number.str, ""));
2249 ast_str_append(msg, 0, "%ston: %d\r\n", prefix, party->number.plan);
2250 if (party->number.valid) {
2251 ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, party->number.plan);
2252 ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix,
2253 party->number.presentation,
2254 ast_describe_caller_presentation(party->number.presentation));
2258 ast_str_append(msg, 0, "%sNameValid: %d\r\n", prefix,
2259 (unsigned) party->name.valid);
2260 ast_str_append(msg, 0, "%sName: %s\r\n", prefix,
2261 S_COR(party->name.valid, party->name.str, ""));
2262 if (party->name.valid) {
2263 ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix,
2264 ast_party_name_charset_describe(party->name.char_set));
2265 ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix,
2266 party->name.presentation,
2267 ast_describe_caller_presentation(party->name.presentation));
2270 #if defined(HAVE_PRI_SUBADDR)
2271 /* Party subaddress */
2272 if (party->subaddress.valid) {
2273 static const char subaddress[] = "Subaddr";
2275 ast_str_append(msg, 0, "%s%s: %s\r\n", prefix, subaddress,
2276 S_OR(party->subaddress.str, ""));
2277 ast_str_append(msg, 0, "%s%sType: %d\r\n", prefix, subaddress,
2278 party->subaddress.type);
2279 ast_str_append(msg, 0, "%s%sOdd: %d\r\n", prefix, subaddress,
2280 party->subaddress.odd_even_indicator);
2282 #endif /* defined(HAVE_PRI_SUBADDR) */
2284 #endif /* defined(HAVE_PRI_MCID) */
2286 #if defined(HAVE_PRI_MCID)
2289 * \brief Handle the MCID event.
2292 * \param pri PRI span control structure.
2293 * \param mcid MCID event parameters.
2294 * \param owner Asterisk channel associated with the call.
2295 * NULL if Asterisk no longer has the ast_channel struct.
2297 * \note Assumes the pri->lock is already obtained.
2298 * \note Assumes the owner channel lock is already obtained if still present.
2302 static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
2304 struct ast_channel *chans[1];
2305 struct ast_str *msg;
2306 struct ast_party_id party;
2308 msg = ast_str_create(4096);
2315 * The owner channel is present.
2316 * Pass the event to the peer as well.
2318 ast_queue_control(owner, AST_CONTROL_MCID);
2320 ast_str_append(&msg, 0, "Channel: %s\r\n", ast_channel_name(owner));
2321 ast_str_append(&msg, 0, "UniqueID: %s\r\n", ast_channel_uniqueid(owner));
2323 sig_pri_event_party_id(&msg, "CallerID", &ast_channel_connected(owner)->id);
2326 * Since we no longer have an owner channel,
2327 * we have to use the caller information supplied by libpri.
2329 ast_party_id_init(&party);
2330 sig_pri_party_id_convert(&party, &mcid->originator, pri);
2331 sig_pri_event_party_id(&msg, "CallerID", &party);
2332 ast_party_id_free(&party);
2335 /* Always use libpri's called party information. */
2336 ast_party_id_init(&party);
2337 sig_pri_party_id_convert(&party, &mcid->answerer, pri);
2338 sig_pri_event_party_id(&msg, "ConnectedID", &party);
2339 ast_party_id_free(&party);
2342 ast_manager_event_multichan(EVENT_FLAG_CALL, "MCID", owner ? 1 : 0, chans, "%s",
2343 ast_str_buffer(msg));
2346 #endif /* defined(HAVE_PRI_MCID) */
2348 #if defined(HAVE_PRI_TRANSFER)
2349 struct xfer_rsp_data {
2350 struct sig_pri_span *pri;
2351 /*! Call to send transfer success/fail response over. */
2353 /*! Invocation ID to use when sending a reply to the transfer request. */
2356 #endif /* defined(HAVE_PRI_TRANSFER) */
2358 #if defined(HAVE_PRI_TRANSFER)
2361 * \brief Send the transfer success/fail response message.
2364 * \param data Callback user data pointer
2365 * \param is_successful TRUE if the transfer was successful.
2369 static void sig_pri_transfer_rsp(void *data, int is_successful)
2371 struct xfer_rsp_data *rsp = data;
2373 pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
2375 #endif /* defined(HAVE_PRI_TRANSFER) */
2377 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2379 * \brief Protocol callback to indicate if transfer will happen.
2382 * \param data Callback user data pointer
2383 * \param is_successful TRUE if the transfer will happen.
2387 typedef void (*xfer_rsp_callback)(void *data, int is_successful);
2388 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2390 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2393 * \brief Attempt to transfer the two calls to each other.
2396 * \param pri PRI span control structure.
2397 * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
2398 * \param call_1_held TRUE if call_1_pri is on hold.
2399 * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
2400 * \param call_2_held TRUE if call_2_pri is on hold.
2401 * \param rsp_callback Protocol callback to indicate if transfer will happen. NULL if not used.
2402 * \param data Callback user data pointer
2404 * \note Assumes the pri->lock is already obtained.
2406 * \retval 0 on success.
2407 * \retval -1 on error.
2409 static int sig_pri_attempt_transfer(struct sig_pri_span *pri, q931_call *call_1_pri, int call_1_held, q931_call *call_2_pri, int call_2_held, xfer_rsp_callback rsp_callback, void *data)
2411 struct attempt_xfer_call {
2413 struct ast_channel *ast;
2418 struct ast_channel *transferee;
2419 struct attempt_xfer_call *call_1;
2420 struct attempt_xfer_call *call_2;
2421 struct attempt_xfer_call *swap_call;
2422 struct attempt_xfer_call c1;
2423 struct attempt_xfer_call c2;
2425 c1.pri = call_1_pri;
2426 c1.held = call_1_held;
2429 c2.pri = call_2_pri;
2430 c2.held = call_2_held;
2433 call_1->chanpos = pri_find_principle_by_call(pri, call_1->pri);
2434 call_2->chanpos = pri_find_principle_by_call(pri, call_2->pri);
2435 if (call_1->chanpos < 0 || call_2->chanpos < 0) {
2436 /* Calls not found in span control. */
2438 /* Transfer failed. */
2439 rsp_callback(data, 0);
2444 /* Attempt to make transferee and target consistent. */
2445 if (!call_1->held && call_2->held) {
2447 * Swap call_1 and call_2 to make call_1 the transferee(held call)
2448 * and call_2 the target(active call).
2455 /* Deadlock avoidance is attempted. */
2456 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2457 sig_pri_lock_owner(pri, call_1->chanpos);
2458 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2459 sig_pri_lock_owner(pri, call_2->chanpos);
2461 call_1->ast = pri->pvts[call_1->chanpos]->owner;
2462 call_2->ast = pri->pvts[call_2->chanpos]->owner;
2463 if (!call_1->ast || !call_2->ast) {
2464 /* At least one owner is not present. */
2466 ast_channel_unlock(call_1->ast);
2469 ast_channel_unlock(call_2->ast);
2471 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2472 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2474 /* Transfer failed. */
2475 rsp_callback(data, 0);
2481 transferee = ast_bridged_channel(call_1->ast);
2486 /* Try masquerading the other way. */
2491 transferee = ast_bridged_channel(call_1->ast);
2496 /* Could not transfer. Neither call is bridged. */
2497 ast_channel_unlock(call_1->ast);
2498 ast_channel_unlock(call_2->ast);
2499 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2500 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2503 /* Transfer failed. */
2504 rsp_callback(data, 0);
2509 ast_verb(3, "TRANSFERRING %s to %s\n", ast_channel_name(call_1->ast), ast_channel_name(call_2->ast));
2512 * Setup transfer masquerade.
2514 * Note: There is an extremely nasty deadlock avoidance issue
2515 * with ast_channel_transfer_masquerade(). Deadlock may be possible if
2516 * the channels involved are proxies (chan_agent channels) and
2517 * it is called with locks. Unfortunately, there is no simple
2518 * or even merely difficult way to guarantee deadlock avoidance
2519 * and still be able to send an ECT success response without the
2520 * possibility of the bridged channel hanging up on us.
2522 ast_mutex_unlock(&pri->lock);
2523 retval = ast_channel_transfer_masquerade(
2525 ast_channel_connected(call_2->ast),
2528 ast_channel_connected(call_1->ast),
2531 /* Reacquire the pri->lock to hold off completion of the transfer masquerade. */
2532 ast_mutex_lock(&pri->lock);
2534 ast_channel_unlock(call_1->ast);
2535 ast_channel_unlock(call_2->ast);
2536 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2537 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2541 * Report transfer status.
2543 * Must do the callback before the masquerade completes to ensure
2544 * that the protocol message goes out before the call leg is
2547 rsp_callback(data, retval ? 0 : 1);
2551 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2553 #if defined(HAVE_PRI_CCSS)
2556 * \brief Compare the CC agent private data by libpri cc_id.
2559 * \param obj pointer to the (user-defined part) of an object.
2560 * \param arg callback argument from ao2_callback()
2561 * \param flags flags from ao2_callback()
2563 * \return values are a combination of enum _cb_results.
2565 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
2567 struct ast_cc_agent *agent_1 = obj;
2568 struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
2569 struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
2571 return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
2572 && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2574 #endif /* defined(HAVE_PRI_CCSS) */
2576 #if defined(HAVE_PRI_CCSS)
2579 * \brief Find the CC agent by libpri cc_id.
2582 * \param pri PRI span control structure.
2583 * \param cc_id CC record ID to find.
2586 * Since agents are refcounted, and this function returns
2587 * a reference to the agent, it is imperative that you decrement
2588 * the refcount of the agent once you have finished using it.
2590 * \retval agent on success.
2591 * \retval NULL not found.
2593 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
2595 struct sig_pri_cc_agent_prv finder = {
2600 return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
2601 sig_pri_cc_type_name);
2603 #endif /* defined(HAVE_PRI_CCSS) */
2605 #if defined(HAVE_PRI_CCSS)
2608 * \brief Compare the CC monitor instance by libpri cc_id.
2611 * \param obj pointer to the (user-defined part) of an object.
2612 * \param arg callback argument from ao2_callback()
2613 * \param flags flags from ao2_callback()
2615 * \return values are a combination of enum _cb_results.
2617 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
2619 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
2620 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
2622 return (monitor_1->pri == monitor_2->pri
2623 && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2625 #endif /* defined(HAVE_PRI_CCSS) */
2627 #if defined(HAVE_PRI_CCSS)
2630 * \brief Find the CC monitor instance by libpri cc_id.
2633 * \param pri PRI span control structure.
2634 * \param cc_id CC record ID to find.
2637 * Since monitor_instances are refcounted, and this function returns
2638 * a reference to the instance, it is imperative that you decrement
2639 * the refcount of the instance once you have finished using it.
2641 * \retval monitor_instance on success.
2642 * \retval NULL not found.
2644 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
2646 struct sig_pri_cc_monitor_instance finder = {
2651 return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
2653 #endif /* defined(HAVE_PRI_CCSS) */
2655 #if defined(HAVE_PRI_CCSS)
2658 * \brief Destroy the given monitor instance.
2661 * \param data Monitor instance to destroy.
2665 static void sig_pri_cc_monitor_instance_destroy(void *data)
2667 struct sig_pri_cc_monitor_instance *monitor_instance = data;
2669 if (monitor_instance->cc_id != -1) {
2670 ast_mutex_lock(&monitor_instance->pri->lock);
2671 pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
2672 ast_mutex_unlock(&monitor_instance->pri->lock);
2674 sig_pri_callbacks.module_unref();
2676 #endif /* defined(HAVE_PRI_CCSS) */
2678 #if defined(HAVE_PRI_CCSS)
2681 * \brief Construct a new monitor instance.
2684 * \param core_id CC core ID.
2685 * \param pri PRI span control structure.
2686 * \param cc_id CC record ID.
2687 * \param device_name Name of device (Asterisk channel name less sequence number).
2690 * Since monitor_instances are refcounted, and this function returns
2691 * a reference to the instance, it is imperative that you decrement
2692 * the refcount of the instance once you have finished using it.
2694 * \retval monitor_instance on success.
2695 * \retval NULL on error.
2697 static struct sig_pri_cc_monitor_instance *sig_pri_cc_monitor_instance_init(int core_id, struct sig_pri_span *pri, long cc_id, const char *device_name)
2699 struct sig_pri_cc_monitor_instance *monitor_instance;
2701 if (!sig_pri_callbacks.module_ref || !sig_pri_callbacks.module_unref) {
2705 monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
2706 sig_pri_cc_monitor_instance_destroy);
2707 if (!monitor_instance) {
2711 monitor_instance->cc_id = cc_id;
2712 monitor_instance->pri = pri;
2713 monitor_instance->core_id = core_id;
2714 strcpy(monitor_instance->name, device_name);
2716 sig_pri_callbacks.module_ref();
2718 ao2_link(sig_pri_cc_monitors, monitor_instance);
2719 return monitor_instance;
2721 #endif /* defined(HAVE_PRI_CCSS) */
2723 #if defined(HAVE_PRI_CCSS)
2726 * \brief Announce to the CC core that protocol CC monitor is available for this call.
2729 * \param pri PRI span control structure.
2730 * \param chanpos Channel position in the span.
2731 * \param cc_id CC record ID.
2732 * \param service CCBS/CCNR indication.
2734 * \note Assumes the pri->lock is already obtained.
2735 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2736 * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
2738 * \retval 0 on success.
2739 * \retval -1 on error.
2741 static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
2743 struct sig_pri_chan *pvt;
2744 struct ast_cc_config_params *cc_params;
2745 struct sig_pri_cc_monitor_instance *monitor;
2746 enum ast_cc_monitor_policies monitor_policy;
2749 char device_name[AST_CHANNEL_NAME];
2750 char dialstring[AST_CHANNEL_NAME];
2752 pvt = pri->pvts[chanpos];
2754 core_id = ast_cc_get_current_core_id(pvt->owner);
2755 if (core_id == -1) {
2759 cc_params = ast_channel_get_cc_config_params(pvt->owner);
2765 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2766 switch (monitor_policy) {
2767 case AST_CC_MONITOR_NEVER:
2768 /* CCSS is not enabled. */
2770 case AST_CC_MONITOR_NATIVE:
2771 case AST_CC_MONITOR_ALWAYS:
2773 * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
2774 * later in the call to sig_pri_cc_generic_check().
2776 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
2777 sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
2778 monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
2782 res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
2785 monitor->cc_id = -1;
2786 ao2_unlink(sig_pri_cc_monitors, monitor);
2787 ao2_ref(monitor, -1);
2790 case AST_CC_MONITOR_GENERIC:
2791 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
2792 sig_pri_get_orig_dialstring(pvt), service, NULL);
2793 /* Say it failed to force caller to cancel native CC. */
2798 #endif /* defined(HAVE_PRI_CCSS) */
2802 * \brief Check if generic CC monitor is needed and request it.
2805 * \param pri PRI span control structure.
2806 * \param chanpos Channel position in the span.
2807 * \param service CCBS/CCNR indication.
2809 * \note Assumes the pri->lock is already obtained.
2810 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2814 static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
2816 struct ast_channel *owner;
2817 struct ast_cc_config_params *cc_params;
2818 #if defined(HAVE_PRI_CCSS)
2819 struct ast_cc_monitor *monitor;
2820 char device_name[AST_CHANNEL_NAME];
2821 #endif /* defined(HAVE_PRI_CCSS) */
2822 enum ast_cc_monitor_policies monitor_policy;
2825 if (!pri->pvts[chanpos]->outgoing) {
2826 /* This is not an outgoing call so it cannot be CC monitor. */
2830 sig_pri_lock_owner(pri, chanpos);
2831 owner = pri->pvts[chanpos]->owner;
2835 core_id = ast_cc_get_current_core_id(owner);
2836 if (core_id == -1) {
2837 /* No CC core setup */
2841 cc_params = ast_channel_get_cc_config_params(owner);
2843 /* Could not get CC config parameters. */
2847 #if defined(HAVE_PRI_CCSS)
2848 ast_channel_get_device_name(owner, device_name, sizeof(device_name));
2849 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
2851 /* CC monitor is already present so no need for generic CC. */
2852 ao2_ref(monitor, -1);
2855 #endif /* defined(HAVE_PRI_CCSS) */
2857 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2858 switch (monitor_policy) {
2859 case AST_CC_MONITOR_NEVER:
2860 /* CCSS is not enabled. */
2862 case AST_CC_MONITOR_NATIVE:
2863 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
2864 /* Request generic CC monitor. */
2865 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
2866 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
2869 case AST_CC_MONITOR_ALWAYS:
2870 if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
2872 * Cannot monitor PTMP TE side since this is not defined.
2873 * We are playing the roll of a phone in this case and
2874 * a phone cannot monitor a party over the network without
2880 * We are either falling back or this is a PTMP NT span.
2881 * Request generic CC monitor.
2883 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
2884 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
2886 case AST_CC_MONITOR_GENERIC:
2887 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
2888 /* Request generic CC monitor. */
2889 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
2890 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
2896 ast_channel_unlock(owner);
2899 #if defined(HAVE_PRI_CCSS)
2902 * \brief The CC link canceled the CC instance.
2905 * \param pri PRI span control structure.
2906 * \param cc_id CC record ID.
2907 * \param is_agent TRUE if the cc_id is for an agent.
2911 static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
2914 struct ast_cc_agent *agent;
2916 agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
2920 ast_cc_failed(agent->core_id, "%s agent got canceled by link",
2921 sig_pri_cc_type_name);
2924 struct sig_pri_cc_monitor_instance *monitor;
2926 monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
2930 monitor->cc_id = -1;
2931 ast_cc_monitor_failed(monitor->core_id, monitor->name,
2932 "%s monitor got canceled by link", sig_pri_cc_type_name);
2933 ao2_ref(monitor, -1);
2936 #endif /* defined(HAVE_PRI_CCSS) */
2938 #if defined(HAVE_PRI_AOC_EVENTS)
2941 * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
2944 * \param value Value to convert to string.
2946 * \return PRI_AOC_CHARGED_ITEM
2948 static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
2951 case AST_AOC_CHARGED_ITEM_NA:
2952 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
2953 case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
2954 return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
2955 case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
2956 return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
2957 case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
2958 return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
2959 case AST_AOC_CHARGED_ITEM_CALL_SETUP:
2960 return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
2961 case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
2962 return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
2963 case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
2964 return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
2966 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
2968 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
2970 #if defined(HAVE_PRI_AOC_EVENTS)
2973 * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
2976 * \param value Value to convert to string.
2978 * \return ast_aoc_charged_item
2980 static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
2983 case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
2984 return AST_AOC_CHARGED_ITEM_NA;
2985 case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
2986 return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
2987 case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
2988 return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
2989 case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
2990 return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
2991 case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
2992 return AST_AOC_CHARGED_ITEM_CALL_SETUP;
2993 case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
2994 return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
2995 case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
2996 return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
2998 return AST_AOC_CHARGED_ITEM_NA;
3000 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3002 #if defined(HAVE_PRI_AOC_EVENTS)
3005 * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
3008 * \return pri enum equivalent.
3010 static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
3013 case AST_AOC_MULT_ONETHOUSANDTH:
3014 return PRI_AOC_MULTIPLIER_THOUSANDTH;
3015 case AST_AOC_MULT_ONEHUNDREDTH:
3016 return PRI_AOC_MULTIPLIER_HUNDREDTH;
3017 case AST_AOC_MULT_ONETENTH:
3018 return PRI_AOC_MULTIPLIER_TENTH;
3019 case AST_AOC_MULT_ONE:
3020 return PRI_AOC_MULTIPLIER_ONE;
3021 case AST_AOC_MULT_TEN:
3022 return PRI_AOC_MULTIPLIER_TEN;
3023 case AST_AOC_MULT_HUNDRED:
3024 return PRI_AOC_MULTIPLIER_HUNDRED;
3025 case AST_AOC_MULT_THOUSAND:
3026 return PRI_AOC_MULTIPLIER_THOUSAND;
3028 return PRI_AOC_MULTIPLIER_ONE;
3031 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3033 #if defined(HAVE_PRI_AOC_EVENTS)
3036 * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
3039 * \return ast enum equivalent.
3041 static int sig_pri_aoc_multiplier_from_pri(const int mult)
3044 case PRI_AOC_MULTIPLIER_THOUSANDTH:
3045 return AST_AOC_MULT_ONETHOUSANDTH;
3046 case PRI_AOC_MULTIPLIER_HUNDREDTH:
3047 return AST_AOC_MULT_ONEHUNDREDTH;
3048 case PRI_AOC_MULTIPLIER_TENTH:
3049 return AST_AOC_MULT_ONETENTH;
3050 case PRI_AOC_MULTIPLIER_ONE:
3051 return AST_AOC_MULT_ONE;
3052 case PRI_AOC_MULTIPLIER_TEN:
3053 return AST_AOC_MULT_TEN;
3054 case PRI_AOC_MULTIPLIER_HUNDRED:
3055 return AST_AOC_MULT_HUNDRED;
3056 case PRI_AOC_MULTIPLIER_THOUSAND:
3057 return AST_AOC_MULT_THOUSAND;
3059 return AST_AOC_MULT_ONE;
3062 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3064 #if defined(HAVE_PRI_AOC_EVENTS)
3067 * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
3070 * \param value Value to convert to ast representation
3072 * \return PRI_AOC_TIME_SCALE
3074 static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
3078 case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3079 return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3080 case AST_AOC_TIME_SCALE_TENTH_SECOND:
3081 return PRI_AOC_TIME_SCALE_TENTH_SECOND;
3082 case AST_AOC_TIME_SCALE_SECOND:
3083 return PRI_AOC_TIME_SCALE_SECOND;
3084 case AST_AOC_TIME_SCALE_TEN_SECOND:
3085 return PRI_AOC_TIME_SCALE_TEN_SECOND;
3086 case AST_AOC_TIME_SCALE_MINUTE:
3087 return PRI_AOC_TIME_SCALE_MINUTE;
3088 case AST_AOC_TIME_SCALE_HOUR:
3089 return PRI_AOC_TIME_SCALE_HOUR;
3090 case AST_AOC_TIME_SCALE_DAY:
3091 return PRI_AOC_TIME_SCALE_DAY;
3094 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3096 #if defined(HAVE_PRI_AOC_EVENTS)
3099 * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
3102 * \param value Value to convert to ast representation
3104 * \return ast aoc time scale
3106 static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
3110 case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3111 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3112 case PRI_AOC_TIME_SCALE_TENTH_SECOND:
3113 return AST_AOC_TIME_SCALE_TENTH_SECOND;
3114 case PRI_AOC_TIME_SCALE_SECOND:
3115 return AST_AOC_TIME_SCALE_SECOND;
3116 case PRI_AOC_TIME_SCALE_TEN_SECOND:
3117 return AST_AOC_TIME_SCALE_TEN_SECOND;
3118 case PRI_AOC_TIME_SCALE_MINUTE:
3119 return AST_AOC_TIME_SCALE_MINUTE;
3120 case PRI_AOC_TIME_SCALE_HOUR:
3121 return AST_AOC_TIME_SCALE_HOUR;
3122 case PRI_AOC_TIME_SCALE_DAY:
3123 return AST_AOC_TIME_SCALE_DAY;
3125 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3127 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3129 #if defined(HAVE_PRI_AOC_EVENTS)
3132 * \brief Handle AOC-S control frame
3135 * \param aoc_s AOC-S event parameters.
3136 * \param owner Asterisk channel associated with the call.
3137 * \param passthrough indicating if this message should be queued on the ast channel
3139 * \note Assumes the pri->lock is already obtained.
3140 * \note Assumes the sig_pri private is locked
3141 * \note Assumes the owner channel lock is already obtained.
3145 static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
3147 struct ast_aoc_decoded *decoded = NULL;
3148 struct ast_aoc_encoded *encoded = NULL;
3149 size_t encoded_size = 0;
3152 if (!owner || !aoc_s) {
3156 if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
3160 for (idx = 0; idx < aoc_s->num_items; ++idx) {
3161 enum ast_aoc_s_charged_item charged_item;
3163 charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
3164 if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
3165 /* Delete the unknown charged item from the list. */
3168 switch (aoc_s->item[idx].rate_type) {
3169 case PRI_AOC_RATE_TYPE_DURATION:
3170 ast_aoc_s_add_rate_duration(decoded,
3172 aoc_s->item[idx].rate.duration.amount.cost,
3173 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
3174 aoc_s->item[idx].rate.duration.currency,
3175 aoc_s->item[idx].rate.duration.time.length,
3176 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
3177 aoc_s->item[idx].rate.duration.granularity.length,
3178 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
3179 aoc_s->item[idx].rate.duration.charging_type);
3181 case PRI_AOC_RATE_TYPE_FLAT:
3182 ast_aoc_s_add_rate_flat(decoded,
3184 aoc_s->item[idx].rate.flat.amount.cost,
3185 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.flat.amount.multiplier),
3186 aoc_s->item[idx].rate.flat.currency);
3188 case PRI_AOC_RATE_TYPE_VOLUME:
3189 ast_aoc_s_add_rate_volume(decoded,
3191 aoc_s->item[idx].rate.volume.unit,
3192 aoc_s->item[idx].rate.volume.amount.cost,
3193 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.volume.amount.multiplier),
3194 aoc_s->item[idx].rate.volume.currency);
3196 case PRI_AOC_RATE_TYPE_SPECIAL_CODE:
3197 ast_aoc_s_add_rate_special_charge_code(decoded,
3199 aoc_s->item[idx].rate.special);
3201 case PRI_AOC_RATE_TYPE_FREE:
3202 ast_aoc_s_add_rate_free(decoded, charged_item, 0);
3204 case PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3205 ast_aoc_s_add_rate_free(decoded, charged_item, 1);
3208 ast_aoc_s_add_rate_na(decoded, charged_item);
3213 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3214 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3217 ast_aoc_manager_event(decoded, owner);
3219 ast_aoc_destroy_decoded(decoded);
3220 ast_aoc_destroy_encoded(encoded);
3222 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3224 #if defined(HAVE_PRI_AOC_EVENTS)
3227 * \brief Generate AOC Request Response
3230 * \param aoc_request
3232 * \note Assumes the pri->lock is already obtained.
3233 * \note Assumes the sig_pri private is locked
3234 * \note Assumes the owner channel lock is already obtained.
3238 static void sig_pri_aoc_request_from_pri(const struct pri_subcmd_aoc_request *aoc_request, struct sig_pri_chan *pvt, q931_call *call)
3246 request = aoc_request->charging_request;
3248 if (request & PRI_AOC_REQUEST_S) {
3249 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
3250 /* An AOC-S response must come from the other side, so save off this invoke_id
3251 * and see if an AOC-S message comes in before the call is answered. */
3252 pvt->aoc_s_request_invoke_id = aoc_request->invoke_id;
3253 pvt->aoc_s_request_invoke_id_valid = 1;
3256 pri_aoc_s_request_response_send(pvt->pri->pri,
3258 aoc_request->invoke_id,
3263 if (request & PRI_AOC_REQUEST_D) {
3264 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
3265 pri_aoc_de_request_response_send(pvt->pri->pri,
3267 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3268 aoc_request->invoke_id);
3270 pri_aoc_de_request_response_send(pvt->pri->pri,
3272 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3273 aoc_request->invoke_id);
3277 if (request & PRI_AOC_REQUEST_E) {
3278 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
3279 pri_aoc_de_request_response_send(pvt->pri->pri,
3281 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3282 aoc_request->invoke_id);
3284 pri_aoc_de_request_response_send(pvt->pri->pri,
3286 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3287 aoc_request->invoke_id);
3291 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3293 #if defined(HAVE_PRI_AOC_EVENTS)
3296 * \brief Generate AOC-D AST_CONTROL_AOC frame
3299 * \param aoc_e AOC-D event parameters.
3300 * \param owner Asterisk channel associated with the call.
3301 * \param passthrough indicating if this message should be queued on the ast channel
3303 * \note Assumes the pri->lock is already obtained.
3304 * \note Assumes the sig_pri private is locked
3305 * \note Assumes the owner channel lock is already obtained.
3309 static void sig_pri_aoc_d_from_pri(const struct pri_subcmd_aoc_d *aoc_d, struct ast_channel *owner, int passthrough)
3311 struct ast_aoc_decoded *decoded = NULL;
3312 struct ast_aoc_encoded *encoded = NULL;
3313 size_t encoded_size = 0;
3314 enum ast_aoc_charge_type type;
3316 if (!owner || !aoc_d) {
3320 switch (aoc_d->charge) {
3321 case PRI_AOC_DE_CHARGE_CURRENCY:
3322 type = AST_AOC_CHARGE_CURRENCY;
3324 case PRI_AOC_DE_CHARGE_UNITS:
3325 type = AST_AOC_CHARGE_UNIT;
3327 case PRI_AOC_DE_CHARGE_FREE:
3328 type = AST_AOC_CHARGE_FREE;
3331 type = AST_AOC_CHARGE_NA;
3335 if (!(decoded = ast_aoc_create(AST_AOC_D, type, 0))) {
3339 switch (aoc_d->billing_accumulation) {
3341 ast_debug(1, "AOC-D billing accumulation has unknown value: %d\n",
3342 aoc_d->billing_accumulation);
3344 case 0:/* subTotal */
3345 ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL);
3348 ast_aoc_set_total_type(decoded, AST_AOC_TOTAL);
3352 switch (aoc_d->billing_id) {
3353 case PRI_AOC_D_BILLING_ID_NORMAL:
3354 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3356 case PRI_AOC_D_BILLING_ID_REVERSE:
3357 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3359 case PRI_AOC_D_BILLING_ID_CREDIT_CARD:
3360 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3362 case PRI_AOC_D_BILLING_ID_NOT_AVAILABLE:
3364 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3368 switch (aoc_d->charge) {
3369 case PRI_AOC_DE_CHARGE_CURRENCY:
3370 ast_aoc_set_currency_info(decoded,
3371 aoc_d->recorded.money.amount.cost,
3372 sig_pri_aoc_multiplier_from_pri(aoc_d->recorded.money.amount.multiplier),
3373 aoc_d->recorded.money.currency);
3375 case PRI_AOC_DE_CHARGE_UNITS:
3378 for (i = 0; i < aoc_d->recorded.unit.num_items; ++i) {
3379 /* if type or number are negative, then they are not present */
3380 ast_aoc_add_unit_entry(decoded,
3381 (aoc_d->recorded.unit.item[i].number >= 0 ? 1 : 0),
3382 aoc_d->recorded.unit.item[i].number,
3383 (aoc_d->recorded.unit.item[i].type >= 0 ? 1 : 0),
3384 aoc_d->recorded.unit.item[i].type);
3390 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3391 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3394 ast_aoc_manager_event(decoded, owner);
3396 ast_aoc_destroy_decoded(decoded);
3397 ast_aoc_destroy_encoded(encoded);
3399 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3401 #if defined(HAVE_PRI_AOC_EVENTS)
3404 * \brief Generate AOC-E AST_CONTROL_AOC frame
3407 * \param aoc_e AOC-E event parameters.
3408 * \param owner Asterisk channel associated with the call.
3409 * \param passthrough indicating if this message should be queued on the ast channel
3411 * \note Assumes the pri->lock is already obtained.
3412 * \note Assumes the sig_pri private is locked
3413 * \note Assumes the owner channel lock is already obtained.
3414 * \note owner channel may be NULL. In that case, generate event only
3418 static void sig_pri_aoc_e_from_pri(const struct pri_subcmd_aoc_e *aoc_e, struct ast_channel *owner, int passthrough)
3420 struct ast_aoc_decoded *decoded = NULL;
3421 struct ast_aoc_encoded *encoded = NULL;
3422 size_t encoded_size = 0;
3423 enum ast_aoc_charge_type type;
3429 switch (aoc_e->charge) {
3430 case PRI_AOC_DE_CHARGE_CURRENCY:
3431 type = AST_AOC_CHARGE_CURRENCY;
3433 case PRI_AOC_DE_CHARGE_UNITS:
3434 type = AST_AOC_CHARGE_UNIT;
3436 case PRI_AOC_DE_CHARGE_FREE:
3437 type = AST_AOC_CHARGE_FREE;
3440 type = AST_AOC_CHARGE_NA;
3444 if (!(decoded = ast_aoc_create(AST_AOC_E, type, 0))) {
3448 switch (aoc_e->associated.charging_type) {
3449 case PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER:
3450 if (!aoc_e->associated.charge.number.valid) {
3453 ast_aoc_set_association_number(decoded, aoc_e->associated.charge.number.str, aoc_e->associated.charge.number.plan);
3455 case PRI_AOC_E_CHARGING_ASSOCIATION_ID:
3456 ast_aoc_set_association_id(decoded, aoc_e->associated.charge.id);
3462 switch (aoc_e->billing_id) {
3463 case PRI_AOC_E_BILLING_ID_NORMAL:
3464 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3466 case PRI_AOC_E_BILLING_ID_REVERSE:
3467 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3469 case PRI_AOC_E_BILLING_ID_CREDIT_CARD:
3470 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3472 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL:
3473 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL);
3475 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY:
3476 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_BUSY);
3478 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY:
3479 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_NO_REPLY);
3481 case PRI_AOC_E_BILLING_ID_CALL_DEFLECTION:
3482 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_DEFLECTION);
3484 case PRI_AOC_E_BILLING_ID_CALL_TRANSFER:
3485 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_TRANSFER);
3487 case PRI_AOC_E_BILLING_ID_NOT_AVAILABLE:
3489 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3493 switch (aoc_e->charge) {
3494 case PRI_AOC_DE_CHARGE_CURRENCY:
3495 ast_aoc_set_currency_info(decoded,
3496 aoc_e->recorded.money.amount.cost,
3497 sig_pri_aoc_multiplier_from_pri(aoc_e->recorded.money.amount.multiplier),
3498 aoc_e->recorded.money.currency);
3500 case PRI_AOC_DE_CHARGE_UNITS:
3503 for (i = 0; i < aoc_e->recorded.unit.num_items; ++i) {
3504 /* if type or number are negative, then they are not present */
3505 ast_aoc_add_unit_entry(decoded,
3506 (aoc_e->recorded.unit.item[i].number >= 0 ? 1 : 0),
3507 aoc_e->recorded.unit.item[i].number,
3508 (aoc_e->recorded.unit.item[i].type >= 0 ? 1 : 0),
3509 aoc_e->recorded.unit.item[i].type);
3514 if (passthrough && owner && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3515 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3518 ast_aoc_manager_event(decoded, owner);
3520 ast_aoc_destroy_decoded(decoded);
3521 ast_aoc_destroy_encoded(encoded);
3523 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3525 #if defined(HAVE_PRI_AOC_EVENTS)
3528 * \brief send an AOC-S message on the current call
3530 * \param pvt sig_pri private channel structure.
3531 * \param generic decoded ast AOC message
3535 * \note Assumes that the PRI lock is already obtained.
3537 static void sig_pri_aoc_s_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3539 struct pri_subcmd_aoc_s aoc_s = { 0, };
3540 const struct ast_aoc_s_entry *entry;
3543 for (idx = 0; idx < ast_aoc_s_get_count(decoded); idx++) {
3544 if (!(entry = ast_aoc_s_get_rate_info(decoded, idx))) {
3548 aoc_s.item[idx].chargeable = sig_pri_aoc_charged_item_to_pri(entry->charged_item);
3550 switch (entry->rate_type) {
3551 case AST_AOC_RATE_TYPE_DURATION:
3552 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_DURATION;
3553 aoc_s.item[idx].rate.duration.amount.cost = entry->rate.duration.amount;
3554 aoc_s.item[idx].rate.duration.amount.multiplier =
3555 sig_pri_aoc_multiplier_from_ast(entry->rate.duration.multiplier);
3556 aoc_s.item[idx].rate.duration.time.length = entry->rate.duration.time;
3557 aoc_s.item[idx].rate.duration.time.scale =
3558 sig_pri_aoc_scale_to_pri(entry->rate.duration.time_scale);
3559 aoc_s.item[idx].rate.duration.granularity.length = entry->rate.duration.granularity_time;
3560 aoc_s.item[idx].rate.duration.granularity.scale =
3561 sig_pri_aoc_scale_to_pri(entry->rate.duration.granularity_time_scale);
3562 aoc_s.item[idx].rate.duration.charging_type = entry->rate.duration.charging_type;
3564 if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
3565 ast_copy_string(aoc_s.item[idx].rate.duration.currency,
3566 entry->rate.duration.currency_name,
3567 sizeof(aoc_s.item[idx].rate.duration.currency));
3570 case AST_AOC_RATE_TYPE_FLAT:
3571 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FLAT;
3572 aoc_s.item[idx].rate.flat.amount.cost = entry->rate.flat.amount;
3573 aoc_s.item[idx].rate.flat.amount.multiplier =
3574 sig_pri_aoc_multiplier_from_ast(entry->rate.flat.multiplier);
3576 if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
3577 ast_copy_string(aoc_s.item[idx].rate.flat.currency,
3578 entry->rate.flat.currency_name,
3579 sizeof(aoc_s.item[idx].rate.flat.currency));
3582 case AST_AOC_RATE_TYPE_VOLUME:
3583 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_VOLUME;
3584 aoc_s.item[idx].rate.volume.unit = entry->rate.volume.volume_unit;
3585 aoc_s.item[idx].rate.volume.amount.cost = entry->rate.volume.amount;
3586 aoc_s.item[idx].rate.volume.amount.multiplier =
3587 sig_pri_aoc_multiplier_from_ast(entry->rate.volume.multiplier);
3589 if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
3590 ast_copy_string(aoc_s.item[idx].rate.volume.currency,
3591 entry->rate.volume.currency_name,
3592 sizeof(aoc_s.item[idx].rate.volume.currency));
3595 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
3596 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_SPECIAL_CODE;
3597 aoc_s.item[idx].rate.special = entry->rate.special_code;
3599 case AST_AOC_RATE_TYPE_FREE:
3600 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE;
3602 case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3603 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING;
3606 case AST_AOC_RATE_TYPE_NA:
3607 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_NOT_AVAILABLE;
3611 aoc_s.num_items = idx;
3613 /* if this rate should be sent as a response to an AOC-S request we will
3614 * have an aoc_s_request_invoke_id associated with this pvt */
3615 if (pvt->aoc_s_request_invoke_id_valid) {
3616 pri_aoc_s_request_response_send(pvt->pri->pri, pvt->call, pvt->aoc_s_request_invoke_id, &aoc_s);
3617 pvt->aoc_s_request_invoke_id_valid = 0;
3619 pri_aoc_s_send(pvt->pri->pri, pvt->call, &aoc_s);
3622 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3624 #if defined(HAVE_PRI_AOC_EVENTS)
3627 * \brief send an AOC-D message on the current call
3629 * \param pvt sig_pri private channel structure.
3630 * \param generic decoded ast AOC message
3634 * \note Assumes that the PRI lock is already obtained.
3636 static void sig_pri_aoc_d_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3638 struct pri_subcmd_aoc_d aoc_d = { 0, };
3640 aoc_d.billing_accumulation = (ast_aoc_get_total_type(decoded) == AST_AOC_TOTAL) ? 1 : 0;
3642 switch (ast_aoc_get_billing_id(decoded)) {
3643 case AST_AOC_BILLING_NORMAL:
3644 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NORMAL;
3646 case AST_AOC_BILLING_REVERSE_CHARGE:
3647 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_REVERSE;
3649 case AST_AOC_BILLING_CREDIT_CARD:
3650 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_CREDIT_CARD;
3652 case AST_AOC_BILLING_NA:
3654 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NOT_AVAILABLE;
3658 switch (ast_aoc_get_charge_type(decoded)) {
3659 case AST_AOC_CHARGE_FREE:
3660 aoc_d.charge = PRI_AOC_DE_CHARGE_FREE;
3662 case AST_AOC_CHARGE_CURRENCY:
3664 const char *currency_name = ast_aoc_get_currency_name(decoded);
3665 aoc_d.charge = PRI_AOC_DE_CHARGE_CURRENCY;
3666 aoc_d.recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3667 aoc_d.recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3668 if (!ast_strlen_zero(currency_name)) {
3669 ast_copy_string(aoc_d.recorded.money.currency, currency_name, sizeof(aoc_d.recorded.money.currency));
3673 case AST_AOC_CHARGE_UNIT:
3675 const struct ast_aoc_unit_entry *entry;
3677 aoc_d.charge = PRI_AOC_DE_CHARGE_UNITS;
3678 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3679 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_d.recorded.unit.item)) {
3680 if (entry->valid_amount) {
3681 aoc_d.recorded.unit.item[i].number = entry->amount;
3683 aoc_d.recorded.unit.item[i].number = -1;
3685 if (entry->valid_type) {
3686 aoc_d.recorded.unit.item[i].type = entry->type;
3688 aoc_d.recorded.unit.item[i].type = -1;
3690 aoc_d.recorded.unit.num_items++;
3697 case AST_AOC_CHARGE_NA:
3699 aoc_d.charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
3703 pri_aoc_d_send(pvt->pri->pri, pvt->call, &aoc_d);
3705 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3707 #if defined(HAVE_PRI_AOC_EVENTS)
3710 * \brief send an AOC-E message on the current call
3712 * \param pvt sig_pri private channel structure.
3713 * \param generic decoded ast AOC message
3717 * \note Assumes that the PRI lock is already obtained.
3719 static void sig_pri_aoc_e_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3721 struct pri_subcmd_aoc_e *aoc_e = &pvt->aoc_e;
3722 const struct ast_aoc_charging_association *ca = ast_aoc_get_association_info(decoded);
3724 memset(aoc_e, 0, sizeof(*aoc_e));
3725 pvt->holding_aoce = 1;
3727 switch (ca->charging_type) {
3728 case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
3729 aoc_e->associated.charge.number.valid = 1;
3730 ast_copy_string(aoc_e->associated.charge.number.str,
3731 ca->charge.number.number,
3732 sizeof(aoc_e->associated.charge.number.str));
3733 aoc_e->associated.charge.number.plan = ca->charge.number.plan;
3734 aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER;
3736 case AST_AOC_CHARGING_ASSOCIATION_ID:
3737 aoc_e->associated.charge.id = ca->charge.id;
3738 aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_ID;
3740 case AST_AOC_CHARGING_ASSOCIATION_NA:
3745 switch (ast_aoc_get_billing_id(decoded)) {
3746 case AST_AOC_BILLING_NORMAL:
3747 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NORMAL;
3749 case AST_AOC_BILLING_REVERSE_CHARGE:
3750 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_REVERSE;
3752 case AST_AOC_BILLING_CREDIT_CARD:
3753 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CREDIT_CARD;
3755 case AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL:
3756 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL;
3758 case AST_AOC_BILLING_CALL_FWD_BUSY:
3759 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY;
3761 case AST_AOC_BILLING_CALL_FWD_NO_REPLY:
3762 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY;
3764 case AST_AOC_BILLING_CALL_DEFLECTION:
3765 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_DEFLECTION;
3767 case AST_AOC_BILLING_CALL_TRANSFER:
3768 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_TRANSFER;
3770 case AST_AOC_BILLING_NA:
3772 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NOT_AVAILABLE;
3776 switch (ast_aoc_get_charge_type(decoded)) {
3777 case AST_AOC_CHARGE_FREE:
3778 aoc_e->charge = PRI_AOC_DE_CHARGE_FREE;
3780 case AST_AOC_CHARGE_CURRENCY:
3782 const char *currency_name = ast_aoc_get_currency_name(decoded);
3783 aoc_e->charge = PRI_AOC_DE_CHARGE_CURRENCY;
3784 aoc_e->recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3785 aoc_e->recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3786 if (!ast_strlen_zero(currency_name)) {
3787 ast_copy_string(aoc_e->recorded.money.currency, currency_name, sizeof(aoc_e->recorded.money.currency));
3791 case AST_AOC_CHARGE_UNIT:
3793 const struct ast_aoc_unit_entry *entry;
3795 aoc_e->charge = PRI_AOC_DE_CHARGE_UNITS;
3796 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3797 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_e->recorded.unit.item)) {
3798 if (entry->valid_amount) {
3799 aoc_e->recorded.unit.item[i].number = entry->amount;
3801 aoc_e->recorded.unit.item[i].number = -1;
3803 if (entry->valid_type) {
3804 aoc_e->recorded.unit.item[i].type = entry->type;
3806 aoc_e->recorded.unit.item[i].type = -1;
3808 aoc_e->recorded.unit.num_items++;
3813 case AST_AOC_CHARGE_NA:
3815 aoc_e->charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
3819 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3821 #if defined(HAVE_PRI_AOC_EVENTS)
3824 * \brief send an AOC-E termination request on ast_channel and set
3827 * \param pri PRI span control structure.
3828 * \param chanpos Channel position in the span.
3829 * \param ms to delay hangup
3831 * \note Assumes the pri->lock is already obtained.
3832 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
3836 static void sig_pri_send_aoce_termination_request(struct sig_pri_span *pri, int chanpos, unsigned int ms)
3838 struct sig_pri_chan *pvt;
3839 struct ast_aoc_decoded *decoded = NULL;
3840 struct ast_aoc_encoded *encoded = NULL;
3841 size_t encoded_size;
3842 struct timeval whentohangup = { 0, };
3844 sig_pri_lock_owner(pri, chanpos);
3845 pvt = pri->pvts[chanpos];
3850 if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, AST_AOC_REQUEST_E))) {
3851 ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
3852 goto cleanup_termination_request;
3855 ast_aoc_set_termination_request(decoded);
3857 if (!(encoded = ast_aoc_encode(decoded, &encoded_size, pvt->owner))) {
3858 ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
3859 goto cleanup_termination_request;
3862 /* convert ms to timeval */
3863 whentohangup.tv_usec = (ms % 1000) * 1000;
3864 whentohangup.tv_sec = ms / 1000;
3866 if (ast_queue_control_data(pvt->owner, AST_CONTROL_AOC, encoded, encoded_size)) {
3867 ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
3868 goto cleanup_termination_request;
3871 pvt->waiting_for_aoce = 1;
3872 ast_channel_setwhentohangup_tv(pvt->owner, whentohangup);
3873 ast_debug(1, "Delaying hangup on %s for aoc-e msg\n", ast_channel_name(pvt->owner));
3875 cleanup_termination_request: