blob: 7a8019d8e4c8a69c100d95e04a3c9d4565804aaa [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 }
Steven Moreland10618f32017-09-22 15:36:23 -070059 hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
60 hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
61 hidl_pointer(hidl_pointer<T>&& other) : hidl_pointer() { *this = std::move(other); }
Steven Moreland337c3ae2016-11-22 13:37:32 -080062
63 hidl_pointer &operator=(const hidl_pointer<T>& other) {
64 mPointer = other.mPointer;
65 return *this;
66 }
67 hidl_pointer &operator=(hidl_pointer<T>&& other) {
68 mPointer = other.mPointer;
69 other.mPointer = nullptr;
70 return *this;
71 }
72 hidl_pointer &operator=(T* ptr) {
73 mPointer = ptr;
74 return *this;
75 }
76
77 operator T*() const {
78 return mPointer;
79 }
80 explicit operator void*() const { // requires explicit cast to avoid ambiguity
81 return mPointer;
82 }
83 T& operator*() const {
84 return *mPointer;
85 }
86 T* operator->() const {
87 return mPointer;
88 }
89 T &operator[](size_t index) {
90 return mPointer[index];
91 }
92 const T &operator[](size_t index) const {
93 return mPointer[index];
94 }
Yifan Hongbe7a6882017-01-05 17:30:17 -080095
Steven Moreland337c3ae2016-11-22 13:37:32 -080096private:
97 union {
98 T* mPointer;
99 uint64_t _pad;
100 };
101};
102
Yifan Hong705e5da2017-03-02 16:59:39 -0800103#define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
Jiyong Park56eb1492017-08-04 16:16:13 +0900104#define HAL_LIBRARY_PATH_VNDK_SP_64BIT "/system/lib64/vndk-sp/hw/"
Yifan Hong705e5da2017-03-02 16:59:39 -0800105#define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
106#define HAL_LIBRARY_PATH_ODM_64BIT "/odm/lib64/hw/"
107#define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
Jiyong Park56eb1492017-08-04 16:16:13 +0900108#define HAL_LIBRARY_PATH_VNDK_SP_32BIT "/system/lib/vndk-sp/hw/"
Yifan Hong705e5da2017-03-02 16:59:39 -0800109#define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
110#define HAL_LIBRARY_PATH_ODM_32BIT "/odm/lib/hw/"
111
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000112#if defined(__LP64__)
Yifan Hong705e5da2017-03-02 16:59:39 -0800113#define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
Jiyong Park56eb1492017-08-04 16:16:13 +0900114#define HAL_LIBRARY_PATH_VNDK_SP HAL_LIBRARY_PATH_VNDK_SP_64BIT
Yifan Hong705e5da2017-03-02 16:59:39 -0800115#define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
116#define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_64BIT
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000117#else
Yifan Hong705e5da2017-03-02 16:59:39 -0800118#define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
Jiyong Park56eb1492017-08-04 16:16:13 +0900119#define HAL_LIBRARY_PATH_VNDK_SP HAL_LIBRARY_PATH_VNDK_SP_32BIT
Yifan Hong705e5da2017-03-02 16:59:39 -0800120#define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
121#define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_32BIT
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000122#endif
123
124// ----------------------------------------------------------------------
125// Class that provides Hidl instrumentation utilities.
126struct HidlInstrumentor {
127 // Event that triggers the instrumentation. e.g. enter of an API call on
128 // the server/client side, exit of an API call on the server/client side
129 // etc.
130 enum InstrumentationEvent {
131 SERVER_API_ENTRY = 0,
132 SERVER_API_EXIT,
133 CLIENT_API_ENTRY,
134 CLIENT_API_EXIT,
135 SYNC_CALLBACK_ENTRY,
136 SYNC_CALLBACK_EXIT,
137 ASYNC_CALLBACK_ENTRY,
138 ASYNC_CALLBACK_EXIT,
139 PASSTHROUGH_ENTRY,
140 PASSTHROUGH_EXIT,
141 };
142
143 // Signature of the instrumentation callback function.
144 using InstrumentationCallback = std::function<void(
145 const InstrumentationEvent event,
146 const char *package,
147 const char *version,
148 const char *interface,
149 const char *method,
150 std::vector<void *> *args)>;
151
152 explicit HidlInstrumentor(
153 const std::string &package,
154 const std::string &insterface);
155 virtual ~HidlInstrumentor();
156
Steven Morelanda29d03a2017-08-03 09:53:49 -0700157 public:
158 const std::vector<InstrumentationCallback>& getInstrumentationCallbacks() {
159 return mInstrumentationCallbacks;
160 }
161 bool isInstrumentationEnabled() { return mEnableInstrumentation; }
162
163 protected:
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000164 // Set mEnableInstrumentation based on system property
165 // hal.instrumentation.enable, register/de-register instrumentation
166 // callbacks if mEnableInstrumentation is true/false.
167 void configureInstrumentation(bool log=true);
168 // Function that lookup and dynamically loads the hidl instrumentation
169 // libraries and registers the instrumentation callback functions.
170 //
171 // The instrumentation libraries should be stored under any of the following
Jiyong Park56eb1492017-08-04 16:16:13 +0900172 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VNDK_SP,
173 // HAL_LIBRARY_PATH_VENDOR and HAL_LIBRARY_PATH_ODM.
174 // The name of instrumentation libraries should follow pattern:
175 // ^profilerPrefix(.*).profiler.so$
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000176 //
177 // Each instrumentation library is expected to implement the instrumentation
178 // function called HIDL_INSTRUMENTATION_FUNCTION.
179 //
180 // A no-op for user build.
181 void registerInstrumentationCallbacks(
182 std::vector<InstrumentationCallback> *instrumentationCallbacks);
183
184 // Utility function to determine whether a give file is a instrumentation
185 // library (i.e. the file name follow the expected pattern).
186 bool isInstrumentationLib(const dirent *file);
187
188 // A list of registered instrumentation callbacks.
189 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
190 // Flag whether to enable instrumentation.
191 bool mEnableInstrumentation;
192 // Prefix to lookup the instrumentation libraries.
193 std::string mInstrumentationLibPackage;
194 // Used for dlsym to load the profiling method for given interface.
195 std::string mInterfaceName;
196
197};
198
Steven Moreland337c3ae2016-11-22 13:37:32 -0800199} // namespace details
200} // namespace hardware
201} // namespace android
202
203#endif // ANDROID_HIDL_INTERNAL_H