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
GnssBatching()34 GnssBatching::GnssBatching()
35 : mMinIntervalMs(1000),
36 mWakeUpOnFifoFull(false),
37 mBatchedLocations(std::vector<GnssLocation>()) {}
~GnssBatching()38 GnssBatching::~GnssBatching() {
39 cleanup();
40 }
41
init(const std::shared_ptr<IGnssBatchingCallback> & callback)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
getBatchSize(int * size)49 ndk::ScopedAStatus GnssBatching::getBatchSize(int* size) {
50 ALOGD("getBatchingSize");
51 *size = BATCH_SIZE;
52 return ndk::ScopedAStatus::ok();
53 }
54
start(const Options & options)55 ndk::ScopedAStatus GnssBatching::start(const Options& options) {
56 ALOGD("start: periodNanos=%" PRId64 ", minDistanceMeters=%f, flags=%d", options.periodNanos,
57 options.minDistanceMeters, options.flags);
58 if (mIsActive) {
59 ALOGW("Gnss has started. Restarting...");
60 stop();
61 }
62
63 // mMinIntervalMs is not smaller than 1 sec
64 long periodNanos = (options.periodNanos < 1e9) ? 1e9 : options.periodNanos;
65 mMinIntervalMs = periodNanos / 1e6;
66 mWakeUpOnFifoFull = (options.flags & IGnssBatching::WAKEUP_ON_FIFO_FULL) ? true : false;
67 mMinDistanceMeters = options.minDistanceMeters;
68
69 mIsActive = true;
70 mThread = std::thread([this]() {
71 while (mIsActive == true) {
72 const auto location = common::Utils::getMockLocation();
73 this->batchLocation(location);
74 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs));
75 }
76 });
77
78 return ndk::ScopedAStatus::ok();
79 }
80
flush()81 ndk::ScopedAStatus GnssBatching::flush() {
82 ALOGD("flush");
83 std::vector<GnssLocation> copy = std::vector<GnssLocation>(mBatchedLocations);
84 ndk::ScopedAStatus status;
85 if (sCallback != nullptr) {
86 sCallback->gnssLocationBatchCb(copy);
87 status = ndk::ScopedAStatus::ok();
88 } else {
89 ALOGE("GnssBatchingCallback is null. flush() failed.");
90 status = ndk::ScopedAStatus::fromServiceSpecificError(IGnss::ERROR_GENERIC);
91 }
92 mBatchedLocations.clear();
93 return status;
94 }
95
stop()96 ndk::ScopedAStatus GnssBatching::stop() {
97 ALOGD("stop");
98 // Do not call flush() at stop()
99 mIsActive = false;
100 if (mThread.joinable()) {
101 mThread.join();
102 }
103 return ndk::ScopedAStatus::ok();
104 }
105
cleanup()106 ndk::ScopedAStatus GnssBatching::cleanup() {
107 ALOGD("cleanup");
108 std::unique_lock<std::mutex> lock(mMutex);
109 if (mIsActive) {
110 stop();
111 }
112 flush();
113
114 sCallback = nullptr;
115 return ndk::ScopedAStatus::ok();
116 }
117
batchLocation(const GnssLocation & location)118 void GnssBatching::batchLocation(const GnssLocation& location) {
119 if (mBatchedLocations.size() > BATCH_SIZE) {
120 mBatchedLocations.erase(mBatchedLocations.begin());
121 }
122 mBatchedLocations.push_back(location);
123 if (mWakeUpOnFifoFull && mBatchedLocations.size() == BATCH_SIZE) {
124 flush();
125 }
126 }
127
128 } // namespace aidl::android::hardware::gnss
129