logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

transactions.c (44775B)


  1. /* Copyright 2021 QMK
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <stddef.h>
  19. #include "crc.h"
  20. #include "debug.h"
  21. #include "matrix.h"
  22. #include "host.h"
  23. #include "action_util.h"
  24. #include "sync_timer.h"
  25. #include "wait.h"
  26. #include "transactions.h"
  27. #include "transport.h"
  28. #include "transaction_id_define.h"
  29. #include "split_util.h"
  30. #include "synchronization_util.h"
  31. #ifdef BACKLIGHT_ENABLE
  32. # include "backlight.h"
  33. #endif
  34. #ifdef RGBLIGHT_ENABLE
  35. # include "rgblight.h"
  36. #endif
  37. #ifdef LED_MATRIX_ENABLE
  38. # include "led_matrix.h"
  39. #endif
  40. #ifdef RGB_MATRIX_ENABLE
  41. # include "rgb_matrix.h"
  42. #endif
  43. #ifdef OLED_ENABLE
  44. # include "oled_driver.h"
  45. #endif
  46. #ifdef ST7565_ENABLE
  47. # include "st7565.h"
  48. #endif
  49. #ifdef ENCODER_ENABLE
  50. # include "encoder.h"
  51. #endif
  52. #ifdef HAPTIC_ENABLE
  53. # include "haptic.h"
  54. #endif
  55. #ifdef POINTING_DEVICE_ENABLE
  56. # include "pointing_device.h"
  57. #endif
  58. #ifdef OS_DETECTION_ENABLE
  59. # include "os_detection.h"
  60. #endif
  61. #ifdef WPM_ENABLE
  62. # include "wpm.h"
  63. #endif
  64. #define SYNC_TIMER_OFFSET 2
  65. #ifndef FORCED_SYNC_THROTTLE_MS
  66. # define FORCED_SYNC_THROTTLE_MS 100
  67. #endif // FORCED_SYNC_THROTTLE_MS
  68. #define sizeof_member(type, member) sizeof(((type *)NULL)->member)
  69. #define trans_initiator2target_initializer_cb(member, cb) \
  70. { sizeof_member(split_shared_memory_t, member), offsetof(split_shared_memory_t, member), 0, 0, cb }
  71. #define trans_initiator2target_initializer(member) trans_initiator2target_initializer_cb(member, NULL)
  72. #define trans_target2initiator_initializer_cb(member, cb) \
  73. { 0, 0, sizeof_member(split_shared_memory_t, member), offsetof(split_shared_memory_t, member), cb }
  74. #define trans_target2initiator_initializer(member) trans_target2initiator_initializer_cb(member, NULL)
  75. #define trans_initiator2target_cb(cb) \
  76. { 0, 0, 0, 0, cb }
  77. #define transport_write(id, data, length) transport_execute_transaction(id, data, length, NULL, 0)
  78. #define transport_read(id, data, length) transport_execute_transaction(id, NULL, 0, data, length)
  79. #define transport_exec(id) transport_execute_transaction(id, NULL, 0, NULL, 0)
  80. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  81. // Forward-declare the RPC callback handlers
  82. void slave_rpc_info_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer);
  83. void slave_rpc_exec_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer);
  84. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  85. ////////////////////////////////////////////////////
  86. // Helpers
  87. static bool transaction_handler_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[], const char *prefix, bool (*handler)(matrix_row_t master_matrix[], matrix_row_t slave_matrix[])) {
  88. int num_retries = is_transport_connected() ? 10 : 1;
  89. for (int iter = 1; iter <= num_retries; ++iter) {
  90. if (iter > 1) {
  91. for (int i = 0; i < iter * iter; ++i) {
  92. wait_us(10);
  93. }
  94. }
  95. bool this_okay = true;
  96. this_okay = handler(master_matrix, slave_matrix);
  97. if (this_okay) return true;
  98. }
  99. dprintf("Failed to execute %s\n", prefix);
  100. return false;
  101. }
  102. #define TRANSACTION_HANDLER_MASTER(prefix) \
  103. do { \
  104. if (!transaction_handler_master(master_matrix, slave_matrix, #prefix, &prefix##_handlers_master)) return false; \
  105. } while (0)
  106. /**
  107. * @brief Constructs a transaction handler that doesn't acquire a lock to the
  108. * split shared memory. Therefore the locking and unlocking has to be done
  109. * manually inside the handler. Use this macro only if the handler is
  110. * non-deterministic in runtime and thus needs a manual lock unlock
  111. * implementation to hold the lock for the shortest possible time.
  112. */
  113. #define TRANSACTION_HANDLER_SLAVE(prefix) \
  114. do { \
  115. prefix##_handlers_slave(master_matrix, slave_matrix); \
  116. } while (0)
  117. /**
  118. * @brief Constructs a transaction handler that automatically acquires a lock to
  119. * safely access the split shared memory and releases the lock again after
  120. * processing the handler. Use this macro if the handler is fast and
  121. * deterministic in runtime and thus holds the lock only for a very short time.
  122. * If not fallback to manually locking and unlocking inside the handler.
  123. */
  124. #define TRANSACTION_HANDLER_SLAVE_AUTOLOCK(prefix) \
  125. do { \
  126. split_shared_memory_lock(); \
  127. prefix##_handlers_slave(master_matrix, slave_matrix); \
  128. split_shared_memory_unlock(); \
  129. } while (0)
  130. inline static bool read_if_checksum_mismatch(int8_t trans_id_checksum, int8_t trans_id_retrieve, uint32_t *last_update, void *destination, const void *equiv_shmem, size_t length) {
  131. uint8_t curr_checksum;
  132. bool okay = transport_read(trans_id_checksum, &curr_checksum, sizeof(curr_checksum));
  133. if (okay && (timer_elapsed32(*last_update) >= FORCED_SYNC_THROTTLE_MS || curr_checksum != crc8(equiv_shmem, length))) {
  134. okay &= transport_read(trans_id_retrieve, destination, length);
  135. okay &= curr_checksum == crc8(equiv_shmem, length);
  136. if (okay) {
  137. *last_update = timer_read32();
  138. }
  139. } else {
  140. memcpy(destination, equiv_shmem, length);
  141. }
  142. return okay;
  143. }
  144. inline static bool send_if_condition(int8_t trans_id, uint32_t *last_update, bool condition, void *source, size_t length) {
  145. bool okay = true;
  146. if (timer_elapsed32(*last_update) >= FORCED_SYNC_THROTTLE_MS || condition) {
  147. okay &= transport_write(trans_id, source, length);
  148. if (okay) {
  149. *last_update = timer_read32();
  150. }
  151. }
  152. return okay;
  153. }
  154. inline static bool send_if_data_mismatch(int8_t trans_id, uint32_t *last_update, void *source, const void *equiv_shmem, size_t length) {
  155. // Just run a memcmp to compare the source and equivalent shmem location
  156. return send_if_condition(trans_id, last_update, (memcmp(source, equiv_shmem, length) != 0), source, length);
  157. }
  158. ////////////////////////////////////////////////////
  159. // Slave matrix
  160. static bool slave_matrix_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  161. static uint32_t last_update = 0;
  162. static matrix_row_t last_matrix[(MATRIX_ROWS) / 2] = {0}; // last successfully-read matrix, so we can replicate if there are checksum errors
  163. matrix_row_t temp_matrix[(MATRIX_ROWS) / 2]; // holding area while we test whether or not checksum is correct
  164. bool okay = read_if_checksum_mismatch(GET_SLAVE_MATRIX_CHECKSUM, GET_SLAVE_MATRIX_DATA, &last_update, temp_matrix, split_shmem->smatrix.matrix, sizeof(split_shmem->smatrix.matrix));
  165. if (okay) {
  166. // Checksum matches the received data, save as the last matrix state
  167. memcpy(last_matrix, temp_matrix, sizeof(temp_matrix));
  168. }
  169. // Copy out the last-known-good matrix state to the slave matrix
  170. memcpy(slave_matrix, last_matrix, sizeof(last_matrix));
  171. return okay;
  172. }
  173. static void slave_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  174. memcpy(split_shmem->smatrix.matrix, slave_matrix, sizeof(split_shmem->smatrix.matrix));
  175. split_shmem->smatrix.checksum = crc8(split_shmem->smatrix.matrix, sizeof(split_shmem->smatrix.matrix));
  176. }
  177. // clang-format off
  178. #define TRANSACTIONS_SLAVE_MATRIX_MASTER() TRANSACTION_HANDLER_MASTER(slave_matrix)
  179. #define TRANSACTIONS_SLAVE_MATRIX_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(slave_matrix)
  180. #define TRANSACTIONS_SLAVE_MATRIX_REGISTRATIONS \
  181. [GET_SLAVE_MATRIX_CHECKSUM] = trans_target2initiator_initializer(smatrix.checksum), \
  182. [GET_SLAVE_MATRIX_DATA] = trans_target2initiator_initializer(smatrix.matrix),
  183. // clang-format on
  184. ////////////////////////////////////////////////////
  185. // Master matrix
  186. #ifdef SPLIT_TRANSPORT_MIRROR
  187. static bool master_matrix_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  188. static uint32_t last_update = 0;
  189. return send_if_data_mismatch(PUT_MASTER_MATRIX, &last_update, master_matrix, split_shmem->mmatrix.matrix, sizeof(split_shmem->mmatrix.matrix));
  190. }
  191. static void master_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  192. // Always copy to the master matrix
  193. memcpy(master_matrix, split_shmem->mmatrix.matrix, sizeof(split_shmem->mmatrix.matrix));
  194. }
  195. # define TRANSACTIONS_MASTER_MATRIX_MASTER() TRANSACTION_HANDLER_MASTER(master_matrix)
  196. # define TRANSACTIONS_MASTER_MATRIX_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(master_matrix)
  197. # define TRANSACTIONS_MASTER_MATRIX_REGISTRATIONS [PUT_MASTER_MATRIX] = trans_initiator2target_initializer(mmatrix.matrix),
  198. #else // SPLIT_TRANSPORT_MIRROR
  199. # define TRANSACTIONS_MASTER_MATRIX_MASTER()
  200. # define TRANSACTIONS_MASTER_MATRIX_SLAVE()
  201. # define TRANSACTIONS_MASTER_MATRIX_REGISTRATIONS
  202. #endif // SPLIT_TRANSPORT_MIRROR
  203. ////////////////////////////////////////////////////
  204. // Encoders
  205. #ifdef ENCODER_ENABLE
  206. static bool encoder_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  207. static uint32_t last_update = 0;
  208. static uint8_t last_checksum = 0;
  209. encoder_events_t temp_events;
  210. bool okay = read_if_checksum_mismatch(GET_ENCODERS_CHECKSUM, GET_ENCODERS_DATA, &last_update, &temp_events, &split_shmem->encoders.events, sizeof(temp_events));
  211. if (okay) {
  212. if (last_checksum != split_shmem->encoders.checksum) {
  213. bool actioned = false;
  214. uint8_t index;
  215. bool clockwise;
  216. while (okay && encoder_dequeue_event_advanced(&split_shmem->encoders.events, &index, &clockwise)) {
  217. okay &= encoder_queue_event(index, clockwise);
  218. actioned = true;
  219. }
  220. if (actioned) {
  221. okay &= transport_exec(CMD_ENCODER_DRAIN);
  222. }
  223. last_checksum = split_shmem->encoders.checksum;
  224. }
  225. }
  226. return okay;
  227. }
  228. static void encoder_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  229. // Always prepare the encoder state for read.
  230. encoder_retrieve_events(&split_shmem->encoders.events);
  231. // Now update the checksum given that the encoders has been written to
  232. split_shmem->encoders.checksum = crc8(&split_shmem->encoders.events, sizeof(split_shmem->encoders.events));
  233. }
  234. static void encoder_handlers_slave_drain(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  235. encoder_signal_queue_drain();
  236. }
  237. // clang-format off
  238. # define TRANSACTIONS_ENCODERS_MASTER() TRANSACTION_HANDLER_MASTER(encoder)
  239. # define TRANSACTIONS_ENCODERS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(encoder)
  240. # define TRANSACTIONS_ENCODERS_REGISTRATIONS \
  241. [GET_ENCODERS_CHECKSUM] = trans_target2initiator_initializer(encoders.checksum), \
  242. [GET_ENCODERS_DATA] = trans_target2initiator_initializer(encoders.events), \
  243. [CMD_ENCODER_DRAIN] = trans_initiator2target_cb(encoder_handlers_slave_drain),
  244. // clang-format on
  245. #else // ENCODER_ENABLE
  246. # define TRANSACTIONS_ENCODERS_MASTER()
  247. # define TRANSACTIONS_ENCODERS_SLAVE()
  248. # define TRANSACTIONS_ENCODERS_REGISTRATIONS
  249. #endif // ENCODER_ENABLE
  250. ////////////////////////////////////////////////////
  251. // Sync timer
  252. #ifndef DISABLE_SYNC_TIMER
  253. static bool sync_timer_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  254. static uint32_t last_update = 0;
  255. bool okay = true;
  256. if (timer_elapsed32(last_update) >= FORCED_SYNC_THROTTLE_MS) {
  257. uint32_t sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET;
  258. okay &= transport_write(PUT_SYNC_TIMER, &sync_timer, sizeof(sync_timer));
  259. if (okay) {
  260. last_update = timer_read32();
  261. }
  262. }
  263. return okay;
  264. }
  265. static void sync_timer_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  266. static uint32_t last_sync_timer = 0;
  267. if (last_sync_timer != split_shmem->sync_timer) {
  268. last_sync_timer = split_shmem->sync_timer;
  269. sync_timer_update(last_sync_timer);
  270. }
  271. }
  272. # define TRANSACTIONS_SYNC_TIMER_MASTER() TRANSACTION_HANDLER_MASTER(sync_timer)
  273. # define TRANSACTIONS_SYNC_TIMER_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(sync_timer)
  274. # define TRANSACTIONS_SYNC_TIMER_REGISTRATIONS [PUT_SYNC_TIMER] = trans_initiator2target_initializer(sync_timer),
  275. #else // DISABLE_SYNC_TIMER
  276. # define TRANSACTIONS_SYNC_TIMER_MASTER()
  277. # define TRANSACTIONS_SYNC_TIMER_SLAVE()
  278. # define TRANSACTIONS_SYNC_TIMER_REGISTRATIONS
  279. #endif // DISABLE_SYNC_TIMER
  280. ////////////////////////////////////////////////////
  281. // Layer state
  282. #if !defined(NO_ACTION_LAYER) && defined(SPLIT_LAYER_STATE_ENABLE)
  283. static bool layer_state_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  284. static uint32_t last_layer_state_update = 0;
  285. static uint32_t last_default_layer_state_update = 0;
  286. bool okay = send_if_condition(PUT_LAYER_STATE, &last_layer_state_update, (layer_state != split_shmem->layers.layer_state), &layer_state, sizeof(layer_state));
  287. if (okay) {
  288. okay &= send_if_condition(PUT_DEFAULT_LAYER_STATE, &last_default_layer_state_update, (default_layer_state != split_shmem->layers.default_layer_state), &default_layer_state, sizeof(default_layer_state));
  289. }
  290. return okay;
  291. }
  292. static void layer_state_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  293. layer_state = split_shmem->layers.layer_state;
  294. default_layer_state = split_shmem->layers.default_layer_state;
  295. }
  296. // clang-format off
  297. # define TRANSACTIONS_LAYER_STATE_MASTER() TRANSACTION_HANDLER_MASTER(layer_state)
  298. # define TRANSACTIONS_LAYER_STATE_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(layer_state)
  299. # define TRANSACTIONS_LAYER_STATE_REGISTRATIONS \
  300. [PUT_LAYER_STATE] = trans_initiator2target_initializer(layers.layer_state), \
  301. [PUT_DEFAULT_LAYER_STATE] = trans_initiator2target_initializer(layers.default_layer_state),
  302. // clang-format on
  303. #else // !defined(NO_ACTION_LAYER) && defined(SPLIT_LAYER_STATE_ENABLE)
  304. # define TRANSACTIONS_LAYER_STATE_MASTER()
  305. # define TRANSACTIONS_LAYER_STATE_SLAVE()
  306. # define TRANSACTIONS_LAYER_STATE_REGISTRATIONS
  307. #endif // !defined(NO_ACTION_LAYER) && defined(SPLIT_LAYER_STATE_ENABLE)
  308. ////////////////////////////////////////////////////
  309. // LED state
  310. #ifdef SPLIT_LED_STATE_ENABLE
  311. static bool led_state_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  312. static uint32_t last_update = 0;
  313. uint8_t led_state = host_keyboard_leds();
  314. return send_if_data_mismatch(PUT_LED_STATE, &last_update, &led_state, &split_shmem->led_state, sizeof(led_state));
  315. }
  316. static void led_state_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  317. void set_split_host_keyboard_leds(uint8_t led_state);
  318. set_split_host_keyboard_leds(split_shmem->led_state);
  319. }
  320. # define TRANSACTIONS_LED_STATE_MASTER() TRANSACTION_HANDLER_MASTER(led_state)
  321. # define TRANSACTIONS_LED_STATE_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(led_state)
  322. # define TRANSACTIONS_LED_STATE_REGISTRATIONS [PUT_LED_STATE] = trans_initiator2target_initializer(led_state),
  323. #else // SPLIT_LED_STATE_ENABLE
  324. # define TRANSACTIONS_LED_STATE_MASTER()
  325. # define TRANSACTIONS_LED_STATE_SLAVE()
  326. # define TRANSACTIONS_LED_STATE_REGISTRATIONS
  327. #endif // SPLIT_LED_STATE_ENABLE
  328. ////////////////////////////////////////////////////
  329. // Mods
  330. #ifdef SPLIT_MODS_ENABLE
  331. static bool mods_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  332. static uint32_t last_update = 0;
  333. bool mods_need_sync = timer_elapsed32(last_update) >= FORCED_SYNC_THROTTLE_MS;
  334. split_mods_sync_t new_mods;
  335. new_mods.real_mods = get_mods();
  336. if (!mods_need_sync && new_mods.real_mods != split_shmem->mods.real_mods) {
  337. mods_need_sync = true;
  338. }
  339. new_mods.weak_mods = get_weak_mods();
  340. if (!mods_need_sync && new_mods.weak_mods != split_shmem->mods.weak_mods) {
  341. mods_need_sync = true;
  342. }
  343. # ifndef NO_ACTION_ONESHOT
  344. new_mods.oneshot_mods = get_oneshot_mods();
  345. if (!mods_need_sync && new_mods.oneshot_mods != split_shmem->mods.oneshot_mods) {
  346. mods_need_sync = true;
  347. }
  348. new_mods.oneshot_locked_mods = get_oneshot_locked_mods();
  349. if (!mods_need_sync && new_mods.oneshot_locked_mods != split_shmem->mods.oneshot_locked_mods) {
  350. mods_need_sync = true;
  351. }
  352. # endif // NO_ACTION_ONESHOT
  353. bool okay = true;
  354. if (mods_need_sync) {
  355. okay &= transport_write(PUT_MODS, &new_mods, sizeof(new_mods));
  356. if (okay) {
  357. last_update = timer_read32();
  358. }
  359. }
  360. return okay;
  361. }
  362. static void mods_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  363. split_shared_memory_lock();
  364. split_mods_sync_t mods;
  365. memcpy(&mods, &split_shmem->mods, sizeof(split_mods_sync_t));
  366. split_shared_memory_unlock();
  367. set_mods(mods.real_mods);
  368. set_weak_mods(mods.weak_mods);
  369. # ifndef NO_ACTION_ONESHOT
  370. set_oneshot_mods(mods.oneshot_mods);
  371. set_oneshot_locked_mods(mods.oneshot_locked_mods);
  372. # endif
  373. }
  374. # define TRANSACTIONS_MODS_MASTER() TRANSACTION_HANDLER_MASTER(mods)
  375. # define TRANSACTIONS_MODS_SLAVE() TRANSACTION_HANDLER_SLAVE(mods)
  376. # define TRANSACTIONS_MODS_REGISTRATIONS [PUT_MODS] = trans_initiator2target_initializer(mods),
  377. #else // SPLIT_MODS_ENABLE
  378. # define TRANSACTIONS_MODS_MASTER()
  379. # define TRANSACTIONS_MODS_SLAVE()
  380. # define TRANSACTIONS_MODS_REGISTRATIONS
  381. #endif // SPLIT_MODS_ENABLE
  382. ////////////////////////////////////////////////////
  383. // Backlight
  384. #ifdef BACKLIGHT_ENABLE
  385. static bool backlight_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  386. static uint32_t last_update = 0;
  387. uint8_t level = is_backlight_enabled() ? get_backlight_level() : 0;
  388. return send_if_condition(PUT_BACKLIGHT, &last_update, (level != split_shmem->backlight_level), &level, sizeof(level));
  389. }
  390. static void backlight_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  391. split_shared_memory_lock();
  392. uint8_t backlight_level = split_shmem->backlight_level;
  393. split_shared_memory_unlock();
  394. backlight_level_noeeprom(backlight_level);
  395. }
  396. # define TRANSACTIONS_BACKLIGHT_MASTER() TRANSACTION_HANDLER_MASTER(backlight)
  397. # define TRANSACTIONS_BACKLIGHT_SLAVE() TRANSACTION_HANDLER_SLAVE(backlight)
  398. # define TRANSACTIONS_BACKLIGHT_REGISTRATIONS [PUT_BACKLIGHT] = trans_initiator2target_initializer(backlight_level),
  399. #else // BACKLIGHT_ENABLE
  400. # define TRANSACTIONS_BACKLIGHT_MASTER()
  401. # define TRANSACTIONS_BACKLIGHT_SLAVE()
  402. # define TRANSACTIONS_BACKLIGHT_REGISTRATIONS
  403. #endif // BACKLIGHT_ENABLE
  404. ////////////////////////////////////////////////////
  405. // RGBLIGHT
  406. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  407. static bool rgblight_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  408. static uint32_t last_update = 0;
  409. rgblight_syncinfo_t rgblight_sync;
  410. rgblight_get_syncinfo(&rgblight_sync);
  411. if (send_if_condition(PUT_RGBLIGHT, &last_update, (rgblight_sync.status.change_flags != 0), &rgblight_sync, sizeof(rgblight_sync))) {
  412. rgblight_clear_change_flags();
  413. } else {
  414. return false;
  415. }
  416. return true;
  417. }
  418. static void rgblight_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  419. split_shared_memory_lock();
  420. // Update the RGB with the new data
  421. rgblight_syncinfo_t rgblight_sync;
  422. memcpy(&rgblight_sync, &split_shmem->rgblight_sync, sizeof(rgblight_syncinfo_t));
  423. split_shmem->rgblight_sync.status.change_flags = 0;
  424. split_shared_memory_unlock();
  425. if (rgblight_sync.status.change_flags != 0) {
  426. rgblight_update_sync(&rgblight_sync, false);
  427. }
  428. }
  429. # define TRANSACTIONS_RGBLIGHT_MASTER() TRANSACTION_HANDLER_MASTER(rgblight)
  430. # define TRANSACTIONS_RGBLIGHT_SLAVE() TRANSACTION_HANDLER_SLAVE(rgblight)
  431. # define TRANSACTIONS_RGBLIGHT_REGISTRATIONS [PUT_RGBLIGHT] = trans_initiator2target_initializer(rgblight_sync),
  432. #else // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  433. # define TRANSACTIONS_RGBLIGHT_MASTER()
  434. # define TRANSACTIONS_RGBLIGHT_SLAVE()
  435. # define TRANSACTIONS_RGBLIGHT_REGISTRATIONS
  436. #endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  437. ////////////////////////////////////////////////////
  438. // LED Matrix
  439. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  440. static bool led_matrix_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  441. static uint32_t last_update = 0;
  442. led_matrix_sync_t led_matrix_sync;
  443. memcpy(&led_matrix_sync.led_matrix, &led_matrix_eeconfig, sizeof(led_eeconfig_t));
  444. led_matrix_sync.led_suspend_state = led_matrix_get_suspend_state();
  445. return send_if_data_mismatch(PUT_LED_MATRIX, &last_update, &led_matrix_sync, &split_shmem->led_matrix_sync, sizeof(led_matrix_sync));
  446. }
  447. static void led_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  448. split_shared_memory_lock();
  449. memcpy(&led_matrix_eeconfig, &split_shmem->led_matrix_sync.led_matrix, sizeof(led_eeconfig_t));
  450. bool led_suspend_state = split_shmem->led_matrix_sync.led_suspend_state;
  451. split_shared_memory_unlock();
  452. led_matrix_set_suspend_state(led_suspend_state);
  453. }
  454. # define TRANSACTIONS_LED_MATRIX_MASTER() TRANSACTION_HANDLER_MASTER(led_matrix)
  455. # define TRANSACTIONS_LED_MATRIX_SLAVE() TRANSACTION_HANDLER_SLAVE(led_matrix)
  456. # define TRANSACTIONS_LED_MATRIX_REGISTRATIONS [PUT_LED_MATRIX] = trans_initiator2target_initializer(led_matrix_sync),
  457. #else // defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  458. # define TRANSACTIONS_LED_MATRIX_MASTER()
  459. # define TRANSACTIONS_LED_MATRIX_SLAVE()
  460. # define TRANSACTIONS_LED_MATRIX_REGISTRATIONS
  461. #endif // defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  462. ////////////////////////////////////////////////////
  463. // RGB Matrix
  464. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  465. static bool rgb_matrix_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  466. static uint32_t last_update = 0;
  467. rgb_matrix_sync_t rgb_matrix_sync;
  468. memcpy(&rgb_matrix_sync.rgb_matrix, &rgb_matrix_config, sizeof(rgb_config_t));
  469. rgb_matrix_sync.rgb_suspend_state = rgb_matrix_get_suspend_state();
  470. return send_if_data_mismatch(PUT_RGB_MATRIX, &last_update, &rgb_matrix_sync, &split_shmem->rgb_matrix_sync, sizeof(rgb_matrix_sync));
  471. }
  472. static void rgb_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  473. split_shared_memory_lock();
  474. memcpy(&rgb_matrix_config, &split_shmem->rgb_matrix_sync.rgb_matrix, sizeof(rgb_config_t));
  475. bool rgb_suspend_state = split_shmem->rgb_matrix_sync.rgb_suspend_state;
  476. split_shared_memory_unlock();
  477. rgb_matrix_set_suspend_state(rgb_suspend_state);
  478. }
  479. # define TRANSACTIONS_RGB_MATRIX_MASTER() TRANSACTION_HANDLER_MASTER(rgb_matrix)
  480. # define TRANSACTIONS_RGB_MATRIX_SLAVE() TRANSACTION_HANDLER_SLAVE(rgb_matrix)
  481. # define TRANSACTIONS_RGB_MATRIX_REGISTRATIONS [PUT_RGB_MATRIX] = trans_initiator2target_initializer(rgb_matrix_sync),
  482. #else // defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  483. # define TRANSACTIONS_RGB_MATRIX_MASTER()
  484. # define TRANSACTIONS_RGB_MATRIX_SLAVE()
  485. # define TRANSACTIONS_RGB_MATRIX_REGISTRATIONS
  486. #endif // defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  487. ////////////////////////////////////////////////////
  488. // WPM
  489. #if defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)
  490. static bool wpm_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  491. static uint32_t last_update = 0;
  492. uint8_t current_wpm = get_current_wpm();
  493. return send_if_condition(PUT_WPM, &last_update, (current_wpm != split_shmem->current_wpm), &current_wpm, sizeof(current_wpm));
  494. }
  495. static void wpm_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  496. set_current_wpm(split_shmem->current_wpm);
  497. }
  498. # define TRANSACTIONS_WPM_MASTER() TRANSACTION_HANDLER_MASTER(wpm)
  499. # define TRANSACTIONS_WPM_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(wpm)
  500. # define TRANSACTIONS_WPM_REGISTRATIONS [PUT_WPM] = trans_initiator2target_initializer(current_wpm),
  501. #else // defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)
  502. # define TRANSACTIONS_WPM_MASTER()
  503. # define TRANSACTIONS_WPM_SLAVE()
  504. # define TRANSACTIONS_WPM_REGISTRATIONS
  505. #endif // defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)
  506. ////////////////////////////////////////////////////
  507. // OLED
  508. #if defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
  509. static bool oled_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  510. static uint32_t last_update = 0;
  511. bool current_oled_state = is_oled_on();
  512. return send_if_condition(PUT_OLED, &last_update, (current_oled_state != split_shmem->current_oled_state), &current_oled_state, sizeof(current_oled_state));
  513. }
  514. static void oled_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  515. split_shared_memory_lock();
  516. uint8_t current_oled_state = split_shmem->current_oled_state;
  517. split_shared_memory_unlock();
  518. if (current_oled_state) {
  519. oled_on();
  520. } else {
  521. oled_off();
  522. }
  523. }
  524. # define TRANSACTIONS_OLED_MASTER() TRANSACTION_HANDLER_MASTER(oled)
  525. # define TRANSACTIONS_OLED_SLAVE() TRANSACTION_HANDLER_SLAVE(oled)
  526. # define TRANSACTIONS_OLED_REGISTRATIONS [PUT_OLED] = trans_initiator2target_initializer(current_oled_state),
  527. #else // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
  528. # define TRANSACTIONS_OLED_MASTER()
  529. # define TRANSACTIONS_OLED_SLAVE()
  530. # define TRANSACTIONS_OLED_REGISTRATIONS
  531. #endif // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
  532. ////////////////////////////////////////////////////
  533. // ST7565
  534. #if defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
  535. static bool st7565_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  536. static uint32_t last_update = 0;
  537. bool current_st7565_state = st7565_is_on();
  538. return send_if_condition(PUT_ST7565, &last_update, (current_st7565_state != split_shmem->current_st7565_state), &current_st7565_state, sizeof(current_st7565_state));
  539. }
  540. static void st7565_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  541. split_shared_memory_lock();
  542. uint8_t current_st7565_state = split_shmem->current_st7565_state;
  543. split_shared_memory_unlock();
  544. if (current_st7565_state) {
  545. st7565_on();
  546. } else {
  547. st7565_off();
  548. }
  549. }
  550. # define TRANSACTIONS_ST7565_MASTER() TRANSACTION_HANDLER_MASTER(st7565)
  551. # define TRANSACTIONS_ST7565_SLAVE() TRANSACTION_HANDLER_SLAVE(st7565)
  552. # define TRANSACTIONS_ST7565_REGISTRATIONS [PUT_ST7565] = trans_initiator2target_initializer(current_st7565_state),
  553. #else // defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
  554. # define TRANSACTIONS_ST7565_MASTER()
  555. # define TRANSACTIONS_ST7565_SLAVE()
  556. # define TRANSACTIONS_ST7565_REGISTRATIONS
  557. #endif // defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
  558. ////////////////////////////////////////////////////
  559. // POINTING
  560. #if defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  561. static bool pointing_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  562. # if defined(POINTING_DEVICE_LEFT)
  563. if (is_keyboard_left()) {
  564. return true;
  565. }
  566. # elif defined(POINTING_DEVICE_RIGHT)
  567. if (!is_keyboard_left()) {
  568. return true;
  569. }
  570. # endif
  571. static uint32_t last_update = 0;
  572. static uint32_t last_cpi_update = 0;
  573. static uint16_t last_cpi = 0;
  574. report_mouse_t temp_state;
  575. uint16_t temp_cpi;
  576. bool okay = read_if_checksum_mismatch(GET_POINTING_CHECKSUM, GET_POINTING_DATA, &last_update, &temp_state, &split_shmem->pointing.report, sizeof(temp_state));
  577. if (okay) pointing_device_set_shared_report(temp_state);
  578. temp_cpi = pointing_device_get_shared_cpi();
  579. if (temp_cpi) {
  580. split_shmem->pointing.cpi = temp_cpi;
  581. okay = send_if_condition(PUT_POINTING_CPI, &last_cpi_update, last_cpi != temp_cpi, &split_shmem->pointing.cpi, sizeof(split_shmem->pointing.cpi));
  582. if (okay) {
  583. last_cpi = temp_cpi;
  584. }
  585. }
  586. return okay;
  587. }
  588. extern const pointing_device_driver_t *pointing_device_driver;
  589. static void pointing_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  590. # if defined(POINTING_DEVICE_LEFT)
  591. if (!is_keyboard_left()) {
  592. return;
  593. }
  594. # elif defined(POINTING_DEVICE_RIGHT)
  595. if (is_keyboard_left()) {
  596. return;
  597. }
  598. # endif
  599. # if (POINTING_DEVICE_TASK_THROTTLE_MS > 0)
  600. static uint32_t last_exec = 0;
  601. if (timer_elapsed32(last_exec) < POINTING_DEVICE_TASK_THROTTLE_MS) {
  602. return;
  603. }
  604. last_exec = timer_read32();
  605. # endif
  606. uint16_t temp_cpi = !pointing_device_driver->get_cpi ? 0 : pointing_device_driver->get_cpi(); // check for NULL
  607. split_shared_memory_lock();
  608. split_slave_pointing_sync_t pointing;
  609. memcpy(&pointing, &split_shmem->pointing, sizeof(split_slave_pointing_sync_t));
  610. split_shared_memory_unlock();
  611. if (pointing.cpi && pointing.cpi != temp_cpi && pointing_device_driver->set_cpi) {
  612. pointing_device_driver->set_cpi(pointing.cpi);
  613. }
  614. pointing.report = pointing_device_driver->get_report((report_mouse_t){0});
  615. // Now update the checksum given that the pointing has been written to
  616. pointing.checksum = crc8(&pointing.report, sizeof(report_mouse_t));
  617. split_shared_memory_lock();
  618. memcpy(&split_shmem->pointing, &pointing, sizeof(split_slave_pointing_sync_t));
  619. split_shared_memory_unlock();
  620. }
  621. # define TRANSACTIONS_POINTING_MASTER() TRANSACTION_HANDLER_MASTER(pointing)
  622. # define TRANSACTIONS_POINTING_SLAVE() TRANSACTION_HANDLER_SLAVE(pointing)
  623. # define TRANSACTIONS_POINTING_REGISTRATIONS [GET_POINTING_CHECKSUM] = trans_target2initiator_initializer(pointing.checksum), [GET_POINTING_DATA] = trans_target2initiator_initializer(pointing.report), [PUT_POINTING_CPI] = trans_initiator2target_initializer(pointing.cpi),
  624. #else // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  625. # define TRANSACTIONS_POINTING_MASTER()
  626. # define TRANSACTIONS_POINTING_SLAVE()
  627. # define TRANSACTIONS_POINTING_REGISTRATIONS
  628. #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  629. ////////////////////////////////////////////////////
  630. // WATCHDOG
  631. #if defined(SPLIT_WATCHDOG_ENABLE)
  632. static bool watchdog_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  633. bool okay = true;
  634. if (!split_watchdog_check()) {
  635. okay = transport_write(PUT_WATCHDOG, &okay, sizeof(okay));
  636. split_watchdog_update(okay);
  637. }
  638. return okay;
  639. }
  640. static void watchdog_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  641. split_watchdog_update(split_shmem->watchdog_pinged);
  642. }
  643. # define TRANSACTIONS_WATCHDOG_MASTER() TRANSACTION_HANDLER_MASTER(watchdog)
  644. # define TRANSACTIONS_WATCHDOG_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(watchdog)
  645. # define TRANSACTIONS_WATCHDOG_REGISTRATIONS [PUT_WATCHDOG] = trans_initiator2target_initializer(watchdog_pinged),
  646. #else // defined(SPLIT_WATCHDOG_ENABLE)
  647. # define TRANSACTIONS_WATCHDOG_MASTER()
  648. # define TRANSACTIONS_WATCHDOG_SLAVE()
  649. # define TRANSACTIONS_WATCHDOG_REGISTRATIONS
  650. #endif // defined(SPLIT_WATCHDOG_ENABLE)
  651. #if defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  652. uint8_t split_haptic_play = 0xFF;
  653. extern haptic_config_t haptic_config;
  654. static bool haptic_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  655. static uint32_t last_update = 0;
  656. split_slave_haptic_sync_t haptic_sync;
  657. memcpy(&haptic_sync.haptic_config, &haptic_config, sizeof(haptic_config_t));
  658. haptic_sync.haptic_play = split_haptic_play;
  659. bool okay = send_if_data_mismatch(PUT_HAPTIC, &last_update, &haptic_sync, &split_shmem->haptic_sync, sizeof(haptic_sync));
  660. split_haptic_play = 0xFF;
  661. return okay;
  662. }
  663. static void haptic_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  664. memcpy(&haptic_config, &split_shmem->haptic_sync.haptic_config, sizeof(haptic_config_t));
  665. if (split_shmem->haptic_sync.haptic_play != 0xFF) {
  666. haptic_set_mode(split_shmem->haptic_sync.haptic_play);
  667. haptic_play();
  668. }
  669. }
  670. // clang-format off
  671. # define TRANSACTIONS_HAPTIC_MASTER() TRANSACTION_HANDLER_MASTER(haptic)
  672. # define TRANSACTIONS_HAPTIC_SLAVE() TRANSACTION_HANDLER_SLAVE(haptic)
  673. # define TRANSACTIONS_HAPTIC_REGISTRATIONS [PUT_HAPTIC] = trans_initiator2target_initializer(haptic_sync),
  674. // clang-format on
  675. #else // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  676. # define TRANSACTIONS_HAPTIC_MASTER()
  677. # define TRANSACTIONS_HAPTIC_SLAVE()
  678. # define TRANSACTIONS_HAPTIC_REGISTRATIONS
  679. #endif // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  680. #if defined(SPLIT_ACTIVITY_ENABLE)
  681. static bool activity_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  682. static uint32_t last_update = 0;
  683. split_slave_activity_sync_t activity_sync;
  684. activity_sync.matrix_timestamp = last_matrix_activity_time();
  685. activity_sync.encoder_timestamp = last_encoder_activity_time();
  686. activity_sync.pointing_device_timestamp = last_pointing_device_activity_time();
  687. return send_if_data_mismatch(PUT_ACTIVITY, &last_update, &activity_sync, &split_shmem->activity_sync, sizeof(activity_sync));
  688. }
  689. static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  690. set_activity_timestamps(split_shmem->activity_sync.matrix_timestamp, split_shmem->activity_sync.encoder_timestamp, split_shmem->activity_sync.pointing_device_timestamp);
  691. }
  692. // clang-format off
  693. # define TRANSACTIONS_ACTIVITY_MASTER() TRANSACTION_HANDLER_MASTER(activity)
  694. # define TRANSACTIONS_ACTIVITY_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(activity)
  695. # define TRANSACTIONS_ACTIVITY_REGISTRATIONS [PUT_ACTIVITY] = trans_initiator2target_initializer(activity_sync),
  696. // clang-format on
  697. #else // defined(SPLIT_ACTIVITY_ENABLE)
  698. # define TRANSACTIONS_ACTIVITY_MASTER()
  699. # define TRANSACTIONS_ACTIVITY_SLAVE()
  700. # define TRANSACTIONS_ACTIVITY_REGISTRATIONS
  701. #endif // defined(SPLIT_ACTIVITY_ENABLE)
  702. ////////////////////////////////////////////////////
  703. // Detected OS
  704. #if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  705. static bool detected_os_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  706. static uint32_t last_detected_os_update = 0;
  707. os_variant_t detected_os = detected_host_os();
  708. bool okay = send_if_condition(PUT_DETECTED_OS, &last_detected_os_update, (detected_os != split_shmem->detected_os), &detected_os, sizeof(os_variant_t));
  709. return okay;
  710. }
  711. static void detected_os_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  712. slave_update_detected_host_os(split_shmem->detected_os);
  713. }
  714. # define TRANSACTIONS_DETECTED_OS_MASTER() TRANSACTION_HANDLER_MASTER(detected_os)
  715. # define TRANSACTIONS_DETECTED_OS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(detected_os)
  716. # define TRANSACTIONS_DETECTED_OS_REGISTRATIONS [PUT_DETECTED_OS] = trans_initiator2target_initializer(detected_os),
  717. #else // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  718. # define TRANSACTIONS_DETECTED_OS_MASTER()
  719. # define TRANSACTIONS_DETECTED_OS_SLAVE()
  720. # define TRANSACTIONS_DETECTED_OS_REGISTRATIONS
  721. #endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  722. ////////////////////////////////////////////////////
  723. split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
  724. // Set defaults
  725. [0 ...(NUM_TOTAL_TRANSACTIONS - 1)] = {0, 0, 0, 0, 0},
  726. #ifdef USE_I2C
  727. [I2C_EXECUTE_CALLBACK] = trans_initiator2target_initializer(transaction_id),
  728. #endif // USE_I2C
  729. // clang-format off
  730. TRANSACTIONS_SLAVE_MATRIX_REGISTRATIONS
  731. TRANSACTIONS_MASTER_MATRIX_REGISTRATIONS
  732. TRANSACTIONS_ENCODERS_REGISTRATIONS
  733. TRANSACTIONS_SYNC_TIMER_REGISTRATIONS
  734. TRANSACTIONS_LAYER_STATE_REGISTRATIONS
  735. TRANSACTIONS_LED_STATE_REGISTRATIONS
  736. TRANSACTIONS_MODS_REGISTRATIONS
  737. TRANSACTIONS_BACKLIGHT_REGISTRATIONS
  738. TRANSACTIONS_RGBLIGHT_REGISTRATIONS
  739. TRANSACTIONS_LED_MATRIX_REGISTRATIONS
  740. TRANSACTIONS_RGB_MATRIX_REGISTRATIONS
  741. TRANSACTIONS_WPM_REGISTRATIONS
  742. TRANSACTIONS_OLED_REGISTRATIONS
  743. TRANSACTIONS_ST7565_REGISTRATIONS
  744. TRANSACTIONS_POINTING_REGISTRATIONS
  745. TRANSACTIONS_WATCHDOG_REGISTRATIONS
  746. TRANSACTIONS_HAPTIC_REGISTRATIONS
  747. TRANSACTIONS_ACTIVITY_REGISTRATIONS
  748. TRANSACTIONS_DETECTED_OS_REGISTRATIONS
  749. // clang-format on
  750. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  751. [PUT_RPC_INFO] = trans_initiator2target_initializer_cb(rpc_info, slave_rpc_info_callback),
  752. [PUT_RPC_REQ_DATA] = trans_initiator2target_initializer(rpc_m2s_buffer),
  753. [EXECUTE_RPC] = trans_initiator2target_initializer_cb(rpc_info.payload.transaction_id, slave_rpc_exec_callback),
  754. [GET_RPC_RESP_DATA] = trans_target2initiator_initializer(rpc_s2m_buffer),
  755. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  756. };
  757. bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  758. TRANSACTIONS_SLAVE_MATRIX_MASTER();
  759. TRANSACTIONS_MASTER_MATRIX_MASTER();
  760. TRANSACTIONS_ENCODERS_MASTER();
  761. TRANSACTIONS_SYNC_TIMER_MASTER();
  762. TRANSACTIONS_LAYER_STATE_MASTER();
  763. TRANSACTIONS_LED_STATE_MASTER();
  764. TRANSACTIONS_MODS_MASTER();
  765. TRANSACTIONS_BACKLIGHT_MASTER();
  766. TRANSACTIONS_RGBLIGHT_MASTER();
  767. TRANSACTIONS_LED_MATRIX_MASTER();
  768. TRANSACTIONS_RGB_MATRIX_MASTER();
  769. TRANSACTIONS_WPM_MASTER();
  770. TRANSACTIONS_OLED_MASTER();
  771. TRANSACTIONS_ST7565_MASTER();
  772. TRANSACTIONS_POINTING_MASTER();
  773. TRANSACTIONS_WATCHDOG_MASTER();
  774. TRANSACTIONS_HAPTIC_MASTER();
  775. TRANSACTIONS_ACTIVITY_MASTER();
  776. TRANSACTIONS_DETECTED_OS_MASTER();
  777. return true;
  778. }
  779. void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  780. TRANSACTIONS_SLAVE_MATRIX_SLAVE();
  781. TRANSACTIONS_MASTER_MATRIX_SLAVE();
  782. TRANSACTIONS_ENCODERS_SLAVE();
  783. TRANSACTIONS_SYNC_TIMER_SLAVE();
  784. TRANSACTIONS_LAYER_STATE_SLAVE();
  785. TRANSACTIONS_LED_STATE_SLAVE();
  786. TRANSACTIONS_MODS_SLAVE();
  787. TRANSACTIONS_BACKLIGHT_SLAVE();
  788. TRANSACTIONS_RGBLIGHT_SLAVE();
  789. TRANSACTIONS_LED_MATRIX_SLAVE();
  790. TRANSACTIONS_RGB_MATRIX_SLAVE();
  791. TRANSACTIONS_WPM_SLAVE();
  792. TRANSACTIONS_OLED_SLAVE();
  793. TRANSACTIONS_ST7565_SLAVE();
  794. TRANSACTIONS_POINTING_SLAVE();
  795. TRANSACTIONS_WATCHDOG_SLAVE();
  796. TRANSACTIONS_HAPTIC_SLAVE();
  797. TRANSACTIONS_ACTIVITY_SLAVE();
  798. TRANSACTIONS_DETECTED_OS_SLAVE();
  799. }
  800. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  801. void transaction_register_rpc(int8_t transaction_id, slave_callback_t callback) {
  802. // Prevent invoking RPC on QMK core sync data
  803. if (transaction_id <= GET_RPC_RESP_DATA) return;
  804. // Set the callback
  805. split_transaction_table[transaction_id].slave_callback = callback;
  806. split_transaction_table[transaction_id].initiator2target_offset = offsetof(split_shared_memory_t, rpc_m2s_buffer);
  807. split_transaction_table[transaction_id].target2initiator_offset = offsetof(split_shared_memory_t, rpc_s2m_buffer);
  808. }
  809. bool transaction_rpc_exec(int8_t transaction_id, uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  810. // Prevent transaction attempts while transport is disconnected
  811. if (!is_transport_connected()) {
  812. return false;
  813. }
  814. // Prevent invoking RPC on QMK core sync data
  815. if (transaction_id <= GET_RPC_RESP_DATA) return false;
  816. // Prevent sizing issues
  817. if (initiator2target_buffer_size > RPC_M2S_BUFFER_SIZE) return false;
  818. if (target2initiator_buffer_size > RPC_S2M_BUFFER_SIZE) return false;
  819. // Prepare the metadata block
  820. rpc_sync_info_t info = {.payload = {.transaction_id = transaction_id, .m2s_length = initiator2target_buffer_size, .s2m_length = target2initiator_buffer_size}};
  821. info.checksum = crc8(&info.payload, sizeof(info.payload));
  822. // Make sure the local side knows that we're not sending the full block of data
  823. split_transaction_table[PUT_RPC_REQ_DATA].initiator2target_buffer_size = initiator2target_buffer_size;
  824. split_transaction_table[GET_RPC_RESP_DATA].target2initiator_buffer_size = target2initiator_buffer_size;
  825. // Run through the sequence:
  826. // * set the transaction ID and lengths
  827. // * send the request data
  828. // * execute RPC callback
  829. // * retrieve the response data
  830. if (!transport_write(PUT_RPC_INFO, &info, sizeof(info))) {
  831. return false;
  832. }
  833. if (!transport_write(PUT_RPC_REQ_DATA, initiator2target_buffer, initiator2target_buffer_size)) {
  834. return false;
  835. }
  836. if (!transport_write(EXECUTE_RPC, &transaction_id, sizeof(transaction_id))) {
  837. return false;
  838. }
  839. if (!transport_read(GET_RPC_RESP_DATA, target2initiator_buffer, target2initiator_buffer_size)) {
  840. return false;
  841. }
  842. return true;
  843. }
  844. void slave_rpc_info_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  845. // The RPC info block contains the intended transaction ID, as well as the sizes for both inbound and outbound data.
  846. // Ignore the args -- the `split_shmem` already has the info, we just need to act upon it.
  847. // We must keep the `split_transaction_table` non-const, so that it is able to be modified at runtime.
  848. split_transaction_table[PUT_RPC_REQ_DATA].initiator2target_buffer_size = split_shmem->rpc_info.payload.m2s_length;
  849. split_transaction_table[GET_RPC_RESP_DATA].target2initiator_buffer_size = split_shmem->rpc_info.payload.s2m_length;
  850. }
  851. void slave_rpc_exec_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  852. // We can assume that the buffer lengths are correctly set, now, given that sequentially the rpc_info callback was already executed.
  853. // Go through the rpc_info and execute _that_ transaction's callback, with the scratch buffers as inputs.
  854. // As a safety precaution we check that the received payload matches its checksum first.
  855. if (crc8(&split_shmem->rpc_info.payload, sizeof(split_shmem->rpc_info.payload)) != split_shmem->rpc_info.checksum) {
  856. return;
  857. }
  858. int8_t transaction_id = split_shmem->rpc_info.payload.transaction_id;
  859. if (transaction_id < NUM_TOTAL_TRANSACTIONS) {
  860. split_transaction_desc_t *trans = &split_transaction_table[transaction_id];
  861. if (trans->slave_callback) {
  862. trans->slave_callback(split_shmem->rpc_info.payload.m2s_length, split_shmem->rpc_m2s_buffer, split_shmem->rpc_info.payload.s2m_length, split_shmem->rpc_s2m_buffer);
  863. }
  864. }
  865. }
  866. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)