-
Notifications
You must be signed in to change notification settings - Fork 37
/
config.c
285 lines (220 loc) · 5.46 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* config.c - Fan control daemon for MacBook
*
* Copyright (C) 2010 Mikael Strom <mikael@sesamiq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
//-----------------------------------------------------------------------------
static FILE *fp;
float temp_avg_floor = 40; // default values if no config file is found
float temp_avg_ceiling = 50;
float temp_TC0P_floor = 40;
float temp_TC0P_ceiling = 50;
float temp_TG0P_floor = 40;
float temp_TG0P_ceiling = 50;
float fan_min = 0;
float fan_max = 6200; // fixed max value
int log_level = 0;
int exclude[MAX_EXCLUDE]; // array of sensors to exclude
//-----------------------------------------------------------------------------
int match(char* name, char* buf)
{
char* start = buf;
char* end = buf;
if(strlen(buf) < 1)
{
return 0;
}
// skip preceeding ws
while(*start && isblank(*start))
{
++start;
}
// skip to end
while(*end)
{
++end;
}
// delete ws backwards from end
while(end > start && isblank(*end))
{
*end = 0;
--end;
}
// compare strings
return strcmp(name, start) == 0;
}
//-----------------------------------------------------------------------------
int get_val(char *buf)
{
while(isblank(*buf))
{
++buf;
}
if(! *buf)
{
return -1;
}
return atoi(buf);
}
//-----------------------------------------------------------------------------
// format is: name : integer
int read_param(char* name, int min_val, int max_val, int def)
{
fseek(fp, 0, SEEK_SET);
while(1)
{
char buf[64];
char *s = fgets(buf, sizeof(buf), fp);
if(s == NULL)
{
break; // exit when no more to read
}
if(buf[0] == '#' || buf[0] == '\n')
{
continue; // skip comments
}
char *colon = strchr(buf, ':'); // find colon
if(colon == NULL)
{
printf("Ill formed line in config file: %s\n", buf);
continue;
}
*colon = 0; // terminate string at colon
if(match(name, buf))
{
int val = get_val(colon + 1); // get value
if(val < 0)
{
printf("Ill formed line in config file: %s\n", buf);
continue;
}
else
{
val = min(max_val, val);// clamp
val = max(min_val, val);
return val; // success
}
}
}
return def;
}
//-----------------------------------------------------------------------------
// format is: exclude : integer {integer}
void read_exclude_list()
{
fseek(fp, 0, SEEK_SET);
while(1)
{
char buf[256];
char *s = fgets(buf, sizeof(buf), fp);
if(s == NULL)
{
break; // exit when no more to read
}
if(buf[0] == '#' || buf[0] == '\n')
{
continue; // skip comments
}
char *colon = strchr(buf, ':'); // find colon
if(colon == NULL)
{
printf("Ill formed line in config file: %s\n", buf);
continue;
}
*colon = 0; // terminate string at colon
if(match("exclude", buf))
{
int i;
char* values = colon + 1;
// get values
for(i = 0; i < MAX_EXCLUDE; ++i)
{
while(isspace(*values) || *values == ',')
{
++values;
}
if(isdigit(*values))
{
int val = get_val(values);
if(val < 0)
{
printf("Ill formed line in config file: %s\n", buf);
return;
}
exclude[i] = val;
while(isdigit(*values))
{
++values;
}
}
else if(*values == 0)
{
return; // done
}
else
{
printf("Ill formed line in config file: %s\n", buf);
return;
}
}
}
}
}
//-----------------------------------------------------------------------------
void read_cfg(char* name)
{
memset(exclude, 0, sizeof(exclude));
fp = fopen(name, "r");
if(fp != NULL)
{
temp_avg_ceiling = read_param("temp_avg_ceiling", 0, 90, 50);
temp_avg_floor = read_param("temp_avg_floor", 0, temp_avg_ceiling - 1, 40);
temp_TC0P_ceiling = read_param("temp_TC0P_ceiling", 0, 90, 65);
temp_TC0P_floor = read_param("temp_TC0P_floor", 0, temp_TC0P_ceiling - 1, 50);
temp_TG0P_ceiling = read_param("temp_TG0P_ceiling", 0, 90, 80);
temp_TG0P_floor = read_param("temp_TG0P_floor", 0, temp_TG0P_ceiling - 1, 65);
fan_min = read_param("fan_min", 0, 6200, 0);
log_level = read_param("log_level", 0, 2, 0);
read_exclude_list();
fclose(fp);
}
else
{
printf("Could not open config file %s\n", name);
}
printf("Using parameters:\n");
printf("\ttemp_avg_floor: %.0f\n", temp_avg_floor);
printf("\ttemp_avg_ceiling: %.0f\n", temp_avg_ceiling);
printf("\ttemp_TC0P_floor: %.0f\n", temp_TC0P_floor);
printf("\ttemp_TC0P_ceiling: %.0f\n", temp_TC0P_ceiling);
printf("\ttemp_TG0P_floor: %.0f\n", temp_TG0P_floor);
printf("\ttemp_TG0P_ceiling: %.0f\n", temp_TG0P_ceiling);
printf("\tfan_min: %.0f\n", fan_min);
if(exclude[0] != 0)
{
int i;
printf("\texclude: ");
for(i = 0; i < MAX_EXCLUDE && exclude[i] != 0; ++i)
{
printf("temp%d_input ", exclude[i]);
}
printf("\n");
}
printf("\tlog_level: %d\n", log_level);
}
//-----------------------------------------------------------------------------