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>
35 #include "asterisk/utils.h"
36 #include "asterisk/options.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/app.h"
39 #include "asterisk/file.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/say.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/astdb.h"
44 #include "asterisk/causes.h"
45 #include "asterisk/musiconhold.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/transcap.h"
48 #include "asterisk/features.h"
51 #ifndef PRI_EVENT_FACILITY
52 #error please update libpri
55 /* define this to send PRI user-user information elements */
56 #undef SUPPORT_USERUSER
59 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
61 #define DEFAULT_PRI_DEBUG 0
64 static int pri_matchdigittimeout = 3000;
66 static int pri_gendigittimeout = 8000;
68 #define DCHAN_NOTINALARM (1 << 0)
69 #define DCHAN_UP (1 << 1)
71 /* Defines to help decode the encoded event channel id. */
72 #define PRI_CHANNEL(p) ((p) & 0xff)
73 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
74 #define PRI_EXPLICIT (1 << 16)
75 #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
76 #define PRI_HELD_CALL (1 << 18)
79 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
81 #define PRI_DEADLOCK_AVOIDANCE(p) \
83 sig_pri_unlock_private(p); \
85 sig_pri_lock_private(p); \
88 static int pri_active_dchan_index(struct sig_pri_pri *pri);
90 static inline void pri_rel(struct sig_pri_pri *pri)
92 ast_mutex_unlock(&pri->lock);
95 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
97 int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
98 ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
99 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
104 static void sig_pri_handle_dchan_exception(struct sig_pri_pri *pri, int index)
106 if (pri->calls->handle_dchan_exception)
107 pri->calls->handle_dchan_exception(pri, index);
110 static void sig_pri_set_dialing(struct sig_pri_chan *p, int flag)
112 if (p->calls->set_dialing)
113 p->calls->set_dialing(p->chan_pvt, flag);
116 static void sig_pri_set_digital(struct sig_pri_chan *p, int flag)
119 if (p->calls->set_digital)
120 p->calls->set_digital(p->chan_pvt, flag);
125 * \brief Set the caller id information in the parent module.
128 * \param p sig_pri channel structure.
132 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
134 struct ast_party_caller caller;
136 if (p->calls->set_callerid) {
137 ast_party_caller_init(&caller);
138 caller.id.number = p->cid_num;
139 caller.id.name = p->cid_name;
140 if (!ast_strlen_zero(p->cid_subaddr)) {
141 caller.id.subaddress.valid = 1;
142 //caller.id.subaddress.type = 0;/* nsap */
143 //caller.id.subaddress.odd_even_indicator = 0;
144 caller.id.subaddress.str = p->cid_subaddr;
146 caller.id.number_type = p->cid_ton;
147 caller.id.number_presentation = p->callingpres;
148 caller.ani = p->cid_ani;
149 caller.ani2 = p->cid_ani2;
150 p->calls->set_callerid(p->chan_pvt, &caller);
156 * \brief Set the Dialed Number Identifier.
159 * \param p sig_pri channel structure.
160 * \param dnid Dialed Number Identifier string.
164 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
166 if (p->calls->set_dnid) {
167 p->calls->set_dnid(p->chan_pvt, dnid);
173 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
176 * \param p sig_pri channel structure.
177 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
181 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
183 if (p->calls->set_rdnis) {
184 p->calls->set_rdnis(p->chan_pvt, rdnis);
188 static void sig_pri_unlock_private(struct sig_pri_chan *p)
190 if (p->calls->unlock_private)
191 p->calls->unlock_private(p->chan_pvt);
194 static void sig_pri_lock_private(struct sig_pri_chan *p)
196 if (p->calls->lock_private)
197 p->calls->lock_private(p->chan_pvt);
200 static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_pri *pri)
203 /* Grab the lock first */
205 res = ast_mutex_trylock(&pri->lock);
207 PRI_DEADLOCK_AVOIDANCE(p);
210 /* Then break the poll */
211 pthread_kill(pri->master, SIGURG);
217 * \brief Convert PRI redirecting reason to asterisk version.
220 * \param pri_reason PRI redirecting reason.
222 * \return Equivalent asterisk redirecting reason value.
224 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
226 enum AST_REDIRECTING_REASON ast_reason;
228 switch (pri_reason) {
229 case PRI_REDIR_FORWARD_ON_BUSY:
230 ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
232 case PRI_REDIR_FORWARD_ON_NO_REPLY:
233 ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
235 case PRI_REDIR_DEFLECTION:
236 ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
238 case PRI_REDIR_UNCONDITIONAL:
239 ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
241 case PRI_REDIR_UNKNOWN:
243 ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
252 * \brief Convert asterisk redirecting reason to PRI version.
255 * \param ast_reason Asterisk redirecting reason.
257 * \return Equivalent PRI redirecting reason value.
259 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
263 switch (ast_reason) {
264 case AST_REDIRECTING_REASON_USER_BUSY:
265 pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
267 case AST_REDIRECTING_REASON_NO_ANSWER:
268 pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
270 case AST_REDIRECTING_REASON_UNCONDITIONAL:
271 pri_reason = PRI_REDIR_UNCONDITIONAL;
273 case AST_REDIRECTING_REASON_DEFLECTION:
274 pri_reason = PRI_REDIR_DEFLECTION;
276 case AST_REDIRECTING_REASON_UNKNOWN:
278 pri_reason = PRI_REDIR_UNKNOWN;
287 * \brief Convert PRI number presentation to asterisk version.
290 * \param pri_presentation PRI number presentation.
292 * \return Equivalent asterisk number presentation value.
294 static int pri_to_ast_presentation(int pri_presentation)
296 int ast_presentation;
298 switch (pri_presentation) {
299 case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
300 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
302 case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
303 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
305 case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
306 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
308 case PRES_ALLOWED_NETWORK_NUMBER:
309 ast_presentation = AST_PRES_ALLOWED_NETWORK_NUMBER;
311 case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
312 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
314 case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
315 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
317 case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
318 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
320 case PRES_PROHIB_NETWORK_NUMBER:
321 ast_presentation = AST_PRES_PROHIB_NETWORK_NUMBER;
323 case PRES_NUMBER_NOT_AVAILABLE:
324 ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
327 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
331 return ast_presentation;
336 * \brief Convert asterisk number presentation to PRI version.
339 * \param ast_presentation Asterisk number presentation.
341 * \return Equivalent PRI number presentation value.
343 static int ast_to_pri_presentation(int ast_presentation)
345 int pri_presentation;
347 switch (ast_presentation) {
348 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
349 pri_presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
351 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
352 pri_presentation = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
354 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
355 pri_presentation = PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
357 case AST_PRES_ALLOWED_NETWORK_NUMBER:
358 pri_presentation = PRES_ALLOWED_NETWORK_NUMBER;
360 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
361 pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
363 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
364 pri_presentation = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
366 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
367 pri_presentation = PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
369 case AST_PRES_PROHIB_NETWORK_NUMBER:
370 pri_presentation = PRES_PROHIB_NETWORK_NUMBER;
372 case AST_PRES_NUMBER_NOT_AVAILABLE:
373 pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
376 pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
380 return pri_presentation;
385 * \brief Determine the overall presentation value for the given party.
388 * \param id Party to determine the overall presentation value.
390 * \return Overall presentation value for the given party converted to ast values.
392 static int overall_ast_presentation(const struct pri_party_id *id)
396 int number_screening;
400 /* Determine name presentation priority. */
401 if (!id->name.valid) {
402 name_value = PRI_PRES_UNAVAILABLE;
405 name_value = id->name.presentation & PRI_PRES_RESTRICTION;
406 switch (name_value) {
407 case PRI_PRES_RESTRICTED:
410 case PRI_PRES_ALLOWED:
413 case PRI_PRES_UNAVAILABLE:
417 name_value = PRI_PRES_UNAVAILABLE;
423 /* Determine number presentation priority. */
424 if (!id->number.valid) {
425 number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
426 number_value = PRI_PRES_UNAVAILABLE;
429 number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
430 number_value = id->number.presentation & PRI_PRES_RESTRICTION;
431 switch (number_value) {
432 case PRI_PRES_RESTRICTED:
435 case PRI_PRES_ALLOWED:
438 case PRI_PRES_UNAVAILABLE:
442 number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
443 number_value = PRI_PRES_UNAVAILABLE;
449 /* Select the wining presentation value. */
450 if (name_priority < number_priority) {
451 number_value = name_value;
454 return pri_to_ast_presentation(number_value | number_screening);
457 #if defined(HAVE_PRI_SUBADDR)
460 * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
463 * \param ast_subaddress Asterisk party subaddress structure.
464 * \param pri_subaddress PRI party subaddress structure.
469 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
474 if (ast_subaddress->str) {
475 ast_free(ast_subaddress->str);
477 if (pri_subaddress->length <= 0) {
478 ast_party_subaddress_init(ast_subaddress);
482 if (!pri_subaddress->type) {
484 ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
487 if (!(cnum = ast_malloc(2 * pri_subaddress->length + 1))) {
488 ast_party_subaddress_init(ast_subaddress);
493 len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
494 for (x = 0; x < len; ++x) {
495 ptr += sprintf(ptr, "%02x", pri_subaddress->data[x]);
498 if (pri_subaddress->odd_even_indicator) {
500 sprintf(ptr, "%01x", (pri_subaddress->data[len]) >> 4);
503 sprintf(ptr, "%02x", pri_subaddress->data[len]);
505 ast_subaddress->str = cnum;
507 ast_subaddress->type = pri_subaddress->type;
508 ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
509 ast_subaddress->valid = 1;
511 #endif /* defined(HAVE_PRI_SUBADDR) */
513 #if defined(HAVE_PRI_SUBADDR)
514 static unsigned char ast_pri_pack_hex_char(char c)
520 } else if (c < ('9' + 1)) {
522 } else if (c < 'A') {
524 } else if (c < ('F' + 1)) {
526 } else if (c < 'a') {
528 } else if (c < ('f' + 1)) {
535 #endif /* defined(HAVE_PRI_SUBADDR) */
537 #if defined(HAVE_PRI_SUBADDR)
540 * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
541 * \details left justified, with 0 padding if odd length.
544 * \param dst pointer to packed byte array.
545 * \param src pointer to null terminated hexadecimal string.
546 * \param maxlen destination array size.
548 * \return Length of byte array
550 * \note The dst is not an ASCIIz string.
551 * \note The src is an ASCIIz hex string.
553 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
556 int len = strlen(src);
558 if (len > (2 * maxlen)) {
562 res = len / 2 + len % 2;
565 *dst = ast_pri_pack_hex_char(*src) << 4;
567 *dst |= ast_pri_pack_hex_char(*src);
571 if (len) { /* 1 left */
572 *dst = ast_pri_pack_hex_char(*src) << 4;
576 #endif /* defined(HAVE_PRI_SUBADDR) */
578 #if defined(HAVE_PRI_SUBADDR)
581 * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
584 * \param pri_subaddress PRI party subaddress structure.
585 * \param ast_subaddress Asterisk party subaddress structure.
589 * \note Assumes that pri_subaddress has been previously memset to zero.
591 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
593 if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
594 pri_subaddress->type = ast_subaddress->type;
595 if (!ast_subaddress->type) {
597 ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
598 sizeof(pri_subaddress->data));
599 pri_subaddress->length = strlen((char *) pri_subaddress->data);
600 pri_subaddress->odd_even_indicator = 0;
601 pri_subaddress->valid = 1;
603 /* 2 = User Specified */
605 * Copy HexString to packed HexData,
606 * if odd length then right pad trailing byte with 0
608 int length = ast_pri_pack_hex_string(pri_subaddress->data,
609 ast_subaddress->str, sizeof(pri_subaddress->data));
611 pri_subaddress->length = length;
612 pri_subaddress->odd_even_indicator = (length & 1);
613 pri_subaddress->valid = 1;
617 #endif /* defined(HAVE_PRI_SUBADDR) */
621 * \brief Fill in the PRI party id from the given asterisk party id.
624 * \param pri_id PRI party id structure.
625 * \param ast_id Asterisk party id structure.
629 * \note Assumes that pri_id has been previously memset to zero.
631 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
635 presentation = ast_to_pri_presentation(ast_id->number_presentation);
636 if (!ast_strlen_zero(ast_id->name)) {
637 pri_id->name.valid = 1;
638 pri_id->name.presentation = presentation;
639 pri_id->name.char_set = PRI_CHAR_SET_ISO8859_1;
640 ast_copy_string(pri_id->name.str, ast_id->name, sizeof(pri_id->name.str));
642 if (!ast_strlen_zero(ast_id->number)) {
643 pri_id->number.valid = 1;
644 pri_id->number.presentation = presentation;
645 pri_id->number.plan = ast_id->number_type;
646 ast_copy_string(pri_id->number.str, ast_id->number, sizeof(pri_id->number.str));
648 #if defined(HAVE_PRI_SUBADDR)
649 sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
650 #endif /* defined(HAVE_PRI_SUBADDR) */
655 * \brief Update the PRI redirecting information for the current call.
658 * \param pvt sig_pri private channel structure.
659 * \param ast Asterisk channel
663 * \note Assumes that the PRI lock is already obtained.
665 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
667 struct pri_party_redirecting pri_redirecting;
668 struct ast_party_redirecting ast_redirecting;
670 /* Gather asterisk redirecting data */
671 ast_redirecting = ast->redirecting;
672 ast_redirecting.from.number = ast->cid.cid_rdnis;
674 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
676 memset(&pri_redirecting, 0, sizeof(pri_redirecting));
677 sig_pri_party_id_from_ast(&pri_redirecting.from, &ast_redirecting.from);
678 sig_pri_party_id_from_ast(&pri_redirecting.to, &ast_redirecting.to);
679 pri_redirecting.count = ast_redirecting.count;
680 pri_redirecting.reason = ast_to_pri_reason(ast_redirecting.reason);
682 pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
687 * \brief Reset DTMF detector.
690 * \param p sig_pri channel structure.
694 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
696 if (p->calls->dsp_reset_and_flush_digits) {
697 p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
701 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
703 if (p->calls->set_echocanceller)
704 return p->calls->set_echocanceller(p->chan_pvt, enable);
709 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
711 if (old_chan->calls->fixup_chans)
712 old_chan->calls->fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
715 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
717 if (p->calls->play_tone)
718 return p->calls->play_tone(p->chan_pvt, tone);
723 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state, int startpbx, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
725 struct ast_channel *c;
727 if (p->calls->new_ast_channel)
728 c = p->calls->new_ast_channel(p->chan_pvt, state, startpbx, ulaw, transfercapability, exten, requestor);
735 p->alreadyhungup = 0;
736 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
737 c->transfercapability = transfercapability;
738 pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
739 sig_pri_set_digital(p, 1);
745 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability)
747 struct ast_channel *ast;
749 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
752 ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, 0, law, transfercapability, p->exten, requestor);
759 int pri_is_up(struct sig_pri_pri *pri)
762 for (x = 0; x < NUM_DCHANS; x++) {
763 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
769 static char *pri_order(int level)
785 /* Returns index of the active dchan */
786 static int pri_active_dchan_index(struct sig_pri_pri *pri)
790 for (x = 0; x < NUM_DCHANS; x++) {
791 if ((pri->dchans[x] == pri->pri))
795 ast_log(LOG_WARNING, "No active dchan found!\n");
799 static int pri_find_dchan(struct sig_pri_pri *pri)
806 for (x = 0; x < NUM_DCHANS; x++) {
807 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
809 if (pri->dchans[x] == old) {
815 /* This is annoying to see on non persistent layer 2 connections. Let's not complain in that case */
816 if (pri->sig != SIG_BRI_PTMP) {
817 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel as D-channel anyway!\n");
820 if (old && (oldslot != newslot))
821 ast_log(LOG_NOTICE, "Switching from d-channel fd %d to fd %d!\n",
822 pri->fds[oldslot], pri->fds[newslot]);
823 pri->pri = pri->dchans[newslot];
827 static void pri_queue_frame(struct sig_pri_chan *p, struct ast_frame *f, struct sig_pri_pri *pri)
829 /* We must unlock the PRI to avoid the possibility of a deadlock */
831 ast_mutex_unlock(&pri->lock);
834 if (ast_channel_trylock(p->owner)) {
835 PRI_DEADLOCK_AVOIDANCE(p);
837 ast_queue_frame(p->owner, f);
838 ast_channel_unlock(p->owner);
845 ast_mutex_lock(&pri->lock);
848 static void pri_queue_control(struct sig_pri_chan *p, int subclass, struct sig_pri_pri *pri)
850 struct ast_frame f = {AST_FRAME_CONTROL, };
852 if (p->calls->queue_control) {
853 p->calls->queue_control(p->chan_pvt, subclass);
856 f.subclass.integer = subclass;
857 pri_queue_frame(p, &f, pri);
860 static int pri_find_principle(struct sig_pri_pri *pri, int channel, q931_call *call)
867 /* Channel is not picked yet. */
871 if (channel & PRI_HELD_CALL) {
873 /* Cannot find a held call without a call. */
877 for (x = 0; x < pri->numchans; ++x) {
879 && pri->pvts[x]->call == call) {
887 span = PRI_SPAN(channel);
888 if (!(channel & PRI_EXPLICIT)) {
891 index = pri_active_dchan_index(pri);
895 span = pri->dchan_logical_span[index];
898 channel = PRI_CHANNEL(channel);
900 for (x = 0; x < pri->numchans; x++) {
902 && pri->pvts[x]->prioffset == channel
903 && pri->pvts[x]->logicalspan == span
904 && !pri->pvts[x]->no_b_channel) {
913 static int pri_fixup_principle(struct sig_pri_pri *pri, int principle, q931_call *call)
917 if (principle < 0 || pri->numchans <= principle) {
925 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
926 /* Call is already on the specified principle. */
930 /* Find the old principle location. */
931 for (x = 0; x < pri->numchans; x++) {
932 struct sig_pri_chan *new_chan;
933 struct sig_pri_chan *old_chan;
935 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
940 new_chan = pri->pvts[principle];
941 old_chan = pri->pvts[x];
943 ast_verb(3, "Moving call from channel %d to channel %d\n",
944 old_chan->channel, new_chan->channel);
945 if (new_chan->owner) {
947 "Can't fix up channel from %d to %d because %d is already in use\n",
948 old_chan->channel, new_chan->channel, new_chan->channel);
952 sig_pri_fixup_chans(old_chan, new_chan);
954 /* Fix it all up now */
955 new_chan->owner = old_chan->owner;
956 old_chan->owner = NULL;
958 new_chan->call = old_chan->call;
959 old_chan->call = NULL;
961 /* Transfer flags from the old channel. */
962 new_chan->alerting = old_chan->alerting;
963 new_chan->alreadyhungup = old_chan->alreadyhungup;
964 new_chan->isidlecall = old_chan->isidlecall;
965 new_chan->proceeding = old_chan->proceeding;
966 new_chan->progress = old_chan->progress;
967 new_chan->setup_ack = old_chan->setup_ack;
968 new_chan->outgoing = old_chan->outgoing;
969 new_chan->digital = old_chan->digital;
970 old_chan->alerting = 0;
971 old_chan->alreadyhungup = 0;
972 old_chan->isidlecall = 0;
973 old_chan->proceeding = 0;
974 old_chan->progress = 0;
975 old_chan->setup_ack = 0;
976 old_chan->outgoing = 0;
977 old_chan->digital = 0;
979 /* More stuff to transfer to the new channel. */
980 #if defined(HAVE_PRI_REVERSE_CHARGE)
981 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
982 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
983 #if defined(HAVE_PRI_SETUP_KEYPAD)
984 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
985 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
987 if (new_chan->no_b_channel) {
988 /* Copy the real channel configuration to the no B channel interface. */
989 new_chan->hidecallerid = old_chan->hidecallerid;
990 new_chan->hidecalleridname = old_chan->hidecalleridname;
991 new_chan->immediate = old_chan->immediate;
992 new_chan->priexclusive = old_chan->priexclusive;
993 new_chan->priindication_oob = old_chan->priindication_oob;
994 new_chan->use_callerid = old_chan->use_callerid;
995 new_chan->use_callingpres = old_chan->use_callingpres;
996 new_chan->stripmsd = old_chan->stripmsd;
997 strcpy(new_chan->context, old_chan->context);
998 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1000 /* Become a member of the old channel span/trunk-group. */
1001 new_chan->logicalspan = old_chan->logicalspan;
1002 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1007 ast_log(LOG_WARNING, "Call specified, but not found?\n");
1011 static char * redirectingreason2str(int redirectingreason)
1013 switch (redirectingreason) {
1021 return "UNCONDITIONAL";
1023 return "NOREDIRECT";
1027 static char *dialplan2str(int dialplan)
1029 if (dialplan == -1) {
1030 return("Dynamically set dialplan in ISDN");
1032 return (pri_plan2str(dialplan));
1035 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_pri *pri, const char *number, const int plan)
1038 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1039 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1041 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1042 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1044 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1045 snprintf(buf, size, "%s%s", pri->localprefix, number);
1047 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1048 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1050 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1051 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1053 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1054 snprintf(buf, size, "%s", number);
1059 /*! \note Assumes the pri->lock is already obtained. */
1060 static int pri_check_restart(struct sig_pri_pri *pri)
1062 #ifdef HAVE_PRI_SERVICE_MESSAGES
1067 } while (pri->resetpos < pri->numchans
1068 && (!pri->pvts[pri->resetpos]
1069 || pri->pvts[pri->resetpos]->no_b_channel
1070 || pri->pvts[pri->resetpos]->call
1071 || pri->pvts[pri->resetpos]->resetting));
1072 if (pri->resetpos < pri->numchans) {
1073 #ifdef HAVE_PRI_SERVICE_MESSAGES
1074 char db_chan_name[20], db_answer[5], state;
1077 /* check if the channel is out of service */
1078 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, pri->pvts[pri->resetpos]->channel);
1080 /* if so, try next channel */
1081 if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
1082 sscanf(db_answer, "%1c:%30d", &state, &why);
1084 ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), not sending RESTART\n", pri->span,
1085 pri->pvts[pri->resetpos]->channel, (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1091 /* Mark the channel as resetting and restart it */
1092 pri->pvts[pri->resetpos]->resetting = 1;
1093 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1096 time(&pri->lastreset);
1101 static int pri_find_empty_chan(struct sig_pri_pri *pri, int backwards)
1109 if (backwards && (x < 0))
1111 if (!backwards && (x >= pri->numchans))
1114 && !pri->pvts[x]->no_b_channel
1115 && !pri->pvts[x]->inalarm
1116 && !pri->pvts[x]->owner) {
1117 ast_debug(1, "Found empty available channel %d/%d\n",
1118 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1129 #if defined(HAVE_PRI_CALL_HOLD)
1132 * \brief Find or create an empty no-B-channel interface to use.
1135 * \param pri sig_pri span controller to find interface.
1137 * \note Assumes the pri->lock is already obtained.
1139 * \retval array-index into private pointer array on success.
1140 * \retval -1 on error.
1142 static int pri_find_empty_nobch(struct sig_pri_pri *pri)
1146 for (idx = 0; idx < pri->numchans; ++idx) {
1148 && pri->pvts[idx]->no_b_channel
1149 && !pri->pvts[idx]->inalarm
1150 && !pri->pvts[idx]->owner) {
1151 ast_debug(1, "Found empty available no B channel interface\n");
1156 /* Need to create a new interface. */
1157 if (pri->calls->new_nobch_intf) {
1158 idx = pri->calls->new_nobch_intf(pri);
1164 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1166 #if defined(HAVE_PRI_CALL_HOLD)
1169 * \brief Find the channel associated with the libpri call.
1172 * \param pri sig_pri span controller to find interface.
1173 * \param call LibPRI opaque call pointer to find.
1175 * \note Assumes the pri->lock is already obtained.
1177 * \retval array-index into private pointer array on success.
1178 * \retval -1 on error.
1180 static int pri_find_pri_call(struct sig_pri_pri *pri, q931_call *call)
1184 for (idx = 0; idx < pri->numchans; ++idx) {
1185 if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
1186 /* Found the channel */
1192 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1194 static void *do_idle_thread(void *v_pvt)
1196 struct sig_pri_chan *pvt = v_pvt;
1197 struct ast_channel *chan = pvt->owner;
1198 struct ast_frame *f;
1200 /* Wait up to 30 seconds for an answer */
1201 int newms, ms = 30000;
1203 ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
1204 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
1205 if (ast_call(chan, ex, 0)) {
1206 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
1210 while ((newms = ast_waitfor(chan, ms)) > 0) {
1216 if (f->frametype == AST_FRAME_CONTROL) {
1217 switch (f->subclass.integer) {
1218 case AST_CONTROL_ANSWER:
1219 /* Launch the PBX */
1220 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
1221 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
1223 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
1225 /* It's already hungup, return immediately */
1227 case AST_CONTROL_BUSY:
1228 ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
1230 case AST_CONTROL_CONGESTION:
1231 ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
1238 /* Hangup the channel since nothing happend */
1243 static void *pri_ss_thread(void *data)
1245 struct sig_pri_chan *p = data;
1246 struct ast_channel *chan = p->owner;
1247 char exten[AST_MAX_EXTENSION];
1253 /* We lost the owner before we could get started. */
1258 * In the bizarre case where the channel has become a zombie before we
1259 * even get started here, abort safely.
1261 if (!chan->tech_pvt) {
1262 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
1267 ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
1269 sig_pri_dsp_reset_and_flush_digits(p);
1271 /* Now loop looking for an extension */
1272 ast_copy_string(exten, p->exten, sizeof(exten));
1273 len = strlen(exten);
1275 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
1276 if (len && !ast_ignore_pattern(chan->context, exten))
1277 sig_pri_play_tone(p, -1);
1279 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
1280 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
1281 timeout = pri_matchdigittimeout;
1283 timeout = pri_gendigittimeout;
1284 res = ast_waitfordigit(chan, timeout);
1286 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
1295 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
1296 if (ast_strlen_zero(exten)) {
1297 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
1301 if (chan->cid.cid_dnid) {
1302 ast_free(chan->cid.cid_dnid);
1304 chan->cid.cid_dnid = ast_strdup(exten);
1306 sig_pri_play_tone(p, -1);
1307 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
1308 /* Start the real PBX */
1309 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1310 sig_pri_dsp_reset_and_flush_digits(p);
1311 sig_pri_set_echocanceller(p, 1);
1312 ast_setstate(chan, AST_STATE_RING);
1313 res = ast_pbx_run(chan);
1315 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
1318 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
1319 chan->hangupcause = AST_CAUSE_UNALLOCATED;
1322 /* Since we send release complete here, we won't get one */
1328 res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
1330 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
1335 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri)
1337 pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
1338 if (!before_start_pri)
1339 pri_find_dchan(pri);
1342 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
1344 pri->dchanavail[index] |= DCHAN_NOTINALARM;
1345 if (!before_start_pri)
1346 pri_restart(pri->dchans[index]);
1351 * \brief Convert libpri party id into asterisk party id.
1354 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
1355 * \param pri_id libpri party id structure containing source information.
1356 * \param pri Span controlling structure.
1358 * \note The filled in ast_id structure needs to be destroyed by
1359 * ast_party_id_free() when it is no longer needed.
1363 static void sig_pri_party_id_convert(struct ast_party_id *ast_id,
1364 const struct pri_party_id *pri_id, struct sig_pri_pri *pri)
1366 char number[AST_MAX_EXTENSION];
1368 if (pri_id->name.valid) {
1369 ast_id->name = ast_strdup(pri_id->name.str);
1371 if (pri_id->number.valid) {
1372 apply_plan_to_number(number, sizeof(number), pri, pri_id->number.str,
1373 pri_id->number.plan);
1374 ast_id->number = ast_strdup(number);
1375 ast_id->number_type = pri_id->number.plan;
1377 if (pri_id->name.valid || pri_id->number.valid) {
1378 ast_id->number_presentation = overall_ast_presentation(pri_id);
1380 #if defined(HAVE_PRI_SUBADDR)
1381 if (pri_id->subaddress.valid) {
1382 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
1384 #endif /* defined(HAVE_PRI_SUBADDR) */
1389 * \brief Convert libpri redirecting information into asterisk redirecting information.
1392 * \param ast_redirecting Asterisk redirecting structure to fill.
1393 * \param pri_redirecting libpri redirecting structure containing source information.
1394 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
1395 * \param pri Span controlling structure.
1397 * \note The filled in ast_redirecting structure needs to be destroyed by
1398 * ast_party_redirecting_free() when it is no longer needed.
1402 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
1403 const struct pri_party_redirecting *pri_redirecting,
1404 const struct ast_party_redirecting *ast_guide,
1405 struct sig_pri_pri *pri)
1407 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
1409 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
1410 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
1411 ast_redirecting->count = pri_redirecting->count;
1412 ast_redirecting->reason = pri_to_ast_reason(pri_redirecting->reason);
1417 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
1420 * \param msn_patterns Comma separated list of MSN patterns to match.
1421 * \param exten Extension to match in the MSN list.
1423 * \retval 1 if matches.
1424 * \retval 0 if no match.
1426 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
1432 msn_list = strdupa(msn_patterns);
1435 pattern = strtok_r(msn_list, ",", &list_tail);
1437 pattern = ast_strip(pattern);
1438 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
1439 /* Extension matched the pattern. */
1442 pattern = strtok_r(NULL, ",", &list_tail);
1444 /* Did not match any pattern in the list. */
1450 * \brief Obtain the sig_pri owner channel lock if the owner exists.
1453 * \param pri sig_pri PRI control structure.
1454 * \param chanpos Channel position in the span.
1456 * \note Assumes the pri->lock is already obtained.
1457 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1461 static void sig_pri_lock_owner(struct sig_pri_pri *pri, int chanpos)
1464 if (!pri->pvts[chanpos]->owner) {
1465 /* There is no owner lock to get. */
1468 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
1469 /* We got the lock */
1472 /* We must unlock the PRI to avoid the possibility of a deadlock */
1473 ast_mutex_unlock(&pri->lock);
1474 PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
1475 ast_mutex_lock(&pri->lock);
1481 * \brief Handle the call associated PRI subcommand events.
1484 * \param pri sig_pri PRI control structure.
1485 * \param chanpos Channel position in the span.
1486 * \param event_id PRI event id
1487 * \param channel PRI encoded span/channel
1488 * \param subcmds Subcommands to process if any. (Could be NULL).
1489 * \param call_rsp libpri opaque call structure to send any responses toward.
1490 * Could be NULL either because it is not available or the call is for the
1491 * dummy call reference. However, this should not be NULL in the cases that
1492 * need to use the pointer to send a response message back.
1494 * \note Assumes the pri->lock is already obtained.
1495 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1499 static void sig_pri_handle_subcmds(struct sig_pri_pri *pri, int chanpos, int event_id,
1500 int channel, const struct pri_subcommands *subcmds, q931_call *call_rsp)
1503 struct ast_channel *owner;
1504 struct ast_party_redirecting ast_redirecting;
1509 for (index = 0; index < subcmds->counter_subcmd; ++index) {
1510 const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
1512 switch (subcmd->cmd) {
1513 case PRI_SUBCMD_CONNECTED_LINE:
1514 sig_pri_lock_owner(pri, chanpos);
1515 owner = pri->pvts[chanpos]->owner;
1517 struct ast_party_connected_line ast_connected;
1518 int caller_id_update;
1520 /* Extract the connected line information */
1521 ast_party_connected_line_init(&ast_connected);
1522 sig_pri_party_id_convert(&ast_connected.id, &subcmd->u.connected_line.id,
1525 caller_id_update = 0;
1526 if (ast_connected.id.name) {
1527 /* Save name for Caller-ID update */
1528 ast_copy_string(pri->pvts[chanpos]->cid_name,
1529 ast_connected.id.name, sizeof(pri->pvts[chanpos]->cid_name));
1530 caller_id_update = 1;
1532 if (ast_connected.id.number) {
1533 /* Save number for Caller-ID update */
1534 ast_copy_string(pri->pvts[chanpos]->cid_num, ast_connected.id.number,
1535 sizeof(pri->pvts[chanpos]->cid_num));
1536 pri->pvts[chanpos]->cid_ton = ast_connected.id.number_type;
1537 caller_id_update = 1;
1539 ast_connected.id.number = ast_strdup("");
1541 ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
1543 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
1544 #if defined(HAVE_PRI_SUBADDR)
1545 if (ast_connected.id.subaddress.valid) {
1546 ast_party_subaddress_set(&owner->cid.subaddress,
1547 &ast_connected.id.subaddress);
1548 if (ast_connected.id.subaddress.str) {
1549 ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
1550 ast_connected.id.subaddress.str,
1551 sizeof(pri->pvts[chanpos]->cid_subaddr));
1554 #endif /* defined(HAVE_PRI_SUBADDR) */
1555 if (caller_id_update) {
1556 pri->pvts[chanpos]->callingpres =
1557 ast_connected.id.number_presentation;
1558 sig_pri_set_caller_id(pri->pvts[chanpos]);
1559 ast_set_callerid(owner, S_OR(ast_connected.id.number, NULL),
1560 S_OR(ast_connected.id.name, NULL),
1561 S_OR(ast_connected.id.number, NULL));
1564 /* Update the connected line information on the other channel */
1565 if (event_id != PRI_EVENT_RING) {
1566 /* This connected_line update was not from a SETUP message. */
1567 ast_channel_queue_connected_line_update(owner, &ast_connected);
1570 ast_party_connected_line_free(&ast_connected);
1571 ast_channel_unlock(owner);
1574 case PRI_SUBCMD_REDIRECTING:
1575 sig_pri_lock_owner(pri, chanpos);
1576 owner = pri->pvts[chanpos]->owner;
1578 sig_pri_redirecting_convert(&ast_redirecting, &subcmd->u.redirecting,
1579 &owner->redirecting, pri);
1581 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
1583 ast_channel_set_redirecting(owner, &ast_redirecting);
1584 if (event_id != PRI_EVENT_RING) {
1585 /* This redirection was not from a SETUP message. */
1586 ast_channel_queue_redirecting_update(owner, &ast_redirecting);
1588 ast_party_redirecting_free(&ast_redirecting);
1590 ast_channel_unlock(owner);
1593 #if defined(HAVE_PRI_CALL_REROUTING)
1594 case PRI_SUBCMD_REROUTING:
1595 sig_pri_lock_owner(pri, chanpos);
1596 owner = pri->pvts[chanpos]->owner;
1598 struct pri_party_redirecting pri_deflection;
1601 ast_channel_unlock(owner);
1602 ast_log(LOG_WARNING,
1603 "CallRerouting/CallDeflection to '%s' without call!\n",
1604 subcmd->u.rerouting.deflection.to.number.str);
1608 pri_deflection = subcmd->u.rerouting.deflection;
1610 ast_string_field_set(owner, call_forward, pri_deflection.to.number.str);
1612 /* Adjust the deflecting to number based upon the subscription option. */
1613 switch (subcmd->u.rerouting.subscription_option) {
1614 case 0: /* noNotification */
1615 case 1: /* notificationWithoutDivertedToNr */
1616 /* Delete the number because the far end is not supposed to see it. */
1617 pri_deflection.to.number.presentation =
1618 PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
1619 pri_deflection.to.number.plan =
1620 (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
1621 pri_deflection.to.number.str[0] = '\0';
1623 case 2: /* notificationWithDivertedToNr */
1625 case 3: /* notApplicable */
1629 sig_pri_redirecting_convert(&ast_redirecting, &pri_deflection,
1630 &owner->redirecting, pri);
1631 ast_channel_set_redirecting(owner, &ast_redirecting);
1632 ast_party_redirecting_free(&ast_redirecting);
1635 * Send back positive ACK to CallRerouting/CallDeflection.
1637 * Note: This call will be hungup by the dial application when
1638 * it processes the call_forward string set above.
1640 pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
1641 PRI_REROUTING_RSP_OK_CLEAR);
1643 /* This line is BUSY to further attempts by this dialing attempt. */
1644 ast_queue_control(owner, AST_CONTROL_BUSY);
1646 ast_channel_unlock(owner);
1649 #endif /* defined(HAVE_PRI_CALL_REROUTING) */
1652 "Unknown call subcommand(%d) in %s event on channel %d/%d on span %d.\n",
1653 subcmd->cmd, pri_event2str(event_id), PRI_SPAN(channel),
1654 PRI_CHANNEL(channel), pri->span);
1660 #if defined(HAVE_PRI_CALL_HOLD)
1663 * \brief Attempt to transfer the active call to the held call.
1666 * \param pri sig_pri PRI control structure.
1667 * \param active_call Active call to transfer.
1668 * \param held_call Held call to transfer.
1670 * \note Assumes the pri->lock is already obtained.
1672 * \retval 0 on success.
1673 * \retval -1 on error.
1675 static int sig_pri_attempt_transfer(struct sig_pri_pri *pri, q931_call *active_call, q931_call *held_call)
1680 struct ast_channel *active_ast;
1681 struct ast_channel *held_ast;
1682 struct ast_channel *bridged;
1684 active_chanpos = pri_find_pri_call(pri, active_call);
1685 held_chanpos = pri_find_pri_call(pri, held_call);
1686 if (active_chanpos < 0 || held_chanpos < 0) {
1690 sig_pri_lock_private(pri->pvts[active_chanpos]);
1691 sig_pri_lock_private(pri->pvts[held_chanpos]);
1692 sig_pri_lock_owner(pri, active_chanpos);
1693 sig_pri_lock_owner(pri, held_chanpos);
1695 active_ast = pri->pvts[active_chanpos]->owner;
1696 held_ast = pri->pvts[held_chanpos]->owner;
1697 if (!active_ast || !held_ast) {
1699 ast_channel_unlock(active_ast);
1702 ast_channel_unlock(held_ast);
1704 sig_pri_unlock_private(pri->pvts[active_chanpos]);
1705 sig_pri_unlock_private(pri->pvts[held_chanpos]);
1709 bridged = ast_bridged_channel(held_ast);
1711 ast_queue_control(held_ast, AST_CONTROL_UNHOLD);
1713 ast_verb(3, "TRANSFERRING %s to %s\n", held_ast->name, active_ast->name);
1714 retval = ast_channel_masquerade(active_ast, bridged);
1717 * Could not transfer. Held channel is not bridged anymore.
1718 * Held party probably got tired of waiting and hung up.
1723 ast_channel_unlock(active_ast);
1724 ast_channel_unlock(held_ast);
1725 sig_pri_unlock_private(pri->pvts[active_chanpos]);
1726 sig_pri_unlock_private(pri->pvts[held_chanpos]);
1730 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1732 #if defined(HAVE_PRI_CALL_HOLD)
1735 * \brief Handle the hold event from libpri.
1738 * \param pri sig_pri PRI control structure.
1739 * \param ev Hold event received.
1741 * \note Assumes the pri->lock is already obtained.
1743 * \retval 0 on success.
1744 * \retval -1 on error.
1746 static int sig_pri_handle_hold(struct sig_pri_pri *pri, pri_event *ev)
1751 struct ast_channel *bridged;
1752 struct ast_channel *owner;
1754 chanpos_old = pri_find_principle(pri, ev->hold.channel, ev->hold.call);
1755 if (chanpos_old < 0) {
1756 ast_log(LOG_WARNING,
1757 "Received HOLD on unconfigured channel %d/%d span %d\n",
1758 PRI_SPAN(ev->hold.channel), PRI_CHANNEL(ev->hold.channel), pri->span);
1761 if (pri->pvts[chanpos_old]->no_b_channel) {
1762 /* Call is already on hold or is call waiting call. */
1766 sig_pri_lock_private(pri->pvts[chanpos_old]);
1767 sig_pri_lock_owner(pri, chanpos_old);
1768 owner = pri->pvts[chanpos_old]->owner;
1771 goto done_with_private;
1773 bridged = ast_bridged_channel(owner);
1775 /* Cannot hold a call that is not bridged. */
1777 goto done_with_owner;
1779 chanpos_new = pri_find_empty_nobch(pri);
1780 if (chanpos_new < 0) {
1781 /* No hold channel available. */
1783 goto done_with_owner;
1785 sig_pri_handle_subcmds(pri, chanpos_old, ev->e, ev->hold.channel, ev->hold.subcmds,
1787 chanpos_new = pri_fixup_principle(pri, chanpos_new, ev->hold.call);
1788 if (chanpos_new < 0) {
1789 /* Should never happen. */
1792 struct ast_frame f = { AST_FRAME_CONTROL, };
1794 f.subclass.integer = AST_CONTROL_HOLD;
1795 ast_queue_frame(owner, &f);
1800 ast_channel_unlock(owner);
1802 sig_pri_unlock_private(pri->pvts[chanpos_old]);
1806 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1808 #if defined(HAVE_PRI_CALL_HOLD)
1811 * \brief Handle the retrieve event from libpri.
1814 * \param pri sig_pri PRI control structure.
1815 * \param ev Retrieve event received.
1817 * \note Assumes the pri->lock is already obtained.
1821 static void sig_pri_handle_retrieve(struct sig_pri_pri *pri, pri_event *ev)
1825 if (!(ev->retrieve.channel & PRI_HELD_CALL)
1826 || pri_find_principle(pri, ev->retrieve.channel, ev->retrieve.call) < 0) {
1827 /* The call is not currently held. */
1828 pri_retrieve_rej(pri->pri, ev->retrieve.call,
1829 PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
1832 if (PRI_CHANNEL(ev->retrieve.channel) == 0xFF) {
1833 chanpos = pri_find_empty_chan(pri, 1);
1835 chanpos = pri_find_principle(pri,
1836 ev->retrieve.channel & ~PRI_HELD_CALL, ev->retrieve.call);
1837 if (ev->retrieve.flexible
1838 && (chanpos < 0 || pri->pvts[chanpos]->owner)) {
1840 * Channel selection is flexible and the requested channel
1841 * is bad or already in use. Pick another channel.
1843 chanpos = pri_find_empty_chan(pri, 1);
1847 pri_retrieve_rej(pri->pri, ev->retrieve.call,
1848 ev->retrieve.flexible ? PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION
1849 : PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
1852 chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve.call);
1854 /* Channel is already in use. */
1855 pri_retrieve_rej(pri->pri, ev->retrieve.call,
1856 PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
1859 sig_pri_lock_private(pri->pvts[chanpos]);
1860 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve.channel,
1861 ev->retrieve.subcmds, ev->retrieve.call);
1863 struct ast_frame f = { AST_FRAME_CONTROL, };
1865 f.subclass.integer = AST_CONTROL_UNHOLD;
1866 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1868 sig_pri_unlock_private(pri->pvts[chanpos]);
1869 pri_retrieve_ack(pri->pri, ev->retrieve.call,
1870 PVT_TO_CHANNEL(pri->pvts[chanpos]));
1872 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1874 static void *pri_dchannel(void *vpri)
1876 struct sig_pri_pri *pri = vpri;
1878 struct pollfd fds[NUM_DCHANS];
1882 struct ast_channel *c;
1883 struct timeval tv, lowest, *next;
1891 char plancallingnum[AST_MAX_EXTENSION];
1892 char plancallingani[AST_MAX_EXTENSION];
1893 char calledtonstr[10];
1894 struct timeval lastidle = { 0, 0 };
1896 struct ast_channel *idle;
1902 gettimeofday(&lastidle, NULL);
1903 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1905 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
1906 /* Need to do idle dialing, check to be sure though */
1907 cc = strchr(pri->idleext, '@');
1911 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
1913 /* Extensions may not be loaded yet */
1914 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
1915 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
1920 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
1923 for (i = 0; i < NUM_DCHANS; i++) {
1924 if (!pri->dchans[i])
1926 fds[i].fd = pri->fds[i];
1927 fds[i].events = POLLIN | POLLPRI;
1932 ast_mutex_lock(&pri->lock);
1933 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
1934 if (pri->resetting && pri_is_up(pri)) {
1935 if (pri->resetpos < 0)
1936 pri_check_restart(pri);
1938 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
1944 /* Look for any idle channels if appropriate */
1945 if (doidling && pri_is_up(pri)) {
1949 for (x = pri->numchans; x >= 0; x--) {
1951 && !pri->pvts[x]->owner
1952 && !pri->pvts[x]->call
1953 && !pri->pvts[x]->no_b_channel) {
1954 if (haveidles < pri->minunused) {
1956 } else if (!pri->pvts[x]->resetting) {
1960 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
1963 if (nextidle > -1) {
1964 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
1965 /* Don't create a new idle call more than once per second */
1966 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
1967 idle = sig_pri_request(pri->pvts[nextidle], AST_FORMAT_ULAW, NULL, 0);
1969 pri->pvts[nextidle]->isidlecall = 1;
1970 if (ast_pthread_create_background(&p, NULL, do_idle_thread, pri->pvts[nextidle])) {
1971 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
1975 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
1976 gettimeofday(&lastidle, NULL);
1978 } else if ((haveidles < pri->minunused) &&
1979 (activeidles > pri->minidle)) {
1980 /* Mark something for hangup if there is something
1981 that can be hungup */
1982 for (x = pri->numchans; x >= 0; x--) {
1983 /* find a candidate channel */
1984 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
1985 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
1987 /* Stop if we have enough idle channels or
1988 can't spare any more active idle ones */
1989 if ((haveidles >= pri->minunused) ||
1990 (activeidles <= pri->minidle))
1996 /* Start with reasonable max */
1997 lowest = ast_tv(60, 0);
1998 for (i = 0; i < NUM_DCHANS; i++) {
1999 /* Find lowest available d-channel */
2000 if (!pri->dchans[i])
2002 if ((next = pri_schedule_next(pri->dchans[i]))) {
2003 /* We need relative time here */
2004 tv = ast_tvsub(*next, ast_tvnow());
2005 if (tv.tv_sec < 0) {
2008 if (doidling || pri->resetting) {
2009 if (tv.tv_sec > 1) {
2013 if (tv.tv_sec > 60) {
2017 } else if (doidling || pri->resetting) {
2018 /* Make sure we stop at least once per second if we're
2019 monitoring idle channels */
2022 /* Don't poll for more than 60 seconds */
2025 if (!i || ast_tvcmp(tv, lowest) < 0) {
2029 ast_mutex_unlock(&pri->lock);
2031 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2032 pthread_testcancel();
2034 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
2035 pthread_testcancel();
2036 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2038 ast_mutex_lock(&pri->lock);
2040 for (which = 0; which < NUM_DCHANS; which++) {
2041 if (!pri->dchans[which])
2043 /* Just a timeout, run the scheduler */
2044 e = pri_schedule_run(pri->dchans[which]);
2048 } else if (res > -1) {
2049 for (which = 0; which < NUM_DCHANS; which++) {
2050 if (!pri->dchans[which])
2052 if (fds[which].revents & POLLPRI) {
2053 sig_pri_handle_dchan_exception(pri, which);
2054 } else if (fds[which].revents & POLLIN) {
2055 e = pri_check_event(pri->dchans[which]);
2060 } else if (errno != EINTR)
2061 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
2065 pri_dump_event(pri->dchans[which], e);
2067 if (e->e != PRI_EVENT_DCHAN_DOWN) {
2068 if (!(pri->dchanavail[which] & DCHAN_UP)) {
2069 ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
2071 pri->dchanavail[which] |= DCHAN_UP;
2073 if (pri->dchanavail[which] & DCHAN_UP) {
2074 ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
2076 pri->dchanavail[which] &= ~DCHAN_UP;
2079 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
2080 /* Must be an NFAS group that has the secondary dchan active */
2081 pri->pri = pri->dchans[which];
2084 case PRI_EVENT_DCHAN_UP:
2085 if (!pri->pri) pri_find_dchan(pri);
2087 /* Note presense of D-channel */
2088 time(&pri->lastreset);
2090 /* Restart in 5 seconds */
2091 if (pri->resetinterval > -1) {
2092 pri->lastreset -= pri->resetinterval;
2093 pri->lastreset += 5;
2096 /* Take the channels from inalarm condition */
2097 for (i = 0; i < pri->numchans; i++)
2099 pri->pvts[i]->inalarm = 0;
2102 case PRI_EVENT_DCHAN_DOWN:
2103 pri_find_dchan(pri);
2104 if (!pri_is_up(pri)) {
2106 if (pri->sig == SIG_BRI_PTMP) {
2107 /* For PTMP connections with non persistent layer 2 we want
2108 * to *not* declare inalarm unless there actually is an alarm */
2111 /* Hangup active channels and put them in alarm mode */
2112 for (i = 0; i < pri->numchans; i++) {
2113 struct sig_pri_chan *p = pri->pvts[i];
2115 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
2116 /* T309 is not enabled : hangup calls when alarm occurs */
2118 if (p->pri && p->pri->pri) {
2119 pri_hangup(p->pri->pri, p->call, -1);
2120 pri_destroycall(p->pri->pri, p->call);
2123 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
2126 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2133 case PRI_EVENT_RESTART:
2134 if (e->restart.channel > -1) {
2135 chanpos = pri_find_principle(pri, e->restart.channel, NULL);
2137 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
2138 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
2140 #ifdef HAVE_PRI_SERVICE_MESSAGES
2141 char db_chan_name[20], db_answer[5], state;
2142 int why, skipit = 0;
2144 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, pri->pvts[chanpos]->channel);
2145 if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
2146 sscanf(db_answer, "%1c:%30d", &state, &why);
2148 ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), ignoring RESTART\n", pri->span,
2149 PRI_CHANNEL(e->restart.channel), (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
2152 ast_db_del(db_chan_name, SRVST_DBKEY);
2156 sig_pri_lock_private(pri->pvts[chanpos]);
2157 #ifdef HAVE_PRI_SERVICE_MESSAGES
2160 ast_verb(3, "B-channel %d/%d restarted on span %d\n",
2161 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
2162 if (pri->pvts[chanpos]->call) {
2163 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
2164 pri->pvts[chanpos]->call = NULL;
2166 #ifdef HAVE_PRI_SERVICE_MESSAGES
2169 /* Force soft hangup if appropriate */
2170 if (pri->pvts[chanpos]->owner)
2171 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
2172 sig_pri_unlock_private(pri->pvts[chanpos]);
2175 ast_verb(3, "Restart requested on entire span %d\n", pri->span);
2176 for (x = 0; x < pri->numchans; x++)
2178 sig_pri_lock_private(pri->pvts[x]);
2179 if (pri->pvts[x]->call) {
2180 pri_destroycall(pri->pri, pri->pvts[x]->call);
2181 pri->pvts[x]->call = NULL;
2183 if (pri->pvts[x]->owner)
2184 ast_softhangup_nolock(pri->pvts[x]->owner, AST_SOFTHANGUP_DEV);
2185 sig_pri_unlock_private(pri->pvts[x]);
2189 case PRI_EVENT_KEYPAD_DIGIT:
2190 chanpos = pri_find_principle(pri, e->digit.channel, e->digit.call);
2192 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
2193 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
2195 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
2197 sig_pri_lock_private(pri->pvts[chanpos]);
2198 sig_pri_handle_subcmds(pri, chanpos, e->e, e->digit.channel,
2199 e->digit.subcmds, e->digit.call);
2200 /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
2201 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2202 && pri->pvts[chanpos]->call == e->digit.call
2203 && pri->pvts[chanpos]->owner) {
2204 /* how to do that */
2205 int digitlen = strlen(e->digit.digits);
2208 for (i = 0; i < digitlen; i++) {
2209 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->digit.digits[i], };
2211 pri_queue_frame(pri->pvts[chanpos], &f, pri);
2214 sig_pri_unlock_private(pri->pvts[chanpos]);
2219 case PRI_EVENT_INFO_RECEIVED:
2220 chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
2222 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
2223 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
2225 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
2227 sig_pri_lock_private(pri->pvts[chanpos]);
2228 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
2229 e->ring.subcmds, e->ring.call);
2230 /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
2231 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2232 && pri->pvts[chanpos]->call == e->ring.call
2233 && pri->pvts[chanpos]->owner) {
2234 /* how to do that */
2235 int digitlen = strlen(e->ring.callednum);
2238 for (i = 0; i < digitlen; i++) {
2239 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->ring.callednum[i], };
2241 pri_queue_frame(pri->pvts[chanpos], &f, pri);
2244 sig_pri_unlock_private(pri->pvts[chanpos]);
2248 #ifdef HAVE_PRI_SERVICE_MESSAGES
2249 case PRI_EVENT_SERVICE:
2250 chanpos = pri_find_principle(pri, e->service.channel, NULL);
2252 ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
2253 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
2255 char db_chan_name[20], db_answer[5], state;
2258 ch = pri->pvts[chanpos]->channel;
2259 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, ch);
2260 if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
2261 sscanf(db_answer, "%1c:%30d", &state, &why);
2262 ast_db_del(db_chan_name, SRVST_DBKEY);
2264 switch (e->service.changestatus) {
2265 case 0: /* in-service */
2267 if (why & SRVST_NEAREND) {
2268 snprintf(db_answer, sizeof(db_answer), "%s:%d", SRVST_TYPE_OOS, SRVST_NEAREND);
2269 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
2270 ast_debug(2, "channel '%d' service state { near: out-of-service, far: in-service }\n", ch);
2274 case 2: /* out-of-service */
2278 why |= SRVST_FAREND;
2280 snprintf(db_answer, sizeof(db_answer), "%s:%d", SRVST_TYPE_OOS, why);
2281 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
2284 ast_log(LOG_ERROR, "Huh? changestatus is: %d\n", e->service.changestatus);
2286 ast_log(LOG_NOTICE, "Channel %d/%d span %d (logical: %d) received a change of service message, status '%d'\n",
2287 PRI_SPAN(e->service.channel), PRI_CHANNEL(e->service.channel), pri->span, ch, e->service.changestatus);
2290 case PRI_EVENT_SERVICE_ACK:
2291 chanpos = pri_find_principle(pri, e->service_ack.channel, NULL);
2293 ast_log(LOG_WARNING, "Received service acknowledge change status '%d' on unconfigured channel %d/%d span %d\n",
2294 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
2296 ast_debug(2, "Channel %d/%d span %d received a change os service acknowledgement message, status '%d'\n",
2297 PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span, e->service_ack.changestatus);
2301 case PRI_EVENT_RING:
2302 if (!ast_strlen_zero(pri->msn_list)
2303 && !sig_pri_msn_match(pri->msn_list, e->ring.callednum)) {
2304 /* The call is not for us so ignore it. */
2306 "Ignoring call to '%s' on span %d. Its not in the MSN list: %s\n",
2307 e->ring.callednum, pri->span, pri->msn_list);
2308 pri_destroycall(pri->pri, e->ring.call);
2311 if (e->ring.channel == -1)
2312 chanpos = pri_find_empty_chan(pri, 1);
2314 chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
2315 /* if no channel specified find one empty */
2317 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
2318 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
2320 sig_pri_lock_private(pri->pvts[chanpos]);
2321 if (pri->pvts[chanpos]->owner) {
2322 if (pri->pvts[chanpos]->call == e->ring.call) {
2323 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
2324 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
2325 sig_pri_unlock_private(pri->pvts[chanpos]);
2328 /* This is where we handle initial glare */
2329 ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiating channel.\n",
2330 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
2331 sig_pri_unlock_private(pri->pvts[chanpos]);
2336 sig_pri_unlock_private(pri->pvts[chanpos]);
2338 if ((chanpos < 0) && (e->ring.flexible))
2339 chanpos = pri_find_empty_chan(pri, 1);
2341 sig_pri_lock_private(pri->pvts[chanpos]);
2342 pri->pvts[chanpos]->call = e->ring.call;
2344 /* Use plancallingnum as a scratch buffer since it is initialized next. */
2345 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri,
2346 e->ring.redirectingnum, e->ring.callingplanrdnis);
2347 sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
2349 /* Setup caller-id info */
2350 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
2351 pri->pvts[chanpos]->cid_ani2 = 0;
2352 if (pri->pvts[chanpos]->use_callerid) {
2353 ast_shrink_phone_number(plancallingnum);
2354 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
2356 if (!ast_strlen_zero(e->ring.callingani)) {
2357 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
2358 ast_shrink_phone_number(plancallingani);
2359 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
2361 pri->pvts[chanpos]->cid_ani[0] = '\0';
2364 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
2365 #if defined(HAVE_PRI_SUBADDR)
2366 if (e->ring.calling.subaddress.valid) {
2367 struct ast_party_subaddress calling_subaddress;
2369 ast_party_subaddress_init(&calling_subaddress);
2370 sig_pri_set_subaddress(&calling_subaddress,
2371 &e->ring.calling.subaddress);
2372 if (calling_subaddress.str) {
2373 ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
2374 calling_subaddress.str,
2375 sizeof(pri->pvts[chanpos]->cid_subaddr));
2377 ast_party_subaddress_free(&calling_subaddress);
2379 #endif /* defined(HAVE_PRI_SUBADDR) */
2380 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
2381 pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
2382 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
2383 if (e->ring.ani2 >= 0) {
2384 pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
2387 pri->pvts[chanpos]->cid_num[0] = '\0';
2388 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
2389 pri->pvts[chanpos]->cid_ani[0] = '\0';
2390 pri->pvts[chanpos]->cid_name[0] = '\0';
2391 pri->pvts[chanpos]->cid_ton = 0;
2392 pri->pvts[chanpos]->callingpres = 0;
2394 sig_pri_set_caller_id(pri->pvts[chanpos]);
2396 /* Set DNID on all incoming calls -- even immediate */
2397 sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
2399 /* If immediate=yes go to s|1 */
2400 if (pri->pvts[chanpos]->immediate) {
2401 ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
2402 pri->pvts[chanpos]->exten[0] = 's';
2403 pri->pvts[chanpos]->exten[1] = '\0';
2405 /* Get called number */
2406 else if (!ast_strlen_zero(e->ring.callednum)) {
2407 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
2408 } else if (pri->overlapdial)
2409 pri->pvts[chanpos]->exten[0] = '\0';
2411 /* Some PRI circuits are set up to send _no_ digits. Handle them as 's'. */
2412 pri->pvts[chanpos]->exten[0] = 's';
2413 pri->pvts[chanpos]->exten[1] = '\0';
2415 /* No number yet, but received "sending complete"? */
2416 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
2417 ast_verb(3, "Going to extension s|1 because of Complete received\n");
2418 pri->pvts[chanpos]->exten[0] = 's';
2419 pri->pvts[chanpos]->exten[1] = '\0';
2422 /* Make sure extension exists (or in overlap dial mode, can exist) */
2423 if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
2424 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
2426 if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
2427 /* Just announce proceeding */
2428 pri->pvts[chanpos]->proceeding = 1;
2429 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
2431 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
2432 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
2434 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
2438 if (!e->ring.complete
2439 && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2440 && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
2442 * Release the PRI lock while we create the channel
2443 * so other threads can send D channel messages.
2445 ast_mutex_unlock(&pri->lock);
2446 c = sig_pri_new_ast_channel(pri->pvts[chanpos],
2447 AST_STATE_RESERVED, 0,
2448 (e->ring.layer1 == PRI_LAYER_1_ALAW)
2449 ? SIG_PRI_ALAW : SIG_PRI_ULAW,
2450 e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
2451 ast_mutex_lock(&pri->lock);
2453 #if defined(HAVE_PRI_SUBADDR)
2454 if (e->ring.calling.subaddress.valid) {
2455 /* Set Calling Subaddress */
2456 sig_pri_lock_owner(pri, chanpos);
2457 sig_pri_set_subaddress(
2458 &pri->pvts[chanpos]->owner->cid.subaddress,
2459 &e->ring.calling.subaddress);
2460 if (!e->ring.calling.subaddress.type
2461 && !ast_strlen_zero(
2462 (char *) e->ring.calling.subaddress.data)) {
2464 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR",
2465 (char *) e->ring.calling.subaddress.data);
2467 ast_channel_unlock(c);
2469 if (e->ring.called_subaddress.valid) {
2470 /* Set Called Subaddress */
2471 sig_pri_lock_owner(pri, chanpos);
2472 sig_pri_set_subaddress(
2473 &pri->pvts[chanpos]->owner->cid.dialed_subaddress,
2474 &e->ring.called_subaddress);
2475 if (!e->ring.called_subaddress.type
2476 && !ast_strlen_zero(
2477 (char *) e->ring.called_subaddress.data)) {
2479 pbx_builtin_setvar_helper(c, "CALLEDSUBADDR",
2480 (char *) e->ring.called_subaddress.data);
2482 ast_channel_unlock(c);
2485 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
2486 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
2488 #endif /* !defined(HAVE_PRI_SUBADDR) */
2489 if (e->ring.ani2 >= 0) {
2490 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
2491 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
2494 #ifdef SUPPORT_USERUSER
2495 if (!ast_strlen_zero(e->ring.useruserinfo)) {
2496 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
2500 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
2501 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
2502 if (e->ring.redirectingreason >= 0) {
2503 /* This is now just a status variable. Use REDIRECTING() dialplan function. */
2504 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
2506 #if defined(HAVE_PRI_REVERSE_CHARGE)
2507 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
2509 #if defined(HAVE_PRI_SETUP_KEYPAD)
2510 ast_copy_string(pri->pvts[chanpos]->keypad_digits,
2511 e->ring.keypad_digits,
2512 sizeof(pri->pvts[chanpos]->keypad_digits));
2513 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
2515 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
2516 e->ring.subcmds, e->ring.call);
2518 if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
2519 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
2520 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
2521 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
2523 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
2524 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
2528 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
2529 pri->pvts[chanpos]->call = NULL;
2534 * Release the PRI lock while we create the channel
2535 * so other threads can send D channel messages.
2537 ast_mutex_unlock(&pri->lock);
2538 c = sig_pri_new_ast_channel(pri->pvts[chanpos],
2540 (e->ring.layer1 == PRI_LAYER_1_ALAW)
2541 ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype,
2542 pri->pvts[chanpos]->exten, NULL);
2543 ast_mutex_lock(&pri->lock);
2546 * It is reasonably safe to set the following
2547 * channel variables while the PRI and DAHDI private
2548 * structures are locked. The PBX has not been
2549 * started yet and it is unlikely that any other task
2550 * will do anything with the channel we have just
2553 #if defined(HAVE_PRI_SUBADDR)
2554 if (e->ring.calling.subaddress.valid) {
2555 /* Set Calling Subaddress */
2556 sig_pri_lock_owner(pri, chanpos);
2557 sig_pri_set_subaddress(
2558 &pri->pvts[chanpos]->owner->cid.subaddress,
2559 &e->ring.calling.subaddress);
2560 if (!e->ring.calling.subaddress.type
2561 && !ast_strlen_zero(
2562 (char *) e->ring.calling.subaddress.data)) {
2564 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR",
2565 (char *) e->ring.calling.subaddress.data);
2567 ast_channel_unlock(c);
2569 if (e->ring.called_subaddress.valid) {
2570 /* Set Called Subaddress */
2571 sig_pri_lock_owner(pri, chanpos);
2572 sig_pri_set_subaddress(
2573 &pri->pvts[chanpos]->owner->cid.dialed_subaddress,
2574 &e->ring.called_subaddress);
2575 if (!e->ring.called_subaddress.type
2576 && !ast_strlen_zero(
2577 (char *) e->ring.called_subaddress.data)) {
2579 pbx_builtin_setvar_helper(c, "CALLEDSUBADDR",
2580 (char *) e->ring.called_subaddress.data);
2582 ast_channel_unlock(c);
2585 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
2586 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
2588 #endif /* !defined(HAVE_PRI_SUBADDR) */
2589 if (e->ring.ani2 >= 0) {
2590 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
2591 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
2594 #ifdef SUPPORT_USERUSER
2595 if (!ast_strlen_zero(e->ring.useruserinfo)) {
2596 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
2600 if (e->ring.redirectingreason >= 0) {
2601 /* This is now just a status variable. Use REDIRECTING() dialplan function. */
2602 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
2604 #if defined(HAVE_PRI_REVERSE_CHARGE)
2605 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
2607 #if defined(HAVE_PRI_SETUP_KEYPAD)
2608 ast_copy_string(pri->pvts[chanpos]->keypad_digits,
2609 e->ring.keypad_digits,
2610 sizeof(pri->pvts[chanpos]->keypad_digits));
2611 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
2613 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
2614 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
2616 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
2617 e->ring.subcmds, e->ring.call);
2619 if (c && !ast_pbx_start(c)) {
2620 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
2621 plancallingnum, pri->pvts[chanpos]->exten,
2622 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
2623 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
2625 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
2626 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
2630 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
2631 pri->pvts[chanpos]->call = NULL;
2636 ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
2637 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
2638 pri->pvts[chanpos]->prioffset, pri->span);
2639 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
2640 pri->pvts[chanpos]->call = NULL;
2641 pri->pvts[chanpos]->exten[0] = '\0';
2643 sig_pri_unlock_private(pri->pvts[chanpos]);
2645 if (e->ring.flexible)
2646 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
2648 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
2651 case PRI_EVENT_RINGING:
2652 chanpos = pri_find_principle(pri, e->ringing.channel, e->ringing.call);
2654 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
2655 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
2657 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
2659 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
2660 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
2662 sig_pri_lock_private(pri->pvts[chanpos]);
2664 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ringing.channel,
2665 e->ringing.subcmds, e->ringing.call);
2666 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
2667 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_RINGING, pri);
2668 pri->pvts[chanpos]->alerting = 1;
2670 #ifdef SUPPORT_USERUSER
2671 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
2672 struct ast_channel *owner;
2674 sig_pri_lock_owner(pri, chanpos);
2675 owner = pri->pvts[chanpos]->owner;
2677 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
2678 e->ringing.useruserinfo);
2679 ast_channel_unlock(owner);
2684 sig_pri_unlock_private(pri->pvts[chanpos]);
2688 case PRI_EVENT_PROGRESS:
2689 /* Get chan value if e->e is not PRI_EVNT_RINGING */
2690 chanpos = pri_find_principle(pri, e->proceeding.channel, e->proceeding.call);
2692 sig_pri_lock_private(pri->pvts[chanpos]);
2693 sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.channel,
2694 e->proceeding.subcmds, e->proceeding.call);
2695 if ((!pri->pvts[chanpos]->progress)
2696 #ifdef PRI_PROGRESS_MASK
2697 || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
2699 || (e->proceeding.progress == 8)
2702 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_PROGRESS, };
2704 if (e->proceeding.cause > -1) {
2705 ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
2707 /* Work around broken, out of spec USER_BUSY cause in a progress message */
2708 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
2709 if (pri->pvts[chanpos]->owner) {
2710 ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
2712 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
2713 f.subclass.integer = AST_CONTROL_BUSY;
2718 ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",