blob: ab16709e30f4ed4e1611c48978911280e3f86126 [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 Hong7f97f442016-11-14 18:31:05 -080034#include <vector>
Martijn Coenen72110162016-08-19 14:28:25 +020035
36namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010037
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010038// this file is included by all hidl interface, so we must forward declare the
39// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010040namespace hidl {
41namespace memory {
42namespace V1_0 {
43 struct IMemory;
44}; // namespace V1_0
45}; // namespace manager
46}; // namespace hidl
47
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010048namespace hidl {
49namespace base {
50namespace V1_0 {
51 struct IBase;
52}; // namespace V1_0
53}; // namespace base
54}; // namespace hidl
55
Martijn Coenen72110162016-08-19 14:28:25 +020056namespace hardware {
57
Yifan Hong24332ef2017-03-07 16:22:19 -080058namespace details {
59// Return true on userdebug / eng builds and false on user builds.
60bool debuggable();
61} // namespace details
62
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010063// hidl_death_recipient is a callback interfaced that can be used with
64// linkToDeath() / unlinkToDeath()
65struct hidl_death_recipient : public virtual RefBase {
66 virtual void serviceDied(uint64_t cookie,
67 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
68};
69
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010070// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
71// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010072// The ownership semantics for this are:
73// 1) The conversion constructor and assignment operator taking a const native_handle_t*
74// do not take ownership of the handle; this is because these operations are usually
75// just done for IPC, and cloning by default is a waste of resources. If you want
76// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
77// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
78// that is because it's not intuitive that this class encapsulates a native_handle_t
79// which needs cloning to be valid; in particular, this allows constructs like this:
80// hidl_handle copy;
81// foo->someHidlCall([&](auto incoming_handle) {
82// copy = incoming_handle;
83// });
84// // copy and its enclosed file descriptors will remain valid here.
85// 3) The move constructor does what you would expect; it only owns the handle if the
86// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010087struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010088 hidl_handle();
89 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010090
Martijn Coenen04b91c02017-01-19 14:14:21 +010091 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010092
Martijn Coenen04b91c02017-01-19 14:14:21 +010093 // copy constructor.
94 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010095
96 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -070097 hidl_handle(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010098
Steven Moreland6ffdc2a2017-01-23 12:34:33 -080099 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100100 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100101
Martijn Coenen04b91c02017-01-19 14:14:21 +0100102 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100103
Martijn Coenene8c36e12017-05-30 16:25:07 -0700104 hidl_handle &operator=(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
Martijn Coenen04b91c02017-01-19 14:14:21 +0100106 void setTo(native_handle_t* handle, bool shouldOwn = false);
107
108 const native_handle_t* operator->() const;
109
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100110 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100111 operator const native_handle_t *() const;
112
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100113 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100114 const native_handle_t *getNativeHandle() const;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100115private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100116 void freeHandle();
117
Andreas Huber6688d602017-03-30 10:58:29 -0700118 details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8)));
119 bool mOwnsHandle __attribute ((aligned(8)));
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100120};
121
Martijn Coenen72110162016-08-19 14:28:25 +0200122struct hidl_string {
123 hidl_string();
124 ~hidl_string();
125
Yifan Hong602b85a2016-10-24 13:40:01 -0700126 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200127 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800128 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700129 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800130 // copy the first length characters from a C-style string.
131 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700132 // copy from an std::string.
133 hidl_string(const std::string &);
134
135 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700136 hidl_string(hidl_string &&) noexcept;
Martijn Coenen72110162016-08-19 14:28:25 +0200137
138 const char *c_str() const;
139 size_t size() const;
140 bool empty() const;
141
Yifan Hong602b85a2016-10-24 13:40:01 -0700142 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700143 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700144 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200145 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700146 // copy from an std::string.
147 hidl_string &operator=(const std::string &);
148 // move assignment operator.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700149 hidl_string &operator=(hidl_string &&other) noexcept;
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // cast to std::string.
151 operator std::string() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700152
Martijn Coenen72110162016-08-19 14:28:25 +0200153 void clear();
154
155 // Reference an external char array. Ownership is _not_ transferred.
156 // Caller is responsible for ensuring that underlying memory is valid
157 // for the lifetime of this hidl_string.
158 void setToExternal(const char *data, size_t size);
159
Andreas Huberebfeb362016-08-25 13:39:05 -0700160 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
161 static const size_t kOffsetOfBuffer;
162
Martijn Coenen72110162016-08-19 14:28:25 +0200163private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100164 details::hidl_pointer<const char> mBuffer;
165 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700166 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200167
Yifan Hong602b85a2016-10-24 13:40:01 -0700168 // copy from data with size. Assume that my memory is freed
169 // (through clear(), for example)
170 void copyFrom(const char *data, size_t size);
171 // move from another hidl_string
172 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200173};
174
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700175// Use NOLINT to suppress missing parentheses warnings around OP.
Steven Moreland551396a2017-02-13 18:33:20 -0800176#define HIDL_STRING_OPERATOR(OP) \
177 inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700178 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800179 } \
180 inline bool operator OP(const hidl_string &hs, const char *s) { \
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700181 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800182 } \
183 inline bool operator OP(const char *s, const hidl_string &hs) { \
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700184 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800185 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800186
Steven Moreland551396a2017-02-13 18:33:20 -0800187HIDL_STRING_OPERATOR(==)
188HIDL_STRING_OPERATOR(!=)
189HIDL_STRING_OPERATOR(<)
190HIDL_STRING_OPERATOR(<=)
191HIDL_STRING_OPERATOR(>)
192HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800193
Steven Moreland551396a2017-02-13 18:33:20 -0800194#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700195
Scott Randolph0c84ab42017-04-03 14:07:14 -0700196// Send our content to the output stream
197std::ostream& operator<<(std::ostream& os, const hidl_string& str);
198
199
Martijn Coenen30791002016-12-01 15:40:46 +0100200// hidl_memory is a structure that can be used to transfer
201// pieces of shared memory between processes. The assumption
202// of this object is that the memory remains accessible as
203// long as the file descriptors in the enclosed mHandle
204// - as well as all of its cross-process dups() - remain opened.
205struct hidl_memory {
206
Martijn Coenen04b91c02017-01-19 14:14:21 +0100207 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100208 }
209
210 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100211 * Creates a hidl_memory object, but doesn't take ownership of
212 * the passed in native_handle_t; callers are responsible for
213 * making sure the handle remains valid while this object is
214 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100215 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100216 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
217 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100218 mSize(size),
219 mName(name)
220 {}
221
222 // copy constructor
223 hidl_memory(const hidl_memory& other) {
224 *this = other;
225 }
226
227 // copy assignment
228 hidl_memory &operator=(const hidl_memory &other) {
229 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100230 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100231 mSize = other.mSize;
232 mName = other.mName;
233 }
234
235 return *this;
236 }
237
Hridya Valsaraju01268892017-02-27 08:48:38 -0800238 // move constructor
Martijn Coenene8c36e12017-05-30 16:25:07 -0700239 hidl_memory(hidl_memory&& other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800240 *this = std::move(other);
241 }
242
243 // move assignment
Martijn Coenene8c36e12017-05-30 16:25:07 -0700244 hidl_memory &operator=(hidl_memory &&other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800245 if (this != &other) {
246 mHandle = std::move(other.mHandle);
247 mSize = other.mSize;
248 mName = std::move(other.mName);
249 other.mSize = 0;
250 }
251
252 return *this;
253 }
254
Martijn Coenen30791002016-12-01 15:40:46 +0100255
256 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100257 }
258
259 const native_handle_t* handle() const {
260 return mHandle;
261 }
262
263 const hidl_string &name() const {
264 return mName;
265 }
266
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800267 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100268 return mSize;
269 }
270
271 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
272 static const size_t kOffsetOfHandle;
273 // offsetof(hidl_memory, mName) exposed since mHandle is private.
274 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800275
Martijn Coenen30791002016-12-01 15:40:46 +0100276private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800277 hidl_handle mHandle __attribute__ ((aligned(8)));
278 uint64_t mSize __attribute__ ((aligned(8)));
279 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100280};
281
Andreas Huber20dce082016-09-22 19:39:13 -0700282////////////////////////////////////////////////////////////////////////////////
283
Martijn Coenen72110162016-08-19 14:28:25 +0200284template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800285struct hidl_vec {
Martijn Coenen72110162016-08-19 14:28:25 +0200286 hidl_vec()
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700287 : mBuffer(nullptr),
Martijn Coenen72110162016-08-19 14:28:25 +0200288 mSize(0),
289 mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800290 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Martijn Coenen72110162016-08-19 14:28:25 +0200291 }
292
Steven Morelandaa661fc2017-10-06 09:40:18 -0700293 // Note, does not initialize primitive types.
Steven Moreland96e9de02017-09-29 14:11:20 -0700294 hidl_vec(size_t size) : hidl_vec() { resize(size); }
295
Yifan Hong602b85a2016-10-24 13:40:01 -0700296 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200297 *this = other;
298 }
299
Martijn Coenene8c36e12017-05-30 16:25:07 -0700300 hidl_vec(hidl_vec<T> &&other) noexcept
Steven Moreland9fbfe472016-11-14 16:49:17 -0800301 : mOwnsBuffer(false) {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100302 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700303 }
304
Steven Morelandb69926a2016-11-15 10:02:57 -0800305 hidl_vec(const std::initializer_list<T> list)
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800306 : mOwnsBuffer(true) {
307 if (list.size() > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800308 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Yifan Hongca1d1bf2016-12-19 14:26:19 -0800309 }
310 mSize = static_cast<uint32_t>(list.size());
Steven Moreland9fbfe472016-11-14 16:49:17 -0800311 mBuffer = new T[mSize];
312
Steven Morelandb69926a2016-11-15 10:02:57 -0800313 size_t idx = 0;
Steven Moreland9fbfe472016-11-14 16:49:17 -0800314 for (auto it = list.begin(); it != list.end(); ++it) {
315 mBuffer[idx++] = *it;
316 }
317 }
318
Yifan Hong602b85a2016-10-24 13:40:01 -0700319 hidl_vec(const std::vector<T> &other) : hidl_vec() {
320 *this = other;
321 }
322
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700323 template <typename InputIterator,
324 typename = typename std::enable_if<std::is_convertible<
325 typename std::iterator_traits<InputIterator>::iterator_category,
326 std::input_iterator_tag>::value>::type>
327 hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) {
328 auto size = std::distance(first, last);
329 if (size > static_cast<int64_t>(UINT32_MAX)) {
330 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
331 }
332 if (size < 0) {
333 details::logAlwaysFatal("size can't be negative.");
334 }
335 mSize = static_cast<uint32_t>(size);
336 mBuffer = new T[mSize];
337
338 size_t idx = 0;
339 for (; first != last; ++first) {
340 mBuffer[idx++] = static_cast<T>(*first);
341 }
342 }
343
Martijn Coenen72110162016-08-19 14:28:25 +0200344 ~hidl_vec() {
345 if (mOwnsBuffer) {
346 delete[] mBuffer;
347 }
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700348 mBuffer = nullptr;
Martijn Coenen72110162016-08-19 14:28:25 +0200349 }
350
Alexey Polyudove2299012016-10-19 09:52:00 -0700351 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200352 // caller's responsibility to ensure that the underlying memory stays
353 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700354 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200355 if (mOwnsBuffer) {
356 delete [] mBuffer;
357 }
358 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100359 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800360 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100361 }
362 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700363 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200364 }
365
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700366 T *data() {
367 return mBuffer;
368 }
369
370 const T *data() const {
371 return mBuffer;
372 }
373
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700374 T *releaseData() {
375 if (!mOwnsBuffer && mSize > 0) {
376 resize(mSize);
377 }
378 mOwnsBuffer = false;
379 return mBuffer;
380 }
381
Martijn Coenene8c36e12017-05-30 16:25:07 -0700382 hidl_vec &operator=(hidl_vec &&other) noexcept {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100383 if (mOwnsBuffer) {
384 delete[] mBuffer;
385 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700386 mBuffer = other.mBuffer;
387 mSize = other.mSize;
388 mOwnsBuffer = other.mOwnsBuffer;
389 other.mOwnsBuffer = false;
390 return *this;
391 }
392
Martijn Coenen72110162016-08-19 14:28:25 +0200393 hidl_vec &operator=(const hidl_vec &other) {
394 if (this != &other) {
395 if (mOwnsBuffer) {
396 delete[] mBuffer;
397 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700398 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200399 }
400
401 return *this;
402 }
403
Yifan Hong602b85a2016-10-24 13:40:01 -0700404 // copy from an std::vector.
405 hidl_vec &operator=(const std::vector<T> &other) {
406 if (mOwnsBuffer) {
407 delete[] mBuffer;
408 }
409 copyFrom(other, other.size());
410 return *this;
411 }
412
413 // cast to an std::vector.
414 operator std::vector<T>() const {
415 std::vector<T> v(mSize);
416 for (size_t i = 0; i < mSize; ++i) {
417 v[i] = mBuffer[i];
418 }
419 return v;
420 }
421
Yifan Hong9fcbb362016-12-20 16:46:41 -0800422 // equality check, assuming that T::operator== is defined.
423 bool operator==(const hidl_vec &other) const {
424 if (mSize != other.size()) {
425 return false;
426 }
427 for (size_t i = 0; i < mSize; ++i) {
428 if (!(mBuffer[i] == other.mBuffer[i])) {
429 return false;
430 }
431 }
432 return true;
433 }
434
435 // inequality check, assuming that T::operator== is defined.
436 inline bool operator!=(const hidl_vec &other) const {
437 return !((*this) == other);
438 }
439
Martijn Coenen72110162016-08-19 14:28:25 +0200440 size_t size() const {
441 return mSize;
442 }
443
444 T &operator[](size_t index) {
445 return mBuffer[index];
446 }
447
448 const T &operator[](size_t index) const {
449 return mBuffer[index];
450 }
451
Steven Morelandaa661fc2017-10-06 09:40:18 -0700452 // Does not initialize primitive types if new size > old size.
Martijn Coenen72110162016-08-19 14:28:25 +0200453 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100454 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800455 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100456 }
Martijn Coenen72110162016-08-19 14:28:25 +0200457 T *newBuffer = new T[size];
458
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100459 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Martijn Coenen72110162016-08-19 14:28:25 +0200460 newBuffer[i] = mBuffer[i];
461 }
462
463 if (mOwnsBuffer) {
464 delete[] mBuffer;
465 }
466 mBuffer = newBuffer;
467
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100468 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200469 mOwnsBuffer = true;
470 }
471
Yifan Hong089ae132016-11-11 11:32:52 -0800472 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
473 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800474
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800475private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800476 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800477 template<bool is_const>
478 class iter : public std::iterator<
479 std::random_access_iterator_tag, /* Category */
480 T,
481 ptrdiff_t, /* Distance */
482 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
483 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800484 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800485 using traits = std::iterator_traits<iter>;
486 using ptr_type = typename traits::pointer;
487 using ref_type = typename traits::reference;
488 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800489 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800490 iter(ptr_type ptr) : mPtr(ptr) { }
491 inline iter &operator++() { mPtr++; return *this; }
492 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
493 inline iter &operator--() { mPtr--; return *this; }
494 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
495 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
496 inline iter operator+(diff_type n) const { return mPtr + n; }
497 inline iter operator-(diff_type n) const { return mPtr - n; }
498 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
499 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
500 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
501 inline ref_type operator*() const { return *mPtr; }
502 inline ptr_type operator->() const { return mPtr; }
503 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
504 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
505 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
506 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
507 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
508 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
509 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800510 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800511 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800512 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800513public:
514 using iterator = iter<false /* is_const */>;
515 using const_iterator = iter<true /* is_const */>;
516
Scott Randolphbb840f72016-11-21 14:39:26 -0800517 iterator begin() { return data(); }
518 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800519 const_iterator begin() const { return data(); }
520 const_iterator end() const { return data()+mSize; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800521
Martijn Coenen72110162016-08-19 14:28:25 +0200522private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100523 details::hidl_pointer<T> mBuffer;
524 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200525 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700526
527 // copy from an array-like object, assuming my resources are freed.
528 template <typename Array>
529 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800530 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700531 mOwnsBuffer = true;
532 if (mSize > 0) {
533 mBuffer = new T[size];
534 for (size_t i = 0; i < size; ++i) {
535 mBuffer[i] = data[i];
536 }
537 } else {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700538 mBuffer = nullptr;
Yifan Hong602b85a2016-10-24 13:40:01 -0700539 }
540 }
Martijn Coenen72110162016-08-19 14:28:25 +0200541};
542
Yifan Hong089ae132016-11-11 11:32:52 -0800543template <typename T>
544const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
545
Andreas Huber20dce082016-09-22 19:39:13 -0700546////////////////////////////////////////////////////////////////////////////////
547
548namespace details {
549
550 template<size_t SIZE1, size_t... SIZES>
551 struct product {
552 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
553 };
554
555 template<size_t SIZE1>
556 struct product<SIZE1> {
557 static constexpr size_t value = SIZE1;
558 };
559
560 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800561 struct std_array {
562 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
563 };
564
565 template<typename T, size_t SIZE1>
566 struct std_array<T, SIZE1> {
567 using type = std::array<T, SIZE1>;
568 };
569
570 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700571 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800572
573 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
574
Andreas Huber20dce082016-09-22 19:39:13 -0700575 explicit accessor(T *base)
576 : mBase(base) {
577 }
578
579 accessor<T, SIZES...> operator[](size_t index) {
580 return accessor<T, SIZES...>(
581 &mBase[index * product<SIZES...>::value]);
582 }
583
Yifan Hong44ab6232016-11-22 16:28:24 -0800584 accessor &operator=(const std_array_type &other) {
585 for (size_t i = 0; i < SIZE1; ++i) {
586 (*this)[i] = other[i];
587 }
588 return *this;
589 }
590
Andreas Huber20dce082016-09-22 19:39:13 -0700591 private:
592 T *mBase;
593 };
594
595 template<typename T, size_t SIZE1>
596 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800597
598 using std_array_type = typename std_array<T, SIZE1>::type;
599
Andreas Huber20dce082016-09-22 19:39:13 -0700600 explicit accessor(T *base)
601 : mBase(base) {
602 }
603
604 T &operator[](size_t index) {
605 return mBase[index];
606 }
607
Yifan Hong44ab6232016-11-22 16:28:24 -0800608 accessor &operator=(const std_array_type &other) {
609 for (size_t i = 0; i < SIZE1; ++i) {
610 (*this)[i] = other[i];
611 }
612 return *this;
613 }
614
Andreas Huber20dce082016-09-22 19:39:13 -0700615 private:
616 T *mBase;
617 };
618
619 template<typename T, size_t SIZE1, size_t... SIZES>
620 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800621
622 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
623
Andreas Huber20dce082016-09-22 19:39:13 -0700624 explicit const_accessor(const T *base)
625 : mBase(base) {
626 }
627
Yifan Hongbe7a6882017-01-05 17:30:17 -0800628 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700629 return const_accessor<T, SIZES...>(
630 &mBase[index * product<SIZES...>::value]);
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 template<typename T, size_t SIZE1>
646 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800647
648 using std_array_type = typename std_array<T, SIZE1>::type;
649
Andreas Huber20dce082016-09-22 19:39:13 -0700650 explicit const_accessor(const T *base)
651 : mBase(base) {
652 }
653
654 const T &operator[](size_t index) const {
655 return mBase[index];
656 }
657
Yifan Hong44ab6232016-11-22 16:28:24 -0800658 operator std_array_type() {
659 std_array_type array;
660 for (size_t i = 0; i < SIZE1; ++i) {
661 array[i] = (*this)[i];
662 }
663 return array;
664 }
665
Andreas Huber20dce082016-09-22 19:39:13 -0700666 private:
667 const T *mBase;
668 };
669
670} // namespace details
671
672////////////////////////////////////////////////////////////////////////////////
673
Yifan Hong44ab6232016-11-22 16:28:24 -0800674// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700675template<typename T, size_t SIZE1, size_t... SIZES>
676struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800677
678 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
679
Andreas Huber20dce082016-09-22 19:39:13 -0700680 hidl_array() = default;
681
Yifan Hong44ab6232016-11-22 16:28:24 -0800682 // Copies the data from source, using T::operator=(const T &).
683 hidl_array(const T *source) {
684 for (size_t i = 0; i < elementCount(); ++i) {
685 mBuffer[i] = source[i];
686 }
687 }
688
689 // Copies the data from the given std::array, using T::operator=(const T &).
690 hidl_array(const std_array_type &array) {
691 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
692 modifier = array;
693 }
694
Andreas Huber20dce082016-09-22 19:39:13 -0700695 T *data() { return mBuffer; }
696 const T *data() const { return mBuffer; }
697
698 details::accessor<T, SIZES...> operator[](size_t index) {
699 return details::accessor<T, SIZES...>(
700 &mBuffer[index * details::product<SIZES...>::value]);
701 }
702
703 details::const_accessor<T, SIZES...> operator[](size_t index) const {
704 return details::const_accessor<T, SIZES...>(
705 &mBuffer[index * details::product<SIZES...>::value]);
706 }
707
Yifan Hong9fcbb362016-12-20 16:46:41 -0800708 // equality check, assuming that T::operator== is defined.
709 bool operator==(const hidl_array &other) const {
710 for (size_t i = 0; i < elementCount(); ++i) {
711 if (!(mBuffer[i] == other.mBuffer[i])) {
712 return false;
713 }
714 }
715 return true;
716 }
717
718 inline bool operator!=(const hidl_array &other) const {
719 return !((*this) == other);
720 }
721
Andreas Huber00a985c2016-09-28 14:24:53 -0700722 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
723
724 static constexpr size_tuple_type size() {
725 return std::make_tuple(SIZE1, SIZES...);
726 }
727
Yifan Hong44ab6232016-11-22 16:28:24 -0800728 static constexpr size_t elementCount() {
729 return details::product<SIZE1, SIZES...>::value;
730 }
731
732 operator std_array_type() const {
733 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
734 }
735
Andreas Huber20dce082016-09-22 19:39:13 -0700736private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800737 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700738};
739
Yifan Hong44ab6232016-11-22 16:28:24 -0800740// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700741template<typename T, size_t SIZE1>
742struct hidl_array<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800743
744 using std_array_type = typename details::std_array<T, SIZE1>::type;
745
Andreas Huber20dce082016-09-22 19:39:13 -0700746 hidl_array() = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800747
748 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800749 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800750 for (size_t i = 0; i < elementCount(); ++i) {
751 mBuffer[i] = source[i];
752 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800753 }
Andreas Huber20dce082016-09-22 19:39:13 -0700754
Yifan Hong44ab6232016-11-22 16:28:24 -0800755 // Copies the data from the given std::array, using T::operator=(const T &).
756 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
757
Andreas Huber20dce082016-09-22 19:39:13 -0700758 T *data() { return mBuffer; }
759 const T *data() const { return mBuffer; }
760
761 T &operator[](size_t index) {
762 return mBuffer[index];
763 }
764
765 const T &operator[](size_t index) const {
766 return mBuffer[index];
767 }
768
Yifan Hong9fcbb362016-12-20 16:46:41 -0800769 // equality check, assuming that T::operator== is defined.
770 bool operator==(const hidl_array &other) const {
771 for (size_t i = 0; i < elementCount(); ++i) {
772 if (!(mBuffer[i] == other.mBuffer[i])) {
773 return false;
774 }
775 }
776 return true;
777 }
778
779 inline bool operator!=(const hidl_array &other) const {
780 return !((*this) == other);
781 }
782
Andreas Huber00a985c2016-09-28 14:24:53 -0700783 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800784 static constexpr size_t elementCount() { return SIZE1; }
785
786 // Copies the data to an std::array, using T::operator=(T).
787 operator std_array_type() const {
788 std_array_type array;
789 for (size_t i = 0; i < SIZE1; ++i) {
790 array[i] = mBuffer[i];
791 }
792 return array;
793 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700794
Andreas Huber20dce082016-09-22 19:39:13 -0700795private:
796 T mBuffer[SIZE1];
797};
798
Martijn Coenen72110162016-08-19 14:28:25 +0200799// ----------------------------------------------------------------------
800// Version functions
801struct hidl_version {
802public:
Chia-I Wu666b76b2016-10-06 14:15:59 +0800803 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {}
Martijn Coenen72110162016-08-19 14:28:25 +0200804
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700805 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200806 return (mMajor == other.get_major() && mMinor == other.get_minor());
807 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200808
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800809 bool operator<(const hidl_version& other) const {
810 return (mMajor < other.get_major() ||
811 (mMajor == other.get_major() && mMinor < other.get_minor()));
812 }
813
814 bool operator>(const hidl_version& other) const {
815 return other < *this;
816 }
817
818 bool operator<=(const hidl_version& other) const {
819 return !(*this > other);
820 }
821
822 bool operator>=(const hidl_version& other) const {
823 return !(*this < other);
824 }
825
Martijn Coenen097a7672016-09-08 16:56:41 +0200826 constexpr uint16_t get_major() const { return mMajor; }
827 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200828
Martijn Coenen72110162016-08-19 14:28:25 +0200829private:
830 uint16_t mMajor;
831 uint16_t mMinor;
832};
833
834inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
835 return hidl_version(major,minor);
836}
837
Yifan Hongbe7a6882017-01-05 17:30:17 -0800838///////////////////// toString functions
839
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800840std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800841
842// toString alias for numeric types
843template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
844inline std::string toString(T t) {
845 return std::to_string(t);
846}
847
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800848namespace details {
849
Yifan Hongbe7a6882017-01-05 17:30:17 -0800850template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
851inline std::string toHexString(T t, bool prefix = true) {
852 std::ostringstream os;
853 if (prefix) { os << std::showbase; }
854 os << std::hex << t;
855 return os.str();
856}
857
858template<>
859inline std::string toHexString(uint8_t t, bool prefix) {
860 return toHexString(static_cast<int32_t>(t), prefix);
861}
862
863template<>
864inline std::string toHexString(int8_t t, bool prefix) {
865 return toHexString(static_cast<int32_t>(t), prefix);
866}
867
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800868template<typename Array>
869std::string arrayToString(const Array &a, size_t size);
870
871template<size_t SIZE1>
872std::string arraySizeToString() {
873 return std::string{"["} + toString(SIZE1) + "]";
874}
875
876template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
877std::string arraySizeToString() {
878 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
879}
880
881template<typename T, size_t SIZE1>
882std::string toString(details::const_accessor<T, SIZE1> a) {
883 return arrayToString(a, SIZE1);
884}
885
886template<typename Array>
887std::string arrayToString(const Array &a, size_t size) {
888 using android::hardware::toString;
889 std::string os;
890 os += "{";
891 for (size_t i = 0; i < size; ++i) {
892 if (i > 0) {
893 os += ", ";
894 }
895 os += toString(a[i]);
896 }
897 os += "}";
898 return os;
899}
900
901template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
902std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
903 return arrayToString(a, SIZE1);
904}
905
906} //namespace details
907
908inline std::string toString(const void *t) {
909 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800910}
911
912// debug string dump. There will be quotes around the string!
913inline std::string toString(const hidl_string &hs) {
914 return std::string{"\""} + hs.c_str() + "\"";
915}
916
917// debug string dump
918inline std::string toString(const hidl_handle &hs) {
919 return toString(hs.getNativeHandle());
920}
921
922inline std::string toString(const hidl_memory &mem) {
923 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
924 + toString(mem.size())
925 + ", .handle = " + toString(mem.handle()) + "}";
926}
927
928inline std::string toString(const sp<hidl_death_recipient> &dr) {
929 return std::string{"death_recipient@"} + toString(dr.get());
930}
931
Yifan Hongbe7a6882017-01-05 17:30:17 -0800932// debug string dump, assuming that toString(T) is defined.
933template<typename T>
934std::string toString(const hidl_vec<T> &a) {
935 std::string os;
936 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800937 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -0800938 return os;
939}
940
Yifan Hongbe7a6882017-01-05 17:30:17 -0800941template<typename T, size_t SIZE1>
942std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800943 return details::arraySizeToString<SIZE1>()
944 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800945}
946
947template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
948std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800949 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
950 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800951}
952
Martijn Coenen72110162016-08-19 14:28:25 +0200953} // namespace hardware
954} // namespace android
955
Martijn Coenenc28f1152016-08-22 14:06:56 +0200956
Martijn Coenen72110162016-08-19 14:28:25 +0200957#endif // ANDROID_HIDL_SUPPORT_H