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 <general_test/estimated_host_time_test.h>
18
19 #include <shared/nano_endian.h>
20 #include <shared/nano_string.h>
21 #include <shared/send_message.h>
22
23 #include <chre.h>
24
25 namespace general_test {
26
EstimatedHostTimeTest()27 EstimatedHostTimeTest::EstimatedHostTimeTest()
28 : Test(CHRE_API_VERSION_1_1),
29 mTimerHandle(CHRE_TIMER_INVALID),
30 mRemainingIterations(25) {}
31
setUp(uint32_t,const void *)32 void EstimatedHostTimeTest::setUp(uint32_t /* messageSize */,
33 const void * /* message */) {
34 mPriorHostTime = chreGetEstimatedHostTime();
35
36 constexpr uint64_t timerInterval = 100000000; // 100 ms
37
38 mTimerHandle =
39 chreTimerSet(timerInterval, &mTimerHandle, false /* oneShot */);
40
41 if (mTimerHandle == CHRE_TIMER_INVALID) {
42 nanoapp_testing::sendFatalFailureToHost(
43 "Unable to set timer for time verification");
44 }
45 }
46
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)47 void EstimatedHostTimeTest::handleEvent(uint32_t senderInstanceId,
48 uint16_t eventType,
49 const void *eventData) {
50 if (eventType == CHRE_EVENT_TIMER) {
51 verifyIncreasingTime();
52 } else {
53 // Verify application processor time is within reason
54 uint64_t currentHostTime = chreGetEstimatedHostTime();
55
56 // TODO: Estimate message RTT to allow stricter accuracy check
57 constexpr uint64_t timeDelta = 50000000; // 50 ms
58
59 uint64_t givenHostTime;
60 const void *message = getMessageDataFromHostEvent(
61 senderInstanceId, eventType, eventData,
62 nanoapp_testing::MessageType::kContinue, sizeof(givenHostTime));
63
64 nanoapp_testing::memcpy(&givenHostTime, message, sizeof(givenHostTime));
65 givenHostTime = nanoapp_testing::littleEndianToHost(givenHostTime);
66
67 if (currentHostTime >= givenHostTime) {
68 if ((currentHostTime - givenHostTime) <= timeDelta) {
69 nanoapp_testing::sendSuccessToHost();
70 } else {
71 nanoapp_testing::sendFatalFailureToHost(
72 "Current time is too far behind of host time");
73 }
74 } else if ((givenHostTime - currentHostTime) <= timeDelta) {
75 nanoapp_testing::sendSuccessToHost();
76 } else {
77 nanoapp_testing::sendFatalFailureToHost(
78 "Current time is too far ahead of host time");
79 }
80 }
81 }
82
verifyIncreasingTime()83 void EstimatedHostTimeTest::verifyIncreasingTime() {
84 if (mRemainingIterations > 0) {
85 uint64_t currentHostTime = chreGetEstimatedHostTime();
86
87 if (currentHostTime > mPriorHostTime) {
88 chreTimerCancel(mTimerHandle);
89 nanoapp_testing::sendMessageToHost(
90 nanoapp_testing::MessageType::kContinue);
91 } else {
92 mPriorHostTime = currentHostTime;
93 }
94
95 --mRemainingIterations;
96 } else {
97 nanoapp_testing::sendFatalFailureToHost("Unable to verify increasing time");
98 }
99 }
100
101 } // namespace general_test
102