seperate files from latest SDK (currently 14.2.0) from good old non-
secure bootloader sdk 11
This commit is contained in:
hathach
2018-04-05 00:35:08 +07:00
parent f18643ae10
commit df71d3444d
151 changed files with 3176 additions and 4107 deletions

View File

@ -0,0 +1,649 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "ble_dfu.h"
#include "ble_types.h"
#include "ble_gatts.h"
#include "ble_srv_common.h"
#include <stddef.h>
#include "sdk_common.h"
#define MAX_DFU_PKT_LEN 20 /**< Maximum length (in bytes) of the DFU Packet characteristic. */
#define PKT_START_DFU_PARAM_LEN 2 /**< Length (in bytes) of the parameters for Packet Start DFU Request. */
#define PKT_INIT_DFU_PARAM_LEN 2 /**< Length (in bytes) of the parameters for Packet Init DFU Request. */
#define PKT_RCPT_NOTIF_REQ_LEN 3 /**< Length (in bytes) of the Packet Receipt Notification Request. */
#define MAX_PKTS_RCPT_NOTIF_LEN 6 /**< Maximum length (in bytes) of the Packets Receipt Notification. */
#define MAX_RESPONSE_LEN 7 /**< Maximum length (in bytes) of the response to a Control Point command. */
#define MAX_NOTIF_BUFFER_LEN MAX(MAX_PKTS_RCPT_NOTIF_LEN, MAX_RESPONSE_LEN) /**< Maximum length (in bytes) of the buffer needed by DFU Service while sending notifications to peer. */
enum
{
OP_CODE_START_DFU = 1, /**< Value of the Op code field for 'Start DFU' command.*/
OP_CODE_RECEIVE_INIT = 2, /**< Value of the Op code field for 'Initialize DFU parameters' command.*/
OP_CODE_RECEIVE_FW = 3, /**< Value of the Op code field for 'Receive firmware image' command.*/
OP_CODE_VALIDATE = 4, /**< Value of the Op code field for 'Validate firmware' command.*/
OP_CODE_ACTIVATE_N_RESET = 5, /**< Value of the Op code field for 'Activate & Reset' command.*/
OP_CODE_SYS_RESET = 6, /**< Value of the Op code field for 'Reset System' command.*/
OP_CODE_IMAGE_SIZE_REQ = 7, /**< Value of the Op code field for 'Report received image size' command.*/
OP_CODE_PKT_RCPT_NOTIF_REQ = 8, /**< Value of the Op code field for 'Request packet receipt notification.*/
OP_CODE_RESPONSE = 16, /**< Value of the Op code field for 'Response.*/
OP_CODE_PKT_RCPT_NOTIF = 17 /**< Value of the Op code field for 'Packets Receipt Notification'.*/
};
static bool m_is_dfu_service_initialized = false; /**< Variable to check if the DFU service was initialized by the application.*/
static uint8_t m_notif_buffer[MAX_NOTIF_BUFFER_LEN]; /**< Buffer used for sending notifications to peer. */
/**@brief Function for adding DFU Packet characteristic to the BLE Stack.
*
* @param[in] p_dfu DFU Service structure.
*
* @return NRF_SUCCESS on success. Otherwise an error code.
*/
static uint32_t dfu_pkt_char_add(ble_dfu_t * const p_dfu)
{
ble_gatts_char_md_t char_md;
ble_gatts_attr_t attr_char_value;
ble_uuid_t char_uuid;
ble_gatts_attr_md_t attr_md;
memset(&char_md, 0, sizeof(char_md));
char_md.char_props.write_wo_resp = 1;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_cccd_md = NULL;
char_md.p_sccd_md = NULL;
char_uuid.type = p_dfu->uuid_type;
char_uuid.uuid = BLE_DFU_PKT_CHAR_UUID;
memset(&attr_md, 0, sizeof(attr_md));
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 0;
attr_md.vlen = 1;
memset(&attr_char_value, 0, sizeof(attr_char_value));
attr_char_value.p_uuid = &char_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = 0;
attr_char_value.init_offs = 0;
attr_char_value.max_len = MAX_DFU_PKT_LEN;
attr_char_value.p_value = NULL;
return sd_ble_gatts_characteristic_add(p_dfu->service_handle,
&char_md,
&attr_char_value,
&p_dfu->dfu_pkt_handles);
}
/**@brief Function for adding DFU Revision characteristic to the BLE Stack.
*
* @param[in] p_dfu DFU Service structure.
*
* @return NRF_SUCCESS on success. Otherwise an error code.
*/
static uint32_t dfu_rev_char_add(ble_dfu_t * const p_dfu, ble_dfu_init_t const * const p_dfu_init)
{
ble_gatts_char_md_t char_md;
ble_gatts_attr_t attr_char_value;
ble_uuid_t char_uuid;
ble_gatts_attr_md_t attr_md;
memset(&char_md, 0, sizeof(char_md));
char_md.char_props.read = 1;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_cccd_md = NULL;
char_md.p_sccd_md = NULL;
char_uuid.type = p_dfu->uuid_type;
char_uuid.uuid = BLE_DFU_REV_CHAR_UUID;
memset(&attr_md, 0, sizeof(attr_md));
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 0;
attr_md.vlen = 1;
memset(&attr_char_value, 0, sizeof(attr_char_value));
attr_char_value.p_uuid = &char_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = sizeof(uint16_t);
attr_char_value.init_offs = 0;
attr_char_value.max_len = sizeof(uint16_t);
attr_char_value.p_value = (uint8_t *)&p_dfu_init->revision;
return sd_ble_gatts_characteristic_add(p_dfu->service_handle,
&char_md,
&attr_char_value,
&p_dfu->dfu_rev_handles);
}
/**@brief Function for adding DFU Control Point characteristic to the BLE Stack.
*
* @param[in] p_dfu DFU Service structure.
*
* @return NRF_SUCCESS on success. Otherwise an error code.
*/
static uint32_t dfu_ctrl_pt_add(ble_dfu_t * const p_dfu)
{
ble_gatts_char_md_t char_md;
ble_gatts_attr_t attr_char_value;
ble_uuid_t char_uuid;
ble_gatts_attr_md_t attr_md;
memset(&char_md, 0, sizeof(char_md));
char_md.char_props.write = 1;
char_md.char_props.notify = 1;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_cccd_md = NULL;
char_md.p_sccd_md = NULL;
char_uuid.type = p_dfu->uuid_type;
char_uuid.uuid = BLE_DFU_CTRL_PT_UUID;
memset(&attr_md, 0, sizeof(attr_md));
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 1;
attr_md.vlen = 1;
memset(&attr_char_value, 0, sizeof(attr_char_value));
attr_char_value.p_uuid = &char_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = 0;
attr_char_value.init_offs = 0;
attr_char_value.max_len = BLE_GATT_ATT_MTU_DEFAULT;
attr_char_value.p_value = NULL;
return sd_ble_gatts_characteristic_add(p_dfu->service_handle,
&char_md,
&attr_char_value,
&p_dfu->dfu_ctrl_pt_handles);
}
/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the S110 SoftDevice.
*
* @param[in] p_dfu DFU Service Structure.
* @param[in] p_ble_evt Pointer to the event received from BLE stack.
*/
static void on_connect(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt)
{
p_dfu->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
}
/**@brief Function for checking if the CCCD of DFU Control point is configured for Notification.
*
* @details This function checks if the CCCD of DFU Control Point characteristic is configured
* for Notification by the DFU Controller.
*
* @param[in] p_dfu DFU Service structure.
*
* @return True if the CCCD of DFU Control Point characteristic is configured for Notification.
* False otherwise.
*/
static bool is_cccd_configured(ble_dfu_t * p_dfu)
{
// Check if the CCCDs are configured.
uint8_t cccd_val_buf[BLE_CCCD_VALUE_LEN];
ble_gatts_value_t gatts_value;
// Initialize value struct.
memset(&gatts_value, 0, sizeof(gatts_value));
gatts_value.len = BLE_CCCD_VALUE_LEN;
gatts_value.offset = 0;
gatts_value.p_value = cccd_val_buf;
// Check the CCCD Value of DFU Control Point.
uint32_t err_code = sd_ble_gatts_value_get(p_dfu->conn_handle,
p_dfu->dfu_ctrl_pt_handles.cccd_handle,
&gatts_value);
if (err_code != NRF_SUCCESS)
{
if (p_dfu->error_handler != NULL)
{
p_dfu->error_handler(err_code);
}
return false;
}
return ble_srv_is_notification_enabled(cccd_val_buf);
}
/**@brief Function for handling a Write event on the Control Point characteristic.
*
* @param[in] p_dfu DFU Service Structure.
* @param[in] p_ble_write_evt Pointer to the write event received from BLE stack.
*
* @return NRF_SUCCESS on successful processing of control point write. Otherwise an error code.
*/
static uint32_t on_ctrl_pt_write(ble_dfu_t * p_dfu, ble_gatts_evt_write_t * p_ble_write_evt)
{
ble_gatts_rw_authorize_reply_params_t auth_reply;
auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
auth_reply.params.write.update = 1;
auth_reply.params.write.offset = p_ble_write_evt->offset;
auth_reply.params.write.len = p_ble_write_evt->len;
auth_reply.params.write.p_data = p_ble_write_evt->data;
if (!is_cccd_configured(p_dfu))
{
// Send an error response to the peer indicating that the CCCD is improperly configured.
auth_reply.params.write.gatt_status =
BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
return (sd_ble_gatts_rw_authorize_reply(p_dfu->conn_handle, &auth_reply));
}
else
{
uint32_t err_code;
auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
err_code = (sd_ble_gatts_rw_authorize_reply(p_dfu->conn_handle, &auth_reply));
VERIFY_SUCCESS(err_code);
}
ble_dfu_evt_t ble_dfu_evt;
switch (p_ble_write_evt->data[0])
{
case OP_CODE_START_DFU:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_START;
if (p_ble_write_evt->len < PKT_START_DFU_PARAM_LEN)
{
return ble_dfu_response_send(p_dfu,
(ble_dfu_procedure_t) p_ble_write_evt->data[0],
BLE_DFU_RESP_VAL_OPER_FAILED);
}
ble_dfu_evt.evt.ble_dfu_pkt_write.len = 1;
ble_dfu_evt.evt.ble_dfu_pkt_write.p_data = &(p_ble_write_evt->data[1]);
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_RECEIVE_INIT:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_RECEIVE_INIT_DATA;
if (p_ble_write_evt->len < PKT_INIT_DFU_PARAM_LEN)
{
return ble_dfu_response_send(p_dfu,
(ble_dfu_procedure_t) p_ble_write_evt->data[0],
BLE_DFU_RESP_VAL_OPER_FAILED);
}
ble_dfu_evt.evt.ble_dfu_pkt_write.len = 1;
ble_dfu_evt.evt.ble_dfu_pkt_write.p_data = &(p_ble_write_evt->data[1]);
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_RECEIVE_FW:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_RECEIVE_APP_DATA;
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_VALIDATE:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_VALIDATE;
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_ACTIVATE_N_RESET:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_ACTIVATE_N_RESET;
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_SYS_RESET:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_SYS_RESET;
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_PKT_RCPT_NOTIF_REQ:
if (p_ble_write_evt->len < PKT_RCPT_NOTIF_REQ_LEN)
{
return (ble_dfu_response_send(p_dfu,
BLE_DFU_PKT_RCPT_REQ_PROCEDURE,
BLE_DFU_RESP_VAL_NOT_SUPPORTED));
}
ble_dfu_evt.evt.pkt_rcpt_notif_req.num_of_pkts =
uint16_decode(&(p_ble_write_evt->data[1]));
if (ble_dfu_evt.evt.pkt_rcpt_notif_req.num_of_pkts == 0)
{
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_PKT_RCPT_NOTIF_DISABLED;
}
else
{
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_PKT_RCPT_NOTIF_ENABLED;
}
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
case OP_CODE_IMAGE_SIZE_REQ:
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_BYTES_RECEIVED_SEND;
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
break;
default:
// Unsupported op code.
return ble_dfu_response_send(p_dfu,
(ble_dfu_procedure_t) p_ble_write_evt->data[0],
BLE_DFU_RESP_VAL_NOT_SUPPORTED);
}
return NRF_SUCCESS;
}
/**@brief Function for handling the @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST event from the S110
* Stack.
*
* @param[in] p_dfu DFU Service Structure.
* @param[in] p_ble_evt Pointer to the event received from BLE stack.
*/
static void on_rw_authorize_req(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt)
{
ble_gatts_evt_rw_authorize_request_t * p_authorize_request;
p_authorize_request = &(p_ble_evt->evt.gatts_evt.params.authorize_request);
if (
(p_authorize_request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
&&
(p_authorize_request->request.write.handle == p_dfu->dfu_ctrl_pt_handles.value_handle)
&&
(p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op != BLE_GATTS_OP_PREP_WRITE_REQ)
&&
(p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
&&
(p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
)
{
uint32_t err_code;
err_code = on_ctrl_pt_write(p_dfu, &(p_authorize_request->request.write));
if (err_code != NRF_SUCCESS && p_dfu->error_handler != NULL)
{
p_dfu->error_handler(err_code);
}
}
}
/**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the S110 SoftDevice.
*
* @param[in] p_dfu DFU Service Structure.
* @param[in] p_ble_evt Pointer to the event received from BLE stack.
*/
static void on_write(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt)
{
if (p_ble_evt->evt.gatts_evt.params.write.handle == p_dfu->dfu_pkt_handles.value_handle)
{
// DFU Packet written
ble_dfu_evt_t ble_dfu_evt;
ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_PACKET_WRITE;
ble_dfu_evt.evt.ble_dfu_pkt_write.len = p_ble_evt->evt.gatts_evt.params.write.len;
ble_dfu_evt.evt.ble_dfu_pkt_write.p_data = p_ble_evt->evt.gatts_evt.params.write.data;
p_dfu->evt_handler(p_dfu, &ble_dfu_evt);
}
}
/**@brief Function for handling the BLE_GAP_EVT_DISCONNECTED event from the S110 SoftDevice.
*
* @param[in] p_dfu DFU Service Structure.
* @param[in] p_ble_evt Pointer to the event received from BLE stack.
*/
static void on_disconnect(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt)
{
p_dfu->conn_handle = BLE_CONN_HANDLE_INVALID;
}
uint32_t ble_dfu_init(ble_dfu_t * p_dfu, ble_dfu_init_t * p_dfu_init)
{
if ((p_dfu == NULL) || (p_dfu_init == NULL) || (p_dfu_init->evt_handler == NULL))
{
return NRF_ERROR_NULL;
}
p_dfu->conn_handle = BLE_CONN_HANDLE_INVALID;
ble_uuid_t service_uuid;
uint32_t err_code;
const ble_uuid128_t base_uuid128 =
{
{
0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00
}
};
service_uuid.uuid = BLE_DFU_SERVICE_UUID;
err_code = sd_ble_uuid_vs_add(&base_uuid128, &(service_uuid.type));
VERIFY_SUCCESS(err_code);
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&service_uuid,
&(p_dfu->service_handle));
VERIFY_SUCCESS(err_code);
p_dfu->uuid_type = service_uuid.type;
err_code = dfu_pkt_char_add(p_dfu);
VERIFY_SUCCESS(err_code);
err_code = dfu_ctrl_pt_add(p_dfu);
VERIFY_SUCCESS(err_code);
err_code = dfu_rev_char_add(p_dfu, p_dfu_init);
VERIFY_SUCCESS(err_code);
p_dfu->evt_handler = p_dfu_init->evt_handler;
if (p_dfu_init->error_handler != NULL)
{
p_dfu->error_handler = p_dfu_init->error_handler;
}
m_is_dfu_service_initialized = true;
return NRF_SUCCESS;
}
void ble_dfu_on_ble_evt(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt)
{
if ((p_dfu == NULL) || (p_ble_evt == NULL))
{
return;
}
if (p_dfu->evt_handler != NULL)
{
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
on_connect(p_dfu, p_ble_evt);
break;
case BLE_GATTS_EVT_WRITE:
on_write(p_dfu, p_ble_evt);
break;
case BLE_GAP_EVT_DISCONNECTED:
on_disconnect(p_dfu, p_ble_evt);
break;
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
on_rw_authorize_req(p_dfu, p_ble_evt);
break;
default:
// No implementation needed.
break;
}
}
}
uint32_t ble_dfu_bytes_rcvd_report(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd)
{
if (p_dfu == NULL)
{
return NRF_ERROR_NULL;
}
if ((p_dfu->conn_handle == BLE_CONN_HANDLE_INVALID) || !m_is_dfu_service_initialized)
{
return NRF_ERROR_INVALID_STATE;
}
ble_gatts_hvx_params_t hvx_params;
uint16_t index = 0;
// Encode the Op Code.
m_notif_buffer[index++] = OP_CODE_RESPONSE;
// Encode the Reqest Op Code.
m_notif_buffer[index++] = OP_CODE_IMAGE_SIZE_REQ;
// Encode the Response Value.
m_notif_buffer[index++] = (uint8_t)BLE_DFU_RESP_VAL_SUCCESS;
index += uint32_encode(num_of_firmware_bytes_rcvd, &m_notif_buffer[index]);
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = p_dfu->dfu_ctrl_pt_handles.value_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = 0;
hvx_params.p_len = &index;
hvx_params.p_data = m_notif_buffer;
return sd_ble_gatts_hvx(p_dfu->conn_handle, &hvx_params);
}
uint32_t ble_dfu_pkts_rcpt_notify(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd)
{
if (p_dfu == NULL)
{
return NRF_ERROR_NULL;
}
if ((p_dfu->conn_handle == BLE_CONN_HANDLE_INVALID) || !m_is_dfu_service_initialized)
{
return NRF_ERROR_INVALID_STATE;
}
ble_gatts_hvx_params_t hvx_params;
uint16_t index = 0;
m_notif_buffer[index++] = OP_CODE_PKT_RCPT_NOTIF;
index += uint32_encode(num_of_firmware_bytes_rcvd, &m_notif_buffer[index]);
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = p_dfu->dfu_ctrl_pt_handles.value_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = 0;
hvx_params.p_len = &index;
hvx_params.p_data = m_notif_buffer;
return sd_ble_gatts_hvx(p_dfu->conn_handle, &hvx_params);
}
uint32_t ble_dfu_response_send(ble_dfu_t * p_dfu,
ble_dfu_procedure_t dfu_proc,
ble_dfu_resp_val_t resp_val)
{
if (p_dfu == NULL)
{
return NRF_ERROR_NULL;
}
if ((p_dfu->conn_handle == BLE_CONN_HANDLE_INVALID) || !m_is_dfu_service_initialized)
{
return NRF_ERROR_INVALID_STATE;
}
ble_gatts_hvx_params_t hvx_params;
uint16_t index = 0;
m_notif_buffer[index++] = OP_CODE_RESPONSE;
// Encode the Request Op code
m_notif_buffer[index++] = (uint8_t)dfu_proc;
// Encode the Response Value.
m_notif_buffer[index++] = (uint8_t)resp_val;
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = p_dfu->dfu_ctrl_pt_handles.value_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = 0;
hvx_params.p_len = &index;
hvx_params.p_data = m_notif_buffer;
return sd_ble_gatts_hvx(p_dfu->conn_handle, &hvx_params);
}

View File

@ -0,0 +1,239 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup ble_sdk_srv_dfu Device Firmware Update Service
* @{
* @ingroup ble_sdk_srv
* @brief Device Firmware Update Service
*
* @details The Device Firmware Update (DFU) service is a GATT based service that can be used for
* performing firmware updates over BLE. Note that this implementation uses vendor
* specific UUIDs for service and characteristics and is intended to demonstrate the
* firmware updates over BLE. Refer @ref bledfu_transport_bleservice and @ref
* bledfu_transport_bleprofile for more information on the service and profile respectively.
*/
#ifndef BLE_DFU_H__
#define BLE_DFU_H__
#include <stdint.h>
#include "ble_gatts.h"
#include "ble_gap.h"
#include "ble.h"
#include "ble_srv_common.h"
#define BLE_DFU_SERVICE_UUID 0x1530 /**< The UUID of the DFU Service. */
#define BLE_DFU_PKT_CHAR_UUID 0x1532 /**< The UUID of the DFU Packet Characteristic. */
#define BLE_DFU_CTRL_PT_UUID 0x1531 /**< The UUID of the DFU Control Point. */
#define BLE_DFU_STATUS_REP_UUID 0x1533 /**< The UUID of the DFU Status Report Characteristic. */
#define BLE_DFU_REV_CHAR_UUID 0x1534 /**< The UUID of the DFU Revision Characteristic. */
/**@brief DFU Event type.
*
* @details This enumeration contains the types of events that will be received from the DFU Service.
*/
typedef enum
{
BLE_DFU_START, /**< The event indicating that the peer wants the application to prepare for a new firmware update. */
BLE_DFU_RECEIVE_INIT_DATA, /**< The event indicating that the peer wants the application to prepare to receive init parameters. */
BLE_DFU_RECEIVE_APP_DATA, /**< The event indicating that the peer wants the application to prepare to receive the new firmware image. */
BLE_DFU_VALIDATE, /**< The event indicating that the peer wants the application to validate the newly received firmware image. */
BLE_DFU_ACTIVATE_N_RESET, /**< The event indicating that the peer wants the application to undergo activate new firmware and restart with new valid application */
BLE_DFU_SYS_RESET, /**< The event indicating that the peer wants the application to undergo a reset and start the currently valid application image.*/
BLE_DFU_PKT_RCPT_NOTIF_ENABLED, /**< The event indicating that the peer has enabled packet receipt notifications. It is the responsibility of the application to call @ref ble_dfu_pkts_rcpt_notify each time the number of packets indicated by num_of_pkts field in @ref ble_dfu_evt_t is received.*/
BLE_DFU_PKT_RCPT_NOTIF_DISABLED, /**< The event indicating that the peer has disabled the packet receipt notifications.*/
BLE_DFU_PACKET_WRITE, /**< The event indicating that the peer has written a value to the 'DFU Packet' characteristic. The data received from the peer will be present in the @ref BLE_DFU_PACKET_WRITE element contained within @ref ble_dfu_evt_t.*/
BLE_DFU_BYTES_RECEIVED_SEND /**< The event indicating that the peer is requesting for the number of bytes of firmware data last received by the application. It is the responsibility of the application to call @ref ble_dfu_pkts_rcpt_notify in response to this event. */
} ble_dfu_evt_type_t;
/**@brief DFU Procedure type.
*
* @details This enumeration contains the types of DFU procedures.
*/
typedef enum
{
BLE_DFU_START_PROCEDURE = 1, /**< DFU Start procedure.*/
BLE_DFU_INIT_PROCEDURE = 2, /**< DFU Initialization procedure.*/
BLE_DFU_RECEIVE_APP_PROCEDURE = 3, /**< Firmware receiving procedure.*/
BLE_DFU_VALIDATE_PROCEDURE = 4, /**< Firmware image validation procedure .*/
BLE_DFU_PKT_RCPT_REQ_PROCEDURE = 8 /**< Packet receipt notification request procedure. */
} ble_dfu_procedure_t;
/**@brief DFU Response value type.
*/
typedef enum
{
BLE_DFU_RESP_VAL_SUCCESS = 1, /**< Success.*/
BLE_DFU_RESP_VAL_INVALID_STATE, /**< Invalid state.*/
BLE_DFU_RESP_VAL_NOT_SUPPORTED, /**< Operation not supported.*/
BLE_DFU_RESP_VAL_DATA_SIZE, /**< Data size exceeds limit.*/
BLE_DFU_RESP_VAL_CRC_ERROR, /**< CRC Error.*/
BLE_DFU_RESP_VAL_OPER_FAILED /**< Operation failed.*/
} ble_dfu_resp_val_t;
/**@brief DFU Packet structure.
*
* @details This structure contains the value of the DFU Packet characteristic as written by the
* peer and the length of the value written. It will be filled by the DFU Service when the
* peer writes to the DFU Packet characteristic.
*/
typedef struct
{
uint8_t * p_data; /**< Pointer to the received packet. This will point to a word aligned memory location.*/
uint8_t len; /**< Length of the packet received. */
} ble_dfu_pkt_write_t;
/**@brief Packet receipt notification request structure.
*
* @details This structure contains the contents of the packet receipt notification request
* sent by the DFU Controller.
*/
typedef struct
{
uint16_t num_of_pkts; /**< The number of packets of firmware data to be received by application before sending the next Packet Receipt Notification to the peer. */
} ble_pkt_rcpt_notif_req_t;
/**@brief DFU Event structure.
*
* @details This structure contains the event generated by the DFU Service based on the data
* received from the peer.
*/
typedef struct
{
ble_dfu_evt_type_t ble_dfu_evt_type; /**< Type of the event.*/
union
{
ble_dfu_pkt_write_t ble_dfu_pkt_write; /**< The DFU packet received. This field is when the @ref ble_dfu_evt_type field is set to @ref BLE_DFU_PACKET_WRITE.*/
ble_pkt_rcpt_notif_req_t pkt_rcpt_notif_req; /**< Packet receipt notification request. This field is when the @ref ble_dfu_evt_type field is set to @ref BLE_DFU_PKT_RCPT_NOTIF_ENABLED.*/
} evt;
} ble_dfu_evt_t;
// Forward declaration of the ble_dfu_t type.
typedef struct ble_dfu_s ble_dfu_t;
/**@brief DFU Service event handler type. */
typedef void (*ble_dfu_evt_handler_t) (ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt);
/**@brief DFU service structure.
*
* @details This structure contains status information related to the service.
*/
struct ble_dfu_s
{
uint16_t conn_handle; /**< Handle of the current connection (as provided by the SoftDevice). This will be BLE_CONN_HANDLE_INVALID when not in a connection. */
uint16_t revision; /**< Handle of DFU Service (as provided by the SoftDevice). */
uint16_t service_handle; /**< Handle of DFU Service (as provided by the SoftDevice). */
uint8_t uuid_type; /**< UUID type assigned for DFU Service by the SoftDevice. */
ble_gatts_char_handles_t dfu_pkt_handles; /**< Handles related to the DFU Packet characteristic. */
ble_gatts_char_handles_t dfu_ctrl_pt_handles; /**< Handles related to the DFU Control Point characteristic. */
ble_gatts_char_handles_t dfu_status_rep_handles; /**< Handles related to the DFU Status Report characteristic. */
ble_gatts_char_handles_t dfu_rev_handles; /**< Handles related to the DFU Revision characteristic. */
ble_dfu_evt_handler_t evt_handler; /**< The event handler to be called when an event is to be sent to the application.*/
ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
};
/**@brief DFU service initialization structure.
*
* @details This structure contains the initialization information for the DFU Service. The
* application needs to fill this structure and pass it to the DFU Service using the
* @ref ble_dfu_init function.
*/
typedef struct
{
uint16_t revision; /**< Revision number to be exposed by the DFU service. */
ble_dfu_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Device Firmware Update Service. */
ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
} ble_dfu_init_t;
/**@brief Function for handling a BLE event.
*
* @details The DFU service expects the application to call this function each time an event
* is received from the SoftDevice. This function processes the event, if it is
* relevant for the DFU service and calls the DFU event handler of the application if
* necessary.
*
* @param[in] p_dfu Pointer to the DFU service structure.
* @param[in] p_ble_evt Pointer to the event received from SoftDevice.
*/
void ble_dfu_on_ble_evt(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt);
/**@brief Function for initializing the DFU service.
*
* @param[out] p_dfu Device Firmware Update service structure. This structure will have to be
* supplied by the application. It will be initialized by this function,
* and will later be used to identify the service instance.
* @param[in] p_dfu_init Information needed to initialize the service.
*
* @return NRF_SUCCESS if the DFU service and its characteristics were successfully added to the
* SoftDevice. Otherwise an error code.
* This function returns NRF_ERROR_NULL if the value of evt_handler in p_dfu_init
* structure provided is NULL or if the pointers supplied as input are NULL.
*/
uint32_t ble_dfu_init(ble_dfu_t * p_dfu, ble_dfu_init_t * p_dfu_init);
/**@brief Function for sending response to a control point command.
*
* @details This function will encode a DFU Control Point response using the given input
* parameters and will send a notification of the same to the peer.
*
* @param[in] p_dfu Pointer to the DFU service structure.
* @param[in] dfu_proc Procedure for which this response is to be sent.
* @param[in] resp_val Response value.
*
* @return NRF_SUCCESS if the DFU Service has successfully requested the SoftDevice to
* send the notification. Otherwise an error code.
* This function returns NRF_ERROR_INVALID_STATE if the device is not connected to a
* peer or if the DFU service is not initialized or if the notification of the DFU
* Status Report characteristic was not enabled by the peer. It returns NRF_ERROR_NULL
* if the pointer p_dfu is NULL.
*/
uint32_t ble_dfu_response_send(ble_dfu_t * p_dfu,
ble_dfu_procedure_t dfu_proc,
ble_dfu_resp_val_t resp_val);
/**@brief Function for notifying the peer about the number of bytes of firmware data received.
*
* @param[in] p_dfu Pointer to the DFU service structure.
* @param[in] num_of_firmware_bytes_rcvd Number of bytes.
*
* @return NRF_SUCCESS if the DFU Service has successfully requested the SoftDevice to send
* the notification. Otherwise an error code.
* This function returns NRF_ERROR_INVALID_STATE if the device is not connected to a
* peer or if the DFU service is not initialized or if the notification of the DFU
* Status Report characteristic was not enabled by the peer. It returns NRF_ERROR_NULL
* if the pointer p_dfu is NULL.
*/
uint32_t ble_dfu_bytes_rcvd_report(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd);
/**@brief Function for sending Packet Receipt Notification to the peer.
*
* This function will encode the number of bytes received as input parameter into a
* notification of the control point characteristic and send it to the peer.
*
* @param[in] p_dfu Pointer to the DFU service structure.
* @param[in] num_of_firmware_bytes_rcvd Number of bytes of firmware image received.
*
* @return NRF_SUCCESS if the DFU Service has successfully requested the SoftDevice to send
* the notification. Otherwise an error code.
* This function returns NRF_ERROR_INVALID_STATE if the device is not connected to a
* peer or if the DFU service is not initialized or if the notification of the DFU
* Status Report characteristic was not enabled by the peer. It returns NRF_ERROR_NULL
* if the pointer p_dfu is NULL.
*/
uint32_t ble_dfu_pkts_rcpt_notify(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd);
#endif // BLE_DFU_H__
/** @} */

View File

@ -0,0 +1,277 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/* Attention!
* To maintain compliance with Nordic Semiconductor ASAs Bluetooth profile
* qualification listings, this section of source code must not be modified.
*/
#include "ble_dis.h"
#include <stdlib.h>
#include <string.h>
#include "app_error.h"
#include "ble_gatts.h"
#include "nordic_common.h"
#include "ble_srv_common.h"
#include "app_util.h"
#define BLE_DIS_SYS_ID_LEN 8 /**< Length of System ID Characteristic Value. */
#define BLE_DIS_PNP_ID_LEN 7 /**< Length of Pnp ID Characteristic Value. */
static uint16_t service_handle;
static ble_gatts_char_handles_t manufact_name_handles;
static ble_gatts_char_handles_t model_num_handles;
static ble_gatts_char_handles_t serial_num_handles;
static ble_gatts_char_handles_t hw_rev_handles;
static ble_gatts_char_handles_t fw_rev_handles;
static ble_gatts_char_handles_t sw_rev_handles;
static ble_gatts_char_handles_t sys_id_handles;
static ble_gatts_char_handles_t reg_cert_data_list_handles;
static ble_gatts_char_handles_t pnp_id_handles;
/**@brief Function for encoding a System ID.
*
* @param[out] p_encoded_buffer Buffer where the encoded data will be written.
* @param[in] p_sys_id System ID to be encoded.
*/
static void sys_id_encode(uint8_t * p_encoded_buffer, const ble_dis_sys_id_t * p_sys_id)
{
APP_ERROR_CHECK_BOOL(p_sys_id != NULL);
APP_ERROR_CHECK_BOOL(p_encoded_buffer != NULL);
p_encoded_buffer[0] = (p_sys_id->manufacturer_id & 0x00000000FF);
p_encoded_buffer[1] = (p_sys_id->manufacturer_id & 0x000000FF00) >> 8;
p_encoded_buffer[2] = (p_sys_id->manufacturer_id & 0x0000FF0000) >> 16;
p_encoded_buffer[3] = (p_sys_id->manufacturer_id & 0x00FF000000) >> 24;
p_encoded_buffer[4] = (p_sys_id->manufacturer_id & 0xFF00000000) >> 32;
p_encoded_buffer[5] = (p_sys_id->organizationally_unique_id & 0x0000FF);
p_encoded_buffer[6] = (p_sys_id->organizationally_unique_id & 0x00FF00) >> 8;
p_encoded_buffer[7] = (p_sys_id->organizationally_unique_id & 0xFF0000) >> 16;
}
/**@brief Function for encoding a PnP ID.
*
* @param[out] p_encoded_buffer Buffer where the encoded data will be written.
* @param[in] p_pnp_id PnP ID to be encoded.
*/
static void pnp_id_encode(uint8_t * p_encoded_buffer, const ble_dis_pnp_id_t * p_pnp_id)
{
uint8_t len = 0;
APP_ERROR_CHECK_BOOL(p_pnp_id != NULL);
APP_ERROR_CHECK_BOOL(p_encoded_buffer != NULL);
p_encoded_buffer[len++] = p_pnp_id->vendor_id_source;
len += uint16_encode(p_pnp_id->vendor_id, &p_encoded_buffer[len]);
len += uint16_encode(p_pnp_id->product_id, &p_encoded_buffer[len]);
len += uint16_encode(p_pnp_id->product_version, &p_encoded_buffer[len]);
APP_ERROR_CHECK_BOOL(len == BLE_DIS_PNP_ID_LEN);
}
/**@brief Function for adding the Characteristic.
*
* @param[in] uuid UUID of characteristic to be added.
* @param[in] p_char_value Initial value of characteristic to be added.
* @param[in] char_len Length of initial value. This will also be the maximum value.
* @param[in] dis_attr_md Security settings of characteristic to be added.
* @param[out] p_handles Handles of new characteristic.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t char_add(uint16_t uuid,
uint8_t * p_char_value,
uint16_t char_len,
const ble_srv_security_mode_t * dis_attr_md,
ble_gatts_char_handles_t * p_handles)
{
ble_uuid_t ble_uuid;
ble_gatts_char_md_t char_md;
ble_gatts_attr_t attr_char_value;
ble_gatts_attr_md_t attr_md;
APP_ERROR_CHECK_BOOL(p_char_value != NULL);
APP_ERROR_CHECK_BOOL(char_len > 0);
// The ble_gatts_char_md_t structure uses bit fields. So we reset the memory to zero.
memset(&char_md, 0, sizeof(char_md));
char_md.char_props.read = 1;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_cccd_md = NULL;
char_md.p_sccd_md = NULL;
BLE_UUID_BLE_ASSIGN(ble_uuid, uuid);
memset(&attr_md, 0, sizeof(attr_md));
attr_md.read_perm = dis_attr_md->read_perm;
attr_md.write_perm = dis_attr_md->write_perm;
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 0;
attr_md.vlen = 0;
memset(&attr_char_value, 0, sizeof(attr_char_value));
attr_char_value.p_uuid = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = char_len;
attr_char_value.init_offs = 0;
attr_char_value.max_len = char_len;
attr_char_value.p_value = p_char_value;
return sd_ble_gatts_characteristic_add(service_handle, &char_md, &attr_char_value, p_handles);
}
uint32_t ble_dis_init(const ble_dis_init_t * p_dis_init)
{
uint32_t err_code;
ble_uuid_t ble_uuid;
// Add service
BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_DEVICE_INFORMATION_SERVICE);
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &service_handle);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
// Add characteristics
if (p_dis_init->manufact_name_str.length > 0)
{
err_code = char_add(BLE_UUID_MANUFACTURER_NAME_STRING_CHAR,
p_dis_init->manufact_name_str.p_str,
p_dis_init->manufact_name_str.length,
&p_dis_init->dis_attr_md,
&manufact_name_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->model_num_str.length > 0)
{
err_code = char_add(BLE_UUID_MODEL_NUMBER_STRING_CHAR,
p_dis_init->model_num_str.p_str,
p_dis_init->model_num_str.length,
&p_dis_init->dis_attr_md,
&model_num_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->serial_num_str.length > 0)
{
err_code = char_add(BLE_UUID_SERIAL_NUMBER_STRING_CHAR,
p_dis_init->serial_num_str.p_str,
p_dis_init->serial_num_str.length,
&p_dis_init->dis_attr_md,
&serial_num_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->hw_rev_str.length > 0)
{
err_code = char_add(BLE_UUID_HARDWARE_REVISION_STRING_CHAR,
p_dis_init->hw_rev_str.p_str,
p_dis_init->hw_rev_str.length,
&p_dis_init->dis_attr_md,
&hw_rev_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->fw_rev_str.length > 0)
{
err_code = char_add(BLE_UUID_FIRMWARE_REVISION_STRING_CHAR,
p_dis_init->fw_rev_str.p_str,
p_dis_init->fw_rev_str.length,
&p_dis_init->dis_attr_md,
&fw_rev_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->sw_rev_str.length > 0)
{
err_code = char_add(BLE_UUID_SOFTWARE_REVISION_STRING_CHAR,
p_dis_init->sw_rev_str.p_str,
p_dis_init->sw_rev_str.length,
&p_dis_init->dis_attr_md,
&sw_rev_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->p_sys_id != NULL)
{
uint8_t encoded_sys_id[BLE_DIS_SYS_ID_LEN];
sys_id_encode(encoded_sys_id, p_dis_init->p_sys_id);
err_code = char_add(BLE_UUID_SYSTEM_ID_CHAR,
encoded_sys_id,
BLE_DIS_SYS_ID_LEN,
&p_dis_init->dis_attr_md,
&sys_id_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->p_reg_cert_data_list != NULL)
{
err_code = char_add(BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR,
p_dis_init->p_reg_cert_data_list->p_list,
p_dis_init->p_reg_cert_data_list->list_len,
&p_dis_init->dis_attr_md,
&reg_cert_data_list_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
if (p_dis_init->p_pnp_id != NULL)
{
uint8_t encoded_pnp_id[BLE_DIS_PNP_ID_LEN];
pnp_id_encode(encoded_pnp_id, p_dis_init->p_pnp_id);
err_code = char_add(BLE_UUID_PNP_ID_CHAR,
encoded_pnp_id,
BLE_DIS_PNP_ID_LEN,
&p_dis_init->dis_attr_md,
&pnp_id_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
}
return NRF_SUCCESS;
}

View File

@ -0,0 +1,98 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/** @file
*
* @defgroup ble_sdk_srv_dis Device Information Service
* @{
* @ingroup ble_sdk_srv
* @brief Device Information Service module.
*
* @details This module implements the Device Information Service.
* During initialization it adds the Device Information Service to the BLE stack database.
* It then encodes the supplied information, and adds the curresponding characteristics.
*
* @note Attention!
* To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
* qualification listings, this section of source code must not be modified.
*/
#ifndef BLE_DIS_H__
#define BLE_DIS_H__
#include <stdint.h>
#include "ble_srv_common.h"
/** @defgroup DIS_VENDOR_ID_SRC_VALUES Vendor ID Source values
* @{
*/
#define BLE_DIS_VENDOR_ID_SRC_BLUETOOTH_SIG 1 /**< Vendor ID assigned by Bluetooth SIG. */
#define BLE_DIS_VENDOR_ID_SRC_USB_IMPL_FORUM 2 /**< Vendor ID assigned by USB Implementer's Forum. */
/** @} */
/**@brief System ID parameters */
typedef struct
{
uint64_t manufacturer_id; /**< Manufacturer ID. Only 5 LSOs shall be used. */
uint32_t organizationally_unique_id; /**< Organizationally unique ID. Only 3 LSOs shall be used. */
} ble_dis_sys_id_t;
/**@brief IEEE 11073-20601 Regulatory Certification Data List Structure */
typedef struct
{
uint8_t * p_list; /**< Pointer the byte array containing the encoded opaque structure based on IEEE 11073-20601 specification. */
uint8_t list_len; /**< Length of the byte array. */
} ble_dis_reg_cert_data_list_t;
/**@brief PnP ID parameters */
typedef struct
{
uint8_t vendor_id_source; /**< Vendor ID Source. see @ref DIS_VENDOR_ID_SRC_VALUES. */
uint16_t vendor_id; /**< Vendor ID. */
uint16_t product_id; /**< Product ID. */
uint16_t product_version; /**< Product Version. */
} ble_dis_pnp_id_t;
/**@brief Device Information Service init structure. This contains all possible characteristics
* needed for initialization of the service.
*/
typedef struct
{
ble_srv_utf8_str_t manufact_name_str; /**< Manufacturer Name String. */
ble_srv_utf8_str_t model_num_str; /**< Model Number String. */
ble_srv_utf8_str_t serial_num_str; /**< Serial Number String. */
ble_srv_utf8_str_t hw_rev_str; /**< Hardware Revision String. */
ble_srv_utf8_str_t fw_rev_str; /**< Firmware Revision String. */
ble_srv_utf8_str_t sw_rev_str; /**< Software Revision String. */
ble_dis_sys_id_t * p_sys_id; /**< System ID. */
ble_dis_reg_cert_data_list_t * p_reg_cert_data_list; /**< IEEE 11073-20601 Regulatory Certification Data List. */
ble_dis_pnp_id_t * p_pnp_id; /**< PnP ID. */
ble_srv_security_mode_t dis_attr_md; /**< Initial Security Setting for Device Information Characteristics. */
} ble_dis_init_t;
/**@brief Function for initializing the Device Information Service.
*
* @details This call allows the application to initialize the device information service.
* It adds the DIS service and DIS characteristics to the database, using the initial
* values supplied through the p_dis_init parameter. Characteristics which are not to be
* added, shall be set to NULL in p_dis_init.
*
* @param[in] p_dis_init The structure containing the values of characteristics needed by the
* service.
*
* @return NRF_SUCCESS on successful initialization of service.
*/
uint32_t ble_dis_init(const ble_dis_init_t * p_dis_init);
#endif // BLE_DIS_H__
/** @} */

View File

@ -0,0 +1,367 @@
/* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/** @file
*
* @defgroup ble_sdk_srv_common Common service definitions
* @{
* @ingroup ble_sdk_srv
* @brief Constants, type definitions, and functions that are common to all services.
*/
#ifndef BLE_SRV_COMMON_H__
#define BLE_SRV_COMMON_H__
#include <stdint.h>
#include <stdbool.h>
#include "ble_types.h"
#include "app_util.h"
#include "ble.h"
#include "ble_gap.h"
#include "ble_gatt.h"
/** @defgroup UUID_SERVICES Service UUID definitions
* @{ */
#define BLE_UUID_ALERT_NOTIFICATION_SERVICE 0x1811 /**< Alert Notification service UUID. */
#define BLE_UUID_BATTERY_SERVICE 0x180F /**< Battery service UUID. */
#define BLE_UUID_BLOOD_PRESSURE_SERVICE 0x1810 /**< Blood Pressure service UUID. */
#define BLE_UUID_CURRENT_TIME_SERVICE 0x1805 /**< Current Time service UUID. */
#define BLE_UUID_CYCLING_SPEED_AND_CADENCE 0x1816 /**< Cycling Speed and Cadence service UUID. */
#define BLE_UUID_LOCATION_AND_NAVIGATION_SERVICE 0x1819 /**< Location and Navigation service UUID. */
#define BLE_UUID_DEVICE_INFORMATION_SERVICE 0x180A /**< Device Information service UUID. */
#define BLE_UUID_GLUCOSE_SERVICE 0x1808 /**< Glucose service UUID. */
#define BLE_UUID_HEALTH_THERMOMETER_SERVICE 0x1809 /**< Health Thermometer service UUID. */
#define BLE_UUID_HEART_RATE_SERVICE 0x180D /**< Heart Rate service UUID. */
#define BLE_UUID_HUMAN_INTERFACE_DEVICE_SERVICE 0x1812 /**< Human Interface Device service UUID. */
#define BLE_UUID_IMMEDIATE_ALERT_SERVICE 0x1802 /**< Immediate Alert service UUID. */
#define BLE_UUID_LINK_LOSS_SERVICE 0x1803 /**< Link Loss service UUID. */
#define BLE_UUID_NEXT_DST_CHANGE_SERVICE 0x1807 /**< Next Dst Change service UUID. */
#define BLE_UUID_PHONE_ALERT_STATUS_SERVICE 0x180E /**< Phone Alert Status service UUID. */
#define BLE_UUID_REFERENCE_TIME_UPDATE_SERVICE 0x1806 /**< Reference Time Update service UUID. */
#define BLE_UUID_RUNNING_SPEED_AND_CADENCE 0x1814 /**< Running Speed and Cadence service UUID. */
#define BLE_UUID_SCAN_PARAMETERS_SERVICE 0x1813 /**< Scan Parameters service UUID. */
#define BLE_UUID_TX_POWER_SERVICE 0x1804 /**< TX Power service UUID. */
#define BLE_UUID_IPSP_SERVICE 0x1820 /**< Internet Protocol Support service UUID. */
#define BLE_UUID_BMS_SERVICE 0x181E /**< BOND MANAGEMENT service UUID*/
#define BLE_UUID_CGM_SERVICE 0x181F /**< Contiunous Glucose Monitoring service UUID*/
#define BLE_UUID_PLX_SERVICE 0x1822 /**< Pulse Oximeter Service UUID*/
/** @} */
/** @defgroup UUID_CHARACTERISTICS Characteristic UUID definitions
* @{ */
#define BLE_UUID_REMOVABLE_CHAR 0x2A3A /**< Removable characteristic UUID. */
#define BLE_UUID_SERVICE_REQUIRED_CHAR 0x2A3B /**< Service Required characteristic UUID. */
#define BLE_UUID_ALERT_CATEGORY_ID_CHAR 0x2A43 /**< Alert Category Id characteristic UUID. */
#define BLE_UUID_ALERT_CATEGORY_ID_BIT_MASK_CHAR 0x2A42 /**< Alert Category Id Bit Mask characteristic UUID. */
#define BLE_UUID_ALERT_LEVEL_CHAR 0x2A06 /**< Alert Level characteristic UUID. */
#define BLE_UUID_ALERT_NOTIFICATION_CONTROL_POINT_CHAR 0x2A44 /**< Alert Notification Control Point characteristic UUID. */
#define BLE_UUID_ALERT_STATUS_CHAR 0x2A3F /**< Alert Status characteristic UUID. */
#define BLE_UUID_BATTERY_LEVEL_CHAR 0x2A19 /**< Battery Level characteristic UUID. */
#define BLE_UUID_BLOOD_PRESSURE_FEATURE_CHAR 0x2A49 /**< Blood Pressure Feature characteristic UUID. */
#define BLE_UUID_BLOOD_PRESSURE_MEASUREMENT_CHAR 0x2A35 /**< Blood Pressure Measurement characteristic UUID. */
#define BLE_UUID_BODY_SENSOR_LOCATION_CHAR 0x2A38 /**< Body Sensor Location characteristic UUID. */
#define BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR 0x2A22 /**< Boot Keyboard Input Report characteristic UUID. */
#define BLE_UUID_BOOT_KEYBOARD_OUTPUT_REPORT_CHAR 0x2A32 /**< Boot Keyboard Output Report characteristic UUID. */
#define BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR 0x2A33 /**< Boot Mouse Input Report characteristic UUID. */
#define BLE_UUID_CURRENT_TIME_CHAR 0x2A2B /**< Current Time characteristic UUID. */
#define BLE_UUID_DATE_TIME_CHAR 0x2A08 /**< Date Time characteristic UUID. */
#define BLE_UUID_DAY_DATE_TIME_CHAR 0x2A0A /**< Day Date Time characteristic UUID. */
#define BLE_UUID_DAY_OF_WEEK_CHAR 0x2A09 /**< Day Of Week characteristic UUID. */
#define BLE_UUID_DST_OFFSET_CHAR 0x2A0D /**< Dst Offset characteristic UUID. */
#define BLE_UUID_EXACT_TIME_256_CHAR 0x2A0C /**< Exact Time 256 characteristic UUID. */
#define BLE_UUID_FIRMWARE_REVISION_STRING_CHAR 0x2A26 /**< Firmware Revision String characteristic UUID. */
#define BLE_UUID_GLUCOSE_FEATURE_CHAR 0x2A51 /**< Glucose Feature characteristic UUID. */
#define BLE_UUID_GLUCOSE_MEASUREMENT_CHAR 0x2A18 /**< Glucose Measurement characteristic UUID. */
#define BLE_UUID_GLUCOSE_MEASUREMENT_CONTEXT_CHAR 0x2A34 /**< Glucose Measurement Context characteristic UUID. */
#define BLE_UUID_HARDWARE_REVISION_STRING_CHAR 0x2A27 /**< Hardware Revision String characteristic UUID. */
#define BLE_UUID_HEART_RATE_CONTROL_POINT_CHAR 0x2A39 /**< Heart Rate Control Point characteristic UUID. */
#define BLE_UUID_HEART_RATE_MEASUREMENT_CHAR 0x2A37 /**< Heart Rate Measurement characteristic UUID. */
#define BLE_UUID_HID_CONTROL_POINT_CHAR 0x2A4C /**< Hid Control Point characteristic UUID. */
#define BLE_UUID_HID_INFORMATION_CHAR 0x2A4A /**< Hid Information characteristic UUID. */
#define BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR 0x2A2A /**< IEEE Regulatory Certification Data List characteristic UUID. */
#define BLE_UUID_INTERMEDIATE_CUFF_PRESSURE_CHAR 0x2A36 /**< Intermediate Cuff Pressure characteristic UUID. */
#define BLE_UUID_INTERMEDIATE_TEMPERATURE_CHAR 0x2A1E /**< Intermediate Temperature characteristic UUID. */
#define BLE_UUID_LOCAL_TIME_INFORMATION_CHAR 0x2A0F /**< Local Time Information characteristic UUID. */
#define BLE_UUID_MANUFACTURER_NAME_STRING_CHAR 0x2A29 /**< Manufacturer Name String characteristic UUID. */
#define BLE_UUID_MEASUREMENT_INTERVAL_CHAR 0x2A21 /**< Measurement Interval characteristic UUID. */
#define BLE_UUID_MODEL_NUMBER_STRING_CHAR 0x2A24 /**< Model Number String characteristic UUID. */
#define BLE_UUID_UNREAD_ALERT_CHAR 0x2A45 /**< Unread Alert characteristic UUID. */
#define BLE_UUID_NEW_ALERT_CHAR 0x2A46 /**< New Alert characteristic UUID. */
#define BLE_UUID_PNP_ID_CHAR 0x2A50 /**< PNP Id characteristic UUID. */
#define BLE_UUID_PROTOCOL_MODE_CHAR 0x2A4E /**< Protocol Mode characteristic UUID. */
#define BLE_UUID_RECORD_ACCESS_CONTROL_POINT_CHAR 0x2A52 /**< Record Access Control Point characteristic UUID. */
#define BLE_UUID_REFERENCE_TIME_INFORMATION_CHAR 0x2A14 /**< Reference Time Information characteristic UUID. */
#define BLE_UUID_REPORT_CHAR 0x2A4D /**< Report characteristic UUID. */
#define BLE_UUID_REPORT_MAP_CHAR 0x2A4B /**< Report Map characteristic UUID. */
#define BLE_UUID_RINGER_CONTROL_POINT_CHAR 0x2A40 /**< Ringer Control Point characteristic UUID. */
#define BLE_UUID_RINGER_SETTING_CHAR 0x2A41 /**< Ringer Setting characteristic UUID. */
#define BLE_UUID_SCAN_INTERVAL_WINDOW_CHAR 0x2A4F /**< Scan Interval Window characteristic UUID. */
#define BLE_UUID_SCAN_REFRESH_CHAR 0x2A31 /**< Scan Refresh characteristic UUID. */
#define BLE_UUID_SERIAL_NUMBER_STRING_CHAR 0x2A25 /**< Serial Number String characteristic UUID. */
#define BLE_UUID_SOFTWARE_REVISION_STRING_CHAR 0x2A28 /**< Software Revision String characteristic UUID. */
#define BLE_UUID_SUPPORTED_NEW_ALERT_CATEGORY_CHAR 0x2A47 /**< Supported New Alert Category characteristic UUID. */
#define BLE_UUID_SUPPORTED_UNREAD_ALERT_CATEGORY_CHAR 0x2A48 /**< Supported Unread Alert Category characteristic UUID. */
#define BLE_UUID_SYSTEM_ID_CHAR 0x2A23 /**< System Id characteristic UUID. */
#define BLE_UUID_TEMPERATURE_MEASUREMENT_CHAR 0x2A1C /**< Temperature Measurement characteristic UUID. */
#define BLE_UUID_TEMPERATURE_TYPE_CHAR 0x2A1D /**< Temperature Type characteristic UUID. */
#define BLE_UUID_TIME_ACCURACY_CHAR 0x2A12 /**< Time Accuracy characteristic UUID. */
#define BLE_UUID_TIME_SOURCE_CHAR 0x2A13 /**< Time Source characteristic UUID. */
#define BLE_UUID_TIME_UPDATE_CONTROL_POINT_CHAR 0x2A16 /**< Time Update Control Point characteristic UUID. */
#define BLE_UUID_TIME_UPDATE_STATE_CHAR 0x2A17 /**< Time Update State characteristic UUID. */
#define BLE_UUID_TIME_WITH_DST_CHAR 0x2A11 /**< Time With Dst characteristic UUID. */
#define BLE_UUID_TIME_ZONE_CHAR 0x2A0E /**< Time Zone characteristic UUID. */
#define BLE_UUID_TX_POWER_LEVEL_CHAR 0x2A07 /**< TX Power Level characteristic UUID. */
#define BLE_UUID_CSC_FEATURE_CHAR 0x2A5C /**< Cycling Speed and Cadence Feature characteristic UUID. */
#define BLE_UUID_CSC_MEASUREMENT_CHAR 0x2A5B /**< Cycling Speed and Cadence Measurement characteristic UUID. */
#define BLE_UUID_RSC_FEATURE_CHAR 0x2A54 /**< Running Speed and Cadence Feature characteristic UUID. */
#define BLE_UUID_SC_CTRLPT_CHAR 0x2A55 /**< Speed and Cadence Control Point UUID. */
#define BLE_UUID_RSC_MEASUREMENT_CHAR 0x2A53 /**< Running Speed and Cadence Measurement characteristic UUID. */
#define BLE_UUID_SENSOR_LOCATION_CHAR 0x2A5D /**< Sensor Location characteristic UUID. */
#define BLE_UUID_EXTERNAL_REPORT_REF_DESCR 0x2907 /**< External Report Reference descriptor UUID. */
#define BLE_UUID_REPORT_REF_DESCR 0x2908 /**< Report Reference descriptor UUID. */
#define BLE_UUID_LN_FEATURE_CHAR 0x2A6A /**< Location Navigation Service, Feature characteristic UUID. */
#define BLE_UUID_LN_POSITION_QUALITY_CHAR 0x2A69 /**< Location Navigation Service, Position quality UUID. */
#define BLE_UUID_LN_LOCATION_AND_SPEED_CHAR 0x2A67 /**< Location Navigation Service, Location and Speed characteristic UUID. */
#define BLE_UUID_LN_NAVIGATION_CHAR 0x2A68 /**< Location Navigation Service, Navigation characteristic UUID. */
#define BLE_UUID_LN_CONTROL_POINT_CHAR 0x2A6B /**< Location Navigation Service, Control point characteristic UUID. */
#define BLE_UUID_BMS_CTRLPT 0x2AA4 /**< BMS Control Point characteristic UUID. */
#define BLE_UUID_BMS_FEATURE 0x2AA5 /**< BMS Feature characteristic UUID. */
#define BLE_UUID_CGM_MEASUREMENT 0x2AA7 /**< CGM Service, Measurement characteristic UUID*/
#define BLE_UUID_CGM_FEATURE 0x2AA8 /**< CGM Service, Feature characteristic UUID*/
#define BLE_UUID_CGM_STATUS 0x2AA9 /**< CGM Service, Status characteristic UUID*/
#define BLE_UUID_CGM_SESSION_START_TIME 0x2AAA /**< CGM Service, session start time characteristic UUID*/
#define BLE_UUID_CGM_SESSION_RUN_TIME 0x2AAB /**< CGM Service, session run time characteristic UUID*/
#define BLE_UUID_CGM_SPECIFIC_OPS_CTRLPT 0x2AAC /**< CGM Service, specific ops ctrlpt characteristic UUID*/
#define BLE_UUID_PLX_SPOT_CHECK_MEAS 0x2A5E /**< PLX Service, spot check measurement characteristic UUID*/
#define BLE_UUID_PLX_CONTINUOUS_MEAS 0x2A5F /**< PLX Service, continuous measurement characteristic UUID*/
#define BLE_UUID_PLX_FEATURES 0x2A60 /**< PLX Service, feature characteristic UUID*/
/** @} */
/** @defgroup ALERT_LEVEL_VALUES Definitions for the Alert Level characteristic values
* @{ */
#define BLE_CHAR_ALERT_LEVEL_NO_ALERT 0x00 /**< No Alert. */
#define BLE_CHAR_ALERT_LEVEL_MILD_ALERT 0x01 /**< Mild Alert. */
#define BLE_CHAR_ALERT_LEVEL_HIGH_ALERT 0x02 /**< High Alert. */
/** @} */
#define BLE_SRV_ENCODED_REPORT_REF_LEN 2 /**< The length of an encoded Report Reference Descriptor. */
#define BLE_CCCD_VALUE_LEN 2 /**< The length of a CCCD value. */
/**@brief Type definition for error handler function that will be called in case of an error in
* a service or a service library module. */
typedef void (*ble_srv_error_handler_t) (uint32_t nrf_error);
/**@brief Value of a Report Reference descriptor.
*
* @details This is mapping information that maps the parent characteristic to the Report ID(s) and
* Report Type(s) defined within a Report Map characteristic.
*/
typedef struct
{
uint8_t report_id; /**< Non-zero value if there is more than one instance of the same Report Type */
uint8_t report_type; /**< Type of Report characteristic (see @ref BLE_HIDS_REPORT_TYPE) */
} ble_srv_report_ref_t;
/**@brief UTF-8 string data type.
*
* @note The type can only hold a pointer to the string data (i.e. not the actual data).
*/
typedef struct
{
uint16_t length; /**< String length. */
uint8_t * p_str; /**< String data. */
} ble_srv_utf8_str_t;
/**@brief Security settings structure.
* @details This structure contains the security options needed during initialization of the
* service.
*/
typedef struct
{
ble_gap_conn_sec_mode_t read_perm; /**< Read permissions. */
ble_gap_conn_sec_mode_t write_perm; /**< Write permissions. */
} ble_srv_security_mode_t;
/**@brief Security settings structure.
* @details This structure contains the security options needed during initialization of the
* service. It can be used when the characteristics contains a CCCD.
*/
typedef struct
{
ble_gap_conn_sec_mode_t cccd_write_perm; /**< Write permissions for Client Characteristic Configuration Descriptor. */
ble_gap_conn_sec_mode_t read_perm; /**< Read permissions. */
ble_gap_conn_sec_mode_t write_perm; /**< Write permissions. */
} ble_srv_cccd_security_mode_t;
/**@brief Function for decoding a CCCD value, and then testing if notification is
* enabled.
*
* @param[in] p_encoded_data Buffer where the encoded CCCD is stored.
*
* @retval TRUE If notification is enabled.
* @retval FALSE Otherwise.
*/
static __INLINE bool ble_srv_is_notification_enabled(uint8_t const * p_encoded_data)
{
uint16_t cccd_value = uint16_decode(p_encoded_data);
return ((cccd_value & BLE_GATT_HVX_NOTIFICATION) != 0);
}
/**@brief Function for decoding a CCCD value, and then testing if indication is
* enabled.
*
* @param[in] p_encoded_data Buffer where the encoded CCCD is stored.
*
* @retval TRUE If indication is enabled.
* @retval FALSE Otherwise.
*/
static __INLINE bool ble_srv_is_indication_enabled(uint8_t const * p_encoded_data)
{
uint16_t cccd_value = uint16_decode(p_encoded_data);
return ((cccd_value & BLE_GATT_HVX_INDICATION) != 0);
}
/**@brief Function for encoding a Report Reference Descriptor.
*
* @param[in] p_encoded_buffer The buffer of the encoded data.
* @param[in] p_report_ref Report Reference value to be encoded.
*
* @return Length of the encoded data.
*/
uint8_t ble_srv_report_ref_encode(uint8_t * p_encoded_buffer,
const ble_srv_report_ref_t * p_report_ref);
/**@brief Function for making a UTF-8 structure refer to an ASCII string.
*
* @param[out] p_utf8 UTF-8 structure to be set.
* @param[in] p_ascii ASCII string to be referred to.
*/
void ble_srv_ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, char * p_ascii);
/**@brief Security Access enumeration.
* @details This enumeration gives the possible requirements for accessing a characteristic value.
*/
typedef enum
{
SEC_NO_ACCESS = 0, /**< Not possible to access. */
SEC_OPEN = 1, /**< Access open. */
SEC_JUST_WORKS = 2, /**< Access possible with 'Just Works' security at least. */
SEC_MITM = 3, /**< Access possible with 'MITM' security at least. */
SEC_SIGNED = 4, /**< Access possible with 'signed' security at least. */
SEC_SIGNED_MITM = 5 /**< Access possible with 'signed and MITM' security at least. */
}security_req_t;
/**@brief Characteristic User Descriptor parameters.
* @details This structure contains the parameters for User Descriptor.
*/
typedef struct
{
uint16_t max_size; /**< Maximum size of the user descriptor*/
uint16_t size; /**< Size of the user descriptor*/
uint8_t *p_char_user_desc; /**< User descriptor content, pointer to a UTF-8 encoded string (non-NULL terminated)*/
bool is_var_len; /**< Indicates if the user descriptor has variable length.*/
ble_gatt_char_props_t char_props; /**< user descriptor properties.*/
bool is_defered_read; /**< Indicate if deferred read operations are supported.*/
bool is_defered_write; /**< Indicate if deferred write operations are supported.*/
security_req_t read_access; /**< Security requirement for reading the user descriptor.*/
security_req_t write_access; /**< Security requirement for writing the user descriptor.*/
bool is_value_user; /**< Indicate if the content of the characteristic is to be stored in the application (user) or in the stack.*/
}ble_add_char_user_desc_t;
/**@brief Add characteristic parameters structure.
* @details This structure contains the parameters needed to use the @ref characteristic_add function.
*/
typedef struct
{
uint16_t uuid; /**< Characteristic UUID (16 bits UUIDs).*/
uint8_t uuid_type; /**< Base UUID. If 0, the Bluetooth SIG UUID will be used. Otherwise, this should be a value returned by @ref sd_ble_uuid_vs_add when adding the base UUID.*/
uint16_t max_len; /**< Maximum length of the characteristic value.*/
uint16_t init_len; /**< Initial length of the characteristic value.*/
uint8_t * p_init_value; /**< Initial encoded value of the characteristic.*/
bool is_var_len; /**< Indicates if the characteristic value has variable length.*/
ble_gatt_char_props_t char_props; /**< Characteristic properties.*/
bool is_defered_read; /**< Indicate if deferred read operations are supported.*/
bool is_defered_write; /**< Indicate if deferred write operations are supported.*/
security_req_t read_access; /**< Security requirement for reading the characteristic value.*/
security_req_t write_access; /**< Security requirement for writing the characteristic value.*/
security_req_t cccd_write_access; /**< Security requirement for writing the characteristic's CCCD.*/
bool is_value_user; /**< Indicate if the content of the characteristic is to be stored in the application (user) or in the stack.*/
ble_add_char_user_desc_t *p_user_descr; /**< Pointer to user descriptor if needed*/
ble_gatts_char_pf_t *p_presentation_format; /**< Pointer to characteristic format if needed*/
} ble_add_char_params_t;
/**@brief Add descriptor parameters structure.
* @details This structure contains the parameters needed to use the @ref descriptor_add function.
*/
typedef struct
{
uint16_t uuid; /**< descriptor UUID (16 bits UUIDs).*/
uint8_t uuid_type; /**< Base UUID. If 0, the Bluetooth SIG UUID will be used. Otherwise, this should be a value returned by @ref sd_ble_uuid_vs_add when adding the base UUID.*/
bool is_defered_read; /**< Indicate if deferred read operations are supported.*/
bool is_defered_write; /**< Indicate if deferred write operations are supported.*/
bool is_var_len; /**< Indicates if the descriptor value has variable length.*/
security_req_t read_access; /**< Security requirement for reading the descriptor value.*/
security_req_t write_access; /**< Security requirement for writing the descriptor value.*/
bool is_value_user; /**< Indicate if the content of the characteristic is to be stored in the application (user) or in the stack.*/
uint16_t init_len; /**< Initial descriptor value length in bytes. */
uint16_t init_offs; /**< Initial descriptor value offset in bytes. If different from zero, the first init_offs bytes of the attribute value will be left uninitialized. */
uint16_t max_len; /**< Maximum descriptor value length in bytes, see @ref BLE_GATTS_ATTR_LENS_MAX for maximum values. */
uint8_t* p_value; /**< Pointer to the value of the descriptor*/
} ble_add_descr_params_t;
/**@brief Function for adding a characteristic to a given service.
*
* If no pointer is given for the initial value,
* the initial length parameter will be ignored and the initial length will be 0.
*
* @param[in] service_handle Handle of the service to which the characteristic is to be added.
* @param[in] p_char_props Information needed to add the characteristic.
* @param[out] p_char_handle Handle of the added characteristic.
*
* @retval NRF_SUCCESS If the characteristic was added successfully. Otherwise, an error code is returned.
*/
uint32_t characteristic_add(uint16_t service_handle,
ble_add_char_params_t * p_char_props,
ble_gatts_char_handles_t * p_char_handle);
/**@brief Function for adding a characteristic's descriptor to a given characteristic.
*
* @param[in] char_handle Handle of the characteristic to which the descriptor is to be added, if @ref BLE_GATT_HANDLE_INVALID is used, it will be placed sequentially.
* @param[in] p_descr_props Information needed to add the descriptor.
* @param[out] p_descr_handle Handle of the added descriptor.
*
* @retval NRF_SUCCESS If the characteristic was added successfully. Otherwise, an error code is returned.
*/
uint32_t descriptor_add(uint16_t char_handle,
ble_add_descr_params_t * p_descr_props,
uint16_t * p_descr_handle);
#endif // BLE_SRV_COMMON_H__
/** @} */

View File

@ -0,0 +1,381 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup persistent_storage Persistent Storage Interface
* @{
* @ingroup app_common
* @brief Abstracted flash interface.
*
* @details An abstracted interface is provided by the module to easily port the application and
* SDK modules to an alternate option. This ensures that the SDK and application are moved
* to alternate persistent storage instead of the one provided by default.
*/
#ifndef PSTORAGE_H__
#define PSTORAGE_H__
#include "pstorage_platform.h"
/**@defgroup ps_opcode Persistent Storage Access Operation Codes
* @{
* @brief Persistent Storage Access Operation Codes.
*
* @details Persistent Storage Access Operation Codes are used by Persistent storage operation
* completion callback @ref pstorage_ntf_cb_t to identify the operation type requested by
* the application.
*/
#define PSTORAGE_STORE_OP_CODE 0x01 /**< Store Operation type. */
#define PSTORAGE_LOAD_OP_CODE 0x02 /**< Load Operation type. */
#define PSTORAGE_CLEAR_OP_CODE 0x03 /**< Clear Operation type. */
#define PSTORAGE_UPDATE_OP_CODE 0x04 /**< Update Operation type. */
/**@} */
/**@defgroup pstorage_data_types Persistent Memory Interface Data Types
* @{
* @brief Data Types needed for interfacing with persistent memory.
*
* @details Data Types needed for interfacing with persistent memory.
*/
/**@brief Persistent storage operation completion callback function type.
*
* @details The persistent storage operation completion callback is used by the interface to report
* success or failure of a flash operation. Since data is not copied for a store operation,
* a callback is an indication that the resident memory can now be reused or freed.
*
* @param[in] handle Identifies the module and block for the callback that is received.
* @param[in] op_code Identifies the operation for the event that is notified.
* @param[in] result Identifies the result of a flash access operation. NRF_SUCCESS implies
* operation succeeded.
*
* @note Unmanaged (abnormal behaviour) error codes from the SoftDevice flash
* access API are forwarded as is and are expected to be handled by the
* application. For details refer to the implementation file and corresponding
* SoftDevice flash API documentation.
*
* @param[in] p_data Identifies the application data pointer. For a store operation, this points
* to the resident source of application memory that the application can now
* free or reuse. When there is a clear operation, this is NULL since no
* application pointer is needed for this operation.
* @param[in] data_len Length data the application provided for the operation.
*/
typedef void (*pstorage_ntf_cb_t)(pstorage_handle_t * p_handle,
uint8_t op_code,
uint32_t result,
uint8_t * p_data,
uint32_t data_len);
/**@brief Struct containing module registration context. */
typedef struct
{
pstorage_ntf_cb_t cb; /**< Persistent storage operation completion callback function @ref pstorage_ntf_cb_t. */
pstorage_size_t block_size; /**< Desired block size for persistent memory storage. For example, if a module has a table with 10 entries, and each entry is 64 bytes in size,
* it can request 10 blocks with a block size of 64 bytes. The module can also request one block that is 640 bytes depending
* on how it would like to access or alter the memory in persistent memory.
* The first option is preferred when it is a single entry that needs to be updated often and doesn't impact the other entries.
* The second option is preferred when table entries are not changed individually but have a common point of loading and storing
* data. */
pstorage_size_t block_count; /** Number of blocks requested by the module; minimum values is 1. */
} pstorage_module_param_t;
/**@} */
/**@defgroup pstorage_routines Persistent Storage Access Routines
* @{
* @brief Functions/Interface SDK modules used to persistently store data.
*
* @details Interface for the Application and SDK modules to load/store information persistently.
* Note: While implementation of each of the persistent storage access functions
* depends on the system and is specific to system/solution, the signature of the
* interface routines should not be altered.
*/
/**@brief Function for initializing the module.
*
* @details Function for initializing the module. This function is called once before any other APIs
* of the module are used.
*
* @retval NRF_SUCCESS Operation success.
*/
uint32_t pstorage_init(void);
/**@brief Function for registering with persistent storage interface.
*
* @param[in] p_module_param Module registration parameter.
* @param[out] p_block_id Block identifier to identify persistent memory blocks when
* registration succeeds. Application is expected to use the block IDs
* for subsequent operations on requested persistent memory. Maximum
* registrations permitted is determined by the configuration of the
* parameter PSTORAGE_NUM_OF_PAGES. If more than one memory block is
* requested, the identifier provided here is the base identifier for the
* first block and used to identify the subsequent block. The application
* uses \@ref pstorage_block_identifier_get with this base identifier and
* block number. Therefore if 10 blocks of size 64 are requested and the
* application wishes to store memory in the 6th block, it shall use
* \@ref pstorage_block_identifier_get with the base ID and provide a
* block number of 5. This way the application is only expected to
* remember the base block identifier.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_NO_MEM Operation failure. Additional registrations can't be
* supported.
*/
uint32_t pstorage_register(pstorage_module_param_t * p_module_param,
pstorage_handle_t * p_block_id);
/**@brief Function for getting block ID with reference to base block identifier provided at the time
* of registration.
*
* @details Function to get the block ID with reference to base block identifier provided at the
* time of registration.
* If more than one memory block was requested when registering, the identifier provided
* here is the base identifier for the first block which is used to identify subsequent
* blocks. The application shall use this routine to get the block identifier, providing
* input as base identifier and block number. Therefore, if 10 blocks of size 64 are
* requested and the application wishes to store memory in the 6th block, it shall use
* \@ref pstorage_block_identifier_get with the base ID and provide a block number of 5.
* This way the application is only expected to remember the base block identifier.
*
* @param[in] p_base_id Base block ID received at the time of registration.
* @param[in] block_num Block Number, with first block numbered zero.
* @param[out] p_block_id Block identifier for the block number requested when the API succeeds.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
*/
uint32_t pstorage_block_identifier_get(pstorage_handle_t * p_base_id,
pstorage_size_t block_num,
pstorage_handle_t * p_block_id);
/**@brief Function for persistently storing data of length 'size' contained in the 'p_src' address
* in the storage module at 'p_dest' address. Equivalent to Storage Write.
*
* @param[in] p_dest Destination address where data is to be stored persistently.
* @param[in] p_src Source address containing data to be stored. API assumes this to be resident
* memory and no intermediate copy of data is made by the API. Must be word
* aligned.
* @param[in] size Size of data to be stored expressed in bytes. Must be word aligned and size +
* offset must be <= block size.
* @param[in] offset Offset in bytes to be applied when writing to the block.
* For example, if within a block of 100 bytes, the application wishes to
* write 20 bytes at an offset of 12, then this field should be set to 12.
* Must be word aligned.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*
* @warning No copy of the data is made, meaning memory provided for the data source that is to
* be written to flash cannot be freed or reused by the application until this procedure
* is complete. The application is notified when the procedure is finished using the
* notification callback registered by the application.
*/
uint32_t pstorage_store(pstorage_handle_t * p_dest,
uint8_t * p_src,
pstorage_size_t size,
pstorage_size_t offset);
/**@brief Function for updating persistently stored data of length 'size' contained in the 'p_src'
* address in the storage module at 'p_dest' address.
*
* @param[in] p_dest Destination address where data is to be updated.
* @param[in] p_src Source address containing data to be stored. API assumes this to be resident
* memory and no intermediate copy of data is made by the API.
* @param[in] size Size of data to be stored expressed in bytes. Must be word aligned and size +
* offset must be <= block size.
* @param[in] offset Offset in bytes to be applied when writing to the block.
* For example, if within a block of 100 bytes, the application wishes to
* write 20 bytes at an offset of 12 bytes, then this field should be set to 12.
* Must be word aligned.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*
* @warning No copy of the data is made, meaning memory provided for the data source that is to
* be written to flash cannot be freed or reused by the application until this procedure
* is complete. The application is notified when the procedure is finished using the
* notification callback registered by the application.
*/
uint32_t pstorage_update(pstorage_handle_t * p_dest,
uint8_t * p_src,
pstorage_size_t size,
pstorage_size_t offset);
/**@brief Function for loading persistently stored data of length 'size' from 'p_src' address
* to 'p_dest' address. Equivalent to Storage Read.
*
* @param[in] p_dest Destination address where persistently stored data is to be loaded.
* @param[in] p_src Source where data is loaded from persistent memory.
* @param[in] size Size of data to be loaded from persistent memory expressed in bytes.
* Should be word aligned.
* @param[in] offset Offset in bytes, to be applied when loading from the block.
* For example, if within a block of 100 bytes, the application wishes to
* load 20 bytes from offset of 12 bytes, then this field should be set to 12.
* Should be word aligned.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*/
uint32_t pstorage_load(uint8_t * p_dest,
pstorage_handle_t * p_src,
pstorage_size_t size,
pstorage_size_t offset);
/**@brief Function for clearing data in persistent memory.
*
* @param[in] p_base_id Base block identifier in persistent memory that needs to be cleared;
* equivalent to an Erase Operation.
* @param[in] size Size of data to be cleared from persistent memory expressed in bytes.
* This parameter is to provision for clearing of certain blocks
* of memory, or all memory blocks in a registered module. If the total size
* of the application module is used (blocks * block size) in combination with
* the identifier for the first block in the module, all blocks in the
* module will be erased. Must be multiple of block size.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*
* @note Clear operations may take time. This API however, does not block until the clear
* procedure is complete. The application is notified of procedure completion using
* a notification callback registered by the application. The 'result' parameter of the
* callback indicates if the procedure was successful or not.
*/
uint32_t pstorage_clear(pstorage_handle_t * p_base_id, pstorage_size_t size);
/**@brief Function for getting the number of pending operations with the module.
*
* @param[out] p_count Number of storage operations pending with the module. If 0, there are no
* outstanding requests.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
*/
uint32_t pstorage_access_status_get(uint32_t * p_count);
#ifdef PSTORAGE_RAW_MODE_ENABLE
/**@brief Function for registering with the persistent storage interface.
*
* @param[in] p_module_param Module registration parameter.
* @param[out] p_block_id Block identifier used to identify persistent memory blocks upon
* successful registration. The application is expected to use the block
* IDs for subsequent operations on requested persistent memory. When
* more than one memory block is requested, this identifier is the base
* identifier for the first block and used to identify subsequent blocks.
* The application shall use \@ref pstorage_block_identifier_get with
* this base identifier and block number. Therefore if 10 blocks of size
* 64 are requested and the application wishes to store memory in the 6th
* block, it shall use \@ref pstorage_block_identifier_get with the base
* ID and provide a block number of 5. Therefore, the application is only
* expected to remember the base block identifier.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*/
uint32_t pstorage_raw_register(pstorage_module_param_t * p_module_param,
pstorage_handle_t * p_block_id);
/**@brief Function for persistently storing data of length 'size' contained in 'p_src' address in
* storage module at 'p_dest' address. Equivalent to Storage Write.
*
* @param[in] p_dest Destination address where data is to be stored persistently.
* @param[in] p_src Source address containing data to be stored. The API assumes this is resident
* memory and no intermediate copy of data is made by the API. Must be word
* aligned.
* @param[in] size Size of data to be stored expressed in bytes. Must be word aligned.
* @param[in] offset Offset in bytes to be applied when writing to the block.
* For example, if within a block of 100 bytes, the application wishes to
* write 20 bytes at an offset of 12 bytes, this field should be set to 12.
* Must be word aligned.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*
* @warning No copy of the data is made, meaning memory provided for data source that is to be
* written to flash cannot be freed or reused by the application until this procedure
* is complete. The application is notified when the procedure is finished using the
* notification callback registered by the application.
*/
uint32_t pstorage_raw_store(pstorage_handle_t * p_dest,
uint8_t * p_src,
pstorage_size_t size,
pstorage_size_t offset);
/**@brief Function for clearing data in persistent memory in raw mode.
*
* @param[in] p_dest Base block identifier in persistent memory that needs to be cleared.
* Equivalent to an Erase Operation.
* @param[in] size Size of data to be cleared from persistent memory expressed in bytes.
* Not used.
*
* @retval NRF_SUCCESS Operation success.
* @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
* initialization.
* @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
* @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
* @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
*
* @note Clear operations may take time. This API, however, does not block until the clear
* procedure is complete. The application is notified of procedure completion using
* a notification callback registered by the application. The 'result' parameter of the
* callback indicates if the procedure was successful or not.
*/
uint32_t pstorage_raw_clear(pstorage_handle_t * p_dest, pstorage_size_t size);
#endif // PSTORAGE_RAW_MODE_ENABLE
/**@} */
/**@} */
#endif // PSTORAGE_H__

View File

@ -0,0 +1,471 @@
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "pstorage.h"
#include <stdlib.h>
#include <stdint.h>
#include "nordic_common.h"
#include "nrf_error.h"
#include "nrf_assert.h"
#include "nrf.h"
#include "nrf_soc.h"
#include "app_util.h"
/** @file
*
* @defgroup persistent_storage_raw Persistent Storage Interface - Raw Mode Implementation
* @{
* @ingroup persistent_storage
* @brief Persistent Storage Interface - Raw Mode Implementation.
*
* @details This file contains the source code for raw mode implementation of pstorage.
* It is intended for special use cases where flash size is critical or the application must have
* full control of the flash, such as DFU. The registration function in this implementation only
* allocates a module id for the queue but does not locate any flash pages for the registrant.
* This implementation provides no safety checking of addresses when clearing or storing data into
* flash. The application is responsible for handling flash addresses and care must therefore be
* taken in application not to erase application area.
* This implementation does not support the @ref pstorage_update function.
*/
#define INVALID_OPCODE 0x00 /**< Invalid op code identifier. */
#ifdef NRF51
#define SOC_MAX_WRITE_SIZE 1024 /**< Maximum write size allowed for a single call to \ref sd_flash_write as specified in the SoC API on the nRF51. */
#elif defined(NRF52832_XXAA) || defined(NRF52840_XXAA)
#define SOC_MAX_WRITE_SIZE 4096 /**< Maximum write size allowed for a single call to \ref sd_flash_write as specified in the SoC API on the nRF52. */
#else
#error No target defined
#endif
/**
* @brief Application registration information.
*
* @details Define application specific information that application needs to maintain to be able
* to process requests from each one of them.
*/
typedef struct
{
pstorage_ntf_cb_t cb; /**< Callback registered with the module to be notified of result of flash access. */
} pstorage_module_table_t;
/**
* @brief Defines command queue element.
*
* @details Defines command queue element. Each element encapsulates needed information to process
* a flash access command.
*/
typedef struct
{
uint8_t op_code; /**< Identifies flash access operation being queued. Element is free is op-code is INVALID_OPCODE */
pstorage_size_t size; /**< Identifies size in bytes requested for the operation. */
pstorage_size_t offset; /**< Offset requested by the application for access operation. */
pstorage_handle_t storage_addr; /**< Address/Identifier for persistent memory. */
uint8_t * p_data_addr; /**< Address/Identifier for data memory. This is assumed to be resident memory. */
} cmd_queue_element_t;
/**
* @brief Defines command queue, an element is free is op_code field is not invalid.
*
* @details Defines commands enqueued for flash access. At any point of time, this queue has one or
* more flash access operation pending if the count field is not zero. When the queue is
* not empty, the rp (read pointer) field points to the flash access command in progress
* or to requested next. The queue implements a simple first in first out algorithm.
* Data addresses are assumed to be resident.
*/
typedef struct
{
uint8_t rp; /**< Read pointer, pointing to flash access that is ongoing or to be requested next. */
uint8_t count; /**< Number of elements in the queue. */
bool flash_access; /**< Flag to ensure an flash event received is for an request issued by the module. */
cmd_queue_element_t cmd[PSTORAGE_CMD_QUEUE_SIZE]; /**< Array to maintain flash access operation details */
}cmd_queue_t;
static cmd_queue_t m_cmd_queue; /**< Flash operation request queue. */
static pstorage_module_table_t m_app_table[PSTORAGE_NUM_OF_PAGES]; /**< Registered application information table. */
static pstorage_size_t m_next_app_instance; /**< Points to the application module instance that can be allocated next */
static pstorage_size_t m_round_val; /**< Round value for multiple round operations. For erase operations, the round value will contain current round counter which is identical to number of pages erased. For store operations, the round value contains current round of operation * SOC_MAX_WRITE_SIZE to ensure each store to the SoC Flash API is within the SoC limit. */
/**
* @brief Function for processing of commands and issuing flash access request to the SoftDevice.
*
* @return The return value received from SoftDevice.
*/
static uint32_t cmd_process(void);
/**
* @brief Function for notifying application of any errors.
*
* @param[in] result Result of event being notified.
* @param[in] p_elem Pointer to the element for which a notification should be given.
*/
static void app_notify(uint32_t result, cmd_queue_element_t * p_elem);
/**
* @defgroup utility_functions Utility internal functions.
* @{
* @details Utility functions needed for interfacing with flash through SoC APIs.
* SoC APIs are non blocking and provide the result of flash access through an event.
*
* @note Only one flash access operation is permitted at a time by SoC. Hence a queue is
* maintained by this module.
*/
/**
* @brief Function for initializing a command queue element.
*
* @param[in] index Index identifying element to be initialized.
*/
static void cmd_queue_element_init(uint32_t index)
{
// Internal function and checks on range of index can be avoided
m_cmd_queue.cmd[index].op_code = INVALID_OPCODE;
m_cmd_queue.cmd[index].size = 0;
m_cmd_queue.cmd[index].storage_addr.module_id = PSTORAGE_NUM_OF_PAGES;
m_cmd_queue.cmd[index].storage_addr.block_id = 0;
m_cmd_queue.cmd[index].p_data_addr = NULL;
m_cmd_queue.cmd[index].offset = 0;
}
/**
* @brief Function for initializing the command queue.
*/
static void cmd_queue_init(void)
{
uint32_t cmd_index;
m_round_val = 0;
m_cmd_queue.rp = 0;
m_cmd_queue.count = 0;
m_cmd_queue.flash_access = false;
for(cmd_index = 0; cmd_index < PSTORAGE_CMD_QUEUE_SIZE; cmd_index++)
{
cmd_queue_element_init(cmd_index);
}
}
/**
* @brief Function for enqueueing a flash access operation.
*
* @param[in] opcode Operation code for the command to queue.
* @param[in] p_storage_addr Pointer to the destination address.
* @param[in] p_data_addr Pointer to the source address containing the data.
* @param[in] size Size of data clear or write.
* @param[in] offset Offset to the address identified by the source data address.
*
* @retval NRF_SUCCESS If the enqueueing succeeded.
* @retval NRF_ERROR_NO_MEM In case the queue is full.
* @return Any error returned by the SoftDevice flash API.
*/
static uint32_t cmd_queue_enqueue(uint8_t opcode,
pstorage_handle_t * p_storage_addr,
uint8_t * p_data_addr,
pstorage_size_t size,
pstorage_size_t offset)
{
uint32_t retval;
if (m_cmd_queue.count != PSTORAGE_CMD_QUEUE_SIZE)
{
uint8_t write_index = m_cmd_queue.rp + m_cmd_queue.count;
if (write_index >= PSTORAGE_CMD_QUEUE_SIZE)
{
write_index -= PSTORAGE_CMD_QUEUE_SIZE;
}
m_cmd_queue.cmd[write_index].op_code = opcode;
m_cmd_queue.cmd[write_index].p_data_addr = p_data_addr;
m_cmd_queue.cmd[write_index].storage_addr = (*p_storage_addr);
m_cmd_queue.cmd[write_index].size = size;
m_cmd_queue.cmd[write_index].offset = offset;
retval = NRF_SUCCESS;
if (m_cmd_queue.flash_access == false)
{
retval = cmd_process();
if (retval == NRF_ERROR_BUSY)
{
// In case of busy error code, it is possible to attempt to access flash.
retval = NRF_SUCCESS;
}
}
m_cmd_queue.count++;
}
else
{
retval = NRF_ERROR_NO_MEM;
}
return retval;
}
/**
* @brief Function for dequeueing a command element.
*
* @retval NRF_SUCCESS If the dequeueing succeeded and next command was processed.
* @return Any error returned by the SoftDevice flash API.
*/
static uint32_t cmd_queue_dequeue(void)
{
uint32_t retval = NRF_SUCCESS;
// If any flash operation is enqueued, schedule
if ((m_cmd_queue.count > 0) && (m_cmd_queue.flash_access == false))
{
retval = cmd_process();
if (retval != NRF_SUCCESS)
{
// Flash could be accessed by other modules, hence a busy error is
// acceptable, but any other error needs to be indicated.
if (retval == NRF_ERROR_BUSY)
{
// In case of busy error code, it is possible to attempt to access flash.
retval = NRF_SUCCESS;
}
}
}
else
{
// No flash access request pending.
}
return retval;
}
/**
* @brief Function for notifying application of any errors.
*
* @param[in] result Result of event being notified.
* @param[in] p_elem Pointer to the element for which a notification should be given.
*/
static void app_notify(uint32_t result, cmd_queue_element_t * p_elem)
{
pstorage_ntf_cb_t ntf_cb;
uint8_t op_code = p_elem->op_code;
ntf_cb = m_app_table[p_elem->storage_addr.module_id].cb;
// Indicate result to client.
ntf_cb(&p_elem->storage_addr,
op_code,
result,
p_elem->p_data_addr,
p_elem->size);
}
/**
* @brief Function for handling of system events from SoftDevice.
*
* @param[in] sys_evt System event received.
*/
void pstorage_sys_event_handler(uint32_t sys_evt)
{
uint32_t retval = NRF_SUCCESS;
// The event shall only be processed if requested by this module.
if (m_cmd_queue.flash_access == true)
{
cmd_queue_element_t * p_cmd;
m_cmd_queue.flash_access = false;
switch (sys_evt)
{
case NRF_EVT_FLASH_OPERATION_SUCCESS:
{
p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp];
m_round_val++;
bool command_finished = ((m_round_val * SOC_MAX_WRITE_SIZE) >= p_cmd->size);
if (command_finished)
{
uint8_t queue_rp = m_cmd_queue.rp;
m_round_val = 0;
m_cmd_queue.count--;
m_cmd_queue.rp++;
if (m_cmd_queue.rp >= PSTORAGE_CMD_QUEUE_SIZE)
{
m_cmd_queue.rp -= PSTORAGE_CMD_QUEUE_SIZE;
}
app_notify(retval, &m_cmd_queue.cmd[queue_rp]);
// Initialize/free the element as it is now processed.
cmd_queue_element_init(queue_rp);
}
// Schedule any queued flash access operations.
retval = cmd_queue_dequeue();
if (retval != NRF_SUCCESS)
{
app_notify(retval, &m_cmd_queue.cmd[m_cmd_queue.rp]);
}
}
break;
case NRF_EVT_FLASH_OPERATION_ERROR:
app_notify(NRF_ERROR_TIMEOUT, &m_cmd_queue.cmd[m_cmd_queue.rp]);
break;
default:
// No implementation needed.
break;
}
}
}
/**
* @brief Function for processing of commands and issuing flash access request to the SoftDevice.
*
* @return The return value received from SoftDevice.
*/
static uint32_t cmd_process(void)
{
uint32_t retval;
uint32_t storage_addr;
cmd_queue_element_t * p_cmd;
retval = NRF_ERROR_FORBIDDEN;
p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp];
storage_addr = p_cmd->storage_addr.block_id;
switch (p_cmd->op_code)
{
case PSTORAGE_STORE_OP_CODE:
{
uint32_t size;
uint32_t offset;
uint8_t * p_data_addr = p_cmd->p_data_addr;
offset = (m_round_val * SOC_MAX_WRITE_SIZE);
size = p_cmd->size - offset;
p_data_addr += offset;
storage_addr += (p_cmd->offset + offset);
if (size < SOC_MAX_WRITE_SIZE)
{
retval = sd_flash_write(((uint32_t *)storage_addr),
(uint32_t *)p_data_addr,
size / sizeof(uint32_t));
}
else
{
retval = sd_flash_write(((uint32_t *)storage_addr),
(uint32_t *)p_data_addr,
SOC_MAX_WRITE_SIZE / sizeof(uint32_t));
}
}
break;
case PSTORAGE_CLEAR_OP_CODE:
{
uint32_t page_number;
page_number = ((storage_addr / PSTORAGE_FLASH_PAGE_SIZE) +
m_round_val);
retval = sd_flash_page_erase(page_number);
}
break;
default:
// Should never reach here.
break;
}
if (retval == NRF_SUCCESS)
{
m_cmd_queue.flash_access = true;
}
return retval;
}
/** @} */
uint32_t pstorage_init(void)
{
cmd_queue_init();
m_next_app_instance = 0;
m_round_val = 0;
for(unsigned int index = 0; index < PSTORAGE_NUM_OF_PAGES; index++)
{
m_app_table[index].cb = NULL;
}
return NRF_SUCCESS;
}
uint32_t pstorage_register(pstorage_module_param_t * p_module_param,
pstorage_handle_t * p_block_id)
{
if (m_next_app_instance == PSTORAGE_NUM_OF_PAGES)
{
return NRF_ERROR_NO_MEM;
}
p_block_id->module_id = m_next_app_instance;
m_app_table[m_next_app_instance++].cb = p_module_param->cb;
return NRF_SUCCESS;
}
uint32_t pstorage_block_identifier_get(pstorage_handle_t * p_base_id,
pstorage_size_t block_num,
pstorage_handle_t * p_block_id)
{
return NRF_ERROR_NOT_SUPPORTED;
}
uint32_t pstorage_store(pstorage_handle_t * p_dest,
uint8_t * p_src,
pstorage_size_t size,
pstorage_size_t offset)
{
// Verify word alignment.
if ((!is_word_aligned(p_src)) || (!is_word_aligned(p_src+offset)))
{
return NRF_ERROR_INVALID_ADDR;
}
return cmd_queue_enqueue(PSTORAGE_STORE_OP_CODE, p_dest, p_src, size, offset);
}
uint32_t pstorage_clear(pstorage_handle_t * p_dest, pstorage_size_t size)
{
return cmd_queue_enqueue(PSTORAGE_CLEAR_OP_CODE, p_dest, NULL , size, 0);
}
/**
* @}
*/

