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 using NetworkEventHandler = std::function<void(const AppExecFwk::InnerEvent::Pointer &)>; 41 using NetworkEventHandleMap = std::map<int, NetworkEventHandler>; 42 explicit NetworkHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 43 ~NetworkHandler() override; 44 45 private: 46 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 47 void InitNetworkEventProcessMap(); 48 void HandleReportLocationMock(const AppExecFwk::InnerEvent::Pointer& event); 49 void HandleRestartAllLocationRequests(const AppExecFwk::InnerEvent::Pointer& event); 50 void HandleStopAllLocationRequests(const AppExecFwk::InnerEvent::Pointer& event); 51 void HandleLocationRequest(const AppExecFwk::InnerEvent::Pointer& event); 52 void HandleSetMocked(const AppExecFwk::InnerEvent::Pointer& event); 53 void HandleClearServiceEvent(const AppExecFwk::InnerEvent::Pointer& event); 54 NetworkEventHandleMap networkEventProcessMap_; 55 }; 56 57 class NlpServiceDeathRecipient : public IRemoteObject::DeathRecipient { 58 public: 59 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 60 NlpServiceDeathRecipient(); 61 ~NlpServiceDeathRecipient() override; 62 }; 63 64 class NetworkAbility : public SystemAbility, public NetworkAbilityStub, public SubAbility { 65 DECLEAR_SYSTEM_ABILITY(NetworkAbility); 66 67 public: 68 DISALLOW_COPY_AND_MOVE(NetworkAbility); 69 NetworkAbility(); 70 ~NetworkAbility() override; 71 static NetworkAbility* GetInstance(); 72 void OnStart() override; 73 void OnStop() override; QueryServiceState()74 ServiceRunningState QueryServiceState() const 75 { 76 return state_; 77 } 78 LocationErrCode SendLocationRequest(WorkRecord &workrecord) override; 79 LocationErrCode SetEnable(bool state) override; 80 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 81 void RequestRecord(WorkRecord &workRecord, bool isAdded) override; 82 LocationErrCode EnableMock() override; 83 LocationErrCode DisableMock() override; 84 LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override; 85 void SendReportMockLocationEvent() override; 86 bool CancelIdleState() override; 87 void UnloadNetworkSystemAbility() override; 88 void ProcessReportLocationMock(); 89 bool ConnectNlpService(); 90 bool ReConnectNlpService(); 91 bool ResetServiceProxy(); 92 void NotifyConnected(const sptr<IRemoteObject>& remoteObject); 93 void NotifyDisConnected(); 94 bool IsMockEnabled(); 95 void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override; 96 void RestartNlpRequests(); 97 void DisconnectAbilityConnect(); 98 void ClearServiceProxy(); 99 private: 100 bool Init(); 101 static void SaDumpInfo(std::string& result); 102 int32_t ReportMockedLocation(const std::shared_ptr<Location> location); 103 bool CheckIfNetworkConnecting(); 104 bool RequestNetworkLocation(WorkRecord &workRecord); 105 bool RemoveNetworkLocation(WorkRecord &workRecord); 106 void RegisterNlpServiceDeathRecipient(); 107 void UnregisterNlpServiceDeathRecipient(); 108 bool IsConnect(); 109 110 ffrt::mutex nlpServiceMutex_; 111 ffrt::mutex connMutex_; 112 sptr<IRemoteObject> nlpServiceProxy_; 113 ffrt::condition_variable connectCondition_; 114 std::shared_ptr<NetworkHandler> networkHandler_; 115 size_t mockLocationIndex_ = 0; 116 bool registerToAbility_ = false; 117 sptr<IRemoteObject::DeathRecipient> nlpServiceRecipient_ = sptr<NlpServiceDeathRecipient>(new 118 (std::nothrow) NlpServiceDeathRecipient()); 119 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 120 sptr<AAFwk::IAbilityConnection> conn_; 121 }; 122 } // namespace Location 123 } // namespace OHOS 124 #endif // FEATURE_NETWORK_SUPPORT 125 #endif // NETWORK_ABILITY_H 126