From dfbe5d537504d2b2adc8cfa1db69b018f6502728 Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Mon, 10 Aug 2020 15:52:54 +0100 Subject: [PATCH] Got a Bluetooth connection working --- part7-bluetooth/bt.c | 71 ++++++++++++++++++++++++++------- part7-bluetooth/bt.h | 1 + part7-bluetooth/kernel.c | 85 ++++++++++++++++++++++++---------------- 3 files changed, 108 insertions(+), 49 deletions(-) diff --git a/part7-bluetooth/bt.c b/part7-bluetooth/bt.c index 1e4718b..d51291d 100644 --- a/part7-bluetooth/bt.c +++ b/part7-bluetooth/bt.c @@ -39,7 +39,6 @@ void bt_writeByte(char byte) { while ((mmio_read(ARM_UART0_FR) & 0x20) != 0); mmio_write(ARM_UART0_DR, (unsigned int)byte); - uart_byte(byte); } void bt_flushrx() @@ -83,6 +82,7 @@ enum { HCI_COMMAND_PKT = 0x01, HCI_EVENT_PKT = 0x04, COMMAND_COMPLETE_CODE = 0x0e, + CONNECT_COMPLETE_CODE = 0x0f, LL_SCAN_ACTIVE = 0x01, LL_ADV_NONCONN_IND = 0x03 @@ -94,23 +94,35 @@ int hciCommandBytes(unsigned char *opcodebytes, unsigned char *data, unsigned ch { unsigned char c=0; - uart_writeText("HCI_START\n"); bt_writeByte(HCI_COMMAND_PKT); bt_writeByte(opcodebytes[0]); bt_writeByte(opcodebytes[1]); bt_writeByte(length); - uart_writeText("\n______ DATA:\n"); while (c++= 2 && buf[0] == LE_ADREPORT_CODE) { - unsigned char numreports = buf[1]; + if (data_len >= 2) { + if (buf[0] == LE_ADREPORT_CODE) { + unsigned char numreports = buf[1]; - if (numreports == 1) { - unsigned char event_type = buf[2]; + if (numreports == 1) { + unsigned char event_type = buf[2]; - if (event_type == 0x00) { - unsigned char buf_len = buf[10]; - unsigned char ad_len = buf[11]; + if (event_type == 0x00) { + unsigned char buf_len = buf[10]; + unsigned char ad_len = buf[11]; - if (ad_len < data_len && - buf_len + 11 == data_len - 1) { - for (int c=9;c>=4;c--) uart_byte(buf[c]); + if (ad_len < data_len && buf_len + 11 == data_len - 1) { + for (int c=9;c>=4;c--) uart_byte(buf[c]); + buf += 11; - buf += 11; + unsigned char rssi = buf[buf_len]; + uart_writeText("-> rssi("); uart_hex(rssi); uart_writeText(")"); - unsigned char rssi = buf[buf_len]; - uart_writeText("-> rssi("); uart_hex(rssi); uart_writeText(")"); + do { + ad_len = buf[0]; + unsigned char ad_type = buf[1]; + buf += 2; - do { - ad_len = buf[0]; - unsigned char ad_type = buf[1]; - buf += 2; + if (ad_len >= 2) { + uart_writeText(" -> adtype("); uart_hex(ad_type); uart_writeText(":"); uart_hex(ad_len); uart_writeText(")"); - if (ad_len >= 2) { - uart_writeText(" -> adtype("); uart_hex(ad_type); uart_writeText(":"); uart_hex(ad_len); uart_writeText(")"); - - if (ad_type == 0x09) { - unsigned int d=0; - uart_writeText(" -> "); - while (d "); + while (d=0;c--) uart_byte(local_addr[c]); uart_writeText("\n"); // Start scanning for devices around us + /* uart_writeText("startActiveScanning()\n"); setLEeventmask(0xff); startActiveScanning(); + */ + + uart_writeText("connect()\n"); + connect(); // Get the Eddystone beacon going /* @@ -149,6 +162,10 @@ void main() startActiveAdvertising(); */ + uart_writeText("Waiting for connection...\n"); + while (!connected) bt_update(); + uart_writeText("Connected - handle "); uart_hex(connection_handle); uart_writeText("\n"); + uart_writeText("Waiting for input...\n"); while (1) { uart_update();