blob: f36aaefbd04d92e9c9d3cac86679188335feab5e [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
42void HandleType::emitReaderWriter(
43 Formatter &out,
44 const std::string &name,
45 const std::string &parcelObj,
46 bool parcelObjIsPointer,
47 bool isReader,
48 ErrorMode mode) const {
49 const std::string parcelObjDeref =
50 parcelObj + (parcelObjIsPointer ? "->" : ".");
51
52 if (isReader) {
53 out << name
54 << " = "
Andreas Hubere852c7d2016-08-09 13:12:18 -070055 << parcelObjDeref
56 << "readNativeHandleNoDup();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070057
58 out << "if ("
59 << name
60 << " == nullptr) {\n";
61
62 out.indent();
63
Iliyan Malchev549e2592016-08-10 08:59:12 -070064 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070065 handleError2(out, mode);
66
67 out.unindent();
68 out << "}\n\n";
69 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070070 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070071 out << parcelObjDeref
Andreas Hubere852c7d2016-08-09 13:12:18 -070072 << "writeNativeHandleNoDup("
Andreas Huber881227d2016-08-02 14:20:21 -070073 << name
Andreas Hubere852c7d2016-08-09 13:12:18 -070074 << ");\n";
Andreas Huber881227d2016-08-02 14:20:21 -070075
76 handleError(out, mode);
77 }
78}
79
80void HandleType::emitReaderWriterEmbedded(
81 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -070082 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -070083 const std::string &name,
84 bool nameIsPointer,
85 const std::string &parcelObj,
86 bool parcelObjIsPointer,
87 bool isReader,
88 ErrorMode mode,
89 const std::string &parentName,
90 const std::string &offsetText) const {
91 if (isReader) {
Iliyan Malchev549e2592016-08-10 08:59:12 -070092 const std::string ptrName = "_hidl_" + name + "_ptr";
Andreas Huber881227d2016-08-02 14:20:21 -070093
94 out << "const native_handle_t *"
95 << ptrName
96 << " = "
97 << parcelObj
98 << (parcelObjIsPointer ? "->" : ".")
99 << "readEmbeddedNativeHandle(\n";
100
101 out.indent();
102 out.indent();
103
104 out << parentName
105 << ",\n"
106 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700107 << ");\n\n";
108
109 out.unindent();
110 out.unindent();
111
112 out << "if ("
113 << ptrName
114 << " == nullptr) {\n";
115
116 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700117 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700118 handleError2(out, mode);
119 out.unindent();
120 out << "}\n\n";
121 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700122 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700123 << parcelObj
124 << (parcelObjIsPointer ? "->" : ".")
125 << "writeEmbeddedNativeHandle(\n";
126
127 out.indent();
128 out.indent();
129
Andreas Hubere852c7d2016-08-09 13:12:18 -0700130 out << (nameIsPointer ? ("*" + name) : name)
131 << ",\n"
Andreas Huber881227d2016-08-02 14:20:21 -0700132 << parentName
133 << ",\n"
134 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700135 << ");\n\n";
136
137 out.unindent();
138 out.unindent();
139
140 handleError(out, mode);
141 }
142}
143
144bool HandleType::needsEmbeddedReadWrite() const {
145 return true;
146}
147
Andreas Huber70a59e12016-08-16 12:57:01 -0700148bool HandleType::isJavaCompatible() const {
149 return false;
150}
151
Andreas Huber85eabdb2016-08-25 11:24:49 -0700152void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
153 *align = *size = 8;
154}
155
Andreas Huberc9410c72016-07-28 12:18:40 -0700156} // namespace android
157