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