nochanges

Dependents:   BLE_Acceleration_Statejudging

Fork of nRF51822 by Nordic Semiconductor

Committer:
MonroeLee
Date:
Thu Jun 09 05:15:47 2016 +0000
Revision:
638:6efcf74f000d
Parent:
37:c29c330d942c
no changes

Who changed what in which revision?

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