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.
configureFmqSyncReadWrite(const MQDescriptor<int32_t,SynchronizedReadWrite> & mqDesc,bool * _aidl_return)25 ndk::ScopedAStatus TestAidlMsgQ::configureFmqSyncReadWrite(
26         const MQDescriptor<int32_t, SynchronizedReadWrite>& mqDesc, bool* _aidl_return) {
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 
getFmqUnsyncWrite(bool configureFmq,bool userFd,MQDescriptor<int32_t,UnsynchronizedWrite> * mqDesc,bool * _aidl_return)42 ndk::ScopedAStatus TestAidlMsgQ::getFmqUnsyncWrite(
43         bool configureFmq, bool userFd, MQDescriptor<int32_t, UnsynchronizedWrite>* mqDesc,
44         bool* _aidl_return) {
45     if (configureFmq) {
46         static constexpr size_t kNumElementsInQueue = 1024;
47         static constexpr size_t kElementSizeBytes = sizeof(int32_t);
48         ::android::base::unique_fd ringbufferFd;
49         if (userFd) {
50             ringbufferFd.reset(
51                     ::ashmem_create_region("UnsyncWrite", kNumElementsInQueue * kElementSizeBytes));
52         }
53         mFmqUnsynchronized.reset(new (std::nothrow) TestAidlMsgQ::MessageQueueUnsync(
54                 kNumElementsInQueue, false, std::move(ringbufferFd),
55                 kNumElementsInQueue * kElementSizeBytes));
56     }
57 
58     if ((mFmqUnsynchronized == nullptr) || (mFmqUnsynchronized->isValid() == false) ||
59         (mqDesc == nullptr)) {
60         *_aidl_return = false;
61     } else {
62         *mqDesc = std::move(mFmqUnsynchronized->dupeDesc());
63         *_aidl_return = true;
64     }
65 
66     return ndk::ScopedAStatus::ok();
67 }
68 
requestBlockingRead(int32_t count)69 ndk::ScopedAStatus TestAidlMsgQ::requestBlockingRead(int32_t count) {
70     std::vector<int32_t> data(count);
71     bool result = mFmqSynchronized->readBlocking(
72             &data[0], count, static_cast<uint32_t>(EventFlagBits::FMQ_NOT_FULL),
73             static_cast<uint32_t>(EventFlagBits::FMQ_NOT_EMPTY), 5000000000 /* timeOutNanos */);
74 
75     if (result == false) {
76         ALOGE("Blocking read fails");
77     }
78     return ndk::ScopedAStatus::ok();
79 }
80 
requestBlockingReadDefaultEventFlagBits(int32_t count)81 ndk::ScopedAStatus TestAidlMsgQ::requestBlockingReadDefaultEventFlagBits(int32_t count) {
82     std::vector<int32_t> data(count);
83     bool result = mFmqSynchronized->readBlocking(&data[0], count);
84 
85     if (result == false) {
86         ALOGE("Blocking read fails");
87     }
88 
89     return ndk::ScopedAStatus::ok();
90 }
91 
requestBlockingReadRepeat(int32_t count,int32_t numIter)92 ndk::ScopedAStatus TestAidlMsgQ::requestBlockingReadRepeat(int32_t count, int32_t numIter) {
93     std::vector<int32_t> data(count);
94     for (int i = 0; i < numIter; i++) {
95         bool result = mFmqSynchronized->readBlocking(
96                 &data[0], count, static_cast<uint32_t>(EventFlagBits::FMQ_NOT_FULL),
97                 static_cast<uint32_t>(EventFlagBits::FMQ_NOT_EMPTY), 5000000000 /* timeOutNanos */);
98 
99         if (result == false) {
100             ALOGE("Blocking read fails");
101             break;
102         }
103     }
104 
105     return ndk::ScopedAStatus::ok();
106 }
107 
requestReadFmqSync(int32_t count,bool * _aidl_return)108 ndk::ScopedAStatus TestAidlMsgQ::requestReadFmqSync(int32_t count, bool* _aidl_return) {
109     std::vector<int32_t> data(count);
110     bool result = mFmqSynchronized->read(&data[0], count) && verifyData(&data[0], count);
111     *_aidl_return = result;
112     return ndk::ScopedAStatus::ok();
113 }
114 
requestReadFmqUnsync(int32_t count,bool * _aidl_return)115 ndk::ScopedAStatus TestAidlMsgQ::requestReadFmqUnsync(int32_t count, bool* _aidl_return) {
116     std::vector<int32_t> data(count);
117     bool result = mFmqUnsynchronized->read(&data[0], count) && verifyData(&data[0], count);
118     *_aidl_return = result;
119     return ndk::ScopedAStatus::ok();
120 }
121 
requestWriteFmqSync(int32_t count,bool * _aidl_return)122 ndk::ScopedAStatus TestAidlMsgQ::requestWriteFmqSync(int32_t count, bool* _aidl_return) {
123     std::vector<int32_t> data(count);
124     for (int i = 0; i < count; i++) {
125         data[i] = i;
126     }
127     bool result = mFmqSynchronized->write(&data[0], count);
128     *_aidl_return = result;
129     return ndk::ScopedAStatus::ok();
130 }
131 
requestWriteFmqUnsync(int32_t count,bool * _aidl_return)132 ndk::ScopedAStatus TestAidlMsgQ::requestWriteFmqUnsync(int32_t count, bool* _aidl_return) {
133     std::vector<int32_t> data(count);
134     for (int i = 0; i < count; i++) {
135         data[i] = i;
136     }
137     if (!mFmqUnsynchronized) {
138         ALOGE("Unsynchronized queue is not configured.");
139         *_aidl_return = false;
140         return ndk::ScopedAStatus::ok();
141     }
142     bool result = mFmqUnsynchronized->write(&data[0], count);
143     *_aidl_return = result;
144     return ndk::ScopedAStatus::ok();
145 }
146 
147 }  // namespace test
148 }  // namespace fmq
149 }  // namespace android
150 }  // namespace aidl
151