テスト用
Fork of nRF51822 by
Diff: nRF51Gap.cpp
- Revision:
- 17:c3e11bb6cb72
- Parent:
- 3:791d672cbbec
- Child:
- 22:c6ee8136847e
--- a/nRF51Gap.cpp Fri May 30 13:32:48 2014 +0100 +++ b/nRF51Gap.cpp Wed Jun 04 09:19:57 2014 +0100 @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #include "nRF51Gap.h" #include "mbed.h" @@ -34,16 +34,16 @@ The optional Scan Response payload if the advertising type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED in \ref GapAdveritinngParams - + @returns \ref ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly @retval BLE_ERROR_BUFFER_OVERFLOW The proposed action would cause a buffer overflow. All advertising payloads must be <= 31 bytes, for example. - + @retval BLE_ERROR_NOT_IMPLEMENTED A feature was requested that is not yet supported in the nRF51 firmware or hardware. @@ -58,18 +58,17 @@ @endcode */ /**************************************************************************/ -ble_error_t nRF51Gap::setAdvertisingData(const GapAdvertisingData & advData, const GapAdvertisingData & scanResponse) +ble_error_t nRF51Gap::setAdvertisingData(const GapAdvertisingData &advData, + const GapAdvertisingData &scanResponse) { /* Make sure we don't exceed the advertising payload length */ - if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) - { + 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; + if (advData.getPayloadLen() == 0) { + return BLE_ERROR_PARAM_OUT_OF_RANGE; } /* Check the scan response payload limits */ @@ -86,13 +85,19 @@ // return BLE_ERROR_PARAM_OUT_OF_RANGE; // } //} - + /* Send advertising data! */ - ASSERT( ERROR_NONE == sd_ble_gap_adv_data_set(advData.getPayload(), advData.getPayloadLen(), - scanResponse.getPayload(), scanResponse.getPayloadLen()), BLE_ERROR_PARAM_OUT_OF_RANGE); + ASSERT(ERROR_NONE == + sd_ble_gap_adv_data_set(advData.getPayload(), + advData.getPayloadLen(), + scanResponse.getPayload(), + scanResponse.getPayloadLen()), + BLE_ERROR_PARAM_OUT_OF_RANGE); - /* Make sure the GAP Service appearance value is aligned with the appearance from GapAdvertisingData */ - ASSERT( ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), BLE_ERROR_PARAM_OUT_OF_RANGE); + /* Make sure the GAP Service appearance value is aligned with the + *appearance from GapAdvertisingData */ + ASSERT(ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), + BLE_ERROR_PARAM_OUT_OF_RANGE); /* ToDo: Perform some checks on the payload, for example the Scan Response can't */ /* contains a flags AD type, etc. */ @@ -104,14 +109,14 @@ /*! @brief Starts the BLE HW, initialising any services that were added before this function was called. - + @note All services must be added before calling this function! - + @returns ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly - + @section EXAMPLE @code @@ -119,63 +124,63 @@ @endcode */ /**************************************************************************/ -ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams & params) +ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams ¶ms) { - /* Make sure we support the advertising type */ - if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) - { + /* 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; } /* Check interval range */ - if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) - { + if (params.getAdvertisingType() == + GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { /* Min delay is slightly longer for unconnectable devices */ if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || - (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) - { + (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { return BLE_ERROR_PARAM_OUT_OF_RANGE; } - } - else - { + } else { if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || - (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) - { + (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { return BLE_ERROR_PARAM_OUT_OF_RANGE; } } /* Check timeout is zero for Connectable Directed */ - if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && - (params.getTimeout() != 0)) - { + if ((params.getAdvertisingType() == + GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && + (params.getTimeout() != 0)) { /* Timeout must be 0 with this type, although we'll never get here */ /* since this isn't implemented yet anyway */ return BLE_ERROR_PARAM_OUT_OF_RANGE; } /* Check timeout for other advertising types */ - if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && - (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) - { + if ((params.getAdvertisingType() != + GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && + (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) { return BLE_ERROR_PARAM_OUT_OF_RANGE; } /* Start Advertising */ - ble_gap_adv_params_t adv_para = { 0 }; + ble_gap_adv_params_t adv_para = {0}; - adv_para.type = params.getAdvertisingType() ; - adv_para.p_peer_addr = NULL ; // Undirected advertisement - adv_para.fp = BLE_GAP_ADV_FP_ANY ; - adv_para.p_whitelist = NULL ; - adv_para.interval = params.getInterval() ; // advertising interval (in units of 0.625 ms) - adv_para.timeout = params.getTimeout() ; + adv_para.type = params.getAdvertisingType(); + adv_para.p_peer_addr = NULL; // Undirected + // advertisement + adv_para.fp = BLE_GAP_ADV_FP_ANY; + adv_para.p_whitelist = NULL; + adv_para.interval = params.getInterval(); // advertising + // interval (in units + // of 0.625 ms) + adv_para.timeout = params.getTimeout(); - ASSERT( ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE); - - state.advertising = 1; + ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), + BLE_ERROR_PARAM_OUT_OF_RANGE); + + state.advertising = 1; return BLE_ERROR_NONE; } @@ -183,12 +188,12 @@ /**************************************************************************/ /*! @brief Stops the BLE HW and disconnects from any devices - + @returns ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly - + @section EXAMPLE @code @@ -198,23 +203,23 @@ /**************************************************************************/ 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; + state.advertising = 0; - return BLE_ERROR_NONE; + return BLE_ERROR_NONE; } /**************************************************************************/ /*! @brief Disconnects if we are connected to a central device - + @returns ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly - + @section EXAMPLE @code @@ -224,14 +229,16 @@ /**************************************************************************/ ble_error_t nRF51Gap::disconnect(void) { - state.advertising = 0; - state.connected = 0; - - /* Disconnect if we are connected to a central device */ - 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; + + /* Disconnect if we are connected to a central device */ + 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; } /**************************************************************************/ @@ -241,9 +248,9 @@ /**************************************************************************/ void nRF51Gap::setConnectionHandle(uint16_t con_handle) { - m_connectionHandle = con_handle; + m_connectionHandle = con_handle; } - + /**************************************************************************/ /*! @brief Gets the 16-bit connection handle @@ -251,13 +258,13 @@ /**************************************************************************/ uint16_t nRF51Gap::getConnectionHandle(void) { - return m_connectionHandle; + return m_connectionHandle; } - + /**************************************************************************/ /*! @brief Sets the BLE device address - + @returns ble_error_t @section EXAMPLE @@ -272,13 +279,17 @@ /**************************************************************************/ ble_error_t nRF51Gap::setAddress(addr_type_t type, const 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; + 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; }