diff --git a/example/embedded/Makefile.inc b/example/embedded/Makefile.inc index 2575f132e..749a5b80b 100644 --- a/example/embedded/Makefile.inc +++ b/example/embedded/Makefile.inc @@ -72,6 +72,7 @@ EXAMPLES = \ spp_and_le_counter \ spp_counter \ spp_streamer \ + gap_le_advertisements \ EXAMPLES_USING_LE = \ ancs_client \ @@ -80,6 +81,7 @@ EXAMPLES_USING_LE = \ le_counter \ le_streamer \ spp_and_le_counter \ + gap_le_advertisements \ # .o for .c CORE_OBJ = $(CORE:.c=.o) @@ -163,6 +165,9 @@ led_counter: ${CORE_OBJ} ${COMMON_OBJ} led_counter.c ble_client: ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_CLIENT_OBJ} ${SM_REAL} ble_client.c ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ +gap_le_advertisements: ${CORE_OBJ} ${COMMON_OBJ} ad_parser.c gap_le_advertisements.c + ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ + clean: rm -f ${EXAMPLES} diff --git a/example/embedded/gap_le_advertisements.c b/example/embedded/gap_le_advertisements.c new file mode 100644 index 000000000..253527346 --- /dev/null +++ b/example/embedded/gap_le_advertisements.c @@ -0,0 +1,244 @@ +/* + * 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_advertisements): GAP LE Advertisements Dumper + * + * @text This example shows how to scan for advertisements and how to parse them. + * + */ + // ***************************************************************************** + + +#include +#include +#include +#include + +#include + +#include "btstack_memory.h" +#include "hci.h" +#include "ad_parser.h" + + +static void handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size); + +static void gap_le_advertisements_setup(){ + hci_register_packet_handler(handle_hci_event); +} + +static char * ad_types[] = { + "", + "Flags", + "Incomplete List of 16-bit Service Class UUIDs", + "Complete List of 16-bit Service Class UUIDs", + "Incomplete List of 32-bit Service Class UUIDs", + "Complete List of 32-bit Service Class UUIDs", + "Incomplete List of 128-bit Service Class UUIDs", + "Complete List of 128-bit Service Class UUIDs", + "Shortened Local Name", + "Complete Local Name", + "Tx Power Level", + "", + "", + "Class of Device", + "Simple Pairing Hash C", + "Simple Pairing Randomizer R", + "Device ID", + "Security Manager TK Value", + "Slave Connection Interval Range", + "", + "List of 16-bit Service Solicitation UUIDs", + "List of 128-bit Service Solicitation UUIDs", + "Service Data", + "Public Target Address", + "Random Target Address", + "Appearance", + "Advertising Interval" +}; + +static char * flags[] = { + "LE Limited Discoverable Mode", + "LE General Discoverable Mode", + "BR/EDR Not Supported", + "Simultaneous LE and BR/EDR to Same Device Capable (Controller)", + "Simultaneous LE and BR/EDR to Same Device Capable (Host)", + "Reserved", + "Reserved", + "Reserved" +}; + +void dump_advertisement_data(uint8_t * adv_data, uint8_t adv_size){ + ad_context_t context; + + for (ad_iterator_init(&context, adv_size, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ + uint8_t data_type = ad_iterator_get_data_type(&context); + uint8_t size = ad_iterator_get_data_len(&context); + uint8_t * data = ad_iterator_get_data(&context); + + if (data_type > 0 && data_type < 0x1B){ + printf("%s: ", ad_types[data_type]); + } + int i; + // Assigned Numbers GAP + switch (data_type){ + case 0x01: // Flags + // show only first octet, ignore rest + printf("\n"); + for (i=0; i<8;i++){ + if (data[0] & (1<