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