libusb: add gap_le_connect_to_1111 example for testing outgoing le secure connection pairing

This commit is contained in:
Matthias Ringwald 2016-05-13 16:52:15 +02:00
parent bccf5e67f5
commit 7cef8eada7
2 changed files with 163 additions and 0 deletions

View File

@ -7,6 +7,12 @@ COMMON += hci_transport_h2_libusb.c btstack_run_loop_posix.c btstack_link_key_db
include ${BTSTACK_ROOT}/example/Makefile.inc
# custom
EXAMPLES += gap_le_connect_to_1111
gap_le_connect_to_1111: ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${SM_REAL_OBJ} ad_parser.o gap_le_connect_to_1111.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
# CC = gcc-fsf-4.9
CFLAGS += -g -Wall -Wmissing-prototypes -Wstrict-prototypes -Wshadow -Werror
# CFLAGS += -Werror

View File

@ -0,0 +1,157 @@
/*
* 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(gap_le_connect_to_1111): GAP LE Advertisements Dumper
*
* @text This example shows how to scan and parse advertisements.
*
*/
// *****************************************************************************
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btstack.h"
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t sm_event_callback_registration;
/* @section GAP LE setup for receiving advertisements
*
* @text GAP LE advertisements are received as custom HCI events of the
* GAP_EVENT_ADVERTISING_REPORT type. To receive them, you'll need to register
* the HCI packet handler, as shown in Listing GAPLEAdvSetup.
*/
/* LISTING_START(GAPLEAdvSetup): Setting up GAP LE client for receiving advertisements */
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void gap_le_connect_to_1111_setup(void){
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
// setup le device db
le_device_db_init();
// setup SM: Display only
sm_init();
sm_event_callback_registration.callback = &packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
// Numeric Comparison
sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
// Passkey entry initiator enter, responder displays
// sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
#ifdef ENABLE_LE_SECURE_CONNECTIONS
sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
#endif
}
/* LISTING_END */
/* LISTING_END */
/* @section HCI packet handler
*
* @text The HCI packet handler has to start the scanning,
* and to handle received advertisements. Advertisements are received
* as HCI event packets of the GAP_EVENT_ADVERTISING_REPORT type,
* see Listing GAPLEAdvPacketHandler.
*/
/* LISTING_START(GAPLEAdvPacketHandler): Scanning and receiving advertisements */
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return;
hci_con_handle_t con_handle;
switch (hci_event_packet_get_type(packet)) {
case BTSTACK_EVENT_STATE:
// BTstack activated, get started
if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
printf("Start scaning!\n");
gap_set_scan_parameters(1,0x0030, 0x0030);
gap_start_scan();
}
break;
case GAP_EVENT_ADVERTISING_REPORT:{
bd_addr_t address;
gap_event_advertising_report_get_address(packet, address);
uint8_t address_type = gap_event_advertising_report_get_address_type(packet);
uint8_t length = gap_event_advertising_report_get_data_length(packet);
const uint8_t * data = gap_event_advertising_report_get_data(packet);
printf("Advertisement event: addr-type %u, addr %s, data[%u] ",
address_type, bd_addr_to_str(address), length);
printf_hexdump(data, length);
if (!ad_data_contains_uuid16(length, (uint8_t *) data, 0x1111)) break;
printf("Found remtoe with UUID 1111, connecting...\n");
gap_stop_scan();
gap_connect(address,address_type);
break;
}
case HCI_EVENT_LE_META:
// wait for connection complete
if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break;
con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
// start pairing
sm_request_pairing(con_handle);
break;
default:
break;
}
}
/* LISTING_END */
int btstack_main(void);
int btstack_main(void)
{
gap_le_connect_to_1111_setup();
// turn on!
hci_power_control(HCI_POWER_ON);
return 0;
}
/* EXAMPLE_END */