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.
This commit is contained in:
Peter Johanson 2024-01-04 22:45:03 -08:00 committed by Pete Johanson
parent 69f962fab2
commit 74875314f8

View File

@ -258,8 +258,10 @@ void send_keyboard_report_callback(struct k_work *work) {
}; };
int err = bt_gatt_notify_cb(conn, &notify_params); int err = bt_gatt_notify_cb(conn, &notify_params);
if (err) { if (err == -EPERM) {
LOG_ERR("Error notifying %d", err); bt_conn_set_security(conn, BT_SECURITY_L2);
} else if (err) {
LOG_DBG("Error notifying %d", err);
} }
bt_conn_unref(conn); bt_conn_unref(conn);
@ -308,7 +310,9 @@ void send_consumer_report_callback(struct k_work *work) {
}; };
int err = bt_gatt_notify_cb(conn, &notify_params); int err = bt_gatt_notify_cb(conn, &notify_params);
if (err) { if (err == -EPERM) {
bt_conn_set_security(conn, BT_SECURITY_L2);
} else if (err) {
LOG_DBG("Error notifying %d", 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, &notify_params); int err = bt_gatt_notify_cb(conn, &notify_params);
if (err) { if (err == -EPERM) {
bt_conn_set_security(conn, BT_SECURITY_L2);
} else if (err) {
LOG_DBG("Error notifying %d", 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, &notify_params); int err = bt_gatt_notify_cb(conn, &notify_params);
if (err) { if (err == -EPERM) {
bt_conn_set_security(conn, BT_SECURITY_L2);
} else if (err) {
LOG_DBG("Error notifying %d", err); LOG_DBG("Error notifying %d", err);
return err;
} }
bt_conn_unref(conn); bt_conn_unref(conn);