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( |
| 37 | const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking) { |
| 38 | ALOGD("setCallback: enableFullTracking: %d", (int)enableFullTracking); |
| 39 | std::unique_lock<std::mutex> lock(mMutex); |
| 40 | sCallback = callback; |
| 41 | |
| 42 | if (mIsActive) { |
| 43 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 44 | stop(); |
| 45 | } |
| 46 | start(); |
| 47 | |
| 48 | return ndk::ScopedAStatus::ok(); |
| 49 | } |
| 50 | |
| 51 | ndk::ScopedAStatus GnssMeasurementInterface::close() { |
| 52 | ALOGD("close"); |
| 53 | stop(); |
| 54 | std::unique_lock<std::mutex> lock(mMutex); |
| 55 | sCallback = nullptr; |
| 56 | return ndk::ScopedAStatus::ok(); |
| 57 | } |
| 58 | |
| 59 | void GnssMeasurementInterface::start() { |
| 60 | ALOGD("start"); |
| 61 | mIsActive = true; |
| 62 | mThread = std::thread([this]() { |
| 63 | while (mIsActive == true) { |
| 64 | auto measurement = Utils::getMockMeasurement(); |
| 65 | this->reportMeasurement(measurement); |
| 66 | |
| 67 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | void GnssMeasurementInterface::stop() { |
| 73 | ALOGD("stop"); |
| 74 | mIsActive = false; |
| 75 | if (mThread.joinable()) { |
| 76 | mThread.join(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void GnssMeasurementInterface::reportMeasurement(const GnssData& data) { |
| 81 | ALOGD("reportMeasurement()"); |
| 82 | std::unique_lock<std::mutex> lock(mMutex); |
| 83 | if (sCallback == nullptr) { |
| 84 | ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); |
| 85 | return; |
| 86 | } |
| 87 | sCallback->gnssMeasurementCb(data); |
| 88 | } |
| 89 | |
| 90 | } // namespace aidl::android::hardware::gnss |