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 Return<IGnssMeasurement::GnssMeasurementStatus>
startTracking(GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)70 MeasurementAPIClient::startTracking(
71 GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
72 {
73 LocationCallbacks locationCallbacks;
74 memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
75 locationCallbacks.size = sizeof(LocationCallbacks);
76
77 if (mGnssMeasurementCbIface_1_1 != nullptr || mGnssMeasurementCbIface != nullptr) {
78 locationCallbacks.gnssMeasurementsCb =
79 [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
80 onGnssMeasurementsCb(gnssMeasurementsNotification);
81 };
82 }
83
84 locAPISetCallbacks(locationCallbacks);
85
86 TrackingOptions options = {};
87 memset(&options, 0, sizeof(TrackingOptions));
88 options.size = sizeof(TrackingOptions);
89 options.minInterval = 1000;
90 options.mode = GNSS_SUPL_MODE_STANDALONE;
91 if (GNSS_POWER_MODE_INVALID != powerMode) {
92 options.powerMode = powerMode;
93 options.tbm = timeBetweenMeasurement;
94 }
95
96 mTracking = true;
97 LOC_LOGd("(powermode: %d) (tbm %d)", (int)powerMode, timeBetweenMeasurement);
98 locAPIStartTracking(options);
99 return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
100 }
101
102 // for GpsMeasurementInterface
measurementClose()103 void MeasurementAPIClient::measurementClose() {
104 LOC_LOGD("%s]: ()", __FUNCTION__);
105 mTracking = false;
106 locAPIStopTracking();
107 }
108
109 // callbacks
onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification)110 void MeasurementAPIClient::onGnssMeasurementsCb(
111 GnssMeasurementsNotification gnssMeasurementsNotification)
112 {
113 LOC_LOGD("%s]: (count: %u active: %d)",
114 __FUNCTION__, gnssMeasurementsNotification.count, mTracking);
115 if (mTracking) {
116 mMutex.lock();
117 sp<V1_0::IGnssMeasurementCallback> gnssMeasurementCbIface = nullptr;
118 sp<IGnssMeasurementCallback> gnssMeasurementCbIface_1_1 = nullptr;
119 if (mGnssMeasurementCbIface_1_1 != nullptr) {
120 gnssMeasurementCbIface_1_1 = mGnssMeasurementCbIface_1_1;
121 } else if (mGnssMeasurementCbIface != nullptr) {
122 gnssMeasurementCbIface = mGnssMeasurementCbIface;
123 }
124 mMutex.unlock();
125
126 if (gnssMeasurementCbIface_1_1 != nullptr) {
127 IGnssMeasurementCallback::GnssData gnssData;
128 convertGnssData_1_1(gnssMeasurementsNotification, gnssData);
129 auto r = gnssMeasurementCbIface_1_1->gnssMeasurementCb(gnssData);
130 if (!r.isOk()) {
131 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
132 __func__, r.description().c_str());
133 }
134 } else if (gnssMeasurementCbIface != nullptr) {
135 V1_0::IGnssMeasurementCallback::GnssData gnssData;
136 convertGnssData(gnssMeasurementsNotification, gnssData);
137 auto r = gnssMeasurementCbIface->GnssMeasurementCb(gnssData);
138 if (!r.isOk()) {
139 LOC_LOGE("%s] Error from GnssMeasurementCb description=%s",
140 __func__, r.description().c_str());
141 }
142 }
143 }
144 }
145
convertGnssMeasurement(GnssMeasurementsData & in,V1_0::IGnssMeasurementCallback::GnssMeasurement & out)146 static void convertGnssMeasurement(GnssMeasurementsData& in,
147 V1_0::IGnssMeasurementCallback::GnssMeasurement& out)
148 {
149 memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssMeasurement));
150 if (in.flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT)
151 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
152 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT)
153 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
154 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT)
155 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
156 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT)
157 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
158 if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT)
159 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
160 if (in.flags & GNSS_MEASUREMENTS_DATA_AUTOMATIC_GAIN_CONTROL_BIT)
161 out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_AUTOMATIC_GAIN_CONTROL;
162 convertGnssSvid(in, out.svid);
163 convertGnssConstellationType(in.svType, out.constellation);
164 out.timeOffsetNs = in.timeOffsetNs;
165 if (in.stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
166 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
167 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
168 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
169 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
170 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
171 if (in.stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
172 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
173 if (in.stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
174 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
175 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
176 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
177 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
178 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
179 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
180 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
181 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
182 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
183 if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
184 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
185 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
186 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
187 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
188 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
189 if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
190 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
191 if (in.stateMask & GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
192 out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
193 out.receivedSvTimeInNs = in.receivedSvTimeNs;
194 out.receivedSvTimeUncertaintyInNs = in.receivedSvTimeUncertaintyNs;
195 out.cN0DbHz = in.carrierToNoiseDbHz;
196 out.pseudorangeRateMps = in.pseudorangeRateMps;
197 out.pseudorangeRateUncertaintyMps = in.pseudorangeRateUncertaintyMps;
198 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
199 out.accumulatedDeltaRangeState |=
200 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
201 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
202 out.accumulatedDeltaRangeState |=
203 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
204 if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
205 out.accumulatedDeltaRangeState |=
206 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
207 out.accumulatedDeltaRangeM = in.adrMeters;
208 out.accumulatedDeltaRangeUncertaintyM = in.adrUncertaintyMeters;
209 out.carrierFrequencyHz = in.carrierFrequencyHz;
210 out.carrierCycles = in.carrierCycles;
211 out.carrierPhase = in.carrierPhase;
212 out.carrierPhaseUncertainty = in.carrierPhaseUncertainty;
213 uint8_t indicator =
214 static_cast<uint8_t>(IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN);
215 if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_PRESENT)
216 indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_PRESENT;
217 if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_NOT_PRESENT)
218 indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATIOR_NOT_PRESENT;
219 out.multipathIndicator =
220 static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(indicator);
221 out.snrDb = in.signalToNoiseRatioDb;
222 out.agcLevelDb = in.agcLevelDb;
223 }
224
convertGnssClock(GnssMeasurementsClock & in,IGnssMeasurementCallback::GnssClock & out)225 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out)
226 {
227 memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssClock));
228 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_LEAP_SECOND_BIT)
229 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_LEAP_SECOND;
230 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_TIME_UNCERTAINTY_BIT)
231 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_TIME_UNCERTAINTY;
232 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_FULL_BIAS_BIT)
233 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_FULL_BIAS;
234 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_BIT)
235 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS;
236 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_UNCERTAINTY_BIT)
237 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS_UNCERTAINTY;
238 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_BIT)
239 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT;
240 if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_UNCERTAINTY_BIT)
241 out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT_UNCERTAINTY;
242 out.leapSecond = in.leapSecond;
243 out.timeNs = in.timeNs;
244 out.timeUncertaintyNs = in.timeUncertaintyNs;
245 out.fullBiasNs = in.fullBiasNs;
246 out.biasNs = in.biasNs;
247 out.biasUncertaintyNs = in.biasUncertaintyNs;
248 out.driftNsps = in.driftNsps;
249 out.driftUncertaintyNsps = in.driftUncertaintyNsps;
250 out.hwClockDiscontinuityCount = in.hwClockDiscontinuityCount;
251 }
252
convertGnssData(GnssMeasurementsNotification & in,V1_0::IGnssMeasurementCallback::GnssData & out)253 static void convertGnssData(GnssMeasurementsNotification& in,
254 V1_0::IGnssMeasurementCallback::GnssData& out)
255 {
256 out.measurementCount = in.count;
257 if (out.measurementCount > static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT)) {
258 LOC_LOGW("%s]: Too many measurement %u. Clamps to %d.",
259 __FUNCTION__, out.measurementCount, V1_0::GnssMax::SVS_COUNT);
260 out.measurementCount = static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT);
261 }
262 for (size_t i = 0; i < out.measurementCount; i++) {
263 convertGnssMeasurement(in.measurements[i], out.measurements[i]);
264 }
265 convertGnssClock(in.clock, out.clock);
266 }
267
convertGnssData_1_1(GnssMeasurementsNotification & in,IGnssMeasurementCallback::GnssData & out)268 static void convertGnssData_1_1(GnssMeasurementsNotification& in,
269 IGnssMeasurementCallback::GnssData& out)
270 {
271 out.measurements.resize(in.count);
272 for (size_t i = 0; i < in.count; i++) {
273 convertGnssMeasurement(in.measurements[i], out.measurements[i].v1_0);
274 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
275 out.measurements[i].accumulatedDeltaRangeState |=
276 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
277 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
278 out.measurements[i].accumulatedDeltaRangeState |=
279 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
280 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
281 out.measurements[i].accumulatedDeltaRangeState |=
282 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
283 if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_HALF_CYCLE_RESOLVED_BIT)
284 out.measurements[i].accumulatedDeltaRangeState |=
285 IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_HALF_CYCLE_RESOLVED;
286 }
287 convertGnssClock(in.clock, out.clock);
288 }
289
290 } // namespace implementation
291 } // namespace V1_1
292 } // namespace gnss
293 } // namespace hardware
294 } // namespace android
295