transactions.c 43 KB

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