View File

@ -0,0 +1,32 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/** @file
*
* @defgroup memory_pool_internal Memory Pool Internal
* @{
* @ingroup memory_pool
*
* @brief Memory pool internal definitions
*/
#ifndef MEM_POOL_INTERNAL_H__
#define MEM_POOL_INTERNAL_H__
#define TX_BUF_SIZE 4u /**< TX buffer size in bytes. */
#define RX_BUF_SIZE 32u /**< RX buffer size in bytes. */
#define RX_BUF_QUEUE_SIZE 8u /**< RX buffer element size. */
#endif // MEM_POOL_INTERNAL_H__
/** @} */

View File

@ -0,0 +1,457 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "sdk_common.h"
#include "bootloader.h"
#include "bootloader_types.h"
#include "bootloader_util.h"
#include "bootloader_settings.h"
#include "dfu.h"
#include "dfu_transport.h"
#include "nrf.h"
#include "app_error.h"
#include "nrf_sdm.h"
#include "nrf_mbr.h"
#include "nordic_common.h"
#include "crc16.h"
#include "pstorage.h"
#include "app_scheduler.h"
#include "nrf_delay.h"
#include "app_timer.h"
#define APP_TIMER_PRESCALER 0
#define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */
#if defined(NRF52832_XXAA) || defined(NRF52840_XXAA)
#define MAX_NUMBER_INTERRUPTS 39
#else
#define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */
#endif
/**@brief Enumeration for specifying current bootloader status.
*/
typedef enum
{
BOOTLOADER_UPDATING, /**< Bootloader status for indicating that an update is in progress. */
BOOTLOADER_SETTINGS_SAVING, /**< Bootloader status for indicating that saving of bootloader settings is in progress. */
BOOTLOADER_COMPLETE, /**< Bootloader status for indicating that all operations for the update procedure has completed and it is safe to reset the system. */
BOOTLOADER_TIMEOUT, /**< Bootloader status field for indicating that a timeout has occured and current update process should be aborted. */
BOOTLOADER_RESET, /**< Bootloader status field for indicating that a reset has been requested and current update process should be aborted. */
} bootloader_status_t;
static pstorage_handle_t m_bootsettings_handle; /**< Pstorage handle to use for registration and identifying the bootloader module on subsequent calls to the pstorage module for load and store of bootloader setting in flash. */
static bootloader_status_t m_update_status; /**< Current update status for the bootloader module to ensure correct behaviour when updating settings and when update completes. */
// Adafruit modification for dual transports and forced startup DFU
extern bool is_ota(void);
APP_TIMER_DEF( _forced_startup_dfu_timer );
volatile bool forced_startup_dfu_packet_received = false;
volatile static bool _terminate_startup_dfu = false;
/**@brief Function for handling callbacks from pstorage module.
*
* @details Handles pstorage results for clear and storage operation. For detailed description of
* the parameters provided with the callback, please refer to \ref pstorage_ntf_cb_t.
*/
static void pstorage_callback_handler(pstorage_handle_t * p_handle,
uint8_t op_code,
uint32_t result,
uint8_t * p_data,
uint32_t data_len)
{
// If we are in BOOTLOADER_SETTINGS_SAVING state and we receive an PSTORAGE_STORE_OP_CODE
// response then settings has been saved and update has completed.
if ((m_update_status == BOOTLOADER_SETTINGS_SAVING) && (op_code == PSTORAGE_STORE_OP_CODE))
{
m_update_status = BOOTLOADER_COMPLETE;
}
APP_ERROR_CHECK(result);
}
// Adafruit modifcation
static void terminate_startup_dfu(void * p_event_data, uint16_t event_size)
{
(void) p_event_data;
(void) event_size;
_terminate_startup_dfu = true;
}
/* Terminate the forced DFU mode on startup if no packets is received
* by put an terminal handler to scheduler
*/
static void forced_startup_dfu_timer_handler(void * p_context)
{
// No packets are received within timeout, terminal and DFU mode
// forced_startup_dfu_packet_received is set by process_dfu_packet() in dfu_transport_serial.c
if (!forced_startup_dfu_packet_received)
{
app_sched_event_put(NULL, 0, terminate_startup_dfu);
}
}
/**@brief Function for waiting for events.
*
* @details This function will place the chip in low power mode while waiting for events from
* the SoftDevice or other peripherals. When interrupted by an event, it will call the
* @ref app_sched_execute function to process the received event. This function will return
* when the final state of the firmware update is reached OR when a tear down is in
* progress.
*/
static void wait_for_events(void)
{
for (;;)
{
// Wait in low power state for any events.
uint32_t err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
// Event received. Process it from the scheduler.
app_sched_execute();
if ((m_update_status == BOOTLOADER_COMPLETE) ||
(m_update_status == BOOTLOADER_TIMEOUT) ||
(m_update_status == BOOTLOADER_RESET))
{
// When update has completed or a timeout/reset occured we will return.
return;
}
// Forced startup dfu mode timeout without any received packet
if (_terminate_startup_dfu)
{
return;
}
}
}
bool bootloader_app_is_valid(uint32_t app_addr)
{
const bootloader_settings_t * p_bootloader_settings;
// There exists an application in CODE region 1.
if (*((uint32_t *)app_addr) == EMPTY_FLASH_MASK)
{
return false;
}
bool success = false;
bootloader_util_settings_get(&p_bootloader_settings);
// The application in CODE region 1 is flagged as valid during update.
if (p_bootloader_settings->bank_0 == BANK_VALID_APP)
{
uint16_t image_crc = 0;
// A stored crc value of 0 indicates that CRC checking is not used.
if (p_bootloader_settings->bank_0_crc != 0)
{
image_crc = crc16_compute((uint8_t *)DFU_BANK_0_REGION_START,
p_bootloader_settings->bank_0_size,
NULL);
}
success = (image_crc == p_bootloader_settings->bank_0_crc);
}
return success;
}
static void bootloader_settings_save(bootloader_settings_t * p_settings)
{
uint32_t err_code = pstorage_clear(&m_bootsettings_handle, sizeof(bootloader_settings_t));
APP_ERROR_CHECK(err_code);
err_code = pstorage_store(&m_bootsettings_handle,
(uint8_t *)p_settings,
sizeof(bootloader_settings_t),
0);
APP_ERROR_CHECK(err_code);
}
void bootloader_dfu_update_process(dfu_update_status_t update_status)
{
static bootloader_settings_t settings;
const bootloader_settings_t * p_bootloader_settings;
bootloader_util_settings_get(&p_bootloader_settings);
if (update_status.status_code == DFU_UPDATE_APP_COMPLETE)
{
settings.bank_0_crc = update_status.app_crc;
settings.bank_0_size = update_status.app_size;
settings.bank_0 = BANK_VALID_APP;
settings.bank_1 = BANK_INVALID_APP;
m_update_status = BOOTLOADER_SETTINGS_SAVING;
bootloader_settings_save(&settings);
}
else if (update_status.status_code == DFU_UPDATE_SD_COMPLETE)
{
settings.bank_0_crc = update_status.app_crc;
settings.bank_0_size = update_status.sd_size +
update_status.bl_size +
update_status.app_size;
settings.bank_0 = BANK_VALID_SD;
settings.bank_1 = BANK_INVALID_APP;
settings.sd_image_size = update_status.sd_size;
settings.bl_image_size = update_status.bl_size;
settings.app_image_size = update_status.app_size;
settings.sd_image_start = update_status.sd_image_start;
m_update_status = BOOTLOADER_SETTINGS_SAVING;
bootloader_settings_save(&settings);
}
else if (update_status.status_code == DFU_UPDATE_BOOT_COMPLETE)
{
settings.bank_0 = p_bootloader_settings->bank_0;
settings.bank_0_crc = p_bootloader_settings->bank_0_crc;
settings.bank_0_size = p_bootloader_settings->bank_0_size;
settings.bank_1 = BANK_VALID_BOOT;
settings.sd_image_size = update_status.sd_size;
settings.bl_image_size = update_status.bl_size;
settings.app_image_size = update_status.app_size;
m_update_status = BOOTLOADER_SETTINGS_SAVING;
bootloader_settings_save(&settings);
}
else if (update_status.status_code == DFU_UPDATE_SD_SWAPPED)
{
if (p_bootloader_settings->bank_0 == BANK_VALID_SD)
{
settings.bank_0_crc = 0;
settings.bank_0_size = 0;
settings.bank_0 = BANK_INVALID_APP;
}
// This handles cases where SoftDevice was not updated, hence bank0 keeps its settings.
else
{
settings.bank_0 = p_bootloader_settings->bank_0;
settings.bank_0_crc = p_bootloader_settings->bank_0_crc;
settings.bank_0_size = p_bootloader_settings->bank_0_size;
}
settings.bank_1 = BANK_INVALID_APP;
settings.sd_image_size = 0;
settings.bl_image_size = 0;
settings.app_image_size = 0;
m_update_status = BOOTLOADER_SETTINGS_SAVING;
bootloader_settings_save(&settings);
}
else if (update_status.status_code == DFU_TIMEOUT)
{
// Timeout has occurred. Close the connection with the DFU Controller.
uint32_t err_code;
if ( is_ota() )
{
err_code = dfu_transport_ble_close();
}else
{
err_code = dfu_transport_serial_close();
}
APP_ERROR_CHECK(err_code);
m_update_status = BOOTLOADER_TIMEOUT;
}
else if (update_status.status_code == DFU_BANK_0_ERASED)
{
settings.bank_0_crc = 0;
settings.bank_0_size = 0;
settings.bank_0 = BANK_INVALID_APP;
settings.bank_1 = p_bootloader_settings->bank_1;
bootloader_settings_save(&settings);
}
else if (update_status.status_code == DFU_RESET)
{
m_update_status = BOOTLOADER_RESET;
}
else
{
// No implementation needed.
}
}
uint32_t bootloader_init(void)
{
uint32_t err_code;
pstorage_module_param_t storage_params = {.cb = pstorage_callback_handler};
err_code = pstorage_init();
VERIFY_SUCCESS(err_code);
m_bootsettings_handle.block_id = BOOTLOADER_SETTINGS_ADDRESS;
err_code = pstorage_register(&storage_params, &m_bootsettings_handle);
return err_code;
}
uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms)
{
uint32_t err_code;
// Clear swap if banked update is used.
err_code = dfu_init();
VERIFY_SUCCESS(err_code);
if ( ota )
{
err_code = dfu_transport_ble_update_start();
}else
{
// timeout_ms > 0 is forced startup DFU
if ( timeout_ms )
{
forced_startup_dfu_packet_received = false;
_terminate_startup_dfu = false;
(void) app_timer_create(&_forced_startup_dfu_timer, APP_TIMER_MODE_SINGLE_SHOT, forced_startup_dfu_timer_handler);
app_timer_start(_forced_startup_dfu_timer, APP_TIMER_TICKS(timeout_ms), NULL);
}
err_code = dfu_transport_serial_update_start();
}
wait_for_events();
// Close Serial transport after done
if ( !ota ) dfu_transport_serial_close();
return err_code;
}
/**@brief Function for disabling all interrupts before jumping from bootloader to application.
*/
static void interrupts_disable(void)
{
uint32_t interrupt_setting_mask;
uint32_t irq = 0; // We start from first interrupt, i.e. interrupt 0.
// Fetch the current interrupt settings.
interrupt_setting_mask = NVIC->ISER[0];
for (; irq < MAX_NUMBER_INTERRUPTS; irq++)
{
if (interrupt_setting_mask & (IRQ_ENABLED << irq))
{
// The interrupt was enabled, and hence disable it.
NVIC_DisableIRQ((IRQn_Type)irq);
}
}
}
void bootloader_app_start(uint32_t app_addr)
{
// If the applications CRC has been checked and passed, the magic number will be written and we
// can start the application safely.
uint32_t err_code = sd_softdevice_disable();
APP_ERROR_CHECK(err_code);
interrupts_disable();
// Disable RTC1
NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
NRF_RTC1->TASKS_STOP = 1;
NRF_RTC1->TASKS_CLEAR = 1;
err_code = sd_softdevice_vector_table_base_set(CODE_REGION_1_START);
APP_ERROR_CHECK(err_code);
bootloader_util_app_start(CODE_REGION_1_START);
}
bool bootloader_dfu_sd_in_progress(void)
{
const bootloader_settings_t * p_bootloader_settings;
bootloader_util_settings_get(&p_bootloader_settings);
if (p_bootloader_settings->bank_0 == BANK_VALID_SD ||
p_bootloader_settings->bank_1 == BANK_VALID_BOOT)
{
return true;
}
return false;
}
uint32_t bootloader_dfu_sd_update_continue(void)
{
uint32_t err_code;
if ((dfu_sd_image_validate() == NRF_SUCCESS) &&
(dfu_bl_image_validate() == NRF_SUCCESS))
{
return NRF_SUCCESS;
}
// Ensure that flash operations are not executed within the first 100 ms seconds to allow
// a debugger to be attached.
nrf_delay_ms(100);
err_code = dfu_sd_image_swap();
APP_ERROR_CHECK(err_code);
err_code = dfu_sd_image_validate();
APP_ERROR_CHECK(err_code);
err_code = dfu_bl_image_swap();
APP_ERROR_CHECK(err_code);
return err_code;
}
uint32_t bootloader_dfu_sd_update_finalize(void)
{
dfu_update_status_t update_status = {DFU_UPDATE_SD_SWAPPED, };
bootloader_dfu_update_process(update_status);
wait_for_events();
return NRF_SUCCESS;
}
void bootloader_settings_get(bootloader_settings_t * const p_settings)
{
const bootloader_settings_t * p_bootloader_settings;
bootloader_util_settings_get(&p_bootloader_settings);
p_settings->bank_0 = p_bootloader_settings->bank_0;
p_settings->bank_0_crc = p_bootloader_settings->bank_0_crc;
p_settings->bank_0_size = p_bootloader_settings->bank_0_size;
p_settings->bank_1 = p_bootloader_settings->bank_1;
p_settings->sd_image_size = p_bootloader_settings->sd_image_size;
p_settings->bl_image_size = p_bootloader_settings->bl_image_size;
p_settings->app_image_size = p_bootloader_settings->app_image_size;
p_settings->sd_image_start = p_bootloader_settings->sd_image_start;
}

