From 69679d9f9ff83dec91911acffb3307a651dd52b0 Mon Sep 17 00:00:00 2001
From: tychovrahe <brunam@seznam.cz>
Date: Mon, 13 Jan 2025 13:11:26 +0100
Subject: [PATCH] feat(ble): add command to erase bond belonging to currently
 connected device

[no changelog]
---
 west/trezor/trezor-ble/src/ble/ble_internal.h |  3 ++
 west/trezor/trezor-ble/src/ble/bonds.c        | 24 ++++++++++++--
 west/trezor/trezor-ble/src/ble/management.c   | 33 ++++++++++++++-----
 3 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/west/trezor/trezor-ble/src/ble/ble_internal.h b/west/trezor/trezor-ble/src/ble/ble_internal.h
index 232659656a..7d21517ad5 100644
--- a/west/trezor/trezor-ble/src/ble/ble_internal.h
+++ b/west/trezor/trezor-ble/src/ble/ble_internal.h
@@ -81,6 +81,7 @@ typedef enum {
   INTERNAL_CMD_ACK = 0x05,
   INTERNAL_CMD_ALLOW_PAIRING = 0x06,
   INTERNAL_CMD_REJECT_PAIRING = 0x07,
+  INTERNAL_CMD_UNPAIR = 0x08,
 } internal_cmd_t;
 
 // BLE management functions
@@ -98,6 +99,8 @@ void management_send_pairing_cancelled_event(void);
 bool bonds_erase_all(void);
 // Get number of bonded devices
 int bonds_get_count(void);
+// Erase current bond
+bool bonds_erase_current(void);
 
 // Advertising functions
 // Initialization
diff --git a/west/trezor/trezor-ble/src/ble/bonds.c b/west/trezor/trezor-ble/src/ble/bonds.c
index 7725e10ebb..33fcaf7e26 100644
--- a/west/trezor/trezor-ble/src/ble/bonds.c
+++ b/west/trezor/trezor-ble/src/ble/bonds.c
@@ -21,6 +21,7 @@
 #include <zephyr/types.h>
 
 #include <zephyr/bluetooth/bluetooth.h>
+#include <zephyr/bluetooth/conn.h>
 
 #include <zephyr/logging/log.h>
 
@@ -31,8 +32,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
 
 #include "ble_internal.h"
 
-
-
 bool bonds_erase_all(void) {
   int err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY);
   if (err) {
@@ -57,3 +56,24 @@ int bonds_get_count(void) {
 
   return bond_cnt;
 }
+
+bool bonds_erase_current(void) {
+  int err;
+  struct bt_conn *current = connection_get_current();
+
+  if (current == NULL) {
+    return false;
+  }
+
+  struct bt_conn_info info;
+
+  err = bt_conn_get_info(current, &info);
+  if (err) {
+    LOG_ERR("Failed to get connection info (err %d)", err);
+    return false;
+  }
+
+  err = bt_unpair(BT_ID_DEFAULT, info.le.dst);
+
+  return err == 0;
+}
diff --git a/west/trezor/trezor-ble/src/ble/management.c b/west/trezor/trezor-ble/src/ble/management.c
index a03d7a79f5..e15e095bc8 100644
--- a/west/trezor/trezor-ble/src/ble/management.c
+++ b/west/trezor/trezor-ble/src/ble/management.c
@@ -62,8 +62,15 @@ void management_send_status_event(void) {
 
 void management_send_success_event(void) {
   uint8_t tx_data[] = {
-      INTERNAL_EVENT_SUCCESS,
-  };
+    INTERNAL_EVENT_SUCCESS,
+};
+  trz_comm_send_msg(NRF_SERVICE_BLE_MANAGER, tx_data, sizeof(tx_data));
+}
+
+void management_send_failure_event(void) {
+  uint8_t tx_data[] = {
+    INTERNAL_EVENT_FAILURE,
+};
   trz_comm_send_msg(NRF_SERVICE_BLE_MANAGER, tx_data, sizeof(tx_data));
 }
 
@@ -91,8 +98,11 @@ void management_send_pairing_request_event(uint8_t *data, uint16_t len) {
 
 static void process_command(uint8_t *data, uint16_t len) {
   uint8_t cmd = data[0];
+  bool success = true;
+  bool send_respons = true;
   switch (cmd) {
     case INTERNAL_CMD_SEND_STATE:
+      send_respons = false;
       management_send_status_event();
       break;
     case INTERNAL_CMD_ADVERTISING_ON:
@@ -103,25 +113,32 @@ static void process_command(uint8_t *data, uint16_t len) {
       break;
     case INTERNAL_CMD_ERASE_BONDS:
       bonds_erase_all();
-      management_send_success_event();
       break;
     case INTERNAL_CMD_DISCONNECT:
       connection_disconnect();
-      management_send_success_event();
     case INTERNAL_CMD_ACK:
       // pb_msg_ack();
       break;
     case INTERNAL_CMD_ALLOW_PAIRING:
       pairing_num_comp_reply(true);
-      management_send_success_event();
-      break;
+    break;
     case INTERNAL_CMD_REJECT_PAIRING:
       pairing_num_comp_reply(false);
-      management_send_success_event();
-      break;
+    break;
+    case INTERNAL_CMD_UNPAIR:
+      success = bonds_erase_current();
+    break;
     default:
       break;
   }
+
+  if (send_respons) {
+    if (success) {
+      management_send_success_event();
+    } else {
+      management_send_failure_event();
+    }
+  }
 }
 
 void management_init(void) { k_sem_give(&management_ok); }