transport_sync.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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) { watchdog_ping_done = true; }
  51. #endif
  52. #ifdef CUSTOM_OLED_DRIVER
  53. # include "oled/oled_stuff.h"
  54. void keylogger_string_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  55. if (initiator2target_buffer_size == OLED_KEYLOGGER_LENGTH) {
  56. memcpy(&keylog_str, initiator2target_buffer, initiator2target_buffer_size);
  57. }
  58. }
  59. #endif
  60. void keyboard_post_init_transport_sync(void) {
  61. // Register keyboard state sync split transaction
  62. transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_state_sync);
  63. transaction_register_rpc(RPC_ID_USER_KEYMAP_SYNC, user_keymap_sync);
  64. transaction_register_rpc(RPC_ID_USER_CONFIG_SYNC, user_config_sync);
  65. #ifdef CUSTOM_OLED_DRIVER
  66. transaction_register_rpc(RPC_ID_USER_KEYLOG_STR, keylogger_string_sync);
  67. #endif
  68. #if defined(SPLIT_WATCHDOG_TIMEOUT)
  69. # if defined(PROTOCOL_LUFA)
  70. wdt_disable();
  71. # endif
  72. transaction_register_rpc(RPC_ID_USER_WATCHDOG_SYNC, watchdog_handler);
  73. watchdog_timer = timer_read32();
  74. #endif
  75. }
  76. void user_transport_update(void) {
  77. if (is_keyboard_master()) {
  78. transport_keymap_config = keymap_config.raw;
  79. transport_userspace_config = userspace_config.raw;
  80. #ifdef AUDIO_ENABLE
  81. user_state.audio_enable = is_audio_on();
  82. user_state.audio_clicky_enable = is_clicky_on();
  83. #endif
  84. #if defined(CUSTOM_POINTING_DEVICE)
  85. user_state.tap_toggling = tap_toggling;
  86. #endif
  87. #ifdef UNICODE_COMMON_ENABLE
  88. user_state.unicode_mode = unicode_config.input_mode;
  89. user_state.unicode_typing_mode = typing_mode;
  90. #endif
  91. #ifdef SWAP_HANDS_ENABLE
  92. user_state.swap_hands = swap_hands;
  93. #endif
  94. user_state.host_driver_disabled = host_driver_disabled;
  95. transport_user_state = user_state.raw;
  96. } else {
  97. keymap_config.raw = transport_keymap_config;
  98. userspace_config.raw = transport_userspace_config;
  99. user_state.raw = transport_user_state;
  100. #ifdef UNICODE_COMMON_ENABLE
  101. unicode_config.input_mode = user_state.unicode_mode;
  102. typing_mode = user_state.unicode_typing_mode;
  103. #endif
  104. #if defined(CUSTOM_POINTING_DEVICE)
  105. tap_toggling = user_state.tap_toggling;
  106. #endif
  107. #ifdef SWAP_HANDS_ENABLE
  108. swap_hands = user_state.swap_hands;
  109. #endif
  110. host_driver_disabled = user_state.host_driver_disabled;
  111. }
  112. }
  113. void user_transport_sync(void) {
  114. if (is_keyboard_master()) {
  115. // Keep track of the last state, so that we can tell if we need to propagate to slave
  116. static uint16_t last_keymap = 0;
  117. static uint32_t last_config = 0, last_sync[4], last_user_state = 0;
  118. bool needs_sync = false;
  119. #ifdef CUSTOM_OLED_DRIVER
  120. static char keylog_temp[OLED_KEYLOGGER_LENGTH] = {0};
  121. #endif
  122. // Check if the state values are different
  123. if (memcmp(&transport_user_state, &last_user_state, sizeof(transport_user_state))) {
  124. needs_sync = true;
  125. memcpy(&last_user_state, &transport_user_state, sizeof(transport_user_state));
  126. }
  127. // Send to slave every 500ms regardless of state change
  128. if (timer_elapsed32(last_sync[0]) > 250) {
  129. needs_sync = true;
  130. }
  131. // Perform the sync if requested
  132. if (needs_sync) {
  133. if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) {
  134. last_sync[0] = timer_read32();
  135. }
  136. needs_sync = false;
  137. }
  138. // Check if the state values are different
  139. if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) {
  140. needs_sync = true;
  141. memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config));
  142. }
  143. // Send to slave every 500ms regardless of state change
  144. if (timer_elapsed32(last_sync[1]) > 250) {
  145. needs_sync = true;
  146. }
  147. // Perform the sync if requested
  148. if (needs_sync) {
  149. if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) {
  150. last_sync[1] = timer_read32();
  151. }
  152. needs_sync = false;
  153. }
  154. // Check if the state values are different
  155. if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) {
  156. needs_sync = true;
  157. memcpy(&last_config, &user_state, sizeof(transport_userspace_config));
  158. }
  159. // Send to slave every 500ms regardless of state change
  160. if (timer_elapsed32(last_sync[2]) > 250) {
  161. needs_sync = true;
  162. }
  163. // Perform the sync if requested
  164. if (needs_sync) {
  165. if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) {
  166. last_sync[2] = timer_read32();
  167. }
  168. needs_sync = false;
  169. }
  170. #ifdef CUSTOM_OLED_DRIVER
  171. // Check if the state values are different
  172. if (memcmp(&keylog_str, &keylog_temp, OLED_KEYLOGGER_LENGTH)) {
  173. needs_sync = true;
  174. memcpy(&keylog_temp, &keylog_str, OLED_KEYLOGGER_LENGTH);
  175. }
  176. if (timer_elapsed32(last_sync[3]) > 250) {
  177. needs_sync = true;
  178. }
  179. // Perform the sync if requested
  180. if (needs_sync) {
  181. if (transaction_rpc_send(RPC_ID_USER_KEYLOG_STR, OLED_KEYLOGGER_LENGTH, &keylog_str)) {
  182. last_sync[3] = timer_read32();
  183. }
  184. needs_sync = false;
  185. }
  186. #endif
  187. }
  188. #if defined(SPLIT_WATCHDOG_TIMEOUT)
  189. if (!watchdog_ping_done) {
  190. if (is_keyboard_master()) {
  191. if (timer_elapsed32(watchdog_timer) > 100) {
  192. uint8_t any_data = 1;
  193. if (transaction_rpc_send(RPC_ID_USER_WATCHDOG_SYNC, sizeof(any_data), &any_data)) {
  194. watchdog_ping_done = true; // successful ping
  195. } else {
  196. dprint("Watchdog ping failed!\n");
  197. }
  198. watchdog_timer = timer_read32();
  199. }
  200. } else {
  201. if (timer_elapsed32(watchdog_timer) > 3500) {
  202. software_reset();
  203. while (1) {
  204. }
  205. }
  206. }
  207. }
  208. #endif
  209. }
  210. void housekeeping_task_user(void) {
  211. // Update kb_state so we can send to slave
  212. user_transport_update();
  213. // Data sync from master to slave
  214. user_transport_sync();
  215. }