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 | |
| 36 | GnssMeasurementInterface::GnssMeasurementInterface() : mMinIntervalMillis(1000) {} |
| 37 | |
| 38 | GnssMeasurementInterface::~GnssMeasurementInterface() { |
| 39 | stop(); |
| 40 | } |
| 41 | |
| 42 | ndk::ScopedAStatus GnssMeasurementInterface::setCallback( |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 43 | const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking, |
| 44 | const bool enableCorrVecOutputs) { |
| 45 | ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking, |
| 46 | (int)enableCorrVecOutputs); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 47 | std::unique_lock<std::mutex> lock(mMutex); |
| 48 | sCallback = callback; |
| 49 | |
| 50 | if (mIsActive) { |
| 51 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 52 | stop(); |
| 53 | } |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 54 | start(enableCorrVecOutputs); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 55 | |
| 56 | return ndk::ScopedAStatus::ok(); |
| 57 | } |
| 58 | |
Yu-Han Yang | 11c0c24 | 2021-12-15 11:22:09 -0800 | [diff] [blame] | 59 | ndk::ScopedAStatus GnssMeasurementInterface::setCallbackWithOptions( |
| 60 | const std::shared_ptr<IGnssMeasurementCallback>& callback, const Options& options) { |
| 61 | ALOGD("setCallbackWithOptions: fullTracking:%d, corrVec:%d, intervalMs:%d", |
| 62 | (int)options.enableFullTracking, (int)options.enableCorrVecOutputs, options.intervalMs); |
| 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 | mMinIntervalMillis = options.intervalMs; |
| 71 | start(options.enableCorrVecOutputs); |
| 72 | |
| 73 | return ndk::ScopedAStatus::ok(); |
| 74 | } |
| 75 | |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 76 | ndk::ScopedAStatus GnssMeasurementInterface::close() { |
| 77 | ALOGD("close"); |
| 78 | stop(); |
| 79 | std::unique_lock<std::mutex> lock(mMutex); |
| 80 | sCallback = nullptr; |
Yu-Han Yang | 11c0c24 | 2021-12-15 11:22:09 -0800 | [diff] [blame] | 81 | mMinIntervalMillis = 1000; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 85 | void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 86 | ALOGD("start"); |
| 87 | mIsActive = true; |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 88 | mThread = std::thread([this, enableCorrVecOutputs]() { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 89 | while (mIsActive == true) { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 90 | std::string rawMeasurementStr = ""; |
| 91 | if (ReplayUtils::hasGnssDeviceFile() && |
| 92 | ReplayUtils::isGnssRawMeasurement( |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 93 | rawMeasurementStr = |
| 94 | DeviceFileReader::Instance().getGnssRawMeasurementData())) { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 95 | ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(), |
| 96 | rawMeasurementStr.c_str()); |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 97 | auto measurement = |
| 98 | GnssRawMeasurementParser::getMeasurementFromStrs(rawMeasurementStr); |
| 99 | if (measurement != nullptr) { |
| 100 | this->reportMeasurement(*measurement); |
| 101 | } |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 102 | } else { |
| 103 | auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs); |
| 104 | this->reportMeasurement(measurement); |
| 105 | } |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 106 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 107 | } |
| 108 | }); |
Sam Dubey | 160e4a1 | 2022-04-22 13:35:08 +0000 | [diff] [blame] | 109 | mThread.detach(); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void GnssMeasurementInterface::stop() { |
| 113 | ALOGD("stop"); |
| 114 | mIsActive = false; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void GnssMeasurementInterface::reportMeasurement(const GnssData& data) { |
| 118 | ALOGD("reportMeasurement()"); |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 119 | std::shared_ptr<IGnssMeasurementCallback> callbackCopy; |
| 120 | { |
| 121 | std::unique_lock<std::mutex> lock(mMutex); |
| 122 | if (sCallback == nullptr) { |
| 123 | ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); |
| 124 | return; |
| 125 | } |
| 126 | callbackCopy = sCallback; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 127 | } |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 128 | callbackCopy->gnssMeasurementCb(data); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } // namespace aidl::android::hardware::gnss |