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 GNSS_ABILITY_H 17 #define GNSS_ABILITY_H 18 #ifdef FEATURE_GNSS_SUPPORT 19 20 #include <mutex> 21 #include <singleton.h> 22 #include <v2_0/ignss_interface.h> 23 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 24 #include <v2_0/igeofence_interface.h> 25 #endif 26 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 27 #include <v2_0/ia_gnss_interface.h> 28 #endif 29 30 #include "event_handler.h" 31 #include "ffrt.h" 32 #include "system_ability.h" 33 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 34 #include "agnss_event_callback.h" 35 #endif 36 #include "common_utils.h" 37 #include "constant_definition.h" 38 #include "gnss_ability_skeleton.h" 39 #include "i_gnss_status_callback.h" 40 #include "i_nmea_message_callback.h" 41 #include "subability_common.h" 42 #include "i_gnss_geofence_callback.h" 43 #include "geofence_request.h" 44 #include "locationhub_ipc_interface_code.h" 45 #include "geofence_event_callback.h" 46 #include "ipc_skeleton.h" 47 48 #ifdef TIME_SERVICE_ENABLE 49 #include "time_manager.h" 50 #endif 51 #ifdef NET_MANAGER_ENABLE 52 #include "net_conn_observer.h" 53 #include "net_conn_client.h" 54 #include "net_specifier.h" 55 #endif 56 57 namespace OHOS { 58 namespace Location { 59 #ifdef __aarch64__ 60 #define VENDOR_GNSS_ADAPTER_SO_PATH "/system/lib64/vendorGnssAdapter.so" 61 #else 62 #define VENDOR_GNSS_ADAPTER_SO_PATH "/system/lib/vendorGnssAdapter.so" 63 #endif 64 65 using HDI::Location::Gnss::V2_0::IGnssInterface; 66 using HDI::Location::Gnss::V2_0::IGnssCallback; 67 using HDI::Location::Gnss::V2_0::GNSS_START_TYPE_NORMAL; 68 using HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_NONE; 69 using HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_SESSION_BEGIN; 70 using HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_SESSION_END; 71 using HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_ENGINE_ON; 72 using HDI::Location::Gnss::V2_0::GNSS_WORKING_STATUS_ENGINE_OFF; 73 using HDI::Location::Gnss::V2_0::GnssAuxiliaryDataType; 74 using HDI::Location::Gnss::V2_0::GnssWorkingMode; 75 using HDI::Location::Gnss::V2_0::GnssConfigPara; 76 using HDI::Location::Gnss::V2_0::GnssRefInfoType; 77 using HDI::Location::Gnss::V2_0::GnssRefInfo; 78 using HDI::Location::Gnss::V2_0::GnssLocationValidity; 79 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 80 using HDI::Location::Agnss::V2_0::IAGnssInterface; 81 using HDI::Location::Agnss::V2_0::IAGnssCallback; 82 using HDI::Location::Agnss::V2_0::AGNSS_TYPE_SUPL; 83 using HDI::Location::Agnss::V2_0::AGnssServerInfo; 84 #endif 85 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 86 using HDI::Location::Geofence::V2_0::IGeofenceInterface; 87 using HDI::Location::Geofence::V2_0::IGeofenceCallback; 88 using HDI::Location::Geofence::V2_0::GeofenceEvent; 89 using HDI::Location::Geofence::V2_0::GeofenceInfo; 90 #endif 91 92 enum class GnssAbilityInterfaceCode { 93 EVENT_REPORT_MOCK_LOCATION = 0x0100, 94 SET_SUBSCRIBER_SET_ID = 0x0101, 95 SET_AGNSS_REF_INFO = 0x0102, 96 RECONNECT_HDI = 0x0103, 97 INIT_HDI = 0x0104, 98 ADD_FENCE = 0x0105, 99 REMOVE_FENCE = 0x0106, 100 ADD_GEOFENCE = 0x0107, 101 REMOVE_GEOFENCE = 0x0108, 102 }; 103 104 typedef struct { 105 std::shared_ptr<GeofenceRequest> request; 106 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 107 sptr<IGeofenceCallback> callback; 108 #endif 109 int requestCode; 110 int retCode; 111 std::vector<CoordinateSystemType> coordinateSystemTypes; 112 } FenceStruct; 113 114 class GnssHandler : public AppExecFwk::EventHandler { 115 public: 116 explicit GnssHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 117 ~GnssHandler() override; 118 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 119 120 private: 121 void InitGnssEventProcessMap(); 122 void HandleReportMockLocation(const AppExecFwk::InnerEvent::Pointer& event); 123 void HandleSendLocationRequest(const AppExecFwk::InnerEvent::Pointer& event); 124 void HandleSetMockedLocations(const AppExecFwk::InnerEvent::Pointer& event); 125 void HandleSendCommands(const AppExecFwk::InnerEvent::Pointer& event); 126 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 127 void HandleSetSubscriberSetId(const AppExecFwk::InnerEvent::Pointer& event); 128 void HandleSetAgnssRefInfo(const AppExecFwk::InnerEvent::Pointer& event); 129 #endif 130 void HandleReconnectHdi(const AppExecFwk::InnerEvent::Pointer& event); 131 void HandleSetEnable(const AppExecFwk::InnerEvent::Pointer& event); 132 void HandleInitHdi(const AppExecFwk::InnerEvent::Pointer& event); 133 void HandleAddFence(const AppExecFwk::InnerEvent::Pointer& event); 134 void HandleRemoveFence(const AppExecFwk::InnerEvent::Pointer& event); 135 void HandleAddGeofence(const AppExecFwk::InnerEvent::Pointer& event); 136 void HandleRemoveGeofence(const AppExecFwk::InnerEvent::Pointer& event); 137 void HandleSendNetworkLocation(const AppExecFwk::InnerEvent::Pointer& event); 138 139 using GnssEventProcessHandle = std::function<void(const AppExecFwk::InnerEvent::Pointer &)>; 140 using GnssEventProcessMap = std::map<uint32_t, GnssEventProcessHandle>; 141 GnssEventProcessMap gnssEventProcessMap_; 142 }; 143 144 class GnssAbility : public SystemAbility, public GnssAbilityStub, public SubAbility { 145 DECLEAR_SYSTEM_ABILITY(GnssAbility); 146 147 public: 148 DISALLOW_COPY_AND_MOVE(GnssAbility); 149 static GnssAbility* GetInstance(); 150 151 GnssAbility(); 152 ~GnssAbility() override; 153 void OnStart() override; 154 void OnStop() override; QueryServiceState()155 ServiceRunningState QueryServiceState() const 156 { 157 return state_; 158 } 159 LocationErrCode SendLocationRequest(WorkRecord &workrecord) override; 160 LocationErrCode SetEnable(bool state) override; 161 LocationErrCode RefrashRequirements() override; 162 LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity) override; 163 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback) override; 164 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity) override; 165 LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback) override; 166 LocationErrCode RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request, 167 const sptr<IRemoteObject>& callback) override; 168 LocationErrCode UnregisterCachedCallback(const sptr<IRemoteObject>& callback) override; 169 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 170 LocationErrCode GetCachedGnssLocationsSize(int &size) override; 171 LocationErrCode FlushCachedGnssLocations() override; 172 LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands) override; 173 LocationErrCode AddFence(std::shared_ptr<GeofenceRequest>& request) override; 174 LocationErrCode RemoveFence(std::shared_ptr<GeofenceRequest>& request) override; 175 LocationErrCode AddGnssGeofence(std::shared_ptr<GeofenceRequest>& request) override; 176 LocationErrCode RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request) override; 177 void ReportGnssSessionStatus(int status); 178 void ReportNmea(int64_t timestamp, const std::string &nmea); 179 void ReportSv(const std::unique_ptr<SatelliteStatus> &sv); 180 LocationErrCode EnableMock() override; 181 LocationErrCode DisableMock() override; 182 LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override; 183 void RequestRecord(WorkRecord &workRecord, bool isAdded) override; 184 void SendReportMockLocationEvent() override; 185 void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override; 186 bool CancelIdleState() override; 187 void UnloadGnssSystemAbility() override; 188 void StartGnss(); 189 void StopGnss(); 190 bool EnableGnss(); 191 void DisableGnss(); 192 bool ConnectHdi(); 193 bool RemoveHdi(); 194 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 195 void SetAgnssServer(); 196 void SetAgnssCallback(); 197 void SetSetId(const SubscriberSetId& id); 198 void SetSetIdImpl(const SubscriberSetId &id); 199 void SetRefInfo(const AGnssRefInfo& refInfo); 200 void SetRefInfoImpl(const AGnssRefInfo &refInfo); 201 #endif 202 void ReConnectHdiImpl(); 203 bool IsMockEnabled(); 204 void ProcessReportLocationMock(); 205 void ReConnectHdi(); 206 bool CheckIfHdiConnected(); 207 void RestGnssWorkStatus(); 208 bool RegisterGnssGeofenceCallback(std::shared_ptr<GeofenceRequest> &request, 209 const sptr<IRemoteObject>& callback); 210 bool UnregisterGnssGeofenceCallback(int fenceId); 211 std::shared_ptr<GeofenceRequest> GetGeofenceRequestByFenceId(int fenceId); 212 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 213 void ReportGeofenceEvent(int fenceId, GeofenceEvent event); 214 void ReportGeofenceOperationResult( 215 int fenceId, GeofenceOperateType type, GeofenceOperateResult result); 216 #endif 217 bool RemoveGnssGeofenceRequestByCallback(sptr<IRemoteObject> callbackObj); 218 LocationErrCode QuerySupportCoordinateSystemType( 219 std::vector<CoordinateSystemType>& coordinateSystemTypes) override; 220 LocationErrCode SendNetworkLocation(const std::unique_ptr<Location>& location) override; 221 LocationErrCode InjectLocation(); 222 LocationErrCode InjectTime(); 223 LocationErrCode UpdateNtpTime(int64_t ntpTime, int64_t elapsedTime); 224 void MonitorNetwork(); 225 226 private: 227 bool Init(); 228 static void SaDumpInfo(std::string& result); 229 bool IsGnssEnabled(); 230 int32_t ReportMockedLocation(const std::shared_ptr<Location> location); 231 bool CheckIfGnssConnecting(); 232 bool IsMockProcessing(); 233 void RegisterLocationHdiDeathRecipient(); 234 bool GetCommandFlags(std::unique_ptr<LocationCommand>& commands, GnssAuxiliaryDataType& flags); 235 LocationErrCode SetPositionMode(); 236 void SendEvent(AppExecFwk::InnerEvent::Pointer& event, MessageParcel &reply); 237 bool ExecuteFenceProcess( 238 GnssInterfaceCode code, std::shared_ptr<GeofenceRequest>& request); 239 int32_t GenerateFenceId(); 240 bool IsGnssfenceRequestMapExist(); 241 bool CheckBundleNameInGnssGeofenceRequestMap(const std::string& bundleName, int fenceId); 242 bool ConnectGnssHdi(); 243 244 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 245 bool ConnectAgnssHdi(); 246 #endif 247 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 248 bool SetGeofenceCallback(); 249 bool ConnectGeofenceHdi(); 250 #endif 251 bool IsDeviceLoaded(const std::string &servName); 252 253 size_t mockLocationIndex_ = 0; 254 bool registerToAbility_ = false; 255 int gnssWorkingStatus_ = 0; 256 std::shared_ptr<GnssHandler> gnssHandler_; 257 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 258 ffrt::mutex gnssMutex_; 259 ffrt::mutex nmeaMutex_; 260 ffrt::mutex hdiMutex_; 261 ffrt::mutex statusMutex_; 262 std::map<sptr<IRemoteObject>, AppIdentity> gnssStatusCallbackMap_; 263 std::map<sptr<IRemoteObject>, AppIdentity> nmeaCallbackMap_; 264 sptr<IGnssCallback> gnssCallback_; 265 Location nlpLocation_; 266 #ifdef TIME_SERVICE_ENABLE 267 TimeManager ntpTime_; 268 #endif 269 #ifdef NET_MANAGER_ENABLE 270 sptr<NetConnObserver> netWorkObserver_ = nullptr; 271 #endif 272 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 273 sptr<IAGnssCallback> agnssCallback_; 274 #endif 275 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 276 sptr<IGeofenceCallback> geofenceCallback_; 277 #endif 278 int32_t fenceId_; 279 ffrt::mutex fenceIdMutex_; 280 ffrt::mutex gnssGeofenceRequestMapMutex_; 281 std::map<std::shared_ptr<GeofenceRequest>, 282 std::pair<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>> gnssGeofenceRequestMap_; 283 }; 284 285 class LocationHdiDeathRecipient : public IRemoteObject::DeathRecipient { 286 public: 287 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 288 LocationHdiDeathRecipient(); 289 ~LocationHdiDeathRecipient() override; 290 }; 291 292 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 293 class AgnssRefInfoMessage { 294 public: 295 void SetAgnssRefInfo(const AGnssRefInfo &refInfo); 296 AGnssRefInfo GetAgnssRefInfo(); 297 298 private: 299 AGnssRefInfo agnssRefInfo_; 300 }; 301 #endif 302 } // namespace Location 303 } // namespace OHOS 304 #endif // FEATURE_GNSS_SUPPORT 305 #endif // GNSS_ABILITY_H 306