1/*
2 * Copyright (C) 2013 The Android Open Source 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#define LOG_TAG "healthd-android"
18
19#include "healthd.h"
20#include "BatteryPropertiesRegistrar.h"
21
22#include <binder/IPCThreadState.h>
23#include <binder/ProcessState.h>
24#include <cutils/klog.h>
25#include <sys/epoll.h>
26
27using namespace android;
28
29static int gBinderFd;
30static sp<BatteryPropertiesRegistrar> gBatteryPropertiesRegistrar;
31
32void healthd_mode_android_battery_update(
33    struct android::BatteryProperties *props) {
34    if (gBatteryPropertiesRegistrar != NULL)
35        gBatteryPropertiesRegistrar->notifyListeners(*props);
36
37    return;
38}
39
40int healthd_mode_android_preparetowait(void) {
41    IPCThreadState::self()->flushCommands();
42    return -1;
43}
44
45static void binder_event(uint32_t /*epevents*/) {
46    IPCThreadState::self()->handlePolledCommands();
47}
48
49void healthd_mode_android_init(struct healthd_config* /*config*/) {
50    ProcessState::self()->setThreadPoolMaxThreadCount(0);
51    IPCThreadState::self()->disableBackgroundScheduling(true);
52    IPCThreadState::self()->setupPolling(&gBinderFd);
53
54    if (gBinderFd >= 0) {
55        if (healthd_register_event(gBinderFd, binder_event))
56            KLOG_ERROR(LOG_TAG,
57                       "Register for binder events failed\n");
58    }
59
60    gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
61    gBatteryPropertiesRegistrar->publish();
62}
63