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 | |
| 59 | ndk::ScopedAStatus GnssMeasurementInterface::close() { |
| 60 | ALOGD("close"); |
| 61 | stop(); |
| 62 | std::unique_lock<std::mutex> lock(mMutex); |
| 63 | sCallback = nullptr; |
| 64 | return ndk::ScopedAStatus::ok(); |
| 65 | } |
| 66 | |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 67 | void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 68 | ALOGD("start"); |
| 69 | mIsActive = true; |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 70 | mThread = std::thread([this, enableCorrVecOutputs]() { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 71 | while (mIsActive == true) { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 72 | std::string rawMeasurementStr = ""; |
| 73 | if (ReplayUtils::hasGnssDeviceFile() && |
| 74 | ReplayUtils::isGnssRawMeasurement( |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 75 | rawMeasurementStr = |
| 76 | DeviceFileReader::Instance().getGnssRawMeasurementData())) { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 77 | ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(), |
| 78 | rawMeasurementStr.c_str()); |
Yuchen He | 3cbf5f3 | 2021-08-30 22:20:30 +0000 | [diff] [blame] | 79 | auto measurement = |
| 80 | GnssRawMeasurementParser::getMeasurementFromStrs(rawMeasurementStr); |
| 81 | if (measurement != nullptr) { |
| 82 | this->reportMeasurement(*measurement); |
| 83 | } |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 84 | } else { |
| 85 | auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs); |
| 86 | this->reportMeasurement(measurement); |
| 87 | } |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 88 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 89 | } |
| 90 | }); |
Yu-Han Yang | 26c7635 | 2021-05-11 13:16:35 -0700 | [diff] [blame] | 91 | mThread.detach(); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void GnssMeasurementInterface::stop() { |
| 95 | ALOGD("stop"); |
| 96 | mIsActive = false; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void GnssMeasurementInterface::reportMeasurement(const GnssData& data) { |
| 100 | ALOGD("reportMeasurement()"); |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 101 | std::shared_ptr<IGnssMeasurementCallback> callbackCopy; |
| 102 | { |
| 103 | std::unique_lock<std::mutex> lock(mMutex); |
| 104 | if (sCallback == nullptr) { |
| 105 | ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); |
| 106 | return; |
| 107 | } |
| 108 | callbackCopy = sCallback; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 109 | } |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 110 | callbackCopy->gnssMeasurementCb(data); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // namespace aidl::android::hardware::gnss |