blob: a16701beaa77db8d336cf589437c832b5771e238 [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>
Scott Randolphbb840f72016-11-21 14:39:26 -080022#include <iterator>
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010023#include <cutils/native_handle.h>
Steven Moreland337c3ae2016-11-22 13:37:32 -080024#include <hidl/HidlInternal.h>
Yifan Honga3c31842016-10-21 10:33:14 -070025#include <hidl/Status.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080026#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080027#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080028#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070029#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080030#include <type_traits>
Iliyan Malchev692070a2016-09-12 16:30:44 -070031#include <utils/Errors.h>
32#include <utils/RefBase.h>
33#include <utils/StrongPointer.h>
Yifan Hong44c0e572017-01-20 15:41:52 -080034#include <vintf/Transport.h>
Yifan Hong7f97f442016-11-14 18:31:05 -080035#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020036
37namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010038
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010039// this file is included by all hidl interface, so we must forward declare the
40// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010041namespace hidl {
42namespace memory {
43namespace V1_0 {
44 struct IMemory;
45}; // namespace V1_0
46}; // namespace manager
47}; // namespace hidl
48
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010049namespace hidl {
50namespace base {
51namespace V1_0 {
52 struct IBase;
53}; // namespace V1_0
54}; // namespace base
55}; // namespace hidl
56
Martijn Coenen72110162016-08-19 14:28:25 +020057namespace hardware {
58
Yifan Hong44c0e572017-01-20 15:41:52 -080059// Get transport method from vendor interface manifest.
Yifan Hong37b36202017-02-28 16:04:22 -080060// interfaceName has the format "android.hardware.foo@1.0::IFoo"
61// instanceName is "default", "ashmem", etc.
Yifan Hong20273f92017-01-30 14:13:19 -080062// If it starts with "android.hidl.", a static map is looked up instead.
Yifan Hong37b36202017-02-28 16:04:22 -080063vintf::Transport getTransport(const std::string &interfaceName,
64 const std::string &instanceName);
Yifan Hong44c0e572017-01-20 15:41:52 -080065
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 &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800131 // copy from a C-style string. nullptr will create an empty 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
Martijn Coenen72110162016-08-19 14:28:25 +0200159 void clear();
160
161 // Reference an external char array. Ownership is _not_ transferred.
162 // Caller is responsible for ensuring that underlying memory is valid
163 // for the lifetime of this hidl_string.
164 void setToExternal(const char *data, size_t size);
165
Andreas Huberebfeb362016-08-25 13:39:05 -0700166 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
167 static const size_t kOffsetOfBuffer;
168
Martijn Coenen72110162016-08-19 14:28:25 +0200169private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100170 details::hidl_pointer<const char> mBuffer;
171 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700172 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200173
Yifan Hong602b85a2016-10-24 13:40:01 -0700174 // copy from data with size. Assume that my memory is freed
175 // (through clear(), for example)
176 void copyFrom(const char *data, size_t size);
177 // move from another hidl_string
178 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200179};
180
Steven Moreland551396a2017-02-13 18:33:20 -0800181#define HIDL_STRING_OPERATOR(OP) \
182 inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \
183 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; \
184 } \
185 inline bool operator OP(const hidl_string &hs, const char *s) { \
186 return strcmp(hs.c_str(), s) OP 0; \
187 } \
188 inline bool operator OP(const char *s, const hidl_string &hs) { \
189 return strcmp(hs.c_str(), s) OP 0; \
190 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800191
Steven Moreland551396a2017-02-13 18:33:20 -0800192HIDL_STRING_OPERATOR(==)
193HIDL_STRING_OPERATOR(!=)
194HIDL_STRING_OPERATOR(<)
195HIDL_STRING_OPERATOR(<=)
196HIDL_STRING_OPERATOR(>)
197HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800198
Steven Moreland551396a2017-02-13 18:33:20 -0800199#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700200
Martijn Coenen30791002016-12-01 15:40:46 +0100201// hidl_memory is a structure that can be used to transfer
202// pieces of shared memory between processes. The assumption
203// of this object is that the memory remains accessible as
204// long as the file descriptors in the enclosed mHandle
205// - as well as all of its cross-process dups() - remain opened.
206struct hidl_memory {
207
Martijn Coenen04b91c02017-01-19 14:14:21 +0100208 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100209 }
210
211 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100212 * Creates a hidl_memory object, but doesn't take ownership of
213 * the passed in native_handle_t; callers are responsible for
214 * making sure the handle remains valid while this object is
215 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100216 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100217 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
218 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100219 mSize(size),
220 mName(name)
221 {}
222
223 // copy constructor
224 hidl_memory(const hidl_memory& other) {
225 *this = other;
226 }
227
228 // copy assignment
229 hidl_memory &operator=(const hidl_memory &other) {
230 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100231 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100232 mSize = other.mSize;
233 mName = other.mName;
234 }
235
236 return *this;
237 }
238
Hridya Valsaraju01268892017-02-27 08:48:38 -0800239 // move constructor
240 hidl_memory(hidl_memory&& other) {
241 *this = std::move(other);
242 }
243
244 // move assignment
245 hidl_memory &operator=(hidl_memory &&other) {
246 if (this != &other) {
247 mHandle = std::move(other.mHandle);
248 mSize = other.mSize;
249 mName = std::move(other.mName);
250 other.mSize = 0;
251 }
252
253 return *this;
254 }
255
Martijn Coenen30791002016-12-01 15:40:46 +0100256
257 ~hidl_memory() {
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:
Martijn Coenen30791002016-12-01 15:40:46 +0100278 hidl_handle mHandle;
279 size_t mSize;
280 hidl_string mName;
281};
282
Andreas Huber20dce082016-09-22 19:39:13 -0700283////////////////////////////////////////////////////////////////////////////////
284
Martijn Coenen72110162016-08-19 14:28:25 +0200285template<typename T>
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100286struct hidl_vec : private details::hidl_log_base {
Martijn Coenen72110162016-08-19 14:28:25 +0200287 hidl_vec()
288 : mBuffer(NULL),
289 mSize(0),
290 mOwnsBuffer(true) {
291 }
292
Yifan Hong602b85a2016-10-24 13:40:01 -0700293 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200294 *this = other;
295 }
296
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100297 hidl_vec(hidl_vec<T> &&other)
Steven Moreland9fbfe472016-11-14 16:49:17 -0800298 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100299 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700300 }
301
Steven Morelandb69926a2016-11-15 10:02:57 -0800302 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800303 : mOwnsBuffer(true) {
304 if (list.size() > UINT32_MAX) {
305 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
306 }
307 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800308 mBuffer = new T[mSize];
309
Steven Morelandb69926a2016-11-15 10:02:57 -0800310 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800311 for (auto it = list.begin(); it != list.end(); ++it) {
312 mBuffer[idx++] = *it;
313 }
314 }
315
Yifan Hong602b85a2016-10-24 13:40:01 -0700316 hidl_vec(const std::vector<T> &other) : hidl_vec() {
317 *this = other;
318 }
319
Martijn Coenen72110162016-08-19 14:28:25 +0200320 ~hidl_vec() {
321 if (mOwnsBuffer) {
322 delete[] mBuffer;
323 }
324 mBuffer = NULL;
325 }
326
Alexey Polyudove2299012016-10-19 09:52:00 -0700327 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200328 // caller's responsibility to ensure that the underlying memory stays
329 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700330 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200331 if (mOwnsBuffer) {
332 delete [] mBuffer;
333 }
334 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100335 if (size > UINT32_MAX) {
336 logAlwaysFatal("external vector size exceeds 2^32 elements.");
337 }
338 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700339 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200340 }
341
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700342 T *data() {
343 return mBuffer;
344 }
345
346 const T *data() const {
347 return mBuffer;
348 }
349
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700350 T *releaseData() {
351 if (!mOwnsBuffer && mSize > 0) {
352 resize(mSize);
353 }
354 mOwnsBuffer = false;
355 return mBuffer;
356 }
357
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700358 hidl_vec &operator=(hidl_vec &&other) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100359 if (mOwnsBuffer) {
360 delete[] mBuffer;
361 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700362 mBuffer = other.mBuffer;
363 mSize = other.mSize;
364 mOwnsBuffer = other.mOwnsBuffer;
365 other.mOwnsBuffer = false;
366 return *this;
367 }
368
Martijn Coenen72110162016-08-19 14:28:25 +0200369 hidl_vec &operator=(const hidl_vec &other) {
370 if (this != &other) {
371 if (mOwnsBuffer) {
372 delete[] mBuffer;
373 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700374 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200375 }
376
377 return *this;
378 }
379
Yifan Hong602b85a2016-10-24 13:40:01 -0700380 // copy from an std::vector.
381 hidl_vec &operator=(const std::vector<T> &other) {
382 if (mOwnsBuffer) {
383 delete[] mBuffer;
384 }
385 copyFrom(other, other.size());
386 return *this;
387 }
388
389 // cast to an std::vector.
390 operator std::vector<T>() const {
391 std::vector<T> v(mSize);
392 for (size_t i = 0; i < mSize; ++i) {
393 v[i] = mBuffer[i];
394 }
395 return v;
396 }
397
Yifan Hong9fcbb362016-12-20 16:46:41 -0800398 // equality check, assuming that T::operator== is defined.
399 bool operator==(const hidl_vec &other) const {
400 if (mSize != other.size()) {
401 return false;
402 }
403 for (size_t i = 0; i < mSize; ++i) {
404 if (!(mBuffer[i] == other.mBuffer[i])) {
405 return false;
406 }
407 }
408 return true;
409 }
410
411 // inequality check, assuming that T::operator== is defined.
412 inline bool operator!=(const hidl_vec &other) const {
413 return !((*this) == other);
414 }
415
Martijn Coenen72110162016-08-19 14:28:25 +0200416 size_t size() const {
417 return mSize;
418 }
419
420 T &operator[](size_t index) {
421 return mBuffer[index];
422 }
423
424 const T &operator[](size_t index) const {
425 return mBuffer[index];
426 }
427
428 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100429 if (size > UINT32_MAX) {
430 logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
431 }
Martijn Coenen72110162016-08-19 14:28:25 +0200432 T *newBuffer = new T[size];
433
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100434 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200435 newBuffer[i] = mBuffer[i];
436 }
437
438 if (mOwnsBuffer) {
439 delete[] mBuffer;
440 }
441 mBuffer = newBuffer;
442
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100443 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200444 mOwnsBuffer = true;
445 }
446
Yifan Hong089ae132016-11-11 11:32:52 -0800447 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
448 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800449
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800450private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800451 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800452 template<bool is_const>
453 class iter : public std::iterator<
454 std::random_access_iterator_tag, /* Category */
455 T,
456 ptrdiff_t, /* Distance */
457 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
458 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800459 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800460 using traits = std::iterator_traits<iter>;
461 using ptr_type = typename traits::pointer;
462 using ref_type = typename traits::reference;
463 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800464 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800465 iter(ptr_type ptr) : mPtr(ptr) { }
466 inline iter &operator++() { mPtr++; return *this; }
467 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
468 inline iter &operator--() { mPtr--; return *this; }
469 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
470 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
471 inline iter operator+(diff_type n) const { return mPtr + n; }
472 inline iter operator-(diff_type n) const { return mPtr - n; }
473 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
474 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
475 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
476 inline ref_type operator*() const { return *mPtr; }
477 inline ptr_type operator->() const { return mPtr; }
478 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
479 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
480 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
481 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
482 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
483 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
484 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800485 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800486 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800487 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800488public:
489 using iterator = iter<false /* is_const */>;
490 using const_iterator = iter<true /* is_const */>;
491
Scott Randolphbb840f72016-11-21 14:39:26 -0800492 iterator begin() { return data(); }
493 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800494 const_iterator begin() const { return data(); }
495 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800496
Martijn Coenen72110162016-08-19 14:28:25 +0200497private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100498 details::hidl_pointer<T> mBuffer;
499 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200500 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700501
502 // copy from an array-like object, assuming my resources are freed.
503 template <typename Array>
504 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800505 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700506 mOwnsBuffer = true;
507 if (mSize > 0) {
508 mBuffer = new T[size];
509 for (size_t i = 0; i < size; ++i) {
510 mBuffer[i] = data[i];
511 }
512 } else {
513 mBuffer = NULL;
514 }
515 }
Martijn Coenen72110162016-08-19 14:28:25 +0200516};
517
Yifan Hong089ae132016-11-11 11:32:52 -0800518template <typename T>
519const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
520
Andreas Huber20dce082016-09-22 19:39:13 -0700521////////////////////////////////////////////////////////////////////////////////
522
523namespace details {
524
525 template<size_t SIZE1, size_t... SIZES>
526 struct product {
527 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
528 };
529
530 template<size_t SIZE1>
531 struct product<SIZE1> {
532 static constexpr size_t value = SIZE1;
533 };
534
535 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800536 struct std_array {
537 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
538 };
539
540 template<typename T, size_t SIZE1>
541 struct std_array<T, SIZE1> {
542 using type = std::array<T, SIZE1>;
543 };
544
545 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700546 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800547
548 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
549
Andreas Huber20dce082016-09-22 19:39:13 -0700550 explicit accessor(T *base)
551 : mBase(base) {
552 }
553
554 accessor<T, SIZES...> operator[](size_t index) {
555 return accessor<T, SIZES...>(
556 &mBase[index * product<SIZES...>::value]);
557 }
558
Yifan Hong44ab6232016-11-22 16:28:24 -0800559 accessor &operator=(const std_array_type &other) {
560 for (size_t i = 0; i < SIZE1; ++i) {
561 (*this)[i] = other[i];
562 }
563 return *this;
564 }
565
Andreas Huber20dce082016-09-22 19:39:13 -0700566 private:
567 T *mBase;
568 };
569
570 template<typename T, size_t SIZE1>
571 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800572
573 using std_array_type = typename std_array<T, SIZE1>::type;
574
Andreas Huber20dce082016-09-22 19:39:13 -0700575 explicit accessor(T *base)
576 : mBase(base) {
577 }
578
579 T &operator[](size_t index) {
580 return mBase[index];
581 }
582
Yifan Hong44ab6232016-11-22 16:28:24 -0800583 accessor &operator=(const std_array_type &other) {
584 for (size_t i = 0; i < SIZE1; ++i) {
585 (*this)[i] = other[i];
586 }
587 return *this;
588 }
589
Andreas Huber20dce082016-09-22 19:39:13 -0700590 private:
591 T *mBase;
592 };
593
594 template<typename T, size_t SIZE1, size_t... SIZES>
595 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800596
597 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
598
Andreas Huber20dce082016-09-22 19:39:13 -0700599 explicit const_accessor(const T *base)
600 : mBase(base) {
601 }
602
Yifan Hongbe7a6882017-01-05 17:30:17 -0800603 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700604 return const_accessor<T, SIZES...>(
605 &mBase[index * product<SIZES...>::value]);
606 }
607
Yifan Hong44ab6232016-11-22 16:28:24 -0800608 operator std_array_type() {
609 std_array_type array;
610 for (size_t i = 0; i < SIZE1; ++i) {
611 array[i] = (*this)[i];
612 }
613 return array;
614 }
615
Andreas Huber20dce082016-09-22 19:39:13 -0700616 private:
617 const T *mBase;
618 };
619
620 template<typename T, size_t SIZE1>
621 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800622
623 using std_array_type = typename std_array<T, SIZE1>::type;
624
Andreas Huber20dce082016-09-22 19:39:13 -0700625 explicit const_accessor(const T *base)
626 : mBase(base) {
627 }
628
629 const T &operator[](size_t index) const {
630 return mBase[index];
631 }
632
Yifan Hong44ab6232016-11-22 16:28:24 -0800633 operator std_array_type() {
634 std_array_type array;
635 for (size_t i = 0; i < SIZE1; ++i) {
636 array[i] = (*this)[i];
637 }
638 return array;
639 }
640
Andreas Huber20dce082016-09-22 19:39:13 -0700641 private:
642 const T *mBase;
643 };
644
645} // namespace details
646
647////////////////////////////////////////////////////////////////////////////////
648
Yifan Hong44ab6232016-11-22 16:28:24 -0800649// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700650template<typename T, size_t SIZE1, size_t... SIZES>
651struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800652
653 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
654
Andreas Huber20dce082016-09-22 19:39:13 -0700655 hidl_array() = default;
656
Yifan Hong44ab6232016-11-22 16:28:24 -0800657 // Copies the data from source, using T::operator=(const T &).
658 hidl_array(const T *source) {
659 for (size_t i = 0; i < elementCount(); ++i) {
660 mBuffer[i] = source[i];
661 }
662 }
663
664 // Copies the data from the given std::array, using T::operator=(const T &).
665 hidl_array(const std_array_type &array) {
666 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
667 modifier = array;
668 }
669
Andreas Huber20dce082016-09-22 19:39:13 -0700670 T *data() { return mBuffer; }
671 const T *data() const { return mBuffer; }
672
673 details::accessor<T, SIZES...> operator[](size_t index) {
674 return details::accessor<T, SIZES...>(
675 &mBuffer[index * details::product<SIZES...>::value]);
676 }
677
678 details::const_accessor<T, SIZES...> operator[](size_t index) const {
679 return details::const_accessor<T, SIZES...>(
680 &mBuffer[index * details::product<SIZES...>::value]);
681 }
682
Yifan Hong9fcbb362016-12-20 16:46:41 -0800683 // equality check, assuming that T::operator== is defined.
684 bool operator==(const hidl_array &other) const {
685 for (size_t i = 0; i < elementCount(); ++i) {
686 if (!(mBuffer[i] == other.mBuffer[i])) {
687 return false;
688 }
689 }
690 return true;
691 }
692
693 inline bool operator!=(const hidl_array &other) const {
694 return !((*this) == other);
695 }
696
Andreas Huber00a985c2016-09-28 14:24:53 -0700697 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
698
699 static constexpr size_tuple_type size() {
700 return std::make_tuple(SIZE1, SIZES...);
701 }
702
Yifan Hong44ab6232016-11-22 16:28:24 -0800703 static constexpr size_t elementCount() {
704 return details::product<SIZE1, SIZES...>::value;
705 }
706
707 operator std_array_type() const {
708 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
709 }
710
Andreas Huber20dce082016-09-22 19:39:13 -0700711private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800712 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700713};
714
Yifan Hong44ab6232016-11-22 16:28:24 -0800715// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700716template<typename T, size_t SIZE1>
717struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800718
719 using std_array_type = typename details::std_array<T, SIZE1>::type;
720
Andreas Huber20dce082016-09-22 19:39:13 -0700721 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800722
723 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800724 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800725 for (size_t i = 0; i < elementCount(); ++i) {
726 mBuffer[i] = source[i];
727 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800728 }
Andreas Huber20dce082016-09-22 19:39:13 -0700729
Yifan Hong44ab6232016-11-22 16:28:24 -0800730 // Copies the data from the given std::array, using T::operator=(const T &).
731 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
732
Andreas Huber20dce082016-09-22 19:39:13 -0700733 T *data() { return mBuffer; }
734 const T *data() const { return mBuffer; }
735
736 T &operator[](size_t index) {
737 return mBuffer[index];
738 }
739
740 const T &operator[](size_t index) const {
741 return mBuffer[index];
742 }
743
Yifan Hong9fcbb362016-12-20 16:46:41 -0800744 // equality check, assuming that T::operator== is defined.
745 bool operator==(const hidl_array &other) const {
746 for (size_t i = 0; i < elementCount(); ++i) {
747 if (!(mBuffer[i] == other.mBuffer[i])) {
748 return false;
749 }
750 }
751 return true;
752 }
753
754 inline bool operator!=(const hidl_array &other) const {
755 return !((*this) == other);
756 }
757
Andreas Huber00a985c2016-09-28 14:24:53 -0700758 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800759 static constexpr size_t elementCount() { return SIZE1; }
760
761 // Copies the data to an std::array, using T::operator=(T).
762 operator std_array_type() const {
763 std_array_type array;
764 for (size_t i = 0; i < SIZE1; ++i) {
765 array[i] = mBuffer[i];
766 }
767 return array;
768 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700769
Andreas Huber20dce082016-09-22 19:39:13 -0700770private:
771 T mBuffer[SIZE1];
772};
773
Martijn Coenen72110162016-08-19 14:28:25 +0200774// ----------------------------------------------------------------------
775// Version functions
776struct hidl_version {
777public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800778 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200779
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700780 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200781 return (mMajor == other.get_major() && mMinor == other.get_minor());
782 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200783
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800784 bool operator<(const hidl_version& other) const {
785 return (mMajor < other.get_major() ||
786 (mMajor == other.get_major() && mMinor < other.get_minor()));
787 }
788
789 bool operator>(const hidl_version& other) const {
790 return other < *this;
791 }
792
793 bool operator<=(const hidl_version& other) const {
794 return !(*this > other);
795 }
796
797 bool operator>=(const hidl_version& other) const {
798 return !(*this < other);
799 }
800
Martijn Coenen097a7672016-09-08 16:56:41 +0200801 constexpr uint16_t get_major() const { return mMajor; }
802 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200803
Martijn Coenen72110162016-08-19 14:28:25 +0200804private:
805 uint16_t mMajor;
806 uint16_t mMinor;
807};
808
809inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
810 return hidl_version(major,minor);
811}
812
Yifan Hongbe7a6882017-01-05 17:30:17 -0800813///////////////////// toString functions
814
815namespace details {
816
817// toString alias for numeric types
818template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
819inline std::string toString(T t) {
820 return std::to_string(t);
821}
822
823template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
824inline std::string toHexString(T t, bool prefix = true) {
825 std::ostringstream os;
826 if (prefix) { os << std::showbase; }
827 os << std::hex << t;
828 return os.str();
829}
830
831template<>
832inline std::string toHexString(uint8_t t, bool prefix) {
833 return toHexString(static_cast<int32_t>(t), prefix);
834}
835
836template<>
837inline std::string toHexString(int8_t t, bool prefix) {
838 return toHexString(static_cast<int32_t>(t), prefix);
839}
840
841inline std::string toString(const void *t, bool prefix = true) {
842 return toHexString(reinterpret_cast<uintptr_t>(t), prefix);
843}
844
845// debug string dump. There will be quotes around the string!
846inline std::string toString(const hidl_string &hs) {
847 return std::string{"\""} + hs.c_str() + "\"";
848}
849
850// debug string dump
851inline std::string toString(const hidl_handle &hs) {
852 return toString(hs.getNativeHandle());
853}
854
855inline std::string toString(const hidl_memory &mem) {
856 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
857 + toString(mem.size())
858 + ", .handle = " + toString(mem.handle()) + "}";
859}
860
861inline std::string toString(const sp<hidl_death_recipient> &dr) {
862 return std::string{"death_recipient@"} + toString(dr.get());
863}
864
865template<typename Array>
866std::string arrayToString(const Array &a, size_t size);
867
868// debug string dump, assuming that toString(T) is defined.
869template<typename T>
870std::string toString(const hidl_vec<T> &a) {
871 std::string os;
872 os += "[" + toString(a.size()) + "]";
873 os += arrayToString(a, a.size());
874 return os;
875}
876
877template<size_t SIZE1>
878std::string arraySizeToString() {
879 return std::string{"["} + toString(SIZE1) + "]";
880}
881
882template<typename T, size_t SIZE1>
883std::string toString(const_accessor<T, SIZE1> a) {
884 return arrayToString(a, SIZE1);
885}
886
887template<typename T, size_t SIZE1>
888std::string toString(const hidl_array<T, SIZE1> &a) {
889 return arraySizeToString<SIZE1>()
890 + toString(const_accessor<T, SIZE1>(a.data()));
891}
892
893template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
894std::string arraySizeToString() {
895 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
896}
897
898
899template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
900std::string toString(const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
901 return arrayToString(a, SIZE1);
902}
903
904template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
905std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
906 return arraySizeToString<SIZE1, SIZE2, SIZES...>()
907 + toString(const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
908}
909
910template<typename Array>
911std::string arrayToString(const Array &a, size_t size) {
912 std::string os;
913 os += "{";
914 for (size_t i = 0; i < size; ++i) {
915 if (i > 0) {
916 os += ", ";
917 }
918 os += toString(a[i]);
919 }
920 os += "}";
921 return os;
922}
923
924} // namespace details
925
926
Martijn Coenen72110162016-08-19 14:28:25 +0200927} // namespace hardware
928} // namespace android
929
Martijn Coenenc28f1152016-08-22 14:06:56 +0200930
Martijn Coenen72110162016-08-19 14:28:25 +0200931#endif // ANDROID_HIDL_SUPPORT_H