F3RP61/LED: f3rp6x_led.c

File f3rp6x_led.c, 2.6 KB (added by Shuei Yamada, 12 years ago)
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/ioctl.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <string.h>
9#include <errno.h>
10#include "asm/fam3rtos/fam3rtos_sysctl.h"
11
12char* onoff[] = {"off", "on"};
13char* ledname[] = {"run", "alm", "err"};
14char* command[] = {
15 "run_off",
16 "run_on",
17 "alm_off",
18 "alm_on",
19 "err_off",
20 "err_on",
21 "status",
22};
23
24void help(char* s)
25{
26 int i;
27 const int ncommand = sizeof(command)/sizeof(command[0])-1;
28 fprintf(stderr, "usage : %s commands\n", s);
29 fprintf(stderr, " commands: %s\n", command[ncommand]);
30 for (i=0; i<ncommand-1; i+=2) {
31 fprintf(stderr, " commands: %s, %s\n", command[i], command[i+1]);
32 }
33 exit(EXIT_SUCCESS);
34}
35
36int main(int argc, char* argv[])
37{
38 int iarg, iled;
39 const int nled = sizeof(ledname)/sizeof(ledname[0]);
40
41 char led[4], status[4];
42
43 if (argc<=1) {
44 help(argv[0]);
45 }
46
47 const char* devicefile = "/dev/m3sysctl";
48
49 const int fd=open(devicefile, O_RDWR);
50 if (fd<0) {
51 fprintf(stderr, "%s: %s\n", strerror(errno), devicefile);
52 return EXIT_FAILURE;
53 }
54
55 unsigned long ledval = 0;
56 for (iarg=1; iarg<argc; iarg++) {
57 if (strcmp(argv[iarg], command[6])==0) {
58 if (ioctl(fd, RP6X_SYSIOC_GETLED, &ledval)==EFAULT) {
59 fprintf(stderr, "%s: %s\n", strerror(errno), devicefile);
60 close(fd);
61 return EXIT_FAILURE;
62 }
63 for (iled=0; iled<nled; iled++) {
64 printf("%s: %s\n", ledname[iled], onoff[(ledval>>iled)&1]);
65 }
66 } else {
67 if (sscanf(argv[iarg], "%3s_%s", led, status)!=2) {
68 help(argv[0]);
69 }
70 if (strcmp(status, onoff[0])!=0 && strcmp(status, onoff[1])!=0) {
71 help(argv[0]);
72 }
73
74 switch(led[0]) {
75 case 'r': // RUN
76 if (status[1]=='n') {
77 ledval |= RP6X_LED_RUN_ON;
78 } else {
79 ledval |= RP6X_LED_RUN_OFF;
80 }
81 break;
82 case 'a': // ALM
83 if (status[1]=='n') {
84 ledval |= RP6X_LED_ALM_ON;
85 } else {
86 ledval |= RP6X_LED_ALM_OFF;
87 }
88 break;
89 case 'e': // ERR
90 if (status[1]=='n') {
91 ledval |= RP6X_LED_ERR_ON;
92 } else {
93 ledval |= RP6X_LED_ERR_OFF;
94 }
95 default:
96 break;
97 }
98 if (ioctl(fd, RP6X_SYSIOC_SETLED, &ledval)==EFAULT) {
99 fprintf(stderr, "%s: %s\n", strerror(errno), devicefile);
100 close(fd);
101 return EXIT_FAILURE;
102 }
103 }
104 }
105
106 close(fd);
107 return ledval;
108}