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