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