Openwear requires RC oscillator to be used
Fork of nRF51822 by
Revision 22:c6ee8136847e, committed 2014-06-09
- Comitter:
- Rohit Grover
- Date:
- Mon Jun 09 09:12:10 2014 +0100
- Parent:
- 21:84599842b5fb
- Child:
- 23:cdab28442479
- Commit message:
- slowly switching to astyle code formatting as recommended by the team
Changed in this revision
--- a/btle/btle.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/btle.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,231 +1,230 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "common/common.h" - -#include "app_timer.h" -#include "btle.h" - -#include "ble_stack_handler_types.h" -#include "ble_radio_notification.h" -#include "ble_flash.h" -#include "ble_bondmngr.h" -#include "ble_conn_params.h" - -#include "btle_gap.h" -#include "btle_advertising.h" -#include "custom/custom_helper.h" - -#include "nordic_common.h" -#include "softdevice_handler.h" -#include "pstorage.h" - -#include "hw/GapEvents.h" -#include "nRF51Gap.h" -#include "nRF51GattServer.h" - -static void service_error_callback(uint32_t nrf_error); -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); - -static error_t bond_manager_init(void); - -static void btle_handler(ble_evt_t *p_ble_evt); - -/**************************************************************************/ -/*! - -*/ -/**************************************************************************/ -static void sys_evt_dispatch(uint32_t sys_evt) -{ - pstorage_sys_event_handler(sys_evt); -} - -/**************************************************************************/ -/*! - @brief Initialises BTLE and the underlying HW/SoftDevice - - @returns -*/ -/**************************************************************************/ -error_t btle_init(void) -{ - APP_TIMER_INIT(0, 8, 5, false); - SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false); - - ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); - ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); - - bond_manager_init(); - btle_gap_init(); - - return ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief - - @param[in] p_ble_evt - - @returns -*/ -/**************************************************************************/ -static void btle_handler(ble_evt_t *p_ble_evt) -{ - /* Library service handlers */ - ble_bondmngr_on_ble_evt(p_ble_evt); - ble_conn_params_on_ble_evt(p_ble_evt); - - /* Custom event handler */ - switch (p_ble_evt->header.evt_id) { - case BLE_GAP_EVT_CONNECTED: - 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 - nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); - ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store()); - nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); - break; - - case BLE_GAP_EVT_SEC_PARAMS_REQUEST: - { - ble_gap_sec_params_t sec_params = {0}; - - sec_params.timeout = 30; /*< Timeout for Pairing Request or - * Security Request (in seconds). */ - sec_params.bond = 1; /**< Perform bonding. */ - sec_params.mitm = CFG_BLE_SEC_PARAM_MITM; - sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES; - sec_params.oob = CFG_BLE_SEC_PARAM_OOB; - 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(nRF51Gap::getInstance(). - getConnectionHandle(), - BLE_GAP_SEC_STATUS_SUCCESS, - &sec_params)); - } - break; - - case BLE_GAP_EVT_TIMEOUT: - if (p_ble_evt->evt.gap_evt.params.timeout.src == - BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { - nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT); - } - break; - - case BLE_GATTC_EVT_TIMEOUT: - case BLE_GATTS_EVT_TIMEOUT: - // Disconnect on GATT Server and Client timeout events. - // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, - // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); - break; - - default: - break; - } - - nRF51GattServer::getInstance().hwCallback(p_ble_evt); -} - -/**************************************************************************/ -/*! - @brief Initialises the bond manager - - @note Bond data will be cleared on reset if the bond delete - button is pressed during initialisation (the button is - defined as CFG_BLE_BOND_DELETE_BUTTON_NUM). - - @returns -*/ -/**************************************************************************/ -static error_t bond_manager_init(void) -{ - ble_bondmngr_init_t bond_para = {0}; - - ASSERT_STATUS ( pstorage_init()); - - bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND; - bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR; - //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ; - bond_para.evt_handler = NULL; - bond_para.error_handler = service_error_callback; - - ASSERT_STATUS( ble_bondmngr_init( &bond_para )); - - /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */ - // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, - // NRF_RADIO_NOTIFICATION_DISTANCE_4560US, - // ble_flash_on_radio_active_evt) ); - - return ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief - @param[in] nrf_error - @returns -*/ -/**************************************************************************/ -static void service_error_callback(uint32_t nrf_error) -{ - ASSERT_STATUS_RET_VOID( nrf_error ); -} - -/**************************************************************************/ -/*! - @brief Callback when an error occurs inside the SoftDevice - - @param[in] line_num - @param[in] p-file_name - - @returns -*/ -/**************************************************************************/ -void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) -{ - ASSERT(false, (void) 0); -} - -/**************************************************************************/ -/*! - @brief Handler for general errors above the SoftDevice layer. - Typically we can' recover from this so we do a reset. - - @param[in] error_code - @param[in] line_num - @param[in] p-file_name - - @returns -*/ -/**************************************************************************/ -void app_error_handler(uint32_t error_code, - uint32_t line_num, - const uint8_t *p_file_name) -{ - ASSERT_STATUS_RET_VOID( error_code ); - NVIC_SystemReset(); -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "common/common.h" + +#include "app_timer.h" +#include "btle.h" + +#include "ble_stack_handler_types.h" +#include "ble_radio_notification.h" +#include "ble_flash.h" +#include "ble_bondmngr.h" +#include "ble_conn_params.h" + +#include "btle_gap.h" +#include "btle_advertising.h" +#include "custom/custom_helper.h" + +#include "nordic_common.h" +#include "softdevice_handler.h" +#include "pstorage.h" + +#include "hw/GapEvents.h" +#include "nRF51Gap.h" +#include "nRF51GattServer.h" + +static void service_error_callback(uint32_t nrf_error); +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); + +static error_t bond_manager_init(void); + +static void btle_handler(ble_evt_t *p_ble_evt); + +/**************************************************************************/ +/*! + +*/ +/**************************************************************************/ +static void sys_evt_dispatch(uint32_t sys_evt) +{ + pstorage_sys_event_handler(sys_evt); +} + +/**************************************************************************/ +/*! + @brief Initialises BTLE and the underlying HW/SoftDevice + + @returns +*/ +/**************************************************************************/ +error_t btle_init(void) +{ + APP_TIMER_INIT(0, 8, 5, false); + SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false); + + ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); + ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); + + bond_manager_init(); + btle_gap_init(); + + return ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief + + @param[in] p_ble_evt + + @returns +*/ +/**************************************************************************/ +static void btle_handler(ble_evt_t *p_ble_evt) +{ + /* Library service handlers */ + ble_bondmngr_on_ble_evt(p_ble_evt); + ble_conn_params_on_ble_evt(p_ble_evt); + + /* Custom event handler */ + switch (p_ble_evt->header.evt_id) { + case BLE_GAP_EVT_CONNECTED: + 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 + nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); + ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store()); + nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); + break; + + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { + ble_gap_sec_params_t sec_params = {0}; + + sec_params.timeout = 30; /*< Timeout for Pairing Request or + * Security Request (in seconds). */ + sec_params.bond = 1; /**< Perform bonding. */ + sec_params.mitm = CFG_BLE_SEC_PARAM_MITM; + sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES; + sec_params.oob = CFG_BLE_SEC_PARAM_OOB; + 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(nRF51Gap::getInstance(). + getConnectionHandle(), + BLE_GAP_SEC_STATUS_SUCCESS, + &sec_params)); + } + break; + + case BLE_GAP_EVT_TIMEOUT: + if (p_ble_evt->evt.gap_evt.params.timeout.src == + BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { + nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT); + } + break; + + case BLE_GATTC_EVT_TIMEOUT: + case BLE_GATTS_EVT_TIMEOUT: + // Disconnect on GATT Server and Client timeout events. + // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, + // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); + break; + + default: + break; + } + + nRF51GattServer::getInstance().hwCallback(p_ble_evt); +} + +/**************************************************************************/ +/*! + @brief Initialises the bond manager + + @note Bond data will be cleared on reset if the bond delete + button is pressed during initialisation (the button is + defined as CFG_BLE_BOND_DELETE_BUTTON_NUM). + + @returns +*/ +/**************************************************************************/ +static error_t bond_manager_init(void) +{ + ble_bondmngr_init_t bond_para = {0}; + + ASSERT_STATUS ( pstorage_init()); + + bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND; + bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR; + //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ; + bond_para.evt_handler = NULL; + bond_para.error_handler = service_error_callback; + + ASSERT_STATUS( ble_bondmngr_init( &bond_para )); + + /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */ + // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, + // NRF_RADIO_NOTIFICATION_DISTANCE_4560US, + // ble_flash_on_radio_active_evt) ); + + return ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief + @param[in] nrf_error + @returns +*/ +/**************************************************************************/ +static void service_error_callback(uint32_t nrf_error) +{ + ASSERT_STATUS_RET_VOID( nrf_error ); +} + +/**************************************************************************/ +/*! + @brief Callback when an error occurs inside the SoftDevice + + @param[in] line_num + @param[in] p-file_name + + @returns +*/ +/**************************************************************************/ +void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) +{ + ASSERT(false, (void) 0); +} + +/**************************************************************************/ +/*! + @brief Handler for general errors above the SoftDevice layer. + Typically we can' recover from this so we do a reset. + + @param[in] error_code + @param[in] line_num + @param[in] p-file_name + + @returns +*/ +/**************************************************************************/ +void app_error_handler(uint32_t error_code, + uint32_t line_num, + const uint8_t *p_file_name) +{ + ASSERT_STATUS_RET_VOID( error_code ); + NVIC_SystemReset(); +}
--- a/btle/btle.h Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/btle.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,35 +1,35 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _BTLE_H_ -#define _BTLE_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "common/common.h" - -#include "ble_srv_common.h" -#include "ble.h" - -error_t btle_init(void); - -#ifdef __cplusplus -} -#endif - -#endif // ifndef _BTLE_H_ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BTLE_H_ +#define _BTLE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "common/common.h" + +#include "ble_srv_common.h" +#include "ble.h" + +error_t btle_init(void); + +#ifdef __cplusplus +} +#endif + +#endif // ifndef _BTLE_H_
--- a/btle/btle_advertising.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/btle_advertising.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,46 +1,46 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "common/common.h" - -#include "ble_advdata.h" -#include "btle.h" - -/**************************************************************************/ -/*! - @brief Starts the advertising process - - @returns -*/ -/**************************************************************************/ -error_t btle_advertising_start(void) -{ - ble_gap_adv_params_t adv_para = {0}; - - /* Set the default advertising parameters */ - adv_para.type = BLE_GAP_ADV_TYPE_ADV_IND; - adv_para.p_peer_addr = NULL; /* Undirected advertising */ - adv_para.fp = BLE_GAP_ADV_FP_ANY; - adv_para.p_whitelist = NULL; - adv_para.interval = (CFG_GAP_ADV_INTERVAL_MS * 8) / 5; /* Advertising - * interval in - * units of 0.625 - * ms */ - adv_para.timeout = CFG_GAP_ADV_TIMEOUT_S; - - ASSERT_STATUS( sd_ble_gap_adv_start(&adv_para)); - - return ERROR_NONE; -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "common/common.h" + +#include "ble_advdata.h" +#include "btle.h" + +/**************************************************************************/ +/*! + @brief Starts the advertising process + + @returns +*/ +/**************************************************************************/ +error_t btle_advertising_start(void) +{ + ble_gap_adv_params_t adv_para = {0}; + + /* Set the default advertising parameters */ + adv_para.type = BLE_GAP_ADV_TYPE_ADV_IND; + adv_para.p_peer_addr = NULL; /* Undirected advertising */ + adv_para.fp = BLE_GAP_ADV_FP_ANY; + adv_para.p_whitelist = NULL; + adv_para.interval = (CFG_GAP_ADV_INTERVAL_MS * 8) / 5; /* Advertising + * interval in + * units of 0.625 + * ms */ + adv_para.timeout = CFG_GAP_ADV_TIMEOUT_S; + + ASSERT_STATUS( sd_ble_gap_adv_start(&adv_para)); + + return ERROR_NONE; +}
--- a/btle/btle_advertising.h Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/btle_advertising.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,24 +1,24 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _BTLE_ADVERTISING_H_ -#define _BTLE_ADVERTISING_H_ - -#include "common/common.h" - -error_t btle_advertising_start(void); - -#endif // ifndef _BTLE_ADVERTISING_H_ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BTLE_ADVERTISING_H_ +#define _BTLE_ADVERTISING_H_ + +#include "common/common.h" + +error_t btle_advertising_start(void); + +#endif // ifndef _BTLE_ADVERTISING_H_
--- a/btle/btle_gap.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/btle_gap.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,96 +1,96 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "common/common.h" - -#include "app_timer.h" -#include "ble_gap.h" -#include "ble_conn_params.h" - -static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST; -static void error_callback(uint32_t nrf_error); - -/**************************************************************************/ -/*! - @brief Initialise GAP in the underlying SoftDevice - - @returns -*/ -/**************************************************************************/ -error_t btle_gap_init(void) -{ - ble_gap_conn_params_t gap_conn_params = {0}; - - gap_conn_params.min_conn_interval = msec_to_1_25msec( - CFG_GAP_CONNECTION_MIN_INTERVAL_MS); // in 1.25ms units - gap_conn_params.max_conn_interval = msec_to_1_25msec( - CFG_GAP_CONNECTION_MAX_INTERVAL_MS); // in 1.25ms unit - gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY; - gap_conn_params.conn_sup_timeout = - CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit - - ble_gap_conn_sec_mode_t sec_mode; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed - - ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, - (const uint8_t *) - CFG_GAP_LOCAL_NAME, - strlen(CFG_GAP_LOCAL_NAME))); - ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE)); - ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params)); - ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL)); - - /* Connection Parameters */ - enum { - FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), - NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), - MAX_UPDATE_COUNT = 3 - }; - - ble_conn_params_init_t cp_init = {0}; - - cp_init.p_conn_params = NULL; - cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY; - cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY; - cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT; - cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID; - cp_init.disconnect_on_fail = true; - cp_init.evt_handler = NULL; - cp_init.error_handler = error_callback; - - ASSERT_STATUS ( ble_conn_params_init(&cp_init)); - - return ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Converts msecs to an integer representing 1.25ms units - - @param[in] ms - The number of milliseconds to conver to 1.25ms units - - @returns The number of 1.25ms units in the supplied number of ms -*/ -/**************************************************************************/ -static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) -{ - return (interval_ms * 4) / 5; -} - -static void error_callback(uint32_t nrf_error) -{ - ASSERT_STATUS_RET_VOID( nrf_error ); -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "common/common.h" + +#include "app_timer.h" +#include "ble_gap.h" +#include "ble_conn_params.h" + +static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST; +static void error_callback(uint32_t nrf_error); + +/**************************************************************************/ +/*! + @brief Initialise GAP in the underlying SoftDevice + + @returns +*/ +/**************************************************************************/ +error_t btle_gap_init(void) +{ + ble_gap_conn_params_t gap_conn_params = {0}; + + gap_conn_params.min_conn_interval = msec_to_1_25msec( + CFG_GAP_CONNECTION_MIN_INTERVAL_MS); // in 1.25ms units + gap_conn_params.max_conn_interval = msec_to_1_25msec( + CFG_GAP_CONNECTION_MAX_INTERVAL_MS); // in 1.25ms unit + gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY; + gap_conn_params.conn_sup_timeout = + CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit + + ble_gap_conn_sec_mode_t sec_mode; + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed + + ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, + (const uint8_t *) + CFG_GAP_LOCAL_NAME, + strlen(CFG_GAP_LOCAL_NAME))); + ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE)); + ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params)); + ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL)); + + /* Connection Parameters */ + enum { + FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), + NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), + MAX_UPDATE_COUNT = 3 + }; + + ble_conn_params_init_t cp_init = {0}; + + cp_init.p_conn_params = NULL; + cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY; + cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY; + cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT; + cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID; + cp_init.disconnect_on_fail = true; + cp_init.evt_handler = NULL; + cp_init.error_handler = error_callback; + + ASSERT_STATUS ( ble_conn_params_init(&cp_init)); + + return ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Converts msecs to an integer representing 1.25ms units + + @param[in] ms + The number of milliseconds to conver to 1.25ms units + + @returns The number of 1.25ms units in the supplied number of ms +*/ +/**************************************************************************/ +static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) +{ + return (interval_ms * 4) / 5; +} + +static void error_callback(uint32_t nrf_error) +{ + ASSERT_STATUS_RET_VOID( nrf_error ); +}
--- a/btle/btle_gap.h Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/btle_gap.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,24 +1,24 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _BTLE_GAP_H_ -#define _BTLE_GAP_H_ - -#include "common/common.h" - -error_t btle_gap_init(void); - -#endif // ifndef _BTLE_GAP_H_ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BTLE_GAP_H_ +#define _BTLE_GAP_H_ + +#include "common/common.h" + +error_t btle_gap_init(void); + +#endif // ifndef _BTLE_GAP_H_
--- a/btle/custom/custom_helper.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/custom/custom_helper.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,247 +1,247 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - #include "custom_helper.h" - -/* - * The current version of the soft-device doesn't handle duplicate 128-bit UUIDs - * very well. It is therefore necessary to filter away duplicates before - * passing long UUIDs to sd_ble_uuid_vs_add(). The following types and data - * structures involved in maintaining a local cache of 128-bit UUIDs. - */ -typedef struct { - LongUUID_t uuid; - uint8_t type; -} converted_uuid_table_entry_t; -static const unsigned UUID_TABLE_MAX_ENTRIES = 8; /* This is the maximum number - * of 128-bit UUIDs with distinct bases that - * we expect to be in use; increase this - * limit if needed. */ -static unsigned uuidTableEntries = 0; /* current usage of the table */ -converted_uuid_table_entry_t convertedUUIDTable[UUID_TABLE_MAX_ENTRIES]; - -/** - * lookup the cache of previously converted 128-bit UUIDs to find a type value. - * @param uuid long UUID - * @param recoveredType the type field of the 3-byte nRF's uuid. - * @return true if a match is found. - */ -static bool -lookupConvertedUUIDTable(const LongUUID_t uuid, uint8_t *recoveredType) -{ - unsigned i; - for (i = 0; i < uuidTableEntries; i++) { - if (memcmp(convertedUUIDTable[i].uuid, - uuid, - LENGTH_OF_LONG_UUID) == 0) { - *recoveredType = convertedUUIDTable[i].type; - return true; - } - } - - return false; -} - -static void -addToConvertedUUIDTable(const LongUUID_t uuid, uint8_t type) -{ - if (uuidTableEntries == UUID_TABLE_MAX_ENTRIES) { - return; /* recovery needed; or at least the user should be - * warned about this fact.*/ - } - - memcpy(convertedUUIDTable[uuidTableEntries].uuid, uuid,LENGTH_OF_LONG_UUID); - convertedUUIDTable[uuidTableEntries].type = type; - uuidTableEntries++; -} - -/** - * The nRF transport has its own 3-byte representation of a UUID. If the user- - * specified UUID is 128-bits wide, then the UUID base needs to be added to the - * soft-device and converted to a 3-byte handle before being used further. This - * function is responsible for this translation of user-specified UUIDs into - * nRF's representation. - * - * @param[in] uuid - * user-specified UUID - * @return nRF - * 3-byte UUID (containing a type and 16-bit UUID) representation - * to be used with SVC calls. - */ -ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid) -{ - ble_uuid_t nordicUUID = { - .uuid = uuid.getShortUUID(), - .type = BLE_UUID_TYPE_UNKNOWN /* to be set below */ - }; - - if (uuid.shortOrLong() == UUID::UUID_TYPE_SHORT) { - nordicUUID.type = BLE_UUID_TYPE_BLE; - } else { - if (!lookupConvertedUUIDTable(uuid.getBaseUUID(), &nordicUUID.type)) { - nordicUUID.type = custom_add_uuid_base(uuid.getBaseUUID()); - addToConvertedUUIDTable(uuid.getBaseUUID(), nordicUUID.type); - } - } - - return nordicUUID; -} - -/**************************************************************************/ -/*! - @brief Adds the base UUID to the custom service. All UUIDs used - by this service are based on this 128-bit UUID. - - @note This UUID needs to be added to the SoftDevice stack before - adding the service's primary service via - 'sd_ble_gatts_service_add' - - @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) - - @returns The UUID type. - A return value of 0 should be considered an error. - - @retval 0x00 BLE_UUID_TYPE_UNKNOWN - @retval 0x01 BLE_UUID_TYPE_BLE - @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN - - @section EXAMPLE - @code - - // Take note that bytes 2/3 are blank since these are used to identify - // the primary service and individual characteristics - #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E" - - uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); - ASSERT(uuid_type > 0, ERROR_NOT_FOUND); - - // We can now safely add the primary service and any characteristics - // for our custom service ... - - @endcode -*/ -/**************************************************************************/ -uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base) -{ - ble_uuid128_t base_uuid; - uint8_t uuid_type = 0; - - /* Reverse the bytes since ble_uuid128_t is LSB */ - for (uint8_t i = 0; i<16; i++) { - base_uuid.uuid128[i] = p_uuid_base[15 - i]; - } - - ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); - - return uuid_type; -} - -/**************************************************************************/ -/*! - -*/ -/**************************************************************************/ -error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, - ble_uuid_t *p_uuid) -{ - LongUUID_t uuid_base_le; - - /* Reverse the bytes since ble_uuid128_t is LSB */ - for (uint8_t i = 0; i<16; i++) { - uuid_base_le[i] = p_uuid_base[15 - i]; - } - - ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid)); - - return ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Adds a new characteristic to the custom service, assigning - properties, a UUID add-on value, etc. - - @param[in] service_handle - @param[in] p_uuid The 16-bit value to add to the base UUID - for this characteristic (normally >1 - since 1 is typically used by the primary - service). - @param[in] char_props The characteristic properties, as - defined by ble_gatt_char_props_t - @param[in] max_length The maximum length of this characeristic - @param[in] p_char_handle - - @returns - @retval ERROR_NONE Everything executed normally -*/ -/**************************************************************************/ -error_t custom_add_in_characteristic(uint16_t service_handle, - ble_uuid_t *p_uuid, - uint8_t properties, - uint8_t *p_data, - uint16_t min_length, - uint16_t max_length, - ble_gatts_char_handles_t *p_char_handle) -{ - /* Characteristic metadata */ - ble_gatts_attr_md_t cccd_md; - ble_gatt_char_props_t char_props; - - memcpy(&char_props, &properties, 1); - - if (char_props.notify || char_props.indicate) { - /* Notification requires cccd */ - memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); - cccd_md.vloc = BLE_GATTS_VLOC_STACK; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - } - - ble_gatts_char_md_t char_md = {0}; - - char_md.char_props = char_props; - char_md.p_cccd_md = - (char_props.notify || char_props.indicate) ? &cccd_md : NULL; - - /* Attribute declaration */ - ble_gatts_attr_md_t attr_md = {0}; - - attr_md.vloc = BLE_GATTS_VLOC_STACK; - attr_md.vlen = (min_length == max_length) ? 0 : 1; - - if (char_props.read || char_props.notify || char_props.indicate) { - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - } - - if (char_props.write) { - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - } - - ble_gatts_attr_t attr_char_value = {0}; - - attr_char_value.p_uuid = p_uuid; - attr_char_value.p_attr_md = &attr_md; - attr_char_value.init_len = min_length; - attr_char_value.max_len = max_length; - attr_char_value.p_value = p_data; - - ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, - &char_md, - &attr_char_value, - p_char_handle)); - - return ERROR_NONE; -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "custom_helper.h" + +/* + * The current version of the soft-device doesn't handle duplicate 128-bit UUIDs + * very well. It is therefore necessary to filter away duplicates before + * passing long UUIDs to sd_ble_uuid_vs_add(). The following types and data + * structures involved in maintaining a local cache of 128-bit UUIDs. + */ +typedef struct { + LongUUID_t uuid; + uint8_t type; +} converted_uuid_table_entry_t; +static const unsigned UUID_TABLE_MAX_ENTRIES = 8; /* This is the maximum number + * of 128-bit UUIDs with distinct bases that + * we expect to be in use; increase this + * limit if needed. */ +static unsigned uuidTableEntries = 0; /* current usage of the table */ +converted_uuid_table_entry_t convertedUUIDTable[UUID_TABLE_MAX_ENTRIES]; + +/** + * lookup the cache of previously converted 128-bit UUIDs to find a type value. + * @param uuid long UUID + * @param recoveredType the type field of the 3-byte nRF's uuid. + * @return true if a match is found. + */ +static bool +lookupConvertedUUIDTable(const LongUUID_t uuid, uint8_t *recoveredType) +{ + unsigned i; + for (i = 0; i < uuidTableEntries; i++) { + if (memcmp(convertedUUIDTable[i].uuid, + uuid, + LENGTH_OF_LONG_UUID) == 0) { + *recoveredType = convertedUUIDTable[i].type; + return true; + } + } + + return false; +} + +static void +addToConvertedUUIDTable(const LongUUID_t uuid, uint8_t type) +{ + if (uuidTableEntries == UUID_TABLE_MAX_ENTRIES) { + return; /* recovery needed; or at least the user should be + * warned about this fact.*/ + } + + memcpy(convertedUUIDTable[uuidTableEntries].uuid, uuid,LENGTH_OF_LONG_UUID); + convertedUUIDTable[uuidTableEntries].type = type; + uuidTableEntries++; +} + +/** + * The nRF transport has its own 3-byte representation of a UUID. If the user- + * specified UUID is 128-bits wide, then the UUID base needs to be added to the + * soft-device and converted to a 3-byte handle before being used further. This + * function is responsible for this translation of user-specified UUIDs into + * nRF's representation. + * + * @param[in] uuid + * user-specified UUID + * @return nRF + * 3-byte UUID (containing a type and 16-bit UUID) representation + * to be used with SVC calls. + */ +ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid) +{ + ble_uuid_t nordicUUID = { + .uuid = uuid.getShortUUID(), + .type = BLE_UUID_TYPE_UNKNOWN /* to be set below */ + }; + + if (uuid.shortOrLong() == UUID::UUID_TYPE_SHORT) { + nordicUUID.type = BLE_UUID_TYPE_BLE; + } else { + if (!lookupConvertedUUIDTable(uuid.getBaseUUID(), &nordicUUID.type)) { + nordicUUID.type = custom_add_uuid_base(uuid.getBaseUUID()); + addToConvertedUUIDTable(uuid.getBaseUUID(), nordicUUID.type); + } + } + + return nordicUUID; +} + +/**************************************************************************/ +/*! + @brief Adds the base UUID to the custom service. All UUIDs used + by this service are based on this 128-bit UUID. + + @note This UUID needs to be added to the SoftDevice stack before + adding the service's primary service via + 'sd_ble_gatts_service_add' + + @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) + + @returns The UUID type. + A return value of 0 should be considered an error. + + @retval 0x00 BLE_UUID_TYPE_UNKNOWN + @retval 0x01 BLE_UUID_TYPE_BLE + @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN + + @section EXAMPLE + @code + + // Take note that bytes 2/3 are blank since these are used to identify + // the primary service and individual characteristics + #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E" + + uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); + ASSERT(uuid_type > 0, ERROR_NOT_FOUND); + + // We can now safely add the primary service and any characteristics + // for our custom service ... + + @endcode +*/ +/**************************************************************************/ +uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base) +{ + ble_uuid128_t base_uuid; + uint8_t uuid_type = 0; + + /* Reverse the bytes since ble_uuid128_t is LSB */ + for (uint8_t i = 0; i<16; i++) { + base_uuid.uuid128[i] = p_uuid_base[15 - i]; + } + + ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); + + return uuid_type; +} + +/**************************************************************************/ +/*! + +*/ +/**************************************************************************/ +error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, + ble_uuid_t *p_uuid) +{ + LongUUID_t uuid_base_le; + + /* Reverse the bytes since ble_uuid128_t is LSB */ + for (uint8_t i = 0; i<16; i++) { + uuid_base_le[i] = p_uuid_base[15 - i]; + } + + ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid)); + + return ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Adds a new characteristic to the custom service, assigning + properties, a UUID add-on value, etc. + + @param[in] service_handle + @param[in] p_uuid The 16-bit value to add to the base UUID + for this characteristic (normally >1 + since 1 is typically used by the primary + service). + @param[in] char_props The characteristic properties, as + defined by ble_gatt_char_props_t + @param[in] max_length The maximum length of this characeristic + @param[in] p_char_handle + + @returns + @retval ERROR_NONE Everything executed normally +*/ +/**************************************************************************/ +error_t custom_add_in_characteristic(uint16_t service_handle, + ble_uuid_t *p_uuid, + uint8_t properties, + uint8_t *p_data, + uint16_t min_length, + uint16_t max_length, + ble_gatts_char_handles_t *p_char_handle) +{ + /* Characteristic metadata */ + ble_gatts_attr_md_t cccd_md; + ble_gatt_char_props_t char_props; + + memcpy(&char_props, &properties, 1); + + if (char_props.notify || char_props.indicate) { + /* Notification requires cccd */ + memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); + } + + ble_gatts_char_md_t char_md = {0}; + + char_md.char_props = char_props; + char_md.p_cccd_md = + (char_props.notify || char_props.indicate) ? &cccd_md : NULL; + + /* Attribute declaration */ + ble_gatts_attr_md_t attr_md = {0}; + + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.vlen = (min_length == max_length) ? 0 : 1; + + if (char_props.read || char_props.notify || char_props.indicate) { + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); + } + + if (char_props.write) { + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); + } + + ble_gatts_attr_t attr_char_value = {0}; + + attr_char_value.p_uuid = p_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = min_length; + attr_char_value.max_len = max_length; + attr_char_value.p_value = p_data; + + ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, + &char_md, + &attr_char_value, + p_char_handle)); + + return ERROR_NONE; +}
--- a/btle/custom/custom_helper.h Fri Jun 06 14:13:32 2014 +0100 +++ b/btle/custom/custom_helper.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,45 +1,45 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _CUSTOM_HELPER_H_ -#define _CUSTOM_HELPER_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "common/common.h" -#include "ble.h" -#include "UUID.h" - -uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base); -error_t custom_decode_uuid(uint8_t const *const p_uuid_base, - ble_uuid_t *p_uuid); -ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid); - -error_t custom_add_in_characteristic(uint16_t service_handle, - ble_uuid_t *p_uuid, - uint8_t properties, - uint8_t *p_data, - uint16_t min_length, - uint16_t max_length, - ble_gatts_char_handles_t *p_char_handle); - -#ifdef __cplusplus -} -#endif - -#endif // ifndef _CUSTOM_HELPER_H_ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _CUSTOM_HELPER_H_ +#define _CUSTOM_HELPER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "common/common.h" +#include "ble.h" +#include "UUID.h" + +uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base); +error_t custom_decode_uuid(uint8_t const *const p_uuid_base, + ble_uuid_t *p_uuid); +ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid); + +error_t custom_add_in_characteristic(uint16_t service_handle, + ble_uuid_t *p_uuid, + uint8_t properties, + uint8_t *p_data, + uint16_t min_length, + uint16_t max_length, + ble_gatts_char_handles_t *p_char_handle); + +#ifdef __cplusplus +} +#endif + +#endif // ifndef _CUSTOM_HELPER_H_
--- a/nRF51822n.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/nRF51822n.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,105 +1,106 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mbed.h" -#include "nRF51822n.h" - -#include "btle/btle.h" - -/** - * The singleton which represents the nRF51822 transport for the BLEDevice. - */ -static nRF51822n deviceInstance; - -/** - * BLE-API requires an implementation of the following function in order to - * obtain its transport handle. - */ -BLEDeviceInstanceBase * -createBLEDeviceInstance(void) { - return (&deviceInstance); -} - -/**************************************************************************/ -/*! - @brief Constructor -*/ -/**************************************************************************/ -nRF51822n::nRF51822n(void) -{ -} - -/**************************************************************************/ -/*! - @brief Destructor -*/ -/**************************************************************************/ -nRF51822n::~nRF51822n(void) -{ -} - -/**************************************************************************/ -/*! - @brief Initialises anything required to start using BLE - - @returns ble_error_t - - @retval BLE_ERROR_NONE - Everything executed properly - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -ble_error_t nRF51822n::init(void) -{ - /* ToDo: Clear memory contents, reset the SD, etc. */ - btle_init(); - - reset(); - - return BLE_ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Resets the BLE HW, removing any existing services and - characteristics - - @returns ble_error_t - - @retval BLE_ERROR_NONE - Everything executed properly - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -ble_error_t nRF51822n::reset(void) -{ - wait(0.5); - - /* Wait for the radio to come back up */ - wait(1); - - return BLE_ERROR_NONE; -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "nRF51822n.h" + +#include "btle/btle.h" + +/** + * The singleton which represents the nRF51822 transport for the BLEDevice. + */ +static nRF51822n deviceInstance; + +/** + * BLE-API requires an implementation of the following function in order to + * obtain its transport handle. + */ +BLEDeviceInstanceBase * +createBLEDeviceInstance(void) +{ + return (&deviceInstance); +} + +/**************************************************************************/ +/*! + @brief Constructor +*/ +/**************************************************************************/ +nRF51822n::nRF51822n(void) +{ +} + +/**************************************************************************/ +/*! + @brief Destructor +*/ +/**************************************************************************/ +nRF51822n::~nRF51822n(void) +{ +} + +/**************************************************************************/ +/*! + @brief Initialises anything required to start using BLE + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51822n::init(void) +{ + /* ToDo: Clear memory contents, reset the SD, etc. */ + btle_init(); + + reset(); + + return BLE_ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Resets the BLE HW, removing any existing services and + characteristics + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51822n::reset(void) +{ + wait(0.5); + + /* Wait for the radio to come back up */ + wait(1); + + return BLE_ERROR_NONE; +}
--- a/nRF51822n.h Fri Jun 06 14:13:32 2014 +0100 +++ b/nRF51822n.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,49 +1,53 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __NRF51822_H__ -#define __NRF51822_H__ - -#define NRF51 -#define DEBUG_NRF_USER -#define BLE_STACK_SUPPORT_REQD -#define BOARD_PCA10001 - -#include "mbed.h" -#include "blecommon.h" -#include "hw/BLEDevice.h" -#include "nRF51Gap.h" -#include "nRF51GattServer.h" - -/**************************************************************************/ -/*! - \brief - -*/ -/**************************************************************************/ -class nRF51822n : public BLEDeviceInstanceBase -{ - public: - nRF51822n(void); - virtual ~nRF51822n(void); - - virtual Gap& getGap() { return nRF51Gap::getInstance(); }; - virtual GattServer& getGattServer() { return nRF51GattServer::getInstance(); }; - virtual ble_error_t init(void); - virtual ble_error_t reset(void); -}; - -#endif +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NRF51822_H__ +#define __NRF51822_H__ + +#define NRF51 +#define DEBUG_NRF_USER +#define BLE_STACK_SUPPORT_REQD +#define BOARD_PCA10001 + +#include "mbed.h" +#include "blecommon.h" +#include "hw/BLEDevice.h" +#include "nRF51Gap.h" +#include "nRF51GattServer.h" + +/**************************************************************************/ +/*! + \brief + +*/ +/**************************************************************************/ +class nRF51822n : public BLEDeviceInstanceBase +{ +public: + nRF51822n(void); + virtual ~nRF51822n(void); + + virtual Gap &getGap() { + return nRF51Gap::getInstance(); + }; + virtual GattServer &getGattServer() { + return nRF51GattServer::getInstance(); + }; + virtual ble_error_t init(void); + virtual ble_error_t reset(void); +}; + +#endif
--- a/nRF51Gap.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/nRF51Gap.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,295 +1,286 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "nRF51Gap.h" -#include "mbed.h" - -#include "common/common.h" -#include "ble_advdata.h" -#include "ble_hci.h" - -/**************************************************************************/ -/*! - @brief Sets the advertising parameters and payload for the device - - @param[in] params - Basic advertising details, including the advertising - delay, timeout and how the device should be advertised - @params[in] advData - The primary advertising data payload - @params[in] scanResponse - 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. - - @retval BLE_ERROR_PARAM_OUT_OF_RANGE - One of the proposed values is outside the valid range. - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -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) { - return BLE_ERROR_BUFFER_OVERFLOW; - } - - /* Make sure we have a payload! */ - if (advData.getPayloadLen() == 0) { - return BLE_ERROR_PARAM_OUT_OF_RANGE; - } - - /* 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; - // } - //} - - /* 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); - - /* 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. */ - - return BLE_ERROR_NONE; -} - -/**************************************************************************/ -/*! - @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 - - @endcode -*/ -/**************************************************************************/ -ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams ¶ms) -{ - /* 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) { - /* Min delay is slightly longer for unconnectable devices */ - if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || - (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { - return BLE_ERROR_PARAM_OUT_OF_RANGE; - } - } else { - if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || - (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)) { - /* 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)) { - return BLE_ERROR_PARAM_OUT_OF_RANGE; - } - - /* Start Advertising */ - 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(); - - ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), - BLE_ERROR_PARAM_OUT_OF_RANGE); - - state.advertising = 1; - - return BLE_ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Stops the BLE HW and disconnects from any devices - - @returns ble_error_t - - @retval BLE_ERROR_NONE - Everything executed properly - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -ble_error_t nRF51Gap::stopAdvertising(void) -{ - /* Stop Advertising */ - ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); - - state.advertising = 0; - - 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 - - @endcode -*/ -/**************************************************************************/ -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; -} - -/**************************************************************************/ -/*! - @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, 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; -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nRF51Gap.h" +#include "mbed.h" + +#include "common/common.h" +#include "ble_advdata.h" +#include "ble_hci.h" + +/**************************************************************************/ +/*! + @brief Sets the advertising parameters and payload for the device + + @param[in] params + Basic advertising details, including the advertising + delay, timeout and how the device should be advertised + @params[in] advData + The primary advertising data payload + @params[in] scanResponse + 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. + + @retval BLE_ERROR_PARAM_OUT_OF_RANGE + One of the proposed values is outside the valid range. + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +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) { + return BLE_ERROR_BUFFER_OVERFLOW; + } + + /* Make sure we have a payload! */ + if (advData.getPayloadLen() == 0) { + return BLE_ERROR_PARAM_OUT_OF_RANGE; + } + + /* 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; + // } + //} + + /* 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); + + /* 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. */ + + return BLE_ERROR_NONE; +} + +/**************************************************************************/ +/*! + @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 + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams ¶ms) +{ + /* 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) { + /* Min delay is slightly longer for unconnectable devices */ + if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || + (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { + return BLE_ERROR_PARAM_OUT_OF_RANGE; + } + } else { + if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || + (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)) { + /* 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)) { + return BLE_ERROR_PARAM_OUT_OF_RANGE; + } + + /* Start Advertising */ + 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(); + + ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), + BLE_ERROR_PARAM_OUT_OF_RANGE); + + state.advertising = 1; + + return BLE_ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Stops the BLE HW and disconnects from any devices + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51Gap::stopAdvertising(void) +{ + /* Stop Advertising */ + ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); + + state.advertising = 0; + + 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 + + @endcode +*/ +/**************************************************************************/ +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; +} + +/**************************************************************************/ +/*! + @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, 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; +}
--- a/nRF51Gap.h Fri Jun 06 14:13:32 2014 +0100 +++ b/nRF51Gap.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,64 +1,63 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __NRF51822_GAP_H__ -#define __NRF51822_GAP_H__ - -#include "mbed.h" -#include "blecommon.h" -#include "ble.h" -#include "GapAdvertisingParams.h" -#include "GapAdvertisingData.h" -#include "hw/Gap.h" - -/**************************************************************************/ -/*! - \brief - -*/ -/**************************************************************************/ -class nRF51Gap : public Gap -{ -public: - static nRF51Gap& getInstance() - { - static nRF51Gap m_instance; - return m_instance; - } - - /* Functions that must be implemented from Gap */ - virtual ble_error_t setAddress(addr_type_t type, - const uint8_t address[6]); - virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, - const GapAdvertisingData &); - virtual ble_error_t startAdvertising(const GapAdvertisingParams &); - virtual ble_error_t stopAdvertising(void); - virtual ble_error_t disconnect(void); - - void setConnectionHandle(uint16_t con_handle); - uint16_t getConnectionHandle(void); - -private: - uint16_t m_connectionHandle; - nRF51Gap() { - m_connectionHandle = BLE_CONN_HANDLE_INVALID; - } - - nRF51Gap(nRF51Gap const&); - void operator=(nRF51Gap const&); -}; - -#endif // ifndef __NRF51822_GAP_H__ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NRF51822_GAP_H__ +#define __NRF51822_GAP_H__ + +#include "mbed.h" +#include "blecommon.h" +#include "ble.h" +#include "GapAdvertisingParams.h" +#include "GapAdvertisingData.h" +#include "hw/Gap.h" + +/**************************************************************************/ +/*! + \brief + +*/ +/**************************************************************************/ +class nRF51Gap : public Gap +{ +public: + static nRF51Gap &getInstance() { + static nRF51Gap m_instance; + return m_instance; + } + + /* Functions that must be implemented from Gap */ + virtual ble_error_t setAddress(addr_type_t type, + const uint8_t address[6]); + virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, + const GapAdvertisingData &); + virtual ble_error_t startAdvertising(const GapAdvertisingParams &); + virtual ble_error_t stopAdvertising(void); + virtual ble_error_t disconnect(void); + + void setConnectionHandle(uint16_t con_handle); + uint16_t getConnectionHandle(void); + +private: + uint16_t m_connectionHandle; + nRF51Gap() { + m_connectionHandle = BLE_CONN_HANDLE_INVALID; + } + + nRF51Gap(nRF51Gap const &); + void operator=(nRF51Gap const &); +}; + +#endif // ifndef __NRF51822_GAP_H__
--- a/nRF51GattServer.cpp Fri Jun 06 14:13:32 2014 +0100 +++ b/nRF51GattServer.cpp Mon Jun 09 09:12:10 2014 +0100 @@ -1,272 +1,253 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "nRF51GattServer.h" -#include "mbed.h" - -#include "common/common.h" -#include "btle/custom/custom_helper.h" - -#include "nRF51Gap.h" - -/**************************************************************************/ -/*! - @brief Adds a new service to the GATT table on the peripheral - - @returns ble_error_t - - @retval BLE_ERROR_NONE - Everything executed properly - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -ble_error_t nRF51GattServer::addService(GattService & service) -{ - /* ToDo: Make sure we don't overflow the array, etc. */ - /* ToDo: Make sure this service UUID doesn't already exist (?) */ - /* ToDo: Basic validation */ - - /* Add the service to the nRF51 */ - ble_uuid_t nordicUUID; - nordicUUID = custom_convert_to_nordic_uuid(service.getUUID()); - ASSERT( ERROR_NONE == - sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, - &nordicUUID, - service.getHandlePtr()), - BLE_ERROR_PARAM_OUT_OF_RANGE ); - - /* Add characteristics to the service */ - for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { - GattCharacteristic *p_char = service.getCharacteristic(i); - - nordicUUID = custom_convert_to_nordic_uuid(p_char->getUUID()); - - ASSERT ( ERROR_NONE == - custom_add_in_characteristic(service.getHandle(), - &nordicUUID, - p_char->getProperties(), - NULL, - p_char->getMinLength(), - p_char->getMaxLength(), - &nrfCharacteristicHandles[ - characteristicCount]), - BLE_ERROR_PARAM_OUT_OF_RANGE ); - - /* Update the characteristic handle */ - p_char->setHandle(characteristicCount); - p_characteristics[characteristicCount++] = p_char; - } - - serviceCount++; - - return BLE_ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Reads the value of a characteristic, based on the service - and characteristic index fields - - @param[in] charHandle - The handle of the GattCharacteristic to read from - @param[in] buffer - Buffer to hold the the characteristic's value - (raw byte array in LSB format) - @param[in] len - The number of bytes read into the buffer - - @returns ble_error_t - - @retval BLE_ERROR_NONE - Everything executed properly - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -ble_error_t nRF51GattServer::readValue(uint16_t charHandle, - uint8_t buffer[], - uint16_t len) -{ - ASSERT( ERROR_NONE == - sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle]. - value_handle, 0, - &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE); - - return BLE_ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Updates the value of a characteristic, based on the service - and characteristic index fields - - @param[in] charHandle - The handle of the GattCharacteristic to write to - @param[in] buffer - Data to use when updating the characteristic's value - (raw byte array in LSB format) - @param[in] len - The number of bytes in buffer - - @returns ble_error_t - - @retval BLE_ERROR_NONE - Everything executed properly - - @section EXAMPLE - - @code - - @endcode -*/ -/**************************************************************************/ -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]->getProperties() & - (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && - (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) { - /* HVX update for the characteristic value */ - ble_gatts_hvx_params_t hvx_params; - - hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; - hvx_params.type = - (p_characteristics[charHandle]->getProperties() & - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? - BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; - hvx_params.offset = 0; - hvx_params.p_data = buffer; - hvx_params.p_len = &len; - - 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) && - (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && - (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) { - ASSERT_INT( ERROR_NONE, - sd_ble_gatts_value_set(nrfCharacteristicHandles[ - charHandle]. - value_handle, - 0, - &len, - buffer), - BLE_ERROR_PARAM_OUT_OF_RANGE ); - } - } else { - ASSERT_INT( ERROR_NONE, - sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle]. - value_handle, - 0, - &len, - buffer), - BLE_ERROR_PARAM_OUT_OF_RANGE ); - } - - return BLE_ERROR_NONE; -} - -/**************************************************************************/ -/*! - @brief Callback handler for events getting pushed up from the SD -*/ -/**************************************************************************/ -void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) -{ - uint16_t handle_value; - GattServerEvents::gattEvent_t event; - - switch (p_ble_evt->header.evt_id) { - case BLE_GATTS_EVT_WRITE: - /* There are 2 use case here: Values being updated & CCCD - *(indicate/notify) enabled */ - - /* 1.) Handle CCCD changes */ - handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; - for (uint8_t i = 0; i<characteristicCount; i++) { - if ((p_characteristics[i]->getProperties() & - (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && - (nrfCharacteristicHandles[i].cccd_handle == handle_value)) { - uint16_t cccd_value = - (p_ble_evt->evt.gatts_evt.params.write.data[1] << - 8) | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* - * Little Endian but M0 may - * be mis-aligned */ - - if (((p_characteristics[i]->getProperties() & - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && - (cccd_value & BLE_GATT_HVX_INDICATION)) || - ((p_characteristics[i]->getProperties() & - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && - (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { - event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; - } else { - event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; - } - - handleEvent(event, i); - return; - } - } - - /* 2.) Changes to the characteristic value will be handled with other - *events below */ - event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; - break; - - case BLE_GATTS_EVT_HVC: - /* Indication confirmation received */ - event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; - handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; - break; - - default: - return; - } - - /* Find index (charHandle) in the pool */ - for (uint8_t i = 0; i<characteristicCount; i++) { - if (nrfCharacteristicHandles[i].value_handle == handle_value) { - handleEvent(event, i); - break; - } - } -} +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nRF51GattServer.h" +#include "mbed.h" + +#include "common/common.h" +#include "btle/custom/custom_helper.h" + +#include "nRF51Gap.h" + +/**************************************************************************/ +/*! + @brief Adds a new service to the GATT table on the peripheral + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51GattServer::addService(GattService &service) +{ + /* ToDo: Make sure we don't overflow the array, etc. */ + /* ToDo: Make sure this service UUID doesn't already exist (?) */ + /* ToDo: Basic validation */ + + /* Add the service to the nRF51 */ + ble_uuid_t nordicUUID; + nordicUUID = custom_convert_to_nordic_uuid(service.getUUID()); + ASSERT( ERROR_NONE == + sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, + &nordicUUID, + service.getHandlePtr()), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + + /* Add characteristics to the service */ + for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { + GattCharacteristic *p_char = service.getCharacteristic(i); + + nordicUUID = custom_convert_to_nordic_uuid(p_char->getUUID()); + + ASSERT ( ERROR_NONE == + custom_add_in_characteristic(service.getHandle(), + &nordicUUID, + p_char->getProperties(), + NULL, + p_char->getMinLength(), + p_char->getMaxLength(), + &nrfCharacteristicHandles[characteristicCount]), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + + /* Update the characteristic handle */ + p_char->setHandle(characteristicCount); + p_characteristics[characteristicCount++] = p_char; + } + + serviceCount++; + + return BLE_ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Reads the value of a characteristic, based on the service + and characteristic index fields + + @param[in] charHandle + The handle of the GattCharacteristic to read from + @param[in] buffer + Buffer to hold the the characteristic's value + (raw byte array in LSB format) + @param[in] len + The number of bytes read into the buffer + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +ble_error_t nRF51GattServer::readValue(uint16_t charHandle, + uint8_t buffer[], + uint16_t len) +{ + ASSERT( ERROR_NONE == + sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), + BLE_ERROR_PARAM_OUT_OF_RANGE); + + return BLE_ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Updates the value of a characteristic, based on the service + and characteristic index fields + + @param[in] charHandle + The handle of the GattCharacteristic to write to + @param[in] buffer + Data to use when updating the characteristic's value + (raw byte array in LSB format) + @param[in] len + The number of bytes in buffer + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @section EXAMPLE + + @code + + @endcode +*/ +/**************************************************************************/ +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]->getProperties() & + (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && + (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) { + /* HVX update for the characteristic value */ + ble_gatts_hvx_params_t hvx_params; + + hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; + hvx_params.type = + (p_characteristics[charHandle]->getProperties() & + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? + BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; + hvx_params.offset = 0; + hvx_params.p_data = buffer; + hvx_params.p_len = &len; + + 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) && + (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && + (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) { + ASSERT_INT( ERROR_NONE, + sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + } + } else { + ASSERT_INT( ERROR_NONE, + sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + } + + return BLE_ERROR_NONE; +} + +/**************************************************************************/ +/*! + @brief Callback handler for events getting pushed up from the SD +*/ +/**************************************************************************/ +void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) +{ + uint16_t handle_value; + GattServerEvents::gattEvent_t event; + + switch (p_ble_evt->header.evt_id) { + case BLE_GATTS_EVT_WRITE: + /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ + + /* 1.) Handle CCCD changes */ + handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; + for (uint8_t i = 0; i<characteristicCount; i++) { + if ((p_characteristics[i]->getProperties() & + (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && + (nrfCharacteristicHandles[i].cccd_handle == handle_value)) { + uint16_t cccd_value = + (p_ble_evt->evt.gatts_evt.params.write.data[1] << 8) | + p_ble_evt->evt.gatts_evt.params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ + + if (((p_characteristics[i]->getProperties() & + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && + (cccd_value & BLE_GATT_HVX_INDICATION)) || + ((p_characteristics[i]->getProperties() & + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && + (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { + event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; + } else { + event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; + } + + handleEvent(event, i); + return; + } + } + + /* 2.) Changes to the characteristic value will be handled with other + * events below */ + event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; + break; + + case BLE_GATTS_EVT_HVC: + /* Indication confirmation received */ + event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; + handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; + break; + + default: + return; + } + + /* Find index (charHandle) in the pool */ + for (uint8_t i = 0; i<characteristicCount; i++) { + if (nrfCharacteristicHandles[i].value_handle == handle_value) { + handleEvent(event, i); + break; + } + } +}
--- a/nRF51GattServer.h Fri Jun 06 14:13:32 2014 +0100 +++ b/nRF51GattServer.h Mon Jun 09 09:12:10 2014 +0100 @@ -1,62 +1,64 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __NRF51822_GATT_SERVER_H__ -#define __NRF51822_GATT_SERVER_H__ - -#include "mbed.h" -#include "blecommon.h" -#include "ble.h" /* nordic ble */ -#include "GattService.h" -#include "hw/GattServer.h" - -#define BLE_TOTAL_CHARACTERISTICS 10 - -/**************************************************************************/ -/*! - \brief - -*/ -/**************************************************************************/ -class nRF51GattServer : public GattServer -{ - public: - static nRF51GattServer& getInstance() - { - static nRF51GattServer m_instance; - return m_instance; - } - - /* Functions that must be implemented from GattServer */ - virtual ble_error_t addService(GattService &); - virtual ble_error_t readValue(uint16_t, uint8_t[], uint16_t); - virtual ble_error_t updateValue(uint16_t, uint8_t[], uint16_t, bool localOnly = false); - - /* nRF51 Functions */ - void eventCallback(void); - void hwCallback(ble_evt_t * p_ble_evt); - - private: - GattCharacteristic* p_characteristics[BLE_TOTAL_CHARACTERISTICS]; - ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS]; - - nRF51GattServer() { serviceCount = 0; characteristicCount = 0; }; - - nRF51GattServer(nRF51GattServer const&); - void operator=(nRF51GattServer const&); -}; - -#endif +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NRF51822_GATT_SERVER_H__ +#define __NRF51822_GATT_SERVER_H__ + +#include "mbed.h" +#include "blecommon.h" +#include "ble.h" /* nordic ble */ +#include "GattService.h" +#include "hw/GattServer.h" + +#define BLE_TOTAL_CHARACTERISTICS 10 + +/**************************************************************************/ +/*! + \brief + +*/ +/**************************************************************************/ +class nRF51GattServer : public GattServer +{ +public: + static nRF51GattServer &getInstance() { + static nRF51GattServer m_instance; + return m_instance; + } + + /* Functions that must be implemented from GattServer */ + virtual ble_error_t addService(GattService &); + virtual ble_error_t readValue(uint16_t, uint8_t[], uint16_t); + virtual ble_error_t updateValue(uint16_t, uint8_t[], uint16_t, bool localOnly = false); + + /* nRF51 Functions */ + void eventCallback(void); + void hwCallback(ble_evt_t *p_ble_evt); + +private: + GattCharacteristic *p_characteristics[BLE_TOTAL_CHARACTERISTICS]; + ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS]; + + nRF51GattServer() { + serviceCount = 0; + characteristicCount = 0; + }; + + nRF51GattServer(nRF51GattServer const &); + void operator=(nRF51GattServer const &); +}; + +#endif