テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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