From: Fred Sundvik <fsundvik@gmail.com>
Date: Sun, 21 Feb 2016 10:53:51 +0000 (+0200)
Subject: Local and remote objects WIP
X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=4ee6eadf9e88b89f017c3c06e2d376cf953f6a42;p=qmk_firmware.git

Local and remote objects WIP
---

diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h
index 01119857d..6f2cf277f 100644
--- a/serial_link/protocol/transport.h
+++ b/serial_link/protocol/transport.h
@@ -22,6 +22,73 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 */
 
+#ifndef SERIAL_LINK_TRANSPORT_H
+#define SERIAL_LINK_TRANSPORT_H
+
+#include "protocol/triple_buffered_object.h"
+
+#define NUM_SLAVES 8
+
+typedef struct {
+    uint16_t element_size;
+    uint16_t buffer_size;
+    uint8_t is_master;
+    uint8_t buffer[] __attribute__((aligned(4)));
+} remote_object_t;
+
+typedef struct {
+    uint16_t element_size;
+    uint8_t destination;
+    uint8_t buffer[] __attribute__((aligned(4)));
+} local_object_t;
+
+#define REMOTE_OBJECT_BUFFER(id, type) \
+typedef struct { \
+    triple_buffer_object_t object; \
+    type buffer[3]; \
+} remote_object_buffer_##id##_t;
+
+#define MASTER_REMOTE_OBJECT(id, type) \
+REMOTE_OBJECT_BUFFER(id, type) \
+typedef struct { \
+    remote_object_t object; \
+    remote_object_buffer_##id##_t buffer; \
+} master_remote_object_##id##_t; \
+master_remote_object_##id##_t remote_object_##id = { \
+     .object = { \
+        .element_size = sizeof(type), \
+        .buffer_size = sizeof(remote_object_buffer_##id##_t), \
+        .is_master = true \
+    }};
+
+#define SLAVE_REMOTE_OBJECT(id, type) \
+REMOTE_OBJECT_BUFFER(id, type) \
+typedef struct { \
+    remote_object_t object; \
+    remote_object_buffer_##id##_t buffer[NUM_SLAVES];\
+} slave_remote_object_##id##_t; \
+slave_remote_object_##id##_t remote_object_##id = { \
+     .object = { \
+        .element_size = sizeof(type), \
+        .buffer_size = sizeof(remote_object_buffer_##id##_t), \
+        .is_master = true \
+    }};
+
+#define LOCAL_OBJECT(id, type) \
+typedef struct { \
+    uint32_t element_size; \
+    uint8_t buffer[NUM_SLAVES][sizeof(type) + 16][3]; \
+} remote_object_##id##_t; \
+remote_object_##id##_t remote_object_##id = {.element_size = sizeof(type) + 16};
+
+#define REMOTE_OBJECT(id) (remote_object_t*)&remote_object_##id
+
+
 void init_transport(void);
 void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size);
 uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size);
+
+void transport_register_master_remote_object(uint8_t id, void* ptr, uint16_t size);
+void transport_register_slave_remote_object(uint8_t id, void* ptr, uint16_t size);
+
+#endif
diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h
index 705f0c49f..d224f36a2 100644
--- a/serial_link/protocol/triple_buffered_object.h
+++ b/serial_link/protocol/triple_buffered_object.h
@@ -27,7 +27,7 @@ SOFTWARE.
 
 typedef struct {
     uint8_t state;
-    uint8_t buffer[];
+    uint8_t buffer[] __attribute__((aligned(4)));
 }triple_buffer_object_t;
 
 void triple_buffer_init(triple_buffer_object_t* object);
diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c
index a64e7446d..f9f5b4773 100644
--- a/serial_link/tests/transport_tests.c
+++ b/serial_link/tests/transport_tests.c
@@ -25,6 +25,32 @@ SOFTWARE.
 #include <cgreen/cgreen.h>
 #include "protocol/transport.c"
 
+typedef struct {
+    uint32_t test;
+} test_object1_t;
+
+typedef struct {
+    uint32_t test1;
+    uint32_t test2;
+} test_object2_t;
+
+MASTER_REMOTE_OBJECT(0, test_object1_t);
+SLAVE_REMOTE_OBJECT(1, test_object1_t);
+MASTER_REMOTE_OBJECT(2, test_object2_t);
+SLAVE_REMOTE_OBJECT(3, test_object2_t);
+
+// We want
+// master -> slave = 1 local(target all), 1 remote object
+// slave -> master = 1 local(target 0), multiple remote objects
+// master -> single slave (multiple local, target id), 1 remote object
+
+remote_object_t* remote_objects[] = {
+    REMOTE_OBJECT(0),
+    REMOTE_OBJECT(1),
+    REMOTE_OBJECT(2),
+    REMOTE_OBJECT(3),
+};
+
 Describe(Transport);
 BeforeEach(Transport) {
     init_transport();