1 /*
2  * Copyright (C) 2018 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/basic_gnss_test.h>
18 
19 #include <chre.h>
20 #include <shared/send_message.h>
21 
22 /*
23  * Test to check expected functionality of the CHRE GNSS APIs.
24  * TODO: Currently the test only exists to verify that expected APIs are
25  *       implemented and doesn't fail. Make the test more comprehensive by
26  *       validating callback results, etc.
27  */
28 namespace general_test {
29 
30 namespace {
31 
32 using nanoapp_testing::sendFatalFailureToHost;
33 
testLocationSessionAsync()34 void testLocationSessionAsync() {
35   if (!chreGnssLocationSessionStartAsync(1000 /* minIntervalMs */,
36                                          0 /* minTimeToNextFixMs */,
37                                          nullptr /* cookie */)) {
38     sendFatalFailureToHost("Failed to start a location session");
39   }
40 }
41 
testMeasurementSessionAsync()42 void testMeasurementSessionAsync() {
43   if (!chreGnssMeasurementSessionStartAsync(1000 /* minIntervalMs */,
44                                             nullptr /* cookie */)) {
45     sendFatalFailureToHost("Failed to start a measurement session");
46   }
47 }
48 
testPassiveListener()49 bool testPassiveListener() {
50   bool success = false;
51   if (!chreGnssConfigurePassiveLocationListener(true /* enable */)) {
52     sendFatalFailureToHost("Failed to enable passive location listener");
53   } else if (!chreGnssConfigurePassiveLocationListener(false /* enable */)) {
54     sendFatalFailureToHost("Failed to disable passive location listener");
55   } else {
56     success = true;
57   }
58 
59   return success;
60 }
61 
62 }  // anonymous namespace
63 
BasicGnssTest()64 BasicGnssTest::BasicGnssTest() : Test(CHRE_API_VERSION_1_1) {}
65 
setUp(uint32_t messageSize,const void *)66 void BasicGnssTest::setUp(uint32_t messageSize, const void * /* message */) {
67   if (messageSize != 0) {
68     sendFatalFailureToHost("Expected 0 byte message, got more bytes:",
69                            &messageSize);
70   } else {
71     uint32_t capabilities = chreGnssGetCapabilities();
72 
73     if (capabilities & CHRE_GNSS_CAPABILITIES_LOCATION) {
74       testLocationSessionAsync();
75     } else {
76       mTestSuccessMarker.markStageAndSuccessOnFinish(
77           BASIC_GNSS_TEST_STAGE_LOCATION);
78     }
79 
80     if (capabilities & CHRE_GNSS_CAPABILITIES_MEASUREMENTS) {
81       testMeasurementSessionAsync();
82     } else {
83       mTestSuccessMarker.markStageAndSuccessOnFinish(
84           BASIC_GNSS_TEST_STAGE_MEASUREMENT);
85     }
86 
87     if (((mApiVersion < CHRE_API_VERSION_1_5) &&
88          (capabilities &
89           CHRE_GNSS_CAPABILITIES_GNSS_ENGINE_BASED_PASSIVE_LISTENER) == 0) ||
90         testPassiveListener()) {
91       mTestSuccessMarker.markStageAndSuccessOnFinish(
92           BASIC_GNSS_TEST_STAGE_LISTENER);
93     }
94   }
95 }
96 
handleGnssAsyncResult(const chreAsyncResult * result)97 void BasicGnssTest::handleGnssAsyncResult(const chreAsyncResult *result) {
98   if (!result->success) {
99     sendFatalFailureToHost("Received unsuccessful GNSS async result");
100   }
101 
102   switch (result->requestType) {
103     case CHRE_GNSS_REQUEST_TYPE_LOCATION_SESSION_START: {
104       if (!chreGnssLocationSessionStopAsync(nullptr /* cookie */)) {
105         sendFatalFailureToHost("Failed to stop a location session");
106       }
107       break;
108     }
109     case CHRE_GNSS_REQUEST_TYPE_LOCATION_SESSION_STOP: {
110       mTestSuccessMarker.markStageAndSuccessOnFinish(
111           BASIC_GNSS_TEST_STAGE_LOCATION);
112       break;
113     }
114     case CHRE_GNSS_REQUEST_TYPE_MEASUREMENT_SESSION_START: {
115       if (!chreGnssMeasurementSessionStopAsync(nullptr /* cookie */)) {
116         sendFatalFailureToHost("Failed to stop a measurement session");
117       }
118       break;
119     }
120     case CHRE_GNSS_REQUEST_TYPE_MEASUREMENT_SESSION_STOP: {
121       mTestSuccessMarker.markStageAndSuccessOnFinish(
122           BASIC_GNSS_TEST_STAGE_MEASUREMENT);
123       break;
124     }
125     default:
126       sendFatalFailureToHost("Unexpected request type");
127       break;
128   }
129 }
130 
handleEvent(uint32_t,uint16_t eventType,const void * eventData)131 void BasicGnssTest::handleEvent(uint32_t /* senderInstanceId */,
132                                 uint16_t eventType, const void *eventData) {
133   switch (eventType) {
134     case CHRE_EVENT_GNSS_ASYNC_RESULT:
135       handleGnssAsyncResult(static_cast<const chreAsyncResult *>(eventData));
136       break;
137     case CHRE_EVENT_GNSS_LOCATION:
138     case CHRE_EVENT_GNSS_DATA:
139       // Do nothing
140       break;
141     default:
142       unexpectedEvent(eventType);
143       break;
144   }
145 }
146 
147 }  // namespace general_test
148