Fix CALLERID() values for sig_pri on incoming calls.
[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
1231                                                         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);
1232
1233                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1234
1235                                                         if (!ast_strlen_zero(e->ring.callingsubaddr)) {
1236                                                                 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
1237                                                         }
1238                                                         if (e->ring.ani2 >= 0) {
1239                                                                 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
1240                                                                 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
1241                                                         }
1242
1243 #ifdef SUPPORT_USERUSER
1244                                                         if (!ast_strlen_zero(e->ring.useruserinfo)) {
1245                                                                 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
1246                                                         }
1247 #endif
1248
1249                                                         snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
1250                                                         pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
1251                                                         if (e->ring.redirectingreason >= 0)
1252                                                                 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
1253 #if defined(HAVE_PRI_REVERSE_CHARGE)
1254                                                         pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
1255 #endif
1256
1257                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1258                                                         ast_mutex_lock(&pri->lock);
1259
1260                                                         pthread_attr_init(&attr);
1261                                                         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1262                                                         if (c && !ast_pthread_create(&threadid, &attr, pri_ss_thread, pri->pvts[chanpos])) {
1263                                                                 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
1264                                                                                 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
1265                                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1266                                                         } else {
1267                                                                 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
1268                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1269                                                                 if (c)
1270                                                                         ast_hangup(c);
1271                                                                 else {
1272                                                                         pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
1273                                                                         pri->pvts[chanpos]->call = NULL;
1274                                                                 }
1275                                                         }
1276                                                         pthread_attr_destroy(&attr);
1277                                                 } else {
1278                                                         /*
1279                                                          * Release the PRI lock while we create the channel
1280                                                          * so other threads can send D channel messages.
1281                                                          */
1282                                                         ast_mutex_unlock(&pri->lock);
1283                                                         c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RING, 1, (e->ring.layer1 == PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
1284
1285                                                         if (c) {
1286                                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1287
1288                                                                 if (e->ring.ani2 >= 0) {
1289                                                                         snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
1290                                                                         pbx_builtin_setvar_helper(c, "ANI2", ani2str);
1291                                                                 }
1292
1293 #ifdef SUPPORT_USERUSER
1294                                                                 if (!ast_strlen_zero(e->ring.useruserinfo)) {
1295                                                                         pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
1296                                                                 }
1297 #endif
1298
1299                                                                 if (e->ring.redirectingreason >= 0)
1300                                                                         pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
1301 #if defined(HAVE_PRI_REVERSE_CHARGE)
1302                                                                 pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
1303 #endif
1304
1305                                                                 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
1306                                                                 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
1307
1308                                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1309                                                                 ast_mutex_lock(&pri->lock);
1310
1311                                                                 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
1312                                                                         plancallingnum, pri->pvts[chanpos]->exten,
1313                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1314                                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1315                                                         } else {
1316
1317                                                                 ast_mutex_lock(&pri->lock);
1318
1319                                                                 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
1320                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
1321                                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
1322                                                                 pri->pvts[chanpos]->call = NULL;
1323                                                         }
1324                                                 }
1325                                         } else {
1326                                                 ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
1327                                                         pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
1328                                                         pri->pvts[chanpos]->prioffset, pri->span);
1329                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
1330                                                 pri->pvts[chanpos]->call = NULL;
1331                                                 pri->pvts[chanpos]->exten[0] = '\0';
1332                                         }
1333                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1334                                 } else {
1335                                         if (e->ring.flexible)
1336                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1337                                         else
1338                                                 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
1339                                 }
1340                                 break;
1341                         case PRI_EVENT_RINGING:
1342                                 chanpos = pri_find_principle(pri, e->ringing.channel);
1343                                 if (chanpos < 0) {
1344                                         ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
1345                                                 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
1346                                 } else {
1347                                         chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
1348                                         if (chanpos < 0) {
1349                                                 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
1350                                                         PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
1351                                         } else {
1352                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1353                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1354                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_RINGING, pri);
1355                                                 pri->pvts[chanpos]->alerting = 1;
1356
1357 #ifdef SUPPORT_USERUSER
1358                                                 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
1359                                                         struct ast_channel *owner = pri->pvts[chanpos]->owner;
1360                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1361                                                         pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo);
1362                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1363                                                 }
1364 #endif
1365
1366                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1367                                         }
1368                                 }
1369                                 break;
1370                         case PRI_EVENT_PROGRESS:
1371                                 /* Get chan value if e->e is not PRI_EVNT_RINGING */
1372                                 chanpos = pri_find_principle(pri, e->proceeding.channel);
1373                                 if (chanpos > -1) {
1374                                         if ((!pri->pvts[chanpos]->progress)
1375 #ifdef PRI_PROGRESS_MASK
1376                                                 || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
1377 #else
1378                                                 || (e->proceeding.progress == 8)
1379 #endif
1380                                                 ) {
1381                                                 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
1382
1383                                                 if (e->proceeding.cause > -1) {
1384                                                         ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
1385
1386                                                         /* Work around broken, out of spec USER_BUSY cause in a progress message */
1387                                                         if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
1388                                                                 if (pri->pvts[chanpos]->owner) {
1389                                                                         ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
1390
1391                                                                         pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
1392                                                                         f.subclass = AST_CONTROL_BUSY;
1393                                                                 }
1394                                                         }
1395                                                 }
1396
1397                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1398                                                 ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
1399                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
1400                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1401                                                 if (
1402 #ifdef PRI_PROGRESS_MASK
1403                                                         e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
1404 #else
1405                                                         e->proceeding.progress == 8
1406 #endif
1407                                                         ) {
1408                                                         /* Bring voice path up */
1409                                                         f.subclass = AST_CONTROL_PROGRESS;
1410                                                         pri_queue_frame(pri->pvts[chanpos], &f, pri);
1411                                                 }
1412                                                 pri->pvts[chanpos]->progress = 1;
1413                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1414                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1415                                         }
1416                                 }
1417                                 break;
1418                         case PRI_EVENT_PROCEEDING:
1419                                 chanpos = pri_find_principle(pri, e->proceeding.channel);
1420                                 if (chanpos > -1) {
1421                                         if (!pri->pvts[chanpos]->proceeding) {
1422                                                 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
1423
1424                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1425                                                 ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
1426                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
1427                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1428                                                 if (
1429 #ifdef PRI_PROGRESS_MASK
1430                                                         e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
1431 #else
1432                                                         e->proceeding.progress == 8
1433 #endif
1434                                                         ) {
1435                                                         /* Bring voice path up */
1436                                                         f.subclass = AST_CONTROL_PROGRESS;
1437                                                         pri_queue_frame(pri->pvts[chanpos], &f, pri);
1438                                                 }
1439                                                 pri->pvts[chanpos]->proceeding = 1;
1440                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1441                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1442                                         }
1443                                 }
1444                                 break;
1445                         case PRI_EVENT_FACNAME:
1446                                 chanpos = pri_find_principle(pri, e->facname.channel);
1447                                 if (chanpos < 0) {
1448                                         ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
1449                                                 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
1450                                 } else {
1451                                         chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
1452                                         if (chanpos < 0) {
1453                                                 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
1454                                                         PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
1455                                         } else {
1456                                                 /* Re-use *69 field for PRI */
1457                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1458                                                 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
1459                                                 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
1460                                                 pri_update_cid(pri->pvts[chanpos], pri);
1461                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1462                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1463                                         }
1464                                 }
1465                                 break;
1466                         case PRI_EVENT_ANSWER:
1467                                 chanpos = pri_find_principle(pri, e->answer.channel);
1468                                 if (chanpos < 0) {
1469                                         ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
1470                                                 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
1471                                 } else {
1472                                         chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
1473                                         if (chanpos < 0) {
1474                                                 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
1475                                                         PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
1476                                         } else {
1477                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1478                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_ANSWER, pri);
1479                                                 /* Enable echo cancellation if it's not on already */
1480                                                 sig_pri_set_dialing(pri->pvts[chanpos], 0);
1481                                                 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
1482
1483 #ifdef SUPPORT_USERUSER
1484                                                 if (!ast_strlen_zero(e->answer.useruserinfo)) {
1485                                                         struct ast_channel *owner = pri->pvts[chanpos]->owner;
1486                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1487                                                         pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo);
1488                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1489                                                 }
1490 #endif
1491
1492                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1493                                         }
1494                                 }
1495                                 break;
1496                         case PRI_EVENT_HANGUP:
1497                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1498                                 if (chanpos < 0) {
1499                                         ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
1500                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1501                                 } else {
1502                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1503                                         if (chanpos > -1) {
1504                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1505                                                 if (!pri->pvts[chanpos]->alreadyhungup) {
1506                                                         /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */
1507                                                         pri->pvts[chanpos]->alreadyhungup = 1;
1508                                                         if (pri->pvts[chanpos]->owner) {
1509                                                                 /* Queue a BUSY instead of a hangup if our cause is appropriate */
1510                                                                 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
1511                                                                 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
1512                                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1513                                                                 else {
1514                                                                         switch (e->hangup.cause) {
1515                                                                         case PRI_CAUSE_USER_BUSY:
1516                                                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
1517                                                                                 break;
1518                                                                         case PRI_CAUSE_CALL_REJECTED:
1519                                                                         case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
1520                                                                         case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
1521                                                                         case PRI_CAUSE_SWITCH_CONGESTION:
1522                                                                         case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
1523                                                                         case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
1524                                                                                 pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
1525                                                                                 break;
1526                                                                         default:
1527                                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1528                                                                                 break;
1529                                                                         }
1530                                                                 }
1531                                                         }
1532                                                         ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
1533                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
1534                                                 } else {
1535                                                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
1536                                                         pri->pvts[chanpos]->call = NULL;
1537                                                 }
1538                                                 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
1539                                                         ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
1540                                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1541                                                         pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
1542                                                         pri->pvts[chanpos]->resetting = 1;
1543                                                 }
1544                                                 if (e->hangup.aoc_units > -1)
1545                                                         ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
1546                                                                 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
1547
1548 #ifdef SUPPORT_USERUSER
1549                                                 if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) {
1550                                                         struct ast_channel *owner = pri->pvts[chanpos]->owner;
1551                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1552                                                         pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
1553                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1554                                                 }
1555 #endif
1556
1557                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1558                                         } else {
1559                                                 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
1560                                                         PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1561                                         }
1562                                 }
1563                                 break;
1564 #ifndef PRI_EVENT_HANGUP_REQ
1565 #error please update libpri
1566 #endif
1567                         case PRI_EVENT_HANGUP_REQ:
1568                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1569                                 if (chanpos < 0) {
1570                                         ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
1571                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1572                                 } else {
1573                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1574                                         if (chanpos > -1) {
1575                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1576                                                 if (pri->pvts[chanpos]->owner) {
1577                                                         pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
1578                                                         if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
1579                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1580                                                         else {
1581                                                                 switch (e->hangup.cause) {
1582                                                                 case PRI_CAUSE_USER_BUSY:
1583                                                                         pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
1584                                                                         break;
1585                                                                 case PRI_CAUSE_CALL_REJECTED:
1586                                                                 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
1587                                                                 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
1588                                                                 case PRI_CAUSE_SWITCH_CONGESTION:
1589                                                                 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
1590                                                                 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
1591                                                                         pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
1592                                                                         break;
1593                                                                 default:
1594                                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1595                                                                         break;
1596                                                                 }
1597                                                         }
1598                                                         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);
1599                                                         if (e->hangup.aoc_units > -1)
1600                                                                 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
1601                                                                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
1602                                                 } else {
1603                                                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
1604                                                         pri->pvts[chanpos]->call = NULL;
1605                                                 }
1606                                                 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
1607                                                         ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
1608                                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1609                                                         pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
1610                                                         pri->pvts[chanpos]->resetting = 1;
1611                                                 }
1612
1613 #ifdef SUPPORT_USERUSER
1614                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1615                                                         struct ast_channel *owner = pri->pvts[chanpos]->owner;
1616                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1617                                                         pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
1618                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1619                                                 }
1620 #endif
1621
1622                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1623                                         } else {
1624                                                 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);
1625                                         }
1626                                 }
1627                                 break;
1628                         case PRI_EVENT_HANGUP_ACK:
1629                                 chanpos = pri_find_principle(pri, e->hangup.channel);
1630                                 if (chanpos < 0) {
1631                                         ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
1632                                                 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1633                                 } else {
1634                                         chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
1635                                         if (chanpos > -1) {
1636                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1637                                                 pri->pvts[chanpos]->call = NULL;
1638                                                 pri->pvts[chanpos]->resetting = 0;
1639                                                 if (pri->pvts[chanpos]->owner) {
1640                                                         ast_verb(3, "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
1641                                                 }
1642
1643 #ifdef SUPPORT_USERUSER
1644                                                 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
1645                                                         struct ast_channel *owner = pri->pvts[chanpos]->owner;
1646                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1647                                                         pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
1648                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1649                                                 }
1650 #endif
1651
1652                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1653                                         }
1654                                 }
1655                                 break;
1656                         case PRI_EVENT_CONFIG_ERR:
1657                                 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err);
1658                                 break;
1659                         case PRI_EVENT_RESTART_ACK:
1660                                 chanpos = pri_find_principle(pri, e->restartack.channel);
1661                                 if (chanpos < 0) {
1662                                         /* Sometime switches (e.g. I421 / British Telecom) don't give us the
1663                                            channel number, so we have to figure it out...  This must be why
1664                                            everybody resets exactly a channel at a time. */
1665                                         for (x = 0; x < pri->numchans; x++) {
1666                                                 if (pri->pvts[x] && pri->pvts[x]->resetting) {
1667                                                         chanpos = x;
1668                                                         sig_pri_lock_private(pri->pvts[chanpos]);
1669                                                         ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
1670                                                                 pri->pvts[chanpos]->prioffset, pri->span);
1671                                                         if (pri->pvts[chanpos]->owner) {
1672                                                                 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
1673                                                                         pri->pvts[chanpos]->prioffset, pri->span);
1674                                                                 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1675                                                         }
1676                                                         pri->pvts[chanpos]->resetting = 0;
1677                                                         ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
1678                                                                 pri->pvts[chanpos]->prioffset, pri->span);
1679                                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1680                                                         if (pri->resetting)
1681                                                                 pri_check_restart(pri);
1682                                                         break;
1683                                                 }
1684                                         }
1685                                         if (chanpos < 0) {
1686                                                 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
1687                                                         PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
1688                                         }
1689                                 } else {
1690                                         if (pri->pvts[chanpos]) {
1691                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1692                                                 if (pri->pvts[chanpos]->owner) {
1693                                                         ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
1694                                                                 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
1695                                                         ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
1696                                                 }
1697                                                 pri->pvts[chanpos]->resetting = 0;
1698                                                 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
1699                                                         pri->pvts[chanpos]->prioffset, pri->span);
1700                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1701                                                 if (pri->resetting)
1702                                                         pri_check_restart(pri);
1703                                         }
1704                                 }
1705                                 break;
1706                         case PRI_EVENT_SETUP_ACK:
1707                                 chanpos = pri_find_principle(pri, e->setup_ack.channel);
1708                                 if (chanpos < 0) {
1709                                         ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
1710                                                 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
1711                                 } else {
1712                                         chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
1713                                         if (chanpos > -1) {
1714                                                 sig_pri_lock_private(pri->pvts[chanpos]);
1715                                                 pri->pvts[chanpos]->setup_ack = 1;
1716                                                 /* Send any queued digits */
1717                                                 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
1718                                                         ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
1719                                                         pri_information(pri->pri, pri->pvts[chanpos]->call,
1720                                                                 pri->pvts[chanpos]->dialdest[x]);
1721                                                 }
1722                                                 sig_pri_unlock_private(pri->pvts[chanpos]);
1723                                         } else
1724                                                 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
1725                                 }
1726                                 break;
1727                         case PRI_EVENT_NOTIFY:
1728                                 chanpos = pri_find_principle(pri, e->notify.channel);
1729                                 if (chanpos < 0) {
1730                                         ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
1731                                                 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
1732                                 } else {
1733                                         struct ast_frame f = { AST_FRAME_CONTROL, };
1734
1735                                         sig_pri_lock_private(pri->pvts[chanpos]);
1736                                         switch (e->notify.info) {
1737                                         case PRI_NOTIFY_REMOTE_HOLD:
1738                                                 f.subclass = AST_CONTROL_HOLD;
1739                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1740                                                 break;
1741                                         case PRI_NOTIFY_REMOTE_RETRIEVAL:
1742                                                 f.subclass = AST_CONTROL_UNHOLD;
1743                                                 pri_queue_frame(pri->pvts[chanpos], &f, pri);
1744                                                 break;
1745                                         }
1746                                         sig_pri_unlock_private(pri->pvts[chanpos]);
1747                                 }
1748                                 break;
1749                         default:
1750                                 ast_debug(1, "Event: %d\n", e->e);
1751                         }
1752                 }
1753                 ast_mutex_unlock(&pri->lock);
1754         }
1755         /* Never reached */
1756         return NULL;
1757 }
1758
1759 void sig_pri_init_pri(struct sig_pri_pri *pri)
1760 {
1761         int i;
1762
1763         memset(pri, 0, sizeof(*pri));
1764
1765         ast_mutex_init(&pri->lock);
1766
1767         pri->master = AST_PTHREADT_NULL;
1768         for (i = 0; i < NUM_DCHANS; i++)
1769                 pri->fds[i] = -1;
1770 }
1771
1772 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
1773 {
1774         int res = 0;
1775 #ifdef SUPPORT_USERUSER
1776         const char *useruser = pbx_builtin_getvar_helper(ast, "USERUSERINFO");
1777 #endif
1778
1779         ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
1780         if (!ast->tech_pvt) {
1781                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1782                 return 0;
1783         }
1784
1785         p->owner = NULL;
1786         p->outgoing = 0;
1787         p->digital = 0;
1788         p->proceeding = 0;
1789         p->progress = 0;
1790         p->alerting = 0;
1791         p->setup_ack = 0;
1792         p->exten[0] = '\0';
1793         sig_pri_set_dialing(p, 0);
1794
1795         if (!p->call) {
1796                 res = 0;
1797                 goto exit;
1798         }
1799
1800         /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
1801         if (!pri_grab(p, p->pri)) {
1802                 if (p->alreadyhungup) {
1803                         ast_log(LOG_DEBUG, "Already hungup...  Calling hangup once, and clearing call\n");
1804
1805 #ifdef SUPPORT_USERUSER
1806                         pri_call_set_useruser(p->call, useruser);
1807 #endif
1808
1809                         pri_hangup(p->pri->pri, p->call, -1);
1810                         p->call = NULL;
1811                 } else {
1812                         const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
1813                         int icause = ast->hangupcause ? ast->hangupcause : -1;
1814                         ast_log(LOG_DEBUG, "Not yet hungup...  Calling hangup once with icause, and clearing call\n");
1815
1816 #ifdef SUPPORT_USERUSER
1817                         pri_call_set_useruser(p->call, useruser);
1818 #endif
1819
1820                         p->alreadyhungup = 1;
1821                         if (cause) {
1822                                 if (atoi(cause))
1823                                         icause = atoi(cause);
1824                         }
1825                         pri_hangup(p->pri->pri, p->call, icause);
1826                 }
1827                 if (res < 0)
1828                         ast_log(LOG_WARNING, "pri_disconnect failed\n");
1829                 pri_rel(p->pri);
1830         } else {
1831                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
1832                 res = -1;
1833         }
1834
1835 exit:
1836         ast->tech_pvt = NULL;
1837         return res;
1838 }
1839
1840 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1)
1841 {
1842         char dest[256]; /* must be same length as p->dialdest */
1843         struct pri_sr *sr;
1844         char *c, *l, *n, *s = NULL;
1845 #ifdef SUPPORT_USERUSER
1846         const char *useruser;
1847 #endif
1848         int pridialplan;
1849         int dp_strip;
1850         int prilocaldialplan;
1851         int ldp_strip;
1852         int exclusive;
1853         const char *rr_str;
1854         int redirect_reason;
1855
1856         ast_log(LOG_DEBUG, "CALLING CID_NAME: %s CID_NUM:: %s\n", ast->cid.cid_name, ast->cid.cid_num);
1857
1858         if (!p->pri) {
1859                 ast_log(LOG_ERROR, "Could not find pri on channel %d\n", p->channel);
1860                 return -1;
1861         }
1862
1863
1864         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1865                 ast_log(LOG_WARNING, "sig_pri_call called on %s, neither down nor reserved\n", ast->name);
1866                 return -1;
1867         }
1868
1869         ast_copy_string(dest, rdest, sizeof(dest));
1870
1871         p->dialdest[0] = '\0';
1872         p->outgoing = 1;
1873
1874         c = strchr(dest, '/');
1875         if (c) {
1876                 c++;
1877         } else {
1878                 c = "";
1879         }
1880
1881         l = NULL;
1882         n = NULL;
1883         if (!p->hidecallerid) {
1884                 l = ast->connected.id.number;
1885                 if (!p->hidecalleridname) {
1886                         n = ast->connected.id.name;
1887                 }
1888         }
1889
1890         if (strlen(c) < p->stripmsd) {
1891                 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1892                 return -1;
1893         }
1894         if (pri_grab(p, p->pri)) {
1895                 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1896                 return -1;
1897         }
1898         if (!(p->call = pri_new_call(p->pri->pri))) {
1899                 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1900                 pri_rel(p->pri);
1901                 return -1;
1902         }
1903         if (!(sr = pri_sr_new())) {
1904                 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1905                 pri_destroycall(p->pri->pri, p->call);
1906                 p->call = NULL;
1907                 pri_rel(p->pri);
1908                 return -1;
1909         }
1910
1911         p->digital = IS_DIGITAL(ast->transfercapability);
1912
1913         /* Should the picked channel be used exclusively? */
1914         if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
1915                 exclusive = 1;
1916         } else {
1917                 exclusive = 0;
1918         }
1919
1920         pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
1921         pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
1922                 (p->digital ? -1 : layer1));
1923
1924         if (p->pri->facilityenable)
1925                 pri_facility_enable(p->pri->pri);
1926
1927         ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
1928         dp_strip = 0;
1929         pridialplan = p->pri->dialplan - 1;
1930         if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
1931                 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1932                         if (pridialplan == -2) {
1933                                 dp_strip = strlen(p->pri->internationalprefix);
1934                         }
1935                         pridialplan = PRI_INTERNATIONAL_ISDN;
1936                 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1937                         if (pridialplan == -2) {
1938                                 dp_strip = strlen(p->pri->nationalprefix);
1939                         }
1940                         pridialplan = PRI_NATIONAL_ISDN;
1941                 } else {
1942                         pridialplan = PRI_LOCAL_ISDN;
1943                 }
1944         }
1945         while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
1946                 switch (c[p->stripmsd]) {
1947                 case 'U':
1948                         pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
1949                         break;
1950                 case 'I':
1951                         pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
1952                         break;
1953                 case 'N':
1954                         pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
1955                         break;
1956                 case 'L':
1957                         pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
1958                         break;
1959                 case 'S':
1960                         pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
1961                         break;
1962                 case 'V':
1963                         pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
1964                         break;
1965                 case 'R':
1966                         pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
1967                         break;
1968                 case 'u':
1969                         pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
1970                         break;
1971                 case 'e':
1972                         pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
1973                         break;
1974                 case 'x':
1975                         pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
1976                         break;
1977                 case 'f':
1978                         pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
1979                         break;
1980                 case 'n':
1981                         pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
1982                         break;
1983                 case 'p':
1984                         pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
1985                         break;
1986                 case 'r':
1987                         pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
1988                         break;
1989 #if defined(HAVE_PRI_REVERSE_CHARGE)
1990                 case 'C':
1991                         pri_sr_set_reversecharge(sr, PRI_REVERSECHARGE_REQUESTED);
1992                         break;
1993 #endif
1994                 default:
1995                         if (isalpha(c[p->stripmsd])) {
1996                                 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
1997                                         c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
1998                         }
1999                         break;
2000                 }
2001                 c++;
2002         }
2003         pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2004
2005         ldp_strip = 0;
2006         prilocaldialplan = p->pri->localdialplan - 1;
2007         if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2008                 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2009                         if (prilocaldialplan == -2) {
2010                                 ldp_strip = strlen(p->pri->internationalprefix);
2011                         }
2012                         prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2013                 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2014                         if (prilocaldialplan == -2) {
2015                                 ldp_strip = strlen(p->pri->nationalprefix);
2016                         }
2017                         prilocaldialplan = PRI_NATIONAL_ISDN;
2018                 } else {
2019                         prilocaldialplan = PRI_LOCAL_ISDN;
2020                 }
2021         }
2022         if (l != NULL) {
2023                 while (*l > '9' && *l != '*' && *l != '#') {
2024                         switch (*l) {
2025                         case 'U':
2026                                 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2027                                 break;
2028                         case 'I':
2029                                 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2030                                 break;
2031                         case 'N':
2032                                 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2033                                 break;
2034                         case 'L':
2035                                 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2036                                 break;
2037                         case 'S':
2038                                 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2039                                 break;
2040                         case 'V':
2041                                 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2042                                 break;
2043                         case 'R':
2044                                 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2045                                 break;
2046                         case 'u':
2047                                 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2048                                 break;
2049                         case 'e':
2050                                 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2051                                 break;
2052                         case 'x':
2053                                 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2054                                 break;
2055                         case 'f':
2056                                 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2057                                 break;
2058                         case 'n':
2059                                 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2060                                 break;
2061                         case 'p':
2062                                 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2063                                 break;
2064                         case 'r':
2065                                 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2066                                 break;
2067                         default:
2068                                 if (isalpha(*l)) {
2069                                         ast_log(LOG_WARNING,
2070                                                 "Unrecognized prilocaldialplan %s modifier: %c\n",
2071                                                 *l > 'Z' ? "NPI" : "TON", *l);
2072                                 }
2073                                 break;
2074                         }
2075                         l++;
2076                 }
2077         }
2078         pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2079                 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2080         if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2081                 if (!strcasecmp(rr_str, "UNKNOWN"))
2082                         redirect_reason = 0;
2083                 else if (!strcasecmp(rr_str, "BUSY"))
2084                         redirect_reason = 1;
2085                 else if (!strcasecmp(rr_str, "NO_REPLY"))
2086                         redirect_reason = 2;
2087                 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2088                         redirect_reason = 15;
2089                 else
2090                         redirect_reason = PRI_REDIR_UNCONDITIONAL;
2091         } else
2092                 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2093         pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2094
2095 #ifdef SUPPORT_USERUSER
2096         /* User-user info */
2097         useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2098         if (useruser)
2099                 pri_sr_set_useruser(sr, useruser);
2100 #endif
2101
2102         if (pri_setup(p->pri->pri, p->call, sr)) {
2103                 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2104                         c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2105                 pri_rel(p->pri);
2106                 pri_sr_free(sr);
2107                 return -1;
2108         }
2109         pri_sr_free(sr);
2110         ast_setstate(ast, AST_STATE_DIALING);
2111         sig_pri_set_dialing(p, 1);
2112         pri_rel(p->pri);
2113         return 0;
2114 }
2115
2116 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
2117 {
2118         int res = -1;
2119
2120         switch (condition) {
2121         case AST_CONTROL_BUSY:
2122                 if (p->priindication_oob) {
2123                         chan->hangupcause = AST_CAUSE_USER_BUSY;
2124                         chan->_softhangup |= AST_SOFTHANGUP_DEV;
2125                         res = 0;
2126                 } else if (!p->progress && p->pri && !p->outgoing) {
2127                         if (p->pri->pri) {
2128                                 if (!pri_grab(p, p->pri)) {
2129 #ifdef HAVE_PRI_PROG_W_CAUSE
2130                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_USER_BUSY); /* cause = 17 */
2131 #else
2132                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2133 #endif
2134                                         pri_rel(p->pri);
2135                                 } else {
2136                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2137                                 }
2138                         }
2139                         p->progress = 1;
2140                         res = sig_pri_play_tone(p, SIG_PRI_TONE_BUSY);
2141                 }
2142                 break;
2143         case AST_CONTROL_RINGING:
2144                 if ((!p->alerting) && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
2145                         if (p->pri->pri) {
2146                                 if (!pri_grab(p, p->pri)) {
2147                                         pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
2148                                         pri_rel(p->pri);
2149                                 } else {
2150                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2151                                 }
2152                         }
2153                         p->alerting = 1;
2154                 }
2155                 res = sig_pri_play_tone(p, SIG_PRI_TONE_RINGTONE);
2156                 if (chan->_state != AST_STATE_UP) {
2157                         if (chan->_state != AST_STATE_RING)
2158                                 ast_setstate(chan, AST_STATE_RINGING);
2159                 }
2160                 break;
2161         case AST_CONTROL_PROCEEDING:
2162                 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
2163                 if (!p->proceeding && p->pri && !p->outgoing) {
2164                         if (p->pri->pri) {
2165                                 if (!pri_grab(p, p->pri)) {
2166                                         pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
2167                                         pri_rel(p->pri);
2168                                 } else {
2169                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2170                                 }
2171                         }
2172                         p->proceeding = 1;
2173                         sig_pri_set_dialing(p, 0);
2174                 }
2175                 /* don't continue in ast_indicate */
2176                 res = 0;
2177                 break;
2178         case AST_CONTROL_PROGRESS:
2179                 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
2180                 p->digital = 0; /* Digital-only calls isn't allowing any inband progress messages */
2181                 if (!p->progress && p->pri && !p->outgoing) {
2182                         if (p->pri->pri) {
2183                                 if (!pri_grab(p, p->pri)) {
2184 #ifdef HAVE_PRI_PROG_W_CAUSE
2185                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);  /* no cause at all */
2186 #else
2187                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2188 #endif
2189                                         pri_rel(p->pri);
2190                                 } else {
2191                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2192                                 }
2193                         }
2194                         p->progress = 1;
2195                 }
2196                 /* don't continue in ast_indicate */
2197                 res = 0;
2198                 break;
2199         case AST_CONTROL_CONGESTION:
2200                 chan->hangupcause = AST_CAUSE_CONGESTION;
2201                 if (p->priindication_oob) {
2202                         chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
2203                         chan->_softhangup |= AST_SOFTHANGUP_DEV;
2204                         res = 0;
2205                 } else if (!p->progress && p->pri && !p->outgoing) {
2206                         if (p->pri) {
2207                                 if (!pri_grab(p, p->pri)) {
2208 #ifdef HAVE_PRI_PROG_W_CAUSE
2209                                         pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION); /* cause = 42 */
2210 #else
2211                                         pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
2212 #endif
2213                                         pri_rel(p->pri);
2214                                 } else {
2215                                         ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2216                                 }
2217                         }
2218                         p->progress = 1;
2219                         res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
2220                 }
2221                 break;
2222         case AST_CONTROL_HOLD:
2223                 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
2224                         if (!pri_grab(p, p->pri)) {
2225                                 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
2226                                 pri_rel(p->pri);
2227                         } else {
2228                                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
2229                         }
2230                 } else
2231                         ast_moh_start(chan, data, p->mohinterpret);
2232                 break;
2233         case AST_CONTROL_UNHOLD:
2234                 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
2235                         if (!pri_grab(p, p->pri)) {
2236                                 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
2237                                 pri_rel(p->pri);
2238                         }
2239                 } else
2240                         ast_moh_stop(chan);
2241                 break;
2242         case AST_CONTROL_SRCUPDATE:
2243                 res = 0;
2244                 break;
2245         case -1:
2246                 res = sig_pri_play_tone(p, -1);
2247                 break;
2248         }
2249
2250         return res;
2251 }
2252
2253 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast)
2254 {
2255         int res = 0;
2256         /* Send a pri acknowledge */
2257         if (!pri_grab(p, p->pri)) {
2258                 p->proceeding = 1;
2259                 sig_pri_set_dialing(p, 0);
2260                 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2261                 pri_rel(p->pri);
2262         } else {
2263                 res = -1;
2264         }
2265         ast_setstate(ast, AST_STATE_UP);
2266         return res;
2267 }
2268
2269 int sig_pri_available(struct sig_pri_chan *p, int channelmatch, ast_group_t groupmatch, int *reason, int *channelmatched, int *groupmatched)
2270 {
2271         /* If no owner definitely available */
2272         if (!p->owner) {
2273                 /* Trust PRI */
2274                 if (p->pri) {
2275 #ifdef HAVE_PRI_SERVICE_MESSAGES
2276                         char db_chan_name[20], db_answer[5], state;
2277                         int why = 0;
2278
2279                         snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, p->pri->span, p->channel);
2280                         if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
2281                                 sscanf(db_answer, "%c:%d", &state, &why);
2282                         }
2283                         if ((p->resetting || p->call) || (why)) {
2284                                 if (why) {
2285                                         *reason = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
2286                                 }
2287 #else
2288                         if (p->resetting || p->call) {
2289 #endif
2290                                 return 0;
2291                         } else {
2292                                 return 1;
2293                         }
2294                 }
2295         }
2296
2297         return 0;
2298 }
2299
2300 /* If return 0, it means this function was able to handle it (pre setup digits).  If non zero, the user of this
2301  * functions should handle it normally (generate inband DTMF) */
2302 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit)
2303 {
2304         if ((ast->_state == AST_STATE_DIALING) && !pvt->proceeding) {
2305                 if (pvt->setup_ack) {
2306                         if (!pri_grab(pvt, pvt->pri)) {
2307                                 pri_information(pvt->pri->pri, pvt->call, digit);
2308                                 pri_rel(pvt->pri);
2309                         } else {
2310                                 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->pri->span);
2311                         }
2312                 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
2313                         int res;
2314                         ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
2315                         res = strlen(pvt->dialdest);
2316                         pvt->dialdest[res++] = digit;
2317                         pvt->dialdest[res] = '\0';
2318                 }
2319                 return 0;
2320         }
2321         return 1;
2322 }
2323
2324 int sig_pri_start_pri(struct sig_pri_pri *pri)
2325 {
2326         int x;
2327         int i;
2328
2329         ast_mutex_init(&pri->lock);
2330
2331         for (i = 0; i < NUM_DCHANS; i++) {
2332                 if (pri->fds[i] == -1) {
2333                         break;
2334                 }
2335
2336                 switch (pri->sig) {
2337                 case SIG_BRI:
2338                         pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
2339                         break;
2340                 case SIG_BRI_PTMP:
2341                         pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
2342                         break;
2343                 default:
2344                         pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
2345 #ifdef HAVE_PRI_SERVICE_MESSAGES
2346                         if (pri->enable_service_message_support) {
2347                                 pri_set_service_message_support(pri->dchans[i], 1);
2348                         }
2349 #endif
2350                         break;
2351                 }
2352
2353                 /* Force overlap dial if we're doing GR-303! */
2354                 pri_set_overlapdial(pri->dchans[i], pri->overlapdial);
2355                 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
2356                 /* Enslave to master if appropriate */
2357                 if (i)
2358                         pri_enslave(pri->dchans[0], pri->dchans[i]);
2359                 if (!pri->dchans[i]) {
2360                         if (pri->fds[i] > 0)
2361                                 close(pri->fds[i]);
2362                         pri->fds[i] = -1;
2363                         ast_log(LOG_ERROR, "Unable to create PRI structure\n");
2364                         return -1;
2365                 }
2366                 pri_set_debug(pri->dchans[i], 0);
2367                 pri_set_nsf(pri->dchans[i], pri->nsf);
2368 #ifdef PRI_GETSET_TIMERS
2369                 for (x = 0; x < PRI_MAX_TIMERS; x++) {
2370                         if (pri->pritimers[x] != 0)
2371                                 pri_set_timer(pri->dchans[i], x, pri->pritimers[x]);
2372                 }
2373 #endif
2374         }
2375         /* Assume primary is the one we use */
2376         pri->pri = pri->dchans[0];
2377         pri->resetpos = -1;
2378         if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
2379                 for (i = 0; i < NUM_DCHANS; i++) {
2380                         if (!pri->dchans[i])
2381                                 break;
2382                         if (pri->fds[i] > 0)
2383                                 close(pri->fds[i]);
2384                         pri->fds[i] = -1;
2385                 }
2386                 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
2387                 return -1;
2388         }
2389         return 0;
2390 }
2391
2392 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm)
2393 {
2394         if (!noalarm) {
2395                 p->inalarm = 1;
2396                 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
2397                         /* T309 is not enabled : hangup calls when alarm occurs */
2398                         if (p->call) {
2399                                 if (p->pri && p->pri->pri) {
2400                                         if (!pri_grab(p, p->pri)) {
2401                                                 pri_hangup(p->pri->pri, p->call, -1);
2402                                                 pri_destroycall(p->pri->pri, p->call);
2403                                                 p->call = NULL;
2404                                                 pri_rel(p->pri);
2405                                         } else
2406                                                 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2407                                 } else
2408                                         ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2409                         } else
2410                                 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
2411                 }
2412                 if (p->owner)
2413                         ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
2414         } else {
2415                 p->inalarm = 0;
2416         }
2417 }
2418
2419 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)
2420 {
2421         struct sig_pri_chan *p;
2422
2423         p = ast_calloc(1, sizeof(*p));
2424
2425         if (!p)
2426                 return p;
2427
2428         p->logicalspan = logicalspan;
2429         p->prioffset = channo;
2430         p->mastertrunkgroup = trunkgroup;
2431
2432         p->calls = callback;
2433         p->chan_pvt = pvt_data;
2434
2435         pri->pvts[pri->numchans++] = p;
2436         p->pri = pri;
2437
2438         return p;
2439 }
2440
2441 static void build_status(char *s, size_t len, int status, int active)
2442 {
2443         if (!s || len < 1) {
2444                 return;
2445         }
2446         s[0] = '\0';
2447         if (!(status & DCHAN_NOTINALARM))
2448                 strncat(s, "In Alarm, ", len - strlen(s) - 1);
2449         if (status & DCHAN_UP)
2450                 strncat(s, "Up", len - strlen(s) - 1);
2451         else
2452                 strncat(s, "Down", len - strlen(s) - 1);
2453         if (active)
2454                 strncat(s, ", Active", len - strlen(s) - 1);
2455         else
2456                 strncat(s, ", Standby", len - strlen(s) - 1);
2457         s[len - 1] = '\0';
2458 }
2459
2460 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_pri *pri)
2461 {
2462         char status[256];
2463         int x;
2464         for (x = 0; x < NUM_DCHANS; x++) {
2465                 if (pri->dchans[x]) {
2466                         build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
2467                         ast_cli(fd, "PRI span %d/%d: %s\n", span, x, status);
2468                 }
2469         }
2470 }
2471
2472 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_pri *pri)
2473 {
2474         int x;
2475         char status[256];
2476
2477         for (x = 0; x < NUM_DCHANS; x++) {
2478                 if (pri->dchans[x]) {
2479 #ifdef PRI_DUMP_INFO_STR
2480                         char *info_str = NULL;
2481 #endif
2482                         ast_cli(fd, "%s D-channel: %d\n", pri_order(x), dchannels[x]);
2483                         build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
2484                         ast_cli(fd, "Status: %s\n", status);
2485 #ifdef PRI_DUMP_INFO_STR
2486                         info_str = pri_dump_info_str(pri->pri);
2487                         if (info_str) {
2488                                 ast_cli(fd, "%s", info_str);
2489                                 free(info_str);
2490                         }
2491 #else
2492                         pri_dump_info(pri->pri);
2493 #endif
2494                         ast_cli(fd, "Overlap Recv: %s\n\n", (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
2495                         ast_cli(fd, "\n");
2496                 }
2497         }
2498 }
2499
2500 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits)
2501 {
2502         sig_pri_lock_private(p);
2503
2504         if (!p->pri || !p->call) {
2505                 ast_debug(1, "Unable to find pri or call on channel!\n");
2506                 sig_pri_unlock_private(p);
2507                 return -1;
2508         }
2509
2510         if (!pri_grab(p, p->pri)) {
2511                 pri_keypad_facility(p->pri->pri, p->call, digits);
2512                 pri_rel(p->pri);
2513         } else {
2514                 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2515                 sig_pri_unlock_private(p);
2516                 return -1;
2517         }
2518
2519         sig_pri_unlock_private(p);
2520
2521         return 0;
2522 }
2523
2524 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)
2525 {
2526         int res = -1;
2527
2528         sig_pri_lock_private(p);
2529
2530         if (!p->pri || !p->call) {
2531                 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2532                 sig_pri_unlock_private(p);
2533                 return -1;
2534         }
2535
2536         switch (p->pri->sig) {
2537         case SIG_PRI:
2538                 if (!pri_grab(p, p->pri)) {
2539                         if (chanstate == AST_STATE_RING) {
2540                                 res = pri_callrerouting_facility(p->pri->pri, p->call, destination, original, reason);
2541                         }
2542                         pri_rel(p->pri);
2543                 } else {
2544                         ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->pri->span);
2545                         sig_pri_unlock_private(p);
2546                         return -1;
2547                 }
2548                 break;
2549         }
2550
2551         sig_pri_unlock_private(p);
2552
2553         return res;
2554 }
2555
2556 #ifdef HAVE_PRI_SERVICE_MESSAGES
2557 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
2558 {
2559         int channel = PVT_TO_CHANNEL(p);
2560         int span = PRI_SPAN(channel);
2561
2562         return pri_maintenance_service(pri, span, channel, changestatus);
2563 }
2564 #endif
2565
2566 #endif /* HAVE_PRI */