blob: 92f0782ed4ab36e458f2914889239c4a1d6b7238 [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.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010032 Return<sp<IBase>> get(const hidl_string& fqName,
33 const hidl_string& name) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070034 Return<bool> add(const hidl_vec<hidl_string>& interfaceChain,
35 const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080036 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070037
Steven Moreland76237812016-11-08 15:59:04 -080038 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080039 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080040 listByInterface_cb _hidl_cb) override;
41
Steven Moreland2173d3c2016-11-09 15:00:58 -080042 Return<bool> registerForNotifications(const hidl_string& fqName,
43 const hidl_string& name,
44 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070045
Martijn Coenen46847a62016-12-19 05:36:12 +010046 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070047private:
Martijn Coenen46847a62016-12-19 05:36:12 +010048 bool remove(const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070049
Steven Moreland2173d3c2016-11-09 15:00:58 -080050 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 Moreland2173d3c2016-11-09 15:00:58 -080085 void sendPackageRegistrationNotification(
86 const hidl_string &fqName,
87 const hidl_string &instanceName) const;
88
Steven Moreland978473f2016-11-28 14:51:07 -080089 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080090 InstanceMap mInstanceMap{};
91
92 std::vector<sp<IServiceNotification>> mPackageListeners{};
93 };
94
Steven Moreland5fb3d652016-11-03 13:45:18 -070095 /**
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 Moreland2173d3c2016-11-09 15:00:58 -0800105 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700106 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700107};
108
109} // namespace implementation
110} // namespace V1_0
111} // namespace manager
112} // namespace hidl
113} // namespace android
114
Steven Moreland5fb3d652016-11-03 13:45:18 -0700115#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H