tizen 2.3 release
[framework/system/deviced.git] / src / logd / src / battery / calibration.c
1 #include <errno.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include <vconf.h>
9
10 #include "macro.h"
11
12 static int default_timeout = 10;
13
14 struct calibration_test {
15         const char *description;
16         int (*func)(void *);
17         void *param;
18         int consumption;
19 };
20
21 static int run_test_battery(int timeout);
22 static int (*run_test)(int) = run_test_battery;
23
24 static int system_cmd(const char *cmd, char *const args[])
25 {
26         pid_t pid = fork();
27
28
29         if (pid == 0) {
30                 if (execv(cmd, args) < 0) {
31                         perror("execv: ");
32                 }
33         } else if (pid == -1) {
34                 fprintf(stderr, "Can't run \"%s\"\n", cmd);
35                 return -errno;
36         } else {
37                 wait(NULL);
38         }
39
40         return 0;
41 }
42
43 static int current_battery_level(void)
44 {
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";
47         FILE *fp = NULL;
48         int level;
49
50         fp = popen(cmd, "r");
51         if (fp == NULL) {
52                 fprintf(stderr, "Can't obtain battery level\n");
53                 return -errno;
54         }
55
56         if (fscanf(fp, "%d", &level) <= 0) {
57                 fprintf(stderr, "Can't read power supply\n");
58                 fclose(fp);
59                 return -EIO;
60         }
61
62         fclose(fp);
63
64         return level;
65 }
66
67 static int power_supply(void)
68 {
69         FILE *fp = NULL;
70         int ps;
71
72         /* TODO: move hardware depended settings to config file */
73         fp = fopen("/sys/class/power_supply/max17047-fuelgauge/current_avg", "r");
74         if (fp == NULL) {
75                 fprintf(stderr, "Can't open current_now\n");
76                 return -errno;
77         }
78
79         if (fscanf(fp, "%d", &ps) <= 0) {
80                 fprintf(stderr, "Can't read power supply\n");
81                 fclose(fp);
82                 return -EIO;
83         }
84         fclose(fp);
85
86         return -ps;
87 }
88
89 static int write_number(const char *file, int number)
90 {
91         FILE *fp = NULL;
92
93         fp = fopen(file, "w");
94         if (fp == NULL) {
95                 fprintf(stderr, "Can't open current_now\n");
96                 return -errno;
97         }
98
99         fprintf(fp, "%d", number);
100         fclose(fp);
101
102         return 0;
103 }
104
105 static int run_test_battery(int timeout)
106 {
107         int level1 = current_battery_level();
108         sleep(timeout);
109         int level2 = current_battery_level();
110
111         return level1 - level2;
112 }
113
114 static int run_test_power_supply(int timeout)
115 {
116         sleep(timeout);
117
118         return power_supply();
119 }
120
121 /* display */
122 static void set_brightness(int value)
123 {
124         write_number("/sys/class/backlight/s6d6aa1-bl/brightness", value);
125 }
126
127 static void display_on(void)
128 {
129         char cmd[] = "/usr/bin/xset";
130         char *const args[] = { cmd, "dpms", "force", "on", NULL };
131         system_cmd(cmd, args);
132 }
133
134 static void display_off(void)
135 {
136         char cmd[] = "/usr/bin/xset";
137         char *const args[] = { cmd, "dpms", "force", "off", NULL };
138         system_cmd(cmd, args);
139 }
140
141 static int run_display_test(void *brightness)
142 {
143         int consumption = 0;
144         display_on();
145         set_brightness((int)brightness);
146         consumption = run_test(default_timeout);
147         display_off();
148
149         return consumption;
150 }
151
152 /* default */
153 static int run_default_test(void *user_data)
154 {
155         display_off();
156
157         return run_test(default_timeout);
158 }
159
160 /* wi-fi */
161 static int wifi_on(void)
162 {
163         char cmd[] = "/usr/bin/wifi-qs";
164         char *const args[] = { cmd, NULL };
165         system_cmd(cmd, args);
166         sleep(10);
167
168         return 0;
169 }
170
171 static int run_wifi_test(void *user_data)
172 {
173         if (wifi_on() < 0)
174                 return -1;
175
176         return run_test(default_timeout);
177 }
178
179 /* gps */
180 static int run_gps_test(void *user_data)
181 {
182         vconf_set_int(VCONFKEY_LOCATION_ENABLED, 1);
183         int consumption = run_test(default_timeout);
184         vconf_set_int(VCONFKEY_LOCATION_ENABLED, 0);
185         return consumption;
186 }
187
188 int main(int argc, char *argv[])
189 {
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},
200         };
201
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;
206                 }
207         }
208         signal(SIGHUP, SIG_IGN);
209         printf("timeout = %d\n", default_timeout);
210
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);
216                 } else {
217                         fprintf(stderr, "Can't run \"%s\" test\n", tests[i].description);
218                 }
219         }
220
221         return 0;
222 }