blob: f0132af03700bc949960e959f9d5c1b7b5e4a1ed [file] [log] [blame]
Steven Moreland5fb3d652016-11-03 13:45:18 -07001#ifndef ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H
2#define ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H
Steven Morelandd83d1102016-10-25 15:01:47 -07003
4#include <android/hidl/manager/1.0/IServiceManager.h>
5#include <hidl/Status.h>
6#include <hidl/MQDescriptor.h>
7#include <map>
Steven Moreland5fb3d652016-11-03 13:45:18 -07008#include <unordered_map>
Steven Morelandd83d1102016-10-25 15:01:47 -07009
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 {
15namespace V1_0 {
16namespace implementation {
17
Martijn Coenen46847a62016-12-19 05:36:12 +010018using ::android::hardware::hidl_death_recipient;
Steven Morelandd83d1102016-10-25 15:01:47 -070019using ::android::hardware::hidl_vec;
20using ::android::hardware::hidl_string;
21using ::android::hardware::hidl_version;
Steven Morelandd83d1102016-10-25 15:01:47 -070022using ::android::hardware::Return;
23using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080024using ::android::hidl::base::V1_0::IBase;
Steven Morelandd83d1102016-10-25 15:01:47 -070025using ::android::hidl::manager::V1_0::IServiceManager;
Steven Moreland2173d3c2016-11-09 15:00:58 -080026using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd83d1102016-10-25 15:01:47 -070027using ::android::sp;
Martijn Coenen46847a62016-12-19 05:36:12 +010028using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070029
Martijn Coenen46847a62016-12-19 05:36:12 +010030struct ServiceManager : public IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070031 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Steven Moreland5fb3d652016-11-03 13:45:18 -070032 Return<void> get(const hidl_string& fqName,
33 const hidl_string& name,
34 get_cb _hidl_cb) override;
35 Return<bool> add(const hidl_vec<hidl_string>& interfaceChain,
36 const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080037 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070038
Steven Moreland76237812016-11-08 15:59:04 -080039 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080040 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080041 listByInterface_cb _hidl_cb) override;
42
Steven Moreland2173d3c2016-11-09 15:00:58 -080043 Return<bool> registerForNotifications(const hidl_string& fqName,
44 const hidl_string& name,
45 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070046
Martijn Coenen46847a62016-12-19 05:36:12 +010047 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070048private:
Martijn Coenen46847a62016-12-19 05:36:12 +010049 bool remove(const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070050
Steven Moreland2173d3c2016-11-09 15:00:58 -080051 using InstanceMap = std::multimap<
52 std::string, // instance name e.x. "manager"
53 std::unique_ptr<HidlService>
54 >;
55
56 struct PackageInterfaceMap {
57 InstanceMap &getInstanceMap();
58 const InstanceMap &getInstanceMap() const;
59
60 /**
61 * Finds a HidlService which supports the desired version. If none,
62 * returns nullptr. HidlService::getService() might also be nullptr
63 * if there are registered IServiceNotification objects for it. Return
64 * value should be treated as a temporary reference.
65 */
66 HidlService *lookupSupporting(
67 const std::string &name,
68 const hidl_version &version);
69 const HidlService *lookupSupporting(
70 const std::string &name,
71 const hidl_version &version) const;
72 /**
73 * Finds a HidlService which is exactly the desired version. If none,
74 * 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 */
78 HidlService *lookupExact(
79 const std::string &name,
80 const hidl_version &version);
81
82 void insertService(std::unique_ptr<HidlService> &&service);
83
84 void addPackageListener(sp<IServiceNotification> listener);
85
Steven Moreland2173d3c2016-11-09 15:00:58 -080086 void sendPackageRegistrationNotification(
87 const hidl_string &fqName,
88 const hidl_string &instanceName) const;
89
Steven Moreland978473f2016-11-28 14:51:07 -080090 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080091 InstanceMap mInstanceMap{};
92
93 std::vector<sp<IServiceNotification>> mPackageListeners{};
94 };
95
Steven Moreland5fb3d652016-11-03 13:45:18 -070096 /**
97 * Access to this map doesn't need to be locked, since hwservicemanager
98 * is single-threaded.
99 *
100 * e.x.
101 * mServiceMap["android.hidl.manager::IServiceManager"]["manager"]
102 * -> HidlService object
103 */
104 std::unordered_map<
105 std::string, // package::interface e.x. "android.hidl.manager::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800106 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700107 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700108};
109
110} // namespace implementation
111} // namespace V1_0
112} // namespace manager
113} // namespace hidl
114} // namespace android
115
Steven Moreland5fb3d652016-11-03 13:45:18 -0700116#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H