blob: 33e1fd500e66b617d58ce073f5e3113bc6277d8b [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
Shinru Hana42956f2021-12-29 15:45:57 +080055ndk::ScopedAStatus GnssBatching::start(const Options& options) {
56 ALOGD("start: periodNanos=%" PRId64 ", minDistanceMeters=%f, flags=%d", options.periodNanos,
57 options.minDistanceMeters, options.flags);
Yu-Han Yang3a75dc02021-09-27 01:01:06 -070058 if (mIsActive) {
59 ALOGW("Gnss has started. Restarting...");
60 stop();
61 }
62
Yu-Han Yang3a75dc02021-09-27 01:01:06 -070063 // mMinIntervalMs is not smaller than 1 sec
Shinru Hana42956f2021-12-29 15:45:57 +080064 long periodNanos = (options.periodNanos < 1e9) ? 1e9 : options.periodNanos;
Yu-Han Yang3a75dc02021-09-27 01:01:06 -070065 mMinIntervalMs = periodNanos / 1e6;
Shinru Hana42956f2021-12-29 15:45:57 +080066 mWakeUpOnFifoFull = (options.flags & IGnssBatching::WAKEUP_ON_FIFO_FULL) ? true : false;
67 mMinDistanceMeters = options.minDistanceMeters;
Yu-Han Yang3a75dc02021-09-27 01:01:06 -070068
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
81ndk::ScopedAStatus GnssBatching::flush() {
82 ALOGD("flush");
Yu-Han Yang3a75dc02021-09-27 01:01:06 -070083 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
96ndk::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
106ndk::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
118void 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