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