blob: bd863262d122ceff0b5075c438ca048d1db1dfe1 [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
Andreas Hubere852c7d2016-08-09 13:12:18 -070027std::string HandleType::getCppType(StorageMode, std::string *extra) const {
Andreas Huber881227d2016-08-02 14:20:21 -070028 extra->clear();
29
Andreas Hubere852c7d2016-08-09 13:12:18 -070030 return "const ::native_handle_t*";
Andreas Huber881227d2016-08-02 14:20:21 -070031}
32
Andreas Huber2831d512016-08-15 09:33:47 -070033std::string HandleType::getJavaType() const {
34 CHECK(!"Should not be here");
35 return std::string();
36}
37
Andreas Huber881227d2016-08-02 14:20:21 -070038void HandleType::emitReaderWriter(
39 Formatter &out,
40 const std::string &name,
41 const std::string &parcelObj,
42 bool parcelObjIsPointer,
43 bool isReader,
44 ErrorMode mode) const {
45 const std::string parcelObjDeref =
46 parcelObj + (parcelObjIsPointer ? "->" : ".");
47
48 if (isReader) {
49 out << name
50 << " = "
Andreas Hubere852c7d2016-08-09 13:12:18 -070051 << parcelObjDeref
52 << "readNativeHandleNoDup();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070053
54 out << "if ("
55 << name
56 << " == nullptr) {\n";
57
58 out.indent();
59
Iliyan Malchev549e2592016-08-10 08:59:12 -070060 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070061 handleError2(out, mode);
62
63 out.unindent();
64 out << "}\n\n";
65 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070066 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070067 out << parcelObjDeref
Andreas Hubere852c7d2016-08-09 13:12:18 -070068 << "writeNativeHandleNoDup("
Andreas Huber881227d2016-08-02 14:20:21 -070069 << name
Andreas Hubere852c7d2016-08-09 13:12:18 -070070 << ");\n";
Andreas Huber881227d2016-08-02 14:20:21 -070071
72 handleError(out, mode);
73 }
74}
75
76void HandleType::emitReaderWriterEmbedded(
77 Formatter &out,
78 const std::string &name,
79 bool nameIsPointer,
80 const std::string &parcelObj,
81 bool parcelObjIsPointer,
82 bool isReader,
83 ErrorMode mode,
84 const std::string &parentName,
85 const std::string &offsetText) const {
86 if (isReader) {
Iliyan Malchev549e2592016-08-10 08:59:12 -070087 const std::string ptrName = "_hidl_" + name + "_ptr";
Andreas Huber881227d2016-08-02 14:20:21 -070088
89 out << "const native_handle_t *"
90 << ptrName
91 << " = "
92 << parcelObj
93 << (parcelObjIsPointer ? "->" : ".")
94 << "readEmbeddedNativeHandle(\n";
95
96 out.indent();
97 out.indent();
98
99 out << parentName
100 << ",\n"
101 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700102 << ");\n\n";
103
104 out.unindent();
105 out.unindent();
106
107 out << "if ("
108 << ptrName
109 << " == nullptr) {\n";
110
111 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700112 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700113 handleError2(out, mode);
114 out.unindent();
115 out << "}\n\n";
116 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700117 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700118 << parcelObj
119 << (parcelObjIsPointer ? "->" : ".")
120 << "writeEmbeddedNativeHandle(\n";
121
122 out.indent();
123 out.indent();
124
Andreas Hubere852c7d2016-08-09 13:12:18 -0700125 out << (nameIsPointer ? ("*" + name) : name)
126 << ",\n"
Andreas Huber881227d2016-08-02 14:20:21 -0700127 << parentName
128 << ",\n"
129 << offsetText
Andreas Huber881227d2016-08-02 14:20:21 -0700130 << ");\n\n";
131
132 out.unindent();
133 out.unindent();
134
135 handleError(out, mode);
136 }
137}
138
139bool HandleType::needsEmbeddedReadWrite() const {
140 return true;
141}
142
Andreas Huber70a59e12016-08-16 12:57:01 -0700143bool HandleType::isJavaCompatible() const {
144 return false;
145}
146
Andreas Huber85eabdb2016-08-25 11:24:49 -0700147void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
148 *align = *size = 8;
149}
150
Andreas Huberc9410c72016-07-28 12:18:40 -0700151} // namespace android
152