From bd03b42fdaacd2c074d9b69a7e6a7a792456e3c7 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Wed, 22 Jan 2014 22:25:56 +0000 Subject: [PATCH] added gap_inquiry --- example/libusb/Makefile | 7 +- example/libusb/gap_inquiry.c | 212 +++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 example/libusb/gap_inquiry.c diff --git a/example/libusb/Makefile b/example/libusb/Makefile index bd9ceaa80..4499fc47e 100644 --- a/example/libusb/Makefile +++ b/example/libusb/Makefile @@ -55,7 +55,9 @@ ATT_OBJ = $(ATT:.c=.o) # create firmware image from common objects and example source file -all: ../../include/btstack/version.h ble_client sdp_rfcomm_query sdp_general_query spp_counter ble_peripheral ble_peripheral_sm_minimal +all: ../../include/btstack/version.h ble_client sdp_rfcomm_query sdp_general_query spp_counter ble_peripheral \ + ble_peripheral_sm_minimal gap_inquiry gap_dedicated_bonding + # ble_client_uart #spp-usb l2cap-server-usb l2cap-client-usb l2cap-server-uart l2cap-client-uart ../../include/btstack/version.h: @@ -76,6 +78,9 @@ spp_counter_ssp: ${CORE_OBJ} ${COMMON_OBJ} spp_counter_ssp.c gap_dedicated_bonding: ${CORE_OBJ} ${COMMON_OBJ} gap_dedicated_bonding.c ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ +gap_inquiry: ${CORE_OBJ} ${COMMON_OBJ} gap_inquiry.c + ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ + # compile .ble description profile.h: profile.gatt python ${BTSTACK_ROOT}/ble/compile-gatt.py $< $@ diff --git a/example/libusb/gap_inquiry.c b/example/libusb/gap_inquiry.c new file mode 100644 index 000000000..832a523f0 --- /dev/null +++ b/example/libusb/gap_inquiry.c @@ -0,0 +1,212 @@ +//***************************************************************************** +// +// minimal setup for HCI code +// +//***************************************************************************** + +#include +#include +#include +#include + +#include "btstack-config.h" + +#include +#include + +#include "debug.h" +#include "btstack_memory.h" +#include "hci.h" +#include "hci_dump.h" + +#define MAX_DEVICES 10 +enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED }; +struct device { + bd_addr_t address; + uint16_t clockOffset; + uint32_t classOfDevice; + uint8_t pageScanRepetitionMode; + uint8_t rssi; + enum DEVICE_STATE state; +}; + +#define INQUIRY_INTERVAL 5 +struct device devices[MAX_DEVICES]; +int deviceCount = 0; + + +enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ; +enum STATE state = INIT; + + +int getDeviceIndexForAddress( bd_addr_t addr){ + int j; + for (j=0; j< deviceCount; j++){ + if (BD_ADDR_CMP(addr, devices[j].address) == 0){ + return j; + } + } + return -1; +} + +void start_scan(void){ + printf("Starting inquiry scan..\n"); + hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); +} + +int has_more_remote_name_requests(void){ + int i; + for (i=0;i= 0) continue; // already in our list + + memcpy(devices[deviceCount].address, addr, 6); + devices[deviceCount].pageScanRepetitionMode = packet [3 + numResponses*(6) + i*1]; + if (event == HCI_EVENT_INQUIRY_RESULT){ + devices[deviceCount].classOfDevice = READ_BT_24(packet, 3 + numResponses*(6+1+1+1) + i*3); + devices[deviceCount].clockOffset = READ_BT_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff; + devices[deviceCount].rssi = 0; + } else { + devices[deviceCount].classOfDevice = READ_BT_24(packet, 3 + numResponses*(6+1+1) + i*3); + devices[deviceCount].clockOffset = READ_BT_16(packet, 3 + numResponses*(6+1+1+3) + i*2) & 0x7fff; + devices[deviceCount].rssi = packet [3 + numResponses*(6+1+1+3+2) + i*1]; + } + devices[deviceCount].state = REMOTE_NAME_REQUEST; + printf("Device found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n", bd_addr_to_str(addr), + devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode, + devices[deviceCount].clockOffset, devices[deviceCount].rssi); + deviceCount++; + } + break; + + case HCI_EVENT_INQUIRY_COMPLETE: + for (i=0;i= 0) { + if (packet[2] == 0) { + printf("Name: '%s'\n", &packet[9]); + devices[index].state = REMOTE_NAME_FETCHED; + } else { + printf("Failed to get name: page timeout\n"); + } + } + continue_remote_names(); + break; + + default: + break; + } + break; + + default: + break; + } +} + +void setup(void){ + /// GET STARTED with BTstack /// + btstack_memory_init(); + run_loop_init(RUN_LOOP_POSIX); + + // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT + hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); + + // init HCI + hci_transport_t * transport = hci_transport_usb_instance(); + hci_uart_config_t * config = NULL; + bt_control_t * control = NULL; + remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; + + hci_init(transport, config, control, remote_db); + hci_register_packet_handler(packet_handler); +} + +// main == setup +int main(void) +{ + setup(); + + // turn on! + hci_power_control(HCI_POWER_ON); + + // go! + run_loop_execute(); + + // happy compiler! + return 0; +} +