Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 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_HIDL_INTERNAL_H |
| 18 | #define ANDROID_HIDL_INTERNAL_H |
| 19 | |
| 20 | #include <cstdint> |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <functional> |
| 23 | #include <string> |
| 24 | #include <vector> |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 25 | #include <utility> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace details { |
| 30 | |
Steven Moreland | 5f82d3e | 2017-07-27 13:57:56 -0700 | [diff] [blame^] | 31 | // tag for pure interfaces (e.x. IFoo) |
| 32 | struct i_tag {}; |
| 33 | |
| 34 | // tag for server interfaces (e.x. BnHwFoo) |
| 35 | struct bnhw_tag {}; |
| 36 | |
| 37 | // tag for proxy interfaces (e.x. BpHwFoo) |
| 38 | struct bphw_tag {}; |
| 39 | |
| 40 | // tag for server interfaces (e.x. BsFoo) |
| 41 | struct bs_tag {}; |
| 42 | |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 43 | //Templated classes can use the below method |
| 44 | //to avoid creating dependencies on liblog. |
| 45 | void logAlwaysFatal(const char *message); |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 46 | |
| 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. |
| 54 | template<typename T> |
| 55 | struct hidl_pointer { |
| 56 | hidl_pointer() |
Hridya Valsaraju | dda2562 | 2017-02-10 16:32:41 -0800 | [diff] [blame] | 57 | : _pad(0) { |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 58 | } |
| 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 Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 101 | |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 102 | private: |
| 103 | union { |
| 104 | T* mPointer; |
| 105 | uint64_t _pad; |
| 106 | }; |
| 107 | }; |
| 108 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 109 | #define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/" |
| 110 | #define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/" |
| 111 | #define HAL_LIBRARY_PATH_ODM_64BIT "/odm/lib64/hw/" |
| 112 | #define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/" |
| 113 | #define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/" |
| 114 | #define HAL_LIBRARY_PATH_ODM_32BIT "/odm/lib/hw/" |
| 115 | |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 116 | #if defined(__LP64__) |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 117 | #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT |
| 118 | #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT |
| 119 | #define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_64BIT |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 120 | #else |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 121 | #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT |
| 122 | #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT |
| 123 | #define HAL_LIBRARY_PATH_ODM HAL_LIBRARY_PATH_ODM_32BIT |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
| 126 | // ---------------------------------------------------------------------- |
| 127 | // Class that provides Hidl instrumentation utilities. |
| 128 | struct HidlInstrumentor { |
| 129 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 130 | // the server/client side, exit of an API call on the server/client side |
| 131 | // etc. |
| 132 | enum InstrumentationEvent { |
| 133 | SERVER_API_ENTRY = 0, |
| 134 | SERVER_API_EXIT, |
| 135 | CLIENT_API_ENTRY, |
| 136 | CLIENT_API_EXIT, |
| 137 | SYNC_CALLBACK_ENTRY, |
| 138 | SYNC_CALLBACK_EXIT, |
| 139 | ASYNC_CALLBACK_ENTRY, |
| 140 | ASYNC_CALLBACK_EXIT, |
| 141 | PASSTHROUGH_ENTRY, |
| 142 | PASSTHROUGH_EXIT, |
| 143 | }; |
| 144 | |
| 145 | // Signature of the instrumentation callback function. |
| 146 | using InstrumentationCallback = std::function<void( |
| 147 | const InstrumentationEvent event, |
| 148 | const char *package, |
| 149 | const char *version, |
| 150 | const char *interface, |
| 151 | const char *method, |
| 152 | std::vector<void *> *args)>; |
| 153 | |
| 154 | explicit HidlInstrumentor( |
| 155 | const std::string &package, |
| 156 | const std::string &insterface); |
| 157 | virtual ~HidlInstrumentor(); |
| 158 | |
| 159 | protected: |
| 160 | // Set mEnableInstrumentation based on system property |
| 161 | // hal.instrumentation.enable, register/de-register instrumentation |
| 162 | // callbacks if mEnableInstrumentation is true/false. |
| 163 | void configureInstrumentation(bool log=true); |
| 164 | // Function that lookup and dynamically loads the hidl instrumentation |
| 165 | // libraries and registers the instrumentation callback functions. |
| 166 | // |
| 167 | // The instrumentation libraries should be stored under any of the following |
| 168 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 169 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 170 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 171 | // |
| 172 | // Each instrumentation library is expected to implement the instrumentation |
| 173 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 174 | // |
| 175 | // A no-op for user build. |
| 176 | void registerInstrumentationCallbacks( |
| 177 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 178 | |
| 179 | // Utility function to determine whether a give file is a instrumentation |
| 180 | // library (i.e. the file name follow the expected pattern). |
| 181 | bool isInstrumentationLib(const dirent *file); |
| 182 | |
| 183 | // A list of registered instrumentation callbacks. |
| 184 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 185 | // Flag whether to enable instrumentation. |
| 186 | bool mEnableInstrumentation; |
| 187 | // Prefix to lookup the instrumentation libraries. |
| 188 | std::string mInstrumentationLibPackage; |
| 189 | // Used for dlsym to load the profiling method for given interface. |
| 190 | std::string mInterfaceName; |
| 191 | |
| 192 | }; |
| 193 | |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 194 | } // namespace details |
| 195 | } // namespace hardware |
| 196 | } // namespace android |
| 197 | |
| 198 | #endif // ANDROID_HIDL_INTERNAL_H |