From 64d6fc5de0b6c2fbe979dea489c057d9106a7f7b Mon Sep 17 00:00:00 2001 From: ktownsend Date: Wed, 18 Dec 2013 18:02:45 +0000 Subject: [PATCH] Cleaned up example code (iBeacon only) --- hw/nrf51822.cpp | 20 ++++++++------ main.cpp | 73 ++++++++----------------------------------------- 2 files changed, 22 insertions(+), 71 deletions(-) diff --git a/hw/nrf51822.cpp b/hw/nrf51822.cpp index ac4c257..0bc8e6c 100644 --- a/hw/nrf51822.cpp +++ b/hw/nrf51822.cpp @@ -11,7 +11,7 @@ void nRF51822::uartCallback(void) /* ToDo: Check responses and set a flag for success/error/etc. */ /* Read serial to clear the RX interrupt */ - uart.getc(); + printf("%c", uart.getc()); } /**************************************************************************/ @@ -88,6 +88,7 @@ ble_error_t nRF51822::setAdvertising(GapAdvertisingParams & params, GapAdvertisi /* Make sure we support the advertising type */ if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { + /* ToDo: This requires a propery security implementation, etc. */ return BLE_ERROR_NOT_IMPLEMENTED; } @@ -132,13 +133,19 @@ ble_error_t nRF51822::setAdvertising(GapAdvertisingParams & params, GapAdvertisi return BLE_ERROR_BUFFER_OVERFLOW; } - /* Make sure we don't exceed the scan response payload length */ + /* Check the scan response payload limits */ if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)) { + /* Check if we're within the upper limit */ if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { return BLE_ERROR_BUFFER_OVERFLOW; } + /* Make sure we have a payload! */ + if (advData.getPayloadLen() == 0) + { + return BLE_ERROR_PARAM_OUT_OF_RANGE; + } } /* ToDo: Perform some checks on the payload, for example the Scan Response can't */ @@ -148,23 +155,18 @@ ble_error_t nRF51822::setAdvertising(GapAdvertisingParams & params, GapAdvertisi /* 1.) Send advertising params, Command IDs = 0x000C, 0x000D, 0x000E */ /* A.) Command ID = 0x000C, Advertising Interval, uint16_t */ - printf("10 0C 00 02 %02X %02X\r\n", (uint8_t)(params.getInterval() & 0xFF), - (uint8_t)(params.getInterval() >> 8)); uart.printf("10 0C 00 02 %02X %02X\r\n", (uint8_t)(params.getInterval() & 0xFF), (uint8_t)(params.getInterval() >> 8)); /* ToDo: Check response */ wait(0.5); /* B.) Command ID = 0x000D, Advertising Timeout, uint16_t */ - printf("10 0D 00 02 %02X %02X\r\n", (uint8_t)(params.getTimeout() & 0xFF), - (uint8_t)(params.getTimeout() >> 8)); uart.printf("10 0D 00 02 %02X %02X\r\n", (uint8_t)(params.getTimeout() & 0xFF), (uint8_t)(params.getTimeout() >> 8)); /* ToDo: Check response */ wait(0.5); /* C.) Command ID = 0x000E, Advertising Type, uint8_t */ - printf("10 0E 00 01 %02X\r\n", (uint8_t)(params.getAdvertisingType())); uart.printf("10 0E 00 01 %02X\r\n", (uint8_t)(params.getAdvertisingType())); /* ToDo: Check response */ wait(0.5); @@ -188,8 +190,8 @@ ble_error_t nRF51822::setAdvertising(GapAdvertisingParams & params, GapAdvertisi /* 3.) Send scan response data, Command ID = 0x000B */ if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)) { - len = advData.getPayloadLen(); - buffer = advData.getPayload(); + len = scanResponse.getPayloadLen(); + buffer = scanResponse.getPayload(); printf("10 0B 00 %02X", len); uart.printf("10 0B 00 %02X", len); for (uint16_t i = 0; i < len; i++) diff --git a/main.cpp b/main.cpp index f7d8dfa..b02e37d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,89 +1,38 @@ #include "mbed.h" - #include "uuid.h" -#include "GattService.h" -#include "GattCharacteristic.h" #include "hw/nrf51822.h" DigitalOut myled ( LED1 ); -/* Radio HW abstraction layer */ +/* Radio HW */ nRF51822 radio; -/* Heart Rate Monitor Service */ -/* See --> https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ -GattService hrmService ( 0x180D ); -GattCharacteristic hrmRate ( 0x2A37, 2, 3, BLE_GATT_CHAR_PROPERTIES_NOTIFY ); -GattCharacteristic hrmLocation ( 0x2A39, 1, 1, BLE_GATT_CHAR_PROPERTIES_READ ); - -/* GAP Advertising Example (iBeacon) */ -GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED ); -GapAdvertisingData advData; -GapAdvertisingData scanResponse; - -uint8_t iBeaconPayload[25] = { 0x4C, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 0x00, 0x00, 0x00, 0x00, 0xC8 }; - void startBeacon(void) { + GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED ); + GapAdvertisingData advData; + GapAdvertisingData scanResponse; + + uint8_t iBeaconPayload[25] = { 0x4C, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 0x00, 0x00, 0x00, 0x00, 0xC8 }; + + /* ToDo: Check error conditions in a shared ASSERT with debug output via printf */ ble_error_t error; /* iBeacon includes the FLAG and MSD fields */ - advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED); - advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, 25); - + error = advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED); + error = advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeaconPayload, 25); + error = radio.reset(); error = radio.setAdvertising(advParams, advData, scanResponse); error = radio.start(); } -void startHRM(void) -{ - uint8_t hrmCounter = 100; - - /* Add the heart rate and sensor location chars to the HRM service */ - hrmService.addCharacteristic(hrmRate); - hrmService.addCharacteristic(hrmLocation); - - /* Reset the BLE hardware to make sure we get a clean start */ - wait(2); - radio.reset(); - - /* Add the services to the radio HAL */ - radio.addService(hrmService); - - /* Start the services so that we can start pushing data out */ - radio.start(); - - /* Set the heart rate monitor location (one time only) */ - /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ - uint8_t location = 0x01; /* Chest */ - radio.writeCharacteristic(hrmService, hrmLocation, (uint8_t*)&location, sizeof(location)); - - while(1) - { - myled = 1; - wait(0.1); - myled = 0; - wait(0.1); - - /* Update the HRM measurement */ - /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ - /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ - hrmCounter++; - if (hrmCounter == 175) hrmCounter = 100; - uint8_t bpm[2] = { 0x00, hrmCounter }; - radio.writeCharacteristic(hrmService, hrmRate, bpm, 2); - } -} - int main() { /* Give the radio some time to boot up and settle */ wait(2); - /* Only use one of these two options! */ startBeacon(); - // startHRM(); while(1); }