diff --git a/src/ble/att_db.c b/src/ble/att_db.c index 98d63b915..97f01663e 100644 --- a/src/ble/att_db.c +++ b/src/ble/att_db.c @@ -1248,3 +1248,31 @@ uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uui return 0; } +// att_read_callback helpers +uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ + if (buffer){ + uint16_t bytes_to_copy = btstack_min(blob_size - offset, buffer_size); + memcpy(buffer, &blob[offset], bytes_to_copy); + return bytes_to_copy; + } else { + return blob_size; + } +} + +uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ + uint8_t value_buffer[4]; + little_endian_store_32(value_buffer, 0, value); + return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size); +} + +uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ + uint8_t value_buffer[2]; + little_endian_store_16(value_buffer, 0, value); + return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size); +} + +uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ + uint8_t value_buffer[1]; + value_buffer[0] = value; + return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size); +} diff --git a/src/ble/att_db.h b/src/ble/att_db.h index f81989fed..53ac8429e 100644 --- a/src/ble/att_db.h +++ b/src/ble/att_db.h @@ -177,7 +177,52 @@ void att_clear_transaction_queue(att_connection_t * att_connection); void att_register_service_handler(att_service_handler_t * handler); - // experimental client API +// att_read_callback helpers for a various data types + +/* + * @brief Handle read of blob like data for att_read_callback + * @param blob of data + * @param blob_size of blob + * @param offset from att_read_callback + * @param buffer from att_read_callback + * @param buffer_size from att_read_callback + * @returns value size for buffer == 0 and num bytes copied otherwise + */ +uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); + +/* + * @brief Handle read of little endian unsigned 32 bit value for att_read_callback + * @param value + * @param offset from att_read_callback + * @param buffer from att_read_callback + * @param buffer_size from att_read_callback + * @returns value size for buffer == 0 and num bytes copied otherwise + */ +uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); + +/* + * @brief Handle read of little endian unsigned 16 bit value for att_read_callback + * @param value + * @param offset from att_read_callback + * @param buffer from att_read_callback + * @param buffer_size from att_read_callback + * @returns value size for buffer == 0 and num bytes copied otherwise + */ +uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); + +/* + * @brief Handle read of single byte for att_read_callback + * @param blob of data + * @param blob_size of blob + * @param offset from att_read_callback + * @param buffer from att_read_callback + * @param buffer_size from att_read_callback + * @returns value size for buffer == 0 and num bytes copied otherwise + */ +uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); + + +// experimental client API uint16_t att_uuid_for_handle(uint16_t attribute_handle);