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