blob: 86fffcf8177ee97ceeeeea0058bb712280c6efa6 [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
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 {
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;
Steven Morelandd83d1102016-10-25 15:01:47 -070021using ::android::hardware::Return;
22using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080023using ::android::hidl::base::V1_0::IBase;
Steven Morelandd83d1102016-10-25 15:01:47 -070024using ::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;
Martijn Coenen46847a62016-12-19 05:36:12 +010027using ::android::wp;
Steven Morelandd83d1102016-10-25 15:01:47 -070028
Martijn Coenen46847a62016-12-19 05:36:12 +010029struct ServiceManager : public IServiceManager, hidl_death_recipient {
Steven Morelandd83d1102016-10-25 15:01:47 -070030 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Martijn Coenen7b02bf92017-01-02 15:17:58 +010031 Return<sp<IBase>> get(const hidl_string& fqName,
Steven Moreland37aed802017-04-06 09:16:42 -070032 const hidl_string& name) override;
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010033 Return<bool> add(const hidl_string& name,
Yifan Hongb3a90f02016-11-23 12:58:04 -080034 const sp<IBase>& service) override;
Steven Moreland5fb3d652016-11-03 13:45:18 -070035
Steven Moreland37aed802017-04-06 09:16:42 -070036 Return<Transport> getTransport(const hidl_string& fqName,
37 const hidl_string& name);
38
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
Yifan Hong83c49f62017-01-25 14:16:34 -080047 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080048 Return<void> registerPassthroughClient(const hidl_string &fqName,
49 const hidl_string &name, int32_t pid) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080050
Martijn Coenen46847a62016-12-19 05:36:12 +010051 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070052private:
Martijn Coenen46847a62016-12-19 05:36:12 +010053 bool remove(const wp<IBase>& who);
Martijn Coenen7fafc142017-03-06 16:17:51 +010054 bool removePackageListener(const wp<IBase>& who);
55 bool removeServiceListener(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -080056 size_t countExistingService() const;
57 void forEachExistingService(std::function<void(const HidlService *)> f) const;
Yifan Hongee531a82017-02-03 15:10:18 -080058 void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
Steven Morelandd83d1102016-10-25 15:01:47 -070059
Steven Moreland6d7e5002017-01-20 15:21:48 -080060 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080061 std::string, // instance name e.x. "manager"
62 std::unique_ptr<HidlService>
63 >;
64
65 struct PackageInterfaceMap {
66 InstanceMap &getInstanceMap();
67 const InstanceMap &getInstanceMap() const;
68
69 /**
Steven Morelandd544cf62017-01-04 15:24:32 -080070 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -080071 * returns nullptr. HidlService::getService() might also be nullptr
72 * if there are registered IServiceNotification objects for it. Return
73 * value should be treated as a temporary reference.
74 */
Steven Morelandd544cf62017-01-04 15:24:32 -080075 HidlService *lookup(
76 const std::string &name);
77 const HidlService *lookup(
78 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080079
80 void insertService(std::unique_ptr<HidlService> &&service);
81
82 void addPackageListener(sp<IServiceNotification> listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +010083 bool removePackageListener(const wp<IBase>& who);
Steven Moreland2173d3c2016-11-09 15:00:58 -080084
Steven Moreland2173d3c2016-11-09 15:00:58 -080085 void sendPackageRegistrationNotification(
86 const hidl_string &fqName,
Martijn Coenen7fafc142017-03-06 16:17:51 +010087 const hidl_string &instanceName);
Steven Moreland2173d3c2016-11-09 15:00:58 -080088
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
Martijn Coenen7ce83be2017-04-07 16:19:32 -070095 AccessControl mAcl;
96
Steven Moreland5fb3d652016-11-03 13:45:18 -070097 /**
98 * Access to this map doesn't need to be locked, since hwservicemanager
99 * is single-threaded.
100 *
101 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -0800102 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -0700103 * -> HidlService object
104 */
Steven Moreland6d7e5002017-01-20 15:21:48 -0800105 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -0800106 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -0800107 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700108 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700109};
110
111} // namespace implementation
112} // namespace V1_0
113} // namespace manager
114} // namespace hidl
115} // namespace android
116
Steven Moreland5fb3d652016-11-03 13:45:18 -0700117#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H