• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/macros.h>
20 #include <shared/nano_endian.h>
21 #include <shared/nano_string.h>
22 #include <shared/send_message.h>
23 
24 #include "chre_api/chre.h"
25 
26 namespace general_test {
27 
EstimatedHostTimeTest()28 EstimatedHostTimeTest::EstimatedHostTimeTest()
29     : Test(CHRE_API_VERSION_1_1),
30       mTimerHandle(CHRE_TIMER_INVALID),
31       mRemainingIterations(25) {}
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 =
40       chreTimerSet(timerInterval, &mTimerHandle, false /* oneShot */);
41 
42   if (mTimerHandle == CHRE_TIMER_INVALID) {
43     EXPECT_FAIL_RETURN("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     EXPECT_FAIL_RETURN("Unable to verify increasing time");
76   }
77 }
78 
79 }  // namespace general_test
80