1 /*
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 "FmqType.h"
18
19 #include "HidlTypeAssertion.h"
20
21 #include <hidl-util/Formatter.h>
22 #include <android-base/logging.h>
23
24 namespace android {
25
FmqType(const char * nsp,const char * name,Scope * parent)26 FmqType::FmqType(const char* nsp, const char* name, Scope* parent)
27 : TemplatedType(parent), mNamespace(nsp), mName(name) {}
28
templatedTypeName() const29 std::string FmqType::templatedTypeName() const {
30 return mName;
31 }
32
fullName() const33 std::string FmqType::fullName() const {
34 return mNamespace +
35 (mNamespace.empty() ? "" : "::") +
36 mName + "<" + mElementType->getCppStackType(true) + ">";
37 }
38
getCppType(StorageMode mode,bool) const39 std::string FmqType::getCppType(
40 StorageMode mode,
41 bool) const {
42
43 const std::string base = fullName();
44
45 switch (mode) {
46 case StorageMode_Stack:
47 return base;
48
49 case StorageMode_Argument:
50 return "const " + base + "&";
51
52 case StorageMode_Result:
53 return "const " + base + "*";
54 }
55 }
56
emitReaderWriter(Formatter & out,const std::string & name,const std::string & parcelObj,bool parcelObjIsPointer,bool isReader,ErrorMode mode) const57 void FmqType::emitReaderWriter(
58 Formatter &out,
59 const std::string &name,
60 const std::string &parcelObj,
61 bool parcelObjIsPointer,
62 bool isReader,
63 ErrorMode mode) const {
64 const std::string parentName = "_hidl_" + name + "_parent";
65
66 out << "size_t " << parentName << ";\n\n";
67
68 const std::string parcelObjDeref =
69 parcelObj + (parcelObjIsPointer ? "->" : ".");
70
71 if (isReader) {
72 out << "_hidl_err = "
73 << parcelObjDeref
74 << "readBuffer("
75 << "sizeof(*"
76 << name
77 << "), &"
78 << parentName
79 << ", "
80 << " reinterpret_cast<const void **>("
81 << "&" << name
82 << "));\n\n";
83
84 handleError(out, mode);
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
emitReaderWriterEmbedded(Formatter & out,size_t,const std::string & name,const std::string &,bool nameIsPointer,const std::string & parcelObj,bool parcelObjIsPointer,bool isReader,ErrorMode mode,const std::string & parentName,const std::string & offsetText) const113 void FmqType::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 fullName(),
136 "" /* childName */,
137 mNamespace);
138 }
139
deepIsJavaCompatible(std::unordered_set<const Type * > *) const140 bool FmqType::deepIsJavaCompatible(std::unordered_set<const Type*>* /* visited */) const {
141 return false;
142 }
143
144 // All MQDescriptor<T, flavor> have the same size.
145 static HidlTypeAssertion assertion(
146 "MQDescriptor<char, ::android::hardware::kSynchronizedReadWrite>", 32);
147
getAlignmentAndSize(size_t * align,size_t * size) const148 void FmqType::getAlignmentAndSize(
149 size_t *align, size_t *size) const {
150 *align = 8; // MQDescriptor<>
151 *size = assertion.size();
152 }
153
needsEmbeddedReadWrite() const154 bool FmqType::needsEmbeddedReadWrite() const {
155 return true;
156 }
157
resultNeedsDeref() const158 bool FmqType::resultNeedsDeref() const {
159 return true;
160 }
161
isCompatibleElementType(const Type * elementType) const162 bool FmqType::isCompatibleElementType(const Type* elementType) const {
163 return (!elementType->isInterface() && !elementType->needsEmbeddedReadWrite());
164 }
165
getVtsType() const166 std::string FmqType::getVtsType() const {
167 if (mName == "MQDescriptorSync") {
168 return "TYPE_FMQ_SYNC";
169 } else if (mName == "MQDescriptorUnsync") {
170 return "TYPE_FMQ_UNSYNC";
171 }
172
173 CHECK(false) << "Invalid FmqType.";
174 return "";
175 }
176
getVtsValueName() const177 std::string FmqType::getVtsValueName() const {
178 return "fmq_value";
179 }
180 } // namespace android
181
182