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