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