blob: 952117ea4e78c49f71680af9a0bc410063d75752 [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;
Steven Moreland5fb3d652016-11-03 13:45:18 -070032 Return<bool> add(const hidl_vec<hidl_string>& interfaceChain,
33 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 Moreland76237812016-11-08 15:59:04 -080036 Return<void> list(list_cb _hidl_cb) override;
Steven Moreland2173d3c2016-11-09 15:00:58 -080037 Return<void> listByInterface(const hidl_string& fqInstanceName,
Steven Moreland76237812016-11-08 15:59:04 -080038 listByInterface_cb _hidl_cb) override;
39
Steven Moreland2173d3c2016-11-09 15:00:58 -080040 Return<bool> registerForNotifications(const hidl_string& fqName,
41 const hidl_string& name,
42 const sp<IServiceNotification>& callback) override;
Steven Morelandd83d1102016-10-25 15:01:47 -070043
Yifan Hong83c49f62017-01-25 14:16:34 -080044 Return<void> debugDump(debugDump_cb _cb) override;
Yifan Hongee531a82017-02-03 15:10:18 -080045 Return<void> registerPassthroughClient(const hidl_string &fqName,
46 const hidl_string &name, int32_t pid) override;
Yifan Hong83c49f62017-01-25 14:16:34 -080047
Martijn Coenen46847a62016-12-19 05:36:12 +010048 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
Steven Morelandd83d1102016-10-25 15:01:47 -070049private:
Martijn Coenen46847a62016-12-19 05:36:12 +010050 bool remove(const wp<IBase>& who);
Yifan Hong83c49f62017-01-25 14:16:34 -080051 size_t countExistingService() const;
52 void forEachExistingService(std::function<void(const HidlService *)> f) const;
Yifan Hongee531a82017-02-03 15:10:18 -080053 void forEachServiceEntry(std::function<void(const HidlService *)> f) const;
Steven Morelandd83d1102016-10-25 15:01:47 -070054
Steven Moreland6d7e5002017-01-20 15:21:48 -080055 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080056 std::string, // instance name e.x. "manager"
57 std::unique_ptr<HidlService>
58 >;
59
60 struct PackageInterfaceMap {
61 InstanceMap &getInstanceMap();
62 const InstanceMap &getInstanceMap() const;
63
64 /**
Steven Morelandd544cf62017-01-04 15:24:32 -080065 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -080066 * returns nullptr. HidlService::getService() might also be nullptr
67 * if there are registered IServiceNotification objects for it. Return
68 * value should be treated as a temporary reference.
69 */
Steven Morelandd544cf62017-01-04 15:24:32 -080070 HidlService *lookup(
71 const std::string &name);
72 const HidlService *lookup(
73 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080074
75 void insertService(std::unique_ptr<HidlService> &&service);
76
77 void addPackageListener(sp<IServiceNotification> listener);
78
Steven Moreland2173d3c2016-11-09 15:00:58 -080079 void sendPackageRegistrationNotification(
80 const hidl_string &fqName,
81 const hidl_string &instanceName) const;
82
Steven Moreland978473f2016-11-28 14:51:07 -080083 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080084 InstanceMap mInstanceMap{};
85
86 std::vector<sp<IServiceNotification>> mPackageListeners{};
87 };
88
Steven Moreland5fb3d652016-11-03 13:45:18 -070089 /**
90 * Access to this map doesn't need to be locked, since hwservicemanager
91 * is single-threaded.
92 *
93 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -080094 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -070095 * -> HidlService object
96 */
Steven Moreland6d7e5002017-01-20 15:21:48 -080097 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -080098 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -080099 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -0700100 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -0700101};
102
103} // namespace implementation
104} // namespace V1_0
105} // namespace manager
106} // namespace hidl
107} // namespace android
108
Steven Moreland5fb3d652016-11-03 13:45:18 -0700109#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H