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