Sasha Kuznetsov | 44d2b0a | 2020-01-16 18:41:39 -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 "GnssAntennaInfo" |
| 18 | |
| 19 | #include "GnssAntennaInfo.h" |
| 20 | #include "Utils.h" |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | |
| 24 | using ::android::hardware::gnss::common::Utils; |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace gnss { |
| 29 | namespace V2_1 { |
| 30 | namespace implementation { |
| 31 | |
| 32 | sp<IGnssAntennaInfoCallback> GnssAntennaInfo::sCallback = nullptr; |
| 33 | |
| 34 | GnssAntennaInfo::GnssAntennaInfo() : mMinIntervalMillis(1000) {} |
| 35 | |
| 36 | GnssAntennaInfo::~GnssAntennaInfo() { |
| 37 | stop(); |
| 38 | } |
| 39 | |
| 40 | // Methods from ::android::hardware::gnss::V2_1::IGnssAntennaInfo follow. |
| 41 | Return<GnssAntennaInfo::GnssAntennaInfoStatus> GnssAntennaInfo::setCallback( |
| 42 | const sp<IGnssAntennaInfoCallback>& callback) { |
| 43 | ALOGD("setCallback"); |
| 44 | std::unique_lock<std::mutex> lock(mMutex); |
| 45 | sCallback = callback; |
| 46 | |
| 47 | if (mIsActive) { |
| 48 | ALOGW("GnssAntennaInfo callback already set. Resetting the callback..."); |
| 49 | stop(); |
| 50 | } |
| 51 | start(); |
| 52 | |
| 53 | return GnssAntennaInfoStatus::SUCCESS; |
| 54 | } |
| 55 | |
| 56 | Return<void> GnssAntennaInfo::close() { |
| 57 | ALOGD("close"); |
| 58 | std::unique_lock<std::mutex> lock(mMutex); |
| 59 | stop(); |
| 60 | sCallback = nullptr; |
| 61 | return Void(); |
| 62 | } |
| 63 | |
| 64 | // Private methods |
| 65 | void GnssAntennaInfo::start() { |
| 66 | ALOGD("start"); |
| 67 | mIsActive = true; |
| 68 | mThread = std::thread([this]() { |
| 69 | while (mIsActive == true) { |
| 70 | if (sCallback != nullptr) { |
| 71 | auto antennaInfos = Utils::getMockAntennaInfos(); |
| 72 | this->reportAntennaInfo(antennaInfos); |
| 73 | } |
| 74 | |
| 75 | /** For mock implementation this is good. On real device, we should only report |
| 76 | antennaInfo at start and when there is a configuration change. **/ |
| 77 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | void GnssAntennaInfo::stop() { |
| 83 | ALOGD("stop"); |
| 84 | mIsActive = false; |
| 85 | if (mThread.joinable()) { |
| 86 | mThread.join(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void GnssAntennaInfo::reportAntennaInfo( |
| 91 | const hidl_vec<IGnssAntennaInfoCallback::GnssAntennaInfo>& antennaInfo) const { |
| 92 | std::unique_lock<std::mutex> lock(mMutex); |
| 93 | |
| 94 | if (sCallback == nullptr) { |
| 95 | ALOGE("%s: No non-null callback", __func__); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | auto ret = sCallback->gnssAntennaInfoCb(antennaInfo); |
| 100 | if (!ret.isOk()) { |
| 101 | ALOGE("%s: Unable to invoke callback", __func__); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } // namespace implementation |
| 106 | } // namespace V2_1 |
| 107 | } // namespace gnss |
| 108 | } // namespace hardware |
| 109 | } // namespace android |