blob: 71448dac3d13153a3ea392f235f1d51665108c2c [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>
Steven Moreland337c3ae2016-11-22 13:37:32 -080023#include <hidl/HidlInternal.h>
Yifan Hong1e265bb2016-11-08 12:33:44 -080024#include <map>
Yifan Hongbe7a6882017-01-05 17:30:17 -080025#include <sstream>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080026#include <stddef.h>
Andreas Huber00a985c2016-09-28 14:24:53 -070027#include <tuple>
Yifan Hong64fdf4d2016-12-09 16:22:45 -080028#include <type_traits>
Steven Morelandf0dd1602019-04-26 14:51:24 -070029#include <vector>
30
31// no requirements on types not used in scatter/gather
32// no requirements on other libraries
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wpadded"
35#include <cutils/native_handle.h>
36#include <hidl/Status.h>
Iliyan Malchev692070a2016-09-12 16:30:44 -070037#include <utils/Errors.h>
38#include <utils/RefBase.h>
39#include <utils/StrongPointer.h>
Steven Morelandf0dd1602019-04-26 14:51:24 -070040#pragma clang diagnostic pop
Martijn Coenen72110162016-08-19 14:28:25 +020041
42namespace android {
Martijn Coenen30791002016-12-01 15:40:46 +010043
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010044// this file is included by all hidl interface, so we must forward declare the
45// IMemory and IBase types.
Martijn Coenen30791002016-12-01 15:40:46 +010046namespace hidl {
47namespace memory {
48namespace V1_0 {
Pirama Arumuga Nainared4c5d02018-04-10 15:46:23 -070049
50struct IMemory;
51
52} // namespace V1_0
53} // namespace memory
54} // namespace hidl
Martijn Coenen30791002016-12-01 15:40:46 +010055
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010056namespace hidl {
57namespace base {
58namespace V1_0 {
Pirama Arumuga Nainared4c5d02018-04-10 15:46:23 -070059
60struct IBase;
61
62} // namespace V1_0
63} // namespace base
64} // namespace hidl
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010065
Martijn Coenen72110162016-08-19 14:28:25 +020066namespace hardware {
67
Yifan Hong24332ef2017-03-07 16:22:19 -080068namespace details {
69// Return true on userdebug / eng builds and false on user builds.
70bool debuggable();
71} // namespace details
72
Martijn Coenen9b8f9c32016-12-09 15:51:06 +010073// hidl_death_recipient is a callback interfaced that can be used with
74// linkToDeath() / unlinkToDeath()
75struct hidl_death_recipient : public virtual RefBase {
76 virtual void serviceDied(uint64_t cookie,
77 const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0;
78};
79
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010080// hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer,
81// so that it can safely be transferred between 32-bit and 64-bit processes.
Martijn Coenen04b91c02017-01-19 14:14:21 +010082// The ownership semantics for this are:
83// 1) The conversion constructor and assignment operator taking a const native_handle_t*
84// do not take ownership of the handle; this is because these operations are usually
85// just done for IPC, and cloning by default is a waste of resources. If you want
86// a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/);
87// 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership;
88// that is because it's not intuitive that this class encapsulates a native_handle_t
89// which needs cloning to be valid; in particular, this allows constructs like this:
90// hidl_handle copy;
91// foo->someHidlCall([&](auto incoming_handle) {
92// copy = incoming_handle;
93// });
94// // copy and its enclosed file descriptors will remain valid here.
95// 3) The move constructor does what you would expect; it only owns the handle if the
96// original did.
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +010097struct hidl_handle {
Martijn Coenen04b91c02017-01-19 14:14:21 +010098 hidl_handle();
99 ~hidl_handle();
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100100
Martijn Coenen04b91c02017-01-19 14:14:21 +0100101 hidl_handle(const native_handle_t *handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100102
Martijn Coenen04b91c02017-01-19 14:14:21 +0100103 // copy constructor.
104 hidl_handle(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100105
106 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700107 hidl_handle(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100108
Steven Moreland6ffdc2a2017-01-23 12:34:33 -0800109 // assignment operators
Martijn Coenen04b91c02017-01-19 14:14:21 +0100110 hidl_handle &operator=(const hidl_handle &other);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100111
Martijn Coenen04b91c02017-01-19 14:14:21 +0100112 hidl_handle &operator=(const native_handle_t *native_handle);
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100113
Martijn Coenene8c36e12017-05-30 16:25:07 -0700114 hidl_handle &operator=(hidl_handle &&other) noexcept;
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100115
Martijn Coenen04b91c02017-01-19 14:14:21 +0100116 void setTo(native_handle_t* handle, bool shouldOwn = false);
117
118 const native_handle_t* operator->() const;
119
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100120 // implicit conversion to const native_handle_t*
Martijn Coenen04b91c02017-01-19 14:14:21 +0100121 operator const native_handle_t *() const;
122
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100123 // explicit conversion
Martijn Coenen04b91c02017-01-19 14:14:21 +0100124 const native_handle_t *getNativeHandle() const;
Nirav Atre564a8d22018-07-26 18:29:12 -0700125
126 // offsetof(hidl_handle, mHandle) exposed since mHandle is private.
127 static const size_t kOffsetOfNativeHandle;
128
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100129private:
Martijn Coenen04b91c02017-01-19 14:14:21 +0100130 void freeHandle();
131
Steven Morelandf0dd1602019-04-26 14:51:24 -0700132 details::hidl_pointer<const native_handle_t> mHandle;
133 bool mOwnsHandle;
134 uint8_t mPad[7];
Martijn Coenen4d1e9cc2016-11-18 15:20:18 +0100135};
136
Martijn Coenen72110162016-08-19 14:28:25 +0200137struct hidl_string {
138 hidl_string();
139 ~hidl_string();
140
Yifan Hong602b85a2016-10-24 13:40:01 -0700141 // copy constructor.
Martijn Coenen72110162016-08-19 14:28:25 +0200142 hidl_string(const hidl_string &);
Steven Morelanda21d84f2017-02-16 09:23:45 -0800143 // copy from a C-style string. nullptr will create an empty string
Steven Morelande03c0872016-10-24 10:43:50 -0700144 hidl_string(const char *);
Steven Moreland53120f72017-01-12 09:39:26 -0800145 // copy the first length characters from a C-style string.
146 hidl_string(const char *, size_t length);
Yifan Hong602b85a2016-10-24 13:40:01 -0700147 // copy from an std::string.
148 hidl_string(const std::string &);
149
150 // move constructor.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700151 hidl_string(hidl_string &&) noexcept;
Martijn Coenen72110162016-08-19 14:28:25 +0200152
153 const char *c_str() const;
154 size_t size() const;
155 bool empty() const;
156
Yifan Hong602b85a2016-10-24 13:40:01 -0700157 // copy assignment operator.
Steven Morelande03c0872016-10-24 10:43:50 -0700158 hidl_string &operator=(const hidl_string &);
Yifan Hong602b85a2016-10-24 13:40:01 -0700159 // copy from a C-style string.
Martijn Coenen72110162016-08-19 14:28:25 +0200160 hidl_string &operator=(const char *s);
Yifan Hong602b85a2016-10-24 13:40:01 -0700161 // copy from an std::string.
162 hidl_string &operator=(const std::string &);
163 // move assignment operator.
Martijn Coenene8c36e12017-05-30 16:25:07 -0700164 hidl_string &operator=(hidl_string &&other) noexcept;
Yifan Hong602b85a2016-10-24 13:40:01 -0700165 // cast to std::string.
166 operator std::string() const;
Steven Morelande03c0872016-10-24 10:43:50 -0700167
Martijn Coenen72110162016-08-19 14:28:25 +0200168 void clear();
169
170 // Reference an external char array. Ownership is _not_ transferred.
171 // Caller is responsible for ensuring that underlying memory is valid
172 // for the lifetime of this hidl_string.
Steven Moreland57c6fcb2018-03-06 11:31:33 -0800173 //
174 // size == strlen(data)
Martijn Coenen72110162016-08-19 14:28:25 +0200175 void setToExternal(const char *data, size_t size);
176
Andreas Huberebfeb362016-08-25 13:39:05 -0700177 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
178 static const size_t kOffsetOfBuffer;
179
Martijn Coenen72110162016-08-19 14:28:25 +0200180private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100181 details::hidl_pointer<const char> mBuffer;
182 uint32_t mSize; // NOT including the terminating '\0'.
Yifan Hong602b85a2016-10-24 13:40:01 -0700183 bool mOwnsBuffer; // if true then mBuffer is a mutable char *
Steven Morelandf0dd1602019-04-26 14:51:24 -0700184 uint8_t mPad[3];
Martijn Coenen72110162016-08-19 14:28:25 +0200185
Yifan Hong602b85a2016-10-24 13:40:01 -0700186 // copy from data with size. Assume that my memory is freed
187 // (through clear(), for example)
188 void copyFrom(const char *data, size_t size);
189 // move from another hidl_string
190 void moveFrom(hidl_string &&);
Martijn Coenen72110162016-08-19 14:28:25 +0200191};
192
Chih-Hung Hsiehb30feca2017-08-01 15:40:48 -0700193// Use NOLINT to suppress missing parentheses warnings around OP.
Janis Danisevskis62a38df2017-11-22 13:47:33 -0800194#define HIDL_STRING_OPERATOR(OP) \
195 inline bool operator OP(const hidl_string& hs1, const hidl_string& hs2) { \
196 return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \
197 } \
198 inline bool operator OP(const hidl_string& hs, const char* s) { \
199 return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \
200 } \
201 inline bool operator OP(const char* s, const hidl_string& hs) { \
202 return strcmp(s, hs.c_str()) OP 0; /* NOLINT */ \
Steven Moreland551396a2017-02-13 18:33:20 -0800203 }
Scott Randolphbb840f72016-11-21 14:39:26 -0800204
Steven Moreland551396a2017-02-13 18:33:20 -0800205HIDL_STRING_OPERATOR(==)
206HIDL_STRING_OPERATOR(!=)
207HIDL_STRING_OPERATOR(<)
208HIDL_STRING_OPERATOR(<=)
209HIDL_STRING_OPERATOR(>)
210HIDL_STRING_OPERATOR(>=)
Scott Randolphbb840f72016-11-21 14:39:26 -0800211
Steven Moreland551396a2017-02-13 18:33:20 -0800212#undef HIDL_STRING_OPERATOR
Yifan Hong5708fb42016-10-26 17:50:29 -0700213
Scott Randolph0c84ab42017-04-03 14:07:14 -0700214// Send our content to the output stream
215std::ostream& operator<<(std::ostream& os, const hidl_string& str);
216
217
Martijn Coenen30791002016-12-01 15:40:46 +0100218// hidl_memory is a structure that can be used to transfer
219// pieces of shared memory between processes. The assumption
220// of this object is that the memory remains accessible as
221// long as the file descriptors in the enclosed mHandle
222// - as well as all of its cross-process dups() - remain opened.
223struct hidl_memory {
224
Martijn Coenen04b91c02017-01-19 14:14:21 +0100225 hidl_memory() : mHandle(nullptr), mSize(0), mName("") {
Martijn Coenen30791002016-12-01 15:40:46 +0100226 }
227
228 /**
Steven Moreland3f8c4162017-10-11 13:47:00 -0700229 * Creates a hidl_memory object whose handle has the same lifetime
230 * as the handle moved into it.
231 */
232 hidl_memory(const hidl_string& name, hidl_handle&& handle, size_t size)
233 : mHandle(std::move(handle)), mSize(size), mName(name) {}
234
235 /**
Martijn Coenen04b91c02017-01-19 14:14:21 +0100236 * Creates a hidl_memory object, but doesn't take ownership of
237 * the passed in native_handle_t; callers are responsible for
238 * making sure the handle remains valid while this object is
239 * used.
Martijn Coenen30791002016-12-01 15:40:46 +0100240 */
Martijn Coenen04b91c02017-01-19 14:14:21 +0100241 hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size)
242 : mHandle(handle),
Martijn Coenen30791002016-12-01 15:40:46 +0100243 mSize(size),
244 mName(name)
245 {}
246
247 // copy constructor
248 hidl_memory(const hidl_memory& other) {
249 *this = other;
250 }
251
252 // copy assignment
253 hidl_memory &operator=(const hidl_memory &other) {
254 if (this != &other) {
Martijn Coenen04b91c02017-01-19 14:14:21 +0100255 mHandle = other.mHandle;
Martijn Coenen30791002016-12-01 15:40:46 +0100256 mSize = other.mSize;
257 mName = other.mName;
258 }
259
260 return *this;
261 }
262
Hridya Valsaraju01268892017-02-27 08:48:38 -0800263 // move constructor
Martijn Coenene8c36e12017-05-30 16:25:07 -0700264 hidl_memory(hidl_memory&& other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800265 *this = std::move(other);
266 }
267
268 // move assignment
Martijn Coenene8c36e12017-05-30 16:25:07 -0700269 hidl_memory &operator=(hidl_memory &&other) noexcept {
Hridya Valsaraju01268892017-02-27 08:48:38 -0800270 if (this != &other) {
271 mHandle = std::move(other.mHandle);
272 mSize = other.mSize;
273 mName = std::move(other.mName);
274 other.mSize = 0;
275 }
276
277 return *this;
278 }
279
Martijn Coenen30791002016-12-01 15:40:46 +0100280
281 ~hidl_memory() {
Martijn Coenen30791002016-12-01 15:40:46 +0100282 }
283
284 const native_handle_t* handle() const {
285 return mHandle;
286 }
287
288 const hidl_string &name() const {
289 return mName;
290 }
291
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800292 uint64_t size() const {
Martijn Coenen30791002016-12-01 15:40:46 +0100293 return mSize;
294 }
295
Howard Chen9bc35c42017-10-13 14:21:46 +0800296 // @return true if it's valid
297 inline bool valid() const { return handle() != nullptr; }
298
Martijn Coenen30791002016-12-01 15:40:46 +0100299 // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
300 static const size_t kOffsetOfHandle;
301 // offsetof(hidl_memory, mName) exposed since mHandle is private.
302 static const size_t kOffsetOfName;
Jeff Tinker0f3461d2017-01-03 10:40:55 -0800303
Martijn Coenen30791002016-12-01 15:40:46 +0100304private:
Steven Morelandf0dd1602019-04-26 14:51:24 -0700305 hidl_handle mHandle;
306 uint64_t mSize;
307 hidl_string mName;
Martijn Coenen30791002016-12-01 15:40:46 +0100308};
309
Howard Chen9bc35c42017-10-13 14:21:46 +0800310// HidlMemory is a wrapper class to support sp<> for hidl_memory. It also
311// provides factory methods to create an instance from hidl_memory or
312// from a opened file descriptor. The number of factory methods can be increase
313// to support other type of hidl_memory without break the ABI.
314class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase {
315public:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800316 static sp<HidlMemory> getInstance(const hidl_memory& mem);
317
Howard Chen9bc35c42017-10-13 14:21:46 +0800318 static sp<HidlMemory> getInstance(hidl_memory&& mem);
319
320 static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size);
321 // @param fd, shall be opened and points to the resource.
322 // @note this method takes the ownership of the fd and will close it in
323 // destructor
Howard Chen8e42f5a2017-11-09 13:58:28 +0800324 // @return nullptr in failure with the fd closed
Howard Chen9bc35c42017-10-13 14:21:46 +0800325 static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
326
Howard Chen280af0a2017-10-20 13:58:36 +0800327 virtual ~HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800328
Howard Chen9bc35c42017-10-13 14:21:46 +0800329protected:
Howard Chen8e42f5a2017-11-09 13:58:28 +0800330 HidlMemory();
331 HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size);
Howard Chen9bc35c42017-10-13 14:21:46 +0800332};
Andreas Huber20dce082016-09-22 19:39:13 -0700333////////////////////////////////////////////////////////////////////////////////
334
Martijn Coenen72110162016-08-19 14:28:25 +0200335template<typename T>
Hridya Valsarajub7370302017-03-08 14:39:23 -0800336struct hidl_vec {
Steven Morelandb37de4a2019-05-06 09:39:47 -0700337 using value_type = T;
338
Steven Morelandf0dd1602019-04-26 14:51:24 -0700339 hidl_vec() : mBuffer(nullptr), mSize(0), mOwnsBuffer(true) {
Hridya Valsaraju6d4acb12017-03-03 14:24:43 -0800340 static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset");
Steven Moreland5cc16912019-04-18 12:54:56 -0700341
Steven Morelandf0dd1602019-04-26 14:51:24 -0700342 // mOwnsBuffer true to match original implementation
Steven Moreland5cc16912019-04-18 12:54:56 -0700343
Steven Morelandf0dd1602019-04-26 14:51:24 -0700344 memset(mPad, 0, sizeof(mPad));
Martijn Coenen72110162016-08-19 14:28:25 +0200345 }
346
Steven Morelandaa661fc2017-10-06 09:40:18 -0700347 // Note, does not initialize primitive types.
Eran Messeri6329c3e2019-02-26 11:34:32 +0000348 hidl_vec(size_t size) : hidl_vec() { resize(size); }
Steven Moreland96e9de02017-09-29 14:11:20 -0700349
Yifan Hong602b85a2016-10-24 13:40:01 -0700350 hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
Martijn Coenen72110162016-08-19 14:28:25 +0200351 *this = other;
352 }
353
Steven Moreland5cc16912019-04-18 12:54:56 -0700354 hidl_vec(hidl_vec<T> &&other) noexcept : hidl_vec() {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100355 *this = std::move(other);
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700356 }
357
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800358 hidl_vec(const std::initializer_list<T> list) : hidl_vec() { *this = list; }
Steven Moreland9fbfe472016-11-14 16:49:17 -0800359
Yifan Hong602b85a2016-10-24 13:40:01 -0700360 hidl_vec(const std::vector<T> &other) : hidl_vec() {
361 *this = other;
362 }
363
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700364 template <typename InputIterator,
365 typename = typename std::enable_if<std::is_convertible<
366 typename std::iterator_traits<InputIterator>::iterator_category,
367 std::input_iterator_tag>::value>::type>
Steven Moreland5cc16912019-04-18 12:54:56 -0700368 hidl_vec(InputIterator first, InputIterator last) : hidl_vec() {
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700369 auto size = std::distance(first, last);
370 if (size > static_cast<int64_t>(UINT32_MAX)) {
371 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
372 }
373 if (size < 0) {
374 details::logAlwaysFatal("size can't be negative.");
375 }
376 mSize = static_cast<uint32_t>(size);
Steven Moreland97e2d172019-04-30 17:56:45 -0700377 mBuffer = new T[mSize]();
Steven Moreland5cc16912019-04-18 12:54:56 -0700378 mOwnsBuffer = true;
Tomasz Wasilczyka9f60732017-06-30 10:53:22 -0700379
380 size_t idx = 0;
381 for (; first != last; ++first) {
382 mBuffer[idx++] = static_cast<T>(*first);
383 }
384 }
385
Martijn Coenen72110162016-08-19 14:28:25 +0200386 ~hidl_vec() {
387 if (mOwnsBuffer) {
388 delete[] mBuffer;
389 }
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700390 mBuffer = nullptr;
Martijn Coenen72110162016-08-19 14:28:25 +0200391 }
392
Alexey Polyudove2299012016-10-19 09:52:00 -0700393 // Reference an existing array, optionally taking ownership. It is the
Martijn Coenen72110162016-08-19 14:28:25 +0200394 // caller's responsibility to ensure that the underlying memory stays
395 // valid for the lifetime of this hidl_vec.
Alexey Polyudove2299012016-10-19 09:52:00 -0700396 void setToExternal(T *data, size_t size, bool shouldOwn = false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200397 if (mOwnsBuffer) {
398 delete [] mBuffer;
399 }
400 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100401 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800402 details::logAlwaysFatal("external vector size exceeds 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100403 }
404 mSize = static_cast<uint32_t>(size);
Alexey Polyudove2299012016-10-19 09:52:00 -0700405 mOwnsBuffer = shouldOwn;
Martijn Coenen72110162016-08-19 14:28:25 +0200406 }
407
Alexey Polyudov6f0c9a12016-10-18 09:37:35 -0700408 T *data() {
409 return mBuffer;
410 }
411
412 const T *data() const {
413 return mBuffer;
414 }
415
Alexey Polyudovc98a99c2016-10-18 09:43:56 -0700416 T *releaseData() {
417 if (!mOwnsBuffer && mSize > 0) {
418 resize(mSize);
419 }
420 mOwnsBuffer = false;
421 return mBuffer;
422 }
423
Martijn Coenene8c36e12017-05-30 16:25:07 -0700424 hidl_vec &operator=(hidl_vec &&other) noexcept {
Janis Danisevskisd3ddf622016-10-24 11:40:50 +0100425 if (mOwnsBuffer) {
426 delete[] mBuffer;
427 }
Alexey Polyudov0ebdbe82016-10-18 09:31:46 -0700428 mBuffer = other.mBuffer;
429 mSize = other.mSize;
430 mOwnsBuffer = other.mOwnsBuffer;
431 other.mOwnsBuffer = false;
432 return *this;
433 }
434
Martijn Coenen72110162016-08-19 14:28:25 +0200435 hidl_vec &operator=(const hidl_vec &other) {
436 if (this != &other) {
437 if (mOwnsBuffer) {
438 delete[] mBuffer;
439 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700440 copyFrom(other, other.mSize);
Martijn Coenen72110162016-08-19 14:28:25 +0200441 }
442
443 return *this;
444 }
445
Yifan Hong602b85a2016-10-24 13:40:01 -0700446 // copy from an std::vector.
447 hidl_vec &operator=(const std::vector<T> &other) {
448 if (mOwnsBuffer) {
449 delete[] mBuffer;
450 }
451 copyFrom(other, other.size());
452 return *this;
453 }
454
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800455 hidl_vec& operator=(const std::initializer_list<T> list) {
456 if (list.size() > UINT32_MAX) {
457 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
458 }
459 if (mOwnsBuffer) {
460 delete[] mBuffer;
461 }
462 mSize = static_cast<uint32_t>(list.size());
Steven Moreland97e2d172019-04-30 17:56:45 -0700463 mBuffer = new T[mSize]();
Siarhei Vishniakouc48d4f62019-02-08 12:48:04 -0800464 mOwnsBuffer = true;
465
466 size_t idx = 0;
467 for (auto it = list.begin(); it != list.end(); ++it) {
468 mBuffer[idx++] = *it;
469 }
470 return *this;
471 }
472
Yifan Hong602b85a2016-10-24 13:40:01 -0700473 // cast to an std::vector.
474 operator std::vector<T>() const {
475 std::vector<T> v(mSize);
476 for (size_t i = 0; i < mSize; ++i) {
477 v[i] = mBuffer[i];
478 }
479 return v;
480 }
481
Yifan Hong9fcbb362016-12-20 16:46:41 -0800482 // equality check, assuming that T::operator== is defined.
483 bool operator==(const hidl_vec &other) const {
484 if (mSize != other.size()) {
485 return false;
486 }
487 for (size_t i = 0; i < mSize; ++i) {
488 if (!(mBuffer[i] == other.mBuffer[i])) {
489 return false;
490 }
491 }
492 return true;
493 }
494
495 // inequality check, assuming that T::operator== is defined.
496 inline bool operator!=(const hidl_vec &other) const {
497 return !((*this) == other);
498 }
499
Martijn Coenen72110162016-08-19 14:28:25 +0200500 size_t size() const {
501 return mSize;
502 }
503
504 T &operator[](size_t index) {
505 return mBuffer[index];
506 }
507
508 const T &operator[](size_t index) const {
509 return mBuffer[index];
510 }
511
Steven Morelandaa661fc2017-10-06 09:40:18 -0700512 // Does not initialize primitive types if new size > old size.
Martijn Coenen72110162016-08-19 14:28:25 +0200513 void resize(size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100514 if (size > UINT32_MAX) {
Hridya Valsarajub7370302017-03-08 14:39:23 -0800515 details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100516 }
Steven Moreland97e2d172019-04-30 17:56:45 -0700517 T* newBuffer = new T[size]();
Martijn Coenen72110162016-08-19 14:28:25 +0200518
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100519 for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) {
Steven Morelanddfbedb82019-06-19 14:51:11 -0700520 newBuffer[i] = std::move(mBuffer[i]);
Martijn Coenen72110162016-08-19 14:28:25 +0200521 }
522
523 if (mOwnsBuffer) {
524 delete[] mBuffer;
525 }
526 mBuffer = newBuffer;
527
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100528 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200529 mOwnsBuffer = true;
530 }
531
Yifan Hong089ae132016-11-11 11:32:52 -0800532 // offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
533 static const size_t kOffsetOfBuffer;
Scott Randolphbb840f72016-11-21 14:39:26 -0800534
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800535private:
Scott Randolphbb840f72016-11-21 14:39:26 -0800536 // Define std interator interface for walking the array contents
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800537 template<bool is_const>
538 class iter : public std::iterator<
539 std::random_access_iterator_tag, /* Category */
540 T,
541 ptrdiff_t, /* Distance */
542 typename std::conditional<is_const, const T *, T *>::type /* Pointer */,
543 typename std::conditional<is_const, const T &, T &>::type /* Reference */>
Scott Randolphbb840f72016-11-21 14:39:26 -0800544 {
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800545 using traits = std::iterator_traits<iter>;
546 using ptr_type = typename traits::pointer;
547 using ref_type = typename traits::reference;
548 using diff_type = typename traits::difference_type;
Scott Randolphbb840f72016-11-21 14:39:26 -0800549 public:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800550 iter(ptr_type ptr) : mPtr(ptr) { }
551 inline iter &operator++() { mPtr++; return *this; }
552 inline iter operator++(int) { iter i = *this; mPtr++; return i; }
553 inline iter &operator--() { mPtr--; return *this; }
554 inline iter operator--(int) { iter i = *this; mPtr--; return i; }
555 inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; }
556 inline iter operator+(diff_type n) const { return mPtr + n; }
557 inline iter operator-(diff_type n) const { return mPtr - n; }
558 inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; }
559 inline iter &operator+=(diff_type n) { mPtr += n; return *this; }
560 inline iter &operator-=(diff_type n) { mPtr -= n; return *this; }
561 inline ref_type operator*() const { return *mPtr; }
562 inline ptr_type operator->() const { return mPtr; }
563 inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; }
564 inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; }
565 inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; }
566 inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; }
567 inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; }
568 inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; }
569 inline ref_type operator[](size_t n) const { return mPtr[n]; }
Scott Randolphbb840f72016-11-21 14:39:26 -0800570 private:
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800571 ptr_type mPtr;
Scott Randolphbb840f72016-11-21 14:39:26 -0800572 };
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800573public:
574 using iterator = iter<false /* is_const */>;
575 using const_iterator = iter<true /* is_const */>;
576
Scott Randolphbb840f72016-11-21 14:39:26 -0800577 iterator begin() { return data(); }
578 iterator end() { return data()+mSize; }
Yifan Hong64fdf4d2016-12-09 16:22:45 -0800579 const_iterator begin() const { return data(); }
580 const_iterator end() const { return data()+mSize; }
Tomasz Wasilczyk93f75922019-07-16 08:44:15 -0700581 iterator find(const T& v) { return std::find(begin(), end(), v); }
582 const_iterator find(const T& v) const { return std::find(begin(), end(), v); }
583 bool contains(const T& v) const { return find(v) != end(); }
Scott Randolphbb840f72016-11-21 14:39:26 -0800584
Tomasz Wasilczyk93f75922019-07-16 08:44:15 -0700585 private:
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100586 details::hidl_pointer<T> mBuffer;
587 uint32_t mSize;
Martijn Coenen72110162016-08-19 14:28:25 +0200588 bool mOwnsBuffer;
Steven Morelandf0dd1602019-04-26 14:51:24 -0700589 uint8_t mPad[3];
Yifan Hong602b85a2016-10-24 13:40:01 -0700590
591 // copy from an array-like object, assuming my resources are freed.
592 template <typename Array>
593 void copyFrom(const Array &data, size_t size) {
Chia-I Wu4b48edc2016-12-07 22:31:17 +0800594 mSize = static_cast<uint32_t>(size);
Yifan Hong602b85a2016-10-24 13:40:01 -0700595 mOwnsBuffer = true;
596 if (mSize > 0) {
Steven Moreland97e2d172019-04-30 17:56:45 -0700597 mBuffer = new T[size]();
Yifan Hong602b85a2016-10-24 13:40:01 -0700598 for (size_t i = 0; i < size; ++i) {
599 mBuffer[i] = data[i];
600 }
601 } else {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700602 mBuffer = nullptr;
Yifan Hong602b85a2016-10-24 13:40:01 -0700603 }
604 }
Martijn Coenen72110162016-08-19 14:28:25 +0200605};
606
Yifan Hong089ae132016-11-11 11:32:52 -0800607template <typename T>
608const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer);
609
Andreas Huber20dce082016-09-22 19:39:13 -0700610////////////////////////////////////////////////////////////////////////////////
611
612namespace details {
613
614 template<size_t SIZE1, size_t... SIZES>
615 struct product {
616 static constexpr size_t value = SIZE1 * product<SIZES...>::value;
617 };
618
619 template<size_t SIZE1>
620 struct product<SIZE1> {
621 static constexpr size_t value = SIZE1;
622 };
623
624 template<typename T, size_t SIZE1, size_t... SIZES>
Yifan Hong44ab6232016-11-22 16:28:24 -0800625 struct std_array {
626 using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>;
627 };
628
629 template<typename T, size_t SIZE1>
630 struct std_array<T, SIZE1> {
631 using type = std::array<T, SIZE1>;
632 };
633
634 template<typename T, size_t SIZE1, size_t... SIZES>
Andreas Huber20dce082016-09-22 19:39:13 -0700635 struct accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800636
637 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
638
Andreas Huber20dce082016-09-22 19:39:13 -0700639 explicit accessor(T *base)
640 : mBase(base) {
641 }
642
643 accessor<T, SIZES...> operator[](size_t index) {
644 return accessor<T, SIZES...>(
645 &mBase[index * product<SIZES...>::value]);
646 }
647
Yifan Hong44ab6232016-11-22 16:28:24 -0800648 accessor &operator=(const std_array_type &other) {
649 for (size_t i = 0; i < SIZE1; ++i) {
650 (*this)[i] = other[i];
651 }
652 return *this;
653 }
654
Andreas Huber20dce082016-09-22 19:39:13 -0700655 private:
656 T *mBase;
657 };
658
659 template<typename T, size_t SIZE1>
660 struct accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800661
662 using std_array_type = typename std_array<T, SIZE1>::type;
663
Andreas Huber20dce082016-09-22 19:39:13 -0700664 explicit accessor(T *base)
665 : mBase(base) {
666 }
667
668 T &operator[](size_t index) {
669 return mBase[index];
670 }
671
Yifan Hong44ab6232016-11-22 16:28:24 -0800672 accessor &operator=(const std_array_type &other) {
673 for (size_t i = 0; i < SIZE1; ++i) {
674 (*this)[i] = other[i];
675 }
676 return *this;
677 }
678
Andreas Huber20dce082016-09-22 19:39:13 -0700679 private:
680 T *mBase;
681 };
682
683 template<typename T, size_t SIZE1, size_t... SIZES>
684 struct const_accessor {
Yifan Hong44ab6232016-11-22 16:28:24 -0800685
686 using std_array_type = typename std_array<T, SIZE1, SIZES...>::type;
687
Andreas Huber20dce082016-09-22 19:39:13 -0700688 explicit const_accessor(const T *base)
689 : mBase(base) {
690 }
691
Yifan Hongbe7a6882017-01-05 17:30:17 -0800692 const_accessor<T, SIZES...> operator[](size_t index) const {
Andreas Huber20dce082016-09-22 19:39:13 -0700693 return const_accessor<T, SIZES...>(
694 &mBase[index * product<SIZES...>::value]);
695 }
696
Yifan Hong44ab6232016-11-22 16:28:24 -0800697 operator std_array_type() {
698 std_array_type array;
699 for (size_t i = 0; i < SIZE1; ++i) {
700 array[i] = (*this)[i];
701 }
702 return array;
703 }
704
Andreas Huber20dce082016-09-22 19:39:13 -0700705 private:
706 const T *mBase;
707 };
708
709 template<typename T, size_t SIZE1>
710 struct const_accessor<T, SIZE1> {
Yifan Hong44ab6232016-11-22 16:28:24 -0800711
712 using std_array_type = typename std_array<T, SIZE1>::type;
713
Andreas Huber20dce082016-09-22 19:39:13 -0700714 explicit const_accessor(const T *base)
715 : mBase(base) {
716 }
717
718 const T &operator[](size_t index) const {
719 return mBase[index];
720 }
721
Yifan Hong44ab6232016-11-22 16:28:24 -0800722 operator std_array_type() {
723 std_array_type array;
724 for (size_t i = 0; i < SIZE1; ++i) {
725 array[i] = (*this)[i];
726 }
727 return array;
728 }
729
Andreas Huber20dce082016-09-22 19:39:13 -0700730 private:
731 const T *mBase;
732 };
733
734} // namespace details
735
736////////////////////////////////////////////////////////////////////////////////
737
Yifan Hong44ab6232016-11-22 16:28:24 -0800738// A multidimensional array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700739template<typename T, size_t SIZE1, size_t... SIZES>
740struct hidl_array {
Yifan Hong44ab6232016-11-22 16:28:24 -0800741
742 using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type;
743
Andreas Huber20dce082016-09-22 19:39:13 -0700744 hidl_array() = default;
Steven Morelanddfbedb82019-06-19 14:51:11 -0700745 hidl_array(const hidl_array&) noexcept = default;
746 hidl_array(hidl_array&&) noexcept = default;
Andreas Huber20dce082016-09-22 19:39:13 -0700747
Yifan Hong44ab6232016-11-22 16:28:24 -0800748 // Copies the data from source, using T::operator=(const T &).
749 hidl_array(const T *source) {
750 for (size_t i = 0; i < elementCount(); ++i) {
751 mBuffer[i] = source[i];
752 }
753 }
754
755 // Copies the data from the given std::array, using T::operator=(const T &).
756 hidl_array(const std_array_type &array) {
757 details::accessor<T, SIZE1, SIZES...> modifier(mBuffer);
758 modifier = array;
759 }
760
Steven Morelanddfbedb82019-06-19 14:51:11 -0700761 hidl_array& operator=(const hidl_array&) noexcept = default;
762 hidl_array& operator=(hidl_array&&) noexcept = default;
763
Andreas Huber20dce082016-09-22 19:39:13 -0700764 T *data() { return mBuffer; }
765 const T *data() const { return mBuffer; }
766
767 details::accessor<T, SIZES...> operator[](size_t index) {
768 return details::accessor<T, SIZES...>(
769 &mBuffer[index * details::product<SIZES...>::value]);
770 }
771
772 details::const_accessor<T, SIZES...> operator[](size_t index) const {
773 return details::const_accessor<T, SIZES...>(
774 &mBuffer[index * details::product<SIZES...>::value]);
775 }
776
Yifan Hong9fcbb362016-12-20 16:46:41 -0800777 // equality check, assuming that T::operator== is defined.
778 bool operator==(const hidl_array &other) const {
779 for (size_t i = 0; i < elementCount(); ++i) {
780 if (!(mBuffer[i] == other.mBuffer[i])) {
781 return false;
782 }
783 }
784 return true;
785 }
786
787 inline bool operator!=(const hidl_array &other) const {
788 return !((*this) == other);
789 }
790
Andreas Huber00a985c2016-09-28 14:24:53 -0700791 using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
792
793 static constexpr size_tuple_type size() {
794 return std::make_tuple(SIZE1, SIZES...);
795 }
796
Yifan Hong44ab6232016-11-22 16:28:24 -0800797 static constexpr size_t elementCount() {
798 return details::product<SIZE1, SIZES...>::value;
799 }
800
801 operator std_array_type() const {
802 return details::const_accessor<T, SIZE1, SIZES...>(mBuffer);
803 }
804
Andreas Huber20dce082016-09-22 19:39:13 -0700805private:
Yifan Hong44ab6232016-11-22 16:28:24 -0800806 T mBuffer[elementCount()];
Andreas Huber20dce082016-09-22 19:39:13 -0700807};
808
Yifan Hong44ab6232016-11-22 16:28:24 -0800809// An array of T's. Assumes that T::operator=(const T &) is defined.
Andreas Huber20dce082016-09-22 19:39:13 -0700810template<typename T, size_t SIZE1>
811struct hidl_array<T, SIZE1> {
Steven Morelandb37de4a2019-05-06 09:39:47 -0700812 using value_type = T;
Yifan Hong44ab6232016-11-22 16:28:24 -0800813 using std_array_type = typename details::std_array<T, SIZE1>::type;
814
Andreas Huber20dce082016-09-22 19:39:13 -0700815 hidl_array() = default;
Steven Morelanddfbedb82019-06-19 14:51:11 -0700816 hidl_array(const hidl_array&) noexcept = default;
817 hidl_array(hidl_array&&) noexcept = default;
Yifan Hong44ab6232016-11-22 16:28:24 -0800818
819 // Copies the data from source, using T::operator=(const T &).
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800820 hidl_array(const T *source) {
Yifan Hong44ab6232016-11-22 16:28:24 -0800821 for (size_t i = 0; i < elementCount(); ++i) {
822 mBuffer[i] = source[i];
823 }
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800824 }
Andreas Huber20dce082016-09-22 19:39:13 -0700825
Yifan Hong44ab6232016-11-22 16:28:24 -0800826 // Copies the data from the given std::array, using T::operator=(const T &).
827 hidl_array(const std_array_type &array) : hidl_array(array.data()) {}
828
Steven Morelanddfbedb82019-06-19 14:51:11 -0700829 hidl_array& operator=(const hidl_array&) noexcept = default;
830 hidl_array& operator=(hidl_array&&) noexcept = default;
831
Andreas Huber20dce082016-09-22 19:39:13 -0700832 T *data() { return mBuffer; }
833 const T *data() const { return mBuffer; }
834
835 T &operator[](size_t index) {
836 return mBuffer[index];
837 }
838
839 const T &operator[](size_t index) const {
840 return mBuffer[index];
841 }
842
Yifan Hong9fcbb362016-12-20 16:46:41 -0800843 // equality check, assuming that T::operator== is defined.
844 bool operator==(const hidl_array &other) const {
845 for (size_t i = 0; i < elementCount(); ++i) {
846 if (!(mBuffer[i] == other.mBuffer[i])) {
847 return false;
848 }
849 }
850 return true;
851 }
852
853 inline bool operator!=(const hidl_array &other) const {
854 return !((*this) == other);
855 }
856
Andreas Huber00a985c2016-09-28 14:24:53 -0700857 static constexpr size_t size() { return SIZE1; }
Yifan Hong44ab6232016-11-22 16:28:24 -0800858 static constexpr size_t elementCount() { return SIZE1; }
859
860 // Copies the data to an std::array, using T::operator=(T).
861 operator std_array_type() const {
862 std_array_type array;
863 for (size_t i = 0; i < SIZE1; ++i) {
864 array[i] = mBuffer[i];
865 }
866 return array;
867 }
Andreas Huber00a985c2016-09-28 14:24:53 -0700868
Andreas Huber20dce082016-09-22 19:39:13 -0700869private:
870 T mBuffer[SIZE1];
871};
872
Martijn Coenen72110162016-08-19 14:28:25 +0200873// ----------------------------------------------------------------------
874// Version functions
875struct hidl_version {
876public:
Steven Moreland5cc16912019-04-18 12:54:56 -0700877 constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {
878 static_assert(sizeof(*this) == 4, "wrong size");
879 }
Martijn Coenen72110162016-08-19 14:28:25 +0200880
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700881 bool operator==(const hidl_version& other) const {
Martijn Coenen72110162016-08-19 14:28:25 +0200882 return (mMajor == other.get_major() && mMinor == other.get_minor());
883 }
Martijn Coenenc28f1152016-08-22 14:06:56 +0200884
Steven Morelandb328f012018-06-25 16:07:22 -0700885 bool operator!=(const hidl_version& other) const {
886 return !(*this == other);
887 }
888
Eino-Ville Talvala19f4db52016-12-14 15:19:28 -0800889 bool operator<(const hidl_version& other) const {
890 return (mMajor < other.get_major() ||
891 (mMajor == other.get_major() && mMinor < other.get_minor()));
892 }
893
894 bool operator>(const hidl_version& other) const {
895 return other < *this;
896 }
897
898 bool operator<=(const hidl_version& other) const {
899 return !(*this > other);
900 }
901
902 bool operator>=(const hidl_version& other) const {
903 return !(*this < other);
904 }
905
Martijn Coenen097a7672016-09-08 16:56:41 +0200906 constexpr uint16_t get_major() const { return mMajor; }
907 constexpr uint16_t get_minor() const { return mMinor; }
Martijn Coenen72110162016-08-19 14:28:25 +0200908
Martijn Coenen72110162016-08-19 14:28:25 +0200909private:
910 uint16_t mMajor;
911 uint16_t mMinor;
912};
913
914inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) {
915 return hidl_version(major,minor);
916}
917
Yifan Hongbe7a6882017-01-05 17:30:17 -0800918///////////////////// toString functions
919
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800920std::string toString(const void *t);
Yifan Hongbe7a6882017-01-05 17:30:17 -0800921
922// toString alias for numeric types
923template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
924inline std::string toString(T t) {
925 return std::to_string(t);
926}
927
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800928namespace details {
929
Yifan Hongbe7a6882017-01-05 17:30:17 -0800930template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
931inline std::string toHexString(T t, bool prefix = true) {
932 std::ostringstream os;
933 if (prefix) { os << std::showbase; }
934 os << std::hex << t;
935 return os.str();
936}
937
938template<>
939inline std::string toHexString(uint8_t t, bool prefix) {
940 return toHexString(static_cast<int32_t>(t), prefix);
941}
942
943template<>
944inline std::string toHexString(int8_t t, bool prefix) {
945 return toHexString(static_cast<int32_t>(t), prefix);
946}
947
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -0800948template<typename Array>
949std::string arrayToString(const Array &a, size_t size);
950
951template<size_t SIZE1>
952std::string arraySizeToString() {
953 return std::string{"["} + toString(SIZE1) + "]";
954}
955
956template<size_t SIZE1, size_t SIZE2, size_t... SIZES>
957std::string arraySizeToString() {
958 return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>();
959}
960
961template<typename T, size_t SIZE1>
962std::string toString(details::const_accessor<T, SIZE1> a) {
963 return arrayToString(a, SIZE1);
964}
965
966template<typename Array>
967std::string arrayToString(const Array &a, size_t size) {
968 using android::hardware::toString;
969 std::string os;
970 os += "{";
971 for (size_t i = 0; i < size; ++i) {
972 if (i > 0) {
973 os += ", ";
974 }
975 os += toString(a[i]);
976 }
977 os += "}";
978 return os;
979}
980
981template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
982std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) {
983 return arrayToString(a, SIZE1);
984}
985
986} //namespace details
987
988inline std::string toString(const void *t) {
989 return details::toHexString(reinterpret_cast<uintptr_t>(t));
Yifan Hongbe7a6882017-01-05 17:30:17 -0800990}
991
992// debug string dump. There will be quotes around the string!
993inline std::string toString(const hidl_string &hs) {
994 return std::string{"\""} + hs.c_str() + "\"";
995}
996
997// debug string dump
998inline std::string toString(const hidl_handle &hs) {
999 return toString(hs.getNativeHandle());
1000}
1001
1002inline std::string toString(const hidl_memory &mem) {
1003 return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = "
1004 + toString(mem.size())
1005 + ", .handle = " + toString(mem.handle()) + "}";
1006}
1007
1008inline std::string toString(const sp<hidl_death_recipient> &dr) {
1009 return std::string{"death_recipient@"} + toString(dr.get());
1010}
1011
Yifan Hongbe7a6882017-01-05 17:30:17 -08001012// debug string dump, assuming that toString(T) is defined.
1013template<typename T>
1014std::string toString(const hidl_vec<T> &a) {
1015 std::string os;
1016 os += "[" + toString(a.size()) + "]";
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -08001017 os += details::arrayToString(a, a.size());
Yifan Hongbe7a6882017-01-05 17:30:17 -08001018 return os;
1019}
1020
Yifan Hongbe7a6882017-01-05 17:30:17 -08001021template<typename T, size_t SIZE1>
1022std::string toString(const hidl_array<T, SIZE1> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -08001023 return details::arraySizeToString<SIZE1>()
1024 + details::toString(details::const_accessor<T, SIZE1>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -08001025}
1026
1027template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES>
1028std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) {
Hridya Valsaraju4ff88a32017-03-09 16:01:02 -08001029 return details::arraySizeToString<SIZE1, SIZE2, SIZES...>()
1030 + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data()));
Yifan Hongbe7a6882017-01-05 17:30:17 -08001031}
1032
Steven Moreland39d9f882018-10-26 15:34:01 -07001033namespace details {
1034// Never instantiated. Used as a placeholder for template variables.
1035template <typename T>
1036struct hidl_invalid_type;
1037
1038// HIDL generates specializations of this for enums. See hidl_enum_range.
1039template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
1040constexpr hidl_invalid_type<T> hidl_enum_values;
1041} // namespace details
1042
Steven Morelandabce0c72017-11-17 15:37:09 -08001043/**
Steven Moreland39d9f882018-10-26 15:34:01 -07001044 * Every HIDL generated enum supports this function.
Steven Moreland5f3a8cf2018-04-25 11:57:35 -07001045 * E.x.: for(const auto v : hidl_enum_range<Enum>) { ... }
Steven Morelandabce0c72017-11-17 15:37:09 -08001046 */
Steven Moreland2f2c9632018-05-01 12:24:14 -07001047template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
Steven Moreland39d9f882018-10-26 15:34:01 -07001048struct hidl_enum_range {
1049 constexpr auto begin() const { return std::begin(details::hidl_enum_values<T>); }
1050 constexpr auto cbegin() const { return begin(); }
1051 constexpr auto rbegin() const { return std::rbegin(details::hidl_enum_values<T>); }
1052 constexpr auto crbegin() const { return rbegin(); }
1053 constexpr auto end() const { return std::end(details::hidl_enum_values<T>); }
1054 constexpr auto cend() const { return end(); }
1055 constexpr auto rend() const { return std::rend(details::hidl_enum_values<T>); }
1056 constexpr auto crend() const { return rend(); }
1057};
Steven Moreland5f3a8cf2018-04-25 11:57:35 -07001058
Steven Moreland2f2c9632018-05-01 12:24:14 -07001059template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
1060struct hidl_enum_iterator {
1061 static_assert(!std::is_enum<T>::value,
1062 "b/78573628: hidl_enum_iterator was renamed to hidl_enum_range because it is not "
1063 "actually an iterator. Please use that type instead.");
1064};
Steven Morelandabce0c72017-11-17 15:37:09 -08001065
Steven Morelanda6752122017-12-28 13:35:20 -08001066/**
1067 * Bitfields in HIDL are the underlying type of the enumeration.
1068 */
1069template <typename Enum>
1070using hidl_bitfield = typename std::underlying_type<Enum>::type;
1071
Martijn Coenen72110162016-08-19 14:28:25 +02001072} // namespace hardware
1073} // namespace android
1074
Martijn Coenenc28f1152016-08-22 14:06:56 +02001075
Martijn Coenen72110162016-08-19 14:28:25 +02001076#endif // ANDROID_HIDL_SUPPORT_H