From 7fe77366389f1aba989481f66ad31465866de926 Mon Sep 17 00:00:00 2001 From: "mila@ringwald.ch" Date: Wed, 3 Dec 2014 22:33:07 +0000 Subject: [PATCH] added log error if peripheral mtu is not iniitalized --- ble/gatt_client.c | 21 +++-- test/gatt_client/Makefile | 2 +- test/gatt_client/gatt_client.c | 162 ++++++++++++++++----------------- test/gatt_client/mock.c | 3 +- 4 files changed, 95 insertions(+), 93 deletions(-) diff --git a/ble/gatt_client.c b/ble/gatt_client.c index 3fb7ba111..8d3eaf523 100644 --- a/ble/gatt_client.c +++ b/ble/gatt_client.c @@ -65,6 +65,13 @@ static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8 static void dummy_notify(le_event_t* event){} +static uint16_t peripheral_mtu(gatt_client_t *peripheral){ + if (peripheral->mtu > l2cap_max_le_mtu()){ + log_info(" problem: peripheral mtu is not initialized\n"); + return l2cap_max_le_mtu(); + } + return peripheral->mtu; +} static uint16_t gatt_client_next_id(){ if (gatt_client_id < 0xFFFF) { @@ -96,8 +103,6 @@ uint16_t gatt_client_register_packet_handler(gatt_client_callback_t gatt_callbac subclient->id = gatt_client_next_id(); subclient->callback = gatt_callback; linked_list_add(&gatt_subclients, (linked_item_t *) subclient); - - printf("regstered callback %p, id %d\n", gatt_callback, subclient->id); return subclient->id; } @@ -172,8 +177,8 @@ static gatt_client_t * provide_context_for_conn_handle(uint16_t con_handle){ if (!context) return NULL; // init state context->handle = con_handle; + context->mtu = ATT_DEFAULT_MTU; context->mtu_state = SEND_MTU_EXCHANGE; - context->gatt_client_state = P_READY; gatt_client_timeout_start(context); linked_list_add(&gatt_client_connections, (linked_item_t*)context); @@ -302,7 +307,7 @@ static void att_prepare_write_request(uint16_t request_type, uint16_t peripheral } static uint16_t write_blob_length(gatt_client_t * peripheral){ - uint16_t max_blob_length = peripheral->mtu - 5; + uint16_t max_blob_length = peripheral_mtu(peripheral) - 5; if (peripheral->attribute_offset >= peripheral->attribute_length) { return 0; } @@ -640,7 +645,7 @@ static inline void trigger_next_prepare_write_query(gatt_client_t * peripheral, static inline void trigger_next_blob_query(gatt_client_t * peripheral, gatt_client_state_t next_query_state, uint16_t received_blob_length){ - uint16_t max_blob_length = peripheral->mtu - 1; + uint16_t max_blob_length = peripheral_mtu(peripheral) - 1; if (received_blob_length < max_blob_length){ gatt_client_handle_transaction_complete(peripheral); emit_gatt_complete_event(peripheral, 0); @@ -700,7 +705,9 @@ static void gatt_client_run(){ switch (peripheral->gatt_client_state){ case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE: case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR: - if (peripheral->attribute_length < peripheral->mtu - 3) break; + + if (peripheral->attribute_length < peripheral_mtu(peripheral) - 3) break; + printf(".. ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH %u > %u\n", peripheral->attribute_length, peripheral_mtu(peripheral)); gatt_client_handle_transaction_complete(peripheral); emit_gatt_complete_event(peripheral, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH); return; @@ -1358,7 +1365,7 @@ le_command_status_t gatt_client_write_value_of_characteristic_without_response(u if (!peripheral) return (le_command_status_t) BTSTACK_MEMORY_ALLOC_FAILED; if (!is_ready(peripheral)) return BLE_PERIPHERAL_IN_WRONG_STATE; - if (value_length >= peripheral->mtu - 3) return BLE_VALUE_TOO_LONG; + if (value_length >= peripheral_mtu(peripheral) - 3) return BLE_VALUE_TOO_LONG; peripheral->subclient_id = gatt_client_id; att_write_request(ATT_WRITE_COMMAND, peripheral->handle, value_handle, value_length, value); diff --git a/test/gatt_client/Makefile b/test/gatt_client/Makefile index d97529e4d..9e5da2084 100644 --- a/test/gatt_client/Makefile +++ b/test/gatt_client/Makefile @@ -41,6 +41,6 @@ le_central: ${CORE_OBJ} ${COMMON_OBJ} le_central.o clean: rm -f gatt_client le_central - rm -f *.o ${BTSTACK_ROOT}/src/*.o + rm -f *.o ${BTSTACK_ROOT}/src/*.o ${BTSTACK_ROOT}/ble/*.o rm -rf *.dSYM \ No newline at end of file diff --git a/test/gatt_client/gatt_client.c b/test/gatt_client/gatt_client.c index f6996f8cd..81de9f34c 100644 --- a/test/gatt_client/gatt_client.c +++ b/test/gatt_client/gatt_client.c @@ -209,7 +209,7 @@ static void handle_ble_client_event(le_event_t * event){ } extern "C" int att_write_callback(uint16_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ - printf("gatt client test, att_write_callback mode %u, handle 0x%04x, offset %u, data ", transaction_mode, con_handle, offset); + // printf("gatt client test, att_write_callback mode %u, handle 0x%04x, offset %u, data ", transaction_mode, con_handle, offset); switch(test){ case WRITE_CHARACTERISTIC_DESCRIPTOR: case WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION: @@ -219,7 +219,6 @@ extern "C" int att_write_callback(uint16_t con_handle, uint16_t attribute_handle result_counter++; break; case WRITE_CHARACTERISTIC_VALUE: - printf("att_write_callback \n"); CHECK_EQUAL(ATT_TRANSACTION_MODE_NONE, transaction_mode); CHECK_EQUAL(0, offset); CHECK_EQUAL_ARRAY((uint8_t *)short_value, buffer, short_value_length); @@ -253,7 +252,6 @@ extern "C" uint16_t att_read_callback(uint16_t handle, uint16_t attribute_handle switch(test){ case READ_CHARACTERISTIC_DESCRIPTOR: case READ_CHARACTERISTIC_VALUE: - printf("result_counter %d\n", result_counter); result_counter++; if (buffer){ return copy_bytes((uint8_t *)short_value, short_value_length, offset, buffer, buffer_size); @@ -274,7 +272,7 @@ extern "C" uint16_t att_read_callback(uint16_t handle, uint16_t attribute_handle static const char * decode_status(le_command_status_t status){ switch (status){ - case BLE_PERIPHERAL_OK: return "BLE_PERIPHERAL_OKmak"; + case BLE_PERIPHERAL_OK: return "BLE_PERIPHERAL_OK"; case BLE_PERIPHERAL_IN_WRONG_STATE: return "BLE_PERIPHERAL_IN_WRONG_STATE"; case BLE_PERIPHERAL_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS: return "BLE_PERIPHERAL_DIFFERENT_CONTEXT_FOR_ADDRESS_ALREADY_EXISTS"; case BLE_PERIPHERAL_NOT_CONNECTED: return "BLE_PERIPHERAL_NOT_CONNECTED"; @@ -473,6 +471,28 @@ TEST(GATTClient, TestDiscoverCharacteristicDescriptor){ } + +TEST(GATTClient, TestWriteClientCharacteristicConfiguration){ + test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; + reset_query_state(); + status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); + + reset_query_state(); + status = gatt_client_discover_characteristics_for_service_by_uuid16(gatt_client_id, gatt_client_handle, &services[0], 0xF100); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); + + reset_query_state(); + status = gatt_client_write_client_characteristic_configuration(gatt_client_id, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); +} + TEST(GATTClient, TestReadCharacteristicDescriptor){ test = READ_CHARACTERISTIC_DESCRIPTOR; reset_query_state(); @@ -501,6 +521,49 @@ TEST(GATTClient, TestReadCharacteristicDescriptor){ } +TEST(GATTClient, TestReadCharacteristicValue){ + test = READ_CHARACTERISTIC_VALUE; + reset_query_state(); + status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); + + reset_query_state(); + status = gatt_client_discover_characteristics_for_service_by_uuid16(gatt_client_id, gatt_client_handle, &services[0], 0xF100); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); + + reset_query_state(); + status = gatt_client_read_value_of_characteristic(gatt_client_id, gatt_client_handle, &characteristics[0]); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 3); +} + +TEST(GATTClient, TestWriteCharacteristicValue){ + test = WRITE_CHARACTERISTIC_VALUE; + reset_query_state(); + status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); + + reset_query_state(); + status = gatt_client_discover_characteristics_for_service_by_uuid16(gatt_client_id, gatt_client_handle, &services[0], 0xF100); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 1); + + + reset_query_state(); + status = gatt_client_write_value_of_characteristic(gatt_client_id, gatt_client_handle, characteristics[0].value_handle, short_value_length, (uint8_t*)short_value); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); +} + + TEST(GATTClient, TestWriteCharacteristicDescriptor){ test = WRITE_CHARACTERISTIC_DESCRIPTOR; reset_query_state(); @@ -525,12 +588,13 @@ TEST(GATTClient, TestWriteCharacteristicDescriptor){ status = gatt_client_write_characteristic_descriptor(gatt_client_id, gatt_client_handle, &descriptors[0], sizeof(indication), indication); CHECK_EQUAL(status, BLE_PERIPHERAL_OK); CHECK_EQUAL(gatt_query_complete, 1); - // CHECK_EQUAL(result_counter, 1); + CHECK_EQUAL(result_counter, 1); } -TEST(GATTClient, TestWriteClientCharacteristicConfiguration){ - test = WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION; + +TEST(GATTClient, TestReadLongCharacteristicValue){ + test = READ_LONG_CHARACTERISTIC_VALUE; reset_query_state(); status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); CHECK_EQUAL(status, BLE_PERIPHERAL_OK); @@ -544,13 +608,12 @@ TEST(GATTClient, TestWriteClientCharacteristicConfiguration){ CHECK_EQUAL(result_counter, 1); reset_query_state(); - status = gatt_client_write_client_characteristic_configuration(gatt_client_id, gatt_client_handle, &characteristics[0], GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); + status = gatt_client_read_long_value_of_characteristic(gatt_client_id, gatt_client_handle, &characteristics[0]); + CHECK_EQUAL(status, BLE_PERIPHERAL_OK); + CHECK_EQUAL(gatt_query_complete, 1); + CHECK_EQUAL(result_counter, 7); } - TEST(GATTClient, TestReadLongCharacteristicDescriptor){ test = READ_LONG_CHARACTERISTIC_DESCRIPTOR; reset_query_state(); @@ -576,10 +639,12 @@ TEST(GATTClient, TestReadLongCharacteristicDescriptor){ status = gatt_client_read_long_characteristic_descriptor(gatt_client_id, gatt_client_handle, &descriptors[0]); CHECK_EQUAL(status, BLE_PERIPHERAL_OK); CHECK_EQUAL(gatt_query_complete, 1); - // CHECK_EQUAL(result_counter, 7); + CHECK_EQUAL(result_counter, 7); } + + TEST(GATTClient, TestWriteLongCharacteristicDescriptor){ test = WRITE_LONG_CHARACTERISTIC_DESCRIPTOR; reset_query_state(); @@ -608,74 +673,6 @@ TEST(GATTClient, TestWriteLongCharacteristicDescriptor){ } -TEST(GATTClient, TestReadCharacteristicValue){ - test = READ_CHARACTERISTIC_VALUE; - reset_query_state(); - status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); - - reset_query_state(); - status = gatt_client_discover_characteristics_for_service_by_uuid16(gatt_client_id, gatt_client_handle, &services[0], 0xF100); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); - - reset_query_state(); - status = gatt_client_read_value_of_characteristic(gatt_client_id, gatt_client_handle, &characteristics[0]); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 3); -} - -/* -TEST(GATTClient, TestReadLongCharacteristicValue){ - test = READ_LONG_CHARACTERISTIC_VALUE; - reset_query_state(); - status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); - - reset_query_state(); - status = gatt_client_discover_characteristics_for_service_by_uuid16(gatt_client_id, gatt_client_handle, &services[0], 0xF100); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); - - reset_query_state(); - status = gatt_client_read_long_value_of_characteristic(gatt_client_id, gatt_client_handle, &characteristics[0]); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 7); -} -*/ - -TEST(GATTClient, TestWriteCharacteristicValue){ - printf("WRITE_CHARACTERISTIC_VALUE \n"); - test = WRITE_CHARACTERISTIC_VALUE; - reset_query_state(); - status = gatt_client_discover_primary_services_by_uuid16(gatt_client_id, gatt_client_handle, service_uuid16); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); - - reset_query_state(); - status = gatt_client_discover_characteristics_for_service_by_uuid16(gatt_client_id, gatt_client_handle, &services[0], 0xF100); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); - CHECK_EQUAL(result_counter, 1); - - - reset_query_state(); - status = gatt_client_write_value_of_characteristic(gatt_client_id, gatt_client_handle, characteristics[0].value_handle, short_value_length, (uint8_t*)short_value); - CHECK_EQUAL(status, BLE_PERIPHERAL_OK); - CHECK_EQUAL(gatt_query_complete, 1); -} - -/* - TEST(GATTClient, TestWriteLongCharacteristicValue){ test = WRITE_LONG_CHARACTERISTIC_VALUE; reset_query_state(); @@ -697,6 +694,7 @@ TEST(GATTClient, TestWriteLongCharacteristicValue){ CHECK_EQUAL(gatt_query_complete, 1); CHECK_EQUAL(result_counter, 1); } + TEST(GATTClient, TestWriteReliableLongCharacteristicValue){ test = WRITE_RELIABLE_LONG_CHARACTERISTIC_VALUE; reset_query_state(); @@ -717,8 +715,6 @@ TEST(GATTClient, TestWriteReliableLongCharacteristicValue){ CHECK_EQUAL(gatt_query_complete, 1); CHECK(result_counter); } -*/ - int main (int argc, const char * argv[]){ att_set_db(profile_data); diff --git a/test/gatt_client/mock.c b/test/gatt_client/mock.c index a8e1174d5..007efa74c 100644 --- a/test/gatt_client/mock.c +++ b/test/gatt_client/mock.c @@ -102,8 +102,7 @@ int sm_cmac_ready(){ return 1; } void sm_cmac_start(sm_key_t k, uint16_t message_len, uint8_t * message, void (*done_handler)(uint8_t hash[8])){ - //sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); - + //sm_notify_client(SM_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); } void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){