12 static int default_timeout = 10;
14 struct calibration_test {
15 const char *description;
21 static int run_test_battery(int timeout);
22 static int (*run_test)(int) = run_test_battery;
24 static int system_cmd(const char *cmd, char *const args[])
30 if (execv(cmd, args) < 0) {
33 } else if (pid == -1) {
34 fprintf(stderr, "Can't run \"%s\"\n", cmd);
43 static int current_battery_level(void)
45 const char *cmd = "/bin/cat /sys/class/power_supply/battery/uevent |\
46 /bin/grep POWER_SUPPLY_CAPACITY_RAW | /usr/bin/cut -d '=' -f 2";
52 fprintf(stderr, "Can't obtain battery level\n");
56 if (fscanf(fp, "%d", &level) <= 0) {
57 fprintf(stderr, "Can't read power supply\n");
67 static int power_supply(void)
72 /* TODO: move hardware depended settings to config file */
73 fp = fopen("/sys/class/power_supply/max17047-fuelgauge/current_avg", "r");
75 fprintf(stderr, "Can't open current_now\n");
79 if (fscanf(fp, "%d", &ps) <= 0) {
80 fprintf(stderr, "Can't read power supply\n");
89 static int write_number(const char *file, int number)
93 fp = fopen(file, "w");
95 fprintf(stderr, "Can't open current_now\n");
99 fprintf(fp, "%d", number);
105 static int run_test_battery(int timeout)
107 int level1 = current_battery_level();
109 int level2 = current_battery_level();
111 return level1 - level2;
114 static int run_test_power_supply(int timeout)
118 return power_supply();
122 static void set_brightness(int value)
124 write_number("/sys/class/backlight/s6d6aa1-bl/brightness", value);
127 static void display_on(void)
129 char cmd[] = "/usr/bin/xset";
130 char *const args[] = { cmd, "dpms", "force", "on", NULL };
131 system_cmd(cmd, args);
134 static void display_off(void)
136 char cmd[] = "/usr/bin/xset";
137 char *const args[] = { cmd, "dpms", "force", "off", NULL };
138 system_cmd(cmd, args);
141 static int run_display_test(void *brightness)
145 set_brightness((int)brightness);
146 consumption = run_test(default_timeout);
153 static int run_default_test(void *user_data)
157 return run_test(default_timeout);
161 static int wifi_on(void)
163 char cmd[] = "/usr/bin/wifi-qs";
164 char *const args[] = { cmd, NULL };
165 system_cmd(cmd, args);
171 static int run_wifi_test(void *user_data)
176 return run_test(default_timeout);
180 static int run_gps_test(void *user_data)
182 vconf_set_int(VCONFKEY_LOCATION_ENABLED, 1);
183 int consumption = run_test(default_timeout);
184 vconf_set_int(VCONFKEY_LOCATION_ENABLED, 0);
188 int main(int argc, char *argv[])
190 struct calibration_test tests[] = {
191 {"default", run_default_test, NULL, 0},
192 {"display 1", run_display_test, (void*)1, 0},
193 {"display 20", run_display_test, (void*)20, 0},
194 {"display 40", run_display_test, (void*)40, 0},
195 {"display 60", run_display_test, (void*)60, 0},
196 {"display 80", run_display_test, (void*)80, 0},
197 {"display 100", run_display_test, (void*)100, 0},
198 {"gps", run_gps_test, NULL, 0},
199 {"wifi", run_wifi_test, NULL, 0},
202 for (int i = 1; i < argc; ++i) {
203 if (strcmp(argv[i], "-p") == 0) {
204 run_test = run_test_power_supply;
205 default_timeout = 30;
208 signal(SIGHUP, SIG_IGN);
209 printf("timeout = %d\n", default_timeout);
211 for (size_t i = 0; i < ARRAY_SIZE(tests); ++i) {
212 tests[i].consumption = tests[i].func(tests[i].param);
213 if (tests[i].consumption >= 0) {
214 printf("%s: %d %.2f\n", tests[i].description, tests[i].consumption,
215 (float)tests[i].consumption / tests[0].consumption);
217 fprintf(stderr, "Can't run \"%s\" test\n", tests[i].description);