add threshold handling

This commit is contained in:
Olaf Rempel 2009-04-19 13:48:57 +02:00
parent c053654089
commit 4f3dcb258f
2 changed files with 102 additions and 25 deletions

View File

@ -36,6 +36,9 @@
#include "logging.h" #include "logging.h"
#include "unixsocket.h" #include "unixsocket.h"
/* eeprom parameters / thresholds & defaults */
#include "../eeprom.h"
#define DEFAULT_CONFIG "alix-usvd.conf" #define DEFAULT_CONFIG "alix-usvd.conf"
#define DEFAULT_LOGFILE "alix-usvd.log" #define DEFAULT_LOGFILE "alix-usvd.log"
#define DEFAULT_SOCKET "alix-usvd.sock" #define DEFAULT_SOCKET "alix-usvd.sock"
@ -64,6 +67,12 @@ enum {
STATE_POWEROFF = 0x10, STATE_POWEROFF = 0x10,
}; };
enum {
I2CDEV_ERR = -1,
I2CDEV_UNKNOWN = 0,
I2CDEV_OK = 1,
};
static struct option opts[] = { static struct option opts[] = {
{"nofork", 0, 0, 'd'}, {"nofork", 0, 0, 'd'},
{"config", 1, 0, 'c'}, {"config", 1, 0, 'c'},
@ -96,11 +105,11 @@ static int i2cdev_open(const char *i2c_device, int i2c_address)
return fd; return fd;
} }
static int i2cdev_open_wrap(void) static int alix_open(void)
{ {
static const char *i2c_device; static const char *i2c_device;
static int i2c_address; static int i2c_address;
static int i2c_last_opened; static int i2c_status = I2CDEV_UNKNOWN;
if (i2c_device == NULL) { if (i2c_device == NULL) {
i2c_device = config_get_string("global", "i2c-device", "/dev/i2c-0"); 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); int dev = i2cdev_open(i2c_device, i2c_address);
if (dev < 0) { 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); log_print(LOG_WARN, "failed to open %s:0x%02x", i2c_device, i2c_address);
i2c_last_opened = -1; i2c_status = I2CDEV_ERR;
return -1; 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); log_print(LOG_INFO, "successfully opened %s:0x%02x", i2c_device, i2c_address);
i2c_last_opened = 1; i2c_status = I2CDEV_OK;
return dev; 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) static char * state2str(int state)
{ {
switch (state) { switch (state) {
@ -167,9 +240,9 @@ static int str2state(char *buf)
return state; 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)); log_print(LOG_INFO, "usv state changed: %s => %s", state2str(old_state), state2str(new_state));
/* TODO: send email */ /* TODO: send email */
@ -182,18 +255,18 @@ static int unix_read_cb(int fd, void *privdata)
if (len < 0) if (len < 0)
return -1; return -1;
int dev = i2cdev_open_wrap(); int dev = alix_open();
if (dev < 0) if (dev < 0)
return 0; return 0;
int new_state = str2state(buf); int new_state = str2state(buf);
if (new_state != -1) { if (new_state != -1) {
if (i2c_smbus_write_word_data(dev, REG_STATUS, new_state) == 0) { if (i2c_smbus_write_word_data(dev, REG_STATUS, new_state) == 0) {
usv_state = new_state; alix_state = new_state;
log_print(LOG_INFO, "set state to %s", state2str(usv_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); write(fd, buf, len);
} else if (strncmp(buf, "status", 6) == 0) { } else if (strncmp(buf, "status", 6) == 0) {
@ -208,9 +281,9 @@ static int unix_read_cb(int fd, void *privdata)
adc_ubat = 0; adc_ubat = 0;
adc_uin = 0; adc_uin = 0;
} else if (new_state != usv_state) { } else if (new_state != alix_state) {
check_state(usv_state, new_state); alix_state_change(alix_state, new_state);
usv_state = new_state; alix_state = new_state;
} }
int len = snprintf(buf, sizeof(buf), "%s:%d:%d:%d", 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) static int status_interval(void *privdata)
{ {
int dev = i2cdev_open_wrap(); int dev = alix_open();
if (dev < 0) if (dev < 0)
return 0; return 0;
@ -247,9 +320,9 @@ static int status_interval(void *privdata)
return 0; return 0;
} }
if (new_state != usv_state) { if (new_state != alix_state) {
check_state(usv_state, new_state); alix_state_change(alix_state, new_state);
usv_state = new_state; alix_state = new_state;
} }
close(dev); close(dev);
@ -268,6 +341,7 @@ int main(int argc, char *argv[])
case 'd': /* debug */ case 'd': /* debug */
debug = 1; debug = 1;
break; break;
case 'c': /* configfile path */ case 'c': /* configfile path */
config = optarg; config = optarg;
break; break;
@ -296,6 +370,8 @@ int main(int argc, char *argv[])
exit(1); exit(1);
if (debug == 0) { if (debug == 0) {
/* TODO: check PID-file */
/* start logging */ /* start logging */
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (log_init(logfile) < 0) if (log_init(logfile) < 0)
@ -321,11 +397,12 @@ int main(int argc, char *argv[])
struct timeval tv = { .tv_sec = interval, .tv_usec = 0 }; struct timeval tv = { .tv_sec = interval, .tv_usec = 0 };
event_add_timeout(&tv, status_interval, NULL); event_add_timeout(&tv, status_interval, NULL);
int dev = i2cdev_open_wrap(); int tmp = check_thresholds();
if (dev != -1) { if (tmp < 0)
/* TODO: check thresholds */ log_print(LOG_WARN, "unable to check thresholds");
close(dev);
} else if (tmp == 1)
log_print(LOG_INFO, "updated thresholds");
event_loop(); event_loop();
return 0; return 0;

View File

@ -17,5 +17,5 @@ uin_loss 12000
uin_restore 14000 uin_restore 14000
ubat_full 13300 ubat_full 13300
ubat_low 12000 ubat_low 12000
ubat_crit 11000 ubat_critical 11000
ibat_full 150 ibat_full 150