/*************************************************************************** * 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 "unixsocket.h" #define DEFAULT_CONFIG "alix-usvd.conf" #define DEFAULT_LOGFILE "alix-usvd.log" #define DEFAULT_SOCKET "alix-usvd.sock" 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, }; 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 i2cdev_open_wrap(void) { static const char *i2c_device; static int i2c_address; static int i2c_last_opened; 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_last_opened != -1) log_print(LOG_WARN, "failed to open %s:0x%02x", i2c_device, i2c_address); i2c_last_opened = -1; return -1; } if (i2c_last_opened < 1) log_print(LOG_INFO, "successfully opened %s:0x%02x", i2c_device, i2c_address); i2c_last_opened = 1; return dev; } 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 usv_state = -1; static void check_state(int old_state, int new_state) { log_print(LOG_INFO, "usv state changed: %s(%d) => %s(%d)", state2str(old_state), old_state, state2str(new_state), new_state); } 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 = i2cdev_open_wrap(); 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)); } int len = snprintf(buf, sizeof(buf), "%s", state2str(usv_state)); write(fd, buf, len); } else if (strncmp(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 != usv_state) { check_state(usv_state, new_state); usv_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 = i2cdev_open_wrap(); 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 != usv_state) { check_state(usv_state, new_state); usv_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) { /* 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); /* TODO: write PID-file */ } log_print(LOG_INFO, "alix-usvd started (pid: %d)", getpid()); const char *socket_path = config_get_string("global", "socket", DEFAULT_SOCKET); unlink(socket_path); 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 dev = i2cdev_open_wrap(); if (dev != -1) { /* TODO: check thresholds */ close(dev); } event_loop(); return 0; }