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, AppIdentity &identity); 123 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback); 124 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity); 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 void HandleStartLocating(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback); 215 216 private: 217 bool Init(); 218 bool CheckSaValid(); 219 #ifdef FEATURE_GNSS_SUPPORT 220 LocationErrCode SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply); 221 #endif 222 void UpdateProxyMap(); 223 bool CheckIfLocatorConnecting(); 224 void UpdateLoadedSaMap(); 225 bool NeedReportCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback); 226 bool ReportSingleCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback, 227 std::unique_ptr<Location>& cacheLocation); 228 bool ReportCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback, 229 std::unique_ptr<Location>& cacheLocation); 230 bool IsCacheVaildScenario(const sptr<RequestConfig>& requestConfig); 231 bool IsSingleRequest(const sptr<RequestConfig>& requestConfig); 232 void SendSwitchState(const int state); 233 bool SetLocationhubStateToSyspara(int value); 234 235 bool registerToAbility_ = false; 236 bool isActionRegistered = false; 237 bool isLocationPrivacyActionRegistered_ = false; 238 std::string deviceId_; 239 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 240 std::shared_ptr<LocatorEventSubscriber> locatorEventSubscriber_; 241 std::shared_ptr<LocatorEventSubscriber> locationPrivacyEventSubscriber_; 242 std::mutex switchMutex_; 243 ffrt::mutex requestsMutex_; 244 ffrt::mutex receiversMutex_; 245 std::mutex proxyMapMutex_; 246 ffrt::mutex permissionMapMutex_; 247 ffrt::mutex loadedSaMapMutex_; 248 std::unique_ptr<std::map<pid_t, sptr<ISwitchCallback>>> switchCallbacks_; 249 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> requests_; 250 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> receivers_; 251 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> proxyMap_; 252 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> loadedSaMap_; 253 std::shared_ptr<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>> permissionMap_; 254 std::shared_ptr<LocatorHandler> locatorHandler_; 255 RequestManager* requestManager_; 256 ReportManager* reportManager_; 257 std::mutex proxyPidsMutex_; 258 std::set<int32_t> proxyPids_; 259 }; 260 261 class LocationMessage { 262 public: 263 void SetAbilityName(std::string abilityName); 264 std::string GetAbilityName(); 265 void SetLocation(const std::unique_ptr<Location>& location); 266 std::unique_ptr<Location> GetLocation(); 267 private: 268 std::string abilityName_; 269 std::unique_ptr<Location> location_; 270 }; 271 272 class LocatorCallbackMessage { 273 public: 274 void SetCallback(const sptr<ILocatorCallback>& callback); 275 sptr<ILocatorCallback> GetCallback(); 276 void SetAppIdentity(AppIdentity& appIdentity); 277 AppIdentity GetAppIdentity(); 278 private: 279 std::string abilityName_; 280 AppIdentity appIdentity_; 281 sptr<ILocatorCallback> callback_; 282 }; 283 284 class LocatorErrorMessage { 285 public: 286 void SetUuid(std::string uuid); 287 std::string GetUuid(); 288 void SetErrCode(int32_t errCode); 289 int32_t GetErrCode(); 290 private: 291 std::string uuid_; 292 int32_t errCode_; 293 }; 294 295 class LocatorSwitchMessage { 296 public: 297 void SetUserId(int32_t userId); 298 int32_t GetUserId(); 299 void SetModeValue(int32_t modeValue); 300 int32_t GetModeValue(); 301 private: 302 int32_t modeValue_; 303 int32_t userId_; 304 }; 305 306 class LocatorCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 307 public: 308 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 309 LocatorCallbackDeathRecipient(int32_t tokenId); 310 ~LocatorCallbackDeathRecipient() override; 311 private: 312 int32_t tokenId_; 313 }; 314 } // namespace Location 315 } // namespace OHOS 316 #endif // LOCATOR_ABILITY_H 317