First attempt to fix #24

This commit is contained in:
Daniel Graziotin 2012-10-24 11:13:25 +02:00
parent eda8443906
commit 45755d6228
4 changed files with 78 additions and 54 deletions

View file

@ -9,11 +9,18 @@ extern const char* program_pid;
struct s_sensors { struct s_sensors {
char* path; char* path;
char* fan_output_path;
char* fan_manual_path;
unsigned int temperature; unsigned int temperature;
struct s_sensors *next; struct s_sensors *next;
}; };
typedef s_sensors t_sensors; struct s_fans {
char* path;
char* fan_output_path;
char* fan_manual_path;
struct s_fans *next;
};
typedef struct s_sensors t_sensors;
typedef struct s_fans t_fans;
#endif #endif

View file

@ -57,18 +57,8 @@ int max_temp = 86; // do not set it > 90
int polling_interval = 7; int polling_interval = 7;
/*
struct s_sensors {
char* path;
char* fan_output_path;
char* fan_manual_path;
unsigned int temperature;
struct s_sensors *next;
};
*/
typedef struct s_sensors t_sensors; typedef struct s_sensors t_sensors;
typedef struct s_fans t_fans;
t_sensors *retrieve_sensors() t_sensors *retrieve_sensors()
{ {
@ -84,12 +74,12 @@ t_sensors *retrieve_sensors()
char number[2]; char number[2];
sprintf(number,"%d",0); sprintf(number,"%d",0);
int i = 0; int counter = 0;
int sensors_found = 0;
for(i = 0; i<10; i++) { for(counter = 0; counter<10; counter++) {
path = (char*) malloc(sizeof( char ) * path_size); path = (char*) malloc(sizeof( char ) * path_size);
sprintf(number,"%d",i); sprintf(number,"%d",counter);
path[0] = '\0'; path[0] = '\0';
strncat( path, path_begin, strlen(path_begin) ); strncat( path, path_begin, strlen(path_begin) );
strncat( path, number, strlen(number) ); strncat( path, number, strlen(number) );
@ -119,22 +109,29 @@ t_sensors *retrieve_sensors()
} }
fclose(file); fclose(file);
sensors_found++;
} }
free(path); free(path);
path = NULL; path = NULL;
} }
if(sensors_head != NULL) { if(verbose) {
find_fans(sensors_head); printf("Found %d sensors\n", sensors_found);
if(daemonize) {
syslog(LOG_INFO, "Found %d sensors", sensors_found);
}
} }
return sensors_head; return sensors_head;
} }
void find_fans(t_sensors* sensors) t_fans *retrieve_fans()
{ {
t_sensors *tmp = sensors;
t_fans *fans_head = NULL;
t_fans *fan = NULL;
char *path_output = NULL; char *path_output = NULL;
char *path_manual = NULL; char *path_manual = NULL;
@ -149,10 +146,10 @@ void find_fans(t_sensors* sensors)
sprintf(number,"%d",0); sprintf(number,"%d",0);
int counter = 0; int counter = 0;
int n_sensors = 0; int fans_found = 0;
int n_fans = 0;
for(int counter = 0; counter<10; counter++) {
for(counter = 0; counter<10; counter++) {
path_output = (char*) malloc(sizeof( char ) * path_min_size); path_output = (char*) malloc(sizeof( char ) * path_min_size);
path_output[0] = '\0'; path_output[0] = '\0';
path_manual = (char*) malloc(sizeof( char ) * path_man_size); path_manual = (char*) malloc(sizeof( char ) * path_man_size);
@ -171,38 +168,53 @@ void find_fans(t_sensors* sensors)
FILE *file = fopen(path_output, "r"); FILE *file = fopen(path_output, "r");
if(file != NULL) { if(file != NULL) {
if (tmp->path != NULL) { fan = (t_fans *) malloc( sizeof( t_fans ) );
tmp->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); fan->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size);
tmp->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); fan->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size);
} strcpy(fan->fan_output_path, path_output);
strcpy(fan->fan_manual_path, path_manual);
strcpy(tmp->fan_output_path, path_output); if (fans_head == NULL) {
strcpy(tmp->fan_manual_path, path_manual); fans_head = fan;
fans_head->next = NULL;
} else {
t_fans *tmp = fans_head;
while (tmp->next != NULL) {
tmp = tmp->next; tmp = tmp->next;
n_fans++; }
n_sensors++;
tmp->next = fan;
tmp->next->next = NULL;
}
fclose(file); fclose(file);
} fans_found++;
}
if(verbose) {
printf("Found %d sensors and %d fans\n", n_sensors, n_fans);
if(daemonize) {
syslog(LOG_INFO, "Found %d sensors and %d fans", n_sensors, n_fans);
}
} }
free(path_output); free(path_output);
path_output = NULL; path_output = NULL;
free(path_manual); free(path_manual);
path_manual = NULL; path_manual = NULL;
}
if(verbose) {
printf("Found %d fans\n", fans_found);
if(daemonize) {
syslog(LOG_INFO, "Found %d fans", fans_found);
}
}
return fans_head;
} }
void set_fans_man(t_sensors *sensors)
void set_fans_man(t_fans *fans)
{ {
t_sensors *tmp = sensors; t_fans *tmp = fans;
FILE *file; FILE *file;
while(tmp != NULL) { while(tmp != NULL) {
@ -238,9 +250,9 @@ t_sensors *refresh_sensors(t_sensors *sensors)
/* Controls the speed of the fan */ /* Controls the speed of the fan */
void set_fan_speed(t_sensors* sensors, int speed) void set_fan_speed(t_fans* fans, int speed)
{ {
t_sensors *tmp = sensors; t_fans *tmp = fans;
FILE *file; FILE *file;
while(tmp != NULL) { while(tmp != NULL) {
@ -359,10 +371,12 @@ void mbpfan()
retrieve_settings(); retrieve_settings();
t_sensors* sensors = retrieve_sensors(); t_sensors* sensors = retrieve_sensors();
set_fans_man(sensors); t_fans* fans = retrieve_fans();
set_fans_man(fans);
new_temp = get_temp(sensors); new_temp = get_temp(sensors);
fan_speed = 2000; fan_speed = 2000;
set_fan_speed(sensors, fan_speed); set_fan_speed(fans, fan_speed);
if(verbose) { if(verbose) {
@ -413,7 +427,7 @@ void mbpfan()
} }
} }
set_fan_speed(sensors, fan_speed); set_fan_speed(fans, fan_speed);
if(verbose) { if(verbose) {
printf("Sleeping for %d seconds\n", polling_interval); printf("Sleeping for %d seconds\n", polling_interval);

View file

@ -40,6 +40,9 @@ extern int polling_interval;
struct s_sensors; struct s_sensors;
typedef struct s_sensors t_sensors; typedef struct s_sensors t_sensors;
struct s_fans;
typedef struct s_fans t_fans;
/** /**
* Tries to use the settings located in * Tries to use the settings located in
* /etc/mbpfan.conf * /etc/mbpfan.conf
@ -63,19 +66,19 @@ t_sensors *refresh_sensors(t_sensors *sensors);
* Detect the fans in /sys/devices/platform/applesmc.768/ * Detect the fans in /sys/devices/platform/applesmc.768/
* Associate each fan to a sensor * Associate each fan to a sensor
*/ */
void find_fans(t_sensors *sensors); t_fans* retrieve_fans();
/** /**
* Given a list of sensors with associated fans * Given a list of sensors with associated fans
* Set them to manual control * Set them to manual control
*/ */
void set_fans_man(t_sensors *sensors); void set_fans_man(t_fans *fans);
/** /**
* Given a list of sensors with associated fans * Given a list of sensors with associated fans
* Change their speed * Change their speed
*/ */
void set_fan_speed(t_sensors* sensors, int speed); void set_fan_speed(t_fans* fans, int speed);
/** /**
* Return average CPU temp in degrees (ceiling) * Return average CPU temp in degrees (ceiling)

View file

@ -33,9 +33,9 @@ static const char *test_sensor_paths()
static const char *test_fan_paths() static const char *test_fan_paths()
{ {
t_sensors* sensors = retrieve_sensors(); t_fans* fans = retrieve_fans();
mu_assert("No sensors found", sensors != NULL); mu_assert("No fans found", fans != NULL);
t_sensors* tmp = sensors; t_fans* tmp = fans;
int found_fan_path = 0; int found_fan_path = 0;
while(tmp != NULL) { while(tmp != NULL) {