Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. |
| 3 | * Not a Contribution |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright (C) 2016 The Android Open Source Project |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #define LOG_TAG "LocSvc_GnssBatchingInterface" |
| 22 | |
| 23 | #include <log_util.h> |
| 24 | #include <BatchingAPIClient.h> |
| 25 | #include "GnssBatching.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace gnss { |
| 30 | namespace V1_1 { |
| 31 | namespace implementation { |
| 32 | |
| 33 | void GnssBatching::GnssBatchingDeathRecipient::serviceDied( |
| 34 | uint64_t cookie, const wp<IBase>& who) { |
| 35 | LOC_LOGE("%s] service died. cookie: %llu, who: %p", |
| 36 | __FUNCTION__, static_cast<unsigned long long>(cookie), &who); |
| 37 | if (mGnssBatching != nullptr) { |
| 38 | mGnssBatching->stop(); |
| 39 | mGnssBatching->cleanup(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | GnssBatching::GnssBatching() : mApi(nullptr) { |
| 44 | mGnssBatchingDeathRecipient = new GnssBatchingDeathRecipient(this); |
| 45 | } |
| 46 | |
| 47 | GnssBatching::~GnssBatching() { |
| 48 | if (mApi != nullptr) { |
| 49 | mApi->destroy(); |
| 50 | mApi = nullptr; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | // Methods from ::android::hardware::gnss::V1_0::IGnssBatching follow. |
| 56 | Return<bool> GnssBatching::init(const sp<IGnssBatchingCallback>& callback) { |
| 57 | if (mApi != nullptr) { |
| 58 | LOC_LOGD("%s]: mApi is NOT nullptr, delete it first", __FUNCTION__); |
| 59 | mApi->destroy(); |
| 60 | mApi = nullptr; |
| 61 | } |
| 62 | |
| 63 | mApi = new BatchingAPIClient(callback); |
| 64 | if (mApi == nullptr) { |
| 65 | LOC_LOGE("%s]: failed to create mApi", __FUNCTION__); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (mGnssBatchingCbIface != nullptr) { |
| 70 | mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient); |
| 71 | } |
| 72 | mGnssBatchingCbIface = callback; |
| 73 | if (mGnssBatchingCbIface != nullptr) { |
| 74 | mGnssBatchingCbIface->linkToDeath(mGnssBatchingDeathRecipient, 0 /*cookie*/); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | Return<uint16_t> GnssBatching::getBatchSize() { |
| 81 | uint16_t ret = 0; |
| 82 | if (mApi == nullptr) { |
| 83 | LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__); |
| 84 | } else { |
| 85 | ret = mApi->getBatchSize(); |
| 86 | } |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | Return<bool> GnssBatching::start(const IGnssBatching::Options& options) { |
| 91 | bool ret = false; |
| 92 | if (mApi == nullptr) { |
| 93 | LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__); |
| 94 | } else { |
| 95 | ret = mApi->startSession(options); |
| 96 | } |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | Return<void> GnssBatching::flush() { |
| 101 | if (mApi == nullptr) { |
| 102 | LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__); |
| 103 | } else { |
| 104 | mApi->flushBatchedLocations(); |
| 105 | } |
| 106 | return Void(); |
| 107 | } |
| 108 | |
| 109 | Return<bool> GnssBatching::stop() { |
| 110 | bool ret = false; |
| 111 | if (mApi == nullptr) { |
| 112 | LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__); |
| 113 | } else { |
| 114 | ret = mApi->stopSession(); |
| 115 | } |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | Return<void> GnssBatching::cleanup() { |
| 120 | if (mGnssBatchingCbIface != nullptr) { |
| 121 | mGnssBatchingCbIface->unlinkToDeath(mGnssBatchingDeathRecipient); |
| 122 | } |
| 123 | return Void(); |
| 124 | } |
| 125 | |
| 126 | } // namespace implementation |
| 127 | } // namespace V1_1 |
| 128 | } // namespace gnss |
| 129 | } // namespace hardware |
| 130 | } // namespace android |