12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include <string.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <errno.h>
- #include <unistd.h>
- #define BUFFER_SIZE 64
- // use sysfs to control the LED on connected device
- int main(int argc, char *argv[]) {
- FILE *f;
- char buf[BUFFER_SIZE];
- char *sysfs_LEDS = "/sys/class/leds/";
- // go to sysfs node, this preempts the need to concatenate user data (argv)
- if(0 != chdir(sysfs_LEDS)) {
- fprintf(stderr, "failed to change directory to %s\n", sysfs_LEDS);
- return 1;
- }
- // [?] argc is one when no parameters supplied and argv[0] is program name
- if(/* less than two parameters, terminate with error */ 2 > argc) {
- fprintf(stderr, "must provide sysfs device name\n");
- // short when we cannot provide user with list of device names
- if(NULL == (f = popen("ls", "r"))) return 1;
- // provide user with list of possible device names
- fprintf(stderr, "you might want to try:\n");
- while(NULL != fgets(buf, BUFFER_SIZE, f)) fprintf(stderr, "%s", buf);
- pclose(f);
- return 1;
- }
- // make sure supplied device name is short and is mostly alpha-numeric
- for(uint8_t i = 0; i < BUFFER_SIZE; i++) {
- char comp = argv[1][i];
- uint8_t okay = 0;
- if('-' == comp || '_' == comp || '\0' == comp) okay |= 1;
- if('a' <= comp && 'z' >= comp) okay |= 1;
- if('A' <= comp && 'Z' >= comp) okay |= 1;
- if('0' <= comp && '9' >= comp) okay |= 1;
- // terminate if device name is not okay
- if(! okay) fprintf(stderr, "Can not deal with this device name.\n");
- // terminate if device name is really long
- if(BUFFER_SIZE - 1 == i && '\0' != comp) {
- fprintf(stderr, "Device name might be too long.\n");
- // clear okay flag so we can fail out
- okay = 0;
- }
- if(! okay) return 1;
- // short if we are done before BUFFER_SIZE characters
- if('\0' == comp) break;
- }
- // [!] argv contains at most 64 of: a..z A..Z 0..9 hyphen or underscore
- if(0 != chdir(argv[1])) {
- fprintf(stderr, "failed to change directory to %s\n", argv[1]);
- return 1;
- }
- // make sure we are in control of the led, not some other source
- { if(NULL == (f = fopen("trigger", "r+"))) {
- fprintf(stderr, "error when opening the trigger: %s\n", strerror(errno));
- return 1;
- }
- if(-1 == fwrite("none\n", 1, 5, f)) {
- fprintf(stderr, "couldn't write \"none\" to trigger: %s\n", strerror(errno));
- return 1;
- }
- fclose(f);
- }
- // blink the leds, on for a second, off for a second
- uint8_t state = 0;
- while(1) {
- // on every loop, reopen the file to make sure it is still there
- { if(NULL == (f = fopen("brightness", "r+"))) {
- fprintf(stderr, "oh no: %s\n", strerror(errno));
- return 1;
- }
- if(-1 == fwrite(state ?"1\n" :"0\n", 1, 2, f)) {
- fprintf(stderr, "uh oh setting \"%d\" led: %s\n", state, strerror(errno));
- return 1;
- }
- fclose(f);
- }
- // one second on, one second off
- sleep(1);
- // toggle state on ... off ... on ... off ...
- state = !state;
- }
- }
|