blob: a2a99a040cf98027c5ada54c7afaf1f8bf326def [file] [log] [blame]
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "GnssHAL_GnssInterface"
18
19#include "Gnss.h"
20#include <GnssUtils.h>
21
22namespace android {
23namespace hardware {
24namespace gnss {
25namespace V1_0 {
26namespace implementation {
27
28std::vector<std::unique_ptr<ThreadFuncArgs>> Gnss::sThreadFuncArgsList;
29sp<IGnssCallback> Gnss::sGnssCbIface = nullptr;
30bool Gnss::sInterfaceExists = false;
Wyatt Riley8791e7b2017-01-17 12:12:25 -080031bool Gnss::sWakelockHeldGnss = false;
32bool Gnss::sWakelockHeldFused = false;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070033
34GpsCallbacks Gnss::sGnssCb = {
35 .size = sizeof(GpsCallbacks),
36 .location_cb = locationCb,
37 .status_cb = statusCb,
38 .sv_status_cb = gpsSvStatusCb,
39 .nmea_cb = nmeaCb,
40 .set_capabilities_cb = setCapabilitiesCb,
41 .acquire_wakelock_cb = acquireWakelockCb,
42 .release_wakelock_cb = releaseWakelockCb,
43 .create_thread_cb = createThreadCb,
44 .request_utc_time_cb = requestUtcTimeCb,
45 .set_system_info_cb = setSystemInfoCb,
46 .gnss_sv_status_cb = gnssSvStatusCb,
47};
48
49Gnss::Gnss(gps_device_t* gnssDevice) {
50 /* Error out if an instance of the interface already exists. */
51 LOG_ALWAYS_FATAL_IF(sInterfaceExists);
52 sInterfaceExists = true;
53
54 if (gnssDevice == nullptr) {
55 ALOGE("%s: Invalid device_t handle", __func__);
56 return;
57 }
58
59 mGnssIface = gnssDevice->get_gps_interface(gnssDevice);
60}
61
62Gnss::~Gnss() {
63 sThreadFuncArgsList.clear();
64}
65
66void Gnss::locationCb(GpsLocation* location) {
67 if (sGnssCbIface == nullptr) {
68 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
69 return;
70 }
71
72 if (location == nullptr) {
73 ALOGE("%s: Invalid location from GNSS HAL", __func__);
74 return;
75 }
76
77 android::hardware::gnss::V1_0::GnssLocation gnssLocation = convertToGnssLocation(location);
78 sGnssCbIface->gnssLocationCb(gnssLocation);
79}
80
81void Gnss::statusCb(GpsStatus* gnssStatus) {
82 if (sGnssCbIface == nullptr) {
83 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
84 return;
85 }
86
87 if (gnssStatus == nullptr) {
88 ALOGE("%s: Invalid GpsStatus from GNSS HAL", __func__);
89 return;
90 }
91
92 IGnssCallback::GnssStatusValue status =
93 static_cast<IGnssCallback::GnssStatusValue>(gnssStatus->status);
94
95 sGnssCbIface->gnssStatusCb(status);
96}
97
98void Gnss::gnssSvStatusCb(GnssSvStatus* status) {
99 if (sGnssCbIface == nullptr) {
100 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
101 return;
102 }
103
104 if (status == nullptr) {
105 ALOGE("Invalid status from GNSS HAL %s", __func__);
106 return;
107 }
108
109 IGnssCallback::GnssSvStatus svStatus;
110 svStatus.numSvs = status->num_svs;
111
112 if (svStatus.numSvs > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
113 ALOGW("Too many satellites %zd. Clamps to %d.", svStatus.numSvs, GnssMax::SVS_COUNT);
114 svStatus.numSvs = static_cast<uint32_t>(GnssMax::SVS_COUNT);
115 }
116
117 for (size_t i = 0; i < svStatus.numSvs; i++) {
118 auto svInfo = status->gnss_sv_list[i];
119 IGnssCallback::GnssSvInfo gnssSvInfo = {
120 .svid = svInfo.svid,
121 .constellation = static_cast<android::hardware::gnss::V1_0::GnssConstellationType>(
122 svInfo.constellation),
123 .cN0Dbhz = svInfo.c_n0_dbhz,
124 .elevationDegrees = svInfo.elevation,
125 .azimuthDegrees = svInfo.azimuth,
gomoc3d92782017-01-11 14:04:21 -0800126 .svFlag = svInfo.flags,
127 // Older chipsets do not provide carrier frequency, hence HAS_CARRIER_FREQUENCY flag
128 // is not set and the carrierFrequencyHz field is set to zero
129 .carrierFrequencyHz = 0
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700130 };
131 svStatus.gnssSvList[i] = gnssSvInfo;
132 }
133
134 sGnssCbIface->gnssSvStatusCb(svStatus);
135}
136
137/*
138 * This enum is used by gpsSvStatusCb() method below to convert GpsSvStatus
139 * to GnssSvStatus for backward compatibility. It is only used by the default
140 * implementation and is not part of the GNSS interface.
141 */
142enum SvidValues : uint16_t {
143 GLONASS_SVID_OFFSET = 64,
144 GLONASS_SVID_COUNT = 24,
145 BEIDOU_SVID_OFFSET = 200,
146 BEIDOU_SVID_COUNT = 35,
147 SBAS_SVID_MIN = 33,
148 SBAS_SVID_MAX = 64,
149 SBAS_SVID_ADD = 87,
150 QZSS_SVID_MIN = 193,
151 QZSS_SVID_MAX = 200
152};
153
154/*
155 * The following code that converts GpsSvStatus to GnssSvStatus is moved here from
156 * GnssLocationProvider. GnssLocationProvider does not require it anymore since GpsSvStatus is
157 * being deprecated and is no longer part of the GNSS interface.
158 */
159void Gnss::gpsSvStatusCb(GpsSvStatus* svInfo) {
160 if (sGnssCbIface == nullptr) {
161 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
162 return;
163 }
164
165 if (svInfo == nullptr) {
166 ALOGE("Invalid status from GNSS HAL %s", __func__);
167 return;
168 }
169
170 IGnssCallback::GnssSvStatus svStatus;
171 svStatus.numSvs = svInfo->num_svs;
172 /*
173 * Clamp the list size since GnssSvStatus can support a maximum of
174 * GnssMax::SVS_COUNT entries.
175 */
176 if (svStatus.numSvs > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
177 ALOGW("Too many satellites %zd. Clamps to %d.", svStatus.numSvs, GnssMax::SVS_COUNT);
178 svStatus.numSvs = static_cast<uint32_t>(GnssMax::SVS_COUNT);
179 }
180
181 uint32_t ephemerisMask = svInfo->ephemeris_mask;
182 uint32_t almanacMask = svInfo->almanac_mask;
183 uint32_t usedInFixMask = svInfo->used_in_fix_mask;
184 /*
185 * Conversion from GpsSvInfo to IGnssCallback::GnssSvInfo happens below.
186 */
187 for (size_t i = 0; i < svStatus.numSvs; i++) {
188 IGnssCallback::GnssSvInfo& info = svStatus.gnssSvList[i];
189 info.svid = svInfo->sv_list[i].prn;
190 if (info.svid >= 1 && info.svid <= 32) {
191 info.constellation = GnssConstellationType::GPS;
192 } else if (info.svid > GLONASS_SVID_OFFSET &&
193 info.svid <= GLONASS_SVID_OFFSET + GLONASS_SVID_COUNT) {
194 info.constellation = GnssConstellationType::GLONASS;
195 info.svid -= GLONASS_SVID_OFFSET;
196 } else if (info.svid > BEIDOU_SVID_OFFSET &&
197 info.svid <= BEIDOU_SVID_OFFSET + BEIDOU_SVID_COUNT) {
198 info.constellation = GnssConstellationType::BEIDOU;
199 info.svid -= BEIDOU_SVID_OFFSET;
200 } else if (info.svid >= SBAS_SVID_MIN && info.svid <= SBAS_SVID_MAX) {
201 info.constellation = GnssConstellationType::SBAS;
202 info.svid += SBAS_SVID_ADD;
203 } else if (info.svid >= QZSS_SVID_MIN && info.svid <= QZSS_SVID_MAX) {
204 info.constellation = GnssConstellationType::QZSS;
205 } else {
206 ALOGD("Unknown constellation type with Svid = %d.", info.svid);
207 info.constellation = GnssConstellationType::UNKNOWN;
208 }
209
210 info.cN0Dbhz = svInfo->sv_list[i].snr;
211 info.elevationDegrees = svInfo->sv_list[i].elevation;
212 info.azimuthDegrees = svInfo->sv_list[i].azimuth;
Yifan Hong35e2aac2016-12-05 12:35:05 -0800213 // TODO: b/31702236
214 info.svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700215
216 /*
217 * Only GPS info is valid for these fields, as these masks are just 32
218 * bits, by GPS prn.
219 */
220 if (info.constellation == GnssConstellationType::GPS) {
221 int32_t svidMask = (1 << (info.svid - 1));
222 if ((ephemerisMask & svidMask) != 0) {
223 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
224 }
225 if ((almanacMask & svidMask) != 0) {
226 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
227 }
228 if ((usedInFixMask & svidMask) != 0) {
229 info.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
230 }
231 }
232 }
233
234 sGnssCbIface->gnssSvStatusCb(svStatus);
235}
236
237void Gnss::nmeaCb(GpsUtcTime timestamp, const char* nmea, int length) {
238 if (sGnssCbIface == nullptr) {
239 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
240 return;
241 }
242
243 android::hardware::hidl_string nmeaString;
244 nmeaString.setToExternal(nmea, length);
245 sGnssCbIface->gnssNmeaCb(timestamp, nmeaString);
246}
247
248void Gnss::setCapabilitiesCb(uint32_t capabilities) {
249 if (sGnssCbIface == nullptr) {
250 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
251 return;
252 }
253
254 sGnssCbIface->gnssSetCapabilitesCb(capabilities);
255}
256
257void Gnss::acquireWakelockCb() {
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800258 acquireWakelockGnss();
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700259}
260
261void Gnss::releaseWakelockCb() {
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800262 releaseWakelockGnss();
263}
264
265
266void Gnss::acquireWakelockGnss() {
267 sWakelockHeldGnss = true;
268 updateWakelock();
269}
270
271void Gnss::releaseWakelockGnss() {
272 sWakelockHeldGnss = false;
273 updateWakelock();
274}
275
276void Gnss::acquireWakelockFused() {
277 sWakelockHeldFused = true;
278 updateWakelock();
279}
280
281void Gnss::releaseWakelockFused() {
282 sWakelockHeldFused = false;
283 updateWakelock();
284}
285
286void Gnss::updateWakelock() {
287 // Track the state of the last request - in case the wake lock in the layer above is reference
288 // counted.
289 static bool sWakelockHeld = false;
290
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700291 if (sGnssCbIface == nullptr) {
292 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
293 return;
294 }
295
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800296 if (sWakelockHeldGnss || sWakelockHeldFused) {
297 if (!sWakelockHeld) {
298 ALOGI("%s: GNSS HAL Wakelock acquired due to gps: %d, fused: %d", __func__,
299 sWakelockHeldGnss, sWakelockHeldFused);
300 sWakelockHeld = true;
301 sGnssCbIface->gnssAcquireWakelockCb();
302 }
303 } else {
304 if (sWakelockHeld) {
305 ALOGI("%s: GNSS HAL Wakelock released", __func__);
306 } else {
307 // To avoid burning power, always release, even if logic got here with sWakelock false
308 // which it shouldn't, unless underlying *.h implementation makes duplicate requests.
309 ALOGW("%s: GNSS HAL Wakelock released, duplicate request", __func__);
310 }
311 sWakelockHeld = false;
312 sGnssCbIface->gnssReleaseWakelockCb();
313 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700314}
315
316void Gnss::requestUtcTimeCb() {
317 if (sGnssCbIface == nullptr) {
318 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
319 return;
320 }
321
322 sGnssCbIface->gnssRequestTimeCb();
323}
324
325pthread_t Gnss::createThreadCb(const char* name, void (*start)(void*), void* arg) {
326 return createPthread(name, start, arg, &sThreadFuncArgsList);
327}
328
329void Gnss::setSystemInfoCb(const LegacyGnssSystemInfo* info) {
330 if (sGnssCbIface == nullptr) {
331 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
332 return;
333 }
334
335 if (info == nullptr) {
336 ALOGE("Invalid GnssSystemInfo from GNSS HAL %s", __func__);
337 return;
338 }
339
340 IGnssCallback::GnssSystemInfo gnssInfo = {
341 .yearOfHw = info->year_of_hw
342 };
343
344 sGnssCbIface->gnssSetSystemInfoCb(gnssInfo);
345}
346
347
348// Methods from ::android::hardware::gnss::V1_0::IGnss follow.
349Return<bool> Gnss::setCallback(const sp<IGnssCallback>& callback) {
350 if (mGnssIface == nullptr) {
351 ALOGE("%s: Gnss interface is unavailable", __func__);
352 return false;
353 }
354
355 sGnssCbIface = callback;
356
357 return (mGnssIface->init(&sGnssCb) == 0);
358}
359
360Return<bool> Gnss::start() {
361 if (mGnssIface == nullptr) {
362 ALOGE("%s: Gnss interface is unavailable", __func__);
363 return false;
364 }
365
366 return (mGnssIface->start() == 0);
367}
368
369Return<bool> Gnss::stop() {
370 if (mGnssIface == nullptr) {
371 ALOGE("%s: Gnss interface is unavailable", __func__);
372 return false;
373 }
374
375 return (mGnssIface->stop() == 0);
376}
377
378Return<void> Gnss::cleanup() {
379 if (mGnssIface == nullptr) {
380 ALOGE("%s: Gnss interface is unavailable", __func__);
381 } else {
382 mGnssIface->cleanup();
383 }
384 return Void();
385}
386
387Return<bool> Gnss::injectLocation(double latitudeDegrees,
388 double longitudeDegrees,
389 float accuracyMeters) {
390 if (mGnssIface == nullptr) {
391 ALOGE("%s: Gnss interface is unavailable", __func__);
392 return false;
393 }
394
395 return (mGnssIface->inject_location(latitudeDegrees, longitudeDegrees, accuracyMeters) == 0);
396}
397
398Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
399 int32_t uncertaintyMs) {
400 if (mGnssIface == nullptr) {
401 ALOGE("%s: Gnss interface is unavailable", __func__);
402 return false;
403 }
404
405 return (mGnssIface->inject_time(timeMs, timeReferenceMs, uncertaintyMs) == 0);
406}
407
408Return<void> Gnss::deleteAidingData(IGnss::GnssAidingData aidingDataFlags) {
409 if (mGnssIface == nullptr) {
410 ALOGE("%s: Gnss interface is unavailable", __func__);
411 } else {
412 mGnssIface->delete_aiding_data(static_cast<GpsAidingData>(aidingDataFlags));
413 }
414 return Void();
415}
416
417Return<bool> Gnss::setPositionMode(IGnss::GnssPositionMode mode,
418 IGnss::GnssPositionRecurrence recurrence,
419 uint32_t minIntervalMs,
420 uint32_t preferredAccuracyMeters,
421 uint32_t preferredTimeMs) {
422 if (mGnssIface == nullptr) {
423 ALOGE("%s: Gnss interface is unavailable", __func__);
424 return false;
425 }
426
427 return (mGnssIface->set_position_mode(static_cast<GpsPositionMode>(mode),
428 static_cast<GpsPositionRecurrence>(recurrence),
429 minIntervalMs,
430 preferredAccuracyMeters,
431 preferredTimeMs) == 0);
432}
433
Martijn Coenen527924a2017-01-04 12:59:48 +0100434Return<sp<IAGnssRil>> Gnss::getExtensionAGnssRil() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700435 if (mGnssIface == nullptr) {
436 ALOGE("%s: Gnss interface is unavailable", __func__);
437 } else {
438 const AGpsRilInterface* agpsRilIface = static_cast<const AGpsRilInterface*>(
439 mGnssIface->get_extension(AGPS_RIL_INTERFACE));
440 if (agpsRilIface == nullptr) {
441 ALOGE("%s GnssRil interface not implemented by GNSS HAL", __func__);
442 } else {
443 mGnssRil = new AGnssRil(agpsRilIface);
444 }
445 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100446 return mGnssRil;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700447}
448
Martijn Coenen527924a2017-01-04 12:59:48 +0100449Return<sp<IGnssConfiguration>> Gnss::getExtensionGnssConfiguration() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700450 if (mGnssIface == nullptr) {
451 ALOGE("%s: Gnss interface is unavailable", __func__);
452 } else {
453 const GnssConfigurationInterface* gnssConfigIface =
454 static_cast<const GnssConfigurationInterface*>(
455 mGnssIface->get_extension(GNSS_CONFIGURATION_INTERFACE));
456
457 if (gnssConfigIface == nullptr) {
458 ALOGE("%s GnssConfiguration interface not implemented by GNSS HAL", __func__);
459 } else {
460 mGnssConfig = new GnssConfiguration(gnssConfigIface);
461 }
462 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100463 return mGnssConfig;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700464}
Martijn Coenen527924a2017-01-04 12:59:48 +0100465Return<sp<IGnssGeofencing>> Gnss::getExtensionGnssGeofencing() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700466 if (mGnssIface == nullptr) {
467 ALOGE("%s: Gnss interface is unavailable", __func__);
468 } else {
469 const GpsGeofencingInterface* gpsGeofencingIface =
470 static_cast<const GpsGeofencingInterface*>(
471 mGnssIface->get_extension(GPS_GEOFENCING_INTERFACE));
472
473 if (gpsGeofencingIface == nullptr) {
474 ALOGE("%s GnssGeofencing interface not implemented by GNSS HAL", __func__);
475 } else {
476 mGnssGeofencingIface = new GnssGeofencing(gpsGeofencingIface);
477 }
478 }
479
Martijn Coenen527924a2017-01-04 12:59:48 +0100480 return mGnssGeofencingIface;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700481}
482
Martijn Coenen527924a2017-01-04 12:59:48 +0100483Return<sp<IAGnss>> Gnss::getExtensionAGnss() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700484 if (mGnssIface == nullptr) {
485 ALOGE("%s: Gnss interface is unavailable", __func__);
486 } else {
487 const AGpsInterface* agpsIface = static_cast<const AGpsInterface*>(
488 mGnssIface->get_extension(AGPS_INTERFACE));
489 if (agpsIface == nullptr) {
490 ALOGE("%s AGnss interface not implemented by GNSS HAL", __func__);
491 } else {
492 mAGnssIface = new AGnss(agpsIface);
493 }
494 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100495 return mAGnssIface;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700496}
497
Martijn Coenen527924a2017-01-04 12:59:48 +0100498Return<sp<IGnssNi>> Gnss::getExtensionGnssNi() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700499 if (mGnssIface == nullptr) {
500 ALOGE("%s: Gnss interface is unavailable", __func__);
501 } else {
502 const GpsNiInterface* gpsNiIface = static_cast<const GpsNiInterface*>(
503 mGnssIface->get_extension(GPS_NI_INTERFACE));
504 if (gpsNiIface == nullptr) {
505 ALOGE("%s GnssNi interface not implemented by GNSS HAL", __func__);
506 } else {
507 mGnssNi = new GnssNi(gpsNiIface);
508 }
509 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100510 return mGnssNi;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700511}
512
Martijn Coenen527924a2017-01-04 12:59:48 +0100513Return<sp<IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700514 if (mGnssIface == nullptr) {
515 ALOGE("%s: Gnss interface is unavailable", __func__);
516 } else {
517 const GpsMeasurementInterface* gpsMeasurementIface =
518 static_cast<const GpsMeasurementInterface*>(
519 mGnssIface->get_extension(GPS_MEASUREMENT_INTERFACE));
520
521 if (gpsMeasurementIface == nullptr) {
522 ALOGE("%s GnssMeasurement interface not implemented by GNSS HAL", __func__);
523 } else {
524 mGnssMeasurement = new GnssMeasurement(gpsMeasurementIface);
525 }
526 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100527 return mGnssMeasurement;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700528}
529
Martijn Coenen527924a2017-01-04 12:59:48 +0100530Return<sp<IGnssNavigationMessage>> Gnss::getExtensionGnssNavigationMessage() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700531 if (mGnssIface == nullptr) {
532 ALOGE("%s: Gnss interface is unavailable", __func__);
533 } else {
534 const GpsNavigationMessageInterface* gpsNavigationMessageIface =
535 static_cast<const GpsNavigationMessageInterface*>(
536 mGnssIface->get_extension(GPS_NAVIGATION_MESSAGE_INTERFACE));
537
538 if (gpsNavigationMessageIface == nullptr) {
539 ALOGE("%s GnssNavigationMessage interface not implemented by GNSS HAL",
540 __func__);
541 } else {
542 mGnssNavigationMessage = new GnssNavigationMessage(gpsNavigationMessageIface);
543 }
544 }
545
Martijn Coenen527924a2017-01-04 12:59:48 +0100546 return mGnssNavigationMessage;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700547}
548
Martijn Coenen527924a2017-01-04 12:59:48 +0100549Return<sp<IGnssXtra>> Gnss::getExtensionXtra() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700550 if (mGnssIface == nullptr) {
551 ALOGE("%s: Gnss interface is unavailable", __func__);
552 } else {
553 const GpsXtraInterface* gpsXtraIface = static_cast<const GpsXtraInterface*>(
554 mGnssIface->get_extension(GPS_XTRA_INTERFACE));
555
556 if (gpsXtraIface == nullptr) {
557 ALOGE("%s GnssXtra interface not implemented by HAL", __func__);
558 } else {
559 mGnssXtraIface = new GnssXtra(gpsXtraIface);
560 }
561 }
562
Martijn Coenen527924a2017-01-04 12:59:48 +0100563 return mGnssXtraIface;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700564}
565
Martijn Coenen527924a2017-01-04 12:59:48 +0100566Return<sp<IGnssDebug>> Gnss::getExtensionGnssDebug() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700567 if (mGnssIface == nullptr) {
568 ALOGE("%s: Gnss interface is unavailable", __func__);
569 } else {
570 const GpsDebugInterface* gpsDebugIface = static_cast<const GpsDebugInterface*>(
571 mGnssIface->get_extension(GPS_DEBUG_INTERFACE));
572
573 if (gpsDebugIface == nullptr) {
574 ALOGE("%s: GnssDebug interface is not implemented by HAL", __func__);
575 } else {
576 mGnssDebug = new GnssDebug(gpsDebugIface);
577 }
578 }
579
Martijn Coenen527924a2017-01-04 12:59:48 +0100580 return mGnssDebug;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700581}
582
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800583Return<sp<IGnssBatching>> Gnss::getExtensionGnssBatching() {
584 if (mGnssIface == nullptr) {
585 ALOGE("%s: Gnss interface is unavailable", __func__);
586 } else {
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800587 hw_module_t* module;
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800588 const FlpLocationInterface* flpLocationIface = nullptr;
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800589 int err = hw_get_module(FUSED_LOCATION_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
590
591 if (err != 0) {
592 ALOGE("gnss flp hw_get_module failed: %d", err);
593 } else if (module == nullptr) {
594 ALOGE("Fused Location hw_get_module returned null module");
595 } else if (module->methods == nullptr) {
596 ALOGE("Fused Location hw_get_module returned null methods");
597 } else {
598 hw_device_t* device;
599 err = module->methods->open(module, FUSED_LOCATION_HARDWARE_MODULE_ID, &device);
600 if (err != 0) {
601 ALOGE("flpDevice open failed: %d", err);
602 } else {
603 flp_device_t * flpDevice = reinterpret_cast<flp_device_t*>(device);
604 flpLocationIface = flpDevice->get_flp_interface(flpDevice);
605 }
606 }
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800607
608 if (flpLocationIface == nullptr) {
609 ALOGE("%s: GnssBatching interface is not implemented by HAL", __func__);
610 } else {
611 mGnssBatching = new GnssBatching(flpLocationIface);
612 }
613 }
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800614 return mGnssBatching;
615}
616
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700617IGnss* HIDL_FETCH_IGnss(const char* hal) {
618 hw_module_t* module;
619 IGnss* iface = nullptr;
620 int err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
621
622 if (err == 0) {
623 hw_device_t* device;
624 err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
625 if (err == 0) {
626 iface = new Gnss(reinterpret_cast<gps_device_t*>(device));
627 } else {
628 ALOGE("gnssDevice open %s failed: %d", hal, err);
629 }
630 } else {
631 ALOGE("gnss hw_get_module %s failed: %d", hal, err);
632 }
633 return iface;
634}
635
636} // namespace implementation
637} // namespace V1_0
638} // namespace gnss
639} // namespace hardware
640} // namespace android