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