/********************************************************************
Header File Inclusion
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
/********************************************************************
Macros
SH_USR_LED_CNT : shell commnad for counting user led devices
SH_USR_LED_DEV : shell command for retrieving user led devices' name
********************************************************************/
#define SH_USR_LED_CNT "find /sys/class/leds/*/ | grep trigger | \
xargs grep \'\\[default-on\\]\' | wc -l"
#define SH_USR_LED_DEV "find /sys/class/leds/*/ | grep trigger | \
xargs grep \'\\[default-on\\]\' | \
awk -F \'/\' '{printf $5\"\\n\" }\'"
#define LEN_NAME 48
#define LEN_CMD 128
#define MAX_COUNT 10
/* quit signal tag */
volatile bool quit = false;
/*=====================restore_devices==================*/
/* set all led devices as [default on] state */
static int restore_devices(char names[][LEN_NAME], int count) {
int i;
char cmd[MAX_COUNT][LEN_CMD];
for (i = 0; i < count; i++) {
snprintf(cmd[i], LEN_CMD,
"echo default-on > /sys/class/leds/%s/trigger", names[i]);
if (system(cmd[i]) != 0) {
fprintf(stderr, "fail to restore devices\n");
return -1;
}
}
return 0;
}
/*=====================turn_off==========================*/
/* turn off all led devices */
static int turn_off(char names[][LEN_NAME], int count) {
int i;
char cmd[MAX_COUNT][LEN_CMD];
for (i = 0; i < count; i++) {
snprintf(cmd[i], LEN_CMD,
"echo 0 > /sys/class/leds/%s/brightness", names[i]);
if (system(cmd[i]) != 0) {
fprintf(stderr, "fail to turn off %s\n", names[i]);
return -1;
}
}
return 0;
}
/*=====================turn_on===========================*/
/* turn on all led devices */
static int turn_on(char names[][LEN_NAME], int count) {
int i;
char cmd[MAX_COUNT][LEN_CMD];
for (i = 0; i < count; i++) {
snprintf(cmd[i], LEN_CMD,
"echo 1 > /sys/class/leds/%s/brightness", names[i]);
if (system(cmd[i]) != 0) {
fprintf(stderr, "fail to turn on %s\n", names[i]);
return -1;
}
}
return 0;
}
/*=====================show_devices======================*/
/* show all led devices */
static void show_devices(char names[][LEN_NAME], int count) {
int i;
printf("twinkle %d leds\n", count);
for (i = 0; i < count; i++)
printf("%d. /sys/class/leds/%s\n", i + 1, names[i]);
}
/*=====================get_devices_names======================*/
/* retrieve user led devices' name */
static int get_devices_names(char names[][LEN_NAME], int count) {
int i;
FILE *f;
/* the popen() function opens a process by creating
* a pipe, forking, and invoking the shell */
f = popen(SH_USR_LED_DEV, "r");
if (! f)
goto err;
for (i = 0; i < count; i++)
if (fscanf(f, "%s", names[i]) == EOF)
goto err;
pclose(f);
return 0;
err:
fprintf(stderr, "something error, maybe there's no led device\n");
if (f)
pclose(f);
return -1;
}
/*=================get_devices_count==================*/
/* retrieve the amount of user led devices */
static int get_devices_count(int *count) {
FILE *f;
/* the popen() function opens a process by creating
* a pipe, forking, and invoking the shell */
f = popen(SH_USR_LED_CNT, "r");
if (! f)
goto err;
if(fscanf(f, "%d", count) == EOF)
goto err;
if (*count == 0) {
printf("warning: there's no led device\n");
return -2;
}
if (*count > MAX_COUNT) {
*count = MAX_COUNT;
printf("warning: max numbuer of leds is %d, dope tail\n", MAX_COUNT);
}
pclose(f);
return 0;
err:
fprintf(stderr, "something error, maybe there's no led device\n");
if (f)
pclose(f);
return -1;
}
/*=====================sig_handle======================*/
/* response to ctrl+c */
static void sig_handle(int signo) {
quit = true;
}
/*==============================main===============================*/
/* this program twinkle the leds which set as [defualt on] during */
/* the time of kernel booting. you can check it by bellow command: */
/* $ cat /sys/class/leds/${device_name}/trigger */
int main(int argc, char **argv) {
int i;
int count;
char names[MAX_COUNT][LEN_NAME];
/* ctrl+c handler */
signal(SIGINT, sig_handle);
if (get_devices_count(&count) < 0)
return -1;
if (get_devices_names(names, count) < 0)
return -1;
show_devices(names, count);
/* twinkle
* 1. turn on all leds
* 2. sleep 0.5 s
* 3. tunn off all leds
* 4. goto 1
* */
for (i = 0; i < 10; i++) {
/* set as true while ctrl+c */
if (quit)
break;
if (turn_on(names, count) < 0)
break;
usleep(500 * 1000);
if (turn_off(names, count) < 0)
break;
}
restore_devices(names, count);
return 0;
}
|