Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2020, 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 | |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 21 | /* Changes from Qualcomm Innovation Center are provided under the following license: |
| 22 | * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved. |
| 23 | * SPDX-License-Identifier: BSD-3-Clause-Clear |
| 24 | */ |
| 25 | |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 26 | #define LOG_TAG "LocSvc_GnssMeasurementInterface" |
| 27 | |
| 28 | #include <log_util.h> |
| 29 | #include "GnssMeasurement.h" |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace gnss { |
| 34 | namespace V2_1 { |
| 35 | namespace implementation { |
| 36 | |
| 37 | void GnssMeasurement::GnssMeasurementDeathRecipient::serviceDied( |
| 38 | uint64_t cookie, const wp<IBase>& who) { |
| 39 | LOC_LOGE("%s] service died. cookie: %llu, who: %p", |
| 40 | __FUNCTION__, static_cast<unsigned long long>(cookie), &who); |
| 41 | if (mGnssMeasurement != nullptr) { |
| 42 | mGnssMeasurement->close(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | GnssMeasurement::GnssMeasurement() { |
| 47 | mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(this); |
| 48 | mApi = new MeasurementAPIClient(); |
| 49 | } |
| 50 | |
| 51 | GnssMeasurement::~GnssMeasurement() { |
| 52 | if (mApi) { |
| 53 | mApi->destroy(); |
| 54 | mApi = nullptr; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow. |
| 59 | Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 60 | const sp<V1_0::IGnssMeasurementCallback>& callback) { |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 61 | return setCallback(callback, mGnssMeasurementCbIface, GNSS_POWER_MODE_INVALID); |
| 62 | } |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 63 | |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 64 | template <typename T> |
| 65 | Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 66 | const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode) { |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 67 | Return<GnssMeasurement::GnssMeasurementStatus> ret = |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 68 | IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC; |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 69 | if (mApi == nullptr) { |
| 70 | LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__); |
| 71 | return ret; |
| 72 | } |
| 73 | |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 74 | if (myCallback != callback) { |
| 75 | if (nullptr == callback) { |
| 76 | LOC_LOGE("%s]: callback is nullptr", __FUNCTION__); |
| 77 | mApi->measurementSetCallback(callback); |
| 78 | close(); |
| 79 | } else { |
| 80 | if (nullptr != myCallback) { |
| 81 | myCallback->unlinkToDeath(mGnssMeasurementDeathRecipient); |
| 82 | } |
| 83 | myCallback = callback; |
| 84 | myCallback->linkToDeath(mGnssMeasurementDeathRecipient, 0); |
| 85 | ret = mApi->measurementSetCallback(callback, powerMode); |
| 86 | } |
| 87 | } |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 88 | |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 89 | return ret; |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void GnssMeasurement::clearInterfaces() { |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 93 | if (mApi != nullptr) { |
| 94 | mApi->measurementSetCallback<V1_0::IGnssMeasurementCallback>(nullptr); |
| 95 | mApi->measurementSetCallback<V1_1::IGnssMeasurementCallback>(nullptr); |
| 96 | mApi->measurementSetCallback<V2_0::IGnssMeasurementCallback>(nullptr); |
| 97 | mApi->measurementSetCallback<V2_1::IGnssMeasurementCallback>(nullptr); |
| 98 | } |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 99 | if (mGnssMeasurementCbIface != nullptr) { |
| 100 | mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient); |
| 101 | mGnssMeasurementCbIface = nullptr; |
| 102 | } |
| 103 | if (mGnssMeasurementCbIface_1_1 != nullptr) { |
| 104 | mGnssMeasurementCbIface_1_1->unlinkToDeath(mGnssMeasurementDeathRecipient); |
| 105 | mGnssMeasurementCbIface_1_1 = nullptr; |
| 106 | } |
| 107 | if (mGnssMeasurementCbIface_2_0 != nullptr) { |
| 108 | mGnssMeasurementCbIface_2_0->unlinkToDeath(mGnssMeasurementDeathRecipient); |
| 109 | mGnssMeasurementCbIface_2_0 = nullptr; |
| 110 | } |
| 111 | if (mGnssMeasurementCbIface_2_1 != nullptr) { |
| 112 | mGnssMeasurementCbIface_2_1->unlinkToDeath(mGnssMeasurementDeathRecipient); |
| 113 | mGnssMeasurementCbIface_2_1 = nullptr; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | Return<void> GnssMeasurement::close() { |
| 118 | if (mApi == nullptr) { |
| 119 | LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__); |
| 120 | return Void(); |
| 121 | } |
| 122 | |
| 123 | clearInterfaces(); |
| 124 | mApi->measurementClose(); |
| 125 | |
| 126 | return Void(); |
| 127 | } |
| 128 | |
| 129 | // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow. |
| 130 | Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1( |
| 131 | const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) { |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 132 | return setCallback(callback, mGnssMeasurementCbIface_1_1, |
| 133 | enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2); |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 134 | } |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 135 | |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 136 | // Methods from ::android::hardware::gnss::V2_0::IGnssMeasurement follow. |
| 137 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0( |
| 138 | const sp<V2_0::IGnssMeasurementCallback>& callback, |
| 139 | bool enableFullTracking) { |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 140 | return setCallback(callback, mGnssMeasurementCbIface_2_0, |
| 141 | enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2); |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Methods from ::android::hardware::gnss::V2_1::IGnssMeasurement follow. |
| 145 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1( |
| 146 | const sp<::android::hardware::gnss::V2_1::IGnssMeasurementCallback>& callback, |
| 147 | bool enableFullTracking) { |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 148 | return setCallback(callback, mGnssMeasurementCbIface_2_1, |
| 149 | enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2); |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace implementation |
| 153 | } // namespace V2_1 |
| 154 | } // namespace gnss |
| 155 | } // namespace hardware |
| 156 | } // namespace android |