1
0

transport_sync.c 8.6 KB

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