Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 "GnssMeasIfaceAidl" |
| 18 | |
| 19 | #include "GnssMeasurementInterface.h" |
| 20 | #include <aidl/android/hardware/gnss/BnGnss.h> |
| 21 | #include <log/log.h> |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 22 | #include "DeviceFileReader.h" |
Zhanghao | d32f378 | 2023-03-21 05:41:55 +0000 | [diff] [blame] | 23 | #include "Gnss.h" |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 24 | #include "GnssRawMeasurementParser.h" |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 25 | #include "GnssReplayUtils.h" |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 26 | #include "Utils.h" |
| 27 | |
| 28 | namespace aidl::android::hardware::gnss { |
| 29 | |
| 30 | using Utils = ::android::hardware::gnss::common::Utils; |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 31 | using ReplayUtils = ::android::hardware::gnss::common::ReplayUtils; |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 32 | using GnssRawMeasurementParser = ::android::hardware::gnss::common::GnssRawMeasurementParser; |
| 33 | using DeviceFileReader = ::android::hardware::gnss::common::DeviceFileReader; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 34 | |
| 35 | std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr; |
| 36 | |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 37 | GnssMeasurementInterface::GnssMeasurementInterface() |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 38 | : mIntervalMs(1000), mLocationIntervalMs(1000) { |
| 39 | mThreads.reserve(2); |
| 40 | } |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 41 | |
| 42 | GnssMeasurementInterface::~GnssMeasurementInterface() { |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 43 | waitForStoppingThreads(); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ndk::ScopedAStatus GnssMeasurementInterface::setCallback( |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 47 | const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking, |
| 48 | const bool enableCorrVecOutputs) { |
| 49 | ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking, |
| 50 | (int)enableCorrVecOutputs); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 51 | { |
| 52 | std::unique_lock<std::mutex> lock(mMutex); |
| 53 | sCallback = callback; |
| 54 | } |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 55 | |
| 56 | if (mIsActive) { |
| 57 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 58 | stop(); |
| 59 | } |
Yu-Han Yang | c5d4f36 | 2022-11-04 22:55:32 +0000 | [diff] [blame] | 60 | start(enableCorrVecOutputs, enableFullTracking); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 61 | |
| 62 | return ndk::ScopedAStatus::ok(); |
| 63 | } |
| 64 | |
Yu-Han Yang | 11c0c24 | 2021-12-15 11:22:09 -0800 | [diff] [blame] | 65 | ndk::ScopedAStatus GnssMeasurementInterface::setCallbackWithOptions( |
| 66 | const std::shared_ptr<IGnssMeasurementCallback>& callback, const Options& options) { |
| 67 | ALOGD("setCallbackWithOptions: fullTracking:%d, corrVec:%d, intervalMs:%d", |
| 68 | (int)options.enableFullTracking, (int)options.enableCorrVecOutputs, options.intervalMs); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 69 | { |
| 70 | std::unique_lock<std::mutex> lock(mMutex); |
| 71 | sCallback = callback; |
| 72 | } |
Yu-Han Yang | 11c0c24 | 2021-12-15 11:22:09 -0800 | [diff] [blame] | 73 | |
| 74 | if (mIsActive) { |
| 75 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 76 | stop(); |
| 77 | } |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 78 | mIntervalMs = std::max(options.intervalMs, 1000); |
Yu-Han Yang | bf5189c | 2023-08-30 22:01:50 +0000 | [diff] [blame] | 79 | mGnss->setGnssMeasurementInterval(mIntervalMs); |
Yu-Han Yang | c5d4f36 | 2022-11-04 22:55:32 +0000 | [diff] [blame] | 80 | start(options.enableCorrVecOutputs, options.enableFullTracking); |
Yu-Han Yang | 11c0c24 | 2021-12-15 11:22:09 -0800 | [diff] [blame] | 81 | |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 85 | ndk::ScopedAStatus GnssMeasurementInterface::close() { |
| 86 | ALOGD("close"); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 87 | if (mIsActive) { |
| 88 | stop(); |
| 89 | } |
| 90 | { |
| 91 | std::unique_lock<std::mutex> lock(mMutex); |
| 92 | sCallback = nullptr; |
| 93 | } |
| 94 | mIntervalMs = 1000; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 95 | return ndk::ScopedAStatus::ok(); |
| 96 | } |
| 97 | |
Yu-Han Yang | c5d4f36 | 2022-11-04 22:55:32 +0000 | [diff] [blame] | 98 | void GnssMeasurementInterface::start(const bool enableCorrVecOutputs, |
| 99 | const bool enableFullTracking) { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 100 | ALOGD("start"); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 101 | |
| 102 | if (mIsActive) { |
| 103 | ALOGD("restarting since measurement has started"); |
| 104 | stop(); |
| 105 | } |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 106 | |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 107 | mIsActive = true; |
Yu-Han Yang | bf5189c | 2023-08-30 22:01:50 +0000 | [diff] [blame] | 108 | mGnss->setGnssMeasurementEnabled(true); |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 109 | mThreads.emplace_back(std::thread([this, enableCorrVecOutputs, enableFullTracking]() { |
| 110 | waitForStoppingThreads(); |
| 111 | mThreadBlocker.reset(); |
| 112 | |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 113 | int intervalMs; |
| 114 | do { |
| 115 | if (!mIsActive) { |
| 116 | break; |
| 117 | } |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 118 | std::string rawMeasurementStr = ""; |
| 119 | if (ReplayUtils::hasGnssDeviceFile() && |
| 120 | ReplayUtils::isGnssRawMeasurement( |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 121 | rawMeasurementStr = |
| 122 | DeviceFileReader::Instance().getGnssRawMeasurementData())) { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 123 | ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(), |
| 124 | rawMeasurementStr.c_str()); |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 125 | auto measurement = |
| 126 | GnssRawMeasurementParser::getMeasurementFromStrs(rawMeasurementStr); |
| 127 | if (measurement != nullptr) { |
| 128 | this->reportMeasurement(*measurement); |
| 129 | } |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 130 | } else { |
Yu-Han Yang | c5d4f36 | 2022-11-04 22:55:32 +0000 | [diff] [blame] | 131 | auto measurement = |
| 132 | Utils::getMockMeasurement(enableCorrVecOutputs, enableFullTracking); |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 133 | this->reportMeasurement(measurement); |
Yu-Han Yang | bf5189c | 2023-08-30 22:01:50 +0000 | [diff] [blame] | 134 | if (!mLocationEnabled || mLocationIntervalMs > mIntervalMs) { |
Zhanghao | d32f378 | 2023-03-21 05:41:55 +0000 | [diff] [blame] | 135 | mGnss->reportSvStatus(); |
| 136 | } |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 137 | } |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 138 | intervalMs = |
| 139 | (mLocationEnabled) ? std::min(mLocationIntervalMs, mIntervalMs) : mIntervalMs; |
| 140 | } while (mIsActive && mThreadBlocker.wait_for(std::chrono::milliseconds(intervalMs))); |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 141 | })); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void GnssMeasurementInterface::stop() { |
| 145 | ALOGD("stop"); |
| 146 | mIsActive = false; |
Yu-Han Yang | bf5189c | 2023-08-30 22:01:50 +0000 | [diff] [blame] | 147 | mGnss->setGnssMeasurementEnabled(false); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 148 | mThreadBlocker.notify(); |
Yu-Han Yang | 23e7826 | 2024-06-11 21:59:02 +0000 | [diff] [blame] | 149 | for (auto iter = mThreads.begin(); iter != mThreads.end();) { |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 150 | if (iter->joinable()) { |
Yu-Han Yang | 23e7826 | 2024-06-11 21:59:02 +0000 | [diff] [blame] | 151 | // Store the thread object by value |
| 152 | std::thread threadToMove = std::move(*iter); |
| 153 | |
| 154 | mFutures.push_back(std::async(std::launch::async, |
| 155 | [threadToMove = std::move(threadToMove)]() mutable { |
| 156 | ALOGD("joining thread"); |
| 157 | threadToMove.join(); |
| 158 | })); |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 159 | } |
Yu-Han Yang | 23e7826 | 2024-06-11 21:59:02 +0000 | [diff] [blame] | 160 | iter = mThreads.erase(iter); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 161 | } |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void GnssMeasurementInterface::reportMeasurement(const GnssData& data) { |
| 165 | ALOGD("reportMeasurement()"); |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 166 | std::shared_ptr<IGnssMeasurementCallback> callbackCopy; |
| 167 | { |
| 168 | std::unique_lock<std::mutex> lock(mMutex); |
| 169 | if (sCallback == nullptr) { |
| 170 | ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); |
| 171 | return; |
| 172 | } |
| 173 | callbackCopy = sCallback; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 174 | } |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 175 | callbackCopy->gnssMeasurementCb(data); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 178 | void GnssMeasurementInterface::setLocationInterval(const int intervalMs) { |
| 179 | mLocationIntervalMs = intervalMs; |
| 180 | } |
| 181 | |
| 182 | void GnssMeasurementInterface::setLocationEnabled(const bool enabled) { |
| 183 | mLocationEnabled = enabled; |
| 184 | } |
| 185 | |
Zhanghao | d32f378 | 2023-03-21 05:41:55 +0000 | [diff] [blame] | 186 | void GnssMeasurementInterface::setGnssInterface(const std::shared_ptr<Gnss>& gnss) { |
| 187 | mGnss = gnss; |
| 188 | } |
| 189 | |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 190 | void GnssMeasurementInterface::waitForStoppingThreads() { |
| 191 | for (auto& future : mFutures) { |
| 192 | ALOGD("Stopping previous thread."); |
| 193 | future.wait(); |
| 194 | ALOGD("Done stopping thread."); |
| 195 | } |
| 196 | mFutures.clear(); |
| 197 | } |
| 198 | |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 199 | } // namespace aidl::android::hardware::gnss |