aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2018-11-19 22:09:59 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2018-11-19 22:09:59 +0100
commitc83b35d6456c8a77e5b8f6a08c9262122c3cbcfc (patch)
tree68230e76666b5ef2a01471fc9d6951a719856662 /include
parent52450ed3034b0ba058ea2c9f9baa2d5f78df6a94 (diff)
downloadble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.tar.gz
ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.tar.bz2
ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.tar.xz
ble-toys-c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc.zip
ByteBuffer:
o Reducing silliness, no allocations by ByteBuffer. o Create StaticByteBuffer as a nice one-liner to create a buffer. LinuxBluetooth: methods that want a buffer needs to pass it in, ByteBuffer is not allocating anymore.
Diffstat (limited to 'include')
-rw-r--r--include/ble/Bluetooth.h2
-rw-r--r--include/ble/ByteBuffer.h29
2 files changed, 19 insertions, 12 deletions
diff --git a/include/ble/Bluetooth.h b/include/ble/Bluetooth.h
index 477bd29..d95d843 100644
--- a/include/ble/Bluetooth.h
+++ b/include/ble/Bluetooth.h
@@ -129,7 +129,7 @@ public:
virtual void writeValue(const BluetoothGattCharacteristicPtr &c, const ByteBuffer &bytes) = 0;
- virtual ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c) = 0;
+ virtual ByteBuffer readValue(const BluetoothGattCharacteristicPtr &c, ByteBuffer& response) = 0;
virtual void discoverServices() = 0;
diff --git a/include/ble/ByteBuffer.h b/include/ble/ByteBuffer.h
index 5e904c2..09deca5 100644
--- a/include/ble/ByteBuffer.h
+++ b/include/ble/ByteBuffer.h
@@ -1,13 +1,9 @@
#ifndef BYTE_STREAM_WRAPPER_H
#define BYTE_STREAM_WRAPPER_H
-#include <cstddef>
#include <cstdint>
-#include <cstdlib>
-#include <iosfwd>
#include <memory>
#include <stdexcept>
-#include <math.h>
namespace FLOAT {
static const uint32_t positive_infinity = 0x007FFFFE;
@@ -38,8 +34,6 @@ public:
class ByteBuffer {
public:
- static ByteBuffer alloc(std::size_t capacity);
-
template<size_t N>
explicit ByteBuffer(uint8_t (&bytes)[N]) : zero(bytes), end_(&bytes[N]), cursor(bytes) {}
@@ -58,7 +52,9 @@ public:
}
inline ByteBuffer &setCursor(size_t newCursor) {
- cursor = (uint8_t *) &zero[newCursor];
+ auto tmp = (uint8_t *) &zero[newCursor];
+ assertCanAccessIndex(tmp);
+ cursor = tmp;
return *this;
}
@@ -66,19 +62,19 @@ public:
cursor += length;
}
- inline const uint8_t *cbegin() {
+ inline const uint8_t *cbegin() const {
return zero;
}
- inline const uint8_t *cend() {
+ inline const uint8_t *cend() const {
return end_;
}
- inline uint8_t *begin() {
+ inline uint8_t *begin() const {
return const_cast<uint8_t *>(zero);
}
- inline uint8_t *end() {
+ inline uint8_t *end() const {
return const_cast<uint8_t *>(end_);
}
@@ -120,6 +116,8 @@ public:
void copy(uint8_t *bytes, size_t length) const;
+ void copy(ByteBuffer& other) const;
+
void reset();
/**
@@ -147,4 +145,13 @@ private:
uint8_t *cursor;
};
+template<size_t N>
+class StaticByteBuffer : public ByteBuffer {
+public:
+ StaticByteBuffer() : ByteBuffer(raw) {}
+
+private:
+ uint8_t raw[N] = {};
+};
+
#endif