Old version
Fork of nRF51822 by
Revision 1:f84abedbf4fb, committed 2014-04-03
- Comitter:
- mbed_official
- Date:
- Thu Apr 03 01:45:38 2014 +0100
- Parent:
- 0:eff01767de02
- Child:
- 2:49e7ee9e88ab
- Commit message:
- Synchronized with git revision 348e2de6c8b2badc1fd1fe7ae044c24c25244a11
Full URL: https://github.com/mbedmicro/mbed/commit/348e2de6c8b2badc1fd1fe7ae044c24c25244a11/
Changed in this revision
--- a/btle/btle.cpp Wed Mar 26 14:38:17 2014 +0000 +++ b/btle/btle.cpp Thu Apr 03 01:45:38 2014 +0100 @@ -94,13 +94,13 @@ switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: - nRF51GattServer::getInstance().m_connectionHandle = p_ble_evt->evt.gap_evt.conn_handle; + nRF51Gap::getInstance().setConnectionHandle( p_ble_evt->evt.gap_evt.conn_handle ); nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED); break; case BLE_GAP_EVT_DISCONNECTED: // Since we are not in a connection and have not started advertising, store bonds - nRF51GattServer::getInstance().m_connectionHandle = BLE_CONN_HANDLE_INVALID; + nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store() ); nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); break; @@ -117,7 +117,7 @@ sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE ; sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE ; - ASSERT_STATUS_RET_VOID ( sd_ble_gap_sec_params_reply(nRF51GattServer::getInstance().m_connectionHandle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_params) ); + ASSERT_STATUS_RET_VOID ( sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().getConnectionHandle(), BLE_GAP_SEC_STATUS_SUCCESS, &sec_params) ); } break;
--- a/nRF51Gap.cpp Wed Mar 26 14:38:17 2014 +0000 +++ b/nRF51Gap.cpp Thu Apr 03 01:45:38 2014 +0100 @@ -19,6 +19,7 @@ #include "common/common.h" #include "ble_advdata.h" +#include "ble_hci.h" /**************************************************************************/ /*! @@ -197,12 +198,12 @@ /**************************************************************************/ ble_error_t nRF51Gap::stopAdvertising(void) { - /* Stop Advertising */ - ASSERT( ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); + /* Stop Advertising */ + ASSERT( ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); state.advertising = 0; - return BLE_ERROR_NONE; + return BLE_ERROR_NONE; } /**************************************************************************/ @@ -223,11 +224,61 @@ /**************************************************************************/ ble_error_t nRF51Gap::disconnect(void) { + state.advertising = 0; + state.connected = 0; + /* Disconnect if we are connected to a central device */ - // ASSERT( ERROR_NONE == sd_ble_gap_disconnect(), BLE_ERROR_PARAM_OUT_OF_RANGE); + ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(m_connectionHandle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION), + BLE_ERROR_PARAM_OUT_OF_RANGE); + + return BLE_ERROR_NONE; +} - state.advertising = 0; - state.connected = 0; - - return BLE_ERROR_NONE; +/**************************************************************************/ +/*! + @brief Sets the 16-bit connection handle +*/ +/**************************************************************************/ +void nRF51Gap::setConnectionHandle(uint16_t con_handle) +{ + m_connectionHandle = con_handle; +} + +/**************************************************************************/ +/*! + @brief Gets the 16-bit connection handle +*/ +/**************************************************************************/ +uint16_t nRF51Gap::getConnectionHandle(void) +{ + return m_connectionHandle; } + +/**************************************************************************/ +/*! + @brief Sets the BLE device address + + @returns ble_error_t + + @section EXAMPLE + + @code + + uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; + nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51Gap::setAddress(addr_type_t type, uint8_t address[6]) +{ + if ( type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) return BLE_ERROR_PARAM_OUT_OF_RANGE; + + ble_gap_addr_t dev_addr; + dev_addr.addr_type = type; + memcpy(dev_addr.addr, address, 6); + + ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(&dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE); + + return BLE_ERROR_NONE; +}
--- a/nRF51Gap.h Wed Mar 26 14:38:17 2014 +0000 +++ b/nRF51Gap.h Thu Apr 03 01:45:38 2014 +0100 @@ -19,6 +19,7 @@ #include "mbed.h" #include "blecommon.h" +#include "ble.h" #include "GapAdvertisingParams.h" #include "GapAdvertisingData.h" #include "hw/Gap.h" @@ -39,13 +40,18 @@ } /* Functions that must be implemented from Gap */ + virtual ble_error_t setAddress(addr_type_t type, uint8_t address[6]); virtual ble_error_t setAdvertisingData(GapAdvertisingData &, GapAdvertisingData &); virtual ble_error_t startAdvertising(GapAdvertisingParams &); virtual ble_error_t stopAdvertising(void); virtual ble_error_t disconnect(void); + void setConnectionHandle(uint16_t con_handle); + uint16_t getConnectionHandle(void); + private: - nRF51Gap() { }; + uint16_t m_connectionHandle; + nRF51Gap() { m_connectionHandle = BLE_CONN_HANDLE_INVALID; }; nRF51Gap(nRF51Gap const&); void operator=(nRF51Gap const&); };
--- a/nRF51GattServer.cpp Wed Mar 26 14:38:17 2014 +0000 +++ b/nRF51GattServer.cpp Thu Apr 03 01:45:38 2014 +0100 @@ -20,6 +20,8 @@ #include "common/common.h" #include "btle/custom/custom_helper.h" +#include "hw/nRF51822n/nRF51Gap.h" + /**************************************************************************/ /*! @brief Adds a new service to the GATT table on the peripheral @@ -138,13 +140,16 @@ /**************************************************************************/ ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly) { + uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle(); + if (localOnly) { /* Only update locally regardless of notify/indicate */ ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); } + if ((p_characteristics[charHandle]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && - (m_connectionHandle != BLE_CONN_HANDLE_INVALID) ) + (gapConnectionHandle != BLE_CONN_HANDLE_INVALID) ) { /* HVX update for the characteristic value */ ble_gatts_hvx_params_t hvx_params; @@ -155,7 +160,7 @@ hvx_params.p_data = buffer; hvx_params.p_len = &len; - error_t error = (error_t) sd_ble_gatts_hvx(m_connectionHandle, &hvx_params); + error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params); /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */ if ( (error != ERROR_NONE ) && (error != ERROR_INVALID_STATE) &&
--- a/nRF51GattServer.h Wed Mar 26 14:38:17 2014 +0000 +++ b/nRF51GattServer.h Thu Apr 03 01:45:38 2014 +0100 @@ -49,13 +49,11 @@ void eventCallback(void); void hwCallback(ble_evt_t * p_ble_evt); - uint16_t m_connectionHandle; // TODO move to private - private: GattCharacteristic* p_characteristics[BLE_TOTAL_CHARACTERISTICS]; ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS]; - nRF51GattServer() { serviceCount = 0; characteristicCount = 0; m_connectionHandle = BLE_CONN_HANDLE_INVALID; }; + nRF51GattServer() { serviceCount = 0; characteristicCount = 0; }; nRF51GattServer(nRF51GattServer const&); void operator=(nRF51GattServer const&);