/*
* Low-level C code for managing power states in an embedded Linux device.
* This uses sysfs interfaces (/sys/power/* and /sys/devices/system/cpu/*)
* for P-state (CPU frequency/governor) and S-state (system suspend) control.
* Requires root privileges to write to sysfs.
*
* Features:
* - Set CPU governor (e.g., "performance", "powersave", "ondemand") for all online CPUs.
* - Set CPU max/min frequency for all online CPUs.
* - Enter system suspend states (e.g., "mem", "standby", "freeze").
* - Power state manager with predefined states: NORMAL, IDLE, LIGHT_SLEEP, DEEP_SLEEP.
*
* Compile: gcc -o power_mgr power_mgr.c
* Usage: sudo ./power_mgr <command> [args]
* Examples:
* sudo ./power_mgr governor powersave # Set all CPUs to powersave governor
* sudo ./power_mgr freq max 800000 # Set max freq to 800 MHz on all CPUs
* sudo ./power_mgr suspend mem # Suspend to RAM
* sudo ./power_mgr state IDLE # Enter IDLE power state
*
* Notes:
* - Assumes up to 32 CPUs (common in embedded SoCs).
* - Frequencies in kHz (e.g., 800000 = 800 MHz).
* - Check /sys/power/state for supported suspend modes.
* - Error handling includes ENOENT for offline CPUs.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#define MAX_CPUS 32
#define PATH_MAX 256
#define BUF_SIZE 32
typedef enum {
POWER_NORMAL, // Performance mode: max freq, performance governor
POWER_IDLE, // Light idle: ondemand governor, medium freq
POWER_LIGHT_SLEEP,// Standby/freeze: minimal activity, standby suspend
POWER_DEEP_SLEEP // Deep sleep: suspend to RAM
} power_state_t;
/* Set governor for a specific CPU (e.g., "cpu0") */
static int set_cpu_governor(const char *cpu, const char *governor) {
char path[PATH_MAX];
int fd;
snprintf(path, sizeof(path), "/sys/devices/system/cpu/%s/cpufreq/scaling_governor", cpu);
fd = open(path, O_WRONLY);
if (fd < 0) {
return -1;
}
if (write(fd, governor, strlen(governor)) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
/* Set governor for all online CPUs (skips ENOENT for offline) */
int set_governor_all(const char *governor) {
char cpu[16];
int i, ret = 0;
for (i = 0; i < MAX_CPUS; i++) {
snprintf(cpu, sizeof(cpu), "cpu%d", i);
if (set_cpu_governor(cpu, governor) < 0) {
if (errno != ENOENT) {
ret = -1;
}
}
}
return ret;
}
/* Set max frequency for a specific CPU (in kHz) */
static int set_cpu_max_freq(const char *cpu, long freq_khz) {
char path[PATH_MAX], buf[BUF_SIZE];
int fd;
snprintf(path, sizeof(path), "/sys/devices/system/cpu/%s/cpufreq/scaling_max_freq", cpu);
fd = open(path, O_WRONLY);
if (fd < 0) {
return -1;
}
snprintf(buf, sizeof(buf), "%ld", freq_khz);
if (write(fd, buf, strlen(buf)) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
/* Set max frequency for all online CPUs */
int set_max_freq_all(long freq_khz) {
char cpu[16];
int i, ret = 0;
for (i = 0; i < MAX_CPUS; i++) {
snprintf(cpu, sizeof(cpu), "cpu%d", i);
if (set_cpu_max_freq(cpu, freq_khz) < 0) {
if (errno != ENOENT) {
ret = -1;
}
}
}
return ret;
}
/* Set min frequency for all online CPUs (symmetric to max) */
int set_min_freq_all(long freq_khz) {
char cpu[16], path[PATH_MAX], buf[BUF_SIZE];
int i, fd, ret = 0;
for (i = 0; i < MAX_CPUS; i++) {
snprintf(cpu, sizeof(cpu), "cpu%d", i);
snprintf(path, sizeof(path), "/sys/devices/system/cpu/%s/cpufreq/scaling_min_freq", cpu);
fd = open(path, O_WRONLY);
if (fd < 0) {
if (errno != ENOENT) ret = -1;
continue;
}
snprintf(buf, sizeof(buf), "%ld", freq_khz);
if (write(fd, buf, strlen(buf)) < 0) {
ret = -1;
}
close(fd);
}
return ret;
}
/* Enter system suspend state (e.g., "mem", "standby", "freeze") */
int enter_suspend(const char *state) {
char path[] = "/sys/power/state";
int fd;
fd = open(path, O_WRONLY);
if (fd < 0) {
perror("open /sys/power/state");
return -1;
}
if (write(fd, state, strlen(state)) < 0) {
perror("write suspend state");
close(fd);
return -1;
}
close(fd);
return 0;
}
/* Set predefined power state */
int set_power_state(power_state_t state) {
int ret = 0;
switch (state) {
case POWER_NORMAL:
ret |= set_governor_all("performance");
ret |= set_max_freq_all(0); // 0 = unlimited (use available_max)
break;
case POWER_IDLE:
ret |= set_governor_all("ondemand");
ret |= set_max_freq_all(800000); // Example: cap at 800 MHz
break;
case POWER_LIGHT_SLEEP:
ret |= set_governor_all("powersave");
ret |= set_max_freq_all(400000); // Low freq
ret |= enter_suspend("standby"); // Or "freeze"
break;
case POWER_DEEP_SLEEP:
ret |= set_governor_all("powersave");
ret |= enter_suspend("mem");
break;
default:
return -1;
}
return ret;
}
/* Print usage */
static void usage(const char *prog) {
printf("Usage: %s <command> [args]\n", prog);
printf("Commands:\n");
printf(" governor <name> # e.g., powersave, performance, ondemand\n");
printf(" freq max <kHz> # Set max freq on all CPUs\n");
printf(" freq min <kHz> # Set min freq on all CPUs\n");
printf(" suspend <state> # e.g., mem, standby, freeze\n");
printf(" state <NORMAL|IDLE|LIGHT_SLEEP|DEEP_SLEEP>\n");
}
/* Main: CLI example */
int main(int argc, char *argv[]) {
if (argc < 2) {
usage(argv[0]);
return 1;
}
if (strcmp(argv[1], "governor") == 0 && argc == 3) {
return set_governor_all(argv[2]);
} else if (strcmp(argv[1], "freq") == 0 && argc == 4) {
long freq = atol(argv[3]);
if (strcmp(argv[2], "max") == 0) {
return set_max_freq_all(freq);
} else if (strcmp(argv[2], "min") == 0) {
return set_min_freq_all(freq);
}
} else if (strcmp(argv[1], "suspend") == 0 && argc == 3) {
return enter_suspend(argv[2]);
} else if (strcmp(argv[1], "state") == 0 && argc == 3) {
if (strcmp(argv[2], "NORMAL") == 0) return set_power_state(POWER_NORMAL);
if (strcmp(argv[2], "IDLE") == 0) return set_power_state(POWER_IDLE);
if (strcmp(argv[2], "LIGHT_SLEEP") == 0) return set_power_state(POWER_LIGHT_SLEEP);
if (strcmp(argv[2], "DEEP_SLEEP") == 0) return set_power_state(POWER_DEEP_SLEEP);
}
usage(argv[0]);
return 1;
}
This code is portable across embedded Linux devices with standard sysfs support (e.g., Raspberry Pi, BeagleBone, or custom ARM SoCs). Customize frequencies and states based on your hardware. Test suspend states first by reading /sys/power/state. For production, integrate into your application or init scripts, and add logging/error recovery as needed.