blob: 66225c525bb86df55088a0c875adc63eb31e911f [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 Moreland6f4fbe12017-07-21 18:07:42 -07004#include <android/hidl/manager/1.1/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
Steven Moreland2173d3c2016-11-09 15:00:58 -08009#include "HidlService.h"
10
Steven Morelandd83d1102016-10-25 15:01:47 -070011namespace android {
12namespace hidl {
13namespace manager {
Steven Morelandd83d1102016-10-25 15:01:47 -070014namespace implementation {
15
Martijn Coenen46847a62016-12-19 05:36:12 +010016using ::android::hardware::hidl_death_recipient;
Steven Morelandd83d1102016-10-25 15:01:47 -070017using ::android::hardware::hidl_vec;
18using ::android::hardware::hidl_string;
Steven Morelandd83d1102016-10-25 15:01:47 -070019using ::android::hardware::Return;
20using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080021using ::android::hidl::base::V1_0::IBase;
Steven Moreland6f4fbe12017-07-21 18:07:42 -070022using ::android::hidl::manager::V1_1::IServiceManager;
Steven Moreland2173d3c2016-11-09 15:00:58 -080023using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd83d1102016-10-25 15:01:47 -070024using ::android::sp;
Martijn Coenen46847a62016-12-19 05:36:12 +010025using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070026
Martijn Coenen46847a62016-12-19 05:36:12 +010027struct ServiceManager : public IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070028 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010029 Return<sp<IBase>> get(const hidl_string& fqName,
Steven Morelandf58feb72017-04-06 09:16:42 -070030 const hidl_string& name) override;
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010031 Return<bool> add(const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080032 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070033
Steven Morelandf58feb72017-04-06 09:16:42 -070034 Return<Transport> getTransport(const hidl_string& fqName,
35 const hidl_string& name);
36
Steven Moreland76237812016-11-08 15:59:04 -080037 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080038 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080039 listByInterface_cb _hidl_cb) override;
40
Steven Moreland2173d3c2016-11-09 15:00:58 -080041 Return<bool> registerForNotifications(const hidl_string& fqName,
42 const hidl_string& name,
43 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070044
Yifan Hong83c49f62017-01-25 14:16:34 -080045 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080046 Return<void> registerPassthroughClient(const hidl_string &fqName,
Martijn Coenenc93fb342017-04-27 09:29:43 -070047 const hidl_string &name) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080048
Steven Moreland6f4fbe12017-07-21 18:07:42 -070049 // Methods from ::android::hidl::manager::V1_1::IServiceManager follow.
50 Return<bool> unregisterForNotifications(const hidl_string& fqName,
51 const hidl_string& name,
52 const sp<IServiceNotification>& callback) override;
53
Martijn Coenen46847a62016-12-19 05:36:12 +010054 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070055private:
Steven Moreland6f4fbe12017-07-21 18:07:42 -070056 bool removeService(const wp<IBase>& who);
Martijn Coenen7fafc142017-03-06 16:17:51 +010057 bool removePackageListener(const wp<IBase>& who);
58 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -080059 size_t countExistingService() const;
60 void forEachExistingService(std::function<void(const HidlService *)> f) const;
Yifan Hongee531a82017-02-03 15:10:18 -080061 void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
Steven Morelandd83d1102016-10-25 15:01:47 -070062
Steven Moreland6d7e5002017-01-20 15:21:48 -080063 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080064 std::string, // instance name e.x. "manager"
65 std::unique_ptr<HidlService>
66 >;
67
68 struct PackageInterfaceMap {
69 InstanceMap &getInstanceMap();
70 const InstanceMap &getInstanceMap() const;
71
72 /**
Steven Morelandd544cf62017-01-04 15:24:32 -080073 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -080074 * returns nullptr. HidlService::getService() might also be nullptr
75 * if there are registered IServiceNotification objects for it. Return
76 * value should be treated as a temporary reference.
77 */
Steven Morelandd544cf62017-01-04 15:24:32 -080078 HidlService *lookup(
79 const std::string &name);
80 const HidlService *lookup(
81 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080082
83 void insertService(std::unique_ptr<HidlService> &&service);
84
85 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +010086 bool removePackageListener(const wp<IBase>& who);
Steven Moreland6f4fbe12017-07-21 18:07:42 -070087 bool removeServiceListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -080088
Steven Moreland2173d3c2016-11-09 15:00:58 -080089 void sendPackageRegistrationNotification(
90 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +010091 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -080092
Steven Moreland978473f2016-11-28 14:51:07 -080093 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080094 InstanceMap mInstanceMap{};
95
96 std::vector<sp<IServiceNotification>> mPackageListeners{};
97 };
98
Steven Moreland5fb3d652016-11-03 13:45:18 -070099 /**
100 * Access to this map doesn't need to be locked, since hwservicemanager
101 * is single-threaded.
102 *
103 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800104 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700105 * -> HidlService object
106 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800107 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800108 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800109 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700110 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700111};
112
113} // namespace implementation
Steven Morelandd83d1102016-10-25 15:01:47 -0700114} // namespace manager
115} // namespace hidl
116} // namespace android
117
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700118#endif // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H