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$")
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.
78 /* put this here until sig_ss7 comes along */
79 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
80 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
88 #include "asterisk/lock.h"
89 #include "asterisk/channel.h"
90 #include "asterisk/config.h"
91 #include "asterisk/module.h"
92 #include "asterisk/pbx.h"
93 #include "asterisk/file.h"
94 #include "asterisk/ulaw.h"
95 #include "asterisk/alaw.h"
96 #include "asterisk/callerid.h"
97 #include "asterisk/adsi.h"
98 #include "asterisk/cli.h"
99 #include "asterisk/cdr.h"
100 #include "asterisk/cel.h"
101 #include "asterisk/features.h"
102 #include "asterisk/musiconhold.h"
103 #include "asterisk/say.h"
104 #include "asterisk/tdd.h"
105 #include "asterisk/app.h"
106 #include "asterisk/dsp.h"
107 #include "asterisk/astdb.h"
108 #include "asterisk/manager.h"
109 #include "asterisk/causes.h"
110 #include "asterisk/term.h"
111 #include "asterisk/utils.h"
112 #include "asterisk/transcap.h"
113 #include "asterisk/stringfields.h"
114 #include "asterisk/abstract_jb.h"
115 #include "asterisk/smdi.h"
116 #include "asterisk/astobj.h"
117 #include "asterisk/event.h"
118 #include "asterisk/devicestate.h"
119 #include "asterisk/paths.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,
274 static struct ast_jb_conf global_jbconf;
277 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
278 * the user hangs up to reset the state machine so ring works properly.
279 * This is used to be able to support kewlstart by putting the zhone in
280 * groundstart mode since their forward disconnect supervision is entirely
281 * broken even though their documentation says it isn't and their support
282 * is entirely unwilling to provide any assistance with their channel banks
283 * even though their web site says they support their products for life.
285 /* #define ZHONE_HACK */
288 * Define if you want to check the hook state for an FXO (FXS signalled) interface
289 * before dialing on it. Certain FXO interfaces always think they're out of
290 * service with this method however.
292 /* #define DAHDI_CHECK_HOOKSTATE */
294 /*! \brief Typically, how many rings before we should send Caller*ID */
295 #define DEFAULT_CIDRINGS 1
297 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
300 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
301 #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))
303 static const char tdesc[] = "DAHDI Telephony Driver"
304 #if defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2)
318 #if defined(HAVE_PRI) || defined(HAVE_SS7)
326 static const char config[] = "chan_dahdi.conf";
328 #define SIG_EM DAHDI_SIG_EM
329 #define SIG_EMWINK (0x0100000 | DAHDI_SIG_EM)
330 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
331 #define SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
332 #define SIG_FEATB (0x0800000 | DAHDI_SIG_EM)
333 #define SIG_E911 (0x1000000 | DAHDI_SIG_EM)
334 #define SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
335 #define SIG_FGC_CAMA (0x4000000 | DAHDI_SIG_EM)
336 #define SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
337 #define SIG_FXSLS DAHDI_SIG_FXSLS
338 #define SIG_FXSGS DAHDI_SIG_FXSGS
339 #define SIG_FXSKS DAHDI_SIG_FXSKS
340 #define SIG_FXOLS DAHDI_SIG_FXOLS
341 #define SIG_FXOGS DAHDI_SIG_FXOGS
342 #define SIG_FXOKS DAHDI_SIG_FXOKS
343 #define SIG_PRI DAHDI_SIG_CLEAR
344 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
345 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
346 #define SIG_SS7 (0x1000000 | DAHDI_SIG_CLEAR)
347 #define SIG_MFCR2 DAHDI_SIG_CAS
348 #define SIG_SF DAHDI_SIG_SF
349 #define SIG_SFWINK (0x0100000 | DAHDI_SIG_SF)
350 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
351 #define SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
352 #define SIG_SF_FEATB (0x0800000 | DAHDI_SIG_SF)
353 #define SIG_EM_E1 DAHDI_SIG_EM_E1
356 #define NUM_SPANS DAHDI_MAX_SPANS
361 #define CHAN_PSEUDO -2
363 #define CALLPROGRESS_PROGRESS 1
364 #define CALLPROGRESS_FAX_OUTGOING 2
365 #define CALLPROGRESS_FAX_INCOMING 4
366 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
368 #define NUM_CADENCE_MAX 25
369 static int num_cadence = 4;
370 static int user_has_defined_cadences = 0;
372 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
373 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
374 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
375 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
376 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
379 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
380 * is 1, the second pause is 2 and so on.
383 static int cidrings[NUM_CADENCE_MAX] = {
384 2, /*!< Right after first long ring */
385 4, /*!< Right after long part */
386 3, /*!< After third chirp */
387 2, /*!< Second spell */
390 /* ETSI EN300 659-1 specifies the ring pulse between 200 and 300 mS */
391 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
393 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
394 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
396 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
397 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
399 static char defaultcic[64] = "";
400 static char defaultozz[64] = "";
402 static char parkinglot[AST_MAX_EXTENSION] = ""; /*!< Default parking lot for this channel */
404 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
405 static char mwimonitornotify[PATH_MAX] = "";
406 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
407 static int mwisend_rpas = 0;
410 static char progzone[10] = "";
412 static int usedistinctiveringdetection = 0;
413 static int distinctiveringaftercid = 0;
415 static int numbufs = 4;
417 static int mwilevel = 512;
418 static int dtmfcid_level = 256;
421 #ifdef PRI_GETSET_TIMERS
422 static int pritimers[PRI_MAX_TIMERS];
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;
517 #define LINKSTATE_INALARM (1 << 0)
518 #define LINKSTATE_STARTING (1 << 1)
519 #define LINKSTATE_UP (1 << 2)
520 #define LINKSTATE_DOWN (1 << 3)
522 #define SS7_NAI_DYNAMIC -1
524 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
527 pthread_t master; /*!< Thread of master */
531 int linkstate[NUM_DCHANS];
535 LINKSET_STATE_DOWN = 0,
538 char called_nai; /*!< Called Nature of Address Indicator */
539 char calling_nai; /*!< Calling Nature of Address Indicator */
540 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
541 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
542 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
543 char unknownprefix[20]; /*!< for unknown dialplans */
545 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
546 int flags; /*!< Linkset flags */
549 static struct dahdi_ss7 linksets[NUM_SPANS];
551 static int cur_ss7type = -1;
552 static int cur_linkset = -1;
553 static int cur_pointcode = -1;
554 static int cur_cicbeginswith = -1;
555 static int cur_adjpointcode = -1;
556 static int cur_networkindicator = -1;
557 static int cur_defaultdpc = -1;
558 #endif /* HAVE_SS7 */
562 pthread_t r2master; /*!< Thread of master */
563 openr2_context_t *protocol_context; /*!< OpenR2 context handle */
564 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
565 int numchans; /*!< Number of channels in this R2 block */
566 int monitored_count; /*!< Number of channels being monitored */
569 struct dahdi_mfcr2_conf {
570 openr2_variant_t variant;
572 int metering_pulse_timeout;
576 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
577 int skip_category_request:1;
580 int allow_collect_calls:1;
582 int accept_on_offer:1;
583 int forced_release:1;
585 int immediate_accept:1;
586 char logdir[OR2_MAX_PATH];
587 char r2proto_file[OR2_MAX_PATH];
588 openr2_log_level_t loglevel;
589 openr2_calling_party_category_t category;
592 /* malloc'd array of malloc'd r2links */
593 static struct dahdi_mfcr2 **r2links;
594 /* how many r2links have been malloc'd */
595 static int r2links_count = 0;
597 #endif /* HAVE_OPENR2 */
602 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
603 int mastertrunkgroup; /*!< What trunk group is our master */
604 int prilogicalspan; /*!< Logical span number within trunk group */
605 struct sig_pri_pri pri;
608 static struct dahdi_pri pris[NUM_SPANS];
611 /*! Shut up the compiler */
615 #define SUB_REAL 0 /*!< Active call */
616 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
617 #define SUB_THREEWAY 2 /*!< Three-way call */
619 /* Polarity states */
620 #define POLARITY_IDLE 0
621 #define POLARITY_REV 1
624 struct distRingData {
628 struct ringContextData {
629 char contextData[AST_MAX_CONTEXT];
631 struct dahdi_distRings {
632 struct distRingData ringnum[3];
633 struct ringContextData ringContext[3];
636 static const char * const subnames[] = {
642 struct dahdi_subchannel {
644 struct ast_channel *owner;
646 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
647 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
648 unsigned int needringing:1;
649 unsigned int needbusy:1;
650 unsigned int needcongestion:1;
651 unsigned int needanswer:1;
652 unsigned int needflash:1;
653 unsigned int needhold:1;
654 unsigned int needunhold:1;
655 unsigned int linear:1;
656 unsigned int inthreeway:1;
657 struct dahdi_confinfo curconf;
660 #define CONF_USER_REAL (1 << 0)
661 #define CONF_USER_THIRDCALL (1 << 1)
665 /* States for sending MWI message
666 * First three states are required for send Ring Pulse Alert Signal
678 struct mwisend_info {
679 struct timeval pause;
680 mwisend_states mwisend_current;
683 /*! Specify the lists dahdi_pvt can be put in. */
685 DAHDI_IFLIST_NONE, /*!< The dahdi_pvt is not in any list. */
686 DAHDI_IFLIST_MAIN, /*!< The dahdi_pvt is in the main interface list */
687 #if defined(HAVE_PRI)
688 DAHDI_IFLIST_NO_B_CHAN, /*!< The dahdi_pvt is in a no B channel interface list */
689 #endif /* defined(HAVE_PRI) */
694 struct callerid_state *cs;
695 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
696 /*!< Up to three channels can be associated with this call */
698 struct dahdi_subchannel sub_unused; /*!< Just a safety precaution */
699 struct dahdi_subchannel subs[3]; /*!< Sub-channels */
700 struct dahdi_confinfo saveconf; /*!< Saved conference info */
702 struct dahdi_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
703 struct dahdi_pvt *master; /*!< Master to us (we follow their conferencing) */
704 int inconference; /*!< If our real should be in the conference */
706 int bufsize; /*!< Size of the buffers */
707 int buf_no; /*!< Number of buffers */
708 int buf_policy; /*!< Buffer policy */
709 int faxbuf_no; /*!< Number of Fax buffers */
710 int faxbuf_policy; /*!< Fax buffer policy */
711 int sig; /*!< Signalling style */
713 * \brief Nonzero if the signaling type is sent over a radio.
714 * \note Set to a couple of nonzero values but it is only tested like a boolean.
717 int outsigmod; /*!< Outbound Signalling style (modifier) */
718 int oprmode; /*!< "Operator Services" mode */
719 struct dahdi_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
720 /*! \brief Amount of gain to increase during caller id */
722 /*! \brief Rx gain set by chan_dahdi.conf */
724 /*! \brief Tx gain set by chan_dahdi.conf */
727 float txdrc; /*!< Dynamic Range Compression factor. a number between 1 and 6ish */
730 int tonezone; /*!< tone zone for this chan, or -1 for default */
731 enum DAHDI_IFLIST which_iflist; /*!< Which interface list is this structure listed? */
732 struct dahdi_pvt *next; /*!< Next channel in list */
733 struct dahdi_pvt *prev; /*!< Prev channel in list */
738 * \brief TRUE if ADSI (Analog Display Services Interface) available
739 * \note Set from the "adsi" value read in from chan_dahdi.conf
743 * \brief TRUE if we can use a polarity reversal to mark when an outgoing
744 * call is answered by the remote party.
745 * \note Set from the "answeronpolarityswitch" value read in from chan_dahdi.conf
747 unsigned int answeronpolarityswitch:1;
749 * \brief TRUE if busy detection is enabled.
750 * (Listens for the beep-beep busy pattern.)
751 * \note Set from the "busydetect" value read in from chan_dahdi.conf
753 unsigned int busydetect:1;
755 * \brief TRUE if call return is enabled.
756 * (*69, if your dialplan doesn't catch this first)
757 * \note Set from the "callreturn" value read in from chan_dahdi.conf
759 unsigned int callreturn:1;
761 * \brief TRUE if busy extensions will hear the call-waiting tone
762 * and can use hook-flash to switch between callers.
763 * \note Can be disabled by dialing *70.
764 * \note Initialized with the "callwaiting" value read in from chan_dahdi.conf
766 unsigned int callwaiting:1;
768 * \brief TRUE if send caller ID for Call Waiting
769 * \note Set from the "callwaitingcallerid" value read in from chan_dahdi.conf
771 unsigned int callwaitingcallerid:1;
773 * \brief TRUE if support for call forwarding enabled.
774 * Dial *72 to enable call forwarding.
775 * Dial *73 to disable call forwarding.
776 * \note Set from the "cancallforward" value read in from chan_dahdi.conf
778 unsigned int cancallforward:1;
780 * \brief TRUE if support for call parking is enabled.
781 * \note Set from the "canpark" value read in from chan_dahdi.conf
783 unsigned int canpark:1;
784 /*! \brief TRUE if to wait for a DTMF digit to confirm answer */
785 unsigned int confirmanswer:1;
787 * \brief TRUE if the channel is to be destroyed on hangup.
788 * (Used by pseudo channels.)
790 unsigned int destroy:1;
791 unsigned int didtdd:1; /*!< flag to say its done it once */
792 /*! \brief TRUE if analog type line dialed no digits in Dial() */
793 unsigned int dialednone:1;
794 /*! \brief TRUE if in the process of dialing digits or sending something. */
795 unsigned int dialing:1;
796 /*! \brief TRUE if the transfer capability of the call is digital. */
797 unsigned int digital:1;
798 /*! \brief TRUE if Do-Not-Disturb is enabled, present only for non sig_analog */
800 /*! \brief XXX BOOLEAN Purpose??? */
801 unsigned int echobreak:1;
803 * \brief TRUE if echo cancellation enabled when bridged.
804 * \note Initialized with the "echocancelwhenbridged" value read in from chan_dahdi.conf
805 * \note Disabled if the echo canceller is not setup.
807 unsigned int echocanbridged:1;
808 /*! \brief TRUE if echo cancellation is turned on. */
809 unsigned int echocanon:1;
810 /*! \brief TRUE if a fax tone has already been handled. */
811 unsigned int faxhandled:1;
812 /*! TRUE if dynamic faxbuffers are configured for use, default is OFF */
813 unsigned int usefaxbuffers:1;
814 /*! TRUE while buffer configuration override is in use */
815 unsigned int bufferoverrideinuse:1;
816 /*! \brief TRUE if over a radio and dahdi_read() has been called. */
817 unsigned int firstradio:1;
819 * \brief TRUE if the call will be considered "hung up" on a polarity reversal.
820 * \note Set from the "hanguponpolarityswitch" value read in from chan_dahdi.conf
822 unsigned int hanguponpolarityswitch:1;
823 /*! \brief TRUE if DTMF detection needs to be done by hardware. */
824 unsigned int hardwaredtmf:1;
826 * \brief TRUE if the outgoing caller ID is blocked/hidden.
827 * \note Caller ID can be disabled by dialing *67.
828 * \note Caller ID can be enabled by dialing *82.
829 * \note Initialized with the "hidecallerid" value read in from chan_dahdi.conf
831 unsigned int hidecallerid:1;
833 * \brief TRUE if hide just the name not the number for legacy PBX use.
834 * \note Only applies to PRI channels.
835 * \note Set from the "hidecalleridname" value read in from chan_dahdi.conf
837 unsigned int hidecalleridname:1;
838 /*! \brief TRUE if DTMF detection is disabled. */
839 unsigned int ignoredtmf:1;
841 * \brief TRUE if the channel should be answered immediately
842 * without attempting to gather any digits.
843 * \note Set from the "immediate" value read in from chan_dahdi.conf
845 unsigned int immediate:1;
846 /*! \brief TRUE if in an alarm condition. */
847 unsigned int inalarm:1;
848 /*! \brief TRUE if TDD in MATE mode */
850 /*! \brief TRUE if we originated the call leg. */
851 unsigned int outgoing:1;
852 /* unsigned int overlapdial:1; unused and potentially confusing */
854 * \brief TRUE if busy extensions will hear the call-waiting tone
855 * and can use hook-flash to switch between callers.
856 * \note Set from the "callwaiting" value read in from chan_dahdi.conf
858 unsigned int permcallwaiting:1;
860 * \brief TRUE if the outgoing caller ID is blocked/restricted/hidden.
861 * \note Set from the "hidecallerid" value read in from chan_dahdi.conf
863 unsigned int permhidecallerid:1;
865 * \brief TRUE if PRI congestion/busy indications are sent out-of-band.
866 * \note Set from the "priindication" value read in from chan_dahdi.conf
868 unsigned int priindication_oob:1;
870 * \brief TRUE if PRI B channels are always exclusively selected.
871 * \note Set from the "priexclusive" value read in from chan_dahdi.conf
873 unsigned int priexclusive:1;
875 * \brief TRUE if we will pulse dial.
876 * \note Set from the "pulsedial" value read in from chan_dahdi.conf
878 unsigned int pulse:1;
879 /*! \brief TRUE if a pulsed digit was detected. (Pulse dial phone detected) */
880 unsigned int pulsedial:1;
881 unsigned int restartpending:1; /*!< flag to ensure counted only once for restart */
883 * \brief TRUE if caller ID is restricted.
884 * \note Set but not used. Should be deleted. Redundant with permhidecallerid.
885 * \note Set from the "restrictcid" value read in from chan_dahdi.conf
887 unsigned int restrictcid:1;
889 * \brief TRUE if three way calling is enabled
890 * \note Set from the "threewaycalling" value read in from chan_dahdi.conf
892 unsigned int threewaycalling:1;
894 * \brief TRUE if call transfer is enabled
895 * \note For FXS ports (either direct analog or over T1/E1):
896 * Support flash-hook call transfer
897 * \note For digital ports using ISDN PRI protocols:
898 * Support switch-side transfer (called 2BCT, RLT or other names)
899 * \note Set from the "transfer" value read in from chan_dahdi.conf
901 unsigned int transfer:1;
903 * \brief TRUE if caller ID is used on this channel.
904 * \note PRI and SS7 spans will save caller ID from the networking peer.
905 * \note FXS ports will generate the caller ID spill.
906 * \note FXO ports will listen for the caller ID spill.
907 * \note Set from the "usecallerid" value read in from chan_dahdi.conf
909 unsigned int use_callerid:1;
911 * \brief TRUE if we will use the calling presentation setting
912 * from the Asterisk channel for outgoing calls.
913 * \note Only applies to PRI and SS7 channels.
914 * \note Set from the "usecallingpres" value read in from chan_dahdi.conf
916 unsigned int use_callingpres:1;
918 * \brief TRUE if distinctive rings are to be detected.
919 * \note For FXO lines
920 * \note Set indirectly from the "usedistinctiveringdetection" value read in from chan_dahdi.conf
922 unsigned int usedistinctiveringdetection:1;
924 * \brief TRUE if we should use the callerid from incoming call on dahdi transfer.
925 * \note Set from the "useincomingcalleridondahditransfer" value read in from chan_dahdi.conf
927 unsigned int dahditrcallerid:1;
929 * \brief TRUE if allowed to flash-transfer to busy channels.
930 * \note Set from the "transfertobusy" value read in from chan_dahdi.conf
932 unsigned int transfertobusy:1;
934 * \brief TRUE if the FXO port monitors for neon type MWI indications from the other end.
935 * \note Set if the "mwimonitor" value read in contains "neon" from chan_dahdi.conf
937 unsigned int mwimonitor_neon:1;
939 * \brief TRUE if the FXO port monitors for fsk type MWI indications from the other end.
940 * \note Set if the "mwimonitor" value read in contains "fsk" from chan_dahdi.conf
942 unsigned int mwimonitor_fsk:1;
944 * \brief TRUE if the FXO port monitors for rpas precursor to fsk MWI indications from the other end.
945 * \note RPAS - Ring Pulse Alert Signal
946 * \note Set if the "mwimonitor" value read in contains "rpas" from chan_dahdi.conf
948 unsigned int mwimonitor_rpas:1;
949 /*! \brief TRUE if an MWI monitor thread is currently active */
950 unsigned int mwimonitoractive:1;
951 /*! \brief TRUE if a MWI message sending thread is active */
952 unsigned int mwisendactive:1;
954 * \brief TRUE if channel is out of reset and ready
955 * \note Set but not used.
957 unsigned int inservice:1;
959 * \brief TRUE if the channel is locally blocked.
960 * \note Applies to SS7 and MFCR2 channels.
962 unsigned int locallyblocked:1;
964 * \brief TRUE if the channel is remotely blocked.
965 * \note Applies to SS7 and MFCR2 channels.
967 unsigned int remotelyblocked:1;
968 #if defined(HAVE_PRI)
969 struct sig_pri_pri *pri;
973 #if defined(HAVE_PRI) || defined(HAVE_SS7)
975 * \brief XXX BOOLEAN Purpose???
976 * \note Applies to SS7 channels.
979 /*! \brief TRUE if channel is alerting/ringing */
980 unsigned int alerting:1;
981 /*! \brief TRUE if the call has already gone/hungup */
982 unsigned int alreadyhungup:1;
984 * \brief TRUE if call is in a proceeding state.
985 * The call has started working its way through the network.
987 unsigned int proceeding:1;
988 /*! \brief TRUE if the call has seen progress through the network. */
989 unsigned int progress:1;
992 * \brief TRUE if SMDI (Simplified Message Desk Interface) is enabled
993 * \note Set from the "usesmdi" value read in from chan_dahdi.conf
995 unsigned int use_smdi:1;
996 struct mwisend_info mwisend_data;
997 /*! \brief The serial port to listen for SMDI data on */
998 struct ast_smdi_interface *smdi_iface;
1000 /*! \brief Distinctive Ring data */
1001 struct dahdi_distRings drings;
1004 * \brief The configured context for incoming calls.
1005 * \note The "context" string read in from chan_dahdi.conf
1007 char context[AST_MAX_CONTEXT];
1009 * \brief Saved context string.
1011 char defcontext[AST_MAX_CONTEXT];
1012 /*! \brief Extension to use in the dialplan. */
1013 char exten[AST_MAX_EXTENSION];
1015 * \brief Language configured for calls.
1016 * \note The "language" string read in from chan_dahdi.conf
1018 char language[MAX_LANGUAGE];
1020 * \brief The configured music-on-hold class to use for calls.
1021 * \note The "musicclass" or "mohinterpret" or "musiconhold" string read in from chan_dahdi.conf
1023 char mohinterpret[MAX_MUSICCLASS];
1025 * \brief Suggested music-on-hold class for peer channel to use for calls.
1026 * \note The "mohsuggest" string read in from chan_dahdi.conf
1028 char mohsuggest[MAX_MUSICCLASS];
1029 char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
1030 #if defined(PRI_ANI) || defined(HAVE_SS7)
1031 /*! \brief Automatic Number Identification number (Alternate PRI caller ID number) */
1032 char cid_ani[AST_MAX_EXTENSION];
1034 /*! \brief Automatic Number Identification code from PRI */
1036 /*! \brief Caller ID number from an incoming call. */
1037 char cid_num[AST_MAX_EXTENSION];
1038 /*! \brief Caller ID Q.931 TON/NPI field values. Set by PRI. Zero otherwise. */
1040 /*! \brief Caller ID name from an incoming call. */
1041 char cid_name[AST_MAX_EXTENSION];
1042 /*! \brief Caller ID subaddress from an incoming call. */
1043 char cid_subaddr[AST_MAX_EXTENSION];
1044 char *origcid_num; /*!< malloced original callerid */
1045 char *origcid_name; /*!< malloced original callerid */
1046 /*! \brief Call waiting number. */
1047 char callwait_num[AST_MAX_EXTENSION];
1048 /*! \brief Call waiting name. */
1049 char callwait_name[AST_MAX_EXTENSION];
1050 /*! \brief Redirecting Directory Number Information Service (RDNIS) number */
1051 char rdnis[AST_MAX_EXTENSION];
1052 /*! \brief Dialed Number Identifier */
1053 char dnid[AST_MAX_EXTENSION];
1055 * \brief Bitmapped groups this belongs to.
1056 * \note The "group" bitmapped group string read in from chan_dahdi.conf
1059 /*! \brief Active PCM encoding format: DAHDI_LAW_ALAW or DAHDI_LAW_MULAW */
1061 int confno; /*!< Our conference */
1062 int confusers; /*!< Who is using our conference */
1063 int propconfno; /*!< Propagated conference number */
1065 * \brief Bitmapped call groups this belongs to.
1066 * \note The "callgroup" bitmapped group string read in from chan_dahdi.conf
1068 ast_group_t callgroup;
1070 * \brief Bitmapped pickup groups this belongs to.
1071 * \note The "pickupgroup" bitmapped group string read in from chan_dahdi.conf
1073 ast_group_t pickupgroup;
1075 * \brief Channel variable list with associated values to set when a channel is created.
1076 * \note The "setvar" strings read in from chan_dahdi.conf
1078 struct ast_variable *vars;
1079 int channel; /*!< Channel Number */
1080 int span; /*!< Span number */
1081 time_t guardtime; /*!< Must wait this much time before using for new call */
1082 int cid_signalling; /*!< CID signalling type bell202 or v23 */
1083 int cid_start; /*!< CID start indicator, polarity or ring or DTMF without warning event */
1084 int dtmfcid_holdoff_state; /*!< State indicator that allows for line to settle before checking for dtmf energy */
1085 struct timeval dtmfcid_delay; /*!< Time value used for allow line to settle */
1086 int callingpres; /*!< The value of calling presentation that we're going to use when placing a PRI call */
1087 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
1088 int cidcwexpire; /*!< When to expire our muting for CID/CW */
1089 /*! \brief Analog caller ID waveform sample buffer */
1090 unsigned char *cidspill;
1091 /*! \brief Position in the cidspill buffer to send out next. */
1093 /*! \brief Length of the cidspill buffer containing samples. */
1095 /*! \brief Ring timeout timer?? */
1098 * \brief Ring timeout base.
1099 * \note Value computed indirectly from "ringtimeout" read in from chan_dahdi.conf
1103 * \brief Number of most significant digits/characters to strip from the dialed number.
1104 * \note Feature is deprecated. Use dialplan logic.
1105 * \note The characters are stripped before the PRI TON/NPI prefix
1106 * characters are processed.
1109 /*! \brief BOOLEAN. XXX Meaning what?? */
1111 /*! \brief Number of call waiting rings. */
1113 /*! \brief Echo cancel parameters. */
1115 struct dahdi_echocanparams head;
1116 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
1119 * \brief Echo training time. 0 = disabled
1120 * \note Set from the "echotraining" value read in from chan_dahdi.conf
1123 /*! \brief Filled with 'w'. XXX Purpose?? */
1126 * \brief Number of times to see "busy" tone before hanging up.
1127 * \note Set from the "busycount" value read in from chan_dahdi.conf
1131 * \brief Length of "busy" tone on time.
1132 * \note Set from the "busypattern" value read in from chan_dahdi.conf
1134 int busy_tonelength;
1136 * \brief Length of "busy" tone off time.
1137 * \note Set from the "busypattern" value read in from chan_dahdi.conf
1139 int busy_quietlength;
1141 * \brief Bitmapped call progress detection flags. CALLPROGRESS_xxx values.
1142 * \note Bits set from the "callprogress" and "faxdetect" values read in from chan_dahdi.conf
1146 * \brief Number of milliseconds to wait for dialtone.
1147 * \note Set from the "waitfordialtone" value read in from chan_dahdi.conf
1149 int waitfordialtone;
1150 struct timeval waitingfordt; /*!< Time we started waiting for dialtone */
1151 struct timeval flashtime; /*!< Last flash-hook time */
1152 /*! \brief Opaque DSP configuration structure. */
1153 struct ast_dsp *dsp;
1154 /*! \brief DAHDI dial operation command struct for ioctl() call. */
1155 struct dahdi_dialoperation dop;
1156 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
1157 /*! \brief Second part of SIG_FEATDMF_TA wink operation. */
1159 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
1160 int amaflags; /*!< AMA Flags */
1161 struct tdd_state *tdd; /*!< TDD flag */
1162 /*! \brief Accumulated call forwarding number. */
1163 char call_forward[AST_MAX_EXTENSION];
1165 * \brief Voice mailbox location.
1166 * \note Set from the "mailbox" string read in from chan_dahdi.conf
1168 char mailbox[AST_MAX_EXTENSION];
1169 /*! \brief Opaque event subscription parameters for message waiting indication support. */
1170 struct ast_event_sub *mwi_event_sub;
1171 /*! \brief Delayed dialing for E911. Overlap digits for ISDN. */
1173 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1174 struct dahdi_vmwi_info mwisend_setting; /*!< Which VMWI methods to use */
1175 unsigned int mwisend_fsk: 1; /*! Variable for enabling FSK MWI handling in chan_dahdi */
1176 unsigned int mwisend_rpas:1; /*! Variable for enabling Ring Pulse Alert before MWI FSK Spill */
1178 int distinctivering; /*!< Which distinctivering to use */
1179 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
1180 /*! \brief Holding place for event injected from outside normal operation. */
1183 * \brief Minimal time period (ms) between the answer polarity
1184 * switch and hangup polarity switch.
1186 int polarityonanswerdelay;
1187 /*! \brief Start delay time if polarityonanswerdelay is nonzero. */
1188 struct timeval polaritydelaytv;
1190 * \brief Send caller ID on FXS after this many rings. Set to 1 for US.
1191 * \note Set from the "sendcalleridafter" value read in from chan_dahdi.conf
1193 int sendcalleridafter;
1194 /*! \brief Current line interface polarity. POLARITY_IDLE, POLARITY_REV */
1196 /*! \brief DSP feature flags: DSP_FEATURE_xxx */
1199 /*! \brief SS7 control parameters */
1200 struct dahdi_ss7 *ss7;
1201 /*! \brief Opaque libss7 call control structure */
1202 struct isup_call *ss7call;
1203 char charge_number[50];
1204 char gen_add_number[50];
1205 char gen_dig_number[50];
1206 char orig_called_num[50];
1207 char redirecting_num[50];
1208 char generic_name[50];
1209 unsigned char gen_add_num_plan;
1210 unsigned char gen_add_nai;
1211 unsigned char gen_add_pres_ind;
1212 unsigned char gen_add_type;
1213 unsigned char gen_dig_type;
1214 unsigned char gen_dig_scheme;
1215 char jip_number[50];
1216 unsigned char lspi_type;
1217 unsigned char lspi_scheme;
1218 unsigned char lspi_context;
1219 char lspi_ident[50];
1220 unsigned int call_ref_ident;
1221 unsigned int call_ref_pc;
1222 unsigned char calling_party_cat;
1224 int cic; /*!< CIC associated with channel */
1225 unsigned int dpc; /*!< CIC's DPC */
1226 unsigned int loopedback:1;
1229 struct dahdi_mfcr2 *mfcr2;
1230 openr2_chan_t *r2chan;
1231 openr2_calling_party_category_t mfcr2_recvd_category;
1232 openr2_calling_party_category_t mfcr2_category;
1233 int mfcr2_dnis_index;
1234 int mfcr2_ani_index;
1236 int mfcr2_answer_pending:1;
1237 int mfcr2_charge_calls:1;
1238 int mfcr2_allow_collect_calls:1;
1239 int mfcr2_forced_release:1;
1240 int mfcr2_dnis_matched:1;
1241 int mfcr2_call_accepted:1;
1242 int mfcr2_accept_on_offer:1;
1244 /*! \brief DTMF digit in progress. 0 when no digit in progress. */
1246 /*! \brief TRUE if confrence is muted. */
1251 static struct dahdi_pvt *iflist = NULL; /*!< Main interface list start */
1252 static struct dahdi_pvt *ifend = NULL; /*!< Main interface list end */
1254 #if defined(HAVE_PRI)
1255 static struct dahdi_parms_pseudo {
1256 int buf_no; /*!< Number of buffers */
1257 int buf_policy; /*!< Buffer policy */
1258 int faxbuf_no; /*!< Number of Fax buffers */
1259 int faxbuf_policy; /*!< Fax buffer policy */
1260 } dahdi_pseudo_parms;
1261 #endif /* defined(HAVE_PRI) */
1263 /*! \brief Channel configuration from chan_dahdi.conf .
1264 * This struct is used for parsing the [channels] section of chan_dahdi.conf.
1265 * Generally there is a field here for every possible configuration item.
1267 * The state of fields is saved along the parsing and whenever a 'channel'
1268 * statement is reached, the current dahdi_chan_conf is used to configure the
1269 * channel (struct dahdi_pvt)
1271 * \see dahdi_chan_init for the default values.
1273 struct dahdi_chan_conf {
1274 struct dahdi_pvt chan;
1276 struct dahdi_pri pri;
1280 struct dahdi_ss7 ss7;
1284 struct dahdi_mfcr2_conf mfcr2;
1286 struct dahdi_params timing;
1287 int is_sig_auto; /*!< Use channel signalling from DAHDI? */
1290 * \brief The serial port to listen for SMDI data on
1291 * \note Set from the "smdiport" string read in from chan_dahdi.conf
1293 char smdi_port[SMDI_MAX_FILENAME_LEN];
1296 /*! returns a new dahdi_chan_conf with default values (by-value) */
1297 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
1299 /* recall that if a field is not included here it is initialized
1300 * to 0 or equivalent
1302 struct dahdi_chan_conf conf = {
1305 .nsf = PRI_NSF_NONE,
1306 .switchtype = PRI_SWITCH_NI2,
1307 .dialplan = PRI_UNKNOWN + 1,
1308 .localdialplan = PRI_NATIONAL_ISDN + 1,
1309 .nodetype = PRI_CPE,
1310 .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
1315 .internationalprefix = "",
1316 .nationalprefix = "",
1318 .privateprefix = "",
1319 .unknownprefix = "",
1320 .resetinterval = -1,
1325 .called_nai = SS7_NAI_NATIONAL,
1326 .calling_nai = SS7_NAI_NATIONAL,
1327 .internationalprefix = "",
1328 .nationalprefix = "",
1329 .subscriberprefix = "",
1335 .variant = OR2_VAR_ITU,
1336 .mfback_timeout = -1,
1337 .metering_pulse_timeout = -1,
1340 .get_ani_first = -1,
1341 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
1342 .skip_category_request = -1,
1345 .allow_collect_calls = 0,
1347 .accept_on_offer = 1,
1348 .forced_release = 0,
1350 .immediate_accept = -1,
1353 .loglevel = OR2_LOG_ERROR | OR2_LOG_WARNING,
1354 .category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER
1358 .context = "default",
1361 .mohinterpret = "default",
1364 .transfertobusy = 1,
1366 .cid_signalling = CID_SIG_BELL,
1367 .cid_start = CID_START_RING,
1368 .dahditrcallerid = 0,
1377 .echocancel.head.tap_length = 1,
1385 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1388 .polarityonanswerdelay = 600,
1390 .sendcalleridafter = DEFAULT_CIDRINGS,
1392 .buf_policy = DAHDI_POLICY_IMMEDIATE,
1407 .smdi_port = "/dev/ttyS0",
1414 static struct ast_channel *dahdi_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
1415 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
1416 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
1417 static int dahdi_sendtext(struct ast_channel *c, const char *text);
1418 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
1419 static int dahdi_hangup(struct ast_channel *ast);
1420 static int dahdi_answer(struct ast_channel *ast);
1421 static struct ast_frame *dahdi_read(struct ast_channel *ast);
1422 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
1423 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
1424 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
1425 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1426 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
1427 static int dahdi_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
1428 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
1429 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value);
1431 static const struct ast_channel_tech dahdi_tech = {
1433 .description = tdesc,
1434 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
1435 .requester = dahdi_request,
1436 .send_digit_begin = dahdi_digit_begin,
1437 .send_digit_end = dahdi_digit_end,
1438 .send_text = dahdi_sendtext,
1440 .hangup = dahdi_hangup,
1441 .answer = dahdi_answer,
1443 .write = dahdi_write,
1444 .bridge = dahdi_bridge,
1445 .exception = dahdi_exception,
1446 .indicate = dahdi_indicate,
1447 .fixup = dahdi_fixup,
1448 .setoption = dahdi_setoption,
1449 .queryoption = dahdi_queryoption,
1450 .func_channel_read = dahdi_func_read,
1451 .func_channel_write = dahdi_func_write,
1454 #define GET_CHANNEL(p) ((p)->channel)
1456 #define SIG_PRI_LIB_HANDLE_CASES \
1463 * \brief Determine if sig_pri handles the signaling.
1466 * \param signaling Signaling to determine if is for sig_pri.
1468 * \return TRUE if the signaling is for sig_pri.
1470 static inline int dahdi_sig_pri_lib_handles(int signaling)
1474 switch (signaling) {
1475 case SIG_PRI_LIB_HANDLE_CASES:
1486 static enum analog_sigtype dahdisig_to_analogsig(int sig)
1490 return ANALOG_SIG_FXOLS;
1492 return ANALOG_SIG_FXOGS;
1494 return ANALOG_SIG_FXOKS;
1496 return ANALOG_SIG_FXSLS;
1498 return ANALOG_SIG_FXSGS;
1500 return ANALOG_SIG_FXSKS;
1502 return ANALOG_SIG_EMWINK;
1504 return ANALOG_SIG_EM;
1506 return ANALOG_SIG_EM_E1;
1508 return ANALOG_SIG_FEATD;
1510 return ANALOG_SIG_FEATDMF;
1514 return ANALOG_SIG_FGC_CAMA;
1515 case SIG_FGC_CAMAMF:
1516 return ANALOG_SIG_FGC_CAMAMF;
1518 return ANALOG_SIG_FEATB;
1520 return ANALOG_SIG_SFWINK;
1522 return ANALOG_SIG_SF;
1524 return ANALOG_SIG_SF_FEATD;
1525 case SIG_SF_FEATDMF:
1526 return ANALOG_SIG_SF_FEATDMF;
1527 case SIG_FEATDMF_TA:
1528 return ANALOG_SIG_FEATDMF_TA;
1530 return ANALOG_SIG_FEATB;
1537 static int analog_tone_to_dahditone(enum analog_tone tone)
1540 case ANALOG_TONE_RINGTONE:
1541 return DAHDI_TONE_RINGTONE;
1542 case ANALOG_TONE_STUTTER:
1543 return DAHDI_TONE_STUTTER;
1544 case ANALOG_TONE_CONGESTION:
1545 return DAHDI_TONE_CONGESTION;
1546 case ANALOG_TONE_DIALTONE:
1547 return DAHDI_TONE_DIALTONE;
1548 case ANALOG_TONE_DIALRECALL:
1549 return DAHDI_TONE_DIALRECALL;
1550 case ANALOG_TONE_INFO:
1551 return DAHDI_TONE_INFO;
1557 static int analogsub_to_dahdisub(enum analog_sub analogsub)
1561 switch (analogsub) {
1562 case ANALOG_SUB_REAL:
1565 case ANALOG_SUB_CALLWAIT:
1566 index = SUB_CALLWAIT;
1568 case ANALOG_SUB_THREEWAY:
1569 index = SUB_THREEWAY;
1572 ast_log(LOG_ERROR, "Unidentified sub!\n");
1579 static enum analog_event dahdievent_to_analogevent(int event);
1580 static int bump_gains(struct dahdi_pvt *p);
1581 static int dahdi_setlinear(int dfd, int linear);
1583 static int my_start_cid_detect(void *pvt, int cid_signalling)
1585 struct dahdi_pvt *p = pvt;
1586 int index = SUB_REAL;
1587 p->cs = callerid_new(cid_signalling);
1589 ast_log(LOG_ERROR, "Unable to alloc callerid\n");
1593 dahdi_setlinear(p->subs[index].dfd, 0);
1598 static int my_stop_cid_detect(void *pvt)
1600 struct dahdi_pvt *p = pvt;
1601 int index = SUB_REAL;
1603 callerid_free(p->cs);
1604 dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
1608 static int my_get_callerid(void *pvt, char *namebuf, char *numbuf, enum analog_event *ev, size_t timeout)
1610 struct dahdi_pvt *p = pvt;
1611 struct analog_pvt *analog_p = p->sig_pvt;
1612 struct pollfd poller;
1614 int index = SUB_REAL;
1616 unsigned char buf[256];
1619 poller.fd = p->subs[SUB_REAL].dfd;
1620 poller.events = POLLPRI | POLLIN;
1623 res = poll(&poller, 1, timeout);
1625 if (poller.revents & POLLPRI) {
1626 *ev = dahdievent_to_analogevent(dahdi_get_event(p->subs[SUB_REAL].dfd));
1630 if (poller.revents & POLLIN) {
1632 /* Change API: remove cid_signalling from get_callerid, add a new start_cid_detect and stop_cid_detect function
1633 * to enable slin mode and allocate cid detector. get_callerid should be able to be called any number of times until
1634 * either a timeout occurs or CID is detected (returns 0). returning 1 should be event received, and -1 should be
1635 * a failure and die, and returning 2 means no event was received. */
1636 res = read(p->subs[index].dfd, buf, sizeof(buf));
1638 if (errno != ELAST) {
1639 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
1640 callerid_free(p->cs);
1645 if (analog_p->ringt > 0) {
1646 if (!(--analog_p->ringt)) {
1647 /* only return if we timeout from a ring event */
1652 if (p->cid_signalling == CID_SIG_V23_JP) {
1653 res = callerid_feed_jp(p->cs, buf, res, AST_LAW(p));
1655 res = callerid_feed(p->cs, buf, res, AST_LAW(p));
1659 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
1664 callerid_get(p->cs, &name, &num, &flags);
1666 ast_copy_string(namebuf, name, ANALOG_MAX_CID);
1668 ast_copy_string(numbuf, num, ANALOG_MAX_CID);
1670 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", num, name, flags);
1675 *ev = ANALOG_EVENT_NONE;
1679 static const char *event2str(int event);
1680 static int restore_gains(struct dahdi_pvt *p);
1682 static int my_distinctive_ring(struct ast_channel *chan, void *pvt, int idx, int *ringdata)
1684 unsigned char buf[256];
1692 int checkaftercid = 0;
1694 struct dahdi_pvt *p = pvt;
1695 struct analog_pvt *analog_p = p->sig_pvt;
1697 if (ringdata == NULL) {
1698 ringdata = curRingData;
1703 /* We must have a ring by now, so, if configured, lets try to listen for
1704 * distinctive ringing */
1705 if ((checkaftercid && distinctiveringaftercid) || !checkaftercid) {
1706 /* Clear the current ring data array so we dont have old data in it. */
1707 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(ringdata); receivedRingT++)
1708 ringdata[receivedRingT] = 0;
1710 if (checkaftercid && distinctiveringaftercid)
1711 ast_verb(3, "Detecting post-CID distinctive ring\n");
1712 /* Check to see if context is what it should be, if not set to be. */
1713 else if (strcmp(p->context,p->defcontext) != 0) {
1714 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
1715 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
1719 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
1720 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
1721 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
1725 if (i & DAHDI_IOMUX_SIGEVENT) {
1726 res = dahdi_get_event(p->subs[idx].dfd);
1727 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
1729 /* Let us detect distinctive ring */
1731 ringdata[receivedRingT] = analog_p->ringt;
1733 if (analog_p->ringt < analog_p->ringt_base/2)
1735 /* Increment the ringT counter so we can match it against
1736 values in chan_dahdi.conf for distinctive ring */
1737 if (++receivedRingT == ARRAY_LEN(ringdata))
1739 } else if (i & DAHDI_IOMUX_READ) {
1740 res = read(p->subs[idx].dfd, buf, sizeof(buf));
1742 if (errno != ELAST) {
1743 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
1749 if (analog_p->ringt > 0) {
1750 if (!(--analog_p->ringt)) {
1758 if ((checkaftercid && usedistinctiveringdetection) || !checkaftercid) {
1759 /* this only shows up if you have n of the dring patterns filled in */
1760 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",ringdata[0],ringdata[1],ringdata[2]);
1761 for (counter = 0; counter < 3; counter++) {
1762 /* Check to see if the rings we received match any of the ones in chan_dahdi.conf for this channel */
1764 /* this only shows up if you have n of the dring patterns filled in */
1765 ast_verb(3, "Checking %d,%d,%d\n",
1766 p->drings.ringnum[counter].ring[0],
1767 p->drings.ringnum[counter].ring[1],
1768 p->drings.ringnum[counter].ring[2]);
1769 for (counter1 = 0; counter1 < 3; counter1++) {
1770 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
1771 if (p->drings.ringnum[counter].ring[counter1] == -1) {
1772 ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
1773 ringdata[counter1]);
1775 } else if (ringdata[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
1776 ringdata[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
1777 ast_verb(3, "Ring pattern matched in range: %d to %d\n",
1778 (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
1779 (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
1784 if (distMatches == 3) {
1785 /* The ring matches, set the context to whatever is for distinctive ring.. */
1786 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
1787 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
1788 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
1793 /* Restore linear mode (if appropriate) for Caller*ID processing */
1794 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
1800 static int send_callerid(struct dahdi_pvt *p);
1802 static int my_stop_callwait(void *pvt)
1804 struct dahdi_pvt *p = pvt;
1805 p->callwaitingrepeat = 0;
1811 static int save_conference(struct dahdi_pvt *p);
1813 static int my_callwait(void *pvt)
1815 struct dahdi_pvt *p = pvt;
1816 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1818 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1819 ast_free(p->cidspill);
1821 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1825 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1826 if (!p->callwaitrings && p->callwaitingcallerid) {
1827 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1829 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1831 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1833 p->cidlen = 2400 + READ_SIZE * 4;
1841 static int my_send_callerid(void *pvt, int cwcid, struct ast_callerid *cid)
1843 struct dahdi_pvt *p = pvt;
1845 ast_debug(2, "Starting cid spill\n");
1848 ast_log(LOG_WARNING, "cidspill already exists??\n");
1849 ast_free(p->cidspill);
1852 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1854 p->cidlen = ast_callerid_generate(p->cidspill, cid->cid_name, cid->cid_num, AST_LAW(p));
1858 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, cid->cid_name, cid->cid_num, AST_LAW(p));
1859 p->cidlen += READ_SIZE * 4;
1867 static int my_dsp_reset_and_flush_digits(void *pvt)
1869 struct dahdi_pvt *p = pvt;
1871 ast_dsp_digitreset(p->dsp);
1876 static int my_dsp_set_digitmode(void *pvt, enum analog_dsp_digitmode mode)
1878 struct dahdi_pvt *p = pvt;
1880 if (p->channel == CHAN_PSEUDO)
1881 ast_log(LOG_ERROR, "You have assumed incorrectly sir!\n");
1883 if (mode == ANALOG_DIGITMODE_DTMF) {
1884 /* If we do hardware dtmf, no need for a DSP */
1885 if (p->hardwaredtmf) {
1887 ast_dsp_free(p->dsp);
1894 p->dsp = ast_dsp_new();
1896 ast_log(LOG_ERROR, "Unable to allocate DSP\n");
1901 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
1902 } else if (mode == ANALOG_DIGITMODE_MF) {
1904 p->dsp = ast_dsp_new();
1906 ast_log(LOG_ERROR, "Unable to allocate DSP\n");
1910 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
1915 static int dahdi_wink(struct dahdi_pvt *p, int index);
1917 static int my_wink(void *pvt, enum analog_sub sub)
1919 struct dahdi_pvt *p = pvt;
1920 int index = analogsub_to_dahdisub(sub);
1921 if (index != SUB_REAL) {
1922 ast_log(LOG_ERROR, "We used a sub other than SUB_REAL (incorrect assumption sir)\n");
1924 return dahdi_wink(p, index);
1927 static void wakeup_sub(struct dahdi_pvt *p, int a);
1929 static int reset_conf(struct dahdi_pvt *p);
1931 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted);
1933 static void my_handle_dtmfup(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
1935 struct ast_frame *f = *dest;
1936 struct dahdi_pvt *p = pvt;
1937 int idx = analogsub_to_dahdisub(analog_index);
1939 ast_debug(1, "DTMF digit: %c on %s\n", f->subclass.integer, ast->name);
1941 if (f->subclass.integer == 'f') {
1942 /* Fax tone -- Handle and return NULL */
1943 if ((p->callprogress & CALLPROGRESS_FAX) && !p->faxhandled) {
1944 /* If faxbuffers are configured, use them for the fax transmission */
1945 if (p->usefaxbuffers && !p->bufferoverrideinuse) {
1946 struct dahdi_bufferinfo bi = {
1947 .txbufpolicy = p->faxbuf_policy,
1948 .bufsize = p->bufsize,
1949 .numbufs = p->faxbuf_no
1953 if ((res = ioctl(p->subs[idx].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
1954 ast_log(LOG_WARNING, "Channel '%s' unable to set buffer policy, reason: %s\n", ast->name, strerror(errno));
1956 p->bufferoverrideinuse = 1;
1960 if (strcmp(ast->exten, "fax")) {
1961 const char *target_context = S_OR(ast->macrocontext, ast->context);
1963 /* We need to unlock 'ast' here because ast_exists_extension has the
1964 * potential to start autoservice on the channel. Such action is prone
1967 ast_mutex_unlock(&p->lock);
1968 ast_channel_unlock(ast);
1969 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
1970 ast_channel_lock(ast);
1971 ast_mutex_lock(&p->lock);
1972 ast_verb(3, "Redirecting %s to fax extension\n", ast->name);
1973 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
1974 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
1975 if (ast_async_goto(ast, target_context, "fax", 1))
1976 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
1978 ast_channel_lock(ast);
1979 ast_mutex_lock(&p->lock);
1980 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
1983 ast_debug(1, "Already in a fax extension, not redirecting\n");
1986 ast_debug(1, "Fax already handled\n");
1988 dahdi_confmute(p, 0);
1989 p->subs[idx].f.frametype = AST_FRAME_NULL;
1990 p->subs[idx].f.subclass.integer = 0;
1991 *dest = &p->subs[idx].f;
1995 static void my_lock_private(void *pvt)
1997 struct dahdi_pvt *p = pvt;
1998 ast_mutex_lock(&p->lock);
2001 static void my_unlock_private(void *pvt)
2003 struct dahdi_pvt *p = pvt;
2004 ast_mutex_unlock(&p->lock);
2007 /* linear_mode = 0 - turn linear mode off, >0 - turn linear mode on
2008 * returns the last value of the linear setting
2010 static int my_set_linear_mode(void *pvt, int idx, int linear_mode)
2012 struct dahdi_pvt *p = pvt;
2015 if (0 > linear_mode || !dahdi_setlinear(p->subs[idx].dfd, linear_mode)) {
2018 oldval = p->subs[idx].linear;
2019 p->subs[idx].linear = linear_mode;
2023 static int get_alarms(struct dahdi_pvt *p);
2024 static void handle_alarms(struct dahdi_pvt *p, int alms);
2025 static void my_get_and_handle_alarms(void *pvt)
2028 struct dahdi_pvt *p = pvt;
2030 res = get_alarms(p);
2031 handle_alarms(p, res);
2034 static void *my_get_sigpvt_bridged_channel(struct ast_channel *chan)
2036 struct dahdi_pvt *p = ast_bridged_channel(chan)->tech_pvt;
2043 static int my_get_sub_fd(void *pvt, enum analog_sub sub)
2045 struct dahdi_pvt *p = pvt;
2046 int dahdi_sub = analogsub_to_dahdisub(sub);
2047 return p->subs[dahdi_sub].dfd;
2050 static void my_set_cadence(void *pvt, int *cidrings, struct ast_channel *ast)
2052 struct dahdi_pvt *p = pvt;
2054 /* Choose proper cadence */
2055 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2056 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
2057 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
2058 *cidrings = cidrings[p->distinctivering - 1];
2060 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
2061 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
2062 *cidrings = p->sendcalleridafter;
2066 static void my_set_dialing(void *pvt, int flag)
2068 struct dahdi_pvt *p = pvt;
2072 #if defined(HAVE_PRI)
2073 static void my_set_digital(void *pvt, int flag)
2075 struct dahdi_pvt *p = pvt;
2080 static void my_set_ringtimeout(void *pvt, int ringt)
2082 struct dahdi_pvt *p = pvt;
2086 static void my_set_waitingfordt(void *pvt, struct ast_channel *ast)
2088 struct dahdi_pvt *p = pvt;
2090 if (p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp) {
2091 ast_log(LOG_DEBUG, "Defer dialing for %dms or dialtone\n", p->waitfordialtone);
2092 gettimeofday(&p->waitingfordt, NULL);
2093 ast_setstate(ast, AST_STATE_OFFHOOK);
2097 static int my_check_waitingfordt(void *pvt)
2099 struct dahdi_pvt *p = pvt;
2101 if (p->waitingfordt.tv_usec) {
2108 static void my_set_confirmanswer(void *pvt, int flag)
2110 struct dahdi_pvt *p = pvt;
2111 p->confirmanswer = flag;
2114 static int my_check_confirmanswer(void *pvt)
2116 struct dahdi_pvt *p = pvt;
2117 if (p->confirmanswer) {
2124 static void my_cancel_cidspill(void *pvt)
2126 struct dahdi_pvt *p = pvt;
2128 ast_free(p->cidspill);
2133 static int my_confmute(void *pvt, int mute)
2135 struct dahdi_pvt *p = pvt;
2136 return dahdi_confmute(p, mute);
2139 static void my_set_pulsedial(void *pvt, int flag)
2141 struct dahdi_pvt *p = pvt;
2142 p->pulsedial = flag;
2145 static void my_increase_ss_count(void)
2147 ast_mutex_lock(&ss_thread_lock);
2149 ast_mutex_unlock(&ss_thread_lock);
2152 static void my_decrease_ss_count(void)
2154 ast_mutex_lock(&ss_thread_lock);
2156 ast_cond_signal(&ss_thread_complete);
2157 ast_mutex_unlock(&ss_thread_lock);
2160 static void my_all_subchannels_hungup(void *pvt)
2162 struct dahdi_pvt *p = pvt;
2169 ast_dsp_free(p->dsp);
2173 law = DAHDI_LAW_DEFAULT;
2174 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
2176 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
2178 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
2184 /* Cleanup owners here */
2185 for (i = 0; i < 3; i++) {
2186 p->subs[i].owner = NULL;
2192 if (num_restart_pending == 0) {
2197 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index);
2199 static int my_conf_del(void *pvt, enum analog_sub sub)
2201 struct dahdi_pvt *p = pvt;
2202 int x = analogsub_to_dahdisub(sub);
2204 return conf_del(p, &p->subs[x], x);
2207 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index, int slavechannel);
2209 static int my_conf_add(void *pvt, enum analog_sub sub)
2211 struct dahdi_pvt *p = pvt;
2212 int x = analogsub_to_dahdisub(sub);
2214 return conf_add(p, &p->subs[x], x, 0);
2217 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out);
2219 static int my_complete_conference_update(void *pvt, int needconference)
2221 struct dahdi_pvt *p = pvt;
2222 int needconf = needconference;
2225 struct dahdi_pvt *slave = NULL;
2227 useslavenative = isslavenative(p, &slave);
2229 /* If we have a slave, add him to our conference now. or DAX
2230 if this is slave native */
2231 for (x = 0; x < MAX_SLAVES; x++) {
2234 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
2236 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
2241 /* If we're supposed to be in there, do so now */
2242 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
2244 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
2246 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
2250 /* If we have a master, add ourselves to his conference */
2252 if (isslavenative(p->master, NULL)) {
2253 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
2255 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
2259 /* Nobody is left (or should be left) in our conference.
2267 static int check_for_conference(struct dahdi_pvt *p);
2269 static int my_check_for_conference(void *pvt)
2271 struct dahdi_pvt *p = pvt;
2272 return check_for_conference(p);
2275 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)
2277 struct dahdi_pvt *p = pvt;
2281 da = analogsub_to_dahdisub(a);
2282 db = analogsub_to_dahdisub(b);
2284 tchan = p->subs[da].chan;
2286 p->subs[da].chan = p->subs[db].chan;
2288 p->subs[db].chan = tchan;
2291 ast_channel_set_fd(ast_a, 0, p->subs[da].dfd);
2293 ast_channel_set_fd(ast_b, 0, p->subs[db].dfd);
2295 p->subs[da].owner = ast_a;
2296 p->subs[db].owner = ast_b;
2304 static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int, const char *);
2306 static struct ast_channel *my_new_analog_ast_channel(void *pvt, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
2308 struct dahdi_pvt *p = pvt;
2309 int dsub = analogsub_to_dahdisub(sub);
2311 return dahdi_new(p, state, startpbx, dsub, 0, 0, requestor ? requestor->linkedid : "");
2314 #if defined(HAVE_PRI) || defined(HAVE_SS7)
2315 static int dahdi_setlaw(int dfd, int law)
2318 res = ioctl(dfd, DAHDI_SETLAW, &law);
2325 #if defined(HAVE_PRI)
2326 static struct ast_channel *my_new_pri_ast_channel(void *pvt, int state, int startpbx, enum sig_pri_law law, int transfercapability, char *exten, const struct ast_channel *requestor)
2328 struct dahdi_pvt *p = pvt;
2332 /* Set to audio mode at this point */
2334 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &audio) == -1)
2335 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, audio, strerror(errno));
2337 if (law != SIG_PRI_DEFLAW) {
2338 dahdi_setlaw(p->subs[SUB_REAL].dfd, (law == SIG_PRI_ULAW) ? DAHDI_LAW_MULAW : DAHDI_LAW_ALAW);
2341 ast_copy_string(p->exten, exten, sizeof(p->exten));
2344 case SIG_PRI_DEFLAW:
2348 newlaw = DAHDI_LAW_ALAW;
2351 newlaw = DAHDI_LAW_MULAW;
2354 return dahdi_new(p, state, startpbx, SUB_REAL, newlaw, transfercapability, requestor ? requestor->linkedid : "");
2356 #endif /* defined(HAVE_PRI) */
2358 static int unalloc_sub(struct dahdi_pvt *p, int x);
2360 static int my_unallocate_sub(void *pvt, enum analog_sub analogsub)
2362 struct dahdi_pvt *p = pvt;
2364 return unalloc_sub(p, analogsub_to_dahdisub(analogsub));
2367 static int alloc_sub(struct dahdi_pvt *p, int x);
2369 static int my_allocate_sub(void *pvt, enum analog_sub analogsub)
2371 struct dahdi_pvt *p = pvt;
2373 return alloc_sub(p, analogsub_to_dahdisub(analogsub));
2376 static int has_voicemail(struct dahdi_pvt *p);
2378 static int my_has_voicemail(void *pvt)
2380 struct dahdi_pvt *p = pvt;
2382 return has_voicemail(p);
2385 static int my_play_tone(void *pvt, enum analog_sub sub, enum analog_tone tone)
2387 struct dahdi_pvt *p = pvt;
2390 index = analogsub_to_dahdisub(sub);
2392 return tone_zone_play_tone(p->subs[index].dfd, analog_tone_to_dahditone(tone));
2395 static enum analog_event dahdievent_to_analogevent(int event)
2397 enum analog_event res;
2400 case DAHDI_EVENT_ONHOOK:
2401 res = ANALOG_EVENT_ONHOOK;
2403 case DAHDI_EVENT_RINGOFFHOOK:
2404 res = ANALOG_EVENT_RINGOFFHOOK;
2406 case DAHDI_EVENT_WINKFLASH:
2407 res = ANALOG_EVENT_WINKFLASH;
2409 case DAHDI_EVENT_ALARM:
2410 res = ANALOG_EVENT_ALARM;
2412 case DAHDI_EVENT_NOALARM:
2413 res = ANALOG_EVENT_NOALARM;
2415 case DAHDI_EVENT_DIALCOMPLETE:
2416 res = ANALOG_EVENT_DIALCOMPLETE;
2418 case DAHDI_EVENT_RINGERON:
2419 res = ANALOG_EVENT_RINGERON;
2421 case DAHDI_EVENT_RINGEROFF:
2422 res = ANALOG_EVENT_RINGEROFF;
2424 case DAHDI_EVENT_HOOKCOMPLETE:
2425 res = ANALOG_EVENT_HOOKCOMPLETE;
2427 case DAHDI_EVENT_PULSE_START:
2428 res = ANALOG_EVENT_PULSE_START;
2430 case DAHDI_EVENT_POLARITY:
2431 res = ANALOG_EVENT_POLARITY;
2433 case DAHDI_EVENT_RINGBEGIN:
2434 res = ANALOG_EVENT_RINGBEGIN;
2436 case DAHDI_EVENT_EC_DISABLED:
2437 res = ANALOG_EVENT_EC_DISABLED;
2439 case DAHDI_EVENT_REMOVED:
2440 res = ANALOG_EVENT_REMOVED;
2442 case DAHDI_EVENT_NEONMWI_ACTIVE:
2443 res = ANALOG_EVENT_NEONMWI_ACTIVE;
2445 case DAHDI_EVENT_NEONMWI_INACTIVE:
2446 res = ANALOG_EVENT_NEONMWI_INACTIVE;
2448 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2449 case DAHDI_EVENT_TX_CED_DETECTED:
2450 res = ANALOG_EVENT_TX_CED_DETECTED;
2452 case DAHDI_EVENT_RX_CED_DETECTED:
2453 res = ANALOG_EVENT_RX_CED_DETECTED;
2455 case DAHDI_EVENT_EC_NLP_DISABLED:
2456 res = ANALOG_EVENT_EC_NLP_DISABLED;
2458 case DAHDI_EVENT_EC_NLP_ENABLED:
2459 res = ANALOG_EVENT_EC_NLP_ENABLED;
2462 case DAHDI_EVENT_PULSEDIGIT:
2463 res = ANALOG_EVENT_PULSEDIGIT;
2465 case DAHDI_EVENT_DTMFDOWN:
2466 res = ANALOG_EVENT_DTMFDOWN;
2468 case DAHDI_EVENT_DTMFUP:
2469 res = ANALOG_EVENT_DTMFUP;
2472 switch(event & 0xFFFF0000) {
2473 case DAHDI_EVENT_PULSEDIGIT:
2474 case DAHDI_EVENT_DTMFDOWN:
2475 case DAHDI_EVENT_DTMFUP:
2476 /* The event includes a digit number in the low word.
2477 * Converting it to a 'enum analog_event' would remove
2478 * that information. Thus it is returned as-is.
2483 res = ANALOG_EVENT_ERROR;
2490 static inline int dahdi_wait_event(int fd);
2492 static int my_wait_event(void *pvt)
2494 struct dahdi_pvt *p = pvt;
2496 return dahdi_wait_event(p->subs[SUB_REAL].dfd);
2499 static int my_get_event(void *pvt)
2501 struct dahdi_pvt *p = pvt;
2504 if (p->fake_event) {
2505 res = p->fake_event;
2508 res = dahdi_get_event(p->subs[SUB_REAL].dfd);
2510 return dahdievent_to_analogevent(res);
2513 static int my_is_off_hook(void *pvt)
2515 struct dahdi_pvt *p = pvt;
2517 struct dahdi_params par;
2519 memset(&par, 0, sizeof(par));
2521 if (p->subs[SUB_REAL].dfd > -1)
2522 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
2524 /* Assume not off hook on CVRS */
2526 par.rxisoffhook = 0;
2529 ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno));
2532 if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
2533 /* When "onhook" that means no battery on the line, and thus
2534 it is out of service..., if it's on a TDM card... If it's a channel
2535 bank, there is no telling... */
2536 return (par.rxbits > -1) || par.rxisoffhook;
2539 return par.rxisoffhook;
2542 static void dahdi_enable_ec(struct dahdi_pvt *p);
2543 static void dahdi_disable_ec(struct dahdi_pvt *p);
2545 static int my_set_echocanceller(void *pvt, int enable)
2547 struct dahdi_pvt *p = pvt;
2552 dahdi_disable_ec(p);
2557 static int dahdi_ring_phone(struct dahdi_pvt *p);
2559 static int my_ring(void *pvt)
2561 struct dahdi_pvt *p = pvt;
2563 return dahdi_ring_phone(p);
2566 static int my_flash(void *pvt)
2568 struct dahdi_pvt *p = pvt;
2569 int func = DAHDI_FLASH;
2570 return ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &func);
2573 static inline int dahdi_set_hook(int fd, int hs);
2575 static int my_off_hook(void *pvt)
2577 struct dahdi_pvt *p = pvt;
2578 return dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
2581 static int my_start(void *pvt)
2583 struct dahdi_pvt *p = pvt;
2584 int x = DAHDI_START;
2586 return ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
2589 static int my_dial_digits(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop)
2591 int index = analogsub_to_dahdisub(sub);
2593 struct dahdi_pvt *p = pvt;
2594 struct dahdi_dialoperation ddop;
2596 if (dop->op != ANALOG_DIAL_OP_REPLACE) {
2597 ast_log(LOG_ERROR, "Fix the dial_digits callback!\n");
2601 if (sub != ANALOG_SUB_REAL)
2602 printf("Trying to dial digits on sub %d\n", sub);
2604 ddop.op = DAHDI_DIAL_OP_REPLACE;
2605 strncpy(ddop.dialstr, dop->dialstr, sizeof(ddop.dialstr));
2607 printf("Dialing %s on %d\n", ddop.dialstr, p->channel);
2609 res = ioctl(p->subs[index].dfd, DAHDI_DIAL, &ddop);
2612 ast_log(LOG_DEBUG, "DAHDI_DIAL ioctl failed on %s: %s\n", p->owner->name, strerror(errno));
2617 static void dahdi_train_ec(struct dahdi_pvt *p);
2619 static int my_train_echocanceller(void *pvt)
2621 struct dahdi_pvt *p = pvt;
2628 static int my_is_dialing(void *pvt, enum analog_sub sub)
2630 struct dahdi_pvt *p = pvt;
2634 index = analogsub_to_dahdisub(sub);
2636 if (ioctl(p->subs[index].dfd, DAHDI_DIALING, &x)) {
2637 ast_log(LOG_DEBUG, "DAHDI_DIALING ioctl failed!\n");
2644 static int my_on_hook(void *pvt)
2646 struct dahdi_pvt *p = pvt;
2647 return dahdi_set_hook(p->subs[ANALOG_SUB_REAL].dfd, DAHDI_ONHOOK);
2650 #if defined(HAVE_PRI)
2651 static void my_pri_fixup_chans(void *chan_old, void *chan_new)
2653 struct dahdi_pvt *old_chan = chan_old;
2654 struct dahdi_pvt *new_chan = chan_new;
2656 new_chan->owner = old_chan->owner;
2657 old_chan->owner = NULL;
2658 if (new_chan->owner) {
2659 new_chan->owner->tech_pvt = new_chan;
2660 new_chan->owner->fds[0] = new_chan->subs[SUB_REAL].dfd;
2661 new_chan->subs[SUB_REAL].owner = old_chan->subs[SUB_REAL].owner;
2662 old_chan->subs[SUB_REAL].owner = NULL;
2664 /* Copy any DSP that may be present */
2665 new_chan->dsp = old_chan->dsp;
2666 new_chan->dsp_features = old_chan->dsp_features;
2667 old_chan->dsp = NULL;
2668 old_chan->dsp_features = 0;
2671 static int sig_pri_tone_to_dahditone(enum sig_pri_tone tone)
2674 case SIG_PRI_TONE_RINGTONE:
2675 return DAHDI_TONE_RINGTONE;
2676 case SIG_PRI_TONE_STUTTER:
2677 return DAHDI_TONE_STUTTER;
2678 case SIG_PRI_TONE_CONGESTION:
2679 return DAHDI_TONE_CONGESTION;
2680 case SIG_PRI_TONE_DIALTONE:
2681 return DAHDI_TONE_DIALTONE;
2682 case SIG_PRI_TONE_DIALRECALL:
2683 return DAHDI_TONE_DIALRECALL;
2684 case SIG_PRI_TONE_INFO:
2685 return DAHDI_TONE_INFO;
2686 case SIG_PRI_TONE_BUSY:
2687 return DAHDI_TONE_BUSY;
2693 static void my_handle_dchan_exception(struct sig_pri_pri *pri, int index)
2697 res = ioctl(pri->fds[index], DAHDI_GETEVENT, &x);
2699 ast_log(LOG_NOTICE, "PRI got event: %s (%d) on D-channel of span %d\n", event2str(x), x, pri->span);
2701 /* Keep track of alarm state */
2702 if (x == DAHDI_EVENT_ALARM) {
2703 pri_event_alarm(pri, index, 0);
2704 } else if (x == DAHDI_EVENT_NOALARM) {
2705 pri_event_noalarm(pri, index, 0);
2709 static int my_pri_play_tone(void *pvt, enum sig_pri_tone tone)
2711 struct dahdi_pvt *p = pvt;
2713 return tone_zone_play_tone(p->subs[SUB_REAL].dfd, sig_pri_tone_to_dahditone(tone));
2718 * \brief Set the caller id information.
2721 * \param pvt DAHDI private structure
2722 * \param caller Caller-id information to set.
2726 static void my_pri_set_callerid(void *pvt, const struct ast_party_caller *caller)
2728 struct dahdi_pvt *p = pvt;
2730 ast_copy_string(p->cid_num, S_OR(caller->id.number, ""), sizeof(p->cid_num));
2731 ast_copy_string(p->cid_name, S_OR(caller->id.name, ""), sizeof(p->cid_name));
2732 if (caller->id.subaddress.valid) {
2733 ast_copy_string(p->cid_subaddr, S_OR(caller->id.subaddress.str, ""),
2734 sizeof(p->cid_subaddr));
2736 p->cid_subaddr[0] = '\0';
2738 p->cid_ton = caller->id.number_type;
2739 p->callingpres = caller->id.number_presentation;
2740 ast_copy_string(p->cid_ani, S_OR(caller->ani, ""), sizeof(p->cid_ani));
2741 p->cid_ani2 = caller->ani2;
2746 * \brief Set the Dialed Number Identifier.
2749 * \param pvt DAHDI private structure
2750 * \param dnid Dialed Number Identifier string.
2754 static void my_pri_set_dnid(void *pvt, const char *dnid)
2756 struct dahdi_pvt *p = pvt;
2758 ast_copy_string(p->dnid, dnid, sizeof(p->dnid));
2763 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
2766 * \param pvt DAHDI private structure
2767 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
2771 static void my_pri_set_rdnis(void *pvt, const char *rdnis)
2773 struct dahdi_pvt *p = pvt;
2775 ast_copy_string(p->rdnis, rdnis, sizeof(p->rdnis));
2778 static int dahdi_new_pri_nobch_channel(struct sig_pri_pri *pri);
2780 static struct sig_pri_callback dahdi_pri_callbacks =
2782 .handle_dchan_exception = my_handle_dchan_exception,
2783 .play_tone = my_pri_play_tone,
2784 .set_echocanceller = my_set_echocanceller,
2785 .dsp_reset_and_flush_digits = my_dsp_reset_and_flush_digits,
2786 .lock_private = my_lock_private,
2787 .unlock_private = my_unlock_private,
2788 .new_ast_channel = my_new_pri_ast_channel,
2789 .fixup_chans = my_pri_fixup_chans,
2790 .set_dialing = my_set_dialing,
2791 .set_digital = my_set_digital,
2792 .set_callerid = my_pri_set_callerid,
2793 .set_dnid = my_pri_set_dnid,
2794 .set_rdnis = my_pri_set_rdnis,
2795 .new_nobch_intf = dahdi_new_pri_nobch_channel,
2797 #endif /* defined(HAVE_PRI) */
2800 * \brief Send MWI state change
2802 * \arg mailbox_full This is the mailbox associated with the FXO line that the
2803 * MWI state has changed on.
2804 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
2805 * whether there are messages waiting or not.
2809 * This function does two things:
2811 * 1) It generates an internal Asterisk event notifying any other module that
2812 * cares about MWI that the state of a mailbox has changed.
2814 * 2) It runs the script specified by the mwimonitornotify option to allow
2815 * some custom handling of the state change.
2817 static void notify_message(char *mailbox_full, int thereornot)
2819 char s[sizeof(mwimonitornotify) + 80];
2820 struct ast_event *event;
2821 char *mailbox, *context;
2823 /* Strip off @default */
2824 context = mailbox = ast_strdupa(mailbox_full);
2825 strsep(&context, "@");
2826 if (ast_strlen_zero(context))
2827 context = "default";
2829 if (!(event = ast_event_new(AST_EVENT_MWI,
2830 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
2831 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2832 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
2833 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
2834 AST_EVENT_IE_END))) {
2838 ast_event_queue_and_cache(event);
2840 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
2841 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
2846 static void my_handle_notify_message(struct ast_channel *chan, void *pvt, int cid_flags, int neon_mwievent)
2848 struct dahdi_pvt *p = pvt;
2850 if (neon_mwievent > -1 && !p->mwimonitor_neon)
2853 if (neon_mwievent == ANALOG_EVENT_NEONMWI_ACTIVE || cid_flags & CID_MSGWAITING) {
2854 ast_log(LOG_NOTICE, "MWI: Channel %d message waiting, mailbox %s\n", p->channel, p->mailbox);
2855 notify_message(p->mailbox, 1);
2856 } else if (neon_mwievent == ANALOG_EVENT_NEONMWI_INACTIVE || cid_flags & CID_NOMSGWAITING) {
2857 ast_log(LOG_NOTICE, "MWI: Channel %d no message waiting, mailbox %s\n", p->channel, p->mailbox);
2858 notify_message(p->mailbox, 0);
2860 /* If the CID had Message waiting payload, assume that this for MWI only and hangup the call */
2861 /* If generated using Ring Pulse Alert, then ring has been answered as a call and needs to be hungup */
2862 if (neon_mwievent == -1 && p->mwimonitor_rpas) {
2870 static struct analog_callback dahdi_analog_callbacks =
2872 .play_tone = my_play_tone,
2873 .get_event = my_get_event,
2874 .wait_event = my_wait_event,
2875 .is_off_hook = my_is_off_hook,
2876 .set_echocanceller = my_set_echocanceller,
2879 .off_hook = my_off_hook,
2880 .dial_digits = my_dial_digits,
2881 .train_echocanceller = my_train_echocanceller,
2882 .on_hook = my_on_hook,
2883 .is_dialing = my_is_dialing,
2884 .allocate_sub = my_allocate_sub,
2885 .unallocate_sub = my_unallocate_sub,
2886 .swap_subs = my_swap_subchannels,
2887 .has_voicemail = my_has_voicemail,
2888 .check_for_conference = my_check_for_conference,
2889 .conf_add = my_conf_add,
2890 .conf_del = my_conf_del,
2891 .complete_conference_update = my_complete_conference_update,
2893 .all_subchannels_hungup = my_all_subchannels_hungup,
2894 .lock_private = my_lock_private,
2895 .unlock_private = my_unlock_private,
2896 .handle_dtmfup = my_handle_dtmfup,
2898 .new_ast_channel = my_new_analog_ast_channel,
2899 .dsp_set_digitmode = my_dsp_set_digitmode,
2900 .dsp_reset_and_flush_digits = my_dsp_reset_and_flush_digits,
2901 .send_callerid = my_send_callerid,
2902 .callwait = my_callwait,
2903 .stop_callwait = my_stop_callwait,
2904 .get_callerid = my_get_callerid,
2905 .start_cid_detect = my_start_cid_detect,
2906 .stop_cid_detect = my_stop_cid_detect,
2907 .handle_notify_message = my_handle_notify_message,
2908 .increase_ss_count = my_increase_ss_count,
2909 .decrease_ss_count = my_decrease_ss_count,
2910 .distinctive_ring = my_distinctive_ring,
2911 .set_linear_mode = my_set_linear_mode,
2912 .get_and_handle_alarms = my_get_and_handle_alarms,
2913 .get_sigpvt_bridged_channel = my_get_sigpvt_bridged_channel,
2914 .get_sub_fd = my_get_sub_fd,
2915 .set_cadence = my_set_cadence,
2916 .set_dialing = my_set_dialing,
2917 .set_ringtimeout = my_set_ringtimeout,
2918 .set_waitingfordt = my_set_waitingfordt,
2919 .check_waitingfordt = my_check_waitingfordt,
2920 .set_confirmanswer = my_set_confirmanswer,
2921 .check_confirmanswer = my_check_confirmanswer,
2922 .cancel_cidspill = my_cancel_cidspill,
2923 .confmute = my_confmute,
2924 .set_pulsedial = my_set_pulsedial,
2927 static struct dahdi_pvt *round_robin[32];
2929 #if defined(HAVE_SS7)
2930 static inline void ss7_rel(struct dahdi_ss7 *ss7)
2932 ast_mutex_unlock(&ss7->lock);
2934 #endif /* defined(HAVE_SS7) */
2936 #if defined(HAVE_SS7)
2937 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
2940 /* Grab the lock first */
2942 res = ast_mutex_trylock(&pri->lock);
2944 DEADLOCK_AVOIDANCE(&pvt->lock);
2947 /* Then break the poll */
2948 if (pri->master != AST_PTHREADT_NULL)
2949 pthread_kill(pri->master, SIGURG);
2952 #endif /* defined(HAVE_SS7) */
2954 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
2957 if (p->subs[SUB_REAL].owner == ast)
2959 else if (p->subs[SUB_CALLWAIT].owner == ast)
2961 else if (p->subs[SUB_THREEWAY].owner == ast)
2966 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
2971 static void wakeup_sub(struct dahdi_pvt *p, int a)
2974 if (p->subs[a].owner) {
2975 if (ast_channel_trylock(p->subs[a].owner)) {
2976 DEADLOCK_AVOIDANCE(&p->lock);
2978 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
2979 ast_channel_unlock(p->subs[a].owner);
2987 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
2990 struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
2995 ast_mutex_unlock(&ss7->lock);
3004 if (ast_channel_trylock(p->owner)) {
3005 DEADLOCK_AVOIDANCE(&p->lock);
3007 ast_queue_frame(p->owner, f);
3008 ast_channel_unlock(p->owner);
3014 #if defined(HAVE_SS7)
3018 ast_mutex_lock(&ss7->lock);
3029 static int dahdi_r2_answer(struct dahdi_pvt *p)
3032 /* openr2 1.1.0 and older does not even define OR2_LIB_INTERFACE
3033 * and does not has support for openr2_chan_answer_call_with_mode
3035 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
3036 const char *double_answer = pbx_builtin_getvar_helper(p->owner, "MFCR2_DOUBLE_ANSWER");
3037 int wants_double_answer = ast_true(double_answer) ? 1 : 0;
3038 if (!double_answer) {
3039 /* this still can result in double answer if the channel context
3040 * was configured that way */
3041 res = openr2_chan_answer_call(p->r2chan);
3042 } else if (wants_double_answer) {
3043 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_DOUBLE);
3045 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_SIMPLE);
3048 res = openr2_chan_answer_call(p->r2chan);
3055 /* should be called with the ast_channel locked */
3056 static openr2_calling_party_category_t dahdi_r2_get_channel_category(struct ast_channel *c)
3058 openr2_calling_party_category_t cat;
3059 const char *catstr = pbx_builtin_getvar_helper(c, "MFCR2_CATEGORY");
3060 struct dahdi_pvt *p = c->tech_pvt;
3061 if (ast_strlen_zero(catstr)) {
3062 ast_debug(1, "No MFC/R2 category specified for chan %s, using default %s\n",
3063 c->name, openr2_proto_get_category_string(p->mfcr2_category));
3064 return p->mfcr2_category;
3066 if ((cat = openr2_proto_get_category(catstr)) == OR2_CALLING_PARTY_CATEGORY_UNKNOWN) {
3067 ast_log(LOG_WARNING, "Invalid category specified '%s' for chan %s, using default %s\n",
3068 catstr, c->name, openr2_proto_get_category_string(p->mfcr2_category));
3069 return p->mfcr2_category;
3071 ast_debug(1, "Using category %s\n", catstr);
3075 static void dahdi_r2_on_call_init(openr2_chan_t *r2chan)
3077 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3078 ast_mutex_lock(&p->lock);
3080 ast_mutex_unlock(&p->lock);
3081 /* TODO: This can happen when some other thread just finished dahdi_request requesting this very same
3082 interface but has not yet seized the line (dahdi_call), and the far end wins and seize the line,
3083 can we avoid this somehow?, at this point when dahdi_call send the seize, it is likely that since
3084 the other end will see our seize as a forced release and drop the call, we will see an invalid
3085 pattern that will be seen and treated as protocol error. */
3086 ast_log(LOG_ERROR, "Collision of calls on chan %d detected!.\n", openr2_chan_get_number(r2chan));
3090 /* better safe than sorry ... */
3091 p->cid_name[0] = '\0';
3092 p->cid_num[0] = '\0';
3093 p->cid_subaddr[0] = '\0';
3096 p->mfcr2_ani_index = '\0';
3097 p->mfcr2_dnis_index = '\0';
3098 p->mfcr2_dnis_matched = 0;
3099 p->mfcr2_answer_pending = 0;
3100 p->mfcr2_call_accepted = 0;
3101 ast_mutex_unlock(&p->lock);
3102 ast_verbose("New MFC/R2 call detected on chan %d.\n", openr2_chan_get_number(r2chan));
3105 static void dahdi_r2_on_hardware_alarm(openr2_chan_t *r2chan, int alarm)
3108 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3109 ast_mutex_lock(&p->lock);
3110 p->inalarm = alarm ? 1 : 0;
3112 res = get_alarms(p);
3113 handle_alarms(p, res);
3115 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
3116 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", "Channel: %d\r\n", p->channel);
3118 ast_mutex_unlock(&p->lock);
3121 static void dahdi_r2_on_os_error(openr2_chan_t *r2chan, int errorcode)
3123 ast_log(LOG_ERROR, "OS error on chan %d: %s\n", openr2_chan_get_number(r2chan), strerror(errorcode));
3126 static void dahdi_r2_on_protocol_error(openr2_chan_t *r2chan, openr2_protocol_error_t reason)
3128 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3129 ast_log(LOG_ERROR, "MFC/R2 protocol error on chan %d: %s\n", openr2_chan_get_number(r2chan), openr2_proto_get_error(reason));
3131 p->owner->hangupcause = AST_CAUSE_PROTOCOL_ERROR;
3132 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3134 ast_mutex_lock(&p->lock);
3136 ast_mutex_unlock(&p->lock);
3139 static void dahdi_r2_disconnect_call(struct dahdi_pvt *p, openr2_call_disconnect_cause_t cause)
3141 if (openr2_chan_disconnect_call(p->r2chan, cause)) {
3142 ast_log(LOG_NOTICE, "Bad! failed to disconnect call on channel %d with reason %s, hope for the best!\n",
3143 p->channel, openr2_proto_get_disconnect_string(cause));
3144 /* force the chan to idle and release the call flag now since we will not see a clean on_call_end */
3145 openr2_chan_set_idle(p->r2chan);
3146 ast_mutex_lock(&p->lock);
3148 ast_mutex_unlock(&p->lock);
3152 static void dahdi_r2_on_call_offered(openr2_chan_t *r2chan, const char *ani, const char *dnis, openr2_calling_party_category_t category)
3154 struct dahdi_pvt *p;
3155 struct ast_channel *c;
3156 ast_verbose("MFC/R2 call offered on chan %d. ANI = %s, DNIS = %s, Category = %s\n",
3157 openr2_chan_get_number(r2chan), ani ? ani : "(restricted)", dnis,
3158 openr2_proto_get_category_string(category));
3159 p = openr2_chan_get_client_data(r2chan);
3160 /* if collect calls are not allowed and this is a collect call, reject it! */
3161 if (!p->mfcr2_allow_collect_calls && category == OR2_CALLING_PARTY_CATEGORY_COLLECT_CALL) {
3162 ast_log(LOG_NOTICE, "Rejecting MFC/R2 collect call\n");
3163 dahdi_r2_disconnect_call(p, OR2_CAUSE_COLLECT_CALL_REJECTED);
3166 ast_mutex_lock(&p->lock);
3167 p->mfcr2_recvd_category = category;
3168 /* if we're not supposed to use CID, clear whatever we have */
3169 if (!p->use_callerid) {
3170 ast_log(LOG_DEBUG, "No CID allowed in configuration, CID is being cleared!\n");
3174 /* if we're supposed to answer immediately, clear DNIS and set 's' exten */
3175 if (p->immediate || !openr2_context_get_max_dnis(openr2_chan_get_context(r2chan))) {
3176 ast_log(LOG_DEBUG, "Setting exten => s because of immediate or 0 DNIS configured\n");
3180 ast_mutex_unlock(&p->lock);
3181 if (!ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
3182 ast_log(LOG_NOTICE, "MFC/R2 call on channel %d requested non-existent extension '%s' in context '%s'. Rejecting call.\n",
3183 p->channel, p->exten, p->context);
3184 dahdi_r2_disconnect_call(p, OR2_CAUSE_UNALLOCATED_NUMBER);
3187 if (!p->mfcr2_accept_on_offer) {
3188 /* The user wants us to start the PBX thread right away without accepting the call first */
3189 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0, NULL);
3191 /* Done here, don't disable reading now since we still need to generate MF tones to accept
3192 the call or reject it and detect the tone off condition of the other end, all of this
3193 will be done in the PBX thread now */
3196 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
3197 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
3198 } else if (p->mfcr2_charge_calls) {
3199 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge on chan %d\n", p->channel);
3200 openr2_chan_accept_call(r2chan, OR2_CALL_WITH_CHARGE);
3202 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge on chan %d\n", p->channel);
3203 openr2_chan_accept_call(r2chan, OR2_CALL_NO_CHARGE);
3207 static void dahdi_r2_on_call_end(openr2_chan_t *r2chan)
3209 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3210 ast_verbose("MFC/R2 call end on channel %d\n", p->channel);
3211 ast_mutex_lock(&p->lock);
3213 ast_mutex_unlock(&p->lock);
3216 static void dahdi_enable_ec(struct dahdi_pvt *p);
3217 static void dahdi_r2_on_call_accepted(openr2_chan_t *r2chan, openr2_call_mode_t mode)
3219 struct dahdi_pvt *p = NULL;
3220 struct ast_channel *c = NULL;
3221 p = openr2_chan_get_client_data(r2chan);