View File

@ -0,0 +1,97 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_bootloader Bootloader API.
* @{
*
* @brief Bootloader module interface.
*/
#ifndef BOOTLOADER_H__
#define BOOTLOADER_H__
#include <stdbool.h>
#include <stdint.h>
#include "bootloader_types.h"
#include <dfu_types.h>
/**@brief Function for initializing the Bootloader.
*
* @retval NRF_SUCCESS If bootloader was succesfully initialized.
*/
uint32_t bootloader_init(void);
/**@brief Function for validating application region in flash.
*
* @param[in] app_addr Address to the region in flash where the application is stored.
*
* @retval true If Application region is valid.
* @retval false If Application region is not valid.
*/
bool bootloader_app_is_valid(uint32_t app_addr);
/**@brief Function for starting the Device Firmware Update.
*
* @retval NRF_SUCCESS If new application image was successfully transferred.
*/
uint32_t bootloader_dfu_start(bool ota, uint32_t timeout_ms);
/**@brief Function for exiting bootloader and booting into application.
*
* @details This function will disable SoftDevice and all interrupts before jumping to application.
* The SoftDevice vector table base for interrupt forwarding will be set the application
* address.
*
* @param[in] app_addr Address to the region where the application is stored.
*/
void bootloader_app_start(uint32_t app_addr);
/**@brief Function for retrieving the bootloader settings.
*
* @param[out] p_settings A copy of the current bootloader settings is returned in the structure
* provided.
*/
void bootloader_settings_get(bootloader_settings_t * const p_settings);
/**@brief Function for processing DFU status update.
*
* @param[in] update_status DFU update status.
*/
void bootloader_dfu_update_process(dfu_update_status_t update_status);
/**@brief Function getting state of SoftDevice update in progress.
* After a successfull SoftDevice transfer the system restarts in orderto disable SoftDevice
* and complete the update.
*
* @retval true A SoftDevice update is in progress. This indicates that second stage
* of a SoftDevice update procedure can be initiated.
* @retval false No SoftDevice update is in progress.
*/
bool bootloader_dfu_sd_in_progress(void);
/**@brief Function for continuing the Device Firmware Update of a SoftDevice.
*
* @retval NRF_SUCCESS If the final stage of SoftDevice update was successful.
*/
uint32_t bootloader_dfu_sd_update_continue(void);
/**@brief Function for finalizing the Device Firmware Update of a SoftDevice.
*
* @retval NRF_SUCCESS If the final stage of SoftDevice update was successful.
*/
uint32_t bootloader_dfu_sd_update_finalize(void);
#endif // BOOTLOADER_H__
/**@} */

View File

@ -0,0 +1,63 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "bootloader_settings.h"
#include <stdint.h>
#include <dfu_types.h>
#if defined ( __CC_ARM )
uint8_t m_boot_settings[CODE_PAGE_SIZE] __attribute__((at(BOOTLOADER_SETTINGS_ADDRESS))) __attribute__((used)); /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
uint32_t m_uicr_bootloader_start_address __attribute__((at(NRF_UICR_BOOT_START_ADDRESS))) = BOOTLOADER_REGION_START; /**< This variable makes the linker script write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
#elif defined ( __GNUC__ )
uint8_t m_boot_settings[CODE_PAGE_SIZE] __attribute__ ((section(".bootloaderSettings"))); /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
volatile uint32_t m_uicr_bootloader_start_address __attribute__ ((section(".uicrBootStartAddress"))) = BOOTLOADER_REGION_START; /**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
#elif defined ( __ICCARM__ )
__no_init uint8_t m_boot_settings[CODE_PAGE_SIZE] @ BOOTLOADER_SETTINGS_ADDRESS; /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
__root const uint32_t m_uicr_bootloader_start_address @ NRF_UICR_BOOT_START_ADDRESS = BOOTLOADER_REGION_START; /**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
#endif
#if defined(NRF52832_XXAA) || defined(NRF52840_XXAA)
#if defined ( __CC_ARM )
uint8_t m_mbr_params_page[CODE_PAGE_SIZE] __attribute__((at(BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS))) __attribute__((used)); /**< This variable reserves a codepage for mbr parameters, to ensure the compiler doesn't locate any code or variables at his location. */
uint32_t m_uicr_mbr_params_page_address __attribute__((at(NRF_UICR_MBR_PARAMS_PAGE_ADDRESS)))
= BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable makes the linker script write the mbr parameters page address to the UICR register. This value will be written in the HEX file and thus written to the UICR when the bootloader is flashed into the chip */
#elif defined (__GNUC__ )
uint8_t m_mbr_params_page[CODE_PAGE_SIZE] __attribute__ ((section(".mbrParamsPage"))); /**< This variable reserves a codepage for mbr parameters, to ensure the compiler doesn't locate any code or variables at his location. */
volatile uint32_t m_uicr_mbr_params_page_address __attribute__ ((section(".uicrMbrParamsPageAddress")))
= BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable makes the linker script write the mbr parameters page address to the UICR register. This value will be written in the HEX file and thus written to the UICR when the bootloader is flashed into the chip */
#elif defined (__ICCARM__ )
__no_init uint8_t m_mbr_params_page[CODE_PAGE_SIZE] @ BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
__root const uint32_t m_uicr_mbr_params_page_address @ NRF_UICR_MBR_PARAMS_PAGE_ADDRESS = BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
#endif
#endif // defined(NRF52832_XXAA) || defined(NRF52840_XXAA)
void bootloader_util_settings_get(const bootloader_settings_t ** pp_bootloader_settings)
{
// Read only pointer to bootloader settings in flash.
bootloader_settings_t const * const p_bootloader_settings =
(bootloader_settings_t *)&m_boot_settings[0];
*pp_bootloader_settings = p_bootloader_settings;
}

View File

@ -0,0 +1,35 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_bootloader_settings Bootloader settings API.
* @{
*
* @brief Bootloader settings module interface.
*/
#ifndef BOOTLOADER_SETTINGS_H__
#define BOOTLOADER_SETTINGS_H__
#include <stdint.h>
#include "bootloader_types.h"
/**@brief Function for getting the bootloader settings.
*
* @param[out] pp_bootloader_settings Bootloader settings.
*/
void bootloader_util_settings_get(const bootloader_settings_t ** pp_bootloader_settings);
#endif // BOOTLOADER_SETTINGS_H__
/**@} */

View File

@ -0,0 +1,59 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_bootloader_types Types and definitions.
* @{
*
* @ingroup nrf_bootloader
*
* @brief Bootloader module type and definitions.
*/
#ifndef BOOTLOADER_TYPES_H__
#define BOOTLOADER_TYPES_H__
#include <stdint.h>
#define BOOTLOADER_DFU_START 0xB1
#define BOOTLOADER_SVC_APP_DATA_PTR_GET 0x02
/**@brief DFU Bank state code, which indicates wether the bank contains: A valid image, invalid image, or an erased flash.
*/
typedef enum
{
BANK_VALID_APP = 0x01,
BANK_VALID_SD = 0xA5,
BANK_VALID_BOOT = 0xAA,
BANK_ERASED = 0xFE,
BANK_INVALID_APP = 0xFF,
} bootloader_bank_code_t;
/**@brief Structure holding bootloader settings for application and bank data.
*/
typedef struct
{
bootloader_bank_code_t bank_0; /**< Variable to store if bank 0 contains a valid application. */
uint16_t bank_0_crc; /**< If bank is valid, this field will contain a valid CRC of the total image. */
bootloader_bank_code_t bank_1; /**< Variable to store if bank 1 has been erased/prepared for new image. Bank 1 is only used in Banked Update scenario. */
uint32_t bank_0_size; /**< Size of active image in bank0 if present, otherwise 0. */
uint32_t sd_image_size; /**< Size of SoftDevice image in bank0 if bank_0 code is BANK_VALID_SD. */
uint32_t bl_image_size; /**< Size of Bootloader image in bank0 if bank_0 code is BANK_VALID_SD. */
uint32_t app_image_size; /**< Size of Application image in bank0 if bank_0 code is BANK_VALID_SD. */
uint32_t sd_image_start; /**< Location in flash where SoftDevice image is stored for SoftDevice update. */
} bootloader_settings_t;
#endif // BOOTLOADER_TYPES_H__
/**@} */

View File

@ -0,0 +1,152 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "bootloader_util.h"
#include <stdint.h>
#include <string.h>
/**
* @brief Function for aborting current application/bootloader jump to to other app/bootloader.
*
* @details This functions will use the address provide to swap the stack pointer and then load
* the address of the reset handler to be executed. It will check current system mode
* (thread/handler) and if in thread mode it will reset into other application.
* If in handler mode \ref isr_abort will be executed to ensure correct exit of handler
* mode and jump into reset handler of other application.
*
* @param[in] start_addr Start address of other application. This address must point to the
initial stack pointer of the application.
*
* @note This function will never return but issue a reset into provided application.
*/
#if defined ( __CC_ARM )
__asm static void bootloader_util_reset(uint32_t start_addr)
{
LDR R5, [R0] ; Get App initial MSP for bootloader.
MSR MSP, R5 ; Set the main stack pointer to the applications MSP.
LDR R0, [R0, #0x04] ; Load Reset handler into R0. This will be first argument to branch instruction (BX).
MOVS R4, #0xFF ; Load ones to R4.
SXTB R4, R4 ; Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
MRS R5, IPSR ; Load IPSR to R5 to check for handler or thread mode.
CMP R5, #0x00 ; Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
BNE isr_abort ; If not zero we need to exit current ISR and jump to reset handler of bootloader.
MOV LR, R4 ; Clear the link register and set to ones to ensure no return, R4 = 0xFFFFFFFF.
BX R0 ; Branch to reset handler of bootloader.
isr_abort
; R4 contains ones from line above. Will be popped as R12 when exiting ISR (Cleaning up the registers).
MOV R5, R4 ; Fill with ones before jumping to reset handling. We be popped as LR when exiting ISR. Ensures no return to application.
MOV R6, R0 ; Move address of reset handler to R6. Will be popped as PC when exiting ISR. Ensures the reset handler will be executed when exist ISR.
MOVS r7, #0x21 ; Move MSB reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. xPSR is 0x21000000 thus MSB is 0x21.
REV r7, r7 ; Reverse byte order to put 0x21 as MSB.
PUSH {r4-r7} ; Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
MOVS R4, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
MOVS R5, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
MOVS R6, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
MOVS R7, #0x00 ; Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
PUSH {r4-r7} ; Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
MOVS R0, #0xF9 ; Move the execution return command into register, 0xFFFFFFF9.
SXTB R0, R0 ; Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
BX R0 ; No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
ALIGN
}
#elif defined ( __GNUC__ )
static inline void bootloader_util_reset(uint32_t start_addr)
{
__asm volatile(
"ldr r0, [%0]\t\n" // Get App initial MSP for bootloader.
"msr msp, r0\t\n" // Set the main stack pointer to the applications MSP.
"ldr r0, [%0, #0x04]\t\n" // Load Reset handler into R0.
"movs r4, #0xFF\t\n" // Move ones to R4.
"sxtb r4, r4\t\n" // Sign extend R4 to obtain 0xFFFFFFFF instead of 0xFF.
"mrs r5, IPSR\t\n" // Load IPSR to R5 to check for handler or thread mode.
"cmp r5, #0x00\t\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
"bne isr_abort\t\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
"mov lr, r4\t\n" // Clear the link register and set to ones to ensure no return.
"bx r0\t\n" // Branch to reset handler of bootloader.
"isr_abort: \t\n"
"mov r5, r4\t\n" // Fill with ones before jumping to reset handling. Will be popped as LR when exiting ISR. Ensures no return to application.
"mov r6, r0\t\n" // Move address of reset handler to R6. Will be popped as PC when exiting ISR. Ensures the reset handler will be executed when exist ISR.
"movs r7, #0x21\t\n" // Move MSB reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. xPSR is 0x21000000 thus MSB is 0x21.
"rev r7, r7\t\n" // Reverse byte order to put 0x21 as MSB.
"push {r4-r7}\t\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
"movs r4, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
"movs r5, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
"movs r6, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
"movs r7, #0x00\t\n" // Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
"push {r4-r7}\t\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
"movs r0, #0xF9\t\n" // Move the execution return command into register, 0xFFFFFFF9.
"sxtb r0, r0\t\n" // Sign extend R0 to obtain 0xFFFFFFF9 instead of 0xF9.
"bx r0\t\n" // No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
".align\t\n"
:: "r" (start_addr) // Argument list for the gcc assembly. start_addr is %0.
: "r0", "r4", "r5", "r6", "r7" // List of register maintained manually.
);
}
#elif defined ( __ICCARM__ )
static inline void bootloader_util_reset(uint32_t start_addr)
{
asm("ldr r5, [%0]\n" // Get App initial MSP for bootloader.
"msr msp, r5\n" // Set the main stack pointer to the applications MSP.
"ldr r0, [%0, #0x04]\n" // Load Reset handler into R0.
"movs r4, #0x00\n" // Load zero into R4.
"mvns r4, r4\n" // Invert R4 to ensure it contain ones.
"mrs r5, IPSR\n" // Load IPSR to R5 to check for handler or thread mode
"cmp r5, #0x00\n" // Compare, if 0 then we are in thread mode and can continue to reset handler of bootloader.
"bne.n isr_abort\n" // If not zero we need to exit current ISR and jump to reset handler of bootloader.
"mov lr, r4\n" // Clear the link register and set to ones to ensure no return.
"bx r0\n" // Branch to reset handler of bootloader.
"isr_abort: \n"
// R4 contains ones from line above. We be popped as R12 when exiting ISR (Cleaning up the registers).
"mov r5, r4\n" // Fill with ones before jumping to reset handling. Will be popped as LR when exiting ISR. Ensures no return to application.
"mov r6, r0\n" // Move address of reset handler to R6. Will be popped as PC when exiting ISR. Ensures the reset handler will be executed when exist ISR.
"movs r7, #0x21\n" // Move MSB reset value of xPSR to R7. Will be popped as xPSR when exiting ISR. xPSR is 0x21000000 thus MSB is 0x21.
"rev r7, r7\n" // Reverse byte order to put 0x21 as MSB.
"push {r4-r7}\n" // Push everything to new stack to allow interrupt handler to fetch it on exiting the ISR.
"movs r4, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R0 when exiting ISR (Cleaning up of the registers).
"movs r5, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R1 when exiting ISR (Cleaning up of the registers).
"movs r6, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R2 when exiting ISR (Cleaning up of the registers).
"movs r7, #0x00\n" // Fill with zeros before jumping to reset handling. We be popped as R3 when exiting ISR (Cleaning up of the registers).
"push {r4-r7}\n" // Push zeros (R4-R7) to stack to prepare for exiting the interrupt routine.
"movs r0, #0x06\n" // Load 0x06 into R6 to prepare for exec return command.
"mvns r0, r0\n" // Invert 0x06 to obtain EXEC_RETURN, 0xFFFFFFF9.
"bx r0\n" // No return - Handler mode will be exited. Stack will be popped and execution will continue in reset handler initializing other application.
:: "r" (start_addr) // Argument list for the IAR assembly. start_addr is %0.
: "r0", "r4", "r5", "r6", "r7"); // List of register maintained manually.
}
#else
#error Compiler not supported.
#endif
void bootloader_util_app_start(uint32_t start_addr)
{
bootloader_util_reset(start_addr);
}

View File

@ -0,0 +1,38 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_bootloader_util Bootloader util API.
* @{
*
* @brief Bootloader util module interface.
*/
#ifndef BOOTLOADER_UTIL_H__
#define BOOTLOADER_UTIL_H__
#include <stdint.h>
#include "bootloader_types.h"
/**@brief Function for starting the application (or bootloader) at the provided address.
*
* @param[in] start_addr Start address.
*
* @note This function will never retrun. Instead it will reset into the application of the
* provided address.
*/
void bootloader_util_app_start(uint32_t start_addr);
#endif // BOOTLOADER_UTIL_H__
/**@} */

View File

@ -0,0 +1,134 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_dfu Device Firmware Update API.
* @{
*
* @brief Device Firmware Update module interface.
*/
#ifndef DFU_H__
#define DFU_H__
#include <dfu_types.h>
#include <stdbool.h>
#include <stdint.h>
/**@brief DFU event callback for asynchronous calls.
*
* @param[in] packet Packet type for which this callback is related. START_PACKET, DATA_PACKET.
* @param[in] result Operation result code. NRF_SUCCESS when a queued operation was successful.
* @param[in] p_data Pointer to the data to which the operation is related.
*/
typedef void (*dfu_callback_t)(uint32_t packet, uint32_t result, uint8_t * p_data);
/**@brief Function for initializing the Device Firmware Update module.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_init(void);
/**@brief Function for registering a callback listener for \ref dfu_data_pkt_handle callbacks.
*
* @param[in] callback_handler Callback handler for receiving DFU events on completed operations
* of DFU packets.
*/
void dfu_register_callback(dfu_callback_t callback_handler);
/**@brief Function for setting the DFU image size.
*
* @details Function sets the DFU image size. This function must be called when an update is started
* in order to notify the DFU of the new image size. If multiple images are to be
* transferred within the same update context then this function must be called with size
* information for each image being transfered.
* If an image type is not being transfered, e.g. SoftDevice but no Application , then the
* image size for application must be zero.
*
* @param[in] p_packet Pointer to the DFU packet containing information on DFU update process to
* be started.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_start_pkt_handle(dfu_update_packet_t * p_packet);
/**@brief Function for handling DFU data packets.
*
* @param[in] p_packet Pointer to the DFU packet.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_data_pkt_handle(dfu_update_packet_t * p_packet);
/**@brief Function for handling DFU init packets.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_init_pkt_handle(dfu_update_packet_t * p_packet);
/**@brief Function for validating a transferred image after the transfer has completed.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_image_validate(void);
/**@brief Function for activating the transfered image after validation has successfully completed.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_image_activate(void);
/**@brief Function for reseting the current update procedure and return to initial state.
*
* @details This function call will result in a system reset to ensure correct system behavior.
* The reset will might be scheduled to execute at a later point in time to ensure pending
* flash operations has completed.
*/
void dfu_reset(void);
/**@brief Function for validating that new bootloader has been correctly installed.
*
* @return NRF_SUCCESS if install was successful. NRF_ERROR_NULL if the images differs.
*/
uint32_t dfu_bl_image_validate(void);
/**@brief Function for validating that new SoftDevicehas been correctly installed.
*
* @return NRF_SUCCESS if install was successful. NRF_ERROR_NULL if the images differs.
*/
uint32_t dfu_sd_image_validate(void);
/**@brief Function for swapping existing bootloader with newly received.
*
* @return NRF_SUCCESS on succesfull swapping. For error code please refer to
* \ref sd_mbr_command_copy_bl_t.
*/
uint32_t dfu_bl_image_swap(void);
/**@brief Function for swapping existing SoftDevice with newly received.
*
* @return NRF_SUCCESS on succesfull swapping. For error code please refer to
* \ref sd_mbr_command_copy_sd_t.
*/
uint32_t dfu_sd_image_swap(void);
/**@brief Function for handling DFU init packet complete.
*
* @return NRF_SUCCESS on success, an error_code otherwise.
*/
uint32_t dfu_init_pkt_complete(void);
#endif // DFU_H__
/** @} */

View File

@ -0,0 +1,87 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup dfu_bank_internal Device Firmware Update internal header for bank handling in DFU.
* @{
*
* @brief Device Firmware Update Bank handling module interface.
*
* @details This header is intended for shared definition and functions between single and dual bank
* implementations used for DFU support. It is not supposed to be used for external access
* to the DFU module.
*
*/
#ifndef DFU_BANK_INTERNAL_H__
#define DFU_BANK_INTERNAL_H__
#include <dfu_types.h>
/**@brief States of the DFU state machine. */
typedef enum
{
DFU_STATE_INIT_ERROR, /**< State for: dfu_init(...) error. */
DFU_STATE_IDLE, /**< State for: idle. */
DFU_STATE_PREPARING, /**< State for: preparing, indicates that the flash is being erased and no data packets can be processed. */
DFU_STATE_RDY, /**< State for: ready. */
DFU_STATE_RX_INIT_PKT, /**< State for: receiving initialization packet. */
DFU_STATE_RX_DATA_PKT, /**< State for: receiving data packet. */
DFU_STATE_VALIDATE, /**< State for: validate. */
DFU_STATE_WAIT_4_ACTIVATE /**< State for: waiting for dfu_image_activate(). */
} dfu_state_t;
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define DFU_TIMEOUT_INTERVAL APP_TIMER_TICKS(300000) /**< DFU timeout interval in units of timer ticks. */
#define IS_UPDATING_SD(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_SD) /**< Macro for determining if a SoftDevice update is ongoing. */
#define IS_UPDATING_BL(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_BL) /**< Macro for determining if a Bootloader update is ongoing. */
#define IS_UPDATING_APP(START_PKT) ((START_PKT).dfu_update_mode & DFU_UPDATE_APP) /**< Macro for determining if a Application update is ongoing. */
#define IMAGE_WRITE_IN_PROGRESS() (m_data_received > 0) /**< Macro for determining if an image write is in progress. */
#define IS_WORD_SIZED(SIZE) ((SIZE & (sizeof(uint32_t) - 1)) == 0) /**< Macro for checking that the provided is word sized. */
/**@cond NO_DOXYGEN */
static uint32_t m_data_received; /**< Amount of received data. */
/**@endcond */
/**@brief Type definition of function used for preparing of the bank before receiving of a
* software image.
*
* @param[in] image_size Size of software image being received.
*/
typedef void (*dfu_bank_prepare_t)(uint32_t image_size);
/**@brief Type definition of function used for handling clear complete of the bank before
* receiving of a software image.
*/
typedef void (*dfu_bank_cleared_t)(void);
/**@brief Type definition of function used for activating of the software image received.
*
* @return NRF_SUCCESS If the image has been successfully activated any other NRF_ERROR code in
* case of a failure.
*/
typedef uint32_t (*dfu_bank_activate_t)(void);
/**@brief Structure for holding of function pointers for needed prepare and activate procedure for
* the requested update procedure.
*/
typedef struct
{
dfu_bank_prepare_t prepare; /**< Function pointer to the prepare function called on start of update procedure. */
dfu_bank_cleared_t cleared; /**< Function pointer to the cleared function called after prepare function completes. */
dfu_bank_activate_t activate; /**< Function pointer to the activate function called on finalizing the update procedure. */
} dfu_bank_func_t;
#endif // DFU_BANK_INTERNAL_H__
/** @} */

View File

@ -0,0 +1,80 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/** @file
*
* @defgroup nrf_dfu_ble_svc DFU BLE SVC
* @{
*
* @brief DFU BLE SVC in bootloader. The DFU BLE SuperVisor Calls allow an application to execute
* functions in the installed bootloader.
*
* @details This module implements handling of SuperVisor Calls in the bootloader.
* SuperVisor Calls allow for an application to execute calls into the bootloader.
* Currently, it is possible to exchange bonding information (like keys) from the
* application to a bootloader supporting DFU OTA using BLE, so the update process can be
* done through an already existing bond.
*
* @note The application must make sure that all SuperVisor Calls (SVC) are forwarded to the
* bootloader to ensure correct behavior. Forwarding of SVCs to the bootloader is
* done using the SoftDevice SVC @ref sd_softdevice_vector_table_base_set with the value
* present in @c NRF_UICR->NRFFW[0].
*/
#ifndef DFU_BLE_SVC_H__
#define DFU_BLE_SVC_H__
#include "nrf_svc.h"
#include <stdint.h>
#include "ble_gap.h"
#include "nrf.h"
#include "nrf_soc.h"
#include "nrf_error_sdm.h"
#define BOOTLOADER_SVC_BASE 0x0 /**< The number of the lowest SVC number reserved for the bootloader. */
#define SYSTEM_SERVICE_ATT_SIZE 8 /**< Size of the system service attribute length including CRC-16 at the end. */
/**@brief The SVC numbers used by the SVC functions in the SoC library. */
enum BOOTLOADER_SVCS
{
DFU_BLE_SVC_PEER_DATA_SET = BOOTLOADER_SVC_BASE, /**< SVC number for the setting of peer data call. */
BOOTLOADER_SVC_LAST
};
/**@brief DFU Peer data structure.
*
* @details This structure contains peer data needed for connection to a bonded device during DFU.
* The peer data must be provided by the application to the bootloader during buttonless
* update. See @ref dfu_ble_svc_peer_data_set. It contains bond information about the
* desired DFU peer.
*/
typedef struct
{
ble_gap_addr_t addr; /**< BLE GAP address of the device that initiated the DFU process. */
ble_gap_irk_t irk; /**< IRK of the device that initiated the DFU process if this device uses Private Resolvable Addresses. */
ble_gap_enc_key_t enc_key; /**< Encryption key structure containing encrypted diversifier and LTK for re-establishing the bond. */
uint8_t sys_serv_attr[SYSTEM_SERVICE_ATT_SIZE]; /**< System service attributes for restoring of Service Changed Indication setting in DFU mode. */
} dfu_ble_peer_data_t;
/**@brief SVC Function for setting peer data containing address, IRK, and LTK to establish bonded
* connection in DFU mode.
*
* @param[in] p_peer_data Pointer to the peer data containing keys for the connection.
*
* @retval NRF_ERROR_NULL If a NULL pointer was provided as argument.
* @retval NRF_SUCCESS If the function completed successfully.
*/
SVCALL(DFU_BLE_SVC_PEER_DATA_SET, uint32_t, dfu_ble_svc_peer_data_set(dfu_ble_peer_data_t * p_peer_data));
#endif // DFU_BLE_SVC_H__
/** @} */

View File

@ -0,0 +1,43 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/** @file
*
* @defgroup nrf_dfu_ble_svc_internal DFU BLE SVC internal
* @{
*
* @brief DFU BLE SVC internal functions in bootloader. The DFU BLE SuperVisor Calls allow an
* application to execute functions in the installed bootloader. This interface provides
* internal Bootloader DFU functions for retrieving data exchanged through SuperVisor Calls.
*
*/
#ifndef DFU_BLE_SVC_INTERNAL_H__
#define DFU_BLE_SVC_INTERNAL_H__
#include <stdint.h>
#include "dfu_ble_svc.h"
#include "ble_gap.h"
/**@brief Internal bootloader/DFU function for retrieving peer data provided from application.
*
* @param[out] p_peer_data Peer data set by application to be used for DFU connection.
*
* @retval NRF_SUCCESS If peer data is valid and can be used for connection.
* @retval NRF_ERROR_NULL If p_peer_data is a NULL pointer.
* @retval NRF_ERROR_INVALID_DATA If peer data is not available or invalid.
*/
uint32_t dfu_ble_peer_data_get(dfu_ble_peer_data_t * p_peer_data);
#endif // DFU_BLE_SVC_INTERNAL_H__
/** @} */

View File

@ -0,0 +1,840 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include <stddef.h>
#include "dfu.h"
#include <dfu_types.h>
#include "dfu_bank_internal.h"
#include "nrf.h"
#include "nrf_sdm.h"
#include "app_error.h"
#include "app_timer.h"
#include "bootloader.h"
#include "bootloader_types.h"
#include "pstorage.h"
#include "nrf_mbr.h"
#include "dfu_init.h"
#include "sdk_common.h"
static dfu_state_t m_dfu_state; /**< Current DFU state. */
static uint32_t m_image_size; /**< Size of the image that will be transmitted. */
static dfu_start_packet_t m_start_packet; /**< Start packet received for this update procedure. Contains update mode and image sizes information to be used for image transfer. */
static uint8_t m_init_packet[128]; /**< Init packet, can hold CRC, Hash, Signed Hash and similar, for image validation, integrety check and authorization checking. */
static uint8_t m_init_packet_length; /**< Length of init packet received. */
static uint16_t m_image_crc; /**< Calculated CRC of the image received. */
APP_TIMER_DEF(m_dfu_timer_id); /**< Application timer id. */
static bool m_dfu_timed_out = false; /**< Boolean flag value for tracking DFU timer timeout state. */
static pstorage_handle_t m_storage_handle_swap; /**< Pstorage handle for the swap area (bank 1). Bank used when updating an application or bootloader without SoftDevice. */
static pstorage_handle_t m_storage_handle_app; /**< Pstorage handle for the application area (bank 0). Bank used when updating a SoftDevice w/wo bootloader. Handle also used when swapping received application from bank 1 to bank 0. */
static pstorage_handle_t * mp_storage_handle_active; /**< Pointer to the pstorage handle for the active bank for receiving of data packets. */
static dfu_callback_t m_data_pkt_cb; /**< Callback from DFU Bank module for notification of asynchronous operation such as flash prepare. */
static dfu_bank_func_t m_functions; /**< Structure holding operations for the selected update process. */
/**@brief Function for handling callbacks from pstorage module.
*
* @details Handles pstorage results for clear and storage operation. For detailed description of
* the parameters provided with the callback, please refer to \ref pstorage_ntf_cb_t.
*/
static void pstorage_callback_handler(pstorage_handle_t * p_handle,
uint8_t op_code,
uint32_t result,
uint8_t * p_data,
uint32_t data_len)
{
switch (op_code)
{
case PSTORAGE_STORE_OP_CODE:
if ((m_dfu_state == DFU_STATE_RX_DATA_PKT) && (m_data_pkt_cb != NULL))
{
m_data_pkt_cb(DATA_PACKET, result, p_data);
}
break;
case PSTORAGE_CLEAR_OP_CODE:
if (m_dfu_state == DFU_STATE_PREPARING)
{
m_functions.cleared();
m_dfu_state = DFU_STATE_RDY;
if (m_data_pkt_cb != NULL)
{
m_data_pkt_cb(START_PACKET, result, p_data);
}
}
break;
default:
break;
}
APP_ERROR_CHECK(result);
}
/**@brief Function for handling the DFU timeout.
*
* @param[in] p_context The timeout context.
*/
static void dfu_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
dfu_update_status_t update_status;
m_dfu_timed_out = true;
update_status.status_code = DFU_TIMEOUT;
bootloader_dfu_update_process(update_status);
}
/**@brief Function for restarting the DFU Timer.
*
* @details This function will stop and restart the DFU timer. This function will be called by the
* functions handling any DFU packet received from the peer that is transferring a firmware
* image.
*/
static uint32_t dfu_timer_restart(void)
{
if (m_dfu_timed_out)
{
// The DFU timer had already timed out.
return NRF_ERROR_INVALID_STATE;
}
uint32_t err_code = app_timer_stop(m_dfu_timer_id);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
return err_code;
}
/**@brief Function for preparing of flash before receiving SoftDevice image.
*
* @details This function will erase current application area to ensure sufficient amount of
* storage for the SoftDevice image. Upon erase complete a callback will be done.
* See \ref dfu_bank_prepare_t for further details.
*/
static void dfu_prepare_func_app_erase(uint32_t image_size)
{
uint32_t err_code;
mp_storage_handle_active = &m_storage_handle_app;
// Doing a SoftDevice update thus current application must be cleared to ensure enough space
// for new SoftDevice.
m_dfu_state = DFU_STATE_PREPARING;
err_code = pstorage_clear(&m_storage_handle_app, m_image_size);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for preparing swap before receiving application or bootloader image.
*
* @details This function will erase current swap area to ensure flash is ready for storage of the
* Application or Bootloader image. Upon erase complete a callback will be done.
* See \ref dfu_bank_prepare_t for further details.
*/
static void dfu_prepare_func_swap_erase(uint32_t image_size)
{
uint32_t err_code;
mp_storage_handle_active = &m_storage_handle_swap;
m_dfu_state = DFU_STATE_PREPARING;
// err_code = pstorage_clear(&m_storage_handle_swap, DFU_IMAGE_MAX_SIZE_BANKED);
err_code = pstorage_clear(&m_storage_handle_swap, image_size);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling behaviour when clear operation has completed.
*/
static void dfu_cleared_func_swap(void)
{
// Do nothing.
}
/**@brief Function for handling behaviour when clear operation has completed.
*/
static void dfu_cleared_func_app(void)
{
dfu_update_status_t update_status = {DFU_BANK_0_ERASED, };
bootloader_dfu_update_process(update_status);
}
/**@brief Function for calculating storage offset for receiving SoftDevice image.
*
* @details When a new SoftDevice is received it will be temporary stored in flash before moved to
* address 0x0. In order to succesfully validate transfer and relocation it is important
* that temporary image and final installed image does not ovwerlap hence an offset must
* be calculated in case new image is larger than currently installed SoftDevice.
*/
uint32_t offset_calculate(uint32_t sd_image_size)
{
uint32_t offset = 0;
if (m_start_packet.sd_image_size > DFU_BANK_0_REGION_START)
{
uint32_t page_mask = (CODE_PAGE_SIZE - 1);
uint32_t diff = m_start_packet.sd_image_size - DFU_BANK_0_REGION_START;
offset = diff & ~page_mask;
// Align offset to next page if image size is not page sized.
if ((diff & page_mask) > 0)
{
offset += CODE_PAGE_SIZE;
}
}
return offset;
}
/**@brief Function for activating received SoftDevice image.
*
* @note This function will not move the SoftDevice image.
* The bootloader settings will be marked as SoftDevice update complete and the swapping of
* current SoftDevice will occur after system reset.
*
* @return NRF_SUCCESS on success.
*/
static uint32_t dfu_activate_sd(void)
{
dfu_update_status_t update_status;
update_status.status_code = DFU_UPDATE_SD_COMPLETE;
update_status.app_crc = m_image_crc;
update_status.sd_image_start = DFU_BANK_0_REGION_START;
update_status.sd_size = m_start_packet.sd_image_size;
update_status.bl_size = m_start_packet.bl_image_size;
update_status.app_size = m_start_packet.app_image_size;
bootloader_dfu_update_process(update_status);
return NRF_SUCCESS;
}
/**@brief Function for activating received Application image.
*
* @details This function will move the received application image fram swap (bank 1) to
* application area (bank 0).
*
* @return NRF_SUCCESS on success. Error code otherwise.
*/
static uint32_t dfu_activate_app(void)
{
uint32_t err_code;
// Erase BANK 0.
err_code = pstorage_clear(&m_storage_handle_app, m_start_packet.app_image_size);
APP_ERROR_CHECK(err_code);
err_code = pstorage_store(&m_storage_handle_app,
(uint8_t *)m_storage_handle_swap.block_id,
m_start_packet.app_image_size,
0);
if (err_code == NRF_SUCCESS)
{
dfu_update_status_t update_status;
memset(&update_status, 0, sizeof(dfu_update_status_t ));
update_status.status_code = DFU_UPDATE_APP_COMPLETE;
update_status.app_crc = m_image_crc;
update_status.app_size = m_start_packet.app_image_size;
bootloader_dfu_update_process(update_status);
}
return err_code;
}
/**@brief Function for activating received Bootloader image.
*
* @note This function will not move the bootloader image.
* The bootloader settings will be marked as Bootloader update complete and the swapping of
* current bootloader will occur after system reset.
*
* @return NRF_SUCCESS on success.
*/
static uint32_t dfu_activate_bl(void)
{
dfu_update_status_t update_status;
update_status.status_code = DFU_UPDATE_BOOT_COMPLETE;
update_status.app_crc = m_image_crc;
update_status.sd_size = m_start_packet.sd_image_size;
update_status.bl_size = m_start_packet.bl_image_size;
update_status.app_size = m_start_packet.app_image_size;
bootloader_dfu_update_process(update_status);
return NRF_SUCCESS;
}
uint32_t dfu_init(void)
{
uint32_t err_code;
pstorage_module_param_t storage_module_param = {.cb = pstorage_callback_handler};
m_init_packet_length = 0;
m_image_crc = 0;
err_code = pstorage_register(&storage_module_param, &m_storage_handle_app);
if (err_code != NRF_SUCCESS)
{
m_dfu_state = DFU_STATE_INIT_ERROR;
return err_code;
}
m_storage_handle_app.block_id = DFU_BANK_0_REGION_START;
m_storage_handle_swap = m_storage_handle_app;
m_storage_handle_swap.block_id = DFU_BANK_1_REGION_START;
// Create the timer to monitor the activity by the peer doing the firmware update.
err_code = app_timer_create(&m_dfu_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
dfu_timeout_handler);
APP_ERROR_CHECK(err_code);
// Start the DFU timer.
err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
m_data_received = 0;
m_dfu_state = DFU_STATE_IDLE;
return NRF_SUCCESS;
}
void dfu_register_callback(dfu_callback_t callback_handler)
{
m_data_pkt_cb = callback_handler;
}
uint32_t dfu_start_pkt_handle(dfu_update_packet_t * p_packet)
{
uint32_t err_code;
m_start_packet = *(p_packet->params.start_packet);
// Check that the requested update procedure is supported.
// Currently the following combinations are allowed:
// - Application
// - SoftDevice
// - Bootloader
// - SoftDevice with Bootloader
if (IS_UPDATING_APP(m_start_packet) &&
(IS_UPDATING_SD(m_start_packet) || IS_UPDATING_BL(m_start_packet)))
{
// App update is only supported independently.
return NRF_ERROR_NOT_SUPPORTED;
}
if (!(IS_WORD_SIZED(m_start_packet.sd_image_size) &&
IS_WORD_SIZED(m_start_packet.bl_image_size) &&
IS_WORD_SIZED(m_start_packet.app_image_size)))
{
// Image_sizes are not a multiple of 4 (word size).
return NRF_ERROR_NOT_SUPPORTED;
}
m_image_size = m_start_packet.sd_image_size + m_start_packet.bl_image_size +
m_start_packet.app_image_size;
if (m_start_packet.bl_image_size > DFU_BL_IMAGE_MAX_SIZE)
{
return NRF_ERROR_DATA_SIZE;
}
if (IS_UPDATING_SD(m_start_packet))
{
if (m_image_size > (DFU_IMAGE_MAX_SIZE_FULL))
{
return NRF_ERROR_DATA_SIZE;
}
m_functions.prepare = dfu_prepare_func_app_erase;
m_functions.cleared = dfu_cleared_func_app;
m_functions.activate = dfu_activate_sd;
}
else
{
if (m_image_size > DFU_IMAGE_MAX_SIZE_BANKED)
{
return NRF_ERROR_DATA_SIZE;
}
m_functions.prepare = dfu_prepare_func_swap_erase;
m_functions.cleared = dfu_cleared_func_swap;
if (IS_UPDATING_BL(m_start_packet))
{
m_functions.activate = dfu_activate_bl;
}
else
{
m_functions.activate = dfu_activate_app;
}
}
switch (m_dfu_state)
{
case DFU_STATE_IDLE:
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
VERIFY_SUCCESS(err_code);
m_functions.prepare(m_image_size);
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_data_pkt_handle(dfu_update_packet_t * p_packet)
{
uint32_t data_length;
uint32_t err_code;
uint32_t * p_data;
VERIFY_PARAM_NOT_NULL(p_packet);
// Check pointer alignment.
if (!is_word_aligned(p_packet->params.data_packet.p_data_packet))
{
// The p_data_packet is not word aligned address.
return NRF_ERROR_INVALID_ADDR;
}
switch (m_dfu_state)
{
case DFU_STATE_RDY:
case DFU_STATE_RX_INIT_PKT:
return NRF_ERROR_INVALID_STATE;
case DFU_STATE_RX_DATA_PKT:
data_length = p_packet->params.data_packet.packet_length * sizeof(uint32_t);
if ((m_data_received + data_length) > m_image_size)
{
// The caller is trying to write more bytes into the flash than the size provided to
// the dfu_image_size_set function. This is treated as a serious error condition and
// an unrecoverable one. Hence point the variable mp_app_write_address to the top of
// the flash area. This will ensure that all future application data packet writes
// will be blocked because of the above check.
m_data_received = 0xFFFFFFFF;
return NRF_ERROR_DATA_SIZE;
}
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
VERIFY_SUCCESS(err_code);
p_data = (uint32_t *)p_packet->params.data_packet.p_data_packet;
err_code = pstorage_store(mp_storage_handle_active,
(uint8_t *)p_data,
data_length,
m_data_received);
VERIFY_SUCCESS(err_code);
m_data_received += data_length;
if (m_data_received != m_image_size)
{
// The entire image is not received yet. More data is expected.
err_code = NRF_ERROR_INVALID_LENGTH;
}
else
{
// The entire image has been received. Return NRF_SUCCESS.
err_code = NRF_SUCCESS;
}
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_init_pkt_complete(void)
{
uint32_t err_code = NRF_ERROR_INVALID_STATE;
// DFU initialization has been done and a start packet has been received.
if (IMAGE_WRITE_IN_PROGRESS())
{
// Image write is already in progress. Cannot handle an init packet now.
return NRF_ERROR_INVALID_STATE;
}
if (m_dfu_state == DFU_STATE_RX_INIT_PKT)
{
err_code = dfu_init_prevalidate(m_init_packet, m_init_packet_length, m_start_packet.dfu_update_mode);
if (err_code == NRF_SUCCESS)
{
m_dfu_state = DFU_STATE_RX_DATA_PKT;
}
else
{
m_init_packet_length = 0;
}
}
return err_code;
}
uint32_t dfu_init_pkt_handle(dfu_update_packet_t * p_packet)
{
uint32_t err_code = NRF_SUCCESS;
uint32_t length;
switch (m_dfu_state)
{
case DFU_STATE_RDY:
m_dfu_state = DFU_STATE_RX_INIT_PKT;
// When receiving init packet in state ready just update and fall through this case.
case DFU_STATE_RX_INIT_PKT:
// DFU initialization has been done and a start packet has been received.
if (IMAGE_WRITE_IN_PROGRESS())
{
// Image write is already in progress. Cannot handle an init packet now.
return NRF_ERROR_INVALID_STATE;
}
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
VERIFY_SUCCESS(err_code);
length = p_packet->params.data_packet.packet_length * sizeof(uint32_t);
if ((m_init_packet_length + length) > sizeof(m_init_packet))
{
return NRF_ERROR_INVALID_LENGTH;
}
memcpy(&m_init_packet[m_init_packet_length],
&p_packet->params.data_packet.p_data_packet[0],
length);
m_init_packet_length += length;
break;
default:
// Either the start packet was not received or dfu_init function was not called before.
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_image_validate()
{
uint32_t err_code;
switch (m_dfu_state)
{
case DFU_STATE_RX_DATA_PKT:
// Check if the application image write has finished.
if (m_data_received != m_image_size)
{
// Image not yet fully transfered by the peer or the peer has attempted to write
// too much data. Hence the validation should fail.
err_code = NRF_ERROR_INVALID_STATE;
}
else
{
m_dfu_state = DFU_STATE_VALIDATE;
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
if (err_code == NRF_SUCCESS)
{
err_code = dfu_init_postvalidate((uint8_t *)mp_storage_handle_active->block_id,
m_image_size);
VERIFY_SUCCESS(err_code);
m_dfu_state = DFU_STATE_WAIT_4_ACTIVATE;
}
}
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_image_activate()
{
uint32_t err_code;
switch (m_dfu_state)
{
case DFU_STATE_WAIT_4_ACTIVATE:
// Stop the DFU Timer because the peer activity need not be monitored any longer.
err_code = app_timer_stop(m_dfu_timer_id);
APP_ERROR_CHECK(err_code);
err_code = m_functions.activate();
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
void dfu_reset(void)
{
dfu_update_status_t update_status;
update_status.status_code = DFU_RESET;
bootloader_dfu_update_process(update_status);
}
static uint32_t dfu_compare_block(uint32_t * ptr1, uint32_t * ptr2, uint32_t len)
{
sd_mbr_command_t sd_mbr_cmd;
sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
sd_mbr_cmd.params.compare.ptr1 = ptr1;
sd_mbr_cmd.params.compare.ptr2 = ptr2;
sd_mbr_cmd.params.compare.len = len / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
static uint32_t dfu_copy_sd(uint32_t * src, uint32_t * dst, uint32_t len)
{
sd_mbr_command_t sd_mbr_cmd;
sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_SD;
sd_mbr_cmd.params.copy_sd.src = src;
sd_mbr_cmd.params.copy_sd.dst = dst;
sd_mbr_cmd.params.copy_sd.len = len / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
static uint32_t dfu_sd_img_block_swap(uint32_t * src,
uint32_t * dst,
uint32_t len,
uint32_t block_size)
{
// It is neccesarry to swap the new SoftDevice in 3 rounds to ensure correct copy of data
// and verifucation of data in case power reset occurs during write to flash.
// To ensure the robustness of swapping the images are compared backwards till start of
// image swap. If the back is identical everything is swapped.
uint32_t err_code = dfu_compare_block(src, dst, len);
if (err_code == NRF_SUCCESS)
{
return err_code;
}
if ((uint32_t)dst > SOFTDEVICE_REGION_START)
{
err_code = dfu_sd_img_block_swap((uint32_t *)((uint32_t)src - block_size),
(uint32_t *)((uint32_t)dst - block_size),
block_size,
block_size);
VERIFY_SUCCESS(err_code);
}
err_code = dfu_copy_sd(src, dst, len);
VERIFY_SUCCESS(err_code);
return dfu_compare_block(src, dst, len);
}
uint32_t dfu_sd_image_swap(void)
{
bootloader_settings_t boot_settings;
bootloader_settings_get(&boot_settings);
if (boot_settings.sd_image_size == 0)
{
return NRF_SUCCESS;
}
if ((SOFTDEVICE_REGION_START + boot_settings.sd_image_size) > boot_settings.sd_image_start)
{
uint32_t err_code;
uint32_t sd_start = SOFTDEVICE_REGION_START;
uint32_t block_size = (boot_settings.sd_image_start - sd_start) / 2;
/* ##### FIX START ##### */
block_size &= ~(uint32_t)(CODE_PAGE_SIZE - 1);
/* ##### FIX END ##### */
uint32_t image_end = boot_settings.sd_image_start + boot_settings.sd_image_size;
uint32_t img_block_start = boot_settings.sd_image_start + 2 * block_size;
uint32_t sd_block_start = sd_start + 2 * block_size;
if (SD_SIZE_GET(MBR_SIZE) < boot_settings.sd_image_size)
{
// This will clear a page thus ensuring the old image is invalidated before swapping.
err_code = dfu_copy_sd((uint32_t *)(sd_start + block_size),
(uint32_t *)(sd_start + block_size),
sizeof(uint32_t));
VERIFY_SUCCESS(err_code);
err_code = dfu_copy_sd((uint32_t *)sd_start, (uint32_t *)sd_start, sizeof(uint32_t));
VERIFY_SUCCESS(err_code);
}
return dfu_sd_img_block_swap((uint32_t *)img_block_start,
(uint32_t *)sd_block_start,
image_end - img_block_start,
block_size);
}
else
{
if (boot_settings.sd_image_size != 0)
{
return dfu_copy_sd((uint32_t *)boot_settings.sd_image_start,
(uint32_t *)SOFTDEVICE_REGION_START,
boot_settings.sd_image_size);
}
}
return NRF_SUCCESS;
}
uint32_t dfu_bl_image_swap(void)
{
bootloader_settings_t bootloader_settings;
sd_mbr_command_t sd_mbr_cmd;
bootloader_settings_get(&bootloader_settings);
if (bootloader_settings.bl_image_size != 0)
{
uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ?
DFU_BANK_1_REGION_START :
bootloader_settings.sd_image_start +
bootloader_settings.sd_image_size;
sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_BL;
sd_mbr_cmd.params.copy_bl.bl_src = (uint32_t *)(bl_image_start);
sd_mbr_cmd.params.copy_bl.bl_len = bootloader_settings.bl_image_size / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
return NRF_SUCCESS;
}
uint32_t dfu_bl_image_validate(void)
{
bootloader_settings_t bootloader_settings;
sd_mbr_command_t sd_mbr_cmd;
bootloader_settings_get(&bootloader_settings);
if (bootloader_settings.bl_image_size != 0)
{
uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ?
DFU_BANK_1_REGION_START :
bootloader_settings.sd_image_start +
bootloader_settings.sd_image_size;
sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)BOOTLOADER_REGION_START;
sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)(bl_image_start);
sd_mbr_cmd.params.compare.len = bootloader_settings.bl_image_size / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
return NRF_SUCCESS;
}
uint32_t dfu_sd_image_validate(void)
{
bootloader_settings_t bootloader_settings;
sd_mbr_command_t sd_mbr_cmd;
bootloader_settings_get(&bootloader_settings);
if (bootloader_settings.sd_image_size == 0)
{
return NRF_SUCCESS;
}
if ((SOFTDEVICE_REGION_START + bootloader_settings.sd_image_size) > bootloader_settings.sd_image_start)
{
uint32_t sd_start = SOFTDEVICE_REGION_START;
uint32_t block_size = (bootloader_settings.sd_image_start - sd_start) / 2;
uint32_t image_end = bootloader_settings.sd_image_start +
bootloader_settings.sd_image_size;
uint32_t img_block_start = bootloader_settings.sd_image_start + 2 * block_size;
uint32_t sd_block_start = sd_start + 2 * block_size;
if (SD_SIZE_GET(MBR_SIZE) < bootloader_settings.sd_image_size)
{
return NRF_ERROR_NULL;
}
return dfu_sd_img_block_swap((uint32_t *)img_block_start,
(uint32_t *)sd_block_start,
image_end - img_block_start,
block_size);
}
sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)SOFTDEVICE_REGION_START;
sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)bootloader_settings.sd_image_start;
sd_mbr_cmd.params.compare.len = bootloader_settings.sd_image_size / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}

View File

@ -0,0 +1,134 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_dfu_init Init packet handling in DFU
* @{
*
* @brief Device Firmware Update module type and function declaration for init packet handling.
*
* @details This header contains basic functionality for performing safety checks on software
* updates for \nRFXX based devices. It provides a skeleton for pre-checking an init packet
* to ensure the following image is compatible with this device. A safety check should
* always be performed to prevent accidental flashing of unsupported applications or a
* wrong combination of application and SoftDevice.
* The device information contains information such as:
* - Device type (2 bytes), for example Heart Rate. The device type is a number defined by
* the customer. It can be located in UICR or FICR.
* - Device revision (2 bytes), for example major revision 1, minor revision 0. The device
* revision is a number defined by the customer. It can be located in UICR or FICR.
* - List of SoftDevices supported by this application, for example
* 0x0049 = S110v6_0_0
* 0xFFFE = S110 development (any SoftDevice accepted),
* - CRC or hash of firmware image
*
* @note This module does not support security features such as image signing, but the corresponding
* implementation allows for such extensions.
* If the init packet is signed by a trusted source, it must be decrypted before it can be
* processed.
*/
#ifndef DFU_INIT_H__
#define DFU_INIT_H__
#include <stdint.h>
#include "nrf.h"
/**@brief Structure contained in an init packet. Contains information on device type, revision, and
* supported SoftDevices.
*/
typedef struct
{
uint16_t device_type; /**< Device type (2 bytes), for example Heart Rate. This number must be defined by the customer before production. It can be located in UICR or FICR. */
uint16_t device_rev; /**< Device revision (2 bytes), for example major revision 1, minor revision 0. This number must be defined by the customer before production. It can be located in UICR or FICR. */
uint32_t app_version; /**< Application version for the image software. This field allows for additional checking, for example ensuring that a downgrade is not allowed. */
uint16_t softdevice_len; /**< Number of different SoftDevice revisions compatible with this application. The list of SoftDevice firmware IDs is defined in @ref softdevice. */
uint16_t softdevice[1]; /**< Variable length array of SoftDevices compatible with this application. The length of the array is specified in the length field. SoftDevice firmware id 0xFFFE indicates any SoftDevice. */
} dfu_init_packet_t;
/**@brief Structure holding basic device information settings.
*/
typedef struct
{
uint16_t device_type; /**< Device type (2 bytes), for example Heart Rate. This number must be defined by the customer before production. It can be located in UICR or FICR. */
uint16_t device_rev; /**< Device revision (2 bytes), for example major revision 1, minor revision 0. This number must be defined by the customer before production. It can be located in UICR or FICR. */
} dfu_device_info_t;
/** The device info offset can be modified to place the device info settings at a different location.
* If the customer reserved UICR location is used for other application specific data, the offset
* must be updated to avoid collision with that data.
*/
/** [DFU UICR DEV offset] */
#define UICR_CUSTOMER_DEVICE_INFO_OFFSET 0x0 /**< Device info offset inside the customer UICR reserved area. Customers may change this value to place the device information in a user-preferred location. */
/** [DFU UICR DEV offset] */
#define UICR_CUSTOMER_RESERVED_OFFSET 0x80 /**< Customer reserved area in the UICR. The area from UICR + 0x80 is reserved for customer usage. */
#define DFU_DEVICE_INFO_BASE (NRF_UICR_BASE + \
UICR_CUSTOMER_RESERVED_OFFSET + \
UICR_CUSTOMER_DEVICE_INFO_OFFSET) /**< The device information base address inside of UICR. */
#define DFU_DEVICE_INFO ((dfu_device_info_t *)DFU_DEVICE_INFO_BASE) /**< The memory mapped structure for device information data. */
#define DFU_DEVICE_TYPE_EMPTY ((uint16_t)0xFFFF) /**< Mask indicating no device type is present in UICR. 0xFFFF is default flash pattern when not written with data. */
#define DFU_DEVICE_REVISION_EMPTY ((uint16_t)0xFFFF) /**< Mask indicating no device revision is present in UICR. 0xFFFF is default flash pattern when not written with data. */
#define DFU_SOFTDEVICE_ANY ((uint16_t)0xFFFE) /**< Mask indicating that any SoftDevice is allowed for updating this application. Allows for easy development. Not to be used in production images. */
/**@brief DFU prevalidate call for pre-checking the received init packet.
*
* @details Pre-validation will safety check the firmware image to be transfered in second stage.
* The function currently checks the device type, device revision, application firmware
* version, and supported SoftDevices. More checks should be added according to
* customer-specific requirements.
*
* @param[in] p_init_data Pointer to the init packet. If the init packet is encrypted or signed,
* it must first be decrypted before being checked.
* @param[in] init_data_len Length of the init data.
*
* @retval NRF_SUCCESS If the pre-validation succeeded, that means the image is
* supported by the device and it is considered to come from a
* trusted source (signing).
* @retval NRF_ERROR_INVALID_DATA If the pre-validation failed, that means the image is not
* supported by the device or comes from an un-trusted source
* (signing).
* @retval NRF_ERROR_INVALID_LENGTH If the size of the init packet is not within the limits of
* the init packet handler.
*/
uint32_t dfu_init_prevalidate(uint8_t * p_init_data, uint32_t init_data_len, uint8_t image_type);
/**@brief DFU postvalidate call for post-checking the received image using the init packet.
*
* @details Post-validation can verify the integrity check the firmware image received before
* activating the image.
* Checks performed can be:
* - A simple CRC as shown in the corresponding implementation of this API in the file
* dfu_init_template.c
* - A hash for better verification of the image.
* - A signature to ensure the image originates from a trusted source.
* Checks are intended to be expanded for customer-specific requirements.
*
* @param[in] p_image Pointer to the received image. The init data provided in the call
* \ref dfu_init_prevalidate will be used for validating the image.
* @param[in] image_len Length of the image data.
*
* @retval NRF_SUCCESS If the post-validation succeeded, that meant the integrity of the
* image has been verified and the image originates from a trusted
* source (signing).
* @retval NRF_ERROR_INVALID_DATA If the post-validation failed, that meant the post check of the
* image failed such as the CRC is not matching the image transfered
* or the verification of the image fails (signing).
*/
uint32_t dfu_init_postvalidate(uint8_t * p_image, uint32_t image_len);
#endif // DFU_INIT_H__
/**@} */

View File

@ -0,0 +1,172 @@
/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_dfu_init_template Template file with an DFU init packet handling example.
* @{
*
* @ingroup nrf_dfu
*
* @brief This file contains a template on how to implement DFU init packet handling.
*
* @details The template shows how device type and revision can be used for a safety check of the
* received image. It shows how validation can be performed in two stages:
* - Stage 1: Pre-check of firmware image before transfer to ensure the firmware matches:
* - Device Type.
* - Device Revision.
* Installed SoftDevice.
* This template can be extended with additional checks according to needs.
* For example, such a check could be the origin of the image (trusted source)
* based on a signature scheme.
* - Stage 2: Post-check of the image after image transfer but before installing firmware.
* For example, such a check could be an integrity check in form of hashing or
* verification of a signature.
* In this template, a simple CRC check is carried out.
* The CRC check can be replaced with other mechanisms, like signing.
*
* @note This module does not support security features such as image signing, but the
* implementation allows for such extension.
* If the init packet is signed by a trusted source, it must be decrypted before it can be
* processed.
*/
#include "dfu_init.h"
#include <stdint.h>
#include <string.h>
#include <dfu_types.h>
#include "nrf_error.h"
#include "crc16.h"
// ADAFRUIT
// All firmware init data must has Device Type ADAFRUIT_DEVICE_TYPE
// SD + Bootloader upgrade must have ADAFRUIT_SD_UNLOCK_CODE in Device Revision
#define ADAFRUIT_DEVICE_TYPE 0x0052 // for nrf52
#define ADAFRUIT_SD_UNLOCK_CODE 0xADAF
#define DFU_INIT_PACKET_EXT_LENGTH_MIN 2 //< Minimum length of the extended init packet. The extended init packet may contain a CRC, a HASH, or other data. This value must be changed according to the requirements of the system. The template uses a minimum value of two in order to hold a CRC. */
#define DFU_INIT_PACKET_EXT_LENGTH_MAX 10 //< Maximum length of the extended init packet. The extended init packet may contain a CRC, a HASH, or other data. This value must be changed according to the requirements of the system. The template uses a maximum value of 10 in order to hold a CRC and any padded data on transport layer without overflow. */
static uint8_t m_extended_packet[DFU_INIT_PACKET_EXT_LENGTH_MAX]; //< Data array for storage of the extended data received. The extended data follows the normal init data of type \ref dfu_init_packet_t. Extended data can be used for a CRC, hash, signature, or other data. */
static uint8_t m_extended_packet_length; //< Length of the extended data received with init packet. */
uint32_t dfu_init_prevalidate(uint8_t * p_init_data, uint32_t init_data_len, uint8_t image_type)
{
uint32_t i = 0;
// In order to support signing or encryption then any init packet decryption function / library
// should be called from here or implemented at this location.
// Length check to ensure valid data are parsed.
if (init_data_len < sizeof(dfu_init_packet_t))
{
return NRF_ERROR_INVALID_LENGTH;
}
// Current template uses clear text data so they can be casted for pre-check.
dfu_init_packet_t * p_init_packet = (dfu_init_packet_t *)p_init_data;
m_extended_packet_length = ((uint32_t)p_init_data + init_data_len) -
(uint32_t)&p_init_packet->softdevice[p_init_packet->softdevice_len];
if (m_extended_packet_length < DFU_INIT_PACKET_EXT_LENGTH_MIN)
{
return NRF_ERROR_INVALID_LENGTH;
}
if (((uint32_t)p_init_data + init_data_len) <
(uint32_t)&p_init_packet->softdevice[p_init_packet->softdevice_len])
{
return NRF_ERROR_INVALID_LENGTH;
}
memcpy(m_extended_packet,
&p_init_packet->softdevice[p_init_packet->softdevice_len],
m_extended_packet_length);
/** [DFU init application version] */
// To support application versioning, this check should be updated.
// This template allows for any application to be installed. However,
// customers can place a revision number at the bottom of the application
// to be verified by the bootloader. This can be done at a location
// relative to the application, for example the application start
// address + 0x0100.
/** [DFU init application version] */
// First check to verify the image to be transfered matches the device type.
// If no Device type is present in DFU_DEVICE_INFO then any image will be accepted.
// if ((DFU_DEVICE_INFO->device_type != DFU_DEVICE_TYPE_EMPTY) &&
// (p_init_packet->device_type != DFU_DEVICE_INFO->device_type))
// {
// return NRF_ERROR_INVALID_DATA;
// }
// Second check to verify the image to be transfered matches the device revision.
// If no Device revision is present in DFU_DEVICE_INFO then any image will be accepted.
// if ((DFU_DEVICE_INFO->device_rev != DFU_DEVICE_REVISION_EMPTY) &&
// (p_init_packet->device_rev != DFU_DEVICE_INFO->device_rev))
if ( p_init_packet->device_type != ADAFRUIT_DEVICE_TYPE )
{
return NRF_ERROR_FORBIDDEN;
}
// Adafruit unlock code must match to upgrade SoftDevice and/or Bootloader
if ( image_type & (DFU_UPDATE_SD | DFU_UPDATE_BL) )
{
if (p_init_packet->device_rev != ADAFRUIT_SD_UNLOCK_CODE)
{
return NRF_ERROR_FORBIDDEN;
}
}
// Third check: Check the array of supported SoftDevices by this application.
// If the installed SoftDevice does not match any SoftDevice in the list then an
// error is returned.
while (i < p_init_packet->softdevice_len)
{
if (p_init_packet->softdevice[i] == DFU_SOFTDEVICE_ANY ||
p_init_packet->softdevice[i++] == SD_FWID_GET(MBR_SIZE))
{
return NRF_SUCCESS;
}
}
// No matching SoftDevice found - Return NRF_ERROR_INVALID_DATA.
return NRF_ERROR_INVALID_DATA;
}
uint32_t dfu_init_postvalidate(uint8_t * p_image, uint32_t image_len)
{
uint16_t image_crc;
uint16_t received_crc;
// In order to support hashing (and signing) then the (decrypted) hash should be fetched and
// the corresponding hash should be calculated over the image at this location.
// If hashing (or signing) is added to the system then the CRC validation should be removed.
// calculate CRC from active block.
image_crc = crc16_compute(p_image, image_len, NULL);
// Decode the received CRC from extended data.
received_crc = uint16_decode((uint8_t *)&m_extended_packet[0]);
// Compare the received and calculated CRC.
if (image_crc != received_crc)
{
return NRF_ERROR_INVALID_DATA;
}
return NRF_SUCCESS;
}

View File

@ -0,0 +1,788 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include <stddef.h>
#include "dfu.h"
#include <dfu_types.h>
#include "dfu_bank_internal.h"
#include "nrf.h"
#include "nrf_sdm.h"
#include "app_error.h"
#include "app_timer.h"
#include "bootloader.h"
#include "bootloader_types.h"
#include "pstorage.h"
#include "nrf_mbr.h"
#include "dfu_init.h"
#include "sdk_common.h"
static dfu_state_t m_dfu_state; /**< Current DFU state. */
static uint32_t m_image_size; /**< Size of the image that will be transmitted. */
static dfu_start_packet_t m_start_packet; /**< Start packet received for this update procedure. Contains update mode and image sizes information to be used for image transfer. */
static uint8_t m_init_packet[64]; /**< Init packet, can hold CRC, Hash, Signed Hash and similar, for image validation, integrety check and authorization checking. */
static uint8_t m_init_packet_length; /**< Length of init packet received. */
static uint16_t m_image_crc; /**< Calculated CRC of the image received. */
APP_TIMER_DEF(m_dfu_timer_id); /**< Application timer id. */
static bool m_dfu_timed_out = false; /**< Boolean flag value for tracking DFU timer timeout state. */
static pstorage_handle_t m_storage_handle_app; /**< Pstorage handle for the application area (bank 0). Bank used when updating a SoftDevice w/wo bootloader. Handle also used when swapping received application from bank 1 to bank 0. */
static pstorage_handle_t * mp_storage_handle_active; /**< Pointer to the pstorage handle for the active bank for receiving of data packets. */
static dfu_callback_t m_data_pkt_cb; /**< Callback from DFU Bank module for notification of asynchronous operation such as flash prepare. */
static dfu_bank_func_t m_functions; /**< Structure holding operations for the selected update process. */
/**@brief Function for handling callbacks from pstorage module.
*
* @details Handles pstorage results for clear and storage operation. For detailed description of
* the parameters provided with the callback, please refer to \ref pstorage_ntf_cb_t.
*/
static void pstorage_callback_handler(pstorage_handle_t * p_handle,
uint8_t op_code,
uint32_t result,
uint8_t * p_data,
uint32_t data_len)
{
switch (op_code)
{
case PSTORAGE_STORE_OP_CODE:
if ((m_dfu_state == DFU_STATE_RX_DATA_PKT) && (m_data_pkt_cb != NULL))
{
m_data_pkt_cb(DATA_PACKET, result, p_data);
}
break;
case PSTORAGE_CLEAR_OP_CODE:
if (m_dfu_state == DFU_STATE_PREPARING)
{
m_functions.cleared();
m_dfu_state = DFU_STATE_RDY;
if (m_data_pkt_cb != NULL)
{
m_data_pkt_cb(START_PACKET, result, p_data);
}
}
break;
default:
break;
}
APP_ERROR_CHECK(result);
}
/**@brief Function for handling the DFU timeout.
*
* @param[in] p_context The timeout context.
*/
static void dfu_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
dfu_update_status_t update_status;
m_dfu_timed_out = true;
update_status.status_code = DFU_TIMEOUT;
bootloader_dfu_update_process(update_status);
}
/**@brief Function for restarting the DFU Timer.
*
* @details This function will stop and restart the DFU timer. This function will be called by the
* functions handling any DFU packet received from the peer that is transferring a firmware
* image.
*/
static uint32_t dfu_timer_restart(void)
{
if (m_dfu_timed_out)
{
// The DFU timer had already timed out.
return NRF_ERROR_INVALID_STATE;
}
uint32_t err_code = app_timer_stop(m_dfu_timer_id);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
return err_code;
}
/**@brief Function for preparing of flash before receiving SoftDevice image.
*
* @details This function will erase current application area to ensure sufficient amount of
* storage for the SoftDevice image. Upon erase complete a callback will be done.
* See \ref dfu_bank_prepare_t for further details.
*/
static void dfu_prepare_func_app_erase(uint32_t image_size)
{
uint32_t err_code;
mp_storage_handle_active = &m_storage_handle_app;
// Doing a SoftDevice update thus current application must be cleared to ensure enough space
// for new SoftDevice.
m_dfu_state = DFU_STATE_PREPARING;
err_code = pstorage_clear(&m_storage_handle_app, m_image_size);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling behaviour when clear operation has completed.
*/
static void dfu_cleared_func_app(void)
{
dfu_update_status_t update_status = {DFU_BANK_0_ERASED, };
bootloader_dfu_update_process(update_status);
}
/**@brief Function for calculating storage offset for receiving SoftDevice image.
*
* @details When a new SoftDevice is received it will be temporary stored in flash before moved to
* address 0x0. In order to succesfully validate transfer and relocation it is important
* that temporary image and final installed image does not ovwerlap hence an offset must
* be calculated in case new image is larger than currently installed SoftDevice.
*/
uint32_t offset_calculate(uint32_t sd_image_size)
{
uint32_t offset = 0;
if (m_start_packet.sd_image_size > DFU_BANK_0_REGION_START)
{
uint32_t page_mask = (CODE_PAGE_SIZE - 1);
uint32_t diff = m_start_packet.sd_image_size - DFU_BANK_0_REGION_START;
offset = diff & ~page_mask;
// Align offset to next page if image size is not page sized.
if ((diff & page_mask) > 0)
{
offset += CODE_PAGE_SIZE;
}
}
return offset;
}
/**@brief Function for activating received SoftDevice image.
*
* @note This function will not move the SoftDevice image.
* The bootloader settings will be marked as SoftDevice update complete and the swapping of
* current SoftDevice will occur after system reset.
*
* @return NRF_SUCCESS on success.
*/
static uint32_t dfu_activate_sd(void)
{
dfu_update_status_t update_status;
update_status.status_code = DFU_UPDATE_SD_COMPLETE;
update_status.app_crc = m_image_crc;
update_status.sd_image_start = DFU_BANK_0_REGION_START;
update_status.sd_size = m_start_packet.sd_image_size;
update_status.bl_size = m_start_packet.bl_image_size;
update_status.app_size = m_start_packet.app_image_size;
bootloader_dfu_update_process(update_status);
return NRF_SUCCESS;
}
/**@brief Function for activating received Application image.
*
* @details This function will move the received application image fram swap (bank 1) to
* application area (bank 0).
*
* @return NRF_SUCCESS on success. Error code otherwise.
*/
static uint32_t dfu_activate_app(void)
{
uint32_t err_code = NRF_SUCCESS;
dfu_update_status_t update_status;
memset(&update_status, 0, sizeof(dfu_update_status_t ));
update_status.status_code = DFU_UPDATE_APP_COMPLETE;
update_status.app_crc = m_image_crc;
update_status.app_size = m_start_packet.app_image_size;
bootloader_dfu_update_process(update_status);
return err_code;
}
/**@brief Function for activating received Bootloader image.
*
* @note This function will not move the bootloader image.
* The bootloader settings will be marked as Bootloader update complete and the swapping of
* current bootloader will occur after system reset.
*
* @return NRF_SUCCESS on success.
*/
static uint32_t dfu_activate_bl(void)
{
dfu_update_status_t update_status;
update_status.status_code = DFU_UPDATE_BOOT_COMPLETE;
update_status.app_crc = m_image_crc;
update_status.sd_size = m_start_packet.sd_image_size;
update_status.bl_size = m_start_packet.bl_image_size;
update_status.app_size = m_start_packet.app_image_size;
bootloader_dfu_update_process(update_status);
return NRF_SUCCESS;
}
uint32_t dfu_init(void)
{
uint32_t err_code;
pstorage_module_param_t storage_module_param = {.cb = pstorage_callback_handler};
m_init_packet_length = 0;
m_image_crc = 0;
err_code = pstorage_register(&storage_module_param, &m_storage_handle_app);
if (err_code != NRF_SUCCESS)
{
m_dfu_state = DFU_STATE_INIT_ERROR;
return err_code;
}
m_storage_handle_app.block_id = DFU_BANK_0_REGION_START;
// Create the timer to monitor the activity by the peer doing the firmware update.
err_code = app_timer_create(&m_dfu_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
dfu_timeout_handler);
APP_ERROR_CHECK(err_code);
// Start the DFU timer.
err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
m_data_received = 0;
m_dfu_state = DFU_STATE_IDLE;
return NRF_SUCCESS;
}
void dfu_register_callback(dfu_callback_t callback_handler)
{
m_data_pkt_cb = callback_handler;
}
uint32_t dfu_start_pkt_handle(dfu_update_packet_t * p_packet)
{
uint32_t err_code;
m_start_packet = *(p_packet->params.start_packet);
// Check that the requested update procedure is supported.
// Currently the following combinations are allowed:
// - Application
// - SoftDevice
// - Bootloader
// - SoftDevice with Bootloader
if (IS_UPDATING_APP(m_start_packet) &&
(IS_UPDATING_SD(m_start_packet) || IS_UPDATING_BL(m_start_packet)))
{
// App update is only supported independently.
return NRF_ERROR_NOT_SUPPORTED;
}
if (!(IS_WORD_SIZED(m_start_packet.sd_image_size) &&
IS_WORD_SIZED(m_start_packet.bl_image_size) &&
IS_WORD_SIZED(m_start_packet.app_image_size)))
{
// Image_sizes are not a multiple of 4 (word size).
return NRF_ERROR_NOT_SUPPORTED;
}
m_image_size = m_start_packet.sd_image_size + m_start_packet.bl_image_size +
m_start_packet.app_image_size;
if (m_start_packet.bl_image_size > DFU_BL_IMAGE_MAX_SIZE)
{
return NRF_ERROR_DATA_SIZE;
}
if (m_image_size > (DFU_IMAGE_MAX_SIZE_FULL))
{
return NRF_ERROR_DATA_SIZE;
}
m_functions.prepare = dfu_prepare_func_app_erase;
m_functions.cleared = dfu_cleared_func_app;
if (IS_UPDATING_SD(m_start_packet))
{
m_functions.activate = dfu_activate_sd;
}
else if (IS_UPDATING_BL(m_start_packet))
{
m_functions.activate = dfu_activate_bl;
}
else
{
m_functions.activate = dfu_activate_app;
}
switch (m_dfu_state)
{
case DFU_STATE_IDLE:
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
VERIFY_SUCCESS(err_code);
m_functions.prepare(m_image_size);
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_data_pkt_handle(dfu_update_packet_t * p_packet)
{
uint32_t data_length;
uint32_t err_code;
uint32_t * p_data;
VERIFY_PARAM_NOT_NULL(p_packet);
// Check pointer alignment.
if (!is_word_aligned(p_packet->params.data_packet.p_data_packet))
{
// The p_data_packet is not word aligned address.
return NRF_ERROR_INVALID_ADDR;
}
switch (m_dfu_state)
{
case DFU_STATE_RDY:
case DFU_STATE_RX_INIT_PKT:
return NRF_ERROR_INVALID_STATE;
case DFU_STATE_RX_DATA_PKT:
data_length = p_packet->params.data_packet.packet_length * sizeof(uint32_t);
if ((m_data_received + data_length) > m_image_size)
{
// The caller is trying to write more bytes into the flash than the size provided to
// the dfu_image_size_set function. This is treated as a serious error condition and
// an unrecoverable one. Hence point the variable mp_app_write_address to the top of
// the flash area. This will ensure that all future application data packet writes
// will be blocked because of the above check.
m_data_received = 0xFFFFFFFF;
return NRF_ERROR_DATA_SIZE;
}
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
VERIFY_SUCCESS(err_code);
p_data = (uint32_t *)p_packet->params.data_packet.p_data_packet;
err_code = pstorage_store(mp_storage_handle_active,
(uint8_t *)p_data,
data_length,
m_data_received);
VERIFY_SUCCESS(err_code);
m_data_received += data_length;
if (m_data_received != m_image_size)
{
// The entire image is not received yet. More data is expected.
err_code = NRF_ERROR_INVALID_LENGTH;
}
else
{
// The entire image has been received. Return NRF_SUCCESS.
err_code = NRF_SUCCESS;
}
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_init_pkt_complete(void)
{
uint32_t err_code = NRF_ERROR_INVALID_STATE;
// DFU initialization has been done and a start packet has been received.
if (IMAGE_WRITE_IN_PROGRESS())
{
// Image write is already in progress. Cannot handle an init packet now.
return NRF_ERROR_INVALID_STATE;
}
if (m_dfu_state == DFU_STATE_RX_INIT_PKT)
{
err_code = dfu_init_prevalidate(m_init_packet, m_init_packet_length, m_start_packet.dfu_update_mode);
if (err_code == NRF_SUCCESS)
{
m_dfu_state = DFU_STATE_RX_DATA_PKT;
}
else
{
m_init_packet_length = 0;
}
}
return err_code;
}
uint32_t dfu_init_pkt_handle(dfu_update_packet_t * p_packet)
{
uint32_t err_code = NRF_SUCCESS;
uint32_t length;
switch (m_dfu_state)
{
case DFU_STATE_RDY:
m_dfu_state = DFU_STATE_RX_INIT_PKT;
// When receiving init packet in state ready just update and fall through this case.
case DFU_STATE_RX_INIT_PKT:
// DFU initialization has been done and a start packet has been received.
if (IMAGE_WRITE_IN_PROGRESS())
{
// Image write is already in progress. Cannot handle an init packet now.
return NRF_ERROR_INVALID_STATE;
}
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
VERIFY_SUCCESS(err_code);
length = p_packet->params.data_packet.packet_length * sizeof(uint32_t);
if ((m_init_packet_length + length) > sizeof(m_init_packet))
{
return NRF_ERROR_INVALID_LENGTH;
}
memcpy(&m_init_packet[m_init_packet_length],
&p_packet->params.data_packet.p_data_packet[0],
length);
m_init_packet_length += length;
break;
default:
// Either the start packet was not received or dfu_init function was not called before.
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_image_validate()
{
uint32_t err_code;
switch (m_dfu_state)
{
case DFU_STATE_RX_DATA_PKT:
// Check if the application image write has finished.
if (m_data_received != m_image_size)
{
// Image not yet fully transfered by the peer or the peer has attempted to write
// too much data. Hence the validation should fail.
err_code = NRF_ERROR_INVALID_STATE;
}
else
{
m_dfu_state = DFU_STATE_VALIDATE;
// Valid peer activity detected. Hence restart the DFU timer.
err_code = dfu_timer_restart();
if (err_code == NRF_SUCCESS)
{
err_code = dfu_init_postvalidate((uint8_t *)mp_storage_handle_active->block_id,
m_image_size);
VERIFY_SUCCESS(err_code);
m_dfu_state = DFU_STATE_WAIT_4_ACTIVATE;
}
}
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
uint32_t dfu_image_activate()
{
uint32_t err_code;
switch (m_dfu_state)
{
case DFU_STATE_WAIT_4_ACTIVATE:
// Stop the DFU Timer because the peer activity need not be monitored any longer.
err_code = app_timer_stop(m_dfu_timer_id);
APP_ERROR_CHECK(err_code);
err_code = m_functions.activate();
break;
default:
err_code = NRF_ERROR_INVALID_STATE;
break;
}
return err_code;
}
void dfu_reset(void)
{
dfu_update_status_t update_status;
update_status.status_code = DFU_RESET;
bootloader_dfu_update_process(update_status);
}
static uint32_t dfu_compare_block(uint32_t * ptr1, uint32_t * ptr2, uint32_t len)
{
sd_mbr_command_t sd_mbr_cmd;
sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
sd_mbr_cmd.params.compare.ptr1 = ptr1;
sd_mbr_cmd.params.compare.ptr2 = ptr2;
sd_mbr_cmd.params.compare.len = len / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
static uint32_t dfu_copy_sd(uint32_t * src, uint32_t * dst, uint32_t len)
{
sd_mbr_command_t sd_mbr_cmd;
sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_SD;
sd_mbr_cmd.params.copy_sd.src = src;
sd_mbr_cmd.params.copy_sd.dst = dst;
sd_mbr_cmd.params.copy_sd.len = len / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
static uint32_t dfu_sd_img_block_swap(uint32_t * src,
uint32_t * dst,
uint32_t len,
uint32_t block_size)
{
// It is neccesarry to swap the new SoftDevice in 3 rounds to ensure correct copy of data
// and verifucation of data in case power reset occurs during write to flash.
// To ensure the robustness of swapping the images are compared backwards till start of
// image swap. If the back is identical everything is swapped.
uint32_t err_code = dfu_compare_block(src, dst, len);
if (err_code == NRF_SUCCESS)
{
return err_code;
}
if ((uint32_t)dst > SOFTDEVICE_REGION_START)
{
err_code = dfu_sd_img_block_swap((uint32_t *)((uint32_t)src - block_size),
(uint32_t *)((uint32_t)dst - block_size),
block_size,
block_size);
VERIFY_SUCCESS(err_code);
}
err_code = dfu_copy_sd(src, dst, len);
VERIFY_SUCCESS(err_code);
return dfu_compare_block(src, dst, len);
}
uint32_t dfu_sd_image_swap(void)
{
bootloader_settings_t boot_settings;
bootloader_settings_get(&boot_settings);
if (boot_settings.sd_image_size == 0)
{
return NRF_SUCCESS;
}
if ((SOFTDEVICE_REGION_START + boot_settings.sd_image_size) > boot_settings.sd_image_start)
{
uint32_t err_code;
uint32_t sd_start = SOFTDEVICE_REGION_START;
uint32_t block_size = (boot_settings.sd_image_start - sd_start) / 2;
/* ##### FIX START ##### */
block_size &= ~(uint32_t)(CODE_PAGE_SIZE - 1);
/* ##### FIX END ##### */
uint32_t image_end = boot_settings.sd_image_start + boot_settings.sd_image_size;
uint32_t img_block_start = boot_settings.sd_image_start + 2 * block_size;
uint32_t sd_block_start = sd_start + 2 * block_size;
if (SD_SIZE_GET(MBR_SIZE) < boot_settings.sd_image_size)
{
// This will clear a page thus ensuring the old image is invalidated before swapping.
err_code = dfu_copy_sd((uint32_t *)(sd_start + block_size),
(uint32_t *)(sd_start + block_size),
sizeof(uint32_t));
VERIFY_SUCCESS(err_code);
err_code = dfu_copy_sd((uint32_t *)sd_start, (uint32_t *)sd_start, sizeof(uint32_t));
VERIFY_SUCCESS(err_code);
}
return dfu_sd_img_block_swap((uint32_t *)img_block_start,
(uint32_t *)sd_block_start,
image_end - img_block_start,
block_size);
}
else
{
if (boot_settings.sd_image_size != 0)
{
return dfu_copy_sd((uint32_t *)boot_settings.sd_image_start,
(uint32_t *)SOFTDEVICE_REGION_START,
boot_settings.sd_image_size);
}
}
return NRF_SUCCESS;
}
uint32_t dfu_bl_image_swap(void)
{
bootloader_settings_t bootloader_settings;
sd_mbr_command_t sd_mbr_cmd;
bootloader_settings_get(&bootloader_settings);
if (bootloader_settings.bl_image_size != 0)
{
uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ?
DFU_BANK_0_REGION_START :
bootloader_settings.sd_image_start +
bootloader_settings.sd_image_size;
sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_BL;
sd_mbr_cmd.params.copy_bl.bl_src = (uint32_t *)(bl_image_start);
sd_mbr_cmd.params.copy_bl.bl_len = bootloader_settings.bl_image_size / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
return NRF_SUCCESS;
}
uint32_t dfu_bl_image_validate(void)
{
bootloader_settings_t bootloader_settings;
sd_mbr_command_t sd_mbr_cmd;
bootloader_settings_get(&bootloader_settings);
if (bootloader_settings.bl_image_size != 0)
{
uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ?
DFU_BANK_0_REGION_START :
bootloader_settings.sd_image_start +
bootloader_settings.sd_image_size;
sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)BOOTLOADER_REGION_START;
sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)(bl_image_start);
sd_mbr_cmd.params.compare.len = bootloader_settings.bl_image_size / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}
return NRF_SUCCESS;
}
uint32_t dfu_sd_image_validate(void)
{
bootloader_settings_t bootloader_settings;
sd_mbr_command_t sd_mbr_cmd;
bootloader_settings_get(&bootloader_settings);
if (bootloader_settings.sd_image_size == 0)
{
return NRF_SUCCESS;
}
if ((SOFTDEVICE_REGION_START + bootloader_settings.sd_image_size) > bootloader_settings.sd_image_start)
{
uint32_t sd_start = SOFTDEVICE_REGION_START;
uint32_t block_size = (bootloader_settings.sd_image_start - sd_start) / 2;
uint32_t image_end = bootloader_settings.sd_image_start +
bootloader_settings.sd_image_size;
uint32_t img_block_start = bootloader_settings.sd_image_start + 2 * block_size;
uint32_t sd_block_start = sd_start + 2 * block_size;
if (SD_SIZE_GET(MBR_SIZE) < bootloader_settings.sd_image_size)
{
return NRF_ERROR_NULL;
}
return dfu_sd_img_block_swap((uint32_t *)img_block_start,
(uint32_t *)sd_block_start,
image_end - img_block_start,
block_size);
}
sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)SOFTDEVICE_REGION_START;
sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)bootloader_settings.sd_image_start;
sd_mbr_cmd.params.compare.len = bootloader_settings.sd_image_size / sizeof(uint32_t);
return sd_mbr_command(&sd_mbr_cmd);
}

View File

@ -0,0 +1,44 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_dfu_transport DFU transport API.
* @{
*
* @brief DFU transport module interface.
*/
#ifndef DFU_TRANSPORT_H__
#define DFU_TRANSPORT_H__
#include <stdint.h>
/**@brief Function for starting the update of Device Firmware.
*
* @retval NRF_SUCCESS Operation success.
*/
uint32_t dfu_transport_serial_update_start(void);
/**@brief Function for closing the transport layer.
*
* @retval NRF_SUCCESS Operation success.
*/
uint32_t dfu_transport_serial_close(void);
uint32_t dfu_transport_ble_update_start(void);
uint32_t dfu_transport_ble_close();
#endif // DFU_TRANSPORT_H__
/**@} */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,314 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include "dfu_transport.h"
#include <stddef.h>
#include "dfu.h"
#include <dfu_types.h>
#include "app_error.h"
#include "app_util.h"
#include "hci_transport.h"
#include "app_timer.h"
#include "app_scheduler.h"
#define MAX_BUFFERS 4u /**< Maximum number of buffers that can be received queued without being consumed. */
/**
* defgroup Data Packet Queue Access Operation Macros
* @{
*/
/** Provides status showing if the queue is full or not. */
#define DATA_QUEUE_FULL() \
(((MAX_BUFFERS -1) == m_data_queue.count) ? true : false)
/** Provides status showing if the queue is empty or not */
#define DATA_QUEUE_EMPTY() \
((0 == m_data_queue.count) ? true : false)
/** Initializes an element of the data queue. */
#define DATA_QUEUE_ELEMENT_INIT(i) \
m_data_queue.data_packet[(i)].packet_type = INVALID_PACKET
/** Sets the packet type of an element of the data queue. */
#define DATA_QUEUE_ELEMENT_SET_PTYPE(i, t) \
m_data_queue.data_packet[(i)].packet_type = (t)
/** Copies a data packet pointer of an element of the data queue. */
#define DATA_QUEUE_ELEMENT_COPY_PDATA(i, dp) \
m_data_queue.data_packet[(i)].params.data_packet.p_data_packet = (uint32_t *)(dp)
/** Sets the packet length of an element in the data queue. */
#define DATA_QUEUE_ELEMENT_SET_PLEN(i, l) \
m_data_queue.data_packet[(i)].params.data_packet.packet_length = (l)
/** Gets a data packet pointer of an element in the data queue. */
#define DATA_QUEUE_ELEMENT_GET_PDATA(i) \
(m_data_queue.data_packet[(i)].params.data_packet.p_data_packet)
/** Gets the packet type of an element in the data queue. */
#define DATA_QUEUE_ELEMENT_GET_PTYPE(i) \
m_data_queue.data_packet[(i)].packet_type
/* @} */
/** Abstracts data packet queue */
typedef struct
{
dfu_update_packet_t data_packet[MAX_BUFFERS]; /**< Bootloader data packets used when processing data from the UART. */
volatile uint8_t count; /**< Counter to maintain number of elements in the queue. */
} dfu_data_queue_t;
static dfu_data_queue_t m_data_queue; /**< Received-data packet queue. */
/**@brief Initializes an element of the data buffer queue.
*
* @param[in] element_index index of the element.
*/
static void data_queue_element_init (uint8_t element_index)
{
DATA_QUEUE_ELEMENT_INIT(element_index);
DATA_QUEUE_ELEMENT_SET_PTYPE(element_index, INVALID_PACKET);
DATA_QUEUE_ELEMENT_COPY_PDATA(element_index, NULL);
DATA_QUEUE_ELEMENT_SET_PLEN(element_index, 0);
}
/** Initializes data buffer queue */
static void data_queue_init(void)
{
uint32_t index;
m_data_queue.count = 0;
for (index = 0; index < MAX_BUFFERS; index++)
{
data_queue_element_init(index);
}
}
/**@brief Function for freeing an element.
*
* @param[in] element_index index of the element.
*/
static uint32_t data_queue_element_free(uint8_t element_index)
{
uint8_t * p_data;
uint32_t retval;
retval = NRF_ERROR_INVALID_PARAM;
if (MAX_BUFFERS > element_index)
{
p_data = (uint8_t *)DATA_QUEUE_ELEMENT_GET_PDATA(element_index);
if (INVALID_PACKET != DATA_QUEUE_ELEMENT_GET_PTYPE(element_index))
{
m_data_queue.count--;
data_queue_element_init (element_index);
retval = hci_transport_rx_pkt_consume((p_data - 4));
APP_ERROR_CHECK(retval);
}
}
else
{
return NRF_ERROR_INVALID_PARAM;
}
return NRF_SUCCESS;
}
/**@brief Function for Allocating element.
*
* @param[in] packet_type packet type.
* @param[out] p_element_index index of the element.
*/
static uint32_t data_queue_element_alloc(uint8_t * p_element_index, uint8_t packet_type)
{
uint32_t retval;
uint32_t index;
retval = NRF_ERROR_NO_MEM;
if (INVALID_PACKET == packet_type)
{
retval = NRF_ERROR_INVALID_PARAM;
}
else if (true == DATA_QUEUE_FULL())
{
retval = NRF_ERROR_NO_MEM;
}
else
{
for (index = 0; index < MAX_BUFFERS; index++)
{
if (INVALID_PACKET == DATA_QUEUE_ELEMENT_GET_PTYPE(index))
{
// Found a free element: allocate, and end search.
*p_element_index = index;
DATA_QUEUE_ELEMENT_SET_PTYPE(index, packet_type);
retval = NRF_SUCCESS;
m_data_queue.count++;
break;
}
}
}
return retval;
}
// Flush everything on disconnect or stop.
static void data_queue_flush(void)
{
uint32_t index;
for (index = 0; index < MAX_BUFFERS; index++)
{
// In this case it does not matter if free succeeded or not as data packets are being flushed because DFU Trnsport was closed
(void)data_queue_element_free(index);
}
}
/**@brief Function for handling the callback events from the dfu module.
* Callbacks are expected when \ref dfu_data_pkt_handle has been executed.
*
* @param[in] packet Packet type for which this callback is related. START_PACKET, DATA_PACKET.
* @param[in] result Operation result code. NRF_SUCCESS when a queued operation was successful.
* @param[in] p_data Pointer to the data to which the operation is related.
*/
static void dfu_cb_handler(uint32_t packet, uint32_t result, uint8_t * p_data)
{
APP_ERROR_CHECK(result);
}
static void process_dfu_packet(void * p_event_data, uint16_t event_size)
{
uint32_t retval;
uint32_t index;
dfu_update_packet_t * packet;
// Adafruit modification for startup dfu
extern bool forced_startup_dfu_packet_received;
forced_startup_dfu_packet_received = true;
while (false == DATA_QUEUE_EMPTY())
{
// Fetch the element to be processed.
for (index = 0; index < MAX_BUFFERS ; index++)
{
packet = &m_data_queue.data_packet[index];
if (INVALID_PACKET != packet->packet_type)
{
extern void blinky_fast_set(bool isFast);
switch (DATA_QUEUE_ELEMENT_GET_PTYPE(index))
{
case DATA_PACKET:
(void)dfu_data_pkt_handle(packet);
break;
case START_PACKET:
packet->params.start_packet =
(dfu_start_packet_t*)packet->params.data_packet.p_data_packet;
retval = dfu_start_pkt_handle(packet);
APP_ERROR_CHECK(retval);
break;
case INIT_PACKET:
(void)dfu_init_pkt_handle(packet);
retval = dfu_init_pkt_complete();
APP_ERROR_CHECK(retval);
blinky_fast_set(true);
break;
case STOP_DATA_PACKET:
(void)dfu_image_validate();
(void)dfu_image_activate();
blinky_fast_set(false);
// Break the loop by returning.
return;
default:
// No implementation needed.
break;
}
// Free the processed element.
retval = data_queue_element_free(index);
APP_ERROR_CHECK(retval);
}
}
}
}
void rpc_transport_event_handler(hci_transport_evt_t event)
{
uint32_t retval;
uint16_t rpc_cmd_length_read = 0;
uint8_t * p_rpc_cmd_buffer = NULL;
uint8_t element_index;
retval = hci_transport_rx_pkt_extract(&p_rpc_cmd_buffer, &rpc_cmd_length_read);
if (NRF_SUCCESS == retval)
{
// Verify if the data queue can buffer the packet.
retval = data_queue_element_alloc(&element_index, p_rpc_cmd_buffer[0]);
if (NRF_SUCCESS == retval)
{
//subtract 1 since we are interested in payload length and not the type field.
DATA_QUEUE_ELEMENT_SET_PLEN(element_index,(rpc_cmd_length_read / sizeof(uint32_t)) - 1);
DATA_QUEUE_ELEMENT_COPY_PDATA(element_index, &p_rpc_cmd_buffer[4]);
retval = app_sched_event_put(NULL, 0, process_dfu_packet);
}
}
if (p_rpc_cmd_buffer != NULL && NRF_SUCCESS != retval)
{
// Free the packet that could not be processed.
retval = hci_transport_rx_pkt_consume(p_rpc_cmd_buffer);
APP_ERROR_CHECK(retval);
}
}
uint32_t dfu_transport_serial_update_start(void)
{
uint32_t err_code;
// Initialize data buffer queue.
data_queue_init();
dfu_register_callback(dfu_cb_handler);
// Open transport layer.
err_code = hci_transport_open();
APP_ERROR_CHECK(err_code);
// Register callback to be run when commands have been received by the transport layer.
err_code = hci_transport_evt_handler_reg(rpc_transport_event_handler);
APP_ERROR_CHECK(err_code);
return NRF_SUCCESS;
}
uint32_t dfu_transport_serial_close(void)
{
// Remove all buffered packets.
data_queue_flush();
return hci_transport_close();
}

View File

@ -0,0 +1,176 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup nrf_dfu_types Types and definitions.
* @{
*
* @ingroup nrf_dfu
*
* @brief Device Firmware Update module type and definitions.
*/
#ifndef DFU_TYPES_H__
#define DFU_TYPES_H__
#include <stdint.h>
#include "nrf_sdm.h"
#include "nrf_mbr.h"
#include "nrf.h"
#include "app_util.h"
#define NRF_UICR_BOOT_START_ADDRESS (NRF_UICR_BASE + 0x14) /**< Register where the bootloader start address is stored in the UICR register. */
#if defined(NRF52832_XXAA) || defined(NRF52840_XXAA)
#define NRF_UICR_MBR_PARAMS_PAGE_ADDRESS (NRF_UICR_BASE + 0x18) /**< Register where the mbr params page is stored in the UICR register. (Only in use in nRF52 MBR).*/
#endif
#define CODE_REGION_1_START SD_SIZE_GET(MBR_SIZE) /**< This field should correspond to the size of Code Region 0, (which is identical to Start of Code Region 1), found in UICR.CLEN0 register. This value is used for compile safety, as the linker will fail if application expands into bootloader. Runtime, the bootloader will use the value found in UICR.CLEN0. */
#define SOFTDEVICE_REGION_START MBR_SIZE /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */
#ifdef NRF51
#define CODE_PAGE_SIZE 0x0400 /**< Size of a flash codepage. Used for size of the reserved flash space in the bootloader region. Will be runtime checked against NRF_UICR->CODEPAGESIZE to ensure the region is correct. */
#ifdef SIGNING
#define BOOTLOADER_REGION_START 0x00039C00 /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */
#define BOOTLOADER_SETTINGS_ADDRESS 0x0003D800 /**< The field specifies the page location of the bootloader settings address. */
#else
#define BOOTLOADER_REGION_START 0x0003C000 /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */
#define BOOTLOADER_SETTINGS_ADDRESS 0x0003FC00 /**< The field specifies the page location of the bootloader settings address. */
#endif
#elif defined(NRF52832_XXAA)
#define BOOTLOADER_REGION_START 0x00074000 /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */
#define BOOTLOADER_SETTINGS_ADDRESS 0x0007F000 /**< The field specifies the page location of the bootloader settings address. */
#define BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS 0x0007E000 /**< The field specifies the page location of the mbr params page address. */
#define CODE_PAGE_SIZE 0x1000 /**< Size of a flash codepage. Used for size of the reserved flash space in the bootloader region. Will be runtime checked against NRF_UICR->CODEPAGESIZE to ensure the region is correct. */
#elif defined(NRF52840_XXAA)
#define BOOTLOADER_REGION_START 0x000F4000 /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */
#define BOOTLOADER_SETTINGS_ADDRESS 0x000FF000 /**< The field specifies the page location of the bootloader settings address. */
#define BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS 0x000FE000 /**< The field specifies the page location of the mbr params page address. */
#define CODE_PAGE_SIZE 0x1000 /**< Size of a flash codepage. Used for size of the reserved flash space in the bootloader region. Will be runtime checked against NRF_UICR->CODEPAGESIZE to ensure the region is correct. */
#else
#error No target defined
#endif
#define DFU_REGION_TOTAL_SIZE (BOOTLOADER_REGION_START - CODE_REGION_1_START) /**< Total size of the region between SD and Bootloader. */
#ifndef DFU_APP_DATA_RESERVED
#define DFU_APP_DATA_RESERVED CODE_PAGE_SIZE * 7 /**< Size of Application Data that must be preserved between application updates. This value must be a multiple of page size. Page size is 0x400 (1024d) bytes, thus this value must be 0x0000, 0x0400, 0x0800, 0x0C00, 0x1000, etc. */
#endif
#define DFU_IMAGE_MAX_SIZE_FULL (DFU_REGION_TOTAL_SIZE - DFU_APP_DATA_RESERVED) /**< Maximum size of an application, excluding save data from the application. */
#define DFU_IMAGE_MAX_SIZE_BANKED (((DFU_IMAGE_MAX_SIZE_FULL) - \
(DFU_IMAGE_MAX_SIZE_FULL % (2 * CODE_PAGE_SIZE)))/2) /**< Maximum size of an application, excluding save data from the application. */
#define DFU_BL_IMAGE_MAX_SIZE (BOOTLOADER_SETTINGS_ADDRESS - BOOTLOADER_REGION_START) /**< Maximum size of a bootloader, excluding save data from the current bootloader. */
#define DFU_BANK_0_REGION_START CODE_REGION_1_START /**< Bank 0 region start. */
#define DFU_BANK_1_REGION_START (DFU_BANK_0_REGION_START + DFU_IMAGE_MAX_SIZE_BANKED) /**< Bank 1 region start. */
#define EMPTY_FLASH_MASK 0xFFFFFFFF /**< Bit mask that defines an empty address in flash. */
#define INVALID_PACKET 0x00 /**< Invalid packet identifies. */
#define INIT_PACKET 0x01 /**< Packet identifies for initialization packet. */
#define STOP_INIT_PACKET 0x02 /**< Packet identifies for stop initialization packet. Used when complete init packet has been received so that the init packet can be used for pre validaiton. */
#define START_PACKET 0x03 /**< Packet identifies for the Data Start Packet. */
#define DATA_PACKET 0x04 /**< Packet identifies for a Data Packet. */
#define STOP_DATA_PACKET 0x05 /**< Packet identifies for the Data Stop Packet. */
#define DFU_UPDATE_SD 0x01 /**< Bit field indicating update of SoftDevice is ongoing. */
#define DFU_UPDATE_BL 0x02 /**< Bit field indicating update of bootloader is ongoing. */
#define DFU_UPDATE_APP 0x04 /**< Bit field indicating update of application is ongoing. */
#define DFU_INIT_RX 0x00 /**< Op Code identifies for receiving init packet. */
#define DFU_INIT_COMPLETE 0x01 /**< Op Code identifies for transmission complete of init packet. */
// Safe guard to ensure during compile time that the DFU_APP_DATA_RESERVED is a multiple of page size.
STATIC_ASSERT((((DFU_APP_DATA_RESERVED) & (CODE_PAGE_SIZE - 1)) == 0x00));
/**@brief Structure holding a start packet containing update mode and image sizes.
*/
typedef struct
{
uint8_t dfu_update_mode; /**< Packet type, used to identify the content of the received packet referenced by data packet. */
uint32_t sd_image_size; /**< Size of the SoftDevice image to be transferred. Zero if no SoftDevice image will be transfered. */
uint32_t bl_image_size; /**< Size of the Bootloader image to be transferred. Zero if no Bootloader image will be transfered. */
uint32_t app_image_size; /**< Size of the application image to be transmitted. Zero if no Bootloader image will be transfered. */
} dfu_start_packet_t;
/**@brief Structure holding a bootloader init/data packet received.
*/
typedef struct
{
uint32_t packet_length; /**< Packet length of the data packet. Each data is word size, meaning length of 4 is 4 words, not bytes. */
uint32_t * p_data_packet; /**< Data Packet received. Each data is a word size entry. */
} dfu_data_packet_t;
/**@brief Structure for holding dfu update packet. Packet type indicate the type of packet.
*/
typedef struct
{
uint32_t packet_type; /**< Packet type, used to identify the content of the received packet referenced by data packet. */
union
{
dfu_data_packet_t data_packet; /**< Used when packet type is INIT_PACKET or DATA_PACKET. Packet contains data received for init or data. */
dfu_start_packet_t * start_packet; /**< Used when packet type is START_DATA_PACKET. Will contain information on software to be updtaed, i.e. SoftDevice, Bootloader and/or Application along with image sizes. */
} params;
} dfu_update_packet_t;
/**@brief DFU status error codes.
*/
typedef enum
{
DFU_UPDATE_APP_COMPLETE, /**< Status update of application complete.*/
DFU_UPDATE_SD_COMPLETE, /**< Status update of SoftDevice update complete. Note that this solely indicates that a new SoftDevice has been received and stored in bank 0 and 1. */
DFU_UPDATE_SD_SWAPPED, /**< Status update of SoftDevice update complete. Note that this solely indicates that a new SoftDevice has been received and stored in bank 0 and 1. */
DFU_UPDATE_BOOT_COMPLETE, /**< Status update complete.*/
DFU_BANK_0_ERASED, /**< Status bank 0 erased.*/
DFU_TIMEOUT, /**< Status timeout.*/
DFU_RESET /**< Status Reset to indicate current update procedure has been aborted and system should reset. */
} dfu_update_status_code_t;
/**@brief Structure holding DFU complete event.
*/
typedef struct
{
dfu_update_status_code_t status_code; /**< Device Firmware Update status. */
uint16_t app_crc; /**< CRC of the recieved application. */
uint32_t sd_size; /**< Size of the recieved SoftDevice. */
uint32_t bl_size; /**< Size of the recieved BootLoader. */
uint32_t app_size; /**< Size of the recieved Application. */
uint32_t sd_image_start; /**< Location in flash where the received SoftDevice image is stored. */
} dfu_update_status_t;
/**@brief Update complete handler type. */
typedef void (*dfu_complete_handler_t)(dfu_update_status_t dfu_update_status);
#endif // DFU_TYPES_H__
/**@} */

View File

@ -0,0 +1,32 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/** @file
*
* @defgroup memory_pool_internal Memory Pool Internal
* @{
* @ingroup memory_pool
*
* @brief Memory pool internal definitions
*/
#ifndef MEM_POOL_INTERNAL_H__
#define MEM_POOL_INTERNAL_H__
#define TX_BUF_SIZE 32u /**< TX buffer size in bytes. */
#define RX_BUF_SIZE 600u /**< RX buffer size in bytes. */
#define RX_BUF_QUEUE_SIZE 2u /**< RX buffer element size. */
#endif // MEM_POOL_INTERNAL_H__
/** @} */

View File

@ -0,0 +1,45 @@
/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@file
*
* @defgroup ble_sdk_bootloader_hci_congfig HCI Transport Layer Configuration
* @{
* @ingroup dfu_bootloader_api
* @brief Definition of HCI Transport Layer configurable parameters
*/
#ifndef HCI_TRANSPORT_CONFIG_H__
#define HCI_TRANSPORT_CONFIG_H__
#include "boards.h" /**< Default include for boards.h which means that default pin numbers will be used for RX, TX, CTS, and RTS on the UART. Other pin number can be used if desired. */
/** This section covers configurable parameters for the HCI Transport SLIP layer. */
#define HCI_SLIP_UART_RX_PIN_NUMBER RX_PIN_NUMBER /**< Defines the UART RX pin number. The default pin for the board is chosen, but can be overwritten. */
#define HCI_SLIP_UART_TX_PIN_NUMBER TX_PIN_NUMBER /**< Defines the UART TX pin number. The default pin for the board is chosen, but can be overwritten. */
#define HCI_SLIP_UART_RTS_PIN_NUMBER RTS_PIN_NUMBER /**< Defines the UART RTS pin number. The default pin for the board is chosen, but can be overwritten. */
#define HCI_SLIP_UART_CTS_PIN_NUMBER CTS_PIN_NUMBER /**< Defines the UART CTS pin number. The default pin for the board is chosen, but can be overwritten. */
#define HCI_SLIP_UART_MODE HWFC /**< Defines the UART mode to be used. Use UART Low Power with Flow Control - Valid values are defined in \ref app_uart_flow_control_t. For further information on the UART Low Power mode, please refer to: \ref app_uart . */
#define HCI_SLIP_UART_BAUDRATE UART_BAUDRATE_BAUDRATE_Baud115200 // UART_BAUDRATE_BAUDRATE_Baud38400 /**< Defines the UART Baud rate. Default is 38400 baud. */
/** This section covers configurable parameters for the HCI Transport layer that are used for calculating correct value for the retransmission timer timeout. */
#define MAX_PACKET_SIZE_IN_BITS 8000u /**< Maximum size of a single application packet in bits. */
#define USED_BAUD_RATE 115200u /**< The used uart baudrate. */
#endif // HCI_TRANSPORT_CONFIG_H__
/** @} */

View File

@ -0,0 +1,425 @@
#include "nrf.h"
#include "nrf_log.h"
#include "nrf_error.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
#include <SEGGER_RTT_Conf.h>
#include <SEGGER_RTT.h>
static char buf_normal_up[BUFFER_SIZE_UP];
static char buf_down[BUFFER_SIZE_DOWN];
uint32_t log_rtt_init(void)
{
static bool initialized = false;
if (initialized)
{
return NRF_SUCCESS;
}
if (SEGGER_RTT_ConfigUpBuffer(LOG_TERMINAL_NORMAL,
"Normal",
buf_normal_up,
BUFFER_SIZE_UP,
SEGGER_RTT_MODE_NO_BLOCK_TRIM
)
!= 0)
{
return NRF_ERROR_INVALID_STATE;
}
if (SEGGER_RTT_ConfigDownBuffer(LOG_TERMINAL_INPUT,
"Input",
buf_down,
BUFFER_SIZE_DOWN,
SEGGER_RTT_MODE_NO_BLOCK_SKIP
)
!= 0)
{
return NRF_ERROR_INVALID_STATE;
}
initialized = true;
return NRF_SUCCESS;
}
// Forward declaration of SEGGER RTT vprintf function
int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
void log_rtt_printf(int terminal_index, char * format_msg, ...)
{
//lint -save -e526 -e628 -e530
va_list p_args;
va_start(p_args, format_msg);
(void)SEGGER_RTT_vprintf(terminal_index, format_msg, &p_args);
va_end(p_args);
//lint -restore
}
__INLINE void log_rtt_write_string(int terminal_index, int num_args, ...)
{
const char* msg;
//lint -save -e516 -e530
va_list p_args;
va_start(p_args, num_args);
//lint -restore
for (int i = 0; i < num_args; i++)
{
//lint -save -e26 -e10 -e64 -e526 -e628 -e530
msg = va_arg(p_args, const char*);
//lint -restore
(void)SEGGER_RTT_WriteString(terminal_index, msg);
}
va_end(p_args);
}
void log_rtt_write_hex(int terminal_index, uint32_t value)
{
char temp[11];
temp[0] = '0';
temp[1] = 'x';
temp[10] = 0; // Null termination
uint8_t nibble;
uint8_t i = 8;
while(i-- != 0)
{
nibble = (value >> (4 * i)) & 0x0F;
temp[9-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
}
(void)SEGGER_RTT_WriteString(terminal_index, temp);
}
void log_rtt_write_hex_char(int terminal_index, uint8_t value)
{
char temp[3];
temp[2] = 0; // Null termination
uint8_t nibble;
uint8_t i = 2;
while(i-- != 0)
{
nibble = (value >> (4 * i)) & 0x0F;
temp[1-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
}
(void)SEGGER_RTT_WriteString(terminal_index, temp);
}
__INLINE int log_rtt_has_input()
{
return SEGGER_RTT_HasKey();
}
uint32_t log_rtt_read_input(char * c)
{
int r;
r = SEGGER_RTT_Read(LOG_TERMINAL_INPUT, c, 1);
if (r == 1)
return NRF_SUCCESS;
else
return NRF_ERROR_NULL;
}
#elif defined(NRF_LOG_USES_UART) && NRF_LOG_USES_UART == 1
#include "app_uart.h"
#include "app_error.h"
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "bsp.h"
#define MAX_TEST_DATA_BYTES (15U) /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 512 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */
static uint8_t m_uart_data;
static bool m_uart_has_input;
void uart_error_cb(app_uart_evt_t * p_event)
{
if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_communication);
}
else if (p_event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_code);
}
}
uint32_t log_uart_init()
{
static bool initialized = false;
if (initialized)
{
return NRF_SUCCESS;
}
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_cb,
APP_IRQ_PRIORITY_LOW,
err_code);
initialized = true;
return err_code;
}
//lint -save -e530 -e64
void log_uart_printf(const char * format_msg, ...)
{
va_list p_args;
va_start(p_args, format_msg);
(void)vprintf(format_msg, p_args);
va_end(p_args);
}
__INLINE void log_uart_write_string_many(int num_args, ...)
{
const char* msg;
va_list p_args;
va_start(p_args, num_args);
for (int i = 0; i < num_args; i++)
{
msg = va_arg(p_args, const char*);
log_uart_write_string(msg);
}
va_end(p_args);
}
__INLINE void log_uart_write_string(const char* msg)
{
while( *msg )
{
(void)app_uart_put(*msg++);
}
}
//lint -restore
void log_uart_write_hex(uint32_t value)
{
uint8_t nibble;
uint8_t i = 8;
(void)app_uart_put('0');
(void)app_uart_put('x');
while( i-- != 0 )
{
nibble = (value >> (4 * i)) & 0x0F;
(void)app_uart_put( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
}
}
void log_uart_write_hex_char(uint8_t c)
{
uint8_t nibble;
uint8_t i = 2;
while( i-- != 0 )
{
nibble = (c >> (4 * i)) & 0x0F;
(void)app_uart_put( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
}
}
__INLINE int log_uart_has_input()
{
if (m_uart_has_input) return 1;
if (app_uart_get(&m_uart_data) == NRF_SUCCESS)
{
m_uart_has_input = true;
return 1;
}
return 0;
}
uint32_t log_uart_read_input(char * c)
{
if (m_uart_has_input)
{
*c = (char)m_uart_data;
m_uart_has_input = false;
return NRF_SUCCESS;
}
if (app_uart_get((uint8_t *)c) == NRF_SUCCESS)
{
return NRF_SUCCESS;
}
return NRF_ERROR_NULL;
}
#elif defined(NRF_LOG_USES_RAW_UART) && NRF_LOG_USES_RAW_UART == 1
#include "app_uart.h"
#include <stdio.h>
#include <string.h>
#include "bsp.h"
uint32_t log_raw_uart_init()
{
// Disable UART
NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled;
// Configure RX/TX pins
nrf_gpio_cfg_output( TX_PIN_NUMBER );
nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
// Set a default baud rate of UART0_CONFIG_BAUDRATE
NRF_UART0->PSELTXD = TX_PIN_NUMBER;
NRF_UART0->BAUDRATE = UART0_CONFIG_BAUDRATE;
NRF_UART0->PSELRTS = 0xFFFFFFFF;
NRF_UART0->PSELCTS = 0xFFFFFFFF;
// Disable parity and interrupt
NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos );
NRF_UART0->CONFIG |= (UART_CONFIG_HWFC_Disabled << UART_CONFIG_HWFC_Pos );
// Re-enable the UART
NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
NRF_UART0->INTENSET = 0;
NRF_UART0->TASKS_STARTTX = 1;
NRF_UART0->TASKS_STARTRX = 1;
return NRF_SUCCESS;
}
void log_raw_uart_printf(const char * format_msg, ...)
{
static char buffer[256];
va_list p_args;
va_start(p_args, format_msg);
sprintf(buffer, format_msg, p_args);
va_end(p_args);
log_raw_uart_write_string(buffer);
}
__INLINE void log_raw_uart_write_char(const char c)
{
NRF_UART0->TXD = c;
while( NRF_UART0->EVENTS_TXDRDY != 1 );
NRF_UART0->EVENTS_TXDRDY = 0;
}
__INLINE void log_raw_uart_write_string_many(int num_args, ...)
{
const char* msg;
va_list p_args;
va_start(p_args, num_args);
for (int i = 0; i < num_args; i++)
{
msg = va_arg(p_args, const char*);
log_raw_uart_write_string(msg);
}
va_end(p_args);
}
__INLINE void log_raw_uart_write_string(const char* msg)
{
while( *msg )
{
NRF_UART0->TXD = *msg++;
while( NRF_UART0->EVENTS_TXDRDY != 1 );
NRF_UART0->EVENTS_TXDRDY = 0;
}
}
void log_raw_uart_write_hex(uint32_t value)
{
uint8_t nibble;
uint8_t i = 8;
log_raw_uart_write_string( "0x" );
while( i-- != 0 )
{
nibble = (value >> (4 * i)) & 0x0F;
log_raw_uart_write_char( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
}
}
void log_raw_uart_write_hex_char(uint8_t c)
{
uint8_t nibble;
uint8_t i = 2;
while( i-- != 0 )
{
nibble = (c >> (4 * i)) & 0x0F;
log_raw_uart_write_hex( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
}
}
__INLINE int log_raw_uart_has_input()
{
return 0;
}
uint32_t log_raw_uart_read_input(char * c)
{
return NRF_ERROR_NULL;
}
#endif // NRF_LOG_USES_RAW_UART == 1
const char* log_hex_char(const char c)
{
static volatile char hex_string[3];
hex_string[2] = 0; // Null termination
uint8_t nibble;
uint8_t i = 2;
while(i-- != 0)
{
nibble = (c >> (4 * i)) & 0x0F;
hex_string[1-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
}
return (const char*) hex_string;
}
const char* log_hex(uint32_t value)
{
static volatile char hex_string[11];
hex_string[0] = '0';
hex_string[1] = 'x';
hex_string[10] = 0;
uint8_t nibble;
uint8_t i = 8;
while(i-- != 0)
{
nibble = (value >> (4 * i)) & 0x0F;
hex_string[9-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
}
return (const char*)hex_string;
}

View File

@ -0,0 +1,715 @@
#ifndef NRF_LOG_H_
#define NRF_LOG_H_
#ifndef DOXYGEN
#include <stdint.h>
#include <stdarg.h>
#include <app_util.h>
#define NRF_LOG_MODULE_REGISTER()
#define NRF_LOG_ERROR(...)
#define NRF_LOG_WARNING(...)
#define NRF_LOG_INFO(...)
#define NRF_LOG_DEBUG(...)
#define NRF_LOG_RAW_INFO(...)
#define NRF_LOG_HEXDUMP_ERROR(p_data, len)
#define NRF_LOG_HEXDUMP_WARNING(p_data, len)
#define NRF_LOG_HEXDUMP_INFO(p_data, len)
#define NRF_LOG_HEXDUMP_DEBUG(p_data, len)
#define NRF_LOG_RAW_HEXDUMP_INFO(p_data, len)
#define NRF_LOG_GETCHAR()
#define NRF_LOG_PUSH(_str)
#ifndef NRF_LOG_USES_RTT
#define NRF_LOG_USES_RTT 0
#endif
#ifndef NRF_LOG_USES_UART
#define NRF_LOG_USES_UART 0
#endif
#ifndef NRF_LOG_USES_RAW_UART
#define NRF_LOG_USES_RAW_UART 0
#endif
#ifndef NRF_LOG_USES_COLORS
#define NRF_LOG_USES_COLORS 1
#endif
#if NRF_LOG_USES_COLORS == 1
#define NRF_LOG_COLOR_DEFAULT "\x1B[0m"
#define NRF_LOG_COLOR_BLACK "\x1B[1;30m"
#define NRF_LOG_COLOR_RED "\x1B[1;31m"
#define NRF_LOG_COLOR_GREEN "\x1B[1;32m"
#define NRF_LOG_COLOR_YELLOW "\x1B[1;33m"
#define NRF_LOG_COLOR_BLUE "\x1B[1;34m"
#define NRF_LOG_COLOR_MAGENTA "\x1B[1;35m"
#define NRF_LOG_COLOR_CYAN "\x1B[1;36m"
#define NRF_LOG_COLOR_WHITE "\x1B[1;37m"
#else
#define NRF_LOG_COLOR_DEFAULT
#define NRF_LOG_COLOR_BLACK
#define NRF_LOG_COLOR_RED
#define NRF_LOG_COLOR_GREEN
#define NRF_LOG_COLOR_YELLOW
#define NRF_LOG_COLOR_BLUE
#define NRF_LOG_COLOR_MAGENTA
#define NRF_LOG_COLOR_CYAN
#define NRF_LOG_COLOR_WHITE
#endif
#if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
#define LOG_TERMINAL_NORMAL (0)
#define LOG_TERMINAL_ERROR (1)
#define LOG_TERMINAL_INPUT (0)
/**@brief Function for initializing the SEGGER RTT logger.
*
* @details See <a href="https://www.segger.com/jlink-rtt.html" target="_blank">segger.com</a>
* for information about SEGGER Real Time Transfer (RTT).
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
*
* @retval NRF_SUCCESS If initialization was successful.
* @retval NRF_ERROR Otherwise.
*/
uint32_t log_rtt_init(void);
/**@brief Function for writing a printf string using RTT.
*
* @details The printf implementation in SEGGER's RTT is more efficient than
* the standard implementation. However, printf requires more processor time
* than other logging functions. Therefore, applications that require logging
* but need it to interfere as little as possible with the execution, should
* avoid using printf.
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_PRINTF
* - @ref NRF_LOG_PRINTF_DEBUG
* - @ref NRF_LOG_PRINTF_ERROR
*
* @param terminal_index Segger RTT terminal index to use as output.
* @param format_msg Printf format string.
*/
void log_rtt_printf(int terminal_index, char * format_msg, ...);
/**@brief Function for writing a string using RTT.
*
* @details The string to write must be null-terminated, but the null termination will not be stored
* in the ring buffer.
* The impact of running this function should be very low compared to writing to UART.
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG
* - @ref NRF_LOG_DEBUG
* - @ref NRF_LOG_ERROR
*
* @param terminal_index Segger RTT terminal index to use as output.
* @param num_args Number of arguments.
*/
void log_rtt_write_string(int terminal_index, int num_args, ...);
/**@brief Function for writing an integer as HEX using RTT.
*
* The output data is formatted as, for example, 0x89ABCDEF.
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_HEX
* - @ref NRF_LOG_HEX_DEBUG
* - @ref NRF_LOG_HEX_ERROR
*
* @param terminal_index Segger RTT terminal index to use as output.
* @param value Integer value to be printed as HEX.
*/
void log_rtt_write_hex(int terminal_index, uint32_t value);
/**@brief Function for writing a single character as HEX using RTT.
*
* The output string is formatted as, for example, AA.
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_HEX_CHAR
* - @ref NRF_LOG_HEX_CHAR_DEBUG
* - @ref NRF_LOG_HEX_CHAR_ERROR
*
* @param terminal_index Segger RTT terminal index to use as output.
* @param value Character to print as HEX.
*/
void log_rtt_write_hex_char(int terminal_index, uint8_t value);
/**@brief Function for checking if data is available in the input buffer.
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
*
* @retval 1 If characters are available to read.
* @retval 0 If no characters are available.
*/
int log_rtt_has_input(void);
/**@brief Function for reading one character from the input buffer.
*
* @param[out] p_char Pointer where to store the character.
*
* This function is available only when NRF_LOG_USES_RTT is defined as 1.
*
* @note Do not call this function directly. Use @ref NRF_LOG_READ_INPUT instead.
*
* @retval NRF_SUCCESS If the character was read out.
* @retval NRF_ERROR_INVALID_DATA If no character could be read.
*/
uint32_t log_rtt_read_input(char* p_char);
#define NRF_LOG_INIT() log_rtt_init() /*!< Initialize the module. */
#define NRF_LOG_PRINTF(...) log_rtt_printf(LOG_TERMINAL_NORMAL, ##__VA_ARGS__) /*!< Print a log message using printf. */
#define NRF_LOG_PRINTF_DEBUG(...) log_rtt_printf(LOG_TERMINAL_NORMAL, ##__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
#define NRF_LOG_PRINTF_ERROR(...) log_rtt_printf(LOG_TERMINAL_ERROR, ##__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
#define NRF_LOG(...) log_rtt_write_string(LOG_TERMINAL_NORMAL, NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message. The input string must be null-terminated. */
#define NRF_LOG_DEBUG(...) log_rtt_write_string(LOG_TERMINAL_NORMAL, NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< If DEBUG is set, print a log message. The input string must be null-terminated. */
#define NRF_LOG_ERROR(...) log_rtt_write_string(LOG_TERMINAL_ERROR, NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message to the error stream. The input string must be null-terminated. */
#define NRF_LOG_HEX(val) log_rtt_write_hex(LOG_TERMINAL_NORMAL, val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_DEBUG(val) log_rtt_write_hex(LOG_TERMINAL_NORMAL, val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_ERROR(val) log_rtt_write_hex(LOG_TERMINAL_ERROR, val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_CHAR(val) log_rtt_write_hex_char(LOG_TERMINAL_NORMAL, val) /*!< Log a character as HEX value (example output: AA). */
#define NRF_LOG_HEX_CHAR_DEBUG(val) log_rtt_write_hex_char(LOG_TERMINAL_NORMAL, val) /*!< If DEBUG is set, log a character as HEX value (example output: AA). */
#define NRF_LOG_HEX_CHAR_ERROR(val) log_rtt_write_hex_char(LOG_TERMINAL_ERROR, val) /*!< Log a character as HEX value to the error stream (example output: AA). */
#define NRF_LOG_HAS_INPUT() log_rtt_has_input() /*!< Check if the input buffer has unconsumed characters. */
#define NRF_LOG_READ_INPUT(p_char) log_rtt_read_input(p_char) /*!< Consume a character from the input buffer. */
#if !defined(DEBUG) && !defined(DOXYGEN)
#undef NRF_LOG_DEBUG
#define NRF_LOG_DEBUG(...)
#undef NRF_LOG_STR_DEBUG
#define NRF_LOG_STR_DEBUG(...)
#undef NRF_LOG_HEX_DEBUG
#define NRF_LOG_HEX_DEBUG(...)
#undef NRF_LOG_HEX_CHAR_DEBUG
#define NRF_LOG_HEX_CHAR_DEBUG(...)
#endif // !defined(DEBUG) && !defined(DOXYGEN)
#elif defined(NRF_LOG_USES_UART) && NRF_LOG_USES_UART == 1
/**@brief Function for initializing the UART logger.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
*
* @retval NRF_SUCCESS If initialization was successful.
* @retval NRF_ERROR Otherwise.
*/
uint32_t log_uart_init(void);
/**@brief Function for logging a printf string to UART.
*
* @details Printf requires more processor time
* than other logging functions. Therefore, applications that require logging
* but need it to interfere as little as possible with the execution, should
* avoid using printf.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note This function is non-blocking. If too much data is sent to the UART,
* some characters might be skipped.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_PRINTF
* - @ref NRF_LOG_PRINTF_DEBUG
* - @ref NRF_LOG_PRINTF_ERROR
*
* @param format_msg Printf format string.
*/
void log_uart_printf(const char * format_msg, ...);
/**@brief Function for logging a single character to UART.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @param c Character.
*/
void log_uart_write_char(const char c);
/**@brief Function for logging null-terminated strings to UART.
*
* @details This function is more efficient than using printf.
* The null termination will not be logged.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG
* - @ref NRF_LOG_DEBUG
* - @ref NRF_LOG_ERROR
*
* @param num_args Number of arguments.
*/
void log_uart_write_string_many(int num_args, ...);
/**@brief Function for logging a null-terminated string to UART.
*
* @details This function is more efficient than using printf.
* The null termination will not be logged.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG
* - @ref NRF_LOG_DEBUG
* - @ref NRF_LOG_ERROR
*
* @param msg Null-terminated string.
*/
void log_uart_write_string(const char* msg);
/**@brief Function for logging an integer value as HEX to UART.
*
* @details The output data is formatted as, for example, 0x89ABCDEF.
* This function is more efficient than printf.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note This function is non-blocking. If too much data is sent to the UART,
* some characters might be skipped.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_HEX
* - @ref NRF_LOG_HEX_DEBUG
* - @ref NRF_LOG_HEX_ERROR
*
* @param value Integer value to be printed as HEX.
*/
void log_uart_write_hex(uint32_t value);
/**@brief Function for logging a single character as HEX to UART.
*
* @details The output string is formatted as, for example, AA.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note This function is non-blocking. If too much data is sent to the UART,
* some characters might be skipped.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_HEX_CHAR
* - @ref NRF_LOG_HEX_CHAR_DEBUG
* - @ref NRF_LOG_HEX_CHAR_ERROR
*
* @param c Character.
*/
void log_uart_write_hex_char(uint8_t c);
/**@brief Function for checking if data is available in the input buffer.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
*
* @retval 1 If characters are available to read.
* @retval 0 If no characters are available.
*/
int log_uart_has_input(void);
/**@brief Function for reading one character from the input buffer.
*
* @param[out] p_char Pointer where to store the character.
*
* This function is available only when NRF_LOG_USES_UART is defined as 1.
*
* @note Do not call this function directly. Use NRF_LOG_READ_INPUT instead.
*
* @retval NRF_SUCCESS If the character was read out.
* @retval NRF_ERROR_INVALID_DATA If no character could be read.
*/
uint32_t log_uart_read_input(char* p_char);
#define NRF_LOG_INIT() log_uart_init() /*!< Initialize the module. */
#define NRF_LOG_PRINTF(...) log_uart_printf(__VA_ARGS__) /*!< Print a log message using printf. */
#define NRF_LOG_PRINTF_DEBUG(...) log_uart_printf(__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
#define NRF_LOG_PRINTF_ERROR(...) log_uart_printf(__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
#define NRF_LOG(...) log_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message. The input string must be null-terminated. */
#define NRF_LOG_DEBUG(...) log_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< If DEBUG is set, print a log message. The input string must be null-terminated. */
#define NRF_LOG_ERROR(...) log_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message to the error stream. The input string must be null-terminated. */
#define NRF_LOG_HEX(val) log_uart_write_hex(val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_DEBUG(val) log_uart_write_hex(val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_ERROR(val) log_uart_write_hex(val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_CHAR(val) log_uart_write_hex_char(val) /*!< Log a character as HEX value (example output: AA). */
#define NRF_LOG_HEX_CHAR_DEBUG(val) log_uart_write_hex_char(val) /*!< If DEBUG is set, log a character as HEX value (example output: AA). */
#define NRF_LOG_HEX_CHAR_ERROR(val) log_uart_write_hex_char(val) /*!< Log a character as HEX value to the error stream (example output: AA). */
#define NRF_LOG_HAS_INPUT() log_uart_has_input() /*!< Check if the input buffer has unconsumed characters. */
#define NRF_LOG_READ_INPUT(p_char) log_uart_read_input(p_char) /*!< Consume a character from the input buffer. */
#if !defined(DEBUG) && !defined(DOXYGEN)
#undef NRF_LOG_DEBUG
#define NRF_LOG_DEBUG(...)
#undef NRF_LOG_PRINTF_DEBUG
#define NRF_LOG_PRINTF_DEBUG(...)
#undef NRF_LOG_STR_DEBUG
#define NRF_LOG_STR_DEBUG(...)
#undef NRF_LOG_HEX_DEBUG
#define NRF_LOG_HEX_DEBUG(...)
#undef NRF_LOG_HEX_CHAR_DEBUG
#define NRF_LOG_HEX_CHAR_DEBUG(...)
#endif // !defined(DEBUG) && !defined(DOXYGEN)
#elif defined(NRF_LOG_USES_RAW_UART) && NRF_LOG_USES_RAW_UART == 1
/**@brief Function for initializing the raw UART logger.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note Do not call this function directly. Use the macro @ref NRF_LOG_INIT instead.
*
* @retval NRF_SUCCESS If initialization was successful.
* @retval NRF_ERROR Otherwise.
*/
uint32_t log_raw_uart_init(void);
/**@brief Function for logging a printf string to raw UART.
*
* @details Printf requires more processor time
* than other logging functions. Therefore, applications that require logging
* but need it to interfere as little as possible with the execution, should
* avoid using printf.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note This function is non-blocking. If too much data is sent to the UART,
* some characters might be skipped.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_PRINTF
* - @ref NRF_LOG_PRINTF_DEBUG
* - @ref NRF_LOG_PRINTF_ERROR
*
* @param format_msg Printf format string.
*/
void log_raw_uart_printf(const char * format_msg, ...);
/**@brief Function for logging a single character to raw UART.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @param c Character.
*/
void log_raw_uart_write_char(const char c);
/**@brief Function for logging null-terminated strings to raw UART.
*
* @details This function is more efficient than using printf.
* The null termination will not be logged.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG
* - @ref NRF_LOG_DEBUG
* - @ref NRF_LOG_ERROR
*
* @param num_args Number of arguments.
*/
void log_raw_uart_write_string_many(int num_args, ...);
/**@brief Function for logging a null-terminated string to raw UART.
*
* @details This function is more efficient than using printf.
* The null termination will not be logged.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG
* - @ref NRF_LOG_DEBUG
* - @ref NRF_LOG_ERROR
*
* @param str Null-terminated string.
*/
void log_raw_uart_write_string(const char * str);
/**@brief Function for logging an integer value as HEX to raw UART.
*
* @details The output data is formatted as, for example, 0x89ABCDEF.
* This function is more efficient than printf.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note This function is non-blocking. If too much data is sent to the UART,
* some characters might be skipped.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_HEX
* - @ref NRF_LOG_HEX_DEBUG
* - @ref NRF_LOG_HEX_ERROR
*
* @param value Integer value to be printed as HEX.
*/
void log_raw_uart_write_hex(uint32_t value);
/**@brief Function for logging a single character as HEX to raw UART.
*
* @details The output string is formatted as, for example, AA.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note This function is non-blocking. If too much data is sent to the UART,
* some characters might be skipped.
*
* @note Do not call this function directly. Use one of the following macros instead:
* - @ref NRF_LOG_HEX_CHAR
* - @ref NRF_LOG_HEX_CHAR_DEBUG
* - @ref NRF_LOG_HEX_CHAR_ERROR
*
* @param c Character.
*/
void log_raw_uart_write_hex_char(uint8_t c);
/**@brief Function for checking if data is available in the input buffer.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note Do not call this function directly. Use @ref NRF_LOG_HAS_INPUT instead.
*
* @retval 1 If characters are available to read.
* @retval 0 If no characters are available.
*/
int log_raw_uart_has_input(void);
/**@brief Function for reading one character from the input buffer.
*
* @param[out] p_char Pointer where to store the character.
*
* This function is available only when NRF_LOG_USES_RAW_UART is defined as 1.
*
* @note Do not call this function directly. Use NRF_LOG_READ_INPUT instead.
*
* @retval NRF_SUCCESS If the character was read out.
* @retval NRF_ERROR_INVALID_DATA If no character could be read.
*/
uint32_t log_raw_uart_read_input(char* p_char);
#define NRF_LOG_INIT() log_raw_uart_init() /*!< nitialize the module. */
#define NRF_LOG_PRINTF(...) log_raw_uart_printf(__VA_ARGS__) /*!< Print a log message using printf. */
#define NRF_LOG_PRINTF_DEBUG(...) log_raw_uart_printf(__VA_ARGS__) /*!< If DEBUG is set, print a log message using printf. */
#define NRF_LOG_PRINTF_ERROR(...) log_raw_uart_printf(__VA_ARGS__) /*!< Print a log message using printf to the error stream. */
#define NRF_LOG(...) log_raw_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message. The input string must be null-terminated. */
#define NRF_LOG_DEBUG(...) log_raw_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< If DEBUG is set, print a log message. The input string must be null-terminated. */
#define NRF_LOG_ERROR(...) log_raw_uart_write_string_many(NUM_VA_ARGS(__VA_ARGS__), ##__VA_ARGS__) /*!< Print a log message to the error stream. The input string must be null-terminated. */
#define NRF_LOG_HEX(val) log_raw_uart_write_hex(val) /*!< Log an integer as HEX value (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_DEBUG(val) log_raw_uart_write_hex(val) /*!< If DEBUG is set, log an integer as HEX value (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_ERROR(val) log_raw_uart_write_hex(val) /*!< Log an integer as HEX value to the error stream (example output: 0x89ABCDEF). */
#define NRF_LOG_HEX_CHAR(val) log_raw_uart_write_hex_char(val) /*!< Log a character as HEX value (example output: AA). */
#define NRF_LOG_HEX_CHAR_DEBUG(val) log_raw_uart_write_hex_char(val) /*!< If DEBUG is set, log a character as HEX value (example output: AA). */
#define NRF_LOG_HEX_CHAR_ERROR(val) log_raw_uart_write_hex_char(val) /*!< Log a character as HEX value to the error stream (example output: AA). */
#define NRF_LOG_HAS_INPUT() log_raw_uart_has_input() /*!< Check if the input buffer has unconsumed characters. */
#define NRF_LOG_READ_INPUT(p_char) log_raw_uart_read_input(p_char) /*!< Consume a character from the input buffer. */
#if !defined(DEBUG) && !defined(DOXYGEN)
#undef NRF_LOG_DEBUG
#define NRF_LOG_DEBUG(...)
#undef NRF_LOG_PRINTF_DEBUG
#define NRF_LOG_PRINTF_DEBUG(...)
#undef NRF_LOG_STR_DEBUG
#define NRF_LOG_STR_DEBUG(...)
#undef NRF_LOG_HEX_DEBUG
#define NRF_LOG_HEX_DEBUG(...)
#undef NRF_LOG_HEX_CHAR_DEBUG
#define NRF_LOG_HEX_CHAR_DEBUG(...)
#endif // !defined(DEBUG) && !defined(DOXYGEN)
#else
#include "nrf_error.h"
#include "nordic_common.h"
// Empty definitions
#define NRF_LOG_INIT() NRF_SUCCESS
#define NRF_LOG(...)
#define NRF_LOG_DEBUG(...)
#define NRF_LOG_ERROR(...)
#define NRF_LOG_PRINTF(...)
#define NRF_LOG_PRINTF_DEBUG(...)
#define NRF_LOG_PRINTF_ERROR(...)
#define NRF_LOG_HEX(val)
#define NRF_LOG_HEX_DEBUG(val)
#define NRF_LOG_HEX_ERROR(val)
#define NRF_LOG_HEX_CHAR(val)
#define NRF_LOG_HEX_CHAR_DEBUG(val)
#define NRF_LOG_HEX_CHAR_ERROR(val)
#define NRF_LOG_HAS_INPUT() 0
#define NRF_LOG_READ_INPUT(ignore) NRF_SUCCESS
#endif
/**@brief Function for writing HEX values.
*
* @note This function not thread-safe. It is written for convenience.
* If you log from different application contexts, you might get different results.
*
* @retval NULL By default.
*/
const char* log_hex(uint32_t value);
/**@brief Function for writing HEX characters.
*
* @note This function not thread-safe. It is written for convenience.
* If you log from different application contexts, you might get different results.
*
* @retval NULL By default.
*/
const char* log_hex_char(const char value);
#else // DOXYGEN
/** @defgroup nrf_log UART/RTT logging
* @{
* @ingroup app_common
*
* @brief Library to output logging information over SEGGER's Real Time Transfer
* (RTT), UART, or raw UART.
*
* This library provides macros that call the respective functions depending on
* which protocol is used. Define LOG_USES_RTT=1 to enable logging over RTT,
* NRF_LOG_USES_UART=1 to enable logging over UART, or NRF_LOG_USES_RAW_UART=1
* to enable logging over raw UART. One of these defines must be set for any of
* the macros to have effect. If you choose to not output information, all
* logging macros can be left in the code without any cost; they will just be
* ignored.
*/
/**@brief Macro for initializing the logger.
*
* @retval NRF_SUCCESS If initialization was successful.
* @retval NRF_ERROR Otherwise.
*/
uint32_t NRF_LOG_INIT(void);
/**@brief Macro for logging null-terminated strings.
*
* @details This function is more efficient than using printf.
* The null termination will not be logged.
*
* @param msg Null-terminated string.
*/
void NRF_LOG(const char* msg);
/**@brief Macro for logging a printf string.
*
* @details Printf requires more processor time
* than other logging functions. Therefore, applications that require logging
* but need it to interfere as little as possible with the execution, should
* avoid using printf.
*
* @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
* If too much data is sent, some characters might be skipped.
*
* @param format_msg Printf format string.
* @param ... Additional arguments replacing format specifiers in format_msg.
*/
void NRF_LOG_PRINTF(const char * format_msg, ...);
/**@brief Macro for logging an integer value as HEX.
*
* @details The output data is formatted as, for example, 0x89ABCDEF.
* This function is more efficient than printf.
*
* @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
* If too much data is sent, some characters might be skipped.
*
* @param value Integer value to be printed as HEX.
*/
void NRF_LOG_HEX(uint32_t value);
/**@brief Macro for logging a single character as HEX.
*
* @details The output string is formatted as, for example, AA.
*
* @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
* If too much data is sent, some characters might be skipped.
*
* @param c Character.
*/
void NRF_LOG_HEX_CHAR(uint8_t c);
/**@brief Macro for checking if data is available in the input buffer.
*
* @note When NRF_LOG_USES_UART is set to 1, this macro is non-blocking.
* If too much data is sent, some characters might be skipped.
*
* @retval 1 If characters are available to read.
* @retval 0 If no characters are available.
*/
int NRF_LOG_HAS_INPUT(void);
/**@brief Macro for reading one character from the input buffer.
*
* @param[out] p_char Pointer where to store the character.
*
* @retval NRF_SUCCESS If the character was read out.
* @retval NRF_ERROR_INVALID_DATA If no character could be read.
*/
uint32_t NRF_LOG_READ_INPUT(char* p_char);
/** @} */
#endif // DOXYGEN
#endif // NRF_LOG_H_

1
lib/sdk11/readme.md Normal file
View File

@ -0,0 +1 @@
This bootloader is based on the non-secure one in the SDK version 11.0.0_89a8197. It still has some dependency/code that is dropped in the later sdk