transactions.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. # endif // NO_ACTION_ONESHOT
  349. bool okay = true;
  350. if (mods_need_sync) {
  351. okay &= transport_write(PUT_MODS, &new_mods, sizeof(new_mods));
  352. if (okay) {
  353. last_update = timer_read32();
  354. }
  355. }
  356. return okay;
  357. }
  358. static void mods_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  359. split_shared_memory_lock();
  360. split_mods_sync_t mods;
  361. memcpy(&mods, &split_shmem->mods, sizeof(split_mods_sync_t));
  362. split_shared_memory_unlock();
  363. set_mods(mods.real_mods);
  364. set_weak_mods(mods.weak_mods);
  365. # ifndef NO_ACTION_ONESHOT
  366. set_oneshot_mods(mods.oneshot_mods);
  367. # endif
  368. }
  369. # define TRANSACTIONS_MODS_MASTER() TRANSACTION_HANDLER_MASTER(mods)
  370. # define TRANSACTIONS_MODS_SLAVE() TRANSACTION_HANDLER_SLAVE(mods)
  371. # define TRANSACTIONS_MODS_REGISTRATIONS [PUT_MODS] = trans_initiator2target_initializer(mods),
  372. #else // SPLIT_MODS_ENABLE
  373. # define TRANSACTIONS_MODS_MASTER()
  374. # define TRANSACTIONS_MODS_SLAVE()
  375. # define TRANSACTIONS_MODS_REGISTRATIONS
  376. #endif // SPLIT_MODS_ENABLE
  377. ////////////////////////////////////////////////////
  378. // Backlight
  379. #ifdef BACKLIGHT_ENABLE
  380. static bool backlight_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  381. static uint32_t last_update = 0;
  382. uint8_t level = is_backlight_enabled() ? get_backlight_level() : 0;
  383. return send_if_condition(PUT_BACKLIGHT, &last_update, (level != split_shmem->backlight_level), &level, sizeof(level));
  384. }
  385. static void backlight_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  386. split_shared_memory_lock();
  387. uint8_t backlight_level = split_shmem->backlight_level;
  388. split_shared_memory_unlock();
  389. backlight_level_noeeprom(backlight_level);
  390. }
  391. # define TRANSACTIONS_BACKLIGHT_MASTER() TRANSACTION_HANDLER_MASTER(backlight)
  392. # define TRANSACTIONS_BACKLIGHT_SLAVE() TRANSACTION_HANDLER_SLAVE(backlight)
  393. # define TRANSACTIONS_BACKLIGHT_REGISTRATIONS [PUT_BACKLIGHT] = trans_initiator2target_initializer(backlight_level),
  394. #else // BACKLIGHT_ENABLE
  395. # define TRANSACTIONS_BACKLIGHT_MASTER()
  396. # define TRANSACTIONS_BACKLIGHT_SLAVE()
  397. # define TRANSACTIONS_BACKLIGHT_REGISTRATIONS
  398. #endif // BACKLIGHT_ENABLE
  399. ////////////////////////////////////////////////////
  400. // RGBLIGHT
  401. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  402. static bool rgblight_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  403. static uint32_t last_update = 0;
  404. rgblight_syncinfo_t rgblight_sync;
  405. rgblight_get_syncinfo(&rgblight_sync);
  406. if (send_if_condition(PUT_RGBLIGHT, &last_update, (rgblight_sync.status.change_flags != 0), &rgblight_sync, sizeof(rgblight_sync))) {
  407. rgblight_clear_change_flags();
  408. } else {
  409. return false;
  410. }
  411. return true;
  412. }
  413. static void rgblight_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  414. split_shared_memory_lock();
  415. // Update the RGB with the new data
  416. rgblight_syncinfo_t rgblight_sync;
  417. memcpy(&rgblight_sync, &split_shmem->rgblight_sync, sizeof(rgblight_syncinfo_t));
  418. split_shmem->rgblight_sync.status.change_flags = 0;
  419. split_shared_memory_unlock();
  420. if (rgblight_sync.status.change_flags != 0) {
  421. rgblight_update_sync(&rgblight_sync, false);
  422. }
  423. }
  424. # define TRANSACTIONS_RGBLIGHT_MASTER() TRANSACTION_HANDLER_MASTER(rgblight)
  425. # define TRANSACTIONS_RGBLIGHT_SLAVE() TRANSACTION_HANDLER_SLAVE(rgblight)
  426. # define TRANSACTIONS_RGBLIGHT_REGISTRATIONS [PUT_RGBLIGHT] = trans_initiator2target_initializer(rgblight_sync),
  427. #else // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  428. # define TRANSACTIONS_RGBLIGHT_MASTER()
  429. # define TRANSACTIONS_RGBLIGHT_SLAVE()
  430. # define TRANSACTIONS_RGBLIGHT_REGISTRATIONS
  431. #endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
  432. ////////////////////////////////////////////////////
  433. // LED Matrix
  434. #if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  435. static bool led_matrix_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  436. static uint32_t last_update = 0;
  437. led_matrix_sync_t led_matrix_sync;
  438. memcpy(&led_matrix_sync.led_matrix, &led_matrix_eeconfig, sizeof(led_eeconfig_t));
  439. led_matrix_sync.led_suspend_state = led_matrix_get_suspend_state();
  440. return send_if_data_mismatch(PUT_LED_MATRIX, &last_update, &led_matrix_sync, &split_shmem->led_matrix_sync, sizeof(led_matrix_sync));
  441. }
  442. static void led_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  443. split_shared_memory_lock();
  444. memcpy(&led_matrix_eeconfig, &split_shmem->led_matrix_sync.led_matrix, sizeof(led_eeconfig_t));
  445. bool led_suspend_state = split_shmem->led_matrix_sync.led_suspend_state;
  446. split_shared_memory_unlock();
  447. led_matrix_set_suspend_state(led_suspend_state);
  448. }
  449. # define TRANSACTIONS_LED_MATRIX_MASTER() TRANSACTION_HANDLER_MASTER(led_matrix)
  450. # define TRANSACTIONS_LED_MATRIX_SLAVE() TRANSACTION_HANDLER_SLAVE(led_matrix)
  451. # define TRANSACTIONS_LED_MATRIX_REGISTRATIONS [PUT_LED_MATRIX] = trans_initiator2target_initializer(led_matrix_sync),
  452. #else // defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  453. # define TRANSACTIONS_LED_MATRIX_MASTER()
  454. # define TRANSACTIONS_LED_MATRIX_SLAVE()
  455. # define TRANSACTIONS_LED_MATRIX_REGISTRATIONS
  456. #endif // defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_SPLIT)
  457. ////////////////////////////////////////////////////
  458. // RGB Matrix
  459. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  460. static bool rgb_matrix_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  461. static uint32_t last_update = 0;
  462. rgb_matrix_sync_t rgb_matrix_sync;
  463. memcpy(&rgb_matrix_sync.rgb_matrix, &rgb_matrix_config, sizeof(rgb_config_t));
  464. rgb_matrix_sync.rgb_suspend_state = rgb_matrix_get_suspend_state();
  465. return send_if_data_mismatch(PUT_RGB_MATRIX, &last_update, &rgb_matrix_sync, &split_shmem->rgb_matrix_sync, sizeof(rgb_matrix_sync));
  466. }
  467. static void rgb_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  468. split_shared_memory_lock();
  469. memcpy(&rgb_matrix_config, &split_shmem->rgb_matrix_sync.rgb_matrix, sizeof(rgb_config_t));
  470. bool rgb_suspend_state = split_shmem->rgb_matrix_sync.rgb_suspend_state;
  471. split_shared_memory_unlock();
  472. rgb_matrix_set_suspend_state(rgb_suspend_state);
  473. }
  474. # define TRANSACTIONS_RGB_MATRIX_MASTER() TRANSACTION_HANDLER_MASTER(rgb_matrix)
  475. # define TRANSACTIONS_RGB_MATRIX_SLAVE() TRANSACTION_HANDLER_SLAVE(rgb_matrix)
  476. # define TRANSACTIONS_RGB_MATRIX_REGISTRATIONS [PUT_RGB_MATRIX] = trans_initiator2target_initializer(rgb_matrix_sync),
  477. #else // defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  478. # define TRANSACTIONS_RGB_MATRIX_MASTER()
  479. # define TRANSACTIONS_RGB_MATRIX_SLAVE()
  480. # define TRANSACTIONS_RGB_MATRIX_REGISTRATIONS
  481. #endif // defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  482. ////////////////////////////////////////////////////
  483. // WPM
  484. #if defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)
  485. static bool wpm_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  486. static uint32_t last_update = 0;
  487. uint8_t current_wpm = get_current_wpm();
  488. return send_if_condition(PUT_WPM, &last_update, (current_wpm != split_shmem->current_wpm), &current_wpm, sizeof(current_wpm));
  489. }
  490. static void wpm_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  491. set_current_wpm(split_shmem->current_wpm);
  492. }
  493. # define TRANSACTIONS_WPM_MASTER() TRANSACTION_HANDLER_MASTER(wpm)
  494. # define TRANSACTIONS_WPM_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(wpm)
  495. # define TRANSACTIONS_WPM_REGISTRATIONS [PUT_WPM] = trans_initiator2target_initializer(current_wpm),
  496. #else // defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)
  497. # define TRANSACTIONS_WPM_MASTER()
  498. # define TRANSACTIONS_WPM_SLAVE()
  499. # define TRANSACTIONS_WPM_REGISTRATIONS
  500. #endif // defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)
  501. ////////////////////////////////////////////////////
  502. // OLED
  503. #if defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
  504. static bool oled_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  505. static uint32_t last_update = 0;
  506. bool current_oled_state = is_oled_on();
  507. return send_if_condition(PUT_OLED, &last_update, (current_oled_state != split_shmem->current_oled_state), &current_oled_state, sizeof(current_oled_state));
  508. }
  509. static void oled_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  510. split_shared_memory_lock();
  511. uint8_t current_oled_state = split_shmem->current_oled_state;
  512. split_shared_memory_unlock();
  513. if (current_oled_state) {
  514. oled_on();
  515. } else {
  516. oled_off();
  517. }
  518. }
  519. # define TRANSACTIONS_OLED_MASTER() TRANSACTION_HANDLER_MASTER(oled)
  520. # define TRANSACTIONS_OLED_SLAVE() TRANSACTION_HANDLER_SLAVE(oled)
  521. # define TRANSACTIONS_OLED_REGISTRATIONS [PUT_OLED] = trans_initiator2target_initializer(current_oled_state),
  522. #else // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
  523. # define TRANSACTIONS_OLED_MASTER()
  524. # define TRANSACTIONS_OLED_SLAVE()
  525. # define TRANSACTIONS_OLED_REGISTRATIONS
  526. #endif // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
  527. ////////////////////////////////////////////////////
  528. // ST7565
  529. #if defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
  530. static bool st7565_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  531. static uint32_t last_update = 0;
  532. bool current_st7565_state = st7565_is_on();
  533. return send_if_condition(PUT_ST7565, &last_update, (current_st7565_state != split_shmem->current_st7565_state), &current_st7565_state, sizeof(current_st7565_state));
  534. }
  535. static void st7565_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  536. split_shared_memory_lock();
  537. uint8_t current_st7565_state = split_shmem->current_st7565_state;
  538. split_shared_memory_unlock();
  539. if (current_st7565_state) {
  540. st7565_on();
  541. } else {
  542. st7565_off();
  543. }
  544. }
  545. # define TRANSACTIONS_ST7565_MASTER() TRANSACTION_HANDLER_MASTER(st7565)
  546. # define TRANSACTIONS_ST7565_SLAVE() TRANSACTION_HANDLER_SLAVE(st7565)
  547. # define TRANSACTIONS_ST7565_REGISTRATIONS [PUT_ST7565] = trans_initiator2target_initializer(current_st7565_state),
  548. #else // defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
  549. # define TRANSACTIONS_ST7565_MASTER()
  550. # define TRANSACTIONS_ST7565_SLAVE()
  551. # define TRANSACTIONS_ST7565_REGISTRATIONS
  552. #endif // defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
  553. ////////////////////////////////////////////////////
  554. // POINTING
  555. #if defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  556. static bool pointing_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  557. # if defined(POINTING_DEVICE_LEFT)
  558. if (is_keyboard_left()) {
  559. return true;
  560. }
  561. # elif defined(POINTING_DEVICE_RIGHT)
  562. if (!is_keyboard_left()) {
  563. return true;
  564. }
  565. # endif
  566. static uint32_t last_update = 0;
  567. static uint16_t last_cpi = 0;
  568. report_mouse_t temp_state;
  569. uint16_t temp_cpi;
  570. bool okay = read_if_checksum_mismatch(GET_POINTING_CHECKSUM, GET_POINTING_DATA, &last_update, &temp_state, &split_shmem->pointing.report, sizeof(temp_state));
  571. if (okay) pointing_device_set_shared_report(temp_state);
  572. temp_cpi = pointing_device_get_shared_cpi();
  573. if (temp_cpi && last_cpi != temp_cpi) {
  574. split_shmem->pointing.cpi = temp_cpi;
  575. okay = transport_write(PUT_POINTING_CPI, &split_shmem->pointing.cpi, sizeof(split_shmem->pointing.cpi));
  576. if (okay) {
  577. last_cpi = temp_cpi;
  578. }
  579. }
  580. return okay;
  581. }
  582. extern const pointing_device_driver_t pointing_device_driver;
  583. static void pointing_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  584. # if defined(POINTING_DEVICE_LEFT)
  585. if (!is_keyboard_left()) {
  586. return;
  587. }
  588. # elif defined(POINTING_DEVICE_RIGHT)
  589. if (is_keyboard_left()) {
  590. return;
  591. }
  592. # endif
  593. # if (POINTING_DEVICE_TASK_THROTTLE_MS > 0)
  594. static uint32_t last_exec = 0;
  595. if (timer_elapsed32(last_exec) < POINTING_DEVICE_TASK_THROTTLE_MS) {
  596. return;
  597. }
  598. last_exec = timer_read32();
  599. # endif
  600. uint16_t temp_cpi = !pointing_device_driver.get_cpi ? 0 : pointing_device_driver.get_cpi(); // check for NULL
  601. split_shared_memory_lock();
  602. split_slave_pointing_sync_t pointing;
  603. memcpy(&pointing, &split_shmem->pointing, sizeof(split_slave_pointing_sync_t));
  604. split_shared_memory_unlock();
  605. if (pointing.cpi && pointing.cpi != temp_cpi && pointing_device_driver.set_cpi) {
  606. pointing_device_driver.set_cpi(pointing.cpi);
  607. }
  608. pointing.report = pointing_device_driver.get_report((report_mouse_t){0});
  609. // Now update the checksum given that the pointing has been written to
  610. pointing.checksum = crc8(&pointing.report, sizeof(report_mouse_t));
  611. split_shared_memory_lock();
  612. memcpy(&split_shmem->pointing, &pointing, sizeof(split_slave_pointing_sync_t));
  613. split_shared_memory_unlock();
  614. }
  615. # define TRANSACTIONS_POINTING_MASTER() TRANSACTION_HANDLER_MASTER(pointing)
  616. # define TRANSACTIONS_POINTING_SLAVE() TRANSACTION_HANDLER_SLAVE(pointing)
  617. # 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),
  618. #else // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  619. # define TRANSACTIONS_POINTING_MASTER()
  620. # define TRANSACTIONS_POINTING_SLAVE()
  621. # define TRANSACTIONS_POINTING_REGISTRATIONS
  622. #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
  623. ////////////////////////////////////////////////////
  624. // WATCHDOG
  625. #if defined(SPLIT_WATCHDOG_ENABLE)
  626. static bool watchdog_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  627. bool okay = true;
  628. if (!split_watchdog_check()) {
  629. okay = transport_write(PUT_WATCHDOG, &okay, sizeof(okay));
  630. split_watchdog_update(okay);
  631. }
  632. return okay;
  633. }
  634. static void watchdog_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  635. split_watchdog_update(split_shmem->watchdog_pinged);
  636. }
  637. # define TRANSACTIONS_WATCHDOG_MASTER() TRANSACTION_HANDLER_MASTER(watchdog)
  638. # define TRANSACTIONS_WATCHDOG_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(watchdog)
  639. # define TRANSACTIONS_WATCHDOG_REGISTRATIONS [PUT_WATCHDOG] = trans_initiator2target_initializer(watchdog_pinged),
  640. #else // defined(SPLIT_WATCHDOG_ENABLE)
  641. # define TRANSACTIONS_WATCHDOG_MASTER()
  642. # define TRANSACTIONS_WATCHDOG_SLAVE()
  643. # define TRANSACTIONS_WATCHDOG_REGISTRATIONS
  644. #endif // defined(SPLIT_WATCHDOG_ENABLE)
  645. #if defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  646. uint8_t split_haptic_play = 0xFF;
  647. extern haptic_config_t haptic_config;
  648. static bool haptic_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  649. static uint32_t last_update = 0;
  650. split_slave_haptic_sync_t haptic_sync;
  651. memcpy(&haptic_sync.haptic_config, &haptic_config, sizeof(haptic_config_t));
  652. haptic_sync.haptic_play = split_haptic_play;
  653. bool okay = send_if_data_mismatch(PUT_HAPTIC, &last_update, &haptic_sync, &split_shmem->haptic_sync, sizeof(haptic_sync));
  654. split_haptic_play = 0xFF;
  655. return okay;
  656. }
  657. static void haptic_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  658. memcpy(&haptic_config, &split_shmem->haptic_sync.haptic_config, sizeof(haptic_config_t));
  659. if (split_shmem->haptic_sync.haptic_play != 0xFF) {
  660. haptic_set_mode(split_shmem->haptic_sync.haptic_play);
  661. haptic_play();
  662. }
  663. }
  664. // clang-format off
  665. # define TRANSACTIONS_HAPTIC_MASTER() TRANSACTION_HANDLER_MASTER(haptic)
  666. # define TRANSACTIONS_HAPTIC_SLAVE() TRANSACTION_HANDLER_SLAVE(haptic)
  667. # define TRANSACTIONS_HAPTIC_REGISTRATIONS [PUT_HAPTIC] = trans_initiator2target_initializer(haptic_sync),
  668. // clang-format on
  669. #else // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  670. # define TRANSACTIONS_HAPTIC_MASTER()
  671. # define TRANSACTIONS_HAPTIC_SLAVE()
  672. # define TRANSACTIONS_HAPTIC_REGISTRATIONS
  673. #endif // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE)
  674. #if defined(SPLIT_ACTIVITY_ENABLE)
  675. static bool activity_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  676. static uint32_t last_update = 0;
  677. split_slave_activity_sync_t activity_sync;
  678. activity_sync.matrix_timestamp = last_matrix_activity_time();
  679. activity_sync.encoder_timestamp = last_encoder_activity_time();
  680. activity_sync.pointing_device_timestamp = last_pointing_device_activity_time();
  681. return send_if_data_mismatch(PUT_ACTIVITY, &last_update, &activity_sync, &split_shmem->activity_sync, sizeof(activity_sync));
  682. }
  683. static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  684. set_activity_timestamps(split_shmem->activity_sync.matrix_timestamp, split_shmem->activity_sync.encoder_timestamp, split_shmem->activity_sync.pointing_device_timestamp);
  685. }
  686. // clang-format off
  687. # define TRANSACTIONS_ACTIVITY_MASTER() TRANSACTION_HANDLER_MASTER(activity)
  688. # define TRANSACTIONS_ACTIVITY_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(activity)
  689. # define TRANSACTIONS_ACTIVITY_REGISTRATIONS [PUT_ACTIVITY] = trans_initiator2target_initializer(activity_sync),
  690. // clang-format on
  691. #else // defined(SPLIT_ACTIVITY_ENABLE)
  692. # define TRANSACTIONS_ACTIVITY_MASTER()
  693. # define TRANSACTIONS_ACTIVITY_SLAVE()
  694. # define TRANSACTIONS_ACTIVITY_REGISTRATIONS
  695. #endif // defined(SPLIT_ACTIVITY_ENABLE)
  696. ////////////////////////////////////////////////////
  697. // Detected OS
  698. #if defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  699. static bool detected_os_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  700. static uint32_t last_detected_os_update = 0;
  701. os_variant_t detected_os = detected_host_os();
  702. bool okay = send_if_condition(PUT_DETECTED_OS, &last_detected_os_update, (detected_os != split_shmem->detected_os), &detected_os, sizeof(os_variant_t));
  703. return okay;
  704. }
  705. static void detected_os_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  706. slave_update_detected_host_os(split_shmem->detected_os);
  707. }
  708. # define TRANSACTIONS_DETECTED_OS_MASTER() TRANSACTION_HANDLER_MASTER(detected_os)
  709. # define TRANSACTIONS_DETECTED_OS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(detected_os)
  710. # define TRANSACTIONS_DETECTED_OS_REGISTRATIONS [PUT_DETECTED_OS] = trans_initiator2target_initializer(detected_os),
  711. #else // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  712. # define TRANSACTIONS_DETECTED_OS_MASTER()
  713. # define TRANSACTIONS_DETECTED_OS_SLAVE()
  714. # define TRANSACTIONS_DETECTED_OS_REGISTRATIONS
  715. #endif // defined(OS_DETECTION_ENABLE) && defined(SPLIT_DETECTED_OS_ENABLE)
  716. ////////////////////////////////////////////////////
  717. split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
  718. // Set defaults
  719. [0 ...(NUM_TOTAL_TRANSACTIONS - 1)] = {0, 0, 0, 0, 0},
  720. #ifdef USE_I2C
  721. [I2C_EXECUTE_CALLBACK] = trans_initiator2target_initializer(transaction_id),
  722. #endif // USE_I2C
  723. // clang-format off
  724. TRANSACTIONS_SLAVE_MATRIX_REGISTRATIONS
  725. TRANSACTIONS_MASTER_MATRIX_REGISTRATIONS
  726. TRANSACTIONS_ENCODERS_REGISTRATIONS
  727. TRANSACTIONS_SYNC_TIMER_REGISTRATIONS
  728. TRANSACTIONS_LAYER_STATE_REGISTRATIONS
  729. TRANSACTIONS_LED_STATE_REGISTRATIONS
  730. TRANSACTIONS_MODS_REGISTRATIONS
  731. TRANSACTIONS_BACKLIGHT_REGISTRATIONS
  732. TRANSACTIONS_RGBLIGHT_REGISTRATIONS
  733. TRANSACTIONS_LED_MATRIX_REGISTRATIONS
  734. TRANSACTIONS_RGB_MATRIX_REGISTRATIONS
  735. TRANSACTIONS_WPM_REGISTRATIONS
  736. TRANSACTIONS_OLED_REGISTRATIONS
  737. TRANSACTIONS_ST7565_REGISTRATIONS
  738. TRANSACTIONS_POINTING_REGISTRATIONS
  739. TRANSACTIONS_WATCHDOG_REGISTRATIONS
  740. TRANSACTIONS_HAPTIC_REGISTRATIONS
  741. TRANSACTIONS_ACTIVITY_REGISTRATIONS
  742. TRANSACTIONS_DETECTED_OS_REGISTRATIONS
  743. // clang-format on
  744. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  745. [PUT_RPC_INFO] = trans_initiator2target_initializer_cb(rpc_info, slave_rpc_info_callback),
  746. [PUT_RPC_REQ_DATA] = trans_initiator2target_initializer(rpc_m2s_buffer),
  747. [EXECUTE_RPC] = trans_initiator2target_initializer_cb(rpc_info.payload.transaction_id, slave_rpc_exec_callback),
  748. [GET_RPC_RESP_DATA] = trans_target2initiator_initializer(rpc_s2m_buffer),
  749. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  750. };
  751. bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  752. TRANSACTIONS_SLAVE_MATRIX_MASTER();
  753. TRANSACTIONS_MASTER_MATRIX_MASTER();
  754. TRANSACTIONS_ENCODERS_MASTER();
  755. TRANSACTIONS_SYNC_TIMER_MASTER();
  756. TRANSACTIONS_LAYER_STATE_MASTER();
  757. TRANSACTIONS_LED_STATE_MASTER();
  758. TRANSACTIONS_MODS_MASTER();
  759. TRANSACTIONS_BACKLIGHT_MASTER();
  760. TRANSACTIONS_RGBLIGHT_MASTER();
  761. TRANSACTIONS_LED_MATRIX_MASTER();
  762. TRANSACTIONS_RGB_MATRIX_MASTER();
  763. TRANSACTIONS_WPM_MASTER();
  764. TRANSACTIONS_OLED_MASTER();
  765. TRANSACTIONS_ST7565_MASTER();
  766. TRANSACTIONS_POINTING_MASTER();
  767. TRANSACTIONS_WATCHDOG_MASTER();
  768. TRANSACTIONS_HAPTIC_MASTER();
  769. TRANSACTIONS_ACTIVITY_MASTER();
  770. TRANSACTIONS_DETECTED_OS_MASTER();
  771. return true;
  772. }
  773. void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  774. TRANSACTIONS_SLAVE_MATRIX_SLAVE();
  775. TRANSACTIONS_MASTER_MATRIX_SLAVE();
  776. TRANSACTIONS_ENCODERS_SLAVE();
  777. TRANSACTIONS_SYNC_TIMER_SLAVE();
  778. TRANSACTIONS_LAYER_STATE_SLAVE();
  779. TRANSACTIONS_LED_STATE_SLAVE();
  780. TRANSACTIONS_MODS_SLAVE();
  781. TRANSACTIONS_BACKLIGHT_SLAVE();
  782. TRANSACTIONS_RGBLIGHT_SLAVE();
  783. TRANSACTIONS_LED_MATRIX_SLAVE();
  784. TRANSACTIONS_RGB_MATRIX_SLAVE();
  785. TRANSACTIONS_WPM_SLAVE();
  786. TRANSACTIONS_OLED_SLAVE();
  787. TRANSACTIONS_ST7565_SLAVE();
  788. TRANSACTIONS_POINTING_SLAVE();
  789. TRANSACTIONS_WATCHDOG_SLAVE();
  790. TRANSACTIONS_HAPTIC_SLAVE();
  791. TRANSACTIONS_ACTIVITY_SLAVE();
  792. TRANSACTIONS_DETECTED_OS_SLAVE();
  793. }
  794. #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
  795. void transaction_register_rpc(int8_t transaction_id, slave_callback_t callback) {
  796. // Prevent invoking RPC on QMK core sync data
  797. if (transaction_id <= GET_RPC_RESP_DATA) return;
  798. // Set the callback
  799. split_transaction_table[transaction_id].slave_callback = callback;
  800. split_transaction_table[transaction_id].initiator2target_offset = offsetof(split_shared_memory_t, rpc_m2s_buffer);
  801. split_transaction_table[transaction_id].target2initiator_offset = offsetof(split_shared_memory_t, rpc_s2m_buffer);
  802. }
  803. 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) {
  804. // Prevent transaction attempts while transport is disconnected
  805. if (!is_transport_connected()) {
  806. return false;
  807. }
  808. // Prevent invoking RPC on QMK core sync data
  809. if (transaction_id <= GET_RPC_RESP_DATA) return false;
  810. // Prevent sizing issues
  811. if (initiator2target_buffer_size > RPC_M2S_BUFFER_SIZE) return false;
  812. if (target2initiator_buffer_size > RPC_S2M_BUFFER_SIZE) return false;
  813. // Prepare the metadata block
  814. rpc_sync_info_t info = {.payload = {.transaction_id = transaction_id, .m2s_length = initiator2target_buffer_size, .s2m_length = target2initiator_buffer_size}};
  815. info.checksum = crc8(&info.payload, sizeof(info.payload));
  816. // Make sure the local side knows that we're not sending the full block of data
  817. split_transaction_table[PUT_RPC_REQ_DATA].initiator2target_buffer_size = initiator2target_buffer_size;
  818. split_transaction_table[GET_RPC_RESP_DATA].target2initiator_buffer_size = target2initiator_buffer_size;
  819. // Run through the sequence:
  820. // * set the transaction ID and lengths
  821. // * send the request data
  822. // * execute RPC callback
  823. // * retrieve the response data
  824. if (!transport_write(PUT_RPC_INFO, &info, sizeof(info))) {
  825. return false;
  826. }
  827. if (!transport_write(PUT_RPC_REQ_DATA, initiator2target_buffer, initiator2target_buffer_size)) {
  828. return false;
  829. }
  830. if (!transport_write(EXECUTE_RPC, &transaction_id, sizeof(transaction_id))) {
  831. return false;
  832. }
  833. if (!transport_read(GET_RPC_RESP_DATA, target2initiator_buffer, target2initiator_buffer_size)) {
  834. return false;
  835. }
  836. return true;
  837. }
  838. void slave_rpc_info_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  839. // The RPC info block contains the intended transaction ID, as well as the sizes for both inbound and outbound data.
  840. // Ignore the args -- the `split_shmem` already has the info, we just need to act upon it.
  841. // We must keep the `split_transaction_table` non-const, so that it is able to be modified at runtime.
  842. split_transaction_table[PUT_RPC_REQ_DATA].initiator2target_buffer_size = split_shmem->rpc_info.payload.m2s_length;
  843. split_transaction_table[GET_RPC_RESP_DATA].target2initiator_buffer_size = split_shmem->rpc_info.payload.s2m_length;
  844. }
  845. void slave_rpc_exec_callback(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
  846. // We can assume that the buffer lengths are correctly set, now, given that sequentially the rpc_info callback was already executed.
  847. // Go through the rpc_info and execute _that_ transaction's callback, with the scratch buffers as inputs.
  848. // As a safety precaution we check that the received payload matches its checksum first.
  849. if (crc8(&split_shmem->rpc_info.payload, sizeof(split_shmem->rpc_info.payload)) != split_shmem->rpc_info.checksum) {
  850. return;
  851. }
  852. int8_t transaction_id = split_shmem->rpc_info.payload.transaction_id;
  853. if (transaction_id < NUM_TOTAL_TRANSACTIONS) {
  854. split_transaction_desc_t *trans = &split_transaction_table[transaction_id];
  855. if (trans->slave_callback) {
  856. 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);
  857. }
  858. }
  859. }
  860. #endif // defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)