#include <unistd.h>
#include <string.h>
-#define BUFFER_SIZE 4096
-
-using namespace std;
-
const int major_version_number = 1;
const int minor_version_number = 2;
const char *default_configuration_file = "/etc/breezed.conf";
-// The time period to check the temperature.
+/* The time period to check the temperature. */
const int polling_delay = 5;
-// Minimum time between last change and a change down.
+/* Minimum time between last change and a change down. */
const int minimum_delay_to_go_down = 30;
-// Gap between a threshold to go up and a threshold to go down to
-// reduce the oscillations.
+/* Gap between a threshold to go up and a threshold to go down to
+ reduce the oscillations. */
const int down_temperature_delta = 2;
char *file_fan = 0;
char *configuration_file;
-//////////////////////////////////////////////////////////////////////
+/******************************************************************/
char *next_word(char *buffer, char *r, int buffer_size) {
char *s;
s = next_word(token, s, buffer_size);
nb_file_thermal++;
}
- file_thermal = new char *[nb_file_thermal];
- file_thermal_fd = new int[nb_file_thermal];
+
+ file_thermal = (char **) malloc(nb_file_thermal * sizeof(char *));
+ file_thermal_fd = (int *) malloc(nb_file_thermal * sizeof(int));
s = definition;
int k = 0;
while(s) {
s = next_word(token, s, buffer_size);
- file_thermal[k] = new char[strlen(token) + 1];
- strcpy(file_thermal[k], token);
+ file_thermal[k] = strdup(token);
k++;
}
}
nb_temperature_thresholds++;
}
- temperature_thresholds = new int[nb_temperature_thresholds];
+ temperature_thresholds =
+ (int *) malloc(nb_temperature_thresholds * sizeof(int));
temperature_thresholds[0] = -1;
}
}
-//////////////////////////////////////////////////////////////////////
+void evaluate_one_configuration_line(char *line, int line_number) {
+ char token[buffer_size];
+ char *s;
-int main(int argc, char **argv) {
+ s = next_word(token, line, buffer_size);
+
+ if(strcmp(token, "thermal_files") == 0) {
+ if(s == 0) {
+ fprintf(stderr, "Missing parameter in %s:%d\n",
+ configuration_file, line_number);
+ exit(1);
+ }
+ define_thermal_files(s);
+ }
+
+ else if(strcmp(token, "debug") == 0) {
+ debug = 1;
+ }
+
+ else if(strcmp(token, "fan_file") == 0) {
+ if(file_fan) {
+ fprintf(stderr, "Fan file already defined.\n");
+ exit(1);
+ }
+ if(s == 0) {
+ fprintf(stderr, "Missing parameter in %s:%d\n",
+ configuration_file, line_number);
+ exit(1);
+ }
+ file_fan = strdup(s);
+ }
+
+ else if(strcmp(token, "temperature_thresholds") == 0) {
+ if(s == 0) {
+ fprintf(stderr, "Missing parameter in %s:%d\n",
+ configuration_file, line_number);
+ exit(1);
+ }
+ define_temperature_thresholds(s);
+ }
+
+ else if(token[0] && token[0] != '#') {
+ fprintf(stderr, "Unknown keyword '%s' in %s:%d.\n",
+ token, configuration_file, line_number);
+ exit(1);
+ }
+}
+
+/******************************************************************/
- char buffer[buffer_size], token[buffer_size];
+int main(int argc, char **argv) {
+ char buffer[buffer_size];
+ int i, t;
- configuration_file = new char[strlen(default_configuration_file) + 1];
- strcpy(configuration_file, default_configuration_file);
+ configuration_file = strdup(default_configuration_file);
- int i = 1;
+ i = 1;
while(i < argc) {
if(strcmp(argv[i], "--debug") == 0 || strcmp(argv[i], "-d") == 0) {
else if(strcmp(argv[i], "--no-configuration-file") == 0 ||
strcmp(argv[i], "-ncf") == 0) {
- delete[] configuration_file;
+ free(configuration_file);
configuration_file = 0;
i++;
}
exit(1);
}
- delete[] configuration_file;
- configuration_file = new char[strlen(argv[i]) + 1];
- strcpy(configuration_file, argv[i]);
+ free(configuration_file);
+ configuration_file = strdup(argv[i]);
i++;
}
fprintf(stderr, "Fan file already defined.\n");
exit(1);
}
- file_fan = new char[strlen(argv[i]) + 1];
- strcpy(file_fan, argv[i]);
+ file_fan = strdup(argv[i]);
i++;
}
}
- //////////////////////////////////////////////////////////////////////
+ /******************************************************************/
if(configuration_file) {
- char raw_line[BUFFER_SIZE];
+ char raw_line[buffer_size];
int start, end, eol, k;
FILE *file;
end -= start;
eol -= start;
start = 0;
- end += fread(raw_line + end, sizeof(char), BUFFER_SIZE - end, file);
+ end += fread(raw_line + end, sizeof(char), buffer_size - end, file);
while(eol < end && raw_line[eol] != '\n') eol++;
}
/* The end of the line is the buffer size, which means the line is
too long */
- if(eol == BUFFER_SIZE) {
- raw_line[BUFFER_SIZE - 1] = '\0';
+ if(eol == buffer_size) {
+ raw_line[buffer_size - 1] = '\0';
fprintf(stderr, "Selector: Line too long (max is %d characters):\n",
- BUFFER_SIZE);
+ buffer_size);
fprintf(stderr, raw_line);
fprintf(stderr, "\n");
exit(1);
configuration_file, line_number, raw_line + start);
}
- s = next_word(token, raw_line + start, buffer_size);
-
- if(strcmp(token, "thermal_files") == 0) {
- if(s == 0) {
- fprintf(stderr, "Missing parameter in %s:%d\n",
- configuration_file, line_number);
- exit(1);
- }
- define_thermal_files(s);
- }
-
- else if(strcmp(token, "debug") == 0) {
- debug = 1;
- }
-
- else if(strcmp(token, "fan_file") == 0) {
- if(file_fan) {
- fprintf(stderr, "Fan file already defined.\n");
- exit(1);
- }
- if(s == 0) {
- fprintf(stderr, "Missing parameter in %s:%d\n",
- configuration_file, line_number);
- exit(1);
- }
- file_fan = new char[strlen(s) + 1];
- strcpy(file_fan, s);
- }
-
- else if(strcmp(token, "temperature_thresholds") == 0) {
- if(s == 0) {
- fprintf(stderr, "Missing parameter in %s:%d\n",
- configuration_file, line_number);
- exit(1);
- }
- define_temperature_thresholds(s);
- }
-
- else if(token[0] && token[0] != '#') {
- fprintf(stderr, "Unknown keyword '%s' in %s:%d.\n",
- token, configuration_file, line_number);
- exit(1);
- }
+ evaluate_one_configuration_line(raw_line + start, line_number);
start = eol + 1;
}
}
- //////////////////////////////////////////////////////////////////////
+ /******************************************************************/
if(nb_temperature_thresholds == 0) {
fprintf(stderr, "No temperature threshold was provided.\n");
exit(1);
}
- for(int i = 0; i < nb_file_thermal; i++) {
+ for(i = 0; i < nb_file_thermal; i++) {
file_thermal_fd[i] = open(file_thermal[i], O_RDONLY);
if(file_thermal_fd[i] < 0) {
fprintf(stderr, "Can not open %s for reading (%s).\n",
exit(1);
}
- //////////////////////////////////////////////////////////////////////
+ /******************************************************************/
if(debug) {
- for(int t = 0; t < nb_file_thermal; t++) {
+ for(t = 0; t < nb_file_thermal; t++) {
printf("file_thermal[%d] %s\n", t, file_thermal[t]);
}
printf("file_fan %s\n", file_fan);
- for(int t = 0; t < nb_temperature_thresholds; t++) {
+ for(t = 0; t < nb_temperature_thresholds; t++) {
printf("temperature_thresholds[%d] %d",
t, temperature_thresholds[t]);
}
}
- //////////////////////////////////////////////////////////////////////
+ /******************************************************************/
while(1) {
printf("Temperature:");
}
- for(int i = 0; i < nb_file_thermal; i++) {
+ for(i = 0; i < nb_file_thermal; i++) {
lseek(file_thermal_fd[i], 0, SEEK_SET);
int size = read(file_thermal_fd[i], buffer, buffer_size);
printf(" %d", t);
}
- // So that we can deal with the new files where the
- // temperature are in 1/1000th of C
+ /* So that we can deal with the new files where the
+ temperature are in 1/1000th of C */
if(t > 1000) t /= 1000;
if(t > temperature) temperature = t;
}
}
- // We set it every time, even when there is no change, to handle
- // when the level has been modified somewhere else (for instance
- // when combing back from suspend).
+ /* We set it every time, even when there is no change, to handle
+ when the level has been modified somewhere else (for instance
+ when combing back from suspend). */
set_fan_level(file_fan_fd, new_level, nb_temperature_thresholds - 1);