blob: 66be37eb3b4ebb768c282e015adcb4111fb4c275 [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;
31
32GpsCallbacks Gnss::sGnssCb = {
33 .size = sizeof(GpsCallbacks),
34 .location_cb = locationCb,
35 .status_cb = statusCb,
36 .sv_status_cb = gpsSvStatusCb,
37 .nmea_cb = nmeaCb,
38 .set_capabilities_cb = setCapabilitiesCb,
39 .acquire_wakelock_cb = acquireWakelockCb,
40 .release_wakelock_cb = releaseWakelockCb,
41 .create_thread_cb = createThreadCb,
42 .request_utc_time_cb = requestUtcTimeCb,
43 .set_system_info_cb = setSystemInfoCb,
44 .gnss_sv_status_cb = gnssSvStatusCb,
45};
46
47Gnss::Gnss(gps_device_t* gnssDevice) {
48 /* Error out if an instance of the interface already exists. */
49 LOG_ALWAYS_FATAL_IF(sInterfaceExists);
50 sInterfaceExists = true;
51
52 if (gnssDevice == nullptr) {
53 ALOGE("%s: Invalid device_t handle", __func__);
54 return;
55 }
56
57 mGnssIface = gnssDevice->get_gps_interface(gnssDevice);
58}
59
60Gnss::~Gnss() {
61 sThreadFuncArgsList.clear();
62}
63
64void Gnss::locationCb(GpsLocation* location) {
65 if (sGnssCbIface == nullptr) {
66 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
67 return;
68 }
69
70 if (location == nullptr) {
71 ALOGE("%s: Invalid location from GNSS HAL", __func__);
72 return;
73 }
74
75 android::hardware::gnss::V1_0::GnssLocation gnssLocation = convertToGnssLocation(location);
76 sGnssCbIface->gnssLocationCb(gnssLocation);
77}
78
79void Gnss::statusCb(GpsStatus* gnssStatus) {
80 if (sGnssCbIface == nullptr) {
81 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
82 return;
83 }
84
85 if (gnssStatus == nullptr) {
86 ALOGE("%s: Invalid GpsStatus from GNSS HAL", __func__);
87 return;
88 }
89
90 IGnssCallback::GnssStatusValue status =
91 static_cast<IGnssCallback::GnssStatusValue>(gnssStatus->status);
92
93 sGnssCbIface->gnssStatusCb(status);
94}
95
96void Gnss::gnssSvStatusCb(GnssSvStatus* status) {
97 if (sGnssCbIface == nullptr) {
98 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
99 return;
100 }
101
102 if (status == nullptr) {
103 ALOGE("Invalid status from GNSS HAL %s", __func__);
104 return;
105 }
106
107 IGnssCallback::GnssSvStatus svStatus;
108 svStatus.numSvs = status->num_svs;
109
110 if (svStatus.numSvs > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
111 ALOGW("Too many satellites %zd. Clamps to %d.", svStatus.numSvs, GnssMax::SVS_COUNT);
112 svStatus.numSvs = static_cast<uint32_t>(GnssMax::SVS_COUNT);
113 }
114
115 for (size_t i = 0; i < svStatus.numSvs; i++) {
116 auto svInfo = status->gnss_sv_list[i];
117 IGnssCallback::GnssSvInfo gnssSvInfo = {
118 .svid = svInfo.svid,
119 .constellation = static_cast<android::hardware::gnss::V1_0::GnssConstellationType>(
120 svInfo.constellation),
121 .cN0Dbhz = svInfo.c_n0_dbhz,
122 .elevationDegrees = svInfo.elevation,
123 .azimuthDegrees = svInfo.azimuth,
Yifan Hong35e2aac2016-12-05 12:35:05 -0800124 .svFlag = svInfo.flags
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700125 };
126 svStatus.gnssSvList[i] = gnssSvInfo;
127 }
128
129 sGnssCbIface->gnssSvStatusCb(svStatus);
130}
131
132/*
133 * This enum is used by gpsSvStatusCb() method below to convert GpsSvStatus
134 * to GnssSvStatus for backward compatibility. It is only used by the default
135 * implementation and is not part of the GNSS interface.
136 */
137enum SvidValues : uint16_t {
138 GLONASS_SVID_OFFSET = 64,
139 GLONASS_SVID_COUNT = 24,
140 BEIDOU_SVID_OFFSET = 200,
141 BEIDOU_SVID_COUNT = 35,
142 SBAS_SVID_MIN = 33,
143 SBAS_SVID_MAX = 64,
144 SBAS_SVID_ADD = 87,
145 QZSS_SVID_MIN = 193,
146 QZSS_SVID_MAX = 200
147};
148
149/*
150 * The following code that converts GpsSvStatus to GnssSvStatus is moved here from
151 * GnssLocationProvider. GnssLocationProvider does not require it anymore since GpsSvStatus is
152 * being deprecated and is no longer part of the GNSS interface.
153 */
154void Gnss::gpsSvStatusCb(GpsSvStatus* svInfo) {
155 if (sGnssCbIface == nullptr) {
156 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
157 return;
158 }
159
160 if (svInfo == nullptr) {
161 ALOGE("Invalid status from GNSS HAL %s", __func__);
162 return;
163 }
164
165 IGnssCallback::GnssSvStatus svStatus;
166 svStatus.numSvs = svInfo->num_svs;
167 /*
168 * Clamp the list size since GnssSvStatus can support a maximum of
169 * GnssMax::SVS_COUNT entries.
170 */
171 if (svStatus.numSvs > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
172 ALOGW("Too many satellites %zd. Clamps to %d.", svStatus.numSvs, GnssMax::SVS_COUNT);
173 svStatus.numSvs = static_cast<uint32_t>(GnssMax::SVS_COUNT);
174 }
175
176 uint32_t ephemerisMask = svInfo->ephemeris_mask;
177 uint32_t almanacMask = svInfo->almanac_mask;
178 uint32_t usedInFixMask = svInfo->used_in_fix_mask;
179 /*
180 * Conversion from GpsSvInfo to IGnssCallback::GnssSvInfo happens below.
181 */
182 for (size_t i = 0; i < svStatus.numSvs; i++) {
183 IGnssCallback::GnssSvInfo& info = svStatus.gnssSvList[i];
184 info.svid = svInfo->sv_list[i].prn;
185 if (info.svid >= 1 && info.svid <= 32) {
186 info.constellation = GnssConstellationType::GPS;
187 } else if (info.svid > GLONASS_SVID_OFFSET &&
188 info.svid <= GLONASS_SVID_OFFSET + GLONASS_SVID_COUNT) {
189 info.constellation = GnssConstellationType::GLONASS;
190 info.svid -= GLONASS_SVID_OFFSET;
191 } else if (info.svid > BEIDOU_SVID_OFFSET &&
192 info.svid <= BEIDOU_SVID_OFFSET + BEIDOU_SVID_COUNT) {
193 info.constellation = GnssConstellationType::BEIDOU;
194 info.svid -= BEIDOU_SVID_OFFSET;
195 } else if (info.svid >= SBAS_SVID_MIN && info.svid <= SBAS_SVID_MAX) {
196 info.constellation = GnssConstellationType::SBAS;
197 info.svid += SBAS_SVID_ADD;
198 } else if (info.svid >= QZSS_SVID_MIN && info.svid <= QZSS_SVID_MAX) {
199 info.constellation = GnssConstellationType::QZSS;
200 } else {
201 ALOGD("Unknown constellation type with Svid = %d.", info.svid);
202 info.constellation = GnssConstellationType::UNKNOWN;
203 }
204
205 info.cN0Dbhz = svInfo->sv_list[i].snr;
206 info.elevationDegrees = svInfo->sv_list[i].elevation;
207 info.azimuthDegrees = svInfo->sv_list[i].azimuth;
Yifan Hong35e2aac2016-12-05 12:35:05 -0800208 // TODO: b/31702236
209 info.svFlag = static_cast<uint8_t>(IGnssCallback::GnssSvFlags::NONE);
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700210
211 /*
212 * Only GPS info is valid for these fields, as these masks are just 32
213 * bits, by GPS prn.
214 */
215 if (info.constellation == GnssConstellationType::GPS) {
216 int32_t svidMask = (1 << (info.svid - 1));
217 if ((ephemerisMask & svidMask) != 0) {
218 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
219 }
220 if ((almanacMask & svidMask) != 0) {
221 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
222 }
223 if ((usedInFixMask & svidMask) != 0) {
224 info.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
225 }
226 }
227 }
228
229 sGnssCbIface->gnssSvStatusCb(svStatus);
230}
231
232void Gnss::nmeaCb(GpsUtcTime timestamp, const char* nmea, int length) {
233 if (sGnssCbIface == nullptr) {
234 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
235 return;
236 }
237
238 android::hardware::hidl_string nmeaString;
239 nmeaString.setToExternal(nmea, length);
240 sGnssCbIface->gnssNmeaCb(timestamp, nmeaString);
241}
242
243void Gnss::setCapabilitiesCb(uint32_t capabilities) {
244 if (sGnssCbIface == nullptr) {
245 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
246 return;
247 }
248
249 sGnssCbIface->gnssSetCapabilitesCb(capabilities);
250}
251
252void Gnss::acquireWakelockCb() {
253 if (sGnssCbIface == nullptr) {
254 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
255 return;
256 }
257
258 sGnssCbIface->gnssAcquireWakelockCb();
259}
260
261void Gnss::releaseWakelockCb() {
262 if (sGnssCbIface == nullptr) {
263 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
264 return;
265 }
266
267 sGnssCbIface->gnssReleaseWakelockCb();
268}
269
270void Gnss::requestUtcTimeCb() {
271 if (sGnssCbIface == nullptr) {
272 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
273 return;
274 }
275
276 sGnssCbIface->gnssRequestTimeCb();
277}
278
279pthread_t Gnss::createThreadCb(const char* name, void (*start)(void*), void* arg) {
280 return createPthread(name, start, arg, &sThreadFuncArgsList);
281}
282
283void Gnss::setSystemInfoCb(const LegacyGnssSystemInfo* info) {
284 if (sGnssCbIface == nullptr) {
285 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
286 return;
287 }
288
289 if (info == nullptr) {
290 ALOGE("Invalid GnssSystemInfo from GNSS HAL %s", __func__);
291 return;
292 }
293
294 IGnssCallback::GnssSystemInfo gnssInfo = {
295 .yearOfHw = info->year_of_hw
296 };
297
298 sGnssCbIface->gnssSetSystemInfoCb(gnssInfo);
299}
300
301
302// Methods from ::android::hardware::gnss::V1_0::IGnss follow.
303Return<bool> Gnss::setCallback(const sp<IGnssCallback>& callback) {
304 if (mGnssIface == nullptr) {
305 ALOGE("%s: Gnss interface is unavailable", __func__);
306 return false;
307 }
308
309 sGnssCbIface = callback;
310
311 return (mGnssIface->init(&sGnssCb) == 0);
312}
313
314Return<bool> Gnss::start() {
315 if (mGnssIface == nullptr) {
316 ALOGE("%s: Gnss interface is unavailable", __func__);
317 return false;
318 }
319
320 return (mGnssIface->start() == 0);
321}
322
323Return<bool> Gnss::stop() {
324 if (mGnssIface == nullptr) {
325 ALOGE("%s: Gnss interface is unavailable", __func__);
326 return false;
327 }
328
329 return (mGnssIface->stop() == 0);
330}
331
332Return<void> Gnss::cleanup() {
333 if (mGnssIface == nullptr) {
334 ALOGE("%s: Gnss interface is unavailable", __func__);
335 } else {
336 mGnssIface->cleanup();
337 }
338 return Void();
339}
340
341Return<bool> Gnss::injectLocation(double latitudeDegrees,
342 double longitudeDegrees,
343 float accuracyMeters) {
344 if (mGnssIface == nullptr) {
345 ALOGE("%s: Gnss interface is unavailable", __func__);
346 return false;
347 }
348
349 return (mGnssIface->inject_location(latitudeDegrees, longitudeDegrees, accuracyMeters) == 0);
350}
351
352Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
353 int32_t uncertaintyMs) {
354 if (mGnssIface == nullptr) {
355 ALOGE("%s: Gnss interface is unavailable", __func__);
356 return false;
357 }
358
359 return (mGnssIface->inject_time(timeMs, timeReferenceMs, uncertaintyMs) == 0);
360}
361
362Return<void> Gnss::deleteAidingData(IGnss::GnssAidingData aidingDataFlags) {
363 if (mGnssIface == nullptr) {
364 ALOGE("%s: Gnss interface is unavailable", __func__);
365 } else {
366 mGnssIface->delete_aiding_data(static_cast<GpsAidingData>(aidingDataFlags));
367 }
368 return Void();
369}
370
371Return<bool> Gnss::setPositionMode(IGnss::GnssPositionMode mode,
372 IGnss::GnssPositionRecurrence recurrence,
373 uint32_t minIntervalMs,
374 uint32_t preferredAccuracyMeters,
375 uint32_t preferredTimeMs) {
376 if (mGnssIface == nullptr) {
377 ALOGE("%s: Gnss interface is unavailable", __func__);
378 return false;
379 }
380
381 return (mGnssIface->set_position_mode(static_cast<GpsPositionMode>(mode),
382 static_cast<GpsPositionRecurrence>(recurrence),
383 minIntervalMs,
384 preferredAccuracyMeters,
385 preferredTimeMs) == 0);
386}
387
388Return<void> Gnss::getExtensionAGnssRil(getExtensionAGnssRil_cb _hidl_cb) {
389 if (mGnssIface == nullptr) {
390 ALOGE("%s: Gnss interface is unavailable", __func__);
391 } else {
392 const AGpsRilInterface* agpsRilIface = static_cast<const AGpsRilInterface*>(
393 mGnssIface->get_extension(AGPS_RIL_INTERFACE));
394 if (agpsRilIface == nullptr) {
395 ALOGE("%s GnssRil interface not implemented by GNSS HAL", __func__);
396 } else {
397 mGnssRil = new AGnssRil(agpsRilIface);
398 }
399 }
400 _hidl_cb(mGnssRil);
401 return Void();
402}
403
404Return<void> Gnss::getExtensionGnssConfiguration(getExtensionGnssConfiguration_cb _hidl_cb) {
405 if (mGnssIface == nullptr) {
406 ALOGE("%s: Gnss interface is unavailable", __func__);
407 } else {
408 const GnssConfigurationInterface* gnssConfigIface =
409 static_cast<const GnssConfigurationInterface*>(
410 mGnssIface->get_extension(GNSS_CONFIGURATION_INTERFACE));
411
412 if (gnssConfigIface == nullptr) {
413 ALOGE("%s GnssConfiguration interface not implemented by GNSS HAL", __func__);
414 } else {
415 mGnssConfig = new GnssConfiguration(gnssConfigIface);
416 }
417 }
418 _hidl_cb(mGnssConfig);
419 return Void();
420}
421Return<void> Gnss::getExtensionGnssGeofencing(getExtensionGnssGeofencing_cb _hidl_cb) {
422 if (mGnssIface == nullptr) {
423 ALOGE("%s: Gnss interface is unavailable", __func__);
424 } else {
425 const GpsGeofencingInterface* gpsGeofencingIface =
426 static_cast<const GpsGeofencingInterface*>(
427 mGnssIface->get_extension(GPS_GEOFENCING_INTERFACE));
428
429 if (gpsGeofencingIface == nullptr) {
430 ALOGE("%s GnssGeofencing interface not implemented by GNSS HAL", __func__);
431 } else {
432 mGnssGeofencingIface = new GnssGeofencing(gpsGeofencingIface);
433 }
434 }
435
436 _hidl_cb(mGnssGeofencingIface);
437 return Void();
438}
439
440Return<void> Gnss::getExtensionAGnss(getExtensionAGnss_cb _hidl_cb) {
441 if (mGnssIface == nullptr) {
442 ALOGE("%s: Gnss interface is unavailable", __func__);
443 } else {
444 const AGpsInterface* agpsIface = static_cast<const AGpsInterface*>(
445 mGnssIface->get_extension(AGPS_INTERFACE));
446 if (agpsIface == nullptr) {
447 ALOGE("%s AGnss interface not implemented by GNSS HAL", __func__);
448 } else {
449 mAGnssIface = new AGnss(agpsIface);
450 }
451 }
452 _hidl_cb(mAGnssIface);
453 return Void();
454}
455
456Return<void> Gnss::getExtensionGnssNi(getExtensionGnssNi_cb _hidl_cb) {
457 if (mGnssIface == nullptr) {
458 ALOGE("%s: Gnss interface is unavailable", __func__);
459 } else {
460 const GpsNiInterface* gpsNiIface = static_cast<const GpsNiInterface*>(
461 mGnssIface->get_extension(GPS_NI_INTERFACE));
462 if (gpsNiIface == nullptr) {
463 ALOGE("%s GnssNi interface not implemented by GNSS HAL", __func__);
464 } else {
465 mGnssNi = new GnssNi(gpsNiIface);
466 }
467 }
468 _hidl_cb(mGnssNi);
469 return Void();
470}
471
472Return<void> Gnss::getExtensionGnssMeasurement(getExtensionGnssMeasurement_cb _hidl_cb) {
473 if (mGnssIface == nullptr) {
474 ALOGE("%s: Gnss interface is unavailable", __func__);
475 } else {
476 const GpsMeasurementInterface* gpsMeasurementIface =
477 static_cast<const GpsMeasurementInterface*>(
478 mGnssIface->get_extension(GPS_MEASUREMENT_INTERFACE));
479
480 if (gpsMeasurementIface == nullptr) {
481 ALOGE("%s GnssMeasurement interface not implemented by GNSS HAL", __func__);
482 } else {
483 mGnssMeasurement = new GnssMeasurement(gpsMeasurementIface);
484 }
485 }
486 _hidl_cb(mGnssMeasurement);
487 return Void();
488}
489
490Return<void> Gnss::getExtensionGnssNavigationMessage(
491 getExtensionGnssNavigationMessage_cb _hidl_cb) {
492 if (mGnssIface == nullptr) {
493 ALOGE("%s: Gnss interface is unavailable", __func__);
494 } else {
495 const GpsNavigationMessageInterface* gpsNavigationMessageIface =
496 static_cast<const GpsNavigationMessageInterface*>(
497 mGnssIface->get_extension(GPS_NAVIGATION_MESSAGE_INTERFACE));
498
499 if (gpsNavigationMessageIface == nullptr) {
500 ALOGE("%s GnssNavigationMessage interface not implemented by GNSS HAL",
501 __func__);
502 } else {
503 mGnssNavigationMessage = new GnssNavigationMessage(gpsNavigationMessageIface);
504 }
505 }
506
507 _hidl_cb(mGnssNavigationMessage);
508 return Void();
509}
510
511Return<void> Gnss::getExtensionXtra(getExtensionXtra_cb _hidl_cb) {
512 if (mGnssIface == nullptr) {
513 ALOGE("%s: Gnss interface is unavailable", __func__);
514 } else {
515 const GpsXtraInterface* gpsXtraIface = static_cast<const GpsXtraInterface*>(
516 mGnssIface->get_extension(GPS_XTRA_INTERFACE));
517
518 if (gpsXtraIface == nullptr) {
519 ALOGE("%s GnssXtra interface not implemented by HAL", __func__);
520 } else {
521 mGnssXtraIface = new GnssXtra(gpsXtraIface);
522 }
523 }
524
525 _hidl_cb(mGnssXtraIface);
526 return Void();
527}
528
529Return<void> Gnss::getExtensionGnssDebug(getExtensionGnssDebug_cb _hidl_cb) {
530 if (mGnssIface == nullptr) {
531 ALOGE("%s: Gnss interface is unavailable", __func__);
532 } else {
533 const GpsDebugInterface* gpsDebugIface = static_cast<const GpsDebugInterface*>(
534 mGnssIface->get_extension(GPS_DEBUG_INTERFACE));
535
536 if (gpsDebugIface == nullptr) {
537 ALOGE("%s: GnssDebug interface is not implemented by HAL", __func__);
538 } else {
539 mGnssDebug = new GnssDebug(gpsDebugIface);
540 }
541 }
542
543 _hidl_cb(mGnssDebug);
544 return Void();
545}
546
547IGnss* HIDL_FETCH_IGnss(const char* hal) {
548 hw_module_t* module;
549 IGnss* iface = nullptr;
550 int err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
551
552 if (err == 0) {
553 hw_device_t* device;
554 err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
555 if (err == 0) {
556 iface = new Gnss(reinterpret_cast<gps_device_t*>(device));
557 } else {
558 ALOGE("gnssDevice open %s failed: %d", hal, err);
559 }
560 } else {
561 ALOGE("gnss hw_get_module %s failed: %d", hal, err);
562 }
563 return iface;
564}
565
566} // namespace implementation
567} // namespace V1_0
568} // namespace gnss
569} // namespace hardware
570} // namespace android