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<int32_t> & mqDesc)27 Return<bool> TestMsgQ::configureFmqSyncReadWrite(
28         const android::hardware::MQDescriptorSync<int32_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,bool userFd,getFmqUnsyncWrite_cb _hidl_cb)44 Return<void> TestMsgQ::getFmqUnsyncWrite(bool configureFmq, bool userFd,
45                                          getFmqUnsyncWrite_cb _hidl_cb) {
46     if (configureFmq) {
47         static constexpr size_t kNumElementsInQueue = 1024;
48         static constexpr size_t kElementSizeBytes = sizeof(int32_t);
49         android::base::unique_fd ringbufferFd;
50         if (userFd) {
51             ringbufferFd.reset(
52                     ::ashmem_create_region("UnsyncWrite", kNumElementsInQueue * kElementSizeBytes));
53         }
54         mFmqUnsynchronized.reset(new (std::nothrow) MessageQueueUnsync(
55                 kNumElementsInQueue, false, std::move(ringbufferFd),
56                 kNumElementsInQueue * kElementSizeBytes));
57     }
58     if ((mFmqUnsynchronized == nullptr) ||
59         (mFmqUnsynchronized->isValid() == false)) {
60         _hidl_cb(false /* ret */, MessageQueueUnsync::Descriptor());
61     } else {
62         _hidl_cb(true /* ret */, *mFmqUnsynchronized->getDesc());
63     }
64     return Void();
65 }
66 
requestWriteFmqSync(int32_t count)67 Return<bool> TestMsgQ::requestWriteFmqSync(int32_t count) {
68     std::vector<int32_t> data(count);
69     for (int i = 0; i < count; i++) {
70         data[i] = i;
71     }
72     bool result = mFmqSynchronized->write(&data[0], count);
73     return result;
74 }
75 
requestReadFmqSync(int32_t count)76 Return<bool> TestMsgQ::requestReadFmqSync(int32_t count) {
77     std::vector<int32_t> data(count);
78     bool result = mFmqSynchronized->read(&data[0], count)
79             && verifyData(&data[0], count);
80     return result;
81 }
82 
requestWriteFmqUnsync(int32_t count)83 Return<bool> TestMsgQ::requestWriteFmqUnsync(int32_t count) {
84     std::vector<int32_t> data(count);
85     for (int i = 0; i < count; i++) {
86         data[i] = i;
87     }
88     bool result = mFmqUnsynchronized->write(&data[0], count);
89     return result;
90 }
91 
requestReadFmqUnsync(int32_t count)92 Return<bool> TestMsgQ::requestReadFmqUnsync(int32_t count) {
93     std::vector<int32_t> data(count);
94     bool result =
95             mFmqUnsynchronized->read(&data[0], count) && verifyData(&data[0], count);
96     return result;
97 }
98 
requestBlockingRead(int32_t count)99 Return<void> TestMsgQ::requestBlockingRead(int32_t count) {
100     std::vector<int32_t> data(count);
101     bool result = mFmqSynchronized->readBlocking(
102             &data[0],
103             count,
104             static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_FULL),
105             static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_EMPTY),
106             5000000000 /* timeOutNanos */);
107 
108     if (result == false) {
109         ALOGE("Blocking read fails");
110     }
111     return Void();
112 }
113 
requestBlockingReadDefaultEventFlagBits(int32_t count)114 Return<void> TestMsgQ::requestBlockingReadDefaultEventFlagBits(int32_t count) {
115     std::vector<int32_t> data(count);
116     bool result = mFmqSynchronized->readBlocking(
117             &data[0],
118             count);
119 
120     if (result == false) {
121         ALOGE("Blocking read fails");
122     }
123 
124     return Void();
125 }
126 
requestBlockingReadRepeat(int32_t count,int32_t numIter)127 Return<void> TestMsgQ::requestBlockingReadRepeat(int32_t count, int32_t numIter) {
128     std::vector<int32_t> data(count);
129     for (int i = 0; i < numIter; i++) {
130         bool result = mFmqSynchronized->readBlocking(
131                 &data[0],
132                 count,
133                 static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_FULL),
134                 static_cast<uint32_t>(ITestMsgQ::EventFlagBits::FMQ_NOT_EMPTY),
135                 5000000000 /* timeOutNanos */);
136 
137         if (result == false) {
138             ALOGE("Blocking read fails");
139             break;
140         }
141     }
142     return Void();
143 }
144 
145 
146 // Methods from ::android::hidl::base::V1_0::IBase follow.
147 
HIDL_FETCH_ITestMsgQ(const char *)148 ITestMsgQ* HIDL_FETCH_ITestMsgQ(const char* /* name */) {
149     return new TestMsgQ();
150 }
151 
152 }  // namespace implementation
153 }  // namespace V1_0
154 }  // namespace msgq
155 }  // namespace tests
156 }  // namespace hardware
157 }  // namespace android
158