1 /* Copyright (c) 2017, 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_0 {
43 namespace implementation {
44
45 static void convertGnssData(GnssMeasurementsNotification& in,
46 IGnssMeasurementCallback::GnssData& out);
47 static void convertGnssMeasurement(GnssMeasurementsData& in,
48 IGnssMeasurementCallback::GnssMeasurement& out);
49 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out);
50
MeasurementAPIClient()51 MeasurementAPIClient::MeasurementAPIClient() :
52 mGnssMeasurementCbIface(nullptr),
53 mTracking(false)
54 {
55 LOC_LOGD("%s]: ()", __FUNCTION__);
56 }
57
~MeasurementAPIClient()58 MeasurementAPIClient::~MeasurementAPIClient()
59 {
60 LOC_LOGD("%s]: ()", __FUNCTION__);
61 }
62
63 // for GpsInterface
64 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback(const sp<IGnssMeasurementCallback> & callback)65 MeasurementAPIClient::measurementSetCallback(const sp<IGnssMeasurementCallback>& callback)
66 {
67 LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
68
69 mMutex.lock();
70 mGnssMeasurementCbIface = callback;
71 mMutex.unlock();
72
73 LocationCallbacks locationCallbacks;
74 memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
75 locationCallbacks.size = sizeof(LocationCallbacks);
76
77 locationCallbacks.trackingCb = nullptr;
78 locationCallbacks.batchingCb = nullptr;
79 locationCallbacks.geofenceBreachCb = nullptr;
80 locationCallbacks.geofenceStatusCb = nullptr;
81 locationCallbacks.gnssLocationInfoCb = nullptr;
82 locationCallbacks.gnssNiCb = nullptr;
83 locationCallbacks.gnssSvCb = nullptr;
84 locationCallbacks.gnssNmeaCb = nullptr;
85
86 locationCallbacks.gnssMeasurementsCb = nullptr;
87 if (mGnssMeasurementCbIface != nullptr) {
88 locationCallbacks.gnssMeasurementsCb =
89 [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
90 onGnssMeasurementsCb(gnssMeasurementsNotification);
91 };
92 }
93
94 locAPISetCallbacks(locationCallbacks);
95 LocationOptions options;
96 memset(&options, 0, sizeof(LocationOptions));
97 options.size = sizeof(LocationOptions);
98 options.minInterval = 1000;
99 options.mode = GNSS_SUPL_MODE_STANDALONE;
100 mTracking = true;
101 LOC_LOGD("%s]: start tracking session", __FUNCTION__);
102 locAPIStartTracking(options);
103
104 return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
105 }
106
107 // for GpsMeasurementInterface
measurementClose()108 void MeasurementAPIClient::measurementClose() {
109 LOC_LOGD("%s]: ()", __FUNCTION__);
110 mTracking = false;
111 locAPIStopTracking();
112 }
113
114 // callbacks
onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification)115 void MeasurementAPIClient::onGnssMeasurementsCb(
116 GnssMeasurementsNotification gnssMeasurementsNotification)
117 {
118 LOC_LOGD("%s]: (count: %zu active: %zu)",
119 __FUNCTION__, gnssMeasurementsNotification.count, mTracking);
120 if (mTracking) {
121 mMutex.lock();
122 auto gnssMeasurementCbIface(mGnssMeasurementCbIface);
123 mMutex.unlock();
124
125 if (gnssMeasurementCbIface != nullptr) {
126 IGnssMeasurementCallback::GnssData gnssData;
127 convertGnssData(gnssMeasurementsNotification, gnssData);
128 auto r = gnssMeasurementCbIface->GnssMeasurementCb(gnssData);
129 if (!r.isOk()) {
130 LOC_LOGE("%s] Error from GnssMeasurementCb description=%s",
131 __func__, r.description().c_str());
132 }
133 }
134 }
135 }
136
convertGnssMeasurement(GnssMeasurementsData & in,IGnssMeasurementCallback::GnssMeasurement & out)137 static void convertGnssMeasurement(GnssMeasurementsData& in,
138 IGnssMeasurementCallback::GnssMeasurement& out)
139 {
140 memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssMeasurement));
141 if (in.flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT)
142 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
143 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT)
144 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
145 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT)
146 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
147 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT)
148 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
149 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT)
150 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
151 if (in.flags & GNSS_MEASUREMENTS_DATA_AUTOMATIC_GAIN_CONTROL_BIT)
152 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_AUTOMATIC_GAIN_CONTROL;
153 out.svid = in.svId;
154 convertGnssConstellationType(in.svType, out.constellation);
155 out.timeOffsetNs = in.timeOffsetNs;
156 if (in.stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
157 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
158 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
159 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
160 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
161 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
162 if (in.stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
163 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
164 if (in.stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
165 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
166 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
167 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
168 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
169 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
170 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
171 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
172 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
173 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
174 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
175 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
176 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
177 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
178 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
179 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
180 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
181 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
182 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
183 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
184 out.receivedSvTimeInNs = in.receivedSvTimeNs;
185 out.receivedSvTimeUncertaintyInNs = in.receivedSvTimeUncertaintyNs;
186 out.cN0DbHz = in.carrierToNoiseDbHz;
187 out.pseudorangeRateMps = in.pseudorangeRateMps;
188 out.pseudorangeRateUncertaintyMps = in.pseudorangeRateUncertaintyMps;
189 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
190 out.accumulatedDeltaRangeState |=
191 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
192 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
193 out.accumulatedDeltaRangeState |=
194 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
195 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
196 out.accumulatedDeltaRangeState |=
197 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
198 out.accumulatedDeltaRangeM = in.adrMeters;
199 out.accumulatedDeltaRangeUncertaintyM = in.adrUncertaintyMeters;
200 out.carrierFrequencyHz = in.carrierFrequencyHz;
201 out.carrierCycles = in.carrierCycles;
202 out.carrierPhase = in.carrierPhase;
203 out.carrierPhaseUncertainty = in.carrierPhaseUncertainty;
204 uint8_t indicator =
205 static_cast<uint8_t>(IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN);
206 if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_PRESENT)
207 indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_PRESENT;
208 if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_NOT_PRESENT)
209 indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATIOR_NOT_PRESENT;
210 out.multipathIndicator =
211 static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(indicator);
212 out.snrDb = in.signalToNoiseRatioDb;
213 out.agcLevelDb = in.agcLevelDb;
214 }
215
convertGnssClock(GnssMeasurementsClock & in,IGnssMeasurementCallback::GnssClock & out)216 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out)
217 {
218 memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssClock));
219 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_LEAP_SECOND_BIT)
220 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_LEAP_SECOND;
221 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_TIME_UNCERTAINTY_BIT)
222 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_TIME_UNCERTAINTY;
223 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_FULL_BIAS_BIT)
224 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_FULL_BIAS;
225 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_BIT)
226 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS;
227 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_UNCERTAINTY_BIT)
228 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS_UNCERTAINTY;
229 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_BIT)
230 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT;
231 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_UNCERTAINTY_BIT)
232 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT_UNCERTAINTY;
233 out.leapSecond = in.leapSecond;
234 out.timeNs = in.timeNs;
235 out.timeUncertaintyNs = in.timeUncertaintyNs;
236 out.fullBiasNs = in.fullBiasNs;
237 out.biasNs = in.biasNs;
238 out.biasUncertaintyNs = in.biasUncertaintyNs;
239 out.driftNsps = in.driftNsps;
240 out.driftUncertaintyNsps = in.driftUncertaintyNsps;
241 out.hwClockDiscontinuityCount = in.hwClockDiscontinuityCount;
242 }
243
convertGnssData(GnssMeasurementsNotification & in,IGnssMeasurementCallback::GnssData & out)244 static void convertGnssData(GnssMeasurementsNotification& in,
245 IGnssMeasurementCallback::GnssData& out)
246 {
247 out.measurementCount = in.count;
248 if (out.measurementCount > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
249 LOC_LOGW("%s]: Too many measurement %zd. Clamps to %d.",
250 __FUNCTION__, out.measurementCount, GnssMax::SVS_COUNT);
251 out.measurementCount = static_cast<uint32_t>(GnssMax::SVS_COUNT);
252 }
253 for (size_t i = 0; i < out.measurementCount; i++) {
254 convertGnssMeasurement(in.measurements[i], out.measurements[i]);
255 }
256 convertGnssClock(in.clock, out.clock);
257 }
258
259 } // namespace implementation
260 } // namespace V1_0
261 } // namespace gnss
262 } // namespace hardware
263 } // namespace android
264