1 /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation, nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #define LOG_NDEBUG 0
31 #define LOG_TAG "LocSvc_MeasurementAPIClient"
32
33 #include <log_util.h>
34 #include <loc_cfg.h>
35
36 #include "LocationUtil.h"
37 #include "MeasurementAPIClient.h"
38
39 namespace android {
40 namespace hardware {
41 namespace gnss {
42 namespace V1_1 {
43 namespace implementation {
44
45 using ::android::hardware::gnss::V1_0::IGnssMeasurement;
46 using ::android::hardware::gnss::V1_1::IGnssMeasurementCallback;
47
48 static void convertGnssData(GnssMeasurementsNotification& in,
49 V1_0::IGnssMeasurementCallback::GnssData& out);
50 static void convertGnssData_1_1(GnssMeasurementsNotification& in,
51 IGnssMeasurementCallback::GnssData& out);
52 static void convertGnssMeasurement(GnssMeasurementsData& in,
53 V1_0::IGnssMeasurementCallback::GnssMeasurement& out);
54 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out);
55
MeasurementAPIClient()56 MeasurementAPIClient::MeasurementAPIClient() :
57 mGnssMeasurementCbIface(nullptr),
58 mGnssMeasurementCbIface_1_1(nullptr),
59 mTracking(false)
60 {
61 LOC_LOGD("%s]: ()", __FUNCTION__);
62 }
63
~MeasurementAPIClient()64 MeasurementAPIClient::~MeasurementAPIClient()
65 {
66 LOC_LOGD("%s]: ()", __FUNCTION__);
67 }
68
69 // for GpsInterface
70 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback> & callback)71 MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
72 {
73 LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
74
75 mMutex.lock();
76 mGnssMeasurementCbIface = callback;
77 mMutex.unlock();
78
79 return startTracking();
80 }
81
82 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback_1_1(const sp<IGnssMeasurementCallback> & callback,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)83 MeasurementAPIClient::measurementSetCallback_1_1(
84 const sp<IGnssMeasurementCallback>& callback,
85 GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
86 {
87 LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
88 __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
89
90 mMutex.lock();
91 mGnssMeasurementCbIface_1_1 = callback;
92 mMutex.unlock();
93
94 return startTracking(powerMode, timeBetweenMeasurement);
95 }
96
97 Return<IGnssMeasurement::GnssMeasurementStatus>
startTracking(GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)98 MeasurementAPIClient::startTracking(
99 GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
100 {
101 LocationCallbacks locationCallbacks;
102 memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
103 locationCallbacks.size = sizeof(LocationCallbacks);
104
105 locationCallbacks.trackingCb = nullptr;
106 locationCallbacks.batchingCb = nullptr;
107 locationCallbacks.geofenceBreachCb = nullptr;
108 locationCallbacks.geofenceStatusCb = nullptr;
109 locationCallbacks.gnssLocationInfoCb = nullptr;
110 locationCallbacks.gnssNiCb = nullptr;
111 locationCallbacks.gnssSvCb = nullptr;
112 locationCallbacks.gnssNmeaCb = nullptr;
113
114 locationCallbacks.gnssMeasurementsCb = nullptr;
115 if (mGnssMeasurementCbIface_1_1 != nullptr || mGnssMeasurementCbIface != nullptr) {
116 locationCallbacks.gnssMeasurementsCb =
117 [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
118 onGnssMeasurementsCb(gnssMeasurementsNotification);
119 };
120 }
121
122 locAPISetCallbacks(locationCallbacks);
123
124 TrackingOptions options = {};
125 memset(&options, 0, sizeof(TrackingOptions));
126 options.size = sizeof(TrackingOptions);
127 options.minInterval = 1000;
128 options.mode = GNSS_SUPL_MODE_STANDALONE;
129 if (GNSS_POWER_MODE_INVALID != powerMode) {
130 options.powerMode = powerMode;
131 options.tbm = timeBetweenMeasurement;
132 }
133
134 mTracking = true;
135 LOC_LOGD("%s]: start tracking session", __FUNCTION__);
136 locAPIStartTracking(options);
137 return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
138 }
139
140 // for GpsMeasurementInterface
measurementClose()141 void MeasurementAPIClient::measurementClose() {
142 LOC_LOGD("%s]: ()", __FUNCTION__);
143 mTracking = false;
144 locAPIStopTracking();
145 }
146
147 // callbacks
onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification)148 void MeasurementAPIClient::onGnssMeasurementsCb(
149 GnssMeasurementsNotification gnssMeasurementsNotification)
150 {
151 LOC_LOGD("%s]: (count: %zu active: %d)",
152 __FUNCTION__, gnssMeasurementsNotification.count, mTracking);
153 if (mTracking) {
154 mMutex.lock();
155 sp<V1_0::IGnssMeasurementCallback> gnssMeasurementCbIface = nullptr;
156 sp<IGnssMeasurementCallback> gnssMeasurementCbIface_1_1 = nullptr;
157 if (mGnssMeasurementCbIface_1_1 != nullptr) {
158 gnssMeasurementCbIface_1_1 = mGnssMeasurementCbIface_1_1;
159 } else if (mGnssMeasurementCbIface != nullptr) {
160 gnssMeasurementCbIface = mGnssMeasurementCbIface;
161 }
162 mMutex.unlock();
163
164 if (gnssMeasurementCbIface_1_1 != nullptr) {
165 IGnssMeasurementCallback::GnssData gnssData;
166 convertGnssData_1_1(gnssMeasurementsNotification, gnssData);
167 auto r = gnssMeasurementCbIface_1_1->gnssMeasurementCb(gnssData);
168 if (!r.isOk()) {
169 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
170 __func__, r.description().c_str());
171 }
172 } else if (gnssMeasurementCbIface != nullptr) {
173 V1_0::IGnssMeasurementCallback::GnssData gnssData;
174 convertGnssData(gnssMeasurementsNotification, gnssData);
175 auto r = gnssMeasurementCbIface->GnssMeasurementCb(gnssData);
176 if (!r.isOk()) {
177 LOC_LOGE("%s] Error from GnssMeasurementCb description=%s",
178 __func__, r.description().c_str());
179 }
180 }
181 }
182 }
183
convertGnssMeasurement(GnssMeasurementsData & in,V1_0::IGnssMeasurementCallback::GnssMeasurement & out)184 static void convertGnssMeasurement(GnssMeasurementsData& in,
185 V1_0::IGnssMeasurementCallback::GnssMeasurement& out)
186 {
187 memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssMeasurement));
188 if (in.flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT)
189 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
190 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT)
191 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
192 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT)
193 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
194 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT)
195 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
196 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT)
197 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
198 if (in.flags & GNSS_MEASUREMENTS_DATA_AUTOMATIC_GAIN_CONTROL_BIT)
199 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_AUTOMATIC_GAIN_CONTROL;
200 out.svid = in.svId;
201 convertGnssConstellationType(in.svType, out.constellation);
202 out.timeOffsetNs = in.timeOffsetNs;
203 if (in.stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
204 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
205 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
206 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
207 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
208 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
209 if (in.stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
210 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
211 if (in.stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
212 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
213 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
214 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
215 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
216 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
217 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
218 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
219 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
220 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
221 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
222 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
223 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
224 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
225 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
226 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
227 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
228 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
229 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
230 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
231 out.receivedSvTimeInNs = in.receivedSvTimeNs;
232 out.receivedSvTimeUncertaintyInNs = in.receivedSvTimeUncertaintyNs;
233 out.cN0DbHz = in.carrierToNoiseDbHz;
234 out.pseudorangeRateMps = in.pseudorangeRateMps;
235 out.pseudorangeRateUncertaintyMps = in.pseudorangeRateUncertaintyMps;
236 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
237 out.accumulatedDeltaRangeState |=
238 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
239 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
240 out.accumulatedDeltaRangeState |=
241 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
242 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
243 out.accumulatedDeltaRangeState |=
244 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
245 out.accumulatedDeltaRangeM = in.adrMeters;
246 out.accumulatedDeltaRangeUncertaintyM = in.adrUncertaintyMeters;
247 out.carrierFrequencyHz = in.carrierFrequencyHz;
248 out.carrierCycles = in.carrierCycles;
249 out.carrierPhase = in.carrierPhase;
250 out.carrierPhaseUncertainty = in.carrierPhaseUncertainty;
251 uint8_t indicator =
252 static_cast<uint8_t>(IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN);
253 if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_PRESENT)
254 indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_PRESENT;
255 if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_NOT_PRESENT)
256 indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATIOR_NOT_PRESENT;
257 out.multipathIndicator =
258 static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(indicator);
259 out.snrDb = in.signalToNoiseRatioDb;
260 out.agcLevelDb = in.agcLevelDb;
261 }
262
convertGnssClock(GnssMeasurementsClock & in,IGnssMeasurementCallback::GnssClock & out)263 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out)
264 {
265 memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssClock));
266 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_LEAP_SECOND_BIT)
267 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_LEAP_SECOND;
268 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_TIME_UNCERTAINTY_BIT)
269 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_TIME_UNCERTAINTY;
270 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_FULL_BIAS_BIT)
271 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_FULL_BIAS;
272 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_BIT)
273 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS;
274 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_UNCERTAINTY_BIT)
275 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS_UNCERTAINTY;
276 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_BIT)
277 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT;
278 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_UNCERTAINTY_BIT)
279 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT_UNCERTAINTY;
280 out.leapSecond = in.leapSecond;
281 out.timeNs = in.timeNs;
282 out.timeUncertaintyNs = in.timeUncertaintyNs;
283 out.fullBiasNs = in.fullBiasNs;
284 out.biasNs = in.biasNs;
285 out.biasUncertaintyNs = in.biasUncertaintyNs;
286 out.driftNsps = in.driftNsps;
287 out.driftUncertaintyNsps = in.driftUncertaintyNsps;
288 out.hwClockDiscontinuityCount = in.hwClockDiscontinuityCount;
289 }
290
convertGnssData(GnssMeasurementsNotification & in,V1_0::IGnssMeasurementCallback::GnssData & out)291 static void convertGnssData(GnssMeasurementsNotification& in,
292 V1_0::IGnssMeasurementCallback::GnssData& out)
293 {
294 out.measurementCount = in.count;
295 if (out.measurementCount > static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT)) {
296 LOC_LOGW("%s]: Too many measurement %u. Clamps to %d.",
297 __FUNCTION__, out.measurementCount, V1_0::GnssMax::SVS_COUNT);
298 out.measurementCount = static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT);
299 }
300 for (size_t i = 0; i < out.measurementCount; i++) {
301 convertGnssMeasurement(in.measurements[i], out.measurements[i]);
302 }
303 convertGnssClock(in.clock, out.clock);
304 }
305
convertGnssData_1_1(GnssMeasurementsNotification & in,IGnssMeasurementCallback::GnssData & out)306 static void convertGnssData_1_1(GnssMeasurementsNotification& in,
307 IGnssMeasurementCallback::GnssData& out)
308 {
309 out.measurements.resize(in.count);
310 for (size_t i = 0; i < in.count; i++) {
311 convertGnssMeasurement(in.measurements[i], out.measurements[i].v1_0);
312 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
313 out.measurements[i].accumulatedDeltaRangeState |=
314 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
315 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
316 out.measurements[i].accumulatedDeltaRangeState |=
317 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
318 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
319 out.measurements[i].accumulatedDeltaRangeState |=
320 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
321 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_HALF_CYCLE_RESOLVED_BIT)
322 out.measurements[i].accumulatedDeltaRangeState |=
323 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_HALF_CYCLE_RESOLVED;
324 }
325 convertGnssClock(in.clock, out.clock);
326 }
327
328 } // namespace implementation
329 } // namespace V1_1
330 } // namespace gnss
331 } // namespace hardware
332 } // namespace android
333