Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "GnssNavigationMessageAidl" |
| 18 | |
| 19 | #include "GnssNavigationMessageInterface.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 namespace ::android::hardware::gnss; |
| 27 | using GnssNavigationMessage = IGnssNavigationMessageCallback::GnssNavigationMessage; |
| 28 | using GnssNavigationMessageType = GnssNavigationMessage::GnssNavigationMessageType; |
| 29 | |
| 30 | std::shared_ptr<IGnssNavigationMessageCallback> GnssNavigationMessageInterface::sCallback = nullptr; |
| 31 | |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 32 | GnssNavigationMessageInterface::GnssNavigationMessageInterface() : mMinIntervalMillis(1000) { |
| 33 | mThreads.reserve(2); |
| 34 | } |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 35 | |
| 36 | GnssNavigationMessageInterface::~GnssNavigationMessageInterface() { |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 37 | waitForStoppingThreads(); |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ndk::ScopedAStatus GnssNavigationMessageInterface::setCallback( |
| 41 | const std::shared_ptr<IGnssNavigationMessageCallback>& callback) { |
| 42 | ALOGD("setCallback"); |
| 43 | std::unique_lock<std::mutex> lock(mMutex); |
| 44 | sCallback = callback; |
| 45 | start(); |
| 46 | return ndk::ScopedAStatus::ok(); |
| 47 | } |
| 48 | |
| 49 | ndk::ScopedAStatus GnssNavigationMessageInterface::close() { |
| 50 | ALOGD("close"); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 51 | if (mIsActive) { |
| 52 | stop(); |
| 53 | } |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 54 | std::unique_lock<std::mutex> lock(mMutex); |
| 55 | sCallback = nullptr; |
| 56 | return ndk::ScopedAStatus::ok(); |
| 57 | } |
| 58 | |
| 59 | void GnssNavigationMessageInterface::start() { |
| 60 | ALOGD("start"); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 61 | |
| 62 | if (mIsActive) { |
| 63 | ALOGD("restarting since nav msg has started"); |
| 64 | stop(); |
| 65 | } |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 66 | |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 67 | mIsActive = true; |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 68 | mThreads.emplace_back(std::thread([this]() { |
| 69 | waitForStoppingThreads(); |
| 70 | mThreadBlocker.reset(); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 71 | do { |
| 72 | if (!mIsActive) { |
| 73 | break; |
| 74 | } |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 75 | GnssNavigationMessage message = { |
| 76 | .svid = 19, |
| 77 | .type = GnssNavigationMessageType::GPS_L1CA, |
| 78 | .status = GnssNavigationMessage::STATUS_PARITY_PASSED, |
| 79 | .messageId = 2, |
| 80 | .submessageId = 3, |
| 81 | .data = std::vector<uint8_t>(40, 0xF9), |
| 82 | }; |
| 83 | this->reportMessage(message); |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 84 | } while (mIsActive && |
| 85 | mThreadBlocker.wait_for(std::chrono::milliseconds(mMinIntervalMillis))); |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 86 | })); |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void GnssNavigationMessageInterface::stop() { |
| 90 | ALOGD("stop"); |
| 91 | mIsActive = false; |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 92 | mThreadBlocker.notify(); |
Yu-Han Yang | e28dd79 | 2023-10-15 17:47:23 +0000 | [diff] [blame] | 93 | for (auto iter = mThreads.begin(); iter != mThreads.end(); ++iter) { |
| 94 | if (iter->joinable()) { |
| 95 | mFutures.push_back(std::async(std::launch::async, [this, iter] { |
| 96 | iter->join(); |
| 97 | mThreads.erase(iter); |
| 98 | })); |
| 99 | } else { |
| 100 | mThreads.erase(iter); |
| 101 | } |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 102 | } |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void GnssNavigationMessageInterface::reportMessage(const GnssNavigationMessage& message) { |
| 106 | ALOGD("reportMessage()"); |
| 107 | std::shared_ptr<IGnssNavigationMessageCallback> callbackCopy; |
| 108 | { |
| 109 | std::unique_lock<std::mutex> lock(mMutex); |
| 110 | if (sCallback == nullptr) { |
| 111 | ALOGE("%s: GnssNavigationMessageInterface::sCallback is null.", __func__); |
| 112 | return; |
| 113 | } |
| 114 | callbackCopy = sCallback; |
| 115 | } |
| 116 | callbackCopy->gnssNavigationMessageCb(message); |
| 117 | } |
| 118 | |
Yu-Han Yang | 19a32b6 | 2022-04-27 09:57:01 -0700 | [diff] [blame] | 119 | void GnssNavigationMessageInterface::waitForStoppingThreads() { |
| 120 | for (auto& future : mFutures) { |
| 121 | ALOGD("Stopping previous thread."); |
| 122 | future.wait(); |
| 123 | ALOGD("Done stopping thread."); |
| 124 | } |
| 125 | mFutures.clear(); |
| 126 | } |
| 127 | |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 128 | } // namespace aidl::android::hardware::gnss |