2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief DAHDI for Pseudo TDM
23 * \author Mark Spencer <markster@digium.com>
25 * Connects to the DAHDI telephony library as well as
26 * libpri. Libpri is optional and needed only if you are
27 * going to use ISDN connections.
29 * You need to install libraries before you attempt to compile
30 * and install the DAHDI channel.
33 * \arg \ref Config_dahdi
35 * \ingroup channel_drivers
37 * \todo Deprecate the "musiconhold" configuration option post 1.4
41 <depend>res_smdi</depend>
42 <depend>dahdi</depend>
43 <depend>tonezone</depend>
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
57 #include <sys/signal.h>
59 #include <sys/ioctl.h>
63 #include <dahdi/user.h>
64 #include <dahdi/tonezone.h>
78 #include "asterisk/lock.h"
79 #include "asterisk/channel.h"
80 #include "asterisk/config.h"
81 #include "asterisk/module.h"
82 #include "asterisk/pbx.h"
83 #include "asterisk/file.h"
84 #include "asterisk/ulaw.h"
85 #include "asterisk/alaw.h"
86 #include "asterisk/callerid.h"
87 #include "asterisk/adsi.h"
88 #include "asterisk/cli.h"
89 #include "asterisk/cdr.h"
90 #include "asterisk/features.h"
91 #include "asterisk/musiconhold.h"
92 #include "asterisk/say.h"
93 #include "asterisk/tdd.h"
94 #include "asterisk/app.h"
95 #include "asterisk/dsp.h"
96 #include "asterisk/astdb.h"
97 #include "asterisk/manager.h"
98 #include "asterisk/causes.h"
99 #include "asterisk/term.h"
100 #include "asterisk/utils.h"
101 #include "asterisk/transcap.h"
102 #include "asterisk/stringfields.h"
103 #include "asterisk/abstract_jb.h"
104 #include "asterisk/smdi.h"
105 #include "asterisk/astobj.h"
106 #include "asterisk/event.h"
107 #include "asterisk/devicestate.h"
108 #include "asterisk/paths.h"
111 <application name="DAHDISendKeypadFacility" language="en_US">
113 Send digits out of band over a PRI.
116 <parameter name="digits" required="true" />
119 <para>This application will send the given string of digits in a Keypad
120 Facility IE over the current channel.</para>
123 <application name="DAHDISendCallreroutingFacility" language="en_US">
125 Send QSIG call rerouting facility over a PRI.
128 <parameter name="destination" required="true">
129 <para>Destination number.</para>
131 <parameter name="original">
132 <para>Original called number.</para>
134 <parameter name="reason">
135 <para>Diversion reason, if not specified defaults to <literal>unknown</literal></para>
139 <para>This application will send a Callrerouting Facility IE over the
140 current channel.</para>
143 <application name="DAHDIAcceptR2Call" language="en_US">
145 Accept an R2 call if its not already accepted (you still need to answer it)
148 <parameter name="charge" required="true">
149 <para>Yes or No.</para>
150 <para>Whether you want to accept the call with charge or without charge.</para>
154 <para>This application will Accept the R2 call either with charge or no charge.</para>
157 <manager name="DAHDITransfer" language="en_US">
159 Transfer DAHDI Channel.
162 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
163 <parameter name="DAHDIChannel" required="true">
164 <para>DAHDI channel name to transfer.</para>
168 <para>Transfer a DAHDI channel.</para>
171 <manager name="DAHDIHangup" language="en_US">
173 Hangup DAHDI Channel.
176 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
177 <parameter name="DAHDIChannel" required="true">
178 <para>DAHDI channel name to hangup.</para>
182 <para>Hangup a DAHDI channel.</para>
185 <manager name="DAHDIDialOffhook" language="en_US">
187 Dial over DAHDI channel while offhook.
190 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
191 <parameter name="DAHDIChannel" required="true" />
192 <parameter name="Number" required="true" />
197 <manager name="DAHDIDNDon" language="en_US">
199 Toggle DAHDI channel Do Not Disturb status ON.
202 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
203 <parameter name="DAHDIChannel" required="true" />
208 <manager name="DAHDIDNDoff" language="en_US">
210 Toggle DAHDI channel Do Not Disturb status OFF.
213 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
214 <parameter name="DAHDIChannel" required="true" />
219 <manager name="DAHDIShowChannels" language="en_US">
221 Show status DAHDI channels.
224 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
225 <parameter name="DAHDIChannel" required="true" />
230 <manager name="DAHDIRestart" language="en_US">
232 Fully Restart DAHDI channels (terminates calls).
235 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
242 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
244 static const char * const lbostr[] = {
245 "0 db (CSU)/0-133 feet (DSX-1)",
246 "133-266 feet (DSX-1)",
247 "266-399 feet (DSX-1)",
248 "399-533 feet (DSX-1)",
249 "533-655 feet (DSX-1)",
255 /*! Global jitterbuffer configuration - by default, jb is disabled */
256 static struct ast_jb_conf default_jbconf =
260 .resync_threshold = -1,
263 static struct ast_jb_conf global_jbconf;
265 /* define this to send PRI user-user information elements */
266 #undef SUPPORT_USERUSER
269 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
270 * the user hangs up to reset the state machine so ring works properly.
271 * This is used to be able to support kewlstart by putting the zhone in
272 * groundstart mode since their forward disconnect supervision is entirely
273 * broken even though their documentation says it isn't and their support
274 * is entirely unwilling to provide any assistance with their channel banks
275 * even though their web site says they support their products for life.
277 /* #define ZHONE_HACK */
280 * Define if you want to check the hook state for an FXO (FXS signalled) interface
281 * before dialing on it. Certain FXO interfaces always think they're out of
282 * service with this method however.
284 /* #define DAHDI_CHECK_HOOKSTATE */
286 /*! \brief Typically, how many rings before we should send Caller*ID */
287 #define DEFAULT_CIDRINGS 1
289 #define CHANNEL_PSEUDO -12
291 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
294 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
295 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FGC_CAMA) || ((p)->sig == SIG_FGC_CAMAMF) || ((p)->sig == SIG_FEATB))
297 static const char tdesc[] = "DAHDI Telephony Driver"
298 #if defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2)
312 #if defined(HAVE_PRI) || defined(HAVE_SS7)
320 static const char config[] = "chan_dahdi.conf";
322 #define SIG_EM DAHDI_SIG_EM
323 #define SIG_EMWINK (0x0100000 | DAHDI_SIG_EM)
324 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
325 #define SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
326 #define SIG_FEATB (0x0800000 | DAHDI_SIG_EM)
327 #define SIG_E911 (0x1000000 | DAHDI_SIG_EM)
328 #define SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
329 #define SIG_FGC_CAMA (0x4000000 | DAHDI_SIG_EM)
330 #define SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
331 #define SIG_FXSLS DAHDI_SIG_FXSLS
332 #define SIG_FXSGS DAHDI_SIG_FXSGS
333 #define SIG_FXSKS DAHDI_SIG_FXSKS
334 #define SIG_FXOLS DAHDI_SIG_FXOLS
335 #define SIG_FXOGS DAHDI_SIG_FXOGS
336 #define SIG_FXOKS DAHDI_SIG_FXOKS
337 #define SIG_PRI DAHDI_SIG_CLEAR
338 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
339 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
340 #define SIG_SS7 (0x1000000 | DAHDI_SIG_CLEAR)
341 #define SIG_MFCR2 DAHDI_SIG_CAS
342 #define SIG_SF DAHDI_SIG_SF
343 #define SIG_SFWINK (0x0100000 | DAHDI_SIG_SF)
344 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
345 #define SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
346 #define SIG_SF_FEATB (0x0800000 | DAHDI_SIG_SF)
347 #define SIG_EM_E1 DAHDI_SIG_EM_E1
348 #define SIG_GR303FXOKS (0x0100000 | DAHDI_SIG_FXOKS)
349 #define SIG_GR303FXSKS (0x0100000 | DAHDI_SIG_FXSKS)
352 #define NUM_SPANS DAHDI_MAX_SPANS
356 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
357 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
359 #define CHAN_PSEUDO -2
361 #define DCHAN_PROVISIONED (1 << 0)
362 #define DCHAN_NOTINALARM (1 << 1)
363 #define DCHAN_UP (1 << 2)
365 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
367 /* Overlap dialing option types */
368 #define DAHDI_OVERLAPDIAL_NONE 0
369 #define DAHDI_OVERLAPDIAL_OUTGOING 1
370 #define DAHDI_OVERLAPDIAL_INCOMING 2
371 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
373 #define CALLPROGRESS_PROGRESS 1
374 #define CALLPROGRESS_FAX_OUTGOING 2
375 #define CALLPROGRESS_FAX_INCOMING 4
376 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
378 #ifdef HAVE_PRI_SERVICE_MESSAGES
379 /*! \brief Persistent Service State */
380 #define SRVST_DBKEY "service-state"
381 /*! \brief The out-of-service SERVICE state */
382 #define SRVST_TYPE_OOS "O"
383 /*! \brief SRVST_INITIALIZED is used to indicate a channel being out-of-service
384 * The SRVST_INITIALIZED is mostly used maintain backwards compatibility but also may
385 * mean that the channel has not yet received a RESTART message. If a channel is
386 * out-of-service with this reason a RESTART message will result in the channel
387 * being put into service. */
388 #define SRVST_INITIALIZED 0
389 /*! \brief SRVST_NEAREND is used to indicate that the near end was put out-of-service */
390 #define SRVST_NEAREND (1 << 0)
391 /*! \brief SRVST_FAREND is used to indicate that the far end was taken out-of-service */
392 #define SRVST_FAREND (1 << 1)
393 /*! \brief SRVST_BOTH is used to indicate that both sides of the channel are out-of-service */
394 #define SRVST_BOTH (SRVST_NEAREND | SRVST_FAREND)
396 /*! \brief The AstDB family */
397 static const char dahdi_db[] = "dahdi/registry";
400 static char defaultcic[64] = "";
401 static char defaultozz[64] = "";
403 static char parkinglot[AST_MAX_EXTENSION] = ""; /*!< Default parking lot for this channel */
405 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
406 static char mwimonitornotify[PATH_MAX] = "";
407 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
408 static int mwisend_rpas = 0;
411 static char progzone[10] = "";
413 static int usedistinctiveringdetection = 0;
414 static int distinctiveringaftercid = 0;
416 static int numbufs = 4;
418 static int mwilevel = 512;
421 static struct ast_channel inuse;
422 #ifdef PRI_GETSET_TIMERS
423 static int pritimers[PRI_MAX_TIMERS];
425 static int pridebugfd = -1;
426 static char pridebugfilename[1024] = "";
429 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
430 static int firstdigittimeout = 16000;
432 /*! \brief How long to wait for following digits (FXO logic) */
433 static int gendigittimeout = 8000;
435 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
436 static int matchdigittimeout = 3000;
438 /*! \brief Protect the interface list (of dahdi_pvt's) */
439 AST_MUTEX_DEFINE_STATIC(iflock);
441 /* QSIG channel mapping option types */
442 #define DAHDI_CHAN_MAPPING_PHYSICAL 0
443 #define DAHDI_CHAN_MAPPING_LOGICAL 1
446 static int ifcount = 0;
449 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
452 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
453 when it's doing something critical. */
454 AST_MUTEX_DEFINE_STATIC(monlock);
456 /*! \brief This is the thread for the monitor which checks for input on the channels
457 which are not currently in use. */
458 static pthread_t monitor_thread = AST_PTHREADT_NULL;
459 static ast_cond_t ss_thread_complete;
460 AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
461 AST_MUTEX_DEFINE_STATIC(restart_lock);
462 static int ss_thread_count = 0;
463 static int num_restart_pending = 0;
465 static int restart_monitor(void);
467 static enum ast_bridge_result dahdi_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
469 static int dahdi_sendtext(struct ast_channel *c, const char *text);
471 static void mwi_event_cb(const struct ast_event *event, void *userdata)
473 /* This module does not handle MWI in an event-based manner. However, it
474 * subscribes to MWI for each mailbox that is configured so that the core
475 * knows that we care about it. Then, chan_dahdi will get the MWI from the
476 * event cache instead of checking the mailbox directly. */
479 /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
480 static inline int dahdi_get_event(int fd)
483 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
488 /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
489 static inline int dahdi_wait_event(int fd)
492 i = DAHDI_IOMUX_SIGEVENT;
493 if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
495 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
500 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
501 #define READ_SIZE 160
503 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
504 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
506 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
507 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
508 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
509 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
510 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
515 * \brief Configured ring timeout base.
516 * \note Value computed from "ringtimeout" read in from chan_dahdi.conf if it exists.
518 static int ringt_base = DEFAULT_RINGT;
522 #define LINKSTATE_INALARM (1 << 0)
523 #define LINKSTATE_STARTING (1 << 1)
524 #define LINKSTATE_UP (1 << 2)
525 #define LINKSTATE_DOWN (1 << 3)
527 #define SS7_NAI_DYNAMIC -1
529 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
532 pthread_t master; /*!< Thread of master */
536 int linkstate[NUM_DCHANS];
540 LINKSET_STATE_DOWN = 0,
543 char called_nai; /*!< Called Nature of Address Indicator */
544 char calling_nai; /*!< Calling Nature of Address Indicator */
545 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
546 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
547 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
548 char unknownprefix[20]; /*!< for unknown dialplans */
550 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
551 int flags; /*!< Linkset flags */
554 static struct dahdi_ss7 linksets[NUM_SPANS];
556 static int cur_ss7type = -1;
557 static int cur_linkset = -1;
558 static int cur_pointcode = -1;
559 static int cur_cicbeginswith = -1;
560 static int cur_adjpointcode = -1;
561 static int cur_networkindicator = -1;
562 static int cur_defaultdpc = -1;
563 #endif /* HAVE_SS7 */
567 pthread_t r2master; /*!< Thread of master */
568 openr2_context_t *protocol_context; /*!< OpenR2 context handle */
569 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
570 int numchans; /*!< Number of channels in this R2 block */
571 int monitored_count; /*!< Number of channels being monitored */
572 ast_mutex_t monitored_count_lock; /*!< lock access to the counter */
573 ast_cond_t do_monitor; /*!< Condition to wake up the monitor thread when there's work to do */
576 struct dahdi_mfcr2_conf {
577 openr2_variant_t variant;
579 int metering_pulse_timeout;
584 int allow_collect_calls:1;
586 int accept_on_offer:1;
587 int forced_release:1;
589 int immediate_accept:1;
590 char logdir[OR2_MAX_PATH];
591 char r2proto_file[OR2_MAX_PATH];
592 openr2_log_level_t loglevel;
593 openr2_calling_party_category_t category;
596 /* malloc'd array of malloc'd r2links */
597 static struct dahdi_mfcr2 **r2links;
598 /* how many r2links have been malloc'd */
599 static int r2links_count = 0;
601 #endif /* HAVE_OPENR2 */
605 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
606 #define PRI_CHANNEL(p) ((p) & 0xff)
607 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
608 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
611 pthread_t master; /*!< Thread of master */
612 ast_mutex_t lock; /*!< Mutex */
613 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
614 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
615 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
616 int minunused; /*!< Min # of channels to keep empty */
617 int minidle; /*!< Min # of "idling" calls to keep active */
618 int nodetype; /*!< Node type */
619 int switchtype; /*!< Type of switch to emulate */
620 int nsf; /*!< Network-Specific Facilities */
621 int dialplan; /*!< Dialing plan */
622 int localdialplan; /*!< Local dialing plan */
623 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
624 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
625 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
626 char privateprefix[20]; /*!< for private dialplans */
627 char unknownprefix[20]; /*!< for unknown dialplans */
628 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
629 int trunkgroup; /*!< What our trunkgroup is */
630 int mastertrunkgroup; /*!< What trunk group is our master */
631 int prilogicalspan; /*!< Logical span number within trunk group */
632 int numchans; /*!< Num of channels we represent */
633 int overlapdial; /*!< In overlap dialing mode */
634 int qsigchannelmapping; /*!< QSIG channel mapping type */
635 int discardremoteholdretrieval; /*!< shall remote hold or remote retrieval notifications be discarded? */
636 int facilityenable; /*!< Enable facility IEs */
637 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
638 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
639 struct pri *pri; /*!< Currently active D-channel */
640 /*! \brief TRUE if to dump PRI event info (Tested but never set) */
642 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
643 /*! \brief Value set but not used */
645 /*! \brief Span number put into user output messages */
647 /*! \brief TRUE if span is being reset/restarted */
649 /*! \brief Current position during a reset (-1 if not started) */
651 #ifdef HAVE_PRI_SERVICE_MESSAGES
652 unsigned int enable_service_message_support:1; /*!< enable SERVICE message support */
654 #ifdef HAVE_PRI_INBANDDISCONNECT
655 unsigned int inbanddisconnect:1; /*!< Should we support inband audio after receiving DISCONNECT? */
657 time_t lastreset; /*!< time when unused channels were last reset */
658 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
659 /*! \brief ISDN signalling type (SIG_PRI, SIG_BRI, SIG_BRI_PTMP, etc...) */
661 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
662 struct dahdi_pvt *crvs; /*!< Member CRV structs */
663 struct dahdi_pvt *crvend; /*!< Pointer to end of CRV structs */
667 static struct dahdi_pri pris[NUM_SPANS];
670 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
672 #define DEFAULT_PRI_DEBUG 0
675 static inline void pri_rel(struct dahdi_pri *pri)
677 ast_mutex_unlock(&pri->lock);
681 /*! Shut up the compiler */
685 #define SUB_REAL 0 /*!< Active call */
686 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
687 #define SUB_THREEWAY 2 /*!< Three-way call */
689 /* Polarity states */
690 #define POLARITY_IDLE 0
691 #define POLARITY_REV 1
694 struct distRingData {
698 struct ringContextData {
699 char contextData[AST_MAX_CONTEXT];
701 struct dahdi_distRings {
702 struct distRingData ringnum[3];
703 struct ringContextData ringContext[3];
706 static const char * const subnames[] = {
712 struct dahdi_subchannel {
714 struct ast_channel *owner;
716 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
717 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
718 unsigned int needringing:1;
719 unsigned int needbusy:1;
720 unsigned int needcongestion:1;
721 unsigned int needcallerid:1;
722 unsigned int needanswer:1;
723 unsigned int needflash:1;
724 unsigned int needhold:1;
725 unsigned int needunhold:1;
726 unsigned int linear:1;
727 unsigned int inthreeway:1;
728 struct dahdi_confinfo curconf;
731 #define CONF_USER_REAL (1 << 0)
732 #define CONF_USER_THIRDCALL (1 << 1)
736 /* States for sending MWI message
737 * First three states are required for send Ring Pulse Alert Signal
749 struct mwisend_info {
750 struct timeval pause;
751 mwisend_states mwisend_current;
754 static struct dahdi_pvt {
756 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
757 /*!< Up to three channels can be associated with this call */
759 struct dahdi_subchannel sub_unused; /*!< Just a safety precaution */
760 struct dahdi_subchannel subs[3]; /*!< Sub-channels */
761 struct dahdi_confinfo saveconf; /*!< Saved conference info */
763 struct dahdi_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
764 struct dahdi_pvt *master; /*!< Master to us (we follow their conferencing) */
765 int inconference; /*!< If our real should be in the conference */
767 int bufsize; /*!< Size of the buffers */
768 int buf_no; /*!< Number of buffers */
769 int buf_policy; /*!< Buffer policy */
770 int faxbuf_no; /*!< Number of Fax buffers */
771 int faxbuf_policy; /*!< Fax buffer policy */
772 int sig; /*!< Signalling style */
774 * \brief Nonzero if the signaling type is sent over a radio.
775 * \note Set to a couple of nonzero values but it is only tested like a boolean.
778 int outsigmod; /*!< Outbound Signalling style (modifier) */
779 int oprmode; /*!< "Operator Services" mode */
780 struct dahdi_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
781 /*! \brief Amount of gain to increase during caller id */
783 /*! \brief Rx gain set by chan_dahdi.conf */
785 /*! \brief Tx gain set by chan_dahdi.conf */
787 int tonezone; /*!< tone zone for this chan, or -1 for default */
788 struct dahdi_pvt *next; /*!< Next channel in list */
789 struct dahdi_pvt *prev; /*!< Prev channel in list */
794 * \brief TRUE if ADSI (Analog Display Services Interface) available
795 * \note Set from the "adsi" value read in from chan_dahdi.conf
799 * \brief TRUE if we can use a polarity reversal to mark when an outgoing
800 * call is answered by the remote party.
801 * \note Set from the "answeronpolarityswitch" value read in from chan_dahdi.conf
803 unsigned int answeronpolarityswitch:1;
805 * \brief TRUE if busy detection is enabled.
806 * (Listens for the beep-beep busy pattern.)
807 * \note Set from the "busydetect" value read in from chan_dahdi.conf
809 unsigned int busydetect:1;
811 * \brief TRUE if call return is enabled.
812 * (*69, if your dialplan doesn't catch this first)
813 * \note Set from the "callreturn" value read in from chan_dahdi.conf
815 unsigned int callreturn:1;
817 * \brief TRUE if busy extensions will hear the call-waiting tone
818 * and can use hook-flash to switch between callers.
819 * \note Can be disabled by dialing *70.
820 * \note Initialized with the "callwaiting" value read in from chan_dahdi.conf
822 unsigned int callwaiting:1;
824 * \brief TRUE if send caller ID for Call Waiting
825 * \note Set from the "callwaitingcallerid" value read in from chan_dahdi.conf
827 unsigned int callwaitingcallerid:1;
829 * \brief TRUE if support for call forwarding enabled.
830 * Dial *72 to enable call forwarding.
831 * Dial *73 to disable call forwarding.
832 * \note Set from the "cancallforward" value read in from chan_dahdi.conf
834 unsigned int cancallforward:1;
836 * \brief TRUE if support for call parking is enabled.
837 * \note Set from the "canpark" value read in from chan_dahdi.conf
839 unsigned int canpark:1;
840 /*! \brief TRUE if to wait for a DTMF digit to confirm answer */
841 unsigned int confirmanswer:1;
843 * \brief TRUE if the channel is to be destroyed on hangup.
844 * (Used by pseudo channels.)
846 unsigned int destroy:1;
847 unsigned int didtdd:1; /*!< flag to say its done it once */
848 /*! \brief TRUE if analog type line dialed no digits in Dial() */
849 unsigned int dialednone:1;
850 /*! \brief TRUE if in the process of dialing digits or sending something. */
851 unsigned int dialing:1;
852 /*! \brief TRUE if the transfer capability of the call is digital. */
853 unsigned int digital:1;
854 /*! \brief TRUE if Do-Not-Disturb is enabled. */
856 /*! \brief XXX BOOLEAN Purpose??? */
857 unsigned int echobreak:1;
859 * \brief TRUE if echo cancellation enabled when bridged.
860 * \note Initialized with the "echocancelwhenbridged" value read in from chan_dahdi.conf
861 * \note Disabled if the echo canceller is not setup.
863 unsigned int echocanbridged:1;
864 /*! \brief TRUE if echo cancellation is turned on. */
865 unsigned int echocanon:1;
866 /*! \brief TRUE if a fax tone has already been handled. */
867 unsigned int faxhandled:1;
868 /*! TRUE if dynamic faxbuffers are configured for use, default is OFF */
869 unsigned int usefaxbuffers:1;
870 /*! TRUE while buffer configuration override is in use */
871 unsigned int bufferoverrideinuse:1;
872 /*! \brief TRUE if over a radio and dahdi_read() has been called. */
873 unsigned int firstradio:1;
875 * \brief TRUE if the call will be considered "hung up" on a polarity reversal.
876 * \note Set from the "hanguponpolarityswitch" value read in from chan_dahdi.conf
878 unsigned int hanguponpolarityswitch:1;
879 /*! \brief TRUE if DTMF detection needs to be done by hardware. */
880 unsigned int hardwaredtmf:1;
882 * \brief TRUE if the outgoing caller ID is blocked/hidden.
883 * \note Caller ID can be disabled by dialing *67.
884 * \note Caller ID can be enabled by dialing *82.
885 * \note Initialized with the "hidecallerid" value read in from chan_dahdi.conf
887 unsigned int hidecallerid:1;
889 * \brief TRUE if hide just the name not the number for legacy PBX use.
890 * \note Only applies to PRI channels.
891 * \note Set from the "hidecalleridname" value read in from chan_dahdi.conf
893 unsigned int hidecalleridname:1;
894 /*! \brief TRUE if DTMF detection is disabled. */
895 unsigned int ignoredtmf:1;
897 * \brief TRUE if the channel should be answered immediately
898 * without attempting to gather any digits.
899 * \note Set from the "immediate" value read in from chan_dahdi.conf
901 unsigned int immediate:1;
902 /*! \brief TRUE if in an alarm condition. */
903 unsigned int inalarm:1;
904 /*! \brief TRUE if TDD in MATE mode */
906 /*! \brief TRUE if we originated the call leg. */
907 unsigned int outgoing:1;
908 /* unsigned int overlapdial:1; unused and potentially confusing */
910 * \brief TRUE if busy extensions will hear the call-waiting tone
911 * and can use hook-flash to switch between callers.
912 * \note Set from the "callwaiting" value read in from chan_dahdi.conf
914 unsigned int permcallwaiting:1;
916 * \brief TRUE if the outgoing caller ID is blocked/restricted/hidden.
917 * \note Set from the "hidecallerid" value read in from chan_dahdi.conf
919 unsigned int permhidecallerid:1;
921 * \brief TRUE if PRI congestion/busy indications are sent out-of-band.
922 * \note Set from the "priindication" value read in from chan_dahdi.conf
924 unsigned int priindication_oob:1;
926 * \brief TRUE if PRI B channels are always exclusively selected.
927 * \note Set from the "priexclusive" value read in from chan_dahdi.conf
929 unsigned int priexclusive:1;
931 * \brief TRUE if we will pulse dial.
932 * \note Set from the "pulsedial" value read in from chan_dahdi.conf
934 unsigned int pulse:1;
935 /*! \brief TRUE if a pulsed digit was detected. (Pulse dial phone detected) */
936 unsigned int pulsedial:1;
937 unsigned int restartpending:1; /*!< flag to ensure counted only once for restart */
939 * \brief TRUE if caller ID is restricted.
940 * \note Set but not used. Should be deleted. Redundant with permhidecallerid.
941 * \note Set from the "restrictcid" value read in from chan_dahdi.conf
943 unsigned int restrictcid:1;
945 * \brief TRUE if three way calling is enabled
946 * \note Set from the "threewaycalling" value read in from chan_dahdi.conf
948 unsigned int threewaycalling:1;
950 * \brief TRUE if call transfer is enabled
951 * \note For FXS ports (either direct analog or over T1/E1):
952 * Support flash-hook call transfer
953 * \note For digital ports using ISDN PRI protocols:
954 * Support switch-side transfer (called 2BCT, RLT or other names)
955 * \note Set from the "transfer" value read in from chan_dahdi.conf
957 unsigned int transfer:1;
959 * \brief TRUE if caller ID is used on this channel.
960 * \note PRI and SS7 spans will save caller ID from the networking peer.
961 * \note FXS ports will generate the caller ID spill.
962 * \note FXO ports will listen for the caller ID spill.
963 * \note Set from the "usecallerid" value read in from chan_dahdi.conf
965 unsigned int use_callerid:1;
967 * \brief TRUE if we will use the calling presentation setting
968 * from the Asterisk channel for outgoing calls.
969 * \note Only applies to PRI and SS7 channels.
970 * \note Set from the "usecallingpres" value read in from chan_dahdi.conf
972 unsigned int use_callingpres:1;
974 * \brief TRUE if distinctive rings are to be detected.
975 * \note For FXO lines
976 * \note Set indirectly from the "usedistinctiveringdetection" value read in from chan_dahdi.conf
978 unsigned int usedistinctiveringdetection:1;
980 * \brief TRUE if we should use the callerid from incoming call on dahdi transfer.
981 * \note Set from the "useincomingcalleridondahditransfer" value read in from chan_dahdi.conf
983 unsigned int dahditrcallerid:1;
985 * \brief TRUE if allowed to flash-transfer to busy channels.
986 * \note Set from the "transfertobusy" value read in from chan_dahdi.conf
988 unsigned int transfertobusy:1;
990 * \brief TRUE if the FXO port monitors for neon type MWI indications from the other end.
991 * \note Set if the "mwimonitor" value read in contains "neon" from chan_dahdi.conf
993 unsigned int mwimonitor_neon:1;
995 * \brief TRUE if the FXO port monitors for fsk type MWI indications from the other end.
996 * \note Set if the "mwimonitor" value read in contains "fsk" from chan_dahdi.conf
998 unsigned int mwimonitor_fsk:1;
1000 * \brief TRUE if the FXO port monitors for rpas precursor to fsk MWI indications from the other end.
1001 * \note RPAS - Ring Pulse Alert Signal
1002 * \note Set if the "mwimonitor" value read in contains "rpas" from chan_dahdi.conf
1004 unsigned int mwimonitor_rpas:1;
1005 /*! \brief TRUE if an MWI monitor thread is currently active */
1006 unsigned int mwimonitoractive:1;
1007 /*! \brief TRUE if a MWI message sending thread is active */
1008 unsigned int mwisendactive:1;
1010 * \brief TRUE if channel is out of reset and ready
1011 * \note Set but not used.
1013 unsigned int inservice:1;
1015 * \brief TRUE if the channel is locally blocked.
1016 * \note Applies to SS7 channels.
1018 unsigned int locallyblocked:1;
1020 * \brief TRUE if the channel is remotely blocked.
1021 * \note Applies to SS7 channels.
1023 unsigned int remotelyblocked:1;
1024 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1026 * \brief XXX BOOLEAN Purpose???
1027 * \note Applies to SS7 channels.
1030 /*! \brief TRUE if channel is alerting/ringing */
1031 unsigned int alerting:1;
1032 /*! \brief TRUE if the call has already gone/hungup */
1033 unsigned int alreadyhungup:1;
1035 * \brief TRUE if this is an idle call
1036 * \note Applies to PRI channels.
1038 unsigned int isidlecall:1;
1040 * \brief TRUE if call is in a proceeding state.
1041 * The call has started working its way through the network.
1043 unsigned int proceeding:1;
1044 /*! \brief TRUE if the call has seen progress through the network. */
1045 unsigned int progress:1;
1047 * \brief TRUE if this channel is being reset/restarted
1048 * \note Applies to PRI channels.
1050 unsigned int resetting:1;
1052 * \brief TRUE if this channel has received a SETUP_ACKNOWLEDGE
1053 * \note Applies to PRI channels.
1055 unsigned int setup_ack:1;
1058 * \brief TRUE if SMDI (Simplified Message Desk Interface) is enabled
1059 * \note Set from the "usesmdi" value read in from chan_dahdi.conf
1061 unsigned int use_smdi:1;
1062 struct mwisend_info mwisend_data;
1063 /*! \brief The serial port to listen for SMDI data on */
1064 struct ast_smdi_interface *smdi_iface;
1066 /*! \brief Distinctive Ring data */
1067 struct dahdi_distRings drings;
1070 * \brief The configured context for incoming calls.
1071 * \note The "context" string read in from chan_dahdi.conf
1073 char context[AST_MAX_CONTEXT];
1075 * \brief Saved context string.
1077 char defcontext[AST_MAX_CONTEXT];
1078 /*! \brief Extension to use in the dialplan. */
1079 char exten[AST_MAX_EXTENSION];
1081 * \brief Language configured for calls.
1082 * \note The "language" string read in from chan_dahdi.conf
1084 char language[MAX_LANGUAGE];
1086 * \brief The configured music-on-hold class to use for calls.
1087 * \note The "musicclass" or "mohinterpret" or "musiconhold" string read in from chan_dahdi.conf
1089 char mohinterpret[MAX_MUSICCLASS];
1091 * \brief Sugggested music-on-hold class for peer channel to use for calls.
1092 * \note The "mohsuggest" string read in from chan_dahdi.conf
1094 char mohsuggest[MAX_MUSICCLASS];
1095 char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
1096 #if defined(PRI_ANI) || defined(HAVE_SS7)
1097 /*! \brief Automatic Number Identification number (Alternate PRI caller ID number) */
1098 char cid_ani[AST_MAX_EXTENSION];
1100 /*! \brief Automatic Number Identification code from PRI */
1102 /*! \brief Caller ID number from an incoming call. */
1103 char cid_num[AST_MAX_EXTENSION];
1104 /*! \brief Caller ID Q.931 TON/NPI field values. Set by PRI. Zero otherwise. */
1106 /*! \brief Caller ID name from an incoming call. */
1107 char cid_name[AST_MAX_EXTENSION];
1108 /*! \brief Last Caller ID number from an incoming call. */
1109 char lastcid_num[AST_MAX_EXTENSION];
1110 /*! \brief Last Caller ID name from an incoming call. */
1111 char lastcid_name[AST_MAX_EXTENSION];
1112 char *origcid_num; /*!< malloced original callerid */
1113 char *origcid_name; /*!< malloced original callerid */
1114 /*! \brief Call waiting number. */
1115 char callwait_num[AST_MAX_EXTENSION];
1116 /*! \brief Call waiting name. */
1117 char callwait_name[AST_MAX_EXTENSION];
1118 /*! \brief Redirecting Directory Number Information Service (RDNIS) number */
1119 char rdnis[AST_MAX_EXTENSION];
1120 /*! \brief Dialed Number Identifier */
1121 char dnid[AST_MAX_EXTENSION];
1123 * \brief Bitmapped groups this belongs to.
1124 * \note The "group" bitmapped group string read in from chan_dahdi.conf
1127 /*! \brief Active PCM encoding format: DAHDI_LAW_ALAW or DAHDI_LAW_MULAW */
1129 int confno; /*!< Our conference */
1130 int confusers; /*!< Who is using our conference */
1131 int propconfno; /*!< Propagated conference number */
1133 * \brief Bitmapped call groups this belongs to.
1134 * \note The "callgroup" bitmapped group string read in from chan_dahdi.conf
1136 ast_group_t callgroup;
1138 * \brief Bitmapped pickup groups this belongs to.
1139 * \note The "pickupgroup" bitmapped group string read in from chan_dahdi.conf
1141 ast_group_t pickupgroup;
1143 * \brief Channel variable list with associated values to set when a channel is created.
1144 * \note The "setvar" strings read in from chan_dahdi.conf
1146 struct ast_variable *vars;
1147 int channel; /*!< Channel Number or CRV */
1148 int span; /*!< Span number */
1149 time_t guardtime; /*!< Must wait this much time before using for new call */
1150 int cid_signalling; /*!< CID signalling type bell202 or v23 */
1151 int cid_start; /*!< CID start indicator, polarity or ring */
1152 int callingpres; /*!< The value of calling presentation that we're going to use when placing a PRI call */
1153 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
1154 int cidcwexpire; /*!< When to expire our muting for CID/CW */
1155 /*! \brief Analog caller ID waveform sample buffer */
1156 unsigned char *cidspill;
1157 /*! \brief Position in the cidspill buffer to send out next. */
1159 /*! \brief Length of the cidspill buffer containing samples. */
1161 /*! \brief Ring timeout timer?? */
1164 * \brief Ring timeout base.
1165 * \note Value computed indirectly from "ringtimeout" read in from chan_dahdi.conf
1169 * \brief Number of most significant digits/characters to strip from the dialed number.
1170 * \note Feature is deprecated. Use dialplan logic.
1171 * \note The characters are stripped before the PRI TON/NPI prefix
1172 * characters are processed.
1175 /*! \brief BOOLEAN. XXX Meaning what?? */
1177 /*! \brief Number of call waiting rings. */
1179 /*! \brief Echo cancel parameters. */
1181 struct dahdi_echocanparams head;
1182 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
1185 * \brief Echo training time. 0 = disabled
1186 * \note Set from the "echotraining" value read in from chan_dahdi.conf
1189 /*! \brief Filled with 'w'. XXX Purpose?? */
1192 * \brief Number of times to see "busy" tone before hanging up.
1193 * \note Set from the "busycount" value read in from chan_dahdi.conf
1197 * \brief Length of "busy" tone on time.
1198 * \note Set from the "busypattern" value read in from chan_dahdi.conf
1200 int busy_tonelength;
1202 * \brief Length of "busy" tone off time.
1203 * \note Set from the "busypattern" value read in from chan_dahdi.conf
1205 int busy_quietlength;
1207 * \brief Bitmapped call progress detection flags. CALLPROGRESS_xxx values.
1208 * \note Bits set from the "callprogress" and "faxdetect" values read in from chan_dahdi.conf
1212 * \brief Number of milliseconds to wait for dialtone.
1213 * \note Set from the "waitfordialtone" value read in from chan_dahdi.conf
1215 int waitfordialtone;
1216 struct timeval waitingfordt; /*!< Time we started waiting for dialtone */
1217 struct timeval flashtime; /*!< Last flash-hook time */
1218 /*! \brief Opaque DSP configuration structure. */
1219 struct ast_dsp *dsp;
1220 //int cref; /*!< Call reference number (Not used) */
1221 /*! \brief DAHDI dial operation command struct for ioctl() call. */
1222 struct dahdi_dialoperation dop;
1223 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
1224 /*! \brief Second part of SIG_FEATDMF_TA wink operation. */
1226 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
1227 int amaflags; /*!< AMA Flags */
1228 struct tdd_state *tdd; /*!< TDD flag */
1229 /*! \brief Accumulated call forwarding number. */
1230 char call_forward[AST_MAX_EXTENSION];
1232 * \brief Voice mailbox location.
1233 * \note Set from the "mailbox" string read in from chan_dahdi.conf
1235 char mailbox[AST_MAX_EXTENSION];
1236 /*! \brief Opaque event subscription parameters for message waiting indication support. */
1237 struct ast_event_sub *mwi_event_sub;
1238 /*! \brief Delayed dialing for E911. Overlap digits for ISDN. */
1240 /*! \brief Time the interface went on-hook. */
1242 /*! \brief TRUE if the FXS port is off-hook */
1243 int fxsoffhookstate;
1244 /*! \brief -1 = unknown, 0 = no messages, 1 = new messages available */
1246 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1247 struct dahdi_vmwi_info mwisend_setting; /*!< Which VMWI methods to use */
1248 unsigned int mwisend_fsk: 1; /*! Variable for enabling FSK MWI handling in chan_dahdi */
1249 unsigned int mwisend_rpas:1; /*! Variable for enabling Ring Pulse Alert before MWI FSK Spill */
1251 int distinctivering; /*!< Which distinctivering to use */
1252 int cidrings; /*!< Which ring to deliver CID on */
1253 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
1254 /*! \brief Holding place for event injected from outside normal operation. */
1257 * \brief Minimal time period (ms) between the answer polarity
1258 * switch and hangup polarity switch.
1260 int polarityonanswerdelay;
1261 /*! \brief Start delay time if polarityonanswerdelay is nonzero. */
1262 struct timeval polaritydelaytv;
1264 * \brief Send caller ID after this many rings.
1265 * \note Set from the "sendcalleridafter" value read in from chan_dahdi.conf
1267 int sendcalleridafter;
1269 /*! \brief DAHDI PRI control parameters */
1270 struct dahdi_pri *pri;
1271 /*! \brief XXX Purpose??? */
1272 struct dahdi_pvt *bearer;
1273 /*! \brief XXX Purpose??? */
1274 struct dahdi_pvt *realcall;
1275 /*! \brief Opaque libpri call control structure */
1277 /*! \brief Channel number in span. */
1279 /*! \brief Logical span number within trunk group */
1282 /*! \brief Current line interface polarity. POLARITY_IDLE, POLARITY_REV */
1284 /*! \brief DSP feature flags: DSP_FEATURE_xxx */
1287 /*! \brief SS7 control parameters */
1288 struct dahdi_ss7 *ss7;
1289 /*! \brief Opaque libss7 call control structure */
1290 struct isup_call *ss7call;
1291 char charge_number[50];
1292 char gen_add_number[50];
1293 char gen_dig_number[50];
1294 char orig_called_num[50];
1295 char redirecting_num[50];
1296 char generic_name[50];
1297 unsigned char gen_add_num_plan;
1298 unsigned char gen_add_nai;
1299 unsigned char gen_add_pres_ind;
1300 unsigned char gen_add_type;
1301 unsigned char gen_dig_type;
1302 unsigned char gen_dig_scheme;
1303 char jip_number[50];
1304 unsigned char lspi_type;
1305 unsigned char lspi_scheme;
1306 unsigned char lspi_context;
1307 char lspi_ident[50];
1308 unsigned int call_ref_ident;
1309 unsigned int call_ref_pc;
1310 unsigned char calling_party_cat;
1312 int cic; /*!< CIC associated with channel */
1313 unsigned int dpc; /*!< CIC's DPC */
1314 unsigned int loopedback:1;
1317 struct dahdi_mfcr2 *mfcr2;
1318 openr2_chan_t *r2chan;
1319 openr2_calling_party_category_t mfcr2_recvd_category;
1320 openr2_calling_party_category_t mfcr2_category;
1321 int mfcr2_dnis_index;
1322 int mfcr2_ani_index;
1324 int mfcr2_answer_pending:1;
1325 int mfcr2_charge_calls:1;
1326 int mfcr2_allow_collect_calls:1;
1327 int mfcr2_forced_release:1;
1328 int mfcr2_dnis_matched:1;
1329 int mfcr2_call_accepted:1;
1330 int mfcr2_accept_on_offer:1;
1332 /*! \brief DTMF digit in progress. 0 when no digit in progress. */
1334 /*! \brief TRUE if confrence is muted. */
1336 } *iflist = NULL, *ifend = NULL;
1338 /*! \brief Channel configuration from chan_dahdi.conf .
1339 * This struct is used for parsing the [channels] section of chan_dahdi.conf.
1340 * Generally there is a field here for every possible configuration item.
1342 * The state of fields is saved along the parsing and whenever a 'channel'
1343 * statement is reached, the current dahdi_chan_conf is used to configure the
1344 * channel (struct dahdi_pvt)
1346 * \see dahdi_chan_init for the default values.
1348 struct dahdi_chan_conf {
1349 struct dahdi_pvt chan;
1351 struct dahdi_pri pri;
1355 struct dahdi_ss7 ss7;
1359 struct dahdi_mfcr2_conf mfcr2;
1361 struct dahdi_params timing;
1362 int is_sig_auto; /*!< Use channel signalling from DAHDI? */
1365 * \brief The serial port to listen for SMDI data on
1366 * \note Set from the "smdiport" string read in from chan_dahdi.conf
1368 char smdi_port[SMDI_MAX_FILENAME_LEN];
1371 /*! returns a new dahdi_chan_conf with default values (by-value) */
1372 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
1374 /* recall that if a field is not included here it is initialized
1375 * to 0 or equivalent
1377 struct dahdi_chan_conf conf = {
1380 .nsf = PRI_NSF_NONE,
1381 .switchtype = PRI_SWITCH_NI2,
1382 .dialplan = PRI_UNKNOWN + 1,
1383 .localdialplan = PRI_NATIONAL_ISDN + 1,
1384 .nodetype = PRI_CPE,
1385 .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
1390 .internationalprefix = "",
1391 .nationalprefix = "",
1393 .privateprefix = "",
1394 .unknownprefix = "",
1395 .resetinterval = -1,
1400 .called_nai = SS7_NAI_NATIONAL,
1401 .calling_nai = SS7_NAI_NATIONAL,
1402 .internationalprefix = "",
1403 .nationalprefix = "",
1404 .subscriberprefix = "",
1410 .variant = OR2_VAR_ITU,
1411 .mfback_timeout = -1,
1412 .metering_pulse_timeout = -1,
1415 .get_ani_first = -1,
1417 .allow_collect_calls = 0,
1419 .accept_on_offer = 1,
1420 .forced_release = 0,
1422 .immediate_accept = -1,
1425 .loglevel = OR2_LOG_ERROR | OR2_LOG_WARNING,
1426 .category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER
1430 .context = "default",
1433 .mohinterpret = "default",
1436 .transfertobusy = 1,
1438 .cid_signalling = CID_SIG_BELL,
1439 .cid_start = CID_START_RING,
1440 .dahditrcallerid = 0,
1449 .echocancel.head.tap_length = 1,
1457 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1460 .polarityonanswerdelay = 600,
1462 .sendcalleridafter = DEFAULT_CIDRINGS,
1464 .buf_policy = DAHDI_POLICY_IMMEDIATE,
1479 .smdi_port = "/dev/ttyS0",
1486 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
1487 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
1488 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
1489 static int dahdi_sendtext(struct ast_channel *c, const char *text);
1490 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
1491 static int dahdi_hangup(struct ast_channel *ast);
1492 static int dahdi_answer(struct ast_channel *ast);
1493 static struct ast_frame *dahdi_read(struct ast_channel *ast);
1494 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
1495 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
1496 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
1497 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1498 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
1499 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
1500 static int handle_init_event(struct dahdi_pvt *i, int event);
1501 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value);
1503 static const struct ast_channel_tech dahdi_tech = {
1505 .description = tdesc,
1506 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
1507 .requester = dahdi_request,
1508 .send_digit_begin = dahdi_digit_begin,
1509 .send_digit_end = dahdi_digit_end,
1510 .send_text = dahdi_sendtext,
1512 .hangup = dahdi_hangup,
1513 .answer = dahdi_answer,
1515 .write = dahdi_write,
1516 .bridge = dahdi_bridge,
1517 .exception = dahdi_exception,
1518 .indicate = dahdi_indicate,
1519 .fixup = dahdi_fixup,
1520 .setoption = dahdi_setoption,
1521 .func_channel_read = dahdi_func_read,
1522 .func_channel_write = dahdi_func_write,
1526 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
1528 #define GET_CHANNEL(p) ((p)->channel)
1531 struct dahdi_pvt *round_robin[32];
1533 #if defined(HAVE_PRI)
1534 static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
1537 /* Grab the lock first */
1539 res = ast_mutex_trylock(&pri->lock);
1541 DEADLOCK_AVOIDANCE(&pvt->lock);
1544 /* Then break the poll */
1545 if (pri->master != AST_PTHREADT_NULL)
1546 pthread_kill(pri->master, SIGURG);
1549 #endif /* defined(HAVE_PRI) */
1551 #if defined(HAVE_SS7)
1552 static inline void ss7_rel(struct dahdi_ss7 *ss7)
1554 ast_mutex_unlock(&ss7->lock);
1556 #endif /* defined(HAVE_SS7) */
1558 #if defined(HAVE_SS7)
1559 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
1562 /* Grab the lock first */
1564 res = ast_mutex_trylock(&pri->lock);
1566 DEADLOCK_AVOIDANCE(&pvt->lock);
1569 /* Then break the poll */
1570 if (pri->master != AST_PTHREADT_NULL)
1571 pthread_kill(pri->master, SIGURG);
1574 #endif /* defined(HAVE_SS7) */
1575 #define NUM_CADENCE_MAX 25
1576 static int num_cadence = 4;
1577 static int user_has_defined_cadences = 0;
1579 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
1580 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
1581 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
1582 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
1583 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
1586 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
1587 * is 1, the second pause is 2 and so on.
1590 static int cidrings[NUM_CADENCE_MAX] = {
1591 2, /*!< Right after first long ring */
1592 4, /*!< Right after long part */
1593 3, /*!< After third chirp */
1594 2, /*!< Second spell */
1597 /* ETSI EN300 659-1 specifies the ring pulse between 200 and 300 mS */
1598 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
1600 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
1601 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
1603 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
1604 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
1606 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
1609 if (p->subs[SUB_REAL].owner == ast)
1611 else if (p->subs[SUB_CALLWAIT].owner == ast)
1613 else if (p->subs[SUB_THREEWAY].owner == ast)
1618 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
1623 static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
1627 ast_mutex_unlock(&pri->lock);
1630 if (p->subs[a].owner) {
1631 if (ast_channel_trylock(p->subs[a].owner)) {
1632 DEADLOCK_AVOIDANCE(&p->lock);
1634 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
1635 ast_channel_unlock(p->subs[a].owner);
1643 ast_mutex_lock(&pri->lock);
1647 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
1650 struct dahdi_pri *pri = (struct dahdi_pri*) data;
1653 struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
1655 /* We must unlock the PRI to avoid the possibility of a deadlock */
1656 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1663 ast_mutex_unlock(&pri->lock);
1668 ast_mutex_unlock(&ss7->lock);
1678 if (ast_channel_trylock(p->owner)) {
1679 DEADLOCK_AVOIDANCE(&p->lock);
1681 ast_queue_frame(p->owner, f);
1682 ast_channel_unlock(p->owner);
1688 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1695 ast_mutex_lock(&pri->lock);
1700 ast_mutex_lock(&ss7->lock);
1710 static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int);
1713 static int dahdi_r2_answer(struct dahdi_pvt *p)
1716 /* openr2 1.1.0 and older does not even define OR2_LIB_INTERFACE
1717 * and does not has support for openr2_chan_answer_call_with_mode
1719 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
1720 const char *double_answer = pbx_builtin_getvar_helper(p->owner, "MFCR2_DOUBLE_ANSWER");
1721 int wants_double_answer = ast_true(double_answer) ? 1 : 0;
1722 if (!double_answer) {
1723 /* this still can result in double answer if the channel context
1724 * was configured that way */
1725 res = openr2_chan_answer_call(p->r2chan);
1726 } else if (wants_double_answer) {
1727 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_DOUBLE);
1729 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_SIMPLE);
1732 res = openr2_chan_answer_call(p->r2chan);
1739 /* should be called with the ast_channel locked */
1740 static openr2_calling_party_category_t dahdi_r2_get_channel_category(struct ast_channel *c)
1742 openr2_calling_party_category_t cat;
1743 const char *catstr = pbx_builtin_getvar_helper(c, "MFCR2_CATEGORY");
1744 struct dahdi_pvt *p = c->tech_pvt;
1745 if (ast_strlen_zero(catstr)) {
1746 ast_debug(1, "No MFC/R2 category specified for chan %s, using default %s\n",
1747 c->name, openr2_proto_get_category_string(p->mfcr2_category));
1748 return p->mfcr2_category;
1750 if ((cat = openr2_proto_get_category(catstr)) == OR2_CALLING_PARTY_CATEGORY_UNKNOWN) {
1751 ast_log(LOG_WARNING, "Invalid category specified '%s' for chan %s, using default %s\n",
1752 catstr, c->name, openr2_proto_get_category_string(p->mfcr2_category));
1753 return p->mfcr2_category;
1755 ast_debug(1, "Using category %s\n", catstr);
1759 static void dahdi_r2_on_call_init(openr2_chan_t *r2chan)
1761 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
1762 ast_mutex_lock(&p->lock);
1764 ast_mutex_unlock(&p->lock);
1765 /* TODO: This can happen when some other thread just finished dahdi_request requesting this very same
1766 interface but has not yet seized the line (dahdi_call), and the far end wins and seize the line,
1767 can we avoid this somehow?, at this point when dahdi_call send the seize, it is likely that since
1768 the other end will see our seize as a forced release and drop the call, we will see an invalid
1769 pattern that will be seen and treated as protocol error. */
1770 ast_log(LOG_ERROR, "Collision of calls on chan %d detected!.\n", openr2_chan_get_number(r2chan));
1774 /* better safe than sorry ... */
1775 p->cid_name[0] = '\0';
1776 p->cid_num[0] = '\0';
1779 p->mfcr2_ani_index = '\0';
1780 p->mfcr2_dnis_index = '\0';
1781 p->mfcr2_dnis_matched = 0;
1782 p->mfcr2_answer_pending = 0;
1783 p->mfcr2_call_accepted = 0;
1784 ast_mutex_unlock(&p->lock);
1785 ast_log(LOG_NOTICE, "New MFC/R2 call detected on chan %d.\n", openr2_chan_get_number(r2chan));
1788 static int get_alarms(struct dahdi_pvt *p);
1789 static void handle_alarms(struct dahdi_pvt *p, int alms);
1790 static void dahdi_r2_on_hardware_alarm(openr2_chan_t *r2chan, int alarm)
1793 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
1794 ast_mutex_lock(&p->lock);
1795 p->inalarm = alarm ? 1 : 0;
1797 res = get_alarms(p);
1798 handle_alarms(p, res);
1800 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
1801 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", "Channel: %d\r\n", p->channel);
1803 ast_mutex_unlock(&p->lock);
1806 static void dahdi_r2_on_os_error(openr2_chan_t *r2chan, int errorcode)
1808 ast_log(LOG_ERROR, "OS error on chan %d: %s\n", openr2_chan_get_number(r2chan), strerror(errorcode));
1811 static void dahdi_r2_on_protocol_error(openr2_chan_t *r2chan, openr2_protocol_error_t reason)
1813 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
1814 ast_log(LOG_ERROR, "MFC/R2 protocol error on chan %d: %s\n", openr2_chan_get_number(r2chan), openr2_proto_get_error(reason));
1816 p->owner->hangupcause = AST_CAUSE_PROTOCOL_ERROR;
1817 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
1819 ast_mutex_lock(&p->lock);
1821 ast_mutex_unlock(&p->lock);
1824 static void dahdi_r2_update_monitor_count(struct dahdi_mfcr2 *mfcr2, int increment)
1826 ast_mutex_lock(&mfcr2->monitored_count_lock);
1828 mfcr2->monitored_count++;
1829 if (mfcr2->monitored_count == 1) {
1830 ast_log(LOG_DEBUG, "At least one device needs monitoring, let's wake up the monitor thread.\n");
1831 ast_cond_signal(&mfcr2->do_monitor);
1834 mfcr2->monitored_count--;
1835 if (mfcr2->monitored_count < 0) {
1836 ast_log(LOG_ERROR, "we have a bug here!.\n");
1839 ast_mutex_unlock(&mfcr2->monitored_count_lock);
1842 static void dahdi_r2_on_call_offered(openr2_chan_t *r2chan, const char *ani, const char *dnis, openr2_calling_party_category_t category)
1844 struct dahdi_pvt *p;
1845 struct ast_channel *c;
1846 ast_log(LOG_NOTICE, "MFC/R2 call offered on chan %d. ANI = %s, DNIS = %s, Category = %s\n",
1847 openr2_chan_get_number(r2chan), ani ? ani : "(restricted)", dnis,
1848 openr2_proto_get_category_string(category));
1849 p = openr2_chan_get_client_data(r2chan);
1850 /* if collect calls are not allowed and this is a collect call, reject it! */
1851 if (!p->mfcr2_allow_collect_calls && category == OR2_CALLING_PARTY_CATEGORY_COLLECT_CALL) {
1852 ast_log(LOG_NOTICE, "Rejecting MFC/R2 collect call\n");
1853 openr2_chan_disconnect_call(r2chan, OR2_CAUSE_COLLECT_CALL_REJECTED);
1856 ast_mutex_lock(&p->lock);
1857 p->mfcr2_recvd_category = category;
1858 /* if we're not supposed to use CID, clear whatever we have */
1859 if (!p->use_callerid) {
1860 ast_log(LOG_DEBUG, "No CID allowed in configuration, CID is being cleared!\n");
1864 /* if we're supposed to answer immediately, clear DNIS and set 's' exten */
1865 if (p->immediate || !openr2_context_get_max_dnis(openr2_chan_get_context(r2chan))) {
1866 ast_log(LOG_DEBUG, "Setting exten => s because of immediate or 0 DNIS configured\n");
1870 ast_mutex_unlock(&p->lock);
1871 if (!ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
1872 ast_log(LOG_NOTICE, "MFC/R2 call on channel %d requested non-existent extension '%s' in context '%s'. Rejecting call.\n",
1873 p->channel, p->exten, p->context);
1874 openr2_chan_disconnect_call(r2chan, OR2_CAUSE_UNALLOCATED_NUMBER);
1877 if (!p->mfcr2_accept_on_offer) {
1878 /* The user wants us to start the PBX thread right away without accepting the call first */
1879 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0);
1881 dahdi_r2_update_monitor_count(p->mfcr2, 0);
1882 /* Done here, don't disable reading now since we still need to generate MF tones to accept
1883 the call or reject it and detect the tone off condition of the other end, all of this
1884 will be done in the PBX thread now */
1887 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
1888 openr2_chan_disconnect_call(r2chan, OR2_CAUSE_OUT_OF_ORDER);
1889 } else if (p->mfcr2_charge_calls) {
1890 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge on chan %d\n", p->channel);
1891 openr2_chan_accept_call(r2chan, OR2_CALL_WITH_CHARGE);
1893 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge on chan %d\n", p->channel);
1894 openr2_chan_accept_call(r2chan, OR2_CALL_NO_CHARGE);
1898 static void dahdi_r2_on_call_end(openr2_chan_t *r2chan)
1900 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
1901 ast_log(LOG_NOTICE, "MFC/R2 call end on chan %d\n", p->channel);
1902 ast_mutex_lock(&p->lock);
1904 ast_mutex_unlock(&p->lock);
1907 static void dahdi_enable_ec(struct dahdi_pvt *p);
1908 static void dahdi_r2_on_call_accepted(openr2_chan_t *r2chan, openr2_call_mode_t mode)
1910 struct dahdi_pvt *p = NULL;
1911 struct ast_channel *c = NULL;
1912 ast_log(LOG_NOTICE, "MFC/R2 call has been accepted on chan %d\n", openr2_chan_get_number(r2chan));
1913 p = openr2_chan_get_client_data(r2chan);
1915 p->mfcr2_call_accepted = 1;
1916 /* if it's an incoming call ... */
1917 if (OR2_DIR_BACKWARD == openr2_chan_get_direction(r2chan)) {
1918 /* If accept on offer is not set, it means at this point the PBX thread is already
1919 launched (was launched in the 'on call offered' handler) and therefore this callback
1920 is being executed already in the PBX thread rather than the monitor thread, don't launch
1921 any other thread, just disable the openr2 reading and answer the call if needed */
1922 if (!p->mfcr2_accept_on_offer) {
1923 openr2_chan_disable_read(r2chan);
1924 if (p->mfcr2_answer_pending) {
1925 ast_log(LOG_DEBUG, "Answering MFC/R2 call after accepting it on chan %d\n", openr2_chan_get_number(r2chan));
1930 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0);
1932 dahdi_r2_update_monitor_count(p->mfcr2, 0);
1933 /* chan_dahdi will take care of reading from now on in the PBX thread, tell the
1934 library to forget about it */
1935 openr2_chan_disable_read(r2chan);
1938 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
1939 /* failed to create the channel, bail out and report it as an out of order line */
1940 openr2_chan_disconnect_call(r2chan, OR2_CAUSE_OUT_OF_ORDER);
1943 /* this is an outgoing call, no need to launch the PBX thread, most likely we're in one already */
1944 ast_log(LOG_NOTICE, "Call accepted on forward channel %d\n", p->channel);
1945 p->subs[SUB_REAL].needringing = 1;
1947 /* chan_dahdi will take care of reading from now on in the PBX thread, tell the
1948 library to forget about it */
1949 openr2_chan_disable_read(r2chan);
1952 static void dahdi_r2_on_call_answered(openr2_chan_t *r2chan)
1954 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
1955 ast_log(LOG_DEBUG, "MFC/R2 call has been answered on chan %d\n", openr2_chan_get_number(r2chan));
1956 p->subs[SUB_REAL].needanswer = 1;
1959 static void dahdi_r2_on_call_read(openr2_chan_t *r2chan, const unsigned char *buf, int buflen)
1961 /*ast_log(LOG_DEBUG, "Read data from dahdi channel %d\n", openr2_chan_get_number(r2chan));*/
1964 static int dahdi_r2_cause_to_ast_cause(openr2_call_disconnect_cause_t cause)
1967 case OR2_CAUSE_BUSY_NUMBER:
1968 return AST_CAUSE_BUSY;
1969 case OR2_CAUSE_NETWORK_CONGESTION:
1970 return AST_CAUSE_CONGESTION;
1971 case OR2_CAUSE_OUT_OF_ORDER:
1972 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1973 case OR2_CAUSE_UNALLOCATED_NUMBER:
1974 return AST_CAUSE_UNREGISTERED;
1975 case OR2_CAUSE_NO_ANSWER:
1976 return AST_CAUSE_NO_ANSWER;
1977 case OR2_CAUSE_NORMAL_CLEARING:
1978 return AST_CAUSE_NORMAL_CLEARING;
1979 case OR2_CAUSE_UNSPECIFIED:
1981 return AST_CAUSE_NOTDEFINED;
1985 static void dahdi_r2_on_call_disconnect(openr2_chan_t *r2chan, openr2_call_disconnect_cause_t cause)
1987 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
1988 ast_verb(3, "MFC/R2 call disconnected on chan %d\n", openr2_chan_get_number(r2chan));
1989 ast_mutex_lock(&p->lock);
1991 ast_mutex_unlock(&p->lock);
1992 /* no owner, therefore we can't use dahdi_hangup to disconnect, do it right now */
1993 openr2_chan_disconnect_call(r2chan, OR2_CAUSE_NORMAL_CLEARING);
1996 /* when we have an owner we don't call openr2_chan_disconnect_call here, that will
1997 be done in dahdi_hangup */
1998 if (p->owner->_state == AST_STATE_UP) {
1999 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2000 ast_mutex_unlock(&p->lock);
2001 } else if (openr2_chan_get_direction(r2chan) == OR2_DIR_FORWARD) {
2002 /* being the forward side we must report what happened to the call to whoever requested it */
2004 case OR2_CAUSE_BUSY_NUMBER:
2005 p->subs[SUB_REAL].needbusy = 1;
2007 case OR2_CAUSE_NETWORK_CONGESTION:
2008 case OR2_CAUSE_OUT_OF_ORDER:
2009 case OR2_CAUSE_UNALLOCATED_NUMBER:
2010 case OR2_CAUSE_NO_ANSWER:
2011 case OR2_CAUSE_UNSPECIFIED:
2012 case OR2_CAUSE_NORMAL_CLEARING:
2013 p->subs[SUB_REAL].needcongestion = 1;
2016 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2018 ast_mutex_unlock(&p->lock);
2020 ast_mutex_unlock(&p->lock);
2021 /* being the backward side and not UP yet, we only need to request hangup */
2022 /* TODO: what about doing this same thing when were AST_STATE_UP? */
2023 ast_queue_hangup_with_cause(p->owner, dahdi_r2_cause_to_ast_cause(cause));
2027 static void dahdi_r2_write_log(openr2_log_level_t level, char *logmessage)
2030 case OR2_LOG_NOTICE:
2031 ast_log(LOG_NOTICE, "%s", logmessage);
2033 case OR2_LOG_WARNING:
2034 ast_log(LOG_WARNING, "%s", logmessage);
2037 ast_log(LOG_ERROR, "%s", logmessage);
2039 case OR2_LOG_STACK_TRACE:
2040 case OR2_LOG_MF_TRACE:
2041 case OR2_LOG_CAS_TRACE:
2043 case OR2_LOG_EX_DEBUG:
2044 ast_log(LOG_DEBUG, "%s", logmessage);
2047 ast_log(LOG_WARNING, "We should handle logging level %d here.\n", level);
2048 ast_log(LOG_DEBUG, "%s", logmessage);
2053 static void dahdi_r2_on_line_blocked(openr2_chan_t *r2chan)
2055 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
2056 ast_mutex_lock(&p->lock);
2057 p->remotelyblocked = 1;
2058 ast_mutex_unlock(&p->lock);
2059 ast_log(LOG_NOTICE, "Far end blocked on chan %d\n", openr2_chan_get_number(r2chan));
2062 static void dahdi_r2_on_line_idle(openr2_chan_t *r2chan)
2064 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
2065 ast_mutex_lock(&p->lock);
2066 p->remotelyblocked = 0;
2067 ast_mutex_unlock(&p->lock);
2068 ast_log(LOG_NOTICE, "Far end unblocked on chan %d\n", openr2_chan_get_number(r2chan));
2071 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
2072 __attribute__((format (printf, 3, 0)));
2073 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
2075 #define CONTEXT_TAG "Context - "
2077 char completemsg[sizeof(logmsg) + sizeof(CONTEXT_TAG) - 1];
2078 vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
2079 snprintf(completemsg, sizeof(completemsg), CONTEXT_TAG "%s", logmsg);
2080 dahdi_r2_write_log(level, completemsg);
2084 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
2085 __attribute__((format (printf, 3, 0)));
2086 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
2088 #define CHAN_TAG "Chan "
2090 char completemsg[sizeof(logmsg) + sizeof(CHAN_TAG) - 1];
2091 vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
2092 snprintf(completemsg, sizeof(completemsg), CHAN_TAG "%d - %s", openr2_chan_get_number(r2chan), logmsg);
2093 dahdi_r2_write_log(level, completemsg);
2096 static int dahdi_r2_on_dnis_digit_received(openr2_chan_t *r2chan, char digit)
2098 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
2099 /* if 'immediate' is set, let's stop requesting DNIS */
2103 p->exten[p->mfcr2_dnis_index] = digit;
2104 p->rdnis[p->mfcr2_dnis_index] = digit;
2105 p->mfcr2_dnis_index++;
2106 p->exten[p->mfcr2_dnis_index] = 0;
2107 p->rdnis[p->mfcr2_dnis_index] = 0;
2108 /* if the DNIS is a match and cannot match more, stop requesting DNIS */
2109 if ((p->mfcr2_dnis_matched ||
2110 (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num) && (p->mfcr2_dnis_matched = 1))) &&
2111 !ast_matchmore_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
2114 /* otherwise keep going */
2118 static void dahdi_r2_on_ani_digit_received(openr2_chan_t *r2chan, char digit)
2120 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
2121 p->cid_num[p->mfcr2_ani_index] = digit;
2122 p->cid_name[p->mfcr2_ani_index] = digit;
2123 p->mfcr2_ani_index++;
2124 p->cid_num[p->mfcr2_ani_index] = 0;
2125 p->cid_name[p->mfcr2_ani_index] = 0;
2128 static openr2_event_interface_t dahdi_r2_event_iface = {
2129 .on_call_init = dahdi_r2_on_call_init,
2130 .on_call_offered = dahdi_r2_on_call_offered,
2131 .on_call_accepted = dahdi_r2_on_call_accepted,
2132 .on_call_answered = dahdi_r2_on_call_answered,
2133 .on_call_disconnect = dahdi_r2_on_call_disconnect,
2134 .on_call_end = dahdi_r2_on_call_end,
2135 .on_call_read = dahdi_r2_on_call_read,
2136 .on_hardware_alarm = dahdi_r2_on_hardware_alarm,
2137 .on_os_error = dahdi_r2_on_os_error,
2138 .on_protocol_error = dahdi_r2_on_protocol_error,
2139 .on_line_blocked = dahdi_r2_on_line_blocked,
2140 .on_line_idle = dahdi_r2_on_line_idle,
2141 /* cast seems to be needed to get rid of the annoying warning regarding format attribute */
2142 .on_context_log = (openr2_handle_context_logging_func)dahdi_r2_on_context_log,
2143 .on_dnis_digit_received = dahdi_r2_on_dnis_digit_received,
2144 .on_ani_digit_received = dahdi_r2_on_ani_digit_received,
2145 /* so far we do nothing with billing pulses */
2146 .on_billing_pulse_received = NULL
2149 static inline int16_t dahdi_r2_alaw_to_linear(uint8_t sample)
2151 return AST_ALAW(sample);
2154 static inline uint8_t dahdi_r2_linear_to_alaw(int sample)
2156 return AST_LIN2A(sample);
2159 static openr2_transcoder_interface_t dahdi_r2_transcode_iface = {
2160 dahdi_r2_alaw_to_linear,
2161 dahdi_r2_linear_to_alaw
2164 #endif /* HAVE_OPENR2 */
2166 static int restore_gains(struct dahdi_pvt *p);
2168 static void swap_subs(struct dahdi_pvt *p, int a, int b)
2172 struct ast_channel *towner;
2174 ast_debug(1, "Swapping %d and %d\n", a, b);
2176 tchan = p->subs[a].chan;
2177 towner = p->subs[a].owner;
2178 tinthreeway = p->subs[a].inthreeway;
2180 p->subs[a].chan = p->subs[b].chan;
2181 p->subs[a].owner = p->subs[b].owner;
2182 p->subs[a].inthreeway = p->subs[b].inthreeway;
2184 p->subs[b].chan = tchan;
2185 p->subs[b].owner = towner;
2186 p->subs[b].inthreeway = tinthreeway;
2188 if (p->subs[a].owner)
2189 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].dfd);
2190 if (p->subs[b].owner)
2191 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].dfd);
2192 wakeup_sub(p, a, NULL);
2193 wakeup_sub(p, b, NULL);
2196 static int dahdi_open(char *fn)
2204 for (x = 0; x < strlen(fn); x++) {
2205 if (!isdigit(fn[x])) {
2213 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
2216 fn = "/dev/dahdi/channel";
2218 fd = open(fn, O_RDWR | O_NONBLOCK);
2220 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
2224 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
2228 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
2233 if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
2234 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
2243 static void dahdi_close(int fd)
2249 static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
2251 dahdi_close(chan_pvt->subs[sub_num].dfd);
2252 chan_pvt->subs[sub_num].dfd = -1;
2255 #if defined(HAVE_PRI)
2256 static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
2258 dahdi_close(pri->fds[fd_num]);
2259 pri->fds[fd_num] = -1;
2261 #endif /* defined(HAVE_PRI) */
2263 #if defined(HAVE_SS7)
2264 static void dahdi_close_ss7_fd(struct dahdi_ss7 *ss7, int fd_num)
2266 dahdi_close(ss7->fds[fd_num]);
2267 ss7->fds[fd_num] = -1;
2269 #endif /* defined(HAVE_SS7) */
2271 static int dahdi_setlinear(int dfd, int linear)
2274 res = ioctl(dfd, DAHDI_SETLINEAR, &linear);
2281 static int alloc_sub(struct dahdi_pvt *p, int x)
2283 struct dahdi_bufferinfo bi;
2285 if (p->subs[x].dfd >= 0) {
2286 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
2290 p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
2291 if (p->subs[x].dfd <= -1) {
2292 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
2296 res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
2298 bi.txbufpolicy = p->buf_policy;
2299 bi.rxbufpolicy = p->buf_policy;
2300 bi.numbufs = p->buf_no;
2301 res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
2303 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
2306 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
2308 if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
2309 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
2310 dahdi_close_sub(p, x);
2311 p->subs[x].dfd = -1;
2314 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
2318 static int unalloc_sub(struct dahdi_pvt *p, int x)
2321 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
2324 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
2325 dahdi_close_sub(p, x);
2326 p->subs[x].linear = 0;
2327 p->subs[x].chan = 0;
2328 p->subs[x].owner = NULL;
2329 p->subs[x].inthreeway = 0;
2330 p->polarity = POLARITY_IDLE;
2331 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
2335 static int digit_to_dtmfindex(char digit)
2338 return DAHDI_TONE_DTMF_BASE + (digit - '0');
2339 else if (digit >= 'A' && digit <= 'D')
2340 return DAHDI_TONE_DTMF_A + (digit - 'A');
2341 else if (digit >= 'a' && digit <= 'd')
2342 return DAHDI_TONE_DTMF_A + (digit - 'a');
2343 else if (digit == '*')
2344 return DAHDI_TONE_DTMF_s;
2345 else if (digit == '#')
2346 return DAHDI_TONE_DTMF_p;
2351 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
2353 struct dahdi_pvt *pvt;
2357 pvt = chan->tech_pvt;
2359 ast_mutex_lock(&pvt->lock);
2361 idx = dahdi_get_index(chan, pvt, 0);
2363 if ((idx != SUB_REAL) || !pvt->owner)
2367 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
2368 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
2369 if (pvt->setup_ack) {
2370 if (!pri_grab(pvt, pvt->pri)) {
2371 pri_information(pvt->pri->pri, pvt->call, digit);
2374 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
2375 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
2377 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
2378 res = strlen(pvt->dialdest);
2379 pvt->dialdest[res++] = digit;
2380 pvt->dialdest[res] = '\0';
2385 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
2388 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
2390 struct dahdi_dialoperation zo = {
2391 .op = DAHDI_DIAL_OP_APPEND,
2394 zo.dialstr[0] = 'T';
2395 zo.dialstr[1] = digit;
2396 zo.dialstr[2] = '\0';
2397 if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
2398 ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
2402 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
2404 pvt->begindigit = digit;
2408 ast_mutex_unlock(&pvt->lock);
2413 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
2415 struct dahdi_pvt *pvt;
2420 pvt = chan->tech_pvt;
2422 ast_mutex_lock(&pvt->lock);
2424 idx = dahdi_get_index(chan, pvt, 0);
2426 if ((idx != SUB_REAL) || !pvt->owner || pvt->pulse)
2430 /* This means that the digit was already sent via PRI signalling */
2431 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
2432 && !pvt->begindigit)
2436 if (pvt->begindigit) {
2438 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
2439 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
2441 pvt->begindigit = 0;
2445 ast_mutex_unlock(&pvt->lock);
2450 static const char * const events[] = {
2463 "Hook Transition Complete",
2468 "Polarity Reversal",
2476 { DAHDI_ALARM_RED, "Red Alarm" },
2477 { DAHDI_ALARM_YELLOW, "Yellow Alarm" },
2478 { DAHDI_ALARM_BLUE, "Blue Alarm" },
2479 { DAHDI_ALARM_RECOVER, "Recovering" },
2480 { DAHDI_ALARM_LOOPBACK, "Loopback" },
2481 { DAHDI_ALARM_NOTOPEN, "Not Open" },
2482 { DAHDI_ALARM_NONE, "None" },
2485 static char *alarm2str(int alm)
2488 for (x = 0; x < ARRAY_LEN(alarms); x++) {
2489 if (alarms[x].alarm & alm)
2490 return alarms[x].name;
2492 return alm ? "Unknown Alarm" : "No Alarm";
2495 static const char *event2str(int event)
2497 static char buf[256];
2498 if ((event < (ARRAY_LEN(events))) && (event > -1))
2499 return events[event];
2500 sprintf(buf, "Event %d", event); /* safe */
2505 static char *dialplan2str(int dialplan)
2507 if (dialplan == -1 || dialplan == -2) {
2508 return("Dynamically set dialplan in ISDN");
2510 return (pri_plan2str(dialplan));
2514 static char *dahdi_sig2str(int sig)
2516 static char buf[256];
2519 return "E & M Immediate";
2521 return "E & M Wink";
2525 return "Feature Group D (DTMF)";
2527 return "Feature Group D (MF)";
2528 case SIG_FEATDMF_TA:
2529 return "Feature Groud D (MF) Tandem Access";
2531 return "Feature Group B (MF)";
2535 return "FGC/CAMA (Dialpulse)";
2536 case SIG_FGC_CAMAMF:
2537 return "FGC/CAMA (MF)";
2539 return "FXS Loopstart";
2541 return "FXS Groundstart";
2543 return "FXS Kewlstart";
2545 return "FXO Loopstart";
2547 return "FXO Groundstart";
2549 return "FXO Kewlstart";
2553 return "ISDN BRI Point to Point";
2555 return "ISDN BRI Point to MultiPoint";
2561 return "SF (Tone) Immediate";
2563 return "SF (Tone) Wink";
2565 return "SF (Tone) with Feature Group D (DTMF)";
2566 case SIG_SF_FEATDMF:
2567 return "SF (Tone) with Feature Group D (MF)";
2569 return "SF (Tone) with Feature Group B (MF)";
2570 case SIG_GR303FXOKS:
2571 return "GR-303 with FXOKS";
2572 case SIG_GR303FXSKS:
2573 return "GR-303 with FXSKS";
2577 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
2582 #define sig2str dahdi_sig2str
2584 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx, int slavechannel)
2586 /* If the conference already exists, and we're already in it
2587 don't bother doing anything */
2588 struct dahdi_confinfo zi;
2590 memset(&zi, 0, sizeof(zi));
2593 if (slavechannel > 0) {
2594 /* If we have only one slave, do a digital mon */
2595 zi.confmode = DAHDI_CONF_DIGITALMON;
2596 zi.confno = slavechannel;
2599 /* Real-side and pseudo-side both participate in conference */
2600 zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
2601 DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
2603 zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
2604 zi.confno = p->confno;
2606 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
2610 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
2611 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->dfd, zi.confmode, zi.confno, strerror(errno));
2614 if (slavechannel < 1) {
2615 p->confno = zi.confno;
2617 memcpy(&c->curconf, &zi, sizeof(c->curconf));
2618 ast_debug(1, "Added %d to conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
2622 static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
2624 /* If they're listening to our channel, they're ours */
2625 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
2627 /* If they're a talker on our (allocated) conference, they're ours */
2628 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
2633 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx)
2635 struct dahdi_confinfo zi;
2636 if (/* Can't delete if there's no dfd */
2638 /* Don't delete from the conference if it's not our conference */
2640 /* Don't delete if we don't think it's conferenced at all (implied) */
2642 memset(&zi, 0, sizeof(zi));
2643 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
2644 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->dfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
2647 ast_debug(1, "Removed %d from conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
2648 memcpy(&c->curconf, &zi, sizeof(c->curconf));
2652 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
2656 struct dahdi_pvt *slave = NULL;
2657 /* Start out optimistic */
2659 /* Update conference state in a stateless fashion */
2660 for (x = 0; x < 3; x++) {
2661 /* Any three-way calling makes slave native mode *definitely* out
2663 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway)
2666 /* If we don't have any 3-way calls, check to see if we have
2667 precisely one slave */
2668 if (useslavenative) {
2669 for (x = 0; x < MAX_SLAVES; x++) {
2672 /* Whoops already have a slave! No
2673 slave native and stop right away */
2678 /* We have one slave so far */
2679 slave = p->slaves[x];
2684 /* If no slave, slave native definitely out */
2687 else if (slave->law != p->law) {
2693 return useslavenative;
2696 static int reset_conf(struct dahdi_pvt *p)
2699 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
2700 if (p->subs[SUB_REAL].dfd > -1) {
2701 struct dahdi_confinfo zi;
2703 memset(&zi, 0, sizeof(zi));
2704 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &zi))
2705 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
2710 static int update_conf(struct dahdi_pvt *p)
2715 struct dahdi_pvt *slave = NULL;
2717 useslavenative = isslavenative(p, &slave);
2718 /* Start with the obvious, general stuff */
2719 for (x = 0; x < 3; x++) {
2720 /* Look for three way calls */
2721 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway) {
2722 conf_add(p, &p->subs[x], x, 0);
2725 conf_del(p, &p->subs[x], x);
2728 /* If we have a slave, add him to our conference now. or DAX
2729 if this is slave native */
2730 for (x = 0; x < MAX_SLAVES; x++) {
2733 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
2735 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
2740 /* If we're supposed to be in there, do so now */
2741 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
2743 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
2745 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
2749 /* If we have a master, add ourselves to his conference */
2751 if (isslavenative(p->master, NULL)) {
2752 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
2754 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
2758 /* Nobody is left (or should be left) in our conference.
2762 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
2766 static void dahdi_enable_ec(struct dahdi_pvt *p)
2773 ast_debug(1, "Echo cancellation already on\n");
2777 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
2780 if (p->echocancel.head.tap_length) {
2781 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2783 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x);
2785 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
2787 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &p->echocancel);
2789 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
2792 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
2795 ast_debug(1, "No echo cancellation requested\n");
2798 static void dahdi_train_ec(struct dahdi_pvt *p)
2803 if (p && p->echocanon && p->echotraining) {
2804 x = p->echotraining;
2805 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOTRAIN, &x);
2807 ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
2809 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
2811 ast_debug(1, "No echo training requested\n");
2815 static void dahdi_disable_ec(struct dahdi_pvt *p)
2820 struct dahdi_echocanparams ecp = { .tap_length = 0 };
2822 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &ecp);
2825 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
2827 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
2833 static void fill_txgain(struct dahdi_gains *g, float gain, int law)
2837 float linear_gain = pow(10.0, gain / 20.0);
2840 case DAHDI_LAW_ALAW:
2841 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
2843 k = (int) (((float) AST_ALAW(j)) * linear_gain);
2844 if (k > 32767) k = 32767;
2845 if (k < -32767) k = -32767;
2846 g->txgain[j] = AST_LIN2A(k);
2852 case DAHDI_LAW_MULAW:
2853 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
2855 k = (int) (((float) AST_MULAW(j)) * linear_gain);
2856 if (k > 32767) k = 32767;
2857 if (k < -32767) k = -32767;
2858 g->txgain[j] = AST_LIN2MU(k);
2867 static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
2871 float linear_gain = pow(10.0, gain / 20.0);
2874 case DAHDI_LAW_ALAW:
2875 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
2877 k = (int) (((float) AST_ALAW(j)) * linear_gain);
2878 if (k > 32767) k = 32767;
2879 if (k < -32767) k = -32767;
2880 g->rxgain[j] = AST_LIN2A(k);
2886 case DAHDI_LAW_MULAW:
2887 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
2889 k = (int) (((float) AST_MULAW(j)) * linear_gain);
2890 if (k > 32767) k = 32767;
2891 if (k < -32767) k = -32767;
2892 g->rxgain[j] = AST_LIN2MU(k);
2901 static int set_actual_txgain(int fd, int chan, float gain, int law)
2903 struct dahdi_gains g;
2906 memset(&g, 0, sizeof(g));
2908 res = ioctl(fd, DAHDI_GETGAINS, &g);
2910 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
2914 fill_txgain(&g, gain, law);
2916 return ioctl(fd, DAHDI_SETGAINS, &g);
2919 static int set_actual_rxgain(int fd, int chan, float gain, int law)
2921 struct dahdi_gains g;
2924 memset(&g, 0, sizeof(g));
2926 res = ioctl(fd, DAHDI_GETGAINS, &g);
2928 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
2932 fill_rxgain(&g, gain, law);
2934 return ioctl(fd, DAHDI_SETGAINS, &g);
2937 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
2939 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
2942 static int bump_gains(struct dahdi_pvt *p)
2946 /* Bump receive gain by value stored in cid_rxgain */
2947 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
2949 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
2956 static int restore_gains(struct dahdi_pvt *p)
2960 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
2962 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
2969 static inline int dahdi_set_hook(int fd, int hs)
2974 res = ioctl(fd, DAHDI_HOOK, &x);
2977 if (errno == EINPROGRESS)
2979 ast_log(LOG_WARNING, "DAHDI hook failed returned %d (trying %d): %s\n", res, hs, strerror(errno));
2980 /* will expectedly fail if phone is off hook during operation, such as during a restart */
2986 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
2990 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2992 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &y);
2994 ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
2996 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_CONFMUTE, &x);
2998 ast_log(LOG_WARNING, "DAHDI confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
3002 static int save_conference(struct dahdi_pvt *p)
3004 struct dahdi_confinfo c;
3006 if (p->saveconf.confmode) {
3007 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
3010 p->saveconf.chan = 0;
3011 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &p->saveconf);
3013 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
3014 p->saveconf.confmode = 0;
3017 memset(&c, 0, sizeof(c));
3018 c.confmode = DAHDI_CONF_NORMAL;
3019 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &c);
3021 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
3024 ast_debug(1, "Disabled conferencing\n");
3029 * \brief Send MWI state change
3031 * \arg mailbox_full This is the mailbox associated with the FXO line that the
3032 * MWI state has changed on.
3033 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
3034 * whether there are messages waiting or not.
3038 * This function does two things:
3040 * 1) It generates an internal Asterisk event notifying any other module that
3041 * cares about MWI that the state of a mailbox has changed.
3043 * 2) It runs the script specified by the mwimonitornotify option to allow
3044 * some custom handling of the state change.
3046 static void notify_message(char *mailbox_full, int thereornot)
3048 char s[sizeof(mwimonitornotify) + 80];
3049 struct ast_event *event;
3050 char *mailbox, *context;
3052 /* Strip off @default */
3053 context = mailbox = ast_strdupa(mailbox_full);
3054 strsep(&context, "@");
3055 if (ast_strlen_zero(context))
3056 context = "default";
3058 if (!(event = ast_event_new(AST_EVENT_MWI,
3059 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
3060 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
3061 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
3062 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
3063 AST_EVENT_IE_END))) {
3067 ast_event_queue_and_cache(event);
3069 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
3070 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
3075 static int restore_conference(struct dahdi_pvt *p)
3078 if (p->saveconf.confmode) {
3079 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &p->saveconf);
3080 p->saveconf.confmode = 0;
3082 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
3086 ast_debug(1, "Restored conferencing\n");
3090 static int send_callerid(struct dahdi_pvt *p);
3092 static int send_cwcidspill(struct dahdi_pvt *p)
3096 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
3098 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
3099 /* Make sure we account for the end */
3100 p->cidlen += READ_SIZE * 4;
3103 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
3107 static int has_voicemail(struct dahdi_pvt *p)
3110 struct ast_event *event;
3111 char *mailbox, *context;
3113 mailbox = context = ast_strdupa(p->mailbox);
3114 strsep(&context, "@");
3115 if (ast_strlen_zero(context))
3116 context = "default";
3118 event = ast_event_get_cached(AST_EVENT_MWI,
3119 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
3120 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
3124 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
3125 ast_event_destroy(event);
3127 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
3132 static int send_callerid(struct dahdi_pvt *p)
3134 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
3136 /* Take out of linear mode if necessary */
3137 if (p->subs[SUB_REAL].linear) {
3138 p->subs[SUB_REAL].linear = 0;
3139 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
3141 while (p->cidpos < p->cidlen) {
3142 res = write(p->subs[SUB_REAL].dfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
3144 if (errno == EAGAIN)
3147 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
3155 ast_free(p->cidspill);
3157 if (p->callwaitcas) {
3158 /* Wait for CID/CW to expire */
3159 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
3161 restore_conference(p);
3165 static int dahdi_callwait(struct ast_channel *ast)
3167 struct dahdi_pvt *p = ast->tech_pvt;
3168 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
3170 ast_log(LOG_WARNING, "Spill already exists?!?\n");
3171 ast_free(p->cidspill);
3173 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
3177 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
3178 if (!p->callwaitrings && p->callwaitingcallerid) {
3179 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
3181 p->cidlen = 2400 + 680 + READ_SIZE * 4;
3183 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
3185 p->cidlen = 2400 + READ_SIZE * 4;
3193 #if defined(HAVE_SS7)
3194 static unsigned char cid_pres2ss7pres(int cid_pres)
3196 return (cid_pres >> 5) & 0x03;
3198 #endif /* defined(HAVE_SS7) */
3200 #if defined(HAVE_SS7)
3201 static unsigned char cid_pres2ss7screen(int cid_pres)
3203 return cid_pres & 0x03;
3205 #endif /* defined(HAVE_SS7) */
3207 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
3209 struct dahdi_pvt *p = ast->tech_pvt;
3210 int x, res, idx,mysig;
3215 char dest[256]; /* must be same length as p->dialdest */
3216 ast_mutex_lock(&p->lock);
3217 ast_copy_string(dest, rdest, sizeof(dest));
3218 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
3219 if ((ast->_state == AST_STATE_BUSY)) {
3220 p->subs[SUB_REAL].needbusy = 1;
3221 ast_mutex_unlock(&p->lock);
3224 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
3225 ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
3226 ast_mutex_unlock(&p->lock);
3229 p->waitingfordt.tv_sec = 0;
3231 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
3233 /* Special pseudo -- automatically up */
3234 ast_setstate(ast, AST_STATE_UP);
3235 ast_mutex_unlock(&p->lock);
3238 x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
3239 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
3241 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
3244 set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
3247 if (p->outsigmod > -1)
3248 mysig = p->outsigmod;
3254 if (p->owner == ast) {
3255 /* Normal ring, on hook */
3257 /* Don't send audio while on hook, until the call is answered */
3259 if (p->use_callerid) {
3260 /* Generate the Caller-ID spill if desired */
3262 ast_log(LOG_WARNING, "cidspill already exists??\n");
3263 ast_free(p->cidspill);
3266 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
3267 p->cidlen = ast_callerid_generate(p->cidspill, ast->connected.id.name, ast->connected.id.number, AST_LAW(p));
3272 /* Choose proper cadence */
3273 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
3274 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
3275 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
3276 p->cidrings = cidrings[p->distinctivering - 1];
3278 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
3279 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
3280 p->cidrings = p->sendcalleridafter;
3283 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
3284 c = strchr(dest, '/');
3287 if (c && (strlen(c) < p->stripmsd)) {
3288 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
3292 p->dop.op = DAHDI_DIAL_OP_REPLACE;
3293 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
3294 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
3296 p->dop.dialstr[0] = '\0';
3299 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
3300 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
3301 ast_mutex_unlock(&p->lock);
3306 /* Call waiting call */
3307 p->callwaitrings = 0;
3308 if (ast->connected.id.number)
3309 ast_copy_string(p->callwait_num, ast->connected.id.number, sizeof(p->callwait_num));
3311 p->callwait_num[0] = '\0';
3312 if (ast->connected.id.name)
3313 ast_copy_string(p->callwait_name, ast->connected.id.name, sizeof(p->callwait_name));
3315 p->callwait_name[0] = '\0';
3316 /* Call waiting tone instead */
3317 if (dahdi_callwait(ast)) {
3318 ast_mutex_unlock(&p->lock);
3321 /* Make ring-back */
3322 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].dfd, DAHDI_TONE_RINGTONE))
3323 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
3325 n = ast->connected.id.name;
3326 l = ast->connected.id.number;
3328 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
3330 p->lastcid_num[0] = '\0';
3332 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
3334 p->lastcid_name[0] = '\0';
3335 ast_setstate(ast, AST_STATE_RINGING);
3336 idx = dahdi_get_index(ast, p, 0);
3338 p->subs[idx].needringing = 1;
3351 case SIG_FGC_CAMAMF:
3356 case SIG_SF_FEATDMF:
3357 case SIG_FEATDMF_TA:
3359 c = strchr(dest, '/');
3364 if (strlen(c) < p->stripmsd) {
3365 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
3366 ast_mutex_unlock(&p->lock);
3370 /* Start the trunk, if not GR-303 */
3374 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
3376 if (errno != EINPROGRESS) {
3377 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
3378 ast_mutex_unlock(&p->lock);
3385 ast_debug(1, "Dialing '%s'\n", c);
3386 p->dop.op = DAHDI_DIAL_OP_REPLACE;
3392 l = ast->connected.id.number;
3394 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
3396 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
3399 l = ast->connected.id.number;
3401 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
3403 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
3405 case SIG_FEATDMF_TA:
3407 const char *cic, *ozz;
3409 /* If you have to go through a Tandem Access point you need to use this */
3410 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
3413 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
3417 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
3418 ast_mutex_unlock(&p->lock);
3421 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
3422 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
3427 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
3430 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
3432 case SIG_FGC_CAMAMF:
3434 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
3438 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
3440 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
3444 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
3445 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
3446 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
3447 p->echorest[sizeof(p->echorest) - 1] = '\0';
3449 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
3453 /* waitfordialtone ? */
3457 if( p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp ) {
3458 ast_log(LOG_DEBUG, "Defer dialling for %dms or dialtone\n", p->waitfordialtone);
3459 gettimeofday(&p->waitingfordt,NULL);
3460 ast_setstate(ast, AST_STATE_OFFHOOK);
3467 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
3468 int saveerr = errno;
3471 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
3472 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
3473 ast_mutex_unlock(&p->lock);
3477 ast_debug(1, "Deferring dialing...\n");
3480 if (ast_strlen_zero(c))
3482 ast_setstate(ast, AST_STATE_DIALING);
3485 /* Special pseudo -- automatically up*/
3486 ast_setstate(ast, AST_STATE_UP);
3493 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
3494 p->dialdest[0] = '\0';
3498 ast_debug(1, "not yet implemented\n");
3499 ast_mutex_unlock(&p->lock);
3504 char ss7_called_nai;
3505 int called_nai_strip;
3506 char ss7_calling_nai;
3507 int calling_nai_strip;
3508 const char *charge_str = NULL;
3509 const char *gen_address = NULL;
3510 const char *gen_digits = NULL;
3511 const char *gen_dig_type = NULL;
3512 const char *gen_dig_scheme = NULL;
3513 const char *gen_name = NULL;
3514 const char *jip_digits = NULL;
3515 const char *lspi_ident = NULL;
3516 const char *rlt_flag = NULL;
3517 const char *call_ref_id = NULL;
3518 const char *call_ref_pc = NULL;
3519 const char *send_far = NULL;
3521 c = strchr(dest, '/');
3527 if (strlen(c) < p->stripmsd) {
3528 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
3529 ast_mutex_unlock(&p->lock);
3533 if (!p->hidecallerid) {
3534 l = ast->connected.id.number;
3539 if (ss7_grab(p, p->ss7)) {
3540 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
3541 ast_mutex_unlock(&p->lock);
3544 p->digital = IS_DIGITAL(ast->transfercapability);
3545 p->ss7call = isup_new_call(p->ss7->ss7);
3549 ast_mutex_unlock(&p->lock);
3550 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
3554 called_nai_strip = 0;
3555 ss7_called_nai = p->ss7->called_nai;
3556 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
3557 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
3558 called_nai_strip = strlen(p->ss7->internationalprefix);
3559 ss7_called_nai = SS7_NAI_INTERNATIONAL;
3560 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
3561 called_nai_strip = strlen(p->ss7->nationalprefix);
3562 ss7_called_nai = SS7_NAI_NATIONAL;
3564 ss7_called_nai = SS7_NAI_SUBSCRIBER;
3567 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
3569 calling_nai_strip = 0;
3570 ss7_calling_nai = p->ss7->calling_nai;
3571 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
3572 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
3573 calling_nai_strip = strlen(p->ss7->internationalprefix);
3574 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
3575 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
3576 calling_nai_strip = strlen(p->ss7->nationalprefix);
3577 ss7_calling_nai = SS7_NAI_NATIONAL;
3579 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
3582 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
3583 p->use_callingpres ? cid_pres2ss7pres(ast->connected.id.number_presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
3584 p->use_callingpres ? cid_pres2ss7screen(ast->connected.id.number_presentation) : SS7_SCREENING_USER_PROVIDED );
3586 isup_set_oli(p->ss7call, ast->connected.ani2);
3587 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
3589 ast_channel_lock(ast);
3590 /* Set the charge number if it is set */
3591 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
3593 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
3595 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");