Commit 3d7a4d73 authored by Pierre Guinault's avatar Pierre Guinault
Browse files

Adding DFU in ble_dsk

parent 5d4061cd
Pipeline #8 skipped
pca10040/s132/arm5/_build/
pca10036/s132/arm5/_build/
pca10028/s132/arm5/_build/
pca10040/s132/arm5/_build/*
pca10036/s132/arm5/_build/*
pca10028/s132/arm5/_build/*
*.TMP
......@@ -36,6 +36,17 @@
#define __BLE_DSK_MAX_STRING_SIZE 64 // maximum size of the String type structures
#define __BLE_DSK_MAX_DATA_SIZE 16 // maximum size of any of the data exchanged over the characteristics
#if WITH_DFU_SERVICE == 1
#define IS_SRVC_CHANGED_CHARACT_PRESENT 1
#define DFU_REV_MAJOR 0x00 /** DFU Major revision number to be exposed. */
#define DFU_REV_MINOR 0x01 /** DFU Minor revision number to be exposed. */
#define DFU_REVISION ((DFU_REV_MAJOR << 8) | DFU_REV_MINOR) /** DFU Revision number to be exposed. Combined of major and minor versions. */
#define APP_SERVICE_HANDLE_START 0x000C /**< Handle of first application specific service when when service changed characteristic is present. */
#define BLE_HANDLE_MAX 0xFFFF /**< Max handle value in BLE. */
#endif // BLE_DFU_APP_SUPPORT
#ifndef APP_TIMER_PRESCALER
#define APP_TIMER_PRESCALER 0 // Value of the RTC1 PRESCALER register.
#endif
......
......@@ -35,11 +35,13 @@
#include "ble_dsk_service.h"
#include "softdevice_handler.h"
#include "SEGGER_RTT.h"
#include "nrf_delay.h"
#define CENTRAL_LINK_COUNT 0 /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
#define PERIPHERAL_LINK_COUNT 1
/* ---------------------------------------------------------------------
* Global variable with all elements on peripherical
* ---------------------------------------------------------------------
......@@ -83,6 +85,10 @@ void ble_dsk_peripherical_init(const ble_dsk_peripherical_t * _init)
&ble_enable_params);
APP_ERROR_CHECK(err_code);
#endif
#if WITH_DFU_SERVCICE == 1
STATIC_ASSERT(BLE_DSK_CONFIG_SERVICE_CHANGE_CHAR); /** When having DFU Service support in application the Service Changed Characteristic should always be present. */
#endif
ble_enable_params.gatts_enable_params.service_changed = _p->peripherical->config_bitfield & BLE_DSK_CONFIG_SERVICE_CHANGE_CHAR;
// Enable BLE stack.
......@@ -115,7 +121,7 @@ void ble_dsk_peripherical_init(const ble_dsk_peripherical_t * _init)
{
dm_init_param_t init_param = {.clear_persistent_data = _p->erase_bonds};
dm_application_param_t register_param;
err_code = dm_init(&init_param);
APP_ERROR_CHECK(err_code);
......@@ -198,10 +204,12 @@ void ble_dsk_peripherical_init(const ble_dsk_peripherical_t * _init)
}
#endif
#if WITH_DFU_SERVICE == 1
if ( (_p->peripherical->basic_service & BLE_DSK_SERVICE_DFU) != 0 ) {
/* if ( (_p->peripherical->basic_service & BLE_DSK_SERVICE_DFU) != 0 ) {
_p->m_adv_uuids[_p->service_count].uuid = BLE_UUID_BATTERY_SERVICE;
_p->m_adv_uuids[_p->service_count].type = BLE_UUID_TYPE_BLE;
// @TODO - DFU Initiate ...
_p->service_count++;
}
}*/
#endif
_p->uuid_type = BLE_UUID_TYPE_BLE;
if ( _p->peripherical->uuid_128 ) {
......@@ -282,15 +290,16 @@ void ble_dsk_peripherical_init(const ble_dsk_peripherical_t * _init)
dfus_init.evt_handler = dfu_app_on_dfu_evt;
dfus_init.revision = DFU_REVISION;
err_code = ble_dfu_init(&m_dfus, &dfus_init);
err_code = ble_dfu_init(&_p->m_dfus, &dfus_init);
APP_ERROR_CHECK(err_code);
dfu_app_reset_prepare_set(reset_prepare);
dfu_app_dm_appl_instance_set(m_app_handle);
dfu_app_dm_appl_instance_set(_p->m_app_handle);
/** @snippet [DFU BLE Service initialization] */
}
}
#endif
// Custom Services creation - init all at once
ble_dsk_service_init();
......@@ -415,6 +424,11 @@ static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
ble_bas_on_ble_evt(&_p->m_bas, p_ble_evt);
#endif
#if WITH_DFU_SERVICE == 1
/** @snippet [Propagating BLE Stack events to DFU Service] */
ble_dfu_on_ble_evt(&_p->m_dfus, p_ble_evt);
/** @snippet [Propagating BLE Stack events to DFU Service] */
#endif
// custom services
ble_dsk_on_event_dispatch(p_ble_evt);
......@@ -495,6 +509,104 @@ static uint32_t device_manager_evt_handler(dm_handle_t const * p_handle,
app_context_load(p_handle);
}
#endif
return NRF_SUCCESS;
}
#if WITH_DFU_SERVICE == 1
/**@brief Function for stopping advertising.
*/
static void advertising_stop(void)
{
uint32_t err_code;
err_code = sd_ble_gap_adv_stop();
APP_ERROR_CHECK(err_code);
// err_code = bsp_indication_set(BSP_INDICATE_IDLE);
// APP_ERROR_CHECK(err_code);
}
/**@brief Function for loading application-specific context after establishing a secure connection.
*
* @details This function will load the application context and check if the ATT table is marked as
* changed. If the ATT table is marked as changed, a Service Changed Indication
* is sent to the peer if the Service Changed CCCD is set to indicate.
*
* @param[in] p_handle The Device Manager handle that identifies the connection for which the context
* should be loaded.
*/
static void app_context_load(dm_handle_t const * p_handle)
{
uint32_t err_code;
static uint32_t context_data;
dm_application_context_t context;
context.len = sizeof(context_data);
context.p_data = (uint8_t *)&context_data;
err_code = dm_application_context_get(p_handle, &context);
if (err_code == NRF_SUCCESS)
{
// Send Service Changed Indication if ATT table has changed.
if ((context_data & (DFU_APP_ATT_TABLE_CHANGED << DFU_APP_ATT_TABLE_POS)) != 0)
{
err_code = sd_ble_gatts_service_changed(_p->m_conn_handle, APP_SERVICE_HANDLE_START, BLE_HANDLE_MAX);
if ((err_code != NRF_SUCCESS) &&
(err_code != BLE_ERROR_INVALID_CONN_HANDLE) &&
(err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != BLE_ERROR_NO_TX_PACKETS) &&
(err_code != NRF_ERROR_BUSY) &&
(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING))
{
APP_ERROR_HANDLER(err_code);
}
}
err_code = dm_application_context_delete(p_handle);
APP_ERROR_CHECK(err_code);
}
else if (err_code == DM_NO_APP_CONTEXT)
{
// No context available. Ignore.
}
else
{
APP_ERROR_HANDLER(err_code);
}
}
/** @snippet [DFU BLE Reset prepare] */
/**@brief Function for preparing for system reset.
*
* @details This function implements @ref dfu_app_reset_prepare_t. It will be called by
* @ref dfu_app_handler.c before entering the bootloader/DFU.
* This allows the current running application to shut down gracefully.
*/
static void reset_prepare(void)
{
uint32_t err_code;
if (_p->m_conn_handle != BLE_CONN_HANDLE_INVALID)
{
// Disconnect from peer.
err_code = sd_ble_gap_disconnect(_p->m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
//err_code = bsp_indication_set(BSP_INDICATE_IDLE);
//APP_ERROR_CHECK(err_code);
}
else
{
// If not connected, the device will be advertising. Hence stop the advertising.
advertising_stop();
}
err_code = ble_conn_params_stop();
APP_ERROR_CHECK(err_code);
nrf_delay_ms(500);
}
/** @snippet [DFU BLE Reset prepare] */
#endif
......@@ -40,7 +40,7 @@
// Includes DFU(Device Firmware Update) - unactivate by setting to 0 in your main file
#ifndef WITH_DFU_SERVICE
#define WITH_DFU_SERVICE 0
#define WITH_DFU_SERVICE 1
#endif
#include "ble_gattc.h"
......@@ -62,6 +62,7 @@
#endif
#if WITH_DFU_SERVICE == 1
#include "ble_dfu.h"
#include "dfu_app_handler.h"
#endif
......@@ -226,6 +227,11 @@ typedef struct ble_dsk_dyn_peripherical_s {
#if WITH_BAS_SERVICE == 1
ble_bas_t m_bas;
#endif
#if WITH_DFU_SERVICE == 1
ble_dfu_t m_dfus; /**< Structure used to identify the DFU service. */
dm_application_instance_t m_app_handle; /**< Application identifier allocated by device manager */
#endif
// Custom services
uint8_t service_handler[__BLE_DSK_MAX_SERVICES]; // index to the corresponding service dynamic structure
......@@ -255,5 +261,9 @@ static void on_conn_params_evt(ble_conn_params_evt_t * p_evt);
static void conn_params_error_handler(uint32_t nrf_error);
static uint32_t device_manager_evt_handler(dm_handle_t const * p_handle, dm_event_t const * p_event, ret_code_t event_result);
#if WITH_DFU_SERVICE == 1
static void advertising_stop(void);
static void app_context_load(dm_handle_t const * p_handle);
static void reset_prepare(void);
#endif
#endif // __BLE_DSK_PERIPHERICAL__
......@@ -148,7 +148,7 @@ const ble_dsk_peripherical_t myPeriph = {
/* add_mac_address after name */ false,
/* Manufacturer name */ "disk91.com",
/* Appearance type */ BLE_APPEARANCE_UNKNOWN,
/* Basic Service to enable */ BLE_DSK_SERVICE_BAS | BLE_DSK_SERVICE_DIS,
/* Basic Service to enable */ BLE_DSK_SERVICE_BAS | BLE_DSK_SERVICE_DIS | BLE_DSK_SERVICE_DFU,
/* UUID are 128b format */ false,
/* 128b uuid to be used */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
/* Configuration bitfield */ BLE_DSK_CONFIG_SERVICE_CHANGE_CHAR,
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment