2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, 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$")
53 #if defined(__NetBSD__) || defined(__FreeBSD__)
58 #include <sys/signal.h>
60 #include <sys/ioctl.h>
64 #include <dahdi/user.h>
65 #include <dahdi/tonezone.h>
66 #include "sig_analog.h"
67 /* Analog signaling is currently still present in chan_dahdi for use with
68 * radio. Sig_analog does not currently handle any radio operations. If
69 * radio only uses analog signaling, then the radio handling logic could
70 * be placed in sig_analog and the duplicated code could be removed.
79 #endif /* defined(HAVE_SS7) */
82 /* put this here until sig_mfcr2 comes along */
83 #define SIG_MFCR2_MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
87 #include "asterisk/lock.h"
88 #include "asterisk/channel.h"
89 #include "asterisk/config.h"
90 #include "asterisk/module.h"
91 #include "asterisk/pbx.h"
92 #include "asterisk/file.h"
93 #include "asterisk/ulaw.h"
94 #include "asterisk/alaw.h"
95 #include "asterisk/callerid.h"
96 #include "asterisk/adsi.h"
97 #include "asterisk/cli.h"
98 #include "asterisk/cdr.h"
99 #include "asterisk/cel.h"
100 #include "asterisk/features.h"
101 #include "asterisk/musiconhold.h"
102 #include "asterisk/say.h"
103 #include "asterisk/tdd.h"
104 #include "asterisk/app.h"
105 #include "asterisk/dsp.h"
106 #include "asterisk/astdb.h"
107 #include "asterisk/manager.h"
108 #include "asterisk/causes.h"
109 #include "asterisk/term.h"
110 #include "asterisk/utils.h"
111 #include "asterisk/transcap.h"
112 #include "asterisk/stringfields.h"
113 #include "asterisk/abstract_jb.h"
114 #include "asterisk/smdi.h"
115 #include "asterisk/astobj.h"
116 #include "asterisk/event.h"
117 #include "asterisk/devicestate.h"
118 #include "asterisk/paths.h"
119 #include "asterisk/ccss.h"
122 <application name="DAHDISendKeypadFacility" language="en_US">
124 Send digits out of band over a PRI.
127 <parameter name="digits" required="true" />
130 <para>This application will send the given string of digits in a Keypad
131 Facility IE over the current channel.</para>
134 <application name="DAHDISendCallreroutingFacility" language="en_US">
136 Send QSIG call rerouting facility over a PRI.
139 <parameter name="destination" required="true">
140 <para>Destination number.</para>
142 <parameter name="original">
143 <para>Original called number.</para>
145 <parameter name="reason">
146 <para>Diversion reason, if not specified defaults to <literal>unknown</literal></para>
150 <para>This application will send a Callrerouting Facility IE over the
151 current channel.</para>
154 <application name="DAHDIAcceptR2Call" language="en_US">
156 Accept an R2 call if its not already accepted (you still need to answer it)
159 <parameter name="charge" required="true">
160 <para>Yes or No.</para>
161 <para>Whether you want to accept the call with charge or without charge.</para>
165 <para>This application will Accept the R2 call either with charge or no charge.</para>
168 <manager name="DAHDITransfer" language="en_US">
170 Transfer DAHDI Channel.
173 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
174 <parameter name="DAHDIChannel" required="true">
175 <para>DAHDI channel name to transfer.</para>
179 <para>Transfer a DAHDI channel.</para>
182 <manager name="DAHDIHangup" language="en_US">
184 Hangup DAHDI Channel.
187 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
188 <parameter name="DAHDIChannel" required="true">
189 <para>DAHDI channel name to hangup.</para>
193 <para>Hangup a DAHDI channel.</para>
196 <manager name="DAHDIDialOffhook" language="en_US">
198 Dial over DAHDI channel while offhook.
201 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
202 <parameter name="DAHDIChannel" required="true" />
203 <parameter name="Number" required="true" />
208 <manager name="DAHDIDNDon" language="en_US">
210 Toggle DAHDI channel Do Not Disturb status ON.
213 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
214 <parameter name="DAHDIChannel" required="true" />
219 <manager name="DAHDIDNDoff" language="en_US">
221 Toggle DAHDI channel Do Not Disturb status OFF.
224 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
225 <parameter name="DAHDIChannel" required="true" />
230 <manager name="DAHDIShowChannels" language="en_US">
232 Show status DAHDI channels.
235 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
236 <parameter name="DAHDIChannel" required="true" />
241 <manager name="DAHDIRestart" language="en_US">
243 Fully Restart DAHDI channels (terminates calls).
246 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
253 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
255 static const char * const lbostr[] = {
256 "0 db (CSU)/0-133 feet (DSX-1)",
257 "133-266 feet (DSX-1)",
258 "266-399 feet (DSX-1)",
259 "399-533 feet (DSX-1)",
260 "533-655 feet (DSX-1)",
266 /*! Global jitterbuffer configuration - by default, jb is disabled */
267 static struct ast_jb_conf default_jbconf =
271 .resync_threshold = -1,
275 static struct ast_jb_conf global_jbconf;
278 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
279 * the user hangs up to reset the state machine so ring works properly.
280 * This is used to be able to support kewlstart by putting the zhone in
281 * groundstart mode since their forward disconnect supervision is entirely
282 * broken even though their documentation says it isn't and their support
283 * is entirely unwilling to provide any assistance with their channel banks
284 * even though their web site says they support their products for life.
286 /* #define ZHONE_HACK */
289 * Define if you want to check the hook state for an FXO (FXS signalled) interface
290 * before dialing on it. Certain FXO interfaces always think they're out of
291 * service with this method however.
293 /* #define DAHDI_CHECK_HOOKSTATE */
295 /*! \brief Typically, how many rings before we should send Caller*ID */
296 #define DEFAULT_CIDRINGS 1
298 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
301 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
302 #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))
304 static const char tdesc[] = "DAHDI Telephony Driver"
305 #if defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2)
307 #if defined(HAVE_PRI)
309 #endif /* defined(HAVE_PRI) */
310 #if defined(HAVE_SS7)
311 #if defined(HAVE_PRI)
313 #endif /* defined(HAVE_PRI) */
315 #endif /* defined(HAVE_SS7) */
316 #if defined(HAVE_OPENR2)
317 #if defined(HAVE_PRI) || defined(HAVE_SS7)
319 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
321 #endif /* defined(HAVE_OPENR2) */
322 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2) */
325 static const char config[] = "chan_dahdi.conf";
327 #define SIG_EM DAHDI_SIG_EM
328 #define SIG_EMWINK (0x0100000 | DAHDI_SIG_EM)
329 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
330 #define SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
331 #define SIG_FEATB (0x0800000 | DAHDI_SIG_EM)
332 #define SIG_E911 (0x1000000 | DAHDI_SIG_EM)
333 #define SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
334 #define SIG_FGC_CAMA (0x4000000 | DAHDI_SIG_EM)
335 #define SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
336 #define SIG_FXSLS DAHDI_SIG_FXSLS
337 #define SIG_FXSGS DAHDI_SIG_FXSGS
338 #define SIG_FXSKS DAHDI_SIG_FXSKS
339 #define SIG_FXOLS DAHDI_SIG_FXOLS
340 #define SIG_FXOGS DAHDI_SIG_FXOGS
341 #define SIG_FXOKS DAHDI_SIG_FXOKS
342 #define SIG_PRI DAHDI_SIG_CLEAR
343 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
344 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
345 #define SIG_SS7 (0x1000000 | DAHDI_SIG_CLEAR)
346 #define SIG_MFCR2 DAHDI_SIG_CAS
347 #define SIG_SF DAHDI_SIG_SF
348 #define SIG_SFWINK (0x0100000 | DAHDI_SIG_SF)
349 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
350 #define SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
351 #define SIG_SF_FEATB (0x0800000 | DAHDI_SIG_SF)
352 #define SIG_EM_E1 DAHDI_SIG_EM_E1
355 #define NUM_SPANS DAHDI_MAX_SPANS
360 #define CHAN_PSEUDO -2
362 #define CALLPROGRESS_PROGRESS 1
363 #define CALLPROGRESS_FAX_OUTGOING 2
364 #define CALLPROGRESS_FAX_INCOMING 4
365 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
367 #define NUM_CADENCE_MAX 25
368 static int num_cadence = 4;
369 static int user_has_defined_cadences = 0;
371 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
372 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
373 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
374 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
375 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
378 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
379 * is 1, the second pause is 2 and so on.
382 static int cidrings[NUM_CADENCE_MAX] = {
383 2, /*!< Right after first long ring */
384 4, /*!< Right after long part */
385 3, /*!< After third chirp */
386 2, /*!< Second spell */
389 /* ETSI EN300 659-1 specifies the ring pulse between 200 and 300 mS */
390 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
392 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
393 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
395 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
396 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
398 static char defaultcic[64] = "";
399 static char defaultozz[64] = "";
401 static char parkinglot[AST_MAX_EXTENSION] = ""; /*!< Default parking lot for this channel */
403 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
404 static char mwimonitornotify[PATH_MAX] = "";
405 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
406 static int mwisend_rpas = 0;
409 static char progzone[10] = "";
411 static int usedistinctiveringdetection = 0;
412 static int distinctiveringaftercid = 0;
414 static int numbufs = 4;
416 static int mwilevel = 512;
417 static int dtmfcid_level = 256;
419 #define REPORT_CHANNEL_ALARMS 1
420 #define REPORT_SPAN_ALARMS 2
421 static int report_alarms = REPORT_CHANNEL_ALARMS;
424 static int pridebugfd = -1;
425 static char pridebugfilename[1024] = "";
428 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
429 static int firstdigittimeout = 16000;
431 /*! \brief How long to wait for following digits (FXO logic) */
432 static int gendigittimeout = 8000;
434 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
435 static int matchdigittimeout = 3000;
437 /*! \brief Protect the interface list (of dahdi_pvt's) */
438 AST_MUTEX_DEFINE_STATIC(iflock);
441 static int ifcount = 0;
444 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
447 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
448 when it's doing something critical. */
449 AST_MUTEX_DEFINE_STATIC(monlock);
451 /*! \brief This is the thread for the monitor which checks for input on the channels
452 which are not currently in use. */
453 static pthread_t monitor_thread = AST_PTHREADT_NULL;
454 static ast_cond_t ss_thread_complete;
455 AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
456 AST_MUTEX_DEFINE_STATIC(restart_lock);
457 static int ss_thread_count = 0;
458 static int num_restart_pending = 0;
460 static int restart_monitor(void);
462 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);
464 static int dahdi_sendtext(struct ast_channel *c, const char *text);
466 static void mwi_event_cb(const struct ast_event *event, void *userdata)
468 /* This module does not handle MWI in an event-based manner. However, it
469 * subscribes to MWI for each mailbox that is configured so that the core
470 * knows that we care about it. Then, chan_dahdi will get the MWI from the
471 * event cache instead of checking the mailbox directly. */
474 /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
475 static inline int dahdi_get_event(int fd)
478 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
483 /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
484 static inline int dahdi_wait_event(int fd)
487 i = DAHDI_IOMUX_SIGEVENT;
488 if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
490 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
495 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
496 #define READ_SIZE 160
498 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
499 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
501 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
502 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
503 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
504 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
505 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
510 * \brief Configured ring timeout base.
511 * \note Value computed from "ringtimeout" read in from chan_dahdi.conf if it exists.
513 static int ringt_base = DEFAULT_RINGT;
515 #if defined(HAVE_SS7)
518 struct sig_ss7_linkset ss7;
521 static struct dahdi_ss7 linksets[NUM_SPANS];
523 static int cur_ss7type = -1;
524 static int cur_linkset = -1;
525 static int cur_pointcode = -1;
526 static int cur_cicbeginswith = -1;
527 static int cur_adjpointcode = -1;
528 static int cur_networkindicator = -1;
529 static int cur_defaultdpc = -1;
530 #endif /* defined(HAVE_SS7) */
534 pthread_t r2master; /*!< Thread of master */
535 openr2_context_t *protocol_context; /*!< OpenR2 context handle */
536 struct dahdi_pvt *pvts[SIG_MFCR2_MAX_CHANNELS]; /*!< Member channel pvt structs */
537 int numchans; /*!< Number of channels in this R2 block */
538 int monitored_count; /*!< Number of channels being monitored */
541 struct dahdi_mfcr2_conf {
542 openr2_variant_t variant;
544 int metering_pulse_timeout;
547 signed int get_ani_first:2;
548 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
549 signed int skip_category_request:2;
551 unsigned int call_files:1;
552 unsigned int allow_collect_calls:1;
553 unsigned int charge_calls:1;
554 unsigned int accept_on_offer:1;
555 unsigned int forced_release:1;
556 unsigned int double_answer:1;
557 signed int immediate_accept:2;
558 char logdir[OR2_MAX_PATH];
559 char r2proto_file[OR2_MAX_PATH];
560 openr2_log_level_t loglevel;
561 openr2_calling_party_category_t category;
564 /* malloc'd array of malloc'd r2links */
565 static struct dahdi_mfcr2 **r2links;
566 /* how many r2links have been malloc'd */
567 static int r2links_count = 0;
569 #endif /* HAVE_OPENR2 */
574 int dchannels[SIG_PRI_NUM_DCHANS]; /*!< What channel are the dchannels on */
575 int mastertrunkgroup; /*!< What trunk group is our master */
576 int prilogicalspan; /*!< Logical span number within trunk group */
577 struct sig_pri_pri pri;
580 static struct dahdi_pri pris[NUM_SPANS];
582 #if defined(HAVE_PRI_CCSS)
583 /*! DAHDI PRI CCSS agent and monitor type name. */
584 static const char dahdi_pri_cc_type[] = "DAHDI/PRI";
585 #endif /* defined(HAVE_PRI_CCSS) */
588 /*! Shut up the compiler */
592 #define SUB_REAL 0 /*!< Active call */
593 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
594 #define SUB_THREEWAY 2 /*!< Three-way call */
596 /* Polarity states */
597 #define POLARITY_IDLE 0
598 #define POLARITY_REV 1
601 struct distRingData {
605 struct ringContextData {
606 char contextData[AST_MAX_CONTEXT];
608 struct dahdi_distRings {
609 struct distRingData ringnum[3];
610 struct ringContextData ringContext[3];
613 static const char * const subnames[] = {
619 struct dahdi_subchannel {
621 struct ast_channel *owner;
623 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
624 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
625 unsigned int needringing:1;
626 unsigned int needbusy:1;
627 unsigned int needcongestion:1;
628 unsigned int needanswer:1;
629 unsigned int needflash:1;
630 unsigned int needhold:1;
631 unsigned int needunhold:1;
632 unsigned int linear:1;
633 unsigned int inthreeway:1;
634 struct dahdi_confinfo curconf;
637 #define CONF_USER_REAL (1 << 0)
638 #define CONF_USER_THIRDCALL (1 << 1)
642 /* States for sending MWI message
643 * First three states are required for send Ring Pulse Alert Signal
655 struct mwisend_info {
656 struct timeval pause;
657 mwisend_states mwisend_current;
660 /*! Specify the lists dahdi_pvt can be put in. */
662 DAHDI_IFLIST_NONE, /*!< The dahdi_pvt is not in any list. */
663 DAHDI_IFLIST_MAIN, /*!< The dahdi_pvt is in the main interface list */
664 #if defined(HAVE_PRI)
665 DAHDI_IFLIST_NO_B_CHAN, /*!< The dahdi_pvt is in a no B channel interface list */
666 #endif /* defined(HAVE_PRI) */
670 ast_mutex_t lock; /*!< Channel private lock. */
671 struct callerid_state *cs;
672 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
673 /*!< Up to three channels can be associated with this call */
675 struct dahdi_subchannel sub_unused; /*!< Just a safety precaution */
676 struct dahdi_subchannel subs[3]; /*!< Sub-channels */
677 struct dahdi_confinfo saveconf; /*!< Saved conference info */
679 struct dahdi_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
680 struct dahdi_pvt *master; /*!< Master to us (we follow their conferencing) */
681 int inconference; /*!< If our real should be in the conference */
683 int bufsize; /*!< Size of the buffers */
684 int buf_no; /*!< Number of buffers */
685 int buf_policy; /*!< Buffer policy */
686 int faxbuf_no; /*!< Number of Fax buffers */
687 int faxbuf_policy; /*!< Fax buffer policy */
688 int sig; /*!< Signalling style */
690 * \brief Nonzero if the signaling type is sent over a radio.
691 * \note Set to a couple of nonzero values but it is only tested like a boolean.
694 int outsigmod; /*!< Outbound Signalling style (modifier) */
695 int oprmode; /*!< "Operator Services" mode */
696 struct dahdi_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
697 /*! \brief Amount of gain to increase during caller id */
699 /*! \brief Rx gain set by chan_dahdi.conf */
701 /*! \brief Tx gain set by chan_dahdi.conf */
704 float txdrc; /*!< Dynamic Range Compression factor. a number between 1 and 6ish */
707 int tonezone; /*!< tone zone for this chan, or -1 for default */
708 enum DAHDI_IFLIST which_iflist; /*!< Which interface list is this structure listed? */
709 struct dahdi_pvt *next; /*!< Next channel in list */
710 struct dahdi_pvt *prev; /*!< Prev channel in list */
715 * \brief TRUE if ADSI (Analog Display Services Interface) available
716 * \note Set from the "adsi" value read in from chan_dahdi.conf
720 * \brief TRUE if we can use a polarity reversal to mark when an outgoing
721 * call is answered by the remote party.
722 * \note Set from the "answeronpolarityswitch" value read in from chan_dahdi.conf
724 unsigned int answeronpolarityswitch:1;
726 * \brief TRUE if busy detection is enabled.
727 * (Listens for the beep-beep busy pattern.)
728 * \note Set from the "busydetect" value read in from chan_dahdi.conf
730 unsigned int busydetect:1;
732 * \brief TRUE if call return is enabled.
733 * (*69, if your dialplan doesn't catch this first)
734 * \note Set from the "callreturn" value read in from chan_dahdi.conf
736 unsigned int callreturn:1;
738 * \brief TRUE if busy extensions will hear the call-waiting tone
739 * and can use hook-flash to switch between callers.
740 * \note Can be disabled by dialing *70.
741 * \note Initialized with the "callwaiting" value read in from chan_dahdi.conf
743 unsigned int callwaiting:1;
745 * \brief TRUE if send caller ID for Call Waiting
746 * \note Set from the "callwaitingcallerid" value read in from chan_dahdi.conf
748 unsigned int callwaitingcallerid:1;
750 * \brief TRUE if support for call forwarding enabled.
751 * Dial *72 to enable call forwarding.
752 * Dial *73 to disable call forwarding.
753 * \note Set from the "cancallforward" value read in from chan_dahdi.conf
755 unsigned int cancallforward:1;
757 * \brief TRUE if support for call parking is enabled.
758 * \note Set from the "canpark" value read in from chan_dahdi.conf
760 unsigned int canpark:1;
761 /*! \brief TRUE if to wait for a DTMF digit to confirm answer */
762 unsigned int confirmanswer:1;
764 * \brief TRUE if the channel is to be destroyed on hangup.
765 * (Used by pseudo channels.)
767 unsigned int destroy:1;
768 unsigned int didtdd:1; /*!< flag to say its done it once */
769 /*! \brief TRUE if analog type line dialed no digits in Dial() */
770 unsigned int dialednone:1;
772 * \brief TRUE if in the process of dialing digits or sending something.
773 * \note This is used as a receive squelch for ISDN until connected.
775 unsigned int dialing:1;
776 /*! \brief TRUE if the transfer capability of the call is digital. */
777 unsigned int digital:1;
778 /*! \brief TRUE if Do-Not-Disturb is enabled, present only for non sig_analog */
780 /*! \brief XXX BOOLEAN Purpose??? */
781 unsigned int echobreak:1;
783 * \brief TRUE if echo cancellation enabled when bridged.
784 * \note Initialized with the "echocancelwhenbridged" value read in from chan_dahdi.conf
785 * \note Disabled if the echo canceller is not setup.
787 unsigned int echocanbridged:1;
788 /*! \brief TRUE if echo cancellation is turned on. */
789 unsigned int echocanon:1;
790 /*! \brief TRUE if a fax tone has already been handled. */
791 unsigned int faxhandled:1;
792 /*! TRUE if dynamic faxbuffers are configured for use, default is OFF */
793 unsigned int usefaxbuffers:1;
794 /*! TRUE while buffer configuration override is in use */
795 unsigned int bufferoverrideinuse:1;
796 /*! \brief TRUE if over a radio and dahdi_read() has been called. */
797 unsigned int firstradio:1;
799 * \brief TRUE if the call will be considered "hung up" on a polarity reversal.
800 * \note Set from the "hanguponpolarityswitch" value read in from chan_dahdi.conf
802 unsigned int hanguponpolarityswitch:1;
803 /*! \brief TRUE if DTMF detection needs to be done by hardware. */
804 unsigned int hardwaredtmf:1;
806 * \brief TRUE if the outgoing caller ID is blocked/hidden.
807 * \note Caller ID can be disabled by dialing *67.
808 * \note Caller ID can be enabled by dialing *82.
809 * \note Initialized with the "hidecallerid" value read in from chan_dahdi.conf
811 unsigned int hidecallerid:1;
813 * \brief TRUE if hide just the name not the number for legacy PBX use.
814 * \note Only applies to PRI channels.
815 * \note Set from the "hidecalleridname" value read in from chan_dahdi.conf
817 unsigned int hidecalleridname:1;
818 /*! \brief TRUE if DTMF detection is disabled. */
819 unsigned int ignoredtmf:1;
821 * \brief TRUE if the channel should be answered immediately
822 * without attempting to gather any digits.
823 * \note Set from the "immediate" value read in from chan_dahdi.conf
825 unsigned int immediate:1;
826 /*! \brief TRUE if in an alarm condition. */
827 unsigned int inalarm:1;
828 /*! \brief TRUE if TDD in MATE mode */
830 /*! \brief TRUE if we originated the call leg. */
831 unsigned int outgoing:1;
832 /* unsigned int overlapdial:1; unused and potentially confusing */
834 * \brief TRUE if busy extensions will hear the call-waiting tone
835 * and can use hook-flash to switch between callers.
836 * \note Set from the "callwaiting" value read in from chan_dahdi.conf
838 unsigned int permcallwaiting:1;
840 * \brief TRUE if the outgoing caller ID is blocked/restricted/hidden.
841 * \note Set from the "hidecallerid" value read in from chan_dahdi.conf
843 unsigned int permhidecallerid:1;
845 * \brief TRUE if PRI congestion/busy indications are sent out-of-band.
846 * \note Set from the "priindication" value read in from chan_dahdi.conf
848 unsigned int priindication_oob:1;
850 * \brief TRUE if PRI B channels are always exclusively selected.
851 * \note Set from the "priexclusive" value read in from chan_dahdi.conf
853 unsigned int priexclusive:1;
855 * \brief TRUE if we will pulse dial.
856 * \note Set from the "pulsedial" value read in from chan_dahdi.conf
858 unsigned int pulse:1;
859 /*! \brief TRUE if a pulsed digit was detected. (Pulse dial phone detected) */
860 unsigned int pulsedial:1;
861 unsigned int restartpending:1; /*!< flag to ensure counted only once for restart */
863 * \brief TRUE if caller ID is restricted.
864 * \note Set but not used. Should be deleted. Redundant with permhidecallerid.
865 * \note Set from the "restrictcid" value read in from chan_dahdi.conf
867 unsigned int restrictcid:1;
869 * \brief TRUE if three way calling is enabled
870 * \note Set from the "threewaycalling" value read in from chan_dahdi.conf
872 unsigned int threewaycalling:1;
874 * \brief TRUE if call transfer is enabled
875 * \note For FXS ports (either direct analog or over T1/E1):
876 * Support flash-hook call transfer
877 * \note For digital ports using ISDN PRI protocols:
878 * Support switch-side transfer (called 2BCT, RLT or other names)
879 * \note Set from the "transfer" value read in from chan_dahdi.conf
881 unsigned int transfer:1;
883 * \brief TRUE if caller ID is used on this channel.
884 * \note PRI and SS7 spans will save caller ID from the networking peer.
885 * \note FXS ports will generate the caller ID spill.
886 * \note FXO ports will listen for the caller ID spill.
887 * \note Set from the "usecallerid" value read in from chan_dahdi.conf
889 unsigned int use_callerid:1;
891 * \brief TRUE if we will use the calling presentation setting
892 * from the Asterisk channel for outgoing calls.
893 * \note Only applies to PRI and SS7 channels.
894 * \note Set from the "usecallingpres" value read in from chan_dahdi.conf
896 unsigned int use_callingpres:1;
898 * \brief TRUE if distinctive rings are to be detected.
899 * \note For FXO lines
900 * \note Set indirectly from the "usedistinctiveringdetection" value read in from chan_dahdi.conf
902 unsigned int usedistinctiveringdetection:1;
904 * \brief TRUE if we should use the callerid from incoming call on dahdi transfer.
905 * \note Set from the "useincomingcalleridondahditransfer" value read in from chan_dahdi.conf
907 unsigned int dahditrcallerid:1;
909 * \brief TRUE if allowed to flash-transfer to busy channels.
910 * \note Set from the "transfertobusy" value read in from chan_dahdi.conf
912 unsigned int transfertobusy:1;
914 * \brief TRUE if the FXO port monitors for neon type MWI indications from the other end.
915 * \note Set if the "mwimonitor" value read in contains "neon" from chan_dahdi.conf
917 unsigned int mwimonitor_neon:1;
919 * \brief TRUE if the FXO port monitors for fsk type MWI indications from the other end.
920 * \note Set if the "mwimonitor" value read in contains "fsk" from chan_dahdi.conf
922 unsigned int mwimonitor_fsk:1;
924 * \brief TRUE if the FXO port monitors for rpas precursor to fsk MWI indications from the other end.
925 * \note RPAS - Ring Pulse Alert Signal
926 * \note Set if the "mwimonitor" value read in contains "rpas" from chan_dahdi.conf
928 unsigned int mwimonitor_rpas:1;
929 /*! \brief TRUE if an MWI monitor thread is currently active */
930 unsigned int mwimonitoractive:1;
931 /*! \brief TRUE if a MWI message sending thread is active */
932 unsigned int mwisendactive:1;
934 * \brief TRUE if channel is out of reset and ready
935 * \note Set but not used.
937 unsigned int inservice:1;
939 * \brief TRUE if the channel is locally blocked.
940 * \note Applies to SS7 and MFCR2 channels.
942 unsigned int locallyblocked:1;
944 * \brief TRUE if the channel is remotely blocked.
945 * \note Applies to SS7 and MFCR2 channels.
947 unsigned int remotelyblocked:1;
949 * \brief TRUE if the channel alarms will be managed also as Span ones
950 * \note Applies to all channels
952 unsigned int manages_span_alarms:1;
954 #if defined(HAVE_PRI)
955 struct sig_pri_pri *pri;
959 * \brief TRUE if SMDI (Simplified Message Desk Interface) is enabled
960 * \note Set from the "usesmdi" value read in from chan_dahdi.conf
962 unsigned int use_smdi:1;
963 struct mwisend_info mwisend_data;
964 /*! \brief The serial port to listen for SMDI data on */
965 struct ast_smdi_interface *smdi_iface;
967 /*! \brief Distinctive Ring data */
968 struct dahdi_distRings drings;
971 * \brief The configured context for incoming calls.
972 * \note The "context" string read in from chan_dahdi.conf
974 char context[AST_MAX_CONTEXT];
976 * \brief Saved context string.
978 char defcontext[AST_MAX_CONTEXT];
979 /*! \brief Extension to use in the dialplan. */
980 char exten[AST_MAX_EXTENSION];
982 * \brief Language configured for calls.
983 * \note The "language" string read in from chan_dahdi.conf
985 char language[MAX_LANGUAGE];
987 * \brief The configured music-on-hold class to use for calls.
988 * \note The "musicclass" or "mohinterpret" or "musiconhold" string read in from chan_dahdi.conf
990 char mohinterpret[MAX_MUSICCLASS];
992 * \brief Suggested music-on-hold class for peer channel to use for calls.
993 * \note The "mohsuggest" string read in from chan_dahdi.conf
995 char mohsuggest[MAX_MUSICCLASS];
996 char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
997 #if defined(HAVE_PRI) || defined(HAVE_SS7)
998 /*! \brief Automatic Number Identification number (Alternate PRI caller ID number) */
999 char cid_ani[AST_MAX_EXTENSION];
1000 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
1001 /*! \brief Automatic Number Identification code from PRI */
1003 /*! \brief Caller ID number from an incoming call. */
1004 char cid_num[AST_MAX_EXTENSION];
1006 * \brief Caller ID tag from incoming call
1007 * \note the "cid_tag" string read in from chan_dahdi.conf
1009 char cid_tag[AST_MAX_EXTENSION];
1010 /*! \brief Caller ID Q.931 TON/NPI field values. Set by PRI. Zero otherwise. */
1012 /*! \brief Caller ID name from an incoming call. */
1013 char cid_name[AST_MAX_EXTENSION];
1014 /*! \brief Caller ID subaddress from an incoming call. */
1015 char cid_subaddr[AST_MAX_EXTENSION];
1016 char *origcid_num; /*!< malloced original callerid */
1017 char *origcid_name; /*!< malloced original callerid */
1018 /*! \brief Call waiting number. */
1019 char callwait_num[AST_MAX_EXTENSION];
1020 /*! \brief Call waiting name. */
1021 char callwait_name[AST_MAX_EXTENSION];
1022 /*! \brief Redirecting Directory Number Information Service (RDNIS) number */
1023 char rdnis[AST_MAX_EXTENSION];
1024 /*! \brief Dialed Number Identifier */
1025 char dnid[AST_MAX_EXTENSION];
1027 * \brief Bitmapped groups this belongs to.
1028 * \note The "group" bitmapped group string read in from chan_dahdi.conf
1031 /*! \brief Default call PCM encoding format: DAHDI_LAW_ALAW or DAHDI_LAW_MULAW. */
1033 /*! \brief Active PCM encoding format: DAHDI_LAW_ALAW or DAHDI_LAW_MULAW */
1035 int confno; /*!< Our conference */
1036 int confusers; /*!< Who is using our conference */
1037 int propconfno; /*!< Propagated conference number */
1039 * \brief Bitmapped call groups this belongs to.
1040 * \note The "callgroup" bitmapped group string read in from chan_dahdi.conf
1042 ast_group_t callgroup;
1044 * \brief Bitmapped pickup groups this belongs to.
1045 * \note The "pickupgroup" bitmapped group string read in from chan_dahdi.conf
1047 ast_group_t pickupgroup;
1049 * \brief Channel variable list with associated values to set when a channel is created.
1050 * \note The "setvar" strings read in from chan_dahdi.conf
1052 struct ast_variable *vars;
1053 int channel; /*!< Channel Number */
1054 int span; /*!< Span number */
1055 time_t guardtime; /*!< Must wait this much time before using for new call */
1056 int cid_signalling; /*!< CID signalling type bell202 or v23 */
1057 int cid_start; /*!< CID start indicator, polarity or ring or DTMF without warning event */
1058 int dtmfcid_holdoff_state; /*!< State indicator that allows for line to settle before checking for dtmf energy */
1059 struct timeval dtmfcid_delay; /*!< Time value used for allow line to settle */
1060 int callingpres; /*!< The value of calling presentation that we're going to use when placing a PRI call */
1061 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
1062 int cidcwexpire; /*!< When to expire our muting for CID/CW */
1063 /*! \brief Analog caller ID waveform sample buffer */
1064 unsigned char *cidspill;
1065 /*! \brief Position in the cidspill buffer to send out next. */
1067 /*! \brief Length of the cidspill buffer containing samples. */
1069 /*! \brief Ring timeout timer?? */
1072 * \brief Ring timeout base.
1073 * \note Value computed indirectly from "ringtimeout" read in from chan_dahdi.conf
1077 * \brief Number of most significant digits/characters to strip from the dialed number.
1078 * \note Feature is deprecated. Use dialplan logic.
1079 * \note The characters are stripped before the PRI TON/NPI prefix
1080 * characters are processed.
1083 /*! \brief BOOLEAN. XXX Meaning what?? */
1085 /*! \brief Number of call waiting rings. */
1087 /*! \brief Echo cancel parameters. */
1089 struct dahdi_echocanparams head;
1090 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
1093 * \brief Echo training time. 0 = disabled
1094 * \note Set from the "echotraining" value read in from chan_dahdi.conf
1097 /*! \brief Filled with 'w'. XXX Purpose?? */
1100 * \brief Number of times to see "busy" tone before hanging up.
1101 * \note Set from the "busycount" value read in from chan_dahdi.conf
1105 * \brief Length of "busy" tone on time.
1106 * \note Set from the "busypattern" value read in from chan_dahdi.conf
1108 int busy_tonelength;
1110 * \brief Length of "busy" tone off time.
1111 * \note Set from the "busypattern" value read in from chan_dahdi.conf
1113 int busy_quietlength;
1115 * \brief Bitmapped call progress detection flags. CALLPROGRESS_xxx values.
1116 * \note Bits set from the "callprogress" and "faxdetect" values read in from chan_dahdi.conf
1120 * \brief Number of milliseconds to wait for dialtone.
1121 * \note Set from the "waitfordialtone" value read in from chan_dahdi.conf
1123 int waitfordialtone;
1124 struct timeval waitingfordt; /*!< Time we started waiting for dialtone */
1125 struct timeval flashtime; /*!< Last flash-hook time */
1126 /*! \brief Opaque DSP configuration structure. */
1127 struct ast_dsp *dsp;
1128 /*! \brief DAHDI dial operation command struct for ioctl() call. */
1129 struct dahdi_dialoperation dop;
1130 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
1131 /*! \brief Second part of SIG_FEATDMF_TA wink operation. */
1133 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
1134 int amaflags; /*!< AMA Flags */
1135 struct tdd_state *tdd; /*!< TDD flag */
1136 /*! \brief Accumulated call forwarding number. */
1137 char call_forward[AST_MAX_EXTENSION];
1139 * \brief Voice mailbox location.
1140 * \note Set from the "mailbox" string read in from chan_dahdi.conf
1142 char mailbox[AST_MAX_EXTENSION];
1143 /*! \brief Opaque event subscription parameters for message waiting indication support. */
1144 struct ast_event_sub *mwi_event_sub;
1145 /*! \brief Delayed dialing for E911. Overlap digits for ISDN. */
1147 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1148 struct dahdi_vmwi_info mwisend_setting; /*!< Which VMWI methods to use */
1149 unsigned int mwisend_fsk: 1; /*! Variable for enabling FSK MWI handling in chan_dahdi */
1150 unsigned int mwisend_rpas:1; /*! Variable for enabling Ring Pulse Alert before MWI FSK Spill */
1152 int distinctivering; /*!< Which distinctivering to use */
1153 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
1154 /*! \brief Holding place for event injected from outside normal operation. */
1157 * \brief Minimal time period (ms) between the answer polarity
1158 * switch and hangup polarity switch.
1160 int polarityonanswerdelay;
1161 /*! \brief Start delay time if polarityonanswerdelay is nonzero. */
1162 struct timeval polaritydelaytv;
1164 * \brief Send caller ID on FXS after this many rings. Set to 1 for US.
1165 * \note Set from the "sendcalleridafter" value read in from chan_dahdi.conf
1167 int sendcalleridafter;
1168 /*! \brief Current line interface polarity. POLARITY_IDLE, POLARITY_REV */
1170 /*! \brief DSP feature flags: DSP_FEATURE_xxx */
1172 #if defined(HAVE_SS7)
1173 /*! \brief SS7 control parameters */
1174 struct sig_ss7_linkset *ss7;
1175 #endif /* defined(HAVE_SS7) */
1177 struct dahdi_mfcr2 *mfcr2;
1178 openr2_chan_t *r2chan;
1179 openr2_calling_party_category_t mfcr2_recvd_category;
1180 openr2_calling_party_category_t mfcr2_category;
1181 int mfcr2_dnis_index;
1182 int mfcr2_ani_index;
1184 int mfcr2_answer_pending:1;
1185 int mfcr2_charge_calls:1;
1186 int mfcr2_allow_collect_calls:1;
1187 int mfcr2_forced_release:1;
1188 int mfcr2_dnis_matched:1;
1189 int mfcr2_call_accepted:1;
1190 int mfcr2_accept_on_offer:1;
1192 /*! \brief DTMF digit in progress. 0 when no digit in progress. */
1194 /*! \brief TRUE if confrence is muted. */
1197 struct ast_cc_config_params *cc_params;
1198 /* DAHDI channel names may differ greatly from the
1199 * string that was provided to an app such as Dial. We
1200 * need to save the original string passed to dahdi_request
1201 * for call completion purposes. This way, we can replicate
1202 * the original dialed string later.
1204 char dialstring[AST_CHANNEL_NAME];
1207 static struct dahdi_pvt *iflist = NULL; /*!< Main interface list start */
1208 static struct dahdi_pvt *ifend = NULL; /*!< Main interface list end */
1210 #if defined(HAVE_PRI)
1211 static struct dahdi_parms_pseudo {
1212 int buf_no; /*!< Number of buffers */
1213 int buf_policy; /*!< Buffer policy */
1214 int faxbuf_no; /*!< Number of Fax buffers */
1215 int faxbuf_policy; /*!< Fax buffer policy */
1216 } dahdi_pseudo_parms;
1217 #endif /* defined(HAVE_PRI) */
1219 /*! \brief Channel configuration from chan_dahdi.conf .
1220 * This struct is used for parsing the [channels] section of chan_dahdi.conf.
1221 * Generally there is a field here for every possible configuration item.
1223 * The state of fields is saved along the parsing and whenever a 'channel'
1224 * statement is reached, the current dahdi_chan_conf is used to configure the
1225 * channel (struct dahdi_pvt)
1227 * \see dahdi_chan_init for the default values.
1229 struct dahdi_chan_conf {
1230 struct dahdi_pvt chan;
1232 struct dahdi_pri pri;
1235 #if defined(HAVE_SS7)
1236 struct dahdi_ss7 ss7;
1237 #endif /* defined(HAVE_SS7) */
1240 struct dahdi_mfcr2_conf mfcr2;
1242 struct dahdi_params timing;
1243 int is_sig_auto; /*!< Use channel signalling from DAHDI? */
1244 /*! Continue configuration even if a channel is not there. */
1245 int ignore_failed_channels;
1248 * \brief The serial port to listen for SMDI data on
1249 * \note Set from the "smdiport" string read in from chan_dahdi.conf
1251 char smdi_port[SMDI_MAX_FILENAME_LEN];
1254 /*! returns a new dahdi_chan_conf with default values (by-value) */
1255 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
1257 /* recall that if a field is not included here it is initialized
1258 * to 0 or equivalent
1260 struct dahdi_chan_conf conf = {
1263 .nsf = PRI_NSF_NONE,
1264 .switchtype = PRI_SWITCH_NI2,
1265 .dialplan = PRI_UNKNOWN + 1,
1266 .localdialplan = PRI_NATIONAL_ISDN + 1,
1267 .nodetype = PRI_CPE,
1268 .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
1270 #if defined(HAVE_PRI_CCSS)
1271 .cc_ptmp_recall_mode = 1,/* specificRecall */
1272 .cc_qsig_signaling_link_req = 1,/* retain */
1273 .cc_qsig_signaling_link_rsp = 1,/* retain */
1274 #endif /* defined(HAVE_PRI_CCSS) */
1279 .internationalprefix = "",
1280 .nationalprefix = "",
1282 .privateprefix = "",
1283 .unknownprefix = "",
1284 .resetinterval = -1,
1287 #if defined(HAVE_SS7)
1289 .called_nai = SS7_NAI_NATIONAL,
1290 .calling_nai = SS7_NAI_NATIONAL,
1291 .internationalprefix = "",
1292 .nationalprefix = "",
1293 .subscriberprefix = "",
1296 #endif /* defined(HAVE_SS7) */
1299 .variant = OR2_VAR_ITU,
1300 .mfback_timeout = -1,
1301 .metering_pulse_timeout = -1,
1304 .get_ani_first = -1,
1305 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
1306 .skip_category_request = -1,
1309 .allow_collect_calls = 0,
1311 .accept_on_offer = 1,
1312 .forced_release = 0,
1314 .immediate_accept = -1,
1317 .loglevel = OR2_LOG_ERROR | OR2_LOG_WARNING,
1318 .category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER
1322 .context = "default",
1326 .mohinterpret = "default",
1329 .transfertobusy = 1,
1331 .cid_signalling = CID_SIG_BELL,
1332 .cid_start = CID_START_RING,
1333 .dahditrcallerid = 0,
1342 .echocancel.head.tap_length = 1,
1350 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1353 .polarityonanswerdelay = 600,
1355 .sendcalleridafter = DEFAULT_CIDRINGS,
1357 .buf_policy = DAHDI_POLICY_IMMEDIATE,
1360 .cc_params = ast_cc_config_params_init(),
1373 .smdi_port = "/dev/ttyS0",
1380 static struct ast_channel *dahdi_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
1381 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
1382 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
1383 static int dahdi_sendtext(struct ast_channel *c, const char *text);
1384 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
1385 static int dahdi_hangup(struct ast_channel *ast);
1386 static int dahdi_answer(struct ast_channel *ast);
1387 static struct ast_frame *dahdi_read(struct ast_channel *ast);
1388 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
1389 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
1390 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
1391 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1392 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
1393 static int dahdi_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
1394 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
1395 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value);
1396 static int dahdi_devicestate(void *data);
1397 static int dahdi_cc_callback(struct ast_channel *inbound, const char *dest, ast_cc_callback_fn callback);
1399 static const struct ast_channel_tech dahdi_tech = {
1401 .description = tdesc,
1402 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
1403 .requester = dahdi_request,
1404 .send_digit_begin = dahdi_digit_begin,
1405 .send_digit_end = dahdi_digit_end,
1406 .send_text = dahdi_sendtext,
1408 .hangup = dahdi_hangup,
1409 .answer = dahdi_answer,
1411 .write = dahdi_write,
1412 .bridge = dahdi_bridge,
1413 .exception = dahdi_exception,
1414 .indicate = dahdi_indicate,
1415 .fixup = dahdi_fixup,
1416 .setoption = dahdi_setoption,
1417 .queryoption = dahdi_queryoption,
1418 .func_channel_read = dahdi_func_read,
1419 .func_channel_write = dahdi_func_write,
1420 .devicestate = dahdi_devicestate,
1421 .cc_callback = dahdi_cc_callback,
1424 #define GET_CHANNEL(p) ((p)->channel)
1426 #define SIG_PRI_LIB_HANDLE_CASES \
1433 * \brief Determine if sig_pri handles the signaling.
1436 * \param signaling Signaling to determine if is for sig_pri.
1438 * \return TRUE if the signaling is for sig_pri.
1440 static inline int dahdi_sig_pri_lib_handles(int signaling)
1444 switch (signaling) {
1445 case SIG_PRI_LIB_HANDLE_CASES:
1456 static enum analog_sigtype dahdisig_to_analogsig(int sig)
1460 return ANALOG_SIG_FXOLS;
1462 return ANALOG_SIG_FXOGS;
1464 return ANALOG_SIG_FXOKS;
1466 return ANALOG_SIG_FXSLS;
1468 return ANALOG_SIG_FXSGS;
1470 return ANALOG_SIG_FXSKS;
1472 return ANALOG_SIG_EMWINK;
1474 return ANALOG_SIG_EM;
1476 return ANALOG_SIG_EM_E1;
1478 return ANALOG_SIG_FEATD;
1480 return ANALOG_SIG_FEATDMF;
1484 return ANALOG_SIG_FGC_CAMA;
1485 case SIG_FGC_CAMAMF:
1486 return ANALOG_SIG_FGC_CAMAMF;
1488 return ANALOG_SIG_FEATB;
1490 return ANALOG_SIG_SFWINK;
1492 return ANALOG_SIG_SF;
1494 return ANALOG_SIG_SF_FEATD;
1495 case SIG_SF_FEATDMF:
1496 return ANALOG_SIG_SF_FEATDMF;
1497 case SIG_FEATDMF_TA:
1498 return ANALOG_SIG_FEATDMF_TA;
1500 return ANALOG_SIG_FEATB;
1507 static int analog_tone_to_dahditone(enum analog_tone tone)
1510 case ANALOG_TONE_RINGTONE:
1511 return DAHDI_TONE_RINGTONE;
1512 case ANALOG_TONE_STUTTER:
1513 return DAHDI_TONE_STUTTER;
1514 case ANALOG_TONE_CONGESTION:
1515 return DAHDI_TONE_CONGESTION;
1516 case ANALOG_TONE_DIALTONE:
1517 return DAHDI_TONE_DIALTONE;
1518 case ANALOG_TONE_DIALRECALL:
1519 return DAHDI_TONE_DIALRECALL;
1520 case ANALOG_TONE_INFO:
1521 return DAHDI_TONE_INFO;
1527 static int analogsub_to_dahdisub(enum analog_sub analogsub)
1531 switch (analogsub) {
1532 case ANALOG_SUB_REAL:
1535 case ANALOG_SUB_CALLWAIT:
1536 index = SUB_CALLWAIT;
1538 case ANALOG_SUB_THREEWAY:
1539 index = SUB_THREEWAY;
1542 ast_log(LOG_ERROR, "Unidentified sub!\n");
1549 static enum analog_event dahdievent_to_analogevent(int event);
1550 static int bump_gains(struct dahdi_pvt *p);
1551 static int dahdi_setlinear(int dfd, int linear);
1553 static int my_start_cid_detect(void *pvt, int cid_signalling)
1555 struct dahdi_pvt *p = pvt;
1556 int index = SUB_REAL;
1557 p->cs = callerid_new(cid_signalling);
1559 ast_log(LOG_ERROR, "Unable to alloc callerid\n");
1563 dahdi_setlinear(p->subs[index].dfd, 0);
1568 static int my_stop_cid_detect(void *pvt)
1570 struct dahdi_pvt *p = pvt;
1571 int index = SUB_REAL;
1573 callerid_free(p->cs);
1574 dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
1578 static int my_get_callerid(void *pvt, char *namebuf, char *numbuf, enum analog_event *ev, size_t timeout)
1580 struct dahdi_pvt *p = pvt;
1581 struct analog_pvt *analog_p = p->sig_pvt;
1582 struct pollfd poller;
1584 int index = SUB_REAL;
1586 unsigned char buf[256];
1589 poller.fd = p->subs[SUB_REAL].dfd;
1590 poller.events = POLLPRI | POLLIN;
1593 res = poll(&poller, 1, timeout);
1595 if (poller.revents & POLLPRI) {
1596 *ev = dahdievent_to_analogevent(dahdi_get_event(p->subs[SUB_REAL].dfd));
1600 if (poller.revents & POLLIN) {
1602 /* Change API: remove cid_signalling from get_callerid, add a new start_cid_detect and stop_cid_detect function
1603 * to enable slin mode and allocate cid detector. get_callerid should be able to be called any number of times until
1604 * either a timeout occurs or CID is detected (returns 0). returning 1 should be event received, and -1 should be
1605 * a failure and die, and returning 2 means no event was received. */
1606 res = read(p->subs[index].dfd, buf, sizeof(buf));
1608 if (errno != ELAST) {
1609 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
1610 callerid_free(p->cs);
1615 if (analog_p->ringt > 0) {
1616 if (!(--analog_p->ringt)) {
1617 /* only return if we timeout from a ring event */
1622 if (p->cid_signalling == CID_SIG_V23_JP) {
1623 res = callerid_feed_jp(p->cs, buf, res, AST_LAW(p));
1625 res = callerid_feed(p->cs, buf, res, AST_LAW(p));
1629 * The previous diagnostic message output likely
1630 * explains why it failed.
1632 ast_log(LOG_WARNING, "Failed to decode CallerID\n");
1637 callerid_get(p->cs, &name, &num, &flags);
1639 ast_copy_string(namebuf, name, ANALOG_MAX_CID);
1641 ast_copy_string(numbuf, num, ANALOG_MAX_CID);
1643 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", num, name, flags);
1648 *ev = ANALOG_EVENT_NONE;
1652 static const char *event2str(int event);
1653 static int restore_gains(struct dahdi_pvt *p);
1655 static int my_distinctive_ring(struct ast_channel *chan, void *pvt, int idx, int *ringdata)
1657 unsigned char buf[256];
1665 int checkaftercid = 0;
1667 struct dahdi_pvt *p = pvt;
1668 struct analog_pvt *analog_p = p->sig_pvt;
1670 if (ringdata == NULL) {
1671 ringdata = curRingData;
1676 /* We must have a ring by now, so, if configured, lets try to listen for
1677 * distinctive ringing */
1678 if ((checkaftercid && distinctiveringaftercid) || !checkaftercid) {
1679 /* Clear the current ring data array so we don't have old data in it. */
1680 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(ringdata); receivedRingT++)
1681 ringdata[receivedRingT] = 0;
1683 if (checkaftercid && distinctiveringaftercid)
1684 ast_verb(3, "Detecting post-CID distinctive ring\n");
1685 /* Check to see if context is what it should be, if not set to be. */
1686 else if (strcmp(p->context,p->defcontext) != 0) {
1687 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
1688 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
1692 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
1693 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
1694 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
1698 if (i & DAHDI_IOMUX_SIGEVENT) {
1699 res = dahdi_get_event(p->subs[idx].dfd);
1700 if (res == DAHDI_EVENT_NOALARM) {
1702 analog_p->inalarm = 0;
1704 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
1706 /* Let us detect distinctive ring */
1708 ringdata[receivedRingT] = analog_p->ringt;
1710 if (analog_p->ringt < analog_p->ringt_base/2)
1712 /* Increment the ringT counter so we can match it against
1713 values in chan_dahdi.conf for distinctive ring */
1714 if (++receivedRingT == ARRAY_LEN(ringdata))
1716 } else if (i & DAHDI_IOMUX_READ) {
1717 res = read(p->subs[idx].dfd, buf, sizeof(buf));
1719 if (errno != ELAST) {
1720 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
1726 if (analog_p->ringt > 0) {
1727 if (!(--analog_p->ringt)) {
1735 if ((checkaftercid && usedistinctiveringdetection) || !checkaftercid) {
1736 /* this only shows up if you have n of the dring patterns filled in */
1737 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",ringdata[0],ringdata[1],ringdata[2]);
1738 for (counter = 0; counter < 3; counter++) {
1739 /* Check to see if the rings we received match any of the ones in chan_dahdi.conf for this channel */
1741 /* this only shows up if you have n of the dring patterns filled in */
1742 ast_verb(3, "Checking %d,%d,%d\n",
1743 p->drings.ringnum[counter].ring[0],
1744 p->drings.ringnum[counter].ring[1],
1745 p->drings.ringnum[counter].ring[2]);
1746 for (counter1 = 0; counter1 < 3; counter1++) {
1747 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
1748 if (p->drings.ringnum[counter].ring[counter1] == -1) {
1749 ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
1750 ringdata[counter1]);
1752 } else if (ringdata[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
1753 ringdata[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
1754 ast_verb(3, "Ring pattern matched in range: %d to %d\n",
1755 (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
1756 (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
1761 if (distMatches == 3) {
1762 /* The ring matches, set the context to whatever is for distinctive ring.. */
1763 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
1764 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
1765 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
1770 /* Restore linear mode (if appropriate) for Caller*ID processing */
1771 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
1777 static int send_callerid(struct dahdi_pvt *p);
1779 static int my_stop_callwait(void *pvt)
1781 struct dahdi_pvt *p = pvt;
1782 p->callwaitingrepeat = 0;
1788 static int save_conference(struct dahdi_pvt *p);
1790 static int my_callwait(void *pvt)
1792 struct dahdi_pvt *p = pvt;
1793 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1795 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1796 ast_free(p->cidspill);
1798 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1802 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1803 if (!p->callwaitrings && p->callwaitingcallerid) {
1804 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1806 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1808 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1810 p->cidlen = 2400 + READ_SIZE * 4;
1818 static int my_send_callerid(void *pvt, int cwcid, struct ast_callerid *cid)
1820 struct dahdi_pvt *p = pvt;
1822 ast_debug(2, "Starting cid spill\n");
1825 ast_log(LOG_WARNING, "cidspill already exists??\n");
1826 ast_free(p->cidspill);
1829 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1831 p->cidlen = ast_callerid_generate(p->cidspill, cid->cid_name, cid->cid_num, AST_LAW(p));
1835 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, cid->cid_name, cid->cid_num, AST_LAW(p));
1836 p->cidlen += READ_SIZE * 4;
1844 static int my_dsp_reset_and_flush_digits(void *pvt)
1846 struct dahdi_pvt *p = pvt;
1848 ast_dsp_digitreset(p->dsp);
1853 static int my_dsp_set_digitmode(void *pvt, enum analog_dsp_digitmode mode)
1855 struct dahdi_pvt *p = pvt;
1857 if (p->channel == CHAN_PSEUDO)
1858 ast_log(LOG_ERROR, "You have assumed incorrectly sir!\n");
1860 if (mode == ANALOG_DIGITMODE_DTMF) {
1861 /* If we do hardware dtmf, no need for a DSP */
1862 if (p->hardwaredtmf) {
1864 ast_dsp_free(p->dsp);
1871 p->dsp = ast_dsp_new();
1873 ast_log(LOG_ERROR, "Unable to allocate DSP\n");
1878 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
1879 } else if (mode == ANALOG_DIGITMODE_MF) {
1881 p->dsp = ast_dsp_new();
1883 ast_log(LOG_ERROR, "Unable to allocate DSP\n");
1887 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
1892 static int dahdi_wink(struct dahdi_pvt *p, int index);
1894 static int my_wink(void *pvt, enum analog_sub sub)
1896 struct dahdi_pvt *p = pvt;
1897 int index = analogsub_to_dahdisub(sub);
1898 if (index != SUB_REAL) {
1899 ast_log(LOG_ERROR, "We used a sub other than SUB_REAL (incorrect assumption sir)\n");
1901 return dahdi_wink(p, index);
1904 static void wakeup_sub(struct dahdi_pvt *p, int a);
1906 static int reset_conf(struct dahdi_pvt *p);
1908 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted);
1910 static void my_handle_dtmfup(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
1912 struct ast_frame *f = *dest;
1913 struct dahdi_pvt *p = pvt;
1914 int idx = analogsub_to_dahdisub(analog_index);
1916 ast_debug(1, "DTMF digit: %c on %s\n", f->subclass.integer, ast->name);
1918 if (f->subclass.integer == 'f') {
1919 /* Fax tone -- Handle and return NULL */
1920 if ((p->callprogress & CALLPROGRESS_FAX) && !p->faxhandled) {
1921 /* If faxbuffers are configured, use them for the fax transmission */
1922 if (p->usefaxbuffers && !p->bufferoverrideinuse) {
1923 struct dahdi_bufferinfo bi = {
1924 .txbufpolicy = p->faxbuf_policy,
1925 .bufsize = p->bufsize,
1926 .numbufs = p->faxbuf_no
1930 if ((res = ioctl(p->subs[idx].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
1931 ast_log(LOG_WARNING, "Channel '%s' unable to set buffer policy, reason: %s\n", ast->name, strerror(errno));
1933 p->bufferoverrideinuse = 1;
1937 if (strcmp(ast->exten, "fax")) {
1938 const char *target_context = S_OR(ast->macrocontext, ast->context);
1940 /* We need to unlock 'ast' here because ast_exists_extension has the
1941 * potential to start autoservice on the channel. Such action is prone
1944 ast_mutex_unlock(&p->lock);
1945 ast_channel_unlock(ast);
1946 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
1947 ast_channel_lock(ast);
1948 ast_mutex_lock(&p->lock);
1949 ast_verb(3, "Redirecting %s to fax extension\n", ast->name);
1950 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
1951 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
1952 if (ast_async_goto(ast, target_context, "fax", 1))
1953 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
1955 ast_channel_lock(ast);
1956 ast_mutex_lock(&p->lock);
1957 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
1960 ast_debug(1, "Already in a fax extension, not redirecting\n");
1963 ast_debug(1, "Fax already handled\n");
1965 dahdi_confmute(p, 0);
1966 p->subs[idx].f.frametype = AST_FRAME_NULL;
1967 p->subs[idx].f.subclass.integer = 0;
1968 *dest = &p->subs[idx].f;
1972 static void my_lock_private(void *pvt)
1974 struct dahdi_pvt *p = pvt;
1975 ast_mutex_lock(&p->lock);
1978 static void my_unlock_private(void *pvt)
1980 struct dahdi_pvt *p = pvt;
1981 ast_mutex_unlock(&p->lock);
1984 /* linear_mode = 0 - turn linear mode off, >0 - turn linear mode on
1985 * returns the last value of the linear setting
1987 static int my_set_linear_mode(void *pvt, int idx, int linear_mode)
1989 struct dahdi_pvt *p = pvt;
1992 if (0 > linear_mode || !dahdi_setlinear(p->subs[idx].dfd, linear_mode)) {
1995 oldval = p->subs[idx].linear;
1996 p->subs[idx].linear = linear_mode;
2000 static int get_alarms(struct dahdi_pvt *p);
2001 static void handle_alarms(struct dahdi_pvt *p, int alms);
2002 static void my_get_and_handle_alarms(void *pvt)
2005 struct dahdi_pvt *p = pvt;
2007 res = get_alarms(p);
2008 handle_alarms(p, res);
2011 static void *my_get_sigpvt_bridged_channel(struct ast_channel *chan)
2013 struct dahdi_pvt *p = ast_bridged_channel(chan)->tech_pvt;
2020 static int my_get_sub_fd(void *pvt, enum analog_sub sub)
2022 struct dahdi_pvt *p = pvt;
2023 int dahdi_sub = analogsub_to_dahdisub(sub);
2024 return p->subs[dahdi_sub].dfd;
2027 static void my_set_cadence(void *pvt, int *cidrings, struct ast_channel *ast)
2029 struct dahdi_pvt *p = pvt;
2031 /* Choose proper cadence */
2032 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2033 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
2034 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
2035 *cidrings = cidrings[p->distinctivering - 1];
2037 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
2038 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
2039 *cidrings = p->sendcalleridafter;
2043 static void my_set_alarm(void *pvt, int in_alarm)
2045 struct dahdi_pvt *p = pvt;
2047 p->inalarm = in_alarm;
2050 static void my_set_dialing(void *pvt, int is_dialing)
2052 struct dahdi_pvt *p = pvt;
2054 p->dialing = is_dialing;
2057 #if defined(HAVE_PRI) || defined(HAVE_SS7)
2058 static void my_set_digital(void *pvt, int is_digital)
2060 struct dahdi_pvt *p = pvt;
2062 p->digital = is_digital;
2064 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
2066 #if defined(HAVE_SS7)
2067 static void my_set_inservice(void *pvt, int is_inservice)
2069 struct dahdi_pvt *p = pvt;
2071 p->inservice = is_inservice;
2073 #endif /* defined(HAVE_SS7) */
2075 #if defined(HAVE_SS7)
2076 static void my_set_locallyblocked(void *pvt, int is_blocked)
2078 struct dahdi_pvt *p = pvt;
2080 p->locallyblocked = is_blocked;
2082 #endif /* defined(HAVE_SS7) */
2084 #if defined(HAVE_SS7)
2085 static void my_set_remotelyblocked(void *pvt, int is_blocked)
2087 struct dahdi_pvt *p = pvt;
2089 p->remotelyblocked = is_blocked;
2091 #endif /* defined(HAVE_SS7) */
2093 static void my_set_ringtimeout(void *pvt, int ringt)
2095 struct dahdi_pvt *p = pvt;
2099 static void my_set_waitingfordt(void *pvt, struct ast_channel *ast)
2101 struct dahdi_pvt *p = pvt;
2103 if (p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp) {
2104 ast_log(LOG_DEBUG, "Defer dialing for %dms or dialtone\n", p->waitfordialtone);
2105 gettimeofday(&p->waitingfordt, NULL);
2106 ast_setstate(ast, AST_STATE_OFFHOOK);
2110 static int my_check_waitingfordt(void *pvt)
2112 struct dahdi_pvt *p = pvt;
2114 if (p->waitingfordt.tv_usec) {
2121 static void my_set_confirmanswer(void *pvt, int flag)
2123 struct dahdi_pvt *p = pvt;
2124 p->confirmanswer = flag;
2127 static int my_check_confirmanswer(void *pvt)
2129 struct dahdi_pvt *p = pvt;
2130 if (p->confirmanswer) {
2137 static void my_cancel_cidspill(void *pvt)
2139 struct dahdi_pvt *p = pvt;
2141 ast_free(p->cidspill);
2146 static int my_confmute(void *pvt, int mute)
2148 struct dahdi_pvt *p = pvt;
2149 return dahdi_confmute(p, mute);
2152 static void my_set_pulsedial(void *pvt, int flag)
2154 struct dahdi_pvt *p = pvt;
2155 p->pulsedial = flag;
2158 static const char *my_get_orig_dialstring(void *pvt)
2160 struct dahdi_pvt *p = pvt;
2162 return p->dialstring;
2165 static void my_increase_ss_count(void)
2167 ast_mutex_lock(&ss_thread_lock);
2169 ast_mutex_unlock(&ss_thread_lock);
2172 static void my_decrease_ss_count(void)
2174 ast_mutex_lock(&ss_thread_lock);
2176 ast_cond_signal(&ss_thread_complete);
2177 ast_mutex_unlock(&ss_thread_lock);
2180 static void my_all_subchannels_hungup(void *pvt)
2182 struct dahdi_pvt *p = pvt;
2189 ast_dsp_free(p->dsp);
2193 p->law = p->law_default;
2194 law = p->law_default;
2195 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
2197 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
2199 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
2205 /* Cleanup owners here */
2206 for (i = 0; i < 3; i++) {
2207 p->subs[i].owner = NULL;
2213 if (num_restart_pending == 0) {
2218 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index);
2220 static int my_conf_del(void *pvt, enum analog_sub sub)
2222 struct dahdi_pvt *p = pvt;
2223 int x = analogsub_to_dahdisub(sub);
2225 return conf_del(p, &p->subs[x], x);
2228 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index, int slavechannel);
2230 static int my_conf_add(void *pvt, enum analog_sub sub)
2232 struct dahdi_pvt *p = pvt;
2233 int x = analogsub_to_dahdisub(sub);
2235 return conf_add(p, &p->subs[x], x, 0);
2238 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out);
2240 static int my_complete_conference_update(void *pvt, int needconference)
2242 struct dahdi_pvt *p = pvt;
2243 int needconf = needconference;
2246 struct dahdi_pvt *slave = NULL;
2248 useslavenative = isslavenative(p, &slave);
2250 /* If we have a slave, add him to our conference now. or DAX
2251 if this is slave native */
2252 for (x = 0; x < MAX_SLAVES; x++) {
2255 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
2257 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
2262 /* If we're supposed to be in there, do so now */
2263 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
2265 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
2267 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
2271 /* If we have a master, add ourselves to his conference */
2273 if (isslavenative(p->master, NULL)) {
2274 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
2276 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
2280 /* Nobody is left (or should be left) in our conference.
2288 static int check_for_conference(struct dahdi_pvt *p);
2290 static int my_check_for_conference(void *pvt)
2292 struct dahdi_pvt *p = pvt;
2293 return check_for_conference(p);
2296 static void my_swap_subchannels(void *pvt, enum analog_sub a, struct ast_channel *ast_a, enum analog_sub b, struct ast_channel *ast_b)
2298 struct dahdi_pvt *p = pvt;
2302 da = analogsub_to_dahdisub(a);
2303 db = analogsub_to_dahdisub(b);
2305 tchan = p->subs[da].chan;
2307 p->subs[da].chan = p->subs[db].chan;
2309 p->subs[db].chan = tchan;
2312 ast_channel_set_fd(ast_a, 0, p->subs[da].dfd);
2314 ast_channel_set_fd(ast_b, 0, p->subs[db].dfd);
2316 p->subs[da].owner = ast_a;
2317 p->subs[db].owner = ast_b;
2325 static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int idx, int law, const char *linkedid);
2327 static struct ast_channel *my_new_analog_ast_channel(void *pvt, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
2329 struct dahdi_pvt *p = pvt;
2330 int dsub = analogsub_to_dahdisub(sub);
2332 return dahdi_new(p, state, startpbx, dsub, 0, requestor ? requestor->linkedid : "");
2335 #if defined(HAVE_PRI) || defined(HAVE_SS7)
2336 static int dahdi_setlaw(int dfd, int law)
2339 res = ioctl(dfd, DAHDI_SETLAW, &law);
2344 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
2346 #if defined(HAVE_PRI)
2347 static struct ast_channel *my_new_pri_ast_channel(void *pvt, int state, enum sig_pri_law law, char *exten, const struct ast_channel *requestor)
2349 struct dahdi_pvt *p = pvt;
2354 case SIG_PRI_LIB_HANDLE_CASES:
2355 if (((struct sig_pri_chan *) p->sig_pvt)->no_b_channel) {
2356 /* PRI nobch pseudo channel. Does not handle ioctl(DAHDI_AUDIOMODE) */
2361 /* Set to audio mode at this point */
2363 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &audio) == -1) {
2364 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n",
2365 p->channel, audio, strerror(errno));
2370 if (law != SIG_PRI_DEFLAW) {
2371 dahdi_setlaw(p->subs[SUB_REAL].dfd, (law == SIG_PRI_ULAW) ? DAHDI_LAW_MULAW : DAHDI_LAW_ALAW);
2374 ast_copy_string(p->exten, exten, sizeof(p->exten));
2377 case SIG_PRI_DEFLAW:
2381 newlaw = DAHDI_LAW_ALAW;
2384 newlaw = DAHDI_LAW_MULAW;
2387 return dahdi_new(p, state, 0, SUB_REAL, newlaw, requestor ? requestor->linkedid : "");
2389 #endif /* defined(HAVE_PRI) */
2391 static int set_actual_gain(int fd, float rxgain, float txgain, float rxdrc, float txdrc, int law);
2393 #if defined(HAVE_PRI)
2396 * \brief Open the PRI channel media path.
2399 * \param p Channel private control structure.
2403 static void my_pri_open_media(void *p)
2405 struct dahdi_pvt *pvt = p;
2410 dfd = pvt->subs[SUB_REAL].dfd;
2412 /* Open the media path. */
2414 res = ioctl(dfd, DAHDI_AUDIOMODE, &set_val);
2416 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n",
2417 pvt->channel, strerror(errno));
2420 /* Set correct companding law for this call. */
2421 res = dahdi_setlaw(dfd, pvt->law);
2423 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pvt->channel);
2426 /* Set correct gain for this call. */
2428 res = set_actual_gain(dfd, 0, 0, pvt->rxdrc, pvt->txdrc, pvt->law);
2430 res = set_actual_gain(dfd, pvt->rxgain, pvt->txgain, pvt->rxdrc, pvt->txdrc,
2434 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pvt->channel);
2437 #endif /* defined(HAVE_PRI) */
2439 static int unalloc_sub(struct dahdi_pvt *p, int x);
2441 static int my_unallocate_sub(void *pvt, enum analog_sub analogsub)
2443 struct dahdi_pvt *p = pvt;
2445 return unalloc_sub(p, analogsub_to_dahdisub(analogsub));
2448 static int alloc_sub(struct dahdi_pvt *p, int x);
2450 static int my_allocate_sub(void *pvt, enum analog_sub analogsub)
2452 struct dahdi_pvt *p = pvt;
2454 return alloc_sub(p, analogsub_to_dahdisub(analogsub));
2457 static int has_voicemail(struct dahdi_pvt *p);
2459 static int my_has_voicemail(void *pvt)
2461 struct dahdi_pvt *p = pvt;
2463 return has_voicemail(p);
2466 static int my_play_tone(void *pvt, enum analog_sub sub, enum analog_tone tone)
2468 struct dahdi_pvt *p = pvt;
2471 index = analogsub_to_dahdisub(sub);
2473 return tone_zone_play_tone(p->subs[index].dfd, analog_tone_to_dahditone(tone));
2476 static enum analog_event dahdievent_to_analogevent(int event)
2478 enum analog_event res;
2481 case DAHDI_EVENT_ONHOOK:
2482 res = ANALOG_EVENT_ONHOOK;
2484 case DAHDI_EVENT_RINGOFFHOOK:
2485 res = ANALOG_EVENT_RINGOFFHOOK;
2487 case DAHDI_EVENT_WINKFLASH:
2488 res = ANALOG_EVENT_WINKFLASH;
2490 case DAHDI_EVENT_ALARM:
2491 res = ANALOG_EVENT_ALARM;
2493 case DAHDI_EVENT_NOALARM:
2494 res = ANALOG_EVENT_NOALARM;
2496 case DAHDI_EVENT_DIALCOMPLETE:
2497 res = ANALOG_EVENT_DIALCOMPLETE;
2499 case DAHDI_EVENT_RINGERON:
2500 res = ANALOG_EVENT_RINGERON;
2502 case DAHDI_EVENT_RINGEROFF:
2503 res = ANALOG_EVENT_RINGEROFF;
2505 case DAHDI_EVENT_HOOKCOMPLETE:
2506 res = ANALOG_EVENT_HOOKCOMPLETE;
2508 case DAHDI_EVENT_PULSE_START:
2509 res = ANALOG_EVENT_PULSE_START;
2511 case DAHDI_EVENT_POLARITY:
2512 res = ANALOG_EVENT_POLARITY;
2514 case DAHDI_EVENT_RINGBEGIN:
2515 res = ANALOG_EVENT_RINGBEGIN;
2517 case DAHDI_EVENT_EC_DISABLED:
2518 res = ANALOG_EVENT_EC_DISABLED;
2520 case DAHDI_EVENT_REMOVED:
2521 res = ANALOG_EVENT_REMOVED;
2523 case DAHDI_EVENT_NEONMWI_ACTIVE:
2524 res = ANALOG_EVENT_NEONMWI_ACTIVE;
2526 case DAHDI_EVENT_NEONMWI_INACTIVE:
2527 res = ANALOG_EVENT_NEONMWI_INACTIVE;
2529 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2530 case DAHDI_EVENT_TX_CED_DETECTED:
2531 res = ANALOG_EVENT_TX_CED_DETECTED;
2533 case DAHDI_EVENT_RX_CED_DETECTED:
2534 res = ANALOG_EVENT_RX_CED_DETECTED;
2536 case DAHDI_EVENT_EC_NLP_DISABLED:
2537 res = ANALOG_EVENT_EC_NLP_DISABLED;
2539 case DAHDI_EVENT_EC_NLP_ENABLED:
2540 res = ANALOG_EVENT_EC_NLP_ENABLED;
2543 case DAHDI_EVENT_PULSEDIGIT:
2544 res = ANALOG_EVENT_PULSEDIGIT;
2546 case DAHDI_EVENT_DTMFDOWN:
2547 res = ANALOG_EVENT_DTMFDOWN;
2549 case DAHDI_EVENT_DTMFUP:
2550 res = ANALOG_EVENT_DTMFUP;
2553 switch(event & 0xFFFF0000) {
2554 case DAHDI_EVENT_PULSEDIGIT:
2555 case DAHDI_EVENT_DTMFDOWN:
2556 case DAHDI_EVENT_DTMFUP:
2557 /* The event includes a digit number in the low word.
2558 * Converting it to a 'enum analog_event' would remove
2559 * that information. Thus it is returned as-is.
2564 res = ANALOG_EVENT_ERROR;
2571 static inline int dahdi_wait_event(int fd);
2573 static int my_wait_event(void *pvt)
2575 struct dahdi_pvt *p = pvt;
2577 return dahdi_wait_event(p->subs[SUB_REAL].dfd);
2580 static int my_get_event(void *pvt)
2582 struct dahdi_pvt *p = pvt;
2585 if (p->fake_event) {
2586 res = p->fake_event;
2589 res = dahdi_get_event(p->subs[SUB_REAL].dfd);
2591 return dahdievent_to_analogevent(res);
2594 static int my_is_off_hook(void *pvt)
2596 struct dahdi_pvt *p = pvt;
2598 struct dahdi_params par;
2600 memset(&par, 0, sizeof(par));
2602 if (p->subs[SUB_REAL].dfd > -1)
2603 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
2605 /* Assume not off hook on CVRS */
2607 par.rxisoffhook = 0;
2610 ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno));
2613 if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
2614 /* When "onhook" that means no battery on the line, and thus
2615 it is out of service..., if it's on a TDM card... If it's a channel
2616 bank, there is no telling... */
2617 return (par.rxbits > -1) || par.rxisoffhook;
2620 return par.rxisoffhook;
2623 static void dahdi_enable_ec(struct dahdi_pvt *p);
2624 static void dahdi_disable_ec(struct dahdi_pvt *p);
2626 static int my_set_echocanceller(void *pvt, int enable)
2628 struct dahdi_pvt *p = pvt;
2633 dahdi_disable_ec(p);
2638 static int dahdi_ring_phone(struct dahdi_pvt *p);
2640 static int my_ring(void *pvt)
2642 struct dahdi_pvt *p = pvt;
2644 return dahdi_ring_phone(p);
2647 static int my_flash(void *pvt)
2649 struct dahdi_pvt *p = pvt;
2650 int func = DAHDI_FLASH;
2651 return ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &func);
2654 static inline int dahdi_set_hook(int fd, int hs);
2656 static int my_off_hook(void *pvt)
2658 struct dahdi_pvt *p = pvt;
2659 return dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
2662 static void my_set_needringing(void *pvt, int value)
2664 struct dahdi_pvt *p = pvt;
2665 p->subs[SUB_REAL].needringing = value;
2668 static int my_start(void *pvt)
2670 struct dahdi_pvt *p = pvt;
2671 int x = DAHDI_START;
2673 return ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
2676 static int my_dial_digits(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop)
2678 int index = analogsub_to_dahdisub(sub);
2680 struct dahdi_pvt *p = pvt;
2681 struct dahdi_dialoperation ddop;
2683 if (dop->op != ANALOG_DIAL_OP_REPLACE) {
2684 ast_log(LOG_ERROR, "Fix the dial_digits callback!\n");
2688 if (sub != ANALOG_SUB_REAL)
2689 printf("Trying to dial digits on sub %d\n", sub);
2691 ddop.op = DAHDI_DIAL_OP_REPLACE;
2692 strncpy(ddop.dialstr, dop->dialstr, sizeof(ddop.dialstr));
2694 printf("Dialing %s on %d\n", ddop.dialstr, p->channel);
2696 res = ioctl(p->subs[index].dfd, DAHDI_DIAL, &ddop);
2699 ast_log(LOG_DEBUG, "DAHDI_DIAL ioctl failed on %s: %s\n", p->owner->name, strerror(errno));
2704 static void dahdi_train_ec(struct dahdi_pvt *p);
2706 static int my_train_echocanceller(void *pvt)
2708 struct dahdi_pvt *p = pvt;
2715 static int my_is_dialing(void *pvt, enum analog_sub sub)
2717 struct dahdi_pvt *p = pvt;
2721 index = analogsub_to_dahdisub(sub);
2723 if (ioctl(p->subs[index].dfd, DAHDI_DIALING, &x)) {
2724 ast_log(LOG_DEBUG, "DAHDI_DIALING ioctl failed!\n");
2731 static int my_on_hook(void *pvt)
2733 struct dahdi_pvt *p = pvt;
2734 return dahdi_set_hook(p->subs[ANALOG_SUB_REAL].dfd, DAHDI_ONHOOK);
2737 #if defined(HAVE_PRI)
2738 static void my_pri_fixup_chans(void *chan_old, void *chan_new)
2740 struct dahdi_pvt *old_chan = chan_old;
2741 struct dahdi_pvt *new_chan = chan_new;
2743 new_chan->owner = old_chan->owner;
2744 old_chan->owner = NULL;
2745 if (new_chan->owner) {
2746 new_chan->owner->tech_pvt = new_chan;
2747 new_chan->owner->fds[0] = new_chan->subs[SUB_REAL].dfd;
2748 new_chan->subs[SUB_REAL].owner = old_chan->subs[SUB_REAL].owner;
2749 old_chan->subs[SUB_REAL].owner = NULL;
2751 /* Copy any DSP that may be present */
2752 new_chan->dsp = old_chan->dsp;
2753 new_chan->dsp_features = old_chan->dsp_features;
2754 old_chan->dsp = NULL;
2755 old_chan->dsp_features = 0;
2757 /* Transfer flags from the old channel. */
2758 new_chan->dialing = old_chan->dialing;
2759 new_chan->digital = old_chan->digital;
2760 new_chan->outgoing = old_chan->outgoing;
2761 old_chan->dialing = 0;
2762 old_chan->digital = 0;
2763 old_chan->outgoing = 0;
2765 /* More stuff to transfer to the new channel. */
2766 new_chan->law = old_chan->law;
2768 #endif /* defined(HAVE_PRI) */
2770 #if defined(HAVE_PRI)
2771 static int sig_pri_tone_to_dahditone(enum sig_pri_tone tone)
2774 case SIG_PRI_TONE_RINGTONE:
2775 return DAHDI_TONE_RINGTONE;
2776 case SIG_PRI_TONE_STUTTER:
2777 return DAHDI_TONE_STUTTER;
2778 case SIG_PRI_TONE_CONGESTION:
2779 return DAHDI_TONE_CONGESTION;
2780 case SIG_PRI_TONE_DIALTONE:
2781 return DAHDI_TONE_DIALTONE;
2782 case SIG_PRI_TONE_DIALRECALL:
2783 return DAHDI_TONE_DIALRECALL;
2784 case SIG_PRI_TONE_INFO:
2785 return DAHDI_TONE_INFO;
2786 case SIG_PRI_TONE_BUSY:
2787 return DAHDI_TONE_BUSY;
2792 #endif /* defined(HAVE_PRI) */
2794 #if defined(HAVE_PRI)
2795 static void my_handle_dchan_exception(struct sig_pri_pri *pri, int index)
2799 res = ioctl(pri->fds[index], DAHDI_GETEVENT, &x);
2801 ast_log(LOG_NOTICE, "PRI got event: %s (%d) on D-channel of span %d\n", event2str(x), x, pri->span);
2803 /* Keep track of alarm state */
2805 case DAHDI_EVENT_ALARM:
2806 pri_event_alarm(pri, index, 0);
2808 case DAHDI_EVENT_NOALARM:
2809 pri_event_noalarm(pri, index, 0);
2815 #endif /* defined(HAVE_PRI) */
2817 #if defined(HAVE_PRI)
2818 static int my_pri_play_tone(void *pvt, enum sig_pri_tone tone)
2820 struct dahdi_pvt *p = pvt;
2822 return tone_zone_play_tone(p->subs[SUB_REAL].dfd, sig_pri_tone_to_dahditone(tone));
2824 #endif /* defined(HAVE_PRI) */
2826 #if defined(HAVE_PRI) || defined(HAVE_SS7)
2829 * \brief Set the caller id information.
2832 * \param pvt DAHDI private structure
2833 * \param caller Caller-id information to set.
2837 static void my_set_callerid(void *pvt, const struct ast_party_caller *caller)
2839 struct dahdi_pvt *p = pvt;
2841 ast_copy_string(p->cid_num, S_OR(caller->id.number, ""), sizeof(p->cid_num));
2842 ast_copy_string(p->cid_name, S_OR(caller->id.name, ""), sizeof(p->cid_name));
2843 if (caller->id.subaddress.valid) {
2844 ast_copy_string(p->cid_subaddr, S_OR(caller->id.subaddress.str, ""),
2845 sizeof(p->cid_subaddr));
2847 p->cid_subaddr[0] = '\0';
2849 p->cid_ton = caller->id.number_type;
2850 p->callingpres = caller->id.number_presentation;
2851 if (caller->id.tag) {
2852 ast_copy_string(p->cid_tag, caller->id.tag, sizeof(p->cid_tag));
2854 ast_copy_string(p->cid_ani, S_OR(caller->ani, ""), sizeof(p->cid_ani));
2855 p->cid_ani2 = caller->ani2;
2857 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
2859 #if defined(HAVE_PRI) || defined(HAVE_SS7)
2862 * \brief Set the Dialed Number Identifier.
2865 * \param pvt DAHDI private structure
2866 * \param dnid Dialed Number Identifier string.
2870 static void my_set_dnid(void *pvt, const char *dnid)
2872 struct dahdi_pvt *p = pvt;
2874 ast_copy_string(p->dnid, dnid, sizeof(p->dnid));
2876 #endif /* defined(HAVE_PRI) || defined(HAVE_SS7) */
2878 #if defined(HAVE_PRI)
2881 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
2884 * \param pvt DAHDI private structure
2885 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
2889 static void my_set_rdnis(void *pvt, const char *rdnis)
2891 struct dahdi_pvt *p = pvt;
2893 ast_copy_string(p->rdnis, rdnis, sizeof(p->rdnis));
2895 #endif /* defined(HAVE_PRI) */
2897 #if defined(HAVE_PRI)
2900 * \brief Make a dialstring for native ISDN CC to recall properly.
2903 * \param priv Channel private control structure.
2904 * \param buf Where to put the modified dialstring.
2905 * \param buf_size Size of modified dialstring buffer.
2908 * original dialstring:
2909 * DAHDI/[i<span>-](g|G|r|R)<group#(0-63)>[c|r<cadance#>|d][/extension[/options]]
2911 * The modified dialstring will have prefixed the channel-group section
2912 * with the ISDN channel restriction.
2915 * DAHDI/i<span>-(g|G|r|R)<group#(0-63)>[c|r<cadance#>|d][/extension[/options]]
2917 * The routine will check to see if the ISDN channel restriction is already
2918 * in the original dialstring.
2922 static void my_pri_make_cc_dialstring(void *priv, char *buf, size_t buf_size)
2925 struct dahdi_pvt *pvt;
2926 AST_DECLARE_APP_ARGS(args,
2927 AST_APP_ARG(tech); /* channel technology token */
2928 AST_APP_ARG(group); /* channel/group token */
2929 //AST_APP_ARG(ext); /* extension token */
2930 //AST_APP_ARG(opts); /* options token */
2931 //AST_APP_ARG(other); /* Any remining unused arguments */
2935 dial = ast_strdupa(pvt->dialstring);
2936 AST_NONSTANDARD_APP_ARGS(args, dial, '/');
2938 ast_copy_string(buf, pvt->dialstring, buf_size);
2942 /* Append the ISDN span channel restriction to the dialstring. */
2943 snprintf(buf, buf_size, "%s/i%d-", args.tech, pvt->pri->span);
2946 if (isdigit(args.group[0]) || args.group[0] == 'i' || strchr(args.group, '!')) {
2947 /* The ISDN span channel restriction is not needed or already
2948 * in the dialstring. */
2949 ast_copy_string(buf, pvt->dialstring, buf_size);
2952 /* Insert the ISDN span channel restriction into the dialstring. */
2953 snprintf(buf, buf_size, "%s/i%d-%s", args.tech, pvt->pri->span, args.group);
2955 #endif /* defined(HAVE_PRI) */
2957 #if defined(HAVE_PRI)
2960 * \brief Reevaluate the PRI span device state.
2963 * \param pri Asterisk D channel control structure.
2967 * \note Assumes the pri->lock is already obtained.
2969 static void dahdi_pri_update_span_devstate(struct sig_pri_pri *pri)
2972 unsigned num_b_chans; /* Number of B channels provisioned on the span. */
2973 unsigned in_use; /* Number of B channels in use on the span. */
2974 unsigned in_alarm; /* TRUE if the span is in alarm condition. */
2975 enum ast_device_state new_state;
2977 /* Count the number of B channels and the number of B channels in use. */
2981 for (idx = pri->numchans; idx--;) {
2982 if (pri->pvts[idx] && !pri->pvts[idx]->no_b_channel) {
2983 /* This is a B channel interface. */
2985 if (pri->pvts[idx]->owner
2986 #if defined(HAVE_PRI_SERVICE_MESSAGES)
2987 /* Out-of-service B channels are "in-use". */
2988 && pri->pvts[idx]->service_status
2989 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
2993 if (!pri->pvts[idx]->inalarm) {
2994 /* There is a channel that is not in alarm. */
3000 /* Update the span congestion device state and report any change. */
3002 new_state = AST_DEVICE_UNAVAILABLE;
3004 new_state = num_b_chans == in_use ? AST_DEVICE_BUSY : AST_DEVICE_NOT_INUSE;
3006 if (pri->congestion_devstate != new_state) {
3007 pri->congestion_devstate = new_state;
3008 ast_devstate_changed(AST_DEVICE_UNKNOWN, "DAHDI/I%d/congestion", pri->span);
3010 #if defined(THRESHOLD_DEVSTATE_PLACEHOLDER)
3011 /* Update the span threshold device state and report any change. */
3013 new_state = AST_DEVICE_UNAVAILABLE;
3014 } else if (!in_use) {
3015 new_state = AST_DEVICE_NOT_INUSE;
3016 } else if (!pri->user_busy_threshold) {
3017 new_state = in_use < num_b_chans ? AST_DEVICE_INUSE : AST_DEVICE_BUSY;
3019 new_state = in_use < pri->user_busy_threshold ? AST_DEVICE_INUSE
3022 if (pri->threshold_devstate != new_state) {
3023 pri->threshold_devstate = new_state;
3024 ast_devstate_changed(AST_DEVICE_UNKNOWN, "DAHDI/I%d/threshold", pri->span);
3026 #endif /* defined(THRESHOLD_DEVSTATE_PLACEHOLDER) */
3028 #endif /* defined(HAVE_PRI) */
3030 #if defined(HAVE_PRI)
3033 * \brief Reference this module.
3038 static void my_module_ref(void)
3040 ast_module_ref(ast_module_info->self);
3042 #endif /* defined(HAVE_PRI) */
3044 #if defined(HAVE_PRI)
3047 * \brief Unreference this module.
3052 static void my_module_unref(void)
3054 ast_module_unref(ast_module_info->self);
3056 #endif /* defined(HAVE_PRI) */
3058 #if defined(HAVE_PRI)
3059 #if defined(HAVE_PRI_CALL_WAITING)
3060 static void my_pri_init_config(void *priv, struct sig_pri_pri *pri);
3061 #endif /* defined(HAVE_PRI_CALL_WAITING) */
3062 static int dahdi_new_pri_nobch_channel(struct sig_pri_pri *pri);
3064 static struct sig_pri_callback dahdi_pri_callbacks =
3066 .handle_dchan_exception = my_handle_dchan_exception,
3067 .play_tone = my_pri_play_tone,
3068 .set_echocanceller = my_set_echocanceller,
3069 .dsp_reset_and_flush_digits = my_dsp_reset_and_flush_digits,
3070 .lock_private = my_lock_private,
3071 .unlock_private = my_unlock_private,
3072 .new_ast_channel = my_new_pri_ast_channel,
3073 .fixup_chans = my_pri_fixup_chans,
3074 .set_alarm = my_set_alarm,
3075 .set_dialing = my_set_dialing,
3076 .set_digital = my_set_digital,
3077 .set_callerid = my_set_callerid,
3078 .set_dnid = my_set_dnid,
3079 .set_rdnis = my_set_rdnis,
3080 .new_nobch_intf = dahdi_new_pri_nobch_channel,
3081 #if defined(HAVE_PRI_CALL_WAITING)
3082 .init_config = my_pri_init_config,
3083 #endif /* defined(HAVE_PRI_CALL_WAITING) */
3084 .get_orig_dialstring = my_get_orig_dialstring,
3085 .make_cc_dialstring = my_pri_make_cc_dialstring,
3086 .update_span_devstate = dahdi_pri_update_span_devstate,
3087 .module_ref = my_module_ref,
3088 .module_unref = my_module_unref,
3089 .open_media = my_pri_open_media,
3091 #endif /* defined(HAVE_PRI) */
3093 #if defined(HAVE_SS7)
3096 * \brief Handle the SS7 link exception.
3099 * \param linkset Controlling linkset for the channel.
3100 * \param which Link index of the signaling channel.
3104 static void my_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
3108 if (ioctl(linkset->fds[which], DAHDI_GETEVENT, &event)) {
3109 ast_log(LOG_ERROR, "SS7: Error in exception retrieval on span %d/%d!\n",
3110 linkset->span, which);
3114 case DAHDI_EVENT_NONE:
3116 case DAHDI_EVENT_ALARM:
3117 ast_log(LOG_ERROR, "SS7 got event: %s(%d) on span %d/%d\n",
3118 event2str(event), event, linkset->span, which);
3119 sig_ss7_link_alarm(linkset, which);
3121 case DAHDI_EVENT_NOALARM:
3122 ast_log(LOG_ERROR, "SS7 got event: %s(%d) on span %d/%d\n",
3123 event2str(event), event, linkset->span, which);
3124 sig_ss7_link_noalarm(linkset, which);
3127 ast_log(LOG_NOTICE, "SS7 got event: %s(%d) on span %d/%d\n",
3128 event2str(event), event, linkset->span, which);
3132 #endif /* defined(HAVE_SS7) */
3134 #if defined(HAVE_SS7)
3135 static void my_ss7_set_loopback(void *pvt, int enable)
3137 struct dahdi_pvt *p = pvt;
3139 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_LOOPBACK, &enable)) {
3140 ast_log(LOG_WARNING, "Unable to set loopback on channel %d: %s\n", p->channel,
3144 #endif /* defined(HAVE_SS7) */
3146 #if defined(HAVE_SS7)
3149 * \brief Create a new asterisk channel structure for SS7.
3152 * \param pvt Private channel structure.
3153 * \param state Initial state of new channel.
3154 * \param law Combanding law to use.
3155 * \param exten Dialplan extension for incoming call.
3156 * \param requestor Channel requesting this new channel.
3158 * \retval ast_channel on success.
3159 * \retval NULL on error.
3161 static struct ast_channel *my_new_ss7_ast_channel(void *pvt, int state, enum sig_ss7_law law, char *exten, const struct ast_channel *requestor)
3163 struct dahdi_pvt *p = pvt;
3167 /* Set to audio mode at this point */
3169 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &audio) == -1)
3170 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n",
3171 p->channel, audio, strerror(errno));
3173 if (law != SIG_SS7_DEFLAW) {
3174 dahdi_setlaw(p->subs[SUB_REAL].dfd,
3175 (law == SIG_SS7_ULAW) ? DAHDI_LAW_MULAW : DAHDI_LAW_ALAW);
3178 ast_copy_string(p->exten, exten, sizeof(p->exten));
3182 case SIG_SS7_DEFLAW:
3186 newlaw = DAHDI_LAW_ALAW;
3189 newlaw = DAHDI_LAW_MULAW;
3192 return dahdi_new(p, state, 0, SUB_REAL, newlaw, requestor ? requestor->linkedid : "");
3194 #endif /* defined(HAVE_SS7) */
3196 #if defined(HAVE_SS7)
3197 static int sig_ss7_tone_to_dahditone(enum sig_ss7_tone tone)
3200 case SIG_SS7_TONE_RINGTONE:
3201 return DAHDI_TONE_RINGTONE;
3202 case SIG_SS7_TONE_STUTTER:
3203 return DAHDI_TONE_STUTTER;
3204 case SIG_SS7_TONE_CONGESTION:
3205 return DAHDI_TONE_CONGESTION;
3206 case SIG_SS7_TONE_DIALTONE:
3207 return DAHDI_TONE_DIALTONE;
3208 case SIG_SS7_TONE_DIALRECALL:
3209 return DAHDI_TONE_DIALRECALL;
3210 case SIG_SS7_TONE_INFO:
3211 return DAHDI_TONE_INFO;
3212 case SIG_SS7_TONE_BUSY:
3213 return DAHDI_TONE_BUSY;
3218 #endif /* defined(HAVE_SS7) */
3220 #if defined(HAVE_SS7)
3221 static int my_ss7_play_tone(void *pvt, enum sig_ss7_tone tone)
3223 struct dahdi_pvt *p = pvt;
3225 return tone_zone_play_tone(p->subs[SUB_REAL].dfd, sig_ss7_tone_to_dahditone(tone));
3227 #endif /* defined(HAVE_SS7) */
3229 #if defined(HAVE_SS7)
3230 static struct sig_ss7_callback dahdi_ss7_callbacks =
3232 .lock_private = my_lock_private,
3233 .unlock_private = my_unlock_private,
3235 .set_echocanceller = my_set_echocanceller,
3236 .set_loopback = my_ss7_set_loopback,
3238 .new_ast_channel = my_new_ss7_ast_channel,
3239 .play_tone = my_ss7_play_tone,
3241 .handle_link_exception = my_handle_link_exception,
3242 .set_alarm = my_set_alarm,
3243 .set_dialing = my_set_dialing,
3244 .set_digital = my_set_digital,
3245 .set_inservice = my_set_inservice,
3246 .set_locallyblocked = my_set_locallyblocked,
3247 .set_remotelyblocked = my_set_remotelyblocked,
3248 .set_callerid = my_set_callerid,
3249 .set_dnid = my_set_dnid,
3251 #endif /* defined(HAVE_SS7) */
3254 * \brief Send MWI state change
3256 * \arg mailbox_full This is the mailbox associated with the FXO line that the
3257 * MWI state has changed on.