blob: d82172a449f9e395a85d7dc0e16e85a0cc60ceca [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
Luca Stefani7d2b6d22016-08-25 14:41:17 +020039#include "healthd/healthd.h"
Steve Kondik75956202016-08-07 23:21:29 -070040#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{
Steve Kondik75956202016-08-07 23:21:29 -070082 char cap_str[STR_LEN];
83 snprintf(cap_str, (STR_LEN - 1), "%d%%", capacity);
Michael W4cc370e2016-08-16 13:06:45 +020084
85 struct frame *f = &anim.frames[0];
86 int font_x, font_y;
87 gr_font_size(&font_x, &font_y);
88 int w = gr_measure(cap_str);
89 int h = gr_get_height(f->surface);
90 int x = (gr_fb_width() - w) / 2;
91 int y = (gr_fb_height() + h) / 2;
92
Steve Kondik75956202016-08-07 23:21:29 -070093 gr_color(255, 255, 255, 255);
Michael W4cc370e2016-08-16 13:06:45 +020094 gr_text(x, y + font_y / 2, cap_str, 0);
Steve Kondik75956202016-08-07 23:21:29 -070095}
96
97#ifdef QCOM_HARDWARE
98enum alarm_time_type {
99 ALARM_TIME,
100 RTC_TIME,
101};
102
103/*
104 * shouldn't be changed after
105 * reading from alarm register
106 */
107static time_t alm_secs;
108
109static int alarm_get_time(enum alarm_time_type time_type,
110 time_t *secs)
111{
112 struct tm tm;
113 unsigned int cmd;
114 int rc, fd = -1;
115
116 if (!secs)
117 return -1;
118
119 fd = open("/dev/rtc0", O_RDONLY);
120 if (fd < 0) {
121 LOGE("Can't open rtc devfs node\n");
122 return -1;
123 }
124
125 switch (time_type) {
126 case ALARM_TIME:
127 cmd = RTC_ALM_READ;
128 break;
129 case RTC_TIME:
130 cmd = RTC_RD_TIME;
131 break;
132 default:
133 LOGE("Invalid time type\n");
134 goto err;
135 }
136
137 rc = ioctl(fd, cmd, &tm);
138 if (rc < 0) {
139 LOGE("Unable to get time\n");
140 goto err;
141 }
142
143 *secs = mktime(&tm) + tm.tm_gmtoff;
144 if (*secs < 0) {
145 LOGE("Invalid seconds = %ld\n", *secs);
146 goto err;
147 }
148
149 close(fd);
150 return 0;
151
152err:
153 close(fd);
154 return -1;
155}
156
157#define ERR_SECS 2
158static int alarm_is_alm_expired()
159{
160 int rc;
161 time_t rtc_secs;
162
163 rc = alarm_get_time(RTC_TIME, &rtc_secs);
164 if (rc < 0)
165 return 0;
166
167 return (alm_secs >= rtc_secs - ERR_SECS &&
168 alm_secs <= rtc_secs + ERR_SECS) ? 1 : 0;
169}
170
171static int timerfd_set_reboot_time_and_wait(time_t secs)
172{
173 int fd;
174 int ret = -1;
175 fd = timerfd_create(CLOCK_REALTIME_ALARM, 0);
176 if (fd < 0) {
177 LOGE("Can't open timerfd alarm node\n");
178 goto err_return;
179 }
180
181 struct itimerspec spec;
182 memset(&spec, 0, sizeof(spec));
183 spec.it_value.tv_sec = secs;
184
185 if (timerfd_settime(fd, 0 /* relative */, &spec, NULL)) {
186 LOGE("Can't set timerfd alarm\n");
187 goto err_close;
188 }
189
190 uint64_t unused;
191 if (read(fd, &unused, sizeof(unused)) < 0) {
192 LOGE("Wait alarm error\n");
193 goto err_close;
194 }
195
196 ret = 0;
197err_close:
198 close(fd);
199err_return:
200 return ret;
201}
202
203static int alarm_set_reboot_time_and_wait(time_t secs)
204{
205 int rc, fd;
206 struct timespec ts;
207
208 fd = open("/dev/alarm", O_RDWR);
209 if (fd < 0) {
210 LOGE("Can't open alarm devfs node, trying timerfd\n");
211 return timerfd_set_reboot_time_and_wait(secs);
212 }
213
214 /* get the elapsed realtime from boot time to now */
215 rc = ioctl(fd, ANDROID_ALARM_GET_TIME(
216 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts);
217 if (rc < 0) {
218 LOGE("Unable to get elapsed realtime\n");
219 goto err;
220 }
221
222 /* calculate the elapsed time from boot time to reboot time */
223 ts.tv_sec += secs;
224 ts.tv_nsec = 0;
225
226 rc = ioctl(fd, ANDROID_ALARM_SET(
227 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP), &ts);
228 if (rc < 0) {
229 LOGE("Unable to set reboot time to %ld\n", secs);
230 goto err;
231 }
232
233 do {
234 rc = ioctl(fd, ANDROID_ALARM_WAIT);
235 } while ((rc < 0 && errno == EINTR) || !alarm_is_alm_expired());
236
237 if (rc <= 0) {
238 LOGE("Unable to wait on alarm\n");
239 goto err;
240 }
241
242 close(fd);
243 return 0;
244
245err:
246 if (fd >= 0)
247 close(fd);
248 return -1;
249}
250
251static void *alarm_thread(void *)
252{
253 time_t rtc_secs, rb_secs;
254 int rc;
255
256 /*
257 * to support power off alarm, the time
258 * stored in alarm register at latest
259 * shutdown time should be some time
260 * earlier than the actual alarm time
261 * set by user
262 */
263 rc = alarm_get_time(ALARM_TIME, &alm_secs);
Adrian DCe91605c2016-08-14 11:40:05 +0200264 LOGI("RTC Alarm %ld\n", alm_secs);
Steve Kondik75956202016-08-07 23:21:29 -0700265 if (rc < 0 || !alm_secs)
266 goto err;
267
268 rc = alarm_get_time(RTC_TIME, &rtc_secs);
Adrian DCe91605c2016-08-14 11:40:05 +0200269 LOGI("RTC Clock %ld\n", rtc_secs);
Steve Kondik75956202016-08-07 23:21:29 -0700270 if (rc < 0)
271 goto err;
272
273 /*
274 * calculate the reboot time after which
275 * the phone will reboot
276 */
277 rb_secs = alm_secs - rtc_secs;
278 if (rb_secs <= 0)
279 goto err;
280
281 rc = alarm_set_reboot_time_and_wait(rb_secs);
282 if (rc < 0)
283 goto err;
284
285 LOGI("Exit from power off charging, reboot the phone!\n");
286 android_reboot(ANDROID_RB_RESTART, 0, 0);
287
288err:
289 LOGE("Exit from alarm thread\n");
290 return NULL;
291}
292#endif
293
294void healthd_board_init(struct healthd_config*)
295{
296 pthread_t tid;
297 char value[PROP_VALUE_MAX];
298 int rc = 0, scale_count = 0, i;
299 GRSurface **scale_frames;
Luca Stefani7d2b6d22016-08-25 14:41:17 +0200300 int scale_fps; // Not in use (charger/cm_battery_scale doesn't have FPS text
301 // chunk). We are using hard-coded frame.disp_time instead.
Steve Kondik75956202016-08-07 23:21:29 -0700302
303 rc = res_create_multi_display_surface("charger/cm_battery_scale",
Luca Stefani7d2b6d22016-08-25 14:41:17 +0200304 &scale_fps, &scale_count, &scale_frames);
Steve Kondik75956202016-08-07 23:21:29 -0700305 if (rc < 0) {
306 LOGE("%s: Unable to load battery scale image", __func__);
307 return;
308 }
309
310 anim.frames = new frame[scale_count];
311 anim.num_frames = scale_count;
312 for (i = 0; i < anim.num_frames; i++) {
313 anim.frames[i].surface = scale_frames[i];
314 anim.frames[i].min_capacity = 100/(scale_count-1) * i;
315 }
316
317#ifdef QCOM_HARDWARE
318 property_get("ro.bootmode", value, "");
319 if (!strcmp("charger", value)) {
320 rc = pthread_create(&tid, NULL, alarm_thread, NULL);
321 if (rc < 0)
322 LOGE("Create alarm thread failed\n");
323 }
324#endif
325}
326
327int healthd_board_battery_update(struct android::BatteryProperties*)
328{
329 // return 0 to log periodic polled battery status to kernel log
330 return 1;
331}
332
333void healthd_board_mode_charger_draw_battery(
334 struct android::BatteryProperties *batt_prop)
335{
336 int start_frame = 0;
337 int capacity = -1;
338
339 if (!font_inited) {
340 gr_set_font("log");
341 font_inited = true;
342 }
343
344 if (batt_prop && batt_prop->batteryLevel >= 0) {
345 capacity = batt_prop->batteryLevel;
346 }
347
348 if (anim.num_frames == 0 || capacity < 0) {
349 LOGE("%s: Unable to draw battery", __func__);
350 return;
351 }
352
353 // Find starting frame to display based on current capacity
354 for (start_frame = 1; start_frame < anim.num_frames; start_frame++) {
355 if (capacity < anim.frames[start_frame].min_capacity)
356 break;
357 }
358 // Always start from the level just below the current capacity
359 start_frame--;
360
361 if (anim.cur_frame < start_frame)
362 anim.cur_frame = start_frame;
363
364 draw_surface_centered(anim.frames[anim.cur_frame].surface);
365 draw_capacity(capacity);
366 // Move to next frame, with max possible frame at max_idx
367 anim.cur_frame = ((anim.cur_frame + 1) % anim.num_frames);
368}
369
370void healthd_board_mode_charger_battery_update(
371 struct android::BatteryProperties*)
372{
373}
374
Adrian DCcadd0be2016-08-14 11:44:31 +0200375#ifdef HEALTHD_BACKLIGHT_PATH
376#ifndef HEALTHD_BACKLIGHT_LEVEL
377#define HEALTHD_BACKLIGHT_LEVEL 100
378#endif
379
380void healthd_board_mode_charger_set_backlight(bool on)
381{
382 int fd;
383 char buffer[10];
384
385 memset(buffer, '\0', sizeof(buffer));
386 fd = open(HEALTHD_BACKLIGHT_PATH, O_RDWR);
387 if (fd < 0) {
388 LOGE("Could not open backlight node : %s\n", strerror(errno));
389 return;
390 }
391 LOGV("Enabling backlight\n");
392 snprintf(buffer, sizeof(buffer), "%d\n", on ? HEALTHD_BACKLIGHT_LEVEL : 0);
393 if (write(fd, buffer, strlen(buffer)) < 0) {
394 LOGE("Could not write to backlight : %s\n", strerror(errno));
395 }
396 close(fd);
397
398#ifdef HEALTHD_SECONDARY_BACKLIGHT_PATH
399 fd = open(HEALTHD_SECONDARY_BACKLIGHT_PATH, O_RDWR);
400 if (fd < 0) {
401 LOGE("Could not open second backlight node : %s\n", strerror(errno));
402 return;
403 }
404 LOGV("Enabling secondary backlight\n");
405 if (write(fd, buffer, strlen(buffer)) < 0) {
406 LOGE("Could not write to second backlight : %s\n", strerror(errno));
407 return;
408 }
409 close(fd);
410#endif
411}
412
413#else
Steve Kondik75956202016-08-07 23:21:29 -0700414void healthd_board_mode_charger_set_backlight(bool)
415{
416}
Adrian DCcadd0be2016-08-14 11:44:31 +0200417#endif
Steve Kondik75956202016-08-07 23:21:29 -0700418
419void healthd_board_mode_charger_init(void)
420{
421}