1 /*
2 * Copyright (C) 2022 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 "locator_ability.h"
17
18 #include "accesstoken_kit.h"
19 #include "event_runner.h"
20 #include "system_ability_definition.h"
21 #include "uri.h"
22
23 #include "common_event_manager.h"
24 #include "common_hisysevent.h"
25 #include "common_utils.h"
26 #include "constant_definition.h"
27 #ifdef FEATURE_GEOCODE_SUPPORT
28 #include "geo_convert_proxy.h"
29 #endif
30 #ifdef FEATURE_GNSS_SUPPORT
31 #include "gnss_ability_proxy.h"
32 #endif
33 #include "locator_background_proxy.h"
34 #include "location_config_manager.h"
35 #include "location_data_rdb_helper.h"
36 #include "location_log.h"
37 #include "location_sa_load_manager.h"
38 #include "locationhub_ipc_interface_code.h"
39 #include "locator_required_data_manager.h"
40 #ifdef FEATURE_NETWORK_SUPPORT
41 #include "network_ability_proxy.h"
42 #endif
43 #ifdef FEATURE_PASSIVE_SUPPORT
44 #include "passive_ability_proxy.h"
45 #endif
46 #include "permission_status_change_cb.h"
47
48 namespace OHOS {
49 namespace Location {
50 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
51 DelayedSingleton<LocatorAbility>::GetInstance().get());
52
53 const uint32_t EVENT_UPDATE_SA = 0x0001;
54 const uint32_t EVENT_INIT_REQUEST_MANAGER = 0x0002;
55 const uint32_t EVENT_APPLY_REQUIREMENTS = 0x0003;
56 const uint32_t EVENT_RETRY_REGISTER_ACTION = 0x0004;
57 const uint32_t EVENT_UNLOAD_SA = 0x0010;
58 const uint32_t RETRY_INTERVAL_UNITE = 1000;
59 const uint32_t RETRY_INTERVAL_OF_INIT_REQUEST_MANAGER = 5 * RETRY_INTERVAL_UNITE;
60 const uint32_t RETRY_INTERVAL_OF_UNLOAD_SA = 30 * RETRY_INTERVAL_UNITE;
61 const float_t PRECISION = 0.000001;
62 const std::string UNLOAD_TASK = "locatior_sa_unload";
63 const std::string WIFI_SCAN_STATE_CHANGE = "wifiScanStateChange";
64
LocatorAbility()65 LocatorAbility::LocatorAbility() : SystemAbility(LOCATION_LOCATOR_SA_ID, true)
66 {
67 switchCallbacks_ = std::make_unique<std::map<pid_t, sptr<ISwitchCallback>>>();
68 requests_ = std::make_shared<std::map<std::string, std::list<std::shared_ptr<Request>>>>();
69 receivers_ = std::make_shared<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>>();
70 proxyMap_ = std::make_shared<std::map<std::string, sptr<IRemoteObject>>>();
71 permissionMap_ = std::make_shared<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>>();
72 InitRequestManagerMap();
73 reportManager_ = DelayedSingleton<ReportManager>::GetInstance();
74 LBSLOGI(LOCATOR, "LocatorAbility constructed.");
75 }
76
~LocatorAbility()77 LocatorAbility::~LocatorAbility() {}
78
OnStart()79 void LocatorAbility::OnStart()
80 {
81 if (state_ == ServiceRunningState::STATE_RUNNING) {
82 LBSLOGI(LOCATOR, "LocatorAbility has already started.");
83 return;
84 }
85 if (!Init()) {
86 LBSLOGE(LOCATOR, "failed to init LocatorAbility");
87 OnStop();
88 return;
89 }
90 state_ = ServiceRunningState::STATE_RUNNING;
91 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
92 LBSLOGI(LOCATOR, "LocatorAbility::OnStart start ability success.");
93 }
94
OnStop()95 void LocatorAbility::OnStop()
96 {
97 state_ = ServiceRunningState::STATE_NOT_START;
98 registerToAbility_ = false;
99 LBSLOGI(LOCATOR, "LocatorAbility::OnStop ability stopped.");
100 }
101
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)102 void LocatorAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
103 {
104 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
105 LBSLOGE(LOCATOR, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
106 return;
107 }
108 if (locatorEventSubscriber_ == nullptr) {
109 LBSLOGE(LOCATOR, "OnAddSystemAbility subscribeer is nullptr");
110 return;
111 }
112 OHOS::EventFwk::MatchingSkills matchingSkills;
113 matchingSkills.AddEvent(MODE_CHANGED_EVENT);
114 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
115 bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(locatorEventSubscriber_);
116 LBSLOGI(LOCATOR, "SubscribeCommonEvent locatorEventSubscriber_ result = %{public}d", result);
117 }
118
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)119 void LocatorAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
120 {
121 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
122 LBSLOGE(LOCATOR, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
123 return;
124 }
125 if (locatorEventSubscriber_ == nullptr) {
126 LBSLOGE(LOCATOR, "OnRemoveSystemAbility subscribeer is nullptr");
127 return;
128 }
129 bool result = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(locatorEventSubscriber_);
130 LBSLOGI(LOCATOR, "UnSubscribeCommonEvent locatorEventSubscriber_ result = %{public}d", result);
131 }
132
Init()133 bool LocatorAbility::Init()
134 {
135 if (registerToAbility_) {
136 return true;
137 }
138 LBSLOGI(LOCATOR, "LocatorAbility Init.");
139 bool ret = Publish(AsObject());
140 if (!ret) {
141 LBSLOGE(LOCATOR, "Init add system ability failed!");
142 return false;
143 }
144
145 deviceId_ = CommonUtils::InitDeviceId();
146 requestManager_ = DelayedSingleton<RequestManager>::GetInstance();
147 locatorHandler_ = std::make_shared<LocatorHandler>(AppExecFwk::EventRunner::Create(true));
148 InitSaAbility();
149 if (locatorHandler_ != nullptr) {
150 locatorHandler_->SendHighPriorityEvent(EVENT_INIT_REQUEST_MANAGER, 0, RETRY_INTERVAL_OF_INIT_REQUEST_MANAGER);
151 }
152 RegisterAction();
153 registerToAbility_ = true;
154 return registerToAbility_;
155 }
156
LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)157 LocatorHandler::LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner) {}
158
~LocatorHandler()159 LocatorHandler::~LocatorHandler() {}
160
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)161 void LocatorHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
162 {
163 auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
164 auto requestManager = DelayedSingleton<RequestManager>::GetInstance();
165 auto locationSaLoadManager = DelayedSingleton<LocationSaLoadManager>::GetInstance();
166 if (locatorAbility == nullptr || requestManager == nullptr || locationSaLoadManager == nullptr) {
167 LBSLOGE(LOCATOR, "GetInstance return null");
168 return;
169 }
170 uint32_t eventId = event->GetInnerEventId();
171 LBSLOGI(LOCATOR, "ProcessEvent event:%{public}d", eventId);
172 switch (eventId) {
173 case EVENT_UPDATE_SA: {
174 if (locatorAbility != nullptr) {
175 locatorAbility->UpdateSaAbilityHandler();
176 }
177 break;
178 }
179 case EVENT_RETRY_REGISTER_ACTION: {
180 if (locatorAbility != nullptr) {
181 locatorAbility->RegisterAction();
182 }
183 break;
184 }
185 case EVENT_INIT_REQUEST_MANAGER: {
186 if (requestManager == nullptr || !requestManager->InitSystemListeners()) {
187 LBSLOGE(LOCATOR, "InitSystemListeners failed");
188 }
189 break;
190 }
191 case EVENT_APPLY_REQUIREMENTS: {
192 if (requestManager != nullptr) {
193 requestManager->HandleRequest();
194 }
195 break;
196 }
197 case EVENT_UNLOAD_SA: {
198 if (locatorAbility != nullptr) {
199 locationSaLoadManager->UnloadLocationSa(LOCATION_LOCATOR_SA_ID);
200 }
201 break;
202 }
203 default:
204 break;
205 }
206 }
207
InitRequestManagerMap()208 void LocatorAbility::InitRequestManagerMap()
209 {
210 std::unique_lock<std::mutex> lock(requestsMutex_);
211 if (requests_ != nullptr) {
212 #ifdef FEATURE_GNSS_SUPPORT
213 std::list<std::shared_ptr<Request>> gnssList;
214 requests_->insert(make_pair(GNSS_ABILITY, gnssList));
215 #endif
216 #ifdef FEATURE_NETWORK_SUPPORT
217 std::list<std::shared_ptr<Request>> networkList;
218 requests_->insert(make_pair(NETWORK_ABILITY, networkList));
219 #endif
220 #ifdef FEATURE_PASSIVE_SUPPORT
221 std::list<std::shared_ptr<Request>> passiveList;
222 requests_->insert(make_pair(PASSIVE_ABILITY, passiveList));
223 #endif
224 }
225 }
226
GetRequests()227 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> LocatorAbility::GetRequests()
228 {
229 std::unique_lock<std::mutex> lock(requestsMutex_);
230 return requests_;
231 }
232
GetActiveRequestNum()233 int LocatorAbility::GetActiveRequestNum()
234 {
235 std::unique_lock<std::mutex> lock(requestsMutex_);
236 int num = 0;
237 #ifdef FEATURE_GNSS_SUPPORT
238 auto gpsListIter = requests_->find(GNSS_ABILITY);
239 if (gpsListIter != requests_->end()) {
240 auto list = &(gpsListIter->second);
241 num += static_cast<int>(list->size());
242 }
243 #endif
244 #ifdef FEATURE_NETWORK_SUPPORT
245 auto networkListIter = requests_->find(NETWORK_ABILITY);
246 if (networkListIter != requests_->end()) {
247 auto list = &(networkListIter->second);
248 num += static_cast<int>(list->size());
249 }
250 #endif
251 return num;
252 }
253
GetReceivers()254 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> LocatorAbility::GetReceivers()
255 {
256 std::unique_lock<std::mutex> lock(receiversMutex_);
257 return receivers_;
258 }
259
GetProxyMap()260 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> LocatorAbility::GetProxyMap()
261 {
262 std::unique_lock<std::mutex> lock(proxyMapMutex_);
263 return proxyMap_;
264 }
265
ApplyRequests()266 void LocatorAbility::ApplyRequests()
267 {
268 if (locatorHandler_ != nullptr) {
269 locatorHandler_->SendHighPriorityEvent(EVENT_APPLY_REQUIREMENTS, 0, RETRY_INTERVAL_UNITE);
270 }
271 }
272
InitSaAbility()273 void LocatorAbility::InitSaAbility()
274 {
275 LBSLOGI(LOCATOR, "initSaAbility start");
276 if (proxyMap_ == nullptr) {
277 return;
278 }
279 UpdateSaAbilityHandler();
280 }
281
CheckSaValid()282 bool LocatorAbility::CheckSaValid()
283 {
284 std::unique_lock<std::mutex> lock(proxyMapMutex_);
285 #ifdef FEATURE_GNSS_SUPPORT
286 auto objectGnss = proxyMap_->find(GNSS_ABILITY);
287 if (objectGnss == proxyMap_->end()) {
288 LBSLOGI(LOCATOR, "gnss sa is null");
289 return false;
290 }
291 #endif
292 #ifdef FEATURE_NETWORK_SUPPORT
293 auto objectNetwork = proxyMap_->find(NETWORK_ABILITY);
294 if (objectNetwork == proxyMap_->end()) {
295 LBSLOGI(LOCATOR, "network sa is null");
296 return false;
297 }
298 #endif
299 #ifdef FEATURE_PASSIVE_SUPPORT
300 auto objectPassive = proxyMap_->find(PASSIVE_ABILITY);
301 if (objectPassive == proxyMap_->end()) {
302 LBSLOGI(LOCATOR, "passive sa is null");
303 return false;
304 }
305 #endif
306 return true;
307 }
308
UpdateSaAbility()309 LocationErrCode LocatorAbility::UpdateSaAbility()
310 {
311 auto event = AppExecFwk::InnerEvent::Get(EVENT_UPDATE_SA, 0);
312 if (locatorHandler_ != nullptr) {
313 locatorHandler_->SendHighPriorityEvent(event);
314 }
315 return ERRCODE_SUCCESS;
316 }
317
UpdateSaAbilityHandler()318 void LocatorAbility::UpdateSaAbilityHandler()
319 {
320 int state = QuerySwitchState();
321 LBSLOGI(LOCATOR, "update location subability enable state, switch state=%{public}d, action registered=%{public}d",
322 state, isActionRegistered);
323 auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
324 if (locatorBackgroundProxy == nullptr) {
325 LBSLOGE(LOCATOR, "UpdateSaAbilityHandler: LocatorBackgroundProxy is nullptr");
326 return;
327 }
328 locatorBackgroundProxy.get()->OnSaStateChange(state == ENABLED);
329 }
330
UnloadSaAbility()331 void LocatorAbility::UnloadSaAbility()
332 {
333 if (locatorHandler_ == nullptr) {
334 LBSLOGE(LOCATOR, "%{public}s locatorHandler is nullptr", __func__);
335 return;
336 }
337 locatorHandler_->RemoveTask(UNLOAD_TASK);
338 if (CheckIfLocatorConnecting()) {
339 return;
340 }
341 auto task = [this]() {
342 auto instance = DelayedSingleton<LocationSaLoadManager>::GetInstance();
343 if (instance == nullptr) {
344 LBSLOGE(LOCATOR, "%{public}s instance is nullptr", __func__);
345 return;
346 }
347 instance->UnloadLocationSa(LOCATION_LOCATOR_SA_ID);
348 };
349 if (locatorHandler_ != nullptr) {
350 locatorHandler_->PostTask(task, UNLOAD_TASK, RETRY_INTERVAL_OF_UNLOAD_SA);
351 }
352 }
353
CheckIfLocatorConnecting()354 bool LocatorAbility::CheckIfLocatorConnecting()
355 {
356 return DelayedSingleton<LocatorRequiredDataManager>::GetInstance()->IsConnecting();
357 }
358
EnableAbility(bool isEnabled)359 LocationErrCode LocatorAbility::EnableAbility(bool isEnabled)
360 {
361 LBSLOGI(LOCATOR, "EnableAbility %{public}d", isEnabled);
362 int modeValue = isEnabled ? 1 : 0;
363 if (modeValue == QuerySwitchState()) {
364 LBSLOGD(LOCATOR, "no need to set location ability, enable:%{public}d", modeValue);
365 return ERRCODE_SUCCESS;
366 }
367 Uri locationDataEnableUri(LOCATION_DATA_URI);
368 LocationErrCode errCode = DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
369 SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, modeValue);
370 if (errCode != ERRCODE_SUCCESS) {
371 LBSLOGE(LOCATOR, "%{public}s: can not set state to db", __func__);
372 return ERRCODE_SERVICE_UNAVAILABLE;
373 }
374 UpdateSaAbility();
375 std::string state = isEnabled ? "enable" : "disable";
376 WriteLocationSwitchStateEvent(state);
377 return ERRCODE_SUCCESS;
378 }
379
GetSwitchState(int & state)380 LocationErrCode LocatorAbility::GetSwitchState(int& state)
381 {
382 state = QuerySwitchState();
383 return ERRCODE_SUCCESS;
384 }
385
QuerySwitchState()386 int LocatorAbility::QuerySwitchState()
387 {
388 int32_t state = DISABLED;
389 Uri locationDataEnableUri(LOCATION_DATA_URI);
390 LocationErrCode errCode = DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
391 GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
392 if (errCode != ERRCODE_SUCCESS) {
393 LBSLOGE(LOCATOR, "%{public}s: can not query state, reset state.", __func__);
394 DelayedSingleton<LocationDataRdbHelper>::GetInstance()->
395 SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
396 }
397 return state;
398 }
399
IsLocationPrivacyConfirmed(const int type,bool & isConfirmed)400 LocationErrCode LocatorAbility::IsLocationPrivacyConfirmed(const int type, bool& isConfirmed)
401 {
402 return LocationConfigManager::GetInstance().GetPrivacyTypeState(type, isConfirmed);
403 }
404
SetLocationPrivacyConfirmStatus(const int type,bool isConfirmed)405 LocationErrCode LocatorAbility::SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed)
406 {
407 return LocationConfigManager::GetInstance().SetPrivacyTypeState(type, isConfirmed);
408 }
409
RegisterSwitchCallback(const sptr<IRemoteObject> & callback,pid_t uid)410 LocationErrCode LocatorAbility::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
411 {
412 if (callback == nullptr) {
413 LBSLOGE(LOCATOR, "register an invalid switch callback");
414 return ERRCODE_INVALID_PARAM;
415 }
416 sptr<IRemoteObject::DeathRecipient> death(new (std::nothrow) SwitchCallbackDeathRecipient());
417 callback->AddDeathRecipient(death);
418 sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
419 if (switchCallback == nullptr) {
420 LBSLOGE(LOCATOR, "cast switch callback fail!");
421 return ERRCODE_INVALID_PARAM;
422 }
423 std::unique_lock<std::mutex> lock(switchMutex_);
424 switchCallbacks_->erase(uid);
425 switchCallbacks_->insert(std::make_pair(uid, switchCallback));
426 LBSLOGD(LOCATOR, "after uid:%{public}d register, switch callback size:%{public}s",
427 uid, std::to_string(switchCallbacks_->size()).c_str());
428 return ERRCODE_SUCCESS;
429 }
430
UnregisterSwitchCallback(const sptr<IRemoteObject> & callback)431 LocationErrCode LocatorAbility::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
432 {
433 if (callback == nullptr) {
434 LBSLOGE(LOCATOR, "unregister an invalid switch callback");
435 return ERRCODE_INVALID_PARAM;
436 }
437 sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
438 if (switchCallback == nullptr) {
439 LBSLOGE(LOCATOR, "cast switch callback fail!");
440 return ERRCODE_INVALID_PARAM;
441 }
442
443 std::unique_lock<std::mutex> lock(switchMutex_);
444 pid_t uid = -1;
445 for (auto iter = switchCallbacks_->begin(); iter != switchCallbacks_->end(); iter++) {
446 sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
447 if (remoteObject == callback) {
448 uid = iter->first;
449 break;
450 }
451 }
452 switchCallbacks_->erase(uid);
453 LBSLOGD(LOCATOR, "after uid:%{public}d unregister, switch callback size:%{public}s",
454 uid, std::to_string(switchCallbacks_->size()).c_str());
455 return ERRCODE_SUCCESS;
456 }
457
458 #ifdef FEATURE_GNSS_SUPPORT
SendGnssRequest(int type,MessageParcel & data,MessageParcel & reply)459 LocationErrCode LocatorAbility::SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply)
460 {
461 auto locationSaLoadManager = DelayedSingleton<LocationSaLoadManager>::GetInstance();
462 if (locationSaLoadManager == nullptr) {
463 return ERRCODE_SERVICE_UNAVAILABLE;
464 }
465 locationSaLoadManager->LoadLocationSa(LOCATION_GNSS_SA_ID);
466 sptr<IRemoteObject> objectGnss =
467 CommonUtils::GetRemoteObject(LOCATION_GNSS_SA_ID, CommonUtils::InitDeviceId());
468 if (objectGnss == nullptr) {
469 return ERRCODE_SERVICE_UNAVAILABLE;
470 }
471 MessageOption option;
472 objectGnss->SendRequest(type, data, reply, option);
473 return LocationErrCode(reply.ReadInt32());
474 }
475 #endif
476
477 #ifdef FEATURE_GNSS_SUPPORT
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)478 LocationErrCode LocatorAbility::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
479 {
480 LBSLOGD(LOCATOR, "uid is: %{public}d", uid);
481 MessageParcel dataToStub;
482 MessageParcel replyToStub;
483 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
484 return ERRCODE_SERVICE_UNAVAILABLE;
485 }
486 dataToStub.WriteRemoteObject(callback);
487 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::REG_GNSS_STATUS), dataToStub, replyToStub);
488 }
489 #endif
490
491 #ifdef FEATURE_GNSS_SUPPORT
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)492 LocationErrCode LocatorAbility::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
493 {
494 MessageParcel dataToStub;
495 MessageParcel replyToStub;
496 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
497 return ERRCODE_SERVICE_UNAVAILABLE;
498 }
499 dataToStub.WriteRemoteObject(callback);
500 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::UNREG_GNSS_STATUS), dataToStub, replyToStub);
501 }
502 #endif
503
504 #ifdef FEATURE_GNSS_SUPPORT
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)505 LocationErrCode LocatorAbility::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
506 {
507 MessageParcel dataToStub;
508 MessageParcel replyToStub;
509 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
510 return ERRCODE_SERVICE_UNAVAILABLE;
511 }
512 dataToStub.WriteRemoteObject(callback);
513 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::REG_NMEA), dataToStub, replyToStub);
514 }
515 #endif
516
517 #ifdef FEATURE_GNSS_SUPPORT
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)518 LocationErrCode LocatorAbility::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
519 {
520 MessageParcel dataToStub;
521 MessageParcel replyToStub;
522 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
523 return ERRCODE_SERVICE_UNAVAILABLE;
524 }
525 dataToStub.WriteRemoteObject(callback);
526 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::UNREG_NMEA), dataToStub, replyToStub);
527 }
528 #endif
529
530 #ifdef FEATURE_GNSS_SUPPORT
RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback,std::string bundleName)531 LocationErrCode LocatorAbility::RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
532 sptr<ICachedLocationsCallback>& callback, std::string bundleName)
533 {
534 MessageParcel dataToStub;
535 MessageParcel replyToStub;
536 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
537 return ERRCODE_SERVICE_UNAVAILABLE;
538 }
539 dataToStub.WriteInt32(request->reportingPeriodSec);
540 dataToStub.WriteBool(request->wakeUpCacheQueueFull);
541 dataToStub.WriteRemoteObject(callback->AsObject());
542 dataToStub.WriteString16(Str8ToStr16(bundleName));
543 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::REG_CACHED), dataToStub, replyToStub);
544 }
545 #endif
546
547 #ifdef FEATURE_GNSS_SUPPORT
UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback> & callback)548 LocationErrCode LocatorAbility::UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback)
549 {
550 MessageParcel dataToStub;
551 MessageParcel replyToStub;
552 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
553 return ERRCODE_SERVICE_UNAVAILABLE;
554 }
555 dataToStub.WriteRemoteObject(callback->AsObject());
556 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::UNREG_CACHED), dataToStub, replyToStub);
557 }
558 #endif
559
560 #ifdef FEATURE_GNSS_SUPPORT
GetCachedGnssLocationsSize(int & size)561 LocationErrCode LocatorAbility::GetCachedGnssLocationsSize(int& size)
562 {
563 MessageParcel dataToStub;
564 MessageParcel replyToStub;
565 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
566 return ERRCODE_SERVICE_UNAVAILABLE;
567 }
568 LocationErrCode errorCode =
569 SendGnssRequest(static_cast<int>(GnssInterfaceCode::GET_CACHED_SIZE), dataToStub, replyToStub);
570 if (errorCode == ERRCODE_SUCCESS) {
571 size = replyToStub.ReadInt32();
572 }
573 return errorCode;
574 }
575 #endif
576
577 #ifdef FEATURE_GNSS_SUPPORT
FlushCachedGnssLocations()578 LocationErrCode LocatorAbility::FlushCachedGnssLocations()
579 {
580 MessageParcel dataToStub;
581 MessageParcel replyToStub;
582 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
583 return ERRCODE_SERVICE_UNAVAILABLE;
584 }
585 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::FLUSH_CACHED), dataToStub, replyToStub);
586 }
587 #endif
588
589 #ifdef FEATURE_GNSS_SUPPORT
SendCommand(std::unique_ptr<LocationCommand> & commands)590 LocationErrCode LocatorAbility::SendCommand(std::unique_ptr<LocationCommand>& commands)
591 {
592 MessageParcel dataToStub;
593 MessageParcel replyToStub;
594 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
595 return ERRCODE_SERVICE_UNAVAILABLE;
596 }
597 dataToStub.WriteInt32(commands->scenario);
598 dataToStub.WriteString16(Str8ToStr16(commands->command));
599 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::SEND_COMMANDS), dataToStub, replyToStub);
600 }
601 #endif
602
603 #ifdef FEATURE_GNSS_SUPPORT
AddFence(std::unique_ptr<GeofenceRequest> & request)604 LocationErrCode LocatorAbility::AddFence(std::unique_ptr<GeofenceRequest>& request)
605 {
606 MessageParcel dataToStub;
607 MessageParcel replyToStub;
608 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
609 return ERRCODE_SERVICE_UNAVAILABLE;
610 }
611 dataToStub.WriteInt32(request->scenario);
612 dataToStub.WriteDouble(request->geofence.latitude);
613 dataToStub.WriteDouble(request->geofence.longitude);
614 dataToStub.WriteDouble(request->geofence.radius);
615 dataToStub.WriteDouble(request->geofence.expiration);
616 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::ADD_FENCE_INFO), dataToStub, replyToStub);
617 }
618 #endif
619
620 #ifdef FEATURE_GNSS_SUPPORT
RemoveFence(std::unique_ptr<GeofenceRequest> & request)621 LocationErrCode LocatorAbility::RemoveFence(std::unique_ptr<GeofenceRequest>& request)
622 {
623 MessageParcel dataToStub;
624 MessageParcel replyToStub;
625 if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
626 return ERRCODE_SERVICE_UNAVAILABLE;
627 }
628 dataToStub.WriteInt32(request->scenario);
629 dataToStub.WriteDouble(request->geofence.latitude);
630 dataToStub.WriteDouble(request->geofence.longitude);
631 dataToStub.WriteDouble(request->geofence.radius);
632 dataToStub.WriteDouble(request->geofence.expiration);
633 return SendGnssRequest(static_cast<int>(GnssInterfaceCode::REMOVE_FENCE_INFO), dataToStub, replyToStub);
634 }
635 #endif
636
637 #ifdef FEATURE_GNSS_SUPPORT
SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj,const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)638 LocationErrCode LocatorAbility::SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj,
639 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
640 {
641 if (obj == nullptr) {
642 LBSLOGE(LOCATOR, "SendLocationMockMsgToGnssSa obj is nullptr");
643 return ERRCODE_SERVICE_UNAVAILABLE;
644 }
645 std::unique_ptr<GnssAbilityProxy> gnssProxy = std::make_unique<GnssAbilityProxy>(obj);
646 LocationErrCode errorCode = ERRCODE_NOT_SUPPORTED;
647 if (msgId == static_cast<int>(LocatorInterfaceCode::ENABLE_LOCATION_MOCK)) {
648 errorCode = gnssProxy->EnableMock();
649 } else if (msgId == static_cast<int>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK)) {
650 errorCode = gnssProxy->DisableMock();
651 } else if (msgId == static_cast<int>(LocatorInterfaceCode::SET_MOCKED_LOCATIONS)) {
652 errorCode = gnssProxy->SetMocked(timeInterval, location);
653 }
654 return errorCode;
655 }
656 #endif
657
658 #ifdef FEATURE_NETWORK_SUPPORT
SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj,const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)659 LocationErrCode LocatorAbility::SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj,
660 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
661 {
662 if (obj == nullptr) {
663 LBSLOGE(LOCATOR, "SendLocationMockMsgToNetworkSa obj is nullptr");
664 return ERRCODE_SERVICE_UNAVAILABLE;
665 }
666 std::unique_ptr<NetworkAbilityProxy> networkProxy =
667 std::make_unique<NetworkAbilityProxy>(obj);
668 LocationErrCode errorCode = ERRCODE_NOT_SUPPORTED;
669 if (msgId == static_cast<int>(LocatorInterfaceCode::ENABLE_LOCATION_MOCK)) {
670 errorCode = networkProxy->EnableMock();
671 } else if (msgId == static_cast<int>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK)) {
672 errorCode = networkProxy->DisableMock();
673 } else if (msgId == static_cast<int>(LocatorInterfaceCode::SET_MOCKED_LOCATIONS)) {
674 errorCode = networkProxy->SetMocked(timeInterval, location);
675 }
676 return errorCode;
677 }
678 #endif
679
680 #ifdef FEATURE_PASSIVE_SUPPORT
SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj,const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)681 LocationErrCode LocatorAbility::SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj,
682 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
683 {
684 if (obj == nullptr) {
685 LBSLOGE(LOCATOR, "SendLocationMockMsgToNetworkSa obj is nullptr");
686 return ERRCODE_SERVICE_UNAVAILABLE;
687 }
688 std::unique_ptr<PassiveAbilityProxy> passiveProxy =
689 std::make_unique<PassiveAbilityProxy>(obj);
690 LocationErrCode errorCode = ERRCODE_NOT_SUPPORTED;
691 if (msgId == static_cast<int>(LocatorInterfaceCode::ENABLE_LOCATION_MOCK)) {
692 errorCode = passiveProxy->EnableMock();
693 } else if (msgId == static_cast<int>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK)) {
694 errorCode = passiveProxy->DisableMock();
695 } else if (msgId == static_cast<int>(LocatorInterfaceCode::SET_MOCKED_LOCATIONS)) {
696 errorCode = passiveProxy->SetMocked(timeInterval, location);
697 }
698 return errorCode;
699 }
700 #endif
701
ProcessLocationMockMsg(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)702 LocationErrCode LocatorAbility::ProcessLocationMockMsg(
703 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
704 {
705 #if !defined(FEATURE_GNSS_SUPPORT) && !defined(FEATURE_NETWORK_SUPPORT) && !defined(FEATURE_PASSIVE_SUPPORT)
706 LBSLOGE(LOCATOR, "%{public}s: mock service unavailable", __func__);
707 return ERRCODE_NOT_SUPPORTED;
708 #endif
709 if (!CheckSaValid()) {
710 UpdateProxyMap();
711 }
712
713 std::unique_lock<std::mutex> lock(proxyMapMutex_);
714 for (auto iter = proxyMap_->begin(); iter != proxyMap_->end(); iter++) {
715 auto obj = iter->second;
716 if (iter->first == GNSS_ABILITY) {
717 #ifdef FEATURE_GNSS_SUPPORT
718 SendLocationMockMsgToGnssSa(obj, timeInterval, location, msgId);
719 #endif
720 } else if (iter->first == NETWORK_ABILITY) {
721 #ifdef FEATURE_NETWORK_SUPPORT
722 SendLocationMockMsgToNetworkSa(obj, timeInterval, location, msgId);
723 #endif
724 } else if (iter->first == PASSIVE_ABILITY) {
725 #ifdef FEATURE_PASSIVE_SUPPORT
726 SendLocationMockMsgToPassiveSa(obj, timeInterval, location, msgId);
727 #endif
728 }
729 }
730 return ERRCODE_SUCCESS;
731 }
732
UpdateProxyMap()733 void LocatorAbility::UpdateProxyMap()
734 {
735 std::unique_lock<std::mutex> lock(proxyMapMutex_);
736 auto locationSaLoadManager = DelayedSingleton<LocationSaLoadManager>::GetInstance();
737 if (locationSaLoadManager == nullptr) {
738 return;
739 }
740 #ifdef FEATURE_GNSS_SUPPORT
741 // init gnss ability sa
742 locationSaLoadManager->LoadLocationSa(LOCATION_GNSS_SA_ID);
743 sptr<IRemoteObject> objectGnss = CommonUtils::GetRemoteObject(LOCATION_GNSS_SA_ID, CommonUtils::InitDeviceId());
744 if (objectGnss != nullptr) {
745 proxyMap_->insert(make_pair(GNSS_ABILITY, objectGnss));
746 } else {
747 LBSLOGE(LOCATOR, "GetRemoteObject gnss sa is null");
748 }
749 #endif
750 #ifdef FEATURE_NETWORK_SUPPORT
751 // init network ability sa
752 locationSaLoadManager->LoadLocationSa(LOCATION_NETWORK_LOCATING_SA_ID);
753 sptr<IRemoteObject> objectNetwork = CommonUtils::GetRemoteObject(LOCATION_NETWORK_LOCATING_SA_ID,
754 CommonUtils::InitDeviceId());
755 if (objectNetwork != nullptr) {
756 proxyMap_->insert(make_pair(NETWORK_ABILITY, objectNetwork));
757 } else {
758 LBSLOGE(LOCATOR, "GetRemoteObject network sa is null");
759 }
760 #endif
761 #ifdef FEATURE_PASSIVE_SUPPORT
762 // init passive ability sa
763 locationSaLoadManager->LoadLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
764 sptr<IRemoteObject> objectPassive = CommonUtils::GetRemoteObject(LOCATION_NOPOWER_LOCATING_SA_ID,
765 CommonUtils::InitDeviceId());
766 if (objectPassive != nullptr) {
767 proxyMap_->insert(make_pair(PASSIVE_ABILITY, objectPassive));
768 } else {
769 LBSLOGE(LOCATOR, "GetRemoteObject passive sa is null");
770 }
771 #endif
772 }
773
EnableLocationMock()774 LocationErrCode LocatorAbility::EnableLocationMock()
775 {
776 int timeInterval = 0;
777 std::vector<std::shared_ptr<Location>> location;
778 return ProcessLocationMockMsg(timeInterval, location,
779 static_cast<int>(LocatorInterfaceCode::ENABLE_LOCATION_MOCK));
780 }
781
DisableLocationMock()782 LocationErrCode LocatorAbility::DisableLocationMock()
783 {
784 int timeInterval = 0;
785 std::vector<std::shared_ptr<Location>> location;
786 return ProcessLocationMockMsg(timeInterval, location,
787 static_cast<int>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK));
788 }
789
SetMockedLocations(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)790 LocationErrCode LocatorAbility::SetMockedLocations(
791 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
792 {
793 return ProcessLocationMockMsg(timeInterval, location,
794 static_cast<int>(LocatorInterfaceCode::SET_MOCKED_LOCATIONS));
795 }
796
StartLocating(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback,AppIdentity & identity)797 LocationErrCode LocatorAbility::StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
798 sptr<ILocatorCallback>& callback, AppIdentity &identity)
799 {
800 #if !defined(FEATURE_GNSS_SUPPORT) && !defined(FEATURE_NETWORK_SUPPORT) && !defined(FEATURE_PASSIVE_SUPPORT)
801 LBSLOGE(LOCATOR, "%{public}s: service unavailable", __func__);
802 return ERRCODE_NOT_SUPPORTED;
803 #endif
804 if (QuerySwitchState() == DISABLED) {
805 ReportErrorStatus(callback, ERROR_SWITCH_UNOPEN);
806 }
807 if (!CheckSaValid()) {
808 UpdateSaAbilityHandler();
809 }
810 // update offset before add request
811 if (reportManager_ == nullptr || requestManager_ == nullptr) {
812 return ERRCODE_SERVICE_UNAVAILABLE;
813 }
814 reportManager_->UpdateRandom();
815 // generate request object according to input params
816 std::shared_ptr<Request> request = std::make_shared<Request>();
817 request->SetUid(identity.GetUid());
818 request->SetPid(identity.GetPid());
819 request->SetTokenId(identity.GetTokenId());
820 request->SetFirstTokenId(identity.GetFirstTokenId());
821 request->SetPackageName(identity.GetBundleName());
822 request->SetRequestConfig(*requestConfig);
823 request->SetLocatorCallBack(callback);
824 request->SetUuid(std::to_string(CommonUtils::IntRandom(MIN_INT_RANDOM, MAX_INT_RANDOM)));
825 LBSLOGI(LOCATOR, "start locating");
826 requestManager_->HandleStartLocating(request);
827 ReportLocationStatus(callback, SESSION_START);
828 return ERRCODE_SUCCESS;
829 }
830
StopLocating(sptr<ILocatorCallback> & callback)831 LocationErrCode LocatorAbility::StopLocating(sptr<ILocatorCallback>& callback)
832 {
833 #if !defined(FEATURE_GNSS_SUPPORT) && !defined(FEATURE_NETWORK_SUPPORT) && !defined(FEATURE_PASSIVE_SUPPORT)
834 LBSLOGE(LOCATOR, "%{public}s: service unavailable", __func__);
835 return ERRCODE_NOT_SUPPORTED;
836 #endif
837 LBSLOGI(LOCATOR, "stop locating");
838 if (requestManager_ == nullptr) {
839 return ERRCODE_SERVICE_UNAVAILABLE;
840 }
841 requestManager_->HandleStopLocating(callback);
842 ReportLocationStatus(callback, SESSION_STOP);
843 return ERRCODE_SUCCESS;
844 }
845
GetCacheLocation(std::unique_ptr<Location> & loc,AppIdentity & identity)846 LocationErrCode LocatorAbility::GetCacheLocation(std::unique_ptr<Location>& loc, AppIdentity &identity)
847 {
848 auto lastLocation = reportManager_->GetLastLocation();
849 loc = reportManager_->GetPermittedLocation(identity.GetUid(), identity.GetTokenId(),
850 identity.GetFirstTokenId(), lastLocation);
851 if (loc == nullptr) {
852 return ERRCODE_LOCATING_FAIL;
853 }
854 if (fabs(loc->GetLatitude() - 0.0) > PRECISION
855 && fabs(loc->GetLongitude() - 0.0) > PRECISION) {
856 return ERRCODE_SUCCESS;
857 }
858 return ERRCODE_LOCATING_FAIL;
859 }
860
ReportLocation(const std::unique_ptr<Location> & location,std::string abilityName)861 LocationErrCode LocatorAbility::ReportLocation(const std::unique_ptr<Location>& location, std::string abilityName)
862 {
863 if (requests_ == nullptr) {
864 return ERRCODE_SERVICE_UNAVAILABLE;
865 }
866 int state = DISABLED;
867 LocationErrCode errorCode = GetSwitchState(state);
868 if (errorCode != ERRCODE_SUCCESS) {
869 return errorCode;
870 }
871 if (state == DISABLED) {
872 LBSLOGE(LOCATOR, "location switch is off");
873 return ERRCODE_SWITCH_OFF;
874 }
875 LBSLOGI(LOCATOR, "start report location");
876 if (reportManager_->OnReportLocation(location, abilityName)) {
877 return ERRCODE_SUCCESS;
878 }
879 return ERRCODE_SERVICE_UNAVAILABLE;
880 }
881
ReportLocationStatus(sptr<ILocatorCallback> & callback,int result)882 LocationErrCode LocatorAbility::ReportLocationStatus(sptr<ILocatorCallback>& callback, int result)
883 {
884 int state = DISABLED;
885 LocationErrCode errorCode = GetSwitchState(state);
886 if (errorCode != ERRCODE_SUCCESS) {
887 return errorCode;
888 }
889 if (state == DISABLED) {
890 LBSLOGE(LOCATOR, "location switch is off");
891 return ERRCODE_SWITCH_OFF;
892 }
893 if (reportManager_->ReportRemoteCallback(callback, ILocatorCallback::RECEIVE_LOCATION_STATUS_EVENT, result)) {
894 return ERRCODE_SUCCESS;
895 }
896 return ERRCODE_SERVICE_UNAVAILABLE;
897 }
898
ReportErrorStatus(sptr<ILocatorCallback> & callback,int result)899 LocationErrCode LocatorAbility::ReportErrorStatus(sptr<ILocatorCallback>& callback, int result)
900 {
901 int state = DISABLED;
902 LocationErrCode errorCode = GetSwitchState(state);
903 if (errorCode != ERRCODE_SUCCESS) {
904 return errorCode;
905 }
906 if (state == DISABLED) {
907 LBSLOGE(LOCATOR, "location switch is off");
908 return ERRCODE_SWITCH_OFF;
909 }
910 if (reportManager_->ReportRemoteCallback(callback, ILocatorCallback::RECEIVE_ERROR_INFO_EVENT, result)) {
911 return ERRCODE_SUCCESS;
912 }
913 return ERRCODE_SERVICE_UNAVAILABLE;
914 }
915
RegisterAction()916 void LocatorAbility::RegisterAction()
917 {
918 if (isActionRegistered) {
919 LBSLOGI(LOCATOR, "action has already registered");
920 return;
921 }
922 OHOS::EventFwk::MatchingSkills matchingSkills;
923 matchingSkills.AddEvent(MODE_CHANGED_EVENT);
924 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
925 locatorEventSubscriber_ = std::make_shared<LocatorEventSubscriber>(subscriberInfo);
926
927 bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(locatorEventSubscriber_);
928 if (!result) {
929 LBSLOGE(LOCATOR, "Failed to subscriber locator event, result = %{public}d", result);
930 isActionRegistered = false;
931 } else {
932 LBSLOGI(LOCATOR, "success to subscriber locator event, result = %{public}d", result);
933 isActionRegistered = true;
934 }
935 }
936
937 #ifdef FEATURE_GEOCODE_SUPPORT
IsGeoConvertAvailable(bool & isAvailable)938 LocationErrCode LocatorAbility::IsGeoConvertAvailable(bool &isAvailable)
939 {
940 MessageParcel dataParcel;
941 MessageParcel replyParcel;
942 if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
943 isAvailable = false;
944 return ERRCODE_SERVICE_UNAVAILABLE;
945 }
946 SendGeoRequest(static_cast<int>(LocatorInterfaceCode::GEO_IS_AVAILABLE), dataParcel, replyParcel);
947 LocationErrCode errorCode = LocationErrCode(replyParcel.ReadInt32());
948 if (errorCode == ERRCODE_SUCCESS) {
949 isAvailable = replyParcel.ReadBool();
950 } else {
951 isAvailable = false;
952 }
953 return errorCode;
954 }
955 #endif
956
957 #ifdef FEATURE_GEOCODE_SUPPORT
GetAddressByCoordinate(MessageParcel & data,MessageParcel & reply,std::string bundleName)958 void LocatorAbility::GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply, std::string bundleName)
959 {
960 LBSLOGI(LOCATOR, "locator_ability GetAddressByCoordinate");
961 MessageParcel dataParcel;
962 if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
963 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
964 return;
965 }
966 dataParcel.WriteString16(data.ReadString16()); // locale
967 dataParcel.WriteDouble(data.ReadDouble()); // latitude
968 dataParcel.WriteDouble(data.ReadDouble()); // longitude
969 dataParcel.WriteInt32(data.ReadInt32()); // maxItems
970 dataParcel.WriteString16(Str8ToStr16(bundleName)); // bundleName
971 SendGeoRequest(static_cast<int>(LocatorInterfaceCode::GET_FROM_COORDINATE), dataParcel, reply);
972 }
973 #endif
974
975 #ifdef FEATURE_GEOCODE_SUPPORT
GetAddressByLocationName(MessageParcel & data,MessageParcel & reply,std::string bundleName)976 void LocatorAbility::GetAddressByLocationName(MessageParcel &data, MessageParcel &reply, std::string bundleName)
977 {
978 LBSLOGI(LOCATOR, "locator_ability GetAddressByLocationName");
979 MessageParcel dataParcel;
980 if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
981 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
982 return;
983 }
984 dataParcel.WriteString16(data.ReadString16()); // locale
985 dataParcel.WriteString16(data.ReadString16()); // description
986 dataParcel.WriteInt32(data.ReadInt32()); // maxItems
987 dataParcel.WriteDouble(data.ReadDouble()); // minLatitude
988 dataParcel.WriteDouble(data.ReadDouble()); // minLongitude
989 dataParcel.WriteDouble(data.ReadDouble()); // maxLatitude
990 dataParcel.WriteDouble(data.ReadDouble()); // maxLongitude
991 dataParcel.WriteString16(Str8ToStr16(bundleName)); // bundleName
992 SendGeoRequest(static_cast<int>(LocatorInterfaceCode::GET_FROM_LOCATION_NAME), dataParcel, reply);
993 }
994 #endif
995
996 #ifdef FEATURE_GEOCODE_SUPPORT
SendGeoRequest(int type,MessageParcel & data,MessageParcel & reply)997 LocationErrCode LocatorAbility::SendGeoRequest(int type, MessageParcel &data, MessageParcel &reply)
998 {
999 auto locationSaLoadManager = DelayedSingleton<LocationSaLoadManager>::GetInstance();
1000 if (locationSaLoadManager == nullptr) {
1001 return ERRCODE_SERVICE_UNAVAILABLE;
1002 }
1003
1004 locationSaLoadManager->LoadLocationSa(LOCATION_GEO_CONVERT_SA_ID);
1005 sptr<IRemoteObject> remoteObject = CommonUtils::GetRemoteObject(LOCATION_GEO_CONVERT_SA_ID,
1006 CommonUtils::InitDeviceId());
1007 if (remoteObject == nullptr) {
1008 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1009 return ERRCODE_SERVICE_UNAVAILABLE;
1010 }
1011 MessageOption option;
1012 remoteObject->SendRequest(type, data, reply, option);
1013 return ERRCODE_SUCCESS;
1014 }
1015 #endif
1016
1017 #ifdef FEATURE_GEOCODE_SUPPORT
EnableReverseGeocodingMock()1018 LocationErrCode LocatorAbility::EnableReverseGeocodingMock()
1019 {
1020 MessageParcel dataParcel;
1021 MessageParcel replyParcel;
1022 if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
1023 return ERRCODE_SERVICE_UNAVAILABLE;
1024 }
1025 SendGeoRequest(static_cast<int>(LocatorInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK), dataParcel, replyParcel);
1026 return LocationErrCode(replyParcel.ReadInt32());
1027 }
1028 #endif
1029
1030 #ifdef FEATURE_GEOCODE_SUPPORT
DisableReverseGeocodingMock()1031 LocationErrCode LocatorAbility::DisableReverseGeocodingMock()
1032 {
1033 MessageParcel dataParcel;
1034 MessageParcel replyParcel;
1035 if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
1036 return ERRCODE_SERVICE_UNAVAILABLE;
1037 }
1038 SendGeoRequest(static_cast<int>(LocatorInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK), dataParcel, replyParcel);
1039 return LocationErrCode(replyParcel.ReadInt32());
1040 }
1041 #endif
1042
1043 #ifdef FEATURE_GEOCODE_SUPPORT
SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)1044 LocationErrCode LocatorAbility::SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
1045 {
1046 MessageParcel dataParcel;
1047 MessageParcel replyParcel;
1048 if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
1049 return ERRCODE_SERVICE_UNAVAILABLE;
1050 }
1051 dataParcel.WriteInt32(mockInfo.size());
1052 for (size_t i = 0; i < mockInfo.size(); i++) {
1053 mockInfo[i]->Marshalling(dataParcel);
1054 }
1055 SendGeoRequest(static_cast<int>(LocatorInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO), dataParcel, replyParcel);
1056 return LocationErrCode(replyParcel.ReadInt32());
1057 }
1058 #endif
1059
ProxyUidForFreeze(int32_t uid,bool isProxy)1060 LocationErrCode LocatorAbility::ProxyUidForFreeze(int32_t uid, bool isProxy)
1061 {
1062 LBSLOGI(LOCATOR, "Start locator proxy, uid: %{public}d, isProxy: %{public}d", uid, isProxy);
1063 std::unique_lock<std::mutex> lock(proxyUidsMutex_);
1064 if (isProxy) {
1065 proxyUids_.insert(uid);
1066 } else {
1067 proxyUids_.erase(uid);
1068 }
1069 return ERRCODE_SUCCESS;
1070 }
1071
ResetAllProxy()1072 LocationErrCode LocatorAbility::ResetAllProxy()
1073 {
1074 LBSLOGI(LOCATOR, "Start locator ResetAllProxy");
1075 std::unique_lock<std::mutex> lock(proxyUidsMutex_);
1076 proxyUids_.clear();
1077 return ERRCODE_SUCCESS;
1078 }
1079
IsProxyUid(int32_t uid)1080 bool LocatorAbility::IsProxyUid(int32_t uid)
1081 {
1082 std::unique_lock<std::mutex> lock(proxyUidsMutex_);
1083 return proxyUids_.find(uid) != proxyUids_.end();
1084 }
1085
RegisterPermissionCallback(const uint32_t callingTokenId,const std::vector<std::string> & permissionNameList)1086 void LocatorAbility::RegisterPermissionCallback(const uint32_t callingTokenId,
1087 const std::vector<std::string>& permissionNameList)
1088 {
1089 std::unique_lock<std::mutex> lock(permissionMapMutex_);
1090 if (permissionMap_ == nullptr) {
1091 LBSLOGE(LOCATOR, "permissionMap is null.");
1092 return;
1093 }
1094 PermStateChangeScope scopeInfo;
1095 scopeInfo.permList = permissionNameList;
1096 scopeInfo.tokenIDs = {callingTokenId};
1097 auto callbackPtr = std::make_shared<PermissionStatusChangeCb>(scopeInfo);
1098 permissionMap_->erase(callingTokenId);
1099 permissionMap_->insert(std::make_pair(callingTokenId, callbackPtr));
1100 LBSLOGD(LOCATOR, "after tokenId:%{public}d register, permission callback size:%{public}s",
1101 callingTokenId, std::to_string(permissionMap_->size()).c_str());
1102 int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr);
1103 if (res != SUCCESS) {
1104 LBSLOGE(LOCATOR, "RegisterPermStateChangeCallback failed.");
1105 }
1106 }
1107
UnregisterPermissionCallback(const uint32_t callingTokenId)1108 void LocatorAbility::UnregisterPermissionCallback(const uint32_t callingTokenId)
1109 {
1110 std::unique_lock<std::mutex> lock(permissionMapMutex_);
1111 if (permissionMap_ == nullptr) {
1112 LBSLOGE(LOCATOR, "permissionMap is null.");
1113 return;
1114 }
1115 auto iter = permissionMap_->find(callingTokenId);
1116 if (iter != permissionMap_->end()) {
1117 auto callbackPtr = iter->second;
1118 int32_t res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr);
1119 if (res != SUCCESS) {
1120 LBSLOGE(LOCATOR, "UnRegisterPermStateChangeCallback failed.");
1121 }
1122 }
1123 permissionMap_->erase(callingTokenId);
1124 LBSLOGD(LOCATOR, "after tokenId:%{public}d unregister, permission callback size:%{public}s",
1125 callingTokenId, std::to_string(permissionMap_->size()).c_str());
1126 }
1127 } // namespace Location
1128 } // namespace OHOS
1129