0af7d7d4be1fecd13eb11fbdbaf995c3bee10d4f
[asterisk/asterisk.git] / channels / chan_dahdi.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief DAHDI for Pseudo TDM
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
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.
28  *
29  * You need to install libraries before you attempt to compile
30  * and install the DAHDI channel.
31  *
32  * \par See also
33  * \arg \ref Config_dahdi
34  *
35  * \ingroup channel_drivers
36  *
37  * \todo Deprecate the "musiconhold" configuration option post 1.4
38  */
39
40 /*** MODULEINFO
41         <depend>res_smdi</depend>
42         <depend>dahdi</depend>
43         <depend>tonezone</depend>
44         <use>pri</use>
45         <use>ss7</use>
46         <use>openr2</use>
47  ***/
48
49 #include "asterisk.h"
50
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
52
53 #ifdef __NetBSD__
54 #include <pthread.h>
55 #include <signal.h>
56 #else
57 #include <sys/signal.h>
58 #endif
59 #include <sys/ioctl.h>
60 #include <math.h>
61 #include <ctype.h>
62
63 #include <dahdi/user.h>
64 #include <dahdi/tonezone.h>
65 #include "sig_analog.h"
66 /* Analog signaling is currently still present in chan_dahdi for use with
67  * radio. Sig_analog does not currently handle any radio operations. If
68  * radio only uses analog signaling, then the radio handling logic could
69  * be placed in sig_analog and the duplicated code could be removed.
70  */
71
72 #ifdef HAVE_PRI
73 #include "sig_pri.h"
74 #endif
75
76 #ifdef HAVE_SS7
77 /* put this here until sig_ss7 comes along */
78 #define NUM_DCHANS              4               /*!< No more than 4 d-channels */
79 #define MAX_CHANNELS    672             /*!< No more than a DS3 per trunk group */
80 #include <libss7.h>
81 #endif
82
83 #ifdef HAVE_OPENR2
84 #include <openr2.h>
85 #endif
86
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
120 /*** DOCUMENTATION
121         <application name="DAHDISendKeypadFacility" language="en_US">
122                 <synopsis>
123                         Send digits out of band over a PRI.
124                 </synopsis>
125                 <syntax>
126                         <parameter name="digits" required="true" />
127                 </syntax>
128                 <description>
129                         <para>This application will send the given string of digits in a Keypad
130                         Facility IE over the current channel.</para>
131                 </description>
132         </application>
133         <application name="DAHDISendCallreroutingFacility" language="en_US">
134                 <synopsis>
135                         Send QSIG call rerouting facility over a PRI.
136                 </synopsis>
137                 <syntax argsep=",">
138                         <parameter name="destination" required="true">
139                                 <para>Destination number.</para>
140                         </parameter>
141                         <parameter name="original">
142                                 <para>Original called number.</para>
143                         </parameter>
144                         <parameter name="reason">
145                                 <para>Diversion reason, if not specified defaults to <literal>unknown</literal></para>
146                         </parameter>
147                 </syntax>
148                 <description>
149                         <para>This application will send a Callrerouting Facility IE over the
150                         current channel.</para>
151                 </description>
152         </application>
153         <application name="DAHDIAcceptR2Call" language="en_US">
154                 <synopsis>
155                         Accept an R2 call if its not already accepted (you still need to answer it)
156                 </synopsis>
157                 <syntax>
158                         <parameter name="charge" required="true">
159                                 <para>Yes or No.</para>
160                                 <para>Whether you want to accept the call with charge or without charge.</para>
161                         </parameter>
162                 </syntax>
163                 <description>
164                         <para>This application will Accept the R2 call either with charge or no charge.</para>
165                 </description>
166         </application>
167         <manager name="DAHDITransfer" language="en_US">
168                 <synopsis>
169                         Transfer DAHDI Channel.
170                 </synopsis>
171                 <syntax>
172                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
173                         <parameter name="DAHDIChannel" required="true">
174                                 <para>DAHDI channel name to transfer.</para>
175                         </parameter>
176                 </syntax>
177                 <description>
178                         <para>Transfer a DAHDI channel.</para>
179                 </description>
180         </manager>
181         <manager name="DAHDIHangup" language="en_US">
182                 <synopsis>
183                         Hangup DAHDI Channel.
184                 </synopsis>
185                 <syntax>
186                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
187                         <parameter name="DAHDIChannel" required="true">
188                                 <para>DAHDI channel name to hangup.</para>
189                         </parameter>
190                 </syntax>
191                 <description>
192                         <para>Hangup a DAHDI channel.</para>
193                 </description>
194         </manager>
195         <manager name="DAHDIDialOffhook" language="en_US">
196                 <synopsis>
197                         Dial over DAHDI channel while offhook.
198                 </synopsis>
199                 <syntax>
200                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
201                         <parameter name="DAHDIChannel" required="true" />
202                         <parameter name="Number" required="true" />
203                 </syntax>
204                 <description>
205                 </description>
206         </manager>
207         <manager name="DAHDIDNDon" language="en_US">
208                 <synopsis>
209                         Toggle DAHDI channel Do Not Disturb status ON.
210                 </synopsis>
211                 <syntax>
212                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
213                         <parameter name="DAHDIChannel" required="true" />
214                 </syntax>
215                 <description>
216                 </description>
217         </manager>
218         <manager name="DAHDIDNDoff" language="en_US">
219                 <synopsis>
220                         Toggle DAHDI channel Do Not Disturb status OFF.
221                 </synopsis>
222                 <syntax>
223                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
224                         <parameter name="DAHDIChannel" required="true" />
225                 </syntax>
226                 <description>
227                 </description>
228         </manager>
229         <manager name="DAHDIShowChannels" language="en_US">
230                 <synopsis>
231                         Show status DAHDI channels.
232                 </synopsis>
233                 <syntax>
234                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
235                         <parameter name="DAHDIChannel" required="true" />
236                 </syntax>
237                 <description>
238                 </description>
239         </manager>
240         <manager name="DAHDIRestart" language="en_US">
241                 <synopsis>
242                         Fully Restart DAHDI channels (terminates calls).
243                 </synopsis>
244                 <syntax>
245                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
246                 </syntax>
247                 <description>
248                 </description>
249         </manager>
250  ***/
251
252 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
253
254 static const char * const lbostr[] = {
255 "0 db (CSU)/0-133 feet (DSX-1)",
256 "133-266 feet (DSX-1)",
257 "266-399 feet (DSX-1)",
258 "399-533 feet (DSX-1)",
259 "533-655 feet (DSX-1)",
260 "-7.5db (CSU)",
261 "-15db (CSU)",
262 "-22.5db (CSU)"
263 };
264
265 /*! Global jitterbuffer configuration - by default, jb is disabled */
266 static struct ast_jb_conf default_jbconf =
267 {
268         .flags = 0,
269         .max_size = -1,
270         .resync_threshold = -1,
271         .impl = ""
272 };
273 static struct ast_jb_conf global_jbconf;
274
275 /*!
276  * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
277  * the user hangs up to reset the state machine so ring works properly.
278  * This is used to be able to support kewlstart by putting the zhone in
279  * groundstart mode since their forward disconnect supervision is entirely
280  * broken even though their documentation says it isn't and their support
281  * is entirely unwilling to provide any assistance with their channel banks
282  * even though their web site says they support their products for life.
283  */
284 /* #define ZHONE_HACK */
285
286 /*! \note
287  * Define if you want to check the hook state for an FXO (FXS signalled) interface
288  * before dialing on it.  Certain FXO interfaces always think they're out of
289  * service with this method however.
290  */
291 /* #define DAHDI_CHECK_HOOKSTATE */
292
293 /*! \brief Typically, how many rings before we should send Caller*ID */
294 #define DEFAULT_CIDRINGS 1
295
296 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
297
298
299 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
300 #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))
301
302 static const char tdesc[] = "DAHDI Telephony Driver"
303 #if defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2)
304         " w/"
305 #endif
306 #ifdef HAVE_PRI
307         "PRI"
308 #endif
309 #ifdef HAVE_SS7
310         #ifdef HAVE_PRI
311         " & SS7"
312         #else
313         "SS7"
314         #endif
315 #endif
316 #ifdef HAVE_OPENR2
317         #if defined(HAVE_PRI) || defined(HAVE_SS7)
318         " & MFC/R2"
319         #else
320         "MFC/R2"
321         #endif
322 #endif
323 ;
324
325 static const char config[] = "chan_dahdi.conf";
326
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
353
354 #ifdef LOTS_OF_SPANS
355 #define NUM_SPANS       DAHDI_MAX_SPANS
356 #else
357 #define NUM_SPANS               32
358 #endif
359
360 #define CHAN_PSEUDO     -2
361
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)
366
367 #define NUM_CADENCE_MAX 25
368 static int num_cadence = 4;
369 static int user_has_defined_cadences = 0;
370
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 */
376 };
377
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.
380  */
381
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 */
387 };
388
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}};
391
392 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
393                         (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
394
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) */)
397
398 static char defaultcic[64] = "";
399 static char defaultozz[64] = "";
400
401 static char parkinglot[AST_MAX_EXTENSION] = "";         /*!< Default parking lot for this channel */
402
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;
407 #endif
408
409 static char progzone[10] = "";
410
411 static int usedistinctiveringdetection = 0;
412 static int distinctiveringaftercid = 0;
413
414 static int numbufs = 4;
415
416 static int mwilevel = 512;
417 static int dtmfcid_level = 256;
418
419 #ifdef HAVE_PRI
420 #ifdef PRI_GETSET_TIMERS
421 static int pritimers[PRI_MAX_TIMERS];
422 #endif
423 static int pridebugfd = -1;
424 static char pridebugfilename[1024] = "";
425 #endif
426
427 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
428 static int firstdigittimeout = 16000;
429
430 /*! \brief How long to wait for following digits (FXO logic) */
431 static int gendigittimeout = 8000;
432
433 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
434 static int matchdigittimeout = 3000;
435
436 /*! \brief Protect the interface list (of dahdi_pvt's) */
437 AST_MUTEX_DEFINE_STATIC(iflock);
438
439
440 static int ifcount = 0;
441
442 #ifdef HAVE_PRI
443 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
444 #endif
445
446 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
447    when it's doing something critical. */
448 AST_MUTEX_DEFINE_STATIC(monlock);
449
450 /*! \brief This is the thread for the monitor which checks for input on the channels
451    which are not currently in use. */
452 static pthread_t monitor_thread = AST_PTHREADT_NULL;
453 static ast_cond_t ss_thread_complete;
454 AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
455 AST_MUTEX_DEFINE_STATIC(restart_lock);
456 static int ss_thread_count = 0;
457 static int num_restart_pending = 0;
458
459 static int restart_monitor(void);
460
461 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);
462
463 static int dahdi_sendtext(struct ast_channel *c, const char *text);
464
465 static void mwi_event_cb(const struct ast_event *event, void *userdata)
466 {
467         /* This module does not handle MWI in an event-based manner.  However, it
468          * subscribes to MWI for each mailbox that is configured so that the core
469          * knows that we care about it.  Then, chan_dahdi will get the MWI from the
470          * event cache instead of checking the mailbox directly. */
471 }
472
473 /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
474 static inline int dahdi_get_event(int fd)
475 {
476         int j;
477         if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
478                 return -1;
479         return j;
480 }
481
482 /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
483 static inline int dahdi_wait_event(int fd)
484 {
485         int i, j = 0;
486         i = DAHDI_IOMUX_SIGEVENT;
487         if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
488                 return -1;
489         if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
490                 return -1;
491         return j;
492 }
493
494 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
495 #define READ_SIZE 160
496
497 #define MASK_AVAIL              (1 << 0)        /*!< Channel available for PRI use */
498 #define MASK_INUSE              (1 << 1)        /*!< Channel currently in use */
499
500 #define CALLWAITING_SILENT_SAMPLES      ( (300 * 8) / READ_SIZE) /*!< 300 ms */
501 #define CALLWAITING_REPEAT_SAMPLES      ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
502 #define CIDCW_EXPIRE_SAMPLES            ( (500 * 8) / READ_SIZE) /*!< 500 ms */
503 #define MIN_MS_SINCE_FLASH                      ( (2000) )      /*!< 2000 ms */
504 #define DEFAULT_RINGT                           ( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
505
506 struct dahdi_pvt;
507
508 /*!
509  * \brief Configured ring timeout base.
510  * \note Value computed from "ringtimeout" read in from chan_dahdi.conf if it exists.
511  */
512 static int ringt_base = DEFAULT_RINGT;
513
514 #ifdef HAVE_SS7
515
516 #define LINKSTATE_INALARM       (1 << 0)
517 #define LINKSTATE_STARTING      (1 << 1)
518 #define LINKSTATE_UP            (1 << 2)
519 #define LINKSTATE_DOWN          (1 << 3)
520
521 #define SS7_NAI_DYNAMIC         -1
522
523 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
524
525 struct dahdi_ss7 {
526         pthread_t master;                                               /*!< Thread of master */
527         ast_mutex_t lock;
528         int fds[NUM_DCHANS];
529         int numsigchans;
530         int linkstate[NUM_DCHANS];
531         int numchans;
532         int type;
533         enum {
534                 LINKSET_STATE_DOWN = 0,
535                 LINKSET_STATE_UP
536         } state;
537         char called_nai;                                                /*!< Called Nature of Address Indicator */
538         char calling_nai;                                               /*!< Calling Nature of Address Indicator */
539         char internationalprefix[10];                                   /*!< country access code ('00' for european dialplans) */
540         char nationalprefix[10];                                        /*!< area access code ('0' for european dialplans) */
541         char subscriberprefix[20];                                      /*!< area access code + area code ('0'+area code for european dialplans) */
542         char unknownprefix[20];                                         /*!< for unknown dialplans */
543         struct ss7 *ss7;
544         struct dahdi_pvt *pvts[MAX_CHANNELS];                           /*!< Member channel pvt structs */
545         int flags;                                                      /*!< Linkset flags */
546 };
547
548 static struct dahdi_ss7 linksets[NUM_SPANS];
549
550 static int cur_ss7type = -1;
551 static int cur_linkset = -1;
552 static int cur_pointcode = -1;
553 static int cur_cicbeginswith = -1;
554 static int cur_adjpointcode = -1;
555 static int cur_networkindicator = -1;
556 static int cur_defaultdpc = -1;
557 #endif /* HAVE_SS7 */
558
559 #ifdef HAVE_OPENR2
560 struct dahdi_mfcr2 {
561         pthread_t r2master;                    /*!< Thread of master */
562         openr2_context_t *protocol_context;    /*!< OpenR2 context handle */
563         struct dahdi_pvt *pvts[MAX_CHANNELS];     /*!< Member channel pvt structs */
564         int numchans;                          /*!< Number of channels in this R2 block */
565         int monitored_count;                   /*!< Number of channels being monitored */
566 };
567
568 struct dahdi_mfcr2_conf {
569         openr2_variant_t variant;
570         int mfback_timeout;
571         int metering_pulse_timeout;
572         int max_ani;
573         int max_dnis;
574         int get_ani_first:1;
575 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
576         int skip_category_request:1;
577 #endif
578         int call_files:1;
579         int allow_collect_calls:1;
580         int charge_calls:1;
581         int accept_on_offer:1;
582         int forced_release:1;
583         int double_answer:1;
584         int immediate_accept:1;
585         char logdir[OR2_MAX_PATH];
586         char r2proto_file[OR2_MAX_PATH];
587         openr2_log_level_t loglevel;
588         openr2_calling_party_category_t category;
589 };
590
591 /* malloc'd array of malloc'd r2links */
592 static struct dahdi_mfcr2 **r2links;
593 /* how many r2links have been malloc'd */
594 static int r2links_count = 0;
595
596 #endif /* HAVE_OPENR2 */
597
598 #ifdef HAVE_PRI
599
600 struct dahdi_pri {
601         int dchannels[NUM_DCHANS];                                      /*!< What channel are the dchannels on */
602         int mastertrunkgroup;                                           /*!< What trunk group is our master */
603         int prilogicalspan;                                             /*!< Logical span number within trunk group */
604         struct sig_pri_pri pri;
605 };
606
607 static struct dahdi_pri pris[NUM_SPANS];
608
609 #else
610 /*! Shut up the compiler */
611 struct dahdi_pri;
612 #endif
613
614 #define SUB_REAL        0                       /*!< Active call */
615 #define SUB_CALLWAIT    1                       /*!< Call-Waiting call on hold */
616 #define SUB_THREEWAY    2                       /*!< Three-way call */
617
618 /* Polarity states */
619 #define POLARITY_IDLE   0
620 #define POLARITY_REV    1
621
622
623 struct distRingData {
624         int ring[3];
625         int range;
626 };
627 struct ringContextData {
628         char contextData[AST_MAX_CONTEXT];
629 };
630 struct dahdi_distRings {
631         struct distRingData ringnum[3];
632         struct ringContextData ringContext[3];
633 };
634
635 static const char * const subnames[] = {
636         "Real",
637         "Callwait",
638         "Threeway"
639 };
640
641 struct dahdi_subchannel {
642         int dfd;
643         struct ast_channel *owner;
644         int chan;
645         short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
646         struct ast_frame f;             /*!< One frame for each channel.  How did this ever work before? */
647         unsigned int needringing:1;
648         unsigned int needbusy:1;
649         unsigned int needcongestion:1;
650         unsigned int needanswer:1;
651         unsigned int needflash:1;
652         unsigned int needhold:1;
653         unsigned int needunhold:1;
654         unsigned int linear:1;
655         unsigned int inthreeway:1;
656         struct dahdi_confinfo curconf;
657 };
658
659 #define CONF_USER_REAL          (1 << 0)
660 #define CONF_USER_THIRDCALL     (1 << 1)
661
662 #define MAX_SLAVES      4
663
664 /* States for sending MWI message
665  * First three states are required for send Ring Pulse Alert Signal
666  */
667 typedef enum {
668         MWI_SEND_NULL = 0,
669         MWI_SEND_SA,
670         MWI_SEND_SA_WAIT,
671         MWI_SEND_PAUSE,
672         MWI_SEND_SPILL,
673         MWI_SEND_CLEANUP,
674         MWI_SEND_DONE,
675 } mwisend_states;
676
677 struct mwisend_info {
678         struct  timeval pause;
679         mwisend_states  mwisend_current;
680 };
681
682 /*! Specify the lists dahdi_pvt can be put in. */
683 enum DAHDI_IFLIST {
684         DAHDI_IFLIST_NONE,      /*!< The dahdi_pvt is not in any list. */
685         DAHDI_IFLIST_MAIN,      /*!< The dahdi_pvt is in the main interface list */
686 };
687
688 struct dahdi_pvt {
689         ast_mutex_t lock;
690         struct callerid_state *cs;
691         struct ast_channel *owner;                      /*!< Our current active owner (if applicable) */
692                                                         /*!< Up to three channels can be associated with this call */
693
694         struct dahdi_subchannel sub_unused;             /*!< Just a safety precaution */
695         struct dahdi_subchannel subs[3];                        /*!< Sub-channels */
696         struct dahdi_confinfo saveconf;                 /*!< Saved conference info */
697
698         struct dahdi_pvt *slaves[MAX_SLAVES];           /*!< Slave to us (follows our conferencing) */
699         struct dahdi_pvt *master;                               /*!< Master to us (we follow their conferencing) */
700         int inconference;                               /*!< If our real should be in the conference */
701
702         int bufsize;                /*!< Size of the buffers */
703         int buf_no;                                     /*!< Number of buffers */
704         int buf_policy;                         /*!< Buffer policy */
705         int faxbuf_no;              /*!< Number of Fax buffers */
706         int faxbuf_policy;          /*!< Fax buffer policy */
707         int sig;                                        /*!< Signalling style */
708         /*!
709          * \brief Nonzero if the signaling type is sent over a radio.
710          * \note Set to a couple of nonzero values but it is only tested like a boolean.
711          */
712         int radio;
713         int outsigmod;                                  /*!< Outbound Signalling style (modifier) */
714         int oprmode;                                    /*!< "Operator Services" mode */
715         struct dahdi_pvt *oprpeer;                              /*!< "Operator Services" peer tech_pvt ptr */
716         /*! \brief Amount of gain to increase during caller id */
717         float cid_rxgain;
718         /*! \brief Rx gain set by chan_dahdi.conf */
719         float rxgain;
720         /*! \brief Tx gain set by chan_dahdi.conf */
721         float txgain;
722         int tonezone;                                   /*!< tone zone for this chan, or -1 for default */
723         enum DAHDI_IFLIST which_iflist; /*!< Which interface list is this structure listed? */
724         struct dahdi_pvt *next;                         /*!< Next channel in list */
725         struct dahdi_pvt *prev;                         /*!< Prev channel in list */
726
727         /* flags */
728
729         /*!
730          * \brief TRUE if ADSI (Analog Display Services Interface) available
731          * \note Set from the "adsi" value read in from chan_dahdi.conf
732          */
733         unsigned int adsi:1;
734         /*!
735          * \brief TRUE if we can use a polarity reversal to mark when an outgoing
736          * call is answered by the remote party.
737          * \note Set from the "answeronpolarityswitch" value read in from chan_dahdi.conf
738          */
739         unsigned int answeronpolarityswitch:1;
740         /*!
741          * \brief TRUE if busy detection is enabled.
742          * (Listens for the beep-beep busy pattern.)
743          * \note Set from the "busydetect" value read in from chan_dahdi.conf
744          */
745         unsigned int busydetect:1;
746         /*!
747          * \brief TRUE if call return is enabled.
748          * (*69, if your dialplan doesn't catch this first)
749          * \note Set from the "callreturn" value read in from chan_dahdi.conf
750          */
751         unsigned int callreturn:1;
752         /*!
753          * \brief TRUE if busy extensions will hear the call-waiting tone
754          * and can use hook-flash to switch between callers.
755          * \note Can be disabled by dialing *70.
756          * \note Initialized with the "callwaiting" value read in from chan_dahdi.conf
757          */
758         unsigned int callwaiting:1;
759         /*!
760          * \brief TRUE if send caller ID for Call Waiting
761          * \note Set from the "callwaitingcallerid" value read in from chan_dahdi.conf
762          */
763         unsigned int callwaitingcallerid:1;
764         /*!
765          * \brief TRUE if support for call forwarding enabled.
766          * Dial *72 to enable call forwarding.
767          * Dial *73 to disable call forwarding.
768          * \note Set from the "cancallforward" value read in from chan_dahdi.conf
769          */
770         unsigned int cancallforward:1;
771         /*!
772          * \brief TRUE if support for call parking is enabled.
773          * \note Set from the "canpark" value read in from chan_dahdi.conf
774          */
775         unsigned int canpark:1;
776         /*! \brief TRUE if to wait for a DTMF digit to confirm answer */
777         unsigned int confirmanswer:1;
778         /*!
779          * \brief TRUE if the channel is to be destroyed on hangup.
780          * (Used by pseudo channels.)
781          */
782         unsigned int destroy:1;
783         unsigned int didtdd:1;                          /*!< flag to say its done it once */
784         /*! \brief TRUE if analog type line dialed no digits in Dial() */
785         unsigned int dialednone:1;
786         /*! \brief TRUE if in the process of dialing digits or sending something. */
787         unsigned int dialing:1;
788         /*! \brief TRUE if the transfer capability of the call is digital. */
789         unsigned int digital:1;
790         /*! \brief TRUE if Do-Not-Disturb is enabled, present only for non sig_analog */
791         unsigned int dnd:1;
792         /*! \brief XXX BOOLEAN Purpose??? */
793         unsigned int echobreak:1;
794         /*!
795          * \brief TRUE if echo cancellation enabled when bridged.
796          * \note Initialized with the "echocancelwhenbridged" value read in from chan_dahdi.conf
797          * \note Disabled if the echo canceller is not setup.
798          */
799         unsigned int echocanbridged:1;
800         /*! \brief TRUE if echo cancellation is turned on. */
801         unsigned int echocanon:1;
802         /*! \brief TRUE if a fax tone has already been handled. */
803         unsigned int faxhandled:1;
804         /*! TRUE if dynamic faxbuffers are configured for use, default is OFF */
805         unsigned int usefaxbuffers:1;
806         /*! TRUE while buffer configuration override is in use */
807         unsigned int bufferoverrideinuse:1;
808         /*! \brief TRUE if over a radio and dahdi_read() has been called. */
809         unsigned int firstradio:1;
810         /*!
811          * \brief TRUE if the call will be considered "hung up" on a polarity reversal.
812          * \note Set from the "hanguponpolarityswitch" value read in from chan_dahdi.conf
813          */
814         unsigned int hanguponpolarityswitch:1;
815         /*! \brief TRUE if DTMF detection needs to be done by hardware. */
816         unsigned int hardwaredtmf:1;
817         /*!
818          * \brief TRUE if the outgoing caller ID is blocked/hidden.
819          * \note Caller ID can be disabled by dialing *67.
820          * \note Caller ID can be enabled by dialing *82.
821          * \note Initialized with the "hidecallerid" value read in from chan_dahdi.conf
822          */
823         unsigned int hidecallerid:1;
824         /*!
825          * \brief TRUE if hide just the name not the number for legacy PBX use.
826          * \note Only applies to PRI channels.
827          * \note Set from the "hidecalleridname" value read in from chan_dahdi.conf
828          */
829         unsigned int hidecalleridname:1;
830         /*! \brief TRUE if DTMF detection is disabled. */
831         unsigned int ignoredtmf:1;
832         /*!
833          * \brief TRUE if the channel should be answered immediately
834          * without attempting to gather any digits.
835          * \note Set from the "immediate" value read in from chan_dahdi.conf
836          */
837         unsigned int immediate:1;
838         /*! \brief TRUE if in an alarm condition. */
839         unsigned int inalarm:1;
840         /*! \brief TRUE if TDD in MATE mode */
841         unsigned int mate:1;
842         /*! \brief TRUE if we originated the call leg. */
843         unsigned int outgoing:1;
844         /* unsigned int overlapdial:1;                  unused and potentially confusing */
845         /*!
846          * \brief TRUE if busy extensions will hear the call-waiting tone
847          * and can use hook-flash to switch between callers.
848          * \note Set from the "callwaiting" value read in from chan_dahdi.conf
849          */
850         unsigned int permcallwaiting:1;
851         /*!
852          * \brief TRUE if the outgoing caller ID is blocked/restricted/hidden.
853          * \note Set from the "hidecallerid" value read in from chan_dahdi.conf
854          */
855         unsigned int permhidecallerid:1;
856         /*!
857          * \brief TRUE if PRI congestion/busy indications are sent out-of-band.
858          * \note Set from the "priindication" value read in from chan_dahdi.conf
859          */
860         unsigned int priindication_oob:1;
861         /*!
862          * \brief TRUE if PRI B channels are always exclusively selected.
863          * \note Set from the "priexclusive" value read in from chan_dahdi.conf
864          */
865         unsigned int priexclusive:1;
866         /*!
867          * \brief TRUE if we will pulse dial.
868          * \note Set from the "pulsedial" value read in from chan_dahdi.conf
869          */
870         unsigned int pulse:1;
871         /*! \brief TRUE if a pulsed digit was detected. (Pulse dial phone detected) */
872         unsigned int pulsedial:1;
873         unsigned int restartpending:1;          /*!< flag to ensure counted only once for restart */
874         /*!
875          * \brief TRUE if caller ID is restricted.
876          * \note Set but not used.  Should be deleted.  Redundant with permhidecallerid.
877          * \note Set from the "restrictcid" value read in from chan_dahdi.conf
878          */
879         unsigned int restrictcid:1;
880         /*!
881          * \brief TRUE if three way calling is enabled
882          * \note Set from the "threewaycalling" value read in from chan_dahdi.conf
883          */
884         unsigned int threewaycalling:1;
885         /*!
886          * \brief TRUE if call transfer is enabled
887          * \note For FXS ports (either direct analog or over T1/E1):
888          *   Support flash-hook call transfer
889          * \note For digital ports using ISDN PRI protocols:
890          *   Support switch-side transfer (called 2BCT, RLT or other names)
891          * \note Set from the "transfer" value read in from chan_dahdi.conf
892          */
893         unsigned int transfer:1;
894         /*!
895          * \brief TRUE if caller ID is used on this channel.
896          * \note PRI and SS7 spans will save caller ID from the networking peer.
897          * \note FXS ports will generate the caller ID spill.
898          * \note FXO ports will listen for the caller ID spill.
899          * \note Set from the "usecallerid" value read in from chan_dahdi.conf
900          */
901         unsigned int use_callerid:1;
902         /*!
903          * \brief TRUE if we will use the calling presentation setting
904          * from the Asterisk channel for outgoing calls.
905          * \note Only applies to PRI and SS7 channels.
906          * \note Set from the "usecallingpres" value read in from chan_dahdi.conf
907          */
908         unsigned int use_callingpres:1;
909         /*!
910          * \brief TRUE if distinctive rings are to be detected.
911          * \note For FXO lines
912          * \note Set indirectly from the "usedistinctiveringdetection" value read in from chan_dahdi.conf
913          */
914         unsigned int usedistinctiveringdetection:1;
915         /*!
916          * \brief TRUE if we should use the callerid from incoming call on dahdi transfer.
917          * \note Set from the "useincomingcalleridondahditransfer" value read in from chan_dahdi.conf
918          */
919         unsigned int dahditrcallerid:1;
920         /*!
921          * \brief TRUE if allowed to flash-transfer to busy channels.
922          * \note Set from the "transfertobusy" value read in from chan_dahdi.conf
923          */
924         unsigned int transfertobusy:1;
925         /*!
926          * \brief TRUE if the FXO port monitors for neon type MWI indications from the other end.
927          * \note Set if the "mwimonitor" value read in contains "neon" from chan_dahdi.conf
928          */
929         unsigned int mwimonitor_neon:1;
930         /*!
931          * \brief TRUE if the FXO port monitors for fsk type MWI indications from the other end.
932          * \note Set if the "mwimonitor" value read in contains "fsk" from chan_dahdi.conf
933          */
934         unsigned int mwimonitor_fsk:1;
935         /*!
936          * \brief TRUE if the FXO port monitors for rpas precursor to fsk MWI indications from the other end.
937          * \note RPAS - Ring Pulse Alert Signal
938          * \note Set if the "mwimonitor" value read in contains "rpas" from chan_dahdi.conf
939          */
940         unsigned int mwimonitor_rpas:1;
941         /*! \brief TRUE if an MWI monitor thread is currently active */
942         unsigned int mwimonitoractive:1;
943         /*! \brief TRUE if a MWI message sending thread is active */
944         unsigned int mwisendactive:1;
945         /*!
946          * \brief TRUE if channel is out of reset and ready
947          * \note Set but not used.
948          */
949         unsigned int inservice:1;
950         /*!
951          * \brief TRUE if the channel is locally blocked.
952          * \note Applies to SS7 and MFCR2 channels.
953          */
954         unsigned int locallyblocked:1;
955         /*!
956          * \brief TRUE if the channel is remotely blocked.
957          * \note Applies to SS7 and MFCR2 channels.
958          */
959         unsigned int remotelyblocked:1;
960 #if defined(HAVE_PRI)
961         struct sig_pri_pri *pri;
962         int prioffset;
963         int logicalspan;
964 #endif
965 #if defined(HAVE_PRI) || defined(HAVE_SS7)
966         /*!
967          * \brief XXX BOOLEAN Purpose???
968          * \note Applies to SS7 channels.
969          */
970         unsigned int rlt:1;
971         /*! \brief TRUE if channel is alerting/ringing */
972         unsigned int alerting:1;
973         /*! \brief TRUE if the call has already gone/hungup */
974         unsigned int alreadyhungup:1;
975         /*!
976          * \brief TRUE if this is an idle call
977          * \note Applies to PRI channels.
978          */
979         unsigned int isidlecall:1;
980         /*!
981          * \brief TRUE if call is in a proceeding state.
982          * The call has started working its way through the network.
983          */
984         unsigned int proceeding:1;
985         /*! \brief TRUE if the call has seen progress through the network. */
986         unsigned int progress:1;
987         /*!
988          * \brief TRUE if this channel is being reset/restarted
989          * \note Applies to PRI channels.
990          */
991         unsigned int resetting:1;
992         /*!
993          * \brief TRUE if this channel has received a SETUP_ACKNOWLEDGE
994          * \note Applies to PRI channels.
995          */
996         unsigned int setup_ack:1;
997 #endif
998         /*!
999          * \brief TRUE if SMDI (Simplified Message Desk Interface) is enabled
1000          * \note Set from the "usesmdi" value read in from chan_dahdi.conf
1001          */
1002         unsigned int use_smdi:1;
1003         struct mwisend_info mwisend_data;
1004         /*! \brief The serial port to listen for SMDI data on */
1005         struct ast_smdi_interface *smdi_iface;
1006
1007         /*! \brief Distinctive Ring data */
1008         struct dahdi_distRings drings;
1009
1010         /*!
1011          * \brief The configured context for incoming calls.
1012          * \note The "context" string read in from chan_dahdi.conf
1013          */
1014         char context[AST_MAX_CONTEXT];
1015         /*!
1016          * \brief Saved context string.
1017          */
1018         char defcontext[AST_MAX_CONTEXT];
1019         /*! \brief Extension to use in the dialplan. */
1020         char exten[AST_MAX_EXTENSION];
1021         /*!
1022          * \brief Language configured for calls.
1023          * \note The "language" string read in from chan_dahdi.conf
1024          */
1025         char language[MAX_LANGUAGE];
1026         /*!
1027          * \brief The configured music-on-hold class to use for calls.
1028          * \note The "musicclass" or "mohinterpret" or "musiconhold" string read in from chan_dahdi.conf
1029          */
1030         char mohinterpret[MAX_MUSICCLASS];
1031         /*!
1032          * \brief Suggested music-on-hold class for peer channel to use for calls.
1033          * \note The "mohsuggest" string read in from chan_dahdi.conf
1034          */
1035         char mohsuggest[MAX_MUSICCLASS];
1036         char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
1037 #if defined(PRI_ANI) || defined(HAVE_SS7)
1038         /*! \brief Automatic Number Identification number (Alternate PRI caller ID number) */
1039         char cid_ani[AST_MAX_EXTENSION];
1040 #endif
1041         /*! \brief Automatic Number Identification code from PRI */
1042         int cid_ani2;
1043         /*! \brief Caller ID number from an incoming call. */
1044         char cid_num[AST_MAX_EXTENSION];
1045         /*! \brief Caller ID Q.931 TON/NPI field values.  Set by PRI. Zero otherwise. */
1046         int cid_ton;
1047         /*! \brief Caller ID name from an incoming call. */
1048         char cid_name[AST_MAX_EXTENSION];
1049         char *origcid_num;                              /*!< malloced original callerid */
1050         char *origcid_name;                             /*!< malloced original callerid */
1051         /*! \brief Call waiting number. */
1052         char callwait_num[AST_MAX_EXTENSION];
1053         /*! \brief Call waiting name. */
1054         char callwait_name[AST_MAX_EXTENSION];
1055         /*! \brief Redirecting Directory Number Information Service (RDNIS) number */
1056         char rdnis[AST_MAX_EXTENSION];
1057         /*! \brief Dialed Number Identifier */
1058         char dnid[AST_MAX_EXTENSION];
1059         /*!
1060          * \brief Bitmapped groups this belongs to.
1061          * \note The "group" bitmapped group string read in from chan_dahdi.conf
1062          */
1063         ast_group_t group;
1064         /*! \brief Active PCM encoding format: DAHDI_LAW_ALAW or DAHDI_LAW_MULAW */
1065         int law;
1066         int confno;                                     /*!< Our conference */
1067         int confusers;                                  /*!< Who is using our conference */
1068         int propconfno;                                 /*!< Propagated conference number */
1069         /*!
1070          * \brief Bitmapped call groups this belongs to.
1071          * \note The "callgroup" bitmapped group string read in from chan_dahdi.conf
1072          */
1073         ast_group_t callgroup;
1074         /*!
1075          * \brief Bitmapped pickup groups this belongs to.
1076          * \note The "pickupgroup" bitmapped group string read in from chan_dahdi.conf
1077          */
1078         ast_group_t pickupgroup;
1079         /*!
1080          * \brief Channel variable list with associated values to set when a channel is created.
1081          * \note The "setvar" strings read in from chan_dahdi.conf
1082          */
1083         struct ast_variable *vars;
1084         int channel;                                    /*!< Channel Number */
1085         int span;                                       /*!< Span number */
1086         time_t guardtime;                               /*!< Must wait this much time before using for new call */
1087         int cid_signalling;                             /*!< CID signalling type bell202 or v23 */
1088         int cid_start;                                  /*!< CID start indicator, polarity or ring or DTMF without warning event */
1089         int dtmfcid_holdoff_state;              /*!< State indicator that allows for line to settle before checking for dtmf energy */
1090         struct timeval  dtmfcid_delay;  /*!< Time value used for allow line to settle */
1091         int callingpres;                                /*!< The value of calling presentation that we're going to use when placing a PRI call */
1092         int callwaitingrepeat;                          /*!< How many samples to wait before repeating call waiting */
1093         int cidcwexpire;                                /*!< When to expire our muting for CID/CW */
1094         /*! \brief Analog caller ID waveform sample buffer */
1095         unsigned char *cidspill;
1096         /*! \brief Position in the cidspill buffer to send out next. */
1097         int cidpos;
1098         /*! \brief Length of the cidspill buffer containing samples. */
1099         int cidlen;
1100         /*! \brief Ring timeout timer?? */
1101         int ringt;
1102         /*!
1103          * \brief Ring timeout base.
1104          * \note Value computed indirectly from "ringtimeout" read in from chan_dahdi.conf
1105          */
1106         int ringt_base;
1107         /*!
1108          * \brief Number of most significant digits/characters to strip from the dialed number.
1109          * \note Feature is deprecated.  Use dialplan logic.
1110          * \note The characters are stripped before the PRI TON/NPI prefix
1111          * characters are processed.
1112          */
1113         int stripmsd;
1114         /*! \brief BOOLEAN. XXX Meaning what?? */
1115         int callwaitcas;
1116         /*! \brief Number of call waiting rings. */
1117         int callwaitrings;
1118         /*! \brief Echo cancel parameters. */
1119         struct {
1120                 struct dahdi_echocanparams head;
1121                 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
1122         } echocancel;
1123         /*!
1124          * \brief Echo training time. 0 = disabled
1125          * \note Set from the "echotraining" value read in from chan_dahdi.conf
1126          */
1127         int echotraining;
1128         /*! \brief Filled with 'w'.  XXX Purpose?? */
1129         char echorest[20];
1130         /*!
1131          * \brief Number of times to see "busy" tone before hanging up.
1132          * \note Set from the "busycount" value read in from chan_dahdi.conf
1133          */
1134         int busycount;
1135         /*!
1136          * \brief Length of "busy" tone on time.
1137          * \note Set from the "busypattern" value read in from chan_dahdi.conf
1138          */
1139         int busy_tonelength;
1140         /*!
1141          * \brief Length of "busy" tone off time.
1142          * \note Set from the "busypattern" value read in from chan_dahdi.conf
1143          */
1144         int busy_quietlength;
1145         /*!
1146          * \brief Bitmapped call progress detection flags. CALLPROGRESS_xxx values.
1147          * \note Bits set from the "callprogress" and "faxdetect" values read in from chan_dahdi.conf
1148          */
1149         int callprogress;
1150         /*!
1151          * \brief Number of milliseconds to wait for dialtone.
1152          * \note Set from the "waitfordialtone" value read in from chan_dahdi.conf
1153          */
1154         int waitfordialtone;
1155         struct timeval waitingfordt;                    /*!< Time we started waiting for dialtone */
1156         struct timeval flashtime;                       /*!< Last flash-hook time */
1157         /*! \brief Opaque DSP configuration structure. */
1158         struct ast_dsp *dsp;
1159         /*! \brief DAHDI dial operation command struct for ioctl() call. */
1160         struct dahdi_dialoperation dop;
1161         int whichwink;                                  /*!< SIG_FEATDMF_TA Which wink are we on? */
1162         /*! \brief Second part of SIG_FEATDMF_TA wink operation. */
1163         char finaldial[64];
1164         char accountcode[AST_MAX_ACCOUNT_CODE];         /*!< Account code */
1165         int amaflags;                                   /*!< AMA Flags */
1166         struct tdd_state *tdd;                          /*!< TDD flag */
1167         /*! \brief Accumulated call forwarding number. */
1168         char call_forward[AST_MAX_EXTENSION];
1169         /*!
1170          * \brief Voice mailbox location.
1171          * \note Set from the "mailbox" string read in from chan_dahdi.conf
1172          */
1173         char mailbox[AST_MAX_EXTENSION];
1174         /*! \brief Opaque event subscription parameters for message waiting indication support. */
1175         struct ast_event_sub *mwi_event_sub;
1176         /*! \brief Delayed dialing for E911.  Overlap digits for ISDN. */
1177         char dialdest[256];
1178 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1179         struct dahdi_vmwi_info mwisend_setting;                         /*!< Which VMWI methods to use */
1180         unsigned int mwisend_fsk: 1;            /*! Variable for enabling FSK MWI handling in chan_dahdi */
1181         unsigned int mwisend_rpas:1;            /*! Variable for enabling Ring Pulse Alert before MWI FSK Spill */
1182 #endif
1183         int distinctivering;                            /*!< Which distinctivering to use */
1184         int dtmfrelax;                                  /*!< whether to run in relaxed DTMF mode */
1185         /*! \brief Holding place for event injected from outside normal operation. */
1186         int fake_event;
1187         /*!
1188          * \brief Minimal time period (ms) between the answer polarity
1189          * switch and hangup polarity switch.
1190          */
1191         int polarityonanswerdelay;
1192         /*! \brief Start delay time if polarityonanswerdelay is nonzero. */
1193         struct timeval polaritydelaytv;
1194         /*!
1195          * \brief Send caller ID on FXS after this many rings. Set to 1 for US.
1196          * \note Set from the "sendcalleridafter" value read in from chan_dahdi.conf
1197          */
1198         int sendcalleridafter;
1199         /*! \brief Current line interface polarity. POLARITY_IDLE, POLARITY_REV */
1200         int polarity;
1201         /*! \brief DSP feature flags: DSP_FEATURE_xxx */
1202         int dsp_features;
1203 #ifdef HAVE_SS7
1204         /*! \brief SS7 control parameters */
1205         struct dahdi_ss7 *ss7;
1206         /*! \brief Opaque libss7 call control structure */
1207         struct isup_call *ss7call;
1208         char charge_number[50];
1209         char gen_add_number[50];
1210         char gen_dig_number[50];
1211         char orig_called_num[50];
1212         char redirecting_num[50];
1213         char generic_name[50];
1214         unsigned char gen_add_num_plan;
1215         unsigned char gen_add_nai;
1216         unsigned char gen_add_pres_ind;
1217         unsigned char gen_add_type;
1218         unsigned char gen_dig_type;
1219         unsigned char gen_dig_scheme;
1220         char jip_number[50];
1221         unsigned char lspi_type;
1222         unsigned char lspi_scheme;
1223         unsigned char lspi_context;
1224         char lspi_ident[50];
1225         unsigned int call_ref_ident;
1226         unsigned int call_ref_pc;
1227         unsigned char calling_party_cat;
1228         int transcap;
1229         int cic;                                                        /*!< CIC associated with channel */
1230         unsigned int dpc;                                               /*!< CIC's DPC */
1231         unsigned int loopedback:1;
1232 #endif
1233 #ifdef HAVE_OPENR2
1234         struct dahdi_mfcr2 *mfcr2;
1235         openr2_chan_t *r2chan;
1236         openr2_calling_party_category_t mfcr2_recvd_category;
1237         openr2_calling_party_category_t mfcr2_category;
1238         int mfcr2_dnis_index;
1239         int mfcr2_ani_index;
1240         int mfcr2call:1;
1241         int mfcr2_answer_pending:1;
1242         int mfcr2_charge_calls:1;
1243         int mfcr2_allow_collect_calls:1;
1244         int mfcr2_forced_release:1;
1245         int mfcr2_dnis_matched:1;
1246         int mfcr2_call_accepted:1;
1247         int mfcr2_accept_on_offer:1;
1248 #endif
1249         /*! \brief DTMF digit in progress.  0 when no digit in progress. */
1250         char begindigit;
1251         /*! \brief TRUE if confrence is muted. */
1252         int muting;
1253         void *sig_pvt;
1254 };
1255
1256 static struct dahdi_pvt *iflist = NULL; /*!< Main interface list start */
1257 static struct dahdi_pvt *ifend = NULL;  /*!< Main interface list end */
1258
1259 /*! \brief Channel configuration from chan_dahdi.conf .
1260  * This struct is used for parsing the [channels] section of chan_dahdi.conf.
1261  * Generally there is a field here for every possible configuration item.
1262  *
1263  * The state of fields is saved along the parsing and whenever a 'channel'
1264  * statement is reached, the current dahdi_chan_conf is used to configure the
1265  * channel (struct dahdi_pvt)
1266  *
1267  * \see dahdi_chan_init for the default values.
1268  */
1269 struct dahdi_chan_conf {
1270         struct dahdi_pvt chan;
1271 #ifdef HAVE_PRI
1272         struct dahdi_pri pri;
1273 #endif
1274
1275 #ifdef HAVE_SS7
1276         struct dahdi_ss7 ss7;
1277 #endif
1278
1279 #ifdef HAVE_OPENR2
1280         struct dahdi_mfcr2_conf mfcr2;
1281 #endif
1282         struct dahdi_params timing;
1283         int is_sig_auto; /*!< Use channel signalling from DAHDI? */
1284
1285         /*!
1286          * \brief The serial port to listen for SMDI data on
1287          * \note Set from the "smdiport" string read in from chan_dahdi.conf
1288          */
1289         char smdi_port[SMDI_MAX_FILENAME_LEN];
1290 };
1291
1292 /*! returns a new dahdi_chan_conf with default values (by-value) */
1293 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
1294 {
1295         /* recall that if a field is not included here it is initialized
1296          * to 0 or equivalent
1297          */
1298         struct dahdi_chan_conf conf = {
1299 #ifdef HAVE_PRI
1300                 .pri.pri = {
1301                         .nsf = PRI_NSF_NONE,
1302                         .switchtype = PRI_SWITCH_NI2,
1303                         .dialplan = PRI_UNKNOWN + 1,
1304                         .localdialplan = PRI_NATIONAL_ISDN + 1,
1305                         .nodetype = PRI_CPE,
1306                         .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
1307
1308                         .minunused = 2,
1309                         .idleext = "",
1310                         .idledial = "",
1311                         .internationalprefix = "",
1312                         .nationalprefix = "",
1313                         .localprefix = "",
1314                         .privateprefix = "",
1315                         .unknownprefix = "",
1316                         .resetinterval = -1,
1317                 },
1318 #endif
1319 #ifdef HAVE_SS7
1320                 .ss7 = {
1321                         .called_nai = SS7_NAI_NATIONAL,
1322                         .calling_nai = SS7_NAI_NATIONAL,
1323                         .internationalprefix = "",
1324                         .nationalprefix = "",
1325                         .subscriberprefix = "",
1326                         .unknownprefix = ""
1327                 },
1328 #endif
1329 #ifdef HAVE_OPENR2
1330                 .mfcr2 = {
1331                         .variant = OR2_VAR_ITU,
1332                         .mfback_timeout = -1,
1333                         .metering_pulse_timeout = -1,
1334                         .max_ani = 10,
1335                         .max_dnis = 4,
1336                         .get_ani_first = -1,
1337 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
1338                         .skip_category_request = -1,
1339 #endif
1340                         .call_files = 0,
1341                         .allow_collect_calls = 0,
1342                         .charge_calls = 1,
1343                         .accept_on_offer = 1,
1344                         .forced_release = 0,
1345                         .double_answer = 0,
1346                         .immediate_accept = -1,
1347                         .logdir = "",
1348                         .r2proto_file = "",
1349                         .loglevel = OR2_LOG_ERROR | OR2_LOG_WARNING,
1350                         .category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER
1351                 },
1352 #endif
1353                 .chan = {
1354                         .context = "default",
1355                         .cid_num = "",
1356                         .cid_name = "",
1357                         .mohinterpret = "default",
1358                         .mohsuggest = "",
1359                         .parkinglot = "",
1360                         .transfertobusy = 1,
1361
1362                         .cid_signalling = CID_SIG_BELL,
1363                         .cid_start = CID_START_RING,
1364                         .dahditrcallerid = 0,
1365                         .use_callerid = 1,
1366                         .sig = -1,
1367                         .outsigmod = -1,
1368
1369                         .cid_rxgain = +5.0,
1370
1371                         .tonezone = -1,
1372
1373                         .echocancel.head.tap_length = 1,
1374
1375                         .busycount = 3,
1376
1377                         .accountcode = "",
1378
1379                         .mailbox = "",
1380
1381 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
1382                         .mwisend_fsk = 1,
1383 #endif
1384                         .polarityonanswerdelay = 600,
1385
1386                         .sendcalleridafter = DEFAULT_CIDRINGS,
1387
1388                         .buf_policy = DAHDI_POLICY_IMMEDIATE,
1389                         .buf_no = numbufs,
1390                         .usefaxbuffers = 0,
1391                 },
1392                 .timing = {
1393                         .prewinktime = -1,
1394                         .preflashtime = -1,
1395                         .winktime = -1,
1396                         .flashtime = -1,
1397                         .starttime = -1,
1398                         .rxwinktime = -1,
1399                         .rxflashtime = -1,
1400                         .debouncetime = -1
1401                 },
1402                 .is_sig_auto = 1,
1403                 .smdi_port = "/dev/ttyS0",
1404         };
1405
1406         return conf;
1407 }
1408
1409
1410 static struct ast_channel *dahdi_request(const char *type, int format, const struct ast_channel *requestor, void *data, int *cause);
1411 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
1412 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
1413 static int dahdi_sendtext(struct ast_channel *c, const char *text);
1414 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
1415 static int dahdi_hangup(struct ast_channel *ast);
1416 static int dahdi_answer(struct ast_channel *ast);
1417 static struct ast_frame *dahdi_read(struct ast_channel *ast);
1418 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
1419 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
1420 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
1421 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
1422 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
1423 static int dahdi_queryoption(struct ast_channel *chan, int option, void *data, int *datalen);
1424 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
1425 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value);
1426
1427 static const struct ast_channel_tech dahdi_tech = {
1428         .type = "DAHDI",
1429         .description = tdesc,
1430         .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
1431         .requester = dahdi_request,
1432         .send_digit_begin = dahdi_digit_begin,
1433         .send_digit_end = dahdi_digit_end,
1434         .send_text = dahdi_sendtext,
1435         .call = dahdi_call,
1436         .hangup = dahdi_hangup,
1437         .answer = dahdi_answer,
1438         .read = dahdi_read,
1439         .write = dahdi_write,
1440         .bridge = dahdi_bridge,
1441         .exception = dahdi_exception,
1442         .indicate = dahdi_indicate,
1443         .fixup = dahdi_fixup,
1444         .setoption = dahdi_setoption,
1445         .queryoption = dahdi_queryoption,
1446         .func_channel_read = dahdi_func_read,
1447         .func_channel_write = dahdi_func_write,
1448 };
1449
1450 #define GET_CHANNEL(p) ((p)->channel)
1451
1452 #define SIG_PRI_LIB_HANDLE_CASES        \
1453         SIG_PRI:                                                \
1454         case SIG_BRI:                                   \
1455         case SIG_BRI_PTMP
1456
1457 /*!
1458  * \internal
1459  * \brief Determine if sig_pri handles the signaling.
1460  * \since 1.6.3
1461  *
1462  * \param signaling Signaling to determine if is for sig_pri.
1463  *
1464  * \return TRUE if the signaling is for sig_pri.
1465  */
1466 static inline int dahdi_sig_pri_lib_handles(int signaling)
1467 {
1468         int handles;
1469
1470         switch (signaling) {
1471         case SIG_PRI_LIB_HANDLE_CASES:
1472                 handles = 1;
1473                 break;
1474         default:
1475                 handles = 0;
1476                 break;
1477         }
1478
1479         return handles;
1480 }
1481
1482 static enum analog_sigtype dahdisig_to_analogsig(int sig)
1483 {
1484         switch (sig) {
1485         case SIG_FXOLS:
1486                 return ANALOG_SIG_FXOLS;
1487         case SIG_FXOGS:
1488                 return ANALOG_SIG_FXOGS;
1489         case SIG_FXOKS:
1490                 return ANALOG_SIG_FXOKS;
1491         case SIG_FXSLS:
1492                 return ANALOG_SIG_FXSLS;
1493         case SIG_FXSGS:
1494                 return ANALOG_SIG_FXSGS;
1495         case SIG_FXSKS:
1496                 return ANALOG_SIG_FXSKS;
1497         case SIG_EMWINK:
1498                 return ANALOG_SIG_EMWINK;
1499         case SIG_EM:
1500                 return ANALOG_SIG_EM;
1501         case SIG_EM_E1:
1502                 return ANALOG_SIG_EM_E1;
1503         case SIG_FEATD:
1504                 return ANALOG_SIG_FEATD;
1505         case SIG_FEATDMF:
1506                 return ANALOG_SIG_FEATDMF;
1507         case SIG_E911:
1508                 return SIG_E911;
1509         case SIG_FGC_CAMA:
1510                 return ANALOG_SIG_FGC_CAMA;
1511         case SIG_FGC_CAMAMF:
1512                 return ANALOG_SIG_FGC_CAMAMF;
1513         case SIG_FEATB:
1514                 return ANALOG_SIG_FEATB;
1515         case SIG_SFWINK:
1516                 return ANALOG_SIG_SFWINK;
1517         case SIG_SF:
1518                 return ANALOG_SIG_SF;
1519         case SIG_SF_FEATD:
1520                 return ANALOG_SIG_SF_FEATD;
1521         case SIG_SF_FEATDMF:
1522                 return ANALOG_SIG_SF_FEATDMF;
1523         case SIG_FEATDMF_TA:
1524                 return ANALOG_SIG_FEATDMF_TA;
1525         case SIG_SF_FEATB:
1526                 return ANALOG_SIG_FEATB;
1527         default:
1528                 return -1;
1529         }
1530 }
1531
1532
1533 static int analog_tone_to_dahditone(enum analog_tone tone)
1534 {
1535         switch (tone) {
1536         case ANALOG_TONE_RINGTONE:
1537                 return DAHDI_TONE_RINGTONE;
1538         case ANALOG_TONE_STUTTER:
1539                 return DAHDI_TONE_STUTTER;
1540         case ANALOG_TONE_CONGESTION:
1541                 return DAHDI_TONE_CONGESTION;
1542         case ANALOG_TONE_DIALTONE:
1543                 return DAHDI_TONE_DIALTONE;
1544         case ANALOG_TONE_DIALRECALL:
1545                 return DAHDI_TONE_DIALRECALL;
1546         case ANALOG_TONE_INFO:
1547                 return DAHDI_TONE_INFO;
1548         default:
1549                 return -1;
1550         }
1551 }
1552
1553 static int analogsub_to_dahdisub(enum analog_sub analogsub)
1554 {
1555         int index;
1556
1557         switch (analogsub) {
1558         case ANALOG_SUB_REAL:
1559                 index = SUB_REAL;
1560                 break;
1561         case ANALOG_SUB_CALLWAIT:
1562                 index = SUB_CALLWAIT;
1563                 break;
1564         case ANALOG_SUB_THREEWAY:
1565                 index = SUB_THREEWAY;
1566                 break;
1567         default:
1568                 ast_log(LOG_ERROR, "Unidentified sub!\n");
1569                 index = SUB_REAL;
1570         }
1571
1572         return index;
1573 }
1574
1575 static enum analog_event dahdievent_to_analogevent(int event);
1576 static int bump_gains(struct dahdi_pvt *p);
1577 static int dahdi_setlinear(int dfd, int linear);
1578
1579 static int my_start_cid_detect(void *pvt, int cid_signalling)
1580 {
1581         struct dahdi_pvt *p = pvt;
1582         int index = SUB_REAL;
1583         p->cs = callerid_new(cid_signalling);
1584         if (!p->cs) {
1585                 ast_log(LOG_ERROR, "Unable to alloc callerid\n");
1586                 return -1;
1587         }
1588         bump_gains(p);
1589         dahdi_setlinear(p->subs[index].dfd, 0);
1590
1591         return 0;
1592 }
1593
1594 static int my_stop_cid_detect(void *pvt)
1595 {
1596         struct dahdi_pvt *p = pvt;
1597         int index = SUB_REAL;
1598         if (p->cs)
1599                 callerid_free(p->cs);
1600         dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
1601         return 0;
1602 }
1603
1604 static int my_get_callerid(void *pvt, char *namebuf, char *numbuf, enum analog_event *ev, size_t timeout)
1605 {
1606         struct dahdi_pvt *p = pvt;
1607         struct analog_pvt *analog_p = p->sig_pvt;
1608         struct pollfd poller;
1609         char *name, *num;
1610         int index = SUB_REAL;
1611         int res;
1612         unsigned char buf[256];
1613         int flags;
1614
1615         poller.fd = p->subs[SUB_REAL].dfd;
1616         poller.events = POLLPRI | POLLIN;
1617         poller.revents = 0;
1618
1619         res = poll(&poller, 1, timeout);
1620
1621         if (poller.revents & POLLPRI) {
1622                 *ev = dahdievent_to_analogevent(dahdi_get_event(p->subs[SUB_REAL].dfd));
1623                 return 1;
1624         }
1625
1626         if (poller.revents & POLLIN) {
1627                 /*** NOTES ***/
1628                 /* Change API: remove cid_signalling from get_callerid, add a new start_cid_detect and stop_cid_detect function
1629                  * to enable slin mode and allocate cid detector. get_callerid should be able to be called any number of times until
1630                  * either a timeout occurs or CID is detected (returns 0). returning 1 should be event received, and -1 should be
1631                  * a failure and die, and returning 2 means no event was received. */
1632                 res = read(p->subs[index].dfd, buf, sizeof(buf));
1633                 if (res < 0) {
1634                         if (errno != ELAST) {
1635                                 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
1636                                 callerid_free(p->cs);
1637                                 return -1;
1638                         }
1639                 }
1640
1641                 if (analog_p->ringt) {
1642                         analog_p->ringt--;
1643                 }
1644                 if (analog_p->ringt == 1) {
1645                         return -1;
1646                 }
1647
1648                 if (p->cid_signalling == CID_SIG_V23_JP) {
1649                         res = callerid_feed_jp(p->cs, buf, res, AST_LAW(p));
1650                 } else {
1651                         res = callerid_feed(p->cs, buf, res, AST_LAW(p));
1652                 }
1653
1654                 if (res < 0) {
1655                         ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
1656                         return -1;
1657                 }
1658
1659                 if (res == 1) {
1660                         callerid_get(p->cs, &name, &num, &flags);
1661                         if (name)
1662                                 ast_copy_string(namebuf, name, ANALOG_MAX_CID);
1663                         if (num)
1664                                 ast_copy_string(numbuf, num, ANALOG_MAX_CID);
1665
1666                         ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", num, name, flags);
1667                         return 0;
1668                 }
1669         }
1670
1671         *ev = ANALOG_EVENT_NONE;
1672         return 2;
1673 }
1674
1675 static const char *event2str(int event);
1676 static int restore_gains(struct dahdi_pvt *p);
1677
1678 static int my_distinctive_ring(struct ast_channel *chan, void *pvt, int idx, int *ringdata)
1679 {
1680         unsigned char buf[256];
1681         int distMatches;
1682         int curRingData[3];
1683         int receivedRingT;
1684         int counter1;
1685         int counter;
1686         int i;
1687         int res;
1688         int checkaftercid = 0;
1689
1690         struct dahdi_pvt *p = pvt;
1691         struct analog_pvt *analog_p = p->sig_pvt;
1692
1693         if (ringdata == NULL) {
1694                 ringdata = curRingData;
1695         } else {
1696                 checkaftercid = 1;
1697         }
1698
1699         /* We must have a ring by now, so, if configured, lets try to listen for
1700          * distinctive ringing */
1701         if ((checkaftercid && distinctiveringaftercid) || !checkaftercid) {
1702                 /* Clear the current ring data array so we dont have old data in it. */
1703                 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(ringdata); receivedRingT++)
1704                         ringdata[receivedRingT] = 0;
1705                 receivedRingT = 0;
1706                 if (checkaftercid && distinctiveringaftercid)
1707                         ast_verb(3, "Detecting post-CID distinctive ring\n");
1708                 /* Check to see if context is what it should be, if not set to be. */
1709                 else if (strcmp(p->context,p->defcontext) != 0) {
1710                         ast_copy_string(p->context, p->defcontext, sizeof(p->context));
1711                         ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
1712                 }
1713
1714                 for (;;) {
1715                         i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
1716                         if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
1717                                 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
1718                                 ast_hangup(chan);
1719                                 return 1;
1720                         }
1721                         if (i & DAHDI_IOMUX_SIGEVENT) {
1722                                 res = dahdi_get_event(p->subs[idx].dfd);
1723                                 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
1724                                 res = 0;
1725                                 /* Let us detect distinctive ring */
1726
1727                                 ringdata[receivedRingT] = analog_p->ringt;
1728
1729                                 if (analog_p->ringt < analog_p->ringt_base/2)
1730                                         break;
1731                                 /* Increment the ringT counter so we can match it against
1732                                    values in chan_dahdi.conf for distinctive ring */
1733                                 if (++receivedRingT == ARRAY_LEN(ringdata))
1734                                         break;
1735                         } else if (i & DAHDI_IOMUX_READ) {
1736                                 res = read(p->subs[idx].dfd, buf, sizeof(buf));
1737                                 if (res < 0) {
1738                                         if (errno != ELAST) {
1739                                                 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
1740                                                 ast_hangup(chan);
1741                                                 return 1;
1742                                         }
1743                                         break;
1744                                 }
1745                                 if (analog_p->ringt)
1746                                         analog_p->ringt--;
1747                                 if (analog_p->ringt == 1) {
1748                                         res = -1;
1749                                         break;
1750                                 }
1751                         }
1752                 }
1753         }
1754         if ((checkaftercid && usedistinctiveringdetection) || !checkaftercid) {
1755                 /* this only shows up if you have n of the dring patterns filled in */
1756                 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",ringdata[0],ringdata[1],ringdata[2]);
1757                 for (counter = 0; counter < 3; counter++) {
1758                 /* Check to see if the rings we received match any of the ones in chan_dahdi.conf for this channel */
1759                         distMatches = 0;
1760                         /* this only shows up if you have n of the dring patterns filled in */
1761                         ast_verb(3, "Checking %d,%d,%d\n",
1762                                         p->drings.ringnum[counter].ring[0],
1763                                         p->drings.ringnum[counter].ring[1],
1764                                         p->drings.ringnum[counter].ring[2]);
1765                         for (counter1 = 0; counter1 < 3; counter1++) {
1766                                 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
1767                                 if (p->drings.ringnum[counter].ring[counter1] == -1) {
1768                                         ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
1769                                         ringdata[counter1]);
1770                                         distMatches++;
1771                                 } else if (ringdata[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
1772                                                                                 ringdata[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
1773                                         ast_verb(3, "Ring pattern matched in range: %d to %d\n",
1774                                         (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
1775                                         (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
1776                                         distMatches++;
1777                                 }
1778                         }
1779
1780                         if (distMatches == 3) {
1781                                 /* The ring matches, set the context to whatever is for distinctive ring.. */
1782                                 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
1783                                 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
1784                                 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
1785                                 break;
1786                         }
1787                 }
1788         }
1789         /* Restore linear mode (if appropriate) for Caller*ID processing */
1790         dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
1791         restore_gains(p);
1792
1793         return 0;
1794 }
1795
1796 static int send_callerid(struct dahdi_pvt *p);
1797
1798 static int my_stop_callwait(void *pvt)
1799 {
1800         struct dahdi_pvt *p = pvt;
1801         p->callwaitingrepeat = 0;
1802         p->cidcwexpire = 0;
1803
1804         return 0;
1805 }
1806
1807 static int save_conference(struct dahdi_pvt *p);
1808
1809 static int my_callwait(void *pvt)
1810 {
1811         struct dahdi_pvt *p = pvt;
1812         p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1813         if (p->cidspill) {
1814                 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1815                 ast_free(p->cidspill);
1816         }
1817         if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1818                 return -1;
1819         save_conference(p);
1820         /* Silence */
1821         memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1822         if (!p->callwaitrings && p->callwaitingcallerid) {
1823                 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1824                 p->callwaitcas = 1;
1825                 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1826         } else {
1827                 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1828                 p->callwaitcas = 0;
1829                 p->cidlen = 2400 + READ_SIZE * 4;
1830         }
1831         p->cidpos = 0;
1832         send_callerid(p);
1833
1834         return 0;
1835 }
1836
1837 static int my_send_callerid(void *pvt, int cwcid, struct ast_callerid *cid)
1838 {
1839         struct dahdi_pvt *p = pvt;
1840
1841         ast_debug(2, "Starting cid spill\n");
1842
1843         if (p->cidspill) {
1844                 ast_log(LOG_WARNING, "cidspill already exists??\n");
1845                 ast_free(p->cidspill);
1846         }
1847
1848         if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1849                 if (cwcid == 0) {
1850                         p->cidlen = ast_callerid_generate(p->cidspill, cid->cid_name, cid->cid_num, AST_LAW(p));
1851                 } else {
1852                         p->callwaitcas = 0;
1853                         p->cidcwexpire = 0;
1854                         p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, cid->cid_name, cid->cid_num, AST_LAW(p));
1855                         p->cidlen += READ_SIZE * 4;
1856                 }
1857                 p->cidpos = 0;
1858                 send_callerid(p);
1859         }
1860         return 0;
1861 }
1862
1863 static int my_dsp_reset_and_flush_digits(void *pvt)
1864 {
1865         struct dahdi_pvt *p = pvt;
1866         if (p->dsp)
1867                 ast_dsp_digitreset(p->dsp);
1868
1869         return 0;
1870 }
1871
1872 static int my_dsp_set_digitmode(void *pvt, enum analog_dsp_digitmode mode)
1873 {
1874         struct dahdi_pvt *p = pvt;
1875
1876         if (p->channel == CHAN_PSEUDO)
1877                 ast_log(LOG_ERROR, "You have assumed incorrectly sir!\n");
1878
1879         if (mode == ANALOG_DIGITMODE_DTMF) {
1880                 /* If we do hardware dtmf, no need for a DSP */
1881                 if (p->hardwaredtmf) {
1882                         if (p->dsp) {
1883                                 ast_dsp_free(p->dsp);
1884                                 p->dsp = NULL;
1885                         }
1886                         return 0;
1887                 }
1888
1889                 if (!p->dsp) {
1890                         p->dsp = ast_dsp_new();
1891                         if (!p->dsp) {
1892                                 ast_log(LOG_ERROR, "Unable to allocate DSP\n");
1893                                 return -1;
1894                         }
1895                 }
1896
1897                 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
1898         } else if (mode == ANALOG_DIGITMODE_MF) {
1899                 if (!p->dsp) {
1900                         p->dsp = ast_dsp_new();
1901                         if (!p->dsp) {
1902                                 ast_log(LOG_ERROR, "Unable to allocate DSP\n");
1903                                 return -1;
1904                         }
1905                 }
1906                 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
1907         }
1908         return 0;
1909 }
1910
1911 static int dahdi_wink(struct dahdi_pvt *p, int index);
1912
1913 static int my_wink(void *pvt, enum analog_sub sub)
1914 {
1915         struct dahdi_pvt *p = pvt;
1916         int index = analogsub_to_dahdisub(sub);
1917         if (index != SUB_REAL) {
1918                 ast_log(LOG_ERROR, "We used a sub other than SUB_REAL (incorrect assumption sir)\n");
1919         }
1920         return dahdi_wink(p, index);
1921 }
1922
1923 static void wakeup_sub(struct dahdi_pvt *p, int a);
1924
1925 static int reset_conf(struct dahdi_pvt *p);
1926
1927 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted);
1928
1929 static void my_handle_dtmfup(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
1930 {
1931         struct ast_frame *f = *dest;
1932         struct dahdi_pvt *p = pvt;
1933         int idx = analogsub_to_dahdisub(analog_index);
1934
1935         ast_debug(1, "DTMF digit: %c on %s\n", f->subclass, ast->name);
1936
1937         if (f->subclass == 'f') {
1938                 /* Fax tone -- Handle and return NULL */
1939                 if ((p->callprogress & CALLPROGRESS_FAX) && !p->faxhandled) {
1940                         /* If faxbuffers are configured, use them for the fax transmission */
1941                         if (p->usefaxbuffers && !p->bufferoverrideinuse) {
1942                                 struct dahdi_bufferinfo bi = {
1943                                         .txbufpolicy = p->faxbuf_policy,
1944                                         .bufsize = p->bufsize,
1945                                         .numbufs = p->faxbuf_no
1946                                 };
1947                                 int res;
1948
1949                                 if ((res = ioctl(p->subs[idx].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
1950                                         ast_log(LOG_WARNING, "Channel '%s' unable to set buffer policy, reason: %s\n", ast->name, strerror(errno));
1951                                 } else {
1952                                         p->bufferoverrideinuse = 1;
1953                                 }
1954                         }
1955                         p->faxhandled = 1;
1956                         if (strcmp(ast->exten, "fax")) {
1957                                 const char *target_context = S_OR(ast->macrocontext, ast->context);
1958
1959                                 /* We need to unlock 'ast' here because ast_exists_extension has the
1960                                  * potential to start autoservice on the channel. Such action is prone
1961                                  * to deadlock.
1962                                  */
1963                                 ast_mutex_unlock(&p->lock);
1964                                 ast_channel_unlock(ast);
1965                                 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
1966                                         ast_channel_lock(ast);
1967                                         ast_mutex_lock(&p->lock);
1968                                         ast_verb(3, "Redirecting %s to fax extension\n", ast->name);
1969                                         /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
1970                                         pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
1971                                         if (ast_async_goto(ast, target_context, "fax", 1))
1972                                                 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
1973                                 } else {
1974                                         ast_channel_lock(ast);
1975                                         ast_mutex_lock(&p->lock);
1976                                         ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
1977                                 }
1978                         } else {
1979                                 ast_debug(1, "Already in a fax extension, not redirecting\n");
1980                         }
1981                 } else {
1982                         ast_debug(1, "Fax already handled\n");
1983                 }
1984                 dahdi_confmute(p, 0);
1985                 p->subs[idx].f.frametype = AST_FRAME_NULL;
1986                 p->subs[idx].f.subclass = 0;
1987                 *dest = &p->subs[idx].f;
1988         }
1989 }
1990
1991 static void my_lock_private(void *pvt)
1992 {
1993         struct dahdi_pvt *p = pvt;
1994         ast_mutex_lock(&p->lock);
1995 }
1996
1997 static void my_unlock_private(void *pvt)
1998 {
1999         struct dahdi_pvt *p = pvt;
2000         ast_mutex_unlock(&p->lock);
2001 }
2002
2003 /* linear_mode = 0 - turn linear mode off, >0 - turn linear mode on
2004 *       returns the last value of the linear setting 
2005 */ 
2006 static int my_set_linear_mode(void *pvt, int idx, int linear_mode)
2007 {
2008         struct dahdi_pvt *p = pvt;
2009         int oldval;
2010         
2011     if (0 > linear_mode || !dahdi_setlinear(p->subs[idx].dfd, linear_mode)) {
2012         return -1;
2013     }
2014         oldval = p->subs[idx].linear;
2015         p->subs[idx].linear = linear_mode;
2016         return oldval;
2017 }
2018
2019 static int get_alarms(struct dahdi_pvt *p);
2020 static void handle_alarms(struct dahdi_pvt *p, int alms);
2021 static void my_get_and_handle_alarms(void *pvt)
2022 {
2023         int res;
2024         struct dahdi_pvt *p = pvt;
2025
2026         res = get_alarms(p);
2027         handle_alarms(p, res);
2028 }
2029
2030 static void *my_get_sigpvt_bridged_channel(struct ast_channel *chan)
2031 {
2032         struct dahdi_pvt *p = ast_bridged_channel(chan)->tech_pvt;
2033         if (p)
2034                 return p->sig_pvt;
2035         else
2036                 return NULL;
2037 }
2038
2039 static int my_get_sub_fd(void *pvt, enum analog_sub sub)
2040 {
2041         struct dahdi_pvt *p = pvt;
2042         int dahdi_sub = analogsub_to_dahdisub(sub);
2043         return p->subs[dahdi_sub].dfd;
2044 }
2045
2046 static void my_set_cadence(void *pvt, int *cidrings, struct ast_channel *ast)
2047 {
2048         struct dahdi_pvt *p = pvt;
2049
2050         /* Choose proper cadence */
2051         if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2052                 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
2053                         ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
2054                 *cidrings = cidrings[p->distinctivering - 1];
2055         } else {
2056                 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
2057                         ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
2058                 *cidrings = p->sendcalleridafter;
2059         }
2060 }
2061
2062 static void my_set_dialing(void *pvt, int flag)
2063 {
2064         struct dahdi_pvt *p = pvt;
2065         p->dialing = flag;
2066 }
2067
2068 static void my_set_ringtimeout(void *pvt, int ringt)
2069 {
2070         struct dahdi_pvt *p = pvt;
2071         p->ringt = ringt;
2072 }
2073
2074 static void my_set_waitingfordt(void *pvt, struct ast_channel *ast)
2075 {
2076         struct dahdi_pvt *p = pvt;
2077
2078         if (p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp) {
2079                 ast_log(LOG_DEBUG, "Defer dialing for %dms or dialtone\n", p->waitfordialtone);
2080                 gettimeofday(&p->waitingfordt, NULL);
2081                 ast_setstate(ast, AST_STATE_OFFHOOK);
2082         }
2083 }
2084
2085 static int my_check_waitingfordt(void *pvt)
2086 {
2087         struct dahdi_pvt *p = pvt;
2088
2089         if (p->waitingfordt.tv_usec) {
2090                 return 1;
2091         }
2092
2093         return 0;
2094 }
2095
2096 static void my_set_confirmanswer(void *pvt, int flag)
2097 {
2098         struct dahdi_pvt *p = pvt;
2099         p->confirmanswer = flag;
2100 }
2101
2102 static int my_check_confirmanswer(void *pvt)
2103 {
2104         struct dahdi_pvt *p = pvt;
2105         if (p->confirmanswer) {
2106                 return 1;
2107         }
2108
2109         return 0;
2110 }
2111
2112 static void my_cancel_cidspill(void *pvt)
2113 {
2114         struct dahdi_pvt *p = pvt;
2115         if (p->cidspill) {
2116                 ast_free(p->cidspill);
2117                 p->cidspill = NULL;
2118         }
2119 }
2120
2121 static int my_confmute(void *pvt, int mute)
2122 {
2123         struct dahdi_pvt *p = pvt;
2124         return dahdi_confmute(p, mute);
2125 }
2126
2127 static void my_set_pulsedial(void *pvt, int flag)
2128 {
2129         struct dahdi_pvt *p = pvt;
2130         p->pulsedial = flag;
2131 }
2132
2133 static void my_increase_ss_count(void)
2134 {
2135         ast_mutex_lock(&ss_thread_lock);
2136         ss_thread_count++;
2137         ast_mutex_unlock(&ss_thread_lock);
2138 }
2139
2140 static void my_decrease_ss_count(void)
2141 {
2142         ast_mutex_lock(&ss_thread_lock);
2143         ss_thread_count--;
2144         ast_cond_signal(&ss_thread_complete);
2145         ast_mutex_unlock(&ss_thread_lock);
2146 }
2147
2148 static void my_all_subchannels_hungup(void *pvt)
2149 {
2150         struct dahdi_pvt *p = pvt;
2151         int res, law;
2152
2153         p->faxhandled = 0;
2154         p->didtdd = 0;
2155
2156         if (p->dsp) {
2157                 ast_dsp_free(p->dsp);
2158                 p->dsp = NULL;
2159         }
2160
2161         law = DAHDI_LAW_DEFAULT;
2162         res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
2163         if (res < 0)
2164                 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
2165
2166         dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
2167
2168 #if 1
2169         {
2170         int i;
2171         p->owner = NULL;
2172         /* Cleanup owners here */
2173         for (i = 0; i < 3; i++) {
2174                 p->subs[i].owner = NULL;
2175         }
2176         }
2177 #endif
2178
2179         reset_conf(p);
2180         if (num_restart_pending == 0) {
2181                 restart_monitor();
2182         }
2183 }
2184
2185 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index);
2186
2187 static int my_conf_del(void *pvt, enum analog_sub sub)
2188 {
2189         struct dahdi_pvt *p = pvt;
2190         int x = analogsub_to_dahdisub(sub);
2191
2192         return conf_del(p, &p->subs[x], x);
2193 }
2194
2195 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index, int slavechannel);
2196
2197 static int my_conf_add(void *pvt, enum analog_sub sub)
2198 {
2199         struct dahdi_pvt *p = pvt;
2200         int x = analogsub_to_dahdisub(sub);
2201
2202         return conf_add(p, &p->subs[x], x, 0);
2203 }
2204
2205 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out);
2206
2207 static int my_complete_conference_update(void *pvt, int needconference)
2208 {
2209         struct dahdi_pvt *p = pvt;
2210         int needconf = needconference;
2211         int x;
2212         int useslavenative;
2213         struct dahdi_pvt *slave = NULL;
2214
2215         useslavenative = isslavenative(p, &slave);
2216
2217         /* If we have a slave, add him to our conference now. or DAX
2218            if this is slave native */
2219         for (x = 0; x < MAX_SLAVES; x++) {
2220                 if (p->slaves[x]) {
2221                         if (useslavenative)
2222                                 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
2223                         else {
2224                                 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
2225                                 needconf++;
2226                         }
2227                 }
2228         }
2229         /* If we're supposed to be in there, do so now */
2230         if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
2231                 if (useslavenative)
2232                         conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
2233                 else {
2234                         conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
2235                         needconf++;
2236                 }
2237         }
2238         /* If we have a master, add ourselves to his conference */
2239         if (p->master) {
2240                 if (isslavenative(p->master, NULL)) {
2241                         conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
2242                 } else {
2243                         conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
2244                 }
2245         }
2246         if (!needconf) {
2247                 /* Nobody is left (or should be left) in our conference.
2248                    Kill it. */
2249                 p->confno = -1;
2250         }
2251
2252         return 0;
2253 }
2254
2255 static int check_for_conference(struct dahdi_pvt *p);
2256
2257 static int my_check_for_conference(void *pvt)
2258 {
2259         struct dahdi_pvt *p = pvt;
2260         return check_for_conference(p);
2261 }
2262
2263 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)
2264 {
2265         struct dahdi_pvt *p = pvt;
2266         int da, db;
2267         int tchan;
2268
2269         da = analogsub_to_dahdisub(a);
2270         db = analogsub_to_dahdisub(b);
2271
2272         tchan = p->subs[da].chan;
2273
2274         p->subs[da].chan = p->subs[db].chan;
2275
2276         p->subs[db].chan = tchan;
2277
2278         if (ast_a)
2279                 ast_channel_set_fd(ast_a, 0, p->subs[da].dfd);
2280         if (ast_b)
2281                 ast_channel_set_fd(ast_b, 0, p->subs[db].dfd);
2282
2283         p->subs[da].owner = ast_a;
2284         p->subs[db].owner = ast_b;
2285
2286         wakeup_sub(p, a);
2287         wakeup_sub(p, b);
2288
2289         return;
2290 }
2291
2292 static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int, const char *);
2293
2294 static struct ast_channel *my_new_analog_ast_channel(void *pvt, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
2295 {
2296         struct dahdi_pvt *p = pvt;
2297         int dsub = analogsub_to_dahdisub(sub);
2298
2299         return dahdi_new(p, state, startpbx, dsub, 0, 0, requestor ? requestor->linkedid : "");
2300 }
2301
2302 #if defined(HAVE_PRI) || defined(HAVE_SS7)
2303 static int dahdi_setlaw(int dfd, int law)
2304 {
2305         int res;
2306         res = ioctl(dfd, DAHDI_SETLAW, &law);
2307         if (res)
2308                 return res;
2309         return 0;
2310 }
2311 #endif
2312
2313 #if defined(HAVE_PRI)
2314 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)
2315 {
2316         struct dahdi_pvt *p = pvt;
2317         int audio;
2318         int newlaw = -1;
2319
2320         /* Set to audio mode at this point */
2321         audio = 1;
2322         if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &audio) == -1)
2323                 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, audio, strerror(errno));
2324
2325         if (law != SIG_PRI_DEFLAW) {
2326                 dahdi_setlaw(p->subs[SUB_REAL].dfd, (law == SIG_PRI_ULAW) ? DAHDI_LAW_MULAW : DAHDI_LAW_ALAW);
2327         }
2328
2329         ast_copy_string(p->exten, exten, sizeof(p->exten));
2330
2331         switch (law) {
2332                 case SIG_PRI_DEFLAW:
2333                         newlaw = 0;
2334                         break;
2335                 case SIG_PRI_ALAW:
2336                         newlaw = DAHDI_LAW_ALAW;
2337                         break;
2338                 case SIG_PRI_ULAW:
2339                         newlaw = DAHDI_LAW_MULAW;
2340                         break;
2341         }
2342         return dahdi_new(p, state, startpbx, SUB_REAL, newlaw, transfercapability, requestor ? requestor->linkedid : "");
2343 }
2344 #endif  /* defined(HAVE_PRI) */
2345
2346 static int unalloc_sub(struct dahdi_pvt *p, int x);
2347
2348 static int my_unallocate_sub(void *pvt, enum analog_sub analogsub)
2349 {
2350         struct dahdi_pvt *p = pvt;
2351
2352         return unalloc_sub(p, analogsub_to_dahdisub(analogsub));
2353 }
2354
2355 static int alloc_sub(struct dahdi_pvt *p, int x);
2356
2357 static int my_allocate_sub(void *pvt, enum analog_sub analogsub)
2358 {
2359         struct dahdi_pvt *p = pvt;
2360
2361         return alloc_sub(p, analogsub_to_dahdisub(analogsub));
2362 }
2363
2364 static int has_voicemail(struct dahdi_pvt *p);
2365
2366 static int my_has_voicemail(void *pvt)
2367 {
2368         struct dahdi_pvt *p = pvt;
2369
2370         return has_voicemail(p);
2371 }
2372
2373 static int my_play_tone(void *pvt, enum analog_sub sub, enum analog_tone tone)
2374 {
2375         struct dahdi_pvt *p = pvt;
2376         int index;
2377
2378         index = analogsub_to_dahdisub(sub);
2379
2380         return tone_zone_play_tone(p->subs[index].dfd, analog_tone_to_dahditone(tone));
2381 }
2382
2383 static enum analog_event dahdievent_to_analogevent(int event)
2384 {
2385         enum analog_event res;
2386
2387         switch (event) {
2388         case DAHDI_EVENT_ONHOOK:
2389                 res = ANALOG_EVENT_ONHOOK;
2390                 break;
2391         case DAHDI_EVENT_RINGOFFHOOK:
2392                 res = ANALOG_EVENT_RINGOFFHOOK;
2393                 break;
2394         case DAHDI_EVENT_WINKFLASH:
2395                 res = ANALOG_EVENT_WINKFLASH;
2396                 break;
2397         case DAHDI_EVENT_ALARM:
2398                 res = ANALOG_EVENT_ALARM;
2399                 break;
2400         case DAHDI_EVENT_NOALARM:
2401                 res = ANALOG_EVENT_NOALARM;
2402                 break;
2403         case DAHDI_EVENT_DIALCOMPLETE:
2404                 res = ANALOG_EVENT_DIALCOMPLETE;
2405                 break;
2406         case DAHDI_EVENT_RINGERON:
2407                 res = ANALOG_EVENT_RINGERON;
2408                 break;
2409         case DAHDI_EVENT_RINGEROFF:
2410                 res = ANALOG_EVENT_RINGEROFF;
2411                 break;
2412         case DAHDI_EVENT_HOOKCOMPLETE:
2413                 res = ANALOG_EVENT_HOOKCOMPLETE;
2414                 break;
2415         case DAHDI_EVENT_PULSE_START:
2416                 res = ANALOG_EVENT_PULSE_START;
2417                 break;
2418         case DAHDI_EVENT_POLARITY:
2419                 res = ANALOG_EVENT_POLARITY;
2420                 break;
2421         case DAHDI_EVENT_RINGBEGIN:
2422                 res = ANALOG_EVENT_RINGBEGIN;
2423                 break;
2424         case DAHDI_EVENT_EC_DISABLED:
2425                 res = ANALOG_EVENT_EC_DISABLED;
2426                 break;
2427         case DAHDI_EVENT_REMOVED:
2428                 res = ANALOG_EVENT_REMOVED;
2429                 break;
2430         case DAHDI_EVENT_NEONMWI_ACTIVE:
2431                 res = ANALOG_EVENT_NEONMWI_ACTIVE;
2432                 break;
2433         case DAHDI_EVENT_NEONMWI_INACTIVE:
2434                 res = ANALOG_EVENT_NEONMWI_INACTIVE;
2435                 break;
2436 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
2437         case DAHDI_EVENT_TX_CED_DETECTED:
2438                 res = ANALOG_EVENT_TX_CED_DETECTED;
2439                 break;
2440         case DAHDI_EVENT_RX_CED_DETECTED:
2441                 res = ANALOG_EVENT_RX_CED_DETECTED;
2442                 break;
2443         case DAHDI_EVENT_EC_NLP_DISABLED:
2444                 res = ANALOG_EVENT_EC_NLP_DISABLED;
2445                 break;
2446         case DAHDI_EVENT_EC_NLP_ENABLED:
2447                 res = ANALOG_EVENT_EC_NLP_ENABLED;
2448                 break;
2449 #endif
2450         case DAHDI_EVENT_PULSEDIGIT:
2451                 res = ANALOG_EVENT_PULSEDIGIT;
2452                 break;
2453         case DAHDI_EVENT_DTMFDOWN:
2454                 res = ANALOG_EVENT_DTMFDOWN;
2455                 break;
2456         case DAHDI_EVENT_DTMFUP:
2457                 res = ANALOG_EVENT_DTMFUP;
2458                 break;
2459         default:
2460                 switch(event & 0xFFFF0000) {
2461                 case DAHDI_EVENT_PULSEDIGIT:
2462                 case DAHDI_EVENT_DTMFDOWN:
2463                 case DAHDI_EVENT_DTMFUP:
2464                         /* The event includes a digit number in the low word.
2465                          * Converting it to a 'enum analog_event' would remove
2466                          * that information. Thus it is returned as-is.
2467                          */
2468                         return event;
2469                 }
2470
2471                 res = ANALOG_EVENT_ERROR;
2472                 break;
2473         }
2474
2475         return res;
2476 }
2477
2478 static inline int dahdi_wait_event(int fd);
2479
2480 static int my_wait_event(void *pvt)
2481 {
2482         struct dahdi_pvt *p = pvt;
2483
2484         return dahdi_wait_event(p->subs[SUB_REAL].dfd);
2485 }
2486
2487 static int my_get_event(void *pvt)
2488 {
2489         struct dahdi_pvt *p = pvt;
2490         int res;
2491
2492         if (p->fake_event) {
2493                 res = p->fake_event;
2494                 p->fake_event = 0;
2495         } else
2496                 res = dahdi_get_event(p->subs[SUB_REAL].dfd);
2497
2498         return dahdievent_to_analogevent(res);
2499 }
2500
2501 static int my_is_off_hook(void *pvt)
2502 {
2503         struct dahdi_pvt *p = pvt;
2504         int res;
2505         struct dahdi_params par;
2506
2507         memset(&par, 0, sizeof(par));
2508
2509         if (p->subs[SUB_REAL].dfd > -1)
2510                 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
2511         else {
2512                 /* Assume not off hook on CVRS */
2513                 res = 0;
2514                 par.rxisoffhook = 0;
2515         }
2516         if (res) {
2517                 ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno));
2518         }
2519
2520         if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
2521                 /* When "onhook" that means no battery on the line, and thus
2522                 it is out of service..., if it's on a TDM card... If it's a channel
2523                 bank, there is no telling... */
2524                 return (par.rxbits > -1) || par.rxisoffhook;
2525         }
2526
2527         return par.rxisoffhook;
2528 }
2529
2530 static void dahdi_enable_ec(struct dahdi_pvt *p);
2531 static void dahdi_disable_ec(struct dahdi_pvt *p);
2532
2533 static int my_set_echocanceller(void *pvt, int enable)
2534 {
2535         struct dahdi_pvt *p = pvt;
2536
2537         if (enable)
2538                 dahdi_enable_ec(p);
2539         else
2540                 dahdi_disable_ec(p);
2541
2542         return 0;
2543 }
2544
2545 static int dahdi_ring_phone(struct dahdi_pvt *p);
2546
2547 static int my_ring(void *pvt)
2548 {
2549         struct dahdi_pvt *p = pvt;
2550
2551         return dahdi_ring_phone(p);
2552 }
2553
2554 static int my_flash(void *pvt)
2555 {
2556         struct dahdi_pvt *p = pvt;
2557         int func = DAHDI_FLASH;
2558         return ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &func);
2559 }
2560
2561 static inline int dahdi_set_hook(int fd, int hs);
2562
2563 static int my_off_hook(void *pvt)
2564 {
2565         struct dahdi_pvt *p = pvt;
2566         return dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
2567 }
2568
2569 static int my_start(void *pvt)
2570 {
2571         struct dahdi_pvt *p = pvt;
2572         int x = DAHDI_START;
2573
2574         return ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
2575 }
2576
2577 static int my_dial_digits(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop)
2578 {
2579         int index = analogsub_to_dahdisub(sub);
2580         int res;
2581         struct dahdi_pvt *p = pvt;
2582         struct dahdi_dialoperation ddop;
2583
2584         if (dop->op != ANALOG_DIAL_OP_REPLACE) {
2585                 ast_log(LOG_ERROR, "Fix the dial_digits callback!\n");
2586                 return -1;
2587         }
2588
2589         if (sub != ANALOG_SUB_REAL)
2590                 printf("Trying to dial digits on sub %d\n", sub);
2591
2592         ddop.op = DAHDI_DIAL_OP_REPLACE;
2593         strncpy(ddop.dialstr, dop->dialstr, sizeof(ddop.dialstr));
2594
2595         printf("Dialing %s on %d\n", ddop.dialstr, p->channel);
2596
2597         res = ioctl(p->subs[index].dfd, DAHDI_DIAL, &ddop);
2598
2599         if (res == -1)
2600                 ast_log(LOG_DEBUG, "DAHDI_DIAL ioctl failed on %s: %s\n", p->owner->name, strerror(errno));
2601
2602         return res;
2603 }
2604
2605 static void dahdi_train_ec(struct dahdi_pvt *p);
2606
2607 static int my_train_echocanceller(void *pvt)
2608 {
2609         struct dahdi_pvt *p = pvt;
2610
2611         dahdi_train_ec(p);
2612
2613         return 0;
2614 }
2615
2616 static int my_is_dialing(void *pvt, enum analog_sub sub)
2617 {
2618         struct dahdi_pvt *p = pvt;
2619         int index;
2620         int x;
2621
2622         index = analogsub_to_dahdisub(sub);
2623
2624         if (ioctl(p->subs[index].dfd, DAHDI_DIALING, &x)) {
2625                 ast_log(LOG_DEBUG, "DAHDI_DIALING ioctl failed!\n");
2626                 return -1;
2627         }
2628
2629         return x;
2630 }
2631
2632 static int my_on_hook(void *pvt)
2633 {
2634         struct dahdi_pvt *p = pvt;
2635         return dahdi_set_hook(p->subs[ANALOG_SUB_REAL].dfd, DAHDI_ONHOOK);
2636 }
2637
2638 #if defined(HAVE_PRI)
2639 static void my_pri_fixup_chans(void *chan_old, void *chan_new)
2640 {
2641         struct dahdi_pvt *old_chan = chan_old;
2642         struct dahdi_pvt *new_chan = chan_new;
2643
2644         new_chan->owner = old_chan->owner;
2645         old_chan->owner = NULL;
2646         if (new_chan->owner) {
2647                 new_chan->owner->tech_pvt = new_chan;
2648                 new_chan->owner->fds[0] = new_chan->subs[SUB_REAL].dfd;
2649                 new_chan->subs[SUB_REAL].owner = old_chan->subs[SUB_REAL].owner;
2650                 old_chan->subs[SUB_REAL].owner = NULL;
2651         }
2652         /* Copy any DSP that may be present */
2653         new_chan->dsp = old_chan->dsp;
2654         new_chan->dsp_features = old_chan->dsp_features;
2655         old_chan->dsp = NULL;
2656         old_chan->dsp_features = 0;
2657 }
2658
2659 static int sig_pri_tone_to_dahditone(enum sig_pri_tone tone)
2660 {
2661         switch (tone) {
2662         case SIG_PRI_TONE_RINGTONE:
2663                 return DAHDI_TONE_RINGTONE;
2664         case SIG_PRI_TONE_STUTTER:
2665                 return DAHDI_TONE_STUTTER;
2666         case SIG_PRI_TONE_CONGESTION:
2667                 return DAHDI_TONE_CONGESTION;
2668         case SIG_PRI_TONE_DIALTONE:
2669                 return DAHDI_TONE_DIALTONE;
2670         case SIG_PRI_TONE_DIALRECALL:
2671                 return DAHDI_TONE_DIALRECALL;
2672         case SIG_PRI_TONE_INFO:
2673                 return DAHDI_TONE_INFO;
2674         case SIG_PRI_TONE_BUSY:
2675                 return DAHDI_TONE_BUSY;
2676         default:
2677                 return -1;
2678         }
2679 }
2680
2681 static void my_handle_dchan_exception(struct sig_pri_pri *pri, int index)
2682 {
2683         int x, res;
2684
2685         res = ioctl(pri->fds[index], DAHDI_GETEVENT, &x);
2686         if (x) {
2687                 ast_log(LOG_NOTICE, "PRI got event: %s (%d) on D-channel of span %d\n", event2str(x), x, pri->span);
2688         }
2689         /* Keep track of alarm state */
2690         if (x == DAHDI_EVENT_ALARM) {
2691                 pri_event_alarm(pri, index, 0);
2692         } else if (x == DAHDI_EVENT_NOALARM) {
2693                 pri_event_noalarm(pri, index, 0);
2694         }
2695 }
2696
2697 static int my_pri_play_tone(void *pvt, enum sig_pri_tone tone)
2698 {
2699         struct dahdi_pvt *p = pvt;
2700
2701         return tone_zone_play_tone(p->subs[SUB_REAL].dfd, sig_pri_tone_to_dahditone(tone));
2702 }
2703
2704 /*!
2705  * \internal
2706  * \brief Set the caller id information.
2707  * \since 1.6.3
2708  *
2709  * \param pvt DAHDI private structure
2710  * \param caller Caller-id information to set.
2711  *
2712  * \return Nothing
2713  */
2714 static void my_pri_set_callerid(void *pvt, const struct ast_party_caller *caller)
2715 {
2716         struct dahdi_pvt *p = pvt;
2717
2718         ast_copy_string(p->cid_num, S_OR(caller->id.number, ""), sizeof(p->cid_num));
2719         ast_copy_string(p->cid_name, S_OR(caller->id.name, ""), sizeof(p->cid_name));
2720         p->cid_ton = caller->id.number_type;
2721         p->callingpres = caller->id.number_presentation;
2722         ast_copy_string(p->cid_ani, S_OR(caller->ani, ""), sizeof(p->cid_ani));
2723         p->cid_ani2 = caller->ani2;
2724 }
2725
2726 /*!
2727  * \internal
2728  * \brief Set the Dialed Number Identifier.
2729  * \since 1.6.3
2730  *
2731  * \param pvt DAHDI private structure
2732  * \param dnid Dialed Number Identifier string.
2733  *
2734  * \return Nothing
2735  */
2736 static void my_pri_set_dnid(void *pvt, const char *dnid)
2737 {
2738         struct dahdi_pvt *p = pvt;
2739
2740         ast_copy_string(p->dnid, dnid, sizeof(p->dnid));
2741 }
2742
2743 /*!
2744  * \internal
2745  * \brief Set the Redirecting Directory Number Information Service (RDNIS).
2746  * \since 1.6.3
2747  *
2748  * \param pvt DAHDI private structure
2749  * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
2750  *
2751  * \return Nothing
2752  */
2753 static void my_pri_set_rdnis(void *pvt, const char *rdnis)
2754 {
2755         struct dahdi_pvt *p = pvt;
2756
2757         ast_copy_string(p->rdnis, rdnis, sizeof(p->rdnis));
2758 }
2759
2760 static struct sig_pri_callback dahdi_pri_callbacks =
2761 {
2762         .handle_dchan_exception = my_handle_dchan_exception,
2763         .play_tone = my_pri_play_tone,
2764         .set_echocanceller = my_set_echocanceller,
2765         .dsp_reset_and_flush_digits = my_dsp_reset_and_flush_digits,
2766         .lock_private = my_lock_private,
2767         .unlock_private = my_unlock_private,
2768         .new_ast_channel = my_new_pri_ast_channel,
2769         .fixup_chans = my_pri_fixup_chans,
2770         .set_dialing = my_set_dialing,
2771         .set_callerid = my_pri_set_callerid,
2772         .set_dnid = my_pri_set_dnid,
2773         .set_rdnis = my_pri_set_rdnis,
2774 };
2775 #endif  /* defined(HAVE_PRI) */
2776
2777 /*!
2778  * \brief Send MWI state change
2779  *
2780  * \arg mailbox_full This is the mailbox associated with the FXO line that the
2781  *      MWI state has changed on.
2782  * \arg thereornot This argument should simply be set to 1 or 0, to indicate
2783  *      whether there are messages waiting or not.
2784  *
2785  *  \return nothing
2786  *
2787  * This function does two things:
2788  *
2789  * 1) It generates an internal Asterisk event notifying any other module that
2790  *    cares about MWI that the state of a mailbox has changed.
2791  *
2792  * 2) It runs the script specified by the mwimonitornotify option to allow
2793  *    some custom handling of the state change.
2794  */
2795 static void notify_message(char *mailbox_full, int thereornot)
2796 {
2797         char s[sizeof(mwimonitornotify) + 80];
2798         struct ast_event *event;
2799         char *mailbox, *context;
2800
2801         /* Strip off @default */
2802         context = mailbox = ast_strdupa(mailbox_full);
2803         strsep(&context, "@");
2804         if (ast_strlen_zero(context))
2805                 context = "default";
2806
2807         if (!(event = ast_event_new(AST_EVENT_MWI,
2808                         AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
2809                         AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2810                         AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
2811                         AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
2812                         AST_EVENT_IE_END))) {
2813                 return;
2814         }
2815
2816         ast_event_queue_and_cache(event);
2817
2818         if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
2819                 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
2820                 ast_safe_system(s);
2821         }
2822 }
2823
2824 static void my_handle_notify_message(struct ast_channel *chan, void *pvt, int cid_flags, int neon_mwievent)
2825 {
2826         struct dahdi_pvt *p = pvt;
2827
2828         if (neon_mwievent > -1 && !p->mwimonitor_neon)
2829                 return;
2830
2831         if (neon_mwievent == ANALOG_EVENT_NEONMWI_ACTIVE || cid_flags & CID_MSGWAITING) {
2832                 ast_log(LOG_NOTICE, "MWI: Channel %d message waiting, mailbox %s\n", p->channel, p->mailbox);
2833                 notify_message(p->mailbox, 1);
2834         } else if (neon_mwievent == ANALOG_EVENT_NEONMWI_INACTIVE || cid_flags & CID_NOMSGWAITING) {
2835                 ast_log(LOG_NOTICE, "MWI: Channel %d no message waiting, mailbox %s\n", p->channel, p->mailbox);
2836                 notify_message(p->mailbox, 0);
2837         }
2838         /* If the CID had Message waiting payload, assume that this for MWI only and hangup the call */
2839         /* If generated using Ring Pulse Alert, then ring has been answered as a call and needs to be hungup */
2840         if (neon_mwievent == -1 && p->mwimonitor_rpas) {
2841                 ast_hangup(chan);
2842                 return;
2843         }
2844 }
2845
2846
2847
2848 static struct analog_callback dahdi_analog_callbacks =
2849 {
2850         .play_tone = my_play_tone,
2851         .get_event = my_get_event,
2852         .wait_event = my_wait_event,
2853         .is_off_hook = my_is_off_hook,
2854         .set_echocanceller = my_set_echocanceller,
2855         .ring = my_ring,
2856         .flash = my_flash,
2857         .off_hook = my_off_hook,
2858         .dial_digits = my_dial_digits,
2859         .train_echocanceller = my_train_echocanceller,
2860         .on_hook = my_on_hook,
2861         .is_dialing = my_is_dialing,
2862         .allocate_sub = my_allocate_sub,
2863         .unallocate_sub = my_unallocate_sub,
2864         .swap_subs = my_swap_subchannels,
2865         .has_voicemail = my_has_voicemail,
2866         .check_for_conference = my_check_for_conference,
2867         .conf_add = my_conf_add,
2868         .conf_del = my_conf_del,
2869         .complete_conference_update = my_complete_conference_update,
2870         .start = my_start,
2871         .all_subchannels_hungup = my_all_subchannels_hungup,
2872         .lock_private = my_lock_private,
2873         .unlock_private = my_unlock_private,
2874         .handle_dtmfup = my_handle_dtmfup,
2875         .wink = my_wink,
2876         .new_ast_channel = my_new_analog_ast_channel,
2877         .dsp_set_digitmode = my_dsp_set_digitmode,
2878         .dsp_reset_and_flush_digits = my_dsp_reset_and_flush_digits,
2879         .send_callerid = my_send_callerid,
2880         .callwait = my_callwait,
2881         .stop_callwait = my_stop_callwait,
2882         .get_callerid = my_get_callerid,
2883         .start_cid_detect = my_start_cid_detect,
2884         .stop_cid_detect = my_stop_cid_detect,
2885         .handle_notify_message = my_handle_notify_message,
2886         .increase_ss_count = my_increase_ss_count,
2887         .decrease_ss_count = my_decrease_ss_count,
2888         .distinctive_ring = my_distinctive_ring,
2889         .set_linear_mode = my_set_linear_mode,
2890         .get_and_handle_alarms = my_get_and_handle_alarms,
2891         .get_sigpvt_bridged_channel = my_get_sigpvt_bridged_channel,
2892         .get_sub_fd = my_get_sub_fd,
2893         .set_cadence = my_set_cadence,
2894         .set_dialing = my_set_dialing,
2895         .set_ringtimeout = my_set_ringtimeout,
2896         .set_waitingfordt = my_set_waitingfordt,
2897         .check_waitingfordt = my_check_waitingfordt,
2898         .set_confirmanswer = my_set_confirmanswer,
2899         .check_confirmanswer = my_check_confirmanswer,
2900         .cancel_cidspill = my_cancel_cidspill,
2901         .confmute = my_confmute,
2902         .set_pulsedial = my_set_pulsedial,
2903 };
2904
2905 static struct dahdi_pvt *round_robin[32];
2906
2907 #if defined(HAVE_SS7)
2908 static inline void ss7_rel(struct dahdi_ss7 *ss7)
2909 {
2910         ast_mutex_unlock(&ss7->lock);
2911 }
2912 #endif  /* defined(HAVE_SS7) */
2913
2914 #if defined(HAVE_SS7)
2915 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
2916 {
2917         int res;
2918         /* Grab the lock first */
2919         do {
2920                 res = ast_mutex_trylock(&pri->lock);
2921                 if (res) {
2922                         DEADLOCK_AVOIDANCE(&pvt->lock);
2923                 }
2924         } while (res);
2925         /* Then break the poll */
2926         if (pri->master != AST_PTHREADT_NULL)
2927                 pthread_kill(pri->master, SIGURG);
2928         return 0;
2929 }
2930 #endif  /* defined(HAVE_SS7) */
2931
2932 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
2933 {
2934         int res;
2935         if (p->subs[SUB_REAL].owner == ast)
2936                 res = 0;
2937         else if (p->subs[SUB_CALLWAIT].owner == ast)
2938                 res = 1;
2939         else if (p->subs[SUB_THREEWAY].owner == ast)
2940                 res = 2;
2941         else {
2942                 res = -1;
2943                 if (!nullok)
2944                         ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
2945         }
2946         return res;
2947 }
2948
2949 static void wakeup_sub(struct dahdi_pvt *p, int a)
2950 {
2951         for (;;) {
2952                 if (p->subs[a].owner) {
2953                         if (ast_channel_trylock(p->subs[a].owner)) {
2954                                 DEADLOCK_AVOIDANCE(&p->lock);
2955                         } else {
2956                                 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
2957                                 ast_channel_unlock(p->subs[a].owner);
2958                                 break;
2959                         }
2960                 } else
2961                         break;
2962         }
2963 }
2964
2965 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
2966 {
2967 #ifdef HAVE_SS7
2968         struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
2969
2970         if (data) {
2971                 switch (p->sig) {
2972                 case SIG_SS7:
2973                         ast_mutex_unlock(&ss7->lock);
2974                         break;
2975                 default:
2976                         break;
2977                 }
2978         }
2979 #endif
2980         for (;;) {
2981                 if (p->owner) {
2982                         if (ast_channel_trylock(p->owner)) {
2983                                 DEADLOCK_AVOIDANCE(&p->lock);
2984                         } else {
2985                                 ast_queue_frame(p->owner, f);
2986                                 ast_channel_unlock(p->owner);
2987                                 break;
2988                         }
2989                 } else
2990                         break;
2991         }
2992 #if defined(HAVE_SS7)
2993         if (data) {
2994                 switch (p->sig) {
2995                 case SIG_SS7:
2996                         ast_mutex_lock(&ss7->lock);
2997                         break;
2998                 default:
2999                         break;
3000                 }
3001         }
3002 #endif
3003 }
3004
3005 #ifdef HAVE_OPENR2
3006
3007 static int dahdi_r2_answer(struct dahdi_pvt *p)
3008 {
3009         int res = 0;
3010         /* openr2 1.1.0 and older does not even define OR2_LIB_INTERFACE
3011         * and does not has support for openr2_chan_answer_call_with_mode
3012         *  */
3013 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
3014         const char *double_answer = pbx_builtin_getvar_helper(p->owner, "MFCR2_DOUBLE_ANSWER");
3015         int wants_double_answer = ast_true(double_answer) ? 1 : 0;
3016         if (!double_answer) {
3017                 /* this still can result in double answer if the channel context
3018                 * was configured that way */
3019                 res = openr2_chan_answer_call(p->r2chan);
3020         } else if (wants_double_answer) {
3021                 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_DOUBLE);
3022         } else {
3023                 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_SIMPLE);
3024         }
3025 #else
3026         res = openr2_chan_answer_call(p->r2chan);
3027 #endif
3028         return res;
3029 }
3030
3031
3032
3033 /* should be called with the ast_channel locked */
3034 static openr2_calling_party_category_t dahdi_r2_get_channel_category(struct ast_channel *c)
3035 {
3036         openr2_calling_party_category_t cat;
3037         const char *catstr = pbx_builtin_getvar_helper(c, "MFCR2_CATEGORY");
3038         struct dahdi_pvt *p = c->tech_pvt;
3039         if (ast_strlen_zero(catstr)) {
3040                 ast_debug(1, "No MFC/R2 category specified for chan %s, using default %s\n",
3041                                 c->name, openr2_proto_get_category_string(p->mfcr2_category));
3042                 return p->mfcr2_category;
3043         }
3044         if ((cat = openr2_proto_get_category(catstr)) == OR2_CALLING_PARTY_CATEGORY_UNKNOWN) {
3045                 ast_log(LOG_WARNING, "Invalid category specified '%s' for chan %s, using default %s\n",
3046                                 catstr, c->name, openr2_proto_get_category_string(p->mfcr2_category));
3047                 return p->mfcr2_category;
3048         }
3049         ast_debug(1, "Using category %s\n", catstr);
3050         return cat;
3051 }
3052
3053 static void dahdi_r2_on_call_init(openr2_chan_t *r2chan)
3054 {
3055         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3056         ast_mutex_lock(&p->lock);
3057         if (p->mfcr2call) {
3058                 ast_mutex_unlock(&p->lock);
3059                 /* TODO: This can happen when some other thread just finished dahdi_request requesting this very same
3060                    interface but has not yet seized the line (dahdi_call), and the far end wins and seize the line,
3061                    can we avoid this somehow?, at this point when dahdi_call send the seize, it is likely that since
3062                    the other end will see our seize as a forced release and drop the call, we will see an invalid
3063                    pattern that will be seen and treated as protocol error. */
3064                 ast_log(LOG_ERROR, "Collision of calls on chan %d detected!.\n", openr2_chan_get_number(r2chan));
3065                 return;
3066         }
3067         p->mfcr2call = 1;
3068         /* better safe than sorry ... */
3069         p->cid_name[0] = '\0';
3070         p->cid_num[0] = '\0';
3071         p->rdnis[0] = '\0';
3072         p->exten[0] = '\0';
3073         p->mfcr2_ani_index = '\0';
3074         p->mfcr2_dnis_index = '\0';
3075         p->mfcr2_dnis_matched = 0;
3076         p->mfcr2_answer_pending = 0;
3077         p->mfcr2_call_accepted = 0;
3078         ast_mutex_unlock(&p->lock);
3079         ast_verbose("New MFC/R2 call detected on chan %d.\n", openr2_chan_get_number(r2chan));
3080 }
3081
3082 static void dahdi_r2_on_hardware_alarm(openr2_chan_t *r2chan, int alarm)
3083 {
3084         int res;
3085         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3086         ast_mutex_lock(&p->lock);
3087         p->inalarm = alarm ? 1 : 0;
3088         if (p->inalarm) {
3089                 res = get_alarms(p);
3090                 handle_alarms(p, res);
3091         } else {
3092                 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
3093                 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", "Channel: %d\r\n", p->channel);
3094         }
3095         ast_mutex_unlock(&p->lock);
3096 }
3097
3098 static void dahdi_r2_on_os_error(openr2_chan_t *r2chan, int errorcode)
3099 {
3100         ast_log(LOG_ERROR, "OS error on chan %d: %s\n", openr2_chan_get_number(r2chan), strerror(errorcode));
3101 }
3102
3103 static void dahdi_r2_on_protocol_error(openr2_chan_t *r2chan, openr2_protocol_error_t reason)
3104 {
3105         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3106         ast_log(LOG_ERROR, "MFC/R2 protocol error on chan %d: %s\n", openr2_chan_get_number(r2chan), openr2_proto_get_error(reason));
3107         if (p->owner) {
3108                 p->owner->hangupcause = AST_CAUSE_PROTOCOL_ERROR;
3109                 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3110         }
3111         ast_mutex_lock(&p->lock);
3112         p->mfcr2call = 0;
3113         ast_mutex_unlock(&p->lock);
3114 }
3115
3116 static void dahdi_r2_disconnect_call(struct dahdi_pvt *p, openr2_call_disconnect_cause_t cause)
3117 {
3118         if (openr2_chan_disconnect_call(p->r2chan, cause)) {
3119                 ast_log(LOG_NOTICE, "Bad! failed to disconnect call on channel %d with reason %s, hope for the best!\n",
3120                    p->channel, openr2_proto_get_disconnect_string(cause));
3121                 /* force the chan to idle and release the call flag now since we will not see a clean on_call_end */
3122                 openr2_chan_set_idle(p->r2chan);
3123                 ast_mutex_lock(&p->lock);
3124                 p->mfcr2call = 0;
3125                 ast_mutex_unlock(&p->lock);
3126         }
3127 }
3128
3129 static void dahdi_r2_on_call_offered(openr2_chan_t *r2chan, const char *ani, const char *dnis, openr2_calling_party_category_t category)
3130 {
3131         struct dahdi_pvt *p;
3132         struct ast_channel *c;
3133         ast_verbose("MFC/R2 call offered on chan %d. ANI = %s, DNIS = %s, Category = %s\n",
3134                         openr2_chan_get_number(r2chan), ani ? ani : "(restricted)", dnis,
3135                         openr2_proto_get_category_string(category));
3136         p = openr2_chan_get_client_data(r2chan);
3137         /* if collect calls are not allowed and this is a collect call, reject it! */
3138         if (!p->mfcr2_allow_collect_calls && category == OR2_CALLING_PARTY_CATEGORY_COLLECT_CALL) {
3139                 ast_log(LOG_NOTICE, "Rejecting MFC/R2 collect call\n");
3140                 dahdi_r2_disconnect_call(p, OR2_CAUSE_COLLECT_CALL_REJECTED);
3141                 return;
3142         }
3143         ast_mutex_lock(&p->lock);
3144         p->mfcr2_recvd_category = category;
3145         /* if we're not supposed to use CID, clear whatever we have */
3146         if (!p->use_callerid) {
3147                 ast_log(LOG_DEBUG, "No CID allowed in configuration, CID is being cleared!\n");
3148                 p->cid_num[0] = 0;
3149                 p->cid_name[0] = 0;
3150         }
3151         /* if we're supposed to answer immediately, clear DNIS and set 's' exten */
3152         if (p->immediate || !openr2_context_get_max_dnis(openr2_chan_get_context(r2chan))) {
3153                 ast_log(LOG_DEBUG, "Setting exten => s because of immediate or 0 DNIS configured\n");
3154                 p->exten[0] = 's';
3155                 p->exten[1] = 0;
3156         }
3157         ast_mutex_unlock(&p->lock);
3158         if (!ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
3159                 ast_log(LOG_NOTICE, "MFC/R2 call on channel %d requested non-existent extension '%s' in context '%s'. Rejecting call.\n",
3160                                 p->channel, p->exten, p->context);
3161                 dahdi_r2_disconnect_call(p, OR2_CAUSE_UNALLOCATED_NUMBER);
3162                 return;
3163         }
3164         if (!p->mfcr2_accept_on_offer) {
3165                 /* The user wants us to start the PBX thread right away without accepting the call first */
3166                 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0, NULL);
3167                 if (c) {
3168                         /* Done here, don't disable reading now since we still need to generate MF tones to accept
3169                            the call or reject it and detect the tone off condition of the other end, all of this
3170                            will be done in the PBX thread now */
3171                         return;
3172                 }
3173                 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
3174                 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
3175         } else if (p->mfcr2_charge_calls) {
3176                 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge on chan %d\n", p->channel);
3177                 openr2_chan_accept_call(r2chan, OR2_CALL_WITH_CHARGE);
3178         } else {
3179                 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge on chan %d\n", p->channel);
3180                 openr2_chan_accept_call(r2chan, OR2_CALL_NO_CHARGE);
3181         }
3182 }
3183
3184 static void dahdi_r2_on_call_end(openr2_chan_t *r2chan)
3185 {
3186         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3187         ast_verbose("MFC/R2 call end on channel %d\n", p->channel);
3188         ast_mutex_lock(&p->lock);
3189         p->mfcr2call = 0;
3190         ast_mutex_unlock(&p->lock);
3191 }
3192
3193 static void dahdi_enable_ec(struct dahdi_pvt *p);
3194 static void dahdi_r2_on_call_accepted(openr2_chan_t *r2chan, openr2_call_mode_t mode)
3195 {
3196         struct dahdi_pvt *p = NULL;
3197         struct ast_channel *c = NULL;
3198         p = openr2_chan_get_client_data(r2chan);
3199         dahdi_enable_ec(p);
3200         p->mfcr2_call_accepted = 1;
3201         /* if it's an incoming call ... */
3202         if (OR2_DIR_BACKWARD == openr2_chan_get_direction(r2chan)) {
3203                 ast_verbose("MFC/R2 call has been accepted on backward channel %d\n", openr2_chan_get_number(r2chan));
3204                 /* If accept on offer is not set, it means at this point the PBX thread is already
3205                    launched (was launched in the 'on call offered' handler) and therefore this callback
3206                    is being executed already in the PBX thread rather than the monitor thread, don't launch
3207                    any other thread, just disable the openr2 reading and answer the call if needed */
3208                 if (!p->mfcr2_accept_on_offer) {
3209                         openr2_chan_disable_read(r2chan);
3210                         if (p->mfcr2_answer_pending) {
3211                                 ast_log(LOG_DEBUG, "Answering MFC/R2 call after accepting it on chan %d\n", openr2_chan_get_number(r2chan));
3212                                 dahdi_r2_answer(p);
3213                         }
3214                         return;
3215                 }
3216                 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0, NULL);
3217                 if (c) {
3218                         /* chan_dahdi will take care of reading from now on in the PBX thread, tell the
3219                            library to forget about it */
3220                         openr2_chan_disable_read(r2chan);
3221                         return;
3222                 }
3223                 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
3224                 /* failed to create the channel, bail out and report it as an out of order line */
3225                 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
3226                 return;
3227         }
3228         /* this is an outgoing call, no need to launch the PBX thread, most likely we're in one already */
3229         ast_verbose("MFC/R2 call has been accepted on forward channel %d\n", p->channel);
3230         p->subs[SUB_REAL].needringing = 1;
3231         p->dialing = 0;
3232         /* chan_dahdi will take care of reading from now on in the PBX thread, tell the library to forget about it */
3233         openr2_chan_disable_read(r2chan);
3234 }
3235
3236 static void dahdi_r2_on_call_answered(openr2_chan_t *r2chan)
3237 {
3238         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3239         ast_verbose("MFC/R2 call has been answered on channel %d\n", openr2_chan_get_number(r2chan));
3240         p->subs[SUB_REAL].needanswer = 1;
3241 }
3242
3243 static void dahdi_r2_on_call_read(openr2_chan_t *r2chan, const unsigned char *buf, int buflen)
3244 {
3245         /*ast_log(LOG_DEBUG, "Read data from dahdi channel %d\n", openr2_chan_get_number(r2chan));*/
3246 }
3247
3248 static int dahdi_r2_cause_to_ast_cause(openr2_call_disconnect_cause_t cause)
3249 {
3250         switch (cause) {
3251         case OR2_CAUSE_BUSY_NUMBER:
3252                 return AST_CAUSE_BUSY;
3253         case OR2_CAUSE_NETWORK_CONGESTION:
3254                 return AST_CAUSE_CONGESTION;
3255         case OR2_CAUSE_OUT_OF_ORDER:
3256                 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
3257         case OR2_CAUSE_UNALLOCATED_NUMBER:
3258                 return AST_CAUSE_UNREGISTERED;
3259         case OR2_CAUSE_NO_ANSWER:
3260                 return AST_CAUSE_NO_ANSWER;
3261         case OR2_CAUSE_NORMAL_CLEARING:
3262                 return AST_CAUSE_NORMAL_CLEARING;
3263         case OR2_CAUSE_UNSPECIFIED:
3264         default:
3265                 return AST_CAUSE_NOTDEFINED;
3266         }
3267 }
3268
3269 static void dahdi_r2_on_call_disconnect(openr2_chan_t *r2chan, openr2_call_disconnect_cause_t cause)
3270 {
3271         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3272         ast_verbose("MFC/R2 call disconnected on channel %d\n", openr2_chan_get_number(r2chan));
3273         ast_mutex_lock(&p->lock);
3274         if (!p->owner) {
3275                 ast_mutex_unlock(&p->lock);
3276                 /* no owner, therefore we can't use dahdi_hangup to disconnect, do it right now */
3277                 dahdi_r2_disconnect_call(p, OR2_CAUSE_NORMAL_CLEARING);
3278                 return;
3279         }
3280         /* when we have an owner we don't call dahdi_r2_disconnect_call here, that will
3281            be done in dahdi_hangup */
3282         if (p->owner->_state == AST_STATE_UP) {
3283                 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3284                 ast_mutex_unlock(&p->lock);
3285         } else if (openr2_chan_get_direction(r2chan) == OR2_DIR_FORWARD) {
3286                 /* being the forward side we must report what happened to the call to whoever requested it */
3287                 switch (cause) {
3288                 case OR2_CAUSE_BUSY_NUMBER:
3289                         p->subs[SUB_REAL].needbusy = 1;
3290                         break;
3291                 case OR2_CAUSE_NETWORK_CONGESTION:
3292                 case OR2_CAUSE_OUT_OF_ORDER:
3293                 case OR2_CAUSE_UNALLOCATED_NUMBER:
3294                 case OR2_CAUSE_NO_ANSWER:
3295                 case OR2_CAUSE_UNSPECIFIED:
3296                 case OR2_CAUSE_NORMAL_CLEARING:
3297                         p->subs[SUB_REAL].needcongestion = 1;
3298                         break;
3299                 default:
3300                         p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3301                 }
3302                 ast_mutex_unlock(&p->lock);
3303         } else {
3304                 ast_mutex_unlock(&p->lock);
3305                 /* being the backward side and not UP yet, we only need to request hangup */
3306                 /* TODO: what about doing this same thing when were AST_STATE_UP? */
3307                 ast_queue_hangup_with_cause(p->owner, dahdi_r2_cause_to_ast_cause(cause));
3308         }
3309 }
3310
3311 static void dahdi_r2_write_log(openr2_log_level_t level, char *logmessage)
3312 {
3313         switch (level) {
3314         case OR2_LOG_NOTICE:
3315                 ast_verbose("%s", logmessage);
3316                 break;
3317         case OR2_LOG_WARNING:
3318                 ast_log(LOG_WARNING, "%s", logmessage);
3319                 break;
3320         case OR2_LOG_ERROR:
3321                 ast_log(LOG_ERROR, "%s", logmessage);
3322                 break;
3323         case OR2_LOG_STACK_TRACE:
3324         case OR2_LOG_MF_TRACE:
3325         case OR2_LOG_CAS_TRACE:
3326         case OR2_LOG_DEBUG:
3327         case OR2_LOG_EX_DEBUG:
3328                 ast_log(LOG_DEBUG, "%s", logmessage);
3329                 break;
3330         default:
3331                 ast_log(LOG_WARNING, "We should handle logging level %d here.\n", level);
3332                 ast_log(LOG_DEBUG, "%s", logmessage);
3333                 break;
3334         }
3335 }
3336
3337 static void dahdi_r2_on_line_blocked(openr2_chan_t *r2chan)
3338 {
3339         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3340         ast_mutex_lock(&p->lock);
3341         p->remotelyblocked = 1;
3342         ast_mutex_unlock(&p->lock);
3343         ast_log(LOG_NOTICE, "Far end blocked on chan %d\n", openr2_chan_get_number(r2chan));
3344 }
3345
3346 static void dahdi_r2_on_line_idle(openr2_chan_t *r2chan)
3347 {
3348         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3349         ast_mutex_lock(&p->lock);
3350         p->remotelyblocked = 0;
3351         ast_mutex_unlock(&p->lock);
3352         ast_log(LOG_NOTICE, "Far end unblocked on chan %d\n", openr2_chan_get_number(r2chan));
3353 }
3354
3355 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
3356         __attribute__((format (printf, 3, 0)));
3357 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
3358 {
3359 #define CONTEXT_TAG "Context - "
3360         char logmsg[256];
3361         char completemsg[sizeof(logmsg) + sizeof(CONTEXT_TAG) - 1];
3362         vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
3363         snprintf(completemsg, sizeof(completemsg), CONTEXT_TAG "%s", logmsg);
3364         dahdi_r2_write_log(level, completemsg);
3365 #undef CONTEXT_TAG
3366 }
3367
3368 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
3369         __attribute__((format (printf, 3, 0)));
3370 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
3371 {
3372 #define CHAN_TAG "Chan "
3373         char logmsg[256];
3374         char completemsg[sizeof(logmsg) + sizeof(CHAN_TAG) - 1];
3375         vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
3376         snprintf(completemsg, sizeof(completemsg), CHAN_TAG "%d - %s", openr2_chan_get_number(r2chan), logmsg);
3377         dahdi_r2_write_log(level, completemsg);
3378 }
3379
3380 static int dahdi_r2_on_dnis_digit_received(openr2_chan_t *r2chan, char digit)
3381 {
3382         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3383         /* if 'immediate' is set, let's stop requesting DNIS */
3384         if (p->immediate) {
3385                 return 0;
3386         }
3387         p->exten[p->mfcr2_dnis_index] = digit;
3388         p->rdnis[p->mfcr2_dnis_index] = digit;
3389         p->mfcr2_dnis_index++;
3390         p->exten[p->mfcr2_dnis_index] = 0;
3391         p->rdnis[p->mfcr2_dnis_index] = 0;
3392         /* if the DNIS is a match and cannot match more, stop requesting DNIS */
3393         if ((p->mfcr2_dnis_matched ||
3394             (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num) && (p->mfcr2_dnis_matched = 1))) &&
3395             !ast_matchmore_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
3396                 return 0;
3397         }
3398         /* otherwise keep going */
3399         return 1;
3400 }
3401
3402 static void dahdi_r2_on_ani_digit_received(openr2_chan_t *r2chan, char digit)
3403 {
3404         struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
3405         p->cid_num[p->mfcr2_ani_index] = digit;
3406         p->cid_name[p->mfcr2_ani_index] = digit;
3407         p->mfcr2_ani_index++;
3408         p->cid_num[p->mfcr2_ani_index] = 0;
3409         p->cid_name[p->mfcr2_ani_index] = 0;
3410 }
3411
3412 static void dahdi_r2_on_billing_pulse_received(openr2_chan_t *r2chan)
3413 {
3414         ast_verbose("MFC/R2 billing pulse received on channel %d\n", openr2_chan_get_number(r2chan));
3415 }
3416
3417 static openr2_event_interface_t dahdi_r2_event_iface = {
3418         .on_call_init = dahdi_r2_on_call_init,
3419         .on_call_offered = dahdi_r2_on_call_offered,
3420         .on_call_accepted = dahdi_r2_on_call_accepted,
3421         .on_call_answered = dahdi_r2_on_call_answered,
3422         .on_call_disconnect = dahdi_r2_on_call_disconnect,
3423         .on_call_end = dahdi_r2_on_call_end,
3424         .on_call_read = dahdi_r2_on_call_read,
3425         .on_hardware_alarm = dahdi_r2_on_hardware_alarm,
3426         .on_os_error = dahdi_r2_on_os_error,
3427         .on_protocol_error = dahdi_r2_on_protocol_error,
3428         .on_line_blocked = dahdi_r2_on_line_blocked,
3429         .on_line_idle = dahdi_r2_on_line_idle,
3430         /* cast seems to be needed to get rid of the annoying warning regarding format attribute  */
3431         .on_context_log = (openr2_handle_context_logging_func)dahdi_r2_on_context_log,
3432         .on_dnis_digit_received = dahdi_r2_on_dnis_digit_received,
3433         .on_ani_digit_received = dahdi_r2_on_ani_digit_received,
3434         /* so far we do nothing with billing pulses */
3435         .on_billing_pulse_received = dahdi_r2_on_billing_pulse_received
3436 };
3437
3438 static inline int16_t dahdi_r2_alaw_to_linear(uint8_t sample)
3439 {
3440         return AST_ALAW(sample);
3441 }
3442
3443 static inline uint8_t dahdi_r2_linear_to_alaw(int sample)
3444 {
3445         return AST_LIN2A(sample);
3446 }
3447
3448 static openr2_transcoder_interface_t dahdi_r2_transcode_iface = {
3449         dahdi_r2_alaw_to_linear,
3450         dahdi_r2_linear_to_alaw
3451 };
3452
3453 #endif /* HAVE_OPENR2 */
3454
3455 static void swap_subs(struct dahdi_pvt *p, int a, int b)
3456 {
3457         int tchan;
3458         int tinthreeway;
3459         struct ast_channel *towner;
3460
3461         ast_debug(1, "Swapping %d and %d\n", a, b);
3462
3463         tchan = p->subs[a].chan;
3464         towner = p->subs[a].owner;
3465         tinthreeway = p->subs[a].inthreeway;
3466
3467         p->subs[a].chan = p->subs[b].chan;
3468         p->subs[a].owner = p->subs[b].owner;
3469         p->subs[a].inthreeway = p->subs[b].inthreeway;
3470
3471         p->subs[b].chan = tchan;
3472         p->subs[b].owner = towner;
3473         p->subs[b].inthreeway = tinthreeway;
3474
3475         if (p->subs[a].owner)
3476                 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].dfd);
3477         if (p->subs[b].owner)
3478                 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].dfd);
3479         wakeup_sub(p, a);
3480         wakeup_sub(p, b);
3481 }
3482
3483 static int dahdi_open(char *fn)
3484 {
3485         int fd;
3486         int isnum;
3487         int chan = 0;
3488         int bs;
3489         int x;
3490         isnum = 1;
3491         for (x = 0; x < strlen(fn); x++) {
3492                 if (!isdigit(fn[x])) {
3493                         isnum = 0;
3494                         break;
3495                 }
3496         }
3497         if (isnum) {
3498                 chan = atoi(fn);
3499                 if (chan < 1) {
3500                         ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
3501                         return -1;
3502                 }
3503                 fn = "/dev/dahdi/channel";
3504         }
3505         fd = open(fn, O_RDWR | O_NONBLOCK);
3506         if (fd < 0) {
3507                 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
3508                 return -1;
3509         }
3510         if (chan) {
3511                 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
3512                         x = errno;
3513                         close(fd);
3514                         errno = x;
3515                         ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
3516                         return -1;
3517                 }
3518         }
3519         bs = READ_SIZE;
3520         if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
3521                 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs,  strerror(errno));
3522                 x = errno;
3523                 close(fd);
3524                 errno = x;
3525                 return -1;
3526         }
3527         return fd;
3528 }
3529
3530 static void dahdi_close(int fd)
3531 {
3532         if (fd > 0)
3533                 close(fd);
3534 }
3535
3536 static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
3537 {
3538         dahdi_close(chan_pvt->subs[sub_num].dfd);
3539         chan_pvt->subs[sub_num].dfd = -1;
3540 }
3541
3542 #if defined(HAVE_PRI)
3543 static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
3544 {
3545         dahdi_close(pri->pri.fds[fd_num]);
3546         pri->pri.fds[fd_num] = -1;
3547 }
3548 #endif  /* defined(HAVE_PRI) */
3549
3550 #if defined(HAVE_SS7)
3551 static void dahdi_close_ss7_fd(struct dahdi_ss7 *ss7, int fd_num)
3552 {
3553         dahdi_close(ss7->fds[fd_num]);
3554         ss7->fds[fd_num] = -1;
3555 }
3556 #endif  /* defined(HAVE_SS7) */
3557
3558 static int dahdi_setlinear(int dfd, int linear)
3559 {
3560         return ioctl(dfd, DAHDI_SETLINEAR, &linear);
3561 }
3562
3563
3564 static int alloc_sub(struct dahdi_pvt *p, int x)
3565 {
3566         struct dahdi_bufferinfo bi;
3567         int res;
3568         if (p->subs[x].dfd >= 0) {
3569                 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
3570                 return -1;
3571         }
3572
3573         p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
3574         if (p->subs[x].dfd <= -1) {
3575                 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
3576                 return -1;
3577         }
3578
3579         res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
3580         if (!res) {
3581                 bi.txbufpolicy = p->buf_policy;
3582                 bi.rxbufpolicy = p->buf_policy;
3583                 bi.numbufs = p->buf_no;
3584                 res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
3585                 if (res < 0) {
3586                         ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
3587                 }
3588         } else
3589                 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
3590
3591         if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
3592                 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
3593                 dahdi_close_sub(p, x);
3594                 p->subs[x].dfd = -1;
3595                 return -1;
3596         }
3597         ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
3598         return 0;
3599 }
3600
3601 static int unalloc_sub(struct dahdi_pvt *p, int x)
3602 {
3603         if (!x) {
3604                 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
3605                 return -1;
3606         }
3607         ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
3608         dahdi_close_sub(p, x);
3609         p->subs[x].linear = 0;
3610         p->subs[x].chan = 0;
3611         p->subs[x].owner = NULL;
3612         p->subs[x].inthreeway = 0;
3613         p->polarity = POLARITY_IDLE;
3614         memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
3615         return 0;
3616 }
3617
3618 static int digit_to_dtmfindex(char digit)
3619 {
3620         if (isdigit(digit))
3621                 return DAHDI_TONE_DTMF_BASE + (digit - '0');
3622         else if (digit >= 'A' && digit <= 'D')
3623                 return DAHDI_TONE_DTMF_A + (digit - 'A');
3624         else if (digit >= 'a' && digit <= 'd')
3625                 return DAHDI_TONE_DTMF_A + (digit - 'a');
3626         else if (digit == '*')
3627                 return DAHDI_TONE_DTMF_s;
3628         else if (digit == '#')
3629                 return DAHDI_TONE_DTMF_p;
3630         else
3631                 return -1;
3632 }
3633
3634 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
3635 {
3636         struct dahdi_pvt *pvt;
3637         int idx;
3638         int dtmf = -1;
3639         int res;
3640
3641         pvt = chan->tech_pvt;
3642
3643         ast_mutex_lock(&pvt->lock);
3644
3645         idx = dahdi_get_index(chan, pvt, 0);
3646
3647         if ((idx != SUB_REAL) || !pvt->owner)
3648                 goto out;
3649
3650 #ifdef HAVE_PRI
3651         switch (pvt->sig) {
3652         case SIG_PRI_LIB_HANDLE_CASES:
3653                 res = sig_pri_digit_begin(pvt->sig_pvt, chan, digit);
3654                 if (!res)
3655                         goto out;
3656                 break;
3657         default:
3658                 break;
3659         }
3660 #endif
3661         if ((dtmf = digit_to_dtmfindex(digit)) == -1)
3662                 goto out;
3663
3664         if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
3665                 struct dahdi_dialoperation zo = {
3666                         .op = DAHDI_DIAL_OP_APPEND,
3667                 };
3668
3669                 zo.dialstr[0] = 'T';
3670                 zo.dialstr[1] = digit;
3671                 zo.dialstr[2] = '\0';
3672                 if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
3673                         ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
3674                 else
3675                         pvt->dialing = 1;
3676         } else {
3677                 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
3678                 pvt->dialing = 1;
3679                 pvt->begindigit = digit;
3680         }
3681
3682 out:
3683         ast_mutex_unlock(&pvt->lock);
3684
3685         return 0;
3686 }
3687
3688 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
3689 {
3690         struct dahdi_pvt *pvt;
3691         int res = 0;
3692         int idx;
3693         int x;
3694
3695         pvt = chan->tech_pvt;
3696
3697         ast_mutex_lock(&pvt->lock);
3698
3699         idx = dahdi_get_index(chan, pvt, 0);
3700
3701         if ((idx != SUB_REAL) || !pvt->owner || pvt->pulse)
3702                 goto out;
3703
3704 #ifdef HAVE_PRI
3705         /* This means that the digit was already sent via PRI signalling */
3706         if (dahdi_sig_pri_lib_handles(pvt->sig) && !pvt->begindigit) {
3707                 goto out;
3708         }
3709 #endif
3710
3711         if (pvt->begindigit) {
3712                 x = -1;
3713                 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
3714                 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
3715                 pvt->dialing = 0;
3716                 pvt->begindigit = 0;
3717         }
3718
3719 out:
3720         ast_mutex_unlock(&pvt->lock);
3721
3722         return res;
3723 }
3724
3725 static const char * const events[] = {
3726         "No event",
3727         "On hook",
3728         "Ring/Answered",
3729         "Wink/Flash",
3730         "Alarm",
3731         "No more alarm",
3732         "HDLC Abort",
3733         "HDLC Overrun",
3734         "HDLC Bad FCS",
3735         "Dial Complete",
3736         "Ringer On",
3737         "Ringer Off",
3738         "Hook Transition Complete",
3739         "Bits Changed",
3740         "Pulse Start",
3741         "Timer Expired",
3742         "Timer Ping",
3743         "Polarity Reversal",
3744         "Ring Begin",
3745 };
3746
3747 static struct {
3748         int alarm;
3749         char *name;