• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "HandleType.h"
18 
19 #include "HidlTypeAssertion.h"
20 
21 #include <hidl-util/Formatter.h>
22 #include <android-base/logging.h>
23 
24 namespace android {
25 
HandleType(Scope * parent)26 HandleType::HandleType(Scope* parent) : Type(parent) {}
27 
isHandle() const28 bool HandleType::isHandle() const {
29     return true;
30 }
31 
typeName() const32 std::string HandleType::typeName() const {
33     return "handle";
34 }
35 
getCppType(StorageMode mode,bool specifyNamespaces) const36 std::string HandleType::getCppType(StorageMode mode,
37                                    bool specifyNamespaces) const {
38     const std::string base =
39           std::string(specifyNamespaces ? "::android::hardware::" : "")
40         + "hidl_handle";
41 
42     switch (mode) {
43         case StorageMode_Stack:
44             return base;
45 
46         case StorageMode_Argument:
47             return "const " + base + "&";
48 
49         case StorageMode_Result:
50             return base;
51     }
52 }
53 
getJavaType(bool) const54 std::string HandleType::getJavaType(bool /* forInitializer */) const {
55     return "android.os.NativeHandle";
56 }
57 
getJavaSuffix() const58 std::string HandleType::getJavaSuffix() const {
59     return "NativeHandle";
60 }
61 
getVtsType() const62 std::string HandleType::getVtsType() const {
63     return "TYPE_HANDLE";
64 }
65 
emitReaderWriter(Formatter & out,const std::string & name,const std::string & parcelObj,bool parcelObjIsPointer,bool isReader,ErrorMode mode) const66 void HandleType::emitReaderWriter(
67         Formatter &out,
68         const std::string &name,
69         const std::string &parcelObj,
70         bool parcelObjIsPointer,
71         bool isReader,
72         ErrorMode mode) const {
73     const std::string parcelObjDeref =
74         parcelObj + (parcelObjIsPointer ? "->" : ".");
75 
76     if (isReader) {
77         out << "const native_handle_t *"
78             << name << "_ptr;\n\n";
79 
80         out << "_hidl_err = "
81             << parcelObjDeref
82             << "readNullableNativeHandleNoDup("
83             << "&" << name << "_ptr"
84             << ");\n\n";
85 
86         handleError(out, mode);
87 
88         out << name << " = " << name << "_ptr;\n";
89     } else {
90         out << "_hidl_err = ";
91         out << parcelObjDeref
92             << "writeNativeHandleNoDup("
93             << name
94             << ");\n";
95 
96         handleError(out, mode);
97     }
98 }
99 
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) const100 void HandleType::emitReaderWriterEmbedded(
101         Formatter &out,
102         size_t /* depth */,
103         const std::string &name,
104         const std::string & /* sanitizedName */,
105         bool nameIsPointer,
106         const std::string &parcelObj,
107         bool parcelObjIsPointer,
108         bool isReader,
109         ErrorMode mode,
110         const std::string &parentName,
111         const std::string &offsetText) const {
112     emitReaderWriterEmbeddedForTypeName(
113             out,
114             name,
115             nameIsPointer,
116             parcelObj,
117             parcelObjIsPointer,
118             isReader,
119             mode,
120             parentName,
121             offsetText,
122             "::android::hardware::hidl_handle",
123             "" /* childName */,
124             "::android::hardware");
125 }
126 
emitJavaFieldInitializer(Formatter & out,const std::string & fieldName) const127 void HandleType::emitJavaFieldInitializer(
128         Formatter &out, const std::string &fieldName) const {
129     const std::string fieldDeclaration = getJavaType(false) + " " + fieldName;
130     emitJavaFieldDefaultInitialValue(out, fieldDeclaration);
131 }
132 
emitJavaFieldDefaultInitialValue(Formatter & out,const std::string & declaredFieldName) const133 void HandleType::emitJavaFieldDefaultInitialValue(
134         Formatter &out, const std::string &declaredFieldName) const {
135     out << declaredFieldName
136         << " = new "
137         << getJavaType(true)
138         << "();\n";
139 }
140 
emitJavaFieldReaderWriter(Formatter & out,size_t,const std::string & parcelName,const std::string & blobName,const std::string & fieldName,const std::string & offset,bool isReader) const141 void HandleType::emitJavaFieldReaderWriter(
142         Formatter &out,
143         size_t /* depth */,
144         const std::string &parcelName,
145         const std::string &blobName,
146         const std::string &fieldName,
147         const std::string &offset,
148         bool isReader) const {
149     if (isReader) {
150         out << fieldName
151             << " = "
152             << parcelName
153             << ".readEmbeddedNativeHandle(\n";
154 
155         out.indent(2, [&] {
156             out << blobName
157                 << ".handle(),\n"
158                 << offset
159                 << " + 0 /* offsetof(hidl_handle, mHandle) */);\n\n";
160         });
161 
162         return;
163     }
164 
165     out << blobName
166         << ".putNativeHandle("
167         << offset
168         << ", "
169         << fieldName
170         << ");\n";
171 }
172 
needsEmbeddedReadWrite() const173 bool HandleType::needsEmbeddedReadWrite() const {
174     return true;
175 }
176 
deepIsJavaCompatible(std::unordered_set<const Type * > *) const177 bool HandleType::deepIsJavaCompatible(std::unordered_set<const Type*>* /* visited */) const {
178     return true;
179 }
180 
181 static HidlTypeAssertion assertion("hidl_handle", 16 /* size */);
getAlignmentAndSize(size_t * align,size_t * size) const182 void HandleType::getAlignmentAndSize(size_t *align, size_t *size) const {
183     *align = 8;  // hidl_handle
184     *size = assertion.size();
185 }
186 
emitVtsTypeDeclarations(Formatter & out) const187 void HandleType::emitVtsTypeDeclarations(Formatter& out) const {
188     out << "type: " << getVtsType() << "\n";
189 }
190 
191 }  // namespace android
192 
193