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