1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ohos.geoLocationManager.proj.hpp"
17 #include "ohos.geoLocationManager.impl.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20 #include "common_utils.h"
21 #include "locator.h"
22 #include "constant_definition.h"
23 #include "geo_address.h"
24 #include "permission_manager.h"
25 #include "ipc_skeleton.h"
26 #include "request_config.h"
27 #include "location_callback_taihe.h"
28 #include "location_switch_callback_taihe.h"
29 #include "country_code_callback_taihe.h"
30 #include "gnss_status_callback_taihe.h"
31 #include "locating_required_data_callback_taihe.h"
32 #include "nmea_message_callback_taihe.h"
33 #include "location_error_callback_taihe.h"
34 #include "bluetooth_scan_result_callback_taihe.h"
35 #include "util.h"
36
37 namespace {
38 // To be implemented.
39 using namespace OHOS::Location;
40 std::vector<OHOS::sptr<LocatorCallbackTaihe>> g_taiheLocationCallbackMap;
41 std::vector<OHOS::sptr<LocatingRequiredDataCallbackTaihe>> g_taiheLocatingRequiredDataCallbackMap;
42 std::vector<OHOS::sptr<CountryCodeCallbackTaihe>> g_taiheCountryCodeCallbackMap;
43 std::vector<OHOS::sptr<NmeaMessageCallbackTaihe>> g_taiheNmeaMessageCallbackMap;
44 std::vector<OHOS::sptr<GnssStatusCallbackTaihe>> g_taiheGnssStatusCallbackMap;
45 std::vector<OHOS::sptr<LocationSwitchCallbackTaihe>> g_taiheLocationSwitchCallbackMap;
46 std::vector<OHOS::sptr<BluetoothScanResultCallbackTaihe>> g_taiheBluetoothScanResultCallbackMap;
47 std::vector<OHOS::sptr<LocationErrorCallbackTaihe>> g_taiheLocationErrorCallbackMap;
48 static constexpr int LASTLOCATION_CACHED_TIME = 10 * 60;
49
GetCurrentLocationType(std::unique_ptr<OHOS::Location::RequestConfig> & config)50 int GetCurrentLocationType(std::unique_ptr<OHOS::Location::RequestConfig>& config)
51 {
52 if (config->GetPriority() == OHOS::Location::LOCATION_PRIORITY_ACCURACY ||
53 (config->GetScenario() == OHOS::Location::SCENE_UNSET &&
54 config->GetPriority() == OHOS::Location::PRIORITY_ACCURACY) ||
55 config->GetScenario() == OHOS::Location::SCENE_NAVIGATION ||
56 config->GetScenario() == OHOS::Location::SCENE_TRAJECTORY_TRACKING ||
57 config->GetScenario() == OHOS::Location::SCENE_CAR_HAILING) {
58 return OHOS::Location::LOCATION_PRIORITY_ACCURACY;
59 } else {
60 return OHOS::Location::LOCATION_PRIORITY_LOCATING_SPEED;
61 }
62 }
63
CreateSingleLocationCallbackHost()64 OHOS::sptr<LocatorCallbackTaihe> CreateSingleLocationCallbackHost()
65 {
66 auto singleLocatorCallbackHost =
67 OHOS::sptr<LocatorCallbackTaihe>(new LocatorCallbackTaihe());
68 singleLocatorCallbackHost->SetFixNumber(1);
69 return singleLocatorCallbackHost;
70 }
71
IsRequestConfigValid(std::unique_ptr<OHOS::Location::RequestConfig> & config)72 bool IsRequestConfigValid(std::unique_ptr<OHOS::Location::RequestConfig>& config)
73 {
74 if (config == nullptr) {
75 return false;
76 }
77 if ((config->GetScenario() > OHOS::Location::SCENE_NO_POWER ||
78 config->GetScenario() < OHOS::Location::SCENE_UNSET) &&
79 (config->GetScenario() > OHOS::Location::LOCATION_SCENE_RIDE ||
80 config->GetScenario() < OHOS::Location::LOCATION_SCENE_NAVIGATION) &&
81 (config->GetScenario() > OHOS::Location::LOCATION_SCENE_NO_POWER_CONSUMPTION ||
82 config->GetScenario() < OHOS::Location::LOCATION_SCENE_HIGH_POWER_CONSUMPTION)) {
83 return false;
84 }
85 if ((config->GetPriority() > OHOS::Location::PRIORITY_FAST_FIRST_FIX ||
86 config->GetPriority() < OHOS::Location::PRIORITY_UNSET) &&
87 (config->GetPriority() > OHOS::Location::LOCATION_PRIORITY_LOCATING_SPEED ||
88 config->GetPriority() < OHOS::Location::LOCATION_PRIORITY_ACCURACY)) {
89 return false;
90 }
91 if (config->GetTimeOut() < 1) {
92 return false;
93 }
94 if (config->GetTimeInterval() < 0) {
95 return false;
96 }
97 if (config->GetDistanceInterval() < 0) {
98 return false;
99 }
100 if (config->GetMaxAccuracy() < 0) {
101 return false;
102 }
103 return true;
104 }
105
IsLocationEnabled()106 bool IsLocationEnabled()
107 {
108 bool isEnabled = false;
109 LocationErrCode errorCode = Locator::GetInstance()->IsLocationEnabledV9(isEnabled);
110 if (errorCode != ERRCODE_SUCCESS) {
111 TH_THROW(std::runtime_error, "isLocationEnabled not implemented");
112 }
113 return isEnabled;
114 }
115
GetAddressesFromLocationSync(::ohos::geoLocationManager::ReverseGeoCodeRequest const & request)116 ::taihe::array<::ohos::geoLocationManager::GeoAddress> GetAddressesFromLocationSync(
117 ::ohos::geoLocationManager::ReverseGeoCodeRequest const& request)
118 {
119 bool isAvailable = false;
120 LocationErrCode errorCode = Locator::GetInstance()->IsGeoServiceAvailableV9(isAvailable);
121 if (errorCode != ERRCODE_SUCCESS) {
122 return {};
123 }
124 if (!isAvailable) {
125 return {};
126 }
127 OHOS::MessageParcel dataParcel;
128 if (!dataParcel.WriteInterfaceToken(LocatorProxy::GetDescriptor())) {
129 LBSLOGE(COUNTRY_CODE, "write interfaceToken fail!");
130 return {};
131 }
132 if (request.locale) {
133 dataParcel.WriteString16(OHOS::Str8ToStr16(std::string(*request.locale))); // locale
134 } else {
135 dataParcel.WriteString16(OHOS::Str8ToStr16("")); // locale
136 }
137 dataParcel.WriteDouble(request.latitude); // latitude
138 dataParcel.WriteDouble(request.longitude); // longitude
139 if (request.maxItems) {
140 dataParcel.WriteInt32(request.maxItems); // maxItems
141 } else {
142 dataParcel.WriteInt32(1); // maxItems
143 }
144 dataParcel.WriteString16(OHOS::Str8ToStr16(CommonUtils::GenerateUuid())); // transId
145 if (request.country) {
146 dataParcel.WriteString16(OHOS::Str8ToStr16(std::string(*request.country))); // country
147 } else {
148 dataParcel.WriteString16(OHOS::Str8ToStr16("")); // locale
149 }
150 std::list<std::shared_ptr<GeoAddress>> replyList;
151 errorCode = Locator::GetInstance()->GetAddressByCoordinateV9(dataParcel, replyList);
152 if (replyList.empty() || errorCode != ERRCODE_SUCCESS) {
153 return {};
154 }
155 std::vector<::ohos::geoLocationManager::GeoAddress> geoAddressList;
156 Util::GeoAddressToTaihe(geoAddressList, replyList);
157 return ::taihe::array<::ohos::geoLocationManager::GeoAddress>{taihe::copy_data_t{},
158 geoAddressList.data(), geoAddressList.size()};
159 }
160
GetCurrentLocationSync(::taihe::optional_view<::ohos::geoLocationManager::CurrentRequest> request)161 ::ohos::geoLocationManager::Location GetCurrentLocationSync(
162 ::taihe::optional_view<::ohos::geoLocationManager::CurrentRequest> request)
163 {
164 ::taihe::map<::taihe::string, ::taihe::string> ret;
165 ::ohos::geoLocationManager::Location result = {
166 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0, {"", ""}, ret, 0, true, 0.0, 0.0, 0.0, 0,
167 static_cast<::ohos::geoLocationManager::LocationSourceType::key_t>(1) };
168 // request to capi request
169 auto requestConfig = std::make_unique<OHOS::Location::RequestConfig>();
170 Util::TaiheCurrentRequestObjToRequestConfig(request, requestConfig);
171 // receive callback
172 requestConfig->SetFixNumber(1);
173 if (!IsRequestConfigValid(requestConfig)) {
174 Util::ThrowBussinessError(LocationErrCode::ERRCODE_INVALID_PARAM);
175 }
176 auto singleLocatorCallbackHost = CreateSingleLocationCallbackHost();
177 if (singleLocatorCallbackHost == nullptr) {
178 Util::ThrowBussinessError(LocationErrCode::ERRCODE_SERVICE_UNAVAILABLE);
179 }
180 singleLocatorCallbackHost->SetLocationPriority(GetCurrentLocationType(requestConfig));
181
182 auto callbackPtr = OHOS::sptr<OHOS::Location::ILocatorCallback>(singleLocatorCallbackHost);
183 OHOS::Location::LocationErrCode errorCode = Locator::GetInstance()->StartLocatingV9(requestConfig, callbackPtr);
184 if (errorCode != OHOS::Location::SUCCESS) {
185 singleLocatorCallbackHost->SetCount(0);
186 }
187 if (requestConfig->GetTimeOut() > OHOS::Location::DEFAULT_TIMEOUT_30S) {
188 singleLocatorCallbackHost->Wait(OHOS::Location::DEFAULT_TIMEOUT_30S);
189 if (singleLocatorCallbackHost->GetSingleLocation() == nullptr) {
190 singleLocatorCallbackHost->Wait(requestConfig->GetTimeOut() - OHOS::Location::DEFAULT_TIMEOUT_30S);
191 }
192 } else {
193 singleLocatorCallbackHost->Wait(requestConfig->GetTimeOut());
194 }
195 Locator::GetInstance()->StopLocatingV9(callbackPtr);
196 if (singleLocatorCallbackHost->GetCount() != 0 && singleLocatorCallbackHost->GetSingleLocation() == nullptr) {
197 std::unique_ptr<OHOS::Location::Location> location = nullptr;
198 Locator::GetInstance()->GetCachedLocationV9(location);
199 int64_t curTime = OHOS::Location::CommonUtils::GetCurrentTimeStamp();
200 if (location != nullptr &&
201 (curTime - location->GetTimeStamp() / OHOS::Location::MILLI_PER_SEC) <= LASTLOCATION_CACHED_TIME) {
202 singleLocatorCallbackHost->SetSingleLocation(location);
203 }
204 }
205 if (singleLocatorCallbackHost != nullptr && singleLocatorCallbackHost->GetSingleLocation() != nullptr) {
206 std::unique_ptr<OHOS::Location::Location> loc =
207 std::make_unique<OHOS::Location::Location>(*singleLocatorCallbackHost->GetSingleLocation());
208 Util::LocationToTaihe(result, loc);
209 } else {
210 Util::ThrowBussinessError(errorCode);
211 }
212 return result;
213 }
214
EnableLocationSync()215 void EnableLocationSync()
216 {
217 LocationErrCode errorCode = Locator::GetInstance()->EnableAbilityV9(true);
218 if (errorCode != ERRCODE_SUCCESS) {
219 Util::ThrowBussinessError(errorCode);
220 }
221 }
222
DisableLocation()223 void DisableLocation()
224 {
225 LocationErrCode errorCode = Locator::GetInstance()->EnableAbilityV9(false);
226 if (errorCode != ERRCODE_SUCCESS) {
227 Util::ThrowBussinessError(errorCode);
228 }
229 }
230
IsLocationPrivacyConfirmed(::ohos::geoLocationManager::LocationPrivacyType type)231 bool IsLocationPrivacyConfirmed(::ohos::geoLocationManager::LocationPrivacyType type)
232 {
233 bool IsLocationPrivacyConfirmed = false;
234 LocationErrCode errorCode = Locator::GetInstance()->IsLocationPrivacyConfirmedV9(type, IsLocationPrivacyConfirmed);
235 if (errorCode != ERRCODE_SUCCESS) {
236 Util::ThrowBussinessError(errorCode);
237 }
238 return IsLocationPrivacyConfirmed;
239 }
240
GetLastLocation()241 ::ohos::geoLocationManager::Location GetLastLocation()
242 {
243 ::taihe::map<::taihe::string, ::taihe::string> ret;
244 ::ohos::geoLocationManager::Location location = {
245 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0, {"", ""}, ret, 0, true, 0.0, 0.0, 0.0, 0,
246 static_cast<::ohos::geoLocationManager::LocationSourceType::key_t>(1) };
247 std::unique_ptr<Location> loc;
248 LocationErrCode errorCode = Locator::GetInstance()->GetCachedLocationV9(loc);
249 if (errorCode != ERRCODE_SUCCESS) {
250 Util::ThrowBussinessError(errorCode);
251 }
252 if (loc != nullptr) {
253 Util::LocationToTaihe(location, loc);
254 }
255 return location;
256 }
257
OnCachedGnssLocationsChange(::ohos::geoLocationManager::CachedGnssLocationsRequest const & request,::taihe::callback_view<void (::taihe::array_view<::ohos::geoLocationManager::Location>)> callback)258 void OnCachedGnssLocationsChange(::ohos::geoLocationManager::CachedGnssLocationsRequest const& request,
259 ::taihe::callback_view<void(::taihe::array_view<::ohos::geoLocationManager::Location>)> callback)
260 {
261 Util::ThrowBussinessError(LocationErrCode::ERRCODE_NOT_SUPPORTED);
262 }
263
OffCachedGnssLocationsChange(::taihe::optional_view<::taihe::callback<void (::taihe::array_view<::ohos::geoLocationManager::Location>)>> callback)264 void OffCachedGnssLocationsChange(
265 ::taihe::optional_view<::taihe::callback<void(::taihe::array_view<::ohos::geoLocationManager::Location>)>>
266 callback)
267 {
268 Util::ThrowBussinessError(LocationErrCode::ERRCODE_NOT_SUPPORTED);
269 }
270
OnLocationChange(::ohos::geoLocationManager::OnRequest const & request,::taihe::callback_view<void (::ohos::geoLocationManager::Location const &)> callback)271 void OnLocationChange(::ohos::geoLocationManager::OnRequest const& request,
272 ::taihe::callback_view<void(::ohos::geoLocationManager::Location const&)> callback)
273 {
274 auto requestConfig = std::make_unique<OHOS::Location::RequestConfig>();
275 Util::TaiheCurrentRequestObjToRequestConfig(request, requestConfig);
276 auto locatorCallbackTaihe = OHOS::sptr<LocatorCallbackTaihe>(new LocatorCallbackTaihe());
277 locatorCallbackTaihe->callback_ =
278 ::taihe::optional<taihe::callback<void(::ohos::geoLocationManager::Location const&)>>{std::in_place_t{},
279 callback};
280 g_taiheLocationCallbackMap.push_back(locatorCallbackTaihe);
281 auto locatorCallback = OHOS::sptr<ILocatorCallback>(locatorCallbackTaihe);
282 LocationErrCode errorCode = Locator::GetInstance()->StartLocatingV9(requestConfig, locatorCallback);
283 if (errorCode != ERRCODE_SUCCESS) {
284 Util::ThrowBussinessError(errorCode);
285 }
286 }
287
OffLocationChange(::taihe::optional_view<::taihe::callback<void (::ohos::geoLocationManager::Location const &)>> callback)288 void OffLocationChange(
289 ::taihe::optional_view<::taihe::callback<void(::ohos::geoLocationManager::Location const&)>> callback)
290 {
291 for (std::uint32_t i = 0; i < g_taiheLocationCallbackMap.size(); i++) {
292 auto locatorCallbackTaihe = g_taiheLocationCallbackMap[i];
293 if (locatorCallbackTaihe->callback_ == callback) {
294 auto locatorCallback = OHOS::sptr<ILocatorCallback>(locatorCallbackTaihe);
295 LocationErrCode errorCode = Locator::GetInstance()->StopLocatingV9(locatorCallback);
296 if (errorCode != ERRCODE_SUCCESS) {
297 Util::ThrowBussinessError(errorCode);
298 }
299 }
300 g_taiheLocationCallbackMap.erase(g_taiheLocationCallbackMap.begin() + i);
301 break;
302 }
303 }
304
OnCountryCodeChange(::taihe::callback_view<void (::ohos::geoLocationManager::CountryCode const &)> callback)305 void OnCountryCodeChange(::taihe::callback_view<void(::ohos::geoLocationManager::CountryCode const&)> callback)
306 {
307 auto countryCodeCallbackTaihe = OHOS::sptr<CountryCodeCallbackTaihe>(new CountryCodeCallbackTaihe());
308 countryCodeCallbackTaihe->callback_ =
309 ::taihe::optional<taihe::callback<void(::ohos::geoLocationManager::CountryCode const&)>>{std::in_place_t{},
310 callback};
311 g_taiheCountryCodeCallbackMap.push_back(countryCodeCallbackTaihe);
312 LocationErrCode errorCode =
313 Locator::GetInstance()->RegisterCountryCodeCallbackV9(countryCodeCallbackTaihe->AsObject());
314 if (errorCode != ERRCODE_SUCCESS) {
315 Util::ThrowBussinessError(errorCode);
316 }
317 }
318
OffCountryCodeChange(::taihe::optional_view<::taihe::callback<void (::ohos::geoLocationManager::CountryCode const &)>> callback)319 void OffCountryCodeChange(
320 ::taihe::optional_view<::taihe::callback<void(::ohos::geoLocationManager::CountryCode const&)>> callback)
321 {
322 for (std::uint32_t i = 0; i < g_taiheCountryCodeCallbackMap.size(); i++) {
323 auto countryCallbackTaihe = g_taiheCountryCodeCallbackMap[i];
324 if (countryCallbackTaihe->callback_ == callback) {
325 LocationErrCode errorCode =
326 Locator::GetInstance()->UnregisterCountryCodeCallbackV9(countryCallbackTaihe->AsObject());
327 if (errorCode != ERRCODE_SUCCESS) {
328 Util::ThrowBussinessError(errorCode);
329 }
330 }
331 g_taiheCountryCodeCallbackMap.erase(g_taiheCountryCodeCallbackMap.begin() + i);
332 break;
333 }
334 }
335
OffNmeaMessage(::taihe::optional_view<::taihe::callback<void (::taihe::string_view)>> callback)336 void OffNmeaMessage(::taihe::optional_view<::taihe::callback<void(::taihe::string_view)>> callback)
337 {
338 for (std::uint32_t i = 0; i < g_taiheNmeaMessageCallbackMap.size(); i++) {
339 auto nmeaCallbackTaihe = g_taiheNmeaMessageCallbackMap[i];
340 if (nmeaCallbackTaihe->callback_ == callback) {
341 LocationErrCode errorCode =
342 Locator::GetInstance()->UnregisterNmeaMessageCallbackV9(nmeaCallbackTaihe->AsObject());
343 if (errorCode != ERRCODE_SUCCESS) {
344 Util::ThrowBussinessError(errorCode);
345 }
346 }
347 g_taiheNmeaMessageCallbackMap.erase(g_taiheNmeaMessageCallbackMap.begin() + i);
348 break;
349 }
350 }
351
OnNmeaMessage(::taihe::callback_view<void (::taihe::string_view)> callback)352 void OnNmeaMessage(::taihe::callback_view<void(::taihe::string_view)> callback)
353 {
354 auto nmeaMessageCallbackTaihe = OHOS::sptr<NmeaMessageCallbackTaihe>(new NmeaMessageCallbackTaihe());
355 nmeaMessageCallbackTaihe->callback_ =
356 ::taihe::optional<::taihe::callback<void(::taihe::string_view)>>{std::in_place_t{}, callback};
357 g_taiheNmeaMessageCallbackMap.push_back(nmeaMessageCallbackTaihe);
358 LocationErrCode errorCode =
359 Locator::GetInstance()->RegisterNmeaMessageCallbackV9(nmeaMessageCallbackTaihe->AsObject());
360 if (errorCode != ERRCODE_SUCCESS) {
361 Util::ThrowBussinessError(errorCode);
362 }
363 }
364
OffSatelliteStatusChange(::taihe::optional_view<::taihe::callback<void (::ohos::geoLocationManager::SatelliteStatusInfo const &)>> callback)365 void OffSatelliteStatusChange(
366 ::taihe::optional_view<::taihe::callback<void(::ohos::geoLocationManager::SatelliteStatusInfo const&)>> callback)
367 {
368 for (std::uint32_t i = 0; i < g_taiheGnssStatusCallbackMap.size(); i++) {
369 auto satelliteStatusTaihe = g_taiheGnssStatusCallbackMap[i];
370 if (satelliteStatusTaihe->callback_ == callback) {
371 LocationErrCode errorCode =
372 Locator::GetInstance()->UnregisterGnssStatusCallbackV9(satelliteStatusTaihe->AsObject());
373 if (errorCode != ERRCODE_SUCCESS) {
374 Util::ThrowBussinessError(errorCode);
375 }
376 }
377 g_taiheGnssStatusCallbackMap.erase(g_taiheGnssStatusCallbackMap.begin() + i);
378 break;
379 }
380 }
381
OnSatelliteStatusChange(::taihe::callback_view<void (::ohos::geoLocationManager::SatelliteStatusInfo const &)> callback)382 void OnSatelliteStatusChange(
383 ::taihe::callback_view<void(::ohos::geoLocationManager::SatelliteStatusInfo const&)> callback)
384 {
385 auto gnssStatusCallbackTaihe = OHOS::sptr<GnssStatusCallbackTaihe>(new GnssStatusCallbackTaihe());
386 gnssStatusCallbackTaihe->callback_ =
387 ::taihe::optional<::taihe::callback<void(::ohos::geoLocationManager::SatelliteStatusInfo const&)>>{
388 std::in_place_t{},
389 callback};
390 g_taiheGnssStatusCallbackMap.push_back(gnssStatusCallbackTaihe);
391 LocationErrCode errorCode =
392 Locator::GetInstance()->RegisterGnssStatusCallbackV9(gnssStatusCallbackTaihe->AsObject());
393 if (errorCode != ERRCODE_SUCCESS) {
394 Util::ThrowBussinessError(errorCode);
395 }
396 }
397
OffLocationEnabledChange(::taihe::optional_view<::taihe::callback<void (bool)>> callback)398 void OffLocationEnabledChange(::taihe::optional_view<::taihe::callback<void(bool)>> callback)
399 {
400 for (std::uint32_t i = 0; i < g_taiheLocationSwitchCallbackMap.size(); i++) {
401 auto locationEnabledCallbackTaihe = g_taiheLocationSwitchCallbackMap[i];
402 if (locationEnabledCallbackTaihe->callback_ == callback) {
403 LocationErrCode errorCode =
404 Locator::GetInstance()->UnregisterSwitchCallbackV9(locationEnabledCallbackTaihe->AsObject());
405 if (errorCode != ERRCODE_SUCCESS) {
406 Util::ThrowBussinessError(errorCode);
407 }
408 }
409 g_taiheLocationSwitchCallbackMap.erase(g_taiheLocationSwitchCallbackMap.begin() + i);
410 break;
411 }
412 }
413
OnLocationEnabledChange(::taihe::callback_view<void (bool)> callback)414 void OnLocationEnabledChange(::taihe::callback_view<void(bool)> callback)
415 {
416 auto switchCallbackTaihe = OHOS::sptr<LocationSwitchCallbackTaihe>(new LocationSwitchCallbackTaihe());
417 switchCallbackTaihe->callback_ = ::taihe::optional<::taihe::callback<void(bool)>>{std::in_place_t{}, callback};
418 g_taiheLocationSwitchCallbackMap.push_back(switchCallbackTaihe);
419 LocationErrCode errorCode = Locator::GetInstance()->RegisterSwitchCallbackV9(switchCallbackTaihe->AsObject());
420 if (errorCode != ERRCODE_SUCCESS) {
421 Util::ThrowBussinessError(errorCode);
422 }
423 }
424
OffBluetoothScanResultChange(::taihe::optional_view<::taihe::callback<void (::ohos::geoLocationManager::BluetoothScanResult const &)>> callback)425 void OffBluetoothScanResultChange(
426 ::taihe::optional_view<::taihe::callback<void(::ohos::geoLocationManager::BluetoothScanResult const&)>> callback)
427 {
428 for (std::uint32_t i = 0; i < g_taiheBluetoothScanResultCallbackMap.size(); i++) {
429 auto bluetoothScanResultCallbackTaihe = g_taiheBluetoothScanResultCallbackMap[i];
430 if (bluetoothScanResultCallbackTaihe->callback_ == callback) {
431 auto bluetoothScanResultCallback =
432 OHOS::sptr<IBluetoothScanResultCallback>(bluetoothScanResultCallbackTaihe);
433 LocationErrCode errorCode =
434 Locator::GetInstance()->UnSubscribeBluetoothScanResultChange(bluetoothScanResultCallback);
435 if (errorCode != ERRCODE_SUCCESS) {
436 Util::ThrowBussinessError(errorCode);
437 }
438 }
439 g_taiheBluetoothScanResultCallbackMap.erase(g_taiheBluetoothScanResultCallbackMap.begin() + i);
440 break;
441 }
442 }
443
OnBluetoothScanResultChange(::taihe::callback_view<void (::ohos::geoLocationManager::BluetoothScanResult const &)> callback)444 void OnBluetoothScanResultChange(
445 ::taihe::callback_view<void(::ohos::geoLocationManager::BluetoothScanResult const&)> callback)
446 {
447 auto bluetoothScanResultCallbackTaihe =
448 OHOS::sptr<BluetoothScanResultCallbackTaihe>(new BluetoothScanResultCallbackTaihe());
449 bluetoothScanResultCallbackTaihe->callback_ =
450 ::taihe::optional<::taihe::callback<void(::ohos::geoLocationManager::BluetoothScanResult const&)>>{
451 std::in_place_t{}, callback};
452 g_taiheBluetoothScanResultCallbackMap.push_back(bluetoothScanResultCallbackTaihe);
453 auto bluetoothScanResultCallback =
454 OHOS::sptr<IBluetoothScanResultCallback>(bluetoothScanResultCallbackTaihe);
455 LocationErrCode errorCode =
456 Locator::GetInstance()->SubscribeBluetoothScanResultChange(bluetoothScanResultCallback);
457 if (errorCode != ERRCODE_SUCCESS) {
458 Util::ThrowBussinessError(errorCode);
459 }
460 }
461
OffLocationError(::taihe::optional_view<::taihe::callback<void (::ohos::geoLocationManager::LocationError)>> callback)462 void OffLocationError(
463 ::taihe::optional_view<::taihe::callback<void(::ohos::geoLocationManager::LocationError)>> callback)
464 {
465 for (std::uint32_t i = 0; i < g_taiheLocationErrorCallbackMap.size(); i++) {
466 auto locationErrorCallbackTaihe = g_taiheLocationErrorCallbackMap[i];
467 if (locationErrorCallbackTaihe->callback_ == callback) {
468 auto locationErrorCallback = OHOS::sptr<ILocatorCallback>(locationErrorCallbackTaihe);
469 LocationErrCode errorCode =
470 Locator::GetInstance()->UnSubscribeLocationError(locationErrorCallback);
471 if (errorCode != ERRCODE_SUCCESS) {
472 Util::ThrowBussinessError(errorCode);
473 }
474 }
475 g_taiheLocationErrorCallbackMap.erase(g_taiheLocationErrorCallbackMap.begin() + i);
476 break;
477 }
478 }
479
OnLocationError(::taihe::callback_view<void (::ohos::geoLocationManager::LocationError)> callback)480 void OnLocationError(::taihe::callback_view<void(::ohos::geoLocationManager::LocationError)> callback)
481 {
482 auto locationErrorCallbackTaihe =
483 OHOS::sptr<LocationErrorCallbackTaihe>(new LocationErrorCallbackTaihe());
484 locationErrorCallbackTaihe->callback_ =
485 ::taihe::optional<::taihe::callback<void(::ohos::geoLocationManager::LocationError)>>{std::in_place_t{},
486 callback};
487 g_taiheLocationErrorCallbackMap.push_back(locationErrorCallbackTaihe);
488 auto locationErrorCallback = OHOS::sptr<ILocatorCallback>(locationErrorCallbackTaihe);
489 LocationErrCode errorCode = Locator::GetInstance()->SubscribeLocationError(locationErrorCallback);
490 if (errorCode != ERRCODE_SUCCESS) {
491 Util::ThrowBussinessError(errorCode);
492 }
493 }
494
OffLocationIconStatusChange(::taihe::optional_view<::taihe::callback<void (::ohos::geoLocationManager::LocationIconStatus)>> callback)495 void OffLocationIconStatusChange(
496 ::taihe::optional_view<::taihe::callback<void(::ohos::geoLocationManager::LocationIconStatus)>> callback)
497 {
498 Util::ThrowBussinessError(LocationErrCode::ERRCODE_NOT_SUPPORTED);
499 }
500
OnLocationIconStatusChange(::taihe::callback_view<void (::ohos::geoLocationManager::LocationIconStatus)> callback)501 void OnLocationIconStatusChange(
502 ::taihe::callback_view<void(::ohos::geoLocationManager::LocationIconStatus)> callback)
503 {
504 Util::ThrowBussinessError(LocationErrCode::ERRCODE_NOT_SUPPORTED);
505 }
506
OnLocatingRequiredDataChange(::ohos::geoLocationManager::LocatingRequiredDataConfig const & config,::taihe::callback_view<void (::taihe::array_view<::ohos::geoLocationManager::LocatingRequiredData>)> callback)507 void OnLocatingRequiredDataChange(
508 ::ohos::geoLocationManager::LocatingRequiredDataConfig const& config,
509 ::taihe::callback_view<void(::taihe::array_view<::ohos::geoLocationManager::LocatingRequiredData>)> callback)
510 {
511 std::unique_ptr<LocatingRequiredDataConfig> dataConfig = std::make_unique<LocatingRequiredDataConfig>();
512 dataConfig->SetType(config.type.get_value());
513 dataConfig->SetNeedStartScan(config.needStartScan);
514 if (config.scanInterval) {
515 dataConfig->SetScanIntervalMs(*config.scanInterval);
516 }
517 if (config.scanTimeout) {
518 dataConfig->SetScanTimeoutMs(*config.scanTimeout);
519 }
520 auto locatingRequiredDataCallbackTaihe =
521 OHOS::sptr<LocatingRequiredDataCallbackTaihe>(new LocatingRequiredDataCallbackTaihe());
522 locatingRequiredDataCallbackTaihe->callback_ =
523 ::taihe::optional<::taihe::callback<void(
524 ::taihe::array_view<::ohos::geoLocationManager::LocatingRequiredData>)>>{std::in_place_t{}, callback};
525 g_taiheLocatingRequiredDataCallbackMap.push_back(locatingRequiredDataCallbackTaihe);
526 auto locatingRequiredDataCallback =
527 OHOS::sptr<ILocatingRequiredDataCallback>(locatingRequiredDataCallbackTaihe);
528 LocationErrCode errorCode =
529 Locator::GetInstance()->RegisterLocatingRequiredDataCallback(dataConfig, locatingRequiredDataCallback);
530 if (errorCode != ERRCODE_SUCCESS) {
531 Util::ThrowBussinessError(errorCode);
532 }
533 }
534
OffLocatingRequiredDataChange(::taihe::optional_view<::taihe::callback<void (::taihe::array_view<::ohos::geoLocationManager::LocatingRequiredData>)>> callback)535 void OffLocatingRequiredDataChange(
536 ::taihe::optional_view<::taihe::callback<
537 void(::taihe::array_view<::ohos::geoLocationManager::LocatingRequiredData>)>> callback)
538 {
539 for (std::uint32_t i = 0; i < g_taiheLocatingRequiredDataCallbackMap.size(); i++) {
540 auto locatingRequiredDataCallbackTaihe = g_taiheLocatingRequiredDataCallbackMap[i];
541 if (locatingRequiredDataCallbackTaihe->callback_ == callback) {
542 auto locatingRequiredDataCallback =
543 OHOS::sptr<ILocatingRequiredDataCallback>(locatingRequiredDataCallbackTaihe);
544 LocationErrCode errorCode =
545 Locator::GetInstance()->UnRegisterLocatingRequiredDataCallback(locatingRequiredDataCallback);
546 if (errorCode != ERRCODE_SUCCESS) {
547 Util::ThrowBussinessError(errorCode);
548 }
549 }
550 g_taiheLocatingRequiredDataCallbackMap.erase(g_taiheLocatingRequiredDataCallbackMap.begin() + i);
551 break;
552 }
553 }
554
OnGnssFenceStatusChange(::ohos::geoLocationManager::GeofenceRequest const & request,uintptr_t want)555 void OnGnssFenceStatusChange(::ohos::geoLocationManager::GeofenceRequest const& request, uintptr_t want)
556 {
557 }
558
OffGnssFenceStatusChange(::ohos::geoLocationManager::GeofenceRequest const & request,uintptr_t want)559 void OffGnssFenceStatusChange(::ohos::geoLocationManager::GeofenceRequest const& request, uintptr_t want)
560 {
561 }
562 } // namespace
563
564 // Since these macros are auto-generate, lint will cause false positive.
565 // NOLINTBEGIN
566 TH_EXPORT_CPP_API_IsLocationEnabled(IsLocationEnabled);
567 TH_EXPORT_CPP_API_GetAddressesFromLocationSync(GetAddressesFromLocationSync);
568 TH_EXPORT_CPP_API_GetCurrentLocationSync(GetCurrentLocationSync);
569 TH_EXPORT_CPP_API_EnableLocationSync(EnableLocationSync);
570 TH_EXPORT_CPP_API_DisableLocation(DisableLocation);
571 TH_EXPORT_CPP_API_IsLocationPrivacyConfirmed(IsLocationPrivacyConfirmed);
572 TH_EXPORT_CPP_API_GetLastLocation(GetLastLocation);
573 TH_EXPORT_CPP_API_OnCachedGnssLocationsChange(OnCachedGnssLocationsChange);
574 TH_EXPORT_CPP_API_OffCachedGnssLocationsChange(OffCachedGnssLocationsChange);
575 TH_EXPORT_CPP_API_OnLocationChange(OnLocationChange);
576 TH_EXPORT_CPP_API_OffLocationChange(OffLocationChange);
577 TH_EXPORT_CPP_API_OnCountryCodeChange(OnCountryCodeChange);
578 TH_EXPORT_CPP_API_OffCountryCodeChange(OffCountryCodeChange);
579 TH_EXPORT_CPP_API_OffNmeaMessage(OffNmeaMessage);
580 TH_EXPORT_CPP_API_OnNmeaMessage(OnNmeaMessage);
581 TH_EXPORT_CPP_API_OffSatelliteStatusChange(OffSatelliteStatusChange);
582 TH_EXPORT_CPP_API_OnSatelliteStatusChange(OnSatelliteStatusChange);
583 TH_EXPORT_CPP_API_OffLocationEnabledChange(OffLocationEnabledChange);
584 TH_EXPORT_CPP_API_OnLocationEnabledChange(OnLocationEnabledChange);
585 TH_EXPORT_CPP_API_OffBluetoothScanResultChange(OffBluetoothScanResultChange);
586 TH_EXPORT_CPP_API_OnBluetoothScanResultChange(OnBluetoothScanResultChange);
587 TH_EXPORT_CPP_API_OffLocationError(OffLocationError);
588 TH_EXPORT_CPP_API_OnLocationError(OnLocationError);
589 TH_EXPORT_CPP_API_OffLocationIconStatusChange(OffLocationIconStatusChange);
590 TH_EXPORT_CPP_API_OnLocationIconStatusChange(OnLocationIconStatusChange);
591 TH_EXPORT_CPP_API_OnLocatingRequiredDataChange(OnLocatingRequiredDataChange);
592 TH_EXPORT_CPP_API_OffLocatingRequiredDataChange(OffLocatingRequiredDataChange);
593 TH_EXPORT_CPP_API_OnGnssFenceStatusChange(OnGnssFenceStatusChange);
594 TH_EXPORT_CPP_API_OffGnssFenceStatusChange(OffGnssFenceStatusChange);
595 // NOLINTEND
596