Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 1 | /* |
| 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 Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 16 | #define LOG_TAG "GnssMeasurement" |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 17 | |
| 18 | #include "GnssMeasurement.h" |
Pierre Fite-Georgel | 12ac2b5 | 2019-01-17 16:56:17 -0800 | [diff] [blame] | 19 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 20 | #include <log/log.h> |
Pierre Fite-Georgel | 12ac2b5 | 2019-01-17 16:56:17 -0800 | [diff] [blame] | 21 | #include <utils/SystemClock.h> |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace gnss { |
| 26 | namespace V2_0 { |
| 27 | namespace implementation { |
| 28 | |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 29 | using GnssConstellationType = V2_0::GnssConstellationType; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 30 | using GnssMeasurementFlags = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags; |
Yu-Han Yang | 031738b | 2019-02-01 19:58:04 -0800 | [diff] [blame] | 31 | using GnssMeasurementState = V2_0::IGnssMeasurementCallback::GnssMeasurementState; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 32 | |
| 33 | sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr; |
| 34 | |
| 35 | GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {} |
| 36 | |
| 37 | GnssMeasurement::~GnssMeasurement() { |
| 38 | stop(); |
| 39 | } |
| 40 | |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 41 | // Methods from V1_0::IGnssMeasurement follow. |
| 42 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 43 | const sp<V1_0::IGnssMeasurementCallback>&) { |
| 44 | // TODO implement |
| 45 | return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; |
| 46 | } |
| 47 | |
| 48 | Return<void> GnssMeasurement::close() { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 49 | ALOGD("close"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 50 | std::unique_lock<std::mutex> lock(mMutex); |
| 51 | stop(); |
| 52 | sCallback = nullptr; |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 53 | return Void(); |
| 54 | } |
| 55 | |
| 56 | // Methods from V1_1::IGnssMeasurement follow. |
| 57 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1( |
| 58 | const sp<V1_1::IGnssMeasurementCallback>&, bool) { |
| 59 | // TODO implement |
| 60 | return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; |
| 61 | } |
| 62 | |
| 63 | // Methods from V2_0::IGnssMeasurement follow. |
| 64 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0( |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 65 | const sp<V2_0::IGnssMeasurementCallback>& callback, bool) { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 66 | ALOGD("setCallback_2_0"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 67 | std::unique_lock<std::mutex> lock(mMutex); |
| 68 | sCallback = callback; |
| 69 | |
| 70 | if (mIsActive) { |
| 71 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 72 | stop(); |
| 73 | } |
| 74 | start(); |
| 75 | |
| 76 | return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS; |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 79 | void GnssMeasurement::start() { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 80 | ALOGD("start"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 81 | mIsActive = true; |
| 82 | mThread = std::thread([this]() { |
| 83 | while (mIsActive == true) { |
| 84 | auto measurement = this->getMockMeasurement(); |
| 85 | this->reportMeasurement(measurement); |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 86 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 87 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | void GnssMeasurement::stop() { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 93 | ALOGD("stop"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 94 | mIsActive = false; |
| 95 | if (mThread.joinable()) { |
| 96 | mThread.join(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | GnssData GnssMeasurement::getMockMeasurement() { |
| 101 | V1_0::IGnssMeasurementCallback::GnssMeasurement measurement_1_0 = { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 102 | .flags = (uint32_t)GnssMeasurementFlags::HAS_CARRIER_FREQUENCY, |
| 103 | .svid = (int16_t)6, |
| 104 | .constellation = V1_0::GnssConstellationType::UNKNOWN, |
| 105 | .timeOffsetNs = 0.0, |
| 106 | .receivedSvTimeInNs = 8195997131077, |
| 107 | .receivedSvTimeUncertaintyInNs = 15, |
| 108 | .cN0DbHz = 30.0, |
| 109 | .pseudorangeRateMps = -484.13739013671875, |
| 110 | .pseudorangeRateUncertaintyMps = 1.0379999876022339, |
| 111 | .accumulatedDeltaRangeState = (uint32_t)V1_0::IGnssMeasurementCallback:: |
| 112 | GnssAccumulatedDeltaRangeState::ADR_STATE_UNKNOWN, |
| 113 | .accumulatedDeltaRangeM = 0.0, |
| 114 | .accumulatedDeltaRangeUncertaintyM = 0.0, |
| 115 | .carrierFrequencyHz = 1.59975e+09, |
| 116 | .multipathIndicator = |
| 117 | V1_0::IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN}; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 118 | V1_1::IGnssMeasurementCallback::GnssMeasurement measurement_1_1 = {.v1_0 = measurement_1_0}; |
Yu-Han Yang | 031738b | 2019-02-01 19:58:04 -0800 | [diff] [blame] | 119 | V2_0::IGnssMeasurementCallback::GnssMeasurement measurement_2_0 = { |
| 120 | .v1_1 = measurement_1_1, |
Yu-Han Yang | 221a85e | 2019-03-11 19:06:10 -0700 | [diff] [blame] | 121 | .codeType = "C", |
Yu-Han Yang | 031738b | 2019-02-01 19:58:04 -0800 | [diff] [blame] | 122 | .state = GnssMeasurementState::STATE_CODE_LOCK | GnssMeasurementState::STATE_BIT_SYNC | |
| 123 | GnssMeasurementState::STATE_SUBFRAME_SYNC | |
| 124 | GnssMeasurementState::STATE_TOW_DECODED | |
| 125 | GnssMeasurementState::STATE_GLO_STRING_SYNC | |
Nick Desaulniers | 477c7e8 | 2019-10-11 13:40:54 -0700 | [diff] [blame] | 126 | GnssMeasurementState::STATE_GLO_TOD_DECODED, |
| 127 | .constellation = GnssConstellationType::GLONASS, |
| 128 | }; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 129 | |
| 130 | hidl_vec<IGnssMeasurementCallback::GnssMeasurement> measurements(1); |
| 131 | measurements[0] = measurement_2_0; |
| 132 | V1_0::IGnssMeasurementCallback::GnssClock clock = {.timeNs = 2713545000000, |
| 133 | .fullBiasNs = -1226701900521857520, |
| 134 | .biasNs = 0.59689998626708984, |
| 135 | .biasUncertaintyNs = 47514.989972114563, |
| 136 | .driftNsps = -51.757811607455452, |
| 137 | .driftUncertaintyNsps = 310.64968328491528, |
| 138 | .hwClockDiscontinuityCount = 1}; |
Pierre Fite-Georgel | 12ac2b5 | 2019-01-17 16:56:17 -0800 | [diff] [blame] | 139 | |
| 140 | ElapsedRealtime timestamp = { |
| 141 | .flags = ElapsedRealtimeFlags::HAS_TIMESTAMP_NS | |
| 142 | ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS, |
| 143 | .timestampNs = static_cast<uint64_t>(::android::elapsedRealtimeNano()), |
| 144 | // This is an hardcoded value indicating a 1ms of uncertainty between the two clocks. |
| 145 | // In an actual implementation provide an estimate of the synchronization uncertainty |
| 146 | // or don't set the field. |
| 147 | .timeUncertaintyNs = 1000000}; |
| 148 | |
| 149 | GnssData gnssData = { |
| 150 | .measurements = measurements, .clock = clock, .elapsedRealtime = timestamp}; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 151 | return gnssData; |
| 152 | } |
| 153 | |
| 154 | void GnssMeasurement::reportMeasurement(const GnssData& data) { |
| 155 | ALOGD("reportMeasurement()"); |
| 156 | std::unique_lock<std::mutex> lock(mMutex); |
| 157 | if (sCallback == nullptr) { |
| 158 | ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); |
| 159 | return; |
| 160 | } |
| 161 | sCallback->gnssMeasurementCb_2_0(data); |
| 162 | } |
| 163 | |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 164 | } // namespace implementation |
| 165 | } // namespace V2_0 |
| 166 | } // namespace gnss |
| 167 | } // namespace hardware |
| 168 | } // namespace android |