Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 287c41ff authored by Yifan Hong's avatar Yifan Hong
Browse files

Remove unused files in healthd.

Test: mma
Change-Id: I19c13aa3f1fdecd3f7072dccfe9dbd580f09e22a
parent 12df1b95
Loading
Loading
Loading
Loading
+0 −118
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "BatteryPropertiesRegistrar.h"
#include <batteryservice/BatteryService.h>
#include <batteryservice/IBatteryPropertiesListener.h>
#include <batteryservice/IBatteryPropertiesRegistrar.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/PermissionCache.h>
#include <private/android_filesystem_config.h>
#include <utils/Errors.h>
#include <utils/Mutex.h>
#include <utils/String16.h>

#include <healthd/healthd.h>

namespace android {

void BatteryPropertiesRegistrar::publish(
    const sp<BatteryPropertiesRegistrar>& service) {
    defaultServiceManager()->addService(String16("batteryproperties"), service);
}

void BatteryPropertiesRegistrar::notifyListeners(const struct BatteryProperties& props) {
    Vector<sp<IBatteryPropertiesListener> > listenersCopy;

    // Binder currently may service an incoming oneway transaction whenever an
    // outbound oneway call is made (if there is already a pending incoming
    // oneway call waiting).  This is considered a bug and may change in the
    // future.  For now, avoid recursive mutex lock while making outbound
    // calls by making a local copy of the current list of listeners.
    {
        Mutex::Autolock _l(mRegistrationLock);
        listenersCopy = mListeners;
    }
    for (size_t i = 0; i < listenersCopy.size(); i++) {
        listenersCopy[i]->batteryPropertiesChanged(props);
    }
}

void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
    {
        if (listener == NULL)
            return;
        Mutex::Autolock _l(mRegistrationLock);
        // check whether this is a duplicate
        for (size_t i = 0; i < mListeners.size(); i++) {
            if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
                return;
            }
        }

        mListeners.add(listener);
        IInterface::asBinder(listener)->linkToDeath(this);
    }
    healthd_battery_update();
}

void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
    if (listener == NULL)
        return;
    Mutex::Autolock _l(mRegistrationLock);
    for (size_t i = 0; i < mListeners.size(); i++) {
        if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
            IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
            mListeners.removeAt(i);
            break;
        }
    }
}

status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {
    return healthd_get_property(id, val);
}

void BatteryPropertiesRegistrar::scheduleUpdate() {
    healthd_battery_update();
}

status_t BatteryPropertiesRegistrar::dump(int fd, const Vector<String16>& /*args*/) {
    IPCThreadState* self = IPCThreadState::self();
    const int pid = self->getCallingPid();
    const int uid = self->getCallingUid();
    if ((uid != AID_SHELL) &&
        !PermissionCache::checkPermission(
                String16("android.permission.DUMP"), pid, uid))
        return PERMISSION_DENIED;

    healthd_dump_battery_state(fd);
    return OK;
}

void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
    Mutex::Autolock _l(mRegistrationLock);

    for (size_t i = 0; i < mListeners.size(); i++) {
        if (IInterface::asBinder(mListeners[i]) == who) {
            mListeners.removeAt(i);
            break;
        }
    }
}

}  // namespace android
+0 −50
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
#define HEALTHD_BATTERYPROPERTIES_REGISTRAR_H

#include <binder/IBinder.h>
#include <utils/Mutex.h>
#include <utils/String16.h>
#include <utils/Vector.h>
#include <batteryservice/BatteryService.h>
#include <batteryservice/IBatteryPropertiesListener.h>
#include <batteryservice/IBatteryPropertiesRegistrar.h>

namespace android {

class BatteryPropertiesRegistrar : public BnBatteryPropertiesRegistrar,
                                   public IBinder::DeathRecipient {
public:
    void publish(const sp<BatteryPropertiesRegistrar>& service);
    void notifyListeners(const struct BatteryProperties& props);
    void scheduleUpdate();

private:
    Mutex mRegistrationLock;
    Vector<sp<IBatteryPropertiesListener> > mListeners;

    void registerListener(const sp<IBatteryPropertiesListener>& listener);
    void unregisterListener(const sp<IBatteryPropertiesListener>& listener);
    status_t getProperty(int id, struct BatteryProperty *val);
    status_t dump(int fd, const Vector<String16>& args);
    void binderDied(const wp<IBinder>& who);
};

};  // namespace android

#endif // HEALTHD_BATTERYPROPERTIES_REGISTRAR_H

healthd/healthd.cpp

deleted100644 → 0
+0 −123
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "healthd"
#define KLOG_LEVEL 6

#include <healthd/healthd.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <cutils/klog.h>

