1/*
2 * Copyright (C) 2017 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 "thermalserviced"
18#include <log/log.h>
19
20#include "thermalserviced.h"
21#include "ThermalService.h"
22#include "libthermalcallback/ThermalCallback.h"
23
24#include <android/hardware/thermal/1.1/IThermal.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include <hidl/HidlTransportSupport.h>
28
29using namespace android;
30using ::android::hardware::thermal::V1_1::IThermal;
31using ::android::hardware::thermal::V1_0::Temperature;
32using ::android::hardware::thermal::V1_1::IThermalCallback;
33using ::android::hardware::thermal::V1_1::implementation::ThermalCallback;
34using ::android::hardware::configureRpcThreadpool;
35using ::android::hardware::hidl_death_recipient;
36using ::android::hidl::base::V1_0::IBase;
37using ::android::os::ThermalService;
38
39template<typename T>
40using Return = hardware::Return<T>;
41
42namespace {
43
44// Our thermalserviced main object
45ThermalServiceDaemon* gThermalServiceDaemon;
46
47// Thermal HAL client
48sp<IThermal> gThermalHal = nullptr;
49
50// Binder death notifier informing of Thermal HAL death.
51struct ThermalServiceDeathRecipient : hidl_death_recipient {
52    virtual void serviceDied(
53        uint64_t cookie __unused, const wp<IBase>& who __unused) {
54        gThermalHal = nullptr;
55        ALOGE("IThermal HAL died");
56        gThermalServiceDaemon->getThermalHal();
57    }
58};
59
60sp<ThermalServiceDeathRecipient> gThermalHalDied = nullptr;
61
62}  // anonymous namespace
63
64void ThermalServiceDaemon::thermalServiceStartup() {
65    // Binder IThermalService startup
66    mThermalService = new android::os::ThermalService;
67    mThermalService->publish(mThermalService);
68    // Register IThermalService object with IThermalCallback
69    if (mThermalCallback != nullptr)
70        mThermalCallback->registerThermalService(mThermalService);
71    IPCThreadState::self()->joinThreadPool();
72}
73
74// Lookup Thermal HAL, register death notifier, register our
75// ThermalCallback with the Thermal HAL.
76void ThermalServiceDaemon::getThermalHal() {
77    gThermalHal = IThermal::getService();
78    if (gThermalHal == nullptr) {
79        ALOGW("Unable to get Thermal HAL V1.1, vendor thermal event notification not available");
80        return;
81    }
82
83    // Binder death notifier for Thermal HAL
84    if (gThermalHalDied == nullptr)
85        gThermalHalDied = new ThermalServiceDeathRecipient();
86
87    if (gThermalHalDied != nullptr)
88        gThermalHal->linkToDeath(gThermalHalDied, 0x451F /* cookie */);
89
90    if (mThermalCallback != nullptr) {
91        Return<void> ret = gThermalHal->registerThermalCallback(
92            mThermalCallback);
93        if (!ret.isOk())
94            ALOGE("registerThermalCallback failed, status: %s",
95                  ret.description().c_str());
96    }
97}
98
99void ThermalServiceDaemon::thermalCallbackStartup() {
100    // HIDL IThermalCallback startup
101    // Need at least 2 threads in thread pool since we wait for dead HAL
102    // to come back on the binder death notification thread and we need
103    // another thread for the incoming service now available call.
104    configureRpcThreadpool(2, false /* callerWillJoin */);
105    mThermalCallback = new ThermalCallback();
106    // Lookup Thermal HAL and register our ThermalCallback.
107    getThermalHal();
108}
109
110int main(int /*argc*/, char** /*argv*/) {
111    gThermalServiceDaemon = new ThermalServiceDaemon();
112    gThermalServiceDaemon->thermalCallbackStartup();
113    gThermalServiceDaemon->thermalServiceStartup();
114    /* NOTREACHED */
115}
116