From 8ecfb6984b37bae34f562a9544bb43035b6617e2 Mon Sep 17 00:00:00 2001 From: Pedro Kiefer Date: Thu, 16 Aug 2012 11:07:46 -0300 Subject: [PATCH] Remove proprietary send_tone_burst, use stock asterisk ast_playtones_ functions Original patch by Jean-Philippe Lord - ASTERISK-16694 --- apps/app_alarmreceiver.c | 119 ++++++++++------------------------------------ 1 file changed, 24 insertions(+), 95 deletions(-) diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c index 79705cf..b44b433 100644 --- a/apps/app_alarmreceiver.c +++ b/apps/app_alarmreceiver.c @@ -55,6 +55,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision: 362635 $") #include "asterisk/callerid.h" #include "asterisk/astdb.h" #include "asterisk/utils.h" +#include "asterisk/indications.h" #define ALMRCV_CONFIG "alarmreceiver.conf" #define ADEMCO_CONTACT_ID "ADEMCO_CONTACT_ID" @@ -144,92 +145,6 @@ static void database_increment(char *key) } /** - * Build a MuLaw data block for a single frequency tone - * - * @param data Data pointer - * @param freq Frequency Tone - * @param loudness Signal Gain - * @param len Data Array Length - * @param x - */ -static void make_tone_burst(unsigned char *data, float freq, float loudness, int len, int *x) -{ - int i; - float val; - - for (i = 0; i < len; i++) { - val = loudness * sin((freq * 2.0 * M_PI * (*x)++)/8000.0); - data[i] = AST_LIN2MU((int)val); - } - - /* wrap back around from 8000 */ - if (*x >= 8000) { - *x = 0; - } -} - -/** - * Send a single tone burst for a specified duration and frequency. - * - * @param chan Asterisk Channel - * @param freq Tone Frequency - * @param duration Duration - * @param tldn Signal Gain - * @return 0 if successful, -1 otherwise - */ -static int send_tone_burst(struct ast_channel *chan, float freq, int duration, int tldn) -{ - int i = 0; - int x = 0; - struct ast_frame *f, wf; - - struct { - unsigned char offset[AST_FRIENDLY_OFFSET]; - unsigned char buf[640]; - } tone_block; - - for (;;) { - - if (ast_waitfor(chan, -1) < 0) { - break; - } - - f = ast_read(chan); - if (!f) { - break; - } - - if (f->frametype == AST_FRAME_VOICE) { - wf.frametype = AST_FRAME_VOICE; - ast_format_set(&wf.subclass.format, AST_FORMAT_ULAW, 0); - wf.offset = AST_FRIENDLY_OFFSET; - wf.mallocd = 0; - wf.data.ptr = tone_block.buf; - wf.datalen = f->datalen; - wf.samples = wf.datalen; - wf.src = __FUNCTION__; - - make_tone_burst(tone_block.buf, freq, (float) tldn, wf.datalen, &x); - - i += wf.datalen / 8; - if (i > duration) { - ast_frfree(f); - return 0; - } - if (ast_write(chan, &wf)) { - ast_verb(4, "AlarmReceiver: Failed to write frame on %s\n", ast_channel_name(chan)); - ast_log(LOG_WARNING, "AlarmReceiver Failed to write frame on %s\n",ast_channel_name(chan)); - ast_frfree(f); - break; - } - } - - ast_frfree(f); - } - return -1; -} - -/** * Receive a fixed length DTMF string. Doesn't give preferential treatment to any digit, * allow different timeout values for the first and all subsequent digits * @@ -478,24 +393,33 @@ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, database_increment("calls-received"); /* Wait for first event */ - ast_verb(4, "AlarmReceiver: Waiting for first event from panel\n"); + ast_verb(4, "AlarmReceiver: Waiting for first event from panel...\n"); while (res >= 0) { if (got_some_digits == 0) { /* Send ACK tone sequence */ ast_verb(4, "AlarmReceiver: Sending 1400Hz 100ms burst (ACK)\n"); - res = send_tone_burst(chan, 1400.0, 100, tldn); - if (!res) - res = ast_safe_sleep(chan, 100); + res = ast_playtones_start(chan, tldn, "1400", 0); if (!res) { + ast_safe_sleep(chan, 100); + ast_playtones_stop(chan); + ast_verb(4, "AlarmReceiver: Sending 2300Hz 100ms burst (ACK)\n"); - res = send_tone_burst(chan, 2300.0, 100, tldn); + res = ast_playtones_start(chan, tldn, "2300", 0); + if (!res) { + ast_safe_sleep(chan, 100); + ast_playtones_stop(chan); + } } } if (exit_on_next) { res = ast_safe_sleep(chan, 200); - send_tone_burst(chan, 1400.0, 900, tldn); + res = ast_playtones_start(chan, tldn, "1400", 0); + if (res == 0) { + ast_safe_sleep(chan, 900); + ast_playtones_stop(chan); + } res = 0; break; } @@ -589,9 +513,14 @@ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, if (res == 0) res = ast_safe_sleep(chan, 200); - /* Send the kissoff tone */ - if (res == 0) - res = send_tone_burst(chan, 1400.0, 900, tldn); + /* Send the kissoff tone (1400 Hz, 900 ms) */ + if (res == 0) { + res = ast_playtones_start(chan, tldn, "1400", 0); + if (res == 0) { + ast_safe_sleep(chan, 900); + ast_playtones_stop(chan); + } + } } return res; -- 1.7.9.6 (Apple Git-31.1)