More transport WIP

example_keyboards
Fred Sundvik 9 years ago
parent 4ee6eadf9e
commit 679bfe7c54

@ -24,10 +24,12 @@ SOFTWARE.
#include "protocol/transport.h" #include "protocol/transport.h"
static uint32_t current_send_frame; static remote_object_t* remote_objects;
static uint32_t num_remote_objects;
void init_transport(void) { void init_transport(remote_object_t* _remote_objects, uint32_t _num_remote_objects) {
current_send_frame = 0; remote_objects = _remote_objects;
num_remote_objects = _num_remote_objects;
} }
void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
@ -35,5 +37,4 @@ 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) { uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) {
return ++current_send_frame;
} }

@ -28,63 +28,66 @@ SOFTWARE.
#include "protocol/triple_buffered_object.h" #include "protocol/triple_buffered_object.h"
#define NUM_SLAVES 8 #define NUM_SLAVES 8
#define LOCAL_OBJECT_EXTRA 16
typedef struct { // master -> slave = 1 local(target all), 1 remote object
uint16_t element_size; // slave -> master = 1 local(target 0), multiple remote objects
uint16_t buffer_size; // master -> single slave (multiple local, target id), 1 remote object
uint8_t is_master; typedef enum {
uint8_t buffer[] __attribute__((aligned(4))); MASTER_TO_ALL_SLAVES,
} remote_object_t; MASTER_TO_SINGLE_SLAVE,
SLAVE_TO_MASTER,
} remote_object_type;
typedef struct { typedef struct {
uint16_t element_size; remote_object_type object_type;
uint8_t destination; uint16_t object_size;
uint8_t buffer[] __attribute__((aligned(4))); uint8_t buffer[] __attribute__((aligned(4)));
} local_object_t; } remote_object_t;
#define REMOTE_OBJECT_BUFFER(id, type) \ #define REMOTE_OBJECT_SIZE(objectsize) \
typedef struct { \ (sizeof(triple_buffer_object_t) + objectsize * 3)
triple_buffer_object_t object; \ #define LOCAL_OBJECT_SIZE(objectsize) \
type buffer[3]; \ (sizeof(triple_buffer_object_t) + (objectsize + LOCAL_OBJECT_EXTRA) * 3)
} remote_object_buffer_##id##_t;
#define MASTER_REMOTE_OBJECT(id, type) \ #define REMOTE_OBJECT_HELPER(name, type, num_local, num_remote) \
REMOTE_OBJECT_BUFFER(id, type) \
typedef struct { \ typedef struct { \
remote_object_t object; \ remote_object_t object; \
remote_object_buffer_##id##_t buffer; \ uint8_t buffer[ \
} master_remote_object_##id##_t; \ num_remote * REMOTE_OBJECT_SIZE(sizeof(type)) + \
master_remote_object_##id##_t remote_object_##id = { \ num_local * LOCAL_OBJECT_SIZE(sizeof(type))]; \
.object = { \ } remote_object_##name##_t;
.element_size = sizeof(type), \
.buffer_size = sizeof(remote_object_buffer_##id##_t), \ #define MASTER_TO_ALL_SLAVES_OBJECT(name, type) \
.is_master = true \ REMOTE_OBJECT_HELPER(name, type, 1, 1) \
}}; remote_object_##name##_t remote_object_##name = { \
.object = { \
#define SLAVE_REMOTE_OBJECT(id, type) \ .object_type = MASTER_TO_ALL_SLAVES, \
REMOTE_OBJECT_BUFFER(id, type) \ .object_size = sizeof(type), \
typedef struct { \ } \
remote_object_t object; \ };
remote_object_buffer_##id##_t buffer[NUM_SLAVES];\
} slave_remote_object_##id##_t; \ #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \
slave_remote_object_##id##_t remote_object_##id = { \ REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \
.object = { \ remote_object_##name##_t remote_object_##name = { \
.element_size = sizeof(type), \ .object = { \
.buffer_size = sizeof(remote_object_buffer_##id##_t), \ .object_type = MASTER_TO_SINGLE_SLAVE, \
.is_master = true \ .object_size = sizeof(type), \
}}; } \
};
#define LOCAL_OBJECT(id, type) \
typedef struct { \ #define SLAVE_TO_MASTER_OBJECT(name, type) \
uint32_t element_size; \ REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \
uint8_t buffer[NUM_SLAVES][sizeof(type) + 16][3]; \ remote_object_##name##_t remote_object_##name = { \
} remote_object_##id##_t; \ .object = { \
remote_object_##id##_t remote_object_##id = {.element_size = sizeof(type) + 16}; .object_type = SLAVE_TO_MASTER, \
.object_size = sizeof(type), \
#define REMOTE_OBJECT(id) (remote_object_t*)&remote_object_##id } \
};
void init_transport(void); #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name
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); 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); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size);

@ -34,32 +34,21 @@ typedef struct {
uint32_t test2; uint32_t test2;
} test_object2_t; } test_object2_t;
MASTER_REMOTE_OBJECT(0, test_object1_t); MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t);
SLAVE_REMOTE_OBJECT(1, test_object1_t); MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t);
MASTER_REMOTE_OBJECT(2, test_object2_t); SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t);
SLAVE_REMOTE_OBJECT(3, test_object2_t);
remote_object_t* test_remote_objects[] = {
// We want REMOTE_OBJECT(master_to_slave),
// master -> slave = 1 local(target all), 1 remote object REMOTE_OBJECT(master_to_single_slave),
// slave -> master = 1 local(target 0), multiple remote objects REMOTE_OBJECT(slave_to_master),
// 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); Describe(Transport);
BeforeEach(Transport) { BeforeEach(Transport) {
init_transport(); init_transport(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t));
} }
AfterEach(Transport) {} AfterEach(Transport) {}
Ensure(Transport, packet_number_is_sequential) { Ensure(Transport, write_to_local_signals_an_event) {
assert_that(transport_send_frame(0, NULL, 0), is_equal_to(1));
assert_that(transport_send_frame(0, NULL, 0), is_equal_to(2));
// It doesn't matter if the destination changes
assert_that(transport_send_frame(1, NULL, 0), is_equal_to(3));
} }

Loading…
Cancel
Save