From 6f5ba0b7f7020e344ca1b5b4ff9f44c66e275a2d Mon Sep 17 00:00:00 2001 From: Pedro Kiefer Date: Fri, 20 Jul 2012 13:11:09 -0300 Subject: [PATCH 4/6] Clean up code --- apps/app_alarmreceiver.c | 132 +++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 85 deletions(-) diff --git a/apps/app_alarmreceiver.c b/apps/app_alarmreceiver.c index 22aaf62..8c6e329 100644 --- a/apps/app_alarmreceiver.c +++ b/apps/app_alarmreceiver.c @@ -111,22 +111,18 @@ static char event_file[14] = "/event-XXXXXX"; * 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 ) +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; } @@ -134,12 +130,9 @@ 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; @@ -151,8 +144,8 @@ static void database_increment( char *key ) */ static void make_tone_burst(unsigned char *data, float freq, float loudness, int len, int *x) { - int i; - float val; + int i; + float val; for (i = 0; i < len; i++) { val = loudness * sin((freq * 2.0 * M_PI * (*x)++)/8000.0); @@ -160,7 +153,6 @@ static void make_tone_burst(unsigned char *data, float freq, float loudness, int } /* wrap back around from 8000 */ - if (*x >= 8000) *x = 0; return; @@ -172,7 +164,6 @@ static void make_tone_burst(unsigned char *data, float freq, float loudness, int */ 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; @@ -185,13 +176,11 @@ static int send_tone_burst(struct ast_channel *chan, float freq, int duration, i for (;;) { if (ast_waitfor(chan, -1) < 0) { - res = -1; break; } f = ast_read(chan); if (!f) { - res = -1; break; } @@ -210,12 +199,11 @@ static int send_tone_burst(struct ast_channel *chan, float freq, int duration, i i += wf.datalen / 8; if (i > duration) { ast_frfree(f); - break; + 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)); - res = -1; ast_frfree(f); break; } @@ -223,7 +211,7 @@ static int send_tone_burst(struct ast_channel *chan, float freq, int duration, i ast_frfree(f); } - return res; + return -1; } /* @@ -238,7 +226,7 @@ static int send_tone_burst(struct ast_channel *chan, float freq, int duration, i */ 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; @@ -246,11 +234,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; + rtn = 1; break; } @@ -259,10 +247,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; } @@ -272,7 +258,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; } @@ -282,8 +268,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 */ @@ -294,15 +280,14 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int } digit_string[i] = '\0'; /* Nul terminate the end of the digit string */ - return res; + return rtn; } /* * Write the metadata to the log file */ -static int write_metadata( FILE *logfile, char *signalling_type, struct ast_channel *chan) +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; @@ -329,43 +314,28 @@ 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) { - ast_verb(3, "AlarmReceiver: can't write metadata\n"); + 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; + return -1; } - - return res; + + return 0; } /* * Write a single event to the log file */ -static int write_event( FILE *logfile, event_node_t *event) +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; } /* @@ -374,8 +344,6 @@ static int write_event( FILE *logfile, event_node_t *event) */ 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)] = ""; int fd; FILE *logfile; @@ -393,33 +361,27 @@ static int log_events(struct ast_channel *chan, char *signalling_type, event_no 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; + 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; } /* -- 1.7.9.5