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