]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/serial_link/protocol/transport.c
Merge branch 'master' of https://github.com/jackhumbert/qmk_firmware
[qmk_firmware.git] / quantum / serial_link / protocol / transport.c
1 /*
2 The MIT License (MIT)
3
4 Copyright (c) 2016 Fred Sundvik
5
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:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
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
22 SOFTWARE.
23 */
24
25 #include "serial_link/protocol/transport.h"
26 #include "serial_link/protocol/frame_router.h"
27 #include "serial_link/protocol/triple_buffered_object.h"
28 #include <string.h>
29
30 #define MAX_REMOTE_OBJECTS 16
31 static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS];
32 static uint32_t num_remote_objects = 0;
33
34 void reinitialize_serial_link_transport(void) {
35     num_remote_objects = 0;
36 }
37
38 void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
39     unsigned int i;
40     for(i=0;i<_num_remote_objects;i++) {
41         remote_object_t* obj = _remote_objects[i];
42         remote_objects[num_remote_objects++] = obj;
43         if (obj->object_type == MASTER_TO_ALL_SLAVES) {
44             triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
45             triple_buffer_init(tb);
46             uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
47             tb = (triple_buffer_object_t*)start;
48             triple_buffer_init(tb);
49         }
50         else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) {
51             uint8_t* start = obj->buffer;
52             unsigned int j;
53             for (j=0;j<NUM_SLAVES;j++) {
54                 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
55                 triple_buffer_init(tb);
56                 start += LOCAL_OBJECT_SIZE(obj->object_size);
57             }
58             triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
59             triple_buffer_init(tb);
60         }
61         else {
62             uint8_t* start = obj->buffer;
63             triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
64             triple_buffer_init(tb);
65             start += LOCAL_OBJECT_SIZE(obj->object_size);
66             unsigned int j;
67             for (j=0;j<NUM_SLAVES;j++) {
68                 tb = (triple_buffer_object_t*)start;
69                 triple_buffer_init(tb);
70                 start += REMOTE_OBJECT_SIZE(obj->object_size);
71             }
72         }
73     }
74 }
75
76 void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
77     uint8_t id = data[size-1];
78     if (id < num_remote_objects) {
79         remote_object_t* obj = remote_objects[id];
80         if (obj->object_size == size - 1) {
81             uint8_t* start;
82             if (obj->object_type == MASTER_TO_ALL_SLAVES) {
83                 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
84             }
85             else if(obj->object_type == SLAVE_TO_MASTER) {
86                 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
87                 start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size);
88             }
89             else {
90                 start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);
91             }
92             triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
93             void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb);
94             memcpy(ptr, data, size - 1);
95             triple_buffer_end_write_internal(tb);
96         }
97     }
98 }
99
100 void update_transport(void) {
101     unsigned int i;
102     for(i=0;i<num_remote_objects;i++) {
103         remote_object_t* obj = remote_objects[i];
104         if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) {
105             triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer;
106             uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
107             if (ptr) {
108                 ptr[obj->object_size] = i;
109                 uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0;
110                 router_send_frame(dest, ptr, obj->object_size + 1);
111             }
112         }
113         else {
114             uint8_t* start = obj->buffer;
115             unsigned int j;
116             for (j=0;j<NUM_SLAVES;j++) {
117                 triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
118                 uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb);
119                 if (ptr) {
120                     ptr[obj->object_size] = i;
121                     uint8_t dest = j + 1;
122                     router_send_frame(dest, ptr, obj->object_size + 1);
123                 }
124                 start += LOCAL_OBJECT_SIZE(obj->object_size);
125             }
126         }
127     }
128 }