blob: 210d5ad0776ffbdf1914db87cf846d979edb41ab [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,
Yifan Hongbe2a3732016-10-05 13:33:41 -070084 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -070085 bool nameIsPointer,
86 const std::string &parcelObj,
87 bool parcelObjIsPointer,
88 bool isReader,
89 ErrorMode mode,
90 const std::string &parentName,
91 const std::string &offsetText) const {
92 if (isReader) {
Yifan Hongbe2a3732016-10-05 13:33:41 -070093 const std::string ptrName = "_hidl_" + sanitizedName + "_ptr";
Andreas Huber881227d2016-08-02 14:20:21 -070094
95 out << "const native_handle_t *"
96 << ptrName
97 << " = "
98 << parcelObj
99 << (parcelObjIsPointer ? "->" : ".")
100 << "readEmbeddedNativeHandle(\n";
101
102 out.indent();
103 out.indent();
104
105 out << parentName
106 << ",\n"
107 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700108 << ");\n\n";
109
110 out.unindent();
111 out.unindent();
112
113 out << "if ("
114 << ptrName
115 << " == nullptr) {\n";
116
117 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700118 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700119 handleError2(out, mode);
120 out.unindent();
121 out << "}\n\n";
122 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700123 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700124 << parcelObj
125 << (parcelObjIsPointer ? "->" : ".")
126 << "writeEmbeddedNativeHandle(\n";
127
128 out.indent();
129 out.indent();
130
Andreas Hubere852c7d2016-08-09 13:12:18 -0700131 out << (nameIsPointer ? ("*" + name) : name)
132 << ",\n"
Andreas Huber881227d2016-08-02 14:20:21 -0700133 << parentName
134 << ",\n"
135 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700136 << ");\n\n";
137
138 out.unindent();
139 out.unindent();
140
141 handleError(out, mode);
142 }
143}
144
145bool HandleType::needsEmbeddedReadWrite() const {
146 return true;
147}
148
Andreas Huber70a59e12016-08-16 12:57:01 -0700149bool HandleType::isJavaCompatible() const {
150 return false;
151}
152
Andreas Huber85eabdb2016-08-25 11:24:49 -0700153void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
154 *align = *size = 8;
155}
156
Andreas Huberc9410c72016-07-28 12:18:40 -0700157} // namespace android
158