1 /* 2 * Copyright (C) 2024 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 using GnssEventProcessHandle = std::function<void(const AppExecFwk::InnerEvent::Pointer &)>; 123 using GnssEventProcessMap = std::map<uint32_t, GnssEventProcessHandle>; 124 GnssEventProcessMap gnssEventProcessMap_; 125 }; 126 127 class GnssAbility : public SystemAbility, public GnssAbilityStub, public SubAbility { 128 public: 129 static GnssAbility* GetInstance(); 130 GnssAbility(); 131 ~GnssAbility() override; 132 void OnStart() override; 133 void OnStop() override; QueryServiceState()134 ServiceRunningState QueryServiceState() const 135 { 136 return state_; 137 } 138 LocationErrCode SendLocationRequest(WorkRecord &workrecord) override; 139 LocationErrCode SetEnable(bool state) override; 140 LocationErrCode RefrashRequirements() override; 141 LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity) override; 142 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback) override; 143 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity) override; 144 LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback) override; 145 LocationErrCode RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request, 146 const sptr<IRemoteObject>& callback) override; 147 LocationErrCode UnregisterCachedCallback(const sptr<IRemoteObject>& callback) override; 148 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 149 LocationErrCode GetCachedGnssLocationsSize(int &size) override; 150 LocationErrCode FlushCachedGnssLocations() override; 151 LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands) override; 152 LocationErrCode AddFence(std::shared_ptr<GeofenceRequest>& request) override; 153 LocationErrCode RemoveFence(std::shared_ptr<GeofenceRequest>& request) override; 154 LocationErrCode AddGnssGeofence(std::shared_ptr<GeofenceRequest>& request) override; 155 LocationErrCode RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request) override; 156 void ReportGnssSessionStatus(int status); 157 void ReportNmea(int64_t timestamp, const std::string &nmea); 158 void ReportSv(const std::unique_ptr<SatelliteStatus> &sv); 159 LocationErrCode EnableMock() override; 160 LocationErrCode DisableMock() override; 161 LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override; 162 void RequestRecord(WorkRecord &workRecord, bool isAdded) override; 163 void SendReportMockLocationEvent() override; 164 void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override; 165 bool CancelIdleState() override; 166 void UnloadGnssSystemAbility() override; 167 void StartGnss(); 168 void StopGnss(); 169 bool EnableGnss(); 170 void DisableGnss(); 171 bool ConnectHdi(); 172 bool RemoveHdi(); 173 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 174 void SetAgnssServer(); 175 void SetAgnssCallback(); 176 void SetSetId(const SubscriberSetId& id); 177 void SetSetIdImpl(const SubscriberSetId &id); 178 void SetRefInfo(const AGnssRefInfo& refInfo); 179 void SetRefInfoImpl(const AGnssRefInfo &refInfo); 180 #endif 181 void ReConnectHdiImpl(); 182 bool IsMockEnabled(); 183 void ProcessReportLocationMock(); 184 void ReConnectHdi(); 185 bool CheckIfHdiConnected(); 186 void RestGnssWorkStatus(); 187 bool RegisterGnssGeofenceCallback(std::shared_ptr<GeofenceRequest> &request, 188 const sptr<IRemoteObject>& callback); 189 bool UnregisterGnssGeofenceCallback(int fenceId); 190 std::shared_ptr<GeofenceRequest> GetGeofenceRequestByFenceId(int fenceId); 191 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 192 void ReportGeofenceEvent(int fenceId, GeofenceEvent event); 193 void ReportGeofenceOperationResult( 194 int fenceId, GeofenceOperateType type, GeofenceOperateResult result); 195 #endif 196 bool RemoveGnssGeofenceRequestByCallback(sptr<IRemoteObject> callbackObj); 197 LocationErrCode QuerySupportCoordinateSystemType( 198 std::vector<CoordinateSystemType>& coordinateSystemTypes) override; 199 LocationErrCode SendNetworkLocation(const std::unique_ptr<Location>& location) override; 200 LocationErrCode InjectLocation(); 201 LocationErrCode InjectTime(); 202 LocationErrCode UpdateNtpTime(int64_t ntpTime, int64_t elapsedTime); 203 void MonitorNetwork(); 204 205 private: 206 bool Init(); 207 static void SaDumpInfo(std::string& result); 208 bool IsGnssEnabled(); 209 int32_t ReportMockedLocation(const std::shared_ptr<Location> location); 210 bool CheckIfGnssConnecting(); 211 bool IsMockProcessing(); 212 void RegisterLocationHdiDeathRecipient(); 213 bool GetCommandFlags(std::unique_ptr<LocationCommand>& commands, GnssAuxiliaryDataType& flags); 214 LocationErrCode SetPositionMode(); 215 void SendEvent(AppExecFwk::InnerEvent::Pointer& event, MessageParcel &reply); 216 bool ExecuteFenceProcess( 217 GnssInterfaceCode code, std::shared_ptr<GeofenceRequest>& request); 218 int32_t GenerateFenceId(); 219 bool IsGnssfenceRequestMapExist(); 220 bool CheckBundleNameInGnssGeofenceRequestMap(const std::string& bundleName, int fenceId); 221 bool ConnectGnssHdi(); 222 223 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 224 bool ConnectAgnssHdi(); 225 #endif 226 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 227 bool SetGeofenceCallback(); 228 bool ConnectGeofenceHdi(); 229 #endif 230 bool IsDeviceLoaded(const std::string &servName); 231 232 size_t mockLocationIndex_ = 0; 233 bool registerToAbility_ = false; 234 int gnssWorkingStatus_ = 0; 235 std::shared_ptr<GnssHandler> gnssHandler_; 236 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 237 ffrt::mutex gnssMutex_; 238 ffrt::mutex nmeaMutex_; 239 ffrt::mutex hdiMutex_; 240 ffrt::mutex statusMutex_; 241 std::map<sptr<IRemoteObject>, AppIdentity> gnssStatusCallbackMap_; 242 std::map<sptr<IRemoteObject>, AppIdentity> nmeaCallbackMap_; 243 sptr<IGnssCallback> gnssCallback_; 244 Location nlpLocation_; 245 #ifdef TIME_SERVICE_ENABLE 246 TimeManager ntpTime_; 247 #endif 248 #ifdef NET_MANAGER_ENABLE 249 sptr<NetConnObserver> netWorkObserver_ = nullptr; 250 #endif 251 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 252 sptr<IAGnssCallback> agnssCallback_; 253 #endif 254 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE 255 sptr<IGeofenceCallback> geofenceCallback_; 256 #endif 257 int32_t fenceId_; 258 ffrt::mutex fenceIdMutex_; 259 ffrt::mutex gnssGeofenceRequestMapMutex_; 260 std::map<std::shared_ptr<GeofenceRequest>, 261 std::pair<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>>> gnssGeofenceRequestMap_; 262 }; 263 264 class LocationHdiDeathRecipient : public IRemoteObject::DeathRecipient { 265 }; 266 267 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 268 class AgnssRefInfoMessage { 269 public: 270 void SetAgnssRefInfo(const AGnssRefInfo &refInfo); 271 AGnssRefInfo GetAgnssRefInfo(); 272 273 private: 274 AGnssRefInfo agnssRefInfo_; 275 }; 276 #endif 277 } // namespace Location 278 } // namespace OHOS 279 #endif // FEATURE_GNSS_SUPPORT 280 #endif // GNSS_ABILITY_H