From ea4a631138611eb07655a097d860f5880a1e6eb6 Mon Sep 17 00:00:00 2001 From: Benedek Kupper Date: Sun, 11 Feb 2024 22:27:58 +0100 Subject: [PATCH] zephyr: hogp: prevent changing protocol mode during CCC write --- c2usb/port/zephyr/bluetooth/hid.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/c2usb/port/zephyr/bluetooth/hid.cpp b/c2usb/port/zephyr/bluetooth/hid.cpp index b449bab..ee8a230 100644 --- a/c2usb/port/zephyr/bluetooth/hid.cpp +++ b/c2usb/port/zephyr/bluetooth/hid.cpp @@ -419,12 +419,20 @@ ssize_t service::ccc_cfg_write(::bt_conn* conn, const gatt::attribute* attr, gat } auto* this_ = reinterpret_cast(attr->user_data); - if (flags != gatt::ccc_flags::NONE) + // clearing the flag has no conditions nor consequences + if (flags == gatt::ccc_flags::NONE) { - return this_->start_app(conn, protocol) ? sizeof(flags) - : BT_GATT_ERR(HOGP_ALREADY_CONNECTED_ERROR); + return sizeof(flags); } - return sizeof(flags); + + // prevent changing protocol mode + if ((protocol == protocol::BOOT) && (this_->get_protocol() != protocol)) + { + return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED); + } + + return this_->start_app(conn, protocol) ? sizeof(flags) + : BT_GATT_ERR(HOGP_ALREADY_CONNECTED_ERROR); } gatt::attribute::builder service::add_input_report(gatt::attribute::builder attr_tail,