blob: 2444edc63bafe9417f01054420b717b983d785c7 [file] [log] [blame]
Yu-Han Yang3a75dc02021-09-27 01:01:06 -07001/*
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
26namespace aidl::android::hardware::gnss {
27
28using namespace ::android::hardware::gnss;
29
30constexpr int BATCH_SIZE = 10;
31
32std::shared_ptr<IGnssBatchingCallback> GnssBatching::sCallback = nullptr;
33
34GnssBatching::GnssBatching()
35 : mMinIntervalMs(1000),
36 mWakeUpOnFifoFull(false),
37 mBatchedLocations(std::vector<GnssLocation>()) {}
38GnssBatching::~GnssBatching() {
39 cleanup();
40}
41
42ndk::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
49ndk::ScopedAStatus GnssBatching::getBatchSize(int* size) {
50 ALOGD("getBatchingSize");
51 *size = BATCH_SIZE;
52 return ndk::ScopedAStatus::ok();
53}
54
55ndk::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
79ndk::ScopedAStatus GnssBatching::flush() {
80 ALOGD("flush");
81 if (mBatchedLocations.empty()) {
82 return ndk::ScopedAStatus::ok();
83 }
84 std::vector<GnssLocation> copy = std::vector<GnssLocation>(mBatchedLocations);
85 ndk::ScopedAStatus status;
86 if (sCallback != nullptr) {
87 sCallback->gnssLocationBatchCb(copy);
88 status = ndk::ScopedAStatus::ok();
89 } else {
90 ALOGE("GnssBatchingCallback is null. flush() failed.");
91 status = ndk::ScopedAStatus::fromServiceSpecificError(IGnss::ERROR_GENERIC);
92 }
93 mBatchedLocations.clear();
94 return status;
95}
96
97ndk::ScopedAStatus GnssBatching::stop() {
98 ALOGD("stop");
99 // Do not call flush() at stop()
100 mIsActive = false;
101 if (mThread.joinable()) {
102 mThread.join();
103 }
104 return ndk::ScopedAStatus::ok();
105}
106
107ndk::ScopedAStatus GnssBatching::cleanup() {
108 ALOGD("cleanup");
109 std::unique_lock<std::mutex> lock(mMutex);
110 if (mIsActive) {
111 stop();
112 }
113 flush();
114
115 sCallback = nullptr;
116 return ndk::ScopedAStatus::ok();
117}
118
119void GnssBatching::batchLocation(const GnssLocation& location) {
120 if (mBatchedLocations.size() > BATCH_SIZE) {
121 mBatchedLocations.erase(mBatchedLocations.begin());
122 }
123 mBatchedLocations.push_back(location);
124 if (mWakeUpOnFifoFull && mBatchedLocations.size() == BATCH_SIZE) {
125 flush();
126 }
127}
128
129} // namespace aidl::android::hardware::gnss