blob: 6ee6024142ecdd9b8a7caa45e5b6ddb7151c2a1b [file] [log] [blame]
Steven Morelandfe66b732019-02-01 14:29:45 -08001/*
2 * Copyright (C) 2016 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
Steven Moreland2173d3c2016-11-09 15:00:58 -080017#define LOG_TAG "hwservicemanager"
18#include "HidlService.h"
19
20#include <android-base/logging.h>
Steven Moreland6f4fbe12017-07-21 18:07:42 -070021#include <hidl/HidlTransportSupport.h>
Steven Morelandd8536202018-09-26 10:56:19 -070022#include <hwbinder/BpHwBinder.h>
Steven Moreland2173d3c2016-11-09 15:00:58 -080023#include <sstream>
24
Steven Morelandd8536202018-09-26 10:56:19 -070025using ::android::hardware::interfacesEqual;
26
Steven Moreland2173d3c2016-11-09 15:00:58 -080027namespace android {
28namespace hidl {
29namespace manager {
Steven Moreland2173d3c2016-11-09 15:00:58 -080030namespace implementation {
31
Peter Kalauskas3b27ec52019-01-17 14:47:24 -080032static constexpr int kNoClientRepeatLimit = 2;
33
Steven Moreland2173d3c2016-11-09 15:00:58 -080034HidlService::HidlService(
Steven Morelandd544cf62017-01-04 15:24:32 -080035 const std::string &interfaceName,
36 const std::string &instanceName,
Steven Morelandcdf94722017-03-21 12:16:31 -070037 const sp<IBase> &service,
38 pid_t pid)
Steven Morelandd544cf62017-01-04 15:24:32 -080039: mInterfaceName(interfaceName),
40 mInstanceName(instanceName),
Steven Morelandcdf94722017-03-21 12:16:31 -070041 mService(service),
42 mPid(pid)
Steven Moreland2173d3c2016-11-09 15:00:58 -080043{}
44
Yifan Hongb3a90f02016-11-23 12:58:04 -080045sp<IBase> HidlService::getService() const {
Steven Moreland2173d3c2016-11-09 15:00:58 -080046 return mService;
47}
Steven Morelandcdf94722017-03-21 12:16:31 -070048void HidlService::setService(sp<IBase> service, pid_t pid) {
Steven Moreland2173d3c2016-11-09 15:00:58 -080049 mService = service;
Steven Morelandcdf94722017-03-21 12:16:31 -070050 mPid = pid;
Steven Moreland2173d3c2016-11-09 15:00:58 -080051
Steven Morelandd8536202018-09-26 10:56:19 -070052 mClientCallbacks.clear();
Steven Moreland573e5322019-01-24 19:00:04 -080053 mHasClients = false;
54 mGuaranteeClient = false;
Steven Moreland9cd9e882019-02-01 18:55:42 -080055 mNoClientsCounter = 0;
Steven Morelandd8536202018-09-26 10:56:19 -070056
Steven Moreland2173d3c2016-11-09 15:00:58 -080057 sendRegistrationNotifications();
58}
Steven Morelandcdf94722017-03-21 12:16:31 -070059
Steven Moreland7185a892019-01-09 18:00:05 -080060pid_t HidlService::getDebugPid() const {
Steven Morelandcdf94722017-03-21 12:16:31 -070061 return mPid;
62}
Steven Morelandd544cf62017-01-04 15:24:32 -080063const std::string &HidlService::getInterfaceName() const {
64 return mInterfaceName;
Steven Moreland2173d3c2016-11-09 15:00:58 -080065}
Steven Morelandd544cf62017-01-04 15:24:32 -080066const std::string &HidlService::getInstanceName() const {
Steven Moreland2173d3c2016-11-09 15:00:58 -080067 return mInstanceName;
68}
Steven Moreland2173d3c2016-11-09 15:00:58 -080069
70void HidlService::addListener(const sp<IServiceNotification> &listener) {
Steven Moreland2173d3c2016-11-09 15:00:58 -080071 if (mService != nullptr) {
Steven Moreland30acc222017-01-26 11:44:38 -080072 auto ret = listener->onRegistration(
73 mInterfaceName, mInstanceName, true /* preexisting */);
Martijn Coenen7fafc142017-03-06 16:17:51 +010074 if (!ret.isOk()) {
75 LOG(ERROR) << "Not adding listener for " << mInterfaceName << "/"
76 << mInstanceName << ": transport error when sending "
77 << "notification for already registered instance.";
78 return;
79 }
Steven Moreland2173d3c2016-11-09 15:00:58 -080080 }
Martijn Coenen7fafc142017-03-06 16:17:51 +010081 mListeners.push_back(listener);
82}
83
84bool HidlService::removeListener(const wp<IBase>& listener) {
85 bool found = false;
86
87 for (auto it = mListeners.begin(); it != mListeners.end();) {
Steven Moreland6f4fbe12017-07-21 18:07:42 -070088 if (interfacesEqual(*it, listener.promote())) {
Martijn Coenen7fafc142017-03-06 16:17:51 +010089 it = mListeners.erase(it);
90 found = true;
91 } else {
92 ++it;
93 }
94 }
95
96 return found;
Steven Moreland2173d3c2016-11-09 15:00:58 -080097}
98
Yifan Hongee531a82017-02-03 15:10:18 -080099void HidlService::registerPassthroughClient(pid_t pid) {
100 mPassthroughClients.insert(pid);
101}
102
103const std::set<pid_t> &HidlService::getPassthroughClients() const {
104 return mPassthroughClients;
105}
106
Steven Moreland105c58b2020-04-06 16:18:50 -0700107void HidlService::addClientCallback(const sp<IClientCallback>& callback, size_t knownClientCount) {
Steven Morelande7cf1422019-02-01 20:17:25 -0800108 if (mHasClients) {
109 // we have this kernel feature, so make sure we're in an updated state
Steven Moreland105c58b2020-04-06 16:18:50 -0700110 forceHandleClientCallbacks(false /*onInterval*/, knownClientCount);
Steven Morelande7cf1422019-02-01 20:17:25 -0800111 }
Steven Moreland2fa0f552019-02-01 19:27:33 -0800112
113 if (mHasClients) {
Steven Morelande7cf1422019-02-01 20:17:25 -0800114 // make sure this callback is in the same state as all of the rest
Steven Moreland2fa0f552019-02-01 19:27:33 -0800115 sendClientCallbackNotification(callback, true /*hasClients*/);
116 }
117
Steven Morelandd8536202018-09-26 10:56:19 -0700118 mClientCallbacks.push_back(callback);
119}
120
121bool HidlService::removeClientCallback(const sp<IClientCallback>& callback) {
122 bool found = false;
123
124 for (auto it = mClientCallbacks.begin(); it != mClientCallbacks.end();) {
125 if (interfacesEqual(*it, callback)) {
126 it = mClientCallbacks.erase(it);
127 found = true;
128 } else {
129 ++it;
130 }
131 }
132
133 return found;
134}
135
Steven Moreland105c58b2020-04-06 16:18:50 -0700136bool HidlService::handleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount) {
Steven Morelande7cf1422019-02-01 20:17:25 -0800137 if (!mClientCallbacks.empty()) {
Steven Moreland105c58b2020-04-06 16:18:50 -0700138 return forceHandleClientCallbacks(isCalledOnInterval, knownClientCount);
Steven Morelande7cf1422019-02-01 20:17:25 -0800139 }
140
Steven Moreland105c58b2020-04-06 16:18:50 -0700141 return false;
Steven Morelande7cf1422019-02-01 20:17:25 -0800142}
143
Steven Moreland105c58b2020-04-06 16:18:50 -0700144bool HidlService::forceHandleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount) {
Steven Moreland501bc8e2019-02-01 16:21:46 -0800145 ssize_t count = getNodeStrongRefCount();
Steven Morelandd8536202018-09-26 10:56:19 -0700146
147 // binder driver doesn't support this feature
Steven Moreland105c58b2020-04-06 16:18:50 -0700148 if (count < 0) return false;
Steven Morelandd8536202018-09-26 10:56:19 -0700149
Steven Moreland105c58b2020-04-06 16:18:50 -0700150 bool hasClients = (size_t)count > knownClientCount;
Steven Morelandd8536202018-09-26 10:56:19 -0700151
Steven Moreland9cd9e882019-02-01 18:55:42 -0800152 if (mGuaranteeClient) {
153 // we have no record of this client
154 if (!mHasClients && !hasClients) {
155 sendClientCallbackNotifications(true);
156 }
157
158 // guarantee is temporary
159 mGuaranteeClient = false;
160 }
161
162 if (hasClients && !mHasClients) {
163 // client was retrieved in some other way
164 sendClientCallbackNotifications(true);
Steven Moreland98db8292018-12-07 13:08:25 -0800165 }
Steven Morelandd8536202018-09-26 10:56:19 -0700166
Peter Kalauskas3b27ec52019-01-17 14:47:24 -0800167 // there are no more clients, but the callback has not been called yet
Steven Moreland9cd9e882019-02-01 18:55:42 -0800168 if (!hasClients && mHasClients && isCalledOnInterval) {
Peter Kalauskas3b27ec52019-01-17 14:47:24 -0800169 mNoClientsCounter++;
Steven Moreland9cd9e882019-02-01 18:55:42 -0800170
171 if (mNoClientsCounter >= kNoClientRepeatLimit) {
172 sendClientCallbackNotifications(false);
173 }
Steven Morelandd8536202018-09-26 10:56:19 -0700174 }
175
Steven Moreland105c58b2020-04-06 16:18:50 -0700176 return mHasClients;
Steven Moreland98db8292018-12-07 13:08:25 -0800177}
178
179void HidlService::guaranteeClient() {
180 mGuaranteeClient = true;
Steven Morelandd8536202018-09-26 10:56:19 -0700181}
182
Steven Moreland2173d3c2016-11-09 15:00:58 -0800183std::string HidlService::string() const {
184 std::stringstream ss;
Steven Morelandd544cf62017-01-04 15:24:32 -0800185 ss << mInterfaceName << "/" << mInstanceName;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800186 return ss.str();
187}
188
Steven Moreland501bc8e2019-02-01 16:21:46 -0800189ssize_t HidlService::getNodeStrongRefCount() {
190 using ::android::hardware::toBinder;
191 using ::android::hardware::BpHwBinder;
192 using ::android::hardware::IBinder;
193
194 if (mService == nullptr) return -1;
195
196 // this justifies the bp cast below, no in-process HALs need this
197 if (!mService->isRemote()) return -1;
198
199 sp<IBinder> binder = toBinder(mService);
200 if (binder == nullptr) return -1;
201
202 sp<BpHwBinder> bpBinder = static_cast<BpHwBinder*>(binder.get());
203 return bpBinder->getNodeStrongRefCount();
204}
205
Martijn Coenen72103a02017-01-18 16:06:34 +0100206void HidlService::sendRegistrationNotifications() {
Steven Moreland2173d3c2016-11-09 15:00:58 -0800207 if (mListeners.size() == 0 || mService == nullptr) {
208 return;
209 }
210
Steven Morelandd544cf62017-01-04 15:24:32 -0800211 hidl_string iface = mInterfaceName;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800212 hidl_string name = mInstanceName;
213
Martijn Coenen72103a02017-01-18 16:06:34 +0100214 for (auto it = mListeners.begin(); it != mListeners.end();) {
215 auto ret = (*it)->onRegistration(iface, name, false /* preexisting */);
216 if (ret.isOk()) {
217 ++it;
218 } else {
Martijn Coenen7fafc142017-03-06 16:17:51 +0100219 LOG(ERROR) << "Dropping registration callback for " << iface << "/" << name
220 << ": transport error.";
Martijn Coenen72103a02017-01-18 16:06:34 +0100221 it = mListeners.erase(it);
222 }
Steven Moreland2173d3c2016-11-09 15:00:58 -0800223 }
224}
225
Steven Moreland98db8292018-12-07 13:08:25 -0800226void HidlService::sendClientCallbackNotifications(bool hasClients) {
Steven Moreland9cd9e882019-02-01 18:55:42 -0800227 CHECK(hasClients != mHasClients) << "Record shows: " << mHasClients
228 << " so we can't tell clients again that we have client: " << hasClients;
229
Steven Moreland98db8292018-12-07 13:08:25 -0800230 LOG(INFO) << "Notifying " << string() << " they have clients: " << hasClients;
231
232 for (const auto& cb : mClientCallbacks) {
Steven Moreland2fa0f552019-02-01 19:27:33 -0800233 sendClientCallbackNotification(cb, hasClients);
Steven Moreland98db8292018-12-07 13:08:25 -0800234 }
Peter Kalauskas3b27ec52019-01-17 14:47:24 -0800235
236 mNoClientsCounter = 0;
237 mHasClients = hasClients;
Steven Moreland98db8292018-12-07 13:08:25 -0800238}
239
Steven Moreland2fa0f552019-02-01 19:27:33 -0800240void HidlService::sendClientCallbackNotification(const sp<IClientCallback>& callback, bool hasClients) {
241 Return<void> ret = callback->onClients(getService(), hasClients);
242 if (!ret.isOk()) {
243 LOG(WARNING) << "onClients callback failed for " << string() << ": " << ret.description();
244 }
245}
246
Steven Moreland98db8292018-12-07 13:08:25 -0800247
Steven Moreland2173d3c2016-11-09 15:00:58 -0800248} // namespace implementation
Steven Moreland2173d3c2016-11-09 15:00:58 -0800249} // namespace manager
250} // namespace hidl
Yifan Hongb3a90f02016-11-23 12:58:04 -0800251} // namespace android