1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*
* Copyright (C) 2007-2008 Byron Bradley (byron.bbradley@gmail.com)
* Copyright (C) 2008, 2009 Martin Michlmayr (tbm@cyrius.com)
* Copyright (C) 2013 Michael Stapelberg (michael+qnap@stapelberg.de)
* Copyright (C) 2013 Ian Campbell (ijc@hellion.org.uk)
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <termios.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/select.h>
#include "picmodule.h"
#include "qnap-pic.h"
static int serial;
static struct termios oldtio, newtio;
static pthread_t qnap_thread;
int qnap_serial_read(unsigned char *buf, int len)
{
int err;
static int error_count = 0;
err = read(serial, buf, len);
if (err != -1) {
buf[err] = 0;
error_count = 0;
} else if (errno == EAGAIN) {
error_count++;
if (error_count == 5)
print_log(LOG_WARNING,
"Contradicting information about data available to be read from /dev/ttyS1.\n"
"Please make sure nothing else is reading things there.");
}
return err;
}
int qnap_serial_write(unsigned char *buf, int len)
{
int err;
err = write(serial, buf, len);
return err;
}
static void *serial_poll(void *data)
{
qnap_serial_cb cb = data;
int err;
fd_set rset;
FD_ZERO(&rset);
FD_SET(serial, &rset);
for (;;) {
err = select(serial + 1, &rset, NULL, NULL, NULL);
if (err <= 0) {
FD_SET(serial, &rset);
continue;
}
cb();
FD_SET(serial, &rset);
}
return NULL;
}
int qnap_serial_poll(qnap_serial_cb cb)
{
return pthread_create(&qnap_thread, NULL, serial_poll, cb);
}
static int set_nonblock(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0)
flags = 0;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int qnap_serial_open(char *device)
{
int err;
if ((serial = open(device , O_RDWR)) < 0) {
print_log(LOG_ERR, "Failed to open %s: %s", device,
strerror(errno));
return -1;
}
err = set_nonblock(serial);
if (err < 0) {
print_log(LOG_ERR, "Error setting nonblock: %s",
strerror(errno));
return -1;
}
tcgetattr(serial, &oldtio);
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);
err = tcsetattr(serial, TCSANOW, &newtio);
if (err < 0) {
print_log(LOG_ERR, "Failed to set attributes for %s: %s",
device, strerror(errno));
return -1;
}
return 0;
}
void qnap_serial_close(void)
{
tcsetattr(serial, TCSANOW, &oldtio);
close(serial);
}
static int qnap_cmd_powerled(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "on") == 0)
code = QNAP_PICCMD_POWER_LED_ON;
else if (strcmp(argv[0], "1hz") == 0)
code = QNAP_PICCMD_POWER_LED_1HZ;
else if (strcmp(argv[0], "2hz") == 0)
code = QNAP_PICCMD_POWER_LED_2HZ;
else if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_POWER_LED_OFF;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_statusled(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "red2hz") == 0)
code = QNAP_PICCMD_STATUS_RED_2HZ;
else if (strcmp(argv[0], "green2hz") == 0)
code = QNAP_PICCMD_STATUS_GREEN_2HZ;
else if (strcmp(argv[0], "greenon") == 0)
code = QNAP_PICCMD_STATUS_GREEN_ON;
else if (strcmp(argv[0], "redon") == 0)
code = QNAP_PICCMD_STATUS_RED_ON;
else if (strcmp(argv[0], "greenred2hz") == 0)
code = QNAP_PICCMD_STATUS_BOTH_2HZ;
else if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_STATUS_OFF;
else if (strcmp(argv[0], "green1hz") == 0)
code = QNAP_PICCMD_STATUS_GREEN_1HZ;
else if (strcmp(argv[0], "red1hz") == 0)
code = QNAP_PICCMD_STATUS_RED_1HZ;
else if (strcmp(argv[0], "greenred1hz") == 0)
code = QNAP_PICCMD_STATUS_BOTH_1HZ;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_usbled(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "on") == 0)
code = QNAP_PICCMD_USB_LED_ON;
else if (strcmp(argv[0], "8hz") == 0)
code = QNAP_PICCMD_USB_LED_8HZ;
else if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_USB_LED_OFF;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_autopower(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "on") == 0)
code = QNAP_PICCMD_AUTOPOWER_ON;
else if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_AUTOPOWER_OFF;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_buzzer(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "short") == 0)
code = QNAP_PICCMD_BUZZER_SHORT;
else if (strcmp(argv[0], "long") == 0)
code = QNAP_PICCMD_BUZZER_LONG;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_fanspeed(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "stop") == 0)
code = QNAP_PICCMD_FAN_STOP;
else if (strcmp(argv[0], "silence") == 0)
code = QNAP_PICCMD_FAN_SILENCE;
else if (strcmp(argv[0], "low") == 0)
code = QNAP_PICCMD_FAN_LOW;
else if (strcmp(argv[0], "medium") == 0)
code = QNAP_PICCMD_FAN_MEDIUM;
else if (strcmp(argv[0], "high") == 0)
code = QNAP_PICCMD_FAN_HIGH;
else if (strcmp(argv[0], "full") == 0)
code = QNAP_PICCMD_FAN_FULL;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_watchdog(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_WDT_OFF;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_eup(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "on") == 0)
code = QNAP_PICCMD_EUP_ENABLE;
else if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_EUP_DISABLE;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_rtc(int argc, const char **argv)
{
unsigned char code = 0;
if (argc != 1)
return -1;
if (strcmp(argv[0], "on") == 0)
code = QNAP_PICCMD_RTC_ENABLE;
else if (strcmp(argv[0], "off") == 0)
code = QNAP_PICCMD_RTC_DISABLE;
else
return -1;
return qnap_serial_write(&code, 1);
}
static int qnap_cmd_wol(int argc, const char **argv)
{
unsigned char code[2];
int len;
if (argc != 1)
return -1;
if (strcmp(argv[0], "on") == 0) {
/* EUP turns the device in such a deep power-saving
* mode that WOL does not work. Therefore, in order
* to have WOL turned on, we also disable EUP. */
code[0] = QNAP_PICCMD_WOL_ENABLE;
code[1] = QNAP_PICCMD_EUP_DISABLE;
len = 2;
} else if (strcmp(argv[0], "off") == 0) {
code[0] = QNAP_PICCMD_WOL_DISABLE;
len = 1;
} else
return -1;
return qnap_serial_write(code, len);
}
int qnap_register_commands(unsigned long features)
{
#define REGISTER1(feat,name,shorthelp,longhelp) do { \
if (features & QNAP_PIC_FEATURE_##feat) { \
int err = register_command(#name, shorthelp, longhelp, qnap_cmd_##name); \
if (err < 0) return err; \
} \
} while(0)
REGISTER1(POWERLED, powerled, "Change the power LED",
"Change the power LED, options are:\n"
"\ton\n\toff\n\t1hz\n\t2hz\n");
REGISTER1(STATUSLED, statusled, "Change the status LED",
"Change the status LED, options are:\n"
"\tred2hz\n\tgreen2hz\n\tgreenon\n\tredon\n"
"\tgreenred2hz\n\toff\n\tgreen1hz\n\tred1hz\n");
REGISTER1(USBLED, usbled, "Set the usbled",
"Set the usbled, options are:\n"
"\ton\n\t8hz\n\toff\n");
REGISTER1(AUTOPOWER, autopower, "Control the automatic power mechanism",
"Control the automatic power mechanism, options are:\n"
"\ton\n\toff\n");
REGISTER1(BUZZER, buzzer, "Buzz",
"Buzz, options are:\n"
"\tshort\n\tlong\n");
REGISTER1(FANSPEED, fanspeed, "Set the fanspeed",
"Set the fanspeed, options are:\n"
"\tstop\n\tsilence\n\tlow\n\tmedium\n"
"\thigh\n\tfull\n");
REGISTER1(WATCHDOG, watchdog, "Disable the PIC watchdog",
"Watchdog options are:\n"
"\toff");
REGISTER1(WOL, wol, "Control Wake on LAN",
"Control Wake on LAN, options are:\n"
"\ton\n\toff");
REGISTER1(EUP, eup, "Control EUP (Energy-using Products) power saving",
"Control EUP, options are:\n"
"\ton\n\toff");
REGISTER1(RTC, rtc, "Control RTC (real time clock)",
"Control RTC, options are:\n"
"\ton\n\toff");
#undef REGISTER1
return 0;
}
|