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