aboutsummaryrefslogtreecommitdiff
path: root/ble/ByteBuffer.h
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-20 22:56:22 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-20 22:56:22 +0100
commite44813dddbf5ba063d29ae1e40862e7a7cbb6f43 (patch)
tree67009d481b8b106af6937a5f386fe4e2e15b1fcc /ble/ByteBuffer.h
parentb6f080193d71334e8afea95ae26afbc03c27fac3 (diff)
downloadble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.tar.gz
ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.tar.bz2
ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.tar.xz
ble-toys-e44813dddbf5ba063d29ae1e40862e7a7cbb6f43.zip
Reorganizing the source code:
o Moving main to apps/ o Moving the library sources to ble/ o Creating cmake files for each piece.
Diffstat (limited to 'ble/ByteBuffer.h')
-rw-r--r--ble/ByteBuffer.h91
1 files changed, 91 insertions, 0 deletions
diff --git a/ble/ByteBuffer.h b/ble/ByteBuffer.h
new file mode 100644
index 0000000..3836966
--- /dev/null
+++ b/ble/ByteBuffer.h
@@ -0,0 +1,91 @@
+#ifndef BYTE_STREAM_WRAPPER_H
+#define BYTE_STREAM_WRAPPER_H
+
+#include <cstdint>
+#include <cstdlib>
+#include <string>
+#include <stdexcept>
+
+// For now
+#include "log.h"
+
+class ByteBufferException : public std::runtime_error {
+public:
+ ByteBufferException(std::string const &what) : std::runtime_error(what) {
+ }
+};
+
+class ByteBuffer {
+public:
+ ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity);
+
+ ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, size_t zero, size_t size);
+
+ inline size_t getSize() const {
+// DF << "end=" << (uint64_t)end << ", zero=" << (uint64_t)zero << ", size=" << (end - zero);
+ return end - zero;
+ }
+
+ inline size_t getCapacity() const {
+ return capacity;
+ }
+
+ inline size_t getCursor() const {
+ return ptr - zero;
+ }
+
+ inline size_t getBytesLeft() const {
+ return end - ptr;
+ }
+
+ inline void setCursor(size_t newCursor) {
+// assertCanAccessRelative(newCursor);
+ ptr = (uint8_t *) &zero[newCursor];
+ }
+
+ inline void skip(size_t length) {
+// checkAndUpdateEnd(length);
+ ptr += length;
+ }
+
+ ByteBuffer &write8(uint8_t value);
+
+ ByteBuffer &write16le(uint16_t value);
+
+ uint8_t get8(size_t index) const;
+
+ uint8_t read8();
+
+ uint16_t read16le();
+
+ void copy(uint8_t *bytes, size_t length) const;
+
+ /**
+ * Creates a view from cursor to size.
+ */
+ ByteBuffer view() const;
+
+ // TODO: should return const
+ ByteBuffer view(size_t length) const;
+
+ std::string toString() const;
+
+private:
+ ByteBuffer(const std::shared_ptr<uint8_t> bytes, size_t capacity, const uint8_t *zero, const uint8_t *end);
+
+ ByteBuffer view(uint8_t *ptr, const uint8_t *end) const;
+
+ void checkAndUpdateEnd(size_t count);
+
+ void assertCanAccessRelative(size_t diff) const;
+
+ void assertCanAccessIndex(uint8_t *p) const;
+
+ const std::shared_ptr<uint8_t> bytes;
+ const size_t capacity;
+ const uint8_t *zero;
+ const uint8_t *end;
+ uint8_t *ptr;
+};
+
+#endif