blob: 73dace45583875228d160bdfb69e0764bcf8cf54 [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;
45
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);
Yifan Hong83c49f62017-01-25 14:16:34 -080049 size_t countExistingService() const;
50 void forEachExistingService(std::function<void(const HidlService *)> f) const;
Steven Morelandd83d1102016-10-25 15:01:47 -070051
Steven Moreland6d7e5002017-01-20 15:21:48 -080052 using InstanceMap = std::map<
Steven Moreland2173d3c2016-11-09 15:00:58 -080053 std::string, // instance name e.x. "manager"
54 std::unique_ptr<HidlService>
55 >;
56
57 struct PackageInterfaceMap {
58 InstanceMap &getInstanceMap();
59 const InstanceMap &getInstanceMap() const;
60
61 /**
Steven Morelandd544cf62017-01-04 15:24:32 -080062 * Finds a HidlService with the desired name. If none,
Steven Moreland2173d3c2016-11-09 15:00:58 -080063 * returns nullptr. HidlService::getService() might also be nullptr
64 * if there are registered IServiceNotification objects for it. Return
65 * value should be treated as a temporary reference.
66 */
Steven Morelandd544cf62017-01-04 15:24:32 -080067 HidlService *lookup(
68 const std::string &name);
69 const HidlService *lookup(
70 const std::string &name) const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080071
72 void insertService(std::unique_ptr<HidlService> &&service);
73
74 void addPackageListener(sp<IServiceNotification> listener);
75
Steven Moreland2173d3c2016-11-09 15:00:58 -080076 void sendPackageRegistrationNotification(
77 const hidl_string &fqName,
78 const hidl_string &instanceName) const;
79
Steven Moreland978473f2016-11-28 14:51:07 -080080 private:
Steven Moreland2173d3c2016-11-09 15:00:58 -080081 InstanceMap mInstanceMap{};
82
83 std::vector<sp<IServiceNotification>> mPackageListeners{};
84 };
85
Steven Moreland5fb3d652016-11-03 13:45:18 -070086 /**
87 * Access to this map doesn't need to be locked, since hwservicemanager
88 * is single-threaded.
89 *
90 * e.x.
Steven Morelandd544cf62017-01-04 15:24:32 -080091 * mServiceMap["android.hidl.manager@1.0::IServiceManager"]["manager"]
Steven Moreland5fb3d652016-11-03 13:45:18 -070092 * -> HidlService object
93 */
Steven Moreland6d7e5002017-01-20 15:21:48 -080094 std::map<
Steven Morelandd544cf62017-01-04 15:24:32 -080095 std::string, // package::interface e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Moreland2173d3c2016-11-09 15:00:58 -080096 PackageInterfaceMap
Steven Moreland5fb3d652016-11-03 13:45:18 -070097 > mServiceMap;
Steven Morelandd83d1102016-10-25 15:01:47 -070098};
99
100} // namespace implementation
101} // namespace V1_0
102} // namespace manager
103} // namespace hidl
104} // namespace android
105
Steven Moreland5fb3d652016-11-03 13:45:18 -0700106#endif // ANDROID_HARDWARE_MANAGER_V1_0_SERVICEMANAGER_H