Skip to content

Commit

Permalink
Detect minimum and maximum fan speeds
Browse files Browse the repository at this point in the history
Default to previous values if detection fails.  Configuration can
still override these values.  Fixes #114.
  • Loading branch information
gaul committed Aug 20, 2018
1 parent e58a67f commit 2a12d5b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
43 changes: 43 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,48 @@ void check_requirements()
}


static int read_value(const char *path)
{
int value = -1;
FILE *file = fopen(path, "r");
if (file != NULL) {
fscanf(file, "%d", &value);
fclose(file);
}
return value;
}


static void set_defaults(void)
{
int i;
char *path;
int value;
for (i = 1; i <= 10; ++i) {
path = smprintf("%s/fan%d_min", APPLESMC_PATH, i);
value = read_value(path);
if (value != -1 && (min_fan_speed == -1 || value < min_fan_speed)) {
min_fan_speed = value;
}
free(path);

path = smprintf("%s/fan%d_max", APPLESMC_PATH, i);
value = read_value(path);
if (value != -1 && (max_fan_speed == -1 || value > max_fan_speed)) {
max_fan_speed = value;
}
free(path);
}

if (min_fan_speed == -1) {
min_fan_speed = 2000;
}
if (max_fan_speed == -1) {
max_fan_speed = 6200;
}
}


int main(int argc, char *argv[])
{

Expand Down Expand Up @@ -133,6 +175,7 @@ int main(int argc, char *argv[])


check_requirements();
set_defaults();

// pointer to mbpfan() function in mbpfan.c
void (*fan_control)() = mbpfan;
Expand Down
19 changes: 15 additions & 4 deletions src/mbpfan.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

int min_fan_speed = 2000;
int max_fan_speed = 6200;
// TODO: per-fan minimum and maximum?
int min_fan_speed = -1;
int max_fan_speed = -1;

/* temperature thresholds
* low_temp - temperature below which fan speed will be at minimum
Expand All @@ -63,8 +64,7 @@ t_sensors* sensors = NULL;
t_fans* fans = NULL;


static char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
static char *smprintf(const char *fmt, ...)
char *smprintf(const char *fmt, ...)
{
char *buf;
int cnt;
Expand Down Expand Up @@ -507,6 +507,17 @@ void mbpfan()

retrieve_settings(NULL);

if (min_fan_speed > max_fan_speed) {
syslog(LOG_INFO, "Invalid fan speeds: %d %d", min_fan_speed, max_fan_speed);
printf("Invalid fan speeds: %d %d\n", min_fan_speed, max_fan_speed);
exit(EXIT_FAILURE);
}
if (low_temp > high_temp || high_temp > max_temp) {
syslog(LOG_INFO, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_fan_speed);
printf("Invalid temperatures: %d %d %d\n", low_temp, high_temp, max_fan_speed);
exit(EXIT_FAILURE);
}

sensors = retrieve_sensors();
fans = retrieve_fans();
set_fans_man(fans);
Expand Down
2 changes: 2 additions & 0 deletions src/mbpfan.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ typedef struct s_sensors t_sensors;
struct s_fans;
typedef struct s_fans t_fans;

char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2)));

/**
* Return true if the kernel is < 3.15.0
*/
Expand Down

0 comments on commit 2a12d5b

Please sign in to comment.