qnapd/pic.c
2012-02-20 20:41:55 +01:00

197 lines
5.4 KiB
C

/***************************************************************************
* 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"
#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_POWER_OFF 0x41
#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
#define PIC_EVENT_POWER_BUTTON 0x40
#define PIC_EVENT_FAN1_ERR 0x73
#define PIC_EVENT_FAN1_OK 0x74
#define PIC_EVENT_FAN2_ERR 0x75
#define PIC_EVENT_FAN2_OK 0x76
#define PIC_EVENT_FAN3_ERR 0x77
#define PIC_EVENT_FAN3_OK 0x78
#define PIC_EVENT_FAN4_ERR 0x79
#define PIC_EVENT_FAN4_OK 0x7A
#define PIC_EVENT_TEMP_RANGE_MIN 0x80 /* 0°C */
#define PIC_EVENT_TEMP_RANGE_MAX 0xC6 /* 70°C */
#define PIC_EVENT_TEMP_WARN 0x38 /* 71-79°C */
#define PIC_EVENT_TEMP_CRIT 0x39 /* >= 80°C */
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;
switch (event)
{
case PIC_EVENT_POWER_BUTTON:
log_print(LOG_DEBUG, "%s(): POWER BUTTON", __FUNCTION__);
break;
case PIC_EVENT_FAN1_ERR:
case PIC_EVENT_FAN1_OK:
log_print(LOG_DEBUG, "%s(): FAN1 %s", __FUNCTION__, (event & 0x01) ? "fail" : "ok");
break;
case PIC_EVENT_FAN2_ERR:
case PIC_EVENT_FAN2_OK:
log_print(LOG_DEBUG, "%s(): FAN2 %s", __FUNCTION__, (event & 0x01) ? "fail" : "ok");
break;
case PIC_EVENT_TEMP_RANGE_MIN ... PIC_EVENT_TEMP_RANGE_MAX:
log_print(LOG_DEBUG, "%s(): TEMP %d°C", __FUNCTION__, event - 128);
break;
case PIC_EVENT_TEMP_WARN:
log_print(LOG_DEBUG, "%s(): TEMP WARNING", __FUNCTION__);
break;
case PIC_EVENT_TEMP_CRIT:
log_print(LOG_DEBUG, "%s(): TEMP CRITICAL", __FUNCTION__);
break;
default:
log_print(LOG_DEBUG, "%s(): unknown event 0x%02x", __FUNCTION__, event);
break;
}
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);
}