Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [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 | #define LOG_TAG "MQDescriptor" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <hidl/MQDescriptor.h> |
| 21 | |
| 22 | #include <cutils/native_handle.h> |
| 23 | #include <sys/mman.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
| 28 | MQDescriptor::MQDescriptor( |
| 29 | const std::vector<GrantorDescriptor>& grantors, |
| 30 | native_handle_t* nhandle, |
| 31 | int32_t flags, |
| 32 | size_t size) |
| 33 | : mHandle(nhandle), |
| 34 | mQuantum(size), |
| 35 | mFlags(flags) { |
| 36 | mGrantors.resize(grantors.size()); |
| 37 | for (size_t i = 0; i < grantors.size(); ++i) { |
| 38 | mGrantors[i] = grantors[i]; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | MQDescriptor::MQDescriptor(const MQDescriptor &other) |
| 43 | : mGrantors(other.mGrantors), |
| 44 | mHandle(nullptr), |
| 45 | mQuantum(other.mQuantum), |
| 46 | mFlags(other.mFlags) { |
| 47 | if (other.mHandle != nullptr) { |
| 48 | mHandle = native_handle_create( |
| 49 | other.mHandle->numFds, other.mHandle->numInts); |
| 50 | |
| 51 | for (int i = 0; i < other.mHandle->numFds; ++i) { |
| 52 | const_cast<native_handle_t *>(mHandle)->data[i] = |
| 53 | dup(other.mHandle->data[i]); |
| 54 | } |
| 55 | |
| 56 | memcpy(&const_cast<native_handle_t *>(mHandle)->data[other.mHandle->numFds], |
| 57 | &other.mHandle->data[other.mHandle->numFds], |
| 58 | other.mHandle->numInts * sizeof(int)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | MQDescriptor::~MQDescriptor() { |
| 63 | if (mHandle != nullptr) { |
| 64 | native_handle_close(const_cast<native_handle_t *>(mHandle)); |
| 65 | native_handle_delete(const_cast<native_handle_t *>(mHandle)); |
| 66 | } |
| 67 | } |
| 68 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 69 | ::android::status_t MQDescriptor::readEmbeddedFromParcel( |
| 70 | const Parcel &parcel, |
| 71 | size_t parentHandle, |
| 72 | size_t parentOffset) { |
| 73 | ::android::status_t _hidl_err = ::android::OK; |
| 74 | |
| 75 | size_t _hidl_grantors_child; |
| 76 | |
| 77 | _hidl_err = const_cast<hidl_vec<GrantorDescriptor> *>( |
| 78 | &mGrantors)->readEmbeddedFromParcel( |
| 79 | parcel, |
| 80 | parentHandle, |
| 81 | parentOffset + offsetof(MQDescriptor, mGrantors), |
| 82 | &_hidl_grantors_child); |
| 83 | |
| 84 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 85 | |
| 86 | const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle( |
| 87 | parentHandle, |
| 88 | parentOffset + offsetof(MQDescriptor, mHandle)); |
| 89 | |
| 90 | if (_hidl_mq_handle_ptr == nullptr) { |
| 91 | _hidl_err = ::android::UNKNOWN_ERROR; |
| 92 | return _hidl_err; |
| 93 | } |
| 94 | |
| 95 | _hidl_error: |
| 96 | return _hidl_err; |
| 97 | } |
| 98 | |
| 99 | ::android::status_t MQDescriptor::writeEmbeddedToParcel( |
| 100 | Parcel *parcel, |
| 101 | size_t parentHandle, |
| 102 | size_t parentOffset) const { |
| 103 | ::android::status_t _hidl_err = ::android::OK; |
| 104 | |
| 105 | size_t _hidl_grantors_child; |
| 106 | |
| 107 | _hidl_err = mGrantors.writeEmbeddedToParcel( |
| 108 | parcel, |
| 109 | parentHandle, |
| 110 | parentOffset + offsetof(MQDescriptor, mGrantors), |
| 111 | &_hidl_grantors_child); |
| 112 | |
| 113 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 114 | |
| 115 | _hidl_err = parcel->writeEmbeddedNativeHandle( |
| 116 | mHandle, |
| 117 | parentHandle, |
| 118 | parentOffset + offsetof(MQDescriptor, mHandle)); |
| 119 | |
| 120 | if (_hidl_err != ::android::OK) { return _hidl_err; } |
| 121 | |
| 122 | _hidl_error: |
| 123 | return _hidl_err; |
| 124 | } |
| 125 | |
Hridya Valsaraju | 4fe70f0 | 2016-09-20 21:02:51 -0700 | [diff] [blame^] | 126 | size_t MQDescriptor::getSize() const { |
| 127 | return mGrantors[DATAPTRPOS].extent; |
| 128 | } |
| 129 | |
| 130 | size_t MQDescriptor::getQuantum() const { return mQuantum; } |
| 131 | |
| 132 | int32_t MQDescriptor::getFlags() const { return mFlags; } |
| 133 | |
| 134 | std::vector<GrantorDescriptor> MQDescriptor::getGrantors() const { |
| 135 | size_t grantor_count = mGrantors.size(); |
| 136 | std::vector<GrantorDescriptor> grantors(grantor_count); |
| 137 | for (size_t i = 0; i < grantor_count; i++) { |
| 138 | grantors[i] = mGrantors[i]; |
| 139 | } |
| 140 | return grantors; |
| 141 | } |
| 142 | |
| 143 | const sp<NativeHandle> MQDescriptor::getNativeHandle() const { |
| 144 | /* |
| 145 | * Create an sp<NativeHandle> from mHandle. |
| 146 | */ |
| 147 | return NativeHandle::create(mHandle, false /* ownsHandle */); |
| 148 | } |
| 149 | |
Andreas Huber | 0a45128 | 2016-08-30 11:27:24 -0700 | [diff] [blame] | 150 | } // namespace hardware |
| 151 | } // namespace android |
| 152 | |