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 #include "TestMsgQ.h"
18 
19 namespace android {
20 namespace hardware {
21 namespace tests {
22 namespace msgq {
23 namespace V1_0 {
24 namespace implementation {
25 
26 // Methods from ::android::hardware::tests::msgq::V1_0::ITestMsgQ follow.
configureFmqSyncReadWrite(const android::hardware::MQDescriptorSync<uint16_t> & mqDesc)27 Return<bool> TestMsgQ::configureFmqSyncReadWrite(
28     const android::hardware::MQDescriptorSync<uint16_t>& mqDesc) {
29     mFmqSynchronized.reset(new (std::nothrow) MessageQueueSync(mqDesc));
30     if ((mFmqSynchronized == nullptr) || (mFmqSynchronized->isValid() == false)) {
31         return false;
32     }
33     /*
34      * Initialize the EventFlag word with bit FMQ_NOT_FULL.
35      */
36     auto evFlagWordPtr = mFmqSynchronized->getEventFlagWord();
37     if (evFlagWordPtr != nullptr) {
38         std::atomic_init(evFlagWordPtr,
39                          static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_FULL));
40     }
41     return true;
42 }
43 
getFmqUnsyncWrite(bool configureFmq,getFmqUnsyncWrite_cb _hidl_cb)44 Return<void> TestMsgQ::getFmqUnsyncWrite(bool configureFmq, getFmqUnsyncWrite_cb _hidl_cb) {
45     if (configureFmq) {
46         static constexpr size_t kNumElementsInQueue = 1024;
47         mFmqUnsynchronized.reset(new (std::nothrow) MessageQueueUnsync(kNumElementsInQueue));
48     }
49     if ((mFmqUnsynchronized == nullptr) ||
50         (mFmqUnsynchronized->isValid() == false)) {
51         _hidl_cb(false /* ret */, MessageQueueUnsync::Descriptor());
52     } else {
53         _hidl_cb(true /* ret */, *mFmqUnsynchronized->getDesc());
54     }
55     return Void();
56 }
57 
requestWriteFmqSync(int32_t count)58 Return<bool> TestMsgQ::requestWriteFmqSync(int32_t count) {
59     std::vector<uint16_t> data(count);
60     for (int i = 0; i < count; i++) {
61         data[i] = i;
62     }
63     bool result = mFmqSynchronized->write(&data[0], count);
64     return result;
65 }
66 
requestReadFmqSync(int32_t count)67 Return<bool> TestMsgQ::requestReadFmqSync(int32_t count) {
68     std::vector<uint16_t> data(count);
69     bool result = mFmqSynchronized->read(&data[0], count)
70             && verifyData(&data[0], count);
71     return result;
72 }
73 
requestWriteFmqUnsync(int32_t count)74 Return<bool> TestMsgQ::requestWriteFmqUnsync(int32_t count) {
75     std::vector<uint16_t> data(count);
76     for (int i = 0; i < count; i++) {
77         data[i] = i;
78     }
79     bool result = mFmqUnsynchronized->write(&data[0], count);
80     return result;
81 }
82 
requestReadFmqUnsync(int32_t count)83 Return<bool> TestMsgQ::requestReadFmqUnsync(int32_t count) {
84     std::vector<uint16_t> data(count);
85     bool result =
86             mFmqUnsynchronized->read(&data[0], count) && verifyData(&data[0], count);
87     return result;
88 }
89 
requestBlockingRead(int32_t count)90 Return<void> TestMsgQ::requestBlockingRead(int32_t count) {
91     std::vector<uint16_t> data(count);
92     bool result = mFmqSynchronized->readBlocking(
93             &data[0],
94             count,
95             static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_FULL),
96             static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_EMPTY),
97             5000000000 /* timeOutNanos */);
98 
99     if (result == false) {
100         ALOGE("Blocking read fails");
101     }
102     return Void();
103 }
104 
requestBlockingReadDefaultEventFlagBits(int32_t count)105 Return<void> TestMsgQ::requestBlockingReadDefaultEventFlagBits(int32_t count) {
106     std::vector<uint16_t> data(count);
107     bool result = mFmqSynchronized->readBlocking(
108             &data[0],
109             count);
110 
111     if (result == false) {
112         ALOGE("Blocking read fails");
113     }
114 
115     return Void();
116 }
117 
requestBlockingReadRepeat(int32_t count,int32_t numIter)118 Return<void> TestMsgQ::requestBlockingReadRepeat(int32_t count, int32_t numIter) {
119     std::vector<uint16_t> data(count);
120     for (int i = 0; i < numIter; i++) {
121         bool result = mFmqSynchronized->readBlocking(
122                 &data[0],
123                 count,
124                 static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_FULL),
125                 static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_EMPTY),
126                 5000000000 /* timeOutNanos */);
127 
128         if (result == false) {
129             ALOGE("Blocking read fails");
130             break;
131         }
132     }
133     return Void();
134 }
135 
136 
137 // Methods from ::android::hidl::base::V1_0::IBase follow.
138 
HIDL_FETCH_ITestMsgQ(const char *)139 ITestMsgQ* HIDL_FETCH_ITestMsgQ(const char* /* name */) {
140     return new TestMsgQ();
141 }
142 
143 }  // namespace implementation
144 }  // namespace V1_0
145 }  // namespace msgq
146 }  // namespace tests
147 }  // namespace hardware
148 }  // namespace android
149