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