1 /*
2 * Copyright (C) 2016 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 <general_test/get_time_test.h>
18
19 #include <cstddef>
20
21 #include <shared/abort.h>
22 #include <shared/nano_endian.h>
23 #include <shared/send_message.h>
24
25 #include <chre.h>
26
27 using nanoapp_testing::MessageType;
28 using nanoapp_testing::sendFatalFailureToHost;
29
30 namespace general_test {
31
GetTimeTest()32 GetTimeTest::GetTimeTest() : Test(CHRE_API_VERSION_1_0), mContinueCount(0) {}
33
setUp(uint32_t messageSize,const void *)34 void GetTimeTest::setUp(uint32_t messageSize, const void * /* message */) {
35 if (messageSize != 0) {
36 sendFatalFailureToHost("GetTime message expects 0 additional bytes, got ",
37 &messageSize);
38 }
39
40 uint64_t firstTime = chreGetTime();
41 if (firstTime == UINT64_C(0)) {
42 sendFatalFailureToHost("chreGetTime() gave 0 well after system boot.");
43 }
44
45 uint64_t prevTime = firstTime;
46
47 // Continuously call chreGetTime() to ensure timestamps are monotonically
48 // increasing and increases at some point after a large number of calls.
49 constexpr size_t kMaxGetTimeCount = 10000;
50 for (size_t i = 0; (prevTime == firstTime && i < kMaxGetTimeCount); i++) {
51 uint64_t nextTime = chreGetTime();
52
53 // We don't require this to have increased, because maybe we're
54 // on a relatively fast processor, or have a low resolution clock.
55 if (nextTime < prevTime) {
56 sendFatalFailureToHost("chreGetTime() is not monotonically increasing");
57 }
58
59 prevTime = nextTime;
60 }
61
62 if (prevTime == firstTime) {
63 sendFatalFailureToHost(
64 "chreGetTime() is not increasing after a large number of calls");
65 }
66
67 prevTime = nanoapp_testing::hostToLittleEndian(prevTime);
68 sendMessageToHost(MessageType::kContinue, &prevTime, sizeof(prevTime));
69
70 // Now we'll wait to get a 'continue' from the host.
71 }
72
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)73 void GetTimeTest::handleEvent(uint32_t senderInstanceId, uint16_t eventType,
74 const void *eventData) {
75 // We ignore the return value, since we expect no data.
76 getMessageDataFromHostEvent(senderInstanceId, eventType, eventData,
77 MessageType::kContinue, 0);
78 if (mContinueCount > 0) {
79 sendFatalFailureToHost("Multiple kContinue messages sent");
80 }
81
82 mContinueCount++;
83 uint64_t time = nanoapp_testing::hostToLittleEndian(chreGetTime());
84 sendMessageToHost(MessageType::kContinue, &time, sizeof(time));
85 // We do nothing else in the CHRE. It's up to the Host to declare
86 // if we've passed.
87 }
88
89 } // namespace general_test
90