2016-02-20 13:49:31 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2016 Fred Sundvik
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-02-21 21:17:59 +00:00
|
|
|
#include "serial_link/protocol/transport.h"
|
|
|
|
#include "serial_link/protocol/frame_router.h"
|
|
|
|
#include "serial_link/protocol/triple_buffered_object.h"
|
|
|
|
#include <string.h>
|
2016-02-20 13:49:31 +00:00
|
|
|
|
2016-03-12 17:35:09 +00:00
|
|
|
#define MAX_REMOTE_OBJECTS 16
|
|
|
|
static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS];
|
|
|
|
static uint32_t num_remote_objects = 0;
|
2016-02-20 13:49:31 +00:00
|
|
|
|
2016-03-12 17:35:09 +00:00
|
|
|
void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
|
2016-02-21 21:17:59 +00:00
|
|
|
unsigned int i;
|
2016-03-12 17:35:09 +00:00
|
|
|
for(i=0;i<_num_remote_objects;i++) {
|
|
|
|
remote_object_t* obj = _remote_objects[i];
|
|
|
|
remote_objects[num_remote_objects++] = obj;
|
2016-02-21 19:15:42 +00:00
|
|
|
if (obj->object_type == MASTER_TO_ALL_SLAVES) {
|
|
|
|
triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
|
|
|
|
triple_buffer_init(tb);
|
|
|
|
uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
|
|
|
|
tb = (triple_buffer_object_t*)start;
|
|
|
|
triple_buffer_init(tb);
|
|
|
|
}
|
|
|
|
else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) {
|
|
|
|
uint8_t* start = obj->buffer;
|
2016-02-21 21:17:59 +00:00
|
|
|
unsigned int j;
|
2016-02-21 19:15:42 +00:00
|
|
|
for (j=0;j<NUM_SLAVES;j++) {
|
|
|
|
triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
|
|
|
|
triple_buffer_init(tb);
|
|
|
|
start += LOCAL_OBJECT_SIZE(obj->object_size);
|
|
|
|
}
|
|
|
|
triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
|
|
|
|
triple_buffer_init(tb);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint8_t* start = obj->buffer;
|
|
|
|
triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
|
|
|
|
triple_buffer_init(tb);
|
|
|
|
start += LOCAL_OBJECT_SIZE(obj->object_size);
|
2016-02-21 21:17:59 +00:00
|
|
|
unsigned int j;
|
2016-02-21 19:15:42 +00:00
|
|
|
for (j=0;j<NUM_SLAVES;j++) {
|
|
|
|
tb = (triple_buffer_object_t*)start;
|
|
|
|
triple_buffer_init(tb);
|
|
|
|
start += REMOTE_OBJECT_SIZE(obj->object_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-20 13:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
|
2016-02-21 19:04:42 +00:00
|
|
|
uint8_t id = data[size-1];
|
2016-03-12 17:41:34 +00:00
|
|
|
if (id < num_remote_objects) {
|
|
|
|
remote_object_t* obj = remote_objects[id];
|
|
|
|
uint8_t* start;
|
|
|
|
if (obj->object_type == MASTER_TO_ALL_SLAVES) {
|
|
|
|
start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
start = obj->buffer + NUM_SLAVES * 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);
|
2016-02-21 19:47:40 +00:00
|
|
|
}
|
2016-02-20 13:49:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:04:42 +00:00
|
|
|
void update_transport(void) {
|
2016-02-21 21:17:59 +00:00
|
|
|
unsigned int i;
|
2016-02-21 19:04:42 +00:00
|
|
|
for(i=0;i<num_remote_objects;i++) {
|
|
|
|
remote_object_t* obj = remote_objects[i];
|
2016-02-21 19:32:01 +00:00
|
|
|
if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
|
2016-02-21 19:04:42 +00:00
|
|
|
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;
|
2016-02-21 19:32:01 +00:00
|
|
|
uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
|
|
|
|
router_send_frame(dest, ptr, obj->object_size + 1);
|
2016-02-21 19:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-21 19:47:40 +00:00
|
|
|
else {
|
|
|
|
uint8_t* start = obj->buffer;
|
2016-02-21 21:17:59 +00:00
|
|
|
unsigned int j;
|
2016-02-21 19:47:40 +00:00
|
|
|
for (j=0;j<NUM_SLAVES;j++) {
|
|
|
|
triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
|
|
|
|
uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
|
|
|
|
if (ptr) {
|
|
|
|
ptr[obj->object_size] = i;
|
|
|
|
uint8_t dest = j + 1;
|
|
|
|
router_send_frame(dest, ptr, obj->object_size + 1);
|
|
|
|
}
|
|
|
|
start += LOCAL_OBJECT_SIZE(obj->object_size);
|
|
|
|
}
|
|
|
|
}
|
2016-02-21 19:04:42 +00:00
|
|
|
}
|
|
|
|
}
|