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 WEARABLE_DISTRIBUTED_NET_CLIENT_H 17 #define WEARABLE_DISTRIBUTED_NET_CLIENT_H 18 19 #include <atomic> 20 #include "iwearable_distributed_net.h" 21 #include "parcel.h" 22 #include "singleton.h" 23 #include "system_ability_definition.h" 24 #include "system_ability_load_callback_stub.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 class WearableDistributedNetLoadCallback : public SystemAbilityLoadCallbackStub { 29 public: 30 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override; 31 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 32 bool IsFailed(); 33 const sptr<IRemoteObject> &GetRemoteObject() const; 34 35 private: 36 std::atomic_bool loadSAFailed_ = false; 37 sptr<IRemoteObject> remoteObject_ = nullptr; 38 std::mutex loadMutex_; 39 }; 40 41 class WearableDistributedNetClient { 42 public: 43 ~WearableDistributedNetClient(); 44 /** 45 * @brief Setup wearable distributed net 46 * 47 * @param tcpPortId tcp port id 48 * @param udpPortId udp port id 49 * @param isMetered is metered or not 50 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 51 * @permission ohos.permission.CONNECTIVITY_INTERNAL 52 * @systemapi Hide this for inner system use. 53 */ 54 int32_t SetupWearableDistributedNet(const int32_t tcpPortId, const int32_t udpPortId, const bool isMetered); 55 56 /** 57 * @brief Teardown wearable distributed net 58 * 59 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 60 * @permission ohos.permission.CONNECTIVITY_INTERNAL 61 * @systemapi Hide this for inner system use. 62 */ 63 int32_t TearDownWearableDistributedNet(); 64 65 /** 66 * @brief update wearable distributed net metered status 67 * 68 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 69 * @permission ohos.permission.CONNECTIVITY_INTERNAL 70 * @systemapi Hide this for inner system use. 71 */ 72 int32_t UpdateWearableDistributedNetMeteredStatus(const bool isMetered); GetInstance()73 static WearableDistributedNetClient &GetInstance() 74 { 75 static WearableDistributedNetClient ins; 76 return ins; 77 } 78 private: 79 class WearableDistributedNetDeathRecipient : public IRemoteObject::DeathRecipient { 80 public: WearableDistributedNetDeathRecipient(WearableDistributedNetClient & client)81 explicit WearableDistributedNetDeathRecipient(WearableDistributedNetClient &client) : client_(client) {} 82 ~WearableDistributedNetDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)83 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 84 { 85 client_.OnRemoteDied(remote); 86 } 87 88 private: 89 WearableDistributedNetClient &client_; 90 }; 91 92 sptr<IWearableDistributedNet> GetProxy(); 93 void RestartWearableDistributedNetManagerSysAbility(); 94 void OnRemoteDied(const wptr<IRemoteObject> &remote); 95 96 private: 97 std::mutex mutex_; 98 sptr<IWearableDistributedNet> wearableDistributedNetService_ = nullptr; 99 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 100 std::mutex loadSaMutex_; 101 }; 102 } // namespace NetManagerStandard 103 } // namespace OHOS 104 #endif // WEARABLE_DISTRIBUTED_NET_CLIENT_H 105