Debian Codegraph @

config.c

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