1 /* Copyright (c) 2017-2020, 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_GnssAPIClient"
32 #define SINGLE_SHOT_MIN_TRACKING_INTERVAL_MSEC (590 * 60 * 60 * 1000) // 590 hours
33
34 #include <log_util.h>
35 #include <loc_cfg.h>
36
37 #include "LocationUtil.h"
38 #include "GnssAPIClient.h"
39 #include <LocContext.h>
40
41 namespace android {
42 namespace hardware {
43 namespace gnss {
44 namespace V2_1 {
45 namespace implementation {
46
47 using ::android::hardware::gnss::V2_1::IGnss;
48 using ::android::hardware::gnss::V2_1::IGnssCallback;
49 using ::android::hardware::gnss::V1_0::IGnssNiCallback;
50 using ::android::hardware::gnss::V2_0::GnssLocation;
51
52 static void convertGnssSvStatus(GnssSvNotification& in, V1_0::IGnssCallback::GnssSvStatus& out);
53 static void convertGnssSvStatus(GnssSvNotification& in,
54 hidl_vec<V2_0::IGnssCallback::GnssSvInfo>& out);
55 static void convertGnssSvStatus(GnssSvNotification& in,
56 hidl_vec<V2_1::IGnssCallback::GnssSvInfo>& out);
57
GnssAPIClient(const sp<V1_0::IGnssCallback> & gpsCb,const sp<V1_0::IGnssNiCallback> & niCb)58 GnssAPIClient::GnssAPIClient(const sp<V1_0::IGnssCallback>& gpsCb,
59 const sp<V1_0::IGnssNiCallback>& niCb) :
60 LocationAPIClientBase(),
61 mGnssCbIface(nullptr),
62 mGnssNiCbIface(nullptr),
63 mControlClient(new LocationAPIControlClient()),
64 mLocationCapabilitiesMask(0),
65 mLocationCapabilitiesCached(false),
66 mTracking(false),
67 mGnssCbIface_2_0(nullptr)
68 {
69 LOC_LOGD("%s]: (%p %p)", __FUNCTION__, &gpsCb, &niCb);
70
71 initLocationOptions();
72 gnssUpdateCallbacks(gpsCb, niCb);
73 }
74
GnssAPIClient(const sp<V2_0::IGnssCallback> & gpsCb)75 GnssAPIClient::GnssAPIClient(const sp<V2_0::IGnssCallback>& gpsCb) :
76 LocationAPIClientBase(),
77 mGnssCbIface(nullptr),
78 mGnssNiCbIface(nullptr),
79 mControlClient(new LocationAPIControlClient()),
80 mLocationCapabilitiesMask(0),
81 mLocationCapabilitiesCached(false),
82 mTracking(false),
83 mGnssCbIface_2_0(nullptr)
84 {
85 LOC_LOGD("%s]: (%p)", __FUNCTION__, &gpsCb);
86
87 initLocationOptions();
88 gnssUpdateCallbacks_2_0(gpsCb);
89 }
90
GnssAPIClient(const sp<V2_1::IGnssCallback> & gpsCb)91 GnssAPIClient::GnssAPIClient(const sp<V2_1::IGnssCallback>& gpsCb) :
92 LocationAPIClientBase(),
93 mGnssCbIface(nullptr),
94 mGnssNiCbIface(nullptr),
95 mControlClient(new LocationAPIControlClient()),
96 mLocationCapabilitiesMask(0),
97 mLocationCapabilitiesCached(false),
98 mTracking(false),
99 mGnssCbIface_2_1(nullptr)
100 {
101 LOC_LOGD("%s]: (%p)", __FUNCTION__, &gpsCb);
102
103 initLocationOptions();
104 gnssUpdateCallbacks_2_1(gpsCb);
105 }
106
~GnssAPIClient()107 GnssAPIClient::~GnssAPIClient()
108 {
109 LOC_LOGD("%s]: ()", __FUNCTION__);
110 if (mControlClient) {
111 delete mControlClient;
112 mControlClient = nullptr;
113 }
114 }
115
initLocationOptions()116 void GnssAPIClient::initLocationOptions()
117 {
118 // set default LocationOptions.
119 memset(&mTrackingOptions, 0, sizeof(TrackingOptions));
120 mTrackingOptions.size = sizeof(TrackingOptions);
121 mTrackingOptions.minInterval = 1000;
122 mTrackingOptions.minDistance = 0;
123 mTrackingOptions.mode = GNSS_SUPL_MODE_STANDALONE;
124 }
125
setCallbacks()126 void GnssAPIClient::setCallbacks()
127 {
128 LocationCallbacks locationCallbacks;
129 memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
130 locationCallbacks.size = sizeof(LocationCallbacks);
131
132 locationCallbacks.trackingCb = nullptr;
133 locationCallbacks.trackingCb = [this](Location location) {
134 onTrackingCb(location);
135 };
136
137 locationCallbacks.batchingCb = nullptr;
138 locationCallbacks.geofenceBreachCb = nullptr;
139 locationCallbacks.geofenceStatusCb = nullptr;
140 locationCallbacks.gnssLocationInfoCb = nullptr;
141 locationCallbacks.gnssNiCb = nullptr;
142 if (mGnssNiCbIface != nullptr) {
143 loc_core::ContextBase* context =
144 loc_core::LocContext::getLocContext(
145 NULL, NULL,
146 loc_core::LocContext::mLocationHalName, false);
147 if (!context->hasAgpsExtendedCapabilities()) {
148 LOC_LOGD("Registering NI CB");
149 locationCallbacks.gnssNiCb = [this](uint32_t id, GnssNiNotification gnssNiNotify) {
150 onGnssNiCb(id, gnssNiNotify);
151 };
152 }
153 }
154
155 locationCallbacks.gnssSvCb = nullptr;
156 locationCallbacks.gnssSvCb = [this](GnssSvNotification gnssSvNotification) {
157 onGnssSvCb(gnssSvNotification);
158 };
159
160 locationCallbacks.gnssNmeaCb = nullptr;
161 locationCallbacks.gnssNmeaCb = [this](GnssNmeaNotification gnssNmeaNotification) {
162 onGnssNmeaCb(gnssNmeaNotification);
163 };
164
165 locationCallbacks.gnssMeasurementsCb = nullptr;
166
167 locAPISetCallbacks(locationCallbacks);
168 }
169
170 // for GpsInterface
gnssUpdateCallbacks(const sp<V1_0::IGnssCallback> & gpsCb,const sp<IGnssNiCallback> & niCb)171 void GnssAPIClient::gnssUpdateCallbacks(const sp<V1_0::IGnssCallback>& gpsCb,
172 const sp<IGnssNiCallback>& niCb)
173 {
174 LOC_LOGD("%s]: (%p %p)", __FUNCTION__, &gpsCb, &niCb);
175
176 mMutex.lock();
177 mGnssCbIface = gpsCb;
178 mGnssNiCbIface = niCb;
179 mMutex.unlock();
180
181 if (mGnssCbIface != nullptr || mGnssNiCbIface != nullptr) {
182 setCallbacks();
183 }
184 }
185
gnssUpdateCallbacks_2_0(const sp<V2_0::IGnssCallback> & gpsCb)186 void GnssAPIClient::gnssUpdateCallbacks_2_0(const sp<V2_0::IGnssCallback>& gpsCb)
187 {
188 LOC_LOGD("%s]: (%p)", __FUNCTION__, &gpsCb);
189
190 mMutex.lock();
191 mGnssCbIface_2_0 = gpsCb;
192 mMutex.unlock();
193
194 if (mGnssCbIface_2_0 != nullptr) {
195 setCallbacks();
196 }
197 }
198
gnssUpdateCallbacks_2_1(const sp<V2_1::IGnssCallback> & gpsCb)199 void GnssAPIClient::gnssUpdateCallbacks_2_1(const sp<V2_1::IGnssCallback>& gpsCb)
200 {
201 LOC_LOGD("%s]: (%p)", __FUNCTION__, &gpsCb);
202
203 mMutex.lock();
204 mGnssCbIface_2_1 = gpsCb;
205 mMutex.unlock();
206
207 if (mGnssCbIface_2_1 != nullptr) {
208 setCallbacks();
209 }
210 }
211
gnssStart()212 bool GnssAPIClient::gnssStart()
213 {
214 LOC_LOGD("%s]: ()", __FUNCTION__);
215
216 mMutex.lock();
217 mTracking = true;
218 mMutex.unlock();
219
220 bool retVal = true;
221 locAPIStartTracking(mTrackingOptions);
222 return retVal;
223 }
224
gnssStop()225 bool GnssAPIClient::gnssStop()
226 {
227 LOC_LOGD("%s]: ()", __FUNCTION__);
228
229 mMutex.lock();
230 mTracking = false;
231 mMutex.unlock();
232
233 bool retVal = true;
234 locAPIStopTracking();
235 return retVal;
236 }
237
gnssSetPositionMode(IGnss::GnssPositionMode mode,IGnss::GnssPositionRecurrence recurrence,uint32_t minIntervalMs,uint32_t preferredAccuracyMeters,uint32_t preferredTimeMs,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)238 bool GnssAPIClient::gnssSetPositionMode(IGnss::GnssPositionMode mode,
239 IGnss::GnssPositionRecurrence recurrence, uint32_t minIntervalMs,
240 uint32_t preferredAccuracyMeters, uint32_t preferredTimeMs,
241 GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
242 {
243 LOC_LOGD("%s]: (%d %d %d %d %d %d %d)", __FUNCTION__,
244 (int)mode, recurrence, minIntervalMs, preferredAccuracyMeters,
245 preferredTimeMs, (int)powerMode, timeBetweenMeasurement);
246 bool retVal = true;
247 memset(&mTrackingOptions, 0, sizeof(TrackingOptions));
248 mTrackingOptions.size = sizeof(TrackingOptions);
249 mTrackingOptions.minInterval = minIntervalMs;
250 if (IGnss::GnssPositionMode::MS_ASSISTED == mode ||
251 IGnss::GnssPositionRecurrence::RECURRENCE_SINGLE == recurrence) {
252 // We set a very large interval to simulate SINGLE mode. Once we report a fix,
253 // the caller should take the responsibility to stop the session.
254 // For MSA, we always treat it as SINGLE mode.
255 mTrackingOptions.minInterval = SINGLE_SHOT_MIN_TRACKING_INTERVAL_MSEC;
256 }
257 if (mode == IGnss::GnssPositionMode::STANDALONE)
258 mTrackingOptions.mode = GNSS_SUPL_MODE_STANDALONE;
259 else if (mode == IGnss::GnssPositionMode::MS_BASED)
260 mTrackingOptions.mode = GNSS_SUPL_MODE_MSB;
261 else if (mode == IGnss::GnssPositionMode::MS_ASSISTED)
262 mTrackingOptions.mode = GNSS_SUPL_MODE_MSA;
263 else {
264 LOC_LOGD("%s]: invalid GnssPositionMode: %d", __FUNCTION__, (int)mode);
265 retVal = false;
266 }
267 if (GNSS_POWER_MODE_INVALID != powerMode) {
268 mTrackingOptions.powerMode = powerMode;
269 mTrackingOptions.tbm = timeBetweenMeasurement;
270 }
271 locAPIUpdateTrackingOptions(mTrackingOptions);
272 return retVal;
273 }
274
275 // for GpsNiInterface
gnssNiRespond(int32_t notifId,IGnssNiCallback::GnssUserResponseType userResponse)276 void GnssAPIClient::gnssNiRespond(int32_t notifId,
277 IGnssNiCallback::GnssUserResponseType userResponse)
278 {
279 LOC_LOGD("%s]: (%d %d)", __FUNCTION__, notifId, static_cast<int>(userResponse));
280 GnssNiResponse data;
281 switch (userResponse) {
282 case IGnssNiCallback::GnssUserResponseType::RESPONSE_ACCEPT:
283 data = GNSS_NI_RESPONSE_ACCEPT;
284 break;
285 case IGnssNiCallback::GnssUserResponseType::RESPONSE_DENY:
286 data = GNSS_NI_RESPONSE_DENY;
287 break;
288 case IGnssNiCallback::GnssUserResponseType::RESPONSE_NORESP:
289 data = GNSS_NI_RESPONSE_NO_RESPONSE;
290 break;
291 default:
292 data = GNSS_NI_RESPONSE_IGNORE;
293 break;
294 }
295
296 locAPIGnssNiResponse(notifId, data);
297 }
298
299 // these apis using LocationAPIControlClient
gnssDeleteAidingData(IGnss::GnssAidingData aidingDataFlags)300 void GnssAPIClient::gnssDeleteAidingData(IGnss::GnssAidingData aidingDataFlags)
301 {
302 LOC_LOGD("%s]: (%02hx)", __FUNCTION__, aidingDataFlags);
303 if (mControlClient == nullptr) {
304 return;
305 }
306 GnssAidingData data;
307 memset(&data, 0, sizeof (GnssAidingData));
308 data.sv.svTypeMask = GNSS_AIDING_DATA_SV_TYPE_GPS_BIT |
309 GNSS_AIDING_DATA_SV_TYPE_GLONASS_BIT |
310 GNSS_AIDING_DATA_SV_TYPE_QZSS_BIT |
311 GNSS_AIDING_DATA_SV_TYPE_BEIDOU_BIT |
312 GNSS_AIDING_DATA_SV_TYPE_GALILEO_BIT |
313 GNSS_AIDING_DATA_SV_TYPE_NAVIC_BIT;
314 data.posEngineMask = STANDARD_POSITIONING_ENGINE;
315
316 if (aidingDataFlags == IGnss::GnssAidingData::DELETE_ALL)
317 data.deleteAll = true;
318 else {
319 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_EPHEMERIS)
320 data.sv.svMask |= GNSS_AIDING_DATA_SV_EPHEMERIS_BIT;
321 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_ALMANAC)
322 data.sv.svMask |= GNSS_AIDING_DATA_SV_ALMANAC_BIT;
323 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_POSITION)
324 data.common.mask |= GNSS_AIDING_DATA_COMMON_POSITION_BIT;
325 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_TIME)
326 data.common.mask |= GNSS_AIDING_DATA_COMMON_TIME_BIT;
327 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_IONO)
328 data.sv.svMask |= GNSS_AIDING_DATA_SV_IONOSPHERE_BIT;
329 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_UTC)
330 data.common.mask |= GNSS_AIDING_DATA_COMMON_UTC_BIT;
331 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_HEALTH)
332 data.sv.svMask |= GNSS_AIDING_DATA_SV_HEALTH_BIT;
333 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_SVDIR)
334 data.sv.svMask |= GNSS_AIDING_DATA_SV_DIRECTION_BIT;
335 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_SVSTEER)
336 data.sv.svMask |= GNSS_AIDING_DATA_SV_STEER_BIT;
337 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_SADATA)
338 data.sv.svMask |= GNSS_AIDING_DATA_SV_SA_DATA_BIT;
339 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_RTI)
340 data.common.mask |= GNSS_AIDING_DATA_COMMON_RTI_BIT;
341 if (aidingDataFlags & IGnss::GnssAidingData::DELETE_CELLDB_INFO)
342 data.common.mask |= GNSS_AIDING_DATA_COMMON_CELLDB_BIT;
343 }
344 mControlClient->locAPIGnssDeleteAidingData(data);
345 }
346
gnssEnable(LocationTechnologyType techType)347 void GnssAPIClient::gnssEnable(LocationTechnologyType techType)
348 {
349 LOC_LOGD("%s]: (%0d)", __FUNCTION__, techType);
350 if (mControlClient == nullptr) {
351 return;
352 }
353 mControlClient->locAPIEnable(techType);
354 }
355
gnssDisable()356 void GnssAPIClient::gnssDisable()
357 {
358 LOC_LOGD("%s]: ()", __FUNCTION__);
359 if (mControlClient == nullptr) {
360 return;
361 }
362 mControlClient->locAPIDisable();
363 }
364
gnssConfigurationUpdate(const GnssConfig & gnssConfig)365 void GnssAPIClient::gnssConfigurationUpdate(const GnssConfig& gnssConfig)
366 {
367 LOC_LOGD("%s]: (%02x)", __FUNCTION__, gnssConfig.flags);
368 if (mControlClient == nullptr) {
369 return;
370 }
371 mControlClient->locAPIGnssUpdateConfig(gnssConfig);
372 }
373
requestCapabilities()374 void GnssAPIClient::requestCapabilities() {
375 // only send capablities if it's already cached, otherwise the first time LocationAPI
376 // is initialized, capabilities will be sent by LocationAPI
377 if (mLocationCapabilitiesCached) {
378 onCapabilitiesCb(mLocationCapabilitiesMask);
379 }
380 }
381
382 // callbacks
onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)383 void GnssAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
384 {
385 LOC_LOGD("%s]: (%02x)", __FUNCTION__, capabilitiesMask);
386 mLocationCapabilitiesMask = capabilitiesMask;
387 mLocationCapabilitiesCached = true;
388
389 mMutex.lock();
390 auto gnssCbIface(mGnssCbIface);
391 auto gnssCbIface_2_0(mGnssCbIface_2_0);
392 auto gnssCbIface_2_1(mGnssCbIface_2_1);
393 mMutex.unlock();
394
395 if (gnssCbIface_2_1 != nullptr ||gnssCbIface_2_0 != nullptr || gnssCbIface != nullptr) {
396
397 uint32_t data = (uint32_t) V2_1::IGnssCallback::Capabilities::ANTENNA_INFO;
398
399 if ((capabilitiesMask & LOCATION_CAPABILITIES_TIME_BASED_TRACKING_BIT) ||
400 (capabilitiesMask & LOCATION_CAPABILITIES_TIME_BASED_BATCHING_BIT) ||
401 (capabilitiesMask & LOCATION_CAPABILITIES_DISTANCE_BASED_TRACKING_BIT) ||
402 (capabilitiesMask & LOCATION_CAPABILITIES_DISTANCE_BASED_BATCHING_BIT))
403 data |= IGnssCallback::Capabilities::SCHEDULING;
404 if (capabilitiesMask & LOCATION_CAPABILITIES_GEOFENCE_BIT)
405 data |= V1_0::IGnssCallback::Capabilities::GEOFENCING;
406 if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT)
407 data |= V1_0::IGnssCallback::Capabilities::MEASUREMENTS;
408 if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSB_BIT)
409 data |= IGnssCallback::Capabilities::MSB;
410 if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSA_BIT)
411 data |= IGnssCallback::Capabilities::MSA;
412 if (capabilitiesMask & LOCATION_CAPABILITIES_AGPM_BIT)
413 data |= IGnssCallback::Capabilities::LOW_POWER_MODE;
414 if (capabilitiesMask & LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT)
415 data |= IGnssCallback::Capabilities::SATELLITE_BLACKLIST;
416 if (capabilitiesMask & LOCATION_CAPABILITIES_MEASUREMENTS_CORRECTION_BIT)
417 data |= V2_0::IGnssCallback::Capabilities::MEASUREMENT_CORRECTIONS;
418
419 IGnssCallback::GnssSystemInfo gnssInfo;
420 if (capabilitiesMask & LOCATION_CAPABILITIES_MEASUREMENTS_CORRECTION_BIT) {
421 gnssInfo.yearOfHw = 2020;
422 } else if (capabilitiesMask & LOCATION_CAPABILITIES_PRIVACY_BIT) {
423 gnssInfo.yearOfHw = 2019;
424 } else if (capabilitiesMask & LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT ||
425 capabilitiesMask & LOCATION_CAPABILITIES_AGPM_BIT) {
426 gnssInfo.yearOfHw = 2018;
427 } else if (capabilitiesMask & LOCATION_CAPABILITIES_DEBUG_NMEA_BIT) {
428 gnssInfo.yearOfHw = 2017;
429 } else if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT) {
430 gnssInfo.yearOfHw = 2016;
431 } else {
432 gnssInfo.yearOfHw = 2015;
433 }
434 LOC_LOGV("%s:%d] set_system_info_cb (%d)", __FUNCTION__, __LINE__, gnssInfo.yearOfHw);
435
436 if (gnssCbIface_2_1 != nullptr) {
437 auto r = gnssCbIface_2_1->gnssSetCapabilitiesCb_2_1(data);
438 if (!r.isOk()) {
439 LOC_LOGE("%s] Error from gnssSetCapabilitiesCb_2_1 description=%s",
440 __func__, r.description().c_str());
441 }
442 r = gnssCbIface_2_1->gnssSetSystemInfoCb(gnssInfo);
443 if (!r.isOk()) {
444 LOC_LOGE("%s] Error from gnssSetSystemInfoCb description=%s",
445 __func__, r.description().c_str());
446 }
447 } else if (gnssCbIface_2_0 != nullptr) {
448 auto r = gnssCbIface_2_0->gnssSetCapabilitiesCb_2_0(data);
449 if (!r.isOk()) {
450 LOC_LOGE("%s] Error from gnssSetCapabilitiesCb_2_0 description=%s",
451 __func__, r.description().c_str());
452 }
453 r = gnssCbIface_2_0->gnssSetSystemInfoCb(gnssInfo);
454 if (!r.isOk()) {
455 LOC_LOGE("%s] Error from gnssSetSystemInfoCb description=%s",
456 __func__, r.description().c_str());
457 }
458 } else if (gnssCbIface != nullptr) {
459 auto r = gnssCbIface->gnssSetCapabilitesCb(data);
460 if (!r.isOk()) {
461 LOC_LOGE("%s] Error from gnssSetCapabilitesCb description=%s",
462 __func__, r.description().c_str());
463 }
464 r = gnssCbIface->gnssSetSystemInfoCb(gnssInfo);
465 if (!r.isOk()) {
466 LOC_LOGE("%s] Error from gnssSetSystemInfoCb description=%s",
467 __func__, r.description().c_str());
468 }
469 }
470
471 }
472
473 }
474
onTrackingCb(Location location)475 void GnssAPIClient::onTrackingCb(Location location)
476 {
477 mMutex.lock();
478 auto gnssCbIface(mGnssCbIface);
479 auto gnssCbIface_2_0(mGnssCbIface_2_0);
480 auto gnssCbIface_2_1(mGnssCbIface_2_1);
481 bool isTracking = mTracking;
482 mMutex.unlock();
483
484 LOC_LOGD("%s]: (flags: %02x isTracking: %d)", __FUNCTION__, location.flags, isTracking);
485
486 if (!isTracking) {
487 return;
488 }
489
490 if (gnssCbIface_2_1 != nullptr) {
491 V2_0::GnssLocation gnssLocation;
492 convertGnssLocation(location, gnssLocation);
493 auto r = gnssCbIface_2_1->gnssLocationCb_2_0(gnssLocation);
494 if (!r.isOk()) {
495 LOC_LOGE("%s] Error from gnssLocationCb_2_0 description=%s",
496 __func__, r.description().c_str());
497 }
498 } else if (gnssCbIface_2_0 != nullptr) {
499 V2_0::GnssLocation gnssLocation;
500 convertGnssLocation(location, gnssLocation);
501 auto r = gnssCbIface_2_0->gnssLocationCb_2_0(gnssLocation);
502 if (!r.isOk()) {
503 LOC_LOGE("%s] Error from gnssLocationCb_2_0 description=%s",
504 __func__, r.description().c_str());
505 }
506 } else if (gnssCbIface != nullptr) {
507 V1_0::GnssLocation gnssLocation;
508 convertGnssLocation(location, gnssLocation);
509 auto r = gnssCbIface->gnssLocationCb(gnssLocation);
510 if (!r.isOk()) {
511 LOC_LOGE("%s] Error from gnssLocationCb description=%s",
512 __func__, r.description().c_str());
513 }
514 } else {
515 LOC_LOGW("%s] No GNSS Interface ready for gnssLocationCb ", __FUNCTION__);
516 }
517
518 }
519
onGnssNiCb(uint32_t id,GnssNiNotification gnssNiNotification)520 void GnssAPIClient::onGnssNiCb(uint32_t id, GnssNiNotification gnssNiNotification)
521 {
522 LOC_LOGD("%s]: (id: %d)", __FUNCTION__, id);
523 mMutex.lock();
524 auto gnssNiCbIface(mGnssNiCbIface);
525 mMutex.unlock();
526
527 if (gnssNiCbIface == nullptr) {
528 LOC_LOGE("%s]: mGnssNiCbIface is nullptr", __FUNCTION__);
529 return;
530 }
531
532 IGnssNiCallback::GnssNiNotification notificationGnss = {};
533
534 notificationGnss.notificationId = id;
535
536 if (gnssNiNotification.type == GNSS_NI_TYPE_VOICE)
537 notificationGnss.niType = IGnssNiCallback::GnssNiType::VOICE;
538 else if (gnssNiNotification.type == GNSS_NI_TYPE_SUPL)
539 notificationGnss.niType = IGnssNiCallback::GnssNiType::UMTS_SUPL;
540 else if (gnssNiNotification.type == GNSS_NI_TYPE_CONTROL_PLANE)
541 notificationGnss.niType = IGnssNiCallback::GnssNiType::UMTS_CTRL_PLANE;
542 else if (gnssNiNotification.type == GNSS_NI_TYPE_EMERGENCY_SUPL)
543 notificationGnss.niType = IGnssNiCallback::GnssNiType::EMERGENCY_SUPL;
544
545 if (gnssNiNotification.options & GNSS_NI_OPTIONS_NOTIFICATION_BIT)
546 notificationGnss.notifyFlags |= IGnssNiCallback::GnssNiNotifyFlags::NEED_NOTIFY;
547 if (gnssNiNotification.options & GNSS_NI_OPTIONS_VERIFICATION_BIT)
548 notificationGnss.notifyFlags |= IGnssNiCallback::GnssNiNotifyFlags::NEED_VERIFY;
549 if (gnssNiNotification.options & GNSS_NI_OPTIONS_PRIVACY_OVERRIDE_BIT)
550 notificationGnss.notifyFlags |= IGnssNiCallback::GnssNiNotifyFlags::PRIVACY_OVERRIDE;
551
552 notificationGnss.timeoutSec = gnssNiNotification.timeout;
553
554 if (gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_ACCEPT)
555 notificationGnss.defaultResponse = IGnssNiCallback::GnssUserResponseType::RESPONSE_ACCEPT;
556 else if (gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_DENY)
557 notificationGnss.defaultResponse = IGnssNiCallback::GnssUserResponseType::RESPONSE_DENY;
558 else if (gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_NO_RESPONSE ||
559 gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_IGNORE)
560 notificationGnss.defaultResponse = IGnssNiCallback::GnssUserResponseType::RESPONSE_NORESP;
561
562 notificationGnss.requestorId = gnssNiNotification.requestor;
563
564 notificationGnss.notificationMessage = gnssNiNotification.message;
565
566 if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_NONE)
567 notificationGnss.requestorIdEncoding =
568 IGnssNiCallback::GnssNiEncodingType::ENC_NONE;
569 else if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_GSM_DEFAULT)
570 notificationGnss.requestorIdEncoding =
571 IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_GSM_DEFAULT;
572 else if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_UTF8)
573 notificationGnss.requestorIdEncoding =
574 IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UTF8;
575 else if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_UCS2)
576 notificationGnss.requestorIdEncoding =
577 IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UCS2;
578
579 if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_NONE)
580 notificationGnss.notificationIdEncoding =
581 IGnssNiCallback::GnssNiEncodingType::ENC_NONE;
582 else if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_GSM_DEFAULT)
583 notificationGnss.notificationIdEncoding =
584 IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_GSM_DEFAULT;
585 else if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_UTF8)
586 notificationGnss.notificationIdEncoding =
587 IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UTF8;
588 else if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_UCS2)
589 notificationGnss.notificationIdEncoding =
590 IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UCS2;
591
592 gnssNiCbIface->niNotifyCb(notificationGnss);
593 }
594
onGnssSvCb(GnssSvNotification gnssSvNotification)595 void GnssAPIClient::onGnssSvCb(GnssSvNotification gnssSvNotification)
596 {
597 LOC_LOGD("%s]: (count: %u)", __FUNCTION__, gnssSvNotification.count);
598 mMutex.lock();
599 auto gnssCbIface(mGnssCbIface);
600 auto gnssCbIface_2_0(mGnssCbIface_2_0);
601 auto gnssCbIface_2_1(mGnssCbIface_2_1);
602 mMutex.unlock();
603
604 if (gnssCbIface_2_1 != nullptr) {
605 hidl_vec<V2_1::IGnssCallback::GnssSvInfo> svInfoList;
606 convertGnssSvStatus(gnssSvNotification, svInfoList);
607 auto r = gnssCbIface_2_1->gnssSvStatusCb_2_1(svInfoList);
608 if (!r.isOk()) {
609 LOC_LOGE("%s] Error from gnssSvStatusCb_2_1 description=%s",
610 __func__, r.description().c_str());
611 }
612 } else if (gnssCbIface_2_0 != nullptr) {
613 hidl_vec<V2_0::IGnssCallback::GnssSvInfo> svInfoList;
614 convertGnssSvStatus(gnssSvNotification, svInfoList);
615 auto r = gnssCbIface_2_0->gnssSvStatusCb_2_0(svInfoList);
616 if (!r.isOk()) {
617 LOC_LOGE("%s] Error from gnssSvStatusCb_2_0 description=%s",
618 __func__, r.description().c_str());
619 }
620 } else if (gnssCbIface != nullptr) {
621 V1_0::IGnssCallback::GnssSvStatus svStatus;
622 convertGnssSvStatus(gnssSvNotification, svStatus);
623 auto r = gnssCbIface->gnssSvStatusCb(svStatus);
624 if (!r.isOk()) {
625 LOC_LOGE("%s] Error from gnssSvStatusCb description=%s",
626 __func__, r.description().c_str());
627 }
628 }
629 }
630
onGnssNmeaCb(GnssNmeaNotification gnssNmeaNotification)631 void GnssAPIClient::onGnssNmeaCb(GnssNmeaNotification gnssNmeaNotification)
632 {
633 mMutex.lock();
634 auto gnssCbIface(mGnssCbIface);
635 auto gnssCbIface_2_0(mGnssCbIface_2_0);
636 auto gnssCbIface_2_1(mGnssCbIface_2_1);
637 mMutex.unlock();
638
639 if (gnssCbIface != nullptr || gnssCbIface_2_0 != nullptr| gnssCbIface_2_1 != nullptr) {
640 const std::string s(gnssNmeaNotification.nmea);
641 std::stringstream ss(s);
642 std::string each;
643 while(std::getline(ss, each, '\n')) {
644 each += '\n';
645 android::hardware::hidl_string nmeaString;
646 nmeaString.setToExternal(each.c_str(), each.length());
647 if (gnssCbIface_2_1 != nullptr) {
648 auto r = gnssCbIface_2_1->gnssNmeaCb(
649 static_cast<V1_0::GnssUtcTime>(gnssNmeaNotification.timestamp), nmeaString);
650 if (!r.isOk()) {
651 LOC_LOGE("%s] Error from gnssCbIface_2_1 nmea=%s length=%u description=%s",
652 __func__, gnssNmeaNotification.nmea, gnssNmeaNotification.length,
653 r.description().c_str());
654 }
655 } else if (gnssCbIface_2_0 != nullptr) {
656 auto r = gnssCbIface_2_0->gnssNmeaCb(
657 static_cast<V1_0::GnssUtcTime>(gnssNmeaNotification.timestamp), nmeaString);
658 if (!r.isOk()) {
659 LOC_LOGE("%s] Error from gnssCbIface_2_0 nmea=%s length=%u description=%s",
660 __func__, gnssNmeaNotification.nmea, gnssNmeaNotification.length,
661 r.description().c_str());
662 }
663 } else if (gnssCbIface != nullptr) {
664 auto r = gnssCbIface->gnssNmeaCb(
665 static_cast<V1_0::GnssUtcTime>(gnssNmeaNotification.timestamp), nmeaString);
666 if (!r.isOk()) {
667 LOC_LOGE("%s] Error from gnssNmeaCb nmea=%s length=%u description=%s",
668 __func__, gnssNmeaNotification.nmea, gnssNmeaNotification.length,
669 r.description().c_str());
670 }
671 }
672 }
673 }
674 }
675
onStartTrackingCb(LocationError error)676 void GnssAPIClient::onStartTrackingCb(LocationError error)
677 {
678 LOC_LOGD("%s]: (%d)", __FUNCTION__, error);
679 mMutex.lock();
680 auto gnssCbIface(mGnssCbIface);
681 auto gnssCbIface_2_0(mGnssCbIface_2_0);
682 auto gnssCbIface_2_1(mGnssCbIface_2_1);
683 mMutex.unlock();
684
685 if (error == LOCATION_ERROR_SUCCESS) {
686 if (gnssCbIface_2_1 != nullptr) {
687 auto r = gnssCbIface_2_1->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_ON);
688 if (!r.isOk()) {
689 LOC_LOGE("%s] Error from gnssStatusCb 2_0 ENGINE_ON description=%s",
690 __func__, r.description().c_str());
691 }
692 r = gnssCbIface_2_1->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_BEGIN);
693 if (!r.isOk()) {
694 LOC_LOGE("%s] Error from gnssStatusCb 2_0 SESSION_BEGIN description=%s",
695 __func__, r.description().c_str());
696 }
697 } else if (gnssCbIface_2_0 != nullptr) {
698 auto r = gnssCbIface_2_0->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_ON);
699 if (!r.isOk()) {
700 LOC_LOGE("%s] Error from gnssStatusCb 2_0 ENGINE_ON description=%s",
701 __func__, r.description().c_str());
702 }
703 r = gnssCbIface_2_0->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_BEGIN);
704 if (!r.isOk()) {
705 LOC_LOGE("%s] Error from gnssStatusCb 2_0 SESSION_BEGIN description=%s",
706 __func__, r.description().c_str());
707 }
708 } else if (gnssCbIface != nullptr) {
709 auto r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_ON);
710 if (!r.isOk()) {
711 LOC_LOGE("%s] Error from gnssStatusCb ENGINE_ON description=%s",
712 __func__, r.description().c_str());
713 }
714 r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_BEGIN);
715 if (!r.isOk()) {
716 LOC_LOGE("%s] Error from gnssStatusCb SESSION_BEGIN description=%s",
717 __func__, r.description().c_str());
718 }
719 }
720 }
721 }
722
onStopTrackingCb(LocationError error)723 void GnssAPIClient::onStopTrackingCb(LocationError error)
724 {
725 LOC_LOGD("%s]: (%d)", __FUNCTION__, error);
726 mMutex.lock();
727 auto gnssCbIface(mGnssCbIface);
728 auto gnssCbIface_2_0(mGnssCbIface_2_0);
729 auto gnssCbIface_2_1(mGnssCbIface_2_1);
730 mMutex.unlock();
731
732 if (error == LOCATION_ERROR_SUCCESS) {
733 if (gnssCbIface_2_1 != nullptr) {
734 auto r = gnssCbIface_2_1->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_END);
735 if (!r.isOk()) {
736 LOC_LOGE("%s] Error from gnssStatusCb 2_0 SESSION_END description=%s",
737 __func__, r.description().c_str());
738 }
739 r = gnssCbIface_2_1->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_OFF);
740 if (!r.isOk()) {
741 LOC_LOGE("%s] Error from gnssStatusCb 2_0 ENGINE_OFF description=%s",
742 __func__, r.description().c_str());
743 }
744 } else if (gnssCbIface_2_0 != nullptr) {
745 auto r = gnssCbIface_2_0->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_END);
746 if (!r.isOk()) {
747 LOC_LOGE("%s] Error from gnssStatusCb 2_0 SESSION_END description=%s",
748 __func__, r.description().c_str());
749 }
750 r = gnssCbIface_2_0->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_OFF);
751 if (!r.isOk()) {
752 LOC_LOGE("%s] Error from gnssStatusCb 2_0 ENGINE_OFF description=%s",
753 __func__, r.description().c_str());
754 }
755
756 } else if (gnssCbIface != nullptr) {
757 auto r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_END);
758 if (!r.isOk()) {
759 LOC_LOGE("%s] Error from gnssStatusCb SESSION_END description=%s",
760 __func__, r.description().c_str());
761 }
762 r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_OFF);
763 if (!r.isOk()) {
764 LOC_LOGE("%s] Error from gnssStatusCb ENGINE_OFF description=%s",
765 __func__, r.description().c_str());
766 }
767 }
768 }
769 }
770
convertGnssSvStatus(GnssSvNotification & in,V1_0::IGnssCallback::GnssSvStatus & out)771 static void convertGnssSvStatus(GnssSvNotification& in, V1_0::IGnssCallback::GnssSvStatus& out)
772 {
773 memset(&out, 0, sizeof(IGnssCallback::GnssSvStatus));
774 out.numSvs = in.count;
775 if (out.numSvs > static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT)) {
776 LOC_LOGW("%s]: Too many satellites %u. Clamps to %d.",
777 __FUNCTION__, out.numSvs, V1_0::GnssMax::SVS_COUNT);
778 out.numSvs = static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT);
779 }
780 for (size_t i = 0; i < out.numSvs; i++) {
781 out.gnssSvList[i].svid = in.gnssSvs[i].svId;
782 convertGnssConstellationType(in.gnssSvs[i].type, out.gnssSvList[i].constellation);
783 out.gnssSvList[i].cN0Dbhz = in.gnssSvs[i].cN0Dbhz;
784 out.gnssSvList[i].elevationDegrees = in.gnssSvs[i].elevation;
785 out.gnssSvList[i].azimuthDegrees = in.gnssSvs[i].azimuth;
786 out.gnssSvList[i].carrierFrequencyHz = in.gnssSvs[i].carrierFrequencyHz;
787 out.gnssSvList[i].svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
788 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_EPHEMER_BIT)
789 out.gnssSvList[i].svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
790 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_ALMANAC_BIT)
791 out.gnssSvList[i].svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
792 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_USED_IN_FIX_BIT)
793 out.gnssSvList[i].svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
794 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_CARRIER_FREQUENCY_BIT)
795 out.gnssSvList[i].svFlag |= IGnssCallback::GnssSvFlags::HAS_CARRIER_FREQUENCY;
796 }
797 }
798
convertGnssSvStatus(GnssSvNotification & in,hidl_vec<V2_0::IGnssCallback::GnssSvInfo> & out)799 static void convertGnssSvStatus(GnssSvNotification& in,
800 hidl_vec<V2_0::IGnssCallback::GnssSvInfo>& out)
801 {
802 out.resize(in.count);
803 for (size_t i = 0; i < in.count; i++) {
804 out[i].v1_0.svid = in.gnssSvs[i].svId;
805 out[i].v1_0.cN0Dbhz = in.gnssSvs[i].cN0Dbhz;
806 out[i].v1_0.elevationDegrees = in.gnssSvs[i].elevation;
807 out[i].v1_0.azimuthDegrees = in.gnssSvs[i].azimuth;
808 out[i].v1_0.carrierFrequencyHz = in.gnssSvs[i].carrierFrequencyHz;
809 out[i].v1_0.svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
810 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_EPHEMER_BIT)
811 out[i].v1_0.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
812 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_ALMANAC_BIT)
813 out[i].v1_0.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
814 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_USED_IN_FIX_BIT)
815 out[i].v1_0.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
816 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_CARRIER_FREQUENCY_BIT)
817 out[i].v1_0.svFlag |= IGnssCallback::GnssSvFlags::HAS_CARRIER_FREQUENCY;
818
819 convertGnssConstellationType(in.gnssSvs[i].type, out[i].constellation);
820 }
821 }
822
convertGnssSvStatus(GnssSvNotification & in,hidl_vec<V2_1::IGnssCallback::GnssSvInfo> & out)823 static void convertGnssSvStatus(GnssSvNotification& in,
824 hidl_vec<V2_1::IGnssCallback::GnssSvInfo>& out)
825 {
826 out.resize(in.count);
827 for (size_t i = 0; i < in.count; i++) {
828 out[i].v2_0.v1_0.svid = in.gnssSvs[i].svId;
829 out[i].v2_0.v1_0.cN0Dbhz = in.gnssSvs[i].cN0Dbhz;
830 out[i].v2_0.v1_0.elevationDegrees = in.gnssSvs[i].elevation;
831 out[i].v2_0.v1_0.azimuthDegrees = in.gnssSvs[i].azimuth;
832 out[i].v2_0.v1_0.carrierFrequencyHz = in.gnssSvs[i].carrierFrequencyHz;
833 out[i].v2_0.v1_0.svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
834 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_EPHEMER_BIT)
835 out[i].v2_0.v1_0.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
836 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_ALMANAC_BIT)
837 out[i].v2_0.v1_0.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
838 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_USED_IN_FIX_BIT)
839 out[i].v2_0.v1_0.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
840 if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_CARRIER_FREQUENCY_BIT)
841 out[i].v2_0.v1_0.svFlag |= IGnssCallback::GnssSvFlags::HAS_CARRIER_FREQUENCY;
842
843 convertGnssConstellationType(in.gnssSvs[i].type, out[i].v2_0.constellation);
844 out[i].basebandCN0DbHz = in.gnssSvs[i].cN0Dbhz;
845 }
846 }
847
848 } // namespace implementation
849 } // namespace V2_1
850 } // namespace gnss
851 } // namespace hardware
852 } // namespace android
853