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