From 415ab91c9d2ed034bbbabf3cbb8502f525b91abb Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:04:42 +0200 Subject: [PATCH] Add master to slave transport --- serial_link/protocol/transport.c | 31 ++++++++++++++++++++++++++--- serial_link/protocol/transport.h | 18 +++++++++++++---- serial_link/tests/transport_tests.c | 28 +++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 7b2c1495..55dae9fa 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -23,18 +23,43 @@ SOFTWARE. */ #include "protocol/transport.h" +#include "protocol/frame_router.h" +#include "protocol/triple_buffered_object.h" -static remote_object_t* remote_objects; +static remote_object_t** remote_objects; static uint32_t num_remote_objects; -void init_transport(remote_object_t* _remote_objects, uint32_t _num_remote_objects) { +void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { - + uint8_t id = data[size-1]; + remote_object_t* obj = remote_objects[id]; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); + } } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { } + +void update_transport(void) { + int i; + for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; + uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb); + if (ptr) { + ptr[obj->object_size] = i; + router_send_frame(0xFF, ptr, obj->object_size + 1); + } + } + } +} diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index c4c52947..fa2e00d2 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -68,9 +68,21 @@ typedef struct { \ } \ }; \ type* begin_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ @@ -103,11 +115,9 @@ typedef struct { \ #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(remote_object_t* remote_objects, uint32_t num_remote_objects); +void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); 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); +void update_transport(void); #endif diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 61b9a650..34d451de 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -25,11 +25,21 @@ SOFTWARE. #include #include #include "protocol/transport.c" +#include "protocol/triple_buffered_object.c" void signal_data_written(void) { mock(); } +static uint8_t sent_data[2048]; +static uint16_t sent_data_size; + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + mock(destination); + memcpy(sent_data + sent_data_size, data, size); + sent_data_size += size; +} + typedef struct { uint32_t test; } test_object1_t; @@ -51,7 +61,8 @@ remote_object_t* test_remote_objects[] = { Describe(Transport); BeforeEach(Transport) { - init_transport(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t)); + init_transport(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); + sent_data_size = 0; } AfterEach(Transport) {} @@ -66,3 +77,18 @@ Ensure(Transport, write_to_local_signals_an_event) { expect(signal_data_written); end_write_master_to_single_slave(1); } + +Ensure(Transport, writes_from_master_to_all_slaves) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 5; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame, + when(destination, is_equal_to(0xFF))); + update_transport(); + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(5)); +}