4e824c7eb727ba598c4e81b9fec511350372341c
[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         /*
605          * In the bizarre case where the channel has become a zombie before we
606          * even get started here, abort safely.
607          */
608         if (!p) {
609                 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
610                 ast_hangup(chan);
611                 return NULL;
612         }
613
614         ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
615
616         /* Now loop looking for an extension */
617         ast_copy_string(exten, p->exten, sizeof(exten));
618         len = strlen(exten);
619         res = 0;
620         while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
621                 if (len && !ast_ignore_pattern(chan->context, exten))
622                         sig_pri_play_tone(p, -1);
623                 else
624                         sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
625                 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
626                         timeout = pri_matchdigittimeout;
627                 else
628                         timeout = pri_gendigittimeout;
629                 res = ast_waitfordigit(chan, timeout);
630                 if (res < 0) {
631                         ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
632                         ast_hangup(chan);
633                         return NULL;
634                 } else if (res) {
635                         exten[len++] = res;
636                         exten[len] = '\0';
637                 } else
638                         goto exit;
639         }
640         /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
641         if (ast_strlen_zero(exten)) {
642                 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
643                 exten[0] = 's';
644                 exten[1] = '\0';
645         }
646         sig_pri_play_tone(p, -1);
647         if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
648                 /* Start the real PBX */
649                 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
650                 sig_pri_set_echocanceller(p, 1);
651                 ast_setstate(chan, AST_STATE_RING);
652                 res = ast_pbx_run(chan);
653                 if (res) {
654                         ast_log(LOG_WARNING, "PBX exited non-zero!\n");
655                 }
656         } else {
657                 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
658                 chan->hangupcause = AST_CAUSE_UNALLOCATED;
659                 ast_hangup(chan);
660                 p->exten[0] = '\0';
661                 /* Since we send release complete here, we won't get one */
662                 p->call = NULL;
663         }
664         return NULL;
665
666 exit:
667         res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
668         if (res < 0)
669                         ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
670         ast_hangup(chan);
671         return NULL;
672 }
673
674 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri)
675 {
676         pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
677         if (!before_start_pri)
678                 pri_find_dchan(pri);
679 }
680
681 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
682 {
683         pri->dchanavail[index] |= DCHAN_NOTINALARM;
684         if (!before_start_pri)
685                 pri_restart(pri->dchans[index]);
686 }
687
688 #if defined(SUPPORT_USERUSER)
689 /*!
690  * \internal
691  * \brief Obtain the sig_pri owner channel lock if the owner exists.
692  * \since 1.6.3
693  *
694  * \param pri sig_pri PRI control structure.
695  * \param chanpos Channel position in the span.
696  *
697  * \note Assumes the pri->lock is already obtained.
698  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
699  *
700  * \return Nothing
701  */
702 static void sig_pri_lock_owner(struct sig_pri_pri *pri, int chanpos)
703 {
704         for (;;) {
705                 if (!pri->pvts[chanpos]->owner) {
706                         /* There is no owner lock to get. */
707                         break;
708                 }
709                 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
710                         /* We got the lock */
711                         break;
712                 }
713                 /* We must unlock the PRI to avoid the possibility of a deadlock */
714                 ast_mutex_unlock(&pri->lock);
715                 PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
716                 ast_mutex_lock(&pri->lock);
717         }
718 }
719 #endif  /* defined(SUPPORT_USERUSER) */
720
721 static void *pri_dchannel(void *vpri)
722 {
723         struct sig_pri_pri *pri = vpri;
724         pri_event *e;
725         struct pollfd fds[NUM_DCHANS];
726         int res;
727         int chanpos = 0;
728         int x;
729         struct ast_channel *c;
730         struct timeval tv, lowest, *next;
731         int doidling=0;
732         char *cc;
733         time_t t;
734         int i, which=-1;
735         int numdchans;
736         pthread_t threadid;
737         char ani2str[6];
738         char plancallingnum[AST_MAX_EXTENSION];
739         char plancallingani[AST_MAX_EXTENSION];
740         char calledtonstr[10];
741         struct timeval lastidle = { 0, 0 };
742         pthread_t p;
743         struct ast_channel *idle;
744         char idlen[80];
745         int nextidle = -1;
746         int haveidles;
747         int activeidles;
748
749         gettimeofday(&lastidle, NULL);
750         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
751
752         if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
753                 /* Need to do idle dialing, check to be sure though */
754                 cc = strchr(pri->idleext, '@');
755                 if (cc) {
756                         *cc = '\0';
757                         cc++;
758                         ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
759 #if 0
760                         /* Extensions may not be loaded yet */
761                         if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
762                                 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
763                         else
764 #endif
765                                 doidling = 1;
766                 } else
767                         ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
768         }
769         for (;;) {
770                 for (i = 0; i < NUM_DCHANS; i++) {
771                         if (!pri->dchans[i])
772                                 break;
773                         fds[i].fd = pri->fds[i];
774                         fds[i].events = POLLIN | POLLPRI;
775                         fds[i].revents = 0;
776                 }
777                 numdchans = i;
778                 time(&t);
779                 ast_mutex_lock(&pri->lock);
780                 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
781                         if (pri->resetting && pri_is_up(pri)) {
782                                 if (pri->resetpos < 0)
783                                         pri_check_restart(pri);
784                         } else {
785                                 if (!pri->resetting     && (t - pri->lastreset) >= pri->resetinterval) {
786                                         pri->resetting = 1;
787                                         pri->resetpos = -1;
788                                 }
789                         }
790                 }
791                 /* Look for any idle channels if appropriate */
792                 if (doidling && pri_is_up(pri)) {
793                         nextidle = -1;
794                         haveidles = 0;
795                         activeidles = 0;
796                         for (x = pri->numchans; x >= 0; x--) {
797                                 if (pri->pvts[x] && !pri->pvts[x]->owner &&
798                                         !pri->pvts[x]->call) {
799                                         if (haveidles < pri->minunused) {
800                                                 haveidles++;
801                                         } else if (!pri->pvts[x]->resetting) {
802                                                 nextidle = x;
803                                                 break;
804                                         }
805                                 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
806                                         activeidles++;
807                         }
808                         if (nextidle > -1) {
809                                 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
810                                         /* Don't create a new idle call more than once per second */
811                                         snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
812                                         idle = sig_pri_request(pri->pvts[nextidle], AST_FORMAT_ULAW, NULL);
813                                         if (idle) {
814                                                 pri->pvts[nextidle]->isidlecall = 1;
815                                                 if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) {
816                                                         ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
817                                                         ast_hangup(idle);
818                                                 }
819                                         } else
820                                                 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
821                                         gettimeofday(&lastidle, NULL);
822                                 }
823                         } else if ((haveidles < pri->minunused) &&
824                                 (activeidles > pri->minidle)) {
825                                 /* Mark something for hangup if there is something
826                                    that can be hungup */
827                                 for (x = pri->numchans; x >= 0; x--) {
828                                         /* find a candidate channel */
829                                         if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
830                                                 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
831                                                 haveidles++;
832                                                 /* Stop if we have enough idle channels or
833                                                   can't spare any more active idle ones */
834                                                 if ((haveidles >= pri->minunused) ||
835                                                         (activeidles <= pri->minidle))
836                                                         break;
837                                         }
838                                 }
839                         }
840                 }
841                 /* Start with reasonable max */
842                 lowest = ast_tv(60, 0);
843                 for (i = 0; i < NUM_DCHANS; i++) {
844                         /* Find lowest available d-channel */
845                         if (!pri->dchans[i])
846                                 break;
847                         if ((next = pri_schedule_next(pri->dchans[i]))) {
848                                 /* We need relative time here */
849                                 tv = ast_tvsub(*next, ast_tvnow());
850                                 if (tv.tv_sec < 0) {
851                                         tv = ast_tv(0,0);
852                                 }
853                                 if (doidling || pri->resetting) {
854                                         if (tv.tv_sec > 1) {
855                                                 tv = ast_tv(1, 0);
856                                         }
857                                 } else {
858                                         if (tv.tv_sec > 60) {
859                                                 tv = ast_tv(60, 0);
860                                         }
861                                 }
862                         } else if (doidling || pri->resetting) {
863                                 /* Make sure we stop at least once per second if we're
864                                    monitoring idle channels */
865                                 tv = ast_tv(1,0);
866                         } else {
867                                 /* Don't poll for more than 60 seconds */
868                                 tv = ast_tv(60, 0);
869                         }
870                         if (!i || ast_tvcmp(tv, lowest) < 0) {
871                                 lowest = tv;
872                         }
873                 }
874                 ast_mutex_unlock(&pri->lock);
875
876                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
877                 pthread_testcancel();
878                 e = NULL;
879                 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
880                 pthread_testcancel();
881                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
882
883                 ast_mutex_lock(&pri->lock);
884                 if (!res) {
885                         for (which = 0; which < NUM_DCHANS; which++) {
886                                 if (!pri->dchans[which])
887                                         break;
888                                 /* Just a timeout, run the scheduler */
889                                 e = pri_schedule_run(pri->dchans[which]);
890                                 if (e)
891                                         break;
892                         }
893                 } else if (res > -1) {
894                         for (which = 0; which < NUM_DCHANS; which++) {
895                                 if (!pri->dchans[which])
896                                         break;
897                                 if (fds[which].revents & POLLPRI) {
898                                         sig_pri_handle_dchan_exception(pri, which);
899                                 } else if (fds[which].revents & POLLIN) {
900                                         e = pri_check_event(pri->dchans[which]);
901                                 }
902                                 if (e)
903                                         break;
904                         }
905                 } else if (errno != EINTR)
906                         ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
907
908                 if (e) {
909                         if (pri->debug)
910                                 pri_dump_event(pri->dchans[which], e);
911
912                         if (e->e != PRI_EVENT_DCHAN_DOWN) {
913                                 if (!(pri->dchanavail[which] & DCHAN_UP)) {
914                                         ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
915                                 }
916                                 pri->dchanavail[which] |= DCHAN_UP;
917                         } else {
918                                 if (pri->dchanavail[which] & DCHAN_UP) {
919                                         ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
920                                 }
921                                 pri->dchanavail[which] &= ~DCHAN_UP;
922                         }
923
924                         if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
925                                 /* Must be an NFAS group that has the secondary dchan active */
926                                 pri->pri = pri->dchans[which];
927
928                         switch (e->e) {
929                         case PRI_EVENT_DCHAN_UP:
930                                 if (!pri->pri) pri_find_dchan(pri);
931
932                                 /* Note presense of D-channel */
933                                 time(&pri->lastreset);
934
935                                 /* Restart in 5 seconds */
936                                 if (pri->resetinterval > -1) {
937                                         pri->lastreset -= pri->resetinterval;
938                                         pri->lastreset += 5;
939                                 }
940                                 pri->resetting = 0;
941                                 /* Take the channels from inalarm condition */
942                                 for (i = 0; i < pri->numchans; i++)
943                                         if (pri->pvts[i]) {
944                                                 pri->pvts[i]->inalarm = 0;
945                                         }
946                                 break;
947                         case PRI_EVENT_DCHAN_DOWN:
948                                 pri_find_dchan(pri);
949                                 if (!pri_is_up(pri)) {
950                                         pri->resetting = 0;
951                                         /* Hangup active channels and put them in alarm mode */
952                                         for (i = 0; i < pri->numchans; i++) {
953                                                 struct sig_pri_chan *p = pri->pvts[i];
954                                                 if (p) {
955                                                         if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
956                                                                 /* T309 is not enabled : hangup calls when alarm occurs */
957                                                                 if (p->call) {
958                                                                         if (p->pri && p->pri->pri) {
959                                                                                 pri_hangup(p->pri->pri, p->call, -1);
960                                                                                 pri_destroycall(p->pri->pri, p->call);
961                                                                                 p->call = NULL;
962                                                                         } else
963                                                                                 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
964                                                                 }
965                                                                 if (p->owner)
966                                                                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
967                                                         }
968                                                         /* For PTMP connections with non persistent layer 2 we want
969                                                          * to *not* declare inalarm unless there actually is an alarm */
970                                                         if (p->pri->sig != SIG_BRI_PTMP) {
971                                                                 p->inalarm = 1;
972                                                         }
973                                                 }
974                                         }
975                                 }
976                                 break;
977                         case PRI_EVENT_RESTART:
978                                 if (e->restart.channel > -1) {
979                                         chanpos = pri_find_principle(pri, e->restart.channel);
980                                         if (chanpos < 0)
981                                                 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
982                                                         PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
983                                         else {
984 #ifdef HAVE_PRI_SERVICE_MESSAGES
985                                                 char db_chan_name[20], db_answer[5], state;
986                                                 int why, skipit = 0;
987
988                                                 ast_mutex_lock(&pri->pvts[chanpos]->service_lock);
989                                                 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[chanpos]->pri->span, pri->pvts[chanpos]->channel);
990                                                 ast_mutex_unlock(&pri->pvts[chanpos]->service_lock);
991
992                                                 if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
993                                                         sscanf(db_answer, "%c:%d", &state, &why);
994                                                         if (why) {
995                                                                 ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), ignoring RESTART\n", pri->span,
996                                                                         e->restart.channel, (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
997                                                                 skipit = 1;
998                                                         } else {
999                                                                 ast_db_del(db_chan_name, SRVST_DBKEY);
1000                                                         }
1001                                                 }
1002                                                 if (!skipit) {
1003 #endif
1004                                                         ast_verb(3, "B-channel %d/%d restarted on span %d\n",
1005                                                                 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
1006                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1007                                                         if (pri->pvts[chanpos]->call) {
1008                                                                 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
1009                                                                 pri->pvts[chanpos]->call = NULL;
1010                                                         }
1011 #ifdef HAVE_PRI_SERVICE_MESSAGES
1012                                                 }
1013 #endif
1014                                                 /* Force soft hangup if appropriate */
1015                                                 if (pri->pvts[chanpos]->owner)
1016                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1017                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1018                                         }
1019                                 } else {
1020                                         ast_verb(3, "Restart on requested on entire span %d\n", pri->span);
1021                                         for (x = 0; x < pri->numchans; x++)
1022                                                 if (pri->pvts[x]) {
1023                                                         sig_pri_lock_private(pri->pvts[x]);
1024                                                         if (pri->pvts[x]->call) {
1025                                                                 pri_destroycall(pri->pri, pri->pvts[x]->call);
1026                                                                 pri->pvts[x]->call = NULL;
1027                                                         }
1028                                                         if (pri->pvts[x]->owner)
1029                                                                 ast_softhangup_nolock(pri->pvts[x]->owner, AST_SOFTHANGUP_DEV);
1030                                                         sig_pri_unlock_private(pri->pvts[x]);
1031                                                 }
1032                                 }
1033                                 break;
1034                         case PRI_EVENT_KEYPAD_DIGIT:
1035                                 chanpos = pri_find_principle(pri, e->digit.channel);
1036                                 if (chanpos < 0) {
1037                                         ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
1038                                                 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
1039                                 } else {
1040                                         chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
1041                                         if (chanpos > -1) {
1042                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1043                                                 /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
1044                                                 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1045                                                         && pri->pvts[chanpos]->call == e->digit.call
1046                                                         && pri->pvts[chanpos]->owner) {
1047                                                         /* how to do that */
1048                                                         int digitlen = strlen(e->digit.digits);
1049                                                         int i;
1050
1051                                                         for (i = 0; i < digitlen; i++) {
1052                                                                 struct ast_frame f = { AST_FRAME_DTMF, e->digit.digits[i], };
1053
1054                                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1055                                                         }
1056                                                 }
1057                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1058                                         }
1059                                 }
1060                                 break;
1061
1062                         case PRI_EVENT_INFO_RECEIVED:
1063                                 chanpos = pri_find_principle(pri, e->ring.channel);
1064                                 if (chanpos < 0) {
1065                                         ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
1066                                                 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1067                                 } else {
1068                                         chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
1069                                         if (chanpos > -1) {
1070                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1071                                                 /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
1072                                                 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1073                                                         && pri->pvts[chanpos]->call == e->ring.call
1074                                                         && pri->pvts[chanpos]->owner) {
1075                                                         /* how to do that */
1076                                                         int digitlen = strlen(e->ring.callednum);
1077                                                         int i;
1078
1079                                                         for (i = 0; i < digitlen; i++) {
1080                                                                 struct ast_frame f = { AST_FRAME_DTMF, e->ring.callednum[i], };
1081
1082                                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1083                                                         }
1084                                                 }
1085                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1086                                         }
1087                                 }
1088                                 break;
1089 #ifdef HAVE_PRI_SERVICE_MESSAGES
1090                         case PRI_EVENT_SERVICE:
1091                                 chanpos = pri_find_principle(pri, e->service.channel);
1092                                 if (chanpos < 0) {
1093                                         ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
1094                                                 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
1095                                 } else {
1096                                         char db_chan_name[20], db_answer[5], state;
1097                                         int ch, why = -1;
1098
1099                                         ast_mutex_lock(&pri->pvts[chanpos]->service_lock);
1100                                         ch = pri->pvts[chanpos]->channel;
1101                                         ast_mutex_unlock(&pri->pvts[chanpos]->service_lock);
1102
1103                                         snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[chanpos]->pri->span, ch);
1104                                         if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
1105                                                 sscanf(db_answer, "%c:%d", &state, &why);
1106                                                 ast_db_del(db_chan_name, SRVST_DBKEY);
1107                                         }
1108                                         switch (e->service.changestatus) {
1109                                         case 0: /* in-service */
1110                                                 if (why > -1) {
1111                                                         if (why & SRVST_NEAREND) {
1112                                                                 snprintf(db_answer, sizeof(db_answer), "%s:%d", SRVST_TYPE_OOS, SRVST_NEAREND);
1113                                                                 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
1114                                                                 ast_debug(2, "channel '%d' service state { near: out-of-service,  far: in-service }\n", ch);
1115                                                         }
1116                                                 }
1117                                                 break;
1118                                         case 2: /* out-of-service */
1119                                                 if (why == -1) {
1120                                                         why = SRVST_FAREND;
1121                                                 } else {
1122                                                         why |= SRVST_FAREND;
1123                                                 }
1124                                                 snprintf(db_answer, sizeof(db_answer), "%s:%d", SRVST_TYPE_OOS, why);
1125                                                 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
1126                                                 break;
1127                                         default:
1128                                                 ast_log(LOG_ERROR, "Huh?  changestatus is: %d\n", e->service.changestatus);
1129                                         }
1130                                         ast_log(LOG_NOTICE, "Channel %d/%d span %d (logical: %d) received a change of service message, status '%d'\n",
1131                                                 PRI_SPAN(e->service.channel), PRI_CHANNEL(e->service.channel), pri->span, ch, e->service.changestatus);
1132                                 }
1133                                 break;
1134                         case PRI_EVENT_SERVICE_ACK:
1135                                 chanpos = pri_find_principle(pri, e->service_ack.channel);
1136                                 if (chanpos < 0) {
1137                                         ast_log(LOG_WARNING, "Received service acknowledge change status '%d' on unconfigured channel %d/%d span %d\n",
1138                                                 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
1139                                 } else {
1140                                         ast_debug(2, "Channel %d/%d span %d received a change os service acknowledgement message, status '%d'\n",
1141                                                 PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span, e->service_ack.changestatus);
1142                                 }
1143                                 break;
1144 #endif
1145                         case PRI_EVENT_RING:
1146                                 if (e->ring.channel == -1)
1147                                         chanpos = pri_find_empty_chan(pri, 1);
1148                                 else
1149                                         chanpos = pri_find_principle(pri, e->ring.channel);
1150                                 /* if no channel specified find one empty */
1151                                 if (chanpos < 0) {
1152                                         ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
1153                                                 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1154                                 } else {
1155                                         sig_pri_lock_private(pri->pvts[chanpos]);
1156                                         if (pri->pvts[chanpos]->owner) {
1157                                                 if (pri->pvts[chanpos]->call == e->ring.call) {
1158                                                         ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
1159                                                                 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1160                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1161                                                         break;
1162                                                 } else {
1163                                                         /* This is where we handle initial glare */
1164                                                         ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d.  Attempting to renegotiating channel.\n",
1165                                                         PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
1166                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1167                                                         chanpos = -1;
1168                                                 }
1169                                         }
1170                                         if (chanpos > -1)
1171                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1172                                 }
1173                                 if ((chanpos < 0) && (e->ring.flexible))
1174                                         chanpos = pri_find_empty_chan(pri, 1);
1175                                 if (chanpos > -1) {
1176                                         sig_pri_lock_private(pri->pvts[chanpos]);
1177                                         pri->pvts[chanpos]->call = e->ring.call;
1178
1179                                         /* Use plancallingnum as a scratch buffer since it is initialized next. */
1180                                         apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri,
1181                                                 e->ring.redirectingnum, e->ring.callingplanrdnis);
1182                                         sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
1183
1184                                         /* Setup caller-id info */
1185                                         apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
1186                                         pri->pvts[chanpos]->cid_ani2 = 0;
1187                                         if (pri->pvts[chanpos]->use_callerid) {
1188                                                 ast_shrink_phone_number(plancallingnum);
1189                                                 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
1190 #ifdef PRI_ANI
1191                                                 if (!ast_strlen_zero(e->ring.callingani)) {
1192                                                         apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
1193                                                         ast_shrink_phone_number(plancallingani);
1194                                                         ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
1195                                                 } else {
1196                                                         pri->pvts[chanpos]->cid_ani[0] = '\0';
1197                                                 }
1198 #endif
1199                                                 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
1200                                                 pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
1201                                                 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
1202                                                 if (e->ring.ani2 >= 0) {
1203                                                         pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
1204                                                 }
1205                                         } else {
1206                                                 pri->pvts[chanpos]->cid_num[0] = '\0';
1207                                                 pri->pvts[chanpos]->cid_ani[0] = '\0';
1208                                                 pri->pvts[chanpos]->cid_name[0] = '\0';
1209                                                 pri->pvts[chanpos]->cid_ton = 0;
1210                                                 pri->pvts[chanpos]->callingpres = 0;
1211                                         }
1212                                         sig_pri_set_caller_id(pri->pvts[chanpos]);
1213
1214                                         /* Set DNID on all incoming calls -- even immediate */
1215                                         sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
1216
1217                                         /* If immediate=yes go to s|1 */
1218                                         if (pri->pvts[chanpos]->immediate) {
1219                                                 ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
1220                                                 pri->pvts[chanpos]->exten[0] = 's';
1221                                                 pri->pvts[chanpos]->exten[1] = '\0';
1222                                         }
1223                                         /* Get called number */
1224                                         else if (!ast_strlen_zero(e->ring.callednum)) {
1225                                                 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
1226                                         } else if (pri->overlapdial)
1227                                                 pri->pvts[chanpos]->exten[0] = '\0';
1228                                         else {
1229                                                 /* Some PRI circuits are set up to send _no_ digits.  Handle them as 's'. */
1230                                                 pri->pvts[chanpos]->exten[0] = 's';
1231                                                 pri->pvts[chanpos]->exten[1] = '\0';
1232                                         }
1233                                         /* No number yet, but received "sending complete"? */
1234                                         if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
1235                                                 ast_verb(3, "Going to extension s|1 because of Complete received\n");
1236                                                 pri->pvts[chanpos]->exten[0] = 's';
1237                                                 pri->pvts[chanpos]->exten[1] = '\0';
1238                                         }
1239
1240                                         /* Make sure extension exists (or in overlap dial mode, can exist) */
1241                                         if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
1242                                                 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
1243                                                 /* Setup law */
1244                                                 if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
1245                                                         /* Just announce proceeding */
1246                                                         pri->pvts[chanpos]->proceeding = 1;
1247                                                         pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
1248                                                 } else {
1249                                                         if (pri->switchtype != PRI_SWITCH_GR303_TMC)
1250                                                                 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
1251                                                         else
1252                                                                 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
1253                                                 }
1254
1255                                                 /* Start PBX */
1256                                                 if (!e->ring.complete
1257                                                         && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1258                                                         && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
1259                                                         /*
1260                                                          * Release the PRI lock while we create the channel
1261                                                          * so other threads can send D channel messages.
1262                                                          */
1263                                                         ast_mutex_unlock(&pri->lock);
1264                                                         c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RESERVED, 0, (e->ring.layer1 = PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
1265                                                         ast_mutex_lock(&pri->lock);
1266                                                         if (c) {
1267                                                                 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
1268                                                                         pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
1269                                                                 }
1270                                                                 if (e->ring.ani2 >= 0) {
1271                                                                         snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
1272                                                                         pbx_builtin_setvar_helper(c, "ANI2", ani2str);
1273                                                                 }
1274
1275 #ifdef SUPPORT_USERUSER
1276                                                                 if (!ast_strlen_zero(e->ring.useruserinfo)) {
1277                                                                         pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
1278                                                                 }
1279 #endif
1280
1281                                                                 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
1282                                                                 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
1283                                                                 if (e->ring.redirectingreason >= 0)
1284                                                                         pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
1285 #if defined(HAVE_PRI_REVERSE_CHARGE)
1286                                                                 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
1287 #endif
1288                                                         }
1289                                                         if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
1290                                                                 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
1291                                                                         plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
1292                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1293                                                         } else {
1294                                                                 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
1295                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1296                                                                 if (c)
1297                                                                         ast_hangup(c);
1298                                                                 else {
1299                                                                         pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
1300                                                                         pri->pvts[chanpos]->call = NULL;
1301                                                                 }
1302                                                         }
1303                                                 } else {
1304                                                         /*
1305                                                          * Release the PRI lock while we create the channel
1306                                                          * so other threads can send D channel messages.
1307                                                          */
1308                                                         ast_mutex_unlock(&pri->lock);
1309                                                         c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RING, 0, (e->ring.layer1 == PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
1310                                                         ast_mutex_lock(&pri->lock);
1311                                                         if (c) {
1312                                                                 /*
1313                                                                  * It is reasonably safe to set the following
1314                                                                  * channel variables while the PRI and DAHDI private
1315                                                                  * structures are locked.  The PBX has not been
1316                                                                  * started yet and it is unlikely that any other task
1317                                                                  * will do anything with the channel we have just
1318                                                                  * created.
1319                                                                  */
1320                                                                 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
1321                                                                         pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
1322                                                                 }
1323                                                                 if (e->ring.ani2 >= 0) {
1324                                                                         snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
1325                                                                         pbx_builtin_setvar_helper(c, "ANI2", ani2str);
1326                                                                 }
1327
1328 #ifdef SUPPORT_USERUSER
1329                                                                 if (!ast_strlen_zero(e->ring.useruserinfo)) {
1330                                                                         pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
1331                                                                 }
1332 #endif
1333
1334                                                                 if (e->ring.redirectingreason >= 0)
1335                                                                         pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
1336 #if defined(HAVE_PRI_REVERSE_CHARGE)
1337                                                                 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
1338 #endif
1339
1340                                                                 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
1341                                                                 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
1342                                                         }
1343                                                         if (c && !ast_pbx_start(c)) {
1344                                                                 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
1345                                                                         plancallingnum, pri->pvts[chanpos]->exten,
1346                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1347                                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1348                                                         } else {
1349                                                                 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
1350                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1351                                                                 if (c) {
1352                                                                         ast_hangup(c);
1353                                                                 } else {
1354                                                                         pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
1355                                                                         pri->pvts[chanpos]->call = NULL;
1356                                                                 }
1357                                                         }
1358                                                 }
1359                                         } else {
1360                                                 ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
1361                                                         pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
1362                                                         pri->pvts[chanpos]->prioffset, pri->span);
1363                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
1364                                                 pri->pvts[chanpos]->call = NULL;
1365                                                 pri->pvts[chanpos]->exten[0] = '\0';
1366                                         }
1367                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1368                                 } else {
1369                                         if (e->ring.flexible)
1370                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1371                                         else
1372                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
1373                                 }
1374                                 break;
1375                         case PRI_EVENT_RINGING:
1376                                 chanpos = pri_find_principle(pri, e->ringing.channel);
1377                                 if (chanpos < 0) {
1378                                         ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
1379                                                 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
1380                                 } else {
1381                                         chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
1382                                         if (chanpos < 0) {
1383                                                 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
1384                                                         PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
1385                                         } else {
1386                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1387                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1388                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_RINGING, pri);
1389                                                 pri->pvts[chanpos]->alerting = 1;
1390
1391 #ifdef SUPPORT_USERUSER
1392                                                 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
1393                                                         struct ast_channel *owner;
1394
1395                                                         sig_pri_lock_owner(pri, chanpos);
1396                                                         owner = pri->pvts[chanpos]->owner;
1397                                                         if (owner) {
1398                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1399                                                                         e->ringing.useruserinfo);
1400                                                                 ast_channel_unlock(owner);
1401                                                         }
1402                                                 }
1403 #endif
1404
1405                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1406                                         }
1407                                 }
1408                                 break;
1409                         case PRI_EVENT_PROGRESS:
1410                                 /* Get chan value if e->e is not PRI_EVNT_RINGING */
1411                                 chanpos = pri_find_principle(pri, e->proceeding.channel);
1412                                 if (chanpos > -1) {
1413                                         if ((!pri->pvts[chanpos]->progress)
1414 #ifdef PRI_PROGRESS_MASK
1415                                                 || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
1416 #else
1417                                                 || (e->proceeding.progress == 8)
1418 #endif
1419                                                 ) {
1420                                                 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
1421
1422                                                 if (e->proceeding.cause > -1) {
1423                                                         ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
1424
1425                                                         /* Work around broken, out of spec USER_BUSY cause in a progress message */
1426                                                         if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
1427                                                                 if (pri->pvts[chanpos]->owner) {
1428                                                                         ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
1429
1430                                                                         pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
1431                                                                         f.subclass = AST_CONTROL_BUSY;
1432                                                                 }
1433                                                         }
1434                                                 }
1435
1436                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1437                                                 ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
1438                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
1439                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1440                                                 if (
1441 #ifdef PRI_PROGRESS_MASK
1442                                                         e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
1443 #else
1444                                                         e->proceeding.progress == 8
1445 #endif
1446                                                         ) {
1447                                                         /* Bring voice path up */
1448                                                         f.subclass = AST_CONTROL_PROGRESS;
1449                                                         pri_queue_frame(pri->pvts[chanpos], &f, pri);
1450                                                 }
1451                                                 pri->pvts[chanpos]->progress = 1;
1452                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1453                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1454                                         }
1455                                 }
1456                                 break;
1457                         case PRI_EVENT_PROCEEDING:
1458                                 chanpos = pri_find_principle(pri, e->proceeding.channel);
1459                                 if (chanpos > -1) {
1460                                         if (!pri->pvts[chanpos]->proceeding) {
1461                                                 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
1462
1463                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1464                                                 ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
1465                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
1466                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1467                                                 if (
1468 #ifdef PRI_PROGRESS_MASK
1469                                                         e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
1470 #else
1471                                                         e->proceeding.progress == 8
1472 #endif
1473                                                         ) {
1474                                                         /* Bring voice path up */
1475                                                         f.subclass = AST_CONTROL_PROGRESS;
1476                                                         pri_queue_frame(pri->pvts[chanpos], &f, pri);
1477                                                 }
1478                                                 pri->pvts[chanpos]->proceeding = 1;
1479                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1480                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1481                                         }
1482                                 }
1483                                 break;
1484                         case PRI_EVENT_FACNAME:
1485                                 chanpos = pri_find_principle(pri, e->facname.channel);
1486                                 if (chanpos < 0) {
1487                                         ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
1488                                                 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
1489                                 } else {
1490                                         chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
1491                                         if (chanpos < 0) {
1492                                                 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
1493                                                         PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
1494                                         } else {
1495                                                 /* Re-use *69 field for PRI */
1496                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1497                                                 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
1498                                                 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
1499                                                 pri_update_cid(pri->pvts[chanpos], pri);
1500                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1501                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1502                                         }
1503                                 }
1504                                 break;
1505                         case PRI_EVENT_ANSWER:
1506                                 chanpos = pri_find_principle(pri, e->answer.channel);
1507                                 if (chanpos < 0) {
1508                                         ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
1509                                                 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
1510                                 } else {
1511                                         chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
1512                                         if (chanpos < 0) {
1513                                                 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
1514                                                         PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
1515                                         } else {
1516                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1517                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_ANSWER, pri);
1518                                                 /* Enable echo cancellation if it's not on already */
1519                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1520                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1521
1522 #ifdef SUPPORT_USERUSER
1523                                                 if (!ast_strlen_zero(e->answer.useruserinfo)) {
1524                                                         struct ast_channel *owner;
1525
1526                                                         sig_pri_lock_owner(pri, chanpos);
1527                                                         owner = pri->pvts[chanpos]->owner;
1528                                                         if (owner) {
1529                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1530                                                                         e->answer.useruserinfo);
1531                                                                 ast_channel_unlock(owner);
1532                                                         }
1533                                                 }
1534 #endif
1535
1536                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1537                                         }
1538                                 }
1539                                 break;
1540                         case PRI_EVENT_HANGUP:
1541                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1542                                 if (chanpos < 0) {
1543                                         ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
1544                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1545                                 } else {
1546                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1547                                         if (chanpos > -1) {
1548                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1549                                                 if (!pri->pvts[chanpos]->alreadyhungup) {
1550                                                         /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */
1551                                                         pri->pvts[chanpos]->alreadyhungup = 1;
1552                                                         if (pri->pvts[chanpos]->owner) {
1553                                                                 /* Queue a BUSY instead of a hangup if our cause is appropriate */
1554                                                                 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
1555                                                                 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
1556                                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1557                                                                 else {
1558                                                                         switch (e->hangup.cause) {
1559                                                                         case PRI_CAUSE_USER_BUSY:
1560                                                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
1561                                                                                 break;
1562                                                                         case PRI_CAUSE_CALL_REJECTED:
1563                                                                         case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
1564                                                                         case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
1565                                                                         case PRI_CAUSE_SWITCH_CONGESTION:
1566                                                                         case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
1567                                                                         case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
1568                                                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
1569                                                                                 break;
1570                                                                         default:
1571                                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1572                                                                                 break;
1573                                                                         }
1574                                                                 }
1575                                                         }
1576                                                         ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
1577                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
1578                                                 } else {
1579                                                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
1580                                                         pri->pvts[chanpos]->call = NULL;
1581                                                 }
1582                                                 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
1583                                                         ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
1584                                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1585                                                         pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
1586                                                         pri->pvts[chanpos]->resetting = 1;
1587                                                 }
1588                                                 if (e->hangup.aoc_units > -1)
1589                                                         ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
1590                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
1591
1592 #ifdef SUPPORT_USERUSER
1593                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1594                                                         struct ast_channel *owner;
1595
1596                                                         sig_pri_lock_owner(pri, chanpos);
1597                                                         owner = pri->pvts[chanpos]->owner;
1598                                                         if (owner) {
1599                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1600                                                                         e->hangup.useruserinfo);
1601                                                                 ast_channel_unlock(owner);
1602                                                         }
1603                                                 }
1604 #endif
1605
1606                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1607                                         } else {
1608                                                 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
1609                                                         PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1610                                         }
1611                                 }
1612                                 break;
1613 #ifndef PRI_EVENT_HANGUP_REQ
1614 #error please update libpri
1615 #endif
1616                         case PRI_EVENT_HANGUP_REQ:
1617                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1618                                 if (chanpos < 0) {
1619                                         ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
1620                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1621                                 } else {
1622                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1623                                         if (chanpos > -1) {
1624                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1625                                                 if (pri->pvts[chanpos]->owner) {
1626                                                         pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
1627                                                         if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
1628                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1629                                                         else {
1630                                                                 switch (e->hangup.cause) {
1631                                                                 case PRI_CAUSE_USER_BUSY:
1632                                                                         pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
1633                                                                         break;
1634                                                                 case PRI_CAUSE_CALL_REJECTED:
1635                                                                 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
1636                                                                 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
1637                                                                 case PRI_CAUSE_SWITCH_CONGESTION:
1638                                                                 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
1639                                                                 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
1640                                                                         pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
1641                                                                         break;
1642                                                                 default:
1643                                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1644                                                                         break;
1645                                                                 }
1646                                                         }
1647                                                         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);
1648                                                         if (e->hangup.aoc_units > -1)
1649                                                                 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
1650                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
1651                                                 } else {
1652                                                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
1653                                                         pri->pvts[chanpos]->call = NULL;
1654                                                 }
1655                                                 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
1656                                                         ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
1657                                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1658                                                         pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
1659                                                         pri->pvts[chanpos]->resetting = 1;
1660                                                 }
1661
1662 #ifdef SUPPORT_USERUSER
1663                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1664                                                         struct ast_channel *owner;
1665
1666                                                         sig_pri_lock_owner(pri, chanpos);
1667                                                         owner = pri->pvts[chanpos]->owner;
1668                                                         if (owner) {
1669                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1670                                                                         e->hangup.useruserinfo);
1671                                                                 ast_channel_unlock(owner);
1672                                                         }
1673                                                 }
1674 #endif
1675
1676                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1677                                         } else {
1678                                                 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);
1679                                         }
1680                                 }
1681                                 break;
1682                         case PRI_EVENT_HANGUP_ACK:
1683                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1684                                 if (chanpos < 0) {
1685                                         ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
1686                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1687                                 } else {
1688                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1689                                         if (chanpos > -1) {
1690                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1691                                                 pri->pvts[chanpos]->call = NULL;
1692                                                 pri->pvts[chanpos]->resetting = 0;
1693                                                 if (pri->pvts[chanpos]->owner) {
1694                                                         ast_verb(3, "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1695                                                 }
1696
1697 #ifdef SUPPORT_USERUSER
1698                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1699                                                         struct ast_channel *owner;
1700
1701                                                         sig_pri_lock_owner(pri, chanpos);
1702                                                         owner = pri->pvts[chanpos]->owner;
1703                                                         if (owner) {
1704                                                                 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
1705                                                                         e->hangup.useruserinfo);
1706                                                                 ast_channel_unlock(owner);
1707                                                         }
1708                                                 }
1709 #endif
1710
1711                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1712                                         }
1713                                 }
1714                                 break;
1715                         case PRI_EVENT_CONFIG_ERR:
1716                                 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err);
1717                                 break;
1718                         case PRI_EVENT_RESTART_ACK:
1719                                 chanpos = pri_find_principle(pri, e->restartack.channel);
1720                                 if (chanpos < 0) {
1721                                         /* Sometime switches (e.g. I421 / British Telecom) don't give us the
1722                                            channel number, so we have to figure it out...  This must be why
1723                                            everybody resets exactly a channel at a time. */
1724                                         for (x = 0; x < pri->numchans; x++) {
1725                                                 if (pri->pvts[x] && pri->pvts[x]->resetting) {
1726                                                         chanpos = x;
1727                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1728                                                         ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
1729                                                                 pri->pvts[chanpos]->prioffset, pri->span);
1730                                                         if (pri->pvts[chanpos]->owner) {
1731                                                                 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
1732                                                                         pri->pvts[chanpos]->prioffset, pri->span);
1733                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1734                                                         }
1735                                                         pri->pvts[chanpos]->resetting = 0;
1736                                                         ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
1737                                                                 pri->pvts[chanpos]->prioffset, pri->span);
1738                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1739                                                         if (pri->resetting)
1740                                                                 pri_check_restart(pri);
1741                                                         break;
1742                                                 }
1743                                         }
1744                                         if (chanpos < 0) {
1745                                                 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
1746                                                         PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
1747                                         }
1748                                 } else {
1749                                         if (pri->pvts[chanpos]) {
1750                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1751                                                 if (pri->pvts[chanpos]->owner) {
1752                                                         ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
1753                                                                 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
1754                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1755                                                 }
1756                                                 pri->pvts[chanpos]->resetting = 0;
1757                                                 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
1758                                                         pri->pvts[chanpos]->prioffset, pri->span);
1759                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1760                                                 if (pri->resetting)
1761                                                         pri_check_restart(pri);
1762                                         }
1763                                 }
1764                                 break;
1765                         case PRI_EVENT_SETUP_ACK:
1766                                 chanpos = pri_find_principle(pri, e->setup_ack.channel);
1767                                 if (chanpos < 0) {
1768                                         ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
1769                                                 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
1770                                 } else {
1771                                         chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
1772                                         if (chanpos > -1) {
1773                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1774                                                 pri->pvts[chanpos]->setup_ack = 1;
1775                                                 /* Send any queued digits */
1776                                                 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
1777                                                         ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
1778                                                         pri_information(pri->pri, pri->pvts[chanpos]->call,
1779                                                                 pri->pvts[chanpos]->dialdest[x]);
1780                                                 }
1781                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1782                                         } else
1783                                                 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
1784                                 }
1785                                 break;
1786                         case PRI_EVENT_NOTIFY:
1787                                 chanpos = pri_find_principle(pri, e->notify.channel);
1788                                 if (chanpos < 0) {
1789                                         ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
1790                                                 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
1791                                 } else if (!pri->discardremoteholdretrieval) {
1792                                         struct ast_frame f = { AST_FRAME_CONTROL, };
1793
1794                                         sig_pri_lock_private(pri->pvts[chanpos]);
1795                                         switch (e->notify.info) {
1796                                         case PRI_NOTIFY_REMOTE_HOLD:
1797                                                 f.subclass = AST_CONTROL_HOLD;
1798                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1799                                                 break;
1800                                         case PRI_NOTIFY_REMOTE_RETRIEVAL:
1801                                                 f.subclass = AST_CONTROL_UNHOLD;
1802                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1803                                                 break;
1804                                         }
1805                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1806                                 }
1807                                 break;
1808                         default:
1809                                 ast_debug(1, "Event: %d\n", e->e);
1810                         }
1811                 }
1812                 ast_mutex_unlock(&pri->lock);
1813         }
1814         /* Never reached */
1815         return NULL;
1816 }
1817
1818 void sig_pri_init_pri(struct sig_pri_pri *pri)
1819 {
1820         int i;
1821
1822         memset(pri, 0, sizeof(*pri));
1823
1824         ast_mutex_init(&pri->lock);
1825
1826         pri->master = AST_PTHREADT_NULL;
1827         for (i = 0; i < NUM_DCHANS; i++)
1828                 pri->fds[i] = -1;
1829 }
1830
1831 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
1832 {
1833         int res = 0;
1834 #ifdef SUPPORT_USERUSER
1835         const char *useruser = pbx_builtin_getvar_helper(ast, "USERUSERINFO");
1836 #endif
1837
1838         ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
1839         if (!ast->tech_pvt) {
1840                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1841                 return 0;
1842         }
1843
1844         p->owner = NULL;
1845         p->outgoing = 0;
1846         p->digital = 0;
1847         p->proceeding = 0;
1848         p->progress = 0;
1849         p->alerting = 0;
1850         p->setup_ack = 0;
1851         p->exten[0] = '\0';
1852         sig_pri_set_dialing(p, 0);
1853
1854         if (!p->call) {
1855                 res = 0;
1856                 goto exit;
1857         }
1858
1859         /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
1860         if (!pri_grab(p, p->pri)) {
1861                 if (p->alreadyhungup) {
1862                         ast_log(LOG_DEBUG, "Already hungup...  Calling hangup once, and clearing call\n");
1863
1864 #ifdef SUPPORT_USERUSER
1865                         pri_call_set_useruser(p->call, useruser);
1866 #endif
1867
1868                         pri_hangup(p->pri->pri, p->call, -1);
1869                         p->call = NULL;
1870                 } else {
1871                         const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
1872                         int icause = ast->hangupcause ? ast->hangupcause : -1;
1873                         ast_log(LOG_DEBUG, "Not yet hungup...  Calling hangup once with icause, and clearing call\n");
1874
1875 #ifdef SUPPORT_USERUSER
1876                         pri_call_set_useruser(p->call, useruser);
1877 #endif
1878
1879                         p->alreadyhungup = 1;
1880                         if (cause) {
1881                                 if (atoi(cause))
1882                                         icause = atoi(cause);
1883                         }
1884                         pri_hangup(p->pri->pri, p->call, icause);
1885                 }
1886                 if (res < 0)
1887                         ast_log(LOG_WARNING, "pri_disconnect failed\n");
1888                 pri_rel(p->pri);
1889         } else {
1890                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
1891                 res = -1;
1892         }
1893
1894 exit:
1895         ast->tech_pvt = NULL;
1896         return res;
1897 }
1898
1899 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1)
1900 {
1901         char dest[256]; /* must be same length as p->dialdest */
1902         struct pri_sr *sr;
1903         char *c, *l, *n, *s = NULL;
1904 #ifdef SUPPORT_USERUSER
1905         const char *useruser;
1906 #endif
1907         int pridialplan;
1908         int dp_strip;
1909         int prilocaldialplan;
1910         int ldp_strip;
1911         int exclusive;
1912         const char *rr_str;
1913         int redirect_reason;
1914
1915         ast_log(LOG_DEBUG, "CALLING CID_NAME: %s CID_NUM:: %s\n", ast->cid.cid_name, ast->cid.cid_num);
1916
1917         if (!p->pri) {
1918                 ast_log(LOG_ERROR, "Could not find pri on channel %d\n", p->channel);
1919                 return -1;
1920         }
1921
1922
1923         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1924                 ast_log(LOG_WARNING, "sig_pri_call called on %s, neither down nor reserved\n", ast->name);
1925                 return -1;
1926         }
1927
1928         ast_copy_string(dest, rdest, sizeof(dest));
1929
1930         p->dialdest[0] = '\0';
1931         p->outgoing = 1;
1932
1933         c = strchr(dest, '/');
1934         if (c) {
1935                 c++;
1936         } else {
1937                 c = "";
1938         }
1939
1940         l = NULL;
1941         n = NULL;
1942         if (!p->hidecallerid) {
1943                 l = ast->connected.id.number;
1944                 if (!p->hidecalleridname) {
1945                         n = ast->connected.id.name;
1946                 }
1947         }
1948
1949         if (strlen(c) < p->stripmsd) {
1950                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1951                 return -1;
1952         }
1953         if (pri_grab(p, p->pri)) {
1954                 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1955                 return -1;
1956         }
1957         if (!(p->call = pri_new_call(p->pri->pri))) {
1958                 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1959                 pri_rel(p->pri);
1960                 return -1;
1961         }
1962         if (!(sr = pri_sr_new())) {
1963                 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1964                 pri_destroycall(p->pri->pri, p->call);
1965                 p->call = NULL;
1966                 pri_rel(p->pri);
1967                 return -1;
1968         }
1969
1970         p->digital = IS_DIGITAL(ast->transfercapability);
1971
1972         /* Should the picked channel be used exclusively? */
1973         if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
1974                 exclusive = 1;
1975         } else {
1976                 exclusive = 0;
1977         }
1978
1979         pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
1980         pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
1981                 (p->digital ? -1 : layer1));
1982
1983         if (p->pri->facilityenable)
1984                 pri_facility_enable(p->pri->pri);
1985
1986         ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
1987         dp_strip = 0;
1988         pridialplan = p->pri->dialplan - 1;
1989         if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
1990                 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1991                         if (pridialplan == -2) {
1992                                 dp_strip = strlen(p->pri->internationalprefix);
1993                         }
1994                         pridialplan = PRI_INTERNATIONAL_ISDN;
1995                 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1996                         if (pridialplan == -2) {
1997                                 dp_strip = strlen(p->pri->nationalprefix);
1998                         }
1999                         pridialplan = PRI_NATIONAL_ISDN;
2000                 } else {
2001                         pridialplan = PRI_LOCAL_ISDN;
2002                 }
2003         }
2004         while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2005                 switch (c[p->stripmsd]) {
2006                 case 'U':
2007                         pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2008                         break;
2009                 case 'I':
2010                         pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2011                         break;
2012                 case 'N':
2013                         pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2014                         break;
2015                 case 'L':
2016                         pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2017                         break;
2018                 case 'S':
2019                         pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2020                         break;
2021                 case 'V':
2022                         pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2023                         break;
2024                 case 'R':
2025                         pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2026                         break;
2027                 case 'u':
2028                         pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2029                         break;
2030                 case 'e':
2031                         pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2032                         break;
2033                 case 'x':
2034                         pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2035                         break;
2036                 case 'f':
2037                         pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2038                         break;
2039                 case 'n':
2040                         pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2041                         break;
2042                 case 'p':
2043                         pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2044                         break;
2045                 case 'r':
2046                         pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2047                         break;
2048 #if defined(HAVE_PRI_REVERSE_CHARGE)
2049                 case 'C':
2050                         pri_sr_set_reversecharge(sr, PRI_REVERSECHARGE_REQUESTED);
2051                         break;
2052 #endif
2053                 default:
2054                         if (isalpha(c[p->stripmsd])) {
2055                                 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
2056                                         c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
2057                         }
2058                         break;
2059                 }
2060                 c++;
2061         }
2062         pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2063
2064         ldp_strip = 0;
2065         prilocaldialplan = p->pri->localdialplan - 1;
2066         if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2067                 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2068                         if (prilocaldialplan == -2) {
2069                                 ldp_strip = strlen(p->pri->internationalprefix);
2070                         }
2071                         prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2072                 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2073                         if (prilocaldialplan == -2) {
2074                                 ldp_strip = strlen(p->pri->nationalprefix);
2075                         }
2076                         prilocaldialplan = PRI_NATIONAL_ISDN;
2077                 } else {
2078                         prilocaldialplan = PRI_LOCAL_ISDN;
2079                 }
2080         }
2081         if (l != NULL) {
2082                 while (*l > '9' && *l != '*' && *l != '#') {
2083                         switch (*l) {
2084                         case 'U':
2085                                 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2086                                 break;
2087                         case 'I':
2088                                 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2089                                 break;
2090                         case 'N':
2091                                 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2092                                 break;
2093                         case 'L':
2094                                 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2095                                 break;
2096                         case 'S':
2097                                 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2098                                 break;
2099                         case 'V':
2100                                 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2101                                 break;
2102                         case 'R':
2103                                 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2104                                 break;
2105                         case 'u':
2106                                 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2107                                 break;
2108                         case 'e':
2109                                 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2110                                 break;
2111                         case 'x':
2112                                 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2113                                 break;
2114                         case 'f':
2115                                 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2116                                 break;
2117                         case 'n':
2118                                 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2119                                 break;
2120                         case 'p':
2121                                 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2122                                 break;
2123                         case 'r':
2124                                 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2125                                 break;
2126                         default:
2127                                 if (isalpha(*l)) {
2128                                         ast_log(LOG_WARNING,
2129                                                 "Unrecognized prilocaldialplan %s modifier: %c\n",
2130                                                 *l > 'Z' ? "NPI" : "TON", *l);
2131                                 }
2132                                 break;
2133                         }
2134                         l++;
2135                 }
2136         }
2137         pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2138                 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2139         if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2140                 if (!strcasecmp(rr_str, "UNKNOWN"))
2141                         redirect_reason = 0;
2142                 else if (!strcasecmp(rr_str, "BUSY"))
2143                         redirect_reason = 1;
2144                 else if (!strcasecmp(rr_str, "NO_REPLY"))
2145                         redirect_reason = 2;
2146                 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2147                         redirect_reason = 15;
2148                 else
2149                         redirect_reason = PRI_REDIR_UNCONDITIONAL;
2150         } else
2151                 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2152         pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2153
2154 #ifdef SUPPORT_USERUSER
2155         /* User-user info */
2156         useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2157         if (useruser)
2158                 pri_sr_set_useruser(sr, useruser);
2159 #endif
2160
2161         if (pri_setup(p->pri->pri, p->call, sr)) {
2162                 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2163                         c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2164                 pri_rel(p->pri);
2165                 pri_sr_free(sr);
2166                 return -1;
2167         }
2168         pri_sr_free(sr);
2169         ast_setstate(ast, AST_STATE_DIALING);
2170         sig_pri_set_dialing(p, 1);
2171         pri_rel(p->pri);
2172         return 0;
2173 }
2174
2175 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
2176 {
2177         int res = -1;
2178
2179         switch (condition) {
2180         case AST_CONTROL_BUSY:
2181                 if (p->priindication_oob) {
2182                         chan->hangupcause = AST_CAUSE_USER_BUSY;
2183                         chan->_softhangup |= AST_SOFTHANGUP_DEV;
2184                         res = 0;
2185                 } else if (!p->progress && p->pri && !p->outgoing) {
2186                         if (p->pri->pri) {
2187                                 if (!pri_grab(p, p->pri)) {
2188 #ifdef HAVE_PRI_PROG_W_CAUSE
2189                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_USER_BUSY); /* cause = 17 */
2190 #else
2191                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2192 #endif
2193                                         pri_rel(p->pri);
2194                                 } else {
2195                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2196                                 }
2197                         }
2198                         p->progress = 1;
2199                         res = sig_pri_play_tone(p, SIG_PRI_TONE_BUSY);
2200                 }
2201                 break;
2202         case AST_CONTROL_RINGING:
2203                 if ((!p->alerting) && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
2204                         if (p->pri->pri) {
2205                                 if (!pri_grab(p, p->pri)) {
2206                                         pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
2207                                         pri_rel(p->pri);
2208                                 } else {
2209                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2210                                 }
2211                         }
2212                         p->alerting = 1;
2213                 }
2214                 res = sig_pri_play_tone(p, SIG_PRI_TONE_RINGTONE);
2215                 if (chan->_state != AST_STATE_UP) {
2216                         if (chan->_state != AST_STATE_RING)
2217                                 ast_setstate(chan, AST_STATE_RINGING);
2218                 }
2219                 break;
2220         case AST_CONTROL_PROCEEDING:
2221                 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
2222                 if (!p->proceeding && p->pri && !p->outgoing) {
2223                         if (p->pri->pri) {
2224                                 if (!pri_grab(p, p->pri)) {
2225                                         pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
2226                                         pri_rel(p->pri);
2227                                 } else {
2228                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2229                                 }
2230                         }
2231                         p->proceeding = 1;
2232                         sig_pri_set_dialing(p, 0);
2233                 }
2234                 /* don't continue in ast_indicate */
2235                 res = 0;
2236                 break;
2237         case AST_CONTROL_PROGRESS:
2238                 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
2239                 p->digital = 0; /* Digital-only calls isn't allowing any inband progress messages */
2240                 if (!p->progress && p->pri && !p->outgoing) {
2241                         if (p->pri->pri) {
2242                                 if (!pri_grab(p, p->pri)) {
2243 #ifdef HAVE_PRI_PROG_W_CAUSE
2244                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);  /* no cause at all */
2245 #else
2246                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2247 #endif
2248                                         pri_rel(p->pri);
2249                                 } else {
2250                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2251                                 }
2252                         }
2253                         p->progress = 1;
2254                 }
2255                 /* don't continue in ast_indicate */
2256                 res = 0;
2257                 break;
2258         case AST_CONTROL_CONGESTION:
2259                 chan->hangupcause = AST_CAUSE_CONGESTION;
2260                 if (p->priindication_oob) {
2261                         chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
2262                         chan->_softhangup |= AST_SOFTHANGUP_DEV;
2263                         res = 0;
2264                 } else if (!p->progress && p->pri && !p->outgoing) {
2265                         if (p->pri) {
2266                                 if (!pri_grab(p, p->pri)) {
2267 #ifdef HAVE_PRI_PROG_W_CAUSE
2268                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION); /* cause = 42 */
2269 #else
2270                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2271 #endif
2272                                         pri_rel(p->pri);
2273                                 } else {
2274                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2275                                 }
2276                         }
2277                         p->progress = 1;
2278                         res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
2279                 }
2280                 break;
2281         case AST_CONTROL_HOLD:
2282                 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
2283                         if (!pri_grab(p, p->pri)) {
2284                                 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
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                 } else
2290                         ast_moh_start(chan, data, p->mohinterpret);
2291                 break;
2292         case AST_CONTROL_UNHOLD:
2293                 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
2294                         if (!pri_grab(p, p->pri)) {
2295                                 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
2296                                 pri_rel(p->pri);
2297                         }
2298                 } else
2299                         ast_moh_stop(chan);
2300                 break;
2301         case AST_CONTROL_SRCUPDATE:
2302                 res = 0;
2303                 break;
2304         case -1:
2305                 res = sig_pri_play_tone(p, -1);
2306                 break;
2307         }
2308
2309         return res;
2310 }
2311
2312 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast)
2313 {
2314         int res = 0;
2315         /* Send a pri acknowledge */
2316         if (!pri_grab(p, p->pri)) {
2317                 p->proceeding = 1;
2318                 sig_pri_set_dialing(p, 0);
2319                 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2320                 pri_rel(p->pri);
2321         } else {
2322                 res = -1;
2323         }
2324         ast_setstate(ast, AST_STATE_UP);
2325         return res;
2326 }
2327
2328 int sig_pri_available(struct sig_pri_chan *p, int channelmatch, ast_group_t groupmatch, int *reason, int *channelmatched, int *groupmatched)
2329 {
2330         /* If no owner definitely available */
2331         if (!p->owner) {
2332                 /* Trust PRI */
2333                 if (p->pri) {
2334 #ifdef HAVE_PRI_SERVICE_MESSAGES
2335                         char db_chan_name[20], db_answer[5], state;
2336                         int why = 0;
2337
2338                         snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, p->pri->span, p->channel);
2339                         if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
2340                                 sscanf(db_answer, "%c:%d", &state, &why);
2341                         }
2342                         if ((p->resetting || p->call) || (why)) {
2343                                 if (why) {
2344                                         *reason = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
2345                                 }
2346 #else
2347                         if (p->resetting || p->call) {
2348 #endif
2349                                 return 0;
2350                         } else {
2351                                 return 1;
2352                         }
2353                 }
2354         }
2355
2356         return 0;
2357 }
2358
2359 /* If return 0, it means this function was able to handle it (pre setup digits).  If non zero, the user of this
2360  * functions should handle it normally (generate inband DTMF) */
2361 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit)
2362 {
2363         if ((ast->_state == AST_STATE_DIALING) && !pvt->proceeding) {
2364                 if (pvt->setup_ack) {
2365                         if (!pri_grab(pvt, pvt->pri)) {
2366                                 pri_information(pvt->pri->pri, pvt->call, digit);
2367                                 pri_rel(pvt->pri);
2368                         } else {
2369                                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->pri->span);
2370                         }
2371                 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
2372                         int res;
2373                         ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
2374                         res = strlen(pvt->dialdest);
2375                         pvt->dialdest[res++] = digit;
2376                         pvt->dialdest[res] = '\0';
2377                 }
2378                 return 0;
2379         }
2380         return 1;
2381 }
2382
2383 int sig_pri_start_pri(struct sig_pri_pri *pri)
2384 {
2385         int x;
2386         int i;
2387
2388         ast_mutex_init(&pri->lock);
2389
2390         for (i = 0; i < NUM_DCHANS; i++) {
2391                 if (pri->fds[i] == -1) {
2392                         break;
2393                 }
2394
2395                 switch (pri->sig) {
2396                 case SIG_BRI:
2397                         pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
2398                         break;
2399                 case SIG_BRI_PTMP:
2400                         pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
2401                         break;
2402                 default:
2403                         pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
2404 #ifdef HAVE_PRI_SERVICE_MESSAGES
2405                         if (pri->enable_service_message_support) {
2406                                 pri_set_service_message_support(pri->dchans[i], 1);
2407                         }
2408 #endif
2409                         break;
2410                 }
2411
2412                 pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
2413 #ifdef HAVE_PRI_PROG_W_CAUSE
2414                 pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
2415 #endif
2416 #ifdef HAVE_PRI_INBANDDISCONNECT
2417                 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
2418 #endif
2419                 /* Enslave to master if appropriate */
2420                 if (i)
2421                         pri_enslave(pri->dchans[0], pri->dchans[i]);
2422                 if (!pri->dchans[i]) {
2423                         if (pri->fds[i] > 0)
2424                                 close(pri->fds[i]);
2425                         pri->fds[i] = -1;
2426                         ast_log(LOG_ERROR, "Unable to create PRI structure\n");
2427                         return -1;
2428                 }
2429                 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
2430                 pri_set_nsf(pri->dchans[i], pri->nsf);
2431 #ifdef PRI_GETSET_TIMERS
2432                 for (x = 0; x < PRI_MAX_TIMERS; x++) {
2433                         if (pri->pritimers[x] != 0)
2434                                 pri_set_timer(pri->dchans[i], x, pri->pritimers[x]);
2435                 }
2436 #endif
2437         }
2438         /* Assume primary is the one we use */
2439         pri->pri = pri->dchans[0];
2440         pri->resetpos = -1;
2441         if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
2442                 for (i = 0; i < NUM_DCHANS; i++) {
2443                         if (!pri->dchans[i])
2444                                 break;
2445                         if (pri->fds[i] > 0)
2446                                 close(pri->fds[i]);
2447                         pri->fds[i] = -1;
2448                 }
2449                 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
2450                 return -1;
2451         }
2452         return 0;
2453 }
2454
2455 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm)
2456 {
2457         if (!noalarm) {
2458                 p->inalarm = 1;
2459                 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
2460                         /* T309 is not enabled : hangup calls when alarm occurs */
2461                         if (p->call) {
2462                                 if (p->pri && p->pri->pri) {
2463                                         if (!pri_grab(p, p->pri)) {
2464                                                 pri_hangup(p->pri->pri, p->call, -1);
2465                                                 pri_destroycall(p->pri->pri, p->call);
2466                                                 p->call = NULL;
2467                                                 pri_rel(p->pri);
2468                                         } else
2469                                                 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2470                                 } else
2471                                         ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2472                         } else
2473                                 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
2474                 }
2475                 if (p->owner)
2476                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2477         } else {
2478                 p->inalarm = 0;
2479         }
2480 }
2481
2482 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)
2483 {
2484         struct sig_pri_chan *p;
2485
2486         p = ast_calloc(1, sizeof(*p));
2487
2488         if (!p)
2489                 return p;
2490
2491         p->logicalspan = logicalspan;
2492         p->prioffset = channo;
2493         p->mastertrunkgroup = trunkgroup;
2494
2495         p->calls = callback;
2496         p->chan_pvt = pvt_data;
2497
2498         pri->pvts[pri->numchans++] = p;
2499         p->pri = pri;
2500
2501         return p;
2502 }
2503
2504 static void build_status(char *s, size_t len, int status, int active)
2505 {
2506         if (!s || len < 1) {
2507                 return;
2508         }
2509         s[0] = '\0';
2510         if (!(status & DCHAN_NOTINALARM))
2511                 strncat(s, "In Alarm, ", len - strlen(s) - 1);
2512         if (status & DCHAN_UP)
2513                 strncat(s, "Up", len - strlen(s) - 1);
2514         else
2515                 strncat(s, "Down", len - strlen(s) - 1);
2516         if (active)
2517                 strncat(s, ", Active", len - strlen(s) - 1);
2518         else
2519                 strncat(s, ", Standby", len - strlen(s) - 1);
2520         s[len - 1] = '\0';
2521 }
2522
2523 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_pri *pri)
2524 {
2525         char status[256];
2526         int x;
2527         for (x = 0; x < NUM_DCHANS; x++) {
2528                 if (pri->dchans[x]) {
2529                         build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
2530                         ast_cli(fd, "PRI span %d/%d: %s\n", span, x, status);
2531                 }
2532         }
2533 }
2534
2535 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_pri *pri)
2536 {
2537         int x;
2538         char status[256];
2539
2540         for (x = 0; x < NUM_DCHANS; x++) {
2541                 if (pri->dchans[x]) {
2542 #ifdef PRI_DUMP_INFO_STR
2543                         char *info_str = NULL;
2544 #endif
2545                         ast_cli(fd, "%s D-channel: %d\n", pri_order(x), dchannels[x]);
2546                         build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
2547                         ast_cli(fd, "Status: %s\n", status);
2548 #ifdef PRI_DUMP_INFO_STR
2549                         info_str = pri_dump_info_str(pri->pri);
2550                         if (info_str) {
2551                                 ast_cli(fd, "%s", info_str);
2552                                 free(info_str);
2553                         }
2554 #else
2555                         pri_dump_info(pri->pri);
2556 #endif
2557                         ast_cli(fd, "Overlap Recv: %s\n\n", (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
2558                         ast_cli(fd, "\n");
2559                 }
2560         }
2561 }
2562
2563 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits)
2564 {
2565         sig_pri_lock_private(p);
2566
2567         if (!p->pri || !p->call) {
2568                 ast_debug(1, "Unable to find pri or call on channel!\n");
2569                 sig_pri_unlock_private(p);
2570                 return -1;
2571         }
2572
2573         if (!pri_grab(p, p->pri)) {
2574                 pri_keypad_facility(p->pri->pri, p->call, digits);
2575                 pri_rel(p->pri);
2576         } else {
2577                 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2578                 sig_pri_unlock_private(p);
2579                 return -1;
2580         }
2581
2582         sig_pri_unlock_private(p);
2583
2584         return 0;
2585 }
2586
2587 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)
2588 {
2589         int res = -1;
2590
2591         sig_pri_lock_private(p);
2592
2593         if (!p->pri || !p->call) {
2594                 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2595                 sig_pri_unlock_private(p);
2596                 return -1;
2597         }
2598
2599         switch (p->pri->sig) {
2600         case SIG_PRI:
2601                 if (!pri_grab(p, p->pri)) {
2602                         if (chanstate == AST_STATE_RING) {
2603                                 res = pri_callrerouting_facility(p->pri->pri, p->call, destination, original, reason);
2604                         }
2605                         pri_rel(p->pri);
2606                 } else {
2607                         ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->pri->span);
2608                         sig_pri_unlock_private(p);
2609                         return -1;
2610                 }
2611                 break;
2612         }
2613
2614         sig_pri_unlock_private(p);
2615
2616         return res;
2617 }
2618
2619 #ifdef HAVE_PRI_SERVICE_MESSAGES
2620 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
2621 {
2622         int channel = PVT_TO_CHANNEL(p);
2623         int span = PRI_SPAN(channel);
2624
2625         return pri_maintenance_service(pri, span, channel, changestatus);
2626 }
2627 #endif
2628
2629 #endif /* HAVE_PRI */