QNAP-TS419p system daemon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.0 KiB

  1. /***************************************************************************
  2. * Copyright (C) 02/2012 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <sys/socket.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <termios.h>
  29. #include "event.h"
  30. #include "lcd.h"
  31. #include "logging.h"
  32. #include "pic.h"
  33. struct picdev {
  34. int fd;
  35. struct termios oldtio;
  36. int fan_speed;
  37. int fan_error;
  38. int temperature;
  39. struct event_fd *read_event;
  40. };
  41. static int pic_close_device(struct picdev *dev)
  42. {
  43. tcsetattr(dev->fd, TCSANOW, &dev->oldtio);
  44. close(dev->fd);
  45. return 0;
  46. }
  47. static int pic_open_device(struct picdev *dev, const char *device)
  48. {
  49. dev->fd = open(device, O_RDWR | O_NOCTTY);
  50. if (dev->fd < 0) {
  51. log_print(LOG_ERROR, "%s(): failed to open '%s'", __FUNCTION__, device);
  52. return -1;
  53. }
  54. if (fcntl(dev->fd, F_SETFD, FD_CLOEXEC) < 0) {
  55. log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
  56. close(dev->fd);
  57. return -1;
  58. }
  59. tcgetattr(dev->fd, &dev->oldtio);
  60. struct termios newtio;
  61. memset(&newtio, 0, sizeof(newtio));
  62. newtio.c_iflag |= IGNBRK;
  63. newtio.c_lflag &= ~(ISIG | ICANON | ECHO);
  64. newtio.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  65. newtio.c_cc[VMIN] = 1;
  66. newtio.c_cc[VTIME] = 0;
  67. cfsetospeed(&newtio, B19200);
  68. cfsetispeed(&newtio, B19200);
  69. int err = tcsetattr(dev->fd, TCSAFLUSH, &newtio);
  70. if (err < 0) {
  71. log_print(LOG_ERROR, "%s(): failed to set termios", __FUNCTION__);
  72. close(dev->fd);
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. int pic_set_fanspeed(struct picdev *dev, char value)
  78. {
  79. if (value < PIC_CMD_FANSPEED_0 || value > PIC_CMD_FANSPEED_5)
  80. return -1;
  81. dev->fan_speed = value - PIC_CMD_FANSPEED_0;
  82. write(dev->fd, &value, 1);
  83. return 0;
  84. }
  85. int pic_set_buzzer(struct picdev *dev, char value)
  86. {
  87. if (value < PIC_CMD_BUZZ_SHORT || value > PIC_CMD_BUZZ_LONG)
  88. return -1;
  89. write(dev->fd, &value, 1);
  90. return 0;
  91. }
  92. int pic_set_status_led(struct picdev *dev, char value)
  93. {
  94. if (value < PIC_CMD_STATUSLED_RED_2HZ || value > PIC_CMD_STATUSLED_REDGREEN_1HZ)
  95. return -1;
  96. write(dev->fd, &value, 1);
  97. return 0;
  98. }
  99. int pic_set_usb_led(struct picdev *dev, char value)
  100. {
  101. if (value < PIC_CMD_USBLED_ON || value > PIC_CMD_USBLED_OFF)
  102. return -1;
  103. write(dev->fd, &value, 1);
  104. return 0;
  105. }
  106. static int pic_read_cb(int fd, void *privdata)
  107. {
  108. struct picdev *dev = (struct picdev *)privdata;
  109. unsigned char event;
  110. int len = read(dev->fd, &event, 1);
  111. if (len < 0)
  112. return -1;
  113. switch (event)
  114. {
  115. case PIC_EVENT_FAN1_ERR:
  116. pic_set_buzzer(dev, PIC_CMD_BUZZ_SHORT);
  117. dev->fan_error = 1;
  118. break;
  119. case PIC_EVENT_FAN1_OK:
  120. dev->fan_error = 0;
  121. break;
  122. case PIC_EVENT_FAN2_ERR:
  123. case PIC_EVENT_FAN2_OK:
  124. break;
  125. case PIC_EVENT_TEMP_RANGE_MIN ... PIC_EVENT_TEMP_RANGE_MAX:
  126. dev->temperature = event - 128;
  127. break;
  128. case PIC_EVENT_TEMP_WARN:
  129. pic_set_buzzer(dev, PIC_CMD_BUZZ_SHORT);
  130. dev->temperature = 75;
  131. break;
  132. case PIC_EVENT_TEMP_CRIT:
  133. dev->temperature = 80;
  134. break;
  135. default:
  136. log_print(LOG_DEBUG, "%s(): unknown event 0x%02x", __FUNCTION__, event);
  137. break;
  138. }
  139. return 0;
  140. }
  141. static int lcdpage_case(struct lcddev *lcd, int event, void *privdata)
  142. {
  143. struct picdev *dev = (struct picdev *)privdata;
  144. switch (event) {
  145. case LCDPAGE_EVENT_BUTTON1:
  146. return LCDPAGE_COMMAND_NEXT;
  147. case LCDPAGE_EVENT_BUTTON2:
  148. {
  149. int fan_speed = PIC_CMD_FANSPEED_0 + dev->fan_speed +1;
  150. if (fan_speed > PIC_CMD_FANSPEED_5)
  151. fan_speed = PIC_CMD_FANSPEED_0;
  152. pic_set_fanspeed(dev, fan_speed);
  153. lcd_set_backlight(lcd, 1);
  154. break;
  155. }
  156. case LCDPAGE_EVENT_ENTER:
  157. lcd_set_backlight(lcd, 1);
  158. break;
  159. case LCDPAGE_EVENT_BACKLIGHT:
  160. case LCDPAGE_EVENT_EXIT:
  161. return 0;
  162. default:
  163. break;
  164. }
  165. char line1_ok[] = "FAN(OK): TEMP:";
  166. char line1_err[] = "FAN(ERR): TEMP:";
  167. char line2[20];
  168. snprintf(line2, sizeof(line2), "[ ] %02d C ", dev->temperature);
  169. int i;
  170. for (i = 1; i <= dev->fan_speed; i++) {
  171. line2[i] = '#';
  172. }
  173. lcd_setlines(lcd, (dev->fan_error ? line1_err : line1_ok), line2);
  174. return 1000;
  175. }
  176. struct picdev * pic_open(const char *devicename, struct lcddev *lcd)
  177. {
  178. struct picdev *dev = malloc(sizeof(struct picdev));
  179. if (dev == NULL) {
  180. log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__);
  181. return NULL;
  182. }
  183. memset(dev, 0, sizeof(struct picdev));
  184. if (pic_open_device(dev, devicename) < 0) {
  185. free(dev);
  186. return NULL;
  187. }
  188. dev->read_event = event_add_readfd(NULL, dev->fd, pic_read_cb, dev);
  189. if (lcd != NULL) {
  190. lcd_addpage_cb(lcd, 190, lcdpage_case, dev);
  191. }
  192. pic_set_fanspeed(dev, PIC_CMD_FANSPEED_1);
  193. pic_set_status_led(dev, PIC_CMD_STATUSLED_GREENON);
  194. return dev;
  195. }
  196. void pic_close(struct picdev *dev)
  197. {
  198. if (dev->read_event != NULL) {
  199. event_remove_fd(dev->read_event);
  200. dev->read_event = NULL;
  201. }
  202. pic_close_device(dev);
  203. free(dev);
  204. }