blob: 89aa5a905f006830a4259708da97990f5a081ad6 [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 {
Pirama Arumuga Nainared4c5d02018-04-10 15:46:23 -070043
44struct IMemory;
45
46} // namespace V1_0
47} // namespace memory
48} // namespace hidl
Martijn Coenen30791002016-12-01 15:40:46 +010049
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010050namespace hidl {
51namespace base {
52namespace V1_0 {
Pirama Arumuga Nainared4c5d02018-04-10 15:46:23 -070053
54struct IBase;
55
56} // namespace V1_0
57} // namespace base
58} // namespace hidl
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010059
Martijn Coenen72110162016-08-19 14:28:25 +020060namespace hardware {
61
Yifan Hong24332ef2017-03-07 16:22:19 -080062namespace details {
63// Return true on userdebug / eng builds and false on user builds.
64bool debuggable();
65} // namespace details
66
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010067// hidl_death_recipient is a callback interfaced that can be used with
68// linkToDeath() / unlinkToDeath()
69struct hidl_death_recipient : public virtual RefBase {
70 virtual void serviceDied(uint64_t cookie,
71 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
72};
73
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010074// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
75// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010076// The ownership semantics for this are:
77// 1) The conversion constructor and assignment operator taking a const native_handle_t*
78// do not take ownership of the handle; this is because these operations are usually
79// just done for IPC, and cloning by default is a waste of resources. If you want
80// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
81// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
82// that is because it's not intuitive that this class encapsulates a native_handle_t
83// which needs cloning to be valid; in particular, this allows constructs like this:
84// hidl_handle copy;
85// foo->someHidlCall([&](auto incoming_handle) {
86// copy = incoming_handle;
87// });
88// // copy and its enclosed file descriptors will remain valid here.
89// 3) The move constructor does what you would expect; it only owns the handle if the
90// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010091struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010092 hidl_handle();
93 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010094
Martijn Coenen04b91c02017-01-19 14:14:21 +010095 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010096
Martijn Coenen04b91c02017-01-19 14:14:21 +010097 // copy constructor.
98 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010099
100 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700101 hidl_handle(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100102
Steven Moreland6ffdc2a2017-01-23 12:34:33 -0800103 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100104 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
Martijn Coenen04b91c02017-01-19 14:14:21 +0100106 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100107
Martijn Coenene8c36e12017-05-30 16:25:07 -0700108 hidl_handle &operator=(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100109
Martijn Coenen04b91c02017-01-19 14:14:21 +0100110 void setTo(native_handle_t* handle, bool shouldOwn = false);
111
112 const native_handle_t* operator->() const;
113
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100114 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100115 operator const native_handle_t *() const;
116
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100117 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100118 const native_handle_t *getNativeHandle() const;
Nirav Atre564a8d22018-07-26 18:29:12 -0700119
120 // offsetof(hidl_handle, mHandle) exposed since mHandle is private.
121 static const size_t kOffsetOfNativeHandle;
122
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100123private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100124 void freeHandle();
125
Andreas Huber6688d602017-03-30 10:58:29 -0700126 details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8)));
127 bool mOwnsHandle __attribute ((aligned(8)));
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100128};
129
Martijn Coenen72110162016-08-19 14:28:25 +0200130struct hidl_string {
131 hidl_string();
132 ~hidl_string();
133
Yifan Hong602b85a2016-10-24 13:40:01 -0700134 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200135 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800136 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700137 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800138 // copy the first length characters from a C-style string.
139 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700140 // copy from an std::string.
141 hidl_string(const std::string &);
142
143 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700144 hidl_string(hidl_string &&) noexcept;
Martijn Coenen72110162016-08-19 14:28:25 +0200145
146 const char *c_str() const;
147 size_t size() const;
148 bool empty() const;
149
Yifan Hong602b85a2016-10-24 13:40:01 -0700150 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700151 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700152 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200153 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700154 // copy from an std::string.
155 hidl_string &operator=(const std::string &);
156 // move assignment operator.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700157 hidl_string &operator=(hidl_string &&other) noexcept;
Yifan Hong602b85a2016-10-24 13:40:01 -0700158 // cast to std::string.
159 operator std::string() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700160
Martijn Coenen72110162016-08-19 14:28:25 +0200161 void clear();
162
163 // Reference an external char array. Ownership is _not_ transferred.
164 // Caller is responsible for ensuring that underlying memory is valid
165 // for the lifetime of this hidl_string.
Steven Moreland57c6fcb2018-03-06 11:31:33 -0800166 //
167 // size == strlen(data)
Martijn Coenen72110162016-08-19 14:28:25 +0200168 void setToExternal(const char *data, size_t size);
169
Andreas Huberebfeb362016-08-25 13:39:05 -0700170 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
171 static const size_t kOffsetOfBuffer;
172
Martijn Coenen72110162016-08-19 14:28:25 +0200173private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100174 details::hidl_pointer<const char> mBuffer;
175 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700176 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Martijn Coenen72110162016-08-19 14:28:25 +0200177
Yifan Hong602b85a2016-10-24 13:40:01 -0700178 // copy from data with size. Assume that my memory is freed
179 // (through clear(), for example)
180 void copyFrom(const char *data, size_t size);
181 // move from another hidl_string
182 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200183};
184
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700185// Use NOLINT to suppress missing parentheses warnings around OP.
Janis Danisevskis62a38df2017-11-22 13:47:33 -0800186#define HIDL_STRING_OPERATOR(OP) \
187 inline bool operator OP(const hidl_string& hs1, const hidl_string& hs2) { \
188 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
189 } \
190 inline bool operator OP(const hidl_string& hs, const char* s) { \
191 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
192 } \
193 inline bool operator OP(const char* s, const hidl_string& hs) { \
194 return strcmp(s, hs.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800195 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800196
Steven Moreland551396a2017-02-13 18:33:20 -0800197HIDL_STRING_OPERATOR(==)
198HIDL_STRING_OPERATOR(!=)
199HIDL_STRING_OPERATOR(<)
200HIDL_STRING_OPERATOR(<=)
201HIDL_STRING_OPERATOR(>)
202HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800203
Steven Moreland551396a2017-02-13 18:33:20 -0800204#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700205
Scott Randolph0c84ab42017-04-03 14:07:14 -0700206// Send our content to the output stream
207std::ostream& operator<<(std::ostream& os, const hidl_string& str);
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
Martijn Coenen04b91c02017-01-19 14:14:21 +0100217 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100218 }
219
220 /**
Steven Moreland3f8c4162017-10-11 13:47:00 -0700221 * Creates a hidl_memory object whose handle has the same lifetime
222 * as the handle moved into it.
223 */
224 hidl_memory(const hidl_string& name, hidl_handle&& handle, size_t size)
225 : mHandle(std::move(handle)), mSize(size), mName(name) {}
226
227 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100228 * Creates a hidl_memory object, but doesn't take ownership of
229 * the passed in native_handle_t; callers are responsible for
230 * making sure the handle remains valid while this object is
231 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100232 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100233 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
234 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100235 mSize(size),
236 mName(name)
237 {}
238
239 // copy constructor
240 hidl_memory(const hidl_memory& other) {
241 *this = other;
242 }
243
244 // copy assignment
245 hidl_memory &operator=(const hidl_memory &other) {
246 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100247 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100248 mSize = other.mSize;
249 mName = other.mName;
250 }
251
252 return *this;
253 }
254
Hridya Valsaraju01268892017-02-27 08:48:38 -0800255 // move constructor
Martijn Coenene8c36e12017-05-30 16:25:07 -0700256 hidl_memory(hidl_memory&& other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800257 *this = std::move(other);
258 }
259
260 // move assignment
Martijn Coenene8c36e12017-05-30 16:25:07 -0700261 hidl_memory &operator=(hidl_memory &&other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800262 if (this != &other) {
263 mHandle = std::move(other.mHandle);
264 mSize = other.mSize;
265 mName = std::move(other.mName);
266 other.mSize = 0;
267 }
268
269 return *this;
270 }
271
Martijn Coenen30791002016-12-01 15:40:46 +0100272
273 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100274 }
275
276 const native_handle_t* handle() const {
277 return mHandle;
278 }
279
280 const hidl_string &name() const {
281 return mName;
282 }
283
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800284 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100285 return mSize;
286 }
287
Howard Chen9bc35c42017-10-13 14:21:46 +0800288 // @return true if it's valid
289 inline bool valid() const { return handle() != nullptr; }
290
Martijn Coenen30791002016-12-01 15:40:46 +0100291 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
292 static const size_t kOffsetOfHandle;
293 // offsetof(hidl_memory, mName) exposed since mHandle is private.
294 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800295
Martijn Coenen30791002016-12-01 15:40:46 +0100296private:
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800297 hidl_handle mHandle __attribute__ ((aligned(8)));
298 uint64_t mSize __attribute__ ((aligned(8)));
299 hidl_string mName __attribute__ ((aligned(8)));
Martijn Coenen30791002016-12-01 15:40:46 +0100300};
301
Howard Chen9bc35c42017-10-13 14:21:46 +0800302// HidlMemory is a wrapper class to support sp<> for hidl_memory. It also
303// provides factory methods to create an instance from hidl_memory or
304// from a opened file descriptor. The number of factory methods can be increase
305// to support other type of hidl_memory without break the ABI.
306class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase {
307public:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800308 static sp<HidlMemory> getInstance(const hidl_memory& mem);
309
Howard Chen9bc35c42017-10-13 14:21:46 +0800310 static sp<HidlMemory> getInstance(hidl_memory&& mem);
311
312 static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size);
313 // @param fd, shall be opened and points to the resource.
314 // @note this method takes the ownership of the fd and will close it in
315 // destructor
Howard Chen8e42f5a2017-11-09 13:58:28 +0800316 // @return nullptr in failure with the fd closed
Howard Chen9bc35c42017-10-13 14:21:46 +0800317 static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
318
Howard Chen280af0a2017-10-20 13:58:36 +0800319 virtual ~HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800320
Howard Chen9bc35c42017-10-13 14:21:46 +0800321protected:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800322 HidlMemory();
323 HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size);
Howard Chen9bc35c42017-10-13 14:21:46 +0800324};
Andreas Huber20dce082016-09-22 19:39:13 -0700325////////////////////////////////////////////////////////////////////////////////
326
Martijn Coenen72110162016-08-19 14:28:25 +0200327template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800328struct hidl_vec {
Steven Morelandb37de4a2019-05-06 09:39:47 -0700329 using value_type = T;
330
Steven Moreland5cc16912019-04-18 12:54:56 -0700331 hidl_vec() {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800332 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Steven Moreland5cc16912019-04-18 12:54:56 -0700333
334 memset(this, 0, sizeof(*this));
335 // mSize is 0
336 // mBuffer is nullptr
337
338 // this is for consistency with the original implementation
339 mOwnsBuffer = true;
Martijn Coenen72110162016-08-19 14:28:25 +0200340 }
341
Steven Morelandaa661fc2017-10-06 09:40:18 -0700342 // Note, does not initialize primitive types.
Eran Messeri6329c3e2019-02-26 11:34:32 +0000343 hidl_vec(size_t size) : hidl_vec() { resize(size); }
Steven Moreland96e9de02017-09-29 14:11:20 -0700344
Yifan Hong602b85a2016-10-24 13:40:01 -0700345 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200346 *this = other;
347 }
348
Steven Moreland5cc16912019-04-18 12:54:56 -0700349 hidl_vec(hidl_vec<T> &&other) noexcept : hidl_vec() {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100350 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700351 }
352
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800353 hidl_vec(const std::initializer_list<T> list) : hidl_vec() { *this = list; }
Steven Moreland9fbfe472016-11-14 16:49:17 -0800354
Yifan Hong602b85a2016-10-24 13:40:01 -0700355 hidl_vec(const std::vector<T> &other) : hidl_vec() {
356 *this = other;
357 }
358
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700359 template <typename InputIterator,
360 typename = typename std::enable_if<std::is_convertible<
361 typename std::iterator_traits<InputIterator>::iterator_category,
362 std::input_iterator_tag>::value>::type>
Steven Moreland5cc16912019-04-18 12:54:56 -0700363 hidl_vec(InputIterator first, InputIterator last) : hidl_vec() {
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700364 auto size = std::distance(first, last);
365 if (size > static_cast<int64_t>(UINT32_MAX)) {
366 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
367 }
368 if (size < 0) {
369 details::logAlwaysFatal("size can't be negative.");
370 }
371 mSize = static_cast<uint32_t>(size);
Steven Moreland97e2d172019-04-30 17:56:45 -0700372 mBuffer = new T[mSize]();
Steven Moreland5cc16912019-04-18 12:54:56 -0700373 mOwnsBuffer = true;
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700374
375 size_t idx = 0;
376 for (; first != last; ++first) {
377 mBuffer[idx++] = static_cast<T>(*first);
378 }
379 }
380
Martijn Coenen72110162016-08-19 14:28:25 +0200381 ~hidl_vec() {
382 if (mOwnsBuffer) {
383 delete[] mBuffer;
384 }
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700385 mBuffer = nullptr;
Martijn Coenen72110162016-08-19 14:28:25 +0200386 }
387
Alexey Polyudove2299012016-10-19 09:52:00 -0700388 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200389 // caller's responsibility to ensure that the underlying memory stays
390 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700391 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200392 if (mOwnsBuffer) {
393 delete [] mBuffer;
394 }
395 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100396 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800397 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100398 }
399 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700400 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200401 }
402
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700403 T *data() {
404 return mBuffer;
405 }
406
407 const T *data() const {
408 return mBuffer;
409 }
410
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700411 T *releaseData() {
412 if (!mOwnsBuffer && mSize > 0) {
413 resize(mSize);
414 }
415 mOwnsBuffer = false;
416 return mBuffer;
417 }
418
Martijn Coenene8c36e12017-05-30 16:25:07 -0700419 hidl_vec &operator=(hidl_vec &&other) noexcept {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100420 if (mOwnsBuffer) {
421 delete[] mBuffer;
422 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700423 mBuffer = other.mBuffer;
424 mSize = other.mSize;
425 mOwnsBuffer = other.mOwnsBuffer;
426 other.mOwnsBuffer = false;
427 return *this;
428 }
429
Martijn Coenen72110162016-08-19 14:28:25 +0200430 hidl_vec &operator=(const hidl_vec &other) {
431 if (this != &other) {
432 if (mOwnsBuffer) {
433 delete[] mBuffer;
434 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700435 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200436 }
437
438 return *this;
439 }
440
Yifan Hong602b85a2016-10-24 13:40:01 -0700441 // copy from an std::vector.
442 hidl_vec &operator=(const std::vector<T> &other) {
443 if (mOwnsBuffer) {
444 delete[] mBuffer;
445 }
446 copyFrom(other, other.size());
447 return *this;
448 }
449
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800450 hidl_vec& operator=(const std::initializer_list<T> list) {
451 if (list.size() > UINT32_MAX) {
452 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
453 }
454 if (mOwnsBuffer) {
455 delete[] mBuffer;
456 }
457 mSize = static_cast<uint32_t>(list.size());
Steven Moreland97e2d172019-04-30 17:56:45 -0700458 mBuffer = new T[mSize]();
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800459 mOwnsBuffer = true;
460
461 size_t idx = 0;
462 for (auto it = list.begin(); it != list.end(); ++it) {
463 mBuffer[idx++] = *it;
464 }
465 return *this;
466 }
467
Yifan Hong602b85a2016-10-24 13:40:01 -0700468 // cast to an std::vector.
469 operator std::vector<T>() const {
470 std::vector<T> v(mSize);
471 for (size_t i = 0; i < mSize; ++i) {
472 v[i] = mBuffer[i];
473 }
474 return v;
475 }
476
Yifan Hong9fcbb362016-12-20 16:46:41 -0800477 // equality check, assuming that T::operator== is defined.
478 bool operator==(const hidl_vec &other) const {
479 if (mSize != other.size()) {
480 return false;
481 }
482 for (size_t i = 0; i < mSize; ++i) {
483 if (!(mBuffer[i] == other.mBuffer[i])) {
484 return false;
485 }
486 }
487 return true;
488 }
489
490 // inequality check, assuming that T::operator== is defined.
491 inline bool operator!=(const hidl_vec &other) const {
492 return !((*this) == other);
493 }
494
Martijn Coenen72110162016-08-19 14:28:25 +0200495 size_t size() const {
496 return mSize;
497 }
498
499 T &operator[](size_t index) {
500 return mBuffer[index];
501 }
502
503 const T &operator[](size_t index) const {
504 return mBuffer[index];
505 }
506
Steven Morelandaa661fc2017-10-06 09:40:18 -0700507 // Does not initialize primitive types if new size > old size.
Martijn Coenen72110162016-08-19 14:28:25 +0200508 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100509 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800510 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100511 }
Steven Moreland97e2d172019-04-30 17:56:45 -0700512 T* newBuffer = new T[size]();
Martijn Coenen72110162016-08-19 14:28:25 +0200513
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100514 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Steven Morelanddfbedb82019-06-19 14:51:11 -0700515 newBuffer[i] = std::move(mBuffer[i]);
Martijn Coenen72110162016-08-19 14:28:25 +0200516 }
517
518 if (mOwnsBuffer) {
519 delete[] mBuffer;
520 }
521 mBuffer = newBuffer;
522
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100523 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200524 mOwnsBuffer = true;
525 }
526
Yifan Hong089ae132016-11-11 11:32:52 -0800527 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
528 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800529
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800530private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800531 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800532 template<bool is_const>
533 class iter : public std::iterator<
534 std::random_access_iterator_tag, /* Category */
535 T,
536 ptrdiff_t, /* Distance */
537 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
538 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800539 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800540 using traits = std::iterator_traits<iter>;
541 using ptr_type = typename traits::pointer;
542 using ref_type = typename traits::reference;
543 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800544 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800545 iter(ptr_type ptr) : mPtr(ptr) { }
546 inline iter &operator++() { mPtr++; return *this; }
547 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
548 inline iter &operator--() { mPtr--; return *this; }
549 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
550 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
551 inline iter operator+(diff_type n) const { return mPtr + n; }
552 inline iter operator-(diff_type n) const { return mPtr - n; }
553 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
554 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
555 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
556 inline ref_type operator*() const { return *mPtr; }
557 inline ptr_type operator->() const { return mPtr; }
558 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
559 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
560 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
561 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
562 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
563 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
564 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800565 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800566 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800567 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800568public:
569 using iterator = iter<false /* is_const */>;
570 using const_iterator = iter<true /* is_const */>;
571
Scott Randolphbb840f72016-11-21 14:39:26 -0800572 iterator begin() { return data(); }
573 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800574 const_iterator begin() const { return data(); }
575 const_iterator end() const { return data()+mSize; }
Tomasz Wasilczyk93f75922019-07-16 08:44:15 -0700576 iterator find(const T& v) { return std::find(begin(), end(), v); }
577 const_iterator find(const T& v) const { return std::find(begin(), end(), v); }
578 bool contains(const T& v) const { return find(v) != end(); }
Scott Randolphbb840f72016-11-21 14:39:26 -0800579
Tomasz Wasilczyk93f75922019-07-16 08:44:15 -0700580 private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100581 details::hidl_pointer<T> mBuffer;
582 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200583 bool mOwnsBuffer;
Yifan Hong602b85a2016-10-24 13:40:01 -0700584
585 // copy from an array-like object, assuming my resources are freed.
586 template <typename Array>
587 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800588 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700589 mOwnsBuffer = true;
590 if (mSize > 0) {
Steven Moreland97e2d172019-04-30 17:56:45 -0700591 mBuffer = new T[size]();
Yifan Hong602b85a2016-10-24 13:40:01 -0700592 for (size_t i = 0; i < size; ++i) {
593 mBuffer[i] = data[i];
594 }
595 } else {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700596 mBuffer = nullptr;
Yifan Hong602b85a2016-10-24 13:40:01 -0700597 }
598 }
Martijn Coenen72110162016-08-19 14:28:25 +0200599};
600
Yifan Hong089ae132016-11-11 11:32:52 -0800601template <typename T>
602const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
603
Andreas Huber20dce082016-09-22 19:39:13 -0700604////////////////////////////////////////////////////////////////////////////////
605
606namespace details {
607
608 template<size_t SIZE1, size_t... SIZES>
609 struct product {
610 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
611 };
612
613 template<size_t SIZE1>
614 struct product<SIZE1> {
615 static constexpr size_t value = SIZE1;
616 };
617
618 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800619 struct std_array {
620 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
621 };
622
623 template<typename T, size_t SIZE1>
624 struct std_array<T, SIZE1> {
625 using type = std::array<T, SIZE1>;
626 };
627
628 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700629 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800630
631 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
632
Andreas Huber20dce082016-09-22 19:39:13 -0700633 explicit accessor(T *base)
634 : mBase(base) {
635 }
636
637 accessor<T, SIZES...> operator[](size_t index) {
638 return accessor<T, SIZES...>(
639 &mBase[index * product<SIZES...>::value]);
640 }
641
Yifan Hong44ab6232016-11-22 16:28:24 -0800642 accessor &operator=(const std_array_type &other) {
643 for (size_t i = 0; i < SIZE1; ++i) {
644 (*this)[i] = other[i];
645 }
646 return *this;
647 }
648
Andreas Huber20dce082016-09-22 19:39:13 -0700649 private:
650 T *mBase;
651 };
652
653 template<typename T, size_t SIZE1>
654 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800655
656 using std_array_type = typename std_array<T, SIZE1>::type;
657
Andreas Huber20dce082016-09-22 19:39:13 -0700658 explicit accessor(T *base)
659 : mBase(base) {
660 }
661
662 T &operator[](size_t index) {
663 return mBase[index];
664 }
665
Yifan Hong44ab6232016-11-22 16:28:24 -0800666 accessor &operator=(const std_array_type &other) {
667 for (size_t i = 0; i < SIZE1; ++i) {
668 (*this)[i] = other[i];
669 }
670 return *this;
671 }
672
Andreas Huber20dce082016-09-22 19:39:13 -0700673 private:
674 T *mBase;
675 };
676
677 template<typename T, size_t SIZE1, size_t... SIZES>
678 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800679
680 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
681
Andreas Huber20dce082016-09-22 19:39:13 -0700682 explicit const_accessor(const T *base)
683 : mBase(base) {
684 }
685
Yifan Hongbe7a6882017-01-05 17:30:17 -0800686 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700687 return const_accessor<T, SIZES...>(
688 &mBase[index * product<SIZES...>::value]);
689 }
690
Yifan Hong44ab6232016-11-22 16:28:24 -0800691 operator std_array_type() {
692 std_array_type array;
693 for (size_t i = 0; i < SIZE1; ++i) {
694 array[i] = (*this)[i];
695 }
696 return array;
697 }
698
Andreas Huber20dce082016-09-22 19:39:13 -0700699 private:
700 const T *mBase;
701 };
702
703 template<typename T, size_t SIZE1>
704 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800705
706 using std_array_type = typename std_array<T, SIZE1>::type;
707
Andreas Huber20dce082016-09-22 19:39:13 -0700708 explicit const_accessor(const T *base)
709 : mBase(base) {
710 }
711
712 const T &operator[](size_t index) const {
713 return mBase[index];
714 }
715
Yifan Hong44ab6232016-11-22 16:28:24 -0800716 operator std_array_type() {
717 std_array_type array;
718 for (size_t i = 0; i < SIZE1; ++i) {
719 array[i] = (*this)[i];
720 }
721 return array;
722 }
723
Andreas Huber20dce082016-09-22 19:39:13 -0700724 private:
725 const T *mBase;
726 };
727
728} // namespace details
729
730////////////////////////////////////////////////////////////////////////////////
731
Yifan Hong44ab6232016-11-22 16:28:24 -0800732// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700733template<typename T, size_t SIZE1, size_t... SIZES>
734struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800735
736 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
737
Andreas Huber20dce082016-09-22 19:39:13 -0700738 hidl_array() = default;
Steven Morelanddfbedb82019-06-19 14:51:11 -0700739 hidl_array(const hidl_array&) noexcept = default;
740 hidl_array(hidl_array&&) noexcept = default;
Andreas Huber20dce082016-09-22 19:39:13 -0700741
Yifan Hong44ab6232016-11-22 16:28:24 -0800742 // Copies the data from source, using T::operator=(const T &).
743 hidl_array(const T *source) {
744 for (size_t i = 0; i < elementCount(); ++i) {
745 mBuffer[i] = source[i];
746 }
747 }
748
749 // Copies the data from the given std::array, using T::operator=(const T &).
750 hidl_array(const std_array_type &array) {
751 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
752 modifier = array;
753 }
754
Steven Morelanddfbedb82019-06-19 14:51:11 -0700755 hidl_array& operator=(const hidl_array&) noexcept = default;
756 hidl_array& operator=(hidl_array&&) noexcept = default;
757
Andreas Huber20dce082016-09-22 19:39:13 -0700758 T *data() { return mBuffer; }
759 const T *data() const { return mBuffer; }
760
761 details::accessor<T, SIZES...> operator[](size_t index) {
762 return details::accessor<T, SIZES...>(
763 &mBuffer[index * details::product<SIZES...>::value]);
764 }
765
766 details::const_accessor<T, SIZES...> operator[](size_t index) const {
767 return details::const_accessor<T, SIZES...>(
768 &mBuffer[index * details::product<SIZES...>::value]);
769 }
770
Yifan Hong9fcbb362016-12-20 16:46:41 -0800771 // equality check, assuming that T::operator== is defined.
772 bool operator==(const hidl_array &other) const {
773 for (size_t i = 0; i < elementCount(); ++i) {
774 if (!(mBuffer[i] == other.mBuffer[i])) {
775 return false;
776 }
777 }
778 return true;
779 }
780
781 inline bool operator!=(const hidl_array &other) const {
782 return !((*this) == other);
783 }
784
Andreas Huber00a985c2016-09-28 14:24:53 -0700785 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
786
787 static constexpr size_tuple_type size() {
788 return std::make_tuple(SIZE1, SIZES...);
789 }
790
Yifan Hong44ab6232016-11-22 16:28:24 -0800791 static constexpr size_t elementCount() {
792 return details::product<SIZE1, SIZES...>::value;
793 }
794
795 operator std_array_type() const {
796 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
797 }
798
Andreas Huber20dce082016-09-22 19:39:13 -0700799private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800800 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700801};
802
Yifan Hong44ab6232016-11-22 16:28:24 -0800803// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700804template<typename T, size_t SIZE1>
805struct hidl_array<T, SIZE1> {
Steven Morelandb37de4a2019-05-06 09:39:47 -0700806 using value_type = T;
Yifan Hong44ab6232016-11-22 16:28:24 -0800807 using std_array_type = typename details::std_array<T, SIZE1>::type;
808
Andreas Huber20dce082016-09-22 19:39:13 -0700809 hidl_array() = default;
Steven Morelanddfbedb82019-06-19 14:51:11 -0700810 hidl_array(const hidl_array&) noexcept = default;
811 hidl_array(hidl_array&&) noexcept = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800812
813 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800814 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800815 for (size_t i = 0; i < elementCount(); ++i) {
816 mBuffer[i] = source[i];
817 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800818 }
Andreas Huber20dce082016-09-22 19:39:13 -0700819
Yifan Hong44ab6232016-11-22 16:28:24 -0800820 // Copies the data from the given std::array, using T::operator=(const T &).
821 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
822
Steven Morelanddfbedb82019-06-19 14:51:11 -0700823 hidl_array& operator=(const hidl_array&) noexcept = default;
824 hidl_array& operator=(hidl_array&&) noexcept = default;
825
Andreas Huber20dce082016-09-22 19:39:13 -0700826 T *data() { return mBuffer; }
827 const T *data() const { return mBuffer; }
828
829 T &operator[](size_t index) {
830 return mBuffer[index];
831 }
832
833 const T &operator[](size_t index) const {
834 return mBuffer[index];
835 }
836
Yifan Hong9fcbb362016-12-20 16:46:41 -0800837 // equality check, assuming that T::operator== is defined.
838 bool operator==(const hidl_array &other) const {
839 for (size_t i = 0; i < elementCount(); ++i) {
840 if (!(mBuffer[i] == other.mBuffer[i])) {
841 return false;
842 }
843 }
844 return true;
845 }
846
847 inline bool operator!=(const hidl_array &other) const {
848 return !((*this) == other);
849 }
850
Andreas Huber00a985c2016-09-28 14:24:53 -0700851 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800852 static constexpr size_t elementCount() { return SIZE1; }
853
854 // Copies the data to an std::array, using T::operator=(T).
855 operator std_array_type() const {
856 std_array_type array;
857 for (size_t i = 0; i < SIZE1; ++i) {
858 array[i] = mBuffer[i];
859 }
860 return array;
861 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700862
Andreas Huber20dce082016-09-22 19:39:13 -0700863private:
864 T mBuffer[SIZE1];
865};
866
Martijn Coenen72110162016-08-19 14:28:25 +0200867// ----------------------------------------------------------------------
868// Version functions
869struct hidl_version {
870public:
Steven Moreland5cc16912019-04-18 12:54:56 -0700871 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {
872 static_assert(sizeof(*this) == 4, "wrong size");
873 }
Martijn Coenen72110162016-08-19 14:28:25 +0200874
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700875 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200876 return (mMajor == other.get_major() && mMinor == other.get_minor());
877 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200878
Steven Morelandb328f012018-06-25 16:07:22 -0700879 bool operator!=(const hidl_version& other) const {
880 return !(*this == other);
881 }
882
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800883 bool operator<(const hidl_version& other) const {
884 return (mMajor < other.get_major() ||
885 (mMajor == other.get_major() && mMinor < other.get_minor()));
886 }
887
888 bool operator>(const hidl_version& other) const {
889 return other < *this;
890 }
891
892 bool operator<=(const hidl_version& other) const {
893 return !(*this > other);
894 }
895
896 bool operator>=(const hidl_version& other) const {
897 return !(*this < other);
898 }
899
Martijn Coenen097a7672016-09-08 16:56:41 +0200900 constexpr uint16_t get_major() const { return mMajor; }
901 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200902
Martijn Coenen72110162016-08-19 14:28:25 +0200903private:
904 uint16_t mMajor;
905 uint16_t mMinor;
906};
907
908inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
909 return hidl_version(major,minor);
910}
911
Yifan Hongbe7a6882017-01-05 17:30:17 -0800912///////////////////// toString functions
913
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800914std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800915
916// toString alias for numeric types
917template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
918inline std::string toString(T t) {
919 return std::to_string(t);
920}
921
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800922namespace details {
923
Yifan Hongbe7a6882017-01-05 17:30:17 -0800924template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
925inline std::string toHexString(T t, bool prefix = true) {
926 std::ostringstream os;
927 if (prefix) { os << std::showbase; }
928 os << std::hex << t;
929 return os.str();
930}
931
932template<>
933inline std::string toHexString(uint8_t t, bool prefix) {
934 return toHexString(static_cast<int32_t>(t), prefix);
935}
936
937template<>
938inline std::string toHexString(int8_t t, bool prefix) {
939 return toHexString(static_cast<int32_t>(t), prefix);
940}
941
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800942template<typename Array>
943std::string arrayToString(const Array &a, size_t size);
944
945template<size_t SIZE1>
946std::string arraySizeToString() {
947 return std::string{"["} + toString(SIZE1) + "]";
948}
949
950template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
951std::string arraySizeToString() {
952 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
953}
954
955template<typename T, size_t SIZE1>
956std::string toString(details::const_accessor<T, SIZE1> a) {
957 return arrayToString(a, SIZE1);
958}
959
960template<typename Array>
961std::string arrayToString(const Array &a, size_t size) {
962 using android::hardware::toString;
963 std::string os;
964 os += "{";
965 for (size_t i = 0; i < size; ++i) {
966 if (i > 0) {
967 os += ", ";
968 }
969 os += toString(a[i]);
970 }
971 os += "}";
972 return os;
973}
974
975template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
976std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
977 return arrayToString(a, SIZE1);
978}
979
980} //namespace details
981
982inline std::string toString(const void *t) {
983 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800984}
985
986// debug string dump. There will be quotes around the string!
987inline std::string toString(const hidl_string &hs) {
988 return std::string{"\""} + hs.c_str() + "\"";
989}
990
991// debug string dump
992inline std::string toString(const hidl_handle &hs) {
993 return toString(hs.getNativeHandle());
994}
995
996inline std::string toString(const hidl_memory &mem) {
997 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
998 + toString(mem.size())
999 + ", .handle = " + toString(mem.handle()) + "}";
1000}
1001
1002inline std::string toString(const sp<hidl_death_recipient> &dr) {
1003 return std::string{"death_recipient@"} + toString(dr.get());
1004}
1005
Yifan Hongbe7a6882017-01-05 17:30:17 -08001006// debug string dump, assuming that toString(T) is defined.
1007template<typename T>
1008std::string toString(const hidl_vec<T> &a) {
1009 std::string os;
1010 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -08001011 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -08001012 return os;
1013}
1014
Yifan Hongbe7a6882017-01-05 17:30:17 -08001015template<typename T, size_t SIZE1>
1016std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -08001017 return details::arraySizeToString<SIZE1>()
1018 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -08001019}
1020
1021template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
1022std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -08001023 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
1024 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -08001025}
1026
Steven Moreland39d9f882018-10-26 15:34:01 -07001027namespace details {
1028// Never instantiated. Used as a placeholder for template variables.
1029template <typename T>
1030struct hidl_invalid_type;
1031
1032// HIDL generates specializations of this for enums. See hidl_enum_range.
1033template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
1034constexpr hidl_invalid_type<T> hidl_enum_values;
1035} // namespace details
1036
Steven Morelandabce0c72017-11-17 15:37:09 -08001037/**
Steven Moreland39d9f882018-10-26 15:34:01 -07001038 * Every HIDL generated enum supports this function.
Steven Moreland5f3a8cf2018-04-25 11:57:35 -07001039 * E.x.: for(const auto v : hidl_enum_range<Enum>) { ... }
Steven Morelandabce0c72017-11-17 15:37:09 -08001040 */
Steven Moreland2f2c9632018-05-01 12:24:14 -07001041template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
Steven Moreland39d9f882018-10-26 15:34:01 -07001042struct hidl_enum_range {
1043 constexpr auto begin() const { return std::begin(details::hidl_enum_values<T>); }
1044 constexpr auto cbegin() const { return begin(); }
1045 constexpr auto rbegin() const { return std::rbegin(details::hidl_enum_values<T>); }
1046 constexpr auto crbegin() const { return rbegin(); }
1047 constexpr auto end() const { return std::end(details::hidl_enum_values<T>); }
1048 constexpr auto cend() const { return end(); }
1049 constexpr auto rend() const { return std::rend(details::hidl_enum_values<T>); }
1050 constexpr auto crend() const { return rend(); }
1051};
Steven Moreland5f3a8cf2018-04-25 11:57:35 -07001052
Steven Moreland2f2c9632018-05-01 12:24:14 -07001053template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
1054struct hidl_enum_iterator {
1055 static_assert(!std::is_enum<T>::value,
1056 "b/78573628: hidl_enum_iterator was renamed to hidl_enum_range because it is not "
1057 "actually an iterator. Please use that type instead.");
1058};
Steven Morelandabce0c72017-11-17 15:37:09 -08001059
Steven Morelanda6752122017-12-28 13:35:20 -08001060/**
1061 * Bitfields in HIDL are the underlying type of the enumeration.
1062 */
1063template <typename Enum>
1064using hidl_bitfield = typename std::underlying_type<Enum>::type;
1065
Martijn Coenen72110162016-08-19 14:28:25 +02001066} // namespace hardware
1067} // namespace android
1068
Martijn Coenenc28f1152016-08-22 14:06:56 +02001069
Martijn Coenen72110162016-08-19 14:28:25 +02001070#endif // ANDROID_HIDL_SUPPORT_H