blob: 38bda57a6f3c2d8e2f5c0a5426e8d3ea5bc02f82 [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 Morelandd5f8f082019-06-06 12:59:20 -070017#pragma once
Steven Morelandd83d1102016-10-25 15:01:47 -070018
Steven Morelanddefbf322018-05-30 14:13:06 -070019#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Morelandd83d1102016-10-25 15:01:47 -070020#include <hidl/Status.h>
21#include <hidl/MQDescriptor.h>
Steven Moreland6d7e5002017-01-20 15:21:48 -080022#include <map>
Steven Morelandd83d1102016-10-25 15:01:47 -070023
Martijn Coenen7ce83be2017-04-07 16:19:32 -070024#include "AccessControl.h"
Steven Moreland2173d3c2016-11-09 15:00:58 -080025#include "HidlService.h"
26
Steven Morelandd83d1102016-10-25 15:01:47 -070027namespace android {
28namespace hidl {
29namespace manager {
Steven Morelandd83d1102016-10-25 15:01:47 -070030namespace implementation {
31
Martijn Coenen46847a62016-12-19 05:36:12 +010032using ::android::hardware::hidl_death_recipient;
Steven Morelandd83d1102016-10-25 15:01:47 -070033using ::android::hardware::hidl_vec;
34using ::android::hardware::hidl_string;
Steven Morelandd83d1102016-10-25 15:01:47 -070035using ::android::hardware::Return;
36using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080037using ::android::hidl::base::V1_0::IBase;
Steven Moreland2173d3c2016-11-09 15:00:58 -080038using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd8536202018-09-26 10:56:19 -070039using ::android::hidl::manager::V1_2::IClientCallback;
Steven Morelandd83d1102016-10-25 15:01:47 -070040using ::android::sp;
Martijn Coenen46847a62016-12-19 05:36:12 +010041using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070042
Steven Morelanddefbf322018-05-30 14:13:06 -070043struct ServiceManager : public V1_2::IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070044 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010045 Return<sp<IBase>> get(const hidl_string& fqName,
Steven Moreland37aed802017-04-06 09:16:42 -070046 const hidl_string& name) override;
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010047 Return<bool> add(const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080048 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070049
Steven Moreland37aed802017-04-06 09:16:42 -070050 Return<Transport> getTransport(const hidl_string& fqName,
51 const hidl_string& name);
52
Steven Moreland76237812016-11-08 15:59:04 -080053 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080054 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080055 listByInterface_cb _hidl_cb) override;
56
Steven Moreland2173d3c2016-11-09 15:00:58 -080057 Return<bool> registerForNotifications(const hidl_string& fqName,
58 const hidl_string& name,
59 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070060
Yifan Hong83c49f62017-01-25 14:16:34 -080061 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080062 Return<void> registerPassthroughClient(const hidl_string &fqName,
Martijn Coenen737de1b2017-04-27 09:29:43 -070063 const hidl_string &name) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080064
Steven Moreland6f4fbe12017-07-21 18:07:42 -070065 // 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 Morelandd8536202018-09-26 10:56:19 -070070 // Methods from ::android::hidl::manager::V1_2::IServiceManager follow.
Steven Morelanddbaab552019-01-24 19:00:04 -080071 Return<bool> registerClientCallback(const hidl_string& fqName,
72 const hidl_string& name,
73 const sp<IBase>& server,
Steven Morelandd8536202018-09-26 10:56:19 -070074 const sp<IClientCallback>& cb) override;
75 Return<bool> unregisterClientCallback(const sp<IBase>& server,
76 const sp<IClientCallback>& cb) override;
Steven Moreland707f22c2018-10-11 12:09:34 -070077 Return<bool> addWithChain(const hidl_string& name,
78 const sp<IBase>& service,
79 const hidl_vec<hidl_string>& chain) override;
Steven Morelandaae4eab2018-10-29 13:00:31 -070080 Return<void> listManifestByInterface(const hidl_string& fqInstanceName,
81 listManifestByInterface_cb _hidl_cb) override;
Steven Morelanddbaab552019-01-24 19:00:04 -080082 Return<bool> tryUnregister(const hidl_string& fqName,
83 const hidl_string& name,
84 const sp<IBase>& service) override;
Steven Morelandd8536202018-09-26 10:56:19 -070085
86 void handleClientCallbacks();
87
Martijn Coenen46847a62016-12-19 05:36:12 +010088 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070089private:
Steven Morelandd90d9f22019-03-20 13:47:30 -070090 bool addImpl(const std::string& name,
Steven Moreland707f22c2018-10-11 12:09:34 -070091 const sp<IBase>& service,
92 const hidl_vec<hidl_string>& interfaceChain,
Xin Libf44d5a2019-03-11 17:50:47 -070093 const AccessControl::CallingContext& callingContext);
Steven Moreland707f22c2018-10-11 12:09:34 -070094
Steven Moreland76936fe2017-09-19 12:18:30 -070095 // 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 Coenen7fafc142017-03-06 16:17:51 +010098 bool removePackageListener(const wp<IBase>& who);
99 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -0800100 size_t countExistingService() const;
Steven Morelandd8536202018-09-26 10:56:19 -0700101
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 Morelandd83d1102016-10-25 15:01:47 -0700107
Steven Morelanddbaab552019-01-24 19:00:04 -0800108 HidlService* lookup(const std::string& fqName, const std::string& name);
109
Steven Moreland6d7e5002017-01-20 15:21:48 -0800110 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -0800111 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 Morelandd544cf62017-01-04 15:24:32 -0800120 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -0800121 * 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 Morelandd544cf62017-01-04 15:24:32 -0800125 HidlService *lookup(
126 const std::string &name);
127 const HidlService *lookup(
128 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800129
130 void insertService(std::unique_ptr<HidlService> &&service);
131
132 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +0100133 bool removePackageListener(const wp<IBase>& who);
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700134 bool removeServiceListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800135
Steven Moreland2173d3c2016-11-09 15:00:58 -0800136 void sendPackageRegistrationNotification(
137 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +0100138 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800139
Steven Moreland978473f2016-11-28 14:51:07 -0800140 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -0800141 InstanceMap mInstanceMap{};
142
143 std::vector<sp<IServiceNotification>> mPackageListeners{};
144 };
145
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700146 AccessControl mAcl;
147
Steven Moreland5fb3d652016-11-03 13:45:18 -0700148 /**
149 * Access to this map doesn't need to be locked, since hwservicemanager
150 * is single-threaded.
151 *
152 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800153 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700154 * -> HidlService object
155 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800156 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800157 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800158 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700159 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700160};
161
162} // namespace implementation
Steven Morelandd83d1102016-10-25 15:01:47 -0700163} // namespace manager
164} // namespace hidl
165} // namespace android