transport_sync.c 8.4 KB

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