1 /* 2 * Copyright (c) 2021-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 ETHERNET_CLIENT_H 17 #define ETHERNET_CLIENT_H 18 19 #include <string> 20 21 #include "i_ethernet_service.h" 22 #include "interface_state_callback.h" 23 #include "parcel.h" 24 #include "singleton.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 class EthernetClient { 29 DECLARE_DELAYED_SINGLETON(EthernetClient) 30 31 public: 32 /** 33 * Get the ethernet MAC address 34 * 35 * @param macAddrList MAC address info list 36 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 37 * @permission ohos.permission.GET_ETHERNET_LOCAL_MAC 38 * @systemapi Hide this for inner system use. 39 */ 40 int32_t GetMacAddress(std::vector<MacAddressInfo> &macAddrList); 41 42 /** 43 * Set the network interface configuration 44 * 45 * @param iface interface name 46 * @param ic interface configuration 47 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 48 * @permission ohos.permission.CONNECTIVITY_INTERNAL 49 * @systemapi Hide this for inner system use. 50 */ 51 int32_t SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic); 52 53 /** 54 * Gets the network interface configuration parameters 55 * 56 * @param iface interface name 57 * @param ifaceConfig interface configuration 58 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 59 * @permission ohos.permission.CONNECTIVITY_INTERNAL 60 * @systemapi Hide this for inner system use. 61 */ 62 int32_t GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig); 63 64 /** 65 * check the network interface is active or not 66 * 67 * @param iface interface name 68 * @param activeStatus active status 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 IsIfaceActive(const std::string &iface, int32_t &activeStatus); 74 75 /** 76 * Gets the list of active devices 77 * 78 * @param activeIfaces list of active interface 79 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 80 * @permission ohos.permission.CONNECTIVITY_INTERNAL 81 * @systemapi Hide this for inner system use. 82 */ 83 int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces); 84 85 /** 86 * Reset all configuration information 87 * 88 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 89 * @permission ohos.permission.CONNECTIVITY_INTERNAL 90 * @systemapi Hide this for inner system use. 91 */ 92 int32_t ResetFactory(); 93 94 /** 95 * Register the callback to monitor interface add/remove state 96 * 97 * @param callback use to receive interface add/remove event. 98 * @return Returns NETMANAGER_EXT_SUCCESS as success, other values as failure 99 * @permission ohos.permission.CONNECTIVITY_INTERNAL 100 * @systemapi Hide this for inner system use. 101 */ 102 int32_t RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback); 103 104 /** 105 * Cancel register the callback to monitor interface add/remove state 106 * 107 * @param callback use to receive interface add/remove event. 108 * @return Returns NETMANAGER_EXT_SUCCESS as success, other values as failure 109 * @permission ohos.permission.CONNECTIVITY_INTERNAL 110 * @systemapi Hide this for inner system use. 111 */ 112 int32_t UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback); 113 114 /** 115 * Set the specified network port up 116 * 117 * @param iface interface name 118 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 119 * @permission ohos.permission.CONNECTIVITY_INTERNAL 120 * @systemapi Hide this for inner system use. 121 */ 122 int32_t SetInterfaceUp(const std::string &iface); 123 124 /** 125 * Set the specified network port down 126 * 127 * @param iface interface name 128 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 129 * @permission ohos.permission.CONNECTIVITY_INTERNAL 130 * @systemapi Hide this for inner system use. 131 */ 132 int32_t SetInterfaceDown(const std::string &iface); 133 134 /** 135 * Get the specified network port configuration 136 * 137 * @param iface interface name 138 * @param cfg interface configuration 139 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 140 * @permission ohos.permission.CONNECTIVITY_INTERNAL 141 * @systemapi Hide this for inner system use. 142 */ 143 int32_t GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg); 144 145 /** 146 * Set the specified network port configuration 147 * 148 * @param iface interface name 149 * @param cfg interface configuration 150 * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error 151 * @permission ohos.permission.CONNECTIVITY_INTERNAL 152 * @systemapi Hide this for inner system use. 153 */ 154 int32_t SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg); 155 156 private: 157 class EthernetDeathRecipient : public IRemoteObject::DeathRecipient { 158 public: EthernetDeathRecipient(EthernetClient & client)159 explicit EthernetDeathRecipient(EthernetClient &client) : client_(client) {} 160 ~EthernetDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)161 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 162 { 163 client_.OnRemoteDied(remote); 164 } 165 166 private: 167 EthernetClient &client_; 168 }; 169 170 private: 171 sptr<IEthernetService> GetProxy(); 172 void RecoverCallback(); 173 void OnRemoteDied(const wptr<IRemoteObject> &remote); 174 175 private: 176 std::mutex mutex_; 177 sptr<IEthernetService> ethernetService_; 178 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 179 sptr<InterfaceStateCallback> callback_; 180 }; 181 } // namespace NetManagerStandard 182 } // namespace OHOS 183 #endif // ETHERNET_CLIENT_H