xref: /aosp_15_r20/system/chre/platform/shared/platform_gnss.cc (revision 84e339476a462649f82315436d70fd732297a399)
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 #ifdef CHRE_GNSS_SUPPORT_ENABLED
18 
19 #include "chre/platform/platform_gnss.h"
20 
21 #include <cinttypes>
22 
23 #include "chre/core/event_loop_manager.h"
24 #include "chre/platform/log.h"
25 #include "chre/platform/shared/pal_system_api.h"
26 
27 namespace chre {
28 
29 const chrePalGnssCallbacks PlatformGnssBase::sGnssCallbacks = {
30     PlatformGnssBase::requestStateResyncCallback,
31     PlatformGnssBase::locationStatusChangeCallback,
32     PlatformGnssBase::locationEventCallback,
33     PlatformGnssBase::measurementStatusChangeCallback,
34     PlatformGnssBase::measurementEventCallback,
35 };
36 
~PlatformGnss()37 PlatformGnss::~PlatformGnss() {
38   if (mGnssApi != nullptr) {
39     LOGD("Platform GNSS closing");
40     prePalApiCall(PalType::GNSS);
41     mGnssApi->close();
42     LOGD("Platform GNSS closed");
43   }
44 }
45 
init()46 void PlatformGnss::init() {
47   prePalApiCall(PalType::GNSS);
48   mGnssApi = chrePalGnssGetApi(CHRE_PAL_GNSS_API_CURRENT_VERSION);
49   if (mGnssApi != nullptr) {
50     if (!mGnssApi->open(&gChrePalSystemApi, &sGnssCallbacks)) {
51       LOGE("GNSS PAL open returned false");
52 
53 #ifdef CHRE_TELEMETRY_SUPPORT_ENABLED
54       EventLoopManagerSingleton::get()->getTelemetryManager().onPalOpenFailure(
55           TelemetryManager::PalType::GNSS);
56 #endif  // CHRE_TELEMETRY_SUPPORT_ENABLED
57 
58       mGnssApi = nullptr;
59     } else {
60       LOGD("Opened GNSS PAL version 0x%08" PRIx32, mGnssApi->moduleVersion);
61     }
62   } else {
63     LOGW("Requested GNSS PAL (version 0x%08" PRIx32 ") not found",
64          CHRE_PAL_GNSS_API_CURRENT_VERSION);
65   }
66 }
67 
getCapabilities()68 uint32_t PlatformGnss::getCapabilities() {
69   if (mGnssApi != nullptr) {
70     prePalApiCall(PalType::GNSS);
71     return mGnssApi->getCapabilities();
72   } else {
73     return CHRE_GNSS_CAPABILITIES_NONE;
74   }
75 }
76 
controlLocationSession(bool enable,Milliseconds minInterval,Milliseconds minTimeToNextFix)77 bool PlatformGnss::controlLocationSession(bool enable, Milliseconds minInterval,
78                                           Milliseconds minTimeToNextFix) {
79   if (mGnssApi != nullptr) {
80     prePalApiCall(PalType::GNSS);
81     return mGnssApi->controlLocationSession(
82         enable, static_cast<uint32_t>(minInterval.getMilliseconds()),
83         static_cast<uint32_t>(minTimeToNextFix.getMilliseconds()));
84   } else {
85     return false;
86   }
87 }
88 
releaseLocationEvent(chreGnssLocationEvent * event)89 void PlatformGnss::releaseLocationEvent(chreGnssLocationEvent *event) {
90   if (mGnssApi != nullptr) {
91     prePalApiCall(PalType::GNSS);
92     mGnssApi->releaseLocationEvent(event);
93   }
94 }
95 
requestStateResyncCallback()96 void PlatformGnssBase::requestStateResyncCallback() {
97   EventLoopManagerSingleton::get()
98       ->getGnssManager()
99       .handleRequestStateResyncCallback();
100 }
101 
locationStatusChangeCallback(bool enabled,uint8_t errorCode)102 void PlatformGnssBase::locationStatusChangeCallback(bool enabled,
103                                                     uint8_t errorCode) {
104   EventLoopManagerSingleton::get()
105       ->getGnssManager()
106       .getLocationSession()
107       .handleStatusChange(enabled, errorCode);
108 }
109 
locationEventCallback(struct chreGnssLocationEvent * event)110 void PlatformGnssBase::locationEventCallback(
111     struct chreGnssLocationEvent *event) {
112   EventLoopManagerSingleton::get()
113       ->getGnssManager()
114       .getLocationSession()
115       .handleReportEvent(event);
116 }
117 
controlMeasurementSession(bool enable,Milliseconds minInterval)118 bool PlatformGnss::controlMeasurementSession(bool enable,
119                                              Milliseconds minInterval) {
120   if (mGnssApi != nullptr) {
121     prePalApiCall(PalType::GNSS);
122     return mGnssApi->controlMeasurementSession(
123         enable, static_cast<uint32_t>(minInterval.getMilliseconds()));
124   } else {
125     return false;
126   }
127 }
128 
releaseMeasurementDataEvent(chreGnssDataEvent * event)129 void PlatformGnss::releaseMeasurementDataEvent(chreGnssDataEvent *event) {
130   if (mGnssApi != nullptr) {
131     prePalApiCall(PalType::GNSS);
132     mGnssApi->releaseMeasurementDataEvent(event);
133   }
134 }
135 
configurePassiveLocationListener(bool enable)136 bool PlatformGnss::configurePassiveLocationListener(bool enable) {
137   bool success = false;
138   if (mGnssApi != nullptr &&
139       mGnssApi->moduleVersion >= CHRE_PAL_GNSS_API_V1_2) {
140     prePalApiCall(PalType::GNSS);
141     success = mGnssApi->configurePassiveLocationListener(enable);
142   }
143   return success;
144 }
145 
measurementStatusChangeCallback(bool enabled,uint8_t errorCode)146 void PlatformGnssBase::measurementStatusChangeCallback(bool enabled,
147                                                        uint8_t errorCode) {
148   EventLoopManagerSingleton::get()
149       ->getGnssManager()
150       .getMeasurementSession()
151       .handleStatusChange(enabled, errorCode);
152 }
153 
measurementEventCallback(struct chreGnssDataEvent * event)154 void PlatformGnssBase::measurementEventCallback(
155     struct chreGnssDataEvent *event) {
156   EventLoopManagerSingleton::get()
157       ->getGnssManager()
158       .getMeasurementSession()
159       .handleReportEvent(event);
160 }
161 
162 }  // namespace chre
163 
164 #endif  // CHRE_GNSS_SUPPORT_ENABLED
165