/* * 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 * */ // ***************************************************************************** /* EXAMPLE_START(ble_peripheral): BLE Peripheral Demo * */ // ***************************************************************************** #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 "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" #define HEARTBEAT_PERIOD_MS 1000 // test profile #include "ble_peripheral_test.h" enum { DISABLE_ADVERTISEMENTS = 1 << 0, SET_ADVERTISEMENT_PARAMS = 1 << 1, SET_ADVERTISEMENT_DATA = 1 << 2, SET_SCAN_RESPONSE_DATA = 1 << 3, ENABLE_ADVERTISEMENTS = 1 << 4, }; static uint16_t todos = 0; ///------ static int advertisements_enabled = 0; static int gap_advertisements = 0; static int gap_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 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 uint16_t handle = 0; static void app_run(void); static void show_usage(void); static void update_advertisements(void); // static bd_addr_t tester_address = {0x00, 0x1B, 0xDC, 0x06, 0x07, 0x5F}; static bd_addr_t tester_address = {0x00, 0x1B, 0xDC, 0x07, 0x32, 0xef}; static int tester_address_type = 0; // general discoverable flags static uint8_t adv_general_discoverable[] = { 2, 01, 02 }; // 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, 0x1a, 0x00, 0x80}; 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), }; static int advertisement_index = 2; // att write queue engine static const char default_value_long[] = "abcdefghijklmnopqrstuvwxyz"; static const char default_value_short[] = "a"; #define ATT_VALUE_MAX_LEN 26 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 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 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); switch (attribute_handle){ case ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE: 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; case ATT_CHARACTERISTIC_GAP_APPEARANCE_01_VALUE_HANDLE: gap_appearance = little_endian_read_16(buffer, 0); printf("Setting appearance to 0x%04x'\n", gap_appearance); return 0; case ATT_CHARACTERISTIC_GAP_PERIPHERAL_PRIVACY_FLAG_01_VALUE_HANDLE: gap_privacy = buffer[0]; printf("Setting privacy to 0x%04x'\n", gap_privacy); update_advertisements(); return 0; case ATT_CHARACTERISTIC_GAP_RECONNECTION_ADDRESS_01_VALUE_HANDLE: reverse_bd_addr(buffer, gap_reconnection_address); printf("Setting Reconnection Address to %s\n", bd_addr_to_str(gap_reconnection_address)); return 0; default: break; } uint16_t uuid16 = att_uuid_for_handle(attribute_handle); if (uuid16){ printf("Resolved to UUID %04x\n", uuid16); switch (uuid16){ case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION: client_configuration = buffer[0]; client_configuration_handle = attribute_handle; printf("Client Configuration set to %u for handle %04x\n", client_configuration, client_configuration_handle); return 0; // ok 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) 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); 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; 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; } switch (c){ case 'a': gap_advertisements = 0; update_advertisements(); show_usage(); break; case 'A': gap_advertisements = 1; update_advertisements(); show_usage(); break; case 'b': gap_bondable = 0; sm_set_authentication_requirements(SM_AUTHREQ_NO_BONDING); show_usage(); break; case 'B': gap_bondable = 1; sm_set_authentication_requirements(SM_AUTHREQ_BONDING); show_usage(); break; case 'c': gap_connectable = 0; update_advertisements(); break; case 'C': gap_connectable = 1; update_advertisements(); break; case 'd': gap_discoverable = 0; update_advertisements(); break; case 'D': gap_discoverable = 1; update_advertisements(); break; case 'r': gap_random = 0; update_advertisements(); break; case 'R': gap_random = 1; update_advertisements(); break; case 'x': gap_directed_connectable = 0; update_advertisements(); break; case 'X': gap_directed_connectable = 1; update_advertisements(); break; case 'y': gap_scannable = 0; update_advertisements(); break; case 'Y': gap_scannable = 1; update_advertisements(); 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'; update_advertisements(); break; case '+': advertisement_index = 10; update_advertisements(); break; case '-': advertisement_index = 11; update_advertisements(); break; case '&': advertisement_index = 12; update_advertisements(); break; case '=': advertisement_index = 13; update_advertisements(); break; case '/': advertisement_index = 14; update_advertisements(); break; case '#': advertisement_index = 15; 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 't': printf("Terminating connection\n"); hci_send_cmd(&hci_disconnect, handle, 0x13); break; case 'z': printf("Sending l2cap connection update parameter request\n"); gap_request_connection_parameter_update(handle, 50, 120, 0, 550); break; case 'l': att_default_value_long = 0; show_usage(); break; case 'L': att_default_value_long = 1; show_usage(); 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)); hci_send_cmd(&hci_le_create_connection, 1000, // scan interval: 625 ms 1000, // scan interval: 625 ms 0, // don't use whitelist 0, // peer address type: public tester_address, // remote bd addr tester_address_type, // random or public 80, // conn interval min 80, // conn interval max (3200 * 0.625) 0, // conn latency 2000, // supervision timeout 0, // min ce length 1000 // max ce length ); break; default: show_usage(); break; } return; } static int get_oob_data_callback(uint8_t addres_type, bd_addr_t addr, uint8_t * oob_data){ if(!sm_have_oob_data) return 0; memcpy(oob_data, sm_oob_data, 16); return 1; } int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]){ printf("BTstack LE Peripheral starting up...\n"); // set up l2cap_le l2cap_init(); // setup le device db le_device_db_init(); // setup SM: Display only sm_init(); sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); sm_set_authentication_requirements( SM_AUTHREQ_BONDING | SM_AUTHREQ_MITM_PROTECTION); // setup ATT server att_server_init(profile_data, att_read_callback, att_write_callback); att_write_queue_init(); att_attributes_init(); att_server_register_packet_handler(app_packet_handler); att_dump_attributes(); btstack_stdin_setup(stdin_process); gap_random_address_set_update_period(5000); gap_random_address_set_mode(GAP_RANDOM_ADDRESS_RESOLVABLE); strcpy(gap_device_name, "BTstack"); sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); sm_io_capabilities = "IO_CAPABILITY_NO_INPUT_NO_OUTPUT"; sm_set_authentication_requirements(0); sm_register_oob_data_callback(get_oob_data_callback); sm_set_encryption_key_size_range(sm_min_key_size, 16); sm_test_set_irk(test_irk); // set one-shot timer heartbeat.process = &heartbeat_handler; btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); btstack_run_loop_add_timer(&heartbeat); // turn on! hci_power_control(HCI_POWER_ON); return 0; } /* EXAMPLE_END */