From c0e4ccda0d4ce717e98d88b1f3a950e13770c137 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 16 Nov 2023 09:25:19 +0100 Subject: [PATCH] example/gatt_counter: print ATT Writes --- example/gatt_counter.c | 28 ++++++++++++++++++---------- example/gatt_counter.gatt | 6 +++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/example/gatt_counter.c b/example/gatt_counter.c index 824754f72..f47cda8e9 100644 --- a/example/gatt_counter.c +++ b/example/gatt_counter.c @@ -252,20 +252,28 @@ static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t a /* * @section ATT Write * - * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification - * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored and used - * in the heartbeat handler to decide if a new value should be sent. See Listing attWrite. + * @text The only valid ATT writes in this example are to the Client Characteristic Configuration, which configures notification + * and indication and to the the Characteristic Value. + * If the ATT handle matches the client configuration handle, the new configuration value is stored and used + * in the heartbeat handler to decide if a new value should be sent. + * If the ATT handle matches the characteristic value handle, we print the write as hexdump + * See Listing attWrite. */ /* LISTING_START(attWrite): ATT Write */ static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ - UNUSED(transaction_mode); - UNUSED(offset); - UNUSED(buffer_size); - - if (att_handle != ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE) return 0; - le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; - con_handle = connection_handle; + switch (att_handle){ + case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: + le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; + con_handle = connection_handle; + break; + case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE: + printf("Write: transaction mode %u, offset %u, data (%u bytes): ", transaction_mode, offset, buffer_size); + printf_hexdump(buffer, buffer_size); + break; + default: + break; + } return 0; } /* LISTING_END */ diff --git a/example/gatt_counter.gatt b/example/gatt_counter.gatt index 8f11e6e79..90ee33759 100644 --- a/example/gatt_counter.gatt +++ b/example/gatt_counter.gatt @@ -1,5 +1,5 @@ PRIMARY_SERVICE, GAP_SERVICE -CHARACTERISTIC, GAP_DEVICE_NAME, READ, "LE Counter" +CHARACTERISTIC, GAP_DEVICE_NAME, READ, "GATT Counter" // add Battery Service #import @@ -9,5 +9,5 @@ CHARACTERISTIC, GATT_DATABASE_HASH, READ, // Counter Service PRIMARY_SERVICE, 0000FF10-0000-1000-8000-00805F9B34FB -// Counter Characteristic, with read and notify -CHARACTERISTIC, 0000FF11-0000-1000-8000-00805F9B34FB, READ | NOTIFY | DYNAMIC, +// Counter Characteristic, with read, write and notify +CHARACTERISTIC, 0000FF11-0000-1000-8000-00805F9B34FB, READ | WRITE | NOTIFY | DYNAMIC,