|
| 1 | +/* MAC Address Option |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <mutex> |
| 8 | +#include "Common/Cpp/Containers/Pimpl.tpp" |
| 9 | +#include "Common/Cpp/Json/JsonValue.h" |
| 10 | +#include "MacAddressOption.h" |
| 11 | + |
| 12 | +#include "Common/Qt/Options/MacAddressWidget.h" |
| 13 | + |
| 14 | +//#include <iostream> |
| 15 | +//using std::cout; |
| 16 | +//using std::endl; |
| 17 | + |
| 18 | +namespace PokemonAutomation{ |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +ConfigWidget* MacAddressCell::make_QtWidget(QWidget& parent){ |
| 23 | + return new MacAddressCellWidget(parent, *this); |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +std::string write_MAC_address(size_t length, const uint8_t* address){ |
| 29 | + static const char HEX_DIGITS[] = "0123456789ABCDEF"; |
| 30 | + std::string ret; |
| 31 | + for (size_t c = 0; c < length; c++){ |
| 32 | + if (!ret.empty()){ |
| 33 | + ret += "-"; |
| 34 | + } |
| 35 | + ret += HEX_DIGITS[address[c] >> 4]; |
| 36 | + ret += HEX_DIGITS[address[c] & 0xf]; |
| 37 | + } |
| 38 | + return ret; |
| 39 | +} |
| 40 | +void parse_MAC_address(size_t length, uint8_t* address, const std::string& str){ |
| 41 | + memset(address, 0, length); |
| 42 | + size_t current_index = 0; |
| 43 | + for (char ch : str){ |
| 44 | + if (current_index >= 2*length){ |
| 45 | + return; |
| 46 | + } |
| 47 | + if ('0' <= ch && ch <= '9'){ |
| 48 | + ch -= '0'; |
| 49 | + }else if ('a' <= ch && ch <= 'f'){ |
| 50 | + ch += 10 - 'a'; |
| 51 | + }else if ('A' <= ch && ch <= 'F'){ |
| 52 | + ch += 10 - 'A'; |
| 53 | + }else{ |
| 54 | + continue; |
| 55 | + } |
| 56 | + address[current_index / 2] |= ch << 4*(1 - current_index % 2); |
| 57 | + current_index++; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +struct MacAddressCell::Data{ |
| 64 | + mutable std::mutex m_lock; |
| 65 | + std::vector<uint8_t> m_current; |
| 66 | + |
| 67 | + Data(size_t bytes, const uint8_t* current) |
| 68 | + : m_current(bytes) |
| 69 | + { |
| 70 | + if (current != nullptr){ |
| 71 | + memcpy(m_current.data(), current, bytes); |
| 72 | + } |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +MacAddressCell::~MacAddressCell() = default; |
| 79 | +MacAddressCell::MacAddressCell(const MacAddressCell& x) |
| 80 | + : ConfigOption(x) |
| 81 | + , m_data(CONSTRUCT_TOKEN, x.m_data->m_current.size(), x.m_data->m_current.data()) |
| 82 | +{} |
| 83 | +void MacAddressCell::operator=(const MacAddressCell& x){ |
| 84 | + if (bytes() != x.bytes()){ |
| 85 | + throw InternalProgramError( |
| 86 | + nullptr, PA_CURRENT_FUNCTION, |
| 87 | + "Attempt to assign a MAC address is mismatching size." |
| 88 | + ); |
| 89 | + } |
| 90 | + { |
| 91 | + std::scoped_lock<std::mutex, std::mutex> lg(m_data->m_lock, x.m_data->m_lock); |
| 92 | + memcpy(m_data->m_current.data(), x.m_data->m_current.data(), bytes()); |
| 93 | + } |
| 94 | + report_value_changed(this); |
| 95 | +} |
| 96 | +MacAddressCell::MacAddressCell( |
| 97 | + LockMode lock_while_running, |
| 98 | + size_t bytes, |
| 99 | + uint8_t* current_value |
| 100 | +) |
| 101 | + : ConfigOption(lock_while_running) |
| 102 | + , m_data(CONSTRUCT_TOKEN, bytes, current_value) |
| 103 | +{} |
| 104 | + |
| 105 | + |
| 106 | +size_t MacAddressCell::bytes() const{ |
| 107 | + return m_data->m_current.size(); |
| 108 | +} |
| 109 | +std::string MacAddressCell::to_string() const{ |
| 110 | + std::lock_guard<std::mutex> lg(m_data->m_lock); |
| 111 | + return write_MAC_address(m_data->m_current.size(), m_data->m_current.data()); |
| 112 | +} |
| 113 | +void MacAddressCell::current_value(uint8_t* address) const{ |
| 114 | + std::lock_guard<std::mutex> lg(m_data->m_lock); |
| 115 | + memcpy(address, m_data->m_current.data(), m_data->m_current.size()); |
| 116 | +} |
| 117 | +void MacAddressCell::set(const uint8_t* address){ |
| 118 | + { |
| 119 | + std::lock_guard<std::mutex> lg(m_data->m_lock); |
| 120 | + if (memcmp(m_data->m_current.data(), address, m_data->m_current.size()) == 0){ |
| 121 | + return; |
| 122 | + } |
| 123 | + memcpy(m_data->m_current.data(), address, m_data->m_current.size()); |
| 124 | + } |
| 125 | + report_value_changed(this); |
| 126 | +} |
| 127 | +void MacAddressCell::set(const std::string& address){ |
| 128 | + std::vector<uint8_t> new_value(m_data->m_current.size()); |
| 129 | + parse_MAC_address(new_value.size(), new_value.data(), address); |
| 130 | + set(new_value.data()); |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +bool MacAddressCell::operator==(const uint8_t* address) const{ |
| 135 | + std::lock_guard<std::mutex> lg(m_data->m_lock); |
| 136 | + return memcmp(m_data->m_current.data(), address, m_data->m_current.size()) == 0; |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +void MacAddressCell::load_json(const JsonValue& json){ |
| 141 | + set(json.to_string_default()); |
| 142 | +} |
| 143 | +JsonValue MacAddressCell::to_json() const{ |
| 144 | + return to_string(); |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +} |
0 commit comments