Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [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_SUPPORT_H |
| 18 | #define ANDROID_HIDL_SUPPORT_H |
| 19 | |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 21 | #include <dirent.h> |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 22 | #include <dlfcn.h> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame^] | 23 | #include <iterator> |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 25 | #include <functional> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 26 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 27 | #include <map> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 28 | #include <tuple> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 29 | #include <utils/Errors.h> |
| 30 | #include <utils/RefBase.h> |
| 31 | #include <utils/StrongPointer.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 32 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace hardware { |
| 36 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 37 | namespace details { |
| 38 | |
| 39 | // hidl_log_base is a base class that templatized |
| 40 | // classes implemented in a header can inherit from, |
| 41 | // to avoid creating dependencies on liblog. |
| 42 | struct hidl_log_base { |
| 43 | void logAlwaysFatal(const char *message); |
| 44 | }; |
| 45 | |
| 46 | // HIDL client/server code should *NOT* use this class. |
| 47 | // |
| 48 | // hidl_pointer wraps a pointer without taking ownership, |
| 49 | // and stores it in a union with a uint64_t. This ensures |
| 50 | // that we always have enough space to store a pointer, |
| 51 | // regardless of whether we're running in a 32-bit or 64-bit |
| 52 | // process. |
| 53 | template<typename T> |
| 54 | struct hidl_pointer { |
| 55 | hidl_pointer() |
| 56 | : mPointer(nullptr) { |
| 57 | } |
| 58 | hidl_pointer(T* ptr) |
| 59 | : mPointer(ptr) { |
| 60 | } |
| 61 | hidl_pointer(const hidl_pointer<T>& other) { |
| 62 | mPointer = other.mPointer; |
| 63 | } |
| 64 | hidl_pointer(hidl_pointer<T>&& other) { |
| 65 | *this = std::move(other); |
| 66 | } |
| 67 | |
| 68 | hidl_pointer &operator=(const hidl_pointer<T>& other) { |
| 69 | mPointer = other.mPointer; |
| 70 | return *this; |
| 71 | } |
| 72 | hidl_pointer &operator=(hidl_pointer<T>&& other) { |
| 73 | mPointer = other.mPointer; |
| 74 | other.mPointer = nullptr; |
| 75 | return *this; |
| 76 | } |
| 77 | hidl_pointer &operator=(T* ptr) { |
| 78 | mPointer = ptr; |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | operator T*() const { |
| 83 | return mPointer; |
| 84 | } |
| 85 | explicit operator void*() const { // requires explicit cast to avoid ambiguity |
| 86 | return mPointer; |
| 87 | } |
| 88 | T& operator*() const { |
| 89 | return *mPointer; |
| 90 | } |
| 91 | T* operator->() const { |
| 92 | return mPointer; |
| 93 | } |
| 94 | T &operator[](size_t index) { |
| 95 | return mPointer[index]; |
| 96 | } |
| 97 | const T &operator[](size_t index) const { |
| 98 | return mPointer[index]; |
| 99 | } |
| 100 | private: |
| 101 | union { |
| 102 | T* mPointer; |
| 103 | uint64_t _pad; |
| 104 | }; |
| 105 | }; |
| 106 | } // namespace details |
| 107 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 108 | struct hidl_string { |
| 109 | hidl_string(); |
| 110 | ~hidl_string(); |
| 111 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 112 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 113 | hidl_string(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 114 | // copy from a C-style string. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 115 | hidl_string(const char *); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 116 | // copy from an std::string. |
| 117 | hidl_string(const std::string &); |
| 118 | |
| 119 | // move constructor. |
| 120 | hidl_string(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 121 | |
| 122 | const char *c_str() const; |
| 123 | size_t size() const; |
| 124 | bool empty() const; |
| 125 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 126 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 127 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 128 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 129 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 130 | // copy from an std::string. |
| 131 | hidl_string &operator=(const std::string &); |
| 132 | // move assignment operator. |
| 133 | hidl_string &operator=(hidl_string &&other); |
| 134 | // cast to std::string. |
| 135 | operator std::string() const; |
| 136 | // cast to C-style string. Caller is responsible |
| 137 | // to maintain this hidl_string alive. |
| 138 | operator const char *() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 139 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 140 | void clear(); |
| 141 | |
| 142 | // Reference an external char array. Ownership is _not_ transferred. |
| 143 | // Caller is responsible for ensuring that underlying memory is valid |
| 144 | // for the lifetime of this hidl_string. |
| 145 | void setToExternal(const char *data, size_t size); |
| 146 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 147 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 148 | static const size_t kOffsetOfBuffer; |
| 149 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 150 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 151 | details::hidl_pointer<const char> mBuffer; |
| 152 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 153 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 154 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 155 | // copy from data with size. Assume that my memory is freed |
| 156 | // (through clear(), for example) |
| 157 | void copyFrom(const char *data, size_t size); |
| 158 | // move from another hidl_string |
| 159 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame^] | 162 | inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) { |
| 163 | return strcmp(hs1.c_str(), hs2.c_str()) == 0; |
| 164 | } |
| 165 | |
| 166 | inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) { |
| 167 | return !(hs1 == hs2); |
| 168 | } |
| 169 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 170 | inline bool operator==(const hidl_string &hs, const char *s) { |
| 171 | return strcmp(hs.c_str(), s) == 0; |
| 172 | } |
| 173 | |
| 174 | inline bool operator!=(const hidl_string &hs, const char *s) { |
| 175 | return !(hs == s); |
| 176 | } |
| 177 | |
| 178 | inline bool operator==(const char *s, const hidl_string &hs) { |
| 179 | return strcmp(hs.c_str(), s) == 0; |
| 180 | } |
| 181 | |
| 182 | inline bool operator!=(const char *s, const hidl_string &hs) { |
| 183 | return !(s == hs); |
| 184 | } |
| 185 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 186 | //////////////////////////////////////////////////////////////////////////////// |
| 187 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 188 | template<typename T> |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 189 | struct hidl_vec : private details::hidl_log_base { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 190 | hidl_vec() |
| 191 | : mBuffer(NULL), |
| 192 | mSize(0), |
| 193 | mOwnsBuffer(true) { |
| 194 | } |
| 195 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 196 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 197 | *this = other; |
| 198 | } |
| 199 | |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 200 | hidl_vec(hidl_vec<T> &&other) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 201 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 202 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 205 | hidl_vec(const std::initializer_list<T> list) |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 206 | : mSize(list.size()), |
| 207 | mOwnsBuffer(true) { |
| 208 | mBuffer = new T[mSize]; |
| 209 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 210 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 211 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 212 | mBuffer[idx++] = *it; |
| 213 | } |
| 214 | } |
| 215 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 216 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 217 | *this = other; |
| 218 | } |
| 219 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 220 | ~hidl_vec() { |
| 221 | if (mOwnsBuffer) { |
| 222 | delete[] mBuffer; |
| 223 | } |
| 224 | mBuffer = NULL; |
| 225 | } |
| 226 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 227 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 228 | // caller's responsibility to ensure that the underlying memory stays |
| 229 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 230 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 231 | if (mOwnsBuffer) { |
| 232 | delete [] mBuffer; |
| 233 | } |
| 234 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 235 | if (size > UINT32_MAX) { |
| 236 | logAlwaysFatal("external vector size exceeds 2^32 elements."); |
| 237 | } |
| 238 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 239 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 242 | T *data() { |
| 243 | return mBuffer; |
| 244 | } |
| 245 | |
| 246 | const T *data() const { |
| 247 | return mBuffer; |
| 248 | } |
| 249 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 250 | T *releaseData() { |
| 251 | if (!mOwnsBuffer && mSize > 0) { |
| 252 | resize(mSize); |
| 253 | } |
| 254 | mOwnsBuffer = false; |
| 255 | return mBuffer; |
| 256 | } |
| 257 | |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 258 | hidl_vec &operator=(hidl_vec &&other) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 259 | if (mOwnsBuffer) { |
| 260 | delete[] mBuffer; |
| 261 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 262 | mBuffer = other.mBuffer; |
| 263 | mSize = other.mSize; |
| 264 | mOwnsBuffer = other.mOwnsBuffer; |
| 265 | other.mOwnsBuffer = false; |
| 266 | return *this; |
| 267 | } |
| 268 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 269 | hidl_vec &operator=(const hidl_vec &other) { |
| 270 | if (this != &other) { |
| 271 | if (mOwnsBuffer) { |
| 272 | delete[] mBuffer; |
| 273 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 274 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | return *this; |
| 278 | } |
| 279 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 280 | // copy from an std::vector. |
| 281 | hidl_vec &operator=(const std::vector<T> &other) { |
| 282 | if (mOwnsBuffer) { |
| 283 | delete[] mBuffer; |
| 284 | } |
| 285 | copyFrom(other, other.size()); |
| 286 | return *this; |
| 287 | } |
| 288 | |
| 289 | // cast to an std::vector. |
| 290 | operator std::vector<T>() const { |
| 291 | std::vector<T> v(mSize); |
| 292 | for (size_t i = 0; i < mSize; ++i) { |
| 293 | v[i] = mBuffer[i]; |
| 294 | } |
| 295 | return v; |
| 296 | } |
| 297 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 298 | size_t size() const { |
| 299 | return mSize; |
| 300 | } |
| 301 | |
| 302 | T &operator[](size_t index) { |
| 303 | return mBuffer[index]; |
| 304 | } |
| 305 | |
| 306 | const T &operator[](size_t index) const { |
| 307 | return mBuffer[index]; |
| 308 | } |
| 309 | |
| 310 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 311 | if (size > UINT32_MAX) { |
| 312 | logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 313 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 314 | T *newBuffer = new T[size]; |
| 315 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 316 | for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 317 | newBuffer[i] = mBuffer[i]; |
| 318 | } |
| 319 | |
| 320 | if (mOwnsBuffer) { |
| 321 | delete[] mBuffer; |
| 322 | } |
| 323 | mBuffer = newBuffer; |
| 324 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 325 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 326 | mOwnsBuffer = true; |
| 327 | } |
| 328 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 329 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 330 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame^] | 331 | |
| 332 | // Define std interator interface for walking the array contents |
| 333 | // TODO: it might be nice to implement a full featured random access iterator... |
| 334 | class iterator : public std::iterator<std::bidirectional_iterator_tag, T> |
| 335 | { |
| 336 | public: |
| 337 | iterator(T* ptr) : mPtr(ptr) { } |
| 338 | iterator operator++() { iterator i = *this; mPtr++; return i; } |
| 339 | iterator operator++(int) { mPtr++; return *this; } |
| 340 | iterator operator--() { iterator i = *this; mPtr--; return i; } |
| 341 | iterator operator--(int) { mPtr--; return *this; } |
| 342 | T& operator*() { return *mPtr; } |
| 343 | T* operator->() { return mPtr; } |
| 344 | bool operator==(const iterator& rhs) const { return mPtr == rhs.mPtr; } |
| 345 | bool operator!=(const iterator& rhs) const { return mPtr != rhs.mPtr; } |
| 346 | private: |
| 347 | T* mPtr; |
| 348 | }; |
| 349 | iterator begin() { return data(); } |
| 350 | iterator end() { return data()+mSize; } |
| 351 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 352 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 353 | details::hidl_pointer<T> mBuffer; |
| 354 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 355 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 356 | |
| 357 | // copy from an array-like object, assuming my resources are freed. |
| 358 | template <typename Array> |
| 359 | void copyFrom(const Array &data, size_t size) { |
| 360 | mSize = size; |
| 361 | mOwnsBuffer = true; |
| 362 | if (mSize > 0) { |
| 363 | mBuffer = new T[size]; |
| 364 | for (size_t i = 0; i < size; ++i) { |
| 365 | mBuffer[i] = data[i]; |
| 366 | } |
| 367 | } else { |
| 368 | mBuffer = NULL; |
| 369 | } |
| 370 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 371 | }; |
| 372 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 373 | template <typename T> |
| 374 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 375 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 376 | //////////////////////////////////////////////////////////////////////////////// |
| 377 | |
| 378 | namespace details { |
| 379 | |
| 380 | template<size_t SIZE1, size_t... SIZES> |
| 381 | struct product { |
| 382 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 383 | }; |
| 384 | |
| 385 | template<size_t SIZE1> |
| 386 | struct product<SIZE1> { |
| 387 | static constexpr size_t value = SIZE1; |
| 388 | }; |
| 389 | |
| 390 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 391 | struct accessor { |
| 392 | explicit accessor(T *base) |
| 393 | : mBase(base) { |
| 394 | } |
| 395 | |
| 396 | accessor<T, SIZES...> operator[](size_t index) { |
| 397 | return accessor<T, SIZES...>( |
| 398 | &mBase[index * product<SIZES...>::value]); |
| 399 | } |
| 400 | |
| 401 | private: |
| 402 | T *mBase; |
| 403 | }; |
| 404 | |
| 405 | template<typename T, size_t SIZE1> |
| 406 | struct accessor<T, SIZE1> { |
| 407 | explicit accessor(T *base) |
| 408 | : mBase(base) { |
| 409 | } |
| 410 | |
| 411 | T &operator[](size_t index) { |
| 412 | return mBase[index]; |
| 413 | } |
| 414 | |
| 415 | private: |
| 416 | T *mBase; |
| 417 | }; |
| 418 | |
| 419 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 420 | struct const_accessor { |
| 421 | explicit const_accessor(const T *base) |
| 422 | : mBase(base) { |
| 423 | } |
| 424 | |
| 425 | const_accessor<T, SIZES...> operator[](size_t index) { |
| 426 | return const_accessor<T, SIZES...>( |
| 427 | &mBase[index * product<SIZES...>::value]); |
| 428 | } |
| 429 | |
| 430 | private: |
| 431 | const T *mBase; |
| 432 | }; |
| 433 | |
| 434 | template<typename T, size_t SIZE1> |
| 435 | struct const_accessor<T, SIZE1> { |
| 436 | explicit const_accessor(const T *base) |
| 437 | : mBase(base) { |
| 438 | } |
| 439 | |
| 440 | const T &operator[](size_t index) const { |
| 441 | return mBase[index]; |
| 442 | } |
| 443 | |
| 444 | private: |
| 445 | const T *mBase; |
| 446 | }; |
| 447 | |
| 448 | } // namespace details |
| 449 | |
| 450 | //////////////////////////////////////////////////////////////////////////////// |
| 451 | |
| 452 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 453 | struct hidl_array { |
| 454 | hidl_array() = default; |
| 455 | |
| 456 | T *data() { return mBuffer; } |
| 457 | const T *data() const { return mBuffer; } |
| 458 | |
| 459 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 460 | return details::accessor<T, SIZES...>( |
| 461 | &mBuffer[index * details::product<SIZES...>::value]); |
| 462 | } |
| 463 | |
| 464 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 465 | return details::const_accessor<T, SIZES...>( |
| 466 | &mBuffer[index * details::product<SIZES...>::value]); |
| 467 | } |
| 468 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 469 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 470 | |
| 471 | static constexpr size_tuple_type size() { |
| 472 | return std::make_tuple(SIZE1, SIZES...); |
| 473 | } |
| 474 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 475 | private: |
| 476 | T mBuffer[details::product<SIZE1, SIZES...>::value]; |
| 477 | }; |
| 478 | |
| 479 | template<typename T, size_t SIZE1> |
| 480 | struct hidl_array<T, SIZE1> { |
| 481 | hidl_array() = default; |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 482 | hidl_array(const T *source) { |
| 483 | memcpy(mBuffer, source, SIZE1 * sizeof(T)); |
| 484 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 485 | |
| 486 | T *data() { return mBuffer; } |
| 487 | const T *data() const { return mBuffer; } |
| 488 | |
| 489 | T &operator[](size_t index) { |
| 490 | return mBuffer[index]; |
| 491 | } |
| 492 | |
| 493 | const T &operator[](size_t index) const { |
| 494 | return mBuffer[index]; |
| 495 | } |
| 496 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 497 | static constexpr size_t size() { return SIZE1; } |
| 498 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 499 | private: |
| 500 | T mBuffer[SIZE1]; |
| 501 | }; |
| 502 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 503 | // ---------------------------------------------------------------------- |
| 504 | // Version functions |
| 505 | struct hidl_version { |
| 506 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 507 | constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {} |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 508 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 509 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 510 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 511 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 512 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 513 | constexpr uint16_t get_major() const { return mMajor; } |
| 514 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 515 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 516 | private: |
| 517 | uint16_t mMajor; |
| 518 | uint16_t mMinor; |
| 519 | }; |
| 520 | |
| 521 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 522 | return hidl_version(major,minor); |
| 523 | } |
| 524 | |
Yifan Hong | c1a60ef | 2016-11-08 18:53:16 -0800 | [diff] [blame] | 525 | struct IBase : virtual public RefBase { |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 526 | virtual bool isRemote() const = 0; |
| 527 | // HIDL reserved methods follow. |
| 528 | virtual ::android::hardware::Return<void> interfaceChain( |
| 529 | std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0; |
| 530 | // descriptor for HIDL reserved methods. |
Steven Moreland | 5a68232 | 2016-11-11 12:32:50 -0800 | [diff] [blame] | 531 | static const char* descriptor; |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 532 | }; |
| 533 | |
Steven Moreland | bdf2666 | 2016-09-02 11:03:15 -0700 | [diff] [blame] | 534 | #if defined(__LP64__) |
| 535 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" |
| 536 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" |
| 537 | #define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" |
| 538 | #else |
| 539 | #define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" |
| 540 | #define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" |
| 541 | #define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" |
| 542 | #endif |
| 543 | |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 544 | #define DECLARE_SERVICE_MANAGER_INTERACTIONS(INTERFACE) \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 545 | static ::android::sp<I##INTERFACE> getService( \ |
Iliyan Malchev | b42ce26 | 2016-09-26 00:09:35 -0700 | [diff] [blame] | 546 | const std::string &serviceName, bool getStub=false); \ |
Steven Moreland | 40ede2c | 2016-11-09 13:51:53 -0800 | [diff] [blame] | 547 | ::android::status_t registerAsService(const std::string &serviceName); \ |
| 548 | static bool registerForNotifications( \ |
| 549 | const std::string &serviceName, \ |
| 550 | const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \ |
| 551 | ¬ification); \ |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 552 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 553 | // ---------------------------------------------------------------------- |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 554 | // Class that provides Hidl instrumentation utilities. |
| 555 | struct HidlInstrumentor { |
| 556 | // Event that triggers the instrumentation. e.g. enter of an API call on |
| 557 | // the server/client side, exit of an API call on the server/client side |
| 558 | // etc. |
| 559 | enum InstrumentationEvent { |
| 560 | SERVER_API_ENTRY = 0, |
| 561 | SERVER_API_EXIT, |
| 562 | CLIENT_API_ENTRY, |
| 563 | CLIENT_API_EXIT, |
| 564 | SYNC_CALLBACK_ENTRY, |
| 565 | SYNC_CALLBACK_EXIT, |
| 566 | ASYNC_CALLBACK_ENTRY, |
| 567 | ASYNC_CALLBACK_EXIT, |
Steven Moreland | cefbd6e | 2016-11-01 10:14:30 -0700 | [diff] [blame] | 568 | PASSTHROUGH_ENTRY, |
| 569 | PASSTHROUGH_EXIT, |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 570 | }; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 571 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 572 | // Signature of the instrumentation callback function. |
| 573 | using InstrumentationCallback = std::function<void( |
| 574 | const InstrumentationEvent event, |
| 575 | const char *package, |
| 576 | const char *version, |
| 577 | const char *interface, |
| 578 | const char *method, |
| 579 | std::vector<void *> *args)>; |
| 580 | |
| 581 | explicit HidlInstrumentor(const std::string &prefix); |
| 582 | virtual ~HidlInstrumentor(); |
| 583 | |
| 584 | protected: |
| 585 | // Function that lookup and dynamically loads the hidl instrumentation |
| 586 | // libraries and registers the instrumentation callback functions. |
| 587 | // |
| 588 | // The instrumentation libraries should be stored under any of the following |
| 589 | // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and |
| 590 | // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should |
| 591 | // follow pattern: ^profilerPrefix(.*).profiler.so$ |
| 592 | // |
| 593 | // Each instrumentation library is expected to implement the instrumentation |
| 594 | // function called HIDL_INSTRUMENTATION_FUNCTION. |
| 595 | // |
| 596 | // A no-op for user build. |
| 597 | void registerInstrumentationCallbacks( |
| 598 | const std::string &profilerPrefix, |
| 599 | std::vector<InstrumentationCallback> *instrumentationCallbacks); |
| 600 | |
| 601 | // Utility function to determine whether a give file is a instrumentation |
| 602 | // library (i.e. the file name follow the expected pattern). |
| 603 | bool isInstrumentationLib( |
| 604 | const std::string &profilerPrefix, |
| 605 | const dirent *file); |
| 606 | // A list of registered instrumentation callbacks. |
| 607 | std::vector<InstrumentationCallback> mInstrumentationCallbacks; |
| 608 | // Flag whether to enable instrumentation. |
| 609 | bool mEnableInstrumentation; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 610 | }; |
| 611 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 612 | } // namespace hardware |
| 613 | } // namespace android |
| 614 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 615 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 616 | #endif // ANDROID_HIDL_SUPPORT_H |
| 617 | |