HidlService.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_HARDWARE_MANAGER_HIDLSERVICE_H
  17. #define ANDROID_HARDWARE_MANAGER_HIDLSERVICE_H
  18. #include <set>
  19. #include <android/hidl/manager/1.2/IServiceManager.h>
  20. #include <hidl/Status.h>
  21. #include <hidl/MQDescriptor.h>
  22. namespace android {
  23. namespace hidl {
  24. namespace manager {
  25. namespace implementation {
  26. using ::android::hardware::hidl_vec;
  27. using ::android::hardware::hidl_string;
  28. using ::android::hardware::Return;
  29. using ::android::hardware::Void;
  30. using ::android::hidl::base::V1_0::IBase;
  31. using ::android::hidl::manager::V1_0::IServiceNotification;
  32. using ::android::hidl::manager::V1_1::IServiceManager;
  33. using ::android::hidl::manager::V1_2::IClientCallback;
  34. using ::android::sp;
  35. struct HidlService {
  36. HidlService(const std::string &interfaceName,
  37. const std::string &instanceName,
  38. const sp<IBase> &service,
  39. const pid_t pid);
  40. HidlService(const std::string &interfaceName,
  41. const std::string &instanceName)
  42. : HidlService(
  43. interfaceName,
  44. instanceName,
  45. nullptr,
  46. static_cast<pid_t>(IServiceManager::PidConstant::NO_PID))
  47. {}
  48. virtual ~HidlService() {}
  49. /**
  50. * Note, getService() can be nullptr. This is because you can have a HidlService
  51. * with registered IServiceNotification objects but no service registered yet.
  52. */
  53. sp<IBase> getService() const;
  54. void setService(sp<IBase> service, pid_t pid);
  55. pid_t getDebugPid() const;
  56. const std::string &getInterfaceName() const;
  57. const std::string &getInstanceName() const;
  58. void addListener(const sp<IServiceNotification> &listener);
  59. bool removeListener(const wp<IBase> &listener);
  60. void registerPassthroughClient(pid_t pid);
  61. // also sends onClients(true) if we have clients
  62. void addClientCallback(const sp<IClientCallback>& callback);
  63. bool removeClientCallback(const sp<IClientCallback>& callback);
  64. // return is number of clients (-1 means this is not implemented or we didn't check)
  65. // count includes one held by hwservicemanager
  66. ssize_t handleClientCallbacks(bool isCalledOnInterval);
  67. // Updates client callbacks (even if mClientCallbacks is emtpy)
  68. // see handleClientCallbacks
  69. ssize_t forceHandleClientCallbacks(bool isCalledOnInterval);
  70. // when giving out a handle to a client, but the kernel might not know this yet
  71. void guaranteeClient();
  72. std::string string() const; // e.x. "[email protected]::IServiceManager/manager"
  73. const std::set<pid_t> &getPassthroughClients() const;
  74. protected:
  75. // mockable number of clients including hwservicemanager. -1 if not implemented or unavailable.
  76. virtual ssize_t getNodeStrongRefCount();
  77. private:
  78. void sendRegistrationNotifications();
  79. // Also updates mHasClients (of what the last callback was)
  80. void sendClientCallbackNotifications(bool hasClients);
  81. // Only sends notification
  82. void sendClientCallbackNotification(const sp<IClientCallback>& callback, bool hasClients);
  83. const std::string mInterfaceName; // e.x. "[email protected]::IServiceManager"
  84. const std::string mInstanceName; // e.x. "manager"
  85. sp<IBase> mService;
  86. std::vector<sp<IServiceNotification>> mListeners{};
  87. std::set<pid_t> mPassthroughClients{};
  88. pid_t mPid = static_cast<pid_t>(IServiceManager::PidConstant::NO_PID);
  89. std::vector<sp<IClientCallback>> mClientCallbacks{};
  90. bool mHasClients = false; // notifications sent on true -> false.
  91. bool mGuaranteeClient = false; // whenever a client is handed out
  92. size_t mNoClientsCounter = 0;
  93. };
  94. } // namespace implementation
  95. } // namespace manager
  96. } // namespace hidl
  97. } // namespace android
  98. #endif // ANDROID_HARDWARE_MANAGER_HIDLSERVICE_H