• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOCATOR_ABILITY_H
17 #define LOCATOR_ABILITY_H
18 
19 #include <map>
20 #include <mutex>
21 #include <singleton.h>
22 
23 #include "event_handler.h"
24 #include "ffrt.h"
25 #include "system_ability.h"
26 
27 #include "app_identity.h"
28 #include "common_utils.h"
29 #include "geo_coding_mock_info.h"
30 #include "i_switch_callback.h"
31 #include "i_cached_locations_callback.h"
32 #include "i_bluetooth_scan_result_callback.h"
33 #include "locator_event_subscriber.h"
34 #include "locator_skeleton.h"
35 #include "permission_status_change_cb.h"
36 #include "request.h"
37 #include "request_manager.h"
38 #include "report_manager.h"
39 #include "want_agent_helper.h"
40 #include "geofence_request.h"
41 #include "common_event_support.h"
42 #ifdef MOVEMENT_CLIENT_ENABLE
43 #include "locator_msdp_monitor_manager.h"
44 #endif
45 #include "proxy_freeze_manager.h"
46 
47 namespace OHOS {
48 namespace Location {
49 class LocatorHandler : public AppExecFwk::EventHandler {
50 public:
51     using LocatorEventHandle = std::function<void(const AppExecFwk::InnerEvent::Pointer &)>;
52     using LocatorEventHandleMap = std::map<int, LocatorEventHandle>;
53     explicit LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
54     ~LocatorHandler() override;
55     void InitLocatorHandlerEventMap();
56     void ConstructDbHandleMap();
57     void ConstructGeocodeHandleMap();
58     void ConstructBluetoohScanHandleMap();
59 private:
60     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
61     void UpdateSaEvent(const AppExecFwk::InnerEvent::Pointer& event);
62     void InitRequestManagerEvent(const AppExecFwk::InnerEvent::Pointer& event);
63     void ApplyRequirementsEvent(const AppExecFwk::InnerEvent::Pointer& event);
64     void RetryRegisterActionEvent(const AppExecFwk::InnerEvent::Pointer& event);
65     void ReportLocationMessageEvent(const AppExecFwk::InnerEvent::Pointer& event);
66     void SendSwitchStateToHifenceEvent(const AppExecFwk::InnerEvent::Pointer& event);
67     void UnloadSaEvent(const AppExecFwk::InnerEvent::Pointer& event);
68     void StartLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event);
69     void StopLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event);
70     void GetCachedLocationSuccess(const AppExecFwk::InnerEvent::Pointer& event);
71     void GetCachedLocationFailed(const AppExecFwk::InnerEvent::Pointer& event);
72     void StartScanBluetoohDeviceEvent(const AppExecFwk::InnerEvent::Pointer& event);
73     void StopScanBluetoohDeviceEvent(const AppExecFwk::InnerEvent::Pointer& event);
74     void RegLocationErrorEvent(const AppExecFwk::InnerEvent::Pointer& event);
75     void UnRegLocationErrorEvent(const AppExecFwk::InnerEvent::Pointer& event);
76     void ReportNetworkLocatingErrorEvent(const AppExecFwk::InnerEvent::Pointer& event);
77     void RequestCheckEvent(const AppExecFwk::InnerEvent::Pointer& event);
78     void SyncStillMovementState(const AppExecFwk::InnerEvent::Pointer& event);
79     void SyncIdleState(const AppExecFwk::InnerEvent::Pointer& event);
80     void SendGeoRequestEvent(const AppExecFwk::InnerEvent::Pointer& event);
81     void SyncSwitchStatus(const AppExecFwk::InnerEvent::Pointer& event);
82     void InitSaAbilityEvent(const AppExecFwk::InnerEvent::Pointer& event);
83     void InitMonitorManagerEvent(const AppExecFwk::InnerEvent::Pointer& event);
84     void IsStandByEvent(const AppExecFwk::InnerEvent::Pointer& event);
85     void SetLocationWorkingStateEvent(const AppExecFwk::InnerEvent::Pointer& event);
86     void SetSwitchStateToDbEvent(const AppExecFwk::InnerEvent::Pointer& event);
87     void SetSwitchStateToDbForUserEvent(const AppExecFwk::InnerEvent::Pointer& event);
88     void WatchSwitchParameter(const AppExecFwk::InnerEvent::Pointer& event);
89     LocatorEventHandleMap locatorHandlerEventMap_;
90 
91     bool IsSwitchObserverReg();
92     void SetIsSwitchObserverReg(bool isSwitchObserverReg);
93     ffrt::mutex isSwitchObserverRegMutex_;
94     bool isSwitchObserverReg_ = false;
95 };
96 
97 typedef struct {
98     bool state;
99     int64_t timeSinceBoot;
100 } AppSwitchIgnoredState;
101 
102 class LocatorAbility : public SystemAbility, public LocatorAbilityStub {
103 DECLEAR_SYSTEM_ABILITY(LocatorAbility);
104 
105 public:
106     DISALLOW_COPY_AND_MOVE(LocatorAbility);
107     static LocatorAbility* GetInstance();
108     LocatorAbility();
109     ~LocatorAbility() override;
110     void OnStart() override;
111     void OnStop() override;
112     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
113     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
114     bool CancelIdleState(uint32_t code) override;
115     void RemoveUnloadTask(uint32_t code) override;
116     void PostUnloadTask(uint32_t code) override;
QueryServiceState()117     ServiceRunningState QueryServiceState() const
118     {
119         return state_;
120     }
121     void InitSaAbility();
122     void InitRequestManagerMap();
123 
124     LocationErrCode UpdateSaAbility();
125     LocationErrCode GetSwitchState(int& state);
126     LocationErrCode EnableAbility(bool isEnabled);
127     LocationErrCode EnableAbilityForUser(bool isEnabled, int32_t userId);
128     LocationErrCode RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid);
129     LocationErrCode UnregisterSwitchCallback(const sptr<IRemoteObject>& callback);
130 #ifdef FEATURE_GNSS_SUPPORT
131     LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity);
132     LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback);
133     LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity);
134     LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback);
135     LocationErrCode RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
136         sptr<ICachedLocationsCallback>& callback, std::string bundleName);
137     LocationErrCode UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback);
138     LocationErrCode GetCachedGnssLocationsSize(int& size);
139     LocationErrCode FlushCachedGnssLocations();
140     LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands);
141     LocationErrCode AddFence(std::shared_ptr<GeofenceRequest>& request);
142     LocationErrCode RemoveFence(std::shared_ptr<GeofenceRequest>& request);
143     LocationErrCode AddGnssGeofence(std::shared_ptr<GeofenceRequest>& request);
144     LocationErrCode RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request);
145 #endif
146     LocationErrCode StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
147         sptr<ILocatorCallback>& callback, AppIdentity &identity);
148     LocationErrCode StopLocating(sptr<ILocatorCallback>& callback);
149     LocationErrCode GetCacheLocation(std::unique_ptr<Location>& loc, AppIdentity &identity);
150 #ifdef FEATURE_GEOCODE_SUPPORT
151     LocationErrCode IsGeoConvertAvailable(bool &isAvailable);
152     void GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply, std::string bundleName);
153     void GetAddressByLocationName(MessageParcel &data, MessageParcel &reply, std::string bundleName);
154     LocationErrCode EnableReverseGeocodingMock();
155     LocationErrCode DisableReverseGeocodingMock();
156     LocationErrCode SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo);
157 #endif
158     LocationErrCode IsLocationPrivacyConfirmed(const int type, bool& isConfirmed);
159     LocationErrCode SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed);
160     LocationErrCode EnableLocationMock();
161     LocationErrCode DisableLocationMock();
162     LocationErrCode SetMockedLocations(
163         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location);
164     LocationErrCode ReportLocation(
165         const std::unique_ptr<Location>& location, std::string abilityName, AppIdentity &identity);
166     LocationErrCode ReportLocationStatus(sptr<ILocatorCallback>& callback, int result);
167     LocationErrCode ReportErrorStatus(sptr<ILocatorCallback>& callback, int result);
168     LocationErrCode ProcessLocationMockMsg(
169         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId);
170 #ifdef FEATURE_GNSS_SUPPORT
171     LocationErrCode SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj,
172         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId);
173 #endif
174 #ifdef FEATURE_NETWORK_SUPPORT
175     LocationErrCode SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj,
176         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId);
177 #endif
178 #ifdef FEATURE_PASSIVE_SUPPORT
179     LocationErrCode SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj,
180         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId);
181 #endif
182     LocationErrCode RegisterWifiScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid);
183     LocationErrCode UnregisterWifiScanInfoCallback(const sptr<IRemoteObject>& callback);
184     LocationErrCode RegisterBluetoothScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid);
185     LocationErrCode UnregisterBluetoothScanInfoCallback(const sptr<IRemoteObject>& callback);
186     LocationErrCode RegisterBleScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid);
187     LocationErrCode UnregisterBleScanInfoCallback(const sptr<IRemoteObject>& callback);
188     LocationErrCode StartScanBluetoohDevice(sptr<IBluetoohScanResultCallback>& callback, AppIdentity &identity);
189     LocationErrCode StopScanBluetoohDevice(sptr<IBluetoohScanResultCallback>& callback, AppIdentity &identity);
190     LocationErrCode RegisterLocationError(sptr<ILocatorCallback>& callback, AppIdentity &identity);
191     LocationErrCode UnregisterLocationError(sptr<ILocatorCallback>& callback, AppIdentity &identity);
192     void ReportLocationError(std::string uuid, int32_t errCode, int32_t netErrCode);
193     LocationErrCode SetLocationSwitchIgnored(bool isEnabled, AppIdentity &identity);
194 
195     std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> GetRequests();
196     std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> GetReceivers();
197     std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> GetProxyMap();
198     void UpdateSaAbilityHandler();
199     void ApplyRequests(int delay);
200     void RegisterAction();
201     void RegisterLocationPrivacyAction();
202     LocationErrCode ProxyForFreeze(std::set<int> pidList, bool isProxy);
203     LocationErrCode ResetAllProxy();
204     int GetActiveRequestNum();
205     void RegisterPermissionCallback(const uint32_t callingTokenId, const std::vector<std::string>& permissionNameList);
206     void UnregisterPermissionCallback(const uint32_t callingTokenId);
207     int UpdatePermissionUsedRecord(uint32_t tokenId, std::string permissionName,
208         int permUsedType, int succCnt, int failCnt);
209     LocationErrCode RemoveInvalidRequests();
210     bool IsInvalidRequest(std::shared_ptr<Request>& request);
211     bool IsProcessRunning(pid_t pid, const uint32_t tokenId);
212 #ifdef FEATURE_GNSS_SUPPORT
213     LocationErrCode QuerySupportCoordinateSystemType(
214         std::vector<CoordinateSystemType>& coordinateSystemTypes);
215     LocationErrCode SendNetworkLocation(const std::unique_ptr<Location>& location);
216 #endif
217     void SyncStillMovementState(bool stillState);
218     void SyncIdleState(bool stillState);
219 #ifdef FEATURE_GEOCODE_SUPPORT
220     LocationErrCode SendGeoRequest(int type, MessageParcel &data, MessageParcel &reply);
221 #endif
222     void ReportDataToResSched(std::string state);
223     bool IsHapCaller(const uint32_t tokenId);
224     void HandleStartLocating(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback);
225     bool GetLocationSwitchIgnoredFlag(uint32_t tokenId);
226 
227 private:
228     bool Init();
229     bool CheckSaValid();
230 #ifdef FEATURE_GNSS_SUPPORT
231     LocationErrCode SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply);
232 #endif
233     void UpdateProxyMap();
234     bool CheckIfLocatorConnecting();
235     void UpdateLoadedSaMap();
236     bool NeedReportCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback);
237     bool ReportSingleCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback,
238         std::unique_ptr<Location>& cacheLocation);
239     bool ReportCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback,
240         std::unique_ptr<Location>& cacheLocation);
241     bool IsCacheVaildScenario(const sptr<RequestConfig>& requestConfig);
242     bool IsSingleRequest(const sptr<RequestConfig>& requestConfig);
243     void SendSwitchState(const int state);
244     bool SetLocationhubStateToSyspara(int value);
245     void SetLocationSwitchIgnoredFlag(uint32_t tokenId, bool enable);
246 
247     bool registerToAbility_ = false;
248     bool isActionRegistered = false;
249     bool isLocationPrivacyActionRegistered_ = false;
250     std::string deviceId_;
251     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
252     std::shared_ptr<LocatorEventSubscriber> locatorEventSubscriber_;
253     std::shared_ptr<LocatorEventSubscriber> locationPrivacyEventSubscriber_;
254     std::mutex switchMutex_;
255     ffrt::mutex requestsMutex_;
256     ffrt::mutex receiversMutex_;
257     std::mutex proxyMapMutex_;
258     ffrt::mutex permissionMapMutex_;
259     ffrt::mutex loadedSaMapMutex_;
260     std::unique_ptr<std::map<pid_t, sptr<ISwitchCallback>>> switchCallbacks_;
261     std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> requests_;
262     std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> receivers_;
263     std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> proxyMap_;
264     std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> loadedSaMap_;
265     std::shared_ptr<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>> permissionMap_;
266     std::shared_ptr<LocatorHandler> locatorHandler_;
267     RequestManager* requestManager_;
268     ReportManager* reportManager_;
269     std::mutex proxyPidsMutex_;
270     std::set<int32_t> proxyPids_;
271     std::map<uint32_t, AppSwitchIgnoredState> locationSettingsIgnoredFlagMap_;
272     std::mutex LocationSwitchIgnoredFlagMutex_;
273     std::mutex testMutex_;
274 };
275 
276 class LocationMessage {
277 public:
278     void SetAbilityName(std::string abilityName);
279     std::string GetAbilityName();
280     void SetLocation(const std::unique_ptr<Location>& location);
281     std::unique_ptr<Location> GetLocation();
282 private:
283     std::string abilityName_;
284     std::unique_ptr<Location> location_;
285 };
286 
287 class LocatorCallbackMessage {
288 public:
289     void SetCallback(const sptr<ILocatorCallback>& callback);
290     sptr<ILocatorCallback> GetCallback();
291     void SetAppIdentity(AppIdentity& appIdentity);
292     AppIdentity GetAppIdentity();
293 private:
294     std::string abilityName_;
295     AppIdentity appIdentity_;
296     sptr<ILocatorCallback> callback_;
297 };
298 
299 class BluetoothScanResultCallbackMessage {
300 public:
301     void SetCallback(const sptr<IBluetoohScanResultCallback>& callback);
302     sptr<IBluetoohScanResultCallback> GetCallback();
303     void SetAppIdentity(AppIdentity& appIdentity);
304     AppIdentity GetAppIdentity();
305 private:
306     sptr<IBluetoohScanResultCallback> callback_;
307     AppIdentity appIdentity_;
308 };
309 
310 class LocatorErrorMessage {
311 public:
312     void SetUuid(std::string uuid);
313     std::string GetUuid();
314     void SetErrCode(int32_t errCode);
315     int32_t GetErrCode();
316     void SetNetErrCode(int32_t netErrCode);
317     int32_t GetNetErrCode();
318 private:
319     std::string uuid_;
320     int32_t errCode_;
321     int32_t netErrCode_;
322 };
323 
324 class LocatorSwitchMessage {
325 public:
326     void SetUserId(int32_t userId);
327     int32_t GetUserId();
328     void SetModeValue(int32_t modeValue);
329     int32_t GetModeValue();
330 private:
331     int32_t modeValue_;
332     int32_t userId_;
333 };
334 
335 class LocatorCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
336 public:
337     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
338     LocatorCallbackDeathRecipient(int32_t tokenId);
339     ~LocatorCallbackDeathRecipient() override;
340 private:
341     int32_t tokenId_;
342 };
343 } // namespace Location
344 } // namespace OHOS
345 #endif // LOCATOR_ABILITY_H
346