From the shell I can activate the leds on my system like this:
#echo 1 > /sys/class/leds/NAME:COLOR:LOCATION/brightness
I want to do the exact same thing from a C program, but I have not been able to find a simple example on how to accomplish this?
sysfs is a pseudo file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel's device model to user space through virtual files.
Adding more to confuse :" ioctl : However, ioctl is deprecated in the kernel, and you will find it hard to get any drivers with new uses of ioctl accepted upstream.
The (obsolete) sysfs() system call returns information about the filesystem types currently present in the kernel.
Open the sysfs node like a file, write '1' to it, and close it again.
For example:
#include <stdio.h>
#include <fcntl.h>
void enable_led() {
int fd;
char d = '1';
fd = open("sys/class/leds/NAME:COLOR:LOCATION/brightness", O_WRONLY);
write (fd, &d, 1);
close(fd);
}
Something like this:
#include <stdio.h>
int main(int argc, char **argv)
{
FILE* f = fopen("/sys/class/leds/NAME:COLOR:LOCATION/brightness", "w");
if (f == NULL) {
fprintf(stderr, "Unable to open path for writing\n");
return 1;
}
fprintf(f, "1\n");
fclose(f);
return 0;
}
I'm not booted into my linux partition, but I suspect it goes something like this:
int f = open("/sys/class/leds/NAME:COLOR:LOCATION/brightness",O_WRONLY);
if (f != -1)
{
write(f, "1", 1);
close(f);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With