diff --git a/daemon/alix-usvd.c b/daemon/alix-usvd.c index 02022ba..ed3af1f 100644 --- a/daemon/alix-usvd.c +++ b/daemon/alix-usvd.c @@ -36,6 +36,9 @@ #include "logging.h" #include "unixsocket.h" +/* eeprom parameters / thresholds & defaults */ +#include "../eeprom.h" + #define DEFAULT_CONFIG "alix-usvd.conf" #define DEFAULT_LOGFILE "alix-usvd.log" #define DEFAULT_SOCKET "alix-usvd.sock" @@ -64,6 +67,12 @@ enum { STATE_POWEROFF = 0x10, }; +enum { + I2CDEV_ERR = -1, + I2CDEV_UNKNOWN = 0, + I2CDEV_OK = 1, +}; + static struct option opts[] = { {"nofork", 0, 0, 'd'}, {"config", 1, 0, 'c'}, @@ -96,11 +105,11 @@ static int i2cdev_open(const char *i2c_device, int i2c_address) return fd; } -static int i2cdev_open_wrap(void) +static int alix_open(void) { static const char *i2c_device; static int i2c_address; - static int i2c_last_opened; + static int i2c_status = I2CDEV_UNKNOWN; if (i2c_device == NULL) { i2c_device = config_get_string("global", "i2c-device", "/dev/i2c-0"); @@ -109,20 +118,84 @@ static int i2cdev_open_wrap(void) int dev = i2cdev_open(i2c_device, i2c_address); if (dev < 0) { - if (i2c_last_opened != -1) + if (i2c_status != I2CDEV_ERR) log_print(LOG_WARN, "failed to open %s:0x%02x", i2c_device, i2c_address); - i2c_last_opened = -1; + i2c_status = I2CDEV_ERR; return -1; } - if (i2c_last_opened < 1) + if (i2c_status != I2CDEV_OK) log_print(LOG_INFO, "successfully opened %s:0x%02x", i2c_device, i2c_address); - i2c_last_opened = 1; + i2c_status = I2CDEV_OK; return dev; } +static int check_thresholds(void) +{ + int dev = alix_open(); + if (dev < 0) + return -1; + + struct ee_param defaults = DEFAULT_PARAMETERS; + + struct ee_param config; + config.uin_loss = config_get_int("thresholds", "uin_loss" , defaults.uin_loss); + config.uin_restore = config_get_int("thresholds", "uin_restore" , defaults.uin_restore); + config.ubat_full = config_get_int("thresholds", "ubat_full" , defaults.ubat_full); + config.ubat_low = config_get_int("thresholds", "ubat_low" , defaults.ubat_low); + config.ubat_critical = config_get_int("thresholds", "ubat_critical" , defaults.ubat_critical); + config.ibat_full = config_get_int("thresholds", "ibat_full" , defaults.ibat_full); + + struct ee_param device; + device.uin_loss = i2c_smbus_read_word_data(dev, REG_UIN_LOSS); + device.uin_restore = i2c_smbus_read_word_data(dev, REG_UIN_RESTORE); + device.ubat_full = i2c_smbus_read_word_data(dev, REG_UBAT_FULL); + device.ubat_low = i2c_smbus_read_word_data(dev, REG_UBAT_LOW); + device.ubat_critical = i2c_smbus_read_word_data(dev, REG_UBAT_CRIT); + device.ibat_full = i2c_smbus_read_word_data(dev, REG_IBAT_FULL); + device.crc16 = 0x0000; + + int changed = 0; + + if (device.uin_loss != config.uin_loss) { + i2c_smbus_write_word_data(dev, REG_UIN_LOSS, config.uin_loss); + changed = 1; + } + + if (device.uin_restore != config.uin_restore) { + i2c_smbus_write_word_data(dev, REG_UIN_RESTORE, config.uin_restore); + changed = 1; + } + + if (device.ubat_full != config.ubat_full) { + i2c_smbus_write_word_data(dev, REG_UBAT_FULL, config.ubat_full); + changed = 1; + } + + if (device.ubat_low != config.ubat_low) { + i2c_smbus_write_word_data(dev, REG_UBAT_LOW, config.ubat_low); + changed = 1; + } + + if (device.ubat_critical != config.ubat_critical) { + i2c_smbus_write_word_data(dev, REG_UBAT_CRIT, config.ubat_critical); + changed = 1; + } + + if (device.ibat_full != config.ibat_full) { + i2c_smbus_write_word_data(dev, REG_IBAT_FULL, config.ibat_full); + changed = 1; + } + + if (changed == 1) + i2c_smbus_write_word_data(dev, REG_CRC, 0x0000); + + close(dev); + return changed; +} + static char * state2str(int state) { switch (state) { @@ -167,9 +240,9 @@ static int str2state(char *buf) return state; } -static int usv_state = -1; +static int alix_state = -1; -static void check_state(int old_state, int new_state) +static void alix_state_change(int old_state, int new_state) { log_print(LOG_INFO, "usv state changed: %s => %s", state2str(old_state), state2str(new_state)); /* TODO: send email */ @@ -182,18 +255,18 @@ static int unix_read_cb(int fd, void *privdata) if (len < 0) return -1; - int dev = i2cdev_open_wrap(); + int dev = alix_open(); if (dev < 0) return 0; int new_state = str2state(buf); if (new_state != -1) { if (i2c_smbus_write_word_data(dev, REG_STATUS, new_state) == 0) { - usv_state = new_state; - log_print(LOG_INFO, "set state to %s", state2str(usv_state)); + alix_state = new_state; + log_print(LOG_INFO, "set state to %s", state2str(alix_state)); } - int len = snprintf(buf, sizeof(buf), "%s", state2str(usv_state)); + int len = snprintf(buf, sizeof(buf), "%s", state2str(alix_state)); write(fd, buf, len); } else if (strncmp(buf, "status", 6) == 0) { @@ -208,9 +281,9 @@ static int unix_read_cb(int fd, void *privdata) adc_ubat = 0; adc_uin = 0; - } else if (new_state != usv_state) { - check_state(usv_state, new_state); - usv_state = new_state; + } else if (new_state != alix_state) { + alix_state_change(alix_state, new_state); + alix_state = new_state; } int len = snprintf(buf, sizeof(buf), "%s:%d:%d:%d", @@ -237,7 +310,7 @@ static int unix_accept_cb(int fd, void *privdata) static int status_interval(void *privdata) { - int dev = i2cdev_open_wrap(); + int dev = alix_open(); if (dev < 0) return 0; @@ -247,9 +320,9 @@ static int status_interval(void *privdata) return 0; } - if (new_state != usv_state) { - check_state(usv_state, new_state); - usv_state = new_state; + if (new_state != alix_state) { + alix_state_change(alix_state, new_state); + alix_state = new_state; } close(dev); @@ -268,6 +341,7 @@ int main(int argc, char *argv[]) case 'd': /* debug */ debug = 1; break; + case 'c': /* configfile path */ config = optarg; break; @@ -296,6 +370,8 @@ int main(int argc, char *argv[]) exit(1); if (debug == 0) { + /* TODO: check PID-file */ + /* start logging */ const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); if (log_init(logfile) < 0) @@ -321,11 +397,12 @@ int main(int argc, char *argv[]) struct timeval tv = { .tv_sec = interval, .tv_usec = 0 }; event_add_timeout(&tv, status_interval, NULL); - int dev = i2cdev_open_wrap(); - if (dev != -1) { - /* TODO: check thresholds */ - close(dev); - } + int tmp = check_thresholds(); + if (tmp < 0) + log_print(LOG_WARN, "unable to check thresholds"); + + else if (tmp == 1) + log_print(LOG_INFO, "updated thresholds"); event_loop(); return 0; diff --git a/daemon/alix-usvd.conf b/daemon/alix-usvd.conf index 964ef0d..31ecb60 100644 --- a/daemon/alix-usvd.conf +++ b/daemon/alix-usvd.conf @@ -17,5 +17,5 @@ uin_loss 12000 uin_restore 14000 ubat_full 13300 ubat_low 12000 -ubat_crit 11000 +ubat_critical 11000 ibat_full 150