From ccf514cdd1c2335c5475eb1c603545c9ec0c92c4 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 4 Jan 2024 22:45:03 -0800 Subject: [PATCH] feat(ble): Request encryption if notifying fails * If attempting to notify and getting an EPERM return value, request upgrading the security of the connection at that moment, since it likely means we got a connection to a bonded host but the connection hasn't been upgraded to encrypted yet. --- app/src/hog.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/hog.c b/app/src/hog.c index 1baf00b53b50..eb60959d20fa 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -258,8 +258,10 @@ void send_keyboard_report_callback(struct k_work *work) { }; int err = bt_gatt_notify_cb(conn, ¬ify_params); - if (err) { - LOG_ERR("Error notifying %d", err); + if (err == -EPERM) { + bt_conn_set_security(conn, BT_SECURITY_L2); + } else if (err) { + LOG_DBG("Error notifying %d", err); } bt_conn_unref(conn); @@ -308,7 +310,9 @@ void send_consumer_report_callback(struct k_work *work) { }; int err = bt_gatt_notify_cb(conn, ¬ify_params); - if (err) { + if (err == -EPERM) { + bt_conn_set_security(conn, BT_SECURITY_L2); + } else if (err) { LOG_DBG("Error notifying %d", err); } @@ -359,7 +363,9 @@ void send_mouse_report_callback(struct k_work *work) { }; int err = bt_gatt_notify_cb(conn, ¬ify_params); - if (err) { + if (err == -EPERM) { + bt_conn_set_security(conn, BT_SECURITY_L2); + } else if (err) { LOG_DBG("Error notifying %d", err); } @@ -380,9 +386,10 @@ int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) { }; int err = bt_gatt_notify_cb(conn, ¬ify_params); - if (err) { + if (err == -EPERM) { + bt_conn_set_security(conn, BT_SECURITY_L2); + } else if (err) { LOG_DBG("Error notifying %d", err); - return err; } bt_conn_unref(conn);