1 #ifndef ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H 2 #define ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H 3 4 #include <android/hidl/manager/1.0/IServiceManager.h> 5 #include <hidl/Status.h> 6 #include <hidl/MQDescriptor.h> 7 #include <map> 8 9 #include "AccessControl.h" 10 #include "HidlService.h" 11 12 namespace android { 13 namespace hidl { 14 namespace manager { 15 namespace V1_0 { 16 namespace implementation { 17 18 using ::android::hardware::hidl_death_recipient; 19 using ::android::hardware::hidl_vec; 20 using ::android::hardware::hidl_string; 21 using ::android::hardware::Return; 22 using ::android::hardware::Void; 23 using ::android::hidl::base::V1_0::IBase; 24 using ::android::hidl::manager::V1_0::IServiceManager; 25 using ::android::hidl::manager::V1_0::IServiceNotification; 26 using ::android::sp; 27 using ::android::wp; 28 29 struct ServiceManager : public IServiceManager, hidl_death_recipient { 30 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow. 31 Return<sp<IBase>> get(const hidl_string& fqName, 32 const hidl_string& name) override; 33 Return<bool> add(const hidl_string& name, 34 const sp<IBase>& service) override; 35 36 Return<Transport> getTransport(const hidl_string& fqName, 37 const hidl_string& name); 38 39 Return<void> list(list_cb _hidl_cb) override; 40 Return<void> listByInterface(const hidl_string& fqInstanceName, 41 listByInterface_cb _hidl_cb) override; 42 43 Return<bool> registerForNotifications(const hidl_string& fqName, 44 const hidl_string& name, 45 const sp<IServiceNotification>& callback) override; 46 47 Return<void> debugDump(debugDump_cb _cb) override; 48 Return<void> registerPassthroughClient(const hidl_string &fqName, 49 const hidl_string &name) override; 50 51 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who); 52 private: 53 bool remove(const wp<IBase>& who); 54 bool removePackageListener(const wp<IBase>& who); 55 bool removeServiceListener(const wp<IBase>& who); 56 size_t countExistingService() const; 57 void forEachExistingService(std::function<void(const HidlService *)> f) const; 58 void forEachServiceEntry(std::function<void(const HidlService *)> f) const; 59 60 using InstanceMap = std::map< 61 std::string, // instance name e.x. "manager" 62 std::unique_ptr<HidlService> 63 >; 64 65 struct PackageInterfaceMap { 66 InstanceMap &getInstanceMap(); 67 const InstanceMap &getInstanceMap() const; 68 69 /** 70 * Finds a HidlService with the desired name. If none, 71 * returns nullptr. HidlService::getService() might also be nullptr 72 * if there are registered IServiceNotification objects for it. Return 73 * value should be treated as a temporary reference. 74 */ 75 HidlService *lookup( 76 const std::string &name); 77 const HidlService *lookup( 78 const std::string &name) const; 79 80 void insertService(std::unique_ptr<HidlService> &&service); 81 82 void addPackageListener(sp<IServiceNotification> listener); 83 bool removePackageListener(const wp<IBase>& who); 84 85 void sendPackageRegistrationNotification( 86 const hidl_string &fqName, 87 const hidl_string &instanceName); 88 89 private: 90 InstanceMap mInstanceMap{}; 91 92 std::vector<sp<IServiceNotification>> mPackageListeners{}; 93 }; 94 95 AccessControl mAcl; 96 97 /** 98 * Access to this map doesn't need to be locked, since hwservicemanager 99 * is single-threaded. 100 * 101 * e.x. 102 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"] 103 * -> HidlService object 104 */ 105 std::map< 106 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager" 107 PackageInterfaceMap 108 > mServiceMap; 109 }; 110 111 } // namespace implementation 112 } // namespace V1_0 113 } // namespace manager 114 } // namespace hidl 115 } // namespace android 116 117 #endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H 118