blob: 016100952ca3d0ccaf53ac3637b722f645ff3e6a [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>
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 {
14namespace V1_0 {
15namespace implementation {
16
Martijn Coenen46847a62016-12-19 05:36:12 +010017using ::android::hardware::hidl_death_recipient;
Steven Morelandd83d1102016-10-25 15:01:47 -070018using ::android::hardware::hidl_vec;
19using ::android::hardware::hidl_string;
Steven Morelandd83d1102016-10-25 15:01:47 -070020using ::android::hardware::Return;
21using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080022using ::android::hidl::base::V1_0::IBase;
Steven Morelandd83d1102016-10-25 15:01:47 -070023using ::android::hidl::manager::V1_0::IServiceManager;
Steven Moreland2173d3c2016-11-09 15:00:58 -080024using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Morelandd83d1102016-10-25 15:01:47 -070025using ::android::sp;
Martijn Coenen46847a62016-12-19 05:36:12 +010026using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070027
Martijn Coenen46847a62016-12-19 05:36:12 +010028struct ServiceManager : public IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070029 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010030 Return<sp<IBase>> get(const hidl_string& fqName,
31 const hidl_string& name) override;
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010032 Return<bool> add(const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080033 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070034
Steven Moreland76237812016-11-08 15:59:04 -080035 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080036 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080037 listByInterface_cb _hidl_cb) override;
38
Steven Moreland2173d3c2016-11-09 15:00:58 -080039 Return<bool> registerForNotifications(const hidl_string& fqName,
40 const hidl_string& name,
41 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070042
Yifan Hong83c49f62017-01-25 14:16:34 -080043 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080044 Return<void> registerPassthroughClient(const hidl_string &fqName,
45 const hidl_string &name, int32_t pid) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080046
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);
Martijn Coenen7fafc142017-03-06 16:17:51 +010050 bool removePackageListener(const wp<IBase>& who);
51 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -080052 size_t countExistingService() const;
53 void forEachExistingService(std::function<void(const HidlService *)> f) const;
Yifan Hongee531a82017-02-03 15:10:18 -080054 void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
Steven Morelandd83d1102016-10-25 15:01:47 -070055
Steven Moreland6d7e5002017-01-20 15:21:48 -080056 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080057 std::string, // instance name e.x. "manager"
58 std::unique_ptr<HidlService>
59 >;
60
61 struct PackageInterfaceMap {
62 InstanceMap &getInstanceMap();
63 const InstanceMap &getInstanceMap() const;
64
65 /**
Steven Morelandd544cf62017-01-04 15:24:32 -080066 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -080067 * returns nullptr. HidlService::getService() might also be nullptr
68 * if there are registered IServiceNotification objects for it. Return
69 * value should be treated as a temporary reference.
70 */
Steven Morelandd544cf62017-01-04 15:24:32 -080071 HidlService *lookup(
72 const std::string &name);
73 const HidlService *lookup(
74 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080075
76 void insertService(std::unique_ptr<HidlService> &&service);
77
78 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +010079 bool removePackageListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -080080
Steven Moreland2173d3c2016-11-09 15:00:58 -080081 void sendPackageRegistrationNotification(
82 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +010083 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -080084
Steven Moreland978473f2016-11-28 14:51:07 -080085 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080086 InstanceMap mInstanceMap{};
87
88 std::vector<sp<IServiceNotification>> mPackageListeners{};
89 };
90
Steven Moreland5fb3d652016-11-03 13:45:18 -070091 /**
92 * Access to this map doesn't need to be locked, since hwservicemanager
93 * is single-threaded.
94 *
95 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -080096 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -070097 * -> HidlService object
98 */
Steven Moreland6d7e5002017-01-20 15:21:48 -080099 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800100 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800101 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700102 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700103};
104
105} // namespace implementation
106} // namespace V1_0
107} // namespace manager
108} // namespace hidl
109} // namespace android
110
Steven Moreland5fb3d652016-11-03 13:45:18 -0700111#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H