blob: e2a6232a791a9d7968546c3353a5f3bb9531d7ba [file] [log] [blame]
Steven Morelandfe66b732019-02-01 14:29:45 -08001/*
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
Steven Moreland6f4fbe12017-07-21 18:07:42 -070017#ifndef ANDROID_HARDWARE_MANAGER_HIDLSERVICE_H
18#define ANDROID_HARDWARE_MANAGER_HIDLSERVICE_H
Steven Moreland2173d3c2016-11-09 15:00:58 -080019
Yifan Hongee531a82017-02-03 15:10:18 -080020#include <set>
21
Steven Morelandd8536202018-09-26 10:56:19 -070022#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland2173d3c2016-11-09 15:00:58 -080023#include <hidl/Status.h>
24#include <hidl/MQDescriptor.h>
Steven Moreland2173d3c2016-11-09 15:00:58 -080025
26namespace android {
27namespace hidl {
28namespace manager {
Steven Moreland2173d3c2016-11-09 15:00:58 -080029namespace implementation {
30
31using ::android::hardware::hidl_vec;
32using ::android::hardware::hidl_string;
Steven Moreland2173d3c2016-11-09 15:00:58 -080033using ::android::hardware::Return;
34using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080035using ::android::hidl::base::V1_0::IBase;
Steven Moreland6f4fbe12017-07-21 18:07:42 -070036using ::android::hidl::manager::V1_0::IServiceNotification;
37using ::android::hidl::manager::V1_1::IServiceManager;
Steven Morelandd8536202018-09-26 10:56:19 -070038using ::android::hidl::manager::V1_2::IClientCallback;
Steven Moreland2173d3c2016-11-09 15:00:58 -080039using ::android::sp;
40
41struct HidlService {
Steven Morelandd544cf62017-01-04 15:24:32 -080042 HidlService(const std::string &interfaceName,
43 const std::string &instanceName,
Steven Morelandcdf94722017-03-21 12:16:31 -070044 const sp<IBase> &service,
45 const pid_t pid);
46 HidlService(const std::string &interfaceName,
47 const std::string &instanceName)
48 : HidlService(
49 interfaceName,
50 instanceName,
51 nullptr,
52 static_cast<pid_t>(IServiceManager::PidConstant::NO_PID))
53 {}
Steven Moreland501bc8e2019-02-01 16:21:46 -080054 virtual ~HidlService() {}
Steven Moreland2173d3c2016-11-09 15:00:58 -080055
56 /**
57 * Note, getService() can be nullptr. This is because you can have a HidlService
58 * with registered IServiceNotification objects but no service registered yet.
59 */
Yifan Hongb3a90f02016-11-23 12:58:04 -080060 sp<IBase> getService() const;
Steven Morelandcdf94722017-03-21 12:16:31 -070061 void setService(sp<IBase> service, pid_t pid);
Steven Moreland7185a892019-01-09 18:00:05 -080062 pid_t getDebugPid() const;
Steven Morelandd544cf62017-01-04 15:24:32 -080063 const std::string &getInterfaceName() const;
64 const std::string &getInstanceName() const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080065
66 void addListener(const sp<IServiceNotification> &listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +010067 bool removeListener(const wp<IBase> &listener);
Yifan Hongee531a82017-02-03 15:10:18 -080068 void registerPassthroughClient(pid_t pid);
Steven Moreland2173d3c2016-11-09 15:00:58 -080069
Steven Moreland2fa0f552019-02-01 19:27:33 -080070 // also sends onClients(true) if we have clients
Steven Morelandd8536202018-09-26 10:56:19 -070071 void addClientCallback(const sp<IClientCallback>& callback);
72 bool removeClientCallback(const sp<IClientCallback>& callback);
Steven Moreland573e5322019-01-24 19:00:04 -080073
Steven Morelande7cf1422019-02-01 20:17:25 -080074 // return is number of clients (-1 means this is not implemented or we didn't check)
Steven Moreland573e5322019-01-24 19:00:04 -080075 // count includes one held by hwservicemanager
76 ssize_t handleClientCallbacks(bool isCalledOnInterval);
Steven Morelandd8536202018-09-26 10:56:19 -070077
Steven Morelande7cf1422019-02-01 20:17:25 -080078 // Updates client callbacks (even if mClientCallbacks is emtpy)
79 // see handleClientCallbacks
80 ssize_t forceHandleClientCallbacks(bool isCalledOnInterval);
81
Steven Moreland98db8292018-12-07 13:08:25 -080082 // when giving out a handle to a client, but the kernel might not know this yet
83 void guaranteeClient();
84
Steven Moreland2173d3c2016-11-09 15:00:58 -080085 std::string string() const; // e.x. "android.hidl.manager@1.0::IServiceManager/manager"
Yifan Hongee531a82017-02-03 15:10:18 -080086 const std::set<pid_t> &getPassthroughClients() const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080087
Steven Moreland501bc8e2019-02-01 16:21:46 -080088protected:
89 // mockable number of clients including hwservicemanager. -1 if not implemented or unavailable.
90 virtual ssize_t getNodeStrongRefCount();
91
Steven Moreland2173d3c2016-11-09 15:00:58 -080092private:
Martijn Coenen72103a02017-01-18 16:06:34 +010093 void sendRegistrationNotifications();
Steven Moreland2fa0f552019-02-01 19:27:33 -080094
95 // Also updates mHasClients (of what the last callback was)
Steven Moreland98db8292018-12-07 13:08:25 -080096 void sendClientCallbackNotifications(bool hasClients);
Steven Moreland2173d3c2016-11-09 15:00:58 -080097
Steven Moreland2fa0f552019-02-01 19:27:33 -080098 // Only sends notification
99 void sendClientCallbackNotification(const sp<IClientCallback>& callback, bool hasClients);
100
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700101 const std::string mInterfaceName; // e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Morelandd544cf62017-01-04 15:24:32 -0800102 const std::string mInstanceName; // e.x. "manager"
Yifan Hongb3a90f02016-11-23 12:58:04 -0800103 sp<IBase> mService;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800104
105 std::vector<sp<IServiceNotification>> mListeners{};
Yifan Hongee531a82017-02-03 15:10:18 -0800106 std::set<pid_t> mPassthroughClients{};
Steven Morelandcdf94722017-03-21 12:16:31 -0700107 pid_t mPid = static_cast<pid_t>(IServiceManager::PidConstant::NO_PID);
Steven Morelandd8536202018-09-26 10:56:19 -0700108
109 std::vector<sp<IClientCallback>> mClientCallbacks{};
110 bool mHasClients = false; // notifications sent on true -> false.
Steven Moreland98db8292018-12-07 13:08:25 -0800111 bool mGuaranteeClient = false; // whenever a client is handed out
Peter Kalauskas3b27ec52019-01-17 14:47:24 -0800112 size_t mNoClientsCounter = 0;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800113};
114
115} // namespace implementation
Steven Moreland2173d3c2016-11-09 15:00:58 -0800116} // namespace manager
117} // namespace hidl
118} // namespace android
119
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700120#endif // ANDROID_HARDWARE_MANAGER_HIDLSERVICE_H