LoboMQ
Loading...
Searching...
No Matches
MACAddrList.cpp
Go to the documentation of this file.
1
10
11bool MACAddrList::isInList(const uint8_t *mac) const {
12 //Find the address in the vector
13 auto it = std::find_if(this->begin(), this->end(), [&](const MACAddress& address) {
14 return address == *reinterpret_cast<const MACAddress*>(mac);
15 });
16
17 return it != this->end(); //if found, true; otherwise, false
18}
19
20bool MACAddrList::isInList(const MACAddress &mac) const {
21 //Find the address in the vector
22 auto it = std::find_if(this->begin(), this->end(), [&](const MACAddress& address) {
23 return address == mac;
24 });
25
26 return it != this->end(); //if found, true; otherwise, false
27}
28
29void MACAddrList::addToList(const uint8_t *mac) {
30 if (!isInList(mac))
31 this->push_back(*reinterpret_cast<const MACAddress*>(mac));
32}
33
35 if (!isInList(mac))
36 this->push_back(mac);
37}
38
39void MACAddrList::addToList(const String &macStr) {
40 MACAddress newAddress;
41 if (sscanf(macStr.c_str(), "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
42 &newAddress[0], &newAddress[1], &newAddress[2], &newAddress[3], &newAddress[4], &newAddress[5]) == 6) {
43 addToList(newAddress.data());
44 }
45}
46
47void MACAddrList::addArrayToList(const std::vector<String>& macArray) { //receives ["AA:BB:CC:DD:EE:FF", "AA:BB:CC:DD:EE:FF"]
48 for (const auto& macStr : macArray)
49 addToList(macStr);
50}
51
52void MACAddrList::addArrayToList(const std::vector<MACAddress>& macArray) {
53 for (const auto& mac : macArray)
54 addToList(mac.data());
55}
56
57bool MACAddrList::removeFromList(const uint8_t *mac) {
58 //Find the address in the vector
59 auto it = std::find_if(this->begin(), this->end(), [&](const MACAddress& address) {
60 return address == *reinterpret_cast<const MACAddress*>(mac);
61 });
62
63 if (it != this->end()) { //if found
64 this->erase(it);
65 return true;
66 }
67
68 return false; //Address not found
69}
70
72 //Find the address in the vector
73 auto it = std::find_if(this->begin(), this->end(), [&](const MACAddress& address) {
74 return address == mac;
75 });
76
77 if (it != this->end()) { //if found
78 this->erase(it);
79 return true;
80 }
81
82 return false; //Address not found
83}
84
86 this->clear();
87}
88
90 String result = "";
91 for (const auto& mac : *this) {
92 char macChar[18];
93 snprintf(macChar, sizeof(macChar), "%02X:%02X:%02X:%02X:%02X:%02X",
94 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
95 result = result + macChar + "\n";
96 }
97 return result;
98}
std::array< uint8_t, 6 > MACAddress
Represents a MAC address as an array of 6 bytes.
Definition MACAddrList.h:21
bool isInList(const uint8_t *mac) const
Checks if a MAC address is in the list.
bool removeFromList(const uint8_t *mac)
Removes a MAC address from the list.
void addArrayToList(const std::vector< String > &macArray)
Adds multiple MAC addresses from an array to the list.
String getAddressListAsString() const
Returns a string representation of all MAC addresses in the list.
void addToList(const uint8_t *mac)
Adds a MAC address to the list if it is not already present.
void clearList()
Clears all the MAC addresses of the list.