summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/connectionbase.cpp16
-rw-r--r--common/connectionbase.h40
2 files changed, 56 insertions, 0 deletions
diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp
new file mode 100644
index 0000000..b2147fa
--- /dev/null
+++ b/common/connectionbase.cpp
@@ -0,0 +1,16 @@
+#include "connectionbase.h"
+
+#include <iostream>
+#include <string>
+
+void ConnectionBase::connected() {
+ std::cout << "Connection established." << std::endl;
+
+ write_data((uint8_t*)"Hei!\n", 5);
+
+ request_data(10);
+}
+
+void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) {
+ std::cout << "Fikk data: " << std::string((char*)data, bytes) << std::endl;
+}
diff --git a/common/connectionbase.h b/common/connectionbase.h
new file mode 100644
index 0000000..1bfc437
--- /dev/null
+++ b/common/connectionbase.h
@@ -0,0 +1,40 @@
+#ifndef CONNECTIONBASE_H
+#define CONNECTIONBASE_H
+
+#include <stdint.h>
+#include <cstdlib>
+
+// Poor excuse for a real Message class.
+class Message {
+ public:
+ typedef Message* p;
+};
+
+class ConnectionBase {
+ protected:
+ //! Signal that connection is established and ready to transfer data.
+ void connected();
+
+ //! Deliver received data.
+ //! \param data Pointer to received data. Ownership is retained by caller.
+ //! \param bytes Size of data.
+ void got_data(uint8_t* data, std::size_t bytes);
+
+ //! Called to request data.
+ //! \param bytes Amount of data requested.
+ virtual void request_data(std::size_t bytes) = 0;
+
+ //! Called to write data.
+ //! \param data Pointer to data to send. Ownership is transferred to callee.
+ //! \param bytes Size of data.
+ virtual void write_data(uint8_t* data, std::size_t bytes) = 0;
+
+ public:
+ //! Send a message.
+ void send(const Message::p& msg);
+
+ //! Get received message or null if queue empty.
+ Message::p recv();
+};
+
+#endif