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