1 /* 2 * Copyright (C) 2021-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 OHOS_MAC_ADDRESS_H 17 #define OHOS_MAC_ADDRESS_H 18 19 #include <cstddef> 20 #include <string> 21 #include <sys/socket.h> 22 23 constexpr int ZERO = 0; 24 constexpr int ONE = 1; 25 constexpr int TWO = 2; 26 constexpr int THREE = 3; 27 constexpr int FOUR = 4; 28 constexpr int FIVE = 5; 29 constexpr int SIX = 6; 30 constexpr int SEVEN = 7; 31 32 /* ETH_ALEN Size of the MAC address binary data */ 33 constexpr size_t MAC_STRING_LENGTH = 6 * 2 + (6 - 1); /* length of the string of mac address */ 34 35 #ifndef MAC_LEN 36 #define MAC_LEN 6 37 #endif 38 39 namespace OHOS { 40 namespace Wifi { 41 class MacAddress { 42 public: 43 /** 44 * @Description Check whether the MAC address is valid. 45 * 46 * @param mac - string of mac address [input] 47 * @return true - legal false - illegal 48 */ 49 static bool IsValidMac(const std::string &mac); 50 51 /** 52 * @Description factory method 53 * 54 * @param mac - string of mac address [input] 55 * @return Parameter invalid, the invalid object INVALID_MAC_ADDRESS is returned. 56 Otherwise, the successful object is returned. 57 */ 58 static MacAddress Create(const std::string &mac); 59 60 /** 61 * @Description factory method 62 * 63 * @param hwAddr - sockaddr structure of mac address [input] 64 * @return Parameter invalid, the invalid object INVALID_MAC_ADDRESS is returned. 65 Otherwise, the successful object is returned. 66 */ 67 static MacAddress Create(const sockaddr &hwAddr); 68 69 /** 70 * @Description Obtaining the MAC address by interface name 71 * 72 * @param ifName - interface name 73 * @param macAddr - Array for storing returned mac data 74 * @return true - success false - fail 75 */ 76 static bool GetMacAddr(const std::string& ifName, unsigned char macAddr[MAC_LEN]); 77 78 static const MacAddress INVALID_MAC_ADDRESS; /* Invalid MAC Address Object Constant */ 79 80 /** 81 * @Description The == operator is overloaded to determine whether 82 two MAC addresses represent the same MAC address. 83 * 84 * @param MacAddress of the compared MAC address. [input] 85 * @return true - same mac false - different mac. 86 */ 87 bool operator==(const MacAddress &) const; 88 89 /** 90 * @Description The == operator is overloaded to determine whether 91 two MAC addresses represent the same MAC address. 92 * 93 * @param sockaddr structure of the compared MAC address. [input] 94 * @return true - same mac false - different mac. 95 */ 96 bool operator==(const struct sockaddr &) const; 97 /** 98 * @Description Supports default destructor methods. 99 * 100 * @param None 101 * @return None 102 */ 103 virtual ~MacAddress() = default; 104 105 /** 106 * @Description Check whether the current mac address is valid. 107 * 108 * @param None 109 * @return true - legal false - illegal 110 */ 111 bool IsValid() const; 112 113 /** 114 * @Description Print the MAC address. 115 * 116 * @param None 117 * @return None 118 */ 119 void Dump() const; 120 121 /** 122 * @Description obtain the string of mac address 123 * 124 * @param None 125 * @return the string of mac address 126 */ 127 const std::string &GetMacAddressWifiString() const; 128 129 /** 130 * @Description obtain the sockaddr structure of mac address 131 * 132 * @param None 133 * @return the sockaddr structure of mac address 134 */ 135 struct sockaddr GetMacAddressWifiSockaddr() const; 136 137 private: 138 /** 139 * @Description Delete Default Construct 140 * 141 * @param None 142 * @return None 143 */ 144 MacAddress() = delete; 145 146 /** 147 * @Description construction method 148 * 149 * @param mac - the string of mac address [input] 150 * @return MacAddress object 151 */ 152 explicit MacAddress(const std::string &mac); 153 154 private: 155 std::string mac_; 156 }; 157 } // namespace Wifi 158 } // namespace OHOS 159 160 #endif /* OHOS_MAC_ADDRESS_H */ 161