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() { |
| 35 | stop(); |
| 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"); |
| 49 | stop(); |
| 50 | std::unique_lock<std::mutex> lock(mMutex); |
| 51 | sCallback = nullptr; |
| 52 | return ndk::ScopedAStatus::ok(); |
| 53 | } |
| 54 | |
| 55 | void GnssNavigationMessageInterface::start() { |
| 56 | ALOGD("start"); |
| 57 | mIsActive = true; |
| 58 | mThread = std::thread([this]() { |
| 59 | while (mIsActive == true) { |
| 60 | GnssNavigationMessage message = { |
| 61 | .svid = 19, |
| 62 | .type = GnssNavigationMessageType::GPS_L1CA, |
| 63 | .status = GnssNavigationMessage::STATUS_PARITY_PASSED, |
| 64 | .messageId = 2, |
| 65 | .submessageId = 3, |
| 66 | .data = std::vector<uint8_t>(40, 0xF9), |
| 67 | }; |
| 68 | this->reportMessage(message); |
| 69 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 70 | } |
| 71 | }); |
Sam Dubey | 160e4a1 | 2022-04-22 13:35:08 +0000 | [diff] [blame] | 72 | mThread.detach(); |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void GnssNavigationMessageInterface::stop() { |
| 76 | ALOGD("stop"); |
| 77 | mIsActive = false; |
Yu-Han Yang | 10cf736 | 2021-10-03 22:32:04 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void GnssNavigationMessageInterface::reportMessage(const GnssNavigationMessage& message) { |
| 81 | ALOGD("reportMessage()"); |
| 82 | std::shared_ptr<IGnssNavigationMessageCallback> callbackCopy; |
| 83 | { |
| 84 | std::unique_lock<std::mutex> lock(mMutex); |
| 85 | if (sCallback == nullptr) { |
| 86 | ALOGE("%s: GnssNavigationMessageInterface::sCallback is null.", __func__); |
| 87 | return; |
| 88 | } |
| 89 | callbackCopy = sCallback; |
| 90 | } |
| 91 | callbackCopy->gnssNavigationMessageCb(message); |
| 92 | } |
| 93 | |
| 94 | } // namespace aidl::android::hardware::gnss |