Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
Revision 52:120bd37b9d0d, committed 2014-07-24
- Comitter:
- Rohit Grover
- Date:
- Thu Jul 24 15:46:29 2014 +0100
- Parent:
- 51:63ba3bcf5cd6
- Child:
- 53:1e5c300cec7f
- Commit message:
- implement APIs for get/setDeviceName(), get/setAppearance(), getVersion(), and setTxPower()
Changed in this revision
--- a/btle/btle.cpp Wed Jul 23 12:01:30 2014 +0100
+++ b/btle/btle.cpp Thu Jul 24 15:46:29 2014 +0100
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
#include "common/common.h"
#include "nordic_common.h"
@@ -34,14 +35,14 @@
#include "softdevice_handler.h"
#include "pstorage.h"
-#include "hw/GapEvents.h"
+#include "GapEvents.h"
#include "nRF51Gap.h"
#include "nRF51GattServer.h"
#if NEED_BOND_MANAGER /* disabled by default */
static void service_error_callback(uint32_t nrf_error);
#endif
-void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
+extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
void app_error_handler(uint32_t error_code,
uint32_t line_num,
const uint8_t *p_file_name);
--- a/nRF51822n.cpp Wed Jul 23 12:01:30 2014 +0100
+++ b/nRF51822n.cpp Thu Jul 24 15:46:29 2014 +0100
@@ -53,6 +53,41 @@
{
}
+const char *nRF51822n::getVersion(void)
+{
+ static char versionString[10];
+ static bool versionFetched = false;
+
+ if (!versionFetched) {
+ ble_version_t version;
+ if (sd_ble_version_get(&version) == NRF_SUCCESS) {
+ snprintf(versionString, sizeof(versionString), "%u.%u", version.version_number, version.subversion_number);
+ versionFetched = true;
+ } else {
+ strncpy(versionString, "unknown", sizeof(versionString));
+ }
+ }
+
+ return versionString;
+}
+
+/* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
+ble_error_t nRF51822n::setTxPower(int8_t txPower)
+{
+ unsigned rc;
+ if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
+ switch (rc) {
+ case NRF_ERROR_BUSY:
+ return BLE_STACK_BUSY;
+ case NRF_ERROR_INVALID_PARAM:
+ default:
+ return BLE_ERROR_PARAM_OUT_OF_RANGE;
+ }
+ }
+
+ return BLE_ERROR_NONE;
+}
+
/**************************************************************************/
/*!
@brief Initialises anything required to start using BLE
--- a/nRF51822n.h Wed Jul 23 12:01:30 2014 +0100
+++ b/nRF51822n.h Thu Jul 24 15:46:29 2014 +0100
@@ -34,12 +34,17 @@
nRF51822n(void);
virtual ~nRF51822n(void);
+ virtual const char *getVersion(void);
+
virtual Gap &getGap() {
return nRF51Gap::getInstance();
};
virtual GattServer &getGattServer() {
return nRF51GattServer::getInstance();
};
+
+ virtual ble_error_t setTxPower(int8_t txPower);
+
virtual ble_error_t init(void);
virtual ble_error_t reset(void);
virtual void waitForEvent(void);
--- a/nRF51Gap.h Wed Jul 23 12:01:30 2014 +0100 +++ b/nRF51Gap.h Thu Jul 24 15:46:29 2014 +0100 @@ -22,7 +22,7 @@ #include "ble.h" #include "GapAdvertisingParams.h" #include "GapAdvertisingData.h" -#include "hw/Gap.h" +#include "public/Gap.h" /**************************************************************************/ /*!
--- a/nRF51GattServer.cpp Wed Jul 23 12:01:30 2014 +0100
+++ b/nRF51GattServer.cpp Thu Jul 24 15:46:29 2014 +0100
@@ -194,6 +194,45 @@
return BLE_ERROR_NONE;
}
+ble_error_t nRF51GattServer::setDeviceName(const uint8_t *deviceName)
+{
+ ble_gap_conn_sec_mode_t sec_mode;
+ BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
+
+ if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) {
+ return BLE_ERROR_NONE;
+ } else {
+ return BLE_ERROR_PARAM_OUT_OF_RANGE;
+ }
+}
+
+ble_error_t nRF51GattServer::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
+{
+ if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) {
+ return BLE_ERROR_NONE;
+ } else {
+ return BLE_ERROR_PARAM_OUT_OF_RANGE;
+ }
+}
+
+ble_error_t nRF51GattServer::setAppearance(uint16_t appearance)
+{
+ if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) {
+ return BLE_ERROR_NONE;
+ } else {
+ return BLE_ERROR_PARAM_OUT_OF_RANGE;
+ }
+}
+
+ble_error_t nRF51GattServer::getAppearance(uint16_t *appearanceP)
+{
+ if (sd_ble_gap_appearance_get(appearanceP)) {
+ return BLE_ERROR_NONE;
+ } else {
+ return BLE_ERROR_PARAM_OUT_OF_RANGE;
+ }
+}
+
/**************************************************************************/
/*!
@brief Callback handler for events getting pushed up from the SD
--- a/nRF51GattServer.h Wed Jul 23 12:01:30 2014 +0100
+++ b/nRF51GattServer.h Thu Jul 24 15:46:29 2014 +0100
@@ -21,7 +21,7 @@
#include "blecommon.h"
#include "ble.h" /* nordic ble */
#include "GattService.h"
-#include "hw/GattServer.h"
+#include "public/GattServer.h"
#define BLE_TOTAL_CHARACTERISTICS 10
@@ -38,6 +38,11 @@
virtual ble_error_t readValue(uint16_t handle, uint8_t buffer[], uint16_t *const lengthP);
virtual ble_error_t updateValue(uint16_t, uint8_t[], uint16_t, bool localOnly = false);
+ virtual ble_error_t setDeviceName(const uint8_t *deviceName);
+ virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
+ virtual ble_error_t setAppearance(uint16_t appearance);
+ virtual ble_error_t getAppearance(uint16_t *appearanceP);
+
/* nRF51 Functions */
void eventCallback(void);
void hwCallback(ble_evt_t *p_ble_evt);
