forked from cschil/mcm-deamon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcm-daemon.c
1664 lines (1479 loc) · 38.8 KB
/
mcm-daemon.c
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
vim: ts=4 ai fdm=marker
Simple system daemon for MyCloudMirror gen2; Ex2 Ultra
(c) 2013 Andreas Boehler, andreas _AT_ aboehler.at
(c) 2017 Martin Mueller, mm _AT_ sig21.net
mod by C. Schiller, schreibcarl@gmail.com
This code is based on a few other people's work and in parts shamelessly copied.
The ThermalTable was provided by Lorenzo Martignoni and the fan control
algorithm is based on his fan-daemon.py implementation.
The MCU protocol was reverse engineered by strace() calls to up_send_daemon and
up_read_daemon of the original firmware.
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 3 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/>.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <signal.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <string.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/uio.h>
#include <syslog.h>
#include <ctype.h>
#include <iniparser.h>
#include "mcm.h"
#include "mcm-daemon.h"
int ls;
int fd;
int ataPorts;
int fanRpm;
int fanSpeed;
int fanMode;
tempState tsys;
tempState *tdisk;
DaemonConfig daemonCfg;
/** @file mcm-daemon.c
@brief Implementation of a free system daemon replacement for
the D-Link DNS-320L NAS
@author Andreas Boehler, andreas _AT_ aboehler.at
@version 1.0
@date 2013/09/12
*/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd;
//int len;
char buf[100];
char ch;
//len = snprintf(buf, sizeof(buf), "%s/gpio%d/value", daemonCfg.gpioDir, gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
syslog(LOG_ERR, "gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
void cleanup(int shut,int s,int howmany)
{
int retval;
/*
* Shutdown and close sock1 completely.
*/
if (shut)
{
retval = shutdown(s,howmany);
if (retval == -1)
syslog(LOG_ERR, "shutdown");
}
retval = close (s);
if (retval)
syslog(LOG_ERR, "close");
}
static void sighandler(int sig)
{
syslog(LOG_DEBUG, "Signal Handler called\n");
switch(sig)
{
case SIGINT:
cleanup(0, ls, 1);
exit(EXIT_SUCCESS);
break;
case SIGTERM:
cleanup(0, ls, 1);
if(daemonCfg.syncOnShutdown)
HandleCommand("systohc", 7, NULL, 0);
exit(EXIT_SUCCESS);
break;
}
}
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
syslog(LOG_ERR, "error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // ignore break signal
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
syslog(LOG_ERR, "error %d from tcsetattr", errno);
return -1;
}
return 0;
}
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
syslog(LOG_ERR, "error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
syslog(LOG_ERR, "error %d setting term attributes", errno);
}
int CheckResponse(char *buf, char *cmd, int len)
{
int i;
//int tmp;
int failure = 0;
// Attention, 5 is hardcoded here and never checked!
for(i=0;i<5;i++)
{
if(buf[i] != cmd[i])
{
syslog(LOG_ERR, "Char %i is %i but should be %i\n", i, buf[i], cmd[i]);
failure = 1;
break;
}
}
if(failure)
{
for(i=0;i<len;i++)
{
syslog(LOG_DEBUG, "Buf/Cmd %i: %i %i\n", i, buf[i], cmd[i]);
}
return ERR_WRONG_ANSWER;
}
/* if(buf[len-1] != cmd[len-1])
{
syslog(LOG_ERR, "Last character does not match! Char %i is %i but should be %i\n", len-1, buf[len-1], cmd[len-1]);
return ERR_WRONG_ANSWER;
}
*/
return SUCCESS;
}
void ClearSerialPort(int fd)
{
char buf[100];
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN;
int n = 0;
int pollrc;
pollrc = poll(fds, 1, 0);
if(pollrc > 0)
{
if(fds[0].revents & POLLIN)
{
syslog(LOG_DEBUG, "Clearing Serial Port...\n");
do
{
n = read(fd, buf, sizeof(buf));
} while(n == sizeof(buf));
}
}
}
int SendCommand(int fd, char *cmd, char *outArray)
{
int nRetries = -1;
int ret;
do
{
ret = _SendCommand(fd, cmd, outArray);
nRetries++;
syslog(LOG_DEBUG, "Try number: %i\n", nRetries+1);
} while((ret != SUCCESS) && (nRetries < daemonCfg.nRetries));
return ret;
}
int _SendCommand(int fd, char *cmd, char *outArray)
{
int n;
int i;
int j;
ssize_t count;
char buf[15]; // We need to keep the DateAndTime values here
// Yes, we're sending byte by byte here - b/c the length of
// commands and responses can vary!
ClearSerialPort(fd); // We clear the serial port in case
// some old data from a previous request is still pending
i=0;
do
{
count = write(fd, &cmd[i], 1);
i++;
usleep(200); // The MCU seems to need some time..
if(count != 1)
{
syslog(LOG_ERR, "Error writing byte %i: %i, count: %ld\n", (i-1), cmd[i-1], (long int)count);
return ERR_WRITE_ERROR;
}
} while(cmd[i-1] != CMD_STOP_MAGIC);
i=0;
do
{
n = read(fd, &buf[i], 1);
i++;
usleep(200); // todo, replace with select and timeout
} while((n == 1) && (buf[i-1] != CMD_STOP_MAGIC));
if(buf[i-1] != CMD_STOP_MAGIC)
{
syslog(LOG_ERR, "Got no stop magic, but read %i bytes!\n", i);
for(j=0;j<i;j++)
{
syslog(LOG_DEBUG, "Buf %i: %i\n", j, buf[j]);
}
return ERR_WRONG_ANSWER;
}
else
{
// If outArray is not NULL, an answer was requested
if(outArray != NULL)
{
if(CheckResponse(buf, cmd, i) != SUCCESS)
{
return ERR_WRONG_ANSWER;
}
// Copy the answer to the outArray
for(j=0; j<i; j++)
{
outArray[j] = buf[j];
}
usleep(50000); // Give the µC some time to answer...
// Wait for ACK from Serial
i=0;
do
{
n = read(fd, &buf[i], 1);
i++;
} while((n == 1) && (buf[i-1] != CMD_STOP_MAGIC));
if(buf[i-1] != CMD_STOP_MAGIC)
{
syslog(LOG_ERR, "Got no stop magic!\n");
for(j=0;j<i;j++)
{
syslog(LOG_DEBUG, "Buf %i: %i\n", j, buf[j]);
}
return ERR_WRONG_ANSWER;
}
CheckResponse(buf, AckFromSerial, i);
syslog(LOG_DEBUG, "Returning %i read bytes\n", n);
return SUCCESS;
}
// Only wait for ACK if no response is expected
else
{
return CheckResponse(buf, AckFromSerial, i);
}
}
}
static char* searchDisk(char *base, char *pref)
{
DIR *dir;
struct dirent *entry;
char next[PATH_MAX];
char *tmp;
tmp = NULL;
errno = 0;
dir = opendir(base);
if ( errno ) {
syslog(LOG_ERR, "error(%d) opening directory: %s\n",errno, base);
return NULL;
}
while ( (entry = readdir(dir)) != 0 ) {
if ( strcmp("..",entry->d_name) == 0 || strcmp(".",entry->d_name) == 0 )
continue;
if ( strcmp(pref, "host") == 0 ) {
if ( strncmp(pref,entry->d_name, strlen(pref)) == 0 ) {
snprintf(next, PATH_MAX, "%s/%s", base, entry->d_name);
tmp = searchDisk(next, "target");
} else
continue;
} else if ( strcmp(pref, "target") == 0 ) {
if ( strncmp(pref,entry->d_name, strlen(pref)) == 0 ) {
snprintf(next, PATH_MAX, "%s/%s", base, entry->d_name);
tmp = searchDisk(next, "");
} else
continue;
} else if ( strlen(pref) == 0 ) {
if ( strspn(entry->d_name, "0123456789:") == strlen(entry->d_name) ) {
snprintf(next, PATH_MAX, "%s/%s", base, entry->d_name);
tmp = searchDisk(next, "block");
} else
continue;
} else if ( strcmp(pref, "block") == 0 ) {
if ( strncmp(pref,entry->d_name, strlen(pref)) == 0 ) {
snprintf(next, PATH_MAX, "%s/%s", base, entry->d_name);
tmp = searchDisk(next, "sd");
} else
continue;
} else if ( strcmp(pref, "sd") == 0 ) {
if ( strncmp("sd", entry->d_name, 2) == 0 ) {
asprintf(&tmp, "%s/%s", "/dev", entry->d_name);
break;
} else
continue;
}
}
closedir(dir);
return tmp;
}
static int numAtaPorts(void) {
int num;
DIR *dir;
struct dirent *entry;
num = 0;
errno = 0;
dir = opendir(daemonCfg.ataPorts);
if ( errno )
syslog(LOG_ERR, "error %d enumerating ata ports\n",errno);
else {
while ( (entry = readdir(dir)) != 0 ) {
if ( strncmp("ata",entry->d_name, 3) == 0 )
num++;
}
}
closedir(dir);
syslog(LOG_DEBUG, "found %d ata ports\n",num);
return num;
}
static char *getDisk(int disk) {
int i;
DIR *dir;
struct dirent *entry;
char *path, *tmp;
tmp = NULL;
if ( disk > ataPorts ) {
//printf("disk %d > number of ports\n", disk);
return NULL;
}
errno = 0;
dir = opendir(daemonCfg.ataPorts);
if ( errno )
syslog(LOG_ERR, "error %d enumerating ata ports\n",errno);
else {
i = 0;
while ( (entry = readdir(dir)) != 0 ) {
if ( strncmp("ata",entry->d_name, 3) == 0 ) {
i++;
if ( i == disk ) {
asprintf(&path, "%s/%s/device",daemonCfg.ataPorts, entry->d_name);
tmp = searchDisk(path, "host");
if (path)
free(path);
}
}
}
}
closedir(dir);
return tmp;
}
static int parseTemp(char *buf, int field) {
char *endptr = NULL;
int i=0, temp = 0;
char *tok;
tok = strtok(buf, "\t\r\n ");
i++;
while (i < field) {
tok = strtok(NULL, "\t\r\n ");
i++;
}
errno = 0;
if (tok != NULL)
temp = (int) strtol(tok, &endptr, 0);
if ( errno == 0 && tok != endptr )
return temp;
return -1;
}
static int readHddTemp(int disk) {
int fd[2];
int status;
pid_t pid;
char buf[2048], *dev;
int temp;
FILE *input;
syslog(LOG_DEBUG, "query disk %d temperature\n", disk);
dev = getDisk(disk);
if ( dev == NULL )
return 0;
if ( pipe(fd) == -1 )
return -1;
pid = fork();
if (pid < 0) {
close(fd[0]);
close(fd[1]);
printf("fork() failed\n");
return -1;
}
if ( pid == 0 ) {
/* child to start smartctl */
dup2(fd[1], 1);
close(fd[0]);
execlp("smartctl", "smartctl", "-n", "standby", "-l", "scttempsts", dev, NULL);
}
close(fd[1]);
input = fdopen(fd[0], "r");
temp = 0;
while ( fgets(buf, 1024, input ) ) {
if ( strncmp("Current Temperature:", buf, 20) == 0 ) {
temp = parseTemp(buf, 3);
}
}
wait(&status);
fclose(input);
free(dev);
return temp;
}
static int readFan()
{
int rpm;
char buf[10];
if(SendCommand(fd, FanSpeedGetCmd, buf) > ERR_WRONG_ANSWER)
{
if ( buf[5] > 0 )
rpm = (int) (300000 / buf[5]);
else
rpm = 0;
syslog(LOG_DEBUG, "Read fan rpm: %i\n", rpm);
return rpm;
}
syslog(LOG_ERR, "Error reading fan speed\n");
return ERR_WRONG_ANSWER;
}
static int readSysTemp()
{
char buf[15];
int temp;
if(SendCommand(fd, ThermalStatusGetCmd, buf) > ERR_WRONG_ANSWER)
{
temp = ThermalTable[(int)buf[5]];
syslog(LOG_DEBUG, "Read system tempterature: %i °C\n", temp);
return temp;
}
syslog(LOG_ERR, "Error reading system temperature\n");
return ERR_WRONG_ANSWER;
}
static int setFanSpeed(int val)
{
char buf[10];
int i;
syslog(LOG_DEBUG, "setFanSpeed %d\n", val);
for (i=0;i<=6;i++) {
buf[i] = FanSpeedSetCmd[i];
}
buf[3] = (char)(val%256);
buf[7] = 0;
if(SendCommand(fd, buf, NULL) == SUCCESS)
return 0;
syslog(LOG_ERR, "Error setting fan speed\n");
return 1;
}
static int DeviceReady(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, DeviceReadyCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int GetFanRpm(char *retMessage, int bufSize, char *cmd)
{
int tmp, len;
tmp = readFan();
if ( tmp <= ERR_WRONG_ANSWER )
{
snprintf(retMessage, bufSize, "ERR %d\n", tmp);
return 1;
}
snprintf(retMessage, bufSize, "%d %d", fanSpeed, tmp);
len = strlen(retMessage);
if(bufSize > 1)
{
retMessage[len] = '\n';
retMessage[len+1] = '\0';
}
return 0;
}
static int GetSysTemp(char *retMessage, int bufSize, char *cmd)
{
int tmp, len;
tmp = readSysTemp();
if ( tmp <= ERR_WRONG_ANSWER )
{
snprintf(retMessage, bufSize, "ERR %d\n", tmp);
return 1;
}
snprintf(retMessage, bufSize, "%d", tmp);
len = strlen(retMessage);
if(bufSize > 1)
{
retMessage[len] = '\n';
retMessage[len+1] = '\0';
}
return 0;
}
static int quit(char *retMessage, int bufSize, char *cmd)
{
strncpy(retMessage, "Bye\n", bufSize);
return 2;
}
static int PwrLedOff(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedOffCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedBlue(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedBlueOnCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedBlueBlink(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedBlueBlinkCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedBlueFade(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedBlueFadeCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedRed(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedRedOnCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedRedBlink(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedRedBlinkCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedOrange(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedOrangeOnCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int PwrLedOrangeBlink(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, PLedOrangeBlinkCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int Shutdown(char *retMessage, int bufSize, char *cmd)
{
strncpy(retMessage, "OK\n", bufSize);
return 3;
}
static int SetFanMode(char *retMessage, int bufSize, char *cmd)
{
char *mode;
strncpy(retMessage, "ERR usage: SetFanMode auto|manual\n", bufSize);
mode = strtok(NULL, "\t\r\n: ");
if ( ! mode )
return 0;
syslog(LOG_DEBUG, "setting fan mode to: %s\n", mode);
strncpy(retMessage, "OK\n", bufSize);
if ( strncasecmp(mode, "auto", 4) == 0 )
fanMode = 1;
else if ( strncasecmp(mode, "manual", 6) == 0 )
fanMode = 0;
else
strncpy(retMessage, "ERR unkown mode\n", bufSize);
return 0;
}
static int SetFanSpeed(char *retMessage, int bufSize, char *cmd)
{
char *speed, *msg, *endptr;
int val;
asprintf(&msg, "ERR usage: SetFanSpeed 0 | %d ... %d\n", daemonCfg.speedMin, daemonCfg.speedMax);
strncpy(retMessage, msg, bufSize);
free(msg);
speed = strtok(NULL, "\t\r\n: ");
if ( ! speed )
return 0;
errno = 0;
val = (int) strtol(speed, &endptr, 0);
strncpy(retMessage, "ERR arg is no number\n", bufSize);
if ( errno != 0 || speed == endptr )
return 0;
strncpy(retMessage, "OK\n", bufSize);
if ( val == 0 )
fanSpeed = 0;
else if ( val < daemonCfg.speedMin )
fanSpeed = daemonCfg.speedMin;
else if ( val > daemonCfg.speedMax )
fanSpeed = daemonCfg.speedMax;
else
fanSpeed = val;
setFanSpeed(fanSpeed);
return 0;
}
#if 0 /* disabled commands that don't work on the WDMC Gen2 */
static int EnPwrRec(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, APREnableCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int DisPwrRec(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, APRDisableCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int GetPwrRec(char *retMessage, int bufSize, char *cmd)
{
int len;
char buf[15];
if(SendCommand(fd, APRStatusCmd, buf) > ERR_WRONG_ANSWER)
{
snprintf(retMessage, bufSize, "%d", buf[5]);
len = strlen(retMessage);
if(bufSize > 1)
{
retMessage[len] = '\n';
retMessage[len+1] = '\0';
}
}
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int EnWOL(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, WOLStatusEnableCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int DisWOL(char *retMessage, int bufSize, char *cmd)
{
if(SendCommand(fd, WOLStatusDisableCmd, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int GetWOL(char *retMessage, int bufSize, char *cmd)
{
int len;
char buf[15];
if(SendCommand(fd, WOLStatusGetCmd, buf) > ERR_WRONG_ANSWER)
{
snprintf(retMessage, bufSize, "%d", buf[5]);
len = strlen(retMessage);
if(bufSize > 1)
{
retMessage[len] = '\n';
retMessage[len+1] = '\0';
}
}
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int ReadRtc(char *retMessage, int bufSize, char *cmd)
{
int i;
char buf[15];
struct tm strTime;
char timeStr[100];
time_t rtcTime;
if(SendCommand(fd, RDateAndTimeCmd, buf) > ERR_WRONG_ANSWER)
{
for(i=5;i<12;i++)
{
buf[i] = (buf[i] & 0x0f) + 10 * ((buf[i] & 0xf0) >> 4); // The other end is a µC (doh!)
}
strTime.tm_year = (100 + (int)buf[11]);
strTime.tm_mon = buf[10]-1;
strTime.tm_mday = buf[9];
strTime.tm_hour = buf[7];
strTime.tm_min = buf[6];
strTime.tm_sec = buf[5];
strTime.tm_isdst = -1;
rtcTime = mktime(&strTime);
strcpy(timeStr, ctime(&rtcTime));
snprintf(retMessage, bufSize, "RTC: %s", timeStr);
}
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int systohc(char *retMessage, int bufSize, char *cmd)
{
int i;
char cmdBuf[15];
time_t sysTime;
struct tm *strSetTime;
// We do nothing, since it doesn't work yet
return 0;
// Copy the command to our buffer
for(i=0;i<13;i++)
{
cmdBuf[i] = WDateAndTimeCmd[i];
}
sysTime = time(NULL);
strSetTime = localtime(&sysTime);
// Put the current local time into the command buffer
cmdBuf[5] = (char)strSetTime->tm_sec;
cmdBuf[6] = (char)strSetTime->tm_min;
cmdBuf[7] = (char)strSetTime->tm_hour;
cmdBuf[8] = (char)strSetTime->tm_wday;
cmdBuf[9] = (char)strSetTime->tm_mday;
cmdBuf[10] = (char)(strSetTime->tm_mon + 1);
cmdBuf[11] = (char)(strSetTime->tm_year - 100);
// And modify the values so that the MCU understands them...
for(i=5;i<12;i++)
{
cmdBuf[i] = ((cmdBuf[i] / 10) << 4) + (cmdBuf[i] % 10);
}
if(SendCommand(fd, cmdBuf, NULL) == SUCCESS)
strncpy(retMessage, "OK\n", bufSize);
else
{
strncpy(retMessage, "ERR\n", bufSize);
return 1;
}
return 0;
}
static int hctosys(char *retMessage, int bufSize, char *cmd)
{
int i;
struct tm strTime;
struct timeval setTime;
time_t rtcTime;
time_t sysTime;
char timeStr[100];
char buf[15];
if(SendCommand(fd, RDateAndTimeCmd, buf) > ERR_WRONG_ANSWER)
{
for(i=5;i<12;i++)
{
buf[i] = (buf[i] & 0x0f) + 10 * ((buf[i] & 0xf0) >> 4); // The other end is a µC (doh!)
}
strTime.tm_year = (100 + (int)buf[11]);
strTime.tm_mon = buf[10]-1;
strTime.tm_mday = buf[9];
strTime.tm_hour = buf[7];
strTime.tm_min = buf[6];
strTime.tm_sec = buf[5];
strTime.tm_isdst = -1;
rtcTime = mktime(&strTime);
strcpy(timeStr, ctime(&rtcTime));
// Retrieve system time
sysTime = time(NULL);