From c255174cf3e55483f14351a69689e24e849445a0 Mon Sep 17 00:00:00 2001
From: Dasky <32983009+daskygit@users.noreply.github.com>
Date: Thu, 6 Oct 2022 10:52:42 +0100
Subject: [PATCH] Implement split comms watchdog (#18599)

---
 data/mappings/info_config.json               |  2 +
 data/schemas/keyboard.jsonschema             |  4 +-
 docs/config_options.md                       |  7 ++++
 docs/feature_split_keyboard.md               | 11 +++++
 quantum/keyboard.c                           |  4 ++
 quantum/split_common/split_util.c            | 43 ++++++++++++++++++++
 quantum/split_common/split_util.h            |  4 ++
 quantum/split_common/transaction_id_define.h |  4 ++
 quantum/split_common/transactions.c          | 33 +++++++++++++++
 quantum/split_common/transport.h             |  4 ++
 10 files changed, 115 insertions(+), 1 deletion(-)

diff --git a/data/mappings/info_config.json b/data/mappings/info_config.json
index 2b8e3cbf90..28567c0d01 100644
--- a/data/mappings/info_config.json
+++ b/data/mappings/info_config.json
@@ -100,6 +100,8 @@
     "SPLIT_USB_DETECT": {"info_key": "split.usb_detect.enabled", "value_type": "bool"},
     "SPLIT_USB_TIMEOUT": {"info_key": "split.usb_detect.timeout", "value_type": "int"},
     "SPLIT_USB_TIMEOUT_POLL": {"info_key": "split.usb_detect.polling_interval", "value_type": "int"},
+    "SPLIT_WATCHDOG_ENABLE": {"info_key": "split.transport.watchdog", "value_type": "bool"},
+    "SPLIT_WATCHDOG_TIMEOUT": {"info_key": "split.transport.watchdog_timeout", "value_type": "int"},
     "SOFT_SERIAL_PIN": {"info_key": "split.soft_serial_pin"},
     "SOFT_SERIAL_SPEED": {"info_key": "split.soft_serial_speed"},
     "TAP_CODE_DELAY": {"info_key": "qmk.tap_keycode_delay", "value_type": "int"},
diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema
index b75465b667..de23005c1e 100644
--- a/data/schemas/keyboard.jsonschema
+++ b/data/schemas/keyboard.jsonschema
@@ -508,7 +508,9 @@
                             "enum": ["custom", "i2c", "serial", "serial_usart"]
                         },
                         "sync_matrix_state": {"type": "boolean"},
