xref: /aosp_15_r20/system/chre/apps/test/chqts/src/general_test/estimated_host_time_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
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_api/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,uint16_t eventType,const void *)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     // Send the estimated host time to the AP for validation.
54     uint64_t currentHostTime = chreGetEstimatedHostTime();
55     nanoapp_testing::sendMessageToHost(nanoapp_testing::MessageType::kContinue,
56                                        &currentHostTime,
57                                        sizeof(currentHostTime));
58   }
59 }
60 
verifyIncreasingTime()61 void EstimatedHostTimeTest::verifyIncreasingTime() {
62   if (mRemainingIterations > 0) {
63     uint64_t currentHostTime = chreGetEstimatedHostTime();
64 
65     if (currentHostTime > mPriorHostTime) {
66       chreTimerCancel(mTimerHandle);
67       nanoapp_testing::sendMessageToHost(
68           nanoapp_testing::MessageType::kContinue);
69     } else {
70       mPriorHostTime = currentHostTime;
71     }
72 
73     --mRemainingIterations;
74   } else {
75     nanoapp_testing::sendFatalFailureToHost("Unable to verify increasing time");
76   }
77 }
78 
79 }  // namespace general_test
80