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 <v1_0/ignss_interface.h> 23 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 24 #include <v1_0/ia_gnss_interface.h> 25 #endif 26 27 #include "event_handler.h" 28 #include "system_ability.h" 29 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 30 #include "agnss_event_callback.h" 31 #endif 32 #include "common_utils.h" 33 #include "constant_definition.h" 34 #include "gnss_ability_skeleton.h" 35 #include "i_gnss_status_callback.h" 36 #include "i_nmea_message_callback.h" 37 #include "subability_common.h" 38 39 namespace OHOS { 40 namespace Location { 41 #ifdef __aarch64__ 42 #define VENDOR_GNSS_ADAPTER_SO_PATH "/system/lib64/vendorGnssAdapter.so" 43 #else 44 #define VENDOR_GNSS_ADAPTER_SO_PATH "/system/lib/vendorGnssAdapter.so" 45 #endif 46 47 using HDI::Location::Gnss::V1_0::IGnssInterface; 48 using HDI::Location::Gnss::V1_0::IGnssCallback; 49 using HDI::Location::Gnss::V1_0::GNSS_START_TYPE_NORMAL; 50 using HDI::Location::Gnss::V1_0::GNSS_STATUS_NONE; 51 using HDI::Location::Gnss::V1_0::GNSS_STATUS_SESSION_BEGIN; 52 using HDI::Location::Gnss::V1_0::GNSS_STATUS_SESSION_END; 53 using HDI::Location::Gnss::V1_0::GNSS_STATUS_ENGINE_ON; 54 using HDI::Location::Gnss::V1_0::GNSS_STATUS_ENGINE_OFF; 55 using HDI::Location::Gnss::V1_0::GnssAuxiliaryData; 56 using HDI::Location::Gnss::V1_0::GnssWorkingMode; 57 using HDI::Location::Gnss::V1_0::GnssConfigPara; 58 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 59 using HDI::Location::Agnss::V1_0::IAGnssInterface; 60 using HDI::Location::Agnss::V1_0::IAGnssCallback; 61 using HDI::Location::Agnss::V1_0::AGNSS_TYPE_SUPL; 62 using HDI::Location::Agnss::V1_0::AGnssServerInfo; 63 #endif 64 65 class GnssHandler : public AppExecFwk::EventHandler { 66 public: 67 explicit GnssHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 68 ~GnssHandler() override; 69 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 70 71 private: 72 void InitGnssEventProcessMap(); 73 void HandleEventReportLocation(const AppExecFwk::InnerEvent::Pointer& event); 74 void HandleSendLocationRequest(const AppExecFwk::InnerEvent::Pointer& event); 75 void HandleSetMockedLocations(const AppExecFwk::InnerEvent::Pointer& event); 76 void HandleSendCommands(const AppExecFwk::InnerEvent::Pointer& event); 77 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 78 void HandleSetSubscriberSetId(const AppExecFwk::InnerEvent::Pointer& event); 79 void HandleSetAgnssRefInfo(const AppExecFwk::InnerEvent::Pointer& event); 80 #endif 81 void HandleReconnectHdi(const AppExecFwk::InnerEvent::Pointer& event); 82 void HandleSetEnable(const AppExecFwk::InnerEvent::Pointer& event); 83 void HandleInitHdi(const AppExecFwk::InnerEvent::Pointer& event); 84 85 using GnssEventProcessHandle = void (GnssHandler::*)(const AppExecFwk::InnerEvent::Pointer& event); 86 using GnssEventProcessMap = std::map<uint32_t, GnssEventProcessHandle>; 87 GnssEventProcessMap gnssEventProcessMap_; 88 }; 89 90 class GnssAbility : public SystemAbility, public GnssAbilityStub, public SubAbility, DelayedSingleton<GnssAbility> { 91 DECLEAR_SYSTEM_ABILITY(GnssAbility); 92 93 public: 94 DISALLOW_COPY_AND_MOVE(GnssAbility); 95 GnssAbility(); 96 ~GnssAbility() override; 97 void OnStart() override; 98 void OnStop() override; QueryServiceState()99 ServiceRunningState QueryServiceState() const 100 { 101 return state_; 102 } 103 LocationErrCode SendLocationRequest(WorkRecord &workrecord) override; 104 LocationErrCode SetEnable(bool state) override; 105 LocationErrCode RefrashRequirements() override; 106 LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid) override; 107 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback) override; 108 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid) override; 109 LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback) override; 110 LocationErrCode RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request, 111 const sptr<IRemoteObject>& callback) override; 112 LocationErrCode UnregisterCachedCallback(const sptr<IRemoteObject>& callback) override; 113 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 114 LocationErrCode GetCachedGnssLocationsSize(int &size) override; 115 LocationErrCode FlushCachedGnssLocations() override; 116 LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands) override; 117 LocationErrCode AddFence(std::unique_ptr<GeofenceRequest>& request) override; 118 LocationErrCode RemoveFence(std::unique_ptr<GeofenceRequest>& request) override; 119 void ReportGnssSessionStatus(int status); 120 void ReportNmea(int64_t timestamp, const std::string &nmea); 121 void ReportSv(const std::unique_ptr<SatelliteStatus> &sv); 122 LocationErrCode EnableMock() override; 123 LocationErrCode DisableMock() override; 124 LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override; 125 void RequestRecord(WorkRecord &workRecord, bool isAdded) override; 126 void SendReportMockLocationEvent() override; 127 void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override; 128 void UnloadGnssSystemAbility() override; 129 void StartGnss(); 130 void StopGnss(); 131 bool EnableGnss(); 132 void DisableGnss(); 133 bool ConnectHdi(); 134 bool RemoveHdi(); 135 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 136 void SetAgnssServer(); 137 void SetAgnssCallback(); 138 void SetSetId(const SubscriberSetId& id); 139 void SetSetIdImpl(const SubscriberSetId &id); 140 void SetRefInfo(const AGnssRefInfo& refInfo); 141 void SetRefInfoImpl(const AGnssRefInfo &refInfo); 142 #endif 143 void ReConnectHdiImpl(); 144 bool IsMockEnabled(); 145 void ProcessReportLocationMock(); 146 void ReConnectHdi(); 147 bool CheckIfHdiConnected(); 148 private: 149 bool Init(); 150 static void SaDumpInfo(std::string& result); 151 bool IsGnssEnabled(); 152 int32_t ReportMockedLocation(const std::shared_ptr<Location> location); 153 bool CheckIfGnssConnecting(); 154 bool IsMockProcessing(); 155 void RegisterLocationHdiDeathRecipient(); 156 bool GetCommandFlags(std::unique_ptr<LocationCommand>& commands, GnssAuxiliaryData& flags); 157 LocationErrCode SetPositionMode(); 158 void SendEvent(AppExecFwk::InnerEvent::Pointer& event, MessageParcel &reply); 159 160 size_t mockLocationIndex_ = 0; 161 bool registerToAbility_ = false; 162 int gnssWorkingStatus_ = 0; 163 std::shared_ptr<GnssHandler> gnssHandler_; 164 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 165 std::mutex gnssMutex_; 166 std::mutex nmeaMutex_; 167 std::mutex hdiMutex_; 168 std::vector<sptr<IGnssStatusCallback>> gnssStatusCallback_; 169 std::vector<sptr<INmeaMessageCallback>> nmeaCallback_; 170 sptr<IGnssInterface> gnssInterface_; 171 sptr<IGnssCallback> gnssCallback_; 172 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 173 sptr<IAGnssCallback> agnssCallback_; 174 sptr<IAGnssInterface> agnssInterface_; 175 #endif 176 }; 177 178 class LocationHdiDeathRecipient : public IRemoteObject::DeathRecipient { 179 public: 180 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 181 LocationHdiDeathRecipient(); 182 ~LocationHdiDeathRecipient() override; 183 }; 184 185 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE 186 class AgnssRefInfoMessage { 187 public: 188 void SetAgnssRefInfo(const AGnssRefInfo &refInfo); 189 AGnssRefInfo GetAgnssRefInfo(); 190 191 private: 192 AGnssRefInfo agnssRefInfo_; 193 }; 194 #endif 195 } // namespace Location 196 } // namespace OHOS 197 #endif // FEATURE_GNSS_SUPPORT 198 #endif // GNSS_ABILITY_H 199