-                        "sync_modifiers": {"type": "boolean"}
+                        "sync_modifiers": {"type": "boolean"},
+                        "watchdog": {"type": "boolean"},
+                        "watchdog_timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"}
                     }
                 },
                 "usb_detect": {
diff --git a/docs/config_options.md b/docs/config_options.md
index 3e322e590a..a6ceb199de 100644
--- a/docs/config_options.md
+++ b/docs/config_options.md
@@ -325,6 +325,13 @@ There are a few different ways to set handedness for split keyboards (listed in
 
 * `#define SPLIT_USB_TIMEOUT_POLL 10`
   * Poll frequency when detecting master/slave when using `SPLIT_USB_DETECT`
+  
+* `#define SPLIT_WATCHDOG_ENABLE`
+  * Reboot slave if no communication from master within timeout.
+  * Helps resolve issue where both sides detect as slave using `SPLIT_USB_DETECT`
+
+* `#define SPLIT_WATCHDOG_TIMEOUT 3000`
+  * Maximum slave timeout when waiting for communication from master when using `SPLIT_WATCHDOG_ENABLE`
 
 * `#define FORCED_SYNC_THROTTLE_MS 100`
   * Deadline for synchronizing data from master to slave when using the QMK-provided split transport.
diff --git a/docs/feature_split_keyboard.md b/docs/feature_split_keyboard.md
index e53b3525cb..7d194284d6 100644
--- a/docs/feature_split_keyboard.md
+++ b/docs/feature_split_keyboard.md
@@ -422,6 +422,17 @@ This sets the maximum timeout when detecting master/slave when using `SPLIT_USB_
 ```
 This sets the poll frequency when detecting master/slave when using `SPLIT_USB_DETECT`
 
+```c
+#define SPLIT_WATCHDOG_ENABLE
+```
+
+This will enable a software watchdog on any side delegated as slave and will reboot the keyboard if no successful communication occurs within `SPLIT_WATCHDOG_TIMEOUT`. This can be particularly helpful when `SPLIT_USB_DETECT` delegates both sides as slave in some circumstances.
+
+```c
+#define SPLIT_WATCHDOG_TIMEOUT 3000
+```
+This set the maximum slave timeout when waiting for communication from master when using `SPLIT_WATCHDOG_ENABLE`
+
 ## Hardware Considerations and Mods
 
 Master/slave delegation is made either by detecting voltage on VBUS connection or waiting for USB communication (`SPLIT_USB_DETECT`). Pro Micro boards can use VBUS detection out of the box and be used with or without `SPLIT_USB_DETECT`.
diff --git a/quantum/keyboard.c b/quantum/keyboard.c
index 626e0a7ee1..280532a5fd 100644
--- a/quantum/keyboard.c
+++ b/quantum/keyboard.c
@@ -586,6 +586,10 @@ void keyboard_task(void) {
 
     quantum_task();
 
+#if defined(SPLIT_WATCHDOG_ENABLE)
+    split_watchdog_task();
+#endif
+
 #if defined(RGBLIGHT_ENABLE)
     rgblight_task();
 #endif
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c
index 0b3338ed6f..9f57c7b9fc 100644
--- a/quantum/split_common/split_util.c
+++ b/quantum/split_common/split_util.c
@@ -74,6 +74,46 @@ static inline bool usbIsActive(void) {
 }
 #endif
 
+#if defined(SPLIT_WATCHDOG_ENABLE)
+#    if !defined(SPLIT_WATCHDOG_TIMEOUT)
+#        if defined(SPLIT_USB_TIMEOUT)
+#            define SPLIT_WATCHDOG_TIMEOUT (SPLIT_USB_TIMEOUT + 100)
+#        else
+#            define SPLIT_WATCHDOG_TIMEOUT 3000
+#        endif
+#    endif
+#    if defined(SPLIT_USB_DETECT)
+_Static_assert(SPLIT_USB_TIMEOUT < SPLIT_WATCHDOG_TIMEOUT, "SPLIT_WATCHDOG_TIMEOUT should not be below SPLIT_USB_TIMEOUT.");
+#    endif
+_Static_assert(SPLIT_MAX_CONNECTION_ERRORS > 0, "SPLIT_WATCHDOG_ENABLE requires SPLIT_MAX_CONNECTION_ERRORS be above 0 for a functioning disconnection check.");
+
+static uint32_t split_watchdog_started = 0;
+static bool     split_watchdog_done    = false;
+
+void split_watchdog_init(void) {
+    split_watchdog_started = timer_read32();
+}
+
+void split_watchdog_update(bool done) {
+    split_watchdog_done = done;
+}
+
+bool split_watchdog_check(void) {
+    if (!is_transport_connected()) {
+        split_watchdog_done = false;
+    }
+    return split_watchdog_done;
+}
+
+void split_watchdog_task(void) {
+    if (!split_watchdog_done && !is_keyboard_master()) {
+        if (timer_elapsed32(split_watchdog_started) > SPLIT_WATCHDOG_TIMEOUT) {
+            mcu_reset();
+        }
+    }
+}
+#endif // defined(SPLIT_WATCHDOG_ENABLE)
+
 #ifdef SPLIT_HAND_MATRIX_GRID
 void matrix_io_delay(void);
 
@@ -179,6 +219,9 @@ void split_pre_init(void) {
 void split_post_init(void) {
     if (!is_keyboard_master()) {
         transport_slave_init();
+#if defined(SPLIT_WATCHDOG_ENABLE)
+        split_watchdog_init();
+#endif
     }
 }
 
diff --git a/quantum/split_common/split_util.h b/quantum/split_common/split_util.h
index c7eabea233..5c9a260a14 100644
--- a/quantum/split_common/split_util.h
+++ b/quantum/split_common/split_util.h
@@ -14,3 +14,7 @@ void split_post_init(void);
 
 bool transport_master_if_connected(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]);
 bool is_transport_connected(void);
+
+void split_watchdog_update(bool done);
+void split_watchdog_task(void);
+bool split_watchdog_check(void);
\ No newline at end of file
diff --git a/quantum/split_common/transaction_id_define.h b/quantum/split_common/transaction_id_define.h
index 761a8884f4..8c19948107 100644
--- a/quantum/split_common/transaction_id_define.h
+++ b/quantum/split_common/transaction_id_define.h
@@ -84,6 +84,10 @@ enum serial_transaction_id {
     PUT_POINTING_CPI,
 #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
 
+#if defined(SPLIT_WATCHDOG_ENABLE)
+    PUT_WATCHDOG,
+#endif // defined(SPLIT_WATCHDOG_ENABLE)
+
 #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
     PUT_RPC_INFO,
     PUT_RPC_REQ_DATA,
diff --git a/quantum/split_common/transactions.c b/quantum/split_common/transactions.c
index 67986e4340..527b2f4caf 100644
--- a/quantum/split_common/transactions.c
+++ b/quantum/split_common/transactions.c
@@ -718,6 +718,36 @@ static void pointing_handlers_slave(matrix_row_t master_matrix[], matrix_row_t s
 
 #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
 
+////////////////////////////////////////////////////
+// WATCHDOG
+
+#if defined(SPLIT_WATCHDOG_ENABLE)
+
+static bool watchdog_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
+    bool okay = true;
+    if (!split_watchdog_check()) {
+        okay = transport_write(PUT_WATCHDOG, &okay, sizeof(okay));
+        split_watchdog_update(okay);
+    }
+    return okay;
+}
+
+static void watchdog_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
+    split_watchdog_update(split_shmem->watchdog_pinged);
+}
+
+#    define TRANSACTIONS_WATCHDOG_MASTER() TRANSACTION_HANDLER_MASTER(watchdog)
+#    define TRANSACTIONS_WATCHDOG_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(watchdog)
+#    define TRANSACTIONS_WATCHDOG_REGISTRATIONS [PUT_WATCHDOG] = trans_initiator2target_initializer(watchdog_pinged),
+
+#else // defined(SPLIT_WATCHDOG_ENABLE)
+
+#    define TRANSACTIONS_WATCHDOG_MASTER()
+#    define TRANSACTIONS_WATCHDOG_SLAVE()
+#    define TRANSACTIONS_WATCHDOG_REGISTRATIONS
+
+#endif // defined(SPLIT_WATCHDOG_ENABLE)
+
 ////////////////////////////////////////////////////
 
 split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
@@ -744,6 +774,7 @@ split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = {
     TRANSACTIONS_OLED_REGISTRATIONS
     TRANSACTIONS_ST7565_REGISTRATIONS
     TRANSACTIONS_POINTING_REGISTRATIONS
+    TRANSACTIONS_WATCHDOG_REGISTRATIONS
 // clang-format on
 
 #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
@@ -770,6 +801,7 @@ bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix
     TRANSACTIONS_OLED_MASTER();
     TRANSACTIONS_ST7565_MASTER();
     TRANSACTIONS_POINTING_MASTER();
+    TRANSACTIONS_WATCHDOG_MASTER();
     return true;
 }
 
@@ -789,6 +821,7 @@ void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[
     TRANSACTIONS_OLED_SLAVE();
     TRANSACTIONS_ST7565_SLAVE();
     TRANSACTIONS_POINTING_SLAVE();
+    TRANSACTIONS_WATCHDOG_SLAVE();
 }
 
 #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
diff --git a/quantum/split_common/transport.h b/quantum/split_common/transport.h
index 06778ad14a..833633edc2 100644
--- a/quantum/split_common/transport.h
+++ b/quantum/split_common/transport.h
@@ -188,6 +188,10 @@ typedef struct _split_shared_memory_t {
     split_slave_pointing_sync_t pointing;
 #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE)
 
+#if defined(SPLIT_WATCHDOG_ENABLE)
+    bool watchdog_pinged;
+#endif // defined(SPLIT_WATCHDOG_ENABLE)
+
 #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER)
     rpc_sync_info_t rpc_info;
     uint8_t         rpc_m2s_buffer[RPC_M2S_BUFFER_SIZE];