From 373ed0c3786d2727e3b22942f2473b631d0d1e45 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Sun, 11 Nov 2012 21:28:51 +0000 Subject: [PATCH] added gap inquiry example --- MSP-EXP430F5438-CC256x/example/Makefile | 8 +- MSP-EXP430F5438-CC256x/example/gap_inquiry.c | 239 +++++++++++++++++++ 2 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 MSP-EXP430F5438-CC256x/example/gap_inquiry.c diff --git a/MSP-EXP430F5438-CC256x/example/Makefile b/MSP-EXP430F5438-CC256x/example/Makefile index 2fce4eece..a6a03bc86 100644 --- a/MSP-EXP430F5438-CC256x/example/Makefile +++ b/MSP-EXP430F5438-CC256x/example/Makefile @@ -2,7 +2,8 @@ # Makefile for MSP-EXP430F5438 demos with PAN1315, PAN1317, PAN1325, PAN1323, or PAN1327 Bluetooth modules # # For the PAN1315/25 modules, please use bluetooth_init_cc2560_2.42.c in COMMON below -# +# Please check instructions at https://code.google.com/p/btstack/wiki/CC256x on where to download the init scripts and how to convert them to *.c files. +# # mspgcc is used: http://sourceforge.net/apps/mediawiki/mspgcc/index.php?title=MSPGCC_Wiki # @@ -53,7 +54,7 @@ LCD_OBJ = $(LCD:.c=.o) # create firmware image from common objects and example source file -all: hid_demo.hex led_counter.hex spp_accel.hex spp_counter.hex spp_flowcontrol.hex +all: hid_demo.hex led_counter.hex spp_accel.hex spp_counter.hex spp_flowcontrol.hex gap_inquiry.hex led_counter.out: ${CORE_OBJ} led_counter.o ${CC} $^ ${LDFLAGS} -o $@ @@ -70,6 +71,9 @@ spp_counter.out: ${CORE_OBJ} ${COMMON_OBJ} spp_counter.o ../firmware/hal_adc.o spp_flowcontrol.out: ${CORE_OBJ} ${COMMON_OBJ} ${LCD_OBJ} spp_flowcontrol.o ../firmware/hal_adc.o ${CC} $^ ${LDFLAGS} -o $@ +gap_inquiry.out: ${CORE_OBJ} ${COMMON_OBJ} ${LCD_OBJ} gap_inquiry.o ../firmware/hal_adc.o + ${CC} $^ ${LDFLAGS} -o $@ + clean: rm -f $ *.o *.out *.hex ../driver/*.o ../../src/*.o ../src/*.o ../firmware/*.o ${BTSTACK_ROOT}/chipset-cc256x/*.o ${BTSTACK_ROOT}/src/*.o diff --git a/MSP-EXP430F5438-CC256x/example/gap_inquiry.c b/MSP-EXP430F5438-CC256x/example/gap_inquiry.c new file mode 100644 index 000000000..8e6b68172 --- /dev/null +++ b/MSP-EXP430F5438-CC256x/example/gap_inquiry.c @@ -0,0 +1,239 @@ +//***************************************************************************** +// +// minimal setup for HCI code +// +//***************************************************************************** +#include +#include +#include +#include + +#include + +#include "bt_control_cc256x.h" +#include "hal_board.h" +#include "hal_compat.h" +#include "hal_usb.h" + +#include +#include + +#include "hci.h" +#include "btstack_memory.h" +#include "remote_device_db.h" +#include "config.h" + + +#define MAX_DEVICES 10 +struct device { + bd_addr_t address; + uint16_t clockOffset; + uint32_t classOfDevice; + uint8_t pageScanRepetitionMode; + uint8_t rssi; + uint8_t state; // 0 empty, 1 found, 2 remote name tried, 3 remote name found +}; + +#define INQUIRY_INTERVAL 5 +struct device devices[MAX_DEVICES]; +int deviceCount = 0; + +enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE, DEVICE_NAME, REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED} ; +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; + 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 %u, 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"); + } + } + if (has_more_remote_name_requests()){ + do_next_remote_name_request(); + break; + } + start_scan(); + break; + + default: + break; + } + break; + + default: + break; + } +} + +void setup(void){ + // stop watchdog timer + WDTCTL = WDTPW + WDTHOLD; + + //Initialize clock and peripherals + halBoardInit(); + halBoardStartXT1(); + halBoardSetSystemClock(SYSCLK_16MHZ); + + // init debug UART + halUsbInit(); + + // init LEDs + LED_PORT_OUT |= LED_1 | LED_2; + LED_PORT_DIR |= LED_1 | LED_2; + + /// GET STARTED with BTstack /// + btstack_memory_init(); + run_loop_init(RUN_LOOP_EMBEDDED); + + // init HCI + hci_transport_t * transport = hci_transport_h4_dma_instance(); + bt_control_t * control = bt_control_cc256x_instance(); + hci_uart_config_t * config = hci_uart_config_cc256x_instance(); + 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; +} +