blob: e85fc8741f81f83fdc238ee7f0aa69e476d2e5d2 [file] [log] [blame]
Martijn Coenen99e6beb2016-12-01 15:48:42 +01001/*
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
17#include "MemoryType.h"
18
19#include <hidl-util/Formatter.h>
20#include <android-base/logging.h>
21
22namespace android {
23
24MemoryType::MemoryType() {}
25
26void MemoryType::addNamedTypesToSet(std::set<const FQName> &) const {
27 // do nothing
28}
29
30std::string MemoryType::getCppType(StorageMode mode,
31 bool specifyNamespaces) const {
32 const std::string base =
33 std::string(specifyNamespaces ? "::android::hardware::" : "")
34 + "hidl_memory";
35
36 switch (mode) {
37 case StorageMode_Stack:
38 return base;
39
40 case StorageMode_Argument:
41 return "const " + base + "&";
42
43 case StorageMode_Result:
44 return "const " + base + "*";
45 }
46}
47
48std::string MemoryType::getVtsType() const {
49 return "TYPE_MEMORY";
50}
51
52void MemoryType::emitReaderWriter(
53 Formatter &out,
54 const std::string &name,
55 const std::string &parcelObj,
56 bool parcelObjIsPointer,
57 bool isReader,
58 ErrorMode mode) const {
59 const std::string parentName = "_hidl_" + name + "_parent";
60 out << "size_t " << parentName << ";\n\n";
61
62 const std::string parcelObjDeref =
63 parcelObj + (parcelObjIsPointer ? "->" : ".");
64
65 if (isReader) {
66 out << name
67 << " = (const ::android::hardware::hidl_memory *)"
68 << parcelObjDeref
69 << "readBuffer("
70 << "&"
71 << parentName
72 << ");\n";
73
74 out << "if ("
75 << name
76 << " == nullptr) {\n";
77
78 out.indent();
79
80 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
81 handleError2(out, mode);
82
83 out.unindent();
84 out << "}\n\n";
85 } else {
86 out << "_hidl_err = "
87 << parcelObjDeref
88 << "writeBuffer(&"
89 << name
90 << ", sizeof("
91 << name
92 << "), &"
93 << parentName
94 << ");\n";
95
96 handleError(out, mode);
97 }
98
99 emitReaderWriterEmbedded(
100 out,
101 0 /* depth */,
102 name,
103 name /* sanitizedName */,
104 isReader /* nameIsPointer */,
105 parcelObj,
106 parcelObjIsPointer,
107 isReader,
108 mode,
109 parentName,
110 "0 /* parentOffset */");
111}
112
113void MemoryType::emitReaderWriterEmbedded(
114 Formatter &out,
115 size_t /* depth */,
116 const std::string &name,
117 const std::string & /*sanitizedName*/,
118 bool nameIsPointer,
119 const std::string &parcelObj,
120 bool parcelObjIsPointer,
121 bool isReader,
122 ErrorMode mode,
123 const std::string &parentName,
124 const std::string &offsetText) const {
125 emitReaderWriterEmbeddedForTypeName(
126 out,
127 name,
128 nameIsPointer,
129 parcelObj,
130 parcelObjIsPointer,
131 isReader,
132 mode,
133 parentName,
134 offsetText,
135 "::android::hardware::hidl_memory",
136 "" /* childName */,
137 "::android::hardware");
138}
139
140bool MemoryType::needsEmbeddedReadWrite() const {
141 return true;
142}
143
144bool MemoryType::resultNeedsDeref() const {
145 return true;
146}
147
148bool MemoryType::isJavaCompatible() const {
149 return false;
150}
151
152void MemoryType::getAlignmentAndSize(size_t *align, size_t *size) const {
153 *align = *size = 8;
154}
155
156status_t MemoryType::emitVtsTypeDeclarations(Formatter &out) const {
157 out << "type: " << getVtsType() << "\n";
158 return OK;
159}
160
161} // namespace android
162