1 /*
2 * Copyright (C) 2020 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/linux/pal_gnss.h"
18 #include "chre/pal/gnss.h"
19
20 #include "chre/util/memory.h"
21 #include "chre/util/unique_ptr.h"
22
23 #include <chrono>
24 #include <cinttypes>
25 #include <future>
26 #include <thread>
27
28 /**
29 * A simulated implementation of the GNSS PAL for the linux platform.
30 */
31 namespace {
32 const struct chrePalSystemApi *gSystemApi = nullptr;
33 const struct chrePalGnssCallbacks *gCallbacks = nullptr;
34
35 //! Thread to deliver asynchronous location data after a CHRE request.
36 std::thread gLocationEventsThread;
37 std::promise<void> gStopLocationEventsThread;
38 std::promise<void> gStartLocationEvents;
39 bool gDelaySendingLocationEvents = false;
40 bool gIsLocationEnabled = false;
41
42 //! Thead to use when delivering a location status update.
43 std::thread gLocationStatusThread;
44
45 //! Thread to deliver asynchronous measurement data after a CHRE request.
46 std::thread gMeasurementEventsThread;
47 std::promise<void> gStopMeasurementEventsThread;
48 bool gIsMeasurementEnabled = false;
49
50 //! Thead to use when delivering a measurement status update.
51 std::thread gMeasurementStatusThread;
52
sendLocationEvents(uint32_t minIntervalMs)53 void sendLocationEvents(uint32_t minIntervalMs) {
54 if (gDelaySendingLocationEvents) {
55 gStartLocationEvents.get_future().wait();
56 }
57 gCallbacks->locationStatusChangeCallback(true, CHRE_ERROR_NONE);
58
59 std::future<void> signal = gStopLocationEventsThread.get_future();
60 while (signal.wait_for(std::chrono::milliseconds(minIntervalMs)) ==
61 std::future_status::timeout) {
62 auto event = chre::MakeUniqueZeroFill<struct chreGnssLocationEvent>();
63 event->timestamp = gSystemApi->getCurrentTime();
64 gCallbacks->locationEventCallback(event.release());
65 }
66 }
67
sendMeasurementEvents(uint32_t minIntervalMs)68 void sendMeasurementEvents(uint32_t minIntervalMs) {
69 gCallbacks->measurementStatusChangeCallback(true, CHRE_ERROR_NONE);
70
71 std::future<void> signal = gStopMeasurementEventsThread.get_future();
72 while (signal.wait_for(std::chrono::milliseconds(minIntervalMs)) ==
73 std::future_status::timeout) {
74 auto event = chre::MakeUniqueZeroFill<struct chreGnssDataEvent>();
75 auto measurement = chre::MakeUniqueZeroFill<struct chreGnssMeasurement>();
76 measurement->c_n0_dbhz = 63.0f;
77
78 event->measurements = measurement.release();
79 event->measurement_count = 1;
80 event->clock.time_ns = static_cast<int64_t>(gSystemApi->getCurrentTime());
81 gCallbacks->measurementEventCallback(event.release());
82 }
83 }
84
stopLocation()85 void stopLocation() {
86 gCallbacks->locationStatusChangeCallback(false, CHRE_ERROR_NONE);
87 }
88
stopMeasurement()89 void stopMeasurement() {
90 gCallbacks->measurementStatusChangeCallback(false, CHRE_ERROR_NONE);
91 }
92
stopLocationThreads()93 void stopLocationThreads() {
94 if (gLocationEventsThread.joinable()) {
95 gStopLocationEventsThread.set_value();
96 gLocationEventsThread.join();
97 }
98 if (gLocationStatusThread.joinable()) {
99 gLocationStatusThread.join();
100 }
101 }
102
stopMeasurementThreads()103 void stopMeasurementThreads() {
104 if (gMeasurementEventsThread.joinable()) {
105 gStopMeasurementEventsThread.set_value();
106 gMeasurementEventsThread.join();
107 }
108 if (gMeasurementStatusThread.joinable()) {
109 gMeasurementStatusThread.join();
110 }
111 }
112
chrePalGnssGetCapabilities()113 uint32_t chrePalGnssGetCapabilities() {
114 return CHRE_GNSS_CAPABILITIES_LOCATION | CHRE_GNSS_CAPABILITIES_MEASUREMENTS |
115 CHRE_GNSS_CAPABILITIES_GNSS_ENGINE_BASED_PASSIVE_LISTENER;
116 }
117
chrePalControlLocationSession(bool enable,uint32_t minIntervalMs,uint32_t)118 bool chrePalControlLocationSession(bool enable, uint32_t minIntervalMs,
119 uint32_t /* minTimeToNextFixMs */) {
120 stopLocationThreads();
121
122 if (enable) {
123 gStartLocationEvents = std::promise<void>();
124 gStopLocationEventsThread = std::promise<void>();
125 gLocationEventsThread = std::thread(sendLocationEvents, minIntervalMs);
126 } else {
127 gLocationStatusThread = std::thread(stopLocation);
128 }
129
130 gIsLocationEnabled = enable;
131
132 return true;
133 }
134
chrePalGnssReleaseLocationEvent(struct chreGnssLocationEvent * event)135 void chrePalGnssReleaseLocationEvent(struct chreGnssLocationEvent *event) {
136 chre::memoryFree(event);
137 }
138
chrePalControlMeasurementSession(bool enable,uint32_t minIntervalMs)139 bool chrePalControlMeasurementSession(bool enable, uint32_t minIntervalMs) {
140 stopMeasurementThreads();
141
142 if (enable) {
143 gStopMeasurementEventsThread = std::promise<void>();
144 gMeasurementEventsThread =
145 std::thread(sendMeasurementEvents, minIntervalMs);
146 } else {
147 gMeasurementStatusThread = std::thread(stopMeasurement);
148 }
149
150 gIsMeasurementEnabled = enable;
151
152 return true;
153 }
154
chrePalGnssReleaseMeasurementDataEvent(struct chreGnssDataEvent * event)155 void chrePalGnssReleaseMeasurementDataEvent(struct chreGnssDataEvent *event) {
156 chre::memoryFree(
157 const_cast<struct chreGnssMeasurement *>(event->measurements));
158 chre::memoryFree(event);
159 }
160
chrePalGnssApiClose()161 void chrePalGnssApiClose() {
162 stopLocationThreads();
163 stopMeasurementThreads();
164 }
165
chrePalGnssApiOpen(const struct chrePalSystemApi * systemApi,const struct chrePalGnssCallbacks * callbacks)166 bool chrePalGnssApiOpen(const struct chrePalSystemApi *systemApi,
167 const struct chrePalGnssCallbacks *callbacks) {
168 chrePalGnssApiClose();
169
170 bool success = false;
171 if (systemApi != nullptr && callbacks != nullptr) {
172 gSystemApi = systemApi;
173 gCallbacks = callbacks;
174 success = true;
175 }
176
177 return success;
178 }
179
180 bool gIsPassiveListenerEnabled = false;
181
chrePalGnssconfigurePassiveLocationListener(bool enable)182 bool chrePalGnssconfigurePassiveLocationListener(bool enable) {
183 gIsPassiveListenerEnabled = enable;
184 return true;
185 }
186
187 } // anonymous namespace
188
chrePalGnssIsLocationEnabled()189 bool chrePalGnssIsLocationEnabled() {
190 return gIsLocationEnabled;
191 }
192
chrePalGnssIsMeasurementEnabled()193 bool chrePalGnssIsMeasurementEnabled() {
194 return gIsMeasurementEnabled;
195 }
196
chrePalGnssIsPassiveLocationListenerEnabled()197 bool chrePalGnssIsPassiveLocationListenerEnabled() {
198 return gIsPassiveListenerEnabled;
199 }
200
chrePalGnssDelaySendingLocationEvents(bool enabled)201 void chrePalGnssDelaySendingLocationEvents(bool enabled) {
202 gDelaySendingLocationEvents = enabled;
203 }
204
chrePalGnssStartSendingLocationEvents()205 void chrePalGnssStartSendingLocationEvents() {
206 CHRE_ASSERT(gDelaySendingLocationEvents);
207 gStartLocationEvents.set_value();
208 }
209
chrePalGnssGetApi(uint32_t requestedApiVersion)210 const struct chrePalGnssApi *chrePalGnssGetApi(uint32_t requestedApiVersion) {
211 static const struct chrePalGnssApi kApi = {
212 .moduleVersion = CHRE_PAL_GNSS_API_CURRENT_VERSION,
213 .open = chrePalGnssApiOpen,
214 .close = chrePalGnssApiClose,
215 .getCapabilities = chrePalGnssGetCapabilities,
216 .controlLocationSession = chrePalControlLocationSession,
217 .releaseLocationEvent = chrePalGnssReleaseLocationEvent,
218 .controlMeasurementSession = chrePalControlMeasurementSession,
219 .releaseMeasurementDataEvent = chrePalGnssReleaseMeasurementDataEvent,
220 .configurePassiveLocationListener =
221 chrePalGnssconfigurePassiveLocationListener,
222 };
223
224 if (!CHRE_PAL_VERSIONS_ARE_COMPATIBLE(kApi.moduleVersion,
225 requestedApiVersion)) {
226 return nullptr;
227 } else {
228 return &kApi;
229 }
230 }
231