Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 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 <hidl-util/Formatter.h> |
| 20 | #include <android-base/logging.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | MemoryType::MemoryType() {} |
| 25 | |
| 26 | void MemoryType::addNamedTypesToSet(std::set<const FQName> &) const { |
| 27 | // do nothing |
| 28 | } |
| 29 | |
| 30 | std::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 | |
| 48 | std::string MemoryType::getVtsType() const { |
Keun Soo Yim | b349e01 | 2017-01-06 16:51:32 -0800 | [diff] [blame] | 49 | return "TYPE_HIDL_MEMORY"; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void 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) { |
Martijn Coenen | 6a082c6 | 2017-01-11 12:47:02 +0100 | [diff] [blame] | 66 | out << "_hidl_err = " |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 67 | << parcelObjDeref |
Martijn Coenen | 6a082c6 | 2017-01-11 12:47:02 +0100 | [diff] [blame] | 68 | << "readBuffer(&" |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 69 | << parentName |
Martijn Coenen | 6a082c6 | 2017-01-11 12:47:02 +0100 | [diff] [blame] | 70 | << ", " |
| 71 | << " reinterpret_cast<const void **>(" |
| 72 | << "&" << name |
| 73 | << "));\n\n"; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 74 | |
Martijn Coenen | 6a082c6 | 2017-01-11 12:47:02 +0100 | [diff] [blame] | 75 | handleError(out, mode); |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 76 | } else { |
| 77 | out << "_hidl_err = " |
| 78 | << parcelObjDeref |
| 79 | << "writeBuffer(&" |
| 80 | << name |
| 81 | << ", sizeof(" |
| 82 | << name |
| 83 | << "), &" |
| 84 | << parentName |
| 85 | << ");\n"; |
| 86 | |
| 87 | handleError(out, mode); |
| 88 | } |
| 89 | |
| 90 | emitReaderWriterEmbedded( |
| 91 | out, |
| 92 | 0 /* depth */, |
| 93 | name, |
| 94 | name /* sanitizedName */, |
| 95 | isReader /* nameIsPointer */, |
| 96 | parcelObj, |
| 97 | parcelObjIsPointer, |
| 98 | isReader, |
| 99 | mode, |
| 100 | parentName, |
| 101 | "0 /* parentOffset */"); |
| 102 | } |
| 103 | |
| 104 | void MemoryType::emitReaderWriterEmbedded( |
| 105 | Formatter &out, |
| 106 | size_t /* depth */, |
| 107 | const std::string &name, |
| 108 | const std::string & /*sanitizedName*/, |
| 109 | bool nameIsPointer, |
| 110 | const std::string &parcelObj, |
| 111 | bool parcelObjIsPointer, |
| 112 | bool isReader, |
| 113 | ErrorMode mode, |
| 114 | const std::string &parentName, |
| 115 | const std::string &offsetText) const { |
| 116 | emitReaderWriterEmbeddedForTypeName( |
| 117 | out, |
| 118 | name, |
| 119 | nameIsPointer, |
| 120 | parcelObj, |
| 121 | parcelObjIsPointer, |
| 122 | isReader, |
| 123 | mode, |
| 124 | parentName, |
| 125 | offsetText, |
| 126 | "::android::hardware::hidl_memory", |
| 127 | "" /* childName */, |
| 128 | "::android::hardware"); |
| 129 | } |
| 130 | |
| 131 | bool MemoryType::needsEmbeddedReadWrite() const { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | bool MemoryType::resultNeedsDeref() const { |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | bool MemoryType::isJavaCompatible() const { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | void MemoryType::getAlignmentAndSize(size_t *align, size_t *size) const { |
Andreas Huber | 60d3b22 | 2017-03-30 09:10:56 -0700 | [diff] [blame] | 144 | *align = 8; // hidl_memory |
| 145 | *size = 40; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | status_t MemoryType::emitVtsTypeDeclarations(Formatter &out) const { |
| 149 | out << "type: " << getVtsType() << "\n"; |
| 150 | return OK; |
| 151 | } |
| 152 | |
| 153 | } // namespace android |
| 154 | |