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_AGnssInterface" |
| 22 | |
| 23 | #include <log_util.h> |
| 24 | #include "Gnss.h" |
| 25 | #include "AGnss.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace gnss { |
| 30 | namespace V1_1 { |
| 31 | namespace implementation { |
| 32 | |
| 33 | static AGnss* spAGnss = nullptr; |
| 34 | |
| 35 | AGnss::AGnss(Gnss* gnss) : mGnss(gnss) { |
| 36 | spAGnss = this; |
| 37 | } |
| 38 | |
| 39 | AGnss::~AGnss() { |
| 40 | spAGnss = nullptr; |
| 41 | } |
| 42 | |
| 43 | void AGnss::agnssStatusIpV4Cb(AGnssExtStatusIpV4 status){ |
| 44 | if (nullptr != spAGnss) { |
| 45 | spAGnss->statusIpV4Cb(status); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void AGnss::statusIpV4Cb(AGnssExtStatusIpV4 status) { |
| 50 | IAGnssCallback::AGnssStatusIpV4 st = {}; |
| 51 | |
| 52 | switch (status.type) { |
| 53 | case LOC_AGPS_TYPE_SUPL: |
| 54 | st.type = IAGnssCallback::AGnssType::TYPE_SUPL; |
| 55 | break; |
| 56 | case LOC_AGPS_TYPE_C2K: |
| 57 | st.type = IAGnssCallback::AGnssType::TYPE_C2K; |
| 58 | break; |
| 59 | default: |
| 60 | LOC_LOGE("invalid type: %d", status.type); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | switch (status.status) { |
| 65 | case LOC_GPS_REQUEST_AGPS_DATA_CONN: |
| 66 | st.status = IAGnssCallback::AGnssStatusValue::REQUEST_AGNSS_DATA_CONN; |
| 67 | break; |
| 68 | case LOC_GPS_RELEASE_AGPS_DATA_CONN: |
| 69 | st.status = IAGnssCallback::AGnssStatusValue::RELEASE_AGNSS_DATA_CONN; |
| 70 | break; |
| 71 | case LOC_GPS_AGPS_DATA_CONNECTED: |
| 72 | st.status = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONNECTED; |
| 73 | break; |
| 74 | case LOC_GPS_AGPS_DATA_CONN_DONE: |
| 75 | st.status = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONN_DONE; |
| 76 | break; |
| 77 | case LOC_GPS_AGPS_DATA_CONN_FAILED: |
| 78 | st.status = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONN_FAILED; |
| 79 | break; |
| 80 | default: |
| 81 | LOC_LOGE("invalid status: %d", status.status); |
| 82 | return; |
| 83 | } |
| 84 | st.ipV4Addr = status.ipV4Addr; |
| 85 | |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 86 | mMutex.lock(); |
| 87 | auto aGnssCbIface = mAGnssCbIface; |
| 88 | mMutex.unlock(); |
| 89 | if (aGnssCbIface != nullptr) { |
| 90 | auto r = aGnssCbIface->agnssStatusIpV4Cb(st); |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 91 | if (!r.isOk()) { |
| 92 | LOC_LOGw("Error invoking AGNSS status cb %s", r.description().c_str()); |
| 93 | } |
| 94 | } else { |
| 95 | LOC_LOGw("setCallback has not been called yet"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | Return<void> AGnss::setCallback(const sp<IAGnssCallback>& callback) { |
| 100 | |
| 101 | if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){ |
| 102 | LOC_LOGE("Null GNSS interface"); |
| 103 | return Void(); |
| 104 | } |
| 105 | |
| 106 | // Save the interface |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 107 | mMutex.lock(); |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 108 | mAGnssCbIface = callback; |
Michael Bestas | 222c86c | 2024-07-18 12:36:55 -0400 | [diff] [blame^] | 109 | mMutex.unlock(); |
Michael Bestas | 3a0209e | 2023-05-04 01:15:47 +0300 | [diff] [blame] | 110 | |
| 111 | AgpsCbInfo cbInfo = {}; |
| 112 | cbInfo.statusV4Cb = (void*)agnssStatusIpV4Cb; |
| 113 | cbInfo.atlType = AGPS_ATL_TYPE_SUPL | AGPS_ATL_TYPE_SUPL_ES; |
| 114 | |
| 115 | mGnss->getGnssInterface()->agpsInit(cbInfo); |
| 116 | return Void(); |
| 117 | } |
| 118 | |
| 119 | Return<bool> AGnss::dataConnClosed() { |
| 120 | |
| 121 | if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){ |
| 122 | LOC_LOGE("Null GNSS interface"); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | mGnss->getGnssInterface()->agpsDataConnClosed(LOC_AGPS_TYPE_SUPL); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | Return<bool> AGnss::dataConnFailed() { |
| 131 | |
| 132 | if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){ |
| 133 | LOC_LOGE("Null GNSS interface"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | mGnss->getGnssInterface()->agpsDataConnFailed(LOC_AGPS_TYPE_SUPL); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | Return<bool> AGnss::dataConnOpen(const hidl_string& apn, |
| 142 | IAGnss::ApnIpType apnIpType) { |
| 143 | |
| 144 | if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){ |
| 145 | LOC_LOGE("Null GNSS interface"); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | /* Validate */ |
| 150 | if(apn.empty()){ |
| 151 | LOC_LOGE("Invalid APN"); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | LOC_LOGD("dataConnOpen APN name = [%s]", apn.c_str()); |
| 156 | |
| 157 | AGpsBearerType bearerType; |
| 158 | switch (apnIpType) { |
| 159 | case IAGnss::ApnIpType::IPV4: |
| 160 | bearerType = AGPS_APN_BEARER_IPV4; |
| 161 | break; |
| 162 | case IAGnss::ApnIpType::IPV6: |
| 163 | bearerType = AGPS_APN_BEARER_IPV6; |
| 164 | break; |
| 165 | case IAGnss::ApnIpType::IPV4V6: |
| 166 | bearerType = AGPS_APN_BEARER_IPV4V6; |
| 167 | break; |
| 168 | default: |
| 169 | bearerType = AGPS_APN_BEARER_IPV4; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | mGnss->getGnssInterface()->agpsDataConnOpen( |
| 174 | LOC_AGPS_TYPE_SUPL, apn.c_str(), apn.size(), (int)bearerType); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | Return<bool> AGnss::setServer(IAGnssCallback::AGnssType type, |
| 179 | const hidl_string& hostname, |
| 180 | int32_t port) { |
| 181 | if (mGnss == nullptr) { |
| 182 | LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | GnssConfig config; |
| 187 | memset(&config, 0, sizeof(GnssConfig)); |
| 188 | config.size = sizeof(GnssConfig); |
| 189 | config.flags = GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT; |
| 190 | config.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer); |
| 191 | if (type == IAGnssCallback::AGnssType::TYPE_SUPL) { |
| 192 | config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_SUPL; |
| 193 | } else if (type == IAGnssCallback::AGnssType::TYPE_C2K) { |
| 194 | config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_C2K; |
| 195 | } else { |
| 196 | LOC_LOGE("%s]: invalid AGnssType: %d", __FUNCTION__, static_cast<int>(type)); |
| 197 | return false; |
| 198 | } |
| 199 | config.assistanceServer.hostName = strdup(hostname.c_str()); |
| 200 | config.assistanceServer.port = port; |
| 201 | return mGnss->updateConfiguration(config); |
| 202 | } |
| 203 | |
| 204 | } // namespace implementation |
| 205 | } // namespace V1_1 |
| 206 | } // namespace gnss |
| 207 | } // namespace hardware |
| 208 | } // namespace android |