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 before <log/log.h> to overwrite the default value.
18 #define LOG_TAG "GnssBatchingJni"
19
20 #include "GnssBatching.h"
21
22 #include "Utils.h"
23
24 using android::hardware::gnss::IGnssBatching;
25 using IGnssBatching_V1_0 = android::hardware::gnss::V1_0::IGnssBatching;
26 using IGnssBatching_V2_0 = android::hardware::gnss::V2_0::IGnssBatching;
27
28 namespace android::gnss {
29
30 // Implementation of GnssBatching (AIDL HAL)
31
GnssBatching(const sp<IGnssBatching> & iGnssBatching)32 GnssBatching::GnssBatching(const sp<IGnssBatching>& iGnssBatching) : mIGnssBatching(iGnssBatching) {
33 assert(mIGnssBatching != nullptr);
34 }
35
init(const std::unique_ptr<GnssBatchingCallback> & callback)36 jboolean GnssBatching::init(const std::unique_ptr<GnssBatchingCallback>& callback) {
37 auto status = mIGnssBatching->init(callback->getAidl());
38 return checkAidlStatus(status, "IGnssBatchingAidl init() failed.");
39 }
40
getBatchSize()41 jint GnssBatching::getBatchSize() {
42 int size = 0;
43 auto status = mIGnssBatching->getBatchSize(&size);
44 if (!checkAidlStatus(status, "IGnssBatchingAidl getBatchSize() failed")) {
45 return 0;
46 }
47 return size;
48 }
49
start(long periodNanos,float minUpdateDistanceMeters,bool wakeOnFifoFull)50 jboolean GnssBatching::start(long periodNanos, float minUpdateDistanceMeters, bool wakeOnFifoFull) {
51 IGnssBatching::Options options;
52 options.flags = (wakeOnFifoFull) ? IGnssBatching::WAKEUP_ON_FIFO_FULL : 0;
53 options.periodNanos = periodNanos;
54 options.minDistanceMeters = minUpdateDistanceMeters;
55 auto status = mIGnssBatching->start(options);
56 return checkAidlStatus(status, "IGnssBatchingAidl start() failed.");
57 }
58
stop()59 jboolean GnssBatching::stop() {
60 auto status = mIGnssBatching->stop();
61 return checkAidlStatus(status, "IGnssBatchingAidl stop() failed.");
62 }
63
flush()64 jboolean GnssBatching::flush() {
65 auto status = mIGnssBatching->flush();
66 return checkAidlStatus(status, "IGnssBatchingAidl flush() failed.");
67 }
68
cleanup()69 jboolean GnssBatching::cleanup() {
70 auto status = mIGnssBatching->cleanup();
71 return checkAidlStatus(status, "IGnssBatchingAidl cleanup() failed");
72 }
73
74 // Implementation of GnssBatching_V1_0
75
GnssBatching_V1_0(const sp<IGnssBatching_V1_0> & iGnssBatching)76 GnssBatching_V1_0::GnssBatching_V1_0(const sp<IGnssBatching_V1_0>& iGnssBatching)
77 : mIGnssBatching_V1_0(iGnssBatching) {
78 assert(mIGnssBatching_V1_0 != nullptr);
79 }
80
init(const std::unique_ptr<GnssBatchingCallback> & callback)81 jboolean GnssBatching_V1_0::init(const std::unique_ptr<GnssBatchingCallback>& callback) {
82 auto result = mIGnssBatching_V1_0->init(callback->getV1_0());
83 return checkHidlReturn(result, "IGnssBatching_V1_0 init() failed.");
84 }
85
getBatchSize()86 jint GnssBatching_V1_0::getBatchSize() {
87 auto result = mIGnssBatching_V1_0->getBatchSize();
88 if (!checkHidlReturn(result, "IGnssBatching getBatchSize() failed.")) {
89 return 0; // failure in binder, don't support batching
90 }
91 return static_cast<jint>(result);
92 }
93
start(long periodNanos,float minUpdateDistanceMeters,bool wakeOnFifoFull)94 jboolean GnssBatching_V1_0::start(long periodNanos, float minUpdateDistanceMeters,
95 bool wakeOnFifoFull) {
96 IGnssBatching_V1_0::Options options;
97 options.periodNanos = periodNanos;
98 if (minUpdateDistanceMeters > 0) {
99 ALOGW("minUpdateDistanceMeters is not supported in 1.0 GNSS HAL.");
100 }
101 if (wakeOnFifoFull) {
102 options.flags = static_cast<uint8_t>(IGnssBatching_V1_0::Flag::WAKEUP_ON_FIFO_FULL);
103 } else {
104 options.flags = 0;
105 }
106
107 auto result = mIGnssBatching_V1_0->start(options);
108 return checkHidlReturn(result, "IGnssBatching start() failed.");
109 }
110
stop()111 jboolean GnssBatching_V1_0::stop() {
112 auto result = mIGnssBatching_V1_0->stop();
113 return checkHidlReturn(result, "IGnssBatching stop() failed.");
114 }
115
flush()116 jboolean GnssBatching_V1_0::flush() {
117 auto result = mIGnssBatching_V1_0->flush();
118 return checkHidlReturn(result, "IGnssBatching flush() failed.");
119 }
120
cleanup()121 jboolean GnssBatching_V1_0::cleanup() {
122 auto result = mIGnssBatching_V1_0->cleanup();
123 return checkHidlReturn(result, "IGnssBatching cleanup() failed.");
124 }
125
126 // Implementation of GnssBatching_V2_0
127
GnssBatching_V2_0(const sp<IGnssBatching_V2_0> & iGnssBatching)128 GnssBatching_V2_0::GnssBatching_V2_0(const sp<IGnssBatching_V2_0>& iGnssBatching)
129 : GnssBatching_V1_0{iGnssBatching}, mIGnssBatching_V2_0(iGnssBatching) {
130 assert(mIGnssBatching_V2_0 != nullptr);
131 }
132
init(const std::unique_ptr<GnssBatchingCallback> & callback)133 jboolean GnssBatching_V2_0::init(const std::unique_ptr<GnssBatchingCallback>& callback) {
134 auto result = mIGnssBatching_V2_0->init_2_0(callback->getV2_0());
135 return checkHidlReturn(result, "IGnssBatching_V2_0 init() failed.");
136 }
137
138 } // namespace android::gnss
139