blob: 8f0c83ab1c91b8421fedd9a5302425f97b7f31d7 [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 Moreland6f4fbe12017-07-21 18:07:42 -070017#ifndef ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
18#define ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
Steven Morelandd83d1102016-10-25 15:01:47 -070019
Steven Morelanddefbf322018-05-30 14:13:06 -070020#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Morelandd83d1102016-10-25 15:01:47 -070021#include <hidl/Status.h>
22#include <hidl/MQDescriptor.h>
Steven Moreland6d7e5002017-01-20 15:21:48 -080023#include <map>
Steven Morelandd83d1102016-10-25 15:01:47 -070024
Martijn Coenen7ce83be2017-04-07 16:19:32 -070025#include "AccessControl.h"
Steven Moreland2173d3c2016-11-09 15:00:58 -080026#include "HidlService.h"
27
Steven Morelandd83d1102016-10-25 15:01:47 -070028namespace android {
29namespace hidl {
30namespace manager {
Steven Morelandd83d1102016-10-25 15:01:47 -070031namespace implementation {
32
Martijn Coenen46847a62016-12-19 05:36:12 +010033using ::android::hardware::hidl_death_recipient;
Steven Morelandd83d1102016-10-25 15:01:47 -070034using ::android::hardware::hidl_vec;
35using ::android::hardware::hidl_string;
Steven Morelandd83d1102016-10-25 15:01:47 -070036using ::android::hardware::Return;
37using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080038using ::android::hidl::base::V1_0::IBase;
Steven Moreland2173d3c2016-11-09 15:00:58 -080039using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd8536202018-09-26 10:56:19 -070040using ::android::hidl::manager::V1_2::IClientCallback;
Steven Morelandd83d1102016-10-25 15:01:47 -070041using ::android::sp;
Martijn Coenen46847a62016-12-19 05:36:12 +010042using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070043
Steven Morelanddefbf322018-05-30 14:13:06 -070044struct ServiceManager : public V1_2::IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070045 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010046 Return<sp<IBase>> get(const hidl_string& fqName,
Steven Moreland37aed802017-04-06 09:16:42 -070047 const hidl_string& name) override;
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010048 Return<bool> add(const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080049 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070050
Steven Moreland37aed802017-04-06 09:16:42 -070051 Return<Transport> getTransport(const hidl_string& fqName,
52 const hidl_string& name);
53
Steven Moreland76237812016-11-08 15:59:04 -080054 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080055 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080056 listByInterface_cb _hidl_cb) override;
57
Steven Moreland2173d3c2016-11-09 15:00:58 -080058 Return<bool> registerForNotifications(const hidl_string& fqName,
59 const hidl_string& name,
60 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070061
Yifan Hong83c49f62017-01-25 14:16:34 -080062 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080063 Return<void> registerPassthroughClient(const hidl_string &fqName,
Martijn Coenen737de1b2017-04-27 09:29:43 -070064 const hidl_string &name) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080065
Steven Moreland6f4fbe12017-07-21 18:07:42 -070066 // Methods from ::android::hidl::manager::V1_1::IServiceManager follow.
67 Return<bool> unregisterForNotifications(const hidl_string& fqName,
68 const hidl_string& name,
69 const sp<IServiceNotification>& callback) override;
70
Steven Morelandd8536202018-09-26 10:56:19 -070071 // Methods from ::android::hidl::manager::V1_2::IServiceManager follow.
Steven Morelanddbaab552019-01-24 19:00:04 -080072 Return<bool> registerClientCallback(const hidl_string& fqName,
73 const hidl_string& name,
74 const sp<IBase>& server,
Steven Morelandd8536202018-09-26 10:56:19 -070075 const sp<IClientCallback>& cb) override;
76 Return<bool> unregisterClientCallback(const sp<IBase>& server,
77 const sp<IClientCallback>& cb) override;
Steven Moreland707f22c2018-10-11 12:09:34 -070078 Return<bool> addWithChain(const hidl_string& name,
79 const sp<IBase>& service,
80 const hidl_vec<hidl_string>& chain) override;
Steven Morelandaae4eab2018-10-29 13:00:31 -070081 Return<void> listManifestByInterface(const hidl_string& fqInstanceName,
82 listManifestByInterface_cb _hidl_cb) override;
Steven Morelanddbaab552019-01-24 19:00:04 -080083 Return<bool> tryUnregister(const hidl_string& fqName,
84 const hidl_string& name,
85 const sp<IBase>& service) override;
Steven Morelandd8536202018-09-26 10:56:19 -070086
87 void handleClientCallbacks();
88
Martijn Coenen46847a62016-12-19 05:36:12 +010089 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070090private:
Steven Moreland707f22c2018-10-11 12:09:34 -070091 bool addImpl(const hidl_string& name,
92 const sp<IBase>& service,
93 const hidl_vec<hidl_string>& interfaceChain,
Xin Libf44d5a2019-03-11 17:50:47 -070094 const AccessControl::CallingContext& callingContext);
Steven Moreland707f22c2018-10-11 12:09:34 -070095
Steven Moreland76936fe2017-09-19 12:18:30 -070096 // if restrictToInstanceName is nullptr, remove all, otherwise only those services
97 // which match this instance name. Returns whether all instances were removed.
98 bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName);
Martijn Coenen7fafc142017-03-06 16:17:51 +010099 bool removePackageListener(const wp<IBase>& who);
100 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -0800101 size_t countExistingService() const;
Steven Morelandd8536202018-09-26 10:56:19 -0700102
103 // true = continue, false = break
104 void forEachExistingService(std::function<bool(const HidlService *)> f) const;
105 void forEachExistingService(std::function<bool(HidlService *)> f);
106 void forEachServiceEntry(std::function<bool(const HidlService *)> f) const;
107 void forEachServiceEntry(std::function<bool(HidlService *)> f);
Steven Morelandd83d1102016-10-25 15:01:47 -0700108
Steven Morelanddbaab552019-01-24 19:00:04 -0800109 HidlService* lookup(const std::string& fqName, const std::string& name);
110
Steven Moreland6d7e5002017-01-20 15:21:48 -0800111 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -0800112 std::string, // instance name e.x. "manager"
113 std::unique_ptr<HidlService>
114 >;
115
116 struct PackageInterfaceMap {
117 InstanceMap &getInstanceMap();
118 const InstanceMap &getInstanceMap() const;
119
120 /**
Steven Morelandd544cf62017-01-04 15:24:32 -0800121 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -0800122 * returns nullptr. HidlService::getService() might also be nullptr
123 * if there are registered IServiceNotification objects for it. Return
124 * value should be treated as a temporary reference.
125 */
Steven Morelandd544cf62017-01-04 15:24:32 -0800126 HidlService *lookup(
127 const std::string &name);
128 const HidlService *lookup(
129 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800130
131 void insertService(std::unique_ptr<HidlService> &&service);
132
133 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +0100134 bool removePackageListener(const wp<IBase>& who);
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700135 bool removeServiceListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800136
Steven Moreland2173d3c2016-11-09 15:00:58 -0800137 void sendPackageRegistrationNotification(
138 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +0100139 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800140
Steven Moreland978473f2016-11-28 14:51:07 -0800141 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -0800142 InstanceMap mInstanceMap{};
143
144 std::vector<sp<IServiceNotification>> mPackageListeners{};
145 };
146
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700147 AccessControl mAcl;
148
Steven Moreland5fb3d652016-11-03 13:45:18 -0700149 /**
150 * Access to this map doesn't need to be locked, since hwservicemanager
151 * is single-threaded.
152 *
153 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800154 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700155 * -> HidlService object
156 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800157 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800158 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800159 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700160 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700161};
162
163} // namespace implementation
Steven Morelandd83d1102016-10-25 15:01:47 -0700164} // namespace manager
165} // namespace hidl
166} // namespace android
167
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700168#endif // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H