qnapd/pic.c

258 lines
6.0 KiB
C
Raw Normal View History

2012-02-14 20:38:12 +01:00
/***************************************************************************
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include "event.h"
2012-12-08 00:39:04 +01:00
#include "lcd.h"
2012-02-14 20:38:12 +01:00
#include "logging.h"
2012-12-08 00:39:04 +01:00
#include "pic.h"
2012-02-20 20:41:55 +01:00
2012-02-14 20:38:12 +01:00
struct picdev {
int fd;
struct termios oldtio;
2012-12-08 00:39:04 +01:00
int fan_speed;
int fan_error;
int temperature;
struct event_fd *read_event;
};
2012-02-14 20:38:12 +01:00
2012-12-08 00:39:04 +01:00
static int pic_close_device(struct picdev *dev)
2012-02-14 20:38:12 +01:00
{
tcsetattr(dev->fd, TCSANOW, &dev->oldtio);
close(dev->fd);
return 0;
}
2012-12-08 00:39:04 +01:00
static int pic_open_device(struct picdev *dev, const char *device)
2012-02-14 20:38:12 +01:00
{
2012-12-08 00:39:04 +01:00
dev->fd = open(device, O_RDWR | O_NOCTTY);
2012-02-14 20:38:12 +01:00
if (dev->fd < 0) {
log_print(LOG_ERROR, "%s(): failed to open '%s'", __FUNCTION__, device);
return -1;
}
2012-12-08 00:39:04 +01:00
if (fcntl(dev->fd, F_SETFD, FD_CLOEXEC) < 0) {
log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
close(dev->fd);
return -1;
}
2012-02-14 20:38:12 +01:00
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;
}
2012-12-08 00:39:04 +01:00
int pic_set_fanspeed(struct picdev *dev, char value)
{
if (value < PIC_CMD_FANSPEED_0 || value > PIC_CMD_FANSPEED_5)
return -1;
dev->fan_speed = value - PIC_CMD_FANSPEED_0;
write(dev->fd, &value, 1);
return 0;
}
int pic_set_buzzer(struct picdev *dev, char value)
{
if (value < PIC_CMD_BUZZ_SHORT || value > PIC_CMD_BUZZ_LONG)
return -1;
write(dev->fd, &value, 1);
return 0;
}
int pic_set_status_led(struct picdev *dev, char value)
{
if (value < PIC_CMD_STATUSLED_RED_2HZ || value > PIC_CMD_STATUSLED_REDGREEN_1HZ)
return -1;
write(dev->fd, &value, 1);
return 0;
}
int pic_set_usb_led(struct picdev *dev, char value)
{
if (value < PIC_CMD_USBLED_ON || value > PIC_CMD_USBLED_OFF)
return -1;
write(dev->fd, &value, 1);
return 0;
}
2012-02-14 20:38:12 +01:00
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;
2012-02-20 20:41:55 +01:00
switch (event)
{
2012-12-08 00:39:04 +01:00
case PIC_EVENT_FAN1_ERR:
pic_set_buzzer(dev, PIC_CMD_BUZZ_SHORT);
dev->fan_error = 1;
2012-02-20 20:41:55 +01:00
break;
case PIC_EVENT_FAN1_OK:
2012-12-08 00:39:04 +01:00
dev->fan_error = 0;
2012-02-20 20:41:55 +01:00
break;
case PIC_EVENT_FAN2_ERR:
case PIC_EVENT_FAN2_OK:
break;
case PIC_EVENT_TEMP_RANGE_MIN ... PIC_EVENT_TEMP_RANGE_MAX:
2012-12-08 00:39:04 +01:00
dev->temperature = event - 128;
2012-02-20 20:41:55 +01:00
break;
case PIC_EVENT_TEMP_WARN:
2012-12-08 00:39:04 +01:00
pic_set_buzzer(dev, PIC_CMD_BUZZ_SHORT);
dev->temperature = 75;
2012-02-20 20:41:55 +01:00
break;
case PIC_EVENT_TEMP_CRIT:
2012-12-08 00:39:04 +01:00
dev->temperature = 80;
2012-02-20 20:41:55 +01:00
break;
default:
log_print(LOG_DEBUG, "%s(): unknown event 0x%02x", __FUNCTION__, event);
break;
}
2012-02-14 20:38:12 +01:00
return 0;
}
2012-12-08 00:39:04 +01:00
static int lcdpage_case(struct lcddev *lcd, int event, void *privdata)
{
struct picdev *dev = (struct picdev *)privdata;
2012-12-09 01:30:37 +01:00
switch (event) {
case LCDPAGE_EVENT_BUTTON1:
return LCDPAGE_COMMAND_NEXT;
2012-12-08 00:39:04 +01:00
2012-12-09 01:30:37 +01:00
case LCDPAGE_EVENT_BUTTON2:
{
int fan_speed = PIC_CMD_FANSPEED_0 + dev->fan_speed +1;
2012-12-08 00:39:04 +01:00
2012-12-09 01:30:37 +01:00
if (fan_speed > PIC_CMD_FANSPEED_5)
fan_speed = PIC_CMD_FANSPEED_0;
2012-12-08 00:39:04 +01:00
2012-12-09 01:30:37 +01:00
pic_set_fanspeed(dev, fan_speed);
lcd_set_backlight(lcd, 1);
break;
}
2012-12-08 00:39:04 +01:00
2012-12-09 01:30:37 +01:00
case LCDPAGE_EVENT_ENTER:
lcd_set_backlight(lcd, 1);
break;
2012-12-08 00:39:04 +01:00
2012-12-09 01:30:37 +01:00
case LCDPAGE_EVENT_BACKLIGHT:
case LCDPAGE_EVENT_EXIT:
return 0;
default:
break;
2012-12-08 00:39:04 +01:00
}
char line1_ok[] = "FAN(OK): TEMP:";
char line1_err[] = "FAN(ERR): TEMP:";
char line2[20];
snprintf(line2, sizeof(line2), "[ ] %02d C ", dev->temperature);
int i;
for (i = 1; i <= dev->fan_speed; i++) {
line2[i] = '#';
}
lcd_setlines(lcd, (dev->fan_error ? line1_err : line1_ok), line2);
return 1000;
}
struct picdev * pic_open(const char *devicename, struct lcddev *lcd)
2012-02-14 20:38:12 +01:00
{
struct picdev *dev = malloc(sizeof(struct picdev));
if (dev == NULL) {
log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
2012-12-08 00:39:04 +01:00
return NULL;
2012-02-14 20:38:12 +01:00
}
memset(dev, 0, sizeof(struct picdev));
2012-12-08 00:39:04 +01:00
if (pic_open_device(dev, devicename) < 0) {
2012-02-14 20:38:12 +01:00
free(dev);
2012-12-08 00:39:04 +01:00
return NULL;
2012-02-14 20:38:12 +01:00
}
2012-12-08 00:39:04 +01:00
dev->read_event = event_add_readfd(NULL, dev->fd, pic_read_cb, dev);
if (lcd != NULL) {
lcd_addpage_cb(lcd, 190, lcdpage_case, dev);
}
pic_set_fanspeed(dev, PIC_CMD_FANSPEED_1);
pic_set_status_led(dev, PIC_CMD_STATUSLED_GREENON);
return dev;
2012-02-14 20:38:12 +01:00
}
2012-12-08 00:39:04 +01:00
void pic_close(struct picdev *dev)
2012-02-14 20:38:12 +01:00
{
2012-12-08 00:39:04 +01:00
if (dev->read_event != NULL) {
event_remove_fd(dev->read_event);
dev->read_event = NULL;
}
pic_close_device(dev);
free(dev);
2012-02-14 20:38:12 +01:00
}