blob: 17d714335fdb69fbfbf196846fb6e278b6e77b14 [file] [log] [blame]
Steven Moreland6f4fbe12017-07-21 18:07:42 -07001#ifndef ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
2#define ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H
Steven Morelandd83d1102016-10-25 15:01:47 -07003
Steven Morelanddefbf322018-05-30 14:13:06 -07004#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Morelandd83d1102016-10-25 15:01:47 -07005#include <hidl/Status.h>
6#include <hidl/MQDescriptor.h>
Steven Moreland6d7e5002017-01-20 15:21:48 -08007#include <map>
Steven Morelandd83d1102016-10-25 15:01:47 -07008
Martijn Coenen7ce83be2017-04-07 16:19:32 -07009#include "AccessControl.h"
Steven Moreland2173d3c2016-11-09 15:00:58 -080010#include "HidlService.h"
11
Steven Morelandd83d1102016-10-25 15:01:47 -070012namespace android {
13namespace hidl {
14namespace manager {
Steven Morelandd83d1102016-10-25 15:01:47 -070015namespace implementation {
16
Martijn Coenen46847a62016-12-19 05:36:12 +010017using ::android::hardware::hidl_death_recipient;
Steven Morelandd83d1102016-10-25 15:01:47 -070018using ::android::hardware::hidl_vec;
19using ::android::hardware::hidl_string;
Steven Morelandd83d1102016-10-25 15:01:47 -070020using ::android::hardware::Return;
21using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080022using ::android::hidl::base::V1_0::IBase;
Steven Moreland2173d3c2016-11-09 15:00:58 -080023using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd8536202018-09-26 10:56:19 -070024using ::android::hidl::manager::V1_2::IClientCallback;
Steven Morelandd83d1102016-10-25 15:01:47 -070025using ::android::sp;
Martijn Coenen46847a62016-12-19 05:36:12 +010026using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070027
Steven Morelanddefbf322018-05-30 14:13:06 -070028struct ServiceManager : public V1_2::IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070029 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010030 Return<sp<IBase>> get(const hidl_string& fqName,
Steven Moreland37aed802017-04-06 09:16:42 -070031 const hidl_string& name) override;
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010032 Return<bool> add(const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080033 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070034
Steven Moreland37aed802017-04-06 09:16:42 -070035 Return<Transport> getTransport(const hidl_string& fqName,
36 const hidl_string& name);
37
Steven Moreland76237812016-11-08 15:59:04 -080038 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080039 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080040 listByInterface_cb _hidl_cb) override;
41
Steven Moreland2173d3c2016-11-09 15:00:58 -080042 Return<bool> registerForNotifications(const hidl_string& fqName,
43 const hidl_string& name,
44 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070045
Yifan Hong83c49f62017-01-25 14:16:34 -080046 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080047 Return<void> registerPassthroughClient(const hidl_string &fqName,
Martijn Coenen737de1b2017-04-27 09:29:43 -070048 const hidl_string &name) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080049
Steven Moreland6f4fbe12017-07-21 18:07:42 -070050 // Methods from ::android::hidl::manager::V1_1::IServiceManager follow.
51 Return<bool> unregisterForNotifications(const hidl_string& fqName,
52 const hidl_string& name,
53 const sp<IServiceNotification>& callback) override;
54
Steven Morelandd8536202018-09-26 10:56:19 -070055 // Methods from ::android::hidl::manager::V1_2::IServiceManager follow.
Steven Morelanddbaab552019-01-24 19:00:04 -080056 Return<bool> registerClientCallback(const hidl_string& fqName,
57 const hidl_string& name,
58 const sp<IBase>& server,
Steven Morelandd8536202018-09-26 10:56:19 -070059 const sp<IClientCallback>& cb) override;
60 Return<bool> unregisterClientCallback(const sp<IBase>& server,
61 const sp<IClientCallback>& cb) override;
Steven Moreland707f22c2018-10-11 12:09:34 -070062 Return<bool> addWithChain(const hidl_string& name,
63 const sp<IBase>& service,
64 const hidl_vec<hidl_string>& chain) override;
Steven Morelandaae4eab2018-10-29 13:00:31 -070065 Return<void> listManifestByInterface(const hidl_string& fqInstanceName,
66 listManifestByInterface_cb _hidl_cb) override;
Steven Morelanddbaab552019-01-24 19:00:04 -080067 Return<bool> tryUnregister(const hidl_string& fqName,
68 const hidl_string& name,
69 const sp<IBase>& service) override;
Steven Morelandd8536202018-09-26 10:56:19 -070070
71 void handleClientCallbacks();
72
Martijn Coenen46847a62016-12-19 05:36:12 +010073 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070074private:
Steven Moreland707f22c2018-10-11 12:09:34 -070075 bool addImpl(const hidl_string& name,
76 const sp<IBase>& service,
77 const hidl_vec<hidl_string>& interfaceChain,
78 const AccessControl::Context &context,
79 pid_t pid);
80
Steven Moreland76936fe2017-09-19 12:18:30 -070081 // if restrictToInstanceName is nullptr, remove all, otherwise only those services
82 // which match this instance name. Returns whether all instances were removed.
83 bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName);
Martijn Coenen7fafc142017-03-06 16:17:51 +010084 bool removePackageListener(const wp<IBase>& who);
85 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -080086 size_t countExistingService() const;
Steven Morelandd8536202018-09-26 10:56:19 -070087
88 // true = continue, false = break
89 void forEachExistingService(std::function<bool(const HidlService *)> f) const;
90 void forEachExistingService(std::function<bool(HidlService *)> f);
91 void forEachServiceEntry(std::function<bool(const HidlService *)> f) const;
92 void forEachServiceEntry(std::function<bool(HidlService *)> f);
Steven Morelandd83d1102016-10-25 15:01:47 -070093
Steven Morelanddbaab552019-01-24 19:00:04 -080094 HidlService* lookup(const std::string& fqName, const std::string& name);
95
Steven Moreland6d7e5002017-01-20 15:21:48 -080096 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080097 std::string, // instance name e.x. "manager"
98 std::unique_ptr<HidlService>
99 >;
100
101 struct PackageInterfaceMap {
102 InstanceMap &getInstanceMap();
103 const InstanceMap &getInstanceMap() const;
104
105 /**
Steven Morelandd544cf62017-01-04 15:24:32 -0800106 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -0800107 * returns nullptr. HidlService::getService() might also be nullptr
108 * if there are registered IServiceNotification objects for it. Return
109 * value should be treated as a temporary reference.
110 */
Steven Morelandd544cf62017-01-04 15:24:32 -0800111 HidlService *lookup(
112 const std::string &name);
113 const HidlService *lookup(
114 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800115
116 void insertService(std::unique_ptr<HidlService> &&service);
117
118 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +0100119 bool removePackageListener(const wp<IBase>& who);
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700120 bool removeServiceListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800121
Steven Moreland2173d3c2016-11-09 15:00:58 -0800122 void sendPackageRegistrationNotification(
123 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +0100124 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800125
Steven Moreland978473f2016-11-28 14:51:07 -0800126 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -0800127 InstanceMap mInstanceMap{};
128
129 std::vector<sp<IServiceNotification>> mPackageListeners{};
130 };
131
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700132 AccessControl mAcl;
133
Steven Moreland5fb3d652016-11-03 13:45:18 -0700134 /**
135 * Access to this map doesn't need to be locked, since hwservicemanager
136 * is single-threaded.
137 *
138 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800139 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700140 * -> HidlService object
141 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800142 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800143 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800144 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700145 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700146};
147
148} // namespace implementation
Steven Morelandd83d1102016-10-25 15:01:47 -0700149} // namespace manager
150} // namespace hidl
151} // namespace android
152
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700153#endif // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H