1 /*
2 * Copyright (C) 2024 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 "self_request_manager.h"
17
18 #include "common_utils.h"
19 #include "constant_definition.h"
20 #include "location_config_manager.h"
21 #include "location_log.h"
22 #include "locator_ability.h"
23 #include "request_manager.h"
24 #include "permission_manager.h"
25 #include "location_data_rdb_manager.h"
26 #include "ipc_skeleton.h"
27
28 namespace OHOS {
29 namespace Location {
30 std::mutex SelfRequestManager::locatorMutex_;
31 const uint32_t EVENT_STARTLOCATING = 0x0100;
32 const uint32_t EVENT_STOPLOCATING = 0x0200;
GetInstance()33 SelfRequestManager* SelfRequestManager::GetInstance()
34 {
35 static SelfRequestManager data;
36 return &data;
37 }
38
SelfRequestManager()39 SelfRequestManager::SelfRequestManager()
40 {
41 selfRequestManagerHandler_ = std::make_shared<SelfRequestManagerHandler>(AppExecFwk::EventRunner::Create(true,
42 AppExecFwk::ThreadMode::FFRT));
43
44 RequestConfig requestConfig;
45 requestConfig.SetPriority(PRIORITY_FAST_FIRST_FIX);
46 requestConfig.SetTimeInterval(1);
47 callback_ = sptr<mLocatorCallback>(new (std::nothrow) SelfRequestManager::mLocatorCallback());
48 if (callback_ == nullptr) {
49 return;
50 }
51 request_ = std::make_shared<Request>();
52 if (request_ == nullptr) {
53 return;
54 }
55 request_->SetUid(SYSTEM_UID);
56 request_->SetPid(getpid());
57 request_->SetPackageName(PROC_NAME);
58 request_->SetRequestConfig(requestConfig);
59 request_->SetLocatorCallBack(callback_);
60 request_->SetUuid(PROC_NAME);
61 request_->SetTokenId(IPCSkeleton::GetCallingTokenID());
62 request_->SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
63 locationSwitch_ = (LocationDataRdbManager::QuerySwitchState() == ENABLED);
64 }
65
~SelfRequestManager()66 SelfRequestManager::~SelfRequestManager()
67 {
68 }
69
ProcessStartSelfRequestEvent()70 void SelfRequestManager::ProcessStartSelfRequestEvent()
71 {
72 std::unique_lock<std::mutex> lock(locatorMutex_, std::defer_lock);
73 lock.lock();
74 if (isLocating_ || !locationSwitch_) {
75 LBSLOGD(LOCATOR, "cancel locating");
76 lock.unlock();
77 return;
78 }
79 isLocating_ = true;
80 lock.unlock();
81 LBSLOGI(LOCATOR, "SelfRequestManager start locating");
82 LocatorAbility::GetInstance()->HandleStartLocating(request_, callback_);
83 }
84
ProcessStopSelfRequestEvent()85 void SelfRequestManager::ProcessStopSelfRequestEvent()
86 {
87 std::unique_lock<std::mutex> lock(locatorMutex_, std::defer_lock);
88 lock.lock();
89 if (!isLocating_) {
90 lock.unlock();
91 return;
92 }
93 isLocating_ = false;
94 lock.unlock();
95 LocatorAbility::GetInstance()->StopLocating(callback_);
96 LBSLOGI(LOCATOR, "SelfRequestManager stop locating");
97 }
98
StopSelfRequest()99 void SelfRequestManager::StopSelfRequest()
100 {
101 selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, 0);
102 }
103
StartSelfRequest()104 void SelfRequestManager::StartSelfRequest()
105 {
106 selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STARTLOCATING, 0, 0);
107 selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, DEFAULT_TIMEOUT_5S);
108 }
109
OnLocationReport(const std::unique_ptr<Location> & location)110 void SelfRequestManager::mLocatorCallback::OnLocationReport(const std::unique_ptr<Location>& location)
111 {
112 LBSLOGD(LOCATOR, "locator background OnLocationReport");
113 SelfRequestManager::GetInstance()->StopSelfRequest();
114 }
115
OnLocatingStatusChange(const int status)116 void SelfRequestManager::mLocatorCallback::OnLocatingStatusChange(const int status)
117 {
118 }
119
OnErrorReport(const int errorCode)120 void SelfRequestManager::mLocatorCallback::OnErrorReport(const int errorCode)
121 {
122 }
123
SelfRequestManagerHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)124 SelfRequestManagerHandler::SelfRequestManagerHandler(
125 const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner) {}
126
~SelfRequestManagerHandler()127 SelfRequestManagerHandler::~SelfRequestManagerHandler() {}
128
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)129 void SelfRequestManagerHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
130 {
131 auto SelfRequestManager = SelfRequestManager::GetInstance();
132 uint32_t eventId = event->GetInnerEventId();
133 switch (eventId) {
134 case EVENT_STARTLOCATING: {
135 SelfRequestManager->ProcessStartSelfRequestEvent();
136 break;
137 }
138 case EVENT_STOPLOCATING: {
139 SelfRequestManager->ProcessStopSelfRequestEvent();
140 break;
141 }
142 default:
143 break;
144 }
145 }
146 } // namespace OHOS
147 } // namespace Location
148