blob: 8e2726669fffb99b5389958ae10ce61ec7358910 [file] [log] [blame]
Todd Poynorb2803522017-05-24 17:19:18 -07001/*
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"
Todd Poynorc7d606d2017-06-22 18:13:56 -070022#include "libthermalcallback/ThermalCallback.h"
Todd Poynorb2803522017-05-24 17:19:18 -070023
Todd Poynorc7d606d2017-06-22 18:13:56 -070024#include <android/hardware/thermal/1.1/IThermal.h>
Todd Poynorb2803522017-05-24 17:19:18 -070025#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
Todd Poynorc7d606d2017-06-22 18:13:56 -070027#include <hidl/HidlTransportSupport.h>
Todd Poynorb2803522017-05-24 17:19:18 -070028
29using namespace android;
Todd Poynorc7d606d2017-06-22 18:13:56 -070030using ::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
Todd Poynorb2803522017-05-24 17:19:18 -070063
64void ThermalServiceDaemon::thermalServiceStartup() {
65 // Binder IThermalService startup
66 mThermalService = new android::os::ThermalService;
67 mThermalService->publish(mThermalService);
Todd Poynorc7d606d2017-06-22 18:13:56 -070068 // Register IThermalService object with IThermalCallback
69 if (mThermalCallback != nullptr)
70 mThermalCallback->registerThermalService(mThermalService);
Todd Poynorb2803522017-05-24 17:19:18 -070071 IPCThreadState::self()->joinThreadPool();
72}
73
Todd Poynorc7d606d2017-06-22 18:13:56 -070074// 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() {
Todd Poynorc7d606d2017-06-22 18:13:56 -0700100 // 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();
Todd Poynorc7d606d2017-06-22 18:13:56 -0700106 // Lookup Thermal HAL and register our ThermalCallback.
107 getThermalHal();
108}
Todd Poynorb2803522017-05-24 17:19:18 -0700109
110int main(int /*argc*/, char** /*argv*/) {
Todd Poynorc7d606d2017-06-22 18:13:56 -0700111 gThermalServiceDaemon = new ThermalServiceDaemon();
112 gThermalServiceDaemon->thermalCallbackStartup();
113 gThermalServiceDaemon->thermalServiceStartup();
114 /* NOTREACHED */
Todd Poynorb2803522017-05-24 17:19:18 -0700115}