blob: baec3e612f707a8320b47c3edb1a684468575b3a [file] [log] [blame]
Devin Moore133cb5e2020-07-07 16:31:22 -07001/*
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
19namespace aidl {
20namespace android {
21namespace fmq {
22namespace test {
23
24// Methods from ::aidl::android::fmq::test::ITestAidlMsgQ follow.
Devin Moore1b2d0a62020-09-22 17:20:54 +000025ndk::ScopedAStatus TestAidlMsgQ::configureFmqSyncReadWrite(
26 const MQDescriptor<int32_t, SynchronizedReadWrite>& mqDesc, bool* _aidl_return) {
Devin Moore133cb5e2020-07-07 16:31:22 -070027 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 Moore1b2d0a62020-09-22 17:20:54 +000042ndk::ScopedAStatus TestAidlMsgQ::getFmqUnsyncWrite(
43 bool configureFmq, MQDescriptor<int32_t, UnsynchronizedWrite>* mqDesc, bool* _aidl_return) {
Devin Moore133cb5e2020-07-07 16:31:22 -070044 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
61ndk::ScopedAStatus TestAidlMsgQ::requestBlockingRead(int32_t count) {
Devin Moore4a2568d2020-09-22 17:20:54 +000062 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -070063 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
73ndk::ScopedAStatus TestAidlMsgQ::requestBlockingReadDefaultEventFlagBits(int32_t count) {
Devin Moore4a2568d2020-09-22 17:20:54 +000074 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -070075 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
84ndk::ScopedAStatus TestAidlMsgQ::requestBlockingReadRepeat(int32_t count, int32_t numIter) {
Devin Moore4a2568d2020-09-22 17:20:54 +000085 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -070086 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
100ndk::ScopedAStatus TestAidlMsgQ::requestReadFmqSync(int32_t count, bool* _aidl_return) {
Devin Moore4a2568d2020-09-22 17:20:54 +0000101 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -0700102 bool result = mFmqSynchronized->read(&data[0], count) && verifyData(&data[0], count);
103 *_aidl_return = result;
104 return ndk::ScopedAStatus::ok();
105}
106
107ndk::ScopedAStatus TestAidlMsgQ::requestReadFmqUnsync(int32_t count, bool* _aidl_return) {
Devin Moore4a2568d2020-09-22 17:20:54 +0000108 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -0700109 bool result = mFmqUnsynchronized->read(&data[0], count) && verifyData(&data[0], count);
110 *_aidl_return = result;
111 return ndk::ScopedAStatus::ok();
112}
113
114ndk::ScopedAStatus TestAidlMsgQ::requestWriteFmqSync(int32_t count, bool* _aidl_return) {
Devin Moore4a2568d2020-09-22 17:20:54 +0000115 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -0700116 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
124ndk::ScopedAStatus TestAidlMsgQ::requestWriteFmqUnsync(int32_t count, bool* _aidl_return) {
Devin Moore4a2568d2020-09-22 17:20:54 +0000125 std::vector<int32_t> data(count);
Devin Moore133cb5e2020-07-07 16:31:22 -0700126 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