From ffa313e80a27005405334db6491075442f6e1abd Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 20 Dec 2018 08:35:51 +0100 Subject: Mac: o Using more natural byte ordering in the code. No external effect. o Using global ==, != and < operators instead of in-class operators for better compatibility with STL. ByteBuffer: o Renaming setPosition() to setCursor(). --- ble/Bluetooth.cpp | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'ble/Bluetooth.cpp') diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp index 16f4224..735a29b 100644 --- a/ble/Bluetooth.cpp +++ b/ble/Bluetooth.cpp @@ -20,48 +20,53 @@ string Mac::str() const { std::ostringstream buf; buf - << setw(2) << hex << setfill('0') << (int) bytes[5] << ":" - << setw(2) << hex << setfill('0') << (int) bytes[4] << ":" - << setw(2) << hex << setfill('0') << (int) bytes[3] << ":" - << setw(2) << hex << setfill('0') << (int) bytes[2] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[0] << ":" << setw(2) << hex << setfill('0') << (int) bytes[1] << ":" - << setw(2) << hex << setfill('0') << (int) bytes[0]; + << setw(2) << hex << setfill('0') << (int) bytes[2] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[3] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[4] << ":" + << setw(2) << hex << setfill('0') << (int) bytes[5]; return buf.str(); } -bool Mac::operator==(Mac &other) const { - const uint8_t *b = bytes; - return memcmp(b, other.bytes, sizeof(bytes)) == 0; +bool operator==(const Mac &a, const Mac &b) { + return memcmp(a.bytes, b.bytes, sizeof(a.bytes)) == 0; } -bool Mac::operator!=(Mac &other) const { - return !operator==(other); +bool operator!=(const Mac &a, const Mac &b) { + return !(a == b); } -bool operator<(const Mac &a, const Mac &b) { +bool operator<(const Mac &a, const Mac &b) +{ return memcmp(a.bytes, b.bytes, sizeof(a.bytes)) < 0; } -void Mac::copy(uint8_t &_5, uint8_t &_4, uint8_t &_3, uint8_t &_2, uint8_t &_1, uint8_t &_0) const { - _5 = bytes[5]; - _4 = bytes[4]; - _3 = bytes[3]; - _2 = bytes[2]; - _1 = bytes[1]; +std::ostream& operator<<(std::ostream& os, const Mac& mac) { +os << mac.str(); +return os; +} + +void Mac::copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, uint8_t &_5) const { _0 = bytes[0]; + _1 = bytes[1]; + _2 = bytes[2]; + _3 = bytes[3]; + _4 = bytes[4]; + _5 = bytes[5]; } Mac Mac::parseMac(string s) { unsigned int bytes[6]; int count = sscanf(s.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", - &bytes[5], &bytes[4], &bytes[3], &bytes[2], &bytes[1], &bytes[0]); + &bytes[0], &bytes[1], &bytes[2], &bytes[3], &bytes[4], &bytes[5]); if (count != 6) { throw BluetoothException("Unable to parseAttributeData mac: " + s); } - return Mac((uint8_t) bytes[5], (uint8_t) bytes[4], (uint8_t) bytes[3], (uint8_t) bytes[2], (uint8_t) bytes[1], (uint8_t) bytes[0]); + return Mac((uint8_t) bytes[0], (uint8_t) bytes[1], (uint8_t) bytes[2], (uint8_t) bytes[3], (uint8_t) bytes[4], (uint8_t) bytes[5]); } // ----------------------------------------------------------------------- -- cgit v1.2.3