d6f732138cc5fc9a63204147e22201de317a2548
[asterisk/asterisk.git] / channels / sig_pri.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2009, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief PRI signaling module
22  *
23  * \author Matthew Fredrickson <creslin@digium.com>
24  */
25
26
27 #include "asterisk.h"
28
29 #ifdef HAVE_PRI
30
31 #include <errno.h>
32 #include <ctype.h>
33 #include <signal.h>
34
35 #include "asterisk/utils.h"
36 #include "asterisk/options.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/file.h"
39 #include "asterisk/callerid.h"
40 #include "asterisk/say.h"
41 #include "asterisk/manager.h"
42 #include "asterisk/astdb.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/musiconhold.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/transcap.h"
47 #include "asterisk/features.h"
48
49 #include "sig_pri.h"
50
51 /* define this to send PRI user-user information elements */
52 #undef SUPPORT_USERUSER
53
54 #if 0
55 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
56 #else
57 #define DEFAULT_PRI_DEBUG 0
58 #endif
59
60 static int pri_matchdigittimeout = 3000;
61
62 static int pri_gendigittimeout = 8000;
63
64 #define DCHAN_NOTINALARM  (1 << 0)
65 #define DCHAN_UP          (1 << 1)
66
67 #define PRI_CHANNEL(p) ((p) & 0xff)
68 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
69 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
70
71
72 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
73
74 #define PRI_DEADLOCK_AVOIDANCE(p) \
75         do { \
76                 sig_pri_unlock_private(p); \
77                 usleep(1); \
78                 sig_pri_lock_private(p); \
79         } while (0)
80
81 static int pri_active_dchan_index(struct sig_pri_pri *pri);
82
83 static inline void pri_rel(struct sig_pri_pri *pri)
84 {
85         ast_mutex_unlock(&pri->lock);
86 }
87
88 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
89 {
90         int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? 0x10000 : 0));
91         ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
92                 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
93
94         return res;
95 }
96
97 static void sig_pri_handle_dchan_exception(struct sig_pri_pri *pri, int index)
98 {
99         if (pri->calls->handle_dchan_exception)
100                 pri->calls->handle_dchan_exception(pri, index);
101 }
102
103 static void sig_pri_set_dialing(struct sig_pri_chan *p, int flag)
104 {
105         if (p->calls->set_dialing)
106                 p->calls->set_dialing(p->chan_pvt, flag);
107 }
108
109 /*!
110  * \internal
111  * \brief Set the caller id information in the parent module.
112  * \since 1.6.3
113  *
114  * \param p sig_pri channel structure.
115  *
116  * \return Nothing
117  */
118 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
119 {
120         struct ast_party_caller caller;
121
122         if (p->calls->set_callerid) {
123                 ast_party_caller_init(&caller);
124                 caller.id.number = p->cid_num;
125                 caller.id.name = p->cid_name;
126                 caller.id.number_type = p->cid_ton;
127                 caller.id.number_presentation = p->callingpres;
128                 caller.ani = p->cid_ani;
129                 caller.ani2 = p->cid_ani2;
130                 p->calls->set_callerid(p->chan_pvt, &caller);
131         }
132 }
133
134 /*!
135  * \internal
136  * \brief Set the Dialed Number Identifier.
137  * \since 1.6.3
138  *
139  * \param p sig_pri channel structure.
140  * \param dnid Dialed Number Identifier string.
141  *
142  * \return Nothing
143  */
144 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
145 {
146         if (p->calls->set_dnid) {
147                 p->calls->set_dnid(p->chan_pvt, dnid);
148         }
149 }
150
151 /*!
152  * \internal
153  * \brief Set the Redirecting Directory Number Information Service (RDNIS).
154  * \since 1.6.3
155  *
156  * \param p sig_pri channel structure.
157  * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
158  *
159  * \return Nothing
160  */
161 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
162 {
163         if (p->calls->set_rdnis) {
164                 p->calls->set_rdnis(p->chan_pvt, rdnis);
165         }
166 }
167
168 static void sig_pri_unlock_private(struct sig_pri_chan *p)
169 {
170         if (p->calls->unlock_private)
171                 p->calls->unlock_private(p->chan_pvt);
172 }
173
174 static void sig_pri_lock_private(struct sig_pri_chan *p)
175 {
176         if (p->calls->lock_private)
177                 p->calls->lock_private(p->chan_pvt);
178 }
179
180 static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_pri *pri)
181 {
182         int res;
183         /* Grab the lock first */
184         do {
185                 res = ast_mutex_trylock(&pri->lock);
186                 if (res) {
187                         PRI_DEADLOCK_AVOIDANCE(p);
188                 }
189         } while (res);
190         /* Then break the poll */
191         pthread_kill(pri->master, SIGURG);
192         return 0;
193 }
194
195 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
196 {
197         if (p->calls->set_echocanceller)
198                 return p->calls->set_echocanceller(p->chan_pvt, enable);
199         else
200                 return -1;
201 }
202
203 static void sig_pri_fixup_chans(struct sig_pri_chan *old, struct sig_pri_chan *new)
204 {
205         if (old->calls->fixup_chans)
206                 old->calls->fixup_chans(old->chan_pvt, new->chan_pvt);
207 }
208
209 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
210 {
211         if (p->calls->play_tone)
212                 return p->calls->play_tone(p->chan_pvt, tone);
213         else
214                 return -1;
215 }
216
217 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)
218 {
219         struct ast_channel *c;
220
221         if (p->calls->new_ast_channel)
222                 c = p->calls->new_ast_channel(p->chan_pvt, state, startpbx, ulaw, transfercapability, exten, requestor);
223         else
224                 return NULL;
225
226         if (!p->owner)
227                 p->owner = c;
228         p->isidlecall = 0;
229         p->alreadyhungup = 0;
230
231         return c;
232 }
233
234 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor)
235 {
236         ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
237
238         return sig_pri_new_ast_channel(p, AST_STATE_RESERVED, 0, law, 0, p->exten, requestor);
239 }
240
241 int pri_is_up(struct sig_pri_pri *pri)
242 {
243         int x;
244         for (x = 0; x < NUM_DCHANS; x++) {
245                 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
246                         return 1;
247         }
248         return 0;
249 }
250
251 static char *pri_order(int level)
252 {
253         switch (level) {
254         case 0:
255                 return "Primary";
256         case 1:
257                 return "Secondary";
258         case 2:
259                 return "Tertiary";
260         case 3:
261                 return "Quaternary";
262         default:
263                 return "<Unknown>";
264         }
265 }
266
267 /* Returns index of the active dchan */
268 static int pri_active_dchan_index(struct sig_pri_pri *pri)
269 {
270         int x;
271
272         for (x = 0; x < NUM_DCHANS; x++) {
273                 if ((pri->dchans[x] == pri->pri))
274                         return x;
275         }
276
277         ast_log(LOG_WARNING, "No active dchan found!\n");
278         return -1;
279 }
280
281 static int pri_find_dchan(struct sig_pri_pri *pri)
282 {
283         int oldslot = -1;
284         struct pri *old;
285         int newslot = -1;
286         int x;
287         old = pri->pri;
288         for (x = 0; x < NUM_DCHANS; x++) {
289                 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
290                         newslot = x;
291                 if (pri->dchans[x] == old) {
292                         oldslot = x;
293                 }
294         }
295         if (newslot < 0) {
296                 newslot = 0;
297                 /* This is annoying to see on non persistent layer 2 connections.  Let's not complain in that case */
298                 if (pri->sig != SIG_BRI_PTMP) {
299                         ast_log(LOG_WARNING, "No D-channels available!  Using Primary channel as D-channel anyway!\n");
300                 }
301         }
302         if (old && (oldslot != newslot))
303                 ast_log(LOG_NOTICE, "Switching from d-channel fd %d to fd %d!\n",
304                         pri->fds[oldslot], pri->fds[newslot]);
305         pri->pri = pri->dchans[newslot];
306         return 0;
307 }
308 static void pri_update_cid(struct sig_pri_chan *p, struct sig_pri_pri *pri)
309 {
310         /* We must unlock the PRI to avoid the possibility of a deadlock */
311         if (pri)
312                 ast_mutex_unlock(&pri->lock);
313         for (;;) {
314                 if (p->owner) {
315                         if (ast_channel_trylock(p->owner)) {
316                                 PRI_DEADLOCK_AVOIDANCE(p);
317                         } else {
318                                 ast_set_callerid(p->owner, S_OR(p->lastcid_num, NULL),
319                                                         S_OR(p->lastcid_name, NULL),
320                                                         S_OR(p->lastcid_num, NULL)
321                                                         );
322                                 ast_channel_unlock(p->owner);
323                                 break;
324                         }
325                 } else
326                         break;
327         }
328         if (pri)
329                 ast_mutex_lock(&pri->lock);
330 }
331
332 static void pri_queue_frame(struct sig_pri_chan *p, struct ast_frame *f, struct sig_pri_pri *pri)
333 {
334         /* We must unlock the PRI to avoid the possibility of a deadlock */
335         if (pri)
336                 ast_mutex_unlock(&pri->lock);
337         for (;;) {
338                 if (p->owner) {
339                         if (ast_channel_trylock(p->owner)) {
340                                 PRI_DEADLOCK_AVOIDANCE(p);
341                         } else {
342                                 ast_queue_frame(p->owner, f);
343                                 ast_channel_unlock(p->owner);
344                                 break;
345                         }
346                 } else
347                         break;
348         }
349         if (pri)
350                 ast_mutex_lock(&pri->lock);
351 }
352
353 static void pri_queue_control(struct sig_pri_chan *p, int subclass, struct sig_pri_pri *pri)
354 {
355         struct ast_frame f = {AST_FRAME_CONTROL, };
356
357         f.subclass = subclass;
358         pri_queue_frame(p, &f, pri);
359 }
360
361 static int pri_find_principle(struct sig_pri_pri *pri, int channel)
362 {
363         int x;
364         int span = PRI_SPAN(channel);
365         int principle = -1;
366         int explicit = PRI_EXPLICIT(channel);
367         channel = PRI_CHANNEL(channel);
368
369         if (!explicit) {
370                 int index = pri_active_dchan_index(pri);
371                 if (index == -1)
372                         return -1;
373                 span = pri->dchan_logical_span[index];
374         }
375
376         for (x = 0; x < pri->numchans; x++) {
377                 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
378                         principle = x;
379                         break;
380                 }
381         }
382
383         return principle;
384 }
385
386 static int pri_fixup_principle(struct sig_pri_pri *pri, int principle, q931_call *c)
387 {
388         int x;
389         if (!c) {
390                 if (principle < 0)
391                         return -1;
392                 return principle;
393         }
394         if ((principle > -1) &&
395                 (principle < pri->numchans) &&
396                 (pri->pvts[principle]) &&
397                 (pri->pvts[principle]->call == c))
398                 return principle;
399         /* First, check for other bearers */
400         for (x = 0; x < pri->numchans; x++) {
401                 if (!pri->pvts[x])
402                         continue;
403                 if (pri->pvts[x]->call == c) {
404                         /* Found our call */
405                         if (principle != x) {
406                                 struct sig_pri_chan *new = pri->pvts[principle], *old = pri->pvts[x];
407
408                                 ast_verb(3, "Moving call from channel %d to channel %d\n",
409                                         old->channel, new->channel);
410                                 if (new->owner) {
411                                         ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
412                                                 old->channel, new->channel, new->channel);
413                                         return -1;
414                                 }
415
416                                 sig_pri_fixup_chans(old, new);
417                                 /* Fix it all up now */
418                                 new->owner = old->owner;
419                                 old->owner = NULL;
420
421                                 new->call = old->call;
422                                 old->call = NULL;
423
424                         }
425                         return principle;
426                 }
427         }
428         ast_log(LOG_WARNING, "Call specified, but not found?\n");
429         return -1;
430 }
431
432 static char * redirectingreason2str(int redirectingreason)
433 {
434         switch (redirectingreason) {
435         case 0:
436                 return "UNKNOWN";
437         case 1:
438                 return "BUSY";
439         case 2:
440                 return "NO_REPLY";
441         case 0xF:
442                 return "UNCONDITIONAL";
443         default:
444                 return "NOREDIRECT";
445         }
446 }
447
448 static char *dialplan2str(int dialplan)
449 {
450         if (dialplan == -1) {
451                 return("Dynamically set dialplan in ISDN");
452         }
453         return (pri_plan2str(dialplan));
454 }
455
456 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_pri *pri, const char *number, const int plan)
457 {
458         switch (plan) {
459         case PRI_INTERNATIONAL_ISDN:            /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
460                 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
461                 break;
462         case PRI_NATIONAL_ISDN:                 /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
463                 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
464                 break;
465         case PRI_LOCAL_ISDN:                    /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
466                 snprintf(buf, size, "%s%s", pri->localprefix, number);
467                 break;
468         case PRI_PRIVATE:                       /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
469                 snprintf(buf, size, "%s%s", pri->privateprefix, number);
470                 break;
471         case PRI_UNKNOWN:                       /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
472                 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
473                 break;
474         default:                                /* other Q.931 dialplan => don't twiddle with callingnum */
475                 snprintf(buf, size, "%s", number);
476                 break;
477         }
478 }
479
480 static int pri_check_restart(struct sig_pri_pri *pri)
481 {
482 #ifdef HAVE_PRI_SERVICE_MESSAGES
483 tryanotherpos:
484 #endif
485         do {
486                 pri->resetpos++;
487         } while ((pri->resetpos < pri->numchans) &&
488                 (!pri->pvts[pri->resetpos] ||
489                 pri->pvts[pri->resetpos]->call ||
490                 pri->pvts[pri->resetpos]->resetting));
491         if (pri->resetpos < pri->numchans) {
492 #ifdef HAVE_PRI_SERVICE_MESSAGES
493                 char db_chan_name[20], db_answer[5], state;
494                 int why;
495
496                 /* check if the channel is out of service */
497                 ast_mutex_lock(&pri->pvts[pri->resetpos]->service_lock);
498                 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[pri->resetpos]->pri->span, pri->pvts[pri->resetpos]->channel);
499                 ast_mutex_unlock(&pri->pvts[pri->resetpos]->service_lock);
500
501                 /* if so, try next channel */
502                 if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
503                         sscanf(db_answer, "%c:%d", &state, &why);
504                         if (why) {
505                                 ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), not sending RESTART\n", pri->span,
506                                 pri->pvts[pri->resetpos]->channel, (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
507                                 goto tryanotherpos;
508                         }
509                 }
510 #endif
511
512                 /* Mark the channel as resetting and restart it */
513                 pri->pvts[pri->resetpos]->resetting = 1;
514                 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
515         } else {
516                 pri->resetting = 0;
517                 time(&pri->lastreset);
518         }
519         return 0;
520 }
521
522 static int pri_find_empty_chan(struct sig_pri_pri *pri, int backwards)
523 {
524         int x;
525         if (backwards)
526                 x = pri->numchans;
527         else
528                 x = 0;
529         for (;;) {
530                 if (backwards && (x < 0))
531                         break;
532                 if (!backwards && (x >= pri->numchans))
533                         break;
534                 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
535                         ast_debug(1, "Found empty available channel %d/%d\n",
536                                 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
537                         return x;
538                 }
539                 if (backwards)
540                         x--;
541                 else
542                         x++;
543         }
544         return -1;
545 }
546
547 static void *do_idle_thread(void *vchan)
548 {
549         struct ast_channel *chan = vchan;
550         struct sig_pri_chan *pvt = chan->tech_pvt;
551         struct ast_frame *f;
552         char ex[80];
553         /* Wait up to 30 seconds for an answer */
554         int newms, ms = 30000;
555         ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
556         snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
557         if (ast_call(chan, ex, 0)) {
558                 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
559                 ast_hangup(chan);
560                 return NULL;
561         }
562         while ((newms = ast_waitfor(chan, ms)) > 0) {
563                 f = ast_read(chan);
564                 if (!f) {
565                         /* Got hangup */
566                         break;
567                 }
568                 if (f->frametype == AST_FRAME_CONTROL) {
569                         switch (f->subclass) {
570                         case AST_CONTROL_ANSWER:
571                                 /* Launch the PBX */
572                                 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
573                                 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
574                                 chan->priority = 1;
575                                 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
576                                 ast_pbx_run(chan);
577                                 /* It's already hungup, return immediately */
578                                 return NULL;
579                         case AST_CONTROL_BUSY:
580                                 ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
581                                 break;
582                         case AST_CONTROL_CONGESTION:
583                                 ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
584                                 break;
585                         };
586                 }
587                 ast_frfree(f);
588                 ms = newms;
589         }
590         /* Hangup the channel since nothing happend */
591         ast_hangup(chan);
592         return NULL;
593 }
594
595 static void *pri_ss_thread(void *data)
596 {
597         struct sig_pri_chan *p = data;
598         struct ast_channel *chan = p->owner;
599         char exten[AST_MAX_EXTENSION];
600         int res;
601         int len;
602         int timeout;
603
604         if (!chan) {
605                 /* We lost the owner before we could get started. */
606                 return NULL;
607         }
608
609         /*
610          * In the bizarre case where the channel has become a zombie before we
611          * even get started here, abort safely.
612          */
613         if (!chan->tech_pvt) {
614                 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
615                 ast_hangup(chan);
616                 return NULL;
617         }
618
619         ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
620
621         /* Now loop looking for an extension */
622         ast_copy_string(exten, p->exten, sizeof(exten));
623         len = strlen(exten);
624         res = 0;
625         while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
626                 if (len && !ast_ignore_pattern(chan->context, exten))
627                         sig_pri_play_tone(p, -1);
628                 else
629                         sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
630                 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
631                         timeout = pri_matchdigittimeout;
632                 else
633                         timeout = pri_gendigittimeout;
634                 res = ast_waitfordigit(chan, timeout);
635                 if (res < 0) {
636                         ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
637                         ast_hangup(chan);
638                         return NULL;
639                 } else if (res) {
640                         exten[len++] = res;
641                         exten[len] = '\0';
642                 } else
643                         goto exit;
644         }
645         /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
646         if (ast_strlen_zero(exten)) {
647                 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
648                 exten[0] = 's';
649                 exten[1] = '\0';
650         }
651         sig_pri_play_tone(p, -1);
652         if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
653                 /* Start the real PBX */
654                 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
655                 sig_pri_set_echocanceller(p, 1);
656                 ast_setstate(chan, AST_STATE_RING);
657                 res = ast_pbx_run(chan);
658                 if (res) {
659                         ast_log(LOG_WARNING, "PBX exited non-zero!\n");
660                 }
661         } else {
662                 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
663                 chan->hangupcause = AST_CAUSE_UNALLOCATED;
664                 ast_hangup(chan);
665                 p->exten[0] = '\0';
666                 /* Since we send release complete here, we won't get one */
667                 p->call = NULL;
668         }
669         return NULL;
670
671 exit:
672         res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
673         if (res < 0)
674                 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
675         ast_hangup(chan);
676         return NULL;
677 }
678
679 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri)
680 {
681         pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
682         if (!before_start_pri)
683                 pri_find_dchan(pri);
684 }
685
686 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
687 {
688         pri->dchanavail[index] |= DCHAN_NOTINALARM;
689         if (!before_start_pri)
690                 pri_restart(pri->dchans[index]);
691 }
692
693 #if defined(SUPPORT_USERUSER)
694 /*!
695  * \internal
696  * \brief Obtain the sig_pri owner channel lock if the owner exists.
697  * \since 1.6.3
698  *
699  * \param pri sig_pri PRI control structure.
700  * \param chanpos Channel position in the span.
701  *
702  * \note Assumes the pri->lock is already obtained.
703  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
704  *
705  * \return Nothing
706  */
707 static void sig_pri_lock_owner(struct sig_pri_pri *pri, int chanpos)
708 {
709         for (;;) {
710                 if (!pri->pvts[chanpos]->owner) {
711                         /* There is no owner lock to get. */
712                         break;
713                 }
714                 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
715                         /* We got the lock */
716                         break;
717                 }
718                 /* We must unlock the PRI to avoid the possibility of a deadlock */
719                 ast_mutex_unlock(&pri->lock);
720                 PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
721                 ast_mutex_lock(&pri->lock);
722         }
723 }
724 #endif  /* defined(SUPPORT_USERUSER) */
725
726 static void *pri_dchannel(void *vpri)
727 {
728         struct sig_pri_pri *pri = vpri;
729         pri_event *e;
730         struct pollfd fds[NUM_DCHANS];
731         int res;
732         int chanpos = 0;
733         int x;
734         struct ast_channel *c;
735         struct timeval tv, lowest, *next;
736         int doidling=0;
737         char *cc;
738         time_t t;
739         int i, which=-1;
740         int numdchans;
741         pthread_t threadid;
742         char ani2str[6];
743         char plancallingnum[AST_MAX_EXTENSION];
744         char plancallingani[AST_MAX_EXTENSION];
745         char calledtonstr[10];
746         struct timeval lastidle = { 0, 0 };
747         pthread_t p;
748         struct ast_channel *idle;
749         char idlen[80];
750         int nextidle = -1;
751         int haveidles;
752         int activeidles;
753
754         gettimeofday(&lastidle, NULL);
755         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
756
757         if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
758                 /* Need to do idle dialing, check to be sure though */
759                 cc = strchr(pri->idleext, '@');
760                 if (cc) {
761                         *cc = '\0';
762                         cc++;
763                         ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
764 #if 0
765                         /* Extensions may not be loaded yet */
766                         if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
767                                 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
768                         else
769 #endif
770                                 doidling = 1;
771                 } else
772                         ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
773         }
774         for (;;) {
775                 for (i = 0; i < NUM_DCHANS; i++) {
776                         if (!pri->dchans[i])
777                                 break;
778                         fds[i].fd = pri->fds[i];
779                         fds[i].events = POLLIN | POLLPRI;
780                         fds[i].revents = 0;
781                 }
782                 numdchans = i;
783                 time(&t);
784                 ast_mutex_lock(&pri->lock);
785                 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
786                         if (pri->resetting && pri_is_up(pri)) {
787                                 if (pri->resetpos < 0)
788                                         pri_check_restart(pri);
789                         } else {
790                                 if (!pri->resetting     && (t - pri->lastreset) >= pri->resetinterval) {
791                                         pri->resetting = 1;
792                                         pri->resetpos = -1;
793                                 }
794                         }
795                 }
796                 /* Look for any idle channels if appropriate */
797                 if (doidling && pri_is_up(pri)) {
798                         nextidle = -1;
799                         haveidles = 0;
800                         activeidles = 0;
801                         for (x = pri->numchans; x >= 0; x--) {
802                                 if (pri->pvts[x] && !pri->pvts[x]->owner &&
803                                         !pri->pvts[x]->call) {
804                                         if (haveidles < pri->minunused) {
805                                                 haveidles++;
806                                         } else if (!pri->pvts[x]->resetting) {
807                                                 nextidle = x;
808                                                 break;
809                                         }
810                                 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
811                                         activeidles++;
812                         }
813                         if (nextidle > -1) {
814                                 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
815                                         /* Don't create a new idle call more than once per second */
816                                         snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
817                                         idle = sig_pri_request(pri->pvts[nextidle], AST_FORMAT_ULAW, NULL);
818                                         if (idle) {
819                                                 pri->pvts[nextidle]->isidlecall = 1;
820                                                 if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) {
821                                                         ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
822                                                         ast_hangup(idle);
823                                                 }
824                                         } else
825                                                 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
826                                         gettimeofday(&lastidle, NULL);
827                                 }
828                         } else if ((haveidles < pri->minunused) &&
829                                 (activeidles > pri->minidle)) {
830                                 /* Mark something for hangup if there is something
831                                    that can be hungup */
832                                 for (x = pri->numchans; x >= 0; x--) {
833                                         /* find a candidate channel */
834                                         if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
835                                                 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
836                                                 haveidles++;
837                                                 /* Stop if we have enough idle channels or
838                                                   can't spare any more active idle ones */
839                                                 if ((haveidles >= pri->minunused) ||
840                                                         (activeidles <= pri->minidle))
841                                                         break;
842                                         }
843                                 }
844                         }
845                 }
846                 /* Start with reasonable max */
847                 lowest = ast_tv(60, 0);
848                 for (i = 0; i < NUM_DCHANS; i++) {
849                         /* Find lowest available d-channel */
850                         if (!pri->dchans[i])
851                                 break;
852                         if ((next = pri_schedule_next(pri->dchans[i]))) {
853                                 /* We need relative time here */
854                                 tv = ast_tvsub(*next, ast_tvnow());
855                                 if (tv.tv_sec < 0) {
856                                         tv = ast_tv(0,0);
857                                 }
858                                 if (doidling || pri->resetting) {
859                                         if (tv.tv_sec > 1) {
860                                                 tv = ast_tv(1, 0);
861                                         }
862                                 } else {
863                                         if (tv.tv_sec > 60) {
864                                                 tv = ast_tv(60, 0);
865                                         }
866                                 }
867                         } else if (doidling || pri->resetting) {
868                                 /* Make sure we stop at least once per second if we're
869                                    monitoring idle channels */
870                                 tv = ast_tv(1,0);
871                         } else {
872                                 /* Don't poll for more than 60 seconds */
873                                 tv = ast_tv(60, 0);
874                         }
875                         if (!i || ast_tvcmp(tv, lowest) < 0) {
876                                 lowest = tv;
877                         }
878                 }
879                 ast_mutex_unlock(&pri->lock);
880
881                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
882                 pthread_testcancel();
883                 e = NULL;
884                 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
885                 pthread_testcancel();
886                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
887
888                 ast_mutex_lock(&pri->lock);
889                 if (!res) {
890                         for (which = 0; which < NUM_DCHANS; which++) {
891                                 if (!pri->dchans[which])
892                                         break;
893                                 /* Just a timeout, run the scheduler */
894                                 e = pri_schedule_run(pri->dchans[which]);
895                                 if (e)
896                                         break;
897                         }
898                 } else if (res > -1) {
899                         for (which = 0; which < NUM_DCHANS; which++) {
900                                 if (!pri->dchans[which])
901                                         break;
902                                 if (fds[which].revents & POLLPRI) {
903                                         sig_pri_handle_dchan_exception(pri, which);
904                                 } else if (fds[which].revents & POLLIN) {
905                                         e = pri_check_event(pri->dchans[which]);
906                                 }
907                                 if (e)
908                                         break;
909                         }
910                 } else if (errno != EINTR)
911                         ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
912
913                 if (e) {
914                         if (pri->debug)
915                                 pri_dump_event(pri->dchans[which], e);
916
917                         if (e->e != PRI_EVENT_DCHAN_DOWN) {
918                                 if (!(pri->dchanavail[which] & DCHAN_UP)) {
919                                         ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
920                                 }
921                                 pri->dchanavail[which] |= DCHAN_UP;
922                         } else {
923                                 if (pri->dchanavail[which] & DCHAN_UP) {
924                                         ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
925                                 }
926                                 pri->dchanavail[which] &= ~DCHAN_UP;
927                         }
928
929                         if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
930                                 /* Must be an NFAS group that has the secondary dchan active */
931                                 pri->pri = pri->dchans[which];
932
933                         switch (e->e) {
934                         case PRI_EVENT_DCHAN_UP:
935                                 if (!pri->pri) pri_find_dchan(pri);
936
937                                 /* Note presense of D-channel */
938                                 time(&pri->lastreset);
939
940                                 /* Restart in 5 seconds */
941                                 if (pri->resetinterval > -1) {
942                                         pri->lastreset -= pri->resetinterval;
943                                         pri->lastreset += 5;
944                                 }
945                                 pri->resetting = 0;
946                                 /* Take the channels from inalarm condition */
947                                 for (i = 0; i < pri->numchans; i++)
948                                         if (pri->pvts[i]) {
949                                                 pri->pvts[i]->inalarm = 0;
950                                         }
951                                 break;
952                         case PRI_EVENT_DCHAN_DOWN:
953                                 pri_find_dchan(pri);
954                                 if (!pri_is_up(pri)) {
955                                         pri->resetting = 0;
956                                         /* Hangup active channels and put them in alarm mode */
957                                         for (i = 0; i < pri->numchans; i++) {
958                                                 struct sig_pri_chan *p = pri->pvts[i];
959                                                 if (p) {
960                                                         if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
961                                                                 /* T309 is not enabled : hangup calls when alarm occurs */
962                                                                 if (p->call) {
963                                                                         if (p->pri && p->pri->pri) {
964                                                                                 pri_hangup(p->pri->pri, p->call, -1);
965                                                                                 pri_destroycall(p->pri->pri, p->call);
966                                                                                 p->call = NULL;
967                                                                         } else
968                                                                                 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
969                                                                 }
970                                                                 if (p->owner)
971                                                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
972                                                         }
973                                                         /* For PTMP connections with non persistent layer 2 we want
974                                                          * to *not* declare inalarm unless there actually is an alarm */
975                                                         if (p->pri->sig != SIG_BRI_PTMP) {
976                                                                 p->inalarm = 1;
977                                                         }
978                                                 }
979                                         }
980                                 }
981                                 break;
982                         case PRI_EVENT_RESTART:
983                                 if (e->restart.channel > -1) {
984                                         chanpos = pri_find_principle(pri, e->restart.channel);
985                                         if (chanpos < 0)
986                                                 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
987                                                         PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
988                                         else {
989 #ifdef HAVE_PRI_SERVICE_MESSAGES
990                                                 char db_chan_name[20], db_answer[5], state;
991                                                 int why, skipit = 0;
992
993                                                 ast_mutex_lock(&pri->pvts[chanpos]->service_lock);
994                                                 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[chanpos]->pri->span, pri->pvts[chanpos]->channel);
995                                                 ast_mutex_unlock(&pri->pvts[chanpos]->service_lock);
996
997                                                 if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
998                                                         sscanf(db_answer, "%c:%d", &state, &why);
999                                                         if (why) {
1000                                                                 ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), ignoring RESTART\n", pri->span,
1001                                                                         e->restart.channel, (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1002                                                                 skipit = 1;
1003                                                         } else {
1004                                                                 ast_db_del(db_chan_name, SRVST_DBKEY);
1005                                                         }
1006                                                 }
1007                                                 if (!skipit) {
1008 #endif
1009                                                         ast_verb(3, "B-channel %d/%d restarted on span %d\n",
1010                                                                 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
1011                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1012                                                         if (pri->pvts[chanpos]->call) {
1013                                                                 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
1014                                                                 pri->pvts[chanpos]->call = NULL;
1015                                                         }
1016 #ifdef HAVE_PRI_SERVICE_MESSAGES
1017                                                 }
1018 #endif
1019                                                 /* Force soft hangup if appropriate */
1020                                                 if (pri->pvts[chanpos]->owner)
1021                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1022                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1023                                         }
1024                                 } else {
1025                                         ast_verb(3, "Restart on requested on entire span %d\n", pri->span);
1026                                         for (x = 0; x < pri->numchans; x++)
1027                                                 if (pri->pvts[x]) {
1028                                                         sig_pri_lock_private(pri->pvts[x]);
1029                                                         if (pri->pvts[x]->call) {
1030                                                                 pri_destroycall(pri->pri, pri->pvts[x]->call);
1031                                                                 pri->pvts[x]->call = NULL;
1032                                                         }
1033                                                         if (pri->pvts[x]->owner)
1034                                                                 ast_softhangup_nolock(pri->pvts[x]->owner, AST_SOFTHANGUP_DEV);
1035                                                         sig_pri_unlock_private(pri->pvts[x]);
1036                                                 }
1037                                 }
1038                                 break;
1039                         case PRI_EVENT_KEYPAD_DIGIT:
1040                                 chanpos = pri_find_principle(pri, e->digit.channel);
1041                                 if (chanpos < 0) {
1042                                         ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
1043                                                 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
1044                                 } else {
1045                                         chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
1046                                         if (chanpos > -1) {
1047                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1048                                                 /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
1049                                                 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1050                                                         && pri->pvts[chanpos]->call == e->digit.call
1051                                                         && pri->pvts[chanpos]->owner) {
1052                                                         /* how to do that */
1053                                                         int digitlen = strlen(e->digit.digits);
1054                                                         int i;
1055
1056                                                         for (i = 0; i < digitlen; i++) {
1057                                                                 struct ast_frame f = { AST_FRAME_DTMF, e->digit.digits[i], };
1058
1059                                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1060                                                         }
1061                                                 }
1062                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1063                                         }
1064                                 }
1065                                 break;
1066
1067                         case PRI_EVENT_INFO_RECEIVED:
1068                                 chanpos = pri_find_principle(pri, e->ring.channel);
1069                                 if (chanpos < 0) {
1070                                         ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
1071                                                 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1072                                 } else {
1073                                         chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
1074                                         if (chanpos > -1) {
1075                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1076                                                 /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
1077                                                 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1078                                                         && pri->pvts[chanpos]->call == e->ring.call
1079                                                         && pri->pvts[chanpos]->owner) {
1080                                                         /* how to do that */
1081                                                         int digitlen = strlen(e->ring.callednum);
1082                                                         int i;
1083
1084                                                         for (i = 0; i < digitlen; i++) {
1085                                                                 struct ast_frame f = { AST_FRAME_DTMF, e->ring.callednum[i], };
1086
1087                                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1088                                                         }
1089                                                 }
1090                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1091                                         }
1092                                 }
1093                                 break;
1094 #ifdef HAVE_PRI_SERVICE_MESSAGES
1095                         case PRI_EVENT_SERVICE:
1096                                 chanpos = pri_find_principle(pri, e->service.channel);
1097                                 if (chanpos < 0) {
1098                                         ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
1099                                                 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
1100                                 } else {
1101                                         char db_chan_name[20], db_answer[5], state;
1102                                         int ch, why = -1;
1103
1104                                         ast_mutex_lock(&pri->pvts[chanpos]->service_lock);
1105                                         ch = pri->pvts[chanpos]->channel;
1106                                         ast_mutex_unlock(&pri->pvts[chanpos]->service_lock);
1107
1108                                         snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[chanpos]->pri->span, ch);
1109                                         if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
1110                                                 sscanf(db_answer, "%c:%d", &state, &why);
1111                                                 ast_db_del(db_chan_name, SRVST_DBKEY);
1112                                         }
1113                                         switch (e->service.changestatus) {
1114                                         case 0: /* in-service */
1115                                                 if (why > -1) {
1116                                                         if (why & SRVST_NEAREND) {
1117                                                                 snprintf(db_answer, sizeof(db_answer), "%s:%d", SRVST_TYPE_OOS, SRVST_NEAREND);
1118                                                                 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
1119                                                                 ast_debug(2, "channel '%d' service state { near: out-of-service,  far: in-service }\n", ch);
1120                                                         }
1121                                                 }
1122                                                 break;
1123                                         case 2: /* out-of-service */
1124                                                 if (why == -1) {
1125                                                         why = SRVST_FAREND;
1126                                                 } else {
1127                                                         why |= SRVST_FAREND;
1128                                                 }
1129                                                 snprintf(db_answer, sizeof(db_answer), "%s:%d", SRVST_TYPE_OOS, why);
1130                                                 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
1131                                                 break;
1132                                         default:
1133                                                 ast_log(LOG_ERROR, "Huh?  changestatus is: %d\n", e->service.changestatus);
1134                                         }
1135                                         ast_log(LOG_NOTICE, "Channel %d/%d span %d (logical: %d) received a change of service message, status '%d'\n",
1136                                                 PRI_SPAN(e->service.channel), PRI_CHANNEL(e->service.channel), pri->span, ch, e->service.changestatus);
1137                                 }
1138                                 break;
1139                         case PRI_EVENT_SERVICE_ACK:
1140                                 chanpos = pri_find_principle(pri, e->service_ack.channel);
1141                                 if (chanpos < 0) {
1142                                         ast_log(LOG_WARNING, "Received service acknowledge change status '%d' on unconfigured channel %d/%d span %d\n",
1143                                                 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
1144                                 } else {
1145                                         ast_debug(2, "Channel %d/%d span %d received a change os service acknowledgement message, status '%d'\n",
1146                                                 PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span, e->service_ack.changestatus);
1147                                 }
1148                                 break;
1149 #endif
1150                         case PRI_EVENT_RING:
1151                                 if (e->ring.channel == -1)
1152                                         chanpos = pri_find_empty_chan(pri, 1);
1153                                 else
1154                                         chanpos = pri_find_principle(pri, e->ring.channel);
1155                                 /* if no channel specified find one empty */
1156                                 if (chanpos < 0) {
1157                                         ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
1158                                                 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1159                                 } else {
1160                                         sig_pri_lock_private(pri->pvts[chanpos]);
1161                                         if (pri->pvts[chanpos]->owner) {
1162                                                 if (pri->pvts[chanpos]->call == e->ring.call) {
1163                                                         ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
1164                                                                 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1165                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1166                                                         break;
1167                                                 } else {
1168                                                         /* This is where we handle initial glare */
1169                                                         ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d.  Attempting to renegotiating channel.\n",
1170                                                         PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1171                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1172                                                         chanpos = -1;
1173                                                 }
1174                                         }
1175                                         if (chanpos > -1)
1176                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1177                                 }
1178                                 if ((chanpos < 0) && (e->ring.flexible))
1179                                         chanpos = pri_find_empty_chan(pri, 1);
1180                                 if (chanpos > -1) {
1181                                         sig_pri_lock_private(pri->pvts[chanpos]);
1182                                         pri->pvts[chanpos]->call = e->ring.call;
1183
1184                                         /* Use plancallingnum as a scratch buffer since it is initialized next. */
1185                                         apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri,
1186                                                 e->ring.redirectingnum, e->ring.callingplanrdnis);
1187                                         sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
1188
1189                                         /* Setup caller-id info */
1190                                         apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
1191                                         pri->pvts[chanpos]->cid_ani2 = 0;
1192                                         if (pri->pvts[chanpos]->use_callerid) {
1193                                                 ast_shrink_phone_number(plancallingnum);
1194                                                 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
1195 #ifdef PRI_ANI
1196                                                 if (!ast_strlen_zero(e->ring.callingani)) {
1197                                                         apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
1198                                                         ast_shrink_phone_number(plancallingani);
1199                                                         ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
1200                                                 } else {
1201                                                         pri->pvts[chanpos]->cid_ani[0] = '\0';
1202                                                 }
1203 #endif
1204                                                 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
1205                                                 pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
1206                                                 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
1207                                                 if (e->ring.ani2 >= 0) {
1208                                                         pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
1209                                                 }
1210                                         } else {
1211                                                 pri->pvts[chanpos]->cid_num[0] = '\0';
1212                                                 pri->pvts[chanpos]->cid_ani[0] = '\0';
1213                                                 pri->pvts[chanpos]->cid_name[0] = '\0';
1214                                                 pri->pvts[chanpos]->cid_ton = 0;
1215                                                 pri->pvts[chanpos]->callingpres = 0;
1216                                         }
1217                                         sig_pri_set_caller_id(pri->pvts[chanpos]);
1218
1219                                         /* Set DNID on all incoming calls -- even immediate */
1220                                         sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
1221
1222                                         /* If immediate=yes go to s|1 */
1223                                         if (pri->pvts[chanpos]->immediate) {
1224                                                 ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
1225                                                 pri->pvts[chanpos]->exten[0] = 's';
1226                                                 pri->pvts[chanpos]->exten[1] = '\0';
1227                                         }
1228                                         /* Get called number */
1229                                         else if (!ast_strlen_zero(e->ring.callednum)) {
1230                                                 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
1231                                         } else if (pri->overlapdial)
1232                                                 pri->pvts[chanpos]->exten[0] = '\0';
1233                                         else {
1234                                                 /* Some PRI circuits are set up to send _no_ digits.  Handle them as 's'. */
1235                                                 pri->pvts[chanpos]->exten[0] = 's';
1236                                                 pri->pvts[chanpos]->exten[1] = '\0';
1237                                         }
1238                                         /* No number yet, but received "sending complete"? */
1239                                         if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
1240                                                 ast_verb(3, "Going to extension s|1 because of Complete received\n");
1241                                                 pri->pvts[chanpos]->exten[0] = 's';
1242                                                 pri->pvts[chanpos]->exten[1] = '\0';
1243                                         }
1244
1245                                         /* Make sure extension exists (or in overlap dial mode, can exist) */
1246                                         if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
1247                                                 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
1248                                                 /* Setup law */
1249                                                 if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
1250                                                         /* Just announce proceeding */
1251                                                         pri->pvts[chanpos]->proceeding = 1;
1252                                                         pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
1253                                                 } else {
1254                                                         if (pri->switchtype != PRI_SWITCH_GR303_TMC)
1255                                                                 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
1256                                                         else
1257                                                                 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
1258                                                 }
1259
1260                                                 /* Start PBX */
1261                                                 if (!e->ring.complete
1262                                                         && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1263                                                         && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
1264                                                         /*
1265                                                          * Release the PRI lock while we create the channel
1266                                                          * so other threads can send D channel messages.
1267                                                          */
1268                                                         ast_mutex_unlock(&pri->lock);
1269                                                         c = sig_pri_new_ast_channel(pri->pvts[chanpos],
1270                                                                 AST_STATE_RESERVED, 0,
1271                                                                 (e->ring.layer1 == PRI_LAYER_1_ALAW)
1272                                                                         ? SIG_PRI_ALAW : SIG_PRI_ULAW,
1273                                                                 e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
1274                                                         ast_mutex_lock(&pri->lock);
1275                                                         if (c) {
1276                                                                 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
1277                                                                         pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
1278                                                                 }
1279                                                                 if (e->ring.ani2 >= 0) {
1280                                                                         snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
1281                                                                         pbx_builtin_setvar_helper(c, "ANI2", ani2str);
1282                                                                 }
1283
1284 #ifdef SUPPORT_USERUSER
1285                                                                 if (!ast_strlen_zero(e->ring.useruserinfo)) {
1286                                                                         pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
1287                                                                 }
1288 #endif
1289
1290                                                                 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
1291                                                                 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
1292                                                                 if (e->ring.redirectingreason >= 0)
1293                                                                         pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
1294 #if defined(HAVE_PRI_REVERSE_CHARGE)
1295                                                                 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
1296 #endif
1297                                                         }
1298                                                         if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
1299                                                                 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
1300                                                                         plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
1301                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1302                                                         } else {
1303                                                                 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
1304                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1305                                                                 if (c)
1306                                                                         ast_hangup(c);
1307                                                                 else {
1308                                                                         pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
1309                                                                         pri->pvts[chanpos]->call = NULL;
1310                                                                 }
1311                                                         }
1312                                                 } else {
1313                                                         /*
1314                                                          * Release the PRI lock while we create the channel
1315                                                          * so other threads can send D channel messages.
1316                                                          */
1317                                                         ast_mutex_unlock(&pri->lock);
1318                                                         c = sig_pri_new_ast_channel(pri->pvts[chanpos],
1319                                                                 AST_STATE_RING, 0,
1320                                                                 (e->ring.layer1 == PRI_LAYER_1_ALAW)
1321                                                                         ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype,
1322                                                                 pri->pvts[chanpos]->exten, NULL);
1323                                                         ast_mutex_lock(&pri->lock);
1324                                                         if (c) {
1325                                                                 /*
1326                                                                  * It is reasonably safe to set the following
1327                                                                  * channel variables while the PRI and DAHDI private
1328                                                                  * structures are locked.  The PBX has not been
1329                                                                  * started yet and it is unlikely that any other task
1330                                                                  * will do anything with the channel we have just
1331                                                                  * created.
1332                                                                  */
1333                                                                 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
1334                                                                         pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
1335                                                                 }
1336                                                                 if (e->ring.ani2 >= 0) {
1337                                                                         snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
1338                                                                         pbx_builtin_setvar_helper(c, "ANI2", ani2str);
1339                                                                 }
1340
1341 #ifdef SUPPORT_USERUSER
1342                                                                 if (!ast_strlen_zero(e->ring.useruserinfo)) {
1343                                                                         pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
1344                                                                 }
1345 #endif
1346
1347                                                                 if (e->ring.redirectingreason >= 0)
1348                                                                         pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
1349 #if defined(HAVE_PRI_REVERSE_CHARGE)
1350                                                                 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
1351 #endif
1352
1353                                                                 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
1354                                                                 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
1355                                                         }
1356                                                         if (c && !ast_pbx_start(c)) {
1357                                                                 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
1358                                                                         plancallingnum, pri->pvts[chanpos]->exten,
1359                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1360                                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1361                                                         } else {
1362                                                                 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
1363                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1364                                                                 if (c) {
1365                                                                         ast_hangup(c);
1366                                                                 } else {
1367                                                                         pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
1368                                                                         pri->pvts[chanpos]->call = NULL;
1369                                                                 }
1370                                                         }
1371                                                 }
1372                                         } else {
1373                                                 ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
1374                                                         pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
1375                                                         pri->pvts[chanpos]->prioffset, pri->span);
1376                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
1377                                                 pri->pvts[chanpos]->call = NULL;
1378                                                 pri->pvts[chanpos]->exten[0] = '\0';
1379                                         }
1380                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1381                                 } else {
1382                                         if (e->ring.flexible)
1383                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1384                                         else
1385                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
1386                                 }
1387                                 break;
1388                         case PRI_EVENT_RINGING:
1389                                 chanpos = pri_find_principle(pri, e->ringing.channel);
1390                                 if (chanpos < 0) {
1391                                         ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
1392                                                 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
1393                                 } else {
1394                                         chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
1395                                         if (chanpos < 0) {
1396                                                 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
1397                                                         PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
1398                                         } else {
1399                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1400                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1401                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_RINGING, pri);
1402                                                 pri->pvts[chanpos]->alerting = 1;
1403
1404 #ifdef SUPPORT_USERUSER
1405                                                 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
1406                                                         struct ast_channel *owner;
1407
1408                                                         sig_pri_lock_owner(pri, chanpos);
1409                                                         owner = pri->pvts[chanpos]->owner;
1410                                                         if (owner) {
1411                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1412                                                                         e->ringing.useruserinfo);
1413                                                                 ast_channel_unlock(owner);
1414                                                         }
1415                                                 }
1416 #endif
1417
1418                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1419                                         }
1420                                 }
1421                                 break;
1422                         case PRI_EVENT_PROGRESS:
1423                                 /* Get chan value if e->e is not PRI_EVNT_RINGING */
1424                                 chanpos = pri_find_principle(pri, e->proceeding.channel);
1425                                 if (chanpos > -1) {
1426                                         if ((!pri->pvts[chanpos]->progress)
1427 #ifdef PRI_PROGRESS_MASK
1428                                                 || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
1429 #else
1430                                                 || (e->proceeding.progress == 8)
1431 #endif
1432                                                 ) {
1433                                                 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
1434
1435                                                 if (e->proceeding.cause > -1) {
1436                                                         ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
1437
1438                                                         /* Work around broken, out of spec USER_BUSY cause in a progress message */
1439                                                         if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
1440                                                                 if (pri->pvts[chanpos]->owner) {
1441                                                                         ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
1442
1443                                                                         pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
1444                                                                         f.subclass = AST_CONTROL_BUSY;
1445                                                                 }
1446                                                         }
1447                                                 }
1448
1449                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1450                                                 ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
1451                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
1452                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1453                                                 if (
1454 #ifdef PRI_PROGRESS_MASK
1455                                                         e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
1456 #else
1457                                                         e->proceeding.progress == 8
1458 #endif
1459                                                         ) {
1460                                                         /* Bring voice path up */
1461                                                         f.subclass = AST_CONTROL_PROGRESS;
1462                                                         pri_queue_frame(pri->pvts[chanpos], &f, pri);
1463                                                 }
1464                                                 pri->pvts[chanpos]->progress = 1;
1465                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1466                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1467                                         }
1468                                 }
1469                                 break;
1470                         case PRI_EVENT_PROCEEDING:
1471                                 chanpos = pri_find_principle(pri, e->proceeding.channel);
1472                                 if (chanpos > -1) {
1473                                         if (!pri->pvts[chanpos]->proceeding) {
1474                                                 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
1475
1476                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1477                                                 ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
1478                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
1479                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1480                                                 if (
1481 #ifdef PRI_PROGRESS_MASK
1482                                                         e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
1483 #else
1484                                                         e->proceeding.progress == 8
1485 #endif
1486                                                         ) {
1487                                                         /* Bring voice path up */
1488                                                         f.subclass = AST_CONTROL_PROGRESS;
1489                                                         pri_queue_frame(pri->pvts[chanpos], &f, pri);
1490                                                 }
1491                                                 pri->pvts[chanpos]->proceeding = 1;
1492                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1493                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1494                                         }
1495                                 }
1496                                 break;
1497                         case PRI_EVENT_FACNAME:
1498                                 chanpos = pri_find_principle(pri, e->facname.channel);
1499                                 if (chanpos < 0) {
1500                                         ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
1501                                                 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
1502                                 } else {
1503                                         chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
1504                                         if (chanpos < 0) {
1505                                                 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
1506                                                         PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
1507                                         } else {
1508                                                 /* Re-use *69 field for PRI */
1509                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1510                                                 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
1511                                                 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
1512                                                 pri_update_cid(pri->pvts[chanpos], pri);
1513                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1514                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1515                                         }
1516                                 }
1517                                 break;
1518                         case PRI_EVENT_ANSWER:
1519                                 chanpos = pri_find_principle(pri, e->answer.channel);
1520                                 if (chanpos < 0) {
1521                                         ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
1522                                                 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
1523                                 } else {
1524                                         chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
1525                                         if (chanpos < 0) {
1526                                                 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
1527                                                         PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
1528                                         } else {
1529                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1530                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_ANSWER, pri);
1531                                                 /* Enable echo cancellation if it's not on already */
1532                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1533                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1534
1535 #ifdef SUPPORT_USERUSER
1536                                                 if (!ast_strlen_zero(e->answer.useruserinfo)) {
1537                                                         struct ast_channel *owner;
1538
1539                                                         sig_pri_lock_owner(pri, chanpos);
1540                                                         owner = pri->pvts[chanpos]->owner;
1541                                                         if (owner) {
1542                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1543                                                                         e->answer.useruserinfo);
1544                                                                 ast_channel_unlock(owner);
1545                                                         }
1546                                                 }
1547 #endif
1548
1549                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1550                                         }
1551                                 }
1552                                 break;
1553                         case PRI_EVENT_HANGUP:
1554                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1555                                 if (chanpos < 0) {
1556                                         ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
1557                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1558                                 } else {
1559                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1560                                         if (chanpos > -1) {
1561                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1562                                                 if (!pri->pvts[chanpos]->alreadyhungup) {
1563                                                         /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */
1564                                                         pri->pvts[chanpos]->alreadyhungup = 1;
1565                                                         if (pri->pvts[chanpos]->owner) {
1566                                                                 /* Queue a BUSY instead of a hangup if our cause is appropriate */
1567                                                                 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
1568                                                                 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
1569                                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1570                                                                 else {
1571                                                                         switch (e->hangup.cause) {
1572                                                                         case PRI_CAUSE_USER_BUSY:
1573                                                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
1574                                                                                 break;
1575                                                                         case PRI_CAUSE_CALL_REJECTED:
1576                                                                         case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
1577                                                                         case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
1578                                                                         case PRI_CAUSE_SWITCH_CONGESTION:
1579                                                                         case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
1580                                                                         case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
1581                                                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
1582                                                                                 break;
1583                                                                         default:
1584                                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1585                                                                                 break;
1586                                                                         }
1587                                                                 }
1588                                                         }
1589                                                         ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
1590                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
1591                                                 } else {
1592                                                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
1593                                                         pri->pvts[chanpos]->call = NULL;
1594                                                 }
1595                                                 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
1596                                                         ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
1597                                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1598                                                         pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
1599                                                         pri->pvts[chanpos]->resetting = 1;
1600                                                 }
1601                                                 if (e->hangup.aoc_units > -1)
1602                                                         ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
1603                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
1604
1605 #ifdef SUPPORT_USERUSER
1606                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1607                                                         struct ast_channel *owner;
1608
1609                                                         sig_pri_lock_owner(pri, chanpos);
1610                                                         owner = pri->pvts[chanpos]->owner;
1611                                                         if (owner) {
1612                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1613                                                                         e->hangup.useruserinfo);
1614                                                                 ast_channel_unlock(owner);
1615                                                         }
1616                                                 }
1617 #endif
1618
1619                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1620                                         } else {
1621                                                 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
1622                                                         PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1623                                         }
1624                                 }
1625                                 break;
1626 #ifndef PRI_EVENT_HANGUP_REQ
1627 #error please update libpri
1628 #endif
1629                         case PRI_EVENT_HANGUP_REQ:
1630                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1631                                 if (chanpos < 0) {
1632                                         ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
1633                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1634                                 } else {
1635                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1636                                         if (chanpos > -1) {
1637                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1638                                                 if (pri->pvts[chanpos]->owner) {
1639                                                         pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
1640                                                         if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
1641                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1642                                                         else {
1643                                                                 switch (e->hangup.cause) {
1644                                                                 case PRI_CAUSE_USER_BUSY:
1645                                                                         pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
1646                                                                         break;
1647                                                                 case PRI_CAUSE_CALL_REJECTED:
1648                                                                 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
1649                                                                 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
1650                                                                 case PRI_CAUSE_SWITCH_CONGESTION:
1651                                                                 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
1652                                                                 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
1653                                                                         pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
1654                                                                         break;
1655                                                                 default:
1656                                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1657                                                                         break;
1658                                                                 }
1659                                                         }
1660                                                         ast_verb(3, "Channel %d/%d, span %d got hangup request, cause %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause);
1661                                                         if (e->hangup.aoc_units > -1)
1662                                                                 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
1663                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
1664                                                 } else {
1665                                                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
1666                                                         pri->pvts[chanpos]->call = NULL;
1667                                                 }
1668                                                 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
1669                                                         ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
1670                                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1671                                                         pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
1672                                                         pri->pvts[chanpos]->resetting = 1;
1673                                                 }
1674
1675 #ifdef SUPPORT_USERUSER
1676                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1677                                                         struct ast_channel *owner;
1678
1679                                                         sig_pri_lock_owner(pri, chanpos);
1680                                                         owner = pri->pvts[chanpos]->owner;
1681                                                         if (owner) {
1682                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1683                                                                         e->hangup.useruserinfo);
1684                                                                 ast_channel_unlock(owner);
1685                                                         }
1686                                                 }
1687 #endif
1688
1689                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1690                                         } else {
1691                                                 ast_log(LOG_WARNING, "Hangup REQ on bad channel %d/%d on span %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1692                                         }
1693                                 }
1694                                 break;
1695                         case PRI_EVENT_HANGUP_ACK:
1696                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1697                                 if (chanpos < 0) {
1698                                         ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
1699                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1700                                 } else {
1701                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1702                                         if (chanpos > -1) {
1703                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1704                                                 pri->pvts[chanpos]->call = NULL;
1705                                                 pri->pvts[chanpos]->resetting = 0;
1706                                                 if (pri->pvts[chanpos]->owner) {
1707                                                         ast_verb(3, "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1708                                                 }
1709
1710 #ifdef SUPPORT_USERUSER
1711                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1712                                                         struct ast_channel *owner;
1713
1714                                                         sig_pri_lock_owner(pri, chanpos);
1715                                                         owner = pri->pvts[chanpos]->owner;
1716                                                         if (owner) {
1717                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1718                                                                         e->hangup.useruserinfo);
1719                                                                 ast_channel_unlock(owner);
1720                                                         }
1721                                                 }
1722 #endif
1723
1724                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1725                                         }
1726                                 }
1727                                 break;
1728                         case PRI_EVENT_CONFIG_ERR:
1729                                 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err);
1730                                 break;
1731                         case PRI_EVENT_RESTART_ACK:
1732                                 chanpos = pri_find_principle(pri, e->restartack.channel);
1733                                 if (chanpos < 0) {
1734                                         /* Sometime switches (e.g. I421 / British Telecom) don't give us the
1735                                            channel number, so we have to figure it out...  This must be why
1736                                            everybody resets exactly a channel at a time. */
1737                                         for (x = 0; x < pri->numchans; x++) {
1738                                                 if (pri->pvts[x] && pri->pvts[x]->resetting) {
1739                                                         chanpos = x;
1740                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1741                                                         ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
1742                                                                 pri->pvts[chanpos]->prioffset, pri->span);
1743                                                         if (pri->pvts[chanpos]->owner) {
1744                                                                 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
1745                                                                         pri->pvts[chanpos]->prioffset, pri->span);
1746                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1747                                                         }
1748                                                         pri->pvts[chanpos]->resetting = 0;
1749                                                         ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
1750                                                                 pri->pvts[chanpos]->prioffset, pri->span);
1751                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1752                                                         if (pri->resetting)
1753                                                                 pri_check_restart(pri);
1754                                                         break;
1755                                                 }
1756                                         }
1757                                         if (chanpos < 0) {
1758                                                 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
1759                                                         PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
1760                                         }
1761                                 } else {
1762                                         if (pri->pvts[chanpos]) {
1763                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1764                                                 if (pri->pvts[chanpos]->owner) {
1765                                                         ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
1766                                                                 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
1767                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1768                                                 }
1769                                                 pri->pvts[chanpos]->resetting = 0;
1770                                                 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
1771                                                         pri->pvts[chanpos]->prioffset, pri->span);
1772                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1773                                                 if (pri->resetting)
1774                                                         pri_check_restart(pri);
1775                                         }
1776                                 }
1777                                 break;
1778                         case PRI_EVENT_SETUP_ACK:
1779                                 chanpos = pri_find_principle(pri, e->setup_ack.channel);
1780                                 if (chanpos < 0) {
1781                                         ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
1782                                                 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
1783                                 } else {
1784                                         chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
1785                                         if (chanpos > -1) {
1786                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1787                                                 pri->pvts[chanpos]->setup_ack = 1;
1788                                                 /* Send any queued digits */
1789                                                 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
1790                                                         ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
1791                                                         pri_information(pri->pri, pri->pvts[chanpos]->call,
1792                                                                 pri->pvts[chanpos]->dialdest[x]);
1793                                                 }
1794                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1795                                         } else
1796                                                 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
1797                                 }
1798                                 break;
1799                         case PRI_EVENT_NOTIFY:
1800                                 chanpos = pri_find_principle(pri, e->notify.channel);
1801                                 if (chanpos < 0) {
1802                                         ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
1803                                                 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
1804                                 } else if (!pri->discardremoteholdretrieval) {
1805                                         struct ast_frame f = { AST_FRAME_CONTROL, };
1806
1807                                         sig_pri_lock_private(pri->pvts[chanpos]);
1808                                         switch (e->notify.info) {
1809                                         case PRI_NOTIFY_REMOTE_HOLD:
1810                                                 f.subclass = AST_CONTROL_HOLD;
1811                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1812                                                 break;
1813                                         case PRI_NOTIFY_REMOTE_RETRIEVAL:
1814                                                 f.subclass = AST_CONTROL_UNHOLD;
1815                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1816                                                 break;
1817                                         }
1818                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1819                                 }
1820                                 break;
1821                         default:
1822                                 ast_debug(1, "Event: %d\n", e->e);
1823                         }
1824                 }
1825                 ast_mutex_unlock(&pri->lock);
1826         }
1827         /* Never reached */
1828         return NULL;
1829 }
1830
1831 void sig_pri_init_pri(struct sig_pri_pri *pri)
1832 {
1833         int i;
1834
1835         memset(pri, 0, sizeof(*pri));
1836
1837         ast_mutex_init(&pri->lock);
1838
1839         pri->master = AST_PTHREADT_NULL;
1840         for (i = 0; i < NUM_DCHANS; i++)
1841                 pri->fds[i] = -1;
1842 }
1843
1844 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
1845 {
1846         int res = 0;
1847 #ifdef SUPPORT_USERUSER
1848         const char *useruser = pbx_builtin_getvar_helper(ast, "USERUSERINFO");
1849 #endif
1850
1851         ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
1852         if (!ast->tech_pvt) {
1853                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1854                 return 0;
1855         }
1856
1857         p->owner = NULL;
1858         p->outgoing = 0;
1859         p->digital = 0;
1860         p->proceeding = 0;
1861         p->progress = 0;
1862         p->alerting = 0;
1863         p->setup_ack = 0;
1864         p->exten[0] = '\0';
1865         sig_pri_set_dialing(p, 0);
1866
1867         if (!p->call) {
1868                 res = 0;
1869                 goto exit;
1870         }
1871
1872         /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
1873         if (!pri_grab(p, p->pri)) {
1874                 if (p->alreadyhungup) {
1875                         ast_log(LOG_DEBUG, "Already hungup...  Calling hangup once, and clearing call\n");
1876
1877 #ifdef SUPPORT_USERUSER
1878                         pri_call_set_useruser(p->call, useruser);
1879 #endif
1880
1881                         pri_hangup(p->pri->pri, p->call, -1);
1882                         p->call = NULL;
1883                 } else {
1884                         const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
1885                         int icause = ast->hangupcause ? ast->hangupcause : -1;
1886                         ast_log(LOG_DEBUG, "Not yet hungup...  Calling hangup once with icause, and clearing call\n");
1887
1888 #ifdef SUPPORT_USERUSER
1889                         pri_call_set_useruser(p->call, useruser);
1890 #endif
1891
1892                         p->alreadyhungup = 1;
1893                         if (cause) {
1894                                 if (atoi(cause))
1895                                         icause = atoi(cause);
1896                         }
1897                         pri_hangup(p->pri->pri, p->call, icause);
1898                 }
1899                 if (res < 0)
1900                         ast_log(LOG_WARNING, "pri_disconnect failed\n");
1901                 pri_rel(p->pri);
1902         } else {
1903                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
1904                 res = -1;
1905         }
1906
1907 exit:
1908         ast->tech_pvt = NULL;
1909         return res;
1910 }
1911
1912 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1)
1913 {
1914         char dest[256]; /* must be same length as p->dialdest */
1915         struct pri_sr *sr;
1916         char *c, *l, *n, *s = NULL;
1917 #ifdef SUPPORT_USERUSER
1918         const char *useruser;
1919 #endif
1920         int pridialplan;
1921         int dp_strip;
1922         int prilocaldialplan;
1923         int ldp_strip;
1924         int exclusive;
1925         const char *rr_str;
1926         int redirect_reason;
1927
1928         ast_log(LOG_DEBUG, "CALLING CID_NAME: %s CID_NUM:: %s\n", ast->cid.cid_name, ast->cid.cid_num);
1929
1930         if (!p->pri) {
1931                 ast_log(LOG_ERROR, "Could not find pri on channel %d\n", p->channel);
1932                 return -1;
1933         }
1934
1935
1936         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1937                 ast_log(LOG_WARNING, "sig_pri_call called on %s, neither down nor reserved\n", ast->name);
1938                 return -1;
1939         }
1940
1941         ast_copy_string(dest, rdest, sizeof(dest));
1942
1943         p->dialdest[0] = '\0';
1944         p->outgoing = 1;
1945
1946         c = strchr(dest, '/');
1947         if (c) {
1948                 c++;
1949         } else {
1950                 c = "";
1951         }
1952
1953         l = NULL;
1954         n = NULL;
1955         if (!p->hidecallerid) {
1956                 l = ast->connected.id.number;
1957                 if (!p->hidecalleridname) {
1958                         n = ast->connected.id.name;
1959                 }
1960         }
1961
1962         if (strlen(c) < p->stripmsd) {
1963                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1964                 return -1;
1965         }
1966         if (pri_grab(p, p->pri)) {
1967                 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1968                 return -1;
1969         }
1970         if (!(p->call = pri_new_call(p->pri->pri))) {
1971                 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1972                 pri_rel(p->pri);
1973                 return -1;
1974         }
1975         if (!(sr = pri_sr_new())) {
1976                 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1977                 pri_destroycall(p->pri->pri, p->call);
1978                 p->call = NULL;
1979                 pri_rel(p->pri);
1980                 return -1;
1981         }
1982
1983         p->digital = IS_DIGITAL(ast->transfercapability);
1984
1985         /* Should the picked channel be used exclusively? */
1986         if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
1987                 exclusive = 1;
1988         } else {
1989                 exclusive = 0;
1990         }
1991
1992         pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
1993         pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
1994                 (p->digital ? -1 : layer1));
1995
1996         if (p->pri->facilityenable)
1997                 pri_facility_enable(p->pri->pri);
1998
1999         ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2000         dp_strip = 0;
2001         pridialplan = p->pri->dialplan - 1;
2002         if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2003                 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2004                         if (pridialplan == -2) {
2005                                 dp_strip = strlen(p->pri->internationalprefix);
2006                         }
2007                         pridialplan = PRI_INTERNATIONAL_ISDN;
2008                 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2009                         if (pridialplan == -2) {
2010                                 dp_strip = strlen(p->pri->nationalprefix);
2011                         }
2012                         pridialplan = PRI_NATIONAL_ISDN;
2013                 } else {
2014                         pridialplan = PRI_LOCAL_ISDN;
2015                 }
2016         }
2017         while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2018                 switch (c[p->stripmsd]) {
2019                 case 'U':
2020                         pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2021                         break;
2022                 case 'I':
2023                         pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2024                         break;
2025                 case 'N':
2026                         pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2027                         break;
2028                 case 'L':
2029                         pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2030                         break;
2031                 case 'S':
2032                         pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2033                         break;
2034                 case 'V':
2035                         pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2036                         break;
2037                 case 'R':
2038                         pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2039                         break;
2040                 case 'u':
2041                         pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2042                         break;
2043                 case 'e':
2044                         pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2045                         break;
2046                 case 'x':
2047                         pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2048                         break;
2049                 case 'f':
2050                         pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2051                         break;
2052                 case 'n':
2053                         pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2054                         break;
2055                 case 'p':
2056                         pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2057                         break;
2058                 case 'r':
2059                         pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2060                         break;
2061 #if defined(HAVE_PRI_REVERSE_CHARGE)
2062                 case 'C':
2063                         pri_sr_set_reversecharge(sr, PRI_REVERSECHARGE_REQUESTED);
2064                         break;
2065 #endif
2066                 default:
2067                         if (isalpha(c[p->stripmsd])) {
2068                                 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
2069                                         c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
2070                         }
2071                         break;
2072                 }
2073                 c++;
2074         }
2075         pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2076
2077         ldp_strip = 0;
2078         prilocaldialplan = p->pri->localdialplan - 1;
2079         if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2080                 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2081                         if (prilocaldialplan == -2) {
2082                                 ldp_strip = strlen(p->pri->internationalprefix);
2083                         }
2084                         prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2085                 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2086                         if (prilocaldialplan == -2) {
2087                                 ldp_strip = strlen(p->pri->nationalprefix);
2088                         }
2089                         prilocaldialplan = PRI_NATIONAL_ISDN;
2090                 } else {
2091                         prilocaldialplan = PRI_LOCAL_ISDN;
2092                 }
2093         }
2094         if (l != NULL) {
2095                 while (*l > '9' && *l != '*' && *l != '#') {
2096                         switch (*l) {
2097                         case 'U':
2098                                 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2099                                 break;
2100                         case 'I':
2101                                 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2102                                 break;
2103                         case 'N':
2104                                 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2105                                 break;
2106                         case 'L':
2107                                 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2108                                 break;
2109                         case 'S':
2110                                 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2111                                 break;
2112                         case 'V':
2113                                 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2114                                 break;
2115                         case 'R':
2116                                 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2117                                 break;
2118                         case 'u':
2119                                 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2120                                 break;
2121                         case 'e':
2122                                 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2123                                 break;
2124                         case 'x':
2125                                 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2126                                 break;
2127                         case 'f':
2128                                 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2129                                 break;
2130                         case 'n':
2131                                 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2132                                 break;
2133                         case 'p':
2134                                 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2135                                 break;
2136                         case 'r':
2137                                 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2138                                 break;
2139                         default:
2140                                 if (isalpha(*l)) {
2141                                         ast_log(LOG_WARNING,
2142                                                 "Unrecognized prilocaldialplan %s modifier: %c\n",
2143                                                 *l > 'Z' ? "NPI" : "TON", *l);
2144                                 }
2145                                 break;
2146                         }
2147                         l++;
2148                 }
2149         }
2150         pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2151                 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2152         if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2153                 if (!strcasecmp(rr_str, "UNKNOWN"))
2154                         redirect_reason = 0;
2155                 else if (!strcasecmp(rr_str, "BUSY"))
2156                         redirect_reason = 1;
2157                 else if (!strcasecmp(rr_str, "NO_REPLY"))
2158                         redirect_reason = 2;
2159                 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2160                         redirect_reason = 15;
2161                 else
2162                         redirect_reason = PRI_REDIR_UNCONDITIONAL;
2163         } else
2164                 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2165         pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2166
2167 #ifdef SUPPORT_USERUSER
2168         /* User-user info */
2169         useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2170         if (useruser)
2171                 pri_sr_set_useruser(sr, useruser);
2172 #endif
2173
2174         if (pri_setup(p->pri->pri, p->call, sr)) {
2175                 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2176                         c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2177                 pri_rel(p->pri);
2178                 pri_sr_free(sr);
2179                 return -1;
2180         }
2181         pri_sr_free(sr);
2182         ast_setstate(ast, AST_STATE_DIALING);
2183         sig_pri_set_dialing(p, 1);
2184         pri_rel(p->pri);
2185         return 0;
2186 }
2187
2188 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
2189 {
2190         int res = -1;
2191
2192         switch (condition) {
2193         case AST_CONTROL_BUSY:
2194                 if (p->priindication_oob) {
2195                         chan->hangupcause = AST_CAUSE_USER_BUSY;
2196                         chan->_softhangup |= AST_SOFTHANGUP_DEV;
2197                         res = 0;
2198                 } else if (!p->progress && p->pri && !p->outgoing) {
2199                         if (p->pri->pri) {
2200                                 if (!pri_grab(p, p->pri)) {
2201 #ifdef HAVE_PRI_PROG_W_CAUSE
2202                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_USER_BUSY); /* cause = 17 */
2203 #else
2204                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2205 #endif
2206                                         pri_rel(p->pri);
2207                                 } else {
2208                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2209                                 }
2210                         }
2211                         p->progress = 1;
2212                         res = sig_pri_play_tone(p, SIG_PRI_TONE_BUSY);
2213                 }
2214                 break;
2215         case AST_CONTROL_RINGING:
2216                 if ((!p->alerting) && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
2217                         if (p->pri->pri) {
2218                                 if (!pri_grab(p, p->pri)) {
2219                                         pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
2220                                         pri_rel(p->pri);
2221                                 } else {
2222                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2223                                 }
2224                         }
2225                         p->alerting = 1;
2226                 }
2227                 res = sig_pri_play_tone(p, SIG_PRI_TONE_RINGTONE);
2228                 if (chan->_state != AST_STATE_UP) {
2229                         if (chan->_state != AST_STATE_RING)
2230                                 ast_setstate(chan, AST_STATE_RINGING);
2231                 }
2232                 break;
2233         case AST_CONTROL_PROCEEDING:
2234                 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
2235                 if (!p->proceeding && p->pri && !p->outgoing) {
2236                         if (p->pri->pri) {
2237                                 if (!pri_grab(p, p->pri)) {
2238                                         pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
2239                                         pri_rel(p->pri);
2240                                 } else {
2241                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2242                                 }
2243                         }
2244                         p->proceeding = 1;
2245                         sig_pri_set_dialing(p, 0);
2246                 }
2247                 /* don't continue in ast_indicate */
2248                 res = 0;
2249                 break;
2250         case AST_CONTROL_PROGRESS:
2251                 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
2252                 p->digital = 0; /* Digital-only calls isn't allowing any inband progress messages */
2253                 if (!p->progress && p->pri && !p->outgoing) {
2254                         if (p->pri->pri) {
2255                                 if (!pri_grab(p, p->pri)) {
2256 #ifdef HAVE_PRI_PROG_W_CAUSE
2257                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);  /* no cause at all */
2258 #else
2259                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2260 #endif
2261                                         pri_rel(p->pri);
2262                                 } else {
2263                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2264                                 }
2265                         }
2266                         p->progress = 1;
2267                 }
2268                 /* don't continue in ast_indicate */
2269                 res = 0;
2270                 break;
2271         case AST_CONTROL_CONGESTION:
2272                 chan->hangupcause = AST_CAUSE_CONGESTION;
2273                 if (p->priindication_oob) {
2274                         chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
2275                         chan->_softhangup |= AST_SOFTHANGUP_DEV;
2276                         res = 0;
2277                 } else if (!p->progress && p->pri && !p->outgoing) {
2278                         if (p->pri) {
2279                                 if (!pri_grab(p, p->pri)) {
2280 #ifdef HAVE_PRI_PROG_W_CAUSE
2281                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION); /* cause = 42 */
2282 #else
2283                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2284 #endif
2285                                         pri_rel(p->pri);
2286                                 } else {
2287                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2288                                 }
2289                         }
2290                         p->progress = 1;
2291                         res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
2292                 }
2293                 break;
2294         case AST_CONTROL_HOLD:
2295                 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
2296                         if (!pri_grab(p, p->pri)) {
2297                                 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
2298                                 pri_rel(p->pri);
2299                         } else {
2300                                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2301                         }
2302                 } else
2303                         ast_moh_start(chan, data, p->mohinterpret);
2304                 break;
2305         case AST_CONTROL_UNHOLD:
2306                 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
2307                         if (!pri_grab(p, p->pri)) {
2308                                 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
2309                                 pri_rel(p->pri);
2310                         }
2311                 } else
2312                         ast_moh_stop(chan);
2313                 break;
2314         case AST_CONTROL_SRCUPDATE:
2315                 res = 0;
2316                 break;
2317         case -1:
2318                 res = sig_pri_play_tone(p, -1);
2319                 break;
2320         }
2321
2322         return res;
2323 }
2324
2325 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast)
2326 {
2327         int res = 0;
2328         /* Send a pri acknowledge */
2329         if (!pri_grab(p, p->pri)) {
2330                 p->proceeding = 1;
2331                 sig_pri_set_dialing(p, 0);
2332                 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2333                 pri_rel(p->pri);
2334         } else {
2335                 res = -1;
2336         }
2337         ast_setstate(ast, AST_STATE_UP);
2338         return res;
2339 }
2340
2341 int sig_pri_available(struct sig_pri_chan *p, int channelmatch, ast_group_t groupmatch, int *reason, int *channelmatched, int *groupmatched)
2342 {
2343         /* If no owner definitely available */
2344         if (!p->owner) {
2345                 /* Trust PRI */
2346                 if (p->pri) {
2347 #ifdef HAVE_PRI_SERVICE_MESSAGES
2348                         char db_chan_name[20], db_answer[5], state;
2349                         int why = 0;
2350
2351                         snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, p->pri->span, p->channel);
2352                         if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
2353                                 sscanf(db_answer, "%c:%d", &state, &why);
2354                         }
2355                         if ((p->resetting || p->call) || (why)) {
2356                                 if (why) {
2357                                         *reason = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
2358                                 }
2359 #else
2360                         if (p->resetting || p->call) {
2361 #endif
2362                                 return 0;
2363                         } else {
2364                                 return 1;
2365                         }
2366                 }
2367         }
2368
2369         return 0;
2370 }
2371
2372 /* If return 0, it means this function was able to handle it (pre setup digits).  If non zero, the user of this
2373  * functions should handle it normally (generate inband DTMF) */
2374 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit)
2375 {
2376         if ((ast->_state == AST_STATE_DIALING) && !pvt->proceeding) {
2377                 if (pvt->setup_ack) {
2378                         if (!pri_grab(pvt, pvt->pri)) {
2379                                 pri_information(pvt->pri->pri, pvt->call, digit);
2380                                 pri_rel(pvt->pri);
2381                         } else {
2382                                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->pri->span);
2383                         }
2384                 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
2385                         int res;
2386                         ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
2387                         res = strlen(pvt->dialdest);
2388                         pvt->dialdest[res++] = digit;
2389                         pvt->dialdest[res] = '\0';
2390                 }
2391                 return 0;
2392         }
2393         return 1;
2394 }
2395
2396 int sig_pri_start_pri(struct sig_pri_pri *pri)
2397 {
2398         int x;
2399         int i;
2400
2401         ast_mutex_init(&pri->lock);
2402
2403         for (i = 0; i < NUM_DCHANS; i++) {
2404                 if (pri->fds[i] == -1) {
2405                         break;
2406                 }
2407
2408                 switch (pri->sig) {
2409                 case SIG_BRI:
2410                         pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
2411                         break;
2412                 case SIG_BRI_PTMP:
2413                         pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
2414                         break;
2415                 default:
2416                         pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
2417 #ifdef HAVE_PRI_SERVICE_MESSAGES
2418                         if (pri->enable_service_message_support) {
2419                                 pri_set_service_message_support(pri->dchans[i], 1);
2420                         }
2421 #endif
2422                         break;
2423                 }
2424
2425                 pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
2426 #ifdef HAVE_PRI_PROG_W_CAUSE
2427                 pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
2428 #endif
2429 #ifdef HAVE_PRI_INBANDDISCONNECT
2430                 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
2431 #endif
2432                 /* Enslave to master if appropriate */
2433                 if (i)
2434                         pri_enslave(pri->dchans[0], pri->dchans[i]);
2435                 if (!pri->dchans[i]) {
2436                         if (pri->fds[i] > 0)
2437                                 close(pri->fds[i]);
2438                         pri->fds[i] = -1;
2439                         ast_log(LOG_ERROR, "Unable to create PRI structure\n");
2440                         return -1;
2441                 }
2442                 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
2443                 pri_set_nsf(pri->dchans[i], pri->nsf);
2444 #ifdef PRI_GETSET_TIMERS
2445                 for (x = 0; x < PRI_MAX_TIMERS; x++) {
2446                         if (pri->pritimers[x] != 0)
2447                                 pri_set_timer(pri->dchans[i], x, pri->pritimers[x]);
2448                 }
2449 #endif
2450         }
2451         /* Assume primary is the one we use */
2452         pri->pri = pri->dchans[0];
2453         pri->resetpos = -1;
2454         if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
2455                 for (i = 0; i < NUM_DCHANS; i++) {
2456                         if (!pri->dchans[i])
2457                                 break;
2458                         if (pri->fds[i] > 0)
2459                                 close(pri->fds[i]);
2460                         pri->fds[i] = -1;
2461                 }
2462                 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
2463                 return -1;
2464         }
2465         return 0;
2466 }
2467
2468 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm)
2469 {
2470         if (!noalarm) {
2471                 p->inalarm = 1;
2472                 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
2473                         /* T309 is not enabled : hangup calls when alarm occurs */
2474                         if (p->call) {
2475                                 if (p->pri && p->pri->pri) {
2476                                         if (!pri_grab(p, p->pri)) {
2477                                                 pri_hangup(p->pri->pri, p->call, -1);
2478                                                 pri_destroycall(p->pri->pri, p->call);
2479                                                 p->call = NULL;
2480                                                 pri_rel(p->pri);
2481                                         } else
2482                                                 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2483                                 } else
2484                                         ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2485                         } else
2486                                 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
2487                 }
2488                 if (p->owner)
2489                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2490         } else {
2491                 p->inalarm = 0;
2492         }
2493 }
2494
2495 struct sig_pri_chan *sig_pri_chan_new(void *pvt_data, struct sig_pri_callback *callback, struct sig_pri_pri *pri, int logicalspan, int channo, int trunkgroup)
2496 {
2497         struct sig_pri_chan *p;
2498
2499         p = ast_calloc(1, sizeof(*p));
2500
2501         if (!p)
2502                 return p;
2503
2504         p->logicalspan = logicalspan;
2505         p->prioffset = channo;
2506         p->mastertrunkgroup = trunkgroup;
2507
2508         p->calls = callback;
2509         p->chan_pvt = pvt_data;
2510
2511         pri->pvts[pri->numchans++] = p;
2512         p->pri = pri;
2513
2514         return p;
2515 }
2516
2517 static void build_status(char *s, size_t len, int status, int active)
2518 {
2519         if (!s || len < 1) {
2520                 return;
2521         }
2522         s[0] = '\0';
2523         if (!(status & DCHAN_NOTINALARM))
2524                 strncat(s, "In Alarm, ", len - strlen(s) - 1);
2525         if (status & DCHAN_UP)
2526                 strncat(s, "Up", len - strlen(s) - 1);
2527         else
2528                 strncat(s, "Down", len - strlen(s) - 1);
2529         if (active)
2530                 strncat(s, ", Active", len - strlen(s) - 1);
2531         else
2532                 strncat(s, ", Standby", len - strlen(s) - 1);
2533         s[len - 1] = '\0';
2534 }
2535
2536 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_pri *pri)
2537 {
2538         char status[256];
2539         int x;
2540         for (x = 0; x < NUM_DCHANS; x++) {
2541                 if (pri->dchans[x]) {
2542                         build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
2543                         ast_cli(fd, "PRI span %d/%d: %s\n", span, x, status);
2544                 }
2545         }
2546 }
2547
2548 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_pri *pri)
2549 {
2550         int x;
2551         char status[256];
2552
2553         for (x = 0; x < NUM_DCHANS; x++) {
2554                 if (pri->dchans[x]) {
2555 #ifdef PRI_DUMP_INFO_STR
2556                         char *info_str = NULL;
2557 #endif
2558                         ast_cli(fd, "%s D-channel: %d\n", pri_order(x), dchannels[x]);
2559                         build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
2560                         ast_cli(fd, "Status: %s\n", status);
2561 #ifdef PRI_DUMP_INFO_STR
2562                         info_str = pri_dump_info_str(pri->pri);
2563                         if (info_str) {
2564                                 ast_cli(fd, "%s", info_str);
2565                                 free(info_str);
2566                         }
2567 #else
2568                         pri_dump_info(pri->pri);
2569 #endif
2570                         ast_cli(fd, "Overlap Recv: %s\n\n", (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
2571                         ast_cli(fd, "\n");
2572                 }
2573         }
2574 }
2575
2576 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits)
2577 {
2578         sig_pri_lock_private(p);
2579
2580         if (!p->pri || !p->call) {
2581                 ast_debug(1, "Unable to find pri or call on channel!\n");
2582                 sig_pri_unlock_private(p);
2583                 return -1;
2584         }
2585
2586         if (!pri_grab(p, p->pri)) {
2587                 pri_keypad_facility(p->pri->pri, p->call, digits);
2588                 pri_rel(p->pri);
2589         } else {
2590                 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2591                 sig_pri_unlock_private(p);
2592                 return -1;
2593         }
2594
2595         sig_pri_unlock_private(p);
2596
2597         return 0;
2598 }
2599
2600 int pri_send_callrerouting_facility_exec(struct sig_pri_chan *p, enum ast_channel_state chanstate, const char *destination, const char *original, const char *reason)
2601 {
2602         int res = -1;
2603
2604         sig_pri_lock_private(p);
2605
2606         if (!p->pri || !p->call) {
2607                 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2608                 sig_pri_unlock_private(p);
2609                 return -1;
2610         }
2611
2612         switch (p->pri->sig) {
2613         case SIG_PRI:
2614                 if (!pri_grab(p, p->pri)) {
2615                         if (chanstate == AST_STATE_RING) {
2616                                 res = pri_callrerouting_facility(p->pri->pri, p->call, destination, original, reason);
2617                         }
2618                         pri_rel(p->pri);
2619                 } else {
2620                         ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->pri->span);
2621                         sig_pri_unlock_private(p);
2622                         return -1;
2623                 }
2624                 break;
2625         }
2626
2627         sig_pri_unlock_private(p);
2628
2629         return res;
2630 }
2631
2632 #ifdef HAVE_PRI_SERVICE_MESSAGES
2633 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
2634 {
2635         int channel = PVT_TO_CHANNEL(p);
2636         int span = PRI_SPAN(channel);
2637
2638         return pri_maintenance_service(pri, span, channel, changestatus);
2639 }
2640 #endif
2641
2642 #endif /* HAVE_PRI */