blob: 0007de994f035253df06d759e8c3fcf817016f9f [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 Morelanddefbf322018-05-30 14:13:06 -07004#include <android/hidl/manager/1.2/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
Martijn Coenen7ce83be2017-04-07 16:19:32 -07009#include "AccessControl.h"
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 {
Steven Morelandd83d1102016-10-25 15:01:47 -070015namespace 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 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
Steven Morelanddefbf322018-05-30 14:13:06 -070027struct ServiceManager : public V1_2::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 Moreland37aed802017-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 Moreland37aed802017-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 Coenen737de1b2017-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 Moreland76936fe2017-09-19 12:18:30 -070056 // if restrictToInstanceName is nullptr, remove all, otherwise only those services
57 // which match this instance name. Returns whether all instances were removed.
58 bool removeService(const wp<IBase>& who, const std::string* restrictToInstanceName);
Martijn Coenen7fafc142017-03-06 16:17:51 +010059 bool removePackageListener(const wp<IBase>& who);
60 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -080061 size_t countExistingService() const;
62 void forEachExistingService(std::function<void(const HidlService *)> f) const;
Yifan Hongee531a82017-02-03 15:10:18 -080063 void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
Steven Morelandd83d1102016-10-25 15:01:47 -070064
Steven Moreland6d7e5002017-01-20 15:21:48 -080065 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080066 std::string, // instance name e.x. "manager"
67 std::unique_ptr<HidlService>
68 >;
69
70 struct PackageInterfaceMap {
71 InstanceMap &getInstanceMap();
72 const InstanceMap &getInstanceMap() const;
73
74 /**
Steven Morelandd544cf62017-01-04 15:24:32 -080075 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -080076 * returns nullptr. HidlService::getService() might also be nullptr
77 * if there are registered IServiceNotification objects for it. Return
78 * value should be treated as a temporary reference.
79 */
Steven Morelandd544cf62017-01-04 15:24:32 -080080 HidlService *lookup(
81 const std::string &name);
82 const HidlService *lookup(
83 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080084
85 void insertService(std::unique_ptr<HidlService> &&service);
86
87 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +010088 bool removePackageListener(const wp<IBase>& who);
Steven Moreland6f4fbe12017-07-21 18:07:42 -070089 bool removeServiceListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -080090
Steven Moreland2173d3c2016-11-09 15:00:58 -080091 void sendPackageRegistrationNotification(
92 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +010093 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -080094
Steven Moreland978473f2016-11-28 14:51:07 -080095 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080096 InstanceMap mInstanceMap{};
97
98 std::vector<sp<IServiceNotification>> mPackageListeners{};
99 };
100
Martijn Coenen7ce83be2017-04-07 16:19:32 -0700101 AccessControl mAcl;
102
Steven Moreland5fb3d652016-11-03 13:45:18 -0700103 /**
104 * Access to this map doesn't need to be locked, since hwservicemanager
105 * is single-threaded.
106 *
107 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800108 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700109 * -> HidlService object
110 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800111 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800112 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800113 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700114 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700115};
116
117} // namespace implementation
Steven Morelandd83d1102016-10-25 15:01:47 -0700118} // namespace manager
119} // namespace hidl
120} // namespace android
121
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700122#endif // ANDROID_HARDWARE_MANAGER_SERVICEMANAGER_H