blob: 37810be6d4cadc172fdea3782a1977c4598b0cf2 [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,
124 .svFlag = static_cast<IGnssCallback::GnssSvFlags>(svInfo.flags)
125 };
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;
208 info.svFlag = IGnssCallback::GnssSvFlags::NONE;
209
210 /*
211 * Only GPS info is valid for these fields, as these masks are just 32
212 * bits, by GPS prn.
213 */
214 if (info.constellation == GnssConstellationType::GPS) {
215 int32_t svidMask = (1 << (info.svid - 1));
216 if ((ephemerisMask & svidMask) != 0) {
217 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_EPHEMERIS_DATA;
218 }
219 if ((almanacMask & svidMask) != 0) {
220 info.svFlag |= IGnssCallback::GnssSvFlags::HAS_ALMANAC_DATA;
221 }
222 if ((usedInFixMask & svidMask) != 0) {
223 info.svFlag |= IGnssCallback::GnssSvFlags::USED_IN_FIX;
224 }
225 }
226 }
227
228 sGnssCbIface->gnssSvStatusCb(svStatus);
229}
230
231void Gnss::nmeaCb(GpsUtcTime timestamp, const char* nmea, int length) {
232 if (sGnssCbIface == nullptr) {
233 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
234 return;
235 }
236
237 android::hardware::hidl_string nmeaString;
238 nmeaString.setToExternal(nmea, length);
239 sGnssCbIface->gnssNmeaCb(timestamp, nmeaString);
240}
241
242void Gnss::setCapabilitiesCb(uint32_t capabilities) {
243 if (sGnssCbIface == nullptr) {
244 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
245 return;
246 }
247
248 sGnssCbIface->gnssSetCapabilitesCb(capabilities);
249}
250
251void Gnss::acquireWakelockCb() {
252 if (sGnssCbIface == nullptr) {
253 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
254 return;
255 }
256
257 sGnssCbIface->gnssAcquireWakelockCb();
258}
259
260void Gnss::releaseWakelockCb() {
261 if (sGnssCbIface == nullptr) {
262 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
263 return;
264 }
265
266 sGnssCbIface->gnssReleaseWakelockCb();
267}
268
269void Gnss::requestUtcTimeCb() {
270 if (sGnssCbIface == nullptr) {
271 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
272 return;
273 }
274
275 sGnssCbIface->gnssRequestTimeCb();
276}
277
278pthread_t Gnss::createThreadCb(const char* name, void (*start)(void*), void* arg) {
279 return createPthread(name, start, arg, &sThreadFuncArgsList);
280}
281
282void Gnss::setSystemInfoCb(const LegacyGnssSystemInfo* info) {
283 if (sGnssCbIface == nullptr) {
284 ALOGE("%s: GNSS Callback Interface configured incorrectly", __func__);
285 return;
286 }
287
288 if (info == nullptr) {
289 ALOGE("Invalid GnssSystemInfo from GNSS HAL %s", __func__);
290 return;
291 }
292
293 IGnssCallback::GnssSystemInfo gnssInfo = {
294 .yearOfHw = info->year_of_hw
295 };
296
297 sGnssCbIface->gnssSetSystemInfoCb(gnssInfo);
298}
299
300
301// Methods from ::android::hardware::gnss::V1_0::IGnss follow.
302Return<bool> Gnss::setCallback(const sp<IGnssCallback>& callback) {
303 if (mGnssIface == nullptr) {
304 ALOGE("%s: Gnss interface is unavailable", __func__);
305 return false;
306 }
307
308 sGnssCbIface = callback;
309
310 return (mGnssIface->init(&sGnssCb) == 0);
311}
312
313Return<bool> Gnss::start() {
314 if (mGnssIface == nullptr) {
315 ALOGE("%s: Gnss interface is unavailable", __func__);
316 return false;
317 }
318
319 return (mGnssIface->start() == 0);
320}
321
322Return<bool> Gnss::stop() {
323 if (mGnssIface == nullptr) {
324 ALOGE("%s: Gnss interface is unavailable", __func__);
325 return false;
326 }
327
328 return (mGnssIface->stop() == 0);
329}
330
331Return<void> Gnss::cleanup() {
332 if (mGnssIface == nullptr) {
333 ALOGE("%s: Gnss interface is unavailable", __func__);
334 } else {
335 mGnssIface->cleanup();
336 }
337 return Void();
338}
339
340Return<bool> Gnss::injectLocation(double latitudeDegrees,
341 double longitudeDegrees,
342 float accuracyMeters) {
343 if (mGnssIface == nullptr) {
344 ALOGE("%s: Gnss interface is unavailable", __func__);
345 return false;
346 }
347
348 return (mGnssIface->inject_location(latitudeDegrees, longitudeDegrees, accuracyMeters) == 0);
349}
350
351Return<bool> Gnss::injectTime(int64_t timeMs, int64_t timeReferenceMs,
352 int32_t uncertaintyMs) {
353 if (mGnssIface == nullptr) {
354 ALOGE("%s: Gnss interface is unavailable", __func__);
355 return false;
356 }
357
358 return (mGnssIface->inject_time(timeMs, timeReferenceMs, uncertaintyMs) == 0);
359}
360
361Return<void> Gnss::deleteAidingData(IGnss::GnssAidingData aidingDataFlags) {
362 if (mGnssIface == nullptr) {
363 ALOGE("%s: Gnss interface is unavailable", __func__);
364 } else {
365 mGnssIface->delete_aiding_data(static_cast<GpsAidingData>(aidingDataFlags));
366 }
367 return Void();
368}
369
370Return<bool> Gnss::setPositionMode(IGnss::GnssPositionMode mode,
371 IGnss::GnssPositionRecurrence recurrence,
372 uint32_t minIntervalMs,
373 uint32_t preferredAccuracyMeters,
374 uint32_t preferredTimeMs) {
375 if (mGnssIface == nullptr) {
376 ALOGE("%s: Gnss interface is unavailable", __func__);
377 return false;
378 }
379
380 return (mGnssIface->set_position_mode(static_cast<GpsPositionMode>(mode),
381 static_cast<GpsPositionRecurrence>(recurrence),
382 minIntervalMs,
383 preferredAccuracyMeters,
384 preferredTimeMs) == 0);
385}
386
387Return<void> Gnss::getExtensionAGnssRil(getExtensionAGnssRil_cb _hidl_cb) {
388 if (mGnssIface == nullptr) {
389 ALOGE("%s: Gnss interface is unavailable", __func__);
390 } else {
391 const AGpsRilInterface* agpsRilIface = static_cast<const AGpsRilInterface*>(
392 mGnssIface->get_extension(AGPS_RIL_INTERFACE));
393 if (agpsRilIface == nullptr) {
394 ALOGE("%s GnssRil interface not implemented by GNSS HAL", __func__);
395 } else {
396 mGnssRil = new AGnssRil(agpsRilIface);
397 }
398 }
399 _hidl_cb(mGnssRil);
400 return Void();
401}
402
403Return<void> Gnss::getExtensionGnssConfiguration(getExtensionGnssConfiguration_cb _hidl_cb) {
404 if (mGnssIface == nullptr) {
405 ALOGE("%s: Gnss interface is unavailable", __func__);
406 } else {
407 const GnssConfigurationInterface* gnssConfigIface =
408 static_cast<const GnssConfigurationInterface*>(
409 mGnssIface->get_extension(GNSS_CONFIGURATION_INTERFACE));
410
411 if (gnssConfigIface == nullptr) {
412 ALOGE("%s GnssConfiguration interface not implemented by GNSS HAL", __func__);
413 } else {
414 mGnssConfig = new GnssConfiguration(gnssConfigIface);
415 }
416 }
417 _hidl_cb(mGnssConfig);
418 return Void();
419}
420Return<void> Gnss::getExtensionGnssGeofencing(getExtensionGnssGeofencing_cb _hidl_cb) {
421 if (mGnssIface == nullptr) {
422 ALOGE("%s: Gnss interface is unavailable", __func__);
423 } else {
424 const GpsGeofencingInterface* gpsGeofencingIface =
425 static_cast<const GpsGeofencingInterface*>(
426 mGnssIface->get_extension(GPS_GEOFENCING_INTERFACE));
427
428 if (gpsGeofencingIface == nullptr) {
429 ALOGE("%s GnssGeofencing interface not implemented by GNSS HAL", __func__);
430 } else {
431 mGnssGeofencingIface = new GnssGeofencing(gpsGeofencingIface);
432 }
433 }
434
435 _hidl_cb(mGnssGeofencingIface);
436 return Void();
437}
438
439Return<void> Gnss::getExtensionAGnss(getExtensionAGnss_cb _hidl_cb) {
440 if (mGnssIface == nullptr) {
441 ALOGE("%s: Gnss interface is unavailable", __func__);
442 } else {
443 const AGpsInterface* agpsIface = static_cast<const AGpsInterface*>(
444 mGnssIface->get_extension(AGPS_INTERFACE));
445 if (agpsIface == nullptr) {
446 ALOGE("%s AGnss interface not implemented by GNSS HAL", __func__);
447 } else {
448 mAGnssIface = new AGnss(agpsIface);
449 }
450 }
451 _hidl_cb(mAGnssIface);
452 return Void();
453}
454
455Return<void> Gnss::getExtensionGnssNi(getExtensionGnssNi_cb _hidl_cb) {
456 if (mGnssIface == nullptr) {
457 ALOGE("%s: Gnss interface is unavailable", __func__);
458 } else {
459 const GpsNiInterface* gpsNiIface = static_cast<const GpsNiInterface*>(
460 mGnssIface->get_extension(GPS_NI_INTERFACE));
461 if (gpsNiIface == nullptr) {
462 ALOGE("%s GnssNi interface not implemented by GNSS HAL", __func__);
463 } else {
464 mGnssNi = new GnssNi(gpsNiIface);
465 }
466 }
467 _hidl_cb(mGnssNi);
468 return Void();
469}
470
471Return<void> Gnss::getExtensionGnssMeasurement(getExtensionGnssMeasurement_cb _hidl_cb) {
472 if (mGnssIface == nullptr) {
473 ALOGE("%s: Gnss interface is unavailable", __func__);
474 } else {
475 const GpsMeasurementInterface* gpsMeasurementIface =
476 static_cast<const GpsMeasurementInterface*>(
477 mGnssIface->get_extension(GPS_MEASUREMENT_INTERFACE));
478
479 if (gpsMeasurementIface == nullptr) {
480 ALOGE("%s GnssMeasurement interface not implemented by GNSS HAL", __func__);
481 } else {
482 mGnssMeasurement = new GnssMeasurement(gpsMeasurementIface);
483 }
484 }
485 _hidl_cb(mGnssMeasurement);
486 return Void();
487}
488
489Return<void> Gnss::getExtensionGnssNavigationMessage(
490 getExtensionGnssNavigationMessage_cb _hidl_cb) {
491 if (mGnssIface == nullptr) {
492 ALOGE("%s: Gnss interface is unavailable", __func__);
493 } else {
494 const GpsNavigationMessageInterface* gpsNavigationMessageIface =
495 static_cast<const GpsNavigationMessageInterface*>(
496 mGnssIface->get_extension(GPS_NAVIGATION_MESSAGE_INTERFACE));
497
498 if (gpsNavigationMessageIface == nullptr) {
499 ALOGE("%s GnssNavigationMessage interface not implemented by GNSS HAL",
500 __func__);
501 } else {
502 mGnssNavigationMessage = new GnssNavigationMessage(gpsNavigationMessageIface);
503 }
504 }
505
506 _hidl_cb(mGnssNavigationMessage);
507 return Void();
508}
509
510Return<void> Gnss::getExtensionXtra(getExtensionXtra_cb _hidl_cb) {
511 if (mGnssIface == nullptr) {
512 ALOGE("%s: Gnss interface is unavailable", __func__);
513 } else {
514 const GpsXtraInterface* gpsXtraIface = static_cast<const GpsXtraInterface*>(
515 mGnssIface->get_extension(GPS_XTRA_INTERFACE));
516
517 if (gpsXtraIface == nullptr) {
518 ALOGE("%s GnssXtra interface not implemented by HAL", __func__);
519 } else {
520 mGnssXtraIface = new GnssXtra(gpsXtraIface);
521 }
522 }
523
524 _hidl_cb(mGnssXtraIface);
525 return Void();
526}
527
528Return<void> Gnss::getExtensionGnssDebug(getExtensionGnssDebug_cb _hidl_cb) {
529 if (mGnssIface == nullptr) {
530 ALOGE("%s: Gnss interface is unavailable", __func__);
531 } else {
532 const GpsDebugInterface* gpsDebugIface = static_cast<const GpsDebugInterface*>(
533 mGnssIface->get_extension(GPS_DEBUG_INTERFACE));
534
535 if (gpsDebugIface == nullptr) {
536 ALOGE("%s: GnssDebug interface is not implemented by HAL", __func__);
537 } else {
538 mGnssDebug = new GnssDebug(gpsDebugIface);
539 }
540 }
541
542 _hidl_cb(mGnssDebug);
543 return Void();
544}
545
546IGnss* HIDL_FETCH_IGnss(const char* hal) {
547 hw_module_t* module;
548 IGnss* iface = nullptr;
549 int err = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
550
551 if (err == 0) {
552 hw_device_t* device;
553 err = module->methods->open(module, GPS_HARDWARE_MODULE_ID, &device);
554 if (err == 0) {
555 iface = new Gnss(reinterpret_cast<gps_device_t*>(device));
556 } else {
557 ALOGE("gnssDevice open %s failed: %d", hal, err);
558 }
559 } else {
560 ALOGE("gnss hw_get_module %s failed: %d", hal, err);
561 }
562 return iface;
563}
564
565} // namespace implementation
566} // namespace V1_0
567} // namespace gnss
568} // namespace hardware
569} // namespace android