blob: 76d1c381462e86f24856ecfa30a6db67c0a96dd8 [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 Morelandd5f8f082019-06-06 12:59:20 -070017#pragma once
Steven Moreland2173d3c2016-11-09 15:00:58 -080018
Yifan Hongee531a82017-02-03 15:10:18 -080019#include <set>
20
Steven Morelandd8536202018-09-26 10:56:19 -070021#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland2173d3c2016-11-09 15:00:58 -080022#include <hidl/Status.h>
23#include <hidl/MQDescriptor.h>
Steven Moreland2173d3c2016-11-09 15:00:58 -080024
25namespace android {
26namespace hidl {
27namespace manager {
Steven Moreland2173d3c2016-11-09 15:00:58 -080028namespace implementation {
29
30using ::android::hardware::hidl_vec;
31using ::android::hardware::hidl_string;
Steven Moreland2173d3c2016-11-09 15:00:58 -080032using ::android::hardware::Return;
33using ::android::hardware::Void;
Yifan Hongb3a90f02016-11-23 12:58:04 -080034using ::android::hidl::base::V1_0::IBase;
Steven Moreland6f4fbe12017-07-21 18:07:42 -070035using ::android::hidl::manager::V1_0::IServiceNotification;
36using ::android::hidl::manager::V1_1::IServiceManager;
Steven Morelandd8536202018-09-26 10:56:19 -070037using ::android::hidl::manager::V1_2::IClientCallback;
Steven Moreland2173d3c2016-11-09 15:00:58 -080038using ::android::sp;
39
40struct HidlService {
Steven Morelandd544cf62017-01-04 15:24:32 -080041 HidlService(const std::string &interfaceName,
42 const std::string &instanceName,
Steven Morelandcdf94722017-03-21 12:16:31 -070043 const sp<IBase> &service,
44 const pid_t pid);
45 HidlService(const std::string &interfaceName,
46 const std::string &instanceName)
47 : HidlService(
48 interfaceName,
49 instanceName,
50 nullptr,
51 static_cast<pid_t>(IServiceManager::PidConstant::NO_PID))
52 {}
Steven Moreland501bc8e2019-02-01 16:21:46 -080053 virtual ~HidlService() {}
Steven Moreland2173d3c2016-11-09 15:00:58 -080054
55 /**
56 * Note, getService() can be nullptr. This is because you can have a HidlService
57 * with registered IServiceNotification objects but no service registered yet.
58 */
Yifan Hongb3a90f02016-11-23 12:58:04 -080059 sp<IBase> getService() const;
Steven Morelandcdf94722017-03-21 12:16:31 -070060 void setService(sp<IBase> service, pid_t pid);
Steven Moreland7185a892019-01-09 18:00:05 -080061 pid_t getDebugPid() const;
Steven Morelandd544cf62017-01-04 15:24:32 -080062 const std::string &getInterfaceName() const;
63 const std::string &getInstanceName() const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080064
65 void addListener(const sp<IServiceNotification> &listener);
Martijn Coenen7fafc142017-03-06 16:17:51 +010066 bool removeListener(const wp<IBase> &listener);
Yifan Hongee531a82017-02-03 15:10:18 -080067 void registerPassthroughClient(pid_t pid);
Steven Moreland2173d3c2016-11-09 15:00:58 -080068
Steven Moreland2fa0f552019-02-01 19:27:33 -080069 // also sends onClients(true) if we have clients
Steven Morelandf083ee32020-04-06 16:18:50 -070070 // knownClientCount, see forceHandleClientCallbacks
71 void addClientCallback(const sp<IClientCallback>& callback, size_t knownClientCount);
Steven Morelandd8536202018-09-26 10:56:19 -070072 bool removeClientCallback(const sp<IClientCallback>& callback);
Steven Morelanddbaab552019-01-24 19:00:04 -080073
Steven Morelandf083ee32020-04-06 16:18:50 -070074 // return is if we are guaranteed to have a client
75 // knownClientCount, see forceHandleClientCallbacks
76 bool handleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount);
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
Steven Morelandf083ee32020-04-06 16:18:50 -070080 //
81 // knownClientCount - this is the number of clients that is currently
82 // expected to be in use by known actors. This number of clients must be
83 // exceeded in order to consider the service to have clients.
84 //
85 // returns whether or not this service has clients
86 bool forceHandleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount);
Steven Morelande7cf1422019-02-01 20:17:25 -080087
Steven Moreland98db8292018-12-07 13:08:25 -080088 // when giving out a handle to a client, but the kernel might not know this yet
89 void guaranteeClient();
90
Steven Moreland2173d3c2016-11-09 15:00:58 -080091 std::string string() const; // e.x. "android.hidl.manager@1.0::IServiceManager/manager"
Yifan Hongee531a82017-02-03 15:10:18 -080092 const std::set<pid_t> &getPassthroughClients() const;
Steven Moreland2173d3c2016-11-09 15:00:58 -080093
Steven Moreland501bc8e2019-02-01 16:21:46 -080094protected:
95 // mockable number of clients including hwservicemanager. -1 if not implemented or unavailable.
96 virtual ssize_t getNodeStrongRefCount();
97
Steven Moreland2173d3c2016-11-09 15:00:58 -080098private:
Martijn Coenen72103a02017-01-18 16:06:34 +010099 void sendRegistrationNotifications();
Steven Moreland2fa0f552019-02-01 19:27:33 -0800100
101 // Also updates mHasClients (of what the last callback was)
Steven Moreland98db8292018-12-07 13:08:25 -0800102 void sendClientCallbackNotifications(bool hasClients);
Steven Moreland2173d3c2016-11-09 15:00:58 -0800103
Steven Moreland2fa0f552019-02-01 19:27:33 -0800104 // Only sends notification
105 void sendClientCallbackNotification(const sp<IClientCallback>& callback, bool hasClients);
106
Steven Moreland6f4fbe12017-07-21 18:07:42 -0700107 const std::string mInterfaceName; // e.x. "android.hidl.manager@1.0::IServiceManager"
Steven Morelandd544cf62017-01-04 15:24:32 -0800108 const std::string mInstanceName; // e.x. "manager"
Yifan Hongb3a90f02016-11-23 12:58:04 -0800109 sp<IBase> mService;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800110
111 std::vector<sp<IServiceNotification>> mListeners{};
Yifan Hongee531a82017-02-03 15:10:18 -0800112 std::set<pid_t> mPassthroughClients{};
Steven Morelandcdf94722017-03-21 12:16:31 -0700113 pid_t mPid = static_cast<pid_t>(IServiceManager::PidConstant::NO_PID);
Steven Morelandd8536202018-09-26 10:56:19 -0700114
115 std::vector<sp<IClientCallback>> mClientCallbacks{};
116 bool mHasClients = false; // notifications sent on true -> false.
Steven Moreland98db8292018-12-07 13:08:25 -0800117 bool mGuaranteeClient = false; // whenever a client is handed out
Peter Kalauskas3b27ec52019-01-17 14:47:24 -0800118 size_t mNoClientsCounter = 0;
Steven Moreland2173d3c2016-11-09 15:00:58 -0800119};
120
121} // namespace implementation
Steven Moreland2173d3c2016-11-09 15:00:58 -0800122} // namespace manager
123} // namespace hidl
124} // namespace android