BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:a607cd9655d7 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
yihui 1:a607cd9655d7 2 *
yihui 1:a607cd9655d7 3 * The information contained herein is property of Nordic Semiconductor ASA.
yihui 1:a607cd9655d7 4 * Terms and conditions of usage are described in detail in NORDIC
yihui 1:a607cd9655d7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
yihui 1:a607cd9655d7 6 *
yihui 1:a607cd9655d7 7 * Licensees are granted free, non-transferable use of the information. NO
yihui 1:a607cd9655d7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
yihui 1:a607cd9655d7 9 * the file.
yihui 1:a607cd9655d7 10 *
yihui 1:a607cd9655d7 11 */
yihui 1:a607cd9655d7 12
yihui 1:a607cd9655d7 13 /**@file
yihui 1:a607cd9655d7 14 *
yihui 1:a607cd9655d7 15 * @defgroup ble_sdk_srv_dfu Device Firmware Update Service
yihui 1:a607cd9655d7 16 * @{
yihui 1:a607cd9655d7 17 * @ingroup ble_sdk_srv
yihui 1:a607cd9655d7 18 * @brief Device Firmware Update Service
yihui 1:a607cd9655d7 19 *
yihui 1:a607cd9655d7 20 * @details The Device Firmware Update (DFU) service is a GATT based service that can be used for
yihui 1:a607cd9655d7 21 * performing firmware updates over BLE. Note that this implementation uses vendor
yihui 1:a607cd9655d7 22 * specific UUIDs for service and characteristics and is intended to demonstrate the
yihui 1:a607cd9655d7 23 * firmware updates over BLE. Refer @ref dfu_ble_service_spec and @ref
yihui 1:a607cd9655d7 24 * dfu_ble_profile_spec for more information on the service and profile respectively.
yihui 1:a607cd9655d7 25 */
yihui 1:a607cd9655d7 26
yihui 1:a607cd9655d7 27 #ifndef BLE_DFU_H__
yihui 1:a607cd9655d7 28 #define BLE_DFU_H__
yihui 1:a607cd9655d7 29
yihui 1:a607cd9655d7 30 #include <stdint.h>
yihui 1:a607cd9655d7 31 #include "ble_gatts.h"
yihui 1:a607cd9655d7 32 #include "ble.h"
yihui 1:a607cd9655d7 33 #include "ble_srv_common.h"
yihui 1:a607cd9655d7 34
yihui 1:a607cd9655d7 35 #define BLE_DFU_SERVICE_UUID 0x1530 /**< The UUID of the DFU Service. */
yihui 1:a607cd9655d7 36 #define BLE_DFU_PKT_CHAR_UUID 0x1532 /**< The UUID of the DFU Packet Characteristic. */
yihui 1:a607cd9655d7 37 #define BLE_DFU_CTRL_PT_UUID 0x1531 /**< The UUID of the DFU Control Point. */
yihui 1:a607cd9655d7 38 #define BLE_DFU_STATUS_REP_UUID 0x1533 /**< The UUID of the DFU Status Report Characteristic. */
yihui 1:a607cd9655d7 39
yihui 1:a607cd9655d7 40
yihui 1:a607cd9655d7 41 /**@brief DFU Event type.
yihui 1:a607cd9655d7 42 *
yihui 1:a607cd9655d7 43 * @details This enumeration contains the types of events that will be received from the DFU Service.
yihui 1:a607cd9655d7 44 */
yihui 1:a607cd9655d7 45 typedef enum
yihui 1:a607cd9655d7 46 {
yihui 1:a607cd9655d7 47 BLE_DFU_START, /**< The event indicating that the peer wants the application to prepare for a new firmware update. */
yihui 1:a607cd9655d7 48 BLE_DFU_RECEIVE_INIT_DATA, /**< The event indicating that the peer wants the application to prepare to receive init parameters. */
yihui 1:a607cd9655d7 49 BLE_DFU_RECEIVE_APP_DATA, /**< The event indicating that the peer wants the application to prepare to receive the new firmware image. */
yihui 1:a607cd9655d7 50 BLE_DFU_VALIDATE, /**< The event indicating that the peer wants the application to validate the newly received firmware image. */
yihui 1:a607cd9655d7 51 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 */
yihui 1:a607cd9655d7 52 BLE_DFU_SYS_RESET, /**< The event indicating that the peer wants the application to undergo a reset and start the currently valid application image.*/
yihui 1:a607cd9655d7 53 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.*/
yihui 1:a607cd9655d7 54 BLE_DFU_PKT_RCPT_NOTIF_DISABLED, /**< The event indicating that the peer has disabled the packet receipt notifications.*/
yihui 1:a607cd9655d7 55 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_pkt_write element contained within @ref ble_dfu_evt_t.*/
yihui 1:a607cd9655d7 56 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. */
yihui 1:a607cd9655d7 57 } ble_dfu_evt_type_t;
yihui 1:a607cd9655d7 58
yihui 1:a607cd9655d7 59 /**@brief DFU Procedure type.
yihui 1:a607cd9655d7 60 *
yihui 1:a607cd9655d7 61 * @details This enumeration contains the types of DFU procedures.
yihui 1:a607cd9655d7 62 */
yihui 1:a607cd9655d7 63 typedef enum
yihui 1:a607cd9655d7 64 {
yihui 1:a607cd9655d7 65 BLE_DFU_START_PROCEDURE = 1, /**< DFU Start procedure.*/
yihui 1:a607cd9655d7 66 BLE_DFU_INIT_PROCEDURE = 2, /**< DFU Initialization procedure.*/
yihui 1:a607cd9655d7 67 BLE_DFU_RECEIVE_APP_PROCEDURE = 3, /**< Firmware receiving procedure.*/
yihui 1:a607cd9655d7 68 BLE_DFU_VALIDATE_PROCEDURE = 4, /**< Firmware image validation procedure .*/
yihui 1:a607cd9655d7 69 BLE_DFU_PKT_RCPT_REQ_PROCEDURE = 8 /**< Packet receipt notification request procedure. */
yihui 1:a607cd9655d7 70 } ble_dfu_procedure_t;
yihui 1:a607cd9655d7 71
yihui 1:a607cd9655d7 72 /**@brief DFU Response value type.
yihui 1:a607cd9655d7 73 */
yihui 1:a607cd9655d7 74 typedef enum
yihui 1:a607cd9655d7 75 {
yihui 1:a607cd9655d7 76 BLE_DFU_RESP_VAL_SUCCESS = 1, /**< Success.*/
yihui 1:a607cd9655d7 77 BLE_DFU_RESP_VAL_INVALID_STATE, /**< Invalid state.*/
yihui 1:a607cd9655d7 78 BLE_DFU_RESP_VAL_NOT_SUPPORTED, /**< Operation not supported.*/
yihui 1:a607cd9655d7 79 BLE_DFU_RESP_VAL_DATA_SIZE, /**< Data size exceeds limit.*/
yihui 1:a607cd9655d7 80 BLE_DFU_RESP_VAL_CRC_ERROR, /**< CRC Error.*/
yihui 1:a607cd9655d7 81 BLE_DFU_RESP_VAL_OPER_FAILED /**< Operation failed.*/
yihui 1:a607cd9655d7 82 } ble_dfu_resp_val_t;
yihui 1:a607cd9655d7 83
yihui 1:a607cd9655d7 84 /**@brief DFU Packet structure.
yihui 1:a607cd9655d7 85 *
yihui 1:a607cd9655d7 86 * @details This structure contains the value of the DFU Packet characteristic as written by the
yihui 1:a607cd9655d7 87 * peer and the length of the value written. It will be filled by the DFU Service when the
yihui 1:a607cd9655d7 88 * peer writes to the DFU Packet characteristic.
yihui 1:a607cd9655d7 89 */
yihui 1:a607cd9655d7 90 typedef struct
yihui 1:a607cd9655d7 91 {
yihui 1:a607cd9655d7 92 uint8_t len; /**< Length of the packet received. */
yihui 1:a607cd9655d7 93 uint8_t * p_data; /**< Pointer to the received packet. This will point to a word aligned memory location.*/
yihui 1:a607cd9655d7 94 } ble_dfu_pkt_write_t;
yihui 1:a607cd9655d7 95
yihui 1:a607cd9655d7 96 /**@brief Packet receipt notification request structure.
yihui 1:a607cd9655d7 97 *
yihui 1:a607cd9655d7 98 * @details This structure contains the contents of the packet receipt notification request
yihui 1:a607cd9655d7 99 * sent by the DFU Controller.
yihui 1:a607cd9655d7 100 */
yihui 1:a607cd9655d7 101 typedef struct
yihui 1:a607cd9655d7 102 {
yihui 1:a607cd9655d7 103 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. */
yihui 1:a607cd9655d7 104 } ble_pkt_rcpt_notif_req_t;
yihui 1:a607cd9655d7 105
yihui 1:a607cd9655d7 106 /**@brief DFU Event structure.
yihui 1:a607cd9655d7 107 *
yihui 1:a607cd9655d7 108 * @details This structure contains the event generated by the DFU Service based on the data
yihui 1:a607cd9655d7 109 * received from the peer.
yihui 1:a607cd9655d7 110 */
yihui 1:a607cd9655d7 111 typedef struct
yihui 1:a607cd9655d7 112 {
yihui 1:a607cd9655d7 113 ble_dfu_evt_type_t ble_dfu_evt_type; /**< Type of the event.*/
yihui 1:a607cd9655d7 114 union
yihui 1:a607cd9655d7 115 {
yihui 1:a607cd9655d7 116 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.*/
yihui 1:a607cd9655d7 117 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.*/
yihui 1:a607cd9655d7 118 } evt;
yihui 1:a607cd9655d7 119 } ble_dfu_evt_t;
yihui 1:a607cd9655d7 120
yihui 1:a607cd9655d7 121 // Forward declaration of the ble_dfu_t type.
yihui 1:a607cd9655d7 122 typedef struct ble_dfu_s ble_dfu_t;
yihui 1:a607cd9655d7 123
yihui 1:a607cd9655d7 124 /**@brief DFU Service event handler type. */
yihui 1:a607cd9655d7 125 typedef void (*ble_dfu_evt_handler_t) (ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt);
yihui 1:a607cd9655d7 126
yihui 1:a607cd9655d7 127 /**@brief DFU service structure.
yihui 1:a607cd9655d7 128 *
yihui 1:a607cd9655d7 129 * @details This structure contains status information related to the service.
yihui 1:a607cd9655d7 130 */
yihui 1:a607cd9655d7 131 typedef struct ble_dfu_s
yihui 1:a607cd9655d7 132 {
yihui 1:a607cd9655d7 133 uint16_t conn_handle; /**< Handle of the current connection (as provided by the S110 SoftDevice). This will be BLE_CONN_HANDLE_INVALID when not in a connection. */
yihui 1:a607cd9655d7 134 uint16_t service_handle; /**< Handle of DFU Service (as provided by the S110 SoftDevice). */
yihui 1:a607cd9655d7 135 uint8_t uuid_type; /**< UUID type assigned for DFU Service by the S110 SoftDevice. */
yihui 1:a607cd9655d7 136 ble_gatts_char_handles_t dfu_pkt_handles; /**< Handles related to the DFU Packet characteristic. */
yihui 1:a607cd9655d7 137 ble_gatts_char_handles_t dfu_ctrl_pt_handles; /**< Handles related to the DFU Control Point characteristic. */
yihui 1:a607cd9655d7 138 ble_gatts_char_handles_t dfu_status_rep_handles; /**< Handles related to the DFU Status Report characteristic. */
yihui 1:a607cd9655d7 139 ble_dfu_evt_handler_t evt_handler; /**< The event handler to be called when an event is to be sent to the application.*/
yihui 1:a607cd9655d7 140 ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
yihui 1:a607cd9655d7 141 } ble_dfu_t;
yihui 1:a607cd9655d7 142
yihui 1:a607cd9655d7 143 /**@brief DFU service initialization structure.
yihui 1:a607cd9655d7 144 *
yihui 1:a607cd9655d7 145 * @details This structure contains the initialization information for the DFU Service. The
yihui 1:a607cd9655d7 146 * application needs to fill this structure and pass it to the DFU Service using the
yihui 1:a607cd9655d7 147 * @ref ble_dfu_init function.
yihui 1:a607cd9655d7 148 */
yihui 1:a607cd9655d7 149 typedef struct
yihui 1:a607cd9655d7 150 {
yihui 1:a607cd9655d7 151 ble_dfu_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Device Firmware Update Service. */
yihui 1:a607cd9655d7 152 ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
yihui 1:a607cd9655d7 153 } ble_dfu_init_t;
yihui 1:a607cd9655d7 154
yihui 1:a607cd9655d7 155 /**@brief Function for handling a BLE event.
yihui 1:a607cd9655d7 156 *
yihui 1:a607cd9655d7 157 * @details The DFU service expects the application to call this function each time an event
yihui 1:a607cd9655d7 158 * is received from the S110 SoftDevice. This function processes the event, if it is
yihui 1:a607cd9655d7 159 * relevant for the DFU service and calls the DFU event handler of the application if
yihui 1:a607cd9655d7 160 * necessary.
yihui 1:a607cd9655d7 161 *
yihui 1:a607cd9655d7 162 * @param[in] p_dfu Pointer to the DFU service structure.
yihui 1:a607cd9655d7 163 * @param[in] p_ble_evt Pointer to the event received from S110 SoftDevice.
yihui 1:a607cd9655d7 164 */
yihui 1:a607cd9655d7 165 void ble_dfu_on_ble_evt(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt);
yihui 1:a607cd9655d7 166
yihui 1:a607cd9655d7 167 /**@brief Function for initializing the DFU service.
yihui 1:a607cd9655d7 168 *
yihui 1:a607cd9655d7 169 * @param[out] p_dfu Device Firmware Update service structure. This structure will have to be
yihui 1:a607cd9655d7 170 * supplied by the application. It will be initialized by this function,
yihui 1:a607cd9655d7 171 * and will later be used to identify the service instance.
yihui 1:a607cd9655d7 172 * @param[in] p_dfu_init Information needed to initialize the service.
yihui 1:a607cd9655d7 173 *
yihui 1:a607cd9655d7 174 * @return NRF_SUCCESS if the DFU service and its characteristics were successfully added to the
yihui 1:a607cd9655d7 175 * S110 SoftDevice. Otherwise an error code.
yihui 1:a607cd9655d7 176 * This function returns NRF_ERROR_NULL if the value of evt_handler in p_dfu_init
yihui 1:a607cd9655d7 177 * structure provided is NULL or if the pointers supplied as input are NULL.
yihui 1:a607cd9655d7 178 */
yihui 1:a607cd9655d7 179 uint32_t ble_dfu_init(ble_dfu_t * p_dfu, ble_dfu_init_t * p_dfu_init);
yihui 1:a607cd9655d7 180
yihui 1:a607cd9655d7 181 /**@brief Function for sending response to a control point command.
yihui 1:a607cd9655d7 182 *
yihui 1:a607cd9655d7 183 * @details This function will encode a DFU Control Point response using the given input
yihui 1:a607cd9655d7 184 * parameters and will send a notification of the same to the peer.
yihui 1:a607cd9655d7 185 *
yihui 1:a607cd9655d7 186 * @param[in] p_dfu Pointer to the DFU service structure.
yihui 1:a607cd9655d7 187 * @param[in] dfu_proc Procedure for which this response is to be sent.
yihui 1:a607cd9655d7 188 * @param[in] resp_val Response value.
yihui 1:a607cd9655d7 189 *
yihui 1:a607cd9655d7 190 * @return NRF_SUCCESS if the DFU Service has successfully requested the S110 SoftDevice to
yihui 1:a607cd9655d7 191 * send the notification. Otherwise an error code.
yihui 1:a607cd9655d7 192 * This function returns NRF_ERROR_INVALID_STATE if the device is not connected to a
yihui 1:a607cd9655d7 193 * peer or if the DFU service is not initialized or if the notification of the DFU
yihui 1:a607cd9655d7 194 * Status Report characteristic was not enabled by the peer. It returns NRF_ERROR_NULL
yihui 1:a607cd9655d7 195 * if the pointer p_dfu is NULL.
yihui 1:a607cd9655d7 196 */
yihui 1:a607cd9655d7 197 uint32_t ble_dfu_response_send(ble_dfu_t * p_dfu,
yihui 1:a607cd9655d7 198 ble_dfu_procedure_t dfu_proc,
yihui 1:a607cd9655d7 199 ble_dfu_resp_val_t resp_val);
yihui 1:a607cd9655d7 200
yihui 1:a607cd9655d7 201 /**@brief Function for notifying the peer about the number of bytes of firmware data received.
yihui 1:a607cd9655d7 202 *
yihui 1:a607cd9655d7 203 * @param[in] p_dfu Pointer to the DFU service structure.
yihui 1:a607cd9655d7 204 * @param[in] num_of_firmware_bytes_rcvd Number of bytes.
yihui 1:a607cd9655d7 205 *
yihui 1:a607cd9655d7 206 * @return NRF_SUCCESS if the DFU Service has successfully requested the S110 SoftDevice to send
yihui 1:a607cd9655d7 207 * the notification. Otherwise an error code.
yihui 1:a607cd9655d7 208 * This function returns NRF_ERROR_INVALID_STATE if the device is not connected to a
yihui 1:a607cd9655d7 209 * peer or if the DFU service is not initialized or if the notification of the DFU
yihui 1:a607cd9655d7 210 * Status Report characteristic was not enabled by the peer. It returns NRF_ERROR_NULL
yihui 1:a607cd9655d7 211 * if the pointer p_dfu is NULL.
yihui 1:a607cd9655d7 212 */
yihui 1:a607cd9655d7 213 uint32_t ble_dfu_bytes_rcvd_report(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd);
yihui 1:a607cd9655d7 214
yihui 1:a607cd9655d7 215 /**@brief Function for sending Packet Receipt Notification to the peer.
yihui 1:a607cd9655d7 216 *
yihui 1:a607cd9655d7 217 * This function will encode the number of bytes received as input parameter into a
yihui 1:a607cd9655d7 218 * notification of the control point characteristic and send it to the peer.
yihui 1:a607cd9655d7 219 *
yihui 1:a607cd9655d7 220 * @param[in] p_dfu Pointer to the DFU service structure.
yihui 1:a607cd9655d7 221 * @param[in] num_of_firmware_bytes_rcvd Number of bytes of firmware image received.
yihui 1:a607cd9655d7 222 *
yihui 1:a607cd9655d7 223 * @return NRF_SUCCESS if the DFU Service has successfully requested the S110 SoftDevice to send
yihui 1:a607cd9655d7 224 * the notification. Otherwise an error code.
yihui 1:a607cd9655d7 225 * This function returns NRF_ERROR_INVALID_STATE if the device is not connected to a
yihui 1:a607cd9655d7 226 * peer or if the DFU service is not initialized or if the notification of the DFU
yihui 1:a607cd9655d7 227 * Status Report characteristic was not enabled by the peer. It returns NRF_ERROR_NULL
yihui 1:a607cd9655d7 228 * if the pointer p_dfu is NULL.
yihui 1:a607cd9655d7 229 */
yihui 1:a607cd9655d7 230 uint32_t ble_dfu_pkts_rcpt_notify(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd);
yihui 1:a607cd9655d7 231
yihui 1:a607cd9655d7 232 #endif // BLE_DFU_H__
yihui 1:a607cd9655d7 233
yihui 1:a607cd9655d7 234 /** @} */