From 8c1a84a011cd1a19c7b2ce419fc34e13cefdc60a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 14 Feb 2012 20:38:12 +0100 Subject: [PATCH] initial pic work --- disktimeout.c | 8 +-- pic.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++ qnapd.c | 7 +++ qnapd.conf | 10 ++-- 4 files changed, 161 insertions(+), 9 deletions(-) create mode 100644 pic.c diff --git a/disktimeout.c b/disktimeout.c index 359ad7e..a64930a 100644 --- a/disktimeout.c +++ b/disktimeout.c @@ -69,7 +69,7 @@ static int diskto_timeout_cb(void *privdata) return -1; } - log_print(LOG_INFO, "disktimeout: %s standby after %ds", entry->device, entry->timeout); +// log_print(LOG_INFO, "disktimeout: %s standby after %ds", entry->device, entry->timeout); unsigned char args[4] = { ATA_OP_STANDBYNOW1, 0, 0, 0}; if (do_drive_cmd(fd, args)) { @@ -228,7 +228,7 @@ static int diskto_check_cb(void *privdata) if (args[2] != 0x00) { if (entry->flags & F_STANDBY) { - log_print(LOG_INFO, "disktimeout: %s is awake, starting timer", entry->device, entry->timeout); +// log_print(LOG_INFO, "disktimeout: %s is awake, starting timer", entry->device, entry->timeout); /* device is not in standby mode, start timer */ if (entry->timeout_event == NULL) { @@ -240,7 +240,7 @@ static int diskto_check_cb(void *privdata) } else { if (!(entry->flags & F_STANDBY)) { - log_print(LOG_INFO, "disktimeout: %s standby, stopping timer", entry->device, entry->timeout); +// log_print(LOG_INFO, "disktimeout: %s standby, stopping timer", entry->device, entry->timeout); /* device is in standby, stop timer */ if (entry->timeout_event != NULL) @@ -279,7 +279,7 @@ int disktimeout_init(void) { int diskcount = config_get_strtokens("disktimeout", "disk", ",", 2, diskto_add_disk, NULL); if (diskcount <= 0) - return -1; + return diskcount; int check_interval = 0; config_get_int("disktimeout", "check_interval", &check_interval, 60); diff --git a/pic.c b/pic.c new file mode 100644 index 0000000..07e1a11 --- /dev/null +++ b/pic.c @@ -0,0 +1,145 @@ +/*************************************************************************** + * Copyright (C) 02/2012 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; either version 2 of the License, or * + * (at your option) any later version. * + * * + * 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 "event.h" +#include "logging.h" + +#define PIC_CMD_FANSPEED_0 0x30 +#define PIC_CMD_FANSPEED_1 0x31 +#define PIC_CMD_FANSPEED_2 0x32 +#define PIC_CMD_FANSPEED_3 0x33 +#define PIC_CMD_FANSPEED_4 0x34 +#define PIC_CMD_FANSPEED_5 0x35 + +#define PIC_CMD_AUTOPOWER_ON 0x48 +#define PIC_CMD_AUTOPOWER_OFF 0x49 + +#define PIC_CMD_POWERLED_OFF 0x4B +#define PIC_CMD_POWERLED_2HZ 0x4C +#define PIC_CMD_POWERLED_ON 0x4D +#define PIC_CMD_POWERLED_1HZ 0x4E + +#define PIC_CMD_BUZZ_SHORT 0x50 +#define PIC_CMD_BUZZ_LONG 0x51 + +#define PIC_CMD_STATUSLED_RED_2HZ 0x54 +#define PIC_CMD_STATUSLED_GREEN_2HZ 0x55 +#define PIC_CMD_STATUSLED_GREENON 0x56 +#define PIC_CMD_STATUSLED_REDON 0x57 +#define PIC_CMD_STATUSLED_REDGREEN_2HZ 0x58 +#define PIC_CMD_STATUSLED_OFF 0x59 +#define PIC_CMD_STATUSLED_GREEN_1HZ 0x5A +#define PIC_CMD_STATUSLED_RED_1HZ 0x5B +#define PIC_CMD_STATUSLED_REDGREEN_1HZ 0x5C + +#define PIC_CMD_USBLED_ON 0x60 +#define PIC_CMD_USBLED_8HZ 0x61 +#define PIC_CMD_USBLED_OFF 0x62 + +struct picdev { + int fd; + struct termios oldtio; +}; + +static struct picdev *g_dev; + +static int pic_close(struct picdev *dev) +{ + tcsetattr(dev->fd, TCSANOW, &dev->oldtio); + close(dev->fd); + return 0; +} + +static int pic_open(struct picdev *dev, const char *device) +{ + dev->fd = open(device, O_RDWR); + if (dev->fd < 0) { + log_print(LOG_ERROR, "%s(): failed to open '%s'", __FUNCTION__, device); + return -1; + } + + tcgetattr(dev->fd, &dev->oldtio); + + struct termios newtio; + memset(&newtio, 0, sizeof(newtio)); + newtio.c_iflag |= IGNBRK; + newtio.c_lflag &= ~(ISIG | ICANON | ECHO); + newtio.c_cflag = B19200 | CS8 | CLOCAL | CREAD; + newtio.c_cc[VMIN] = 1; + newtio.c_cc[VTIME] = 0; + cfsetospeed(&newtio, B19200); + cfsetispeed(&newtio, B19200); + + int err = tcsetattr(dev->fd, TCSAFLUSH, &newtio); + if (err < 0) { + log_print(LOG_ERROR, "%s(): failed to set termios", __FUNCTION__); + close(dev->fd); + return -1; + } + + return 0; +} + +static int pic_read_cb(int fd, void *privdata) +{ + struct picdev *dev = (struct picdev *)privdata; + + unsigned char event; + int len = read(dev->fd, &event, 1); + if (len < 0) + return -1; + + log_print(LOG_DEBUG, "%s(): 0x%02x", __FUNCTION__, event); + return 0; +} + +void pic_init(void) +{ + struct picdev *dev = malloc(sizeof(struct picdev)); + if (dev == NULL) { + log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__); + return; + } + + memset(dev, 0, sizeof(struct picdev)); + + if (pic_open(dev, "/dev/ttyS1")< 0) { + free(dev); + return; + } + + event_add_readfd(NULL, dev->fd, pic_read_cb, dev); + g_dev = dev; // HACK +} + +void pic_exit(void) +{ + pic_close(g_dev); +} diff --git a/qnapd.c b/qnapd.c index 1dd3fb3..206347a 100644 --- a/qnapd.c +++ b/qnapd.c @@ -38,6 +38,9 @@ #include "pidfile.h" #include "signals.h" +void pic_init(void); +void pic_exit(void); + #define LCD_DEVICE "/dev/ttyS0" #define LCD_TIMEOUT 15 @@ -243,6 +246,8 @@ int main(int argc, char *argv[]) break; } + pic_init(); + if (disktimeout_init() < 0) break; @@ -251,6 +256,8 @@ int main(int argc, char *argv[]) disktimeout_exit(); + pic_exit(); + if (dev != NULL) lcd_close(dev); diff --git a/qnapd.conf b/qnapd.conf index 32a9c6a..a8c8b35 100644 --- a/qnapd.conf +++ b/qnapd.conf @@ -1,7 +1,7 @@ [global] #lcddevice /dev/ttyS0 -lcddevice dummy +#lcddevice dummy pidfile qnapd.pid logfile qnapd.log @@ -9,7 +9,7 @@ logfile qnapd.log [disktimeout] check_interval 60 -disk /dev/sda,7200 -disk /dev/sdb,7200 -disk /dev/sdc,7200 -disk /dev/sdd,7200 +#disk /dev/sda,7200 +#disk /dev/sdb,7200 +#disk /dev/sdc,7200 +#disk /dev/sdd,7200