From 3e183be5333825b232875892cff5e039037c5bff Mon Sep 17 00:00:00 2001 From: Sergey Popov Date: Tue, 29 Nov 2011 13:47:48 +0400 Subject: [PATCH] Move gammu-related functions into separate file. Change "check_gammu_error" function to return error state. Some configure.ac dependecies added --- Makefile.am | 2 +- configure.ac | 2 ++ yasnd-gammu.c | 14 ++++++++++++++ yasnd.c | 41 +++++++++-------------------------------- yasnd.h | 24 ++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 yasnd-gammu.c create mode 100644 yasnd.h diff --git a/Makefile.am b/Makefile.am index a8a4935..552c043 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ INCLUDES=${gammu_CFLAGS} yasnd_LDADD = ${libconfuse_LIBS} yasnd_LDADD += ${gammu_LIBS} bin_PROGRAMS = yasnd -yasnd_SOURCES = yasnd.c +yasnd_SOURCES = yasnd.c yasnd-gammu.c distclean-local: rm -rf aclocal.m4 autom4te.cache depcomp install-sh missing configure Makefile.in config.h* &>/dev/null diff --git a/configure.ac b/configure.ac index c6f5100..e59799f 100644 --- a/configure.ac +++ b/configure.ac @@ -19,7 +19,9 @@ AC_CHECK_HEADERS([fcntl.h signal.h stdlib.h string.h syslog.h unistd.h]) AC_TYPE_PID_T # Checks for library functions. +AC_CHECK_FUNCS([strdup]) AC_FUNC_FORK +AC_FUNC_MALLOC AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/yasnd-gammu.c b/yasnd-gammu.c new file mode 100644 index 0000000..b59ead3 --- /dev/null +++ b/yasnd-gammu.c @@ -0,0 +1,14 @@ +#include "yasnd.h" + +GSM_Error error; // structure to store possible gammu errors + +bool check_gammu_error(GSM_Error err) +{ + if (err == ERR_NONE) { + return false; + } + char tmp[150]; + sprintf(tmp,"gammu failure: %s\n", GSM_ErrorString(err)); + log_event(tmp); + return true; +} diff --git a/yasnd.c b/yasnd.c index 223e8db..afb32b6 100644 --- a/yasnd.c +++ b/yasnd.c @@ -10,17 +10,8 @@ #include #include #include -#include -// Debug verbosity -#define DEBUG_BASE 1 -#define DEBUG_ALL 2 - -typedef struct { - char* hostname; // address of host - pid_t helper_pid; // pid of helper('pinger') child process - int fail_count; // how many times in a row host was unreachable -} host_decl; +#include "yasnd.h" cfg_t *cfg; // pointer to configuration structure host_decl* hosts=NULL; // structure with hosts' definitions @@ -49,17 +40,6 @@ void* allocate_memory(int bytes) } } -void check_gammu_error(GSM_Error err) -{ - if (err == ERR_NONE) { - return; - } - char tmp[150]; - sprintf(tmp,"gammu failure: %s\n", GSM_ErrorString(err)); - log_event(tmp); - exit(EXIT_FAILURE); -} - void init() { // Initialize config parsing library @@ -93,17 +73,14 @@ void init() // initialize gammu structures GSM_InitLocales(NULL); state_machine = GSM_AllocStateMachine(); - // read gammu config - INI_Section *gammu_cfg; - GSM_Error error; - /* Find it */ - error = GSM_FindGammuRC(&gammu_cfg, NULL); - check_gammu_error(error); - /* Read it */ - error = GSM_ReadConfig(gammu_cfg, GSM_GetConfig(state_machine, 0), 0); - check_gammu_error(error); - /* Free allocated memory */ - INI_Free(gammu_cfg); + /* Set gammu configuration */ + GSM_Config *gammu_cfg = GSM_GetConfig(state_machine, 0); + // first, free old values and set new one + free(gammu_cfg->Device); + gammu_cfg->Device = strdup("/dev/ttyACM0"); + free(gammu_cfg->Connection); + gammu_cfg->Connection = strdup("at"); + strcpy(gammu_cfg->Model, "at"); /* We care only about first configuration */ GSM_SetConfigNum(state_machine, 1); } diff --git a/yasnd.h b/yasnd.h new file mode 100644 index 0000000..6158963 --- /dev/null +++ b/yasnd.h @@ -0,0 +1,24 @@ +#ifndef YASND_H +#define YASND_H + +// Debug verbosity +#define DEBUG_BASE 1 +#define DEBUG_ALL 2 + +#include +#include + +extern void log_event(const char *message); +extern bool check_gammu_error(GSM_Error err); + +typedef struct { + char* hostname; // address of host + pid_t helper_pid; // pid of helper('pinger') child process + int fail_count; // how many times in a row host was unreachable +} host_decl; + +extern int hosts_count; // count of hosts +extern int debug_flag; +extern GSM_Error error; // structure to store possible gammu errors + +#endif