• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef LOCATION_HICOLLIE_ENABLE
28 #include "xcollie/xcollie.h"
29 #include "xcollie/xcollie_define.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Location {
34 std::mutex SelfRequestManager::locatorMutex_;
35 const uint32_t EVENT_STARTLOCATING = 0x0100;
36 const uint32_t EVENT_STOPLOCATING = 0x0200;
37 const int TIMEOUT_WATCHDOG = 60; // s
GetInstance()38 SelfRequestManager* SelfRequestManager::GetInstance()
39 {
40     static SelfRequestManager data;
41     return &data;
42 }
43 
SelfRequestManager()44 SelfRequestManager::SelfRequestManager()
45 {
46     selfRequestManagerHandler_ = std::make_shared<SelfRequestManagerHandler>(AppExecFwk::EventRunner::Create(true,
47         AppExecFwk::ThreadMode::FFRT));
48 
49     auto requestConfig = std::make_unique<RequestConfig>();
50     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
51     requestConfig->SetTimeInterval(1);
52     callback_ = sptr<mLocatorCallback>(new (std::nothrow) SelfRequestManager::mLocatorCallback());
53     if (callback_ == nullptr) {
54         return;
55     }
56     request_ = std::make_shared<Request>();
57     if (request_ == nullptr) {
58         return;
59     }
60     request_->SetUid(SYSTEM_UID);
61     request_->SetPid(getpid());
62     request_->SetPackageName(PROC_NAME);
63     request_->SetRequestConfig(*requestConfig);
64     request_->SetLocatorCallBack(callback_);
65     request_->SetUuid(PROC_NAME);
66     request_->SetTokenId(IPCSkeleton::GetCallingTokenID());
67     request_->SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
68     proxySwtich_ = (LocationDataRdbManager::QuerySwitchState() == ENABLED);
69 }
70 
~SelfRequestManager()71 SelfRequestManager::~SelfRequestManager()
72 {
73 }
74 
StartLocatorThread()75 void SelfRequestManager::StartLocatorThread()
76 {
77     auto requestManager = RequestManager::GetInstance();
78     std::unique_lock<std::mutex> lock(locatorMutex_, std::defer_lock);
79     lock.lock();
80     if (isLocating_ || !proxySwtich_) {
81         LBSLOGD(LOCATOR, "cancel locating");
82         lock.unlock();
83         return;
84     }
85     isLocating_ = true;
86     lock.unlock();
87     LBSLOGI(LOCATOR, "SelfRequestManager start locating");
88     requestManager->HandleStartLocating(request_);
89 }
90 
StopLocatorThread()91 void SelfRequestManager::StopLocatorThread()
92 {
93     auto locatorAbility = LocatorAbility::GetInstance();
94     std::unique_lock<std::mutex> lock(locatorMutex_, std::defer_lock);
95     lock.lock();
96     if (!isLocating_) {
97         lock.unlock();
98         return;
99     }
100     isLocating_ = false;
101     lock.unlock();
102     locatorAbility->StopLocating(callback_);
103     LBSLOGI(LOCATOR, "SelfRequestManager stop locating");
104 }
105 
StopSelfRequest()106 void SelfRequestManager::StopSelfRequest()
107 {
108     selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, 0);
109 }
110 
StartSelfRequest()111 void SelfRequestManager::StartSelfRequest()
112 {
113     selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STARTLOCATING, 0, 0);
114     selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, DEFAULT_TIMEOUT_5S);
115 }
116 
OnLocationReport(const std::unique_ptr<Location> & location)117 void SelfRequestManager::mLocatorCallback::OnLocationReport(const std::unique_ptr<Location>& location)
118 {
119     LBSLOGD(LOCATOR, "locator background OnLocationReport");
120     SelfRequestManager::GetInstance()->StopSelfRequest();
121 }
122 
OnLocatingStatusChange(const int status)123 void SelfRequestManager::mLocatorCallback::OnLocatingStatusChange(const int status)
124 {
125 }
126 
OnErrorReport(const int errorCode)127 void SelfRequestManager::mLocatorCallback::OnErrorReport(const int errorCode)
128 {
129 }
130 
SelfRequestManagerHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)131 SelfRequestManagerHandler::SelfRequestManagerHandler(
132     const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner)
133 {
134     InitSelfRequestManagerHandlerEventMap();
135 }
136 
~SelfRequestManagerHandler()137 SelfRequestManagerHandler::~SelfRequestManagerHandler() {}
138 
InitSelfRequestManagerHandlerEventMap()139 void SelfRequestManagerHandler::InitSelfRequestManagerHandlerEventMap()
140 {
141     if (selfRequestManagerHandlerEventMap_.size() != 0) {
142         return;
143     }
144     selfRequestManagerHandlerEventMap_[EVENT_STARTLOCATING] =
145         [this](const AppExecFwk::InnerEvent::Pointer& event) { StartLocatingEvent(event); };
146     selfRequestManagerHandlerEventMap_[EVENT_STOPLOCATING] =
147         [this](const AppExecFwk::InnerEvent::Pointer& event) { StopLocatingEvent(event); };
148 }
149 
StartLocatingEvent(const AppExecFwk::InnerEvent::Pointer & event)150 void SelfRequestManagerHandler::StartLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event)
151 {
152     auto SelfRequestManager = SelfRequestManager::GetInstance();
153     SelfRequestManager->StartLocatorThread();
154 }
155 
StopLocatingEvent(const AppExecFwk::InnerEvent::Pointer & event)156 void SelfRequestManagerHandler::StopLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event)
157 {
158     auto SelfRequestManager = SelfRequestManager::GetInstance();
159     SelfRequestManager->StopLocatorThread();
160 }
161 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)162 void SelfRequestManagerHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
163 {
164     uint32_t eventId = event->GetInnerEventId();
165     LBSLOGD(LOCATOR, "SelfRequestManagerHandler processEvent event:%{public}d, timestamp = %{public}s",
166         eventId, std::to_string(CommonUtils::GetCurrentTimeStamp()).c_str());
167     auto handleFunc = selfRequestManagerHandlerEventMap_.find(eventId);
168     if (handleFunc != selfRequestManagerHandlerEventMap_.end() && handleFunc->second != nullptr) {
169         auto memberFunc = handleFunc->second;
170 #ifdef LOCATION_HICOLLIE_ENABLE
171         int tid = gettid();
172         std::string moduleName = "SelfRequestManagerHandler";
173         XCollieCallback callbackFunc = [moduleName, eventId, tid](void *) {
174             LBSLOGE(LOCATOR, "TimeoutCallback tid:%{public}d moduleName:%{public}s excute eventId:%{public}u timeout.",
175                 tid, moduleName.c_str(), eventId);
176         };
177         std::string dfxInfo = moduleName + "_" + std::to_string(eventId) + "_" + std::to_string(tid);
178         int timerId = HiviewDFX::XCollie::GetInstance().SetTimer(dfxInfo, TIMEOUT_WATCHDOG, callbackFunc, nullptr,
179             HiviewDFX::XCOLLIE_FLAG_LOG|HiviewDFX::XCOLLIE_FLAG_RECOVERY);
180         memberFunc(event);
181         HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
182 #else
183         memberFunc(event);
184 #endif
185     }
186 }
187 } // namespace OHOS
188 } // namespace Location
189