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 17 #pragma once 18 19 #include <set> 20 21 #include <android/hidl/manager/1.2/IServiceManager.h> 22 #include <hidl/Status.h> 23 #include <hidl/MQDescriptor.h> 24 25 namespace android { 26 namespace hidl { 27 namespace manager { 28 namespace implementation { 29 30 using ::android::hardware::hidl_vec; 31 using ::android::hardware::hidl_string; 32 using ::android::hardware::Return; 33 using ::android::hardware::Void; 34 using ::android::hidl::base::V1_0::IBase; 35 using ::android::hidl::manager::V1_0::IServiceNotification; 36 using ::android::hidl::manager::V1_1::IServiceManager; 37 using ::android::hidl::manager::V1_2::IClientCallback; 38 using ::android::sp; 39 40 struct HidlService { 41 HidlService(const std::string &interfaceName, 42 const std::string &instanceName, 43 const sp<IBase> &service, 44 const pid_t pid); HidlServiceHidlService45 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 {} ~HidlServiceHidlService53 virtual ~HidlService() {} 54 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 */ 59 sp<IBase> getService() const; 60 void setService(sp<IBase> service, pid_t pid); 61 pid_t getDebugPid() const; 62 const std::string &getInterfaceName() const; 63 const std::string &getInstanceName() const; 64 65 void addListener(const sp<IServiceNotification> &listener); 66 bool removeListener(const wp<IBase> &listener); 67 void registerPassthroughClient(pid_t pid); 68 69 // also sends onClients(true) if we have clients 70 // knownClientCount, see forceHandleClientCallbacks 71 void addClientCallback(const sp<IClientCallback>& callback, size_t knownClientCount); 72 bool removeClientCallback(const sp<IClientCallback>& callback); 73 74 // return is if we are guaranteed to have a client 75 // knownClientCount, see forceHandleClientCallbacks 76 bool handleClientCallbacks(bool isCalledOnInterval, size_t knownClientCount); 77 78 // Updates client callbacks (even if mClientCallbacks is emtpy) 79 // see handleClientCallbacks 80 // 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); 87 88 // when giving out a handle to a client, but the kernel might not know this yet 89 void guaranteeClient(); 90 91 std::string string() const; // e.x. "android.hidl.manager@1.0::IServiceManager/manager" 92 const std::set<pid_t> &getPassthroughClients() const; 93 94 protected: 95 // mockable number of clients including hwservicemanager. -1 if not implemented or unavailable. 96 virtual ssize_t getNodeStrongRefCount(); 97 98 private: 99 void sendRegistrationNotifications(); 100 101 // Also updates mHasClients (of what the last callback was) 102 void sendClientCallbackNotifications(bool hasClients); 103 104 // Only sends notification 105 void sendClientCallbackNotification(const sp<IClientCallback>& callback, bool hasClients); 106 107 const std::string mInterfaceName; // e.x. "android.hidl.manager@1.0::IServiceManager" 108 const std::string mInstanceName; // e.x. "manager" 109 sp<IBase> mService; 110 111 std::vector<sp<IServiceNotification>> mListeners{}; 112 std::set<pid_t> mPassthroughClients{}; 113 pid_t mPid = static_cast<pid_t>(IServiceManager::PidConstant::NO_PID); 114 115 std::vector<sp<IClientCallback>> mClientCallbacks{}; 116 bool mHasClients = false; // notifications sent on true -> false. 117 bool mGuaranteeClient = false; // whenever a client is handed out 118 size_t mNoClientsCounter = 0; 119 }; 120 121 } // namespace implementation 122 } // namespace manager 123 } // namespace hidl 124 } // namespace android 125