1 /* Copyright (c) 2017-2019, 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 V1_1 {
45 namespace implementation {
46 
47 using ::android::hardware::gnss::V1_0::IGnss;
48 using ::android::hardware::gnss::V1_0::IGnssCallback;
49 using ::android::hardware::gnss::V1_0::IGnssNiCallback;
50 using ::android::hardware::gnss::V1_0::GnssLocation;
51 
52 static void convertGnssSvStatus(GnssSvNotification& in, IGnssCallback::GnssSvStatus& out);
53 
GnssAPIClient(const sp<IGnssCallback> & gpsCb,const sp<IGnssNiCallback> & niCb)54 GnssAPIClient::GnssAPIClient(const sp<IGnssCallback>& gpsCb,
55     const sp<IGnssNiCallback>& niCb) :
56     LocationAPIClientBase(),
57     mGnssCbIface(nullptr),
58     mGnssNiCbIface(nullptr),
59     mControlClient(new LocationAPIControlClient()),
60     mLocationCapabilitiesMask(0),
61     mLocationCapabilitiesCached(false)
62 {
63     LOC_LOGD("%s]: (%p %p)", __FUNCTION__, &gpsCb, &niCb);
64 
65     // set default LocationOptions.
66     memset(&mTrackingOptions, 0, sizeof(TrackingOptions));
67     mTrackingOptions.size = sizeof(TrackingOptions);
68     mTrackingOptions.minInterval = 1000;
69     mTrackingOptions.minDistance = 0;
70     mTrackingOptions.mode = GNSS_SUPL_MODE_STANDALONE;
71 
72     gnssUpdateCallbacks(gpsCb, niCb);
73 }
74 
~GnssAPIClient()75 GnssAPIClient::~GnssAPIClient()
76 {
77     LOC_LOGD("%s]: ()", __FUNCTION__);
78     if (mControlClient) {
79         delete mControlClient;
80         mControlClient = nullptr;
81     }
82 }
83 
84 // for GpsInterface
gnssUpdateCallbacks(const sp<IGnssCallback> & gpsCb,const sp<IGnssNiCallback> & niCb)85 void GnssAPIClient::gnssUpdateCallbacks(const sp<IGnssCallback>& gpsCb,
86     const sp<IGnssNiCallback>& niCb)
87 {
88     LOC_LOGD("%s]: (%p %p)", __FUNCTION__, &gpsCb, &niCb);
89 
90     mMutex.lock();
91     mGnssCbIface = gpsCb;
92     mGnssNiCbIface = niCb;
93     mMutex.unlock();
94 
95     LocationCallbacks locationCallbacks;
96     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
97     locationCallbacks.size = sizeof(LocationCallbacks);
98 
99     locationCallbacks.trackingCb = nullptr;
100     if (mGnssCbIface != nullptr) {
101         locationCallbacks.trackingCb = [this](Location location) {
102             onTrackingCb(location);
103         };
104     }
105 
106     locationCallbacks.batchingCb = nullptr;
107     locationCallbacks.geofenceBreachCb = nullptr;
108     locationCallbacks.geofenceStatusCb = nullptr;
109     locationCallbacks.gnssLocationInfoCb = nullptr;
110 
111     locationCallbacks.gnssNiCb = nullptr;
112     loc_core::ContextBase* context =
113             loc_core::LocContext::getLocContext(
114                     NULL, NULL,
115                     loc_core::LocContext::mLocationHalName, false);
116     if (mGnssNiCbIface != nullptr && !context->hasAgpsExtendedCapabilities()) {
117         LOC_LOGD("Registering NI CB");
118         locationCallbacks.gnssNiCb = [this](uint32_t id, GnssNiNotification gnssNiNotification) {
119             onGnssNiCb(id, gnssNiNotification);
120         };
121     }
122 
123     locationCallbacks.gnssSvCb = nullptr;
124     if (mGnssCbIface != nullptr) {
125         locationCallbacks.gnssSvCb = [this](GnssSvNotification gnssSvNotification) {
126             onGnssSvCb(gnssSvNotification);
127         };
128     }
129 
130     locationCallbacks.gnssNmeaCb = nullptr;
131     if (mGnssCbIface != nullptr) {
132         locationCallbacks.gnssNmeaCb = [this](GnssNmeaNotification gnssNmeaNotification) {
133             onGnssNmeaCb(gnssNmeaNotification);
134         };
135     }
136 
137     locationCallbacks.gnssMeasurementsCb = nullptr;
138 
139     locAPISetCallbacks(locationCallbacks);
140 }
141 
gnssStart()142 bool GnssAPIClient::gnssStart()
143 {
144     LOC_LOGD("%s]: ()", __FUNCTION__);
145     bool retVal = true;
146     locAPIStartTracking(mTrackingOptions);
147     return retVal;
148 }
149 
gnssStop()150 bool GnssAPIClient::gnssStop()
151 {
152     LOC_LOGD("%s]: ()", __FUNCTION__);
153     bool retVal = true;
154     locAPIStopTracking();
155     return retVal;
156 }
157 
gnssSetPositionMode(IGnss::GnssPositionMode mode,IGnss::GnssPositionRecurrence recurrence,uint32_t minIntervalMs,uint32_t preferredAccuracyMeters,uint32_t preferredTimeMs,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)158 bool GnssAPIClient::gnssSetPositionMode(IGnss::GnssPositionMode mode,
159         IGnss::GnssPositionRecurrence recurrence, uint32_t minIntervalMs,
160         uint32_t preferredAccuracyMeters, uint32_t preferredTimeMs,
161         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
162 {
163     LOC_LOGD("%s]: (%d %d %d %d %d %d %d)", __FUNCTION__,
164             (int)mode, recurrence, minIntervalMs, preferredAccuracyMeters,
165             preferredTimeMs, (int)powerMode, timeBetweenMeasurement);
166     bool retVal = true;
167     memset(&mTrackingOptions, 0, sizeof(TrackingOptions));
168     mTrackingOptions.size = sizeof(TrackingOptions);
169     mTrackingOptions.minInterval = minIntervalMs;
170     if (IGnss::GnssPositionMode::MS_ASSISTED == mode ||
171             IGnss::GnssPositionRecurrence::RECURRENCE_SINGLE == recurrence) {
172         // We set a very large interval to simulate SINGLE mode. Once we report a fix,
173         // the caller should take the responsibility to stop the session.
174         // For MSA, we always treat it as SINGLE mode.
175         mTrackingOptions.minInterval = SINGLE_SHOT_MIN_TRACKING_INTERVAL_MSEC;
176     }
177     mTrackingOptions.minDistance = preferredAccuracyMeters;
178     if (mode == IGnss::GnssPositionMode::STANDALONE)
179         mTrackingOptions.mode = GNSS_SUPL_MODE_STANDALONE;
180     else if (mode == IGnss::GnssPositionMode::MS_BASED)
181         mTrackingOptions.mode = GNSS_SUPL_MODE_MSB;
182     else if (mode ==  IGnss::GnssPositionMode::MS_ASSISTED)
183         mTrackingOptions.mode = GNSS_SUPL_MODE_MSA;
184     else {
185         LOC_LOGD("%s]: invalid GnssPositionMode: %d", __FUNCTION__, (int)mode);
186         retVal = false;
187     }
188     if (GNSS_POWER_MODE_INVALID != powerMode) {
189         mTrackingOptions.powerMode = powerMode;
190         mTrackingOptions.tbm = timeBetweenMeasurement;
191     }
192     locAPIUpdateTrackingOptions(mTrackingOptions);
193     return retVal;
194 }
195 
196 // for GpsNiInterface
gnssNiRespond(int32_t notifId,IGnssNiCallback::GnssUserResponseType userResponse)197 void GnssAPIClient::gnssNiRespond(int32_t notifId,
198         IGnssNiCallback::GnssUserResponseType userResponse)
199 {
200     LOC_LOGD("%s]: (%d %d)", __FUNCTION__, notifId, static_cast<int>(userResponse));
201     GnssNiResponse data;
202     switch (userResponse) {
203     case IGnssNiCallback::GnssUserResponseType::RESPONSE_ACCEPT:
204         data = GNSS_NI_RESPONSE_ACCEPT;
205         break;
206     case IGnssNiCallback::GnssUserResponseType::RESPONSE_DENY:
207         data = GNSS_NI_RESPONSE_DENY;
208         break;
209     case IGnssNiCallback::GnssUserResponseType::RESPONSE_NORESP:
210         data = GNSS_NI_RESPONSE_NO_RESPONSE;
211         break;
212     default:
213         data = GNSS_NI_RESPONSE_IGNORE;
214         break;
215     }
216 
217     locAPIGnssNiResponse(notifId, data);
218 }
219 
220 // these apis using LocationAPIControlClient
gnssDeleteAidingData(IGnss::GnssAidingData aidingDataFlags)221 void GnssAPIClient::gnssDeleteAidingData(IGnss::GnssAidingData aidingDataFlags)
222 {
223     LOC_LOGD("%s]: (%02hx)", __FUNCTION__, aidingDataFlags);
224     if (mControlClient == nullptr) {
225         return;
226     }
227     GnssAidingData data;
228     memset(&data, 0, sizeof (GnssAidingData));
229     data.sv.svTypeMask = GNSS_AIDING_DATA_SV_TYPE_GPS_BIT |
230         GNSS_AIDING_DATA_SV_TYPE_GLONASS_BIT |
231         GNSS_AIDING_DATA_SV_TYPE_QZSS_BIT |
232         GNSS_AIDING_DATA_SV_TYPE_BEIDOU_BIT |
233         GNSS_AIDING_DATA_SV_TYPE_GALILEO_BIT;
234 
235     if (aidingDataFlags == IGnss::GnssAidingData::DELETE_ALL)
236         data.deleteAll = true;
237     else {
238         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_EPHEMERIS)
239             data.sv.svMask |= GNSS_AIDING_DATA_SV_EPHEMERIS_BIT;
240         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_ALMANAC)
241             data.sv.svMask |= GNSS_AIDING_DATA_SV_ALMANAC_BIT;
242         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_POSITION)
243             data.common.mask |= GNSS_AIDING_DATA_COMMON_POSITION_BIT;
244         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_TIME)
245             data.common.mask |= GNSS_AIDING_DATA_COMMON_TIME_BIT;
246         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_IONO)
247             data.sv.svMask |= GNSS_AIDING_DATA_SV_IONOSPHERE_BIT;
248         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_UTC)
249             data.common.mask |= GNSS_AIDING_DATA_COMMON_UTC_BIT;
250         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_HEALTH)
251             data.sv.svMask |= GNSS_AIDING_DATA_SV_HEALTH_BIT;
252         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_SVDIR)
253             data.sv.svMask |= GNSS_AIDING_DATA_SV_DIRECTION_BIT;
254         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_SVSTEER)
255             data.sv.svMask |= GNSS_AIDING_DATA_SV_STEER_BIT;
256         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_SADATA)
257             data.sv.svMask |= GNSS_AIDING_DATA_SV_SA_DATA_BIT;
258         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_RTI)
259             data.common.mask |= GNSS_AIDING_DATA_COMMON_RTI_BIT;
260         if (aidingDataFlags & IGnss::GnssAidingData::DELETE_CELLDB_INFO)
261             data.common.mask |= GNSS_AIDING_DATA_COMMON_CELLDB_BIT;
262     }
263     mControlClient->locAPIGnssDeleteAidingData(data);
264 }
265 
gnssEnable(LocationTechnologyType techType)266 void GnssAPIClient::gnssEnable(LocationTechnologyType techType)
267 {
268     LOC_LOGD("%s]: (%0d)", __FUNCTION__, techType);
269     if (mControlClient == nullptr) {
270         return;
271     }
272     mControlClient->locAPIEnable(techType);
273 }
274 
gnssDisable()275 void GnssAPIClient::gnssDisable()
276 {
277     LOC_LOGD("%s]: ()", __FUNCTION__);
278     if (mControlClient == nullptr) {
279         return;
280     }
281     mControlClient->locAPIDisable();
282 }
283 
gnssConfigurationUpdate(const GnssConfig & gnssConfig)284 void GnssAPIClient::gnssConfigurationUpdate(const GnssConfig& gnssConfig)
285 {
286     LOC_LOGD("%s]: (%02x)", __FUNCTION__, gnssConfig.flags);
287     if (mControlClient == nullptr) {
288         return;
289     }
290     mControlClient->locAPIGnssUpdateConfig(gnssConfig);
291 }
292 
requestCapabilities()293 void GnssAPIClient::requestCapabilities() {
294     // only send capablities if it's already cached, otherwise the first time LocationAPI
295     // is initialized, capabilities will be sent by LocationAPI
296     if (mLocationCapabilitiesCached) {
297         onCapabilitiesCb(mLocationCapabilitiesMask);
298     }
299 }
300 
301 // callbacks
onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)302 void GnssAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
303 {
304     LOC_LOGD("%s]: (%02x)", __FUNCTION__, capabilitiesMask);
305     mLocationCapabilitiesMask = capabilitiesMask;
306     mLocationCapabilitiesCached = true;
307 
308     mMutex.lock();
309     auto gnssCbIface(mGnssCbIface);
310     mMutex.unlock();
311 
312     if (gnssCbIface != nullptr) {
313         uint32_t data = 0;
314         if ((capabilitiesMask & LOCATION_CAPABILITIES_TIME_BASED_TRACKING_BIT) ||
315                 (capabilitiesMask & LOCATION_CAPABILITIES_TIME_BASED_BATCHING_BIT) ||
316                 (capabilitiesMask & LOCATION_CAPABILITIES_DISTANCE_BASED_TRACKING_BIT) ||
317                 (capabilitiesMask & LOCATION_CAPABILITIES_DISTANCE_BASED_BATCHING_BIT))
318             data |= IGnssCallback::Capabilities::SCHEDULING;
319         if (capabilitiesMask & LOCATION_CAPABILITIES_GEOFENCE_BIT)
320             data |= IGnssCallback::Capabilities::GEOFENCING;
321         if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT)
322             data |= IGnssCallback::Capabilities::MEASUREMENTS;
323         if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSB_BIT)
324             data |= IGnssCallback::Capabilities::MSB;
325         if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSA_BIT)
326             data |= IGnssCallback::Capabilities::MSA;
327         auto r = gnssCbIface->gnssSetCapabilitesCb(data);
328         if (!r.isOk()) {
329             LOC_LOGE("%s] Error from gnssSetCapabilitesCb description=%s",
330                 __func__, r.description().c_str());
331         }
332     }
333     if (gnssCbIface != nullptr) {
334         IGnssCallback::GnssSystemInfo gnssInfo;
335         if (capabilitiesMask & LOCATION_CAPABILITIES_CONSTELLATION_ENABLEMENT_BIT ||
336             capabilitiesMask & LOCATION_CAPABILITIES_AGPM_BIT) {
337             gnssInfo.yearOfHw = 2018;
338         } else if (capabilitiesMask & LOCATION_CAPABILITIES_DEBUG_NMEA_BIT) {
339             gnssInfo.yearOfHw = 2017;
340         } else if (capabilitiesMask & LOCATION_CAPABILITIES_GNSS_MEASUREMENTS_BIT) {
341             gnssInfo.yearOfHw = 2016;
342         } else {
343             gnssInfo.yearOfHw = 2015;
344         }
345         LOC_LOGV("%s:%d] set_system_info_cb (%d)", __FUNCTION__, __LINE__, gnssInfo.yearOfHw);
346         auto r = gnssCbIface->gnssSetSystemInfoCb(gnssInfo);
347         if (!r.isOk()) {
348             LOC_LOGE("%s] Error from gnssSetSystemInfoCb description=%s",
349                 __func__, r.description().c_str());
350         }
351     }
352 }
353 
onTrackingCb(Location location)354 void GnssAPIClient::onTrackingCb(Location location)
355 {
356     LOC_LOGD("%s]: (flags: %02x)", __FUNCTION__, location.flags);
357     mMutex.lock();
358     auto gnssCbIface(mGnssCbIface);
359     mMutex.unlock();
360 
361     if (gnssCbIface != nullptr) {
362         GnssLocation gnssLocation;
363         convertGnssLocation(location, gnssLocation);
364         auto r = gnssCbIface->gnssLocationCb(gnssLocation);
365         if (!r.isOk()) {
366             LOC_LOGE("%s] Error from gnssLocationCb description=%s",
367                 __func__, r.description().c_str());
368         }
369     }
370 }
371 
onGnssNiCb(uint32_t id,GnssNiNotification gnssNiNotification)372 void GnssAPIClient::onGnssNiCb(uint32_t id, GnssNiNotification gnssNiNotification)
373 {
374     LOC_LOGD("%s]: (id: %d)", __FUNCTION__, id);
375     mMutex.lock();
376     auto gnssNiCbIface(mGnssNiCbIface);
377     mMutex.unlock();
378 
379     if (gnssNiCbIface == nullptr) {
380         LOC_LOGE("%s]: mGnssNiCbIface is nullptr", __FUNCTION__);
381         return;
382     }
383 
384     IGnssNiCallback::GnssNiNotification notificationGnss = {};
385 
386     notificationGnss.notificationId = id;
387 
388     if (gnssNiNotification.type == GNSS_NI_TYPE_VOICE)
389         notificationGnss.niType = IGnssNiCallback::GnssNiType::VOICE;
390     else if (gnssNiNotification.type == GNSS_NI_TYPE_SUPL)
391         notificationGnss.niType = IGnssNiCallback::GnssNiType::UMTS_SUPL;
392     else if (gnssNiNotification.type == GNSS_NI_TYPE_CONTROL_PLANE)
393         notificationGnss.niType = IGnssNiCallback::GnssNiType::UMTS_CTRL_PLANE;
394     else if (gnssNiNotification.type == GNSS_NI_TYPE_EMERGENCY_SUPL)
395         notificationGnss.niType = IGnssNiCallback::GnssNiType::EMERGENCY_SUPL;
396 
397     if (gnssNiNotification.options & GNSS_NI_OPTIONS_NOTIFICATION_BIT)
398         notificationGnss.notifyFlags |= IGnssNiCallback::GnssNiNotifyFlags::NEED_NOTIFY;
399     if (gnssNiNotification.options & GNSS_NI_OPTIONS_VERIFICATION_BIT)
400         notificationGnss.notifyFlags |= IGnssNiCallback::GnssNiNotifyFlags::NEED_VERIFY;
401     if (gnssNiNotification.options & GNSS_NI_OPTIONS_PRIVACY_OVERRIDE_BIT)
402         notificationGnss.notifyFlags |= IGnssNiCallback::GnssNiNotifyFlags::PRIVACY_OVERRIDE;
403 
404     notificationGnss.timeoutSec = gnssNiNotification.timeout;
405 
406     if (gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_ACCEPT)
407         notificationGnss.defaultResponse = IGnssNiCallback::GnssUserResponseType::RESPONSE_ACCEPT;
408     else if (gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_DENY)
409         notificationGnss.defaultResponse = IGnssNiCallback::GnssUserResponseType::RESPONSE_DENY;
410     else if (gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_NO_RESPONSE ||
411             gnssNiNotification.timeoutResponse == GNSS_NI_RESPONSE_IGNORE)
412         notificationGnss.defaultResponse = IGnssNiCallback::GnssUserResponseType::RESPONSE_NORESP;
413 
414     notificationGnss.requestorId = gnssNiNotification.requestor;
415 
416     notificationGnss.notificationMessage = gnssNiNotification.message;
417 
418     if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_NONE)
419         notificationGnss.requestorIdEncoding =
420             IGnssNiCallback::GnssNiEncodingType::ENC_NONE;
421     else if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_GSM_DEFAULT)
422         notificationGnss.requestorIdEncoding =
423             IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_GSM_DEFAULT;
424     else if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_UTF8)
425         notificationGnss.requestorIdEncoding =
426             IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UTF8;
427     else if (gnssNiNotification.requestorEncoding == GNSS_NI_ENCODING_TYPE_UCS2)
428         notificationGnss.requestorIdEncoding =
429             IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UCS2;
430 
431     if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_NONE)
432         notificationGnss.notificationIdEncoding =
433             IGnssNiCallback::GnssNiEncodingType::ENC_NONE;
434     else if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_GSM_DEFAULT)
435         notificationGnss.notificationIdEncoding =
436             IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_GSM_DEFAULT;
437     else if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_UTF8)
438         notificationGnss.notificationIdEncoding =
439             IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UTF8;
440     else if (gnssNiNotification.messageEncoding == GNSS_NI_ENCODING_TYPE_UCS2)
441         notificationGnss.notificationIdEncoding =
442             IGnssNiCallback::GnssNiEncodingType::ENC_SUPL_UCS2;
443 
444     gnssNiCbIface->niNotifyCb(notificationGnss);
445 }
446 
onGnssSvCb(GnssSvNotification gnssSvNotification)447 void GnssAPIClient::onGnssSvCb(GnssSvNotification gnssSvNotification)
448 {
449     LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, gnssSvNotification.count);
450     mMutex.lock();
451     auto gnssCbIface(mGnssCbIface);
452     mMutex.unlock();
453 
454     if (gnssCbIface != nullptr) {
455         IGnssCallback::GnssSvStatus svStatus;
456         convertGnssSvStatus(gnssSvNotification, svStatus);
457         auto r = gnssCbIface->gnssSvStatusCb(svStatus);
458         if (!r.isOk()) {
459             LOC_LOGE("%s] Error from gnssSvStatusCb description=%s",
460                 __func__, r.description().c_str());
461         }
462     }
463 }
464 
onGnssNmeaCb(GnssNmeaNotification gnssNmeaNotification)465 void GnssAPIClient::onGnssNmeaCb(GnssNmeaNotification gnssNmeaNotification)
466 {
467     mMutex.lock();
468     auto gnssCbIface(mGnssCbIface);
469     mMutex.unlock();
470 
471     if (gnssCbIface != nullptr) {
472         const std::string s(gnssNmeaNotification.nmea);
473         std::stringstream ss(s);
474         std::string each;
475         while(std::getline(ss, each, '\n')) {
476             each += '\n';
477             android::hardware::hidl_string nmeaString;
478             nmeaString.setToExternal(each.c_str(), each.length());
479             auto r = gnssCbIface->gnssNmeaCb(
480                     static_cast<V1_0::GnssUtcTime>(gnssNmeaNotification.timestamp), nmeaString);
481             if (!r.isOk()) {
482                 LOC_LOGE("%s] Error from gnssNmeaCb nmea=%s length=%zu description=%s", __func__,
483                             gnssNmeaNotification.nmea, gnssNmeaNotification.length,
484                             r.description().c_str());
485             }
486         }
487     }
488 }
489 
onStartTrackingCb(LocationError error)490 void GnssAPIClient::onStartTrackingCb(LocationError error)
491 {
492     LOC_LOGD("%s]: (%d)", __FUNCTION__, error);
493     mMutex.lock();
494     auto gnssCbIface(mGnssCbIface);
495     mMutex.unlock();
496 
497     if (error == LOCATION_ERROR_SUCCESS && gnssCbIface != nullptr) {
498         auto r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_ON);
499         if (!r.isOk()) {
500             LOC_LOGE("%s] Error from gnssStatusCb ENGINE_ON description=%s",
501                 __func__, r.description().c_str());
502         }
503         r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_BEGIN);
504         if (!r.isOk()) {
505             LOC_LOGE("%s] Error from gnssStatusCb SESSION_BEGIN description=%s",
506                 __func__, r.description().c_str());
507         }
508     }
509 }
510 
onStopTrackingCb(LocationError error)511 void GnssAPIClient::onStopTrackingCb(LocationError error)
512 {
513     LOC_LOGD("%s]: (%d)", __FUNCTION__, error);
514     mMutex.lock();
515     auto gnssCbIface(mGnssCbIface);
516     mMutex.unlock();
517 
518     if (error == LOCATION_ERROR_SUCCESS && gnssCbIface != nullptr) {
519         auto r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::SESSION_END);
520         if (!r.isOk()) {
521             LOC_LOGE("%s] Error from gnssStatusCb SESSION_END description=%s",
522                 __func__, r.description().c_str());
523         }
524         r = gnssCbIface->gnssStatusCb(IGnssCallback::GnssStatusValue::ENGINE_OFF);
525         if (!r.isOk()) {
526             LOC_LOGE("%s] Error from gnssStatusCb ENGINE_OFF description=%s",
527                 __func__, r.description().c_str());
528         }
529     }
530 }
531 
convertGnssSvStatus(GnssSvNotification & in,IGnssCallback::GnssSvStatus & out)532 static void convertGnssSvStatus(GnssSvNotification& in, IGnssCallback::GnssSvStatus& out)
533 {
534     memset(&out, 0, sizeof(IGnssCallback::GnssSvStatus));
535     out.numSvs = in.count;
536     if (out.numSvs > static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT)) {
537         LOC_LOGW("%s]: Too many satellites %u. Clamps to %d.",
538                 __FUNCTION__,  out.numSvs, V1_0::GnssMax::SVS_COUNT);
539         out.numSvs = static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT);
540     }
541     for (size_t i = 0; i < out.numSvs; i++) {
542         IGnssCallback::GnssSvInfo& info = out.gnssSvList[i];
543         info.svid = in.gnssSvs[i].svId;
544         convertGnssConstellationType(in.gnssSvs[i].type, info.constellation);
545         info.cN0Dbhz = in.gnssSvs[i].cN0Dbhz;
546         info.elevationDegrees = in.gnssSvs[i].elevation;
547         info.azimuthDegrees = in.gnssSvs[i].azimuth;
548         info.carrierFrequencyHz = in.gnssSvs[i].carrierFrequencyHz;
549         info.svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
550         if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_EPHEMER_BIT)
551             info.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
552         if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_ALMANAC_BIT)
553             info.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
554         if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_USED_IN_FIX_BIT)
555             info.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
556         if (in.gnssSvs[i].gnssSvOptionsMask & GNSS_SV_OPTIONS_HAS_CARRIER_FREQUENCY_BIT)
557             info.svFlag |= IGnssCallback::GnssSvFlags::HAS_CARRIER_FREQUENCY;
558     }
559 }
560 
561 }  // namespace implementation
562 }  // namespace V1_1
563 }  // namespace gnss
564 }  // namespace hardware
565 }  // namespace android
566