blob: e9c628bb948523a7b177ae06e7984d4c74b175c5 [file] [log] [blame]
Steven Moreland337c3ae2016-11-22 13:37:32 -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
17#ifndef ANDROID_HIDL_INTERNAL_H
18#define ANDROID_HIDL_INTERNAL_H
19
20#include <cstdint>
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000021#include <dirent.h>
22#include <functional>
23#include <string>
24#include <vector>
Steven Moreland337c3ae2016-11-22 13:37:32 -080025#include <utility>
26
27namespace android {
28namespace hardware {
29namespace details {
30
Steven Moreland5f82d3e2017-07-27 13:57:56 -070031// tag for pure interfaces (e.x. IFoo)
32struct i_tag {};
33
34// tag for server interfaces (e.x. BnHwFoo)
35struct bnhw_tag {};
36
37// tag for proxy interfaces (e.x. BpHwFoo)
38struct bphw_tag {};
39
Steven Moreland396f1952017-07-31 18:44:51 -070040// tag for passthrough interfaces (e.x. BsFoo)
Steven Moreland5f82d3e2017-07-27 13:57:56 -070041struct bs_tag {};
42
Hridya Valsarajub7370302017-03-08 14:39:23 -080043//Templated classes can use the below method
44//to avoid creating dependencies on liblog.
45void logAlwaysFatal(const char *message);
Steven Moreland337c3ae2016-11-22 13:37:32 -080046
47// HIDL client/server code should *NOT* use this class.
48//
49// hidl_pointer wraps a pointer without taking ownership,
50// and stores it in a union with a uint64_t. This ensures
51// that we always have enough space to store a pointer,
52// regardless of whether we're running in a 32-bit or 64-bit
53// process.
54template<typename T>
55struct hidl_pointer {
56 hidl_pointer()
Hridya Valsarajudda25622017-02-10 16:32:41 -080057 : _pad(0) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080058 }
59 hidl_pointer(T* ptr)
60 : mPointer(ptr) {
61 }
62 hidl_pointer(const hidl_pointer<T>& other) {
63 mPointer = other.mPointer;
64 }
65 hidl_pointer(hidl_pointer<T>&& other) {
66 *this = std::move(other);
67 }
68
69 hidl_pointer &operator=(const hidl_pointer<T>& other) {
70 mPointer = other.mPointer;
71 return *this;
72 }
73 hidl_pointer &operator=(hidl_pointer<T>&& other) {
74 mPointer = other.mPointer;
75 other.mPointer = nullptr;
76 return *this;
77 }
78 hidl_pointer &operator=(T* ptr) {
79 mPointer = ptr;
80 return *this;
81 }
82
83 operator T*() const {
84 return mPointer;
85 }
86 explicit operator void*() const { // requires explicit cast to avoid ambiguity
87 return mPointer;
88 }
89 T& operator*() const {
90 return *mPointer;
91 }
92 T* operator->() const {
93 return mPointer;
94 }
95 T &operator[](size_t index) {
96 return mPointer[index];
97 }
98 const T &operator[](size_t index) const {
99 return mPointer[index];
100 }
Yifan Hongbe7a6882017-01-05 17:30:17 -0800101
Steven Moreland337c3ae2016-11-22 13:37:32 -0800102private:
103 union {
104 T* mPointer;
105 uint64_t _pad;
106 };
107};
108
Yifan Hong705e5da2017-03-02 16:59:39 -0800109#define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
Jiyong Park56eb1492017-08-04 16:16:13 +0900110#define HAL_LIBRARY_PATH_VNDK_SP_64BIT "/system/lib64/vndk-sp/hw/"
Yifan Hong705e5da2017-03-02 16:59:39 -0800111#define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
112#define HAL_LIBRARY_PATH_ODM_64BIT "/odm/lib64/hw/"
113#define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
Jiyong Park56eb1492017-08-04 16:16:13 +0900114#define HAL_LIBRARY_PATH_VNDK_SP_32BIT "/system/lib/vndk-sp/hw/"
Yifan Hong705e5da2017-03-02 16:59:39 -0800115#define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
116#define HAL_LIBRARY_PATH_ODM_32BIT "/odm/lib/hw/"
117
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000118#if defined(__LP64__)
Yifan Hong705e5da2017-03-02 16:59:39 -0800119#define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
Jiyong Park56eb1492017-08-04 16:16:13 +0900120#define HAL_LIBRARY_PATH_VNDK_SP HAL_LIBRARY_PATH_VNDK_SP_64BIT
Yifan Hong705e5da2017-03-02 16:59:39 -0800121#define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
122#define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_64BIT
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000123#else
Yifan Hong705e5da2017-03-02 16:59:39 -0800124#define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
Jiyong Park56eb1492017-08-04 16:16:13 +0900125#define HAL_LIBRARY_PATH_VNDK_SP HAL_LIBRARY_PATH_VNDK_SP_32BIT
Yifan Hong705e5da2017-03-02 16:59:39 -0800126#define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
127#define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_32BIT
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000128#endif
129
130// ----------------------------------------------------------------------
131// Class that provides Hidl instrumentation utilities.
132struct HidlInstrumentor {
133 // Event that triggers the instrumentation. e.g. enter of an API call on
134 // the server/client side, exit of an API call on the server/client side
135 // etc.
136 enum InstrumentationEvent {
137 SERVER_API_ENTRY = 0,
138 SERVER_API_EXIT,
139 CLIENT_API_ENTRY,
140 CLIENT_API_EXIT,
141 SYNC_CALLBACK_ENTRY,
142 SYNC_CALLBACK_EXIT,
143 ASYNC_CALLBACK_ENTRY,
144 ASYNC_CALLBACK_EXIT,
145 PASSTHROUGH_ENTRY,
146 PASSTHROUGH_EXIT,
147 };
148
149 // Signature of the instrumentation callback function.
150 using InstrumentationCallback = std::function<void(
151 const InstrumentationEvent event,
152 const char *package,
153 const char *version,
154 const char *interface,
155 const char *method,
156 std::vector<void *> *args)>;
157
158 explicit HidlInstrumentor(
159 const std::string &package,
160 const std::string &insterface);
161 virtual ~HidlInstrumentor();
162
Steven Morelanda29d03a2017-08-03 09:53:49 -0700163 public:
164 const std::vector<InstrumentationCallback>& getInstrumentationCallbacks() {
165 return mInstrumentationCallbacks;
166 }
167 bool isInstrumentationEnabled() { return mEnableInstrumentation; }
168
169 protected:
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000170 // Set mEnableInstrumentation based on system property
171 // hal.instrumentation.enable, register/de-register instrumentation
172 // callbacks if mEnableInstrumentation is true/false.
173 void configureInstrumentation(bool log=true);
174 // Function that lookup and dynamically loads the hidl instrumentation
175 // libraries and registers the instrumentation callback functions.
176 //
177 // The instrumentation libraries should be stored under any of the following
Jiyong Park56eb1492017-08-04 16:16:13 +0900178 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VNDK_SP,
179 // HAL_LIBRARY_PATH_VENDOR and HAL_LIBRARY_PATH_ODM.
180 // The name of instrumentation libraries should follow pattern:
181 // ^profilerPrefix(.*).profiler.so$
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000182 //
183 // Each instrumentation library is expected to implement the instrumentation
184 // function called HIDL_INSTRUMENTATION_FUNCTION.
185 //
186 // A no-op for user build.
187 void registerInstrumentationCallbacks(
188 std::vector<InstrumentationCallback> *instrumentationCallbacks);
189
190 // Utility function to determine whether a give file is a instrumentation
191 // library (i.e. the file name follow the expected pattern).
192 bool isInstrumentationLib(const dirent *file);
193
194 // A list of registered instrumentation callbacks.
195 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
196 // Flag whether to enable instrumentation.
197 bool mEnableInstrumentation;
198 // Prefix to lookup the instrumentation libraries.
199 std::string mInstrumentationLibPackage;
200 // Used for dlsym to load the profiling method for given interface.
201 std::string mInterfaceName;
202
203};
204
Steven Moreland337c3ae2016-11-22 13:37:32 -0800205} // namespace details
206} // namespace hardware
207} // namespace android
208
209#endif // ANDROID_HIDL_INTERNAL_H