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