• 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 #include "hook_utils.h"
16 #include "location_log.h"
17 #include "constant_definition.h"
18 
19 namespace OHOS {
20 namespace Location {
21 static HOOK_MGR* locatorHookMgr_ = nullptr;
22 
GetLocationExtHookMgr()23 HOOK_MGR* HookUtils::GetLocationExtHookMgr()
24 {
25     if (locatorHookMgr_ != nullptr) {
26         return locatorHookMgr_;
27     }
28 
29     locatorHookMgr_ = HookMgrCreate("locationHookMgr");
30     return locatorHookMgr_;
31 }
32 
ExecuteHookWhenWifiScanStateChanged(const std::vector<std::shared_ptr<LocatingRequiredData>> & result)33 void HookUtils::ExecuteHookWhenWifiScanStateChanged(
34     const std::vector<std::shared_ptr<LocatingRequiredData>>& result)
35 {
36     WifiScanResult scanResult;
37     scanResult.result = result;
38     ExecuteHook(LocationProcessStage::WIFI_SCAN_STATE_CHANGE, (void *)&scanResult, nullptr);
39 }
40 
RegisterHook(LocationProcessStage stage,int prio,OhosHook hook)41 LocationErrCode HookUtils::RegisterHook(LocationProcessStage stage, int prio, OhosHook hook)
42 {
43     auto ret = HookMgrAdd(GetLocationExtHookMgr(), static_cast<int>(stage), prio, hook);
44     if (ret == 0) {
45         return ERRCODE_SUCCESS;
46     }
47     LBSLOGE(LOCATOR, "%{public}s stage = %{public}d add failed ret = %{public}d",
48         __func__, static_cast<int>(stage), ret);
49     return ERRCODE_SERVICE_UNAVAILABLE;
50 }
51 
UnregisterHook(LocationProcessStage stage,OhosHook hook)52 void HookUtils::UnregisterHook(LocationProcessStage stage, OhosHook hook)
53 {
54     HookMgrDel(GetLocationExtHookMgr(), static_cast<int>(stage), hook);
55 }
56 
ExecuteHook(LocationProcessStage stage,void * executionContext,const HOOK_EXEC_OPTIONS * options)57 LocationErrCode HookUtils::ExecuteHook(
58     LocationProcessStage stage, void *executionContext, const HOOK_EXEC_OPTIONS *options)
59 {
60     auto ret = HookMgrExecute(GetLocationExtHookMgr(), static_cast<int>(stage), executionContext, options);
61     if (ret == 0) {
62         return ERRCODE_SUCCESS;
63     }
64     LBSLOGE(LOCATOR, "%{public}s stage = %{public}d execute failed ret = %{public}d",
65         __func__, static_cast<int>(stage), ret);
66     return ERRCODE_SERVICE_UNAVAILABLE;
67 }
68 
ExecuteHookWhenStartLocation(std::shared_ptr<Request> request)69 void HookUtils::ExecuteHookWhenStartLocation(std::shared_ptr<Request> request)
70 {
71     LocationSupplicantInfo reportStruct;
72     reportStruct.request = *request;
73     reportStruct.retCode = true;
74     ExecuteHook(LocationProcessStage::LOCATOR_SA_START_LOCATING, (void *)&reportStruct, nullptr);
75 }
76 
ExecuteHookWhenStopLocation(std::shared_ptr<Request> request)77 void HookUtils::ExecuteHookWhenStopLocation(std::shared_ptr<Request> request)
78 {
79     LocationSupplicantInfo reportStruct;
80     reportStruct.request = *request;
81     reportStruct.retCode = true;
82     ExecuteHook(LocationProcessStage::REQUEST_MANAGER_HANDLE_STOP, (void *)&reportStruct, nullptr);
83 }
84 
ExecuteHookWhenGetAddressFromLocation(std::string packageName)85 void HookUtils::ExecuteHookWhenGetAddressFromLocation(std::string packageName)
86 {
87     LocationSupplicantInfo reportStruct;
88     reportStruct.abilityName = packageName;
89     reportStruct.retCode = true;
90     ExecuteHook(
91         LocationProcessStage::LOCATOR_SA_GET_ADDRESSES_FROM_LOCATION_PROCESS, (void *)&reportStruct, nullptr);
92 }
93 
ExecuteHookWhenGetAddressFromLocationName(std::string packageName)94 void HookUtils::ExecuteHookWhenGetAddressFromLocationName(std::string packageName)
95 {
96     LocationSupplicantInfo reportStruct;
97     reportStruct.abilityName = packageName;
98     reportStruct.retCode = true;
99     ExecuteHook(
100         LocationProcessStage::LOCATOR_SA_GET_ADDRESSES_FROM_LOCATIONNAME_PROCESS, (void *)&reportStruct, nullptr);
101 }
102 
ExecuteHookWhenReportInnerInfo(int32_t event,std::vector<std::string> & names,std::vector<std::string> & values)103 void HookUtils::ExecuteHookWhenReportInnerInfo(
104     int32_t event, std::vector<std::string>& names, std::vector<std::string>& values)
105 {
106     DfxInnerInfo innerInfo;
107     innerInfo.eventId = event;
108     innerInfo.names = names;
109     innerInfo.values = values;
110     ExecuteHook(LocationProcessStage::WRITE_DFX_INNER_EVENT_PROCESS, (void *)&innerInfo, nullptr);
111 }
112 
ExecuteHookWhenAddWorkRecord(bool stillState,bool idleState,std::string abilityName,std::string bundleName)113 bool HookUtils::ExecuteHookWhenAddWorkRecord(bool stillState, bool idleState, std::string abilityName,
114     std::string bundleName)
115 {
116     LocatorRequestStruct locatorRequestStruct;
117     locatorRequestStruct.deviceStillState = stillState;
118     locatorRequestStruct.deviceIdleState = idleState;
119     locatorRequestStruct.abilityName = abilityName;
120     locatorRequestStruct.bundleName = bundleName;
121     locatorRequestStruct.result = false;
122     ExecuteHook(
123         LocationProcessStage::ADD_REQUEST_TO_WORK_RECORD, (void *)&locatorRequestStruct, nullptr);
124     return locatorRequestStruct.result;
125 }
126 
ExecuteHookWhenCheckAppForUser(std::string packageName)127 bool HookUtils::ExecuteHookWhenCheckAppForUser(std::string packageName)
128 {
129     LocatorRequestStruct locatorRequestStruct;
130     locatorRequestStruct.bundleName = packageName;
131     locatorRequestStruct.result = false;
132     ExecuteHook(
133         LocationProcessStage::LOCATOR_SA_LOCATION_PERMISSION_CHECK, (void *)&locatorRequestStruct, nullptr);
134     return locatorRequestStruct.result;
135 }
136 
CheckGnssLocationValidity(const std::unique_ptr<Location> & location)137 bool HookUtils::CheckGnssLocationValidity(const std::unique_ptr<Location>& location)
138 {
139     GnssLocationValidStruct gnssLocationValidStruct;
140     gnssLocationValidStruct.location = *location;
141     gnssLocationValidStruct.result = true;
142     HookUtils::ExecuteHook(
143         LocationProcessStage::CHECK_GNSS_LOCATION_VALIDITY, (void *)&gnssLocationValidStruct, nullptr);
144     return gnssLocationValidStruct.result;
145 }
146 
ExecuteHookReportManagerGetCacheLocation(std::string packageName,int nlpRequestType)147 int HookUtils::ExecuteHookReportManagerGetCacheLocation(std::string packageName, int nlpRequestType)
148 {
149     LocatorRequestStruct locatorRequestStruct;
150     locatorRequestStruct.bundleName = packageName;
151     locatorRequestStruct.nlpRequestType = nlpRequestType;
152     locatorRequestStruct.result = false;
153     ExecuteHook(
154         LocationProcessStage::REPORT_MANAGER_GET_CACHE_LOCATION_PROCESS, (void *)&locatorRequestStruct, nullptr);
155     return locatorRequestStruct.cacheTime;
156 }
157 
ExecuteHookEnableAbility(std::string packageName,bool isEnabled,int32_t userId)158 bool HookUtils::ExecuteHookEnableAbility(std::string packageName, bool isEnabled, int32_t userId)
159 {
160     EnableAbilityStruct enableAbilityStruct;
161     enableAbilityStruct.bundleName = packageName;
162     enableAbilityStruct.isEnabled = isEnabled;
163     enableAbilityStruct.userId = userId;
164     enableAbilityStruct.result = true;
165     ExecuteHook(
166         LocationProcessStage::ENABLE_ABILITY_PROCESS, (void *)&enableAbilityStruct, nullptr);
167     return enableAbilityStruct.result;
168 }
169 
ExecuteHookWhenPreStartLocating(std::string packageName)170 bool HookUtils::ExecuteHookWhenPreStartLocating(std::string packageName)
171 {
172     LocatorRequestStruct locatorRequestStruct;
173     locatorRequestStruct.bundleName = packageName;
174     locatorRequestStruct.result = true;
175     ExecuteHook(
176         LocationProcessStage::PRE_START_LOCATING_PROCESS, (void *)&locatorRequestStruct, nullptr);
177     return locatorRequestStruct.result;
178 }
179 
ExecuteHookWhenAddNetworkRequest(std::string uuidTemp)180 bool HookUtils::ExecuteHookWhenAddNetworkRequest(std::string uuidTemp)
181 {
182     NetworkRequestInfo info;
183     info.uuid = uuidTemp;
184     info.result = false;
185     HookUtils::ExecuteHook(LocationProcessStage::NETWORK_SA_ADD_REQUEST_PROCESS, ((void *)(&info)), nullptr);
186     return info.result;
187 }
188 
ExecuteHookWhenRemoveNetworkRequest(std::string uuidTemp)189 bool HookUtils::ExecuteHookWhenRemoveNetworkRequest(std::string uuidTemp)
190 {
191     NetworkRequestInfo info;
192     info.uuid = uuidTemp;
193     info.result = false;
194     HookUtils::ExecuteHook(LocationProcessStage::NETWORK_SA_REMOVE_REQUEST_PROCESS, ((void *)(&info)), nullptr);
195     return info.result;
196 }
197 
ExecuteHookWhenSetAgnssServer(std::string & addrName,int & port)198 bool HookUtils::ExecuteHookWhenSetAgnssServer(std::string& addrName, int& port)
199 {
200     AgnssStruct agnssStruct;
201     agnssStruct.addrName = &addrName;
202     agnssStruct.port = &port;
203     agnssStruct.result = false;
204     ExecuteHook(LocationProcessStage::SET_AGNSS_SERVER_PROCESS, (void *)&agnssStruct, nullptr);
205     return agnssStruct.result;
206 }
207 
ExecuteHookWhenSimStateChange(const std::string & data)208 bool HookUtils::ExecuteHookWhenSimStateChange(const std::string& data)
209 {
210     LocatorRequestStruct locatorRequestStruct;
211     locatorRequestStruct.bundleName = data;
212     locatorRequestStruct.result = true;
213     ExecuteHook(
214         LocationProcessStage::SIM_STATE_CHANGED_PROCESS, (void *)&locatorRequestStruct, nullptr);
215     return locatorRequestStruct.result;
216 }
217 
ExecuteHookWhenStartScanBluetoothDevice(const std::string & packageName,const std::string & type)218 bool HookUtils::ExecuteHookWhenStartScanBluetoothDevice(const std::string& packageName, const std::string& type)
219 {
220     ScanStruct scanStruct;
221     scanStruct.packageName = packageName;
222     scanStruct.type = type;
223     scanStruct.result = false;
224     ExecuteHook(
225         LocationProcessStage::START_SCAN_BLUETOOTH_DEVICE_PROCESS, (void *)&scanStruct, nullptr);
226     return scanStruct.result;
227 }
228 
ExecuteHookWhenReportBluetoothScanResult(const std::string & packageName,const std::string & type)229 bool HookUtils::ExecuteHookWhenReportBluetoothScanResult(const std::string& packageName, const std::string& type)
230 {
231     ScanStruct scanStruct;
232     scanStruct.packageName = packageName;
233     scanStruct.type = type;
234     scanStruct.result = false;
235     ExecuteHook(
236         LocationProcessStage::REPORT_BLUETOOTH_SCAN_RESULT_PROCESS, (void *)&scanStruct, nullptr);
237     return scanStruct.result;
238 }
239 
ExecuteHookWhenCustConfigPolicyChange()240 bool HookUtils::ExecuteHookWhenCustConfigPolicyChange()
241 {
242     ExecuteHook(
243         LocationProcessStage::CUST_CONFIG_POLICY_CHANGE_PROCESS, nullptr, nullptr);
244     return true;
245 }
246 
ExecuteHookWhenCheckIsAppBackground(const std::string & packageName)247 bool HookUtils::ExecuteHookWhenCheckIsAppBackground(const std::string& packageName)
248 {
249     LocatorRequestStruct locatorRequestStruct;
250     locatorRequestStruct.bundleName = packageName;
251     locatorRequestStruct.result = true;
252     ExecuteHook(
253         LocationProcessStage::IS_APP_BACKGROUND_PROCESS, (void *)&locatorRequestStruct, nullptr);
254     return locatorRequestStruct.result;
255 }
256 
ExecuteHookWhenSyncSwitchStates(int status)257 bool HookUtils::ExecuteHookWhenSyncSwitchStates(int status)
258 {
259     LocationStatusStruct statusStruct;
260     if (status == 1) {
261         statusStruct.locationEnable = true;
262     } else {
263         statusStruct.locationEnable = false;
264     }
265     ExecuteHook(
266         LocationProcessStage::LOCATOR_SA_SYNC_SWITCH_STATUS, (void *)&statusStruct, nullptr);
267     return true;
268 }
269 
ExecuteHookWhenReportBeaconFenceOperateResult(const std::string & fenceId,int transitionEvent,const std::string & fenceExtensionAbilityName,const std::string & packageName)270 bool HookUtils::ExecuteHookWhenReportBeaconFenceOperateResult(const std::string& fenceId, int transitionEvent,
271     const std::string& fenceExtensionAbilityName, const std::string& packageName)
272 {
273     BeaconFenceStruct beaconFenceStruct;
274     beaconFenceStruct.fenceId = fenceId;
275     beaconFenceStruct.transitionEvent = transitionEvent;
276     beaconFenceStruct.fenceExtensionAbilityName = fenceExtensionAbilityName;
277     beaconFenceStruct.packageName = packageName;
278     beaconFenceStruct.result = true;
279     ExecuteHook(
280         LocationProcessStage::REPORT_BEACON_FENCE_OPERATE_RESULT_PROCESS, (void *)&beaconFenceStruct, nullptr);
281     return beaconFenceStruct.result;
282 }
283 
ExecuteHookWhenRemoveBeaconFenceByCallback(const std::string & packageName)284 bool HookUtils::ExecuteHookWhenRemoveBeaconFenceByCallback(const std::string& packageName)
285 {
286     BeaconFenceStruct beaconFenceStruct;
287     beaconFenceStruct.result = false;
288     beaconFenceStruct.packageName = packageName;
289     ExecuteHook(
290         LocationProcessStage::BEACON_FENCE_TRANSITION_STATUS_CHANGE_PROCESS, (void *)&beaconFenceStruct, nullptr);
291     return beaconFenceStruct.result;
292 }
293 
ExecuteHookWhenBeaconFenceTransitionStatusChange(const std::string & packageName)294 bool HookUtils::ExecuteHookWhenBeaconFenceTransitionStatusChange(const std::string& packageName)
295 {
296     BeaconFenceStruct beaconFenceStruct;
297     beaconFenceStruct.result = false;
298     beaconFenceStruct.packageName = packageName;
299     ExecuteHook(
300         LocationProcessStage::BEACON_FENCE_TRANSITION_STATUS_CHANGE_PROCESS, (void *)&beaconFenceStruct, nullptr);
301     return beaconFenceStruct.result;
302 }
303 } // namespace Location
304 } // namespace OHOS
305