Steven Moreland | fe66b73 | 2019-02-01 14:29:45 -0800 | [diff] [blame] | 1 | /* |
| 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 Moreland | d5f8f08 | 2019-06-06 12:59:20 -0700 | [diff] [blame] | 17 | #pragma once |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 18 | |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 19 | #include <set> |
| 20 | |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 21 | #include <android/hidl/manager/1.2/IServiceManager.h> |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 22 | #include <hidl/Status.h> |
| 23 | #include <hidl/MQDescriptor.h> |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hidl { |
| 27 | namespace manager { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 28 | namespace implementation { |
| 29 | |
| 30 | using ::android::hardware::hidl_vec; |
| 31 | using ::android::hardware::hidl_string; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 32 | using ::android::hardware::Return; |
| 33 | using ::android::hardware::Void; |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 34 | using ::android::hidl::base::V1_0::IBase; |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 35 | using ::android::hidl::manager::V1_0::IServiceNotification; |
| 36 | using ::android::hidl::manager::V1_1::IServiceManager; |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 37 | using ::android::hidl::manager::V1_2::IClientCallback; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 38 | using ::android::sp; |
| 39 | |
| 40 | struct HidlService { |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 41 | HidlService(const std::string &interfaceName, |
| 42 | const std::string &instanceName, |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 43 | const sp<IBase> &service, |
| 44 | const pid_t pid); |
| 45 | HidlService(const std::string &interfaceName, |
| 46 | const std::string &instanceName) |
| 47 | : HidlService( |
| 48 | interfaceName, |
| 49 | instanceName, |
| 50 | nullptr, |
| 51 | static_cast<pid_t>(IServiceManager::PidConstant::NO_PID)) |
| 52 | {} |
Steven Moreland | 501bc8e | 2019-02-01 16:21:46 -0800 | [diff] [blame] | 53 | virtual ~HidlService() {} |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Note, getService() can be nullptr. This is because you can have a HidlService |
| 57 | * with registered IServiceNotification objects but no service registered yet. |
| 58 | */ |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 59 | sp<IBase> getService() const; |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 60 | void setService(sp<IBase> service, pid_t pid); |
Steven Moreland | 7185a89 | 2019-01-09 18:00:05 -0800 | [diff] [blame] | 61 | pid_t getDebugPid() const; |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 62 | const std::string &getInterfaceName() const; |
| 63 | const std::string &getInstanceName() const; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 64 | |
| 65 | void addListener(const sp<IServiceNotification> &listener); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 66 | bool removeListener(const wp<IBase> &listener); |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 67 | void registerPassthroughClient(pid_t pid); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 68 | |
Steven Moreland | 2fa0f55 | 2019-02-01 19:27:33 -0800 | [diff] [blame] | 69 | // also sends onClients(true) if we have clients |
Steven Moreland | f083ee3 | 2020-04-06 16:18:50 -0700 | [diff] [blame] | 70 | // knownClientCount, see forceHandleClientCallbacks |
| 71 | void addClientCallback(const sp<IClientCallback>& callback, size_t knownClientCount); |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 72 | bool removeClientCallback(const sp<IClientCallback>& callback); |
Steven Moreland | dbaab55 | 2019-01-24 19:00:04 -0800 | [diff] [blame] | 73 | |
Steven Moreland | f083ee3 | 2020-04-06 16:18:50 -0700 | [diff] [blame] | 74 | // return is if we are guaranteed to have a client |
| 75 | // knownClientCount, see forceHandleClientCallbacks |
| 76 | bool handleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount); |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 77 | |
Steven Moreland | e7cf142 | 2019-02-01 20:17:25 -0800 | [diff] [blame] | 78 | // Updates client callbacks (even if mClientCallbacks is emtpy) |
| 79 | // see handleClientCallbacks |
Steven Moreland | f083ee3 | 2020-04-06 16:18:50 -0700 | [diff] [blame] | 80 | // |
| 81 | // knownClientCount - this is the number of clients that is currently |
| 82 | // expected to be in use by known actors. This number of clients must be |
| 83 | // exceeded in order to consider the service to have clients. |
| 84 | // |
| 85 | // returns whether or not this service has clients |
| 86 | bool forceHandleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount); |
Steven Moreland | e7cf142 | 2019-02-01 20:17:25 -0800 | [diff] [blame] | 87 | |
Steven Moreland | 98db829 | 2018-12-07 13:08:25 -0800 | [diff] [blame] | 88 | // when giving out a handle to a client, but the kernel might not know this yet |
| 89 | void guaranteeClient(); |
| 90 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 91 | std::string string() const; // e.x. "android.hidl.manager@1.0::IServiceManager/manager" |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 92 | const std::set<pid_t> &getPassthroughClients() const; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 93 | |
Steven Moreland | 501bc8e | 2019-02-01 16:21:46 -0800 | [diff] [blame] | 94 | protected: |
| 95 | // mockable number of clients including hwservicemanager. -1 if not implemented or unavailable. |
| 96 | virtual ssize_t getNodeStrongRefCount(); |
| 97 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 98 | private: |
Martijn Coenen | 72103a0 | 2017-01-18 16:06:34 +0100 | [diff] [blame] | 99 | void sendRegistrationNotifications(); |
Steven Moreland | 2fa0f55 | 2019-02-01 19:27:33 -0800 | [diff] [blame] | 100 | |
| 101 | // Also updates mHasClients (of what the last callback was) |
Steven Moreland | 98db829 | 2018-12-07 13:08:25 -0800 | [diff] [blame] | 102 | void sendClientCallbackNotifications(bool hasClients); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 103 | |
Steven Moreland | 2fa0f55 | 2019-02-01 19:27:33 -0800 | [diff] [blame] | 104 | // Only sends notification |
| 105 | void sendClientCallbackNotification(const sp<IClientCallback>& callback, bool hasClients); |
| 106 | |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 107 | const std::string mInterfaceName; // e.x. "android.hidl.manager@1.0::IServiceManager" |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 108 | const std::string mInstanceName; // e.x. "manager" |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 109 | sp<IBase> mService; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 110 | |
| 111 | std::vector<sp<IServiceNotification>> mListeners{}; |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 112 | std::set<pid_t> mPassthroughClients{}; |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 113 | pid_t mPid = static_cast<pid_t>(IServiceManager::PidConstant::NO_PID); |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 114 | |
| 115 | std::vector<sp<IClientCallback>> mClientCallbacks{}; |
| 116 | bool mHasClients = false; // notifications sent on true -> false. |
Steven Moreland | 98db829 | 2018-12-07 13:08:25 -0800 | [diff] [blame] | 117 | bool mGuaranteeClient = false; // whenever a client is handed out |
Peter Kalauskas | 3b27ec5 | 2019-01-17 14:47:24 -0800 | [diff] [blame] | 118 | size_t mNoClientsCounter = 0; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace implementation |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 122 | } // namespace manager |
| 123 | } // namespace hidl |
| 124 | } // namespace android |