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 Enable wearable distributed net 58 * 59 * @param enableFlag is enable or disable 60 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 61 * @permission ohos.permission.CONNECTIVITY_INTERNAL 62 * @systemapi Hide this for inner system use. 63 */ 64 int32_t EnableWearableDistributedNet(bool enableFlag); 65 66 /** 67 * @brief Teardown wearable distributed net 68 * 69 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 70 * @permission ohos.permission.CONNECTIVITY_INTERNAL 71 * @systemapi Hide this for inner system use. 72 */ 73 int32_t TearDownWearableDistributedNet(); 74 75 /** 76 * @brief update wearable distributed net metered status 77 * 78 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 79 * @permission ohos.permission.CONNECTIVITY_INTERNAL 80 * @systemapi Hide this for inner system use. 81 */ 82 int32_t UpdateWearableDistributedNetMeteredStatus(const bool isMetered); GetInstance()83 static WearableDistributedNetClient &GetInstance() 84 { 85 static WearableDistributedNetClient ins; 86 return ins; 87 } 88 private: 89 class WearableDistributedNetDeathRecipient : public IRemoteObject::DeathRecipient { 90 public: WearableDistributedNetDeathRecipient(WearableDistributedNetClient & client)91 explicit WearableDistributedNetDeathRecipient(WearableDistributedNetClient &client) : client_(client) {} 92 ~WearableDistributedNetDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)93 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 94 { 95 client_.OnRemoteDied(remote); 96 } 97 98 private: 99 WearableDistributedNetClient &client_; 100 }; 101 102 sptr<IWearableDistributedNet> GetProxy(); 103 void RestartWearableDistributedNetManagerSysAbility(); 104 void OnRemoteDied(const wptr<IRemoteObject> &remote); 105 106 private: 107 std::mutex mutex_; 108 sptr<IWearableDistributedNet> wearableDistributedNetService_ = nullptr; 109 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 110 std::mutex loadSaMutex_; 111 }; 112 } // namespace NetManagerStandard 113 } // namespace OHOS 114 #endif // WEARABLE_DISTRIBUTED_NET_CLIENT_H 115