/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define BTSTACK_FILE__ "ble_peripheral_test.c" /* * ble_peripheral_test.c : Tool for testig BLE peripheral */ #include #include #include #include #include #include #include #include "btstack_config.h" #include "ble/att_db.h" #include "ble/att_server.h" #include "ble/le_device_db.h" #include "ble/sm.h" #include "bluetooth_gatt.h" #include "btstack_debug.h" #include "btstack_event.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "gap.h" #include "hci.h" #include "hci_dump.h" #include "l2cap.h" #include "btstack_stdin.h" #include "bluetooth_data_types.h" #ifdef ENABLE_GATT_OVER_CLASSIC #include "classic/gatt_sdp.h" #include "classic/sdp_util.h" static uint8_t gatt_service_buffer[70]; #endif #define HEARTBEAT_PERIOD_MS 1000 // test profile #include "gatt_server_test.h" ///------ static int gap_advertisements = 0; static int gap_discoverable = 0; static int gap_limited_discoverable = 0; static int gap_connectable = 0; static int gap_privacy = 0; static int gap_random = 0; static int gap_bondable = 0; static int gap_directed_connectable = 0; static int gap_scannable = 0; static bool gap_sc_only; static int gap_adv_filter_policy; static char gap_device_name[20]; static uint16_t gap_appearance = 0; static bd_addr_t gap_reconnection_address; static int att_default_value_long = 0; // static uint8_t test_irk[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; static uint8_t test_irk[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static char * sm_io_capabilities = NULL; static int sm_mitm_protection = 0; static int sm_have_oob_data = 0; static uint8_t * sm_oob_data = (uint8_t *) "0123456789012345"; // = { 0x30...0x39, 0x30..0x35} // static uint8_t sm_oob_data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; static int sm_min_key_size = 7; static int master_addr_type; static bd_addr_t master_address; static int ui_passkey = 0; static int ui_digits_for_passkey = 0; static btstack_timer_source_t heartbeat; static uint8_t counter = 0; static int update_client = 0; static int client_configuration = 0; static uint16_t client_configuration_handle; static hci_con_handle_t handle = HCI_CON_HANDLE_INVALID; static int le_device_db_index; static bool dynamic_db; static uint8_t gatt_database_hash[16]; static btstack_crypto_aes128_cmac_t gatt_aes_cmac_context; static btstack_packet_callback_registration_t sm_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration; static void app_run(void); static void show_usage(void); static void update_advertisements(void); static bd_addr_t tester_address = {0x00, 0x1B, 0xDC, 0x07, 0x32, 0xef}; // static bd_addr_t tester_address = {0x00, 0x1B, 0xDC, 0x06, 0x07, 0x5F}; // static bd_addr_t tester_address = {0x00, 0x1B, 0xDC, 0x08, 0xe2, 0x72}; static int tester_address_type = 0; // general discoverable flags static uint8_t adv_general_discoverable[] = { 2, 01, 02 }; // limited discoverable flags static uint8_t adv_limited_discoverable[] = { 2, 01, 01 }; // non discoverable flags static uint8_t adv_non_discoverable[] = { 2, 01, 00 }; // AD Manufacturer Specific Data - Ericsson, 1, 2, 3, 4 static uint8_t adv_data_1[] = { 7, 0xff, 0x00, 0x00, 1, 2, 3, 4 }; // AD Local Name - 'BTstack' static uint8_t adv_data_2[] = { 8, 0x09, 'B', 'T', 's', 't', 'a', 'c', 'k' }; // AD Flags - 2 - General Discoverable mode -- flags are always prepended static uint8_t adv_data_3[] = { }; // AD Service Data - 0x1812 HID over LE static uint8_t adv_data_4[] = { 3, 0x16, 0x12, 0x18 }; // AD Service Solicitation - 0x1812 HID over LE static uint8_t adv_data_5[] = { 3, 0x14, 0x12, 0x18 }; // AD Services static uint8_t adv_data_6[] = { 3, 0x03, 0x12, 0x18 }; // AD Slave Preferred Connection Interval Range - no min, no max static uint8_t adv_data_7[] = { 5, 0x12, 0xff, 0xff, 0xff, 0xff }; // AD Tx Power Level - +4 dBm static uint8_t adv_data_8[] = { 2, 0x0a, 4 }; // AD OOB static uint8_t adv_data_9[] = { 2, 0x11, 3 }; // AD TK Value static uint8_t adv_data_0[] = { 17, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // AD LE Bluetooth Device Address - 66:55:44:33:22:11 public address static uint8_t adv_data_le_bd_addr[] = { 8, 0x1b, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x00}; // AD Appearance static uint8_t adv_data_appearance[] = { 3, 0x19, 0x00, 0x00}; // AD LE Role - Peripheral only static uint8_t adv_data_le_role[] = {2 , 0x1c, 0x00}; // AD Public Target Address static uint8_t adv_data_public_target_address[] = { 7, 0x17, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; // AD Random Target Address static uint8_t adv_data_random_target_address[] = { 7, 0x18, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; // AD Advertising Interval static uint8_t adv_data_advertising_interval[] = { 3, BLUETOOTH_DATA_TYPE_ADVERTISING_INTERVAL, 0x00, 0x80}; // AD URI static uint8_t adv_data_uri[] = {21, BLUETOOTH_DATA_TYPE_URI, 0x016,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x62,0x6C,0x75,0x65,0x74,0x6F,0x6F,0x74,0x68,0x2E,0x63,0x6F,0x6D}; static uint8_t adv_data_len; static uint8_t adv_data[32]; static uint8_t scan_data_len; static uint8_t scan_data[32]; typedef struct { uint16_t len; uint8_t * data; } advertisement_t; #define ADV(a) { sizeof(a), &a[0]} static advertisement_t advertisements[] = { ADV(adv_data_0), ADV(adv_data_1), ADV(adv_data_2), ADV(adv_data_3), ADV(adv_data_4), ADV(adv_data_5), ADV(adv_data_6), ADV(adv_data_7), ADV(adv_data_8), ADV(adv_data_9), ADV(adv_data_le_bd_addr), ADV(adv_data_appearance), ADV(adv_data_le_role), ADV(adv_data_public_target_address), ADV(adv_data_random_target_address), ADV(adv_data_advertising_interval), ADV(adv_data_uri) }; static int advertisement_index = 2; // signed write static uint8_t signed_write_characteristic_value; // att write queue engine static const char default_value_long[] = "0123456789abcdef0123456789abcdef0123456789abcdef!!"; static const char default_value_short[] = "a"; #define ATT_VALUE_MAX_LEN 50 typedef struct { uint16_t handle; uint16_t len; uint8_t value[ATT_VALUE_MAX_LEN]; } attribute_t; #define ATT_NUM_WRITE_QUEUES 2 static attribute_t att_write_queues[ATT_NUM_WRITE_QUEUES]; #define ATT_NUM_ATTRIBUTES 10 static attribute_t att_attributes[ATT_NUM_ATTRIBUTES]; static void att_write_queue_init(void){ int i; for (i=0;i= 0); printf("Setup Attribute %04x, len %u, value: %s\n", attribute_handle, len, value); btstack_assert(len <= ATT_VALUE_MAX_LEN); att_attributes[index].handle = attribute_handle; att_attributes[index].len = len; memcpy(att_attributes[index].value, value, len); } static void att_attributes_init(void){ int i; for (i=0;i att_value_len) { return 0; } uint16_t bytes_to_copy = att_value_len - offset; if (!buffer) return bytes_to_copy; if (bytes_to_copy > buffer_size){ bytes_to_copy = buffer_size; } memcpy(buffer, &att_value[offset], bytes_to_copy); return bytes_to_copy; } // write requests static void att_write_update_attribute(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t * value_data, uint16_t value_len){ UNUSED(con_handle); switch (attribute_handle){ case ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE: memcpy(gap_device_name, value_data, value_len); gap_device_name[value_len]=0; printf("Setting device name to '%s'\n", gap_device_name); break; case ATT_CHARACTERISTIC_GAP_APPEARANCE_01_VALUE_HANDLE: gap_appearance = little_endian_read_16(value_data, 0); printf("Setting appearance to 0x%04x'\n", gap_appearance); break; case ATT_CHARACTERISTIC_GAP_PERIPHERAL_PRIVACY_FLAG_01_VALUE_HANDLE: gap_privacy = value_data[0]; printf("Setting privacy to 0x%04x'\n", gap_privacy); update_advertisements(); break; case ATT_CHARACTERISTIC_GAP_RECONNECTION_ADDRESS_01_VALUE_HANDLE: reverse_bd_addr(value_data, gap_reconnection_address); printf("Setting Reconnection Address to %s\n", bd_addr_to_str(gap_reconnection_address)); break; case ATT_CHARACTERISTIC_FFFB_01_VALUE_HANDLE: signed_write_characteristic_value = value_data[0]; break; default: break; } } static int att_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ printf("WRITE Callback, handle %04x, mode %u, offset %u, data: ", attribute_handle, transaction_mode, offset); printf_hexdump(buffer, buffer_size); // lookup UUID for attribute handle uint16_t uuid16 = att_uuid_for_handle(attribute_handle); uint16_t device_name_len; if (uuid16){ printf("- Resolved to UUID %04x\n", uuid16); switch (uuid16){ case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION: handle = con_handle; client_configuration_handle = attribute_handle; client_configuration = buffer[0]; printf("- Client Configuration set to %u for handle %04x\n", client_configuration, client_configuration_handle); return 0; // ok case GAP_APPEARANCE_UUID: printf("Write GAP Appearance\n"); return 0; case GAP_DEVICE_NAME_UUID: device_name_len = btstack_min(buffer_size + 1, sizeof(gap_device_name) - 1); memcpy(gap_device_name, buffer, buffer_size); gap_device_name[buffer_size]=0; printf("Setting device name to '%s'\n", gap_device_name); return 0; default: break; } } // check transaction mode int attributes_index; int writes_index; switch (transaction_mode){ case ATT_TRANSACTION_MODE_NONE: attributes_index = att_attribute_for_handle(attribute_handle); if (attributes_index < 0){ attributes_index = att_attribute_for_handle(0); if (attributes_index < 0) return 0; // ok, but we couldn't store it (our fault) printf("- Setup new attribute buffer for UUID %04x, long %u, index %u\n", uuid16, att_default_value_long, attributes_index); att_attributes[attributes_index].handle = attribute_handle; // not written before uint8_t * att_value; uint16_t att_value_len; if (att_default_value_long){ att_value = (uint8_t*) default_value_long; att_value_len = strlen(default_value_long); } else { att_value = (uint8_t*) default_value_short; att_value_len = strlen(default_value_short); } att_attributes[attributes_index].len = att_value_len; } if (buffer_size > att_attributes[attributes_index].len) return ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH; att_attributes[attributes_index].len = buffer_size; memcpy(att_attributes[attributes_index].value, buffer, buffer_size); // commit changes att_write_update_attribute(con_handle, attribute_handle, buffer, buffer_size); break; case ATT_TRANSACTION_MODE_ACTIVE: writes_index = att_write_queue_for_handle(attribute_handle); if (writes_index < 0) return ATT_ERROR_PREPARE_QUEUE_FULL; if (offset > att_write_queues[writes_index].len) return ATT_ERROR_INVALID_OFFSET; if (buffer_size + offset > ATT_VALUE_MAX_LEN) return ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH; // check offset for known attributes switch (attribute_handle){ case ATT_CHARACTERISTIC_GAP_APPEARANCE_01_VALUE_HANDLE: if (offset > 1) return ATT_ERROR_INVALID_OFFSET; if (offset + buffer_size > 2) return ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH; break; default: break; } att_write_queues[writes_index].len = buffer_size + offset; memcpy(&(att_write_queues[writes_index].value[offset]), buffer, buffer_size); break; case ATT_TRANSACTION_MODE_EXECUTE: for (writes_index=0 ; writes_index '9') return; printf("%c", c); fflush(stdout); ui_passkey = ui_passkey * 10 + c - '0'; ui_digits_for_passkey--; if (ui_digits_for_passkey == 0){ printf("\nSending Passkey '%06x'\n", ui_passkey); sm_passkey_input(handle, ui_passkey); } return; } int i; switch (c){ case 'a': printf("a - advertisements off\n"); gap_advertisements = 0; gap_advertisements_enable(gap_advertisements); // show_usage(); break; case 'A': printf("A - advertisements on\n"); gap_advertisements = 1; gap_advertisements_enable(gap_advertisements); // show_usage(); break; case 'b': printf("b - bondable off\n"); gap_bondable = 0; gap_set_bondable_mode(gap_bondable); sm_set_authentication_requirements(SM_AUTHREQ_NO_BONDING); break; case 'B': printf("B - bondable on\n"); gap_bondable = 1; gap_set_bondable_mode(gap_bondable); sm_set_authentication_requirements(SM_AUTHREQ_BONDING); break; case 'c': printf("c - connectable off\n"); gap_connectable = 0; update_advertisements(); break; case 'C': printf("C - connectable on\n"); gap_connectable = 1; update_advertisements(); break; case 'd': printf("d - discoverable off\n"); gap_discoverable = 0; update_advertisements(); break; case 'D': printf("D - discoverable on\n"); gap_discoverable = 1; update_advertisements(); break; case 't': printf("t - limited discoverable off\n"); hci_send_cmd(&hci_write_current_iac_lap_two_iacs, 2, GAP_IAC_GENERAL_INQUIRY, GAP_IAC_GENERAL_INQUIRY); gap_limited_discoverable = 0; update_advertisements(); break; case 'T': printf("T - limited discoverable on\n"); gap_limited_discoverable = 1; hci_send_cmd(&hci_write_current_iac_lap_two_iacs, 2, GAP_IAC_LIMITED_INQUIRY, GAP_IAC_GENERAL_INQUIRY); update_advertisements(); break; case 'r': printf("r - Non-Resolvable Private Address\n"); gap_random = 1; gap_random_address_set_mode(GAP_RANDOM_ADDRESS_NON_RESOLVABLE); update_advertisements(); break; case 'R': printf("R - Resolvable Private Addressn\n"); gap_random = 1; gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE); update_advertisements(); break; case 'x': printf("x - directed connectable off\n"); gap_directed_connectable = 0; update_advertisements(); break; case 'X': printf("X - directed connectable on\n"); gap_directed_connectable = 1; update_advertisements(); break; case 'y': printf("y - scannable off\n"); gap_scannable = 0; update_advertisements(); break; case 'Y': printf("Y - scannable on\n"); gap_scannable = 1; update_advertisements(); break; case 'q': printf("Add PTS to whitelist"); gap_whitelist_add(tester_address_type, tester_address); break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': advertisement_index = c - '0'; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '+': advertisement_index = 10; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '-': advertisement_index = 11; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '&': advertisement_index = 12; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '=': advertisement_index = 13; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '/': advertisement_index = 14; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '#': advertisement_index = 15; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case '*': advertisement_index = 16; printf("%s\n", (char*)adv_name[advertisement_index]); update_advertisements(); break; case 's': printf("SM: sending security request\n"); sm_send_security_request(handle); break; case 'e': sm_io_capabilities = "IO_CAPABILITY_DISPLAY_ONLY"; sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); // show_usage(); break; case 'f': sm_io_capabilities = "IO_CAPABILITY_DISPLAY_YES_NO"; sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO); // show_usage(); break; case 'g': sm_io_capabilities = "IO_CAPABILITY_NO_INPUT_NO_OUTPUT"; sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); // show_usage(); break; case 'h': sm_io_capabilities = "IO_CAPABILITY_KEYBOARD_ONLY"; sm_set_io_capabilities(IO_CAPABILITY_KEYBOARD_ONLY); // show_usage(); break; case 'i': sm_io_capabilities = "IO_CAPABILITY_KEYBOARD_DISPLAY"; sm_set_io_capabilities(IO_CAPABILITY_KEYBOARD_DISPLAY); // show_usage(); break; case 'Z': printf("Terminating connection\n"); gap_disconnect(handle); break; case 'z': printf("Sending l2cap connection update parameter request\n"); gap_request_connection_parameter_update(handle, 50, 120, 0, 550); break; case 'l': printf("Filter policy: off\n"); gap_adv_filter_policy = 0; update_advertisements(); break; case 'L': printf("Filter policy: on\n"); gap_adv_filter_policy = 3; update_advertisements(); break; case 'p': gap_privacy = 0; // show_usage(); break; case 'P': gap_privacy = 1; // show_usage(); break; case 'o': sm_have_oob_data = 0; // show_usage(); break; case 'O': sm_have_oob_data = 1; // show_usage(); break; case 'k': sm_min_key_size = 7; sm_set_encryption_key_size_range(7, 16); // show_usage(); break; case 'K': sm_min_key_size = 16; sm_set_encryption_key_size_range(16, 16); // show_usage(); break; case 'm': sm_mitm_protection = 0; update_auth_req(); // show_usage(); break; case 'M': sm_mitm_protection = 1; update_auth_req(); // show_usage(); break; case 'j': printf("Create L2CAP Connection to %s\n", bd_addr_to_str(tester_address)); gap_connect(tester_address, tester_address_type); break; case 'J': printf("Secure Connections Only Mode active\n"); gap_set_secure_connections_only_mode(true); sm_set_secure_connections_only_mode(true); break; case 'n': printf("Switch to dynamic database\n"); dynamic_db = true; att_set_db(att_db_util_get_address()); att_db_util_add_service_uuid16(ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE); att_db_util_add_characteristic_uuid16(0x2b2a, ATT_PROPERTY_READ | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0); att_db_util_hash_calc(&gatt_aes_cmac_context, gatt_database_hash, &gatt_hash_calculated, NULL); break; case 'N': printf("Adding Characteristic to dynamic database\n"); att_db_util_add_service_uuid16(0xfff0); att_db_util_add_characteristic_uuid16(0xfff1, ATT_PROPERTY_READ | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0); att_db_util_hash_calc(&gatt_aes_cmac_context, gatt_database_hash, &gatt_hash_calculated, NULL); break; case 'u': printf("Activate Security Mode 2\n"); gap_set_security_mode(GAP_SECURITY_MODE_2); break; case 'F': printf("Drop bonding information\n"); for (i=0;i