blob: 6c00c3d22c4271e08b766edb29c64083f348366e [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.
Yifan Hong20273f92017-01-30 14:13:19 -080063// name has the format "android.hardware.foo@1.0::IFoo"
64// If it starts with "android.hidl.", a static map is looked up instead.
65vintf::Transport getTransport(const std::string &name);
Yifan Hong44c0e572017-01-20 15:41:52 -080066
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010067// hidl_death_recipient is a callback interfaced that can be used with
68// linkToDeath() / unlinkToDeath()
69struct hidl_death_recipient : public virtual RefBase {
70 virtual void serviceDied(uint64_t cookie,
71 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
72};
73
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010074// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
75// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010076// The ownership semantics for this are:
77// 1) The conversion constructor and assignment operator taking a const native_handle_t*
78// do not take ownership of the handle; this is because these operations are usually
79// just done for IPC, and cloning by default is a waste of resources. If you want
80// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
81// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
82// that is because it's not intuitive that this class encapsulates a native_handle_t
83// which needs cloning to be valid; in particular, this allows constructs like this:
84// hidl_handle copy;
85// foo->someHidlCall([&](auto incoming_handle) {
86// copy = incoming_handle;
87// });
88// // copy and its enclosed file descriptors will remain valid here.
89// 3) The move constructor does what you would expect; it only owns the handle if the
90// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010091struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010092 hidl_handle();
93 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010094
Martijn Coenen04b91c02017-01-19 14:14:21 +010095 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010096
Martijn Coenen04b91c02017-01-19 14:14:21 +010097 // copy constructor.
98 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010099
100 // move constructor.
Martijn Coenen04b91c02017-01-19 14:14:21 +0100101 hidl_handle(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100102
Steven Moreland6ffdc2a2017-01-23 12:34:33 -0800103 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100104 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
Martijn Coenen04b91c02017-01-19 14:14:21 +0100106 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100107
Martijn Coenen04b91c02017-01-19 14:14:21 +0100108 hidl_handle &operator=(hidl_handle &&other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100109
Martijn Coenen04b91c02017-01-19 14:14:21 +0100110 void setTo(native_handle_t* handle, bool shouldOwn = false);
111
112 const native_handle_t* operator->() const;
113
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100114 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100115 operator const native_handle_t *() const;
116
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100117 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100118 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100119private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100120 void freeHandle();
121
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100122 details::hidl_pointer<const native_handle_t> mHandle;
Martijn Coenen04b91c02017-01-19 14:14:21 +0100123 bool mOwnsHandle;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100124};
125
Martijn Coenen72110162016-08-19 14:28:25 +0200126struct hidl_string {
127 hidl_string();
128 ~hidl_string();
129
Yifan Hong602b85a2016-10-24 13:40:01 -0700130 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200131 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700132 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700133 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800134 // copy the first length characters from a C-style string.
135 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700136 // copy from an std::string.
137 hidl_string(const std::string &);
138
139 // move constructor.
140 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200141
142 const char *c_str() const;
143 size_t size() const;
144 bool empty() const;
145
Yifan Hong602b85a2016-10-24 13:40:01 -0700146 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700147 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700148 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200149 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // copy from an std::string.
151 hidl_string &operator=(const std::string &);
152 // move assignment operator.
153 hidl_string &operator=(hidl_string &&other);
154 // cast to std::string.
155 operator std::string() const;
156 // cast to C-style string. Caller is responsible
157 // to maintain this hidl_string alive.
158 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700159
Martijn Coenen72110162016-08-19 14:28:25 +0200160 void clear();
161
162 // Reference an external char array. Ownership is _not_ transferred.
163 // Caller is responsible for ensuring that underlying memory is valid
164 // for the lifetime of this hidl_string.
165 void setToExternal(const char *data, size_t size);
166
Andreas Huberebfeb362016-08-25 13:39:05 -0700167 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
168 static const size_t kOffsetOfBuffer;
169
Martijn Coenen72110162016-08-19 14:28:25 +0200170private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100171 details::hidl_pointer<const char> mBuffer;
172 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700173 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200174
Yifan Hong602b85a2016-10-24 13:40:01 -0700175 // copy from data with size. Assume that my memory is freed
176 // (through clear(), for example)
177 void copyFrom(const char *data, size_t size);
178 // move from another hidl_string
179 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200180};
181
Steven Moreland551396a2017-02-13 18:33:20 -0800182#define HIDL_STRING_OPERATOR(OP) \
183 inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \
184 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; \
185 } \
186 inline bool operator OP(const hidl_string &hs, const char *s) { \
187 return strcmp(hs.c_str(), s) OP 0; \
188 } \
189 inline bool operator OP(const char *s, const hidl_string &hs) { \
190 return strcmp(hs.c_str(), s) OP 0; \
191 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800192
Steven Moreland551396a2017-02-13 18:33:20 -0800193HIDL_STRING_OPERATOR(==)
194HIDL_STRING_OPERATOR(!=)
195HIDL_STRING_OPERATOR(<)
196HIDL_STRING_OPERATOR(<=)
197HIDL_STRING_OPERATOR(>)
198HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800199
Steven Moreland551396a2017-02-13 18:33:20 -0800200#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700201
Martijn Coenen30791002016-12-01 15:40:46 +0100202// hidl_memory is a structure that can be used to transfer
203// pieces of shared memory between processes. The assumption
204// of this object is that the memory remains accessible as
205// long as the file descriptors in the enclosed mHandle
206// - as well as all of its cross-process dups() - remain opened.
207struct hidl_memory {
208
Martijn Coenen04b91c02017-01-19 14:14:21 +0100209 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100210 }
211
212 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100213 * Creates a hidl_memory object, but doesn't take ownership of
214 * the passed in native_handle_t; callers are responsible for
215 * making sure the handle remains valid while this object is
216 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100217 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100218 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
219 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100220 mSize(size),
221 mName(name)
222 {}
223
224 // copy constructor
225 hidl_memory(const hidl_memory& other) {
226 *this = other;
227 }
228
229 // copy assignment
230 hidl_memory &operator=(const hidl_memory &other) {
231 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100232 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100233 mSize = other.mSize;
234 mName = other.mName;
235 }
236
237 return *this;
238 }
239
240 // TODO move constructor/move assignment
241
242 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100243 }
244
245 const native_handle_t* handle() const {
246 return mHandle;
247 }
248
249 const hidl_string &name() const {
250 return mName;
251 }
252
253 size_t size() const {
254 return mSize;
255 }
256
257 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
258 static const size_t kOffsetOfHandle;
259 // offsetof(hidl_memory, mName) exposed since mHandle is private.
260 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800261
Martijn Coenen30791002016-12-01 15:40:46 +0100262private:
Martijn Coenen30791002016-12-01 15:40:46 +0100263 hidl_handle mHandle;
264 size_t mSize;
265 hidl_string mName;
266};
267
Andreas Huber20dce082016-09-22 19:39:13 -0700268////////////////////////////////////////////////////////////////////////////////
269
Martijn Coenen72110162016-08-19 14:28:25 +0200270template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100271struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200272 hidl_vec()
273 : mBuffer(NULL),
274 mSize(0),
275 mOwnsBuffer(true) {
276 }
277
Yifan Hong602b85a2016-10-24 13:40:01 -0700278 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200279 *this = other;
280 }
281
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100282 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800283 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100284 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700285 }
286
Steven Morelandb69926a2016-11-15 10:02:57 -0800287 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800288 : mOwnsBuffer(true) {
289 if (list.size() > UINT32_MAX) {
290 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
291 }
292 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800293 mBuffer = new T[mSize];
294
Steven Morelandb69926a2016-11-15 10:02:57 -0800295 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800296 for (auto it = list.begin(); it != list.end(); ++it) {
297 mBuffer[idx++] = *it;
298 }
299 }
300
Yifan Hong602b85a2016-10-24 13:40:01 -0700301 hidl_vec(const std::vector<T> &other) : hidl_vec() {
302 *this = other;
303 }
304
Martijn Coenen72110162016-08-19 14:28:25 +0200305 ~hidl_vec() {
306 if (mOwnsBuffer) {
307 delete[] mBuffer;
308 }
309 mBuffer = NULL;
310 }
311
Alexey Polyudove2299012016-10-19 09:52:00 -0700312 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200313 // caller's responsibility to ensure that the underlying memory stays
314 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700315 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200316 if (mOwnsBuffer) {
317 delete [] mBuffer;
318 }
319 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100320 if (size > UINT32_MAX) {
321 logAlwaysFatal("external vector size exceeds 2^32 elements.");
322 }
323 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700324 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200325 }
326
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700327 T *data() {
328 return mBuffer;
329 }
330
331 const T *data() const {
332 return mBuffer;
333 }
334
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700335 T *releaseData() {
336 if (!mOwnsBuffer && mSize > 0) {
337 resize(mSize);
338 }
339 mOwnsBuffer = false;
340 return mBuffer;
341 }
342
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700343 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100344 if (mOwnsBuffer) {
345 delete[] mBuffer;
346 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700347 mBuffer = other.mBuffer;
348 mSize = other.mSize;
349 mOwnsBuffer = other.mOwnsBuffer;
350 other.mOwnsBuffer = false;
351 return *this;
352 }
353
Martijn Coenen72110162016-08-19 14:28:25 +0200354 hidl_vec &operator=(const hidl_vec &other) {
355 if (this != &other) {
356 if (mOwnsBuffer) {
357 delete[] mBuffer;
358 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700359 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200360 }
361
362 return *this;
363 }
364
Yifan Hong602b85a2016-10-24 13:40:01 -0700365 // copy from an std::vector.
366 hidl_vec &operator=(const std::vector<T> &other) {
367 if (mOwnsBuffer) {
368 delete[] mBuffer;
369 }
370 copyFrom(other, other.size());
371 return *this;
372 }
373
374 // cast to an std::vector.
375 operator std::vector<T>() const {
376 std::vector<T> v(mSize);
377 for (size_t i = 0; i < mSize; ++i) {
378 v[i] = mBuffer[i];
379 }
380 return v;
381 }
382
Yifan Hong9fcbb362016-12-20 16:46:41 -0800383 // equality check, assuming that T::operator== is defined.
384 bool operator==(const hidl_vec &other) const {
385 if (mSize != other.size()) {
386 return false;
387 }
388 for (size_t i = 0; i < mSize; ++i) {
389 if (!(mBuffer[i] == other.mBuffer[i])) {
390 return false;
391 }
392 }
393 return true;
394 }
395
396 // inequality check, assuming that T::operator== is defined.
397 inline bool operator!=(const hidl_vec &other) const {
398 return !((*this) == other);
399 }
400
Martijn Coenen72110162016-08-19 14:28:25 +0200401 size_t size() const {
402 return mSize;
403 }
404
405 T &operator[](size_t index) {
406 return mBuffer[index];
407 }
408
409 const T &operator[](size_t index) const {
410 return mBuffer[index];
411 }
412
413 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100414 if (size > UINT32_MAX) {
415 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
416 }
Martijn Coenen72110162016-08-19 14:28:25 +0200417 T *newBuffer = new T[size];
418
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100419 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200420 newBuffer[i] = mBuffer[i];
421 }
422
423 if (mOwnsBuffer) {
424 delete[] mBuffer;
425 }
426 mBuffer = newBuffer;
427
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100428 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200429 mOwnsBuffer = true;
430 }
431
Yifan Hong089ae132016-11-11 11:32:52 -0800432 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
433 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800434
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800435private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800436 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800437 template<bool is_const>
438 class iter : public std::iterator<
439 std::random_access_iterator_tag, /* Category */
440 T,
441 ptrdiff_t, /* Distance */
442 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
443 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800444 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800445 using traits = std::iterator_traits<iter>;
446 using ptr_type = typename traits::pointer;
447 using ref_type = typename traits::reference;
448 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800449 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800450 iter(ptr_type ptr) : mPtr(ptr) { }
451 inline iter &operator++() { mPtr++; return *this; }
452 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
453 inline iter &operator--() { mPtr--; return *this; }
454 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
455 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
456 inline iter operator+(diff_type n) const { return mPtr + n; }
457 inline iter operator-(diff_type n) const { return mPtr - n; }
458 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
459 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
460 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
461 inline ref_type operator*() const { return *mPtr; }
462 inline ptr_type operator->() const { return mPtr; }
463 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
464 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
465 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
466 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
467 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
468 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
469 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800470 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800471 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800472 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800473public:
474 using iterator = iter<false /* is_const */>;
475 using const_iterator = iter<true /* is_const */>;
476
Scott Randolphbb840f72016-11-21 14:39:26 -0800477 iterator begin() { return data(); }
478 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800479 const_iterator begin() const { return data(); }
480 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800481
Martijn Coenen72110162016-08-19 14:28:25 +0200482private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100483 details::hidl_pointer<T> mBuffer;
484 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200485 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700486
487 // copy from an array-like object, assuming my resources are freed.
488 template <typename Array>
489 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800490 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700491 mOwnsBuffer = true;
492 if (mSize > 0) {
493 mBuffer = new T[size];
494 for (size_t i = 0; i < size; ++i) {
495 mBuffer[i] = data[i];
496 }
497 } else {
498 mBuffer = NULL;
499 }
500 }
Martijn Coenen72110162016-08-19 14:28:25 +0200501};
502
Yifan Hong089ae132016-11-11 11:32:52 -0800503template <typename T>
504const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
505
Andreas Huber20dce082016-09-22 19:39:13 -0700506////////////////////////////////////////////////////////////////////////////////
507
508namespace details {
509
510 template<size_t SIZE1, size_t... SIZES>
511 struct product {
512 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
513 };
514
515 template<size_t SIZE1>
516 struct product<SIZE1> {
517 static constexpr size_t value = SIZE1;
518 };
519
520 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800521 struct std_array {
522 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
523 };
524
525 template<typename T, size_t SIZE1>
526 struct std_array<T, SIZE1> {
527 using type = std::array<T, SIZE1>;
528 };
529
530 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700531 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800532
533 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
534
Andreas Huber20dce082016-09-22 19:39:13 -0700535 explicit accessor(T *base)
536 : mBase(base) {
537 }
538
539 accessor<T, SIZES...> operator[](size_t index) {
540 return accessor<T, SIZES...>(
541 &mBase[index * product<SIZES...>::value]);
542 }
543
Yifan Hong44ab6232016-11-22 16:28:24 -0800544 accessor &operator=(const std_array_type &other) {
545 for (size_t i = 0; i < SIZE1; ++i) {
546 (*this)[i] = other[i];
547 }
548 return *this;
549 }
550
Andreas Huber20dce082016-09-22 19:39:13 -0700551 private:
552 T *mBase;
553 };
554
555 template<typename T, size_t SIZE1>
556 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800557
558 using std_array_type = typename std_array<T, SIZE1>::type;
559
Andreas Huber20dce082016-09-22 19:39:13 -0700560 explicit accessor(T *base)
561 : mBase(base) {
562 }
563
564 T &operator[](size_t index) {
565 return mBase[index];
566 }
567
Yifan Hong44ab6232016-11-22 16:28:24 -0800568 accessor &operator=(const std_array_type &other) {
569 for (size_t i = 0; i < SIZE1; ++i) {
570 (*this)[i] = other[i];
571 }
572 return *this;
573 }
574
Andreas Huber20dce082016-09-22 19:39:13 -0700575 private:
576 T *mBase;
577 };
578
579 template<typename T, size_t SIZE1, size_t... SIZES>
580 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800581
582 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
583
Andreas Huber20dce082016-09-22 19:39:13 -0700584 explicit const_accessor(const T *base)
585 : mBase(base) {
586 }
587
Yifan Hongbe7a6882017-01-05 17:30:17 -0800588 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700589 return const_accessor<T, SIZES...>(
590 &mBase[index * product<SIZES...>::value]);
591 }
592
Yifan Hong44ab6232016-11-22 16:28:24 -0800593 operator std_array_type() {
594 std_array_type array;
595 for (size_t i = 0; i < SIZE1; ++i) {
596 array[i] = (*this)[i];
597 }
598 return array;
599 }
600
Andreas Huber20dce082016-09-22 19:39:13 -0700601 private:
602 const T *mBase;
603 };
604
605 template<typename T, size_t SIZE1>
606 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800607
608 using std_array_type = typename std_array<T, SIZE1>::type;
609
Andreas Huber20dce082016-09-22 19:39:13 -0700610 explicit const_accessor(const T *base)
611 : mBase(base) {
612 }
613
614 const T &operator[](size_t index) const {
615 return mBase[index];
616 }
617
Yifan Hong44ab6232016-11-22 16:28:24 -0800618 operator std_array_type() {
619 std_array_type array;
620 for (size_t i = 0; i < SIZE1; ++i) {
621 array[i] = (*this)[i];
622 }
623 return array;
624 }
625
Andreas Huber20dce082016-09-22 19:39:13 -0700626 private:
627 const T *mBase;
628 };
629
630} // namespace details
631
632////////////////////////////////////////////////////////////////////////////////
633
Yifan Hong44ab6232016-11-22 16:28:24 -0800634// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700635template<typename T, size_t SIZE1, size_t... SIZES>
636struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800637
638 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
639
Andreas Huber20dce082016-09-22 19:39:13 -0700640 hidl_array() = default;
641
Yifan Hong44ab6232016-11-22 16:28:24 -0800642 // Copies the data from source, using T::operator=(const T &).
643 hidl_array(const T *source) {
644 for (size_t i = 0; i < elementCount(); ++i) {
645 mBuffer[i] = source[i];
646 }
647 }
648
649 // Copies the data from the given std::array, using T::operator=(const T &).
650 hidl_array(const std_array_type &array) {
651 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
652 modifier = array;
653 }
654
Andreas Huber20dce082016-09-22 19:39:13 -0700655 T *data() { return mBuffer; }
656 const T *data() const { return mBuffer; }
657
658 details::accessor<T, SIZES...> operator[](size_t index) {
659 return details::accessor<T, SIZES...>(
660 &mBuffer[index * details::product<SIZES...>::value]);
661 }
662
663 details::const_accessor<T, SIZES...> operator[](size_t index) const {
664 return details::const_accessor<T, SIZES...>(
665 &mBuffer[index * details::product<SIZES...>::value]);
666 }
667
Yifan Hong9fcbb362016-12-20 16:46:41 -0800668 // equality check, assuming that T::operator== is defined.
669 bool operator==(const hidl_array &other) const {
670 for (size_t i = 0; i < elementCount(); ++i) {
671 if (!(mBuffer[i] == other.mBuffer[i])) {
672 return false;
673 }
674 }
675 return true;
676 }
677
678 inline bool operator!=(const hidl_array &other) const {
679 return !((*this) == other);
680 }
681
Andreas Huber00a985c2016-09-28 14:24:53 -0700682 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
683
684 static constexpr size_tuple_type size() {
685 return std::make_tuple(SIZE1, SIZES...);
686 }
687
Yifan Hong44ab6232016-11-22 16:28:24 -0800688 static constexpr size_t elementCount() {
689 return details::product<SIZE1, SIZES...>::value;
690 }
691
692 operator std_array_type() const {
693 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
694 }
695
Andreas Huber20dce082016-09-22 19:39:13 -0700696private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800697 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700698};
699
Yifan Hong44ab6232016-11-22 16:28:24 -0800700// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700701template<typename T, size_t SIZE1>
702struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800703
704 using std_array_type = typename details::std_array<T, SIZE1>::type;
705
Andreas Huber20dce082016-09-22 19:39:13 -0700706 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800707
708 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800709 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800710 for (size_t i = 0; i < elementCount(); ++i) {
711 mBuffer[i] = source[i];
712 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800713 }
Andreas Huber20dce082016-09-22 19:39:13 -0700714
Yifan Hong44ab6232016-11-22 16:28:24 -0800715 // Copies the data from the given std::array, using T::operator=(const T &).
716 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
717
Andreas Huber20dce082016-09-22 19:39:13 -0700718 T *data() { return mBuffer; }
719 const T *data() const { return mBuffer; }
720
721 T &operator[](size_t index) {
722 return mBuffer[index];
723 }
724
725 const T &operator[](size_t index) const {
726 return mBuffer[index];
727 }
728
Yifan Hong9fcbb362016-12-20 16:46:41 -0800729 // equality check, assuming that T::operator== is defined.
730 bool operator==(const hidl_array &other) const {
731 for (size_t i = 0; i < elementCount(); ++i) {
732 if (!(mBuffer[i] == other.mBuffer[i])) {
733 return false;
734 }
735 }
736 return true;
737 }
738
739 inline bool operator!=(const hidl_array &other) const {
740 return !((*this) == other);
741 }
742
Andreas Huber00a985c2016-09-28 14:24:53 -0700743 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800744 static constexpr size_t elementCount() { return SIZE1; }
745
746 // Copies the data to an std::array, using T::operator=(T).
747 operator std_array_type() const {
748 std_array_type array;
749 for (size_t i = 0; i < SIZE1; ++i) {
750 array[i] = mBuffer[i];
751 }
752 return array;
753 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700754
Andreas Huber20dce082016-09-22 19:39:13 -0700755private:
756 T mBuffer[SIZE1];
757};
758
Martijn Coenen72110162016-08-19 14:28:25 +0200759// ----------------------------------------------------------------------
760// Version functions
761struct hidl_version {
762public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800763 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200764
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700765 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200766 return (mMajor == other.get_major() && mMinor == other.get_minor());
767 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200768
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800769 bool operator<(const hidl_version& other) const {
770 return (mMajor < other.get_major() ||
771 (mMajor == other.get_major() && mMinor < other.get_minor()));
772 }
773
774 bool operator>(const hidl_version& other) const {
775 return other < *this;
776 }
777
778 bool operator<=(const hidl_version& other) const {
779 return !(*this > other);
780 }
781
782 bool operator>=(const hidl_version& other) const {
783 return !(*this < other);
784 }
785
Martijn Coenen097a7672016-09-08 16:56:41 +0200786 constexpr uint16_t get_major() const { return mMajor; }
787 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200788
Martijn Coenen72110162016-08-19 14:28:25 +0200789private:
790 uint16_t mMajor;
791 uint16_t mMinor;
792};
793
794inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
795 return hidl_version(major,minor);
796}
797
Steven Morelandbdf26662016-09-02 11:03:15 -0700798#if defined(__LP64__)
799#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
800#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
801#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
802#else
803#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
804#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
805#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
806#endif
807
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700808// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700809// Class that provides Hidl instrumentation utilities.
810struct HidlInstrumentor {
811 // Event that triggers the instrumentation. e.g. enter of an API call on
812 // the server/client side, exit of an API call on the server/client side
813 // etc.
814 enum InstrumentationEvent {
815 SERVER_API_ENTRY = 0,
816 SERVER_API_EXIT,
817 CLIENT_API_ENTRY,
818 CLIENT_API_EXIT,
819 SYNC_CALLBACK_ENTRY,
820 SYNC_CALLBACK_EXIT,
821 ASYNC_CALLBACK_ENTRY,
822 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700823 PASSTHROUGH_ENTRY,
824 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700825 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700826
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700827 // Signature of the instrumentation callback function.
828 using InstrumentationCallback = std::function<void(
829 const InstrumentationEvent event,
830 const char *package,
831 const char *version,
832 const char *interface,
833 const char *method,
834 std::vector<void *> *args)>;
835
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800836 explicit HidlInstrumentor(
837 const std::string &package,
838 const std::string &insterface);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700839 virtual ~HidlInstrumentor();
840
841 protected:
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800842 // Set mEnableInstrumentation based on system property
843 // hal.instrumentation.enable, register/de-register instrumentation
844 // callbacks if mEnableInstrumentation is true/false.
845 void configureInstrumentation(bool log=true);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700846 // Function that lookup and dynamically loads the hidl instrumentation
847 // libraries and registers the instrumentation callback functions.
848 //
849 // The instrumentation libraries should be stored under any of the following
850 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
851 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
852 // follow pattern: ^profilerPrefix(.*).profiler.so$
853 //
854 // Each instrumentation library is expected to implement the instrumentation
855 // function called HIDL_INSTRUMENTATION_FUNCTION.
856 //
857 // A no-op for user build.
858 void registerInstrumentationCallbacks(
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700859 std::vector<InstrumentationCallback> *instrumentationCallbacks);
860
861 // Utility function to determine whether a give file is a instrumentation
862 // library (i.e. the file name follow the expected pattern).
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800863 bool isInstrumentationLib(const dirent *file);
864
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700865 // A list of registered instrumentation callbacks.
866 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
867 // Flag whether to enable instrumentation.
868 bool mEnableInstrumentation;
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800869 // Prefix to lookup the instrumentation libraries.
Zhuoyao Zhang93c1e3a2017-01-23 17:37:49 -0800870 std::string mInstrumentationLibPackage;
871 // Used for dlsym to load the profiling method for given interface.
872 std::string mInterfaceName;
873
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