1/*
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 "LocSvc_GnssDebugInterface"
18
19#include <log/log.h>
20#include <log_util.h>
21#include "Gnss.h"
22#include "GnssDebug.h"
23#include "LocationUtil.h"
24
25namespace android {
26namespace hardware {
27namespace gnss {
28namespace V1_0 {
29namespace implementation {
30
31using ::android::hardware::hidl_vec;
32
33#define GNSS_DEBUG_UNKNOWN_UTC_TIME     (1483228800000ULL) // 1/1/2017 00:00 GMT
34#define GNSS_DEBUG_UNKNOWN_UTC_TIME_UNC (1.57783680E17) // 5 years in ns
35
36GnssDebug::GnssDebug(Gnss* gnss) : mGnss(gnss)
37{
38}
39
40/*
41 * This methods requests position, time and satellite ephemeris debug information
42 * from the HAL.
43 *
44 * @return void
45*/
46Return<void> GnssDebug::getDebugData(getDebugData_cb _hidl_cb)
47{
48    LOC_LOGD("%s]: ", __func__);
49
50    DebugData data = { };
51
52    if((nullptr == mGnss) || (nullptr == mGnss->getGnssInterface())){
53        LOC_LOGE("GnssDebug - Null GNSS interface");
54        _hidl_cb(data);
55        return Void();
56    }
57
58    // get debug report snapshot via hal interface
59    GnssDebugReport reports = { };
60    mGnss->getGnssInterface()->getDebugReport(reports);
61
62    // location block
63    if (reports.mLocation.mValid) {
64        data.position.valid = true;
65        data.position.latitudeDegrees = reports.mLocation.mLocation.latitude;
66        data.position.longitudeDegrees = reports.mLocation.mLocation.longitude;
67        data.position.altitudeMeters = reports.mLocation.mLocation.altitude;
68
69        data.position.speedMetersPerSec =
70            (double)(reports.mLocation.mLocation.speed);
71        data.position.bearingDegrees =
72            (double)(reports.mLocation.mLocation.bearing);
73        data.position.horizontalAccuracyMeters =
74            (double)(reports.mLocation.mLocation.accuracy);
75        data.position.verticalAccuracyMeters =
76            reports.mLocation.verticalAccuracyMeters;
77        data.position.speedAccuracyMetersPerSecond =
78            reports.mLocation.speedAccuracyMetersPerSecond;
79        data.position.bearingAccuracyDegrees =
80            reports.mLocation.bearingAccuracyDegrees;
81
82        timeval tv_now, tv_report;
83        tv_report.tv_sec  = reports.mLocation.mUtcReported.tv_sec;
84        tv_report.tv_usec = reports.mLocation.mUtcReported.tv_nsec / 1000ULL;
85        gettimeofday(&tv_now, NULL);
86        data.position.ageSeconds =
87            (tv_now.tv_sec - tv_report.tv_sec) +
88            (float)((tv_now.tv_usec - tv_report.tv_usec)) / 1000000;
89    }
90    else {
91        data.position.valid = false;
92    }
93
94    // time block
95    if (reports.mTime.mValid) {
96        data.time.timeEstimate = reports.mTime.timeEstimate;
97        data.time.timeUncertaintyNs = reports.mTime.timeUncertaintyNs;
98        data.time.frequencyUncertaintyNsPerSec =
99            reports.mTime.frequencyUncertaintyNsPerSec;
100    }
101    else {
102        data.time.timeEstimate = GNSS_DEBUG_UNKNOWN_UTC_TIME;
103        data.time.timeUncertaintyNs = (float)(GNSS_DEBUG_UNKNOWN_UTC_TIME_UNC);
104        data.time.frequencyUncertaintyNsPerSec = 0;
105    }
106
107    // satellite data block
108    SatelliteData s = { };
109    std::vector<SatelliteData> s_array = { };
110
111    for (uint32_t i=0; i<reports.mSatelliteInfo.size(); i++) {
112        memset(&s, 0, sizeof(s));
113        s.svid = reports.mSatelliteInfo[i].svid;
114        convertGnssConstellationType(
115            reports.mSatelliteInfo[i].constellation, s.constellation);
116        convertGnssEphemerisType(
117            reports.mSatelliteInfo[i].mEphemerisType, s.ephemerisType);
118        convertGnssEphemerisSource(
119            reports.mSatelliteInfo[i].mEphemerisSource, s.ephemerisSource);
120        convertGnssEphemerisHealth(
121            reports.mSatelliteInfo[i].mEphemerisHealth, s.ephemerisHealth);
122
123        s.ephemerisAgeSeconds =
124            reports.mSatelliteInfo[i].ephemerisAgeSeconds;
125        s.serverPredictionIsAvailable =
126            reports.mSatelliteInfo[i].serverPredictionIsAvailable;
127        s.serverPredictionAgeSeconds =
128            reports.mSatelliteInfo[i].serverPredictionAgeSeconds;
129
130        s_array.push_back(s);
131    }
132    data.satelliteDataArray = s_array;
133
134    // callback HIDL with collected debug data
135    _hidl_cb(data);
136    return Void();
137}
138
139}  // namespace implementation
140}  // namespace V1_0
141}  // namespace gnss
142}  // namespace hardware
143}  // namespace android
144