move timeval-diff function into utils.h from app_alarmreceiver.c
[asterisk/asterisk.git] / apps / app_alarmreceiver.c
index b8300f9..bdf264e 100755 (executable)
@@ -3,7 +3,7 @@
  *
  * Central Station Alarm receiver for Ademco Contact ID  
  * 
- * Copyright (C) 2004 Steve Rodgers
+ * Copyright (C)  2004 - 2005 Steve Rodgers
  *
  * Steve Rodgers <hwstar@rodgers.sdcoxmail.com>
  *
  *
  */ 
  
-#include <asterisk/lock.h>
-#include <asterisk/file.h>
-#include <asterisk/logger.h>
-#include <asterisk/channel.h>
-#include <asterisk/pbx.h>
-#include <asterisk/module.h>
-#include <asterisk/translate.h>
-#include <asterisk/ulaw.h>
-#include <asterisk/options.h>
-#include <asterisk/app.h>
-#include <asterisk/dsp.h>
-#include <asterisk/config.h>
-#include <asterisk/localtime.h>
-#include <asterisk/callerid.h>
-#include <asterisk/astdb.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <pthread.h>
 #include <sys/time.h>
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/lock.h"
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/translate.h"
+#include "asterisk/ulaw.h"
+#include "asterisk/options.h"
+#include "asterisk/app.h"
+#include "asterisk/dsp.h"
+#include "asterisk/config.h"
+#include "asterisk/localtime.h"
+#include "asterisk/callerid.h"
+#include "asterisk/astdb.h"
+
 #define ALMRCV_CONFIG "alarmreceiver.conf"
 #define ADEMCO_CONTACT_ID "ADEMCO_CONTACT_ID"
 
@@ -56,7 +60,7 @@ static char *tdesc = "Alarm Receiver for Asterisk";
 
 static char *app = "AlarmReceiver";
 
-static char *synopsis = "Provide support for receving alarm reports from a burglar or fire alarm panel\n";
+static char *synopsis = "Provide support for receving alarm reports from a burglar or fire alarm panel";
 static char *descrip =
 "Alarm receiver application for Asterisk. Only 1 signalling format is supported at this time:\n"
 "Ademco Contact ID. This application should be called whenever there is an alarm panel calling in\n"
@@ -123,7 +127,7 @@ static void database_increment( char *key )
        if(option_verbose >= 4)
                ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: New value for %s: %u\n", key, v);
                
-       snprintf(value, sizeof(value) - 1, "%u", v);
+       snprintf(value, sizeof(value), "%u", v);
        
        res = ast_db_put(db_family, key, value);
        
@@ -215,20 +219,6 @@ static int send_tone_burst(struct ast_channel *chan, float freq, int duration, i
 }
 
 /*
-* Return the difference in milliseconds between two timeval structs
-*/
-
-static int ms_diff(struct timeval *tv1, struct timeval *tv2){
-
-       int     ms;
-       
-       ms = (tv1->tv_sec - tv2->tv_sec) * 1000;
-       ms += (tv1->tv_usec - tv2->tv_usec) / 1000;
-       
-       return(ms);
-}
-
-/*
 * 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.
@@ -252,7 +242,7 @@ static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int
                gettimeofday(&now,NULL);
                
                  /* if outa time, leave */
-               if (ms_diff(&now,&lastdigittime) > 
+               if (ast_tvdiff_ms(&lastdigittime, &now) > 
                    ((i > 0) ? sdto : fdto)){
                        if(option_verbose >= 4)
                                ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: DTMF Digit Timeout on %s\n", chan->name);
@@ -319,8 +309,8 @@ static int write_metadata( FILE *logfile, char *signalling_type, struct ast_chan
        char timestamp[80];
        
        /* Extract the caller ID location */
-       
-       strncpy(workstring, chan->callerid, sizeof(workstring) - 1);
+       if (chan->cid.cid_num)
+               strncpy(workstring, chan->cid.cid_num, sizeof(workstring) - 1);
        workstring[sizeof(workstring) - 1] = '\0';
        
        ast_callerid_parse(workstring, &cn, &cl);
@@ -390,7 +380,7 @@ static int log_events(struct ast_channel *chan,  char *signalling_type, event_no
 {
 
        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;
@@ -399,8 +389,8 @@ static int log_events(struct ast_channel *chan,  char *signalling_type, event_no
                
                /* Make a template */
                
-               strcpy(workstring, event_spool_dir);
-               strcat(workstring, event_file);
+               strncpy(workstring, event_spool_dir, sizeof(workstring) - 1);
+               strncat(workstring, event_file, sizeof(workstring) - strlen(workstring) - 1);
                
                /* Make the temporary file */
                
@@ -587,9 +577,11 @@ static int receive_ademco_contact_id( struct ast_channel *chan, void *data, int
                        res = -1;
                         break;
                }
+
+               memset(enew, 0, sizeof(event_node_t));
                
                enew->next = NULL;
-               strncpy(enew->data, event, sizeof(enew->data));
+               strncpy(enew->data, event, sizeof(enew->data) - 1);
 
                /*
                * Insert event onto end of list
@@ -639,7 +631,7 @@ static int alarmreceiver_exec(struct ast_channel *chan, void *data)
        int res = 0;
        struct localuser *u;
        event_node_t *elp, *efree;
-       char signalling_type[64];
+       char signalling_type[64] = "";
 
        event_node_t *event_head = NULL;
 
@@ -662,7 +654,7 @@ static int alarmreceiver_exec(struct ast_channel *chan, void *data)
 
        /* Set default values for this invokation of the application */
        
-       strcpy(signalling_type, ADEMCO_CONTACT_ID);
+       strncpy(signalling_type, ADEMCO_CONTACT_ID, sizeof(signalling_type) - 1);
 
 
        /* Answer the channel if it is not already */
@@ -746,7 +738,7 @@ static int load_config(void)
 
        /* Read in the config file */
 
-       cfg = ast_load(ALMRCV_CONFIG);
+       cfg = ast_config_load(ALMRCV_CONFIG);
                                                                                                                                   
        if(!cfg){
        
@@ -816,7 +808,7 @@ static int load_config(void)
                        strncpy(db_family, p, sizeof(db_family) - 1);
                        db_family[sizeof(db_family) - 1] = '\0';
                }
-
+               ast_config_destroy(cfg);
        }
        return 0;