transactions.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 uint16_t last_cpi = 0;
  573. report_mouse_t temp_state;
  574. uint16_t temp_cpi;
  575. bool okay = read_if_checksum_mismatch(GET_POINTING_CHECKSUM, GET_POINTING_DATA, &last_update, &temp_state, &split_shmem->pointing.report, sizeof(temp_state));
  576. if (okay) pointing_device_set_shared_report(temp_state);
  577. temp_cpi = pointing_device_get_shared_cpi();
  578. if (temp_cpi && last_cpi != temp_cpi) {
  579. split_shmem->pointing.cpi = temp_cpi;
  580. okay = transport_write(PUT_POINTING_CPI, &split_shmem->pointing.cpi, sizeof(split_shmem->pointing.cpi));
  581. if (okay) {
  582. last_cpi = temp_cpi;
  583. }
  584. }
  585. return okay;
  586. }
  587. extern const pointing_device_driver_t pointing_device_driver;
  588. static void pointing_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  589. # if defined(POINTING_DEVICE_LEFT)
  590. if (!is_keyboard_left()) {
  591. return;
  592. }
  593. # elif defined(POINTING_DEVICE_RIGHT)
  594. if (is_keyboard_left()) {
  595. return;
  596. }
  597. # endif
  598. # if (POINTING_DEVICE_TASK_THROTTLE_MS > 0)
  599. static uint32_t last_exec = 0;
  600. if (timer_elapsed32(last_exec) < POINTING_DEVICE_TASK_THROTTLE_MS) {
  601. return;
  602. }
  603. last_exec = timer_read32();
  604. # endif
  605. uint16_t temp_cpi = !pointing_device_driver.get_cpi ? 0 : pointing_device_driver.get_cpi(); // check for NULL
  606. split_shared_memory_lock();
  607. split_slave_pointing_sync_t pointing;
  608. memcpy(&pointing, &split_shmem->pointing, sizeof(split_slave_pointing_sync_t));
  609. split_shared_memory_unlock();
  610. if (pointing.cpi && pointing.cpi != temp_cpi && pointing_device_driver.set_cpi) {
  611. pointing_device_driver.set_cpi(pointing.cpi);
  612. }
  613. pointing.report = pointing_device_driver.get_report((report_mouse_t){0});
  614. // Now update the checksum given that the pointing has been written to
  615. pointing.checksum = crc8(&pointing.report, sizeof(report_mouse_t));
  616. split_shared_memory_lock();
  617. memcpy(&split_shmem->pointing, &pointing, sizeof(split_slave_pointing_sync_t));
  618. split_shared_memory_unlock();
  619. }
  620. # define TRANSACTIONS_POINTING_MASTER() TRANSACTION_HANDLER_MASTER(pointing)
  621. # define TRANSACTIONS_POINTING_SLAVE() TRANSACTION_HANDLER_SLAVE(pointing)
  622. # 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),
  623. #else // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  624. # define TRANSACTIONS_POINTING_MASTER()
  625. # define TRANSACTIONS_POINTING_SLAVE()
  626. # define TRANSACTIONS_POINTING_REGISTRATIONS
  627. #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  628. ////////////////////////////////////////////////////
  629. // WATCHDOG
  630. #if defined(SPLIT_WATCHDOG_ENABLE)
  631. static bool watchdog_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  632. bool okay = true;
  633. if (!split_watchdog_check()) {
  634. okay = transport_write(PUT_WATCHDOG, &okay, sizeof(okay));
  635. split_watchdog_update(okay);
  636. }
  637. return okay;
  638. }
  639. static void watchdog_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  640. split_watchdog_update(split_shmem->watchdog_pinged);
  641. }
  642. # define TRANSACTIONS_WATCHDOG_MASTER() TRANSACTION_HANDLER_MASTER(watchdog)
  643. # define TRANSACTIONS_WATCHDOG_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(watchdog)
  644. # define TRANSACTIONS_WATCHDOG_REGISTRATIONS [PUT_WATCHDOG] = trans_initiator2target_initializer(watchdog_pinged),
  645. #else // defined(SPLIT_WATCHDOG_ENABLE)
  646. # define TRANSACTIONS_WATCHDOG_MASTER()
  647. # define TRANSACTIONS_WATCHDOG_SLAVE()
  648. # define TRANSACTIONS_WATCHDOG_REGISTRATIONS
  649. #endif // defined(SPLIT_WATCHDOG_ENABLE)
  650. #if defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  651. uint8_t split_haptic_play = 0xFF;
  652. extern haptic_config_t haptic_config;
  653. static bool haptic_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  654. static uint32_t last_update = 0;
  655. split_slave_haptic_sync_t haptic_sync;
  656. memcpy(&haptic_sync.haptic_config, &haptic_config, sizeof(haptic_config_t));
  657. haptic_sync.haptic_play = split_haptic_play;
  658. bool okay = send_if_data_mismatch(PUT_HAPTIC, &last_update, &haptic_sync, &split_shmem->haptic_sync, sizeof(haptic_sync));
  659. split_haptic_play = 0xFF;
  660. return okay;
  661. }
  662. static void haptic_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  663. memcpy(&haptic_config, &split_shmem->haptic_sync.haptic_config, sizeof(haptic_config_t));
  664. if (split_shmem->haptic_sync.haptic_play != 0xFF) {
  665. haptic_set_mode(split_shmem->haptic_sync.haptic_play);
  666. haptic_play();
  667. }
  668. }
  669. // clang-format off
  670. # define TRANSACTIONS_HAPTIC_MASTER() TRANSACTION_HANDLER_MASTER(haptic)
  671. # define TRANSACTIONS_HAPTIC_SLAVE() TRANSACTION_HANDLER_SLAVE(haptic)
  672. # define TRANSACTIONS_HAPTIC_REGISTRATIONS [PUT_HAPTIC] = trans_initiator2target_initializer(haptic_sync),
  673. // clang-format on
  674. #else // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  675. # define TRANSACTIONS_HAPTIC_MASTER()
  676. # define TRANSACTIONS_HAPTIC_SLAVE()
  677. # define TRANSACTIONS_HAPTIC_REGISTRATIONS
  678. #endif // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  679. #if defined(SPLIT_ACTIVITY_ENABLE)
  680. static bool activity_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  681. static uint32_t last_update = 0;
  682. split_slave_activity_sync_t activity_sync;
  683. activity_sync.matrix_timestamp = last_matrix_activity_time();
  684. activity_sync.encoder_timestamp = last_encoder_activity_time();
  685. activity_sync.pointing_device_timestamp = last_pointing_device_activity_time();
  686. return send_if_data_mismatch(PUT_ACTIVITY, &last_update, &activity_sync, &split_shmem->activity_sync, sizeof(activity_sync));
  687. }
  688. static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  689. set_activity_timestamps(split_shmem->activity_sync.matrix_timestamp, split_shmem->activity_sync.encoder_timestamp, split_shmem->activity_sync.pointing_device_timestamp);
  690. }
  691. // clang-format off
  692. # define TRANSACTIONS_ACTIVITY_MASTER() TRANSACTION_HANDLER_MASTER(activity)
  693. # define TRANSACTIONS_ACTIVITY_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(activity)
  694. # define TRANSACTIONS_ACTIVITY_REGISTRATIONS [PUT_ACTIVITY] = trans_initiator2target_initializer(activity_sync),
  695. // clang-format on
  696. #else // defined(SPLIT_ACTIVITY_ENABLE)
  697. # define TRANSACTIONS_ACTIVITY_MASTER()
  698. # define TRANSACTIONS_ACTIVITY_SLAVE()
  699. # define TRANSACTIONS_ACTIVITY_REGISTRATIONS
  700. #endif // defined(SPLIT_ACTIVITY_ENABLE)
  701. ////////////////////////////////////////////////////
  702. // Detected OS
  703. #if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  704. static bool detected_os_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  705. static uint32_t last_detected_os_update = 0;
  706. os_variant_t detected_os = detected_host_os();
  707. bool okay = send_if_condition(PUT_DETECTED_OS, &last_detected_os_update, (detected_os != split_shmem->detected_os), &detected_os, sizeof(os_variant_t));
  708. return okay;
  709. }
  710. static void detected_os_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  711. slave_update_detected_host_os(split_shmem->detected_os);
  712. }
  713. # define TRANSACTIONS_DETECTED_OS_MASTER() TRANSACTION_HANDLER_MASTER(detected_os)
  714. # define TRANSACTIONS_DETECTED_OS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(detected_os)
  715. # define TRANSACTIONS_DETECTED_OS_REGISTRATIONS [PUT_DETECTED_OS] = trans_initiator2target_initializer(detected_os),
  716. #else // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  717. # define TRANSACTIONS_DETECTED_OS_MASTER()
  718. # define TRANSACTIONS_DETECTED_OS_SLAVE()
  719. # define TRANSACTIONS_DETECTED_OS_REGISTRATIONS
  720. #endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  721. ////////////////////////////////////////////////////
  722. split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
  723. // Set defaults
  724. [0 ...(NUM_TOTAL_TRANSACTIONS - 1)] = {0, 0, 0, 0, 0},
  725. #ifdef USE_I2C
  726. [I2C_EXECUTE_CALLBACK] = trans_initiator2target_initializer(transaction_id),
  727. #endif // USE_I2C
  728. // clang-format off
  729. TRANSACTIONS_SLAVE_MATRIX_REGISTRATIONS
  730. TRANSACTIONS_MASTER_MATRIX_REGISTRATIONS
  731. TRANSACTIONS_ENCODERS_REGISTRATIONS
  732. TRANSACTIONS_SYNC_TIMER_REGISTRATIONS
  733. TRANSACTIONS_LAYER_STATE_REGISTRATIONS
  734. TRANSACTIONS_LED_STATE_REGISTRATIONS
  735. TRANSACTIONS_MODS_REGISTRATIONS
  736. TRANSACTIONS_BACKLIGHT_REGISTRATIONS
  737. TRANSACTIONS_RGBLIGHT_REGISTRATIONS
  738. TRANSACTIONS_LED_MATRIX_REGISTRATIONS
  739. TRANSACTIONS_RGB_MATRIX_REGISTRATIONS
  740. TRANSACTIONS_WPM_REGISTRATIONS
  741. TRANSACTIONS_OLED_REGISTRATIONS
  742. TRANSACTIONS_ST7565_REGISTRATIONS
  743. TRANSACTIONS_POINTING_REGISTRATIONS
  744. TRANSACTIONS_WATCHDOG_REGISTRATIONS
  745. TRANSACTIONS_HAPTIC_REGISTRATIONS
  746. TRANSACTIONS_ACTIVITY_REGISTRATIONS
  747. TRANSACTIONS_DETECTED_OS_REGISTRATIONS
  748. // clang-format on
  749. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  750. [PUT_RPC_INFO] = trans_initiator2target_initializer_cb(rpc_info, slave_rpc_info_callback),
  751. [PUT_RPC_REQ_DATA] = trans_initiator2target_initializer(rpc_m2s_buffer),
  752. [EXECUTE_RPC] = trans_initiator2target_initializer_cb(rpc_info.payload.transaction_id, slave_rpc_exec_callback),
  753. [GET_RPC_RESP_DATA] = trans_target2initiator_initializer(rpc_s2m_buffer),
  754. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  755. };
  756. bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  757. TRANSACTIONS_SLAVE_MATRIX_MASTER();
  758. TRANSACTIONS_MASTER_MATRIX_MASTER();
  759. TRANSACTIONS_ENCODERS_MASTER();
  760. TRANSACTIONS_SYNC_TIMER_MASTER();
  761. TRANSACTIONS_LAYER_STATE_MASTER();
  762. TRANSACTIONS_LED_STATE_MASTER();
  763. TRANSACTIONS_MODS_MASTER();
  764. TRANSACTIONS_BACKLIGHT_MASTER();
  765. TRANSACTIONS_RGBLIGHT_MASTER();
  766. TRANSACTIONS_LED_MATRIX_MASTER();
  767. TRANSACTIONS_RGB_MATRIX_MASTER();
  768. TRANSACTIONS_WPM_MASTER();
  769. TRANSACTIONS_OLED_MASTER();
  770. TRANSACTIONS_ST7565_MASTER();
  771. TRANSACTIONS_POINTING_MASTER();
  772. TRANSACTIONS_WATCHDOG_MASTER();
  773. TRANSACTIONS_HAPTIC_MASTER();
  774. TRANSACTIONS_ACTIVITY_MASTER();
  775. TRANSACTIONS_DETECTED_OS_MASTER();
  776. return true;
  777. }
  778. void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  779. TRANSACTIONS_SLAVE_MATRIX_SLAVE();
  780. TRANSACTIONS_MASTER_MATRIX_SLAVE();
  781. TRANSACTIONS_ENCODERS_SLAVE();
  782. TRANSACTIONS_SYNC_TIMER_SLAVE();
  783. TRANSACTIONS_LAYER_STATE_SLAVE();
  784. TRANSACTIONS_LED_STATE_SLAVE();
  785. TRANSACTIONS_MODS_SLAVE();
  786. TRANSACTIONS_BACKLIGHT_SLAVE();
  787. TRANSACTIONS_RGBLIGHT_SLAVE();
  788. TRANSACTIONS_LED_MATRIX_SLAVE();
  789. TRANSACTIONS_RGB_MATRIX_SLAVE();
  790. TRANSACTIONS_WPM_SLAVE();
  791. TRANSACTIONS_OLED_SLAVE();
  792. TRANSACTIONS_ST7565_SLAVE();
  793. TRANSACTIONS_POINTING_SLAVE();
  794. TRANSACTIONS_WATCHDOG_SLAVE();
  795. TRANSACTIONS_HAPTIC_SLAVE();
  796. TRANSACTIONS_ACTIVITY_SLAVE();
  797. TRANSACTIONS_DETECTED_OS_SLAVE();
  798. }
  799. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  800. void transaction_register_rpc(int8_t transaction_id, slave_callback_t callback) {
  801. // Prevent invoking RPC on QMK core sync data
  802. if (transaction_id <= GET_RPC_RESP_DATA) return;
  803. // Set the callback
  804. split_transaction_table[transaction_id].slave_callback = callback;
  805. split_transaction_table[transaction_id].initiator2target_offset = offsetof(split_shared_memory_t, rpc_m2s_buffer);
  806. split_transaction_table[transaction_id].target2initiator_offset = offsetof(split_shared_memory_t, rpc_s2m_buffer);
  807. }
  808. 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) {
  809. // Prevent transaction attempts while transport is disconnected
  810. if (!is_transport_connected()) {
  811. return false;
  812. }
  813. // Prevent invoking RPC on QMK core sync data
  814. if (transaction_id <= GET_RPC_RESP_DATA) return false;
  815. // Prevent sizing issues
  816. if (initiator2target_buffer_size > RPC_M2S_BUFFER_SIZE) return false;
  817. if (target2initiator_buffer_size > RPC_S2M_BUFFER_SIZE) return false;
  818. // Prepare the metadata block
  819. rpc_sync_info_t info = {.payload = {.transaction_id = transaction_id, .m2s_length = initiator2target_buffer_size, .s2m_length = target2initiator_buffer_size}};
  820. info.checksum = crc8(&info.payload, sizeof(info.payload));
  821. // Make sure the local side knows that we're not sending the full block of data
  822. split_transaction_table[PUT_RPC_REQ_DATA].initiator2target_buffer_size = initiator2target_buffer_size;
  823. split_transaction_table[GET_RPC_RESP_DATA].target2initiator_buffer_size = target2initiator_buffer_size;
  824. // Run through the sequence:
  825. // * set the transaction ID and lengths
  826. // * send the request data
  827. // * execute RPC callback
  828. // * retrieve the response data
  829. if (!transport_write(PUT_RPC_INFO, &info, sizeof(info))) {
  830. return false;
  831. }
  832. if (!transport_write(PUT_RPC_REQ_DATA, initiator2target_buffer, initiator2target_buffer_size)) {
  833. return false;
  834. }
  835. if (!transport_write(EXECUTE_RPC, &transaction_id, sizeof(transaction_id))) {
  836. return false;
  837. }
  838. if (!transport_read(GET_RPC_RESP_DATA, target2initiator_buffer, target2initiator_buffer_size)) {
  839. return false;
  840. }
  841. return true;
  842. }
  843. void slave_rpc_info_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  844. // The RPC info block contains the intended transaction ID, as well as the sizes for both inbound and outbound data.
  845. // Ignore the args -- the `split_shmem` already has the info, we just need to act upon it.
  846. // We must keep the `split_transaction_table` non-const, so that it is able to be modified at runtime.
  847. split_transaction_table[PUT_RPC_REQ_DATA].initiator2target_buffer_size = split_shmem->rpc_info.payload.m2s_length;
  848. split_transaction_table[GET_RPC_RESP_DATA].target2initiator_buffer_size = split_shmem->rpc_info.payload.s2m_length;
  849. }
  850. void slave_rpc_exec_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  851. // We can assume that the buffer lengths are correctly set, now, given that sequentially the rpc_info callback was already executed.
  852. // Go through the rpc_info and execute _that_ transaction's callback, with the scratch buffers as inputs.
  853. // As a safety precaution we check that the received payload matches its checksum first.
  854. if (crc8(&split_shmem->rpc_info.payload, sizeof(split_shmem->rpc_info.payload)) != split_shmem->rpc_info.checksum) {
  855. return;
  856. }
  857. int8_t transaction_id = split_shmem->rpc_info.payload.transaction_id;
  858. if (transaction_id < NUM_TOTAL_TRANSACTIONS) {
  859. split_transaction_desc_t *trans = &split_transaction_table[transaction_id];
  860. if (trans->slave_callback) {
  861. 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);
  862. }
  863. }
  864. }
  865. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)