blob: afb659c6682c8323314ce0a72f67b6287ab55595 [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
Wyatt Riley917640b2017-03-16 16:25:55 -070049uint32_t Gnss::sCapabilitiesCached = 0;
50uint16_t Gnss::sYearOfHwCached = 0;
51
52Gnss::Gnss(gps_device_t* gnssDevice) :
53 mDeathRecipient(new GnssHidlDeathRecipient(this)) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070054 /* Error out if an instance of the interface already exists. */
55 LOG_ALWAYS_FATAL_IF(sInterfaceExists);
56 sInterfaceExists = true;
57
58 if (gnssDevice == nullptr) {
59 ALOGE("%s: Invalid device_t handle", __func__);
60 return;
61 }
62
63 mGnssIface = gnssDevice->get_gps_interface(gnssDevice);
64}
65
66Gnss::~Gnss() {
Hridya Valsarajue020ce22017-02-15 20:31:18 -080067 sInterfaceExists = false;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070068 sThreadFuncArgsList.clear();
69}
70
71void Gnss::locationCb(GpsLocation* location) {
72 if (sGnssCbIface == nullptr) {
73 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
74 return;
75 }
76
77 if (location == nullptr) {
78 ALOGE("%s: Invalid location from GNSS HAL", __func__);
79 return;
80 }
81
82 android::hardware::gnss::V1_0::GnssLocation gnssLocation = convertToGnssLocation(location);
Hridya Valsarajue020ce22017-02-15 20:31:18 -080083 auto ret = sGnssCbIface->gnssLocationCb(gnssLocation);
84 if (!ret.isOk()) {
85 ALOGE("%s: Unable to invoke callback", __func__);
86 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070087}
88
89void Gnss::statusCb(GpsStatus* gnssStatus) {
90 if (sGnssCbIface == nullptr) {
91 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
92 return;
93 }
94
95 if (gnssStatus == nullptr) {
96 ALOGE("%s: Invalid GpsStatus from GNSS HAL", __func__);
97 return;
98 }
99
100 IGnssCallback::GnssStatusValue status =
101 static_cast<IGnssCallback::GnssStatusValue>(gnssStatus->status);
102
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800103 auto ret = sGnssCbIface->gnssStatusCb(status);
104 if (!ret.isOk()) {
105 ALOGE("%s: Unable to invoke callback", __func__);
106 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700107}
108
109void Gnss::gnssSvStatusCb(GnssSvStatus* status) {
110 if (sGnssCbIface == nullptr) {
111 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
112 return;
113 }
114
115 if (status == nullptr) {
116 ALOGE("Invalid status from GNSS HAL %s", __func__);
117 return;
118 }
119
120 IGnssCallback::GnssSvStatus svStatus;
121 svStatus.numSvs = status->num_svs;
122
123 if (svStatus.numSvs > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
124 ALOGW("Too many satellites %zd. Clamps to %d.", svStatus.numSvs, GnssMax::SVS_COUNT);
125 svStatus.numSvs = static_cast<uint32_t>(GnssMax::SVS_COUNT);
126 }
127
128 for (size_t i = 0; i < svStatus.numSvs; i++) {
129 auto svInfo = status->gnss_sv_list[i];
130 IGnssCallback::GnssSvInfo gnssSvInfo = {
131 .svid = svInfo.svid,
132 .constellation = static_cast<android::hardware::gnss::V1_0::GnssConstellationType>(
133 svInfo.constellation),
134 .cN0Dbhz = svInfo.c_n0_dbhz,
135 .elevationDegrees = svInfo.elevation,
136 .azimuthDegrees = svInfo.azimuth,
gomoc3d92782017-01-11 14:04:21 -0800137 .svFlag = svInfo.flags,
138 // Older chipsets do not provide carrier frequency, hence HAS_CARRIER_FREQUENCY flag
139 // is not set and the carrierFrequencyHz field is set to zero
140 .carrierFrequencyHz = 0
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700141 };
142 svStatus.gnssSvList[i] = gnssSvInfo;
143 }
144
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800145 auto ret = sGnssCbIface->gnssSvStatusCb(svStatus);
146 if (!ret.isOk()) {
147 ALOGE("%s: Unable to invoke callback", __func__);
148 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700149}
150
151/*
152 * This enum is used by gpsSvStatusCb() method below to convert GpsSvStatus
153 * to GnssSvStatus for backward compatibility. It is only used by the default
154 * implementation and is not part of the GNSS interface.
155 */
156enum SvidValues : uint16_t {
157 GLONASS_SVID_OFFSET = 64,
158 GLONASS_SVID_COUNT = 24,
159 BEIDOU_SVID_OFFSET = 200,
160 BEIDOU_SVID_COUNT = 35,
161 SBAS_SVID_MIN = 33,
162 SBAS_SVID_MAX = 64,
163 SBAS_SVID_ADD = 87,
164 QZSS_SVID_MIN = 193,
165 QZSS_SVID_MAX = 200
166};
167
168/*
169 * The following code that converts GpsSvStatus to GnssSvStatus is moved here from
170 * GnssLocationProvider. GnssLocationProvider does not require it anymore since GpsSvStatus is
171 * being deprecated and is no longer part of the GNSS interface.
172 */
173void Gnss::gpsSvStatusCb(GpsSvStatus* svInfo) {
174 if (sGnssCbIface == nullptr) {
175 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
176 return;
177 }
178
179 if (svInfo == nullptr) {
180 ALOGE("Invalid status from GNSS HAL %s", __func__);
181 return;
182 }
183
184 IGnssCallback::GnssSvStatus svStatus;
185 svStatus.numSvs = svInfo->num_svs;
186 /*
187 * Clamp the list size since GnssSvStatus can support a maximum of
188 * GnssMax::SVS_COUNT entries.
189 */
190 if (svStatus.numSvs > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
191 ALOGW("Too many satellites %zd. Clamps to %d.", svStatus.numSvs, GnssMax::SVS_COUNT);
192 svStatus.numSvs = static_cast<uint32_t>(GnssMax::SVS_COUNT);
193 }
194
195 uint32_t ephemerisMask = svInfo->ephemeris_mask;
196 uint32_t almanacMask = svInfo->almanac_mask;
197 uint32_t usedInFixMask = svInfo->used_in_fix_mask;
198 /*
199 * Conversion from GpsSvInfo to IGnssCallback::GnssSvInfo happens below.
200 */
201 for (size_t i = 0; i < svStatus.numSvs; i++) {
202 IGnssCallback::GnssSvInfo& info = svStatus.gnssSvList[i];
203 info.svid = svInfo->sv_list[i].prn;
204 if (info.svid >= 1 && info.svid <= 32) {
205 info.constellation = GnssConstellationType::GPS;
206 } else if (info.svid > GLONASS_SVID_OFFSET &&
207 info.svid <= GLONASS_SVID_OFFSET + GLONASS_SVID_COUNT) {
208 info.constellation = GnssConstellationType::GLONASS;
209 info.svid -= GLONASS_SVID_OFFSET;
210 } else if (info.svid > BEIDOU_SVID_OFFSET &&
211 info.svid <= BEIDOU_SVID_OFFSET + BEIDOU_SVID_COUNT) {
212 info.constellation = GnssConstellationType::BEIDOU;
213 info.svid -= BEIDOU_SVID_OFFSET;
214 } else if (info.svid >= SBAS_SVID_MIN && info.svid <= SBAS_SVID_MAX) {
215 info.constellation = GnssConstellationType::SBAS;
216 info.svid += SBAS_SVID_ADD;
217 } else if (info.svid >= QZSS_SVID_MIN && info.svid <= QZSS_SVID_MAX) {
218 info.constellation = GnssConstellationType::QZSS;
219 } else {
220 ALOGD("Unknown constellation type with Svid = %d.", info.svid);
221 info.constellation = GnssConstellationType::UNKNOWN;
222 }
223
224 info.cN0Dbhz = svInfo->sv_list[i].snr;
225 info.elevationDegrees = svInfo->sv_list[i].elevation;
226 info.azimuthDegrees = svInfo->sv_list[i].azimuth;
Yifan Hong35e2aac2016-12-05 12:35:05 -0800227 // TODO: b/31702236
228 info.svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700229
230 /*
231 * Only GPS info is valid for these fields, as these masks are just 32
232 * bits, by GPS prn.
233 */
234 if (info.constellation == GnssConstellationType::GPS) {
235 int32_t svidMask = (1 << (info.svid - 1));
236 if ((ephemerisMask & svidMask) != 0) {
237 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
238 }
239 if ((almanacMask & svidMask) != 0) {
240 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
241 }
242 if ((usedInFixMask & svidMask) != 0) {
243 info.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
244 }
245 }
246 }
247
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800248 auto ret = sGnssCbIface->gnssSvStatusCb(svStatus);
249 if (!ret.isOk()) {
250 ALOGE("%s: Unable to invoke callback", __func__);
251 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700252}
253
254void Gnss::nmeaCb(GpsUtcTime timestamp, const char* nmea, int length) {
255 if (sGnssCbIface == nullptr) {
256 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
257 return;
258 }
259
260 android::hardware::hidl_string nmeaString;
261 nmeaString.setToExternal(nmea, length);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800262 auto ret = sGnssCbIface->gnssNmeaCb(timestamp, nmeaString);
263 if (!ret.isOk()) {
264 ALOGE("%s: Unable to invoke callback", __func__);
265 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700266}
267
268void Gnss::setCapabilitiesCb(uint32_t capabilities) {
269 if (sGnssCbIface == nullptr) {
270 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
271 return;
272 }
273
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800274 auto ret = sGnssCbIface->gnssSetCapabilitesCb(capabilities);
275 if (!ret.isOk()) {
276 ALOGE("%s: Unable to invoke callback", __func__);
277 }
Wyatt Riley917640b2017-03-16 16:25:55 -0700278
279 // Save for reconnection when some legacy hal's don't resend this info
280 sCapabilitiesCached = capabilities;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700281}
282
283void Gnss::acquireWakelockCb() {
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800284 acquireWakelockGnss();
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700285}
286
287void Gnss::releaseWakelockCb() {
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800288 releaseWakelockGnss();
289}
290
291
292void Gnss::acquireWakelockGnss() {
293 sWakelockHeldGnss = true;
294 updateWakelock();
295}
296
297void Gnss::releaseWakelockGnss() {
298 sWakelockHeldGnss = false;
299 updateWakelock();
300}
301
302void Gnss::acquireWakelockFused() {
303 sWakelockHeldFused = true;
304 updateWakelock();
305}
306
307void Gnss::releaseWakelockFused() {
308 sWakelockHeldFused = false;
309 updateWakelock();
310}
311
312void Gnss::updateWakelock() {
313 // Track the state of the last request - in case the wake lock in the layer above is reference
314 // counted.
315 static bool sWakelockHeld = false;
316
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700317 if (sGnssCbIface == nullptr) {
318 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
319 return;
320 }
321
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800322 if (sWakelockHeldGnss || sWakelockHeldFused) {
323 if (!sWakelockHeld) {
324 ALOGI("%s: GNSS HAL Wakelock acquired due to gps: %d, fused: %d", __func__,
325 sWakelockHeldGnss, sWakelockHeldFused);
326 sWakelockHeld = true;
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800327 auto ret = sGnssCbIface->gnssAcquireWakelockCb();
328 if (!ret.isOk()) {
329 ALOGE("%s: Unable to invoke callback", __func__);
330 }
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800331 }
332 } else {
333 if (sWakelockHeld) {
334 ALOGI("%s: GNSS HAL Wakelock released", __func__);
335 } else {
336 // To avoid burning power, always release, even if logic got here with sWakelock false
337 // which it shouldn't, unless underlying *.h implementation makes duplicate requests.
338 ALOGW("%s: GNSS HAL Wakelock released, duplicate request", __func__);
339 }
340 sWakelockHeld = false;
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800341 auto ret = sGnssCbIface->gnssReleaseWakelockCb();
342 if (!ret.isOk()) {
343 ALOGE("%s: Unable to invoke callback", __func__);
344 }
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800345 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700346}
347
348void Gnss::requestUtcTimeCb() {
349 if (sGnssCbIface == nullptr) {
350 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
351 return;
352 }
353
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800354 auto ret = sGnssCbIface->gnssRequestTimeCb();
355 if (!ret.isOk()) {
356 ALOGE("%s: Unable to invoke callback", __func__);
357 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700358}
359
360pthread_t Gnss::createThreadCb(const char* name, void (*start)(void*), void* arg) {
361 return createPthread(name, start, arg, &sThreadFuncArgsList);
362}
363
364void Gnss::setSystemInfoCb(const LegacyGnssSystemInfo* info) {
365 if (sGnssCbIface == nullptr) {
366 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
367 return;
368 }
369
370 if (info == nullptr) {
371 ALOGE("Invalid GnssSystemInfo from GNSS HAL %s", __func__);
372 return;
373 }
374
375 IGnssCallback::GnssSystemInfo gnssInfo = {
376 .yearOfHw = info->year_of_hw
377 };
378
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800379 auto ret = sGnssCbIface->gnssSetSystemInfoCb(gnssInfo);
380 if (!ret.isOk()) {
381 ALOGE("%s: Unable to invoke callback", __func__);
382 }
Wyatt Riley917640b2017-03-16 16:25:55 -0700383
384 // Save for reconnection when some legacy hal's don't resend this info
385 sYearOfHwCached = info->year_of_hw;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700386}
387
388
389// Methods from ::android::hardware::gnss::V1_0::IGnss follow.
390Return<bool> Gnss::setCallback(const sp<IGnssCallback>& callback) {
391 if (mGnssIface == nullptr) {
392 ALOGE("%s: Gnss interface is unavailable", __func__);
393 return false;
394 }
395
Wyatt Riley917640b2017-03-16 16:25:55 -0700396 if (callback == nullptr) {
397 ALOGE("%s: Null callback ignored", __func__);
398 return false;
399 }
400
401 if (sGnssCbIface != NULL) {
402 ALOGW("%s called more than once. Unexpected unless test.", __func__);
403 sGnssCbIface->unlinkToDeath(mDeathRecipient);
404 }
405
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700406 sGnssCbIface = callback;
Wyatt Riley917640b2017-03-16 16:25:55 -0700407 callback->linkToDeath(mDeathRecipient, 0 /*cookie*/);
408
409 // If this was received in the past, send it up again to refresh caller.
410 // mGnssIface will override after init() is called below, if needed
411 // (though it's unlikely the gps.h capabilities or system info will change.)
412 if (sCapabilitiesCached != 0) {
413 setCapabilitiesCb(sCapabilitiesCached);
414 }
415 if (sYearOfHwCached != 0) {
416 LegacyGnssSystemInfo info;
417 info.year_of_hw = sYearOfHwCached;
418 setSystemInfoCb(&info);
419 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700420
421 return (mGnssIface->init(&sGnssCb) == 0);
422}
423
424Return<bool> Gnss::start() {
425 if (mGnssIface == nullptr) {
426 ALOGE("%s: Gnss interface is unavailable", __func__);
427 return false;
428 }
429
430 return (mGnssIface->start() == 0);
431}
432
433Return<bool> Gnss::stop() {
434 if (mGnssIface == nullptr) {
435 ALOGE("%s: Gnss interface is unavailable", __func__);
436 return false;
437 }
438
439 return (mGnssIface->stop() == 0);
440}
441
442Return<void> Gnss::cleanup() {
443 if (mGnssIface == nullptr) {
444 ALOGE("%s: Gnss interface is unavailable", __func__);
445 } else {
446 mGnssIface->cleanup();
447 }
448 return Void();
449}
450
451Return<bool> Gnss::injectLocation(double latitudeDegrees,
452 double longitudeDegrees,
453 float accuracyMeters) {
454 if (mGnssIface == nullptr) {
455 ALOGE("%s: Gnss interface is unavailable", __func__);
456 return false;
457 }
458
459 return (mGnssIface->inject_location(latitudeDegrees, longitudeDegrees, accuracyMeters) == 0);
460}
461
462Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
463 int32_t uncertaintyMs) {
464 if (mGnssIface == nullptr) {
465 ALOGE("%s: Gnss interface is unavailable", __func__);
466 return false;
467 }
468
469 return (mGnssIface->inject_time(timeMs, timeReferenceMs, uncertaintyMs) == 0);
470}
471
472Return<void> Gnss::deleteAidingData(IGnss::GnssAidingData aidingDataFlags) {
473 if (mGnssIface == nullptr) {
474 ALOGE("%s: Gnss interface is unavailable", __func__);
475 } else {
476 mGnssIface->delete_aiding_data(static_cast<GpsAidingData>(aidingDataFlags));
477 }
478 return Void();
479}
480
481Return<bool> Gnss::setPositionMode(IGnss::GnssPositionMode mode,
482 IGnss::GnssPositionRecurrence recurrence,
483 uint32_t minIntervalMs,
484 uint32_t preferredAccuracyMeters,
485 uint32_t preferredTimeMs) {
486 if (mGnssIface == nullptr) {
487 ALOGE("%s: Gnss interface is unavailable", __func__);
488 return false;
489 }
490
491 return (mGnssIface->set_position_mode(static_cast<GpsPositionMode>(mode),
492 static_cast<GpsPositionRecurrence>(recurrence),
493 minIntervalMs,
494 preferredAccuracyMeters,
495 preferredTimeMs) == 0);
496}
497
Martijn Coenen527924a2017-01-04 12:59:48 +0100498Return<sp<IAGnssRil>> Gnss::getExtensionAGnssRil() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700499 if (mGnssIface == nullptr) {
500 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800501 return nullptr;
502 }
503
504 if (mGnssRil == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700505 const AGpsRilInterface* agpsRilIface = static_cast<const AGpsRilInterface*>(
506 mGnssIface->get_extension(AGPS_RIL_INTERFACE));
507 if (agpsRilIface == nullptr) {
508 ALOGE("%s GnssRil interface not implemented by GNSS HAL", __func__);
509 } else {
510 mGnssRil = new AGnssRil(agpsRilIface);
511 }
512 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100513 return mGnssRil;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700514}
515
Martijn Coenen527924a2017-01-04 12:59:48 +0100516Return<sp<IGnssConfiguration>> Gnss::getExtensionGnssConfiguration() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700517 if (mGnssIface == nullptr) {
518 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800519 return nullptr;
520 }
521
522 if (mGnssConfig == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700523 const GnssConfigurationInterface* gnssConfigIface =
524 static_cast<const GnssConfigurationInterface*>(
525 mGnssIface->get_extension(GNSS_CONFIGURATION_INTERFACE));
526
527 if (gnssConfigIface == nullptr) {
528 ALOGE("%s GnssConfiguration interface not implemented by GNSS HAL", __func__);
529 } else {
530 mGnssConfig = new GnssConfiguration(gnssConfigIface);
531 }
532 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100533 return mGnssConfig;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700534}
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800535
Martijn Coenen527924a2017-01-04 12:59:48 +0100536Return<sp<IGnssGeofencing>> Gnss::getExtensionGnssGeofencing() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700537 if (mGnssIface == nullptr) {
538 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800539 return nullptr;
540 }
541
542 if (mGnssGeofencingIface == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700543 const GpsGeofencingInterface* gpsGeofencingIface =
544 static_cast<const GpsGeofencingInterface*>(
545 mGnssIface->get_extension(GPS_GEOFENCING_INTERFACE));
546
547 if (gpsGeofencingIface == nullptr) {
548 ALOGE("%s GnssGeofencing interface not implemented by GNSS HAL", __func__);
549 } else {
550 mGnssGeofencingIface = new GnssGeofencing(gpsGeofencingIface);
551 }
552 }
553
Martijn Coenen527924a2017-01-04 12:59:48 +0100554 return mGnssGeofencingIface;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700555}
556
Martijn Coenen527924a2017-01-04 12:59:48 +0100557Return<sp<IAGnss>> Gnss::getExtensionAGnss() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700558 if (mGnssIface == nullptr) {
559 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800560 return nullptr;
561 }
562
563 if (mAGnssIface == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700564 const AGpsInterface* agpsIface = static_cast<const AGpsInterface*>(
565 mGnssIface->get_extension(AGPS_INTERFACE));
566 if (agpsIface == nullptr) {
567 ALOGE("%s AGnss interface not implemented by GNSS HAL", __func__);
568 } else {
569 mAGnssIface = new AGnss(agpsIface);
570 }
571 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100572 return mAGnssIface;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700573}
574
Martijn Coenen527924a2017-01-04 12:59:48 +0100575Return<sp<IGnssNi>> Gnss::getExtensionGnssNi() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700576 if (mGnssIface == nullptr) {
577 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800578 return nullptr;
579 }
580
581 if (mGnssNi == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700582 const GpsNiInterface* gpsNiIface = static_cast<const GpsNiInterface*>(
583 mGnssIface->get_extension(GPS_NI_INTERFACE));
584 if (gpsNiIface == nullptr) {
585 ALOGE("%s GnssNi interface not implemented by GNSS HAL", __func__);
586 } else {
587 mGnssNi = new GnssNi(gpsNiIface);
588 }
589 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100590 return mGnssNi;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700591}
592
Martijn Coenen527924a2017-01-04 12:59:48 +0100593Return<sp<IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700594 if (mGnssIface == nullptr) {
595 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800596 return nullptr;
597 }
598
599 if (mGnssMeasurement == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700600 const GpsMeasurementInterface* gpsMeasurementIface =
601 static_cast<const GpsMeasurementInterface*>(
602 mGnssIface->get_extension(GPS_MEASUREMENT_INTERFACE));
603
604 if (gpsMeasurementIface == nullptr) {
605 ALOGE("%s GnssMeasurement interface not implemented by GNSS HAL", __func__);
606 } else {
607 mGnssMeasurement = new GnssMeasurement(gpsMeasurementIface);
608 }
609 }
Martijn Coenen527924a2017-01-04 12:59:48 +0100610 return mGnssMeasurement;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700611}
612
Martijn Coenen527924a2017-01-04 12:59:48 +0100613Return<sp<IGnssNavigationMessage>> Gnss::getExtensionGnssNavigationMessage() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700614 if (mGnssIface == nullptr) {
615 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800616 return nullptr;
617 }
618
619 if (mGnssNavigationMessage == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700620 const GpsNavigationMessageInterface* gpsNavigationMessageIface =
621 static_cast<const GpsNavigationMessageInterface*>(
622 mGnssIface->get_extension(GPS_NAVIGATION_MESSAGE_INTERFACE));
623
624 if (gpsNavigationMessageIface == nullptr) {
625 ALOGE("%s GnssNavigationMessage interface not implemented by GNSS HAL",
626 __func__);
627 } else {
628 mGnssNavigationMessage = new GnssNavigationMessage(gpsNavigationMessageIface);
629 }
630 }
631
Martijn Coenen527924a2017-01-04 12:59:48 +0100632 return mGnssNavigationMessage;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700633}
634
Martijn Coenen527924a2017-01-04 12:59:48 +0100635Return<sp<IGnssXtra>> Gnss::getExtensionXtra() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700636 if (mGnssIface == nullptr) {
637 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800638 return nullptr;
639 }
640
641 if (mGnssXtraIface == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700642 const GpsXtraInterface* gpsXtraIface = static_cast<const GpsXtraInterface*>(
643 mGnssIface->get_extension(GPS_XTRA_INTERFACE));
644
645 if (gpsXtraIface == nullptr) {
646 ALOGE("%s GnssXtra interface not implemented by HAL", __func__);
647 } else {
648 mGnssXtraIface = new GnssXtra(gpsXtraIface);
649 }
650 }
651
Martijn Coenen527924a2017-01-04 12:59:48 +0100652 return mGnssXtraIface;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700653}
654
Martijn Coenen527924a2017-01-04 12:59:48 +0100655Return<sp<IGnssDebug>> Gnss::getExtensionGnssDebug() {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700656 if (mGnssIface == nullptr) {
657 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800658 return nullptr;
659 }
660
661 if (mGnssDebug == nullptr) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700662 const GpsDebugInterface* gpsDebugIface = static_cast<const GpsDebugInterface*>(
663 mGnssIface->get_extension(GPS_DEBUG_INTERFACE));
664
665 if (gpsDebugIface == nullptr) {
666 ALOGE("%s: GnssDebug interface is not implemented by HAL", __func__);
667 } else {
668 mGnssDebug = new GnssDebug(gpsDebugIface);
669 }
670 }
671
Martijn Coenen527924a2017-01-04 12:59:48 +0100672 return mGnssDebug;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700673}
674
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800675Return<sp<IGnssBatching>> Gnss::getExtensionGnssBatching() {
676 if (mGnssIface == nullptr) {
677 ALOGE("%s: Gnss interface is unavailable", __func__);
Hridya Valsarajue020ce22017-02-15 20:31:18 -0800678 return nullptr;
679 }
680
681 if (mGnssBatching == nullptr) {
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800682 hw_module_t* module;
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800683 const FlpLocationInterface* flpLocationIface = nullptr;
Wyatt Riley8791e7b2017-01-17 12:12:25 -0800684 int err = hw_get_module(FUSED_LOCATION_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
685
686 if (err != 0) {
687 ALOGE("gnss flp hw_get_module failed: %d", err);
688 } else if (module == nullptr) {
689 ALOGE("Fused Location hw_get_module returned null module");
690 } else if (module->methods == nullptr) {
691 ALOGE("Fused Location hw_get_module returned null methods");
692 } else {
693 hw_device_t* device;
694 err = module->methods->open(module, FUSED_LOCATION_HARDWARE_MODULE_ID, &device);
695 if (err != 0) {
696 ALOGE("flpDevice open failed: %d", err);
697 } else {
698 flp_device_t * flpDevice = reinterpret_cast<flp_device_t*>(device);
699 flpLocationIface = flpDevice->get_flp_interface(flpDevice);
700 }
701 }
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800702
703 if (flpLocationIface == nullptr) {
704 ALOGE("%s: GnssBatching interface is not implemented by HAL", __func__);
705 } else {
706 mGnssBatching = new GnssBatching(flpLocationIface);
707 }
708 }
Wyatt Rileyad03ab22016-12-14 14:54:29 -0800709 return mGnssBatching;
710}
711
Wyatt Riley917640b2017-03-16 16:25:55 -0700712void Gnss::handleHidlDeath() {
713 ALOGW("GNSS service noticed HIDL death. Stopping all GNSS operations.");
714
715 // commands down to the HAL implementation
716 stop(); // stop ongoing GPS tracking
717 if (mGnssMeasurement != nullptr) {
718 mGnssMeasurement->close();
719 }
720 if (mGnssNavigationMessage != nullptr) {
721 mGnssNavigationMessage->close();
722 }
723 if (mGnssBatching != nullptr) {
724 mGnssBatching->stop();
725 mGnssBatching->cleanup();
726 }
727 cleanup();
728
729 /*
730 * This has died, so close it off in case (race condition) callbacks happen
731 * before HAL processes above messages.
732 */
733 sGnssCbIface = nullptr;
734}
735
Chris Phoenixab5c1e12017-01-23 15:14:08 -0800736IGnss* HIDL_FETCH_IGnss(const char* /* hal */) {
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700737 hw_module_t* module;
738 IGnss* iface = nullptr;
739 int err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
740
741 if (err == 0) {
742 hw_device_t* device;
743 err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
744 if (err == 0) {
745 iface = new Gnss(reinterpret_cast<gps_device_t*>(device));
746 } else {
Chris Phoenixab5c1e12017-01-23 15:14:08 -0800747 ALOGE("gnssDevice open %s failed: %d", GPS_HARDWARE_MODULE_ID, err);
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700748 }
749 } else {
Chris Phoenixab5c1e12017-01-23 15:14:08 -0800750 ALOGE("gnss hw_get_module %s failed: %d", GPS_HARDWARE_MODULE_ID, err);
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700751 }
752 return iface;
753}
754
755} // namespace implementation
756} // namespace V1_0
757} // namespace gnss
758} // namespace hardware
759} // namespace android