Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 1 | #ifndef ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H |
| 2 | #define ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 3 | |
| 4 | #include <android/hidl/manager/1.0/IServiceManager.h> |
| 5 | #include <hidl/Status.h> |
| 6 | #include <hidl/MQDescriptor.h> |
| 7 | #include <map> |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 8 | #include <unordered_map> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 9 | |
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 { |
| 15 | namespace V1_0 { |
| 16 | namespace implementation { |
| 17 | |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 18 | using ::android::hardware::hidl_death_recipient; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 19 | using ::android::hardware::hidl_vec; |
| 20 | using ::android::hardware::hidl_string; |
| 21 | using ::android::hardware::hidl_version; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 22 | using ::android::hardware::Return; |
| 23 | using ::android::hardware::Void; |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 24 | using ::android::hidl::base::V1_0::IBase; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 25 | using ::android::hidl::manager::V1_0::IServiceManager; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 26 | using ::android::hidl::manager::V1_0::IServiceNotification; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 27 | using ::android::sp; |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 28 | using ::android::wp; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 29 | |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 30 | struct ServiceManager : public IServiceManager, hidl_death_recipient { |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 31 | // Methods from ::android::hidl::manager::V1_0::IServiceManager follow. |
Martijn Coenen | 7b02bf9 | 2017-01-02 15:17:58 +0100 | [diff] [blame^] | 32 | Return<sp<IBase>> get(const hidl_string& fqName, |
| 33 | const hidl_string& name) override; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 34 | Return<bool> add(const hidl_vec<hidl_string>& interfaceChain, |
| 35 | const hidl_string& name, |
Yifan Hong | b3a90f0 | 2016-11-23 12:58:04 -0800 | [diff] [blame] | 36 | const sp<IBase>& service) override; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 46 | virtual void serviceDied(uint64_t cookie, const wp<IBase>& who); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 47 | private: |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 48 | bool remove(const wp<IBase>& who); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 49 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 50 | using InstanceMap = std::multimap< |
| 51 | std::string, // instance name e.x. "manager" |
| 52 | std::unique_ptr<HidlService> |
| 53 | >; |
| 54 | |
| 55 | struct PackageInterfaceMap { |
| 56 | InstanceMap &getInstanceMap(); |
| 57 | const InstanceMap &getInstanceMap() const; |
| 58 | |
| 59 | /** |
| 60 | * Finds a HidlService which supports the desired version. If none, |
| 61 | * returns nullptr. HidlService::getService() might also be nullptr |
| 62 | * if there are registered IServiceNotification objects for it. Return |
| 63 | * value should be treated as a temporary reference. |
| 64 | */ |
| 65 | HidlService *lookupSupporting( |
| 66 | const std::string &name, |
| 67 | const hidl_version &version); |
| 68 | const HidlService *lookupSupporting( |
| 69 | const std::string &name, |
| 70 | const hidl_version &version) const; |
| 71 | /** |
| 72 | * Finds a HidlService which is exactly the desired version. If none, |
| 73 | * returns nullptr. HidlService::getService() might also be nullptr |
| 74 | * if there are registered IServiceNotification objects for it. Return |
| 75 | * value should be treated as a temporary reference. |
| 76 | */ |
| 77 | HidlService *lookupExact( |
| 78 | const std::string &name, |
| 79 | const hidl_version &version); |
| 80 | |
| 81 | void insertService(std::unique_ptr<HidlService> &&service); |
| 82 | |
| 83 | void addPackageListener(sp<IServiceNotification> listener); |
| 84 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 85 | void sendPackageRegistrationNotification( |
| 86 | const hidl_string &fqName, |
| 87 | const hidl_string &instanceName) const; |
| 88 | |
Steven Moreland | 978473f | 2016-11-28 14:51:07 -0800 | [diff] [blame] | 89 | private: |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 90 | InstanceMap mInstanceMap{}; |
| 91 | |
| 92 | std::vector<sp<IServiceNotification>> mPackageListeners{}; |
| 93 | }; |
| 94 | |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Access to this map doesn't need to be locked, since hwservicemanager |
| 97 | * is single-threaded. |
| 98 | * |
| 99 | * e.x. |
| 100 | * mServiceMap["android.hidl.manager::IServiceManager"]["manager"] |
| 101 | * -> HidlService object |
| 102 | */ |
| 103 | std::unordered_map< |
| 104 | std::string, // package::interface e.x. "android.hidl.manager::IServiceManager" |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 105 | PackageInterfaceMap |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 106 | > mServiceMap; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace implementation |
| 110 | } // namespace V1_0 |
| 111 | } // namespace manager |
| 112 | } // namespace hidl |
| 113 | } // namespace android |
| 114 | |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 115 | #endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H |