aboutsummaryrefslogtreecommitdiff
path: root/ble/ByteBuffer.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-21 23:58:39 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-21 23:58:39 +0100
commit5926b05afa21eaac36c185e7fc458710efa30b02 (patch)
tree1d835b53fcb3dbc44b07084155a37874ce8325dc /ble/ByteBuffer.cpp
parenta76e09808905d280282a81958cb4c68f71f18ca4 (diff)
downloadble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.tar.gz
ble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.tar.bz2
ble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.tar.xz
ble-toys-5926b05afa21eaac36c185e7fc458710efa30b02.zip
o Support for reading and writing characteristics.
Diffstat (limited to 'ble/ByteBuffer.cpp')
-rw-r--r--ble/ByteBuffer.cpp37
1 files changed, 33 insertions, 4 deletions
diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp
index 820c638..377334b 100644
--- a/ble/ByteBuffer.cpp
+++ b/ble/ByteBuffer.cpp
@@ -2,19 +2,34 @@
#include <string.h>
#include <sstream>
#include <iomanip>
+#include <cassert>
+#include "log.h"
using namespace std;
+ByteBuffer ByteBuffer::alloc(std::size_t capacity) {
+ auto bytes = shared_ptr<uint8_t>(new uint8_t[capacity], [](uint8_t* p) {
+ delete[] p;
+ });
+ return ByteBuffer(bytes, capacity, (size_t) 0, (size_t) 0);
+}
+
ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity) :
- bytes(bytes), capacity(capacity), zero(bytes.get()), end(&bytes.get()[capacity]) {
- ptr = (uint8_t *) zero;
+ bytes(bytes), capacity(capacity), zero(bytes.get()), end(bytes.get()) {
+ ptr = const_cast<uint8_t *>(zero);
}
-ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t zero, size_t size) :
+ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t size) :
+ bytes(bytes), capacity(capacity), zero(bytes.get()), end(&bytes.get()[size]) {
+ assert(size <= capacity);
+ ptr = const_cast<uint8_t *>(this->zero);
+}
+
+ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t size, size_t zero) :
bytes(bytes), capacity(capacity), zero(&bytes.get()[zero]), end(&bytes.get()[size]) {
assert(zero <= size);
assert(size <= capacity);
- ptr = (uint8_t *) zero;
+ ptr = const_cast<uint8_t *>(this->zero);
}
ByteBuffer::ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, const uint8_t *zero, const uint8_t *end) :
@@ -34,6 +49,20 @@ ByteBuffer &ByteBuffer::write16le(uint16_t value) {
return *this;
}
+ByteBuffer &ByteBuffer::write(const ByteBuffer &value) {
+ return write(value.zero, value.getSize());
+}
+
+ByteBuffer &ByteBuffer::write(const uint8_t *bytes, size_t len) {
+ checkAndUpdateEnd(len);
+
+ memcpy(ptr, bytes, len);
+
+ ptr += len;
+
+ return *this;
+}
+
uint8_t ByteBuffer::get8(size_t index) const {
assertCanAccessRelative(index);
return ptr[index];