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