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