blob: 399c4134d4811eaed939604acc465b6f2220642e [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,
94 const AccessControl::Context &context,
95 pid_t pid);
96
Steven Moreland76936fe2017-09-19 12:18:30 -070097 // if restrictToInstanceName is nullptr, remove all, otherwise only those services
98 // which match this instance name. Returns whether all instances were removed.
99 bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName);
Martijn Coenen7fafc142017-03-06 16:17:51 +0100100 bool removePackageListener(const wp<IBase>& who);
101 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -0800102 size_t countExistingService() const;
Steven Morelandd8536202018-09-26 10:56:19 -0700103
104 // true = continue, false = break
105 void forEachExistingService(std::function<bool(const HidlService *)> f) const;
106 void forEachExistingService(std::function<bool(HidlService *)> f);
107 void forEachServiceEntry(std::function<bool(const HidlService *)> f) const;
108 void forEachServiceEntry(std::function<bool(HidlService *)> f);
Steven Morelandd83d1102016-10-25 15:01:47 -0700109
Steven Morelanddbaab552019-01-24 19:00:04 -0800110 HidlService* lookup(const std::string& fqName, const std::string& name);
111
Steven Moreland6d7e5002017-01-20 15:21:48 -0800112 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -0800113 std::string, // instance name e.x. "manager"
114 std::unique_ptr<HidlService>
115 >;
116
117 struct PackageInterfaceMap {
118 InstanceMap &getInstanceMap();
119 const InstanceMap &getInstanceMap() const;
120
121 /**
Steven Morelandd544cf62017-01-04 15:24:32 -0800122 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -0800123 * returns nullptr. HidlService::getService() might also be nullptr
124 * if there are registered IServiceNotification objects for it. Return
125 * value should be treated as a temporary reference.
126 */
Steven Morelandd544cf62017-01-04 15:24:32 -0800127 HidlService *lookup(
128 const std::string &name);
129 const HidlService *lookup(
130 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800131
132 void insertService(std::unique_ptr<HidlService> &&service);
133
134 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +0100135 bool removePackageListener(const wp<IBase>& who);
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700136 bool removeServiceListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800137
Steven Moreland2173d3c2016-11-09 15:00:58 -0800138 void sendPackageRegistrationNotification(
139 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +0100140 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800141
Steven Moreland978473f2016-11-28 14:51:07 -0800142 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -0800143 InstanceMap mInstanceMap{};
144
145 std::vector<sp<IServiceNotification>> mPackageListeners{};
146 };
147
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700148 AccessControl mAcl;
149
Steven Moreland5fb3d652016-11-03 13:45:18 -0700150 /**
151 * Access to this map doesn't need to be locked, since hwservicemanager
152 * is single-threaded.
153 *
154 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800155 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700156 * -> HidlService object
157 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800158 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800159 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800160 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700161 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700162};
163
164} // namespace implementation
Steven Morelandd83d1102016-10-25 15:01:47 -0700165} // namespace manager
166} // namespace hidl
167} // namespace android
168
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700169#endif // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H