blob: 1ab2e69de386196514288c195ed9f6d92d571f6a [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
30std::string HandleType::getCppType(StorageMode,
31 std::string *extra,
32 bool specifyNamespaces) const {
Andreas Huber881227d2016-08-02 14:20:21 -070033 extra->clear();
34
Steven Moreland979e0992016-09-07 09:18:08 -070035 const std::string base =
36 std::string(specifyNamespaces ? "::" : "")
37 + "native_handle_t";
38
39 return "const " + base + "*";
Andreas Huber881227d2016-08-02 14:20:21 -070040}
41
Andreas Huber2831d512016-08-15 09:33:47 -070042std::string HandleType::getJavaType() const {
43 CHECK(!"Should not be here");
44 return std::string();
45}
46
Andreas Huber881227d2016-08-02 14:20:21 -070047void HandleType::emitReaderWriter(
48 Formatter &out,
49 const std::string &name,
50 const std::string &parcelObj,
51 bool parcelObjIsPointer,
52 bool isReader,
53 ErrorMode mode) const {
54 const std::string parcelObjDeref =
55 parcelObj + (parcelObjIsPointer ? "->" : ".");
56
57 if (isReader) {
58 out << name
59 << " = "
Andreas Hubere852c7d2016-08-09 13:12:18 -070060 << parcelObjDeref
61 << "readNativeHandleNoDup();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070062
63 out << "if ("
64 << name
65 << " == nullptr) {\n";
66
67 out.indent();
68
Iliyan Malchev549e2592016-08-10 08:59:12 -070069 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070070 handleError2(out, mode);
71
72 out.unindent();
73 out << "}\n\n";
74 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070075 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070076 out << parcelObjDeref
Andreas Hubere852c7d2016-08-09 13:12:18 -070077 << "writeNativeHandleNoDup("
Andreas Huber881227d2016-08-02 14:20:21 -070078 << name
Andreas Hubere852c7d2016-08-09 13:12:18 -070079 << ");\n";
Andreas Huber881227d2016-08-02 14:20:21 -070080
81 handleError(out, mode);
82 }
83}
84
85void HandleType::emitReaderWriterEmbedded(
86 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -070087 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -070088 const std::string &name,
89 bool nameIsPointer,
90 const std::string &parcelObj,
91 bool parcelObjIsPointer,
92 bool isReader,
93 ErrorMode mode,
94 const std::string &parentName,
95 const std::string &offsetText) const {
96 if (isReader) {
Iliyan Malchev549e2592016-08-10 08:59:12 -070097 const std::string ptrName = "_hidl_" + name + "_ptr";
Andreas Huber881227d2016-08-02 14:20:21 -070098
99 out << "const native_handle_t *"
100 << ptrName
101 << " = "
102 << parcelObj
103 << (parcelObjIsPointer ? "->" : ".")
104 << "readEmbeddedNativeHandle(\n";
105
106 out.indent();
107 out.indent();
108
109 out << parentName
110 << ",\n"
111 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700112 << ");\n\n";
113
114 out.unindent();
115 out.unindent();
116
117 out << "if ("
118 << ptrName
119 << " == nullptr) {\n";
120
121 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700122 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700123 handleError2(out, mode);
124 out.unindent();
125 out << "}\n\n";
126 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700127 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700128 << parcelObj
129 << (parcelObjIsPointer ? "->" : ".")
130 << "writeEmbeddedNativeHandle(\n";
131
132 out.indent();
133 out.indent();
134
Andreas Hubere852c7d2016-08-09 13:12:18 -0700135 out << (nameIsPointer ? ("*" + name) : name)
136 << ",\n"
Andreas Huber881227d2016-08-02 14:20:21 -0700137 << parentName
138 << ",\n"
139 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700140 << ");\n\n";
141
142 out.unindent();
143 out.unindent();
144
145 handleError(out, mode);
146 }
147}
148
149bool HandleType::needsEmbeddedReadWrite() const {
150 return true;
151}
152
Andreas Huber70a59e12016-08-16 12:57:01 -0700153bool HandleType::isJavaCompatible() const {
154 return false;
155}
156
Andreas Huber85eabdb2016-08-25 11:24:49 -0700157void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
158 *align = *size = 8;
159}
160
Andreas Huberc9410c72016-07-28 12:18:40 -0700161} // namespace android
162