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 #pragma once
18 
19 #include <aidl/android/fmq/test/BnTestAidlMsgQ.h>
20 #include <aidl/android/fmq/test/EventFlagBits.h>
21 #include <fmq/AidlMessageQueue.h>
22 #include <fmq/EventFlag.h>
23 
24 namespace aidl {
25 namespace android {
26 namespace fmq {
27 namespace test {
28 
29 using ::aidl::android::fmq::test::EventFlagBits;
30 using ::aidl::android::fmq::test::ITestAidlMsgQ;
31 
32 using ::aidl::android::hardware::common::fmq::MQDescriptor;
33 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
34 using ::aidl::android::hardware::common::fmq::UnsynchronizedWrite;
35 using ::android::hardware::kSynchronizedReadWrite;
36 using ::android::hardware::kUnsynchronizedWrite;
37 using ::android::hardware::MQFlavor;
38 
39 using ::android::AidlMessageQueue;
40 
41 struct TestAidlMsgQ : public BnTestAidlMsgQ {
42     typedef AidlMessageQueue<int32_t, SynchronizedReadWrite> MessageQueueSync;
43     typedef AidlMessageQueue<int32_t, UnsynchronizedWrite> MessageQueueUnsync;
44 
TestAidlMsgQTestAidlMsgQ45     TestAidlMsgQ() : mFmqSynchronized(nullptr), mFmqUnsynchronized(nullptr) {}
46 
47     // Methods from ::aidl::android::fmq::test::ITestAidlMsgQ follow.
48     ndk::ScopedAStatus configureFmqSyncReadWrite(
49             const MQDescriptor<int32_t, SynchronizedReadWrite>& mqDesc,
50             bool* _aidl_return) override;
51     ndk::ScopedAStatus getFmqUnsyncWrite(bool configureFmq, bool userFd,
52                                          MQDescriptor<int32_t, UnsynchronizedWrite>* mqDesc,
53                                          bool* _aidl_return) override;
54     ndk::ScopedAStatus requestBlockingRead(int32_t count) override;
55     ndk::ScopedAStatus requestBlockingReadDefaultEventFlagBits(int32_t count) override;
56     ndk::ScopedAStatus requestBlockingReadRepeat(int32_t count, int32_t numIter) override;
57     ndk::ScopedAStatus requestReadFmqSync(int32_t count, bool* _aidl_return) override;
58     ndk::ScopedAStatus requestReadFmqUnsync(int32_t count, bool* _aidl_return) override;
59     ndk::ScopedAStatus requestWriteFmqSync(int32_t count, bool* _aidl_return) override;
60     ndk::ScopedAStatus requestWriteFmqUnsync(int32_t count, bool* _aidl_return) override;
61 
62   private:
63     std::unique_ptr<MessageQueueSync> mFmqSynchronized;
64     std::unique_ptr<MessageQueueUnsync> mFmqUnsynchronized;
65 
66     /*
67      * Utility function to verify data read from the fast message queue.
68      */
verifyDataTestAidlMsgQ69     bool verifyData(int32_t* data, int count) {
70         for (int i = 0; i < count; i++) {
71             if (data[i] != i) return false;
72         }
73         return true;
74     }
75 };
76 
77 }  // namespace test
78 }  // namespace fmq
79 }  // namespace android
80 }  // namespace aidl
81