blob: dc23db316308ab8293f7e030ad6d192ebbb05226 [file] [log] [blame]
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -08001/*
2 * Copyright (C) 2018 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 */
Yu-Han Yang030d0332018-12-09 10:47:42 -080016#define LOG_TAG "GnssMeasurement"
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -080017
18#include "GnssMeasurement.h"
Yu-Han Yang030d0332018-12-09 10:47:42 -080019#include <log/log.h>
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -080020
21namespace android {
22namespace hardware {
23namespace gnss {
24namespace V2_0 {
25namespace implementation {
26
Yu-Han Yang030d0332018-12-09 10:47:42 -080027using GnssConstellationType = V1_0::GnssConstellationType;
28using GnssMeasurementFlags = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags;
29using GnssMeasurementState = V1_0::IGnssMeasurementCallback::GnssMeasurementState;
30
31sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr;
32
33GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {}
34
35GnssMeasurement::~GnssMeasurement() {
36 stop();
37}
38
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -080039// Methods from V1_0::IGnssMeasurement follow.
40Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
41 const sp<V1_0::IGnssMeasurementCallback>&) {
42 // TODO implement
43 return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
44}
45
46Return<void> GnssMeasurement::close() {
Yu-Han Yang030d0332018-12-09 10:47:42 -080047 std::unique_lock<std::mutex> lock(mMutex);
48 stop();
49 sCallback = nullptr;
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -080050 return Void();
51}
52
53// Methods from V1_1::IGnssMeasurement follow.
54Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
55 const sp<V1_1::IGnssMeasurementCallback>&, bool) {
56 // TODO implement
57 return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
58}
59
60// Methods from V2_0::IGnssMeasurement follow.
61Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
Yu-Han Yang030d0332018-12-09 10:47:42 -080062 const sp<V2_0::IGnssMeasurementCallback>& callback, bool) {
63 std::unique_lock<std::mutex> lock(mMutex);
64 sCallback = callback;
65
66 if (mIsActive) {
67 ALOGW("GnssMeasurement callback already set. Resetting the callback...");
68 stop();
69 }
70 start();
71
72 return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -080073}
74
Yu-Han Yang030d0332018-12-09 10:47:42 -080075void GnssMeasurement::start() {
76 mIsActive = true;
77 mThread = std::thread([this]() {
78 while (mIsActive == true) {
79 auto measurement = this->getMockMeasurement();
80 this->reportMeasurement(measurement);
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -080081
Yu-Han Yang030d0332018-12-09 10:47:42 -080082 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
83 }
84 });
85}
86
87void GnssMeasurement::stop() {
88 mIsActive = false;
89 if (mThread.joinable()) {
90 mThread.join();
91 }
92}
93
94GnssData GnssMeasurement::getMockMeasurement() {
95 V1_0::IGnssMeasurementCallback::GnssMeasurement measurement_1_0 = {
96 .flags = (uint32_t)GnssMeasurementFlags::HAS_CARRIER_FREQUENCY,
97 .svid = (int16_t)6,
98 .constellation = GnssConstellationType::GLONASS,
99 .timeOffsetNs = 0.0,
100 .state = GnssMeasurementState::STATE_CODE_LOCK | GnssMeasurementState::STATE_BIT_SYNC |
101 GnssMeasurementState::STATE_SUBFRAME_SYNC |
102 GnssMeasurementState::STATE_TOW_DECODED |
103 GnssMeasurementState::STATE_GLO_STRING_SYNC |
104 GnssMeasurementState::STATE_GLO_TOD_DECODED,
105 .receivedSvTimeInNs = 8195997131077,
106 .receivedSvTimeUncertaintyInNs = 15,
107 .cN0DbHz = 30.0,
108 .pseudorangeRateMps = -484.13739013671875,
109 .pseudorangeRateUncertaintyMps = 1.0379999876022339,
110 .accumulatedDeltaRangeState = (uint32_t)
111 V1_0::IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_UNKNOWN,
112 .accumulatedDeltaRangeM = 0.0,
113 .accumulatedDeltaRangeUncertaintyM = 0.0,
114 .carrierFrequencyHz = 1.59975e+09,
115 .multipathIndicator =
116 V1_0::IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN};
117 V1_1::IGnssMeasurementCallback::GnssMeasurement measurement_1_1 = {.v1_0 = measurement_1_0};
118 V2_0::IGnssMeasurementCallback::GnssMeasurement measurement_2_0 = {
119 .v1_1 = measurement_1_1,
120 .codeType = IGnssMeasurementCallback::GnssMeasurementCodeType::CODE_TYPE_C};
121
122 hidl_vec<IGnssMeasurementCallback::GnssMeasurement> measurements(1);
123 measurements[0] = measurement_2_0;
124 V1_0::IGnssMeasurementCallback::GnssClock clock = {.timeNs = 2713545000000,
125 .fullBiasNs = -1226701900521857520,
126 .biasNs = 0.59689998626708984,
127 .biasUncertaintyNs = 47514.989972114563,
128 .driftNsps = -51.757811607455452,
129 .driftUncertaintyNsps = 310.64968328491528,
130 .hwClockDiscontinuityCount = 1};
131 GnssData gnssData = {.measurements = measurements, .clock = clock};
132 return gnssData;
133}
134
135void GnssMeasurement::reportMeasurement(const GnssData& data) {
136 ALOGD("reportMeasurement()");
137 std::unique_lock<std::mutex> lock(mMutex);
138 if (sCallback == nullptr) {
139 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
140 return;
141 }
142 sCallback->gnssMeasurementCb_2_0(data);
143}
144
Yu-Han Yang9c6c20b2018-11-06 14:12:49 -0800145} // namespace implementation
146} // namespace V2_0
147} // namespace gnss
148} // namespace hardware
149} // namespace android