diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-12-20 08:35:51 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-12-20 08:35:51 +0100 |
commit | ffa313e80a27005405334db6491075442f6e1abd (patch) | |
tree | 23a48c91cfc0810afe88cc6104d0667516fbd4a9 /ble | |
parent | af416f81b14a3bee682e300b165a6efdb9739d54 (diff) | |
download | ble-toys-ffa313e80a27005405334db6491075442f6e1abd.tar.gz ble-toys-ffa313e80a27005405334db6491075442f6e1abd.tar.bz2 ble-toys-ffa313e80a27005405334db6491075442f6e1abd.tar.xz ble-toys-ffa313e80a27005405334db6491075442f6e1abd.zip |
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().
Diffstat (limited to 'ble')
-rw-r--r-- | ble/Bluetooth.cpp | 43 | ||||
-rw-r--r-- | ble/LinuxBluetooth.cpp | 13 |
2 files changed, 31 insertions, 25 deletions
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]); } // ----------------------------------------------------------------------- diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index 9e7193f..3c4925c 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -307,7 +307,7 @@ void LinuxBluetoothGatt::writeHandle(uint16_t handle, const ByteBuffer &bytes) { writeAndRead(buffer, buffer); auto cursor = buffer.getPosition(); - buffer.setPosition(0); + buffer.setCursor(0); AttPdu::parseWrite(buffer); @@ -319,7 +319,7 @@ void LinuxBluetoothGatt::writeHandle(uint16_t handle, const ByteBuffer &bytes) { } } -ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer &response) { +ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer& response) { uint8_t b[mtu]; ByteBuffer buffer{b, mtu}; @@ -328,7 +328,7 @@ ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristicPtr &c writeAndRead(buffer, response); auto cursor = response.getPosition(); - response.setPosition(0); + response.setCursor(0); AttPdu::parseRead(response); @@ -428,7 +428,7 @@ void LinuxBluetoothGatt::discoverServices() { } while (startHandle != 0xffff); } -void LinuxBluetoothGatt::writeAndRead(const ByteBuffer &buffer, ByteBuffer &response) { +void LinuxBluetoothGatt::writeAndRead(const ByteBuffer &buffer, ByteBuffer& response) { // LOG_DEBUG("pdu size=" << out.getCursor()); auto to_be_written = buffer.getPosition(); @@ -447,8 +447,9 @@ void LinuxBluetoothGatt::writeAndRead(const ByteBuffer &buffer, ByteBuffer &resp throw BluetoothException(&device, "read(): " + errnoAsString()); } - response.setPosition(static_cast<size_t>(r)); + response.setCursor(static_cast<size_t>(r)); LOG_DEBUG("read: " << r << " bytes: " << response.viewBeginningToCursor().toString()); + response.setCursor(static_cast<size_t>(r)); } void LinuxBluetoothGatt::writeL2cap(ByteBuffer &buffer) { @@ -459,7 +460,7 @@ void LinuxBluetoothGatt::writeL2cap(ByteBuffer &buffer) { throw BluetoothException(&device, "Expected to write " + to_string(to_be_written) + " but wrote only " + to_string(written)); } - buffer.setPosition(0); + buffer.setCursor(0); } AttVariant LinuxBluetoothGatt::processAvailableMessages(ByteBuffer &buffer) { |