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