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" |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame^] | 19 | #include <log/log.h> |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace gnss { |
| 24 | namespace V2_0 { |
| 25 | namespace implementation { |
| 26 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame^] | 27 | using GnssConstellationType = V1_0::GnssConstellationType; |
| 28 | using GnssMeasurementFlags = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags; |
| 29 | using GnssMeasurementState = V1_0::IGnssMeasurementCallback::GnssMeasurementState; |
| 30 | |
| 31 | sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr; |
| 32 | |
| 33 | GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {} |
| 34 | |
| 35 | GnssMeasurement::~GnssMeasurement() { |
| 36 | stop(); |
| 37 | } |
| 38 | |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 39 | // Methods from V1_0::IGnssMeasurement follow. |
| 40 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 41 | const sp<V1_0::IGnssMeasurementCallback>&) { |
| 42 | // TODO implement |
| 43 | return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; |
| 44 | } |
| 45 | |
| 46 | Return<void> GnssMeasurement::close() { |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame^] | 47 | std::unique_lock<std::mutex> lock(mMutex); |
| 48 | stop(); |
| 49 | sCallback = nullptr; |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 50 | return Void(); |
| 51 | } |
| 52 | |
| 53 | // Methods from V1_1::IGnssMeasurement follow. |
| 54 | Return<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. |
| 61 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0( |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame^] | 62 | 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 Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame^] | 75 | void 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 Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 81 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame^] | 82 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 83 | } |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | void GnssMeasurement::stop() { |
| 88 | mIsActive = false; |
| 89 | if (mThread.joinable()) { |
| 90 | mThread.join(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | GnssData 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 | |
| 135 | void 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 Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 145 | } // namespace implementation |
| 146 | } // namespace V2_0 |
| 147 | } // namespace gnss |
| 148 | } // namespace hardware |
| 149 | } // namespace android |