Add slave to master transport

This commit is contained in:
Fred Sundvik 2016-02-21 21:32:01 +02:00
parent 64b7efeb72
commit 7b88b8b7e1
3 changed files with 42 additions and 7 deletions

View file

@ -71,13 +71,18 @@ void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_obje
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) {
uint8_t id = data[size-1]; uint8_t id = data[size-1];
remote_object_t* obj = remote_objects[id]; remote_object_t* obj = remote_objects[id];
uint8_t* start;
if (obj->object_type == MASTER_TO_ALL_SLAVES) { if (obj->object_type == MASTER_TO_ALL_SLAVES) {
uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); 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);
} }
else if(obj->object_type == SLAVE_TO_MASTER) {
start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
start += (from - 1) * REMOTE_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) { uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) {
@ -87,12 +92,13 @@ void update_transport(void) {
int i; int i;
for(i=0;i<num_remote_objects;i++) { for(i=0;i<num_remote_objects;i++) {
remote_object_t* obj = remote_objects[i]; remote_object_t* obj = remote_objects[i];
if (obj->object_type == MASTER_TO_ALL_SLAVES) { if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; 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); uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
if (ptr) { if (ptr) {
ptr[obj->object_size] = i; ptr[obj->object_size] = i;
router_send_frame(0xFF, ptr, obj->object_size + 1); uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
router_send_frame(dest, ptr, obj->object_size + 1);
} }
} }
} }

View file

@ -108,9 +108,22 @@ typedef struct { \
} \ } \
}; \ }; \
type* begin_write_##name(void) { \ 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) { \ 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(); \ signal_data_written(); \
}\
type* read_##name(uint8_t slave) { \
remote_object_t* obj = (remote_object_t*)&remote_object_##name; \
uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\
start+=slave * REMOTE_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 REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name

View file

@ -92,3 +92,19 @@ Ensure(Transport, writes_from_master_to_all_slaves) {
assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2, is_not_equal_to(NULL));
assert_that(obj2->test, is_equal_to(5)); assert_that(obj2->test, is_equal_to(5));
} }
Ensure(Transport, writes_from_slave_to_master) {
update_transport();
test_object1_t* obj = begin_write_slave_to_master();
obj->test = 7;
expect(signal_data_written);
end_write_slave_to_master();
expect(router_send_frame,
when(destination, is_equal_to(0)));
update_transport();
transport_recv_frame(3, sent_data, sent_data_size);
test_object1_t* obj2 = read_slave_to_master(2);
assert_that(read_slave_to_master(0), is_equal_to(NULL));
assert_that(obj2, is_not_equal_to(NULL));
assert_that(obj2->test, is_equal_to(7));
}