blob: 1c5ddc2412e7af5089bee9752fdebb2aea9fd89f [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
18using ::android::hardware::hidl_vec;
19using ::android::hardware::hidl_string;
20using ::android::hardware::hidl_version;
21using ::android::hardware::IBinder;
22using ::android::hardware::Return;
23using ::android::hardware::Void;
24using ::android::hidl::manager::V1_0::IServiceManager;
Steven Moreland2173d3c2016-11-09 15:00:58 -080025using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd83d1102016-10-25 15:01:47 -070026using ::android::sp;
27
Steven Morelandd83d1102016-10-25 15:01:47 -070028struct ServiceManager : public IServiceManager {
29 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Steven Moreland5fb3d652016-11-03 13:45:18 -070030 Return<void> get(const hidl_string& fqName,
31 const hidl_string& name,
32 get_cb _hidl_cb) override;
33 Return<bool> add(const hidl_vec<hidl_string>& interfaceChain,
34 const hidl_string& name,
35 const sp<IBinder>& service) override;
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
45private:
46
Steven Moreland2173d3c2016-11-09 15:00:58 -080047 using InstanceMap = std::multimap<
48 std::string, // instance name e.x. "manager"
49 std::unique_ptr<HidlService>
50 >;
51
52 struct PackageInterfaceMap {
53 InstanceMap &getInstanceMap();
54 const InstanceMap &getInstanceMap() const;
55
56 /**
57 * Finds a HidlService which supports the desired version. If none,
58 * returns nullptr. HidlService::getService() might also be nullptr
59 * if there are registered IServiceNotification objects for it. Return
60 * value should be treated as a temporary reference.
61 */
62 HidlService *lookupSupporting(
63 const std::string &name,
64 const hidl_version &version);
65 const HidlService *lookupSupporting(
66 const std::string &name,
67 const hidl_version &version) const;
68 /**
69 * Finds a HidlService which is exactly the desired version. If none,
70 * returns nullptr. HidlService::getService() might also be nullptr
71 * if there are registered IServiceNotification objects for it. Return
72 * value should be treated as a temporary reference.
73 */
74 HidlService *lookupExact(
75 const std::string &name,
76 const hidl_version &version);
77
78 void insertService(std::unique_ptr<HidlService> &&service);
79
80 void addPackageListener(sp<IServiceNotification> listener);
81
Steven Moreland2173d3c2016-11-09 15:00:58 -080082 void sendPackageRegistrationNotification(
83 const hidl_string &fqName,
84 const hidl_string &instanceName) const;
85
Steven Moreland978473f2016-11-28 14:51:07 -080086 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080087 InstanceMap mInstanceMap{};
88
89 std::vector<sp<IServiceNotification>> mPackageListeners{};
90 };
91
Steven Moreland5fb3d652016-11-03 13:45:18 -070092 /**
93 * Access to this map doesn't need to be locked, since hwservicemanager
94 * is single-threaded.
95 *
96 * e.x.
97 * mServiceMap["android.hidl.manager::IServiceManager"]["manager"]
98 * -> HidlService object
99 */
100 std::unordered_map<
101 std::string, // package::interface e.x. "android.hidl.manager::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800102 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700103 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700104};
105
106} // namespace implementation
107} // namespace V1_0
108} // namespace manager
109} // namespace hidl
110} // namespace android
111
Steven Moreland5fb3d652016-11-03 13:45:18 -0700112#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H