blob: 29afb1aa8a118203e41698a4995526f656b204d2 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "HandleType.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/Formatter.h>
Andreas Huber2831d512016-08-15 09:33:47 -070020#include <android-base/logging.h>
21
Andreas Huberc9410c72016-07-28 12:18:40 -070022namespace android {
23
24HandleType::HandleType() {}
25
Steven Moreland979e0992016-09-07 09:18:08 -070026void HandleType::addNamedTypesToSet(std::set<const FQName> &) const {
27 // do nothing
28}
29
Martijn Coenenac587892016-11-17 15:14:19 +010030std::string HandleType::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070031 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -070032 const std::string base =
33 std::string(specifyNamespaces ? "::" : "")
34 + "native_handle_t";
Martijn Coenenac587892016-11-17 15:14:19 +010035 if (mode == StorageMode_Compound) {
36 const std::string hidl_pointer_type =
37 std::string(specifyNamespaces ? "::android::hardware::details::hidl_pointer" :
38 "hidl_pointer");
39 return hidl_pointer_type + "<" + base + ">";
40 } else {
41 return "const " + base + "*";
42 }
Andreas Huber881227d2016-08-02 14:20:21 -070043}
44
45void HandleType::emitReaderWriter(
46 Formatter &out,
47 const std::string &name,
48 const std::string &parcelObj,
49 bool parcelObjIsPointer,
50 bool isReader,
51 ErrorMode mode) const {
52 const std::string parcelObjDeref =
53 parcelObj + (parcelObjIsPointer ? "->" : ".");
54
55 if (isReader) {
56 out << name
57 << " = "
Andreas Hubere852c7d2016-08-09 13:12:18 -070058 << parcelObjDeref
59 << "readNativeHandleNoDup();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070060
61 out << "if ("
62 << name
63 << " == nullptr) {\n";
64
65 out.indent();
66
Iliyan Malchev549e2592016-08-10 08:59:12 -070067 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070068 handleError2(out, mode);
69
70 out.unindent();
71 out << "}\n\n";
72 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070073 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070074 out << parcelObjDeref
Andreas Hubere852c7d2016-08-09 13:12:18 -070075 << "writeNativeHandleNoDup("
Andreas Huber881227d2016-08-02 14:20:21 -070076 << name
Andreas Hubere852c7d2016-08-09 13:12:18 -070077 << ");\n";
Andreas Huber881227d2016-08-02 14:20:21 -070078
79 handleError(out, mode);
80 }
81}
82
Yifan Hong244e82d2016-11-11 11:13:57 -080083bool HandleType::useNameInEmitReaderWriterEmbedded(bool isReader) const {
84 return !isReader;
85}
86
Andreas Huber881227d2016-08-02 14:20:21 -070087void HandleType::emitReaderWriterEmbedded(
88 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -070089 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -070090 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -070091 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -070092 bool nameIsPointer,
93 const std::string &parcelObj,
94 bool parcelObjIsPointer,
95 bool isReader,
96 ErrorMode mode,
97 const std::string &parentName,
98 const std::string &offsetText) const {
99 if (isReader) {
Yifan Hongbe2a3732016-10-05 13:33:41 -0700100 const std::string ptrName = "_hidl_" + sanitizedName + "_ptr";
Andreas Huber881227d2016-08-02 14:20:21 -0700101
102 out << "const native_handle_t *"
103 << ptrName
104 << " = "
105 << parcelObj
106 << (parcelObjIsPointer ? "->" : ".")
107 << "readEmbeddedNativeHandle(\n";
108
109 out.indent();
110 out.indent();
111
112 out << parentName
113 << ",\n"
114 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700115 << ");\n\n";
116
117 out.unindent();
118 out.unindent();
119
120 out << "if ("
121 << ptrName
122 << " == nullptr) {\n";
123
124 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700125 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700126 handleError2(out, mode);
127 out.unindent();
128 out << "}\n\n";
129 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700130 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700131 << parcelObj
132 << (parcelObjIsPointer ? "->" : ".")
133 << "writeEmbeddedNativeHandle(\n";
134
135 out.indent();
136 out.indent();
137
Andreas Hubere852c7d2016-08-09 13:12:18 -0700138 out << (nameIsPointer ? ("*" + name) : name)
139 << ",\n"
Andreas Huber881227d2016-08-02 14:20:21 -0700140 << parentName
141 << ",\n"
142 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700143 << ");\n\n";
144
145 out.unindent();
146 out.unindent();
147
148 handleError(out, mode);
149 }
150}
151
152bool HandleType::needsEmbeddedReadWrite() const {
153 return true;
154}
155
Andreas Huber70a59e12016-08-16 12:57:01 -0700156bool HandleType::isJavaCompatible() const {
157 return false;
158}
159
Andreas Huber85eabdb2016-08-25 11:24:49 -0700160void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
161 *align = *size = 8;
162}
163
Andreas Huberc9410c72016-07-28 12:18:40 -0700164} // namespace android
165