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 NETWORK_ABILITY_H 17 #define NETWORK_ABILITY_H 18 #ifdef FEATURE_NETWORK_SUPPORT 19 20 #include <string> 21 #include <singleton.h> 22 23 #include "ability_connect_callback_interface.h" 24 #include "event_handler.h" 25 #include "event_runner.h" 26 #include "ffrt.h" 27 #include "system_ability.h" 28 29 #include "common_utils.h" 30 #include "constant_definition.h" 31 #include "network_ability_skeleton.h" 32 #include "subability_common.h" 33 34 namespace OHOS { 35 namespace Location { 36 static constexpr int REQUEST_NETWORK_LOCATION = 1; 37 static constexpr int REMOVE_NETWORK_LOCATION = 2; 38 class NetworkHandler : public AppExecFwk::EventHandler { 39 public: 40 explicit NetworkHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 41 ~NetworkHandler() override; 42 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 43 }; 44 45 class NetworkAbility : public SystemAbility, public NetworkAbilityStub, public SubAbility { 46 DECLEAR_SYSTEM_ABILITY(NetworkAbility); 47 48 public: 49 DISALLOW_COPY_AND_MOVE(NetworkAbility); 50 NetworkAbility(); 51 ~NetworkAbility() override; 52 static NetworkAbility* GetInstance(); 53 void OnStart() override; 54 void OnStop() override; QueryServiceState()55 ServiceRunningState QueryServiceState() const 56 { 57 return state_; 58 } 59 LocationErrCode SendLocationRequest(WorkRecord &workrecord) override; 60 LocationErrCode SetEnable(bool state) override; 61 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 62 void RequestRecord(WorkRecord &workRecord, bool isAdded) override; 63 LocationErrCode EnableMock() override; 64 LocationErrCode DisableMock() override; 65 LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override; 66 void SendReportMockLocationEvent() override; 67 bool CancelIdleState() override; 68 void UnloadNetworkSystemAbility() override; 69 void ProcessReportLocationMock(); 70 bool ConnectNlpService(); 71 bool ReConnectNlpService(); 72 bool ResetServiceProxy(); 73 void NotifyConnected(const sptr<IRemoteObject>& remoteObject); 74 void NotifyDisConnected(); 75 bool IsMockEnabled(); 76 void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override; 77 void ReportLocationError(int32_t errCode, std::string errMsg, std::string uuid); 78 private: 79 bool Init(); 80 static void SaDumpInfo(std::string& result); 81 int32_t ReportMockedLocation(const std::shared_ptr<Location> location); 82 bool CheckIfNetworkConnecting(); 83 bool RequestNetworkLocation(WorkRecord &workRecord); 84 bool RemoveNetworkLocation(WorkRecord &workRecord); 85 void RegisterNLPServiceDeathRecipient(); 86 bool IsConnect(); 87 88 ffrt::mutex mutex_; 89 sptr<IRemoteObject> nlpServiceProxy_; 90 ffrt::condition_variable connectCondition_; 91 std::shared_ptr<NetworkHandler> networkHandler_; 92 size_t mockLocationIndex_ = 0; 93 bool registerToAbility_ = false; 94 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 95 sptr<AAFwk::IAbilityConnection> conn_; 96 }; 97 98 class NLPServiceDeathRecipient : public IRemoteObject::DeathRecipient { 99 public: 100 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 101 NLPServiceDeathRecipient(); 102 ~NLPServiceDeathRecipient() override; 103 }; 104 } // namespace Location 105 } // namespace OHOS 106 #endif // FEATURE_NETWORK_SUPPORT 107 #endif // NETWORK_ABILITY_H 108