Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 "TestAidlMsgQ.h" |
| 18 | |
| 19 | namespace aidl { |
| 20 | namespace android { |
| 21 | namespace fmq { |
| 22 | namespace test { |
| 23 | |
| 24 | // Methods from ::aidl::android::fmq::test::ITestAidlMsgQ follow. |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame^] | 25 | ndk::ScopedAStatus TestAidlMsgQ::configureFmqSyncReadWrite( |
| 26 | const MQDescriptor<int32_t, SynchronizedReadWrite>& mqDesc, bool* _aidl_return) { |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 27 | mFmqSynchronized.reset(new (std::nothrow) TestAidlMsgQ::MessageQueueSync(mqDesc)); |
| 28 | if ((mFmqSynchronized == nullptr) || (mFmqSynchronized->isValid() == false)) { |
| 29 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 30 | } |
| 31 | /* |
| 32 | * Initialize the EventFlag word with bit FMQ_NOT_FULL. |
| 33 | */ |
| 34 | auto evFlagWordPtr = mFmqSynchronized->getEventFlagWord(); |
| 35 | if (evFlagWordPtr != nullptr) { |
| 36 | std::atomic_init(evFlagWordPtr, static_cast<uint32_t>(EventFlagBits::FMQ_NOT_FULL)); |
| 37 | } |
| 38 | *_aidl_return = true; |
| 39 | return ndk::ScopedAStatus::ok(); |
| 40 | } |
| 41 | |
Devin Moore | 1b2d0a6 | 2020-09-22 17:20:54 +0000 | [diff] [blame^] | 42 | ndk::ScopedAStatus TestAidlMsgQ::getFmqUnsyncWrite( |
| 43 | bool configureFmq, MQDescriptor<int32_t, UnsynchronizedWrite>* mqDesc, bool* _aidl_return) { |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 44 | if (configureFmq) { |
| 45 | static constexpr size_t kNumElementsInQueue = 1024; |
| 46 | mFmqUnsynchronized.reset(new (std::nothrow) |
| 47 | TestAidlMsgQ::MessageQueueUnsync(kNumElementsInQueue)); |
| 48 | } |
| 49 | |
| 50 | if ((mFmqUnsynchronized == nullptr) || (mFmqUnsynchronized->isValid() == false) || |
| 51 | (mqDesc == nullptr)) { |
| 52 | *_aidl_return = false; |
| 53 | } else { |
| 54 | *mqDesc = std::move(mFmqUnsynchronized->dupeDesc()); |
| 55 | *_aidl_return = true; |
| 56 | } |
| 57 | |
| 58 | return ndk::ScopedAStatus::ok(); |
| 59 | } |
| 60 | |
| 61 | ndk::ScopedAStatus TestAidlMsgQ::requestBlockingRead(int32_t count) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 62 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 63 | bool result = mFmqSynchronized->readBlocking( |
| 64 | &data[0], count, static_cast<uint32_t>(EventFlagBits::FMQ_NOT_FULL), |
| 65 | static_cast<uint32_t>(EventFlagBits::FMQ_NOT_EMPTY), 5000000000 /* timeOutNanos */); |
| 66 | |
| 67 | if (result == false) { |
| 68 | ALOGE("Blocking read fails"); |
| 69 | } |
| 70 | return ndk::ScopedAStatus::ok(); |
| 71 | } |
| 72 | |
| 73 | ndk::ScopedAStatus TestAidlMsgQ::requestBlockingReadDefaultEventFlagBits(int32_t count) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 74 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 75 | bool result = mFmqSynchronized->readBlocking(&data[0], count); |
| 76 | |
| 77 | if (result == false) { |
| 78 | ALOGE("Blocking read fails"); |
| 79 | } |
| 80 | |
| 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ndk::ScopedAStatus TestAidlMsgQ::requestBlockingReadRepeat(int32_t count, int32_t numIter) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 85 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 86 | for (int i = 0; i < numIter; i++) { |
| 87 | bool result = mFmqSynchronized->readBlocking( |
| 88 | &data[0], count, static_cast<uint32_t>(EventFlagBits::FMQ_NOT_FULL), |
| 89 | static_cast<uint32_t>(EventFlagBits::FMQ_NOT_EMPTY), 5000000000 /* timeOutNanos */); |
| 90 | |
| 91 | if (result == false) { |
| 92 | ALOGE("Blocking read fails"); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | |
| 100 | ndk::ScopedAStatus TestAidlMsgQ::requestReadFmqSync(int32_t count, bool* _aidl_return) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 101 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 102 | bool result = mFmqSynchronized->read(&data[0], count) && verifyData(&data[0], count); |
| 103 | *_aidl_return = result; |
| 104 | return ndk::ScopedAStatus::ok(); |
| 105 | } |
| 106 | |
| 107 | ndk::ScopedAStatus TestAidlMsgQ::requestReadFmqUnsync(int32_t count, bool* _aidl_return) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 108 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 109 | bool result = mFmqUnsynchronized->read(&data[0], count) && verifyData(&data[0], count); |
| 110 | *_aidl_return = result; |
| 111 | return ndk::ScopedAStatus::ok(); |
| 112 | } |
| 113 | |
| 114 | ndk::ScopedAStatus TestAidlMsgQ::requestWriteFmqSync(int32_t count, bool* _aidl_return) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 115 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 116 | for (int i = 0; i < count; i++) { |
| 117 | data[i] = i; |
| 118 | } |
| 119 | bool result = mFmqSynchronized->write(&data[0], count); |
| 120 | *_aidl_return = result; |
| 121 | return ndk::ScopedAStatus::ok(); |
| 122 | } |
| 123 | |
| 124 | ndk::ScopedAStatus TestAidlMsgQ::requestWriteFmqUnsync(int32_t count, bool* _aidl_return) { |
Devin Moore | 4a2568d | 2020-09-22 17:20:54 +0000 | [diff] [blame] | 125 | std::vector<int32_t> data(count); |
Devin Moore | 133cb5e | 2020-07-07 16:31:22 -0700 | [diff] [blame] | 126 | for (int i = 0; i < count; i++) { |
| 127 | data[i] = i; |
| 128 | } |
| 129 | if (!mFmqUnsynchronized) { |
| 130 | ALOGE("Unsynchronized queue is not configured."); |
| 131 | *_aidl_return = false; |
| 132 | return ndk::ScopedAStatus::ok(); |
| 133 | } |
| 134 | bool result = mFmqUnsynchronized->write(&data[0], count); |
| 135 | *_aidl_return = result; |
| 136 | return ndk::ScopedAStatus::ok(); |
| 137 | } |
| 138 | |
| 139 | } // namespace test |
| 140 | } // namespace fmq |
| 141 | } // namespace android |
| 142 | } // namespace aidl |