2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010 Digium, Inc.
6 * Richard Mudgett <rmudgett@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 SS7 signaling module.
23 * \author Matthew Fredrickson <creslin@digium.com>
24 * \author Richard Mudgett <rmudgett@digium.com>
27 * \arg \ref AstCREDITS
37 #include "asterisk/pbx.h"
38 #include "asterisk/causes.h"
39 #include "asterisk/musiconhold.h"
40 #include "asterisk/transcap.h"
44 /* ------------------------------------------------------------------- */
46 #define SIG_SS7_DEADLOCK_AVOIDANCE(p) \
48 sig_ss7_unlock_private(p); \
50 sig_ss7_lock_private(p); \
53 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
55 if (p->calls->unlock_private) {
56 p->calls->unlock_private(p->chan_pvt);
60 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
62 if (p->calls->lock_private) {
63 p->calls->lock_private(p->chan_pvt);
67 static void sig_ss7_deadlock_avoidance_private(struct sig_ss7_chan *p)
69 if (p->calls->deadlock_avoidance_private) {
70 p->calls->deadlock_avoidance_private(p->chan_pvt);
72 /* Fallback to the old way if callback not present. */
73 SIG_SS7_DEADLOCK_AVOIDANCE(p);
77 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
79 p->inalarm = in_alarm;
80 if (p->calls->set_alarm) {
81 p->calls->set_alarm(p->chan_pvt, in_alarm);
85 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
87 if (p->calls->set_dialing) {
88 p->calls->set_dialing(p->chan_pvt, is_dialing);
92 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
94 if (p->calls->set_digital) {
95 p->calls->set_digital(p->chan_pvt, is_digital);
99 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
101 if (p->calls->set_inservice) {
102 p->calls->set_inservice(p->chan_pvt, is_inservice);
106 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
108 p->locallyblocked = is_blocked;
109 if (p->calls->set_locallyblocked) {
110 p->calls->set_locallyblocked(p->chan_pvt, is_blocked);
114 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
116 p->remotelyblocked = is_blocked;
117 if (p->calls->set_remotelyblocked) {
118 p->calls->set_remotelyblocked(p->chan_pvt, is_blocked);
124 * \brief Set the caller id information in the parent module.
127 * \param p sig_ss7 channel structure.
131 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
133 struct ast_party_caller caller;
135 if (p->calls->set_callerid) {
136 ast_party_caller_init(&caller);
138 caller.id.name.str = p->cid_name;
139 caller.id.name.presentation = p->callingpres;
140 caller.id.name.valid = 1;
142 caller.id.number.str = p->cid_num;
143 caller.id.number.plan = p->cid_ton;
144 caller.id.number.presentation = p->callingpres;
145 caller.id.number.valid = 1;
147 if (!ast_strlen_zero(p->cid_subaddr)) {
148 caller.id.subaddress.valid = 1;
149 //caller.id.subaddress.type = 0;/* nsap */
150 //caller.id.subaddress.odd_even_indicator = 0;
151 caller.id.subaddress.str = p->cid_subaddr;
154 caller.ani.number.str = p->cid_ani;
155 //caller.ani.number.plan = p->xxx;
156 //caller.ani.number.presentation = p->xxx;
157 caller.ani.number.valid = 1;
159 caller.ani2 = p->cid_ani2;
160 p->calls->set_callerid(p->chan_pvt, &caller);
166 * \brief Set the Dialed Number Identifier.
169 * \param p sig_ss7 channel structure.
170 * \param dnid Dialed Number Identifier string.
174 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
176 if (p->calls->set_dnid) {
177 p->calls->set_dnid(p->chan_pvt, dnid);
181 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
185 if (p->calls->play_tone) {
186 res = p->calls->play_tone(p->chan_pvt, tone);
193 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
195 if (p->calls->set_echocanceller) {
196 return p->calls->set_echocanceller(p->chan_pvt, enable);
201 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
203 if (p->loopedback != enable) {
204 p->loopedback = enable;
205 if (p->calls->set_loopback) {
206 p->calls->set_loopback(p->chan_pvt, enable);
211 static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
213 struct ast_channel *ast;
215 if (p->calls->new_ast_channel) {
216 ast = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
224 p->alreadyhungup = 0;
225 ast->transfercapability = transfercapability;
226 pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
227 ast_transfercapability2str(transfercapability));
228 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
229 sig_ss7_set_digital(p, 1);
235 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
237 if (linkset->calls->handle_link_exception) {
238 linkset->calls->handle_link_exception(linkset, which);
244 * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
247 * \param ss7 sig_ss7 SS7 control structure.
248 * \param chanpos Channel position in the span.
250 * \note Assumes the ss7->lock is already obtained.
251 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
255 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
258 if (!ss7->pvts[chanpos]->owner) {
259 /* There is no owner lock to get. */
262 if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
263 /* We got the lock */
266 /* We must unlock the SS7 to avoid the possibility of a deadlock */
267 ast_mutex_unlock(&ss7->lock);
268 sig_ss7_deadlock_avoidance_private(ss7->pvts[chanpos]);
269 ast_mutex_lock(&ss7->lock);
275 * \brief Queue the given frame onto the owner channel.
278 * \param ss7 sig_ss7 SS7 control structure.
279 * \param chanpos Channel position in the span.
280 * \param frame Frame to queue onto the owner channel.
282 * \note Assumes the ss7->lock is already obtained.
283 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
287 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
289 sig_ss7_lock_owner(ss7, chanpos);
290 if (ss7->pvts[chanpos]->owner) {
291 ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
292 ast_channel_unlock(ss7->pvts[chanpos]->owner);
298 * \brief Queue a control frame of the specified subclass onto the owner channel.
301 * \param ss7 sig_ss7 SS7 control structure.
302 * \param chanpos Channel position in the span.
303 * \param subclass Control frame subclass to queue onto the owner channel.
305 * \note Assumes the ss7->lock is already obtained.
306 * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
310 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
312 struct ast_frame f = {AST_FRAME_CONTROL, };
313 struct sig_ss7_chan *p = ss7->pvts[chanpos];
315 if (p->calls->queue_control) {
316 p->calls->queue_control(p->chan_pvt, subclass);
319 f.subclass.integer = subclass;
320 sig_ss7_queue_frame(ss7, chanpos, &f);
323 static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
327 for (i = 0; i < linkset->numchans; i++) {
328 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
336 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
338 unsigned char status[32];
339 struct sig_ss7_chan *p = NULL;
342 for (i = 0; i < linkset->numchans; i++) {
343 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
344 p = linkset->pvts[i];
345 offset = p->cic - startcic;
347 if (p->locallyblocked)
348 status[offset] |= (1 << 0) | (1 << 4);
349 if (p->remotelyblocked)
350 status[offset] |= (1 << 1) | (1 << 5);
353 status[offset] |= (1 << 3);
355 status[offset] |= (1 << 2);
357 status[offset] |= 0x3 << 2;
362 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
364 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
368 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
372 for (i = 0; i < linkset->numchans; i++) {
373 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
374 sig_ss7_lock_private(linkset->pvts[i]);
375 if (linkset->pvts[i]->owner)
376 linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
377 sig_ss7_unlock_private(linkset->pvts[i]);
382 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
386 for (i = 0; i < linkset->numchans; i++) {
387 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
390 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
392 sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
397 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
401 for (i = 0; i < linkset->numchans; i++) {
402 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
403 sig_ss7_set_inservice(linkset->pvts[i], 1);
407 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
409 int i, startcic = -1, endcic, dpc;
411 if (linkset->numchans <= 0)
414 startcic = linkset->pvts[0]->cic;
415 /* DB: CIC's DPC fix */
416 dpc = linkset->pvts[0]->dpc;
418 for (i = 0; i < linkset->numchans; i++) {
419 if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
422 endcic = linkset->pvts[i]->cic;
423 ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
424 isup_grs(linkset->ss7, startcic, endcic, dpc);
426 /* DB: CIC's DPC fix */
427 if (linkset->pvts[i+1]) {
428 startcic = linkset->pvts[i+1]->cic;
429 dpc = linkset->pvts[i+1]->dpc;
435 /* This function is assumed to be called with the private channel lock and linkset lock held */
436 static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
438 struct ss7 *ss7 = linkset->ss7;
440 struct ast_channel *c;
443 if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
444 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
445 isup_acm(ss7, p->ss7call);
447 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
450 if (linkset->type == SS7_ITU) {
457 * Release the SS7 lock while we create the channel so other
458 * threads can send messages. We must also release the private
459 * lock to prevent deadlock while creating the channel.
461 ast_mutex_unlock(&linkset->lock);
462 sig_ss7_unlock_private(p);
463 c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
465 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
466 ast_mutex_lock(&linkset->lock);
467 sig_ss7_lock_private(p);
468 isup_rel(linkset->ss7, p->ss7call, -1);
469 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
470 p->alreadyhungup = 1;
474 /* Hold the channel and private lock while we setup the channel. */
476 sig_ss7_lock_private(p);
478 sig_ss7_set_echocanceller(p, 1);
481 * It is reasonably safe to set the following
482 * channel variables while the channel private
483 * structure is locked. The PBX has not been
484 * started yet and it is unlikely that any other task
485 * will do anything with the channel we have just
488 * We only reference these variables in the context of the ss7_linkset function
489 * when receiving either and IAM or a COT message.
491 if (!ast_strlen_zero(p->charge_number)) {
492 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
493 /* Clear this after we set it */
494 p->charge_number[0] = 0;
496 if (!ast_strlen_zero(p->gen_add_number)) {
497 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
498 /* Clear this after we set it */
499 p->gen_add_number[0] = 0;
501 if (!ast_strlen_zero(p->jip_number)) {
502 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
503 /* Clear this after we set it */
504 p->jip_number[0] = 0;
506 if (!ast_strlen_zero(p->gen_dig_number)) {
507 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
508 /* Clear this after we set it */
509 p->gen_dig_number[0] = 0;
511 if (!ast_strlen_zero(p->orig_called_num)) {
512 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
513 /* Clear this after we set it */
514 p->orig_called_num[0] = 0;
517 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
518 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
519 /* Clear this after we set it */
522 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
523 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
524 /* Clear this after we set it */
525 p->gen_dig_scheme = 0;
527 if (!ast_strlen_zero(p->lspi_ident)) {
528 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
529 /* Clear this after we set it */
530 p->lspi_ident[0] = 0;
533 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
534 pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
535 /* Clear this after we set it */
536 p->call_ref_ident = 0;
538 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
539 pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
540 /* Clear this after we set it */
543 snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
544 pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
545 /* Clear this after we set it */
546 p->calling_party_cat = 0;
548 if (!ast_strlen_zero(p->redirecting_num)) {
549 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
550 /* Clear this after we set it */
551 p->redirecting_num[0] = 0;
553 if (!ast_strlen_zero(p->generic_name)) {
554 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
555 /* Clear this after we set it */
556 p->generic_name[0] = 0;
559 sig_ss7_unlock_private(p);
560 ast_channel_unlock(c);
562 if (ast_pbx_start(c)) {
563 ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", c->name, p->cic);
566 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
569 /* Must return with linkset and private lock. */
570 ast_mutex_lock(&linkset->lock);
571 sig_ss7_lock_private(p);
574 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
576 if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
583 case SS7_NAI_INTERNATIONAL:
584 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
586 case SS7_NAI_NATIONAL:
587 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
589 case SS7_NAI_SUBSCRIBER:
590 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
592 case SS7_NAI_UNKNOWN:
593 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
596 snprintf(buf, size, "%s", number);
601 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
603 return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
606 /* This is a thread per linkset that handles all received events from libss7. */
607 void *ss7_linkset(void *data)
610 struct timeval *next = NULL, tv;
611 struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
612 struct ss7 *ss7 = linkset->ss7;
614 struct sig_ss7_chan *p;
616 struct pollfd pollers[SIG_SS7_NUM_DCHANS];
621 ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
625 ast_mutex_lock(&linkset->lock);
626 if ((next = ss7_schedule_next(ss7))) {
628 tv.tv_sec = next->tv_sec - tv.tv_sec;
629 tv.tv_usec = next->tv_usec - tv.tv_usec;
630 if (tv.tv_usec < 0) {
631 tv.tv_usec += 1000000;
638 nextms = tv.tv_sec * 1000;
639 nextms += tv.tv_usec / 1000;
641 ast_mutex_unlock(&linkset->lock);
643 for (i = 0; i < linkset->numsigchans; i++) {
644 pollers[i].fd = linkset->fds[i];
645 pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
646 pollers[i].revents = 0;
649 res = poll(pollers, linkset->numsigchans, nextms);
650 if ((res < 0) && (errno != EINTR)) {
651 ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
653 ast_mutex_lock(&linkset->lock);
654 ss7_schedule_run(ss7);
655 ast_mutex_unlock(&linkset->lock);
659 ast_mutex_lock(&linkset->lock);
660 for (i = 0; i < linkset->numsigchans; i++) {
661 if (pollers[i].revents & POLLPRI) {
662 sig_ss7_handle_link_exception(linkset, i);
664 if (pollers[i].revents & POLLIN) {
665 res = ss7_read(ss7, pollers[i].fd);
667 if (pollers[i].revents & POLLOUT) {
668 res = ss7_write(ss7, pollers[i].fd);
670 ast_debug(1, "Error in write %s\n", strerror(errno));
675 while ((e = ss7_check_event(ss7))) {
678 if (linkset->state != LINKSET_STATE_UP) {
679 ast_verbose("--- SS7 Up ---\n");
680 ss7_reset_linkset(linkset);
682 linkset->state = LINKSET_STATE_UP;
685 ast_verbose("--- SS7 Down ---\n");
686 linkset->state = LINKSET_STATE_DOWN;
687 for (i = 0; i < linkset->numchans; i++) {
688 p = linkset->pvts[i];
690 sig_ss7_set_alarm(p, 1);
695 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
698 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
701 chanpos = ss7_find_cic(linkset, e->cpg.cic, e->cpg.opc);
703 ast_log(LOG_WARNING, "CPG on unconfigured CIC %d\n", e->cpg.cic);
706 p = linkset->pvts[chanpos];
707 sig_ss7_lock_private(p);
708 switch (e->cpg.event) {
709 case CPG_EVENT_ALERTING:
710 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
711 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
713 sig_ss7_lock_owner(linkset, chanpos);
715 ast_setstate(p->owner, AST_STATE_RINGING);
716 ast_channel_unlock(p->owner);
718 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
720 case CPG_EVENT_PROGRESS:
721 case CPG_EVENT_INBANDINFO:
723 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
724 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
726 sig_ss7_set_dialing(p, 0);
727 #if 0 /* This code no longer seems to be necessary so I did not convert it. */
728 if (p->dsp && p->dsp_features) {
729 ast_dsp_set_features(p->dsp, p->dsp_features);
736 ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
740 sig_ss7_unlock_private(p);
743 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
744 chanpos = ss7_find_cic(linkset, e->rsc.cic, e->rsc.opc);
746 ast_log(LOG_WARNING, "RSC on unconfigured CIC %d\n", e->rsc.cic);
749 p = linkset->pvts[chanpos];
750 sig_ss7_lock_private(p);
751 sig_ss7_set_inservice(p, 1);
752 sig_ss7_set_remotelyblocked(p, 0);
754 isup_set_call_dpc(e->rsc.call, dpc);
755 sig_ss7_lock_owner(linkset, chanpos);
758 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
759 ast_channel_unlock(p->owner);
761 sig_ss7_unlock_private(p);
762 isup_rlc(ss7, e->rsc.call);
765 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
766 chanpos = ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
768 ast_log(LOG_WARNING, "GRS on unconfigured CIC %d\n", e->grs.startcic);
771 p = linkset->pvts[chanpos];
772 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
773 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
774 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
777 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
778 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
781 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
782 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
783 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
786 ast_debug(1, "Got IAM for CIC %d and called number %s, calling number %s\n", e->iam.cic, e->iam.called_party_num, e->iam.calling_party_num);
787 chanpos = ss7_find_cic(linkset, e->iam.cic, e->iam.opc);
789 ast_log(LOG_WARNING, "IAM on unconfigured CIC %d\n", e->iam.cic);
790 isup_rel(ss7, e->iam.call, -1);
793 p = linkset->pvts[chanpos];
794 sig_ss7_lock_private(p);
796 if (p->ss7call == e->iam.call) {
797 sig_ss7_unlock_private(p);
798 ast_log(LOG_WARNING, "Duplicate IAM requested on CIC %d\n", e->iam.cic);
801 sig_ss7_unlock_private(p);
802 ast_log(LOG_WARNING, "Ring requested on CIC %d already in use!\n", e->iam.cic);
808 p->ss7call = e->iam.call;
809 isup_set_call_dpc(p->ss7call, dpc);
811 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
812 ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
813 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
818 if (!ast_strlen_zero(e->iam.called_party_num)) {
819 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
820 e->iam.called_party_num, e->iam.called_nai);
821 sig_ss7_set_dnid(p, p->exten);
827 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
829 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
830 st = strchr(p->exten, '#');
838 p->cid_ani[0] = '\0';
839 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
840 ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
842 p->cid_name[0] = '\0';
844 p->cid_ani2 = e->iam.oli_ani2;
846 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
847 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
848 p->gen_add_type = e->iam.gen_add_type;
849 p->gen_add_nai = e->iam.gen_add_nai;
850 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
851 p->gen_add_num_plan = e->iam.gen_add_num_plan;
852 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
853 p->gen_dig_type = e->iam.gen_dig_type;
854 p->gen_dig_scheme = e->iam.gen_dig_scheme;
855 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
856 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
857 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
858 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
859 p->calling_party_cat = e->iam.calling_party_cat;
861 sig_ss7_set_caller_id(p);
863 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
864 if (e->iam.cot_check_required) {
865 sig_ss7_loopback(p, 1);
867 ss7_start_call(p, linkset);
869 ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
870 p->alreadyhungup = 1;
871 isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
873 sig_ss7_unlock_private(p);
876 chanpos = ss7_find_cic(linkset, e->cot.cic, e->cot.opc);
878 ast_log(LOG_WARNING, "COT on unconfigured CIC %d\n", e->cot.cic);
879 isup_rel(ss7, e->cot.call, -1);
882 p = linkset->pvts[chanpos];
884 sig_ss7_lock_private(p);
886 sig_ss7_loopback(p, 0);
887 ss7_start_call(p, linkset);
889 sig_ss7_unlock_private(p);
892 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
893 chanpos = ss7_find_cic(linkset, e->ccr.cic, e->ccr.opc);
895 ast_log(LOG_WARNING, "CCR on unconfigured CIC %d\n", e->ccr.cic);
899 p = linkset->pvts[chanpos];
901 sig_ss7_lock_private(p);
902 sig_ss7_loopback(p, 1);
903 sig_ss7_unlock_private(p);
905 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
908 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
909 chanpos = ss7_find_cic(linkset, e->cvt.cic, e->cvt.opc);
911 ast_log(LOG_WARNING, "CVT on unconfigured CIC %d\n", e->cvt.cic);
915 p = linkset->pvts[chanpos];
917 sig_ss7_lock_private(p);
918 sig_ss7_loopback(p, 1);
919 sig_ss7_unlock_private(p);
921 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
924 chanpos = ss7_find_cic(linkset, e->rel.cic, e->rel.opc);
926 ast_log(LOG_WARNING, "REL on unconfigured CIC %d\n", e->rel.cic);
929 p = linkset->pvts[chanpos];
930 sig_ss7_lock_private(p);
931 sig_ss7_lock_owner(linkset, chanpos);
933 p->owner->hangupcause = e->rel.cause;
934 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
935 ast_channel_unlock(p->owner);
937 ast_log(LOG_WARNING, "REL on channel (CIC %d) without owner!\n", p->cic);
940 /* End the loopback if we have one */
941 sig_ss7_loopback(p, 0);
943 isup_rlc(ss7, e->rel.call);
946 sig_ss7_unlock_private(p);
949 chanpos = ss7_find_cic(linkset, e->acm.cic, e->acm.opc);
951 ast_log(LOG_WARNING, "ACM on unconfigured CIC %d\n", e->acm.cic);
952 isup_rel(ss7, e->acm.call, -1);
955 p = linkset->pvts[chanpos];
957 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
959 if (e->acm.call_ref_ident > 0) {
960 p->rlt = 1; /* Setting it but not using it here*/
963 sig_ss7_lock_private(p);
964 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
965 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
966 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
968 sig_ss7_set_dialing(p, 0);
969 /* Send alerting if subscriber is free */
970 if (e->acm.called_party_status_ind == 1) {
971 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
972 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
974 sig_ss7_lock_owner(linkset, chanpos);
976 ast_setstate(p->owner, AST_STATE_RINGING);
977 ast_channel_unlock(p->owner);
979 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
981 sig_ss7_unlock_private(p);
985 chanpos = ss7_find_cic(linkset, e->cgb.startcic, e->cgb.opc);
987 ast_log(LOG_WARNING, "CGB on unconfigured CIC %d\n", e->cgb.startcic);
990 p = linkset->pvts[chanpos];
991 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
992 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
995 chanpos = ss7_find_cic(linkset, e->cgu.startcic, e->cgu.opc);
997 ast_log(LOG_WARNING, "CGU on unconfigured CIC %d\n", e->cgu.startcic);
1000 p = linkset->pvts[chanpos];
1001 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
1002 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
1004 case ISUP_EVENT_UCIC:
1005 chanpos = ss7_find_cic(linkset, e->ucic.cic, e->ucic.opc);
1007 ast_log(LOG_WARNING, "UCIC on unconfigured CIC %d\n", e->ucic.cic);
1010 p = linkset->pvts[chanpos];
1011 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
1012 sig_ss7_lock_private(p);
1013 sig_ss7_set_remotelyblocked(p, 1);
1014 sig_ss7_set_inservice(p, 0);
1015 sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
1017 case ISUP_EVENT_BLO:
1018 chanpos = ss7_find_cic(linkset, e->blo.cic, e->blo.opc);
1020 ast_log(LOG_WARNING, "BLO on unconfigured CIC %d\n", e->blo.cic);
1023 p = linkset->pvts[chanpos];
1024 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
1025 sig_ss7_lock_private(p);
1026 sig_ss7_set_remotelyblocked(p, 1);
1027 sig_ss7_unlock_private(p);
1028 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
1030 case ISUP_EVENT_BLA:
1031 chanpos = ss7_find_cic(linkset, e->bla.cic, e->bla.opc);
1033 ast_log(LOG_WARNING, "BLA on unconfigured CIC %d\n", e->bla.cic);
1036 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
1037 p = linkset->pvts[chanpos];
1038 sig_ss7_lock_private(p);
1039 sig_ss7_set_locallyblocked(p, 1);
1040 sig_ss7_unlock_private(p);
1042 case ISUP_EVENT_UBL:
1043 chanpos = ss7_find_cic(linkset, e->ubl.cic, e->ubl.opc);
1045 ast_log(LOG_WARNING, "UBL on unconfigured CIC %d\n", e->ubl.cic);
1048 p = linkset->pvts[chanpos];
1049 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
1050 sig_ss7_lock_private(p);
1051 sig_ss7_set_remotelyblocked(p, 0);
1052 sig_ss7_unlock_private(p);
1053 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
1055 case ISUP_EVENT_UBA:
1056 chanpos = ss7_find_cic(linkset, e->uba.cic, e->uba.opc);
1058 ast_log(LOG_WARNING, "UBA on unconfigured CIC %d\n", e->uba.cic);
1061 p = linkset->pvts[chanpos];
1062 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
1063 sig_ss7_lock_private(p);
1064 sig_ss7_set_locallyblocked(p, 0);
1065 sig_ss7_unlock_private(p);
1067 case ISUP_EVENT_CON:
1068 case ISUP_EVENT_ANM:
1069 if (e->e == ISUP_EVENT_CON)
1074 chanpos = ss7_find_cic(linkset, cic, (e->e == ISUP_EVENT_ANM) ? e->anm.opc : e->con.opc);
1076 ast_log(LOG_WARNING, "ANM/CON on unconfigured CIC %d\n", cic);
1077 isup_rel(ss7, (e->e == ISUP_EVENT_ANM) ? e->anm.call : e->con.call, -1);
1080 p = linkset->pvts[chanpos];
1081 sig_ss7_lock_private(p);
1082 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1083 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
1085 sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
1086 #if 0 /* This code no longer seems to be necessary so I did not convert it. */
1087 if (p->dsp && p->dsp_features) {
1088 ast_dsp_set_features(p->dsp, p->dsp_features);
1089 p->dsp_features = 0;
1092 sig_ss7_set_echocanceller(p, 1);
1093 sig_ss7_unlock_private(p);
1096 case ISUP_EVENT_RLC:
1097 chanpos = ss7_find_cic(linkset, e->rlc.cic, e->rlc.opc);
1099 ast_log(LOG_WARNING, "RLC on unconfigured CIC %d\n", e->rlc.cic);
1102 p = linkset->pvts[chanpos];
1103 sig_ss7_lock_private(p);
1104 if (p->alreadyhungup)
1107 ast_log(LOG_NOTICE, "Received RLC out and we haven't sent REL. Ignoring.\n");
1108 sig_ss7_unlock_private(p);
1111 case ISUP_EVENT_FAA:
1112 chanpos = ss7_find_cic(linkset, e->faa.cic, e->faa.opc);
1114 ast_log(LOG_WARNING, "FAA on unconfigured CIC %d\n", e->faa.cic);
1117 p = linkset->pvts[chanpos];
1118 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
1119 sig_ss7_lock_private(p);
1120 if (p->alreadyhungup){
1122 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
1124 sig_ss7_unlock_private(p);
1128 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
1132 ast_mutex_unlock(&linkset->lock);
1138 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
1140 ast_mutex_unlock(&ss7->lock);
1143 static void ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
1146 /* Grab the lock first */
1148 res = ast_mutex_trylock(&ss7->lock);
1150 sig_ss7_deadlock_avoidance_private(pvt);
1153 /* Then break the poll */
1154 if (ss7->master != AST_PTHREADT_NULL)
1155 pthread_kill(ss7->master, SIGURG);
1159 * \brief Notify the SS7 layer that the link is in alarm.
1162 * \param linkset Controlling linkset for the channel.
1163 * \param which Link index of the signaling channel.
1167 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
1169 linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
1170 linkset->linkstate[which] &= ~LINKSTATE_UP;
1171 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1175 * \brief Notify the SS7 layer that the link is no longer in alarm.
1178 * \param linkset Controlling linkset for the channel.
1179 * \param which Link index of the signaling channel.
1183 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
1185 linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
1186 linkset->linkstate[which] |= LINKSTATE_STARTING;
1187 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1191 * \brief Setup and add a SS7 link channel.
1194 * \param linkset Controlling linkset for the channel.
1195 * \param which Link index of the signaling channel.
1196 * \param ss7type Switch type of the linkset
1197 * \param transport Signaling transport of channel.
1198 * \param inalarm Non-zero if the channel is in alarm.
1199 * \param networkindicator User configuration parameter.
1200 * \param pointcode User configuration parameter.
1201 * \param adjpointcode User configuration parameter.
1203 * \retval 0 on success.
1204 * \retval -1 on error.
1206 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
1208 if (!linkset->ss7) {
1209 linkset->type = ss7type;
1210 linkset->ss7 = ss7_new(ss7type);
1211 if (!linkset->ss7) {
1212 ast_log(LOG_ERROR, "Can't create new SS7!\n");
1217 ss7_set_network_ind(linkset->ss7, networkindicator);
1218 ss7_set_pc(linkset->ss7, pointcode);
1220 if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
1221 ast_log(LOG_WARNING, "Could not add SS7 link!\n");
1225 linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
1226 ss7_link_alarm(linkset->ss7, linkset->fds[which]);
1228 linkset->linkstate[which] = LINKSTATE_DOWN;
1229 ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
1232 ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
1238 * \brief Determine if the specified channel is available for an outgoing call.
1241 * \param p Signaling private structure pointer.
1243 * \retval TRUE if the channel is available.
1245 int sig_ss7_available(struct sig_ss7_chan *p)
1248 /* Something is wrong here. A SS7 channel without the ss7 pointer? */
1252 if (!p->inalarm && !p->owner && !p->ss7call
1253 && !p->locallyblocked && !p->remotelyblocked) {
1260 static unsigned char cid_pres2ss7pres(int cid_pres)
1262 return (cid_pres >> 5) & 0x03;
1265 static unsigned char cid_pres2ss7screen(int cid_pres)
1267 return cid_pres & 0x03;
1271 * \brief Dial out using the specified SS7 channel.
1274 * \param p Signaling private structure pointer.
1275 * \param ast Asterisk channel structure pointer.
1276 * \param rdest Dialstring.
1278 * \retval 0 on success.
1279 * \retval -1 on error.
1281 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, char *rdest)
1283 char ss7_called_nai;
1284 int called_nai_strip;
1285 char ss7_calling_nai;
1286 int calling_nai_strip;
1287 const char *charge_str = NULL;
1288 const char *gen_address = NULL;
1289 const char *gen_digits = NULL;
1290 const char *gen_dig_type = NULL;
1291 const char *gen_dig_scheme = NULL;
1292 const char *gen_name = NULL;
1293 const char *jip_digits = NULL;
1294 const char *lspi_ident = NULL;
1295 const char *rlt_flag = NULL;
1296 const char *call_ref_id = NULL;
1297 const char *call_ref_pc = NULL;
1298 const char *send_far = NULL;
1303 ast_copy_string(dest, rdest, sizeof(dest));
1305 c = strchr(dest, '/');
1311 if (strlen(c) < p->stripmsd) {
1312 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1316 if (!p->hidecallerid) {
1317 l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
1322 ss7_grab(p, p->ss7);
1324 p->ss7call = isup_new_call(p->ss7->ss7);
1327 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
1331 called_nai_strip = 0;
1332 ss7_called_nai = p->ss7->called_nai;
1333 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
1334 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1335 called_nai_strip = strlen(p->ss7->internationalprefix);
1336 ss7_called_nai = SS7_NAI_INTERNATIONAL;
1337 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1338 called_nai_strip = strlen(p->ss7->nationalprefix);
1339 ss7_called_nai = SS7_NAI_NATIONAL;
1341 ss7_called_nai = SS7_NAI_SUBSCRIBER;
1344 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
1346 calling_nai_strip = 0;
1347 ss7_calling_nai = p->ss7->calling_nai;
1348 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
1349 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
1350 calling_nai_strip = strlen(p->ss7->internationalprefix);
1351 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
1352 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
1353 calling_nai_strip = strlen(p->ss7->nationalprefix);
1354 ss7_calling_nai = SS7_NAI_NATIONAL;
1356 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
1359 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
1360 p->use_callingpres ? cid_pres2ss7pres(ast->connected.id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
1361 p->use_callingpres ? cid_pres2ss7screen(ast->connected.id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
1363 isup_set_oli(p->ss7call, ast->connected.ani2);
1364 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
1366 /* Set the charge number if it is set */
1367 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
1369 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
1371 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
1373 isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type); /* need to add some types here for NAI,PRES,TYPE */
1375 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
1376 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
1377 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
1379 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
1381 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
1383 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
1385 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
1387 isup_set_jip_digits(p->ss7call, jip_digits);
1389 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
1391 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
1393 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
1394 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
1395 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
1398 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
1399 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
1400 if (call_ref_id && call_ref_pc) {
1401 isup_set_callref(p->ss7call, atoi(call_ref_id),
1402 call_ref_pc ? atoi(call_ref_pc) : 0);
1405 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
1406 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
1407 (isup_far(p->ss7->ss7, p->ss7call));
1409 p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
1410 isup_iam(p->ss7->ss7, p->ss7call);
1411 sig_ss7_set_dialing(p, 1);
1412 ast_setstate(ast, AST_STATE_DIALING);
1418 * \brief SS7 hangup channel.
1421 * \param p Signaling private structure pointer.
1422 * \param ast Asterisk channel structure pointer.
1424 * \retval 0 on success.
1425 * \retval -1 on error.
1427 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
1431 if (!ast->tech_pvt) {
1432 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1437 sig_ss7_set_dialing(p, 0);
1438 p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
1443 /* Perform low level hangup if no owner left */
1445 ss7_grab(p, p->ss7);
1446 if (!p->alreadyhungup) {
1447 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
1448 int icause = ast->hangupcause ? ast->hangupcause : -1;
1452 icause = atoi(cause);
1455 isup_rel(p->ss7->ss7, p->ss7call, icause);
1456 p->alreadyhungup = 1;
1458 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
1467 * \brief SS7 answer channel.
1470 * \param p Signaling private structure pointer.
1471 * \param ast Asterisk channel structure pointer.
1473 * \retval 0 on success.
1474 * \retval -1 on error.
1476 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
1480 ss7_grab(p, p->ss7);
1481 if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
1482 p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
1484 res = isup_anm(p->ss7->ss7, p->ss7call);
1490 * \brief Fix up a channel: If a channel is consumed, this is called. Basically update any ->owner links.
1493 * \param oldchan Old channel pointer to replace.
1494 * \param newchan New channel pointer to set.
1495 * \param pchan Signaling private structure pointer.
1499 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
1501 if (pchan->owner == oldchan) {
1502 pchan->owner = newchan;
1507 * \brief SS7 answer channel.
1510 * \param p Signaling private structure pointer.
1511 * \param chan Asterisk channel structure pointer.
1512 * \param condition AST control frame subtype.
1513 * \param data AST control frame payload contents.
1514 * \param datalen Length of payload contents.
1516 * \retval 0 on success.
1517 * \retval -1 on error or indication condition not handled.
1519 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
1523 switch (condition) {
1524 case AST_CONTROL_BUSY:
1525 res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
1527 case AST_CONTROL_RINGING:
1528 if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
1529 p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
1530 if (p->ss7 && p->ss7->ss7) {
1531 ss7_grab(p, p->ss7);
1532 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
1534 if (p->rlt != 1) /* No need to send CPG if call will be RELEASE */
1535 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
1540 res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
1542 if (chan->_state != AST_STATE_UP && chan->_state != AST_STATE_RING) {
1543 ast_setstate(chan, AST_STATE_RINGING);
1546 case AST_CONTROL_PROCEEDING:
1547 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
1548 /* This IF sends the FAR for an answered ALEG call */
1549 if (chan->_state == AST_STATE_UP && (p->rlt != 1)){
1550 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
1554 if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
1555 p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
1556 if (p->ss7 && p->ss7->ss7) {
1557 ss7_grab(p, p->ss7);
1558 isup_acm(p->ss7->ss7, p->ss7call);
1562 /* don't continue in ast_indicate */
1565 case AST_CONTROL_PROGRESS:
1566 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
1567 if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
1568 p->progress = 1;/* No need to send inband-information progress again. */
1569 if (p->ss7 && p->ss7->ss7) {
1570 ss7_grab(p, p->ss7);
1571 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
1573 /* enable echo canceler here on SS7 calls */
1574 sig_ss7_set_echocanceller(p, 1);
1577 /* don't continue in ast_indicate */
1580 case AST_CONTROL_INCOMPLETE:
1581 /* If the channel is connected, wait for additional input */
1582 if (p->call_level == SIG_SS7_CALL_LEVEL_CONNECT) {
1586 chan->hangupcause = AST_CAUSE_INVALID_NUMBER_FORMAT;
1588 case AST_CONTROL_CONGESTION:
1589 chan->hangupcause = AST_CAUSE_CONGESTION;
1591 case AST_CONTROL_HOLD:
1592 ast_moh_start(chan, data, p->mohinterpret);
1594 case AST_CONTROL_UNHOLD:
1597 case AST_CONTROL_SRCUPDATE:
1601 res = sig_ss7_play_tone(p, -1);
1608 * \brief SS7 channel request.
1611 * \param p Signaling private structure pointer.
1612 * \param law Companding law preferred
1613 * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
1614 * \param transfercapability
1616 * \retval ast_channel on success.
1617 * \retval NULL on error.
1619 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
1621 struct ast_channel *ast;
1624 ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1632 * \brief Delete the sig_ss7 private channel structure.
1635 * \param doomed sig_ss7 private channel structure to delete.
1639 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
1645 * \brief Create a new sig_ss7 private channel structure.
1648 * \param pvt_data Upper layer private data structure.
1649 * \param callback Callbacks to the upper layer.
1650 * \param ss7 Controlling linkset for the channel.
1652 * \retval sig_ss7_chan on success.
1653 * \retval NULL on error.
1655 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_callback *callback, struct sig_ss7_linkset *ss7)
1657 struct sig_ss7_chan *pvt;
1659 pvt = ast_calloc(1, sizeof(*pvt));
1664 pvt->calls = callback;
1665 pvt->chan_pvt = pvt_data;
1672 * \brief Initialize the SS7 linkset control.
1675 * \param ss7 sig_ss7 SS7 control structure.
1679 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
1683 memset(ss7, 0, sizeof(*ss7));
1685 ast_mutex_init(&ss7->lock);
1687 ss7->master = AST_PTHREADT_NULL;
1688 for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
1693 /* ------------------------------------------------------------------- */
1695 #endif /* defined(HAVE_SS7) */