]> 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 add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) {
35     unsigned int i;
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);
45         }
46         else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) {
47             uint8_t* start = obj->buffer;
48             unsigned int j;
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);
53             }
54             triple_buffer_object_t* tb = (triple_buffer_object_t*)start;
55             triple_buffer_init(tb);
56         }
57         else {
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);
62             unsigned int j;
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);
67             }
68         }
69     }
70 }
71
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) {
77             uint8_t* start;
78             if (obj->object_type == MASTER_TO_ALL_SLAVES) {
79                 start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);
80             }
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);
84             }
85             else {
86                 start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);
87             }
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);
92         }
93     }
94 }
95
96 void update_transport(void) {
97     unsigned int i;
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);
103             if (ptr) {
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);
107             }
108         }
109         else {
110             uint8_t* start = obj->buffer;
111             unsigned int j;
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);
115                 if (ptr) {
116                     ptr[obj->object_size] = i;
117                     uint8_t dest = j + 1;
118                     router_send_frame(dest, ptr, obj->object_size + 1);
119                 }
120                 start += LOCAL_OBJECT_SIZE(obj->object_size);
121             }
122         }
123     }
124 }