blob: c5aa3e64719400d7c4d21aeaa678894a29da7198 [file] [log] [blame]
Steve Kondik75956202016-08-07 23:21:29 -07001/*
2 * Copyright (C) 2016 The CyanogenMod Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <stdbool.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <time.h>
26#include <unistd.h>
27
28#include <cutils/android_reboot.h>
29#include <cutils/klog.h>
30#include <cutils/misc.h>
31#include <cutils/uevent.h>
32#include <cutils/properties.h>
33
34#include <pthread.h>
35#include <linux/android_alarm.h>
36#include <sys/timerfd.h>
37#include <linux/rtc.h>
38
39#include "healthd.h"
40#include "minui/minui.h"
41
42#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
43#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
44#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
45#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
46
47struct frame {
48 int min_capacity;
49 GRSurface *surface;
50};
51
52struct animation {
53 struct frame *frames;
54 int cur_frame;
55 int num_frames;
56};
57
58static struct animation anim = {
59 .frames = NULL,
60 .cur_frame = 0,
61 .num_frames = 0,
62};
63
64static bool font_inited;
65
66static int draw_surface_centered(GRSurface* surface)
67{
68 int w, h, x, y;
69
70 w = gr_get_width(surface);
71 h = gr_get_height(surface);
72 x = (gr_fb_width() - w) / 2 ;
73 y = (gr_fb_height() - h) / 2 ;
74
75 gr_blit(surface, 0, 0, w, h, x, y);
76 return y + h;
77}
78
79#define STR_LEN 64
80static void draw_capacity(int capacity)
81{
82 struct frame *f = &anim.frames[0];
83 int w = gr_get_width(f->surface);
84 int h = gr_get_height(f->surface);
85 int x = (gr_fb_width() - w) / 2 ;
86 int y = (gr_fb_height() + h) / 2;
87
88 char cap_str[STR_LEN];
89 snprintf(cap_str, (STR_LEN - 1), "%d%%", capacity);
90 gr_color(255, 255, 255, 255);
91 gr_text(x, y * 1.05, cap_str, 0);
92}
93
94#ifdef QCOM_HARDWARE
95enum alarm_time_type {
96 ALARM_TIME,
97 RTC_TIME,
98};
99
100/*
101 * shouldn't be changed after
102 * reading from alarm register
103 */
104static time_t alm_secs;
105
106static int alarm_get_time(enum alarm_time_type time_type,
107 time_t *secs)
108{
109 struct tm tm;
110 unsigned int cmd;
111 int rc, fd = -1;
112
113 if (!secs)
114 return -1;
115
116 fd = open("/dev/rtc0", O_RDONLY);
117 if (fd < 0) {
118 LOGE("Can't open rtc devfs node\n");
119 return -1;
120 }
121
122 switch (time_type) {
123 case ALARM_TIME:
124 cmd = RTC_ALM_READ;
125 break;
126 case RTC_TIME:
127 cmd = RTC_RD_TIME;
128 break;
129 default:
130 LOGE("Invalid time type\n");
131 goto err;
132 }
133
134 rc = ioctl(fd, cmd, &tm);
135 if (rc < 0) {
136 LOGE("Unable to get time\n");
137 goto err;
138 }
139
140 *secs = mktime(&tm) + tm.tm_gmtoff;
141 if (*secs < 0) {
142 LOGE("Invalid seconds = %ld\n", *secs);
143 goto err;
144 }
145
146 close(fd);
147 return 0;
148
149err:
150 close(fd);
151 return -1;
152}
153
154#define ERR_SECS 2
155static int alarm_is_alm_expired()
156{
157 int rc;
158 time_t rtc_secs;
159
160 rc = alarm_get_time(RTC_TIME, &rtc_secs);
161 if (rc < 0)
162 return 0;
163
164 return (alm_secs >= rtc_secs - ERR_SECS &&
165 alm_secs <= rtc_secs + ERR_SECS) ? 1 : 0;
166}
167
168static int timerfd_set_reboot_time_and_wait(time_t secs)
169{
170 int fd;
171 int ret = -1;
172 fd = timerfd_create(CLOCK_REALTIME_ALARM, 0);
173 if (fd < 0) {
174 LOGE("Can't open timerfd alarm node\n");
175 goto err_return;
176 }
177
178 struct itimerspec spec;
179 memset(&spec, 0, sizeof(spec));
180 spec.it_value.tv_sec = secs;
181
182 if (timerfd_settime(fd, 0 /* relative */, &spec, NULL)) {
183 LOGE("Can't set timerfd alarm\n");
184 goto err_close;
185 }
186
187 uint64_t unused;
188 if (read(fd, &unused, sizeof(unused)) < 0) {
189 LOGE("Wait alarm error\n");
190 goto err_close;
191 }
192
193 ret = 0;
194err_close:
195 close(fd);
196err_return:
197 return ret;
198}
199
200static int alarm_set_reboot_time_and_wait(time_t secs)
201{
202 int rc, fd;
203 struct timespec ts;
204
205 fd = open("/dev/alarm", O_RDWR);
206 if (fd < 0) {
207 LOGE("Can't open alarm devfs node, trying timerfd\n");
208 return timerfd_set_reboot_time_and_wait(secs);
209 }
210
211 /* get the elapsed realtime from boot time to now */
212 rc = ioctl(fd, ANDROID_ALARM_GET_TIME(
213 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts);
214 if (rc < 0) {
215 LOGE("Unable to get elapsed realtime\n");
216 goto err;
217 }
218
219 /* calculate the elapsed time from boot time to reboot time */
220 ts.tv_sec += secs;
221 ts.tv_nsec = 0;
222
223 rc = ioctl(fd, ANDROID_ALARM_SET(
224 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts);
225 if (rc < 0) {
226 LOGE("Unable to set reboot time to %ld\n", secs);
227 goto err;
228 }
229
230 do {
231 rc = ioctl(fd, ANDROID_ALARM_WAIT);
232 } while ((rc < 0 && errno == EINTR) || !alarm_is_alm_expired());
233
234 if (rc <= 0) {
235 LOGE("Unable to wait on alarm\n");
236 goto err;
237 }
238
239 close(fd);
240 return 0;
241
242err:
243 if (fd >= 0)
244 close(fd);
245 return -1;
246}
247
248static void *alarm_thread(void *)
249{
250 time_t rtc_secs, rb_secs;
251 int rc;
252
253 /*
254 * to support power off alarm, the time
255 * stored in alarm register at latest
256 * shutdown time should be some time
257 * earlier than the actual alarm time
258 * set by user
259 */
260 rc = alarm_get_time(ALARM_TIME, &alm_secs);
261 if (rc < 0 || !alm_secs)
262 goto err;
263
264 rc = alarm_get_time(RTC_TIME, &rtc_secs);
265 if (rc < 0)
266 goto err;
267
268 /*
269 * calculate the reboot time after which
270 * the phone will reboot
271 */
272 rb_secs = alm_secs - rtc_secs;
273 if (rb_secs <= 0)
274 goto err;
275
276 rc = alarm_set_reboot_time_and_wait(rb_secs);
277 if (rc < 0)
278 goto err;
279
280 LOGI("Exit from power off charging, reboot the phone!\n");
281 android_reboot(ANDROID_RB_RESTART, 0, 0);
282
283err:
284 LOGE("Exit from alarm thread\n");
285 return NULL;
286}
287#endif
288
289void healthd_board_init(struct healthd_config*)
290{
291 pthread_t tid;
292 char value[PROP_VALUE_MAX];
293 int rc = 0, scale_count = 0, i;
294 GRSurface **scale_frames;
295
296 rc = res_create_multi_display_surface("charger/cm_battery_scale",
297 &scale_count, &scale_frames);
298 if (rc < 0) {
299 LOGE("%s: Unable to load battery scale image", __func__);
300 return;
301 }
302
303 anim.frames = new frame[scale_count];
304 anim.num_frames = scale_count;
305 for (i = 0; i < anim.num_frames; i++) {
306 anim.frames[i].surface = scale_frames[i];
307 anim.frames[i].min_capacity = 100/(scale_count-1) * i;
308 }
309
310#ifdef QCOM_HARDWARE
311 property_get("ro.bootmode", value, "");
312 if (!strcmp("charger", value)) {
313 rc = pthread_create(&tid, NULL, alarm_thread, NULL);
314 if (rc < 0)
315 LOGE("Create alarm thread failed\n");
316 }
317#endif
318}
319
320int healthd_board_battery_update(struct android::BatteryProperties*)
321{
322 // return 0 to log periodic polled battery status to kernel log
323 return 1;
324}
325
326void healthd_board_mode_charger_draw_battery(
327 struct android::BatteryProperties *batt_prop)
328{
329 int start_frame = 0;
330 int capacity = -1;
331
332 if (!font_inited) {
333 gr_set_font("log");
334 font_inited = true;
335 }
336
337 if (batt_prop && batt_prop->batteryLevel >= 0) {
338 capacity = batt_prop->batteryLevel;
339 }
340
341 if (anim.num_frames == 0 || capacity < 0) {
342 LOGE("%s: Unable to draw battery", __func__);
343 return;
344 }
345
346 // Find starting frame to display based on current capacity
347 for (start_frame = 1; start_frame < anim.num_frames; start_frame++) {
348 if (capacity < anim.frames[start_frame].min_capacity)
349 break;
350 }
351 // Always start from the level just below the current capacity
352 start_frame--;
353
354 if (anim.cur_frame < start_frame)
355 anim.cur_frame = start_frame;
356
357 draw_surface_centered(anim.frames[anim.cur_frame].surface);
358 draw_capacity(capacity);
359 // Move to next frame, with max possible frame at max_idx
360 anim.cur_frame = ((anim.cur_frame + 1) % anim.num_frames);
361}
362
363void healthd_board_mode_charger_battery_update(
364 struct android::BatteryProperties*)
365{
366}
367
368void healthd_board_mode_charger_set_backlight(bool)
369{
370}
371
372void healthd_board_mode_charger_init(void)
373{
374}