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<uint16_t, kSynchronizedReadWrite> MessageQueueSync;
53     typedef MessageQueue<uint16_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<uint16_t>& mqDesc) override;
59     Return<void> getFmqUnsyncWrite(bool configureFmq, getFmqUnsyncWrite_cb _hidl_cb) override;
60     Return<bool> requestWriteFmqSync(int32_t count) override;
61     Return<bool> requestReadFmqSync(int32_t count) override;
62     Return<bool> requestWriteFmqUnsync(int32_t count) override;
63     Return<bool> requestReadFmqUnsync(int32_t count) override;
64     Return<void> requestBlockingRead(int32_t count) override;
65     Return<void> requestBlockingReadDefaultEventFlagBits(int32_t count) override;
66     Return<void> requestBlockingReadRepeat(int32_t count, int32_t numIter) override;
67 
68     // Methods from ::android::hidl::base::V1_0::IBase follow.
69 private:
70     std::unique_ptr<MessageQueueSync> mFmqSynchronized;
71     std::unique_ptr<MessageQueueUnsync> mFmqUnsynchronized;
72 
73     /*
74      * Utility function to verify data read from the fast message queue.
75      */
verifyDataTestMsgQ76     bool verifyData(uint16_t* data, int count) {
77         for (int i = 0; i < count; i++) {
78             if (data[i] != i) return false;
79         }
80         return true;
81     }
82 };
83 
84 extern "C" ITestMsgQ* HIDL_FETCH_ITestMsgQ(const char* name);
85 
86 }  // namespace implementation
87 }  // namespace V1_0
88 }  // namespace msgq
89 }  // namespace tests
90 }  // namespace hardware
91 }  // namespace android
92 
93 #endif  // ANDROID_HARDWARE_TESTS_MSGQ_V1_0_TESTMSGQ_H
94