Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -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 "GnssBatchingAidl" |
| 18 | |
| 19 | #include "GnssBatching.h" |
| 20 | #include <aidl/android/hardware/gnss/BnGnss.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <log/log.h> |
| 23 | #include <utils/SystemClock.h> |
| 24 | #include "Utils.h" |
| 25 | |
| 26 | namespace aidl::android::hardware::gnss { |
| 27 | |
| 28 | using namespace ::android::hardware::gnss; |
| 29 | |
| 30 | constexpr int BATCH_SIZE = 10; |
| 31 | |
| 32 | std::shared_ptr<IGnssBatchingCallback> GnssBatching::sCallback = nullptr; |
| 33 | |
| 34 | GnssBatching::GnssBatching() |
| 35 | : mMinIntervalMs(1000), |
| 36 | mWakeUpOnFifoFull(false), |
| 37 | mBatchedLocations(std::vector<GnssLocation>()) {} |
| 38 | GnssBatching::~GnssBatching() { |
| 39 | cleanup(); |
| 40 | } |
| 41 | |
| 42 | ndk::ScopedAStatus GnssBatching::init(const std::shared_ptr<IGnssBatchingCallback>& callback) { |
| 43 | ALOGD("init"); |
| 44 | std::unique_lock<std::mutex> lock(mMutex); |
| 45 | sCallback = callback; |
| 46 | return ndk::ScopedAStatus::ok(); |
| 47 | } |
| 48 | |
| 49 | ndk::ScopedAStatus GnssBatching::getBatchSize(int* size) { |
| 50 | ALOGD("getBatchingSize"); |
| 51 | *size = BATCH_SIZE; |
| 52 | return ndk::ScopedAStatus::ok(); |
| 53 | } |
| 54 | |
| 55 | ndk::ScopedAStatus GnssBatching::start(int64_t periodNanos, int flags) { |
| 56 | ALOGD("start: periodNanos=%" PRId64 ", flags=%d", periodNanos, flags); |
| 57 | if (mIsActive) { |
| 58 | ALOGW("Gnss has started. Restarting..."); |
| 59 | stop(); |
| 60 | } |
| 61 | |
| 62 | mWakeUpOnFifoFull = (flags & IGnssBatching::WAKEUP_ON_FIFO_FULL) ? true : false; |
| 63 | // mMinIntervalMs is not smaller than 1 sec |
| 64 | periodNanos = (periodNanos < 1e9) ? 1e9 : periodNanos; |
| 65 | mMinIntervalMs = periodNanos / 1e6; |
| 66 | |
| 67 | mIsActive = true; |
| 68 | mThread = std::thread([this]() { |
| 69 | while (mIsActive == true) { |
| 70 | const auto location = common::Utils::getMockLocation(); |
| 71 | this->batchLocation(location); |
| 72 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs)); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | return ndk::ScopedAStatus::ok(); |
| 77 | } |
| 78 | |
| 79 | ndk::ScopedAStatus GnssBatching::flush() { |
| 80 | ALOGD("flush"); |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 81 | std::vector<GnssLocation> copy = std::vector<GnssLocation>(mBatchedLocations); |
| 82 | ndk::ScopedAStatus status; |
| 83 | if (sCallback != nullptr) { |
| 84 | sCallback->gnssLocationBatchCb(copy); |
| 85 | status = ndk::ScopedAStatus::ok(); |
| 86 | } else { |
| 87 | ALOGE("GnssBatchingCallback is null. flush() failed."); |
| 88 | status = ndk::ScopedAStatus::fromServiceSpecificError(IGnss::ERROR_GENERIC); |
| 89 | } |
| 90 | mBatchedLocations.clear(); |
| 91 | return status; |
| 92 | } |
| 93 | |
| 94 | ndk::ScopedAStatus GnssBatching::stop() { |
| 95 | ALOGD("stop"); |
| 96 | // Do not call flush() at stop() |
| 97 | mIsActive = false; |
| 98 | if (mThread.joinable()) { |
| 99 | mThread.join(); |
| 100 | } |
| 101 | return ndk::ScopedAStatus::ok(); |
| 102 | } |
| 103 | |
| 104 | ndk::ScopedAStatus GnssBatching::cleanup() { |
| 105 | ALOGD("cleanup"); |
| 106 | std::unique_lock<std::mutex> lock(mMutex); |
| 107 | if (mIsActive) { |
| 108 | stop(); |
| 109 | } |
| 110 | flush(); |
| 111 | |
| 112 | sCallback = nullptr; |
| 113 | return ndk::ScopedAStatus::ok(); |
| 114 | } |
| 115 | |
| 116 | void GnssBatching::batchLocation(const GnssLocation& location) { |
| 117 | if (mBatchedLocations.size() > BATCH_SIZE) { |
| 118 | mBatchedLocations.erase(mBatchedLocations.begin()); |
| 119 | } |
| 120 | mBatchedLocations.push_back(location); |
| 121 | if (mWakeUpOnFifoFull && mBatchedLocations.size() == BATCH_SIZE) { |
| 122 | flush(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } // namespace aidl::android::hardware::gnss |