From 99007a0eddb01bb39b56d859d85c81814acd7140 Mon Sep 17 00:00:00 2001 From: Pedro Kiefer Date: Thu, 16 Aug 2012 11:55:59 -0300 Subject: [PATCH] Mainly code cleanup Add support for Audio Call Next Event Fix documentation Use ast_playtones_ functions instead of proprietary ones --- apps/app_alarmreceiver.c | 548 ++++++++++++++++++++++------------------------ 1 file changed, 260 insertions(+), 288 deletions(-) diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c index 2059b6d..b44b433 100644 --- a/apps/app_alarmreceiver.c +++ b/apps/app_alarmreceiver.c @@ -55,9 +55,13 @@ 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" +#define ADEMCO_MSG_TYPE_1 "18" +#define ADEMCO_MSG_TYPE_2 "98" +#define ADEMCO_AUDIO_CALL_NEXT "606" struct event_node{ char data[17]; @@ -102,29 +106,28 @@ static char time_stamp_format[128] = {"%a %b %d, %Y @ %H:%M:%S %Z"}; /* Misc variables */ static char event_file[14] = "/event-XXXXXX"; -/* -* Attempt to access a database variable and increment it, -* provided that the user defined db-family in alarmreceiver.conf -* The alarmreceiver app will write statistics to a few variables -* in this family if it is defined. If the new key doesn't exist in the -* family, then create it and set its value to 1. -*/ -static void database_increment( char *key ) +/** + * Attempt to access a database variable and increment it + * provided that the user defined db-family in alarmreceiver.conf + * + * The alarmreceiver app will write statistics to a few variables + * in this family if it is defined. If the new key doesn't exist in the + * family, then create it and set its value to 1. + * + * @param key A database key to increment + */ +static void database_increment(char *key) { - int res = 0; unsigned v; char value[16]; - if (ast_strlen_zero(db_family)) return; /* If not defined, don't do anything */ - res = ast_db_get(db_family, key, value, sizeof(value) - 1); - - if (res) { + if (ast_db_get(db_family, key, value, sizeof(value) - 1)) { ast_verb(4, "AlarmReceiver: Creating database entry %s and setting to 1\n", key); /* Guess we have to create it */ - res = ast_db_put(db_family, key, "1"); + ast_db_put(db_family, key, "1"); return; } @@ -132,110 +135,29 @@ static void database_increment( char *key ) v++; ast_verb(4, "AlarmReceiver: New value for %s: %u\n", key, v); - snprintf(value, sizeof(value), "%u", v); - res = ast_db_put(db_family, key, value); - - if (res) + if (ast_db_put(db_family, key, value)) { ast_verb(4, "AlarmReceiver: database_increment write error\n"); - - return; -} - - -/* -* Build a MuLaw data block for a single frequency tone -*/ -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; return; } -/* -* Send a single tone burst for a specifed duration and frequency. -* Returns 0 if successful -*/ -static int send_tone_burst(struct ast_channel *chan, float freq, int duration, int tldn) -{ - int res = 0; - 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) { - res = -1; - break; - } - - f = ast_read(chan); - if (!f) { - res = -1; - 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; - - make_tone_burst(tone_block.buf, freq, (float) tldn, wf.datalen, &x); - - i += wf.datalen / 8; - if (i > duration) { - ast_frfree(f); - break; - } - 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)); - res = -1; - ast_frfree(f); - break; - } - } - - ast_frfree(f); - } - return res; -} - -/* -* Receive a string of DTMF digits where the length of the digit string is known in advance. Do not give preferential -* treatment to any digit value, and allow separate time out values to be specified for the first digit and all subsequent -* digits. -* -* Returns 0 if all digits successfully received. -* Returns 1 if a digit time out occurred -* Returns -1 if the caller hung up or there was a channel error. -* -*/ +/** + * 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 + * + * @param chan Asterisk Channel + * @param digit_string Digits String + * @param length Length of the String + * @param fdto First Digit Timeout + * @param sdto Other Digits Timeout + * @return 0 if all digits were successfully received, 1 if a timeout occurred, -1 if the caller hung up or on channel errors + */ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int length, int fdto, int sdto) { - int res = 0; + int rtn = 0; int i = 0; int r; struct ast_frame *f; @@ -243,11 +165,11 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int lastdigittime = ast_tvnow(); for (;;) { - /* if outa time, leave */ + /* if timed out, leave */ if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) > ((i > 0) ? sdto : fdto)) { ast_verb(4, "AlarmReceiver: DTMF Digit Timeout on %s\n", ast_channel_name(chan)); - ast_debug(1,"AlarmReceiver: DTMF timeout on chan %s\n",ast_channel_name(chan)); - res = 1; + ast_debug(1, "AlarmReceiver: DTMF timeout on chan %s\n", ast_channel_name(chan)); + rtn = 1; break; } @@ -256,10 +178,8 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int continue; } - f = ast_read(chan); - - if (f == NULL) { - res = -1; + if ((f = ast_read(chan)) == NULL) { + rtn = -1; break; } @@ -269,7 +189,7 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int ast_channel_hangupcause_set(chan, f->data.uint32); } ast_frfree(f); - res = -1; + rtn = -1; break; } @@ -279,8 +199,8 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int continue; } - digit_string[i++] = f->subclass.integer; /* save digit */ - + /* save digit */ + digit_string[i++] = f->subclass.integer; ast_frfree(f); /* If we have all the digits we expect, leave */ @@ -290,23 +210,28 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int lastdigittime = ast_tvnow(); } - digit_string[i] = '\0'; /* Nul terminate the end of the digit string */ - return res; + /* Nul terminate the end of the digit string */ + digit_string[i] = '\0'; + return rtn; } -/* -* Write the metadata to the log file -*/ -static int write_metadata( FILE *logfile, char *signalling_type, struct ast_channel *chan) +/** + * Write metadata to log file + * + * @param logfile Log File Pointer + * @param signalling_type Signaling Type + * @param chan Asterisk Channel + * @return 0 if successful, -1 otherwise + */ +static int write_metadata(FILE *logfile, char *signalling_type, struct ast_channel *chan) { - int res = 0; struct timeval t; struct ast_tm now; char *cl; char *cn; char workstring[80]; char timestamp[80]; - + /* Extract the caller ID location */ ast_copy_string(workstring, S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""), @@ -326,136 +251,180 @@ static int write_metadata( FILE *logfile, char *signalling_type, struct ast_chan /* Format the time */ ast_strftime(timestamp, sizeof(timestamp), time_stamp_format, &now); - res = fprintf(logfile, "\n\n[metadata]\n\n"); - if (res >= 0) { - res = fprintf(logfile, "PROTOCOL=%s\n", signalling_type); - } - if (res >= 0) { - res = fprintf(logfile, "CALLINGFROM=%s\n", cl); - } - if (res >= 0) { - res = fprintf(logfile, "CALLERNAME=%s\n", cn); - } - if (res >= 0) { - res = fprintf(logfile, "TIMESTAMP=%s\n\n", timestamp); - } - if (res >= 0) { - res = fprintf(logfile, "[events]\n\n"); - } - if (res < 0) { + if (fprintf(logfile, "\n\n[metadata]\n\n" + "PROTOCOL=%s\n" + "CALLINGFROM=%s\n" + "CALLERNAME=%s\n" + "TIMESTAMP=%s\n\n" + "[events]\n\n", signalling_type, cl, cn, timestamp) < 0) { ast_verb(3, "AlarmReceiver: can't write metadata\n"); - ast_debug(1,"AlarmReceiver: can't write metadata\n"); - } else { - res = 0; + ast_debug(1, "AlarmReceiver: can't write metadata\n"); + return -1; } - return res; + return 0; } -/* -* Write a single event to the log file -*/ -static int write_event( FILE *logfile, event_node_t *event) +/** + * Log a single event + * + * @param logfile Log File Pointer + * @param event Event Structure + * @return 0 if successful, -1 otherwise + */ +static int write_event(FILE *logfile, event_node_t *event) { - int res = 0; - if (fprintf(logfile, "%s\n", event->data) < 0) - res = -1; - - return res; + return -1; + return 0; } -/* -* If we are configured to log events, do so here. -* -*/ -static int log_events(struct ast_channel *chan, char *signalling_type, event_node_t *event) +/** + * Log events if configuration key logindividualevents is enabled + * + * @param chan Asterisk Channel + * @param signalling_type Signaling Type + * @param event Event Structure + * @return + */ +static int log_events(struct ast_channel *chan, char *signalling_type, event_node_t *event) { - - int res = 0; - char workstring[sizeof(event_spool_dir)+sizeof(event_file)] = ""; + char workstring[sizeof(event_spool_dir) + sizeof(event_file)] = ""; int fd; FILE *logfile; event_node_t *elp = event; - + if (!ast_strlen_zero(event_spool_dir)) { - + /* Make a template */ ast_copy_string(workstring, event_spool_dir, sizeof(workstring)); strncat(workstring, event_file, sizeof(workstring) - strlen(workstring) - 1); - + /* Make the temporary file */ fd = mkstemp(workstring); - + if (fd == -1) { ast_verb(3, "AlarmReceiver: can't make temporary file\n"); - ast_debug(1,"AlarmReceiver: can't make temporary file\n"); - res = -1; + ast_debug(1, "AlarmReceiver: can't make temporary file\n"); + return -1; } - if (!res) { - logfile = fdopen(fd, "w"); - if (logfile) { - /* Write the file */ - res = write_metadata(logfile, signalling_type, chan); - if (!res) - while ((!res) && (elp != NULL)) { - res = write_event(logfile, elp); - elp = elp->next; - } - if (!res) { - if (fflush(logfile) == EOF) - res = -1; - if (!res) { - if (fclose(logfile) == EOF) - res = -1; - } - } - } else - res = -1; + if ((logfile = fdopen(fd, "w")) == NULL) { + return -1; + } + + /* Write the file */ + if (write_metadata(logfile, signalling_type, chan) != 0) { + fflush(logfile); + fclose(logfile); + return -1; } + + while ((write_event(logfile, elp) > 0) && (elp != NULL)) { + elp = elp->next; + } + + fflush(logfile); + fclose(logfile); } - return res; + return 0; } -/* -* This function implements the logic to receive the Ademco contact ID format. -* -* The function will return 0 when the caller hangs up, else a -1 if there was a problem. -*/ -static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, int fdto, int sdto, int tldn, event_node_t **ehead) +/** + * Verify Ademco checksum + * + * @param event Received DTMF String + * @return 0 if successful, -1 otherwise + */ +static int ademco_verify_checksum(char *event) { + static char digit_map[15] = "0123456789*#ABC"; + static unsigned char digit_weights[15] = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15 }; + + int checksum = 0; int i, j; + + for (j = 0, checksum = 0; j < 16; j++) { + for (i = 0; i < sizeof(digit_map); i++) { + if (digit_map[i] == event[j]) + break; + } + + if (i == 16) + break; + checksum += digit_weights[i]; + } + + if (i == 16) { + ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]); + return -1; + } + + /* Checksum is mod(15) of the total */ + if ((checksum % 15) == 0) { + return 0; + } + + return -1; +} + +/** + * Receive Ademco ContactID Data String + * + * @param chan + * @param data + * @param fdto + * @param sdto + * @param tldn + * @param ehead + * @return 0 if successful, -1 otherwise + */ +static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, int fdto, int sdto, int tldn, event_node_t **ehead) +{ int res = 0; - int checksum; + int exit_on_next = 0; char event[17]; event_node_t *enew, *elp; int got_some_digits = 0; int events_received = 0; int ack_retries = 0; - - static char digit_map[15] = "0123456789*#ABC"; - static unsigned char digit_weights[15] = {10,1,2,3,4,5,6,7,8,9,11,12,13,14,15}; 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 ( res >= 0) + + if (exit_on_next) { + res = ast_safe_sleep(chan, 200); + res = ast_playtones_start(chan, tldn, "1400", 0); + if (res == 0) { + ast_safe_sleep(chan, 900); + ast_playtones_stop(chan); + } + res = 0; + break; + } + + if (res >= 0) res = receive_dtmf_digits(chan, event, sizeof(event) - 1, fdto, sdto); if (res < 0) { if (events_received == 0) { @@ -489,28 +458,7 @@ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, ast_debug(1, "AlarmReceiver: Received event: %s\n", event); /* Calculate checksum */ - - for (j = 0, checksum = 0; j < 16; j++) { - for (i = 0; i < sizeof(digit_map); i++) { - if (digit_map[i] == event[j]) - break; - } - - if (i == 16) - break; - - checksum += digit_weights[i]; - } - if (i == 16) { - ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]); - continue; /* Bad character */ - } - - /* Checksum is mod(15) of the total */ - - checksum = checksum % 15; - - if (checksum) { + if (ademco_verify_checksum(event)) { database_increment("checksum-errors"); ast_verb(2, "AlarmReceiver: Nonzero checksum\n"); ast_debug(1, "AlarmReceiver: Nonzero checksum\n"); @@ -518,9 +466,8 @@ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, } /* Check the message type for correctness */ - - if (strncmp(event + 4, "18", 2)) { - if (strncmp(event + 4, "98", 2)) { + if (strncmp(event + 4, ADEMCO_MSG_TYPE_1, 2)) { + if (strncmp(event + 4, ADEMCO_MSG_TYPE_2, 2)) { database_increment("format-errors"); ast_verb(2, "AlarmReceiver: Wrong message type\n"); ast_debug(1, "AlarmReceiver: Wrong message type\n"); @@ -550,6 +497,12 @@ static int receive_ademco_contact_id(struct ast_channel *chan, const void *data, elp->next = enew; } + /* Audio call follows, exit alarm receiver app */ + if (!strncmp(event + 7, ADEMCO_AUDIO_CALL_NEXT, 3)) { + ast_verb(4, "AlarmReceiver: App exiting... Audio call next!\n"); + exit_on_next = 1; + } + if (res > 0) res = 0; @@ -560,18 +513,26 @@ 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; } -/* -* This is the main function called by Asterisk Core whenever the App is invoked in the extension logic. -* This function will always return 0. -*/ +/** + * This is the main function called by Asterisk Core whenever the App is invoked in the extension logic. + * + * @param chan + * @param data + * @return Always return 0 + */ static int alarmreceiver_exec(struct ast_channel *chan, const char *data) { int res = 0; @@ -641,13 +602,15 @@ static int alarmreceiver_exec(struct ast_channel *chan, const char *data) return 0; } -/* -* Load the configuration from the configuration file -*/ +/** + * Load the configuration from the configuration file + * + * @return 1 if sucessfull, 0 otherwise + */ static int load_config(void) { struct ast_config *cfg; - const char *p; + const char *value; struct ast_flags config_flags = { 0 }; /* Read in the config file */ @@ -657,70 +620,79 @@ static int load_config(void) ast_verb(4, "AlarmReceiver: No config file\n"); return 0; } else if (cfg == CONFIG_STATUS_FILEINVALID) { - ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", ALMRCV_CONFIG); + ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", + ALMRCV_CONFIG); return 0; - } else { - p = ast_variable_retrieve(cfg, "general", "eventcmd"); - if (p) { - ast_copy_string(event_app, p, sizeof(event_app)); + } + + if ((value = ast_variable_retrieve(cfg, "general", "eventcmd")) != NULL) { + ast_copy_string(event_app, value, sizeof(event_app)); + } + + if ((value = ast_variable_retrieve(cfg, "general", "loudness")) != NULL) { + toneloudness = atoi(value); + if (toneloudness < 100) { + toneloudness = 100; } - p = ast_variable_retrieve(cfg, "general", "loudness"); - if (p) { - toneloudness = atoi(p); - if(toneloudness < 100) - toneloudness = 100; - if(toneloudness > 8192) - toneloudness = 8192; + if (toneloudness > 8192) { + toneloudness = 8192; } - p = ast_variable_retrieve(cfg, "general", "fdtimeout"); - if (p) { - fdtimeout = atoi(p); - if(fdtimeout < 1000) - fdtimeout = 1000; - if(fdtimeout > 10000) - fdtimeout = 10000; + } + + if ((value = ast_variable_retrieve(cfg, "general", "fdtimeout")) != NULL) { + fdtimeout = atoi(value); + if (fdtimeout < 1000) { + fdtimeout = 1000; + } + if (fdtimeout > 10000) { + fdtimeout = 10000; } + } - p = ast_variable_retrieve(cfg, "general", "sdtimeout"); - if (p) { - sdtimeout = atoi(p); - if(sdtimeout < 110) - sdtimeout = 110; - if(sdtimeout > 4000) - sdtimeout = 4000; + if ((value = ast_variable_retrieve(cfg, "general", "sdtimeout")) != NULL) { + sdtimeout = atoi(value); + if (sdtimeout < 110) { + sdtimeout = 110; + } + if (sdtimeout > 4000) { + sdtimeout = 4000; } + } - p = ast_variable_retrieve(cfg, "general", "logindividualevents"); - if (p) - log_individual_events = ast_true(p); + if ((value = ast_variable_retrieve(cfg, "general", "logindividualevents")) != NULL) { + log_individual_events = ast_true(value); + } - p = ast_variable_retrieve(cfg, "general", "eventspooldir"); - if (p) { - ast_copy_string(event_spool_dir, p, sizeof(event_spool_dir)); - } + if ((value = ast_variable_retrieve(cfg, "general", "eventspooldir")) != NULL) { + ast_copy_string(event_spool_dir, value, sizeof(event_spool_dir)); + } - p = ast_variable_retrieve(cfg, "general", "timestampformat"); - if (p) { - ast_copy_string(time_stamp_format, p, sizeof(time_stamp_format)); - } + if ((value = ast_variable_retrieve(cfg, "general", "timestampformat")) != NULL) { + ast_copy_string(time_stamp_format, value, sizeof(time_stamp_format)); + } - p = ast_variable_retrieve(cfg, "general", "db-family"); - if (p) { - ast_copy_string(db_family, p, sizeof(db_family)); - } - ast_config_destroy(cfg); + if ((value = ast_variable_retrieve(cfg, "general", "db-family")) != NULL) { + ast_copy_string(db_family, value, sizeof(db_family)); } + + ast_config_destroy(cfg); + return 1; } -/* -* These functions are required to implement an Asterisk App. -*/ +/** + * Unregister Alarm Receiver App + * @return + */ static int unload_module(void) { return ast_unregister_application(app); } +/** + * Register Alarm Receiver App + * @return + */ static int load_module(void) { if (load_config()) { -- 1.7.9.6 (Apple Git-31.1)