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