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 | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 22 | #include "GnssReplayUtils.h" |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 23 | #include "Utils.h" |
| 24 | |
| 25 | namespace aidl::android::hardware::gnss { |
| 26 | |
| 27 | using Utils = ::android::hardware::gnss::common::Utils; |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 28 | using ReplayUtils = ::android::hardware::gnss::common::ReplayUtils; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 29 | |
| 30 | std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr; |
| 31 | |
| 32 | GnssMeasurementInterface::GnssMeasurementInterface() : mMinIntervalMillis(1000) {} |
| 33 | |
| 34 | GnssMeasurementInterface::~GnssMeasurementInterface() { |
| 35 | stop(); |
| 36 | } |
| 37 | |
| 38 | ndk::ScopedAStatus GnssMeasurementInterface::setCallback( |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 39 | const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking, |
| 40 | const bool enableCorrVecOutputs) { |
| 41 | ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking, |
| 42 | (int)enableCorrVecOutputs); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 43 | std::unique_lock<std::mutex> lock(mMutex); |
| 44 | sCallback = callback; |
| 45 | |
| 46 | if (mIsActive) { |
| 47 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 48 | stop(); |
| 49 | } |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 50 | start(enableCorrVecOutputs); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 51 | |
| 52 | return ndk::ScopedAStatus::ok(); |
| 53 | } |
| 54 | |
| 55 | ndk::ScopedAStatus GnssMeasurementInterface::close() { |
| 56 | ALOGD("close"); |
| 57 | stop(); |
| 58 | std::unique_lock<std::mutex> lock(mMutex); |
| 59 | sCallback = nullptr; |
| 60 | return ndk::ScopedAStatus::ok(); |
| 61 | } |
| 62 | |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 63 | void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 64 | ALOGD("start"); |
| 65 | mIsActive = true; |
Shinru Han | 4abab50 | 2020-12-09 15:07:18 +0800 | [diff] [blame] | 66 | mThread = std::thread([this, enableCorrVecOutputs]() { |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 67 | while (mIsActive == true) { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 68 | std::string rawMeasurementStr = ""; |
| 69 | if (ReplayUtils::hasGnssDeviceFile() && |
| 70 | ReplayUtils::isGnssRawMeasurement( |
| 71 | rawMeasurementStr = ReplayUtils::getDataFromDeviceFile( |
| 72 | std::string( |
| 73 | ::android::hardware::gnss::common::CMD_GET_RAWMEASUREMENT), |
| 74 | mMinIntervalMillis))) { |
| 75 | // TODO: implement rawMeasurementStr parser and report measurement. |
| 76 | ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(), |
| 77 | rawMeasurementStr.c_str()); |
| 78 | auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs); |
| 79 | this->reportMeasurement(measurement); |
| 80 | } else { |
| 81 | auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs); |
| 82 | this->reportMeasurement(measurement); |
| 83 | } |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 84 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 85 | } |
| 86 | }); |
Yu-Han Yang | 26c7635 | 2021-05-11 13:16:35 -0700 | [diff] [blame] | 87 | mThread.detach(); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void GnssMeasurementInterface::stop() { |
| 91 | ALOGD("stop"); |
| 92 | mIsActive = false; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void GnssMeasurementInterface::reportMeasurement(const GnssData& data) { |
| 96 | ALOGD("reportMeasurement()"); |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 97 | std::shared_ptr<IGnssMeasurementCallback> callbackCopy; |
| 98 | { |
| 99 | std::unique_lock<std::mutex> lock(mMutex); |
| 100 | if (sCallback == nullptr) { |
| 101 | ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); |
| 102 | return; |
| 103 | } |
| 104 | callbackCopy = sCallback; |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 105 | } |
Yu-Han Yang | 266c36a | 2021-05-27 16:00:59 -0700 | [diff] [blame] | 106 | callbackCopy->gnssMeasurementCb(data); |
Yu-Han Yang | 0483230 | 2020-11-20 09:51:18 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace aidl::android::hardware::gnss |