blob: 3f7248c7507dddf49eb0a010ef14e0cd5ddb692f [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
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 Malchev692070a2016-09-12 16:30:44 -070020#include <algorithm>
Yifan Hong44ab6232016-11-22 16:28:24 -080021#include <array>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070022#include <dirent.h>
Scott Randolphbb840f72016-11-21 14:39:26 -080023#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010024#include <cutils/native_handle.h>
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -070025#include <cutils/properties.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <functional>
Steven Moreland337c3ae2016-11-22 13:37:32 -080027#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070028#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080029#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080030#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080031#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070032#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080033#include <type_traits>
Iliyan Malchev692070a2016-09-12 16:30:44 -070034#include <utils/Errors.h>
35#include <utils/RefBase.h>
36#include <utils/StrongPointer.h>
Yifan Hong44c0e572017-01-20 15:41:52 -080037#include <vintf/Transport.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080038#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020039
40namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010041
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010042// this file is included by all hidl interface, so we must forward declare the
43// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010044namespace hidl {
45namespace memory {
46namespace V1_0 {
47 struct IMemory;
48}; // namespace V1_0
49}; // namespace manager
50}; // namespace hidl
51
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010052namespace hidl {
53namespace base {
54namespace V1_0 {
55 struct IBase;
56}; // namespace V1_0
57}; // namespace base
58}; // namespace hidl
59
Martijn Coenen72110162016-08-19 14:28:25 +020060namespace hardware {
61
Yifan Hong44c0e572017-01-20 15:41:52 -080062// Get transport method from vendor interface manifest.
63// name has the format "android.hardware.foo"
64vintf::Transport getTransportFromManifest(const std::string &name);
65
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010066// hidl_death_recipient is a callback interfaced that can be used with
67// linkToDeath() / unlinkToDeath()
68struct hidl_death_recipient : public virtual RefBase {
69 virtual void serviceDied(uint64_t cookie,
70 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
71};
72
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010073// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
74// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010075// The ownership semantics for this are:
76// 1) The conversion constructor and assignment operator taking a const native_handle_t*
77// do not take ownership of the handle; this is because these operations are usually
78// just done for IPC, and cloning by default is a waste of resources. If you want
79// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
80// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
81// that is because it's not intuitive that this class encapsulates a native_handle_t
82// which needs cloning to be valid; in particular, this allows constructs like this:
83// hidl_handle copy;
84// foo->someHidlCall([&](auto incoming_handle) {
85// copy = incoming_handle;
86// });
87// // copy and its enclosed file descriptors will remain valid here.
88// 3) The move constructor does what you would expect; it only owns the handle if the
89// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010090struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010091 hidl_handle();
92 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010093
Martijn Coenen04b91c02017-01-19 14:14:21 +010094 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010095
Martijn Coenen04b91c02017-01-19 14:14:21 +010096 // copy constructor.
97 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010098
99 // move constructor.
Martijn Coenen04b91c02017-01-19 14:14:21 +0100100 hidl_handle(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100101
Steven Moreland6ffdc2a2017-01-23 12:34:33 -0800102 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100103 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100104
Martijn Coenen04b91c02017-01-19 14:14:21 +0100105 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100106
Martijn Coenen04b91c02017-01-19 14:14:21 +0100107 hidl_handle &operator=(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100108
Martijn Coenen04b91c02017-01-19 14:14:21 +0100109 void setTo(native_handle_t* handle, bool shouldOwn = false);
110
111 const native_handle_t* operator->() const;
112
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100113 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100114 operator const native_handle_t *() const;
115
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100116 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100117 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100118private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100119 void freeHandle();
120
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100121 details::hidl_pointer<const native_handle_t> mHandle;
Martijn Coenen04b91c02017-01-19 14:14:21 +0100122 bool mOwnsHandle;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100123};
124
Martijn Coenen72110162016-08-19 14:28:25 +0200125struct hidl_string {
126 hidl_string();
127 ~hidl_string();
128
Yifan Hong602b85a2016-10-24 13:40:01 -0700129 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200130 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700131 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700132 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800133 // copy the first length characters from a C-style string.
134 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700135 // copy from an std::string.
136 hidl_string(const std::string &);
137
138 // move constructor.
139 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200140
141 const char *c_str() const;
142 size_t size() const;
143 bool empty() const;
144
Yifan Hong602b85a2016-10-24 13:40:01 -0700145 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700146 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700147 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200148 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700149 // copy from an std::string.
150 hidl_string &operator=(const std::string &);
151 // move assignment operator.
152 hidl_string &operator=(hidl_string &&other);
153 // cast to std::string.
154 operator std::string() const;
155 // cast to C-style string. Caller is responsible
156 // to maintain this hidl_string alive.
157 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700158
Steven Morelanda2a81842017-01-20 14:48:15 -0800159 bool operator< (const hidl_string &rhs) const;
160
Martijn Coenen72110162016-08-19 14:28:25 +0200161 void clear();
162
163 // Reference an external char array. Ownership is _not_ transferred.
164 // Caller is responsible for ensuring that underlying memory is valid
165 // for the lifetime of this hidl_string.
166 void setToExternal(const char *data, size_t size);
167
Andreas Huberebfeb362016-08-25 13:39:05 -0700168 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
169 static const size_t kOffsetOfBuffer;
170
Martijn Coenen72110162016-08-19 14:28:25 +0200171private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100172 details::hidl_pointer<const char> mBuffer;
173 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700174 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200175
Yifan Hong602b85a2016-10-24 13:40:01 -0700176 // copy from data with size. Assume that my memory is freed
177 // (through clear(), for example)
178 void copyFrom(const char *data, size_t size);
179 // move from another hidl_string
180 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200181};
182
Scott Randolphbb840f72016-11-21 14:39:26 -0800183inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
184 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
185}
186
187inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
188 return !(hs1 == hs2);
189}
190
Yifan Hong5708fb42016-10-26 17:50:29 -0700191inline bool operator==(const hidl_string &hs, const char *s) {
192 return strcmp(hs.c_str(), s) == 0;
193}
194
195inline bool operator!=(const hidl_string &hs, const char *s) {
196 return !(hs == s);
197}
198
199inline bool operator==(const char *s, const hidl_string &hs) {
200 return strcmp(hs.c_str(), s) == 0;
201}
202
203inline bool operator!=(const char *s, const hidl_string &hs) {
204 return !(s == hs);
205}
206
Martijn Coenen30791002016-12-01 15:40:46 +0100207// hidl_memory is a structure that can be used to transfer
208// pieces of shared memory between processes. The assumption
209// of this object is that the memory remains accessible as
210// long as the file descriptors in the enclosed mHandle
211// - as well as all of its cross-process dups() - remain opened.
212struct hidl_memory {
213
Martijn Coenen04b91c02017-01-19 14:14:21 +0100214 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100215 }
216
217 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100218 * Creates a hidl_memory object, but doesn't take ownership of
219 * the passed in native_handle_t; callers are responsible for
220 * making sure the handle remains valid while this object is
221 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100222 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100223 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
224 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100225 mSize(size),
226 mName(name)
227 {}
228
229 // copy constructor
230 hidl_memory(const hidl_memory& other) {
231 *this = other;
232 }
233
234 // copy assignment
235 hidl_memory &operator=(const hidl_memory &other) {
236 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100237 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100238 mSize = other.mSize;
239 mName = other.mName;
240 }
241
242 return *this;
243 }
244
245 // TODO move constructor/move assignment
246
247 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100248 }
249
250 const native_handle_t* handle() const {
251 return mHandle;
252 }
253
254 const hidl_string &name() const {
255 return mName;
256 }
257
258 size_t size() const {
259 return mSize;
260 }
261
262 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
263 static const size_t kOffsetOfHandle;
264 // offsetof(hidl_memory, mName) exposed since mHandle is private.
265 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800266
Martijn Coenen30791002016-12-01 15:40:46 +0100267private:
Martijn Coenen30791002016-12-01 15:40:46 +0100268 hidl_handle mHandle;
269 size_t mSize;
270 hidl_string mName;
271};
272
Andreas Huber20dce082016-09-22 19:39:13 -0700273////////////////////////////////////////////////////////////////////////////////
274
Martijn Coenen72110162016-08-19 14:28:25 +0200275template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100276struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200277 hidl_vec()
278 : mBuffer(NULL),
279 mSize(0),
280 mOwnsBuffer(true) {
281 }
282
Yifan Hong602b85a2016-10-24 13:40:01 -0700283 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200284 *this = other;
285 }
286
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100287 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800288 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100289 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700290 }
291
Steven Morelandb69926a2016-11-15 10:02:57 -0800292 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800293 : mOwnsBuffer(true) {
294 if (list.size() > UINT32_MAX) {
295 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
296 }
297 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800298 mBuffer = new T[mSize];
299
Steven Morelandb69926a2016-11-15 10:02:57 -0800300 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800301 for (auto it = list.begin(); it != list.end(); ++it) {
302 mBuffer[idx++] = *it;
303 }
304 }
305
Yifan Hong602b85a2016-10-24 13:40:01 -0700306 hidl_vec(const std::vector<T> &other) : hidl_vec() {
307 *this = other;
308 }
309
Martijn Coenen72110162016-08-19 14:28:25 +0200310 ~hidl_vec() {
311 if (mOwnsBuffer) {
312 delete[] mBuffer;
313 }
314 mBuffer = NULL;
315 }
316
Alexey Polyudove2299012016-10-19 09:52:00 -0700317 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200318 // caller's responsibility to ensure that the underlying memory stays
319 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700320 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200321 if (mOwnsBuffer) {
322 delete [] mBuffer;
323 }
324 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100325 if (size > UINT32_MAX) {
326 logAlwaysFatal("external vector size exceeds 2^32 elements.");
327 }
328 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700329 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200330 }
331
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700332 T *data() {
333 return mBuffer;
334 }
335
336 const T *data() const {
337 return mBuffer;
338 }
339
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700340 T *releaseData() {
341 if (!mOwnsBuffer && mSize > 0) {
342 resize(mSize);
343 }
344 mOwnsBuffer = false;
345 return mBuffer;
346 }
347
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700348 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100349 if (mOwnsBuffer) {
350 delete[] mBuffer;
351 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700352 mBuffer = other.mBuffer;
353 mSize = other.mSize;
354 mOwnsBuffer = other.mOwnsBuffer;
355 other.mOwnsBuffer = false;
356 return *this;
357 }
358
Martijn Coenen72110162016-08-19 14:28:25 +0200359 hidl_vec &operator=(const hidl_vec &other) {
360 if (this != &other) {
361 if (mOwnsBuffer) {
362 delete[] mBuffer;
363 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700364 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200365 }
366
367 return *this;
368 }
369
Yifan Hong602b85a2016-10-24 13:40:01 -0700370 // copy from an std::vector.
371 hidl_vec &operator=(const std::vector<T> &other) {
372 if (mOwnsBuffer) {
373 delete[] mBuffer;
374 }
375 copyFrom(other, other.size());
376 return *this;
377 }
378
379 // cast to an std::vector.
380 operator std::vector<T>() const {
381 std::vector<T> v(mSize);
382 for (size_t i = 0; i < mSize; ++i) {
383 v[i] = mBuffer[i];
384 }
385 return v;
386 }
387
Yifan Hong9fcbb362016-12-20 16:46:41 -0800388 // equality check, assuming that T::operator== is defined.
389 bool operator==(const hidl_vec &other) const {
390 if (mSize != other.size()) {
391 return false;
392 }
393 for (size_t i = 0; i < mSize; ++i) {
394 if (!(mBuffer[i] == other.mBuffer[i])) {
395 return false;
396 }
397 }
398 return true;
399 }
400
401 // inequality check, assuming that T::operator== is defined.
402 inline bool operator!=(const hidl_vec &other) const {
403 return !((*this) == other);
404 }
405
Martijn Coenen72110162016-08-19 14:28:25 +0200406 size_t size() const {
407 return mSize;
408 }
409
410 T &operator[](size_t index) {
411 return mBuffer[index];
412 }
413
414 const T &operator[](size_t index) const {
415 return mBuffer[index];
416 }
417
418 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100419 if (size > UINT32_MAX) {
420 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
421 }
Martijn Coenen72110162016-08-19 14:28:25 +0200422 T *newBuffer = new T[size];
423
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100424 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200425 newBuffer[i] = mBuffer[i];
426 }
427
428 if (mOwnsBuffer) {
429 delete[] mBuffer;
430 }
431 mBuffer = newBuffer;
432
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100433 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200434 mOwnsBuffer = true;
435 }
436
Yifan Hong089ae132016-11-11 11:32:52 -0800437 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
438 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800439
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800440private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800441 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800442 template<bool is_const>
443 class iter : public std::iterator<
444 std::random_access_iterator_tag, /* Category */
445 T,
446 ptrdiff_t, /* Distance */
447 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
448 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800449 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800450 using traits = std::iterator_traits<iter>;
451 using ptr_type = typename traits::pointer;
452 using ref_type = typename traits::reference;
453 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800454 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800455 iter(ptr_type ptr) : mPtr(ptr) { }
456 inline iter &operator++() { mPtr++; return *this; }
457 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
458 inline iter &operator--() { mPtr--; return *this; }
459 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
460 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
461 inline iter operator+(diff_type n) const { return mPtr + n; }
462 inline iter operator-(diff_type n) const { return mPtr - n; }
463 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
464 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
465 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
466 inline ref_type operator*() const { return *mPtr; }
467 inline ptr_type operator->() const { return mPtr; }
468 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
469 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
470 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
471 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
472 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
473 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
474 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800475 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800476 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800477 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800478public:
479 using iterator = iter<false /* is_const */>;
480 using const_iterator = iter<true /* is_const */>;
481
Scott Randolphbb840f72016-11-21 14:39:26 -0800482 iterator begin() { return data(); }
483 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800484 const_iterator begin() const { return data(); }
485 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800486
Martijn Coenen72110162016-08-19 14:28:25 +0200487private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100488 details::hidl_pointer<T> mBuffer;
489 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200490 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700491
492 // copy from an array-like object, assuming my resources are freed.
493 template <typename Array>
494 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800495 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700496 mOwnsBuffer = true;
497 if (mSize > 0) {
498 mBuffer = new T[size];
499 for (size_t i = 0; i < size; ++i) {
500 mBuffer[i] = data[i];
501 }
502 } else {
503 mBuffer = NULL;
504 }
505 }
Martijn Coenen72110162016-08-19 14:28:25 +0200506};
507
Yifan Hong089ae132016-11-11 11:32:52 -0800508template <typename T>
509const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
510
Andreas Huber20dce082016-09-22 19:39:13 -0700511////////////////////////////////////////////////////////////////////////////////
512
513namespace details {
514
515 template<size_t SIZE1, size_t... SIZES>
516 struct product {
517 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
518 };
519
520 template<size_t SIZE1>
521 struct product<SIZE1> {
522 static constexpr size_t value = SIZE1;
523 };
524
525 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800526 struct std_array {
527 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
528 };
529
530 template<typename T, size_t SIZE1>
531 struct std_array<T, SIZE1> {
532 using type = std::array<T, SIZE1>;
533 };
534
535 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700536 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800537
538 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
539
Andreas Huber20dce082016-09-22 19:39:13 -0700540 explicit accessor(T *base)
541 : mBase(base) {
542 }
543
544 accessor<T, SIZES...> operator[](size_t index) {
545 return accessor<T, SIZES...>(
546 &mBase[index * product<SIZES...>::value]);
547 }
548
Yifan Hong44ab6232016-11-22 16:28:24 -0800549 accessor &operator=(const std_array_type &other) {
550 for (size_t i = 0; i < SIZE1; ++i) {
551 (*this)[i] = other[i];
552 }
553 return *this;
554 }
555
Andreas Huber20dce082016-09-22 19:39:13 -0700556 private:
557 T *mBase;
558 };
559
560 template<typename T, size_t SIZE1>
561 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800562
563 using std_array_type = typename std_array<T, SIZE1>::type;
564
Andreas Huber20dce082016-09-22 19:39:13 -0700565 explicit accessor(T *base)
566 : mBase(base) {
567 }
568
569 T &operator[](size_t index) {
570 return mBase[index];
571 }
572
Yifan Hong44ab6232016-11-22 16:28:24 -0800573 accessor &operator=(const std_array_type &other) {
574 for (size_t i = 0; i < SIZE1; ++i) {
575 (*this)[i] = other[i];
576 }
577 return *this;
578 }
579
Andreas Huber20dce082016-09-22 19:39:13 -0700580 private:
581 T *mBase;
582 };
583
584 template<typename T, size_t SIZE1, size_t... SIZES>
585 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800586
587 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
588
Andreas Huber20dce082016-09-22 19:39:13 -0700589 explicit const_accessor(const T *base)
590 : mBase(base) {
591 }
592
Yifan Hongbe7a6882017-01-05 17:30:17 -0800593 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700594 return const_accessor<T, SIZES...>(
595 &mBase[index * product<SIZES...>::value]);
596 }
597
Yifan Hong44ab6232016-11-22 16:28:24 -0800598 operator std_array_type() {
599 std_array_type array;
600 for (size_t i = 0; i < SIZE1; ++i) {
601 array[i] = (*this)[i];
602 }
603 return array;
604 }
605
Andreas Huber20dce082016-09-22 19:39:13 -0700606 private:
607 const T *mBase;
608 };
609
610 template<typename T, size_t SIZE1>
611 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800612
613 using std_array_type = typename std_array<T, SIZE1>::type;
614
Andreas Huber20dce082016-09-22 19:39:13 -0700615 explicit const_accessor(const T *base)
616 : mBase(base) {
617 }
618
619 const T &operator[](size_t index) const {
620 return mBase[index];
621 }
622
Yifan Hong44ab6232016-11-22 16:28:24 -0800623 operator std_array_type() {
624 std_array_type array;
625 for (size_t i = 0; i < SIZE1; ++i) {
626 array[i] = (*this)[i];
627 }
628 return array;
629 }
630
Andreas Huber20dce082016-09-22 19:39:13 -0700631 private:
632 const T *mBase;
633 };
634
635} // namespace details
636
637////////////////////////////////////////////////////////////////////////////////
638
Yifan Hong44ab6232016-11-22 16:28:24 -0800639// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700640template<typename T, size_t SIZE1, size_t... SIZES>
641struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800642
643 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
644
Andreas Huber20dce082016-09-22 19:39:13 -0700645 hidl_array() = default;
646
Yifan Hong44ab6232016-11-22 16:28:24 -0800647 // Copies the data from source, using T::operator=(const T &).
648 hidl_array(const T *source) {
649 for (size_t i = 0; i < elementCount(); ++i) {
650 mBuffer[i] = source[i];
651 }
652 }
653
654 // Copies the data from the given std::array, using T::operator=(const T &).
655 hidl_array(const std_array_type &array) {
656 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
657 modifier = array;
658 }
659
Andreas Huber20dce082016-09-22 19:39:13 -0700660 T *data() { return mBuffer; }
661 const T *data() const { return mBuffer; }
662
663 details::accessor<T, SIZES...> operator[](size_t index) {
664 return details::accessor<T, SIZES...>(
665 &mBuffer[index * details::product<SIZES...>::value]);
666 }
667
668 details::const_accessor<T, SIZES...> operator[](size_t index) const {
669 return details::const_accessor<T, SIZES...>(
670 &mBuffer[index * details::product<SIZES...>::value]);
671 }
672
Yifan Hong9fcbb362016-12-20 16:46:41 -0800673 // equality check, assuming that T::operator== is defined.
674 bool operator==(const hidl_array &other) const {
675 for (size_t i = 0; i < elementCount(); ++i) {
676 if (!(mBuffer[i] == other.mBuffer[i])) {
677 return false;
678 }
679 }
680 return true;
681 }
682
683 inline bool operator!=(const hidl_array &other) const {
684 return !((*this) == other);
685 }
686
Andreas Huber00a985c2016-09-28 14:24:53 -0700687 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
688
689 static constexpr size_tuple_type size() {
690 return std::make_tuple(SIZE1, SIZES...);
691 }
692
Yifan Hong44ab6232016-11-22 16:28:24 -0800693 static constexpr size_t elementCount() {
694 return details::product<SIZE1, SIZES...>::value;
695 }
696
697 operator std_array_type() const {
698 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
699 }
700
Andreas Huber20dce082016-09-22 19:39:13 -0700701private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800702 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700703};
704
Yifan Hong44ab6232016-11-22 16:28:24 -0800705// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700706template<typename T, size_t SIZE1>
707struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800708
709 using std_array_type = typename details::std_array<T, SIZE1>::type;
710
Andreas Huber20dce082016-09-22 19:39:13 -0700711 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800712
713 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800714 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800715 for (size_t i = 0; i < elementCount(); ++i) {
716 mBuffer[i] = source[i];
717 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800718 }
Andreas Huber20dce082016-09-22 19:39:13 -0700719
Yifan Hong44ab6232016-11-22 16:28:24 -0800720 // Copies the data from the given std::array, using T::operator=(const T &).
721 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
722
Andreas Huber20dce082016-09-22 19:39:13 -0700723 T *data() { return mBuffer; }
724 const T *data() const { return mBuffer; }
725
726 T &operator[](size_t index) {
727 return mBuffer[index];
728 }
729
730 const T &operator[](size_t index) const {
731 return mBuffer[index];
732 }
733
Yifan Hong9fcbb362016-12-20 16:46:41 -0800734 // equality check, assuming that T::operator== is defined.
735 bool operator==(const hidl_array &other) const {
736 for (size_t i = 0; i < elementCount(); ++i) {
737 if (!(mBuffer[i] == other.mBuffer[i])) {
738 return false;
739 }
740 }
741 return true;
742 }
743
744 inline bool operator!=(const hidl_array &other) const {
745 return !((*this) == other);
746 }
747
Andreas Huber00a985c2016-09-28 14:24:53 -0700748 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800749 static constexpr size_t elementCount() { return SIZE1; }
750
751 // Copies the data to an std::array, using T::operator=(T).
752 operator std_array_type() const {
753 std_array_type array;
754 for (size_t i = 0; i < SIZE1; ++i) {
755 array[i] = mBuffer[i];
756 }
757 return array;
758 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700759
Andreas Huber20dce082016-09-22 19:39:13 -0700760private:
761 T mBuffer[SIZE1];
762};
763
Martijn Coenen72110162016-08-19 14:28:25 +0200764// ----------------------------------------------------------------------
765// Version functions
766struct hidl_version {
767public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800768 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200769
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700770 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200771 return (mMajor == other.get_major() && mMinor == other.get_minor());
772 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200773
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800774 bool operator<(const hidl_version& other) const {
775 return (mMajor < other.get_major() ||
776 (mMajor == other.get_major() && mMinor < other.get_minor()));
777 }
778
779 bool operator>(const hidl_version& other) const {
780 return other < *this;
781 }
782
783 bool operator<=(const hidl_version& other) const {
784 return !(*this > other);
785 }
786
787 bool operator>=(const hidl_version& other) const {
788 return !(*this < other);
789 }
790
Martijn Coenen097a7672016-09-08 16:56:41 +0200791 constexpr uint16_t get_major() const { return mMajor; }
792 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200793
Martijn Coenen72110162016-08-19 14:28:25 +0200794private:
795 uint16_t mMajor;
796 uint16_t mMinor;
797};
798
799inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
800 return hidl_version(major,minor);
801}
802
Steven Morelandbdf26662016-09-02 11:03:15 -0700803#if defined(__LP64__)
804#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
805#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
806#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
807#else
808#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
809#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
810#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
811#endif
812
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700813// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700814// Class that provides Hidl instrumentation utilities.
815struct HidlInstrumentor {
816 // Event that triggers the instrumentation. e.g. enter of an API call on
817 // the server/client side, exit of an API call on the server/client side
818 // etc.
819 enum InstrumentationEvent {
820 SERVER_API_ENTRY = 0,
821 SERVER_API_EXIT,
822 CLIENT_API_ENTRY,
823 CLIENT_API_EXIT,
824 SYNC_CALLBACK_ENTRY,
825 SYNC_CALLBACK_EXIT,
826 ASYNC_CALLBACK_ENTRY,
827 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700828 PASSTHROUGH_ENTRY,
829 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700830 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700831
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700832 // Signature of the instrumentation callback function.
833 using InstrumentationCallback = std::function<void(
834 const InstrumentationEvent event,
835 const char *package,
836 const char *version,
837 const char *interface,
838 const char *method,
839 std::vector<void *> *args)>;
840
841 explicit HidlInstrumentor(const std::string &prefix);
842 virtual ~HidlInstrumentor();
843
844 protected:
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800845 // Set mEnableInstrumentation based on system property
846 // hal.instrumentation.enable, register/de-register instrumentation
847 // callbacks if mEnableInstrumentation is true/false.
848 void configureInstrumentation(bool log=true);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700849 // Function that lookup and dynamically loads the hidl instrumentation
850 // libraries and registers the instrumentation callback functions.
851 //
852 // The instrumentation libraries should be stored under any of the following
853 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
854 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
855 // follow pattern: ^profilerPrefix(.*).profiler.so$
856 //
857 // Each instrumentation library is expected to implement the instrumentation
858 // function called HIDL_INSTRUMENTATION_FUNCTION.
859 //
860 // A no-op for user build.
861 void registerInstrumentationCallbacks(
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700862 std::vector<InstrumentationCallback> *instrumentationCallbacks);
863
864 // Utility function to determine whether a give file is a instrumentation
865 // library (i.e. the file name follow the expected pattern).
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800866 bool isInstrumentationLib(const dirent *file);
867
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700868 // A list of registered instrumentation callbacks.
869 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
870 // Flag whether to enable instrumentation.
871 bool mEnableInstrumentation;
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800872 // Prefix to lookup the instrumentation libraries.
873 std::string mInstrumentationLibPrefix;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700874};
875
Yifan Hongbe7a6882017-01-05 17:30:17 -0800876///////////////////// toString functions
877
878namespace details {
879
880// toString alias for numeric types
881template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
882inline std::string toString(T t) {
883 return std::to_string(t);
884}
885
886template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
887inline std::string toHexString(T t, bool prefix = true) {
888 std::ostringstream os;
889 if (prefix) { os << std::showbase; }
890 os << std::hex << t;
891 return os.str();
892}
893
894template<>
895inline std::string toHexString(uint8_t t, bool prefix) {
896 return toHexString(static_cast<int32_t>(t), prefix);
897}
898
899template<>
900inline std::string toHexString(int8_t t, bool prefix) {
901 return toHexString(static_cast<int32_t>(t), prefix);
902}
903
904inline std::string toString(const void *t, bool prefix = true) {
905 return toHexString(reinterpret_cast<uintptr_t>(t), prefix);
906}
907
908// debug string dump. There will be quotes around the string!
909inline std::string toString(const hidl_string &hs) {
910 return std::string{"\""} + hs.c_str() + "\"";
911}
912
913// debug string dump
914inline std::string toString(const hidl_handle &hs) {
915 return toString(hs.getNativeHandle());
916}
917
918inline std::string toString(const hidl_memory &mem) {
919 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
920 + toString(mem.size())
921 + ", .handle = " + toString(mem.handle()) + "}";
922}
923
924inline std::string toString(const sp<hidl_death_recipient> &dr) {
925 return std::string{"death_recipient@"} + toString(dr.get());
926}
927
928template<typename Array>
929std::string arrayToString(const Array &a, size_t size);
930
931// debug string dump, assuming that toString(T) is defined.
932template<typename T>
933std::string toString(const hidl_vec<T> &a) {
934 std::string os;
935 os += "[" + toString(a.size()) + "]";
936 os += arrayToString(a, a.size());
937 return os;
938}
939
940template<size_t SIZE1>
941std::string arraySizeToString() {
942 return std::string{"["} + toString(SIZE1) + "]";
943}
944
945template<typename T, size_t SIZE1>
946std::string toString(const_accessor<T, SIZE1> a) {
947 return arrayToString(a, SIZE1);
948}
949
950template<typename T, size_t SIZE1>
951std::string toString(const hidl_array<T, SIZE1> &a) {
952 return arraySizeToString<SIZE1>()
953 + toString(const_accessor<T, SIZE1>(a.data()));
954}
955
956template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
957std::string arraySizeToString() {
958 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
959}
960
961
962template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
963std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
964 return arrayToString(a, SIZE1);
965}
966
967template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
968std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
969 return arraySizeToString<SIZE1, SIZE2, SIZES...>()
970 + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
971}
972
973template<typename Array>
974std::string arrayToString(const Array &a, size_t size) {
975 std::string os;
976 os += "{";
977 for (size_t i = 0; i < size; ++i) {
978 if (i > 0) {
979 os += ", ";
980 }
981 os += toString(a[i]);
982 }
983 os += "}";
984 return os;
985}
986
987} // namespace details
988
989
Martijn Coenen72110162016-08-19 14:28:25 +0200990} // namespace hardware
991} // namespace android
992
Martijn Coenenc28f1152016-08-22 14:06:56 +0200993
Martijn Coenen72110162016-08-19 14:28:25 +0200994#endif // ANDROID_HIDL_SUPPORT_H