diff --git a/daemon/Makefile.am b/daemon/Makefile.am index b29e8d0..f9e7b1b 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -3,7 +3,7 @@ include_HEADERS = $(top_srcdir)/yasnd.h yasnd_LDADD = ${libconfuse_LIBS} yasnd_LDADD += $(top_srcdir)/lib/libyasnd.la sbin_PROGRAMS = yasnd -yasnd_SOURCES = yasnd.c yasnd-ipc.c +yasnd_SOURCES = yasnd.c yasnd-ipc.c yasnd-lpt.c yasnd_CPPFLAGS = -I$(top_srcdir) distclean-local: diff --git a/daemon/yasnd-lpt.c b/daemon/yasnd-lpt.c new file mode 100644 index 0000000..b4c55cb --- /dev/null +++ b/daemon/yasnd-lpt.c @@ -0,0 +1,76 @@ +#include +#include +#include + +bool lpt_enable=false; // control usage of LPT port to reset target devices +int lpt_port=0x378; // LPT port in hex(0x378 is usually LPT1) + +/* + Function, that turn off all LPT port pins. + This is needed to unlock LPT control circuit, when + management computer is just booted +*/ +void lpt_init() +{ + if (lpt_enable) + { + if (ioperm (lpt_port, 3, 1)) + { + log_event("Error: Unlocking the circuit fails due to LPT port access error"); + // disable LPT port usage + lpt_enable=false; + return; + } + // Unlock circuit + outb (0, lpt_port); + } +} + +/* + Function, that turn on first LPT port pin and turn off others. + This is needed to lock LPT control circuit, when + management computer is rebooted +*/ +void lpt_lock_cond(bool ignore_options) +{ + if (lpt_enable || ignore_options) + { + if (ioperm (lpt_port, 3, 1)) + { + log_event("Error: Locking the circuit fails due to LPT port access error"); + // disable LPT port usage + lpt_enable=false; + return; + } + // Lock circuit + outb (1, lpt_port); + } +} + +/* + Function, that turn on only one LPT port pin, defined by it argument, wait a second, + and turn off all pins. That is enough to reset specified host. +*/ +void reset_pin(int pin_num) +{ + if (lpt_enable) + { + if (pin_num==0) + return; // there is no LPT control for target host + if (pin_num<2 || pin_num>8) + { + log_event("Error: incorrent LPT pin number"); + return; + } + int pins = 1<<(pin_num-1); + if (ioperm (lpt_port, 3, 1)) + { + log_event("Error: LPT port access error"); + return; + } + // Reset host + outb (pins, lpt_port); + sleep(1); + outb (0, lpt_port); + } +} diff --git a/daemon/yasnd-lpt.h b/daemon/yasnd-lpt.h new file mode 100644 index 0000000..af6cb47 --- /dev/null +++ b/daemon/yasnd-lpt.h @@ -0,0 +1,14 @@ +#ifndef YASND_LPT_H +#define YASND_LPT_H + +// External variables +extern bool lpt_enable; // control usage of LPT port to reset target devices +extern int lpt_port; // LPT port in hex(0x378 is usually LPT1) + +// External functions +extern void lpt_init(); +extern void lpt_lock_cond(bool ignore_options); +extern void lpt_lock(); +extern void reset_pin(int pin_num); + +#endif diff --git a/daemon/yasnd.c b/daemon/yasnd.c index 368dba9..fa023ec 100644 --- a/daemon/yasnd.c +++ b/daemon/yasnd.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -7,6 +6,7 @@ #include #include "yasnd.h" +#include "yasnd-lpt.h" cfg_t *cfg; // pointer to configuration structure char pidfile[]="/var/run/yasnd.pid"; // pidfile path @@ -18,8 +18,6 @@ char* phone_device=NULL; // phone device for gammu, for example: "/dev/ttyACM0" char* phone_model=NULL; // gammu phone model, for example: "at" char* phone_connection=NULL; // gammu phone connection type, for example: "at" char* recipient_number=NULL; // recipient of sms alerts -bool lpt_enable=false; // control usage of LPT port to reset target devices -int lpt_port=0x378; // LPT port in hex(0x378 is usually LPT1) bool loop_locked=false; // flag for locking main loop while config re-reading int failures_first=0; // count of failures while hosts checking, that triggers SMS sending int failures_second=0; // count of failures while hosts checking, that triggers host's reset @@ -48,47 +46,6 @@ void* allocate_memory(int bytes) return result; } -/* - Function, that turn off all LPT port pins. - This is needed to unlock LPT control circuit, when - management computer is just booted -*/ -void lpt_init() -{ - if (lpt_enable) - { - if (ioperm (lpt_port, 3, 1)) - { - log_event("Error: Unlocking the circuit fails due to LPT port access error"); - // disable LPT port usage - lpt_enable=false; - return; - } - // Unlock circuit - outb (0, lpt_port); - } -} - -/* - Function, that turn on first LPT port pin and turn off others. - This is needed to lock LPT control circuit, when - management computer is rebooted -*/ -void lpt_lock_cond(bool ignore_options) -{ - if (lpt_enable || ignore_options) - { - if (ioperm (lpt_port, 3, 1)) - { - log_event("Error: Locking the circuit fails due to LPT port access error"); - // disable LPT port usage - lpt_enable=false; - return; - } - // Lock circuit - outb (1, lpt_port); - } -} // Previous function, but without ignoring lpt_enable option void lpt_lock() @@ -236,34 +193,6 @@ void check_host(int num,host_decl* host) host->helper_pid=pid; } -/* - Function, that turn on only one LPT port pin, defined by it argument, wait a second, - and turn off all pins. That is enough to reset specified host. -*/ -void reset_pin(int pin_num) -{ - if (lpt_enable) - { - if (pin_num==0) - return; // there is no LPT control for target host - if (pin_num<2 || pin_num>8) - { - log_event("Error: incorrent LPT pin number"); - return; - } - int pins = 1<<(pin_num-1); - if (ioperm (lpt_port, 3, 1)) - { - log_event("Error: LPT port access error"); - return; - } - // Reset host - outb (pins, lpt_port); - sleep(1); - outb (0, lpt_port); - } -} - int loop_function() { // set default signal handlers