xref: /aosp_15_r20/system/chre/chpp/test/gnss_test.cpp (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2021 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 <gtest/gtest.h>
18 
19 #include "chre/pal/gnss.h"
20 #include "chre/platform/shared/pal_system_api.h"
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <thread>
25 
26 #include "app_test_base.h"
27 #include "chpp/clients/gnss.h"
28 #include "chpp/log.h"
29 #include "chpp/platform/platform_gnss.h"
30 
31 /*
32  * Test suite for the CHPP GNSS client/service
33  */
34 namespace chpp {
35 namespace {
36 
37 class ChppGnssTest : public AppTestBase {};
38 
39 const struct chrePalGnssApi *gApi;
40 
chrePalRequestStateResync()41 void chrePalRequestStateResync() {}
42 
chrePalLocationStatusChangeCallback(bool,uint8_t)43 void chrePalLocationStatusChangeCallback(bool /* enabled */,
44                                          uint8_t /* errorCode */) {}
45 
chrePalLocationEventCallback(struct chreGnssLocationEvent * event)46 void chrePalLocationEventCallback(struct chreGnssLocationEvent *event) {
47   CHPP_LOGI("Got location event");
48   gApi->releaseLocationEvent(event);
49 }
50 
chrePalMeasurementStatusChangeCallback(bool,uint8_t)51 void chrePalMeasurementStatusChangeCallback(bool /* enabled */,
52                                             uint8_t /*  errorCode */) {}
53 
chrePalMeasurementEventCallback(struct chreGnssDataEvent * event)54 void chrePalMeasurementEventCallback(struct chreGnssDataEvent *event) {
55   CHPP_LOGI("Got measurement event");
56   gApi->releaseMeasurementDataEvent(event);
57 }
58 
TEST_F(ChppGnssTest,SimpleGnss)59 TEST_F(ChppGnssTest, SimpleGnss) {
60   gApi = chppPalGnssGetApi(CHRE_PAL_GNSS_API_CURRENT_VERSION);
61   ASSERT_NE(gApi, nullptr);
62 
63   static const struct chrePalGnssCallbacks kCallbacks = {
64       .requestStateResync = chrePalRequestStateResync,
65       .locationStatusChangeCallback = chrePalLocationStatusChangeCallback,
66       .locationEventCallback = chrePalLocationEventCallback,
67       .measurementStatusChangeCallback = chrePalMeasurementStatusChangeCallback,
68       .measurementEventCallback = chrePalMeasurementEventCallback,
69   };
70   bool success = gApi->open(&chre::gChrePalSystemApi, &kCallbacks);
71   ASSERT_TRUE(success);
72 
73   for (size_t i = 0; i < 10; i++) {
74     gnssPalSendLocationEvent();
75     gnssPalSendMeasurementEvent();
76   }
77 
78   gApi->close();
79 }
80 
TEST_F(ChppGnssTest,GnssCapabilitiesTest)81 TEST_F(ChppGnssTest, GnssCapabilitiesTest) {
82   gApi = chppPalGnssGetApi(CHRE_PAL_GNSS_API_CURRENT_VERSION);
83   ASSERT_NE(gApi, nullptr);
84 
85   static const struct chrePalGnssCallbacks kCallbacks = {
86       .requestStateResync = chrePalRequestStateResync,
87       .locationStatusChangeCallback = chrePalLocationStatusChangeCallback,
88       .locationEventCallback = chrePalLocationEventCallback,
89       .measurementStatusChangeCallback = chrePalMeasurementStatusChangeCallback,
90       .measurementEventCallback = chrePalMeasurementEventCallback,
91   };
92   bool success = gApi->open(&chre::gChrePalSystemApi, &kCallbacks);
93   ASSERT_TRUE(success);
94 
95   // Set the linkActive flag to false so that CHPP link layer does not
96   // receive/send message, which causes the capabilities to be set to the
97   // default CHPP_GNSS_DEFAULT_CAPABILITIES
98   mClientLinkContext.isLinkActive = false;
99   uint32_t capabilities = gApi->getCapabilities();
100   ASSERT_EQ(capabilities, CHPP_GNSS_DEFAULT_CAPABILITIES);
101   mClientLinkContext.isLinkActive = true;
102 
103   gApi->close();
104 }
105 
106 }  //  anonymous namespace
107 }  //  namespace chpp
108