1 /*
2  * Copyright (C) 2017 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 #ifndef ANDROID_HARDWARE_TESTS_MSGQ_V1_0_TESTMSGQ_H
18 #define ANDROID_HARDWARE_TESTS_MSGQ_V1_0_TESTMSGQ_H
19 
20 #include <android/hardware/tests/msgq/1.0/ITestMsgQ.h>
21 #include <hidl/MQDescriptor.h>
22 #include <hidl/Status.h>
23 #include <fmq/MessageQueue.h>
24 #include <fmq/EventFlag.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace tests {
29 namespace msgq {
30 namespace V1_0 {
31 namespace implementation {
32 
33 using ::android::hardware::tests::msgq::V1_0::ITestMsgQ;
34 using ::android::hidl::base::V1_0::DebugInfo;
35 using ::android::hidl::base::V1_0::IBase;
36 using ::android::hardware::hidl_array;
37 using ::android::hardware::hidl_memory;
38 using ::android::hardware::hidl_string;
39 using ::android::hardware::hidl_vec;
40 using ::android::hardware::Return;
41 using ::android::hardware::Void;
42 using ::android::sp;
43 
44 using android::hardware::kSynchronizedReadWrite;
45 using android::hardware::kUnsynchronizedWrite;
46 using android::hardware::MQDescriptorSync;
47 using android::hardware::MQDescriptorUnsync;
48 
49 using android::hardware::MessageQueue;
50 
51 struct TestMsgQ : public ITestMsgQ {
52     typedef MessageQueue<int32_t, kSynchronizedReadWrite> MessageQueueSync;
53     typedef MessageQueue<int32_t, kUnsynchronizedWrite> MessageQueueUnsync;
54 
TestMsgQTestMsgQ55     TestMsgQ() : mFmqSynchronized(nullptr), mFmqUnsynchronized(nullptr) {}
56 
57     // Methods from ::android::hardware::tests::msgq::V1_0::ITestMsgQ follow.
58     Return<bool> configureFmqSyncReadWrite(const MQDescriptorSync<int32_t>& mqDesc) override;
59     Return<void> getFmqUnsyncWrite(bool configureFmq, bool userFd,
60                                    getFmqUnsyncWrite_cb _hidl_cb) override;
61     Return<bool> requestWriteFmqSync(int32_t count) override;
62     Return<bool> requestReadFmqSync(int32_t count) override;
63     Return<bool> requestWriteFmqUnsync(int32_t count) override;
64     Return<bool> requestReadFmqUnsync(int32_t count) override;
65     Return<void> requestBlockingRead(int32_t count) override;
66     Return<void> requestBlockingReadDefaultEventFlagBits(int32_t count) override;
67     Return<void> requestBlockingReadRepeat(int32_t count, int32_t numIter) override;
68 
69     // Methods from ::android::hidl::base::V1_0::IBase follow.
70 private:
71     std::unique_ptr<MessageQueueSync> mFmqSynchronized;
72     std::unique_ptr<MessageQueueUnsync> mFmqUnsynchronized;
73 
74     /*
75      * Utility function to verify data read from the fast message queue.
76      */
verifyDataTestMsgQ77     bool verifyData(int32_t* data, int count) {
78         for (int i = 0; i < count; i++) {
79             if (data[i] != i) return false;
80         }
81         return true;
82     }
83 };
84 
85 extern "C" ITestMsgQ* HIDL_FETCH_ITestMsgQ(const char* name);
86 
87 }  // namespace implementation
88 }  // namespace V1_0
89 }  // namespace msgq
90 }  // namespace tests
91 }  // namespace hardware
92 }  // namespace android
93 
94 #endif  // ANDROID_HARDWARE_TESTS_MSGQ_V1_0_TESTMSGQ_H
95