4 Copyright (c) 2016 Fred Sundvik
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "serial_link/protocol/transport.h"
26 #include "serial_link/protocol/frame_router.h"
27 #include "serial_link/protocol/triple_buffered_object.h"
30 #define MAX_REMOTE_OBJECTS 16
31 static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS];
32 static uint32_t num_remote_objects = 0;
34 void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
36 for(i=0;i<_num_remote_objects;i++) {
37 remote_object_t* obj = _remote_objects[i];
38 remote_objects[num_remote_objects++] = obj;
39 if (obj->object_type == MASTER_TO_ALL_SLAVES) {
40 triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
41 triple_buffer_init(tb);
42 uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
43 tb = (triple_buffer_object_t*)start;
44 triple_buffer_init(tb);
46 else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) {
47 uint8_t* start = obj->buffer;
49 for (j=0;j<NUM_SLAVES;j++) {
50 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
51 triple_buffer_init(tb);
52 start += LOCAL_OBJECT_SIZE(obj->object_size);
54 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
55 triple_buffer_init(tb);
58 uint8_t* start = obj->buffer;
59 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
60 triple_buffer_init(tb);
61 start += LOCAL_OBJECT_SIZE(obj->object_size);
63 for (j=0;j<NUM_SLAVES;j++) {
64 tb = (triple_buffer_object_t*)start;
65 triple_buffer_init(tb);
66 start += REMOTE_OBJECT_SIZE(obj->object_size);
72 void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
73 uint8_t id = data[size-1];
74 if (id < num_remote_objects) {
75 remote_object_t* obj = remote_objects[id];
76 if (obj->object_size == size - 1) {
78 if (obj->object_type == MASTER_TO_ALL_SLAVES) {
79 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
81 else if(obj->object_type == SLAVE_TO_MASTER) {
82 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
83 start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size);
86 start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);
88 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
89 void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb);
90 memcpy(ptr, data, size - 1);
91 triple_buffer_end_write_internal(tb);
96 void update_transport(void) {
98 for(i=0;i<num_remote_objects;i++) {
99 remote_object_t* obj = remote_objects[i];
100 if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
101 triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
102 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
104 ptr[obj->object_size] = i;
105 uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
106 router_send_frame(dest, ptr, obj->object_size + 1);
110 uint8_t* start = obj->buffer;
112 for (j=0;j<NUM_SLAVES;j++) {
113 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
114 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
116 ptr[obj->object_size] = i;
117 uint8_t dest = j + 1;
118 router_send_frame(dest, ptr, obj->object_size + 1);
120 start += LOCAL_OBJECT_SIZE(obj->object_size);