blob: 4a17c3e444707e397aa46d3e7eb8a71e16c58e02 [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.
75struct hidl_handle {
76 hidl_handle() {
77 mHandle = nullptr;
78 }
79 ~hidl_handle() {
80 }
81
82 // copy constructors.
83 hidl_handle(const native_handle_t *handle) {
84 mHandle = handle;
85 }
86
87 hidl_handle(const hidl_handle &other) {
88 mHandle = other.mHandle;
89 }
90
91 // move constructor.
92 hidl_handle(hidl_handle &&other) {
93 *this = std::move(other);
94 }
95
Steven Moreland6ffdc2a2017-01-23 12:34:33 -080096 // assignment operators
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010097 hidl_handle &operator=(const hidl_handle &other) {
98 mHandle = other.mHandle;
99 return *this;
100 }
101
102 hidl_handle &operator=(const native_handle_t *native_handle) {
103 mHandle = native_handle;
104 return *this;
105 }
106
107 hidl_handle &operator=(hidl_handle &&other) {
108 mHandle = other.mHandle;
109 other.mHandle = nullptr;
110 return *this;
111 }
112
113 const native_handle_t* operator->() const {
114 return mHandle;
115 }
116 // implicit conversion to const native_handle_t*
117 operator const native_handle_t *() const {
118 return mHandle;
119 }
120 // explicit conversion
121 const native_handle_t *getNativeHandle() const {
122 return mHandle;
123 }
124private:
125 details::hidl_pointer<const native_handle_t> mHandle;
126};
127
Martijn Coenen72110162016-08-19 14:28:25 +0200128struct hidl_string {
129 hidl_string();
130 ~hidl_string();
131
Yifan Hong602b85a2016-10-24 13:40:01 -0700132 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200133 hidl_string(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700134 // copy from a C-style string.
Steven Morelande03c0872016-10-24 10:43:50 -0700135 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800136 // copy the first length characters from a C-style string.
137 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700138 // copy from an std::string.
139 hidl_string(const std::string &);
140
141 // move constructor.
142 hidl_string(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200143
144 const char *c_str() const;
145 size_t size() const;
146 bool empty() const;
147
Yifan Hong602b85a2016-10-24 13:40:01 -0700148 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700149 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200151 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700152 // copy from an std::string.
153 hidl_string &operator=(const std::string &);
154 // move assignment operator.
155 hidl_string &operator=(hidl_string &&other);
156 // cast to std::string.
157 operator std::string() const;
158 // cast to C-style string. Caller is responsible
159 // to maintain this hidl_string alive.
160 operator const char *() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700161
Steven Morelanda2a81842017-01-20 14:48:15 -0800162 bool operator< (const hidl_string &rhs) const;
163
Martijn Coenen72110162016-08-19 14:28:25 +0200164 void clear();
165
166 // Reference an external char array. Ownership is _not_ transferred.
167 // Caller is responsible for ensuring that underlying memory is valid
168 // for the lifetime of this hidl_string.
169 void setToExternal(const char *data, size_t size);
170
Andreas Huberebfeb362016-08-25 13:39:05 -0700171 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
172 static const size_t kOffsetOfBuffer;
173
Martijn Coenen72110162016-08-19 14:28:25 +0200174private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100175 details::hidl_pointer<const char> mBuffer;
176 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700177 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200178
Yifan Hong602b85a2016-10-24 13:40:01 -0700179 // copy from data with size. Assume that my memory is freed
180 // (through clear(), for example)
181 void copyFrom(const char *data, size_t size);
182 // move from another hidl_string
183 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200184};
185
Scott Randolphbb840f72016-11-21 14:39:26 -0800186inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
187 return strcmp(hs1.c_str(), hs2.c_str()) == 0;
188}
189
190inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
191 return !(hs1 == hs2);
192}
193
Yifan Hong5708fb42016-10-26 17:50:29 -0700194inline bool operator==(const hidl_string &hs, const char *s) {
195 return strcmp(hs.c_str(), s) == 0;
196}
197
198inline bool operator!=(const hidl_string &hs, const char *s) {
199 return !(hs == s);
200}
201
202inline bool operator==(const char *s, const hidl_string &hs) {
203 return strcmp(hs.c_str(), s) == 0;
204}
205
206inline bool operator!=(const char *s, const hidl_string &hs) {
207 return !(s == hs);
208}
209
Martijn Coenen30791002016-12-01 15:40:46 +0100210// hidl_memory is a structure that can be used to transfer
211// pieces of shared memory between processes. The assumption
212// of this object is that the memory remains accessible as
213// long as the file descriptors in the enclosed mHandle
214// - as well as all of its cross-process dups() - remain opened.
215struct hidl_memory {
216
217 hidl_memory() : mOwnsHandle(false), mHandle(nullptr), mSize(0), mName("") {
218 }
219
220 /**
221 * Creates a hidl_memory object and takes ownership of the handle.
222 */
223 hidl_memory(const hidl_string &name, const hidl_handle &handle, size_t size)
224 : mOwnsHandle(true),
225 mHandle(handle),
226 mSize(size),
227 mName(name)
228 {}
229
230 // copy constructor
231 hidl_memory(const hidl_memory& other) {
232 *this = other;
233 }
234
235 // copy assignment
236 hidl_memory &operator=(const hidl_memory &other) {
237 if (this != &other) {
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800238 cleanup();
239
240 if (other.mHandle == nullptr) {
241 mHandle = nullptr;
242 mOwnsHandle = false;
243 } else {
244 mOwnsHandle = true;
245 mHandle = native_handle_clone(other.mHandle);
246 }
Martijn Coenen30791002016-12-01 15:40:46 +0100247 mSize = other.mSize;
248 mName = other.mName;
249 }
250
251 return *this;
252 }
253
254 // TODO move constructor/move assignment
255
256 ~hidl_memory() {
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800257 cleanup();
Martijn Coenen30791002016-12-01 15:40:46 +0100258 }
259
260 const native_handle_t* handle() const {
261 return mHandle;
262 }
263
264 const hidl_string &name() const {
265 return mName;
266 }
267
268 size_t size() const {
269 return mSize;
270 }
271
272 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
273 static const size_t kOffsetOfHandle;
274 // offsetof(hidl_memory, mName) exposed since mHandle is private.
275 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800276
Martijn Coenen30791002016-12-01 15:40:46 +0100277private:
278 bool mOwnsHandle;
279 hidl_handle mHandle;
280 size_t mSize;
281 hidl_string mName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800282
283 void cleanup() {
284 // TODO(b/33812533): native_handle_delete
285 if (mOwnsHandle && mHandle != nullptr) {
286 native_handle_close(mHandle);
287 }
288 }
Martijn Coenen30791002016-12-01 15:40:46 +0100289};
290
Andreas Huber20dce082016-09-22 19:39:13 -0700291////////////////////////////////////////////////////////////////////////////////
292
Martijn Coenen72110162016-08-19 14:28:25 +0200293template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100294struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200295 hidl_vec()
296 : mBuffer(NULL),
297 mSize(0),
298 mOwnsBuffer(true) {
299 }
300
Yifan Hong602b85a2016-10-24 13:40:01 -0700301 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200302 *this = other;
303 }
304
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100305 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800306 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100307 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700308 }
309
Steven Morelandb69926a2016-11-15 10:02:57 -0800310 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800311 : mOwnsBuffer(true) {
312 if (list.size() > UINT32_MAX) {
313 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
314 }
315 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800316 mBuffer = new T[mSize];
317
Steven Morelandb69926a2016-11-15 10:02:57 -0800318 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800319 for (auto it = list.begin(); it != list.end(); ++it) {
320 mBuffer[idx++] = *it;
321 }
322 }
323
Yifan Hong602b85a2016-10-24 13:40:01 -0700324 hidl_vec(const std::vector<T> &other) : hidl_vec() {
325 *this = other;
326 }
327
Martijn Coenen72110162016-08-19 14:28:25 +0200328 ~hidl_vec() {
329 if (mOwnsBuffer) {
330 delete[] mBuffer;
331 }
332 mBuffer = NULL;
333 }
334
Alexey Polyudove2299012016-10-19 09:52:00 -0700335 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200336 // caller's responsibility to ensure that the underlying memory stays
337 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700338 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200339 if (mOwnsBuffer) {
340 delete [] mBuffer;
341 }
342 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100343 if (size > UINT32_MAX) {
344 logAlwaysFatal("external vector size exceeds 2^32 elements.");
345 }
346 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700347 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200348 }
349
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700350 T *data() {
351 return mBuffer;
352 }
353
354 const T *data() const {
355 return mBuffer;
356 }
357
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700358 T *releaseData() {
359 if (!mOwnsBuffer && mSize > 0) {
360 resize(mSize);
361 }
362 mOwnsBuffer = false;
363 return mBuffer;
364 }
365
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700366 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100367 if (mOwnsBuffer) {
368 delete[] mBuffer;
369 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700370 mBuffer = other.mBuffer;
371 mSize = other.mSize;
372 mOwnsBuffer = other.mOwnsBuffer;
373 other.mOwnsBuffer = false;
374 return *this;
375 }
376
Martijn Coenen72110162016-08-19 14:28:25 +0200377 hidl_vec &operator=(const hidl_vec &other) {
378 if (this != &other) {
379 if (mOwnsBuffer) {
380 delete[] mBuffer;
381 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700382 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200383 }
384
385 return *this;
386 }
387
Yifan Hong602b85a2016-10-24 13:40:01 -0700388 // copy from an std::vector.
389 hidl_vec &operator=(const std::vector<T> &other) {
390 if (mOwnsBuffer) {
391 delete[] mBuffer;
392 }
393 copyFrom(other, other.size());
394 return *this;
395 }
396
397 // cast to an std::vector.
398 operator std::vector<T>() const {
399 std::vector<T> v(mSize);
400 for (size_t i = 0; i < mSize; ++i) {
401 v[i] = mBuffer[i];
402 }
403 return v;
404 }
405
Yifan Hong9fcbb362016-12-20 16:46:41 -0800406 // equality check, assuming that T::operator== is defined.
407 bool operator==(const hidl_vec &other) const {
408 if (mSize != other.size()) {
409 return false;
410 }
411 for (size_t i = 0; i < mSize; ++i) {
412 if (!(mBuffer[i] == other.mBuffer[i])) {
413 return false;
414 }
415 }
416 return true;
417 }
418
419 // inequality check, assuming that T::operator== is defined.
420 inline bool operator!=(const hidl_vec &other) const {
421 return !((*this) == other);
422 }
423
Martijn Coenen72110162016-08-19 14:28:25 +0200424 size_t size() const {
425 return mSize;
426 }
427
428 T &operator[](size_t index) {
429 return mBuffer[index];
430 }
431
432 const T &operator[](size_t index) const {
433 return mBuffer[index];
434 }
435
436 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100437 if (size > UINT32_MAX) {
438 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
439 }
Martijn Coenen72110162016-08-19 14:28:25 +0200440 T *newBuffer = new T[size];
441
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100442 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200443 newBuffer[i] = mBuffer[i];
444 }
445
446 if (mOwnsBuffer) {
447 delete[] mBuffer;
448 }
449 mBuffer = newBuffer;
450
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100451 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200452 mOwnsBuffer = true;
453 }
454
Yifan Hong089ae132016-11-11 11:32:52 -0800455 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
456 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800457
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800458private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800459 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800460 template<bool is_const>
461 class iter : public std::iterator<
462 std::random_access_iterator_tag, /* Category */
463 T,
464 ptrdiff_t, /* Distance */
465 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
466 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800467 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800468 using traits = std::iterator_traits<iter>;
469 using ptr_type = typename traits::pointer;
470 using ref_type = typename traits::reference;
471 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800472 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800473 iter(ptr_type ptr) : mPtr(ptr) { }
474 inline iter &operator++() { mPtr++; return *this; }
475 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
476 inline iter &operator--() { mPtr--; return *this; }
477 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
478 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
479 inline iter operator+(diff_type n) const { return mPtr + n; }
480 inline iter operator-(diff_type n) const { return mPtr - n; }
481 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
482 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
483 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
484 inline ref_type operator*() const { return *mPtr; }
485 inline ptr_type operator->() const { return mPtr; }
486 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
487 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
488 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
489 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
490 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
491 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
492 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800493 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800494 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800495 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800496public:
497 using iterator = iter<false /* is_const */>;
498 using const_iterator = iter<true /* is_const */>;
499
Scott Randolphbb840f72016-11-21 14:39:26 -0800500 iterator begin() { return data(); }
501 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800502 const_iterator begin() const { return data(); }
503 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800504
Martijn Coenen72110162016-08-19 14:28:25 +0200505private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100506 details::hidl_pointer<T> mBuffer;
507 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200508 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700509
510 // copy from an array-like object, assuming my resources are freed.
511 template <typename Array>
512 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800513 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700514 mOwnsBuffer = true;
515 if (mSize > 0) {
516 mBuffer = new T[size];
517 for (size_t i = 0; i < size; ++i) {
518 mBuffer[i] = data[i];
519 }
520 } else {
521 mBuffer = NULL;
522 }
523 }
Martijn Coenen72110162016-08-19 14:28:25 +0200524};
525
Yifan Hong089ae132016-11-11 11:32:52 -0800526template <typename T>
527const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
528
Andreas Huber20dce082016-09-22 19:39:13 -0700529////////////////////////////////////////////////////////////////////////////////
530
531namespace details {
532
533 template<size_t SIZE1, size_t... SIZES>
534 struct product {
535 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
536 };
537
538 template<size_t SIZE1>
539 struct product<SIZE1> {
540 static constexpr size_t value = SIZE1;
541 };
542
543 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800544 struct std_array {
545 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
546 };
547
548 template<typename T, size_t SIZE1>
549 struct std_array<T, SIZE1> {
550 using type = std::array<T, SIZE1>;
551 };
552
553 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700554 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800555
556 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
557
Andreas Huber20dce082016-09-22 19:39:13 -0700558 explicit accessor(T *base)
559 : mBase(base) {
560 }
561
562 accessor<T, SIZES...> operator[](size_t index) {
563 return accessor<T, SIZES...>(
564 &mBase[index * product<SIZES...>::value]);
565 }
566
Yifan Hong44ab6232016-11-22 16:28:24 -0800567 accessor &operator=(const std_array_type &other) {
568 for (size_t i = 0; i < SIZE1; ++i) {
569 (*this)[i] = other[i];
570 }
571 return *this;
572 }
573
Andreas Huber20dce082016-09-22 19:39:13 -0700574 private:
575 T *mBase;
576 };
577
578 template<typename T, size_t SIZE1>
579 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800580
581 using std_array_type = typename std_array<T, SIZE1>::type;
582
Andreas Huber20dce082016-09-22 19:39:13 -0700583 explicit accessor(T *base)
584 : mBase(base) {
585 }
586
587 T &operator[](size_t index) {
588 return mBase[index];
589 }
590
Yifan Hong44ab6232016-11-22 16:28:24 -0800591 accessor &operator=(const std_array_type &other) {
592 for (size_t i = 0; i < SIZE1; ++i) {
593 (*this)[i] = other[i];
594 }
595 return *this;
596 }
597
Andreas Huber20dce082016-09-22 19:39:13 -0700598 private:
599 T *mBase;
600 };
601
602 template<typename T, size_t SIZE1, size_t... SIZES>
603 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800604
605 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
606
Andreas Huber20dce082016-09-22 19:39:13 -0700607 explicit const_accessor(const T *base)
608 : mBase(base) {
609 }
610
Yifan Hongbe7a6882017-01-05 17:30:17 -0800611 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700612 return const_accessor<T, SIZES...>(
613 &mBase[index * product<SIZES...>::value]);
614 }
615
Yifan Hong44ab6232016-11-22 16:28:24 -0800616 operator std_array_type() {
617 std_array_type array;
618 for (size_t i = 0; i < SIZE1; ++i) {
619 array[i] = (*this)[i];
620 }
621 return array;
622 }
623
Andreas Huber20dce082016-09-22 19:39:13 -0700624 private:
625 const T *mBase;
626 };
627
628 template<typename T, size_t SIZE1>
629 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800630
631 using std_array_type = typename std_array<T, SIZE1>::type;
632
Andreas Huber20dce082016-09-22 19:39:13 -0700633 explicit const_accessor(const T *base)
634 : mBase(base) {
635 }
636
637 const T &operator[](size_t index) const {
638 return mBase[index];
639 }
640
Yifan Hong44ab6232016-11-22 16:28:24 -0800641 operator std_array_type() {
642 std_array_type array;
643 for (size_t i = 0; i < SIZE1; ++i) {
644 array[i] = (*this)[i];
645 }
646 return array;
647 }
648
Andreas Huber20dce082016-09-22 19:39:13 -0700649 private:
650 const T *mBase;
651 };
652
653} // namespace details
654
655////////////////////////////////////////////////////////////////////////////////
656
Yifan Hong44ab6232016-11-22 16:28:24 -0800657// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700658template<typename T, size_t SIZE1, size_t... SIZES>
659struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800660
661 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
662
Andreas Huber20dce082016-09-22 19:39:13 -0700663 hidl_array() = default;
664
Yifan Hong44ab6232016-11-22 16:28:24 -0800665 // Copies the data from source, using T::operator=(const T &).
666 hidl_array(const T *source) {
667 for (size_t i = 0; i < elementCount(); ++i) {
668 mBuffer[i] = source[i];
669 }
670 }
671
672 // Copies the data from the given std::array, using T::operator=(const T &).
673 hidl_array(const std_array_type &array) {
674 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
675 modifier = array;
676 }
677
Andreas Huber20dce082016-09-22 19:39:13 -0700678 T *data() { return mBuffer; }
679 const T *data() const { return mBuffer; }
680
681 details::accessor<T, SIZES...> operator[](size_t index) {
682 return details::accessor<T, SIZES...>(
683 &mBuffer[index * details::product<SIZES...>::value]);
684 }
685
686 details::const_accessor<T, SIZES...> operator[](size_t index) const {
687 return details::const_accessor<T, SIZES...>(
688 &mBuffer[index * details::product<SIZES...>::value]);
689 }
690
Yifan Hong9fcbb362016-12-20 16:46:41 -0800691 // equality check, assuming that T::operator== is defined.
692 bool operator==(const hidl_array &other) const {
693 for (size_t i = 0; i < elementCount(); ++i) {
694 if (!(mBuffer[i] == other.mBuffer[i])) {
695 return false;
696 }
697 }
698 return true;
699 }
700
701 inline bool operator!=(const hidl_array &other) const {
702 return !((*this) == other);
703 }
704
Andreas Huber00a985c2016-09-28 14:24:53 -0700705 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
706
707 static constexpr size_tuple_type size() {
708 return std::make_tuple(SIZE1, SIZES...);
709 }
710
Yifan Hong44ab6232016-11-22 16:28:24 -0800711 static constexpr size_t elementCount() {
712 return details::product<SIZE1, SIZES...>::value;
713 }
714
715 operator std_array_type() const {
716 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
717 }
718
Andreas Huber20dce082016-09-22 19:39:13 -0700719private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800720 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700721};
722
Yifan Hong44ab6232016-11-22 16:28:24 -0800723// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700724template<typename T, size_t SIZE1>
725struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800726
727 using std_array_type = typename details::std_array<T, SIZE1>::type;
728
Andreas Huber20dce082016-09-22 19:39:13 -0700729 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800730
731 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800732 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800733 for (size_t i = 0; i < elementCount(); ++i) {
734 mBuffer[i] = source[i];
735 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800736 }
Andreas Huber20dce082016-09-22 19:39:13 -0700737
Yifan Hong44ab6232016-11-22 16:28:24 -0800738 // Copies the data from the given std::array, using T::operator=(const T &).
739 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
740
Andreas Huber20dce082016-09-22 19:39:13 -0700741 T *data() { return mBuffer; }
742 const T *data() const { return mBuffer; }
743
744 T &operator[](size_t index) {
745 return mBuffer[index];
746 }
747
748 const T &operator[](size_t index) const {
749 return mBuffer[index];
750 }
751
Yifan Hong9fcbb362016-12-20 16:46:41 -0800752 // equality check, assuming that T::operator== is defined.
753 bool operator==(const hidl_array &other) const {
754 for (size_t i = 0; i < elementCount(); ++i) {
755 if (!(mBuffer[i] == other.mBuffer[i])) {
756 return false;
757 }
758 }
759 return true;
760 }
761
762 inline bool operator!=(const hidl_array &other) const {
763 return !((*this) == other);
764 }
765
Andreas Huber00a985c2016-09-28 14:24:53 -0700766 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800767 static constexpr size_t elementCount() { return SIZE1; }
768
769 // Copies the data to an std::array, using T::operator=(T).
770 operator std_array_type() const {
771 std_array_type array;
772 for (size_t i = 0; i < SIZE1; ++i) {
773 array[i] = mBuffer[i];
774 }
775 return array;
776 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700777
Andreas Huber20dce082016-09-22 19:39:13 -0700778private:
779 T mBuffer[SIZE1];
780};
781
Martijn Coenen72110162016-08-19 14:28:25 +0200782// ----------------------------------------------------------------------
783// Version functions
784struct hidl_version {
785public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800786 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200787
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700788 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200789 return (mMajor == other.get_major() && mMinor == other.get_minor());
790 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200791
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800792 bool operator<(const hidl_version& other) const {
793 return (mMajor < other.get_major() ||
794 (mMajor == other.get_major() && mMinor < other.get_minor()));
795 }
796
797 bool operator>(const hidl_version& other) const {
798 return other < *this;
799 }
800
801 bool operator<=(const hidl_version& other) const {
802 return !(*this > other);
803 }
804
805 bool operator>=(const hidl_version& other) const {
806 return !(*this < other);
807 }
808
Martijn Coenen097a7672016-09-08 16:56:41 +0200809 constexpr uint16_t get_major() const { return mMajor; }
810 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200811
Martijn Coenen72110162016-08-19 14:28:25 +0200812private:
813 uint16_t mMajor;
814 uint16_t mMinor;
815};
816
817inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
818 return hidl_version(major,minor);
819}
820
Steven Morelandbdf26662016-09-02 11:03:15 -0700821#if defined(__LP64__)
822#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
823#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
824#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
825#else
826#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
827#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
828#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
829#endif
830
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700831// ----------------------------------------------------------------------
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700832// Class that provides Hidl instrumentation utilities.
833struct HidlInstrumentor {
834 // Event that triggers the instrumentation. e.g. enter of an API call on
835 // the server/client side, exit of an API call on the server/client side
836 // etc.
837 enum InstrumentationEvent {
838 SERVER_API_ENTRY = 0,
839 SERVER_API_EXIT,
840 CLIENT_API_ENTRY,
841 CLIENT_API_EXIT,
842 SYNC_CALLBACK_ENTRY,
843 SYNC_CALLBACK_EXIT,
844 ASYNC_CALLBACK_ENTRY,
845 ASYNC_CALLBACK_EXIT,
Steven Morelandcefbd6e2016-11-01 10:14:30 -0700846 PASSTHROUGH_ENTRY,
847 PASSTHROUGH_EXIT,
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700848 };
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700849
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700850 // Signature of the instrumentation callback function.
851 using InstrumentationCallback = std::function<void(
852 const InstrumentationEvent event,
853 const char *package,
854 const char *version,
855 const char *interface,
856 const char *method,
857 std::vector<void *> *args)>;
858
859 explicit HidlInstrumentor(const std::string &prefix);
860 virtual ~HidlInstrumentor();
861
862 protected:
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800863 // Set mEnableInstrumentation based on system property
864 // hal.instrumentation.enable, register/de-register instrumentation
865 // callbacks if mEnableInstrumentation is true/false.
866 void configureInstrumentation(bool log=true);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700867 // Function that lookup and dynamically loads the hidl instrumentation
868 // libraries and registers the instrumentation callback functions.
869 //
870 // The instrumentation libraries should be stored under any of the following
871 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
872 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
873 // follow pattern: ^profilerPrefix(.*).profiler.so$
874 //
875 // Each instrumentation library is expected to implement the instrumentation
876 // function called HIDL_INSTRUMENTATION_FUNCTION.
877 //
878 // A no-op for user build.
879 void registerInstrumentationCallbacks(
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700880 std::vector<InstrumentationCallback> *instrumentationCallbacks);
881
882 // Utility function to determine whether a give file is a instrumentation
883 // library (i.e. the file name follow the expected pattern).
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800884 bool isInstrumentationLib(const dirent *file);
885
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700886 // A list of registered instrumentation callbacks.
887 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
888 // Flag whether to enable instrumentation.
889 bool mEnableInstrumentation;
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800890 // Prefix to lookup the instrumentation libraries.
891 std::string mInstrumentationLibPrefix;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700892};
893
Yifan Hongbe7a6882017-01-05 17:30:17 -0800894///////////////////// toString functions
895
896namespace details {
897
898// toString alias for numeric types
899template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
900inline std::string toString(T t) {
901 return std::to_string(t);
902}
903
904template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
905inline std::string toHexString(T t, bool prefix = true) {
906 std::ostringstream os;
907 if (prefix) { os << std::showbase; }
908 os << std::hex << t;
909 return os.str();
910}
911
912template<>
913inline std::string toHexString(uint8_t t, bool prefix) {
914 return toHexString(static_cast<int32_t>(t), prefix);
915}
916
917template<>
918inline std::string toHexString(int8_t t, bool prefix) {
919 return toHexString(static_cast<int32_t>(t), prefix);
920}
921
922inline std::string toString(const void *t, bool prefix = true) {
923 return toHexString(reinterpret_cast<uintptr_t>(t), prefix);
924}
925
926// debug string dump. There will be quotes around the string!
927inline std::string toString(const hidl_string &hs) {
928 return std::string{"\""} + hs.c_str() + "\"";
929}
930
931// debug string dump
932inline std::string toString(const hidl_handle &hs) {
933 return toString(hs.getNativeHandle());
934}
935
936inline std::string toString(const hidl_memory &mem) {
937 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
938 + toString(mem.size())
939 + ", .handle = " + toString(mem.handle()) + "}";
940}
941
942inline std::string toString(const sp<hidl_death_recipient> &dr) {
943 return std::string{"death_recipient@"} + toString(dr.get());
944}
945
946template<typename Array>
947std::string arrayToString(const Array &a, size_t size);
948
949// debug string dump, assuming that toString(T) is defined.
950template<typename T>
951std::string toString(const hidl_vec<T> &a) {
952 std::string os;
953 os += "[" + toString(a.size()) + "]";
954 os += arrayToString(a, a.size());
955 return os;
956}
957
958template<size_t SIZE1>
959std::string arraySizeToString() {
960 return std::string{"["} + toString(SIZE1) + "]";
961}
962
963template<typename T, size_t SIZE1>
964std::string toString(const_accessor<T, SIZE1> a) {
965 return arrayToString(a, SIZE1);
966}
967
968template<typename T, size_t SIZE1>
969std::string toString(const hidl_array<T, SIZE1> &a) {
970 return arraySizeToString<SIZE1>()
971 + toString(const_accessor<T, SIZE1>(a.data()));
972}
973
974template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
975std::string arraySizeToString() {
976 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
977}
978
979
980template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
981std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
982 return arrayToString(a, SIZE1);
983}
984
985template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
986std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
987 return arraySizeToString<SIZE1, SIZE2, SIZES...>()
988 + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
989}
990
991template<typename Array>
992std::string arrayToString(const Array &a, size_t size) {
993 std::string os;
994 os += "{";
995 for (size_t i = 0; i < size; ++i) {
996 if (i > 0) {
997 os += ", ";
998 }
999 os += toString(a[i]);
1000 }
1001 os += "}";
1002 return os;
1003}
1004
1005} // namespace details
1006
1007
Martijn Coenen72110162016-08-19 14:28:25 +02001008} // namespace hardware
1009} // namespace android
1010
Martijn Coenenc28f1152016-08-22 14:06:56 +02001011
Martijn Coenen72110162016-08-19 14:28:25 +02001012#endif // ANDROID_HIDL_SUPPORT_H