#include <android/hardware/health/1.0/IHealth.h>
#include <android/hardware/health/1.0/types.h>
#include <hal_conversion.h>

using namespace android;

using IHealth = ::android::hardware::health::V1_0::IHealth;
using Result = ::android::hardware::health::V1_0::Result;
using HealthConfig = ::android::hardware::health::V1_0::HealthConfig;
using HealthInfo = ::android::hardware::health::V1_0::HealthInfo;

using ::android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
using ::android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;

// device specific hal interface;
static sp<IHealth> gHealth;

// main healthd loop
extern int healthd_main(void);

// Android mode
extern void healthd_mode_android_init(struct healthd_config *config);
extern int healthd_mode_android_preparetowait(void);
extern void healthd_mode_android_heartbeat(void);
extern void healthd_mode_android_battery_update(
    struct android::BatteryProperties *props);

static struct healthd_mode_ops android_ops = {
    .init = healthd_mode_android_init,
    .preparetowait = healthd_mode_android_preparetowait,
    .heartbeat = healthd_mode_android_heartbeat,
    .battery_update = healthd_mode_android_battery_update,
};

// default energy counter property redirect to talk to device
// HAL
static int healthd_board_get_energy_counter(int64_t *energy) {

    if (gHealth == nullptr) {
        return NAME_NOT_FOUND;
    }

    Result result = Result::NOT_SUPPORTED;
    gHealth->energyCounter([=, &result] (Result ret, int64_t energyOut) {
                result = ret;
                *energy = energyOut;
            });

    return result == Result::SUCCESS ? OK : NAME_NOT_FOUND;
}

void healthd_board_init(struct healthd_config *config) {

    // Initialize the board HAL - Equivalent of healthd_board_init(config)
    // in charger/recovery mode.

    gHealth = IHealth::getService();
    if (gHealth == nullptr) {
        KLOG_WARNING(LOG_TAG, "unable to get HAL interface, using defaults\n");
        return;
    }

    HealthConfig halConfig;
    convertToHealthConfig(config, halConfig);
    gHealth->init(halConfig, [=] (const auto &halConfigOut) {
            convertFromHealthConfig(halConfigOut, config);
            // always redirect energy counter queries
            config->energyCounter = healthd_board_get_energy_counter;
            });
}

int healthd_board_battery_update(struct android::BatteryProperties *props) {
    int logthis = 0;

    if (gHealth == nullptr) {
        return logthis;
    }

    HealthInfo info;
    convertToHealthInfo(props, info);
    gHealth->update(info,
            [=, &logthis] (int32_t ret, const auto &infoOut) {
                logthis = ret;
                convertFromHealthInfo(infoOut, props);
            });

    return logthis;
}

int main(int /*argc*/, char ** /*argv*/) {

    healthd_mode_ops = &android_ops;

    return healthd_main();
}

healthd/healthd_mode_android.cpp

deleted100644 → 0
+0 −65
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "healthd-android"

#include <healthd/healthd.h>
#include "BatteryPropertiesRegistrar.h"

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <cutils/klog.h>
#include <sys/epoll.h>

using namespace android;

static int gBinderFd;
static sp<BatteryPropertiesRegistrar> gBatteryPropertiesRegistrar;

void healthd_mode_android_battery_update(
    struct android::BatteryProperties *props) {
    if (gBatteryPropertiesRegistrar != NULL)
        gBatteryPropertiesRegistrar->notifyListeners(*props);

    return;
}

int healthd_mode_android_preparetowait(void) {
    IPCThreadState::self()->flushCommands();
    return -1;
}

void healthd_mode_android_heartbeat(void) {
}

static void binder_event(uint32_t /*epevents*/) {
    IPCThreadState::self()->handlePolledCommands();
}

void healthd_mode_android_init(struct healthd_config* /*config*/) {
    ProcessState::self()->setThreadPoolMaxThreadCount(0);
    IPCThreadState::self()->disableBackgroundScheduling(true);
    IPCThreadState::self()->setupPolling(&gBinderFd);

    if (gBinderFd >= 0) {
        if (healthd_register_event(gBinderFd, binder_event))
            KLOG_ERROR(LOG_TAG,
                       "Register for binder events failed\n");
    }

    gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
    gBatteryPropertiesRegistrar->publish(gBatteryPropertiesRegistrar);
}
+5 −0
Original line number Diff line number Diff line
@@ -82,8 +82,13 @@ enum EventWakeup {

int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup = EVENT_NO_WAKEUP_FD);
void healthd_battery_update();

// deprecated.
// TODO(b/62229583): This function should be removed since it is only used by
// BatteryPropertiesRegistrar.
android::status_t healthd_get_property(int id,
    struct android::BatteryProperty *val);

void healthd_dump_battery_state(int fd);

struct healthd_mode_ops {