1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "transport_sync.h"
#include "transactions.h"
#include <string.h>
#ifdef __AVR__
# include <avr/wdt.h>
#endif
#ifdef CUSTOM_UNICODE_ENABLE
#include "process_unicode_common.h"
extern unicode_config_t unicode_config;
#endif
#ifdef AUDIO_ENABLE
# include "audio.h"
extern audio_config_t audio_config;
extern bool delayed_tasks_run;
#endif
#if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
extern bool tap_toggling;
#endif
#ifdef SWAP_HANDS_ENABLE
extern bool swap_hands;
#endif
static bool watchdog_ping_done = false;
static uint32_t watchdog_timer = 0;
extern userspace_config_t userspace_config;
extern bool host_driver_disabled;
uint16_t transport_keymap_config = 0;
uint32_t transport_userspace_config = 0, transport_user_state = 0;
user_runtime_config_t user_state;
void user_state_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
if (initiator2target_buffer_size == sizeof(transport_user_state)) {
memcpy(&transport_user_state, initiator2target_buffer, initiator2target_buffer_size);
}
}
void user_keymap_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
if (initiator2target_buffer_size == sizeof(transport_keymap_config)) {
memcpy(&transport_keymap_config, initiator2target_buffer, initiator2target_buffer_size);
}
}
void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
if (initiator2target_buffer_size == sizeof(transport_userspace_config)) {
memcpy(&transport_userspace_config, initiator2target_buffer, initiator2target_buffer_size);
}
}
void watchdog_handler(uint8_t in_buflen, const void* in_data, uint8_t out_buflen, void* out_data) { watchdog_ping_done = true; }
void keyboard_post_init_transport_sync(void) {
// Register keyboard state sync split transaction
transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_state_sync);
transaction_register_rpc(RPC_ID_USER_KEYMAP_SYNC, user_keymap_sync);
transaction_register_rpc(RPC_ID_USER_CONFIG_SYNC, user_config_sync);
#ifdef __AVR__
wdt_disable();
#endif
transaction_register_rpc(RPC_ID_USER_WATCHDOG_SYNC, watchdog_handler);
watchdog_timer = timer_read32();
}
void user_transport_update(void) {
if (is_keyboard_master()) {
transport_keymap_config = keymap_config.raw;
transport_userspace_config = userspace_config.raw;
#ifdef AUDIO_ENABLE
user_state.audio_enable = is_audio_on();
user_state.audio_clicky_enable = is_clicky_on();
#endif
#if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
user_state.tap_toggling = tap_toggling;
#endif
#ifdef UNICODE_ENABLE
user_state.unicode_mode = unicode_config.input_mode;
#endif
#ifdef SWAP_HANDS_ENABLE
user_state.swap_hands = swap_hands;
#endif
user_state.host_driver_disabled = host_driver_disabled;
transport_user_state = user_state.raw;
} else {
keymap_config.raw = transport_keymap_config;
userspace_config.raw = transport_userspace_config;
user_state.raw = transport_user_state;
#ifdef UNICODE_ENABLE
unicode_config.input_mode = user_state.unicode_mode;
#endif
#if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
tap_toggling = user_state.tap_toggling;
#endif
#ifdef SWAP_HANDS_ENABLE
swap_hands = user_state.swap_hands;
#endif
host_driver_disabled = user_state.host_driver_disabled;
}
}
void user_transport_sync(void) {
if (is_keyboard_master()) {
// Keep track of the last state, so that we can tell if we need to propagate to slave
static uint16_t last_keymap = 0;
static uint32_t last_config = 0, last_sync[3], last_user_state = 0;
bool needs_sync = false;
// Check if the state values are different
if (memcmp(&transport_user_state, &last_user_state, sizeof(transport_user_state))) {
needs_sync = true;
memcpy(&last_user_state, &transport_user_state, sizeof(transport_user_state));
}
// Send to slave every 500ms regardless of state change
if (timer_elapsed32(last_sync[0]) > 250) {
needs_sync = true;
}
// Perform the sync if requested
if (needs_sync) {
if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) {
last_sync[0] = timer_read32();
}
needs_sync = false;
}
// Check if the state values are different
if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) {
needs_sync = true;
memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config));
}
// Send to slave every 500ms regardless of state change
if (timer_elapsed32(last_sync[1]) > 250) {
needs_sync = true;
}
// Perform the sync if requested
if (needs_sync) {
if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) {
last_sync[1] = timer_read32();
}
needs_sync = false;
}
// Check if the state values are different
if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) {
needs_sync = true;
memcpy(&last_config, &user_state, sizeof(transport_userspace_config));
}
// Send to slave every 500ms regardless of state change
if (timer_elapsed32(last_sync[2]) > 250) {
needs_sync = true;
}
// Perform the sync if requested
if (needs_sync) {
if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) {
last_sync[2] = timer_read32();
}
}
}
if (!watchdog_ping_done) {
if (is_keyboard_master()) {
if (timer_elapsed32(watchdog_timer) > 100) {
uint8_t any_data = 1;
if (transaction_rpc_send(RPC_ID_USER_WATCHDOG_SYNC, sizeof(any_data), &any_data)) {
watchdog_ping_done = true; // successful ping
} else {
dprint("Watchdog ping failed!\n");
}
watchdog_timer = timer_read32();
}
} else {
if (timer_elapsed32(watchdog_timer) > 3500) {
#ifdef __AVR__
wdt_enable(WDTO_250MS);
#else
NVIC_SystemReset();
#endif
while (1) {
}
}
}
}
}
void housekeeping_task_user(void) {
// Update kb_state so we can send to slave
user_transport_update();
// Data sync from master to slave
user_transport_sync();
}
|