/*************************************************************************** * Copyright (C) 03/2009 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; version 2 of the License * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include "i2c-dev.h" #include "configfile.h" #include "event.h" #include "logging.h" #include "pidfile.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_PIDFILE "alix-usvd.pid" #define DEFAULT_SOCKET "alix-usvd.sock" #define SENDMAIL "/usr/sbin/sendmail -t" enum { REG_STATUS = 0x00, REG_CURRENT = 0x10, REG_UBAT = 0x11, REG_UIN = 0x12, REG_UIN_LOSS = 0x20, REG_UIN_RESTORE = 0x21, REG_UBAT_FULL = 0x22, REG_UBAT_LOW = 0x23, REG_UBAT_CRIT = 0x24, REG_IBAT_FULL = 0x25, REG_CRC = 0x26, }; enum { STATE_IDLE = 0x01, STATE_TEST = 0x02, STATE_CHARGE = 0x04, STATE_DISCHARGE = 0x08, 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'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; static int i2cdev_open(const char *i2c_device, int i2c_address) { int fd = open(i2c_device, O_RDWR); if (fd < 0) return -1; unsigned long funcs; if (ioctl(fd, I2C_FUNCS, &funcs)) { close(fd); return -1; } if ((funcs & I2C_FUNC_SMBUS_WORD_DATA) != I2C_FUNC_SMBUS_WORD_DATA) { close(fd); return -1; } if (ioctl(fd, I2C_SLAVE, i2c_address) < 0) { close(fd); return -1; } return fd; } static int alix_open(void) { static const char *i2c_device; static int i2c_address; static int i2c_status = I2CDEV_UNKNOWN; if (i2c_device == NULL) { i2c_device = config_get_string("global", "i2c-device", "/dev/i2c-0"); i2c_address = config_get_int("global", "i2c-address" , 16); } int dev = i2cdev_open(i2c_device, i2c_address); if (dev < 0) { if (i2c_status != I2CDEV_ERR) log_print(LOG_WARN, "failed to open %s:0x%02x", i2c_device, i2c_address); i2c_status = I2CDEV_ERR; return -1; } if (i2c_status != I2CDEV_OK) log_print(LOG_INFO, "successfully opened %s:0x%02x", i2c_device, i2c_address); 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) { log_print(LOG_INFO, "update UIN_LOSS: %d => %d", 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) { log_print(LOG_INFO, "update UIN_RESTORE: %d => %d", 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) { log_print(LOG_INFO, "update UBAT_FULL: %d => %d", 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) { log_print(LOG_INFO, "update UBAT_LOW: %d => %d", 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) { log_print(LOG_INFO, "update UBAT_CRIT: %d => %d", 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) { log_print(LOG_INFO, "update IBAT_FULL: %d => %d", 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) { case STATE_IDLE: return "IDLE"; case STATE_TEST: return "TEST"; case STATE_CHARGE: return "CHARGE"; case STATE_DISCHARGE: return "DISCHARGE"; case STATE_POWEROFF: return "POWEROFF"; default: return "UNKNOWN"; } } static int str2state(char *buf) { int state = -1; if (strncasecmp(buf, "IDLE", 4) == 0) state = STATE_IDLE; else if (strncasecmp(buf, "TEST", 4) == 0) state = STATE_TEST; else if (strncasecmp(buf, "CHARGE", 6) == 0) state = STATE_CHARGE; else if (strncasecmp(buf, "DISCHARGE", 9) == 0) state = STATE_DISCHARGE; else if (strncasecmp(buf, "POWEROFF", 8) == 0) state = STATE_POWEROFF; return state; } static int alix_state = -1; static void alix_state_change(int old_state, int new_state) { static const char *mail_from; static const char *mail_to; static int mail_data; if (mail_data == 0) { mail_from = config_get_string("alerts", "mail-from", NULL); mail_to = config_get_string("alerts", "mail-to", NULL); mail_data = 1; } log_print(LOG_INFO, "usv state changed: %s => %s", state2str(old_state), state2str(new_state)); if (mail_to == NULL) return; FILE *mail = popen(SENDMAIL, "w"); if (mail == NULL) return; fprintf(mail, "From: %s\n", mail_from); fprintf(mail, "To: %s\n", mail_to); fprintf(mail, "Subject: alix-usvd state change: %s => %s\n\n", state2str(old_state), state2str(new_state)); fprintf(mail, "Faithfully yours, etc.\n"); fclose(mail); return; } static int unix_read_cb(int fd, void *privdata) { char buf[64]; int len = read(fd, buf, sizeof(buf)); if (len < 0) return -1; 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) { alix_state_change(alix_state, new_state); alix_state = new_state; } int len = snprintf(buf, sizeof(buf), "%s", state2str(alix_state)); write(fd, buf, len); } else if (strncasecmp(buf, "status", 6) == 0) { new_state = i2c_smbus_read_word_data(dev, REG_STATUS); int adc_ibat = i2c_smbus_read_word_data(dev, REG_CURRENT); int adc_ubat = i2c_smbus_read_word_data(dev, REG_UBAT); int adc_uin = i2c_smbus_read_word_data(dev, REG_UIN); if (new_state < 0 || adc_ibat < 0 || adc_ubat < 0 || adc_uin < 0) { new_state = -1; adc_ibat = 0; adc_ubat = 0; adc_uin = 0; } 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", state2str(new_state), (int16_t)adc_ibat, adc_ubat, adc_uin); write(fd, buf, len); } close(dev); return -1; } static int unix_accept_cb(int fd, void *privdata) { int con = accept(fd, NULL, NULL); if (con < 0) { log_print(LOG_ERROR, "unix_accept_cb: accept()"); return 0; } event_add_readfd(NULL, con, unix_read_cb, NULL); return 0; } static int status_interval(void *privdata) { int dev = alix_open(); if (dev < 0) return 0; int new_state = i2c_smbus_read_word_data(dev, REG_STATUS); if (new_state == -1) { log_print(LOG_WARN, "failed to get status from i2c-device"); return 0; } if (new_state != alix_state) { alix_state_change(alix_state, new_state); alix_state = new_state; } close(dev); return 0; } int main(int argc, char *argv[]) { char *config = DEFAULT_CONFIG; int debug = 0; int code, arg = 0; do { code = getopt_long(argc, argv, "dc:h", opts, &arg); switch (code) { case 'd': /* debug */ debug = 1; break; case 'c': /* configfile path */ config = optarg; break; case 'h': /* help */ printf( "Usage: alix-usvd [-d] [-c config]\n" "Options: \n" " --debug -d do not fork and log to stderr\n" " --config -c configfile use this configfile\n" " --help -h this help\n" "\n"); exit(0); break; case '?': /* error */ exit(1); break; default: /* unknown / all options parsed */ break; } } while (code != -1); /* parse config file */ if (config_parse(config) < 0) exit(1); if (debug == 0) { /* check pidfile */ const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE); if (pidfile_check(pidfile, 1) != 0) { log_print(LOG_ERROR, "alix-usvd already running"); exit(1); } /* start logging */ const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); if (log_init(logfile) < 0) exit(1); /* zum daemon mutieren */ daemon(-1, 0); /* create pidfile */ if (pidfile_create(pidfile) < 0) { log_print(LOG_ERROR, "failed to create pidfile %s", pidfile); exit(1); } } log_print(LOG_INFO, "alix-usvd started (pid: %d)", getpid()); const char *socket_path = config_get_string("global", "socket", DEFAULT_SOCKET); int sockfd = unix_listen(socket_path); if (sockfd < 0) exit(1); event_add_readfd(NULL, sockfd, unix_accept_cb, NULL); int interval = config_get_int("global", "check-interval", 60); struct timeval tv = { .tv_sec = interval, .tv_usec = 0 }; event_add_timeout(&tv, status_interval, NULL); 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; }