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 | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 18 | |
Steven Moreland | defbf32 | 2018-05-30 14:13:06 -0700 | [diff] [blame] | 19 | #include <android/hidl/manager/1.2/IServiceManager.h> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 20 | #include <hidl/Status.h> |
| 21 | #include <hidl/MQDescriptor.h> |
Steven Moreland | 6d7e500 | 2017-01-20 15:21:48 -0800 | [diff] [blame] | 22 | #include <map> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 23 | |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 24 | #include "AccessControl.h" |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 25 | #include "HidlService.h" |
| 26 | |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace hidl { |
| 29 | namespace manager { |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 30 | namespace implementation { |
| 31 | |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 32 | using ::android::hardware::hidl_death_recipient; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 33 | using ::android::hardware::hidl_vec; |
| 34 | using ::android::hardware::hidl_string; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 35 | using ::android::hardware::Return; |
| 36 | using ::android::hardware::Void; |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 37 | using ::android::hidl::base::V1_0::IBase; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 38 | using ::android::hidl::manager::V1_0::IServiceNotification; |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 39 | using ::android::hidl::manager::V1_2::IClientCallback; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 40 | using ::android::sp; |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 41 | using ::android::wp; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 42 | |
Steven Moreland | defbf32 | 2018-05-30 14:13:06 -0700 | [diff] [blame] | 43 | struct ServiceManager : public V1_2::IServiceManager, hidl_death_recipient { |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 44 | // Methods from ::android::hidl::manager::V1_0::IServiceManager follow. |
Martijn Coenen | 7b02bf9 | 2017-01-02 15:17:58 +0100 | [diff] [blame] | 45 | Return<sp<IBase>> get(const hidl_string& fqName, |
Steven Moreland | 37aed80 | 2017-04-06 09:16:42 -0700 | [diff] [blame] | 46 | const hidl_string& name) override; |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 47 | Return<bool> add(const hidl_string& name, |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 48 | const sp<IBase>& service) override; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 49 | |
Steven Moreland | 37aed80 | 2017-04-06 09:16:42 -0700 | [diff] [blame] | 50 | Return<Transport> getTransport(const hidl_string& fqName, |
| 51 | const hidl_string& name); |
| 52 | |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 53 | Return<void> list(list_cb _hidl_cb) override; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 54 | Return<void> listByInterface(const hidl_string& fqInstanceName, |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 55 | listByInterface_cb _hidl_cb) override; |
| 56 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 57 | Return<bool> registerForNotifications(const hidl_string& fqName, |
| 58 | const hidl_string& name, |
| 59 | const sp<IServiceNotification>& callback) override; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 60 | |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 61 | Return<void> debugDump(debugDump_cb _cb) override; |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 62 | Return<void> registerPassthroughClient(const hidl_string &fqName, |
Martijn Coenen | 737de1b | 2017-04-27 09:29:43 -0700 | [diff] [blame] | 63 | const hidl_string &name) override; |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 64 | |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 65 | // Methods from ::android::hidl::manager::V1_1::IServiceManager follow. |
| 66 | Return<bool> unregisterForNotifications(const hidl_string& fqName, |
| 67 | const hidl_string& name, |
| 68 | const sp<IServiceNotification>& callback) override; |
| 69 | |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 70 | // Methods from ::android::hidl::manager::V1_2::IServiceManager follow. |
Steven Moreland | dbaab55 | 2019-01-24 19:00:04 -0800 | [diff] [blame] | 71 | Return<bool> registerClientCallback(const hidl_string& fqName, |
| 72 | const hidl_string& name, |
| 73 | const sp<IBase>& server, |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 74 | const sp<IClientCallback>& cb) override; |
| 75 | Return<bool> unregisterClientCallback(const sp<IBase>& server, |
| 76 | const sp<IClientCallback>& cb) override; |
Steven Moreland | 707f22c | 2018-10-11 12:09:34 -0700 | [diff] [blame] | 77 | Return<bool> addWithChain(const hidl_string& name, |
| 78 | const sp<IBase>& service, |
| 79 | const hidl_vec<hidl_string>& chain) override; |
Steven Moreland | aae4eab | 2018-10-29 13:00:31 -0700 | [diff] [blame] | 80 | Return<void> listManifestByInterface(const hidl_string& fqInstanceName, |
| 81 | listManifestByInterface_cb _hidl_cb) override; |
Steven Moreland | dbaab55 | 2019-01-24 19:00:04 -0800 | [diff] [blame] | 82 | Return<bool> tryUnregister(const hidl_string& fqName, |
| 83 | const hidl_string& name, |
| 84 | const sp<IBase>& service) override; |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 85 | |
| 86 | void handleClientCallbacks(); |
| 87 | |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 88 | virtual void serviceDied(uint64_t cookie, const wp<IBase>& who); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 89 | private: |
Steven Moreland | d90d9f2 | 2019-03-20 13:47:30 -0700 | [diff] [blame] | 90 | bool addImpl(const std::string& name, |
Steven Moreland | 707f22c | 2018-10-11 12:09:34 -0700 | [diff] [blame] | 91 | const sp<IBase>& service, |
| 92 | const hidl_vec<hidl_string>& interfaceChain, |
Xin Li | bf44d5a | 2019-03-11 17:50:47 -0700 | [diff] [blame] | 93 | const AccessControl::CallingContext& callingContext); |
Steven Moreland | 707f22c | 2018-10-11 12:09:34 -0700 | [diff] [blame] | 94 | |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 95 | // if restrictToInstanceName is nullptr, remove all, otherwise only those services |
| 96 | // which match this instance name. Returns whether all instances were removed. |
| 97 | bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 98 | bool removePackageListener(const wp<IBase>& who); |
| 99 | bool removeServiceListener(const wp<IBase>& who); |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 100 | size_t countExistingService() const; |
Steven Moreland | d853620 | 2018-09-26 10:56:19 -0700 | [diff] [blame] | 101 | |
| 102 | // true = continue, false = break |
| 103 | void forEachExistingService(std::function<bool(const HidlService *)> f) const; |
| 104 | void forEachExistingService(std::function<bool(HidlService *)> f); |
| 105 | void forEachServiceEntry(std::function<bool(const HidlService *)> f) const; |
| 106 | void forEachServiceEntry(std::function<bool(HidlService *)> f); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 107 | |
Steven Moreland | dbaab55 | 2019-01-24 19:00:04 -0800 | [diff] [blame] | 108 | HidlService* lookup(const std::string& fqName, const std::string& name); |
| 109 | |
Steven Moreland | 6d7e500 | 2017-01-20 15:21:48 -0800 | [diff] [blame] | 110 | using InstanceMap = std::map< |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 111 | std::string, // instance name e.x. "manager" |
| 112 | std::unique_ptr<HidlService> |
| 113 | >; |
| 114 | |
| 115 | struct PackageInterfaceMap { |
| 116 | InstanceMap &getInstanceMap(); |
| 117 | const InstanceMap &getInstanceMap() const; |
| 118 | |
| 119 | /** |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 120 | * Finds a HidlService with the desired name. If none, |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 121 | * returns nullptr. HidlService::getService() might also be nullptr |
| 122 | * if there are registered IServiceNotification objects for it. Return |
| 123 | * value should be treated as a temporary reference. |
| 124 | */ |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 125 | HidlService *lookup( |
| 126 | const std::string &name); |
| 127 | const HidlService *lookup( |
| 128 | const std::string &name) const; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 129 | |
| 130 | void insertService(std::unique_ptr<HidlService> &&service); |
| 131 | |
| 132 | void addPackageListener(sp<IServiceNotification> listener); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 133 | bool removePackageListener(const wp<IBase>& who); |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 134 | bool removeServiceListener(const wp<IBase>& who); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 135 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 136 | void sendPackageRegistrationNotification( |
| 137 | const hidl_string &fqName, |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 138 | const hidl_string &instanceName); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 139 | |
Steven Moreland | 978473f | 2016-11-28 14:51:07 -0800 | [diff] [blame] | 140 | private: |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 141 | InstanceMap mInstanceMap{}; |
| 142 | |
| 143 | std::vector<sp<IServiceNotification>> mPackageListeners{}; |
| 144 | }; |
| 145 | |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 146 | AccessControl mAcl; |
| 147 | |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Access to this map doesn't need to be locked, since hwservicemanager |
| 150 | * is single-threaded. |
| 151 | * |
| 152 | * e.x. |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 153 | * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"] |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 154 | * -> HidlService object |
| 155 | */ |
Steven Moreland | 6d7e500 | 2017-01-20 15:21:48 -0800 | [diff] [blame] | 156 | std::map< |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 157 | std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager" |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 158 | PackageInterfaceMap |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 159 | > mServiceMap; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace implementation |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 163 | } // namespace manager |
| 164 | } // namespace hidl |
| 165 | } // namespace android |