blob: c2f42eaab747852d04d7c1cc647fba2efeba7a22 [file] [log] [blame]
Steve Kondik75956202016-08-07 23:21:29 -07001/*
2 * Copyright (C) 2016 The CyanogenMod Project
Zhao Wei Liewc8537522017-02-05 12:52:35 +08003 * 2017 The LineageOS Project
Steve Kondik75956202016-08-07 23:21:29 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <errno.h>
19#include <fcntl.h>
20#include <stdbool.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <time.h>
27#include <unistd.h>
28
29#include <cutils/android_reboot.h>
30#include <cutils/klog.h>
31#include <cutils/misc.h>
32#include <cutils/uevent.h>
33#include <cutils/properties.h>
34
35#include <pthread.h>
Steve Kondik75956202016-08-07 23:21:29 -070036#include <linux/rtc.h>
Luca Stefani76dd1e52017-09-08 09:13:06 -040037#include <linux/time.h>
38#include <sys/epoll.h>
39#include <sys/timerfd.h>
Steve Kondik75956202016-08-07 23:21:29 -070040
Luca Stefani7d2b6d22016-08-25 14:41:17 +020041#include "healthd/healthd.h"
Steve Kondik75956202016-08-07 23:21:29 -070042#include "minui/minui.h"
43
44#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
45#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
46#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
47#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
48
Michael Wea82b9f2017-07-22 21:53:47 +020049static const GRFont* gr_font = NULL;
50
Steve Kondik75956202016-08-07 23:21:29 -070051struct frame {
52 int min_capacity;
53 GRSurface *surface;
54};
55
56struct animation {
57 struct frame *frames;
58 int cur_frame;
59 int num_frames;
60};
61
62static struct animation anim = {
63 .frames = NULL,
64 .cur_frame = 0,
65 .num_frames = 0,
66};
67
Michael Wea82b9f2017-07-22 21:53:47 +020068static const GRFont* get_font()
69{
70 return gr_font;
71}
72
Steve Kondik75956202016-08-07 23:21:29 -070073static int draw_surface_centered(GRSurface* surface)
74{
75 int w, h, x, y;
76
77 w = gr_get_width(surface);
78 h = gr_get_height(surface);
79 x = (gr_fb_width() - w) / 2 ;
80 y = (gr_fb_height() - h) / 2 ;
81
82 gr_blit(surface, 0, 0, w, h, x, y);
83 return y + h;
84}
85
86#define STR_LEN 64
87static void draw_capacity(int capacity)
88{
Steve Kondik75956202016-08-07 23:21:29 -070089 char cap_str[STR_LEN];
90 snprintf(cap_str, (STR_LEN - 1), "%d%%", capacity);
Michael W4cc370e2016-08-16 13:06:45 +020091
92 struct frame *f = &anim.frames[0];
93 int font_x, font_y;
Michael Wea82b9f2017-07-22 21:53:47 +020094 gr_font_size(get_font(), &font_x, &font_y);
95 int w = gr_measure(get_font(), cap_str);
Michael W4cc370e2016-08-16 13:06:45 +020096 int h = gr_get_height(f->surface);
97 int x = (gr_fb_width() - w) / 2;
98 int y = (gr_fb_height() + h) / 2;
99
Steve Kondik75956202016-08-07 23:21:29 -0700100 gr_color(255, 255, 255, 255);
Michael Wea82b9f2017-07-22 21:53:47 +0200101 gr_text(get_font(), x, y + font_y / 2, cap_str, 0);
Steve Kondik75956202016-08-07 23:21:29 -0700102}
103
104#ifdef QCOM_HARDWARE
105enum alarm_time_type {
106 ALARM_TIME,
107 RTC_TIME,
108};
109
Steve Kondik75956202016-08-07 23:21:29 -0700110static int alarm_get_time(enum alarm_time_type time_type,
111 time_t *secs)
112{
113 struct tm tm;
114 unsigned int cmd;
115 int rc, fd = -1;
116
117 if (!secs)
118 return -1;
119
120 fd = open("/dev/rtc0", O_RDONLY);
121 if (fd < 0) {
122 LOGE("Can't open rtc devfs node\n");
123 return -1;
124 }
125
126 switch (time_type) {
127 case ALARM_TIME:
128 cmd = RTC_ALM_READ;
129 break;
130 case RTC_TIME:
131 cmd = RTC_RD_TIME;
132 break;
133 default:
134 LOGE("Invalid time type\n");
135 goto err;
136 }
137
138 rc = ioctl(fd, cmd, &tm);
139 if (rc < 0) {
140 LOGE("Unable to get time\n");
141 goto err;
142 }
143
144 *secs = mktime(&tm) + tm.tm_gmtoff;
145 if (*secs < 0) {
146 LOGE("Invalid seconds = %ld\n", *secs);
147 goto err;
148 }
149
150 close(fd);
151 return 0;
152
153err:
154 close(fd);
155 return -1;
156}
157
Luca Stefani76dd1e52017-09-08 09:13:06 -0400158static void alarm_reboot(void)
Steve Kondik75956202016-08-07 23:21:29 -0700159{
Luca Stefani76dd1e52017-09-08 09:13:06 -0400160 LOGI("alarm time is up, reboot the phone!\n");
161 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
162 LINUX_REBOOT_CMD_RESTART2, "rtc");
Steve Kondik75956202016-08-07 23:21:29 -0700163}
164
165static int alarm_set_reboot_time_and_wait(time_t secs)
166{
Luca Stefani76dd1e52017-09-08 09:13:06 -0400167 int rc, epollfd, nevents;
168 int fd = 0;
Steve Kondik75956202016-08-07 23:21:29 -0700169 struct timespec ts;
Luca Stefani76dd1e52017-09-08 09:13:06 -0400170 epoll_event event, events[1];
171 struct itimerspec itval;
Steve Kondik75956202016-08-07 23:21:29 -0700172
Luca Stefani76dd1e52017-09-08 09:13:06 -0400173 epollfd = epoll_create(1);
174 if (epollfd < 0) {
175 LOGE("epoll_create failed\n");
176 goto err;
177 }
178
179 fd = timerfd_create(CLOCK_REALTIME_ALARM, 0);
Steve Kondik75956202016-08-07 23:21:29 -0700180 if (fd < 0) {
Luca Stefani76dd1e52017-09-08 09:13:06 -0400181 LOGE("timerfd_create failed\n");
Steve Kondik75956202016-08-07 23:21:29 -0700182 goto err;
183 }
184
Luca Stefani76dd1e52017-09-08 09:13:06 -0400185 event.events = EPOLLIN | EPOLLWAKEUP;
186 event.data.ptr = (void *)alarm_reboot;
187 rc = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
Steve Kondik75956202016-08-07 23:21:29 -0700188 if (rc < 0) {
Luca Stefani76dd1e52017-09-08 09:13:06 -0400189 LOGE("epoll_ctl(EPOLL_CTL_ADD) failed \n");
Steve Kondik75956202016-08-07 23:21:29 -0700190 goto err;
191 }
192
Luca Stefani76dd1e52017-09-08 09:13:06 -0400193 itval.it_value.tv_sec = secs;
194 itval.it_value.tv_nsec = 0;
Steve Kondik75956202016-08-07 23:21:29 -0700195
Luca Stefani76dd1e52017-09-08 09:13:06 -0400196 itval.it_interval.tv_sec = 0;
197 itval.it_interval.tv_nsec = 0;
198
199 rc = timerfd_settime(fd, TFD_TIMER_ABSTIME, &itval, NULL);
200 if (rc < 0) {
201 LOGE("timerfd_settime failed %d\n",rc);
202 goto err;
203 }
204
205 nevents = epoll_wait(epollfd, events, 1, -1);
206
207 if (nevents <= 0) {
Steve Kondik75956202016-08-07 23:21:29 -0700208 LOGE("Unable to wait on alarm\n");
209 goto err;
Luca Stefani76dd1e52017-09-08 09:13:06 -0400210 } else {
211 (*(void (*)())events[0].data.ptr)();
Steve Kondik75956202016-08-07 23:21:29 -0700212 }
213
Luca Stefani76dd1e52017-09-08 09:13:06 -0400214 close(epollfd);
Steve Kondik75956202016-08-07 23:21:29 -0700215 close(fd);
216 return 0;
217
218err:
Luca Stefani76dd1e52017-09-08 09:13:06 -0400219 if (epollfd > 0)
220 close(epollfd);
221
Steve Kondik75956202016-08-07 23:21:29 -0700222 if (fd >= 0)
223 close(fd);
224 return -1;
225}
226
Luca Stefani76dd1e52017-09-08 09:13:06 -0400227/*
228 * 10s the estimated time from timestamp of alarm thread start
229 * to timestamp of android boot completed.
230 */
231#define TIME_DELTA 10
232
233/* seconds of 1 minute*/
234#define ONE_MINUTE 60
Steve Kondik75956202016-08-07 23:21:29 -0700235static void *alarm_thread(void *)
236{
Luca Stefani76dd1e52017-09-08 09:13:06 -0400237 time_t rtc_secs, alarm_secs;
Steve Kondik75956202016-08-07 23:21:29 -0700238 int rc;
Luca Stefani76dd1e52017-09-08 09:13:06 -0400239 timespec ts;
Steve Kondik75956202016-08-07 23:21:29 -0700240
241 /*
242 * to support power off alarm, the time
243 * stored in alarm register at latest
244 * shutdown time should be some time
245 * earlier than the actual alarm time
246 * set by user
247 */
Luca Stefani76dd1e52017-09-08 09:13:06 -0400248 rc = alarm_get_time(ALARM_TIME, &alarm_secs);
249 if (rc < 0 || !alarm_secs)
Steve Kondik75956202016-08-07 23:21:29 -0700250 goto err;
251
252 rc = alarm_get_time(RTC_TIME, &rtc_secs);
Luca Stefani76dd1e52017-09-08 09:13:06 -0400253 if (rc < 0 || !rtc_secs)
254 goto err;
255 LOGI("alarm time in rtc is %ld, rtc time is %ld\n", alarm_secs, rtc_secs);
256
257 if (alarm_secs <= rtc_secs) {
258 clock_gettime(CLOCK_BOOTTIME, &ts);
259
260 /*
261 * It is possible that last power off alarm time is up at this point.
262 * (alarm_secs + ONE_MINUTE) is the final alarm time to fire.
263 * (rtc_secs + ts.tv_sec + TIME_DELTA) is the estimated time of next
264 * boot completed to fire alarm.
265 * If the final alarm time is less than the estimated time of next boot
266 * completed to fire, that means it is not able to fire the last power
267 * off alarm at the right time, so just miss it.
268 */
269 if (alarm_secs + ONE_MINUTE < rtc_secs + ts.tv_sec + TIME_DELTA) {
270 LOGE("alarm is missed\n");
271 goto err;
272 }
273
274 alarm_reboot();
275 }
276
277 rc = alarm_set_reboot_time_and_wait(alarm_secs);
Steve Kondik75956202016-08-07 23:21:29 -0700278 if (rc < 0)
279 goto err;
280
Steve Kondik75956202016-08-07 23:21:29 -0700281err:
282 LOGE("Exit from alarm thread\n");
283 return NULL;
284}
285#endif
286
287void healthd_board_init(struct healthd_config*)
288{
289 pthread_t tid;
290 char value[PROP_VALUE_MAX];
291 int rc = 0, scale_count = 0, i;
292 GRSurface **scale_frames;
Jackeagled6811aa2019-09-24 08:26:40 +0200293 int scale_fps; // Not in use (charger/bliss_battery_scale doesn't have FPS text
Luca Stefani7d2b6d22016-08-25 14:41:17 +0200294 // chunk). We are using hard-coded frame.disp_time instead.
Steve Kondik75956202016-08-07 23:21:29 -0700295
Jackeagled6811aa2019-09-24 08:26:40 +0200296 rc = res_create_multi_display_surface("charger/bliss_battery_scale",
Zhao Wei Liewc8537522017-02-05 12:52:35 +0800297 &scale_count, &scale_fps, &scale_frames);
Steve Kondik75956202016-08-07 23:21:29 -0700298 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
Steve Kondik75956202016-08-07 23:21:29 -0700332 if (batt_prop && batt_prop->batteryLevel >= 0) {
333 capacity = batt_prop->batteryLevel;
334 }
335
336 if (anim.num_frames == 0 || capacity < 0) {
337 LOGE("%s: Unable to draw battery", __func__);
338 return;
339 }
340
341 // Find starting frame to display based on current capacity
342 for (start_frame = 1; start_frame < anim.num_frames; start_frame++) {
343 if (capacity < anim.frames[start_frame].min_capacity)
344 break;
345 }
346 // Always start from the level just below the current capacity
347 start_frame--;
348
349 if (anim.cur_frame < start_frame)
350 anim.cur_frame = start_frame;
351
352 draw_surface_centered(anim.frames[anim.cur_frame].surface);
353 draw_capacity(capacity);
354 // Move to next frame, with max possible frame at max_idx
355 anim.cur_frame = ((anim.cur_frame + 1) % anim.num_frames);
356}
357
358void healthd_board_mode_charger_battery_update(
359 struct android::BatteryProperties*)
360{
361}
362
Adrian DCcadd0be2016-08-14 11:44:31 +0200363#ifdef HEALTHD_BACKLIGHT_PATH
364#ifndef HEALTHD_BACKLIGHT_LEVEL
365#define HEALTHD_BACKLIGHT_LEVEL 100
366#endif
367
368void healthd_board_mode_charger_set_backlight(bool on)
369{
370 int fd;
371 char buffer[10];
372
373 memset(buffer, '\0', sizeof(buffer));
374 fd = open(HEALTHD_BACKLIGHT_PATH, O_RDWR);
375 if (fd < 0) {
376 LOGE("Could not open backlight node : %s\n", strerror(errno));
377 return;
378 }
379 LOGV("Enabling backlight\n");
380 snprintf(buffer, sizeof(buffer), "%d\n", on ? HEALTHD_BACKLIGHT_LEVEL : 0);
381 if (write(fd, buffer, strlen(buffer)) < 0) {
382 LOGE("Could not write to backlight : %s\n", strerror(errno));
383 }
384 close(fd);
385
386#ifdef HEALTHD_SECONDARY_BACKLIGHT_PATH
387 fd = open(HEALTHD_SECONDARY_BACKLIGHT_PATH, O_RDWR);
388 if (fd < 0) {
389 LOGE("Could not open second backlight node : %s\n", strerror(errno));
390 return;
391 }
392 LOGV("Enabling secondary backlight\n");
393 if (write(fd, buffer, strlen(buffer)) < 0) {
394 LOGE("Could not write to second backlight : %s\n", strerror(errno));
395 return;
396 }
397 close(fd);
398#endif
399}
400
401#else
Steve Kondik75956202016-08-07 23:21:29 -0700402void healthd_board_mode_charger_set_backlight(bool)
403{
404}
Adrian DCcadd0be2016-08-14 11:44:31 +0200405#endif
Steve Kondik75956202016-08-07 23:21:29 -0700406
407void healthd_board_mode_charger_init(void)
408{
Michael Wea82b9f2017-07-22 21:53:47 +0200409 GRFont* tmp_font;
410 int res = gr_init_font("font_log", &tmp_font);
411 if (res == 0) {
412 gr_font = tmp_font;
413 } else {
414 LOGW("Couldn't open font, falling back to default!\n");
415 gr_font = gr_sys_font();
416 }
417
Steve Kondik75956202016-08-07 23:21:29 -0700418}