blob: f1a55721a79691bbcc4fbf9b2a0aab4322dad8eb [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
19#include "Formatter.h"
20
Andreas Huber2831d512016-08-15 09:33:47 -070021#include <android-base/logging.h>
22
Andreas Huberc9410c72016-07-28 12:18:40 -070023namespace android {
24
25HandleType::HandleType() {}
26
Steven Moreland979e0992016-09-07 09:18:08 -070027void HandleType::addNamedTypesToSet(std::set<const FQName> &) const {
28 // do nothing
29}
30
31std::string HandleType::getCppType(StorageMode,
32 std::string *extra,
33 bool specifyNamespaces) const {
Andreas Huber881227d2016-08-02 14:20:21 -070034 extra->clear();
35
Steven Moreland979e0992016-09-07 09:18:08 -070036 const std::string base =
37 std::string(specifyNamespaces ? "::" : "")
38 + "native_handle_t";
39
40 return "const " + base + "*";
Andreas Huber881227d2016-08-02 14:20:21 -070041}
42
Andreas Huber2831d512016-08-15 09:33:47 -070043std::string HandleType::getJavaType() const {
44 CHECK(!"Should not be here");
45 return std::string();
46}
47
Andreas Huber881227d2016-08-02 14:20:21 -070048void HandleType::emitReaderWriter(
49 Formatter &out,
50 const std::string &name,
51 const std::string &parcelObj,
52 bool parcelObjIsPointer,
53 bool isReader,
54 ErrorMode mode) const {
55 const std::string parcelObjDeref =
56 parcelObj + (parcelObjIsPointer ? "->" : ".");
57
58 if (isReader) {
59 out << name
60 << " = "
Andreas Hubere852c7d2016-08-09 13:12:18 -070061 << parcelObjDeref
62 << "readNativeHandleNoDup();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070063
64 out << "if ("
65 << name
66 << " == nullptr) {\n";
67
68 out.indent();
69
Iliyan Malchev549e2592016-08-10 08:59:12 -070070 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070071 handleError2(out, mode);
72
73 out.unindent();
74 out << "}\n\n";
75 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070076 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070077 out << parcelObjDeref
Andreas Hubere852c7d2016-08-09 13:12:18 -070078 << "writeNativeHandleNoDup("
Andreas Huber881227d2016-08-02 14:20:21 -070079 << name
Andreas Hubere852c7d2016-08-09 13:12:18 -070080 << ");\n";
Andreas Huber881227d2016-08-02 14:20:21 -070081
82 handleError(out, mode);
83 }
84}
85
86void HandleType::emitReaderWriterEmbedded(
87 Formatter &out,
88 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