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