From 8bec31fe764b19da746e97175c4106b73463319b Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Mon, 1 Feb 2016 18:19:02 +0000 Subject: [PATCH] microbit: Additional validation on MicroBitRadio methods MicroBitRadio methods that touch internal RADIO registers now ensure that the BLE stack is not running before changing such registers. --- inc/MicroBitRadio.h | 7 ++++--- source/ble-services/MicroBitRadio.cpp | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/inc/MicroBitRadio.h b/inc/MicroBitRadio.h index 8895ed2..d34263f 100644 --- a/inc/MicroBitRadio.h +++ b/inc/MicroBitRadio.h @@ -92,7 +92,8 @@ class MicroBitRadio : MicroBitComponent * Change the transmission and reception band of the radio to the given channel * * @param band a frequency band in the range 0 - 100. Each step is 1MHz wide, based at 2400MHz. - * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range. + * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range, + * or MICROBIT_NOT_SUPPORTED if the BLE stack is running. * */ int setFrequencyBand(int band); @@ -129,7 +130,7 @@ class MicroBitRadio : MicroBitComponent * Sets the radio to listen to packets sent with the given group id. * * @param group The group to join. A micro:bit can only listen to one group ID at any time. - * @return MICROBIT_OK on success. + * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the BLE stack is running. */ int setGroup(uint8_t group); @@ -165,7 +166,7 @@ class MicroBitRadio : MicroBitComponent * The call will wait until the transmission of the packet has completed before returning. * * @param data The packet contents to transmit. - * @return MICROBIT_OK on success. + * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the BLE stack is running. */ int send(PacketBuffer *buffer); }; diff --git a/source/ble-services/MicroBitRadio.cpp b/source/ble-services/MicroBitRadio.cpp index 8f31b31..e118182 100644 --- a/source/ble-services/MicroBitRadio.cpp +++ b/source/ble-services/MicroBitRadio.cpp @@ -75,11 +75,15 @@ int MicroBitRadio::setTransmitPower(int power) * Change the transmission and reception band of the radio to the given channel * * @param band a frequency band in the range 0 - 100. Each step is 1MHz wide, based at 2400MHz. - * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range. + * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range, + * or MICROBIT_NOT_SUPPORTED if the BLE stack is running. * */ int MicroBitRadio::setFrequencyBand(int band) { + if (uBit.ble) + return MICROBIT_NOT_SUPPORTED; + if (band < 0 || band > 100) return MICROBIT_INVALID_PARAMETER; @@ -270,13 +274,17 @@ int MicroBitRadio::disable() * Sets the radio to listen to packets sent with the given group id. * * @param group The group to join. A micro:bit can only listen to one group ID at any time. - * @return MICROBIT_OK on success. + * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the BLE stack is running. */ int MicroBitRadio::setGroup(uint8_t group) { - // Record our group id locally, and also append it to the address of this device, - // to allow the RADIO module to filter for us. + if (uBit.ble) + return MICROBIT_NOT_SUPPORTED; + + // Record our group id locally this->group = group; + + // Also append it to the address of this device, to allow the RADIO module to filter for us. NRF_RADIO->PREFIX0 = (uint32_t)group; return MICROBIT_OK; @@ -357,10 +365,13 @@ PacketBuffer* MicroBitRadio::recv() * The call will wait until the transmission of the packet has completed before returning. * * @param data The packet contents to transmit. - * @return MICROBIT_OK on success. + * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the BLE stack is running. */ int MicroBitRadio::send(PacketBuffer *buffer) { + if (uBit.ble) + return MICROBIT_NOT_SUPPORTED; + if (buffer == NULL) return MICROBIT_INVALID_PARAMETER;