Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.
source/nordic_sdk/components/ble/ble_services/ble_dfu/ble_dfu.c@28:041dac1366b2, 2016-04-07 (annotated)
- Committer:
- vcoubard
- Date:
- Thu Apr 07 17:37:56 2016 +0100
- Revision:
- 28:041dac1366b2
- Parent:
- 20:a90c48eb1d30
- Child:
- 29:286940b7ee5a
Synchronized with git rev 012b8118
Author: Liyou Zhou
Pull in files from sdk 10.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 28:041dac1366b2 | 1 | /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved. |
vcoubard | 28:041dac1366b2 | 2 | * |
vcoubard | 28:041dac1366b2 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
vcoubard | 28:041dac1366b2 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
vcoubard | 28:041dac1366b2 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
vcoubard | 28:041dac1366b2 | 6 | * |
vcoubard | 28:041dac1366b2 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
vcoubard | 28:041dac1366b2 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
vcoubard | 28:041dac1366b2 | 9 | * the file. |
vcoubard | 28:041dac1366b2 | 10 | * |
Vincent Coubard |
0:f2542974c862 | 11 | */ |
Vincent Coubard |
0:f2542974c862 | 12 | |
Vincent Coubard |
0:f2542974c862 | 13 | #include "ble_dfu.h" |
Vincent Coubard |
0:f2542974c862 | 14 | #include "nrf_error.h" |
Vincent Coubard |
0:f2542974c862 | 15 | #include "ble_types.h" |
Vincent Coubard |
0:f2542974c862 | 16 | #include "ble_gatts.h" |
Vincent Coubard |
0:f2542974c862 | 17 | #include "app_util.h" |
Vincent Coubard |
0:f2542974c862 | 18 | #include "ble_srv_common.h" |
Vincent Coubard |
0:f2542974c862 | 19 | #include "nordic_common.h" |
Vincent Coubard |
0:f2542974c862 | 20 | #include <stdint.h> |
Vincent Coubard |
0:f2542974c862 | 21 | #include <string.h> |
Vincent Coubard |
0:f2542974c862 | 22 | #include <stddef.h> |
Vincent Coubard |
0:f2542974c862 | 23 | |
Vincent Coubard |
0:f2542974c862 | 24 | #define MAX_DFU_PKT_LEN 20 /**< Maximum length (in bytes) of the DFU Packet characteristic. */ |
Vincent Coubard |
0:f2542974c862 | 25 | #define PKT_START_DFU_PARAM_LEN 2 /**< Length (in bytes) of the parameters for Packet Start DFU Request. */ |
Vincent Coubard |
0:f2542974c862 | 26 | #define PKT_INIT_DFU_PARAM_LEN 2 /**< Length (in bytes) of the parameters for Packet Init DFU Request. */ |
Vincent Coubard |
0:f2542974c862 | 27 | #define PKT_RCPT_NOTIF_REQ_LEN 3 /**< Length (in bytes) of the Packet Receipt Notification Request. */ |
Vincent Coubard |
0:f2542974c862 | 28 | #define MAX_PKTS_RCPT_NOTIF_LEN 6 /**< Maximum length (in bytes) of the Packets Receipt Notification. */ |
Vincent Coubard |
0:f2542974c862 | 29 | #define MAX_RESPONSE_LEN 7 /**< Maximum length (in bytes) of the response to a Control Point command. */ |
Vincent Coubard |
0:f2542974c862 | 30 | #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. */ |
Vincent Coubard |
0:f2542974c862 | 31 | |
Vincent Coubard |
0:f2542974c862 | 32 | enum |
Vincent Coubard |
0:f2542974c862 | 33 | { |
Vincent Coubard |
0:f2542974c862 | 34 | OP_CODE_START_DFU = 1, /**< Value of the Op code field for 'Start DFU' command.*/ |
Vincent Coubard |
0:f2542974c862 | 35 | OP_CODE_RECEIVE_INIT = 2, /**< Value of the Op code field for 'Initialize DFU parameters' command.*/ |
Vincent Coubard |
0:f2542974c862 | 36 | OP_CODE_RECEIVE_FW = 3, /**< Value of the Op code field for 'Receive firmware image' command.*/ |
Vincent Coubard |
0:f2542974c862 | 37 | OP_CODE_VALIDATE = 4, /**< Value of the Op code field for 'Validate firmware' command.*/ |
Vincent Coubard |
0:f2542974c862 | 38 | OP_CODE_ACTIVATE_N_RESET = 5, /**< Value of the Op code field for 'Activate & Reset' command.*/ |
Vincent Coubard |
0:f2542974c862 | 39 | OP_CODE_SYS_RESET = 6, /**< Value of the Op code field for 'Reset System' command.*/ |
Vincent Coubard |
0:f2542974c862 | 40 | OP_CODE_IMAGE_SIZE_REQ = 7, /**< Value of the Op code field for 'Report received image size' command.*/ |
Vincent Coubard |
0:f2542974c862 | 41 | OP_CODE_PKT_RCPT_NOTIF_REQ = 8, /**< Value of the Op code field for 'Request packet receipt notification.*/ |
Vincent Coubard |
0:f2542974c862 | 42 | OP_CODE_RESPONSE = 16, /**< Value of the Op code field for 'Response.*/ |
Vincent Coubard |
0:f2542974c862 | 43 | OP_CODE_PKT_RCPT_NOTIF = 17 /**< Value of the Op code field for 'Packets Receipt Notification'.*/ |
Vincent Coubard |
0:f2542974c862 | 44 | }; |
Vincent Coubard |
0:f2542974c862 | 45 | |
Vincent Coubard |
0:f2542974c862 | 46 | static bool m_is_dfu_service_initialized = false; /**< Variable to check if the DFU service was initialized by the application.*/ |
Vincent Coubard |
0:f2542974c862 | 47 | static uint8_t m_notif_buffer[MAX_NOTIF_BUFFER_LEN]; /**< Buffer used for sending notifications to peer. */ |
Vincent Coubard |
0:f2542974c862 | 48 | |
Vincent Coubard |
0:f2542974c862 | 49 | /**@brief Function for adding DFU Packet characteristic to the BLE Stack. |
Vincent Coubard |
0:f2542974c862 | 50 | * |
Vincent Coubard |
0:f2542974c862 | 51 | * @param[in] p_dfu DFU Service structure. |
Vincent Coubard |
0:f2542974c862 | 52 | * |
Vincent Coubard |
0:f2542974c862 | 53 | * @return NRF_SUCCESS on success. Otherwise an error code. |
Vincent Coubard |
0:f2542974c862 | 54 | */ |
Vincent Coubard |
0:f2542974c862 | 55 | static uint32_t dfu_pkt_char_add(ble_dfu_t * const p_dfu) |
Vincent Coubard |
0:f2542974c862 | 56 | { |
Vincent Coubard |
0:f2542974c862 | 57 | ble_gatts_char_md_t char_md; |
Vincent Coubard |
0:f2542974c862 | 58 | ble_gatts_attr_t attr_char_value; |
Vincent Coubard |
0:f2542974c862 | 59 | ble_uuid_t char_uuid; |
Vincent Coubard |
0:f2542974c862 | 60 | ble_gatts_attr_md_t attr_md; |
Vincent Coubard |
0:f2542974c862 | 61 | |
Vincent Coubard |
0:f2542974c862 | 62 | memset(&char_md, 0, sizeof(char_md)); |
Vincent Coubard |
0:f2542974c862 | 63 | |
Vincent Coubard |
0:f2542974c862 | 64 | char_md.char_props.write_wo_resp = 1; |
Vincent Coubard |
0:f2542974c862 | 65 | char_md.p_char_user_desc = NULL; |
Vincent Coubard |
0:f2542974c862 | 66 | char_md.p_char_pf = NULL; |
Vincent Coubard |
0:f2542974c862 | 67 | char_md.p_user_desc_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 68 | char_md.p_cccd_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 69 | char_md.p_sccd_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 70 | |
Vincent Coubard |
0:f2542974c862 | 71 | char_uuid.type = p_dfu->uuid_type; |
Vincent Coubard |
0:f2542974c862 | 72 | char_uuid.uuid = BLE_DFU_PKT_CHAR_UUID; |
Vincent Coubard |
0:f2542974c862 | 73 | |
Vincent Coubard |
0:f2542974c862 | 74 | memset(&attr_md, 0, sizeof(attr_md)); |
Vincent Coubard |
0:f2542974c862 | 75 | |
Vincent Coubard |
0:f2542974c862 | 76 | BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm); |
Vincent Coubard |
0:f2542974c862 | 77 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
Vincent Coubard |
0:f2542974c862 | 78 | |
Vincent Coubard |
0:f2542974c862 | 79 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
Vincent Coubard |
0:f2542974c862 | 80 | attr_md.rd_auth = 0; |
Vincent Coubard |
0:f2542974c862 | 81 | attr_md.wr_auth = 0; |
Vincent Coubard |
0:f2542974c862 | 82 | attr_md.vlen = 1; |
Vincent Coubard |
0:f2542974c862 | 83 | |
Vincent Coubard |
0:f2542974c862 | 84 | memset(&attr_char_value, 0, sizeof(attr_char_value)); |
Vincent Coubard |
0:f2542974c862 | 85 | |
Vincent Coubard |
0:f2542974c862 | 86 | attr_char_value.p_uuid = &char_uuid; |
Vincent Coubard |
0:f2542974c862 | 87 | attr_char_value.p_attr_md = &attr_md; |
Vincent Coubard |
0:f2542974c862 | 88 | attr_char_value.init_len = 0; |
Vincent Coubard |
0:f2542974c862 | 89 | attr_char_value.init_offs = 0; |
Vincent Coubard |
0:f2542974c862 | 90 | attr_char_value.max_len = MAX_DFU_PKT_LEN; |
Vincent Coubard |
0:f2542974c862 | 91 | attr_char_value.p_value = NULL; |
Vincent Coubard |
0:f2542974c862 | 92 | |
Vincent Coubard |
0:f2542974c862 | 93 | return sd_ble_gatts_characteristic_add(p_dfu->service_handle, |
Vincent Coubard |
0:f2542974c862 | 94 | &char_md, |
Vincent Coubard |
0:f2542974c862 | 95 | &attr_char_value, |
Vincent Coubard |
0:f2542974c862 | 96 | &p_dfu->dfu_pkt_handles); |
Vincent Coubard |
0:f2542974c862 | 97 | } |
Vincent Coubard |
0:f2542974c862 | 98 | |
Vincent Coubard |
0:f2542974c862 | 99 | |
Vincent Coubard |
0:f2542974c862 | 100 | /**@brief Function for adding DFU Revision characteristic to the BLE Stack. |
Vincent Coubard |
0:f2542974c862 | 101 | * |
Vincent Coubard |
0:f2542974c862 | 102 | * @param[in] p_dfu DFU Service structure. |
Vincent Coubard |
0:f2542974c862 | 103 | * |
Vincent Coubard |
0:f2542974c862 | 104 | * @return NRF_SUCCESS on success. Otherwise an error code. |
Vincent Coubard |
0:f2542974c862 | 105 | */ |
Vincent Coubard |
0:f2542974c862 | 106 | static uint32_t dfu_rev_char_add(ble_dfu_t * const p_dfu, ble_dfu_init_t const * const p_dfu_init) |
Vincent Coubard |
0:f2542974c862 | 107 | { |
Vincent Coubard |
0:f2542974c862 | 108 | ble_gatts_char_md_t char_md; |
Vincent Coubard |
0:f2542974c862 | 109 | ble_gatts_attr_t attr_char_value; |
Vincent Coubard |
0:f2542974c862 | 110 | ble_uuid_t char_uuid; |
Vincent Coubard |
0:f2542974c862 | 111 | ble_gatts_attr_md_t attr_md; |
Vincent Coubard |
0:f2542974c862 | 112 | |
Vincent Coubard |
0:f2542974c862 | 113 | memset(&char_md, 0, sizeof(char_md)); |
Vincent Coubard |
0:f2542974c862 | 114 | |
Vincent Coubard |
0:f2542974c862 | 115 | char_md.char_props.read = 1; |
Vincent Coubard |
0:f2542974c862 | 116 | char_md.p_char_user_desc = NULL; |
Vincent Coubard |
0:f2542974c862 | 117 | char_md.p_char_pf = NULL; |
Vincent Coubard |
0:f2542974c862 | 118 | char_md.p_user_desc_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 119 | char_md.p_cccd_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 120 | char_md.p_sccd_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 121 | |
Vincent Coubard |
0:f2542974c862 | 122 | char_uuid.type = p_dfu->uuid_type; |
Vincent Coubard |
0:f2542974c862 | 123 | char_uuid.uuid = BLE_DFU_REV_CHAR_UUID; |
Vincent Coubard |
0:f2542974c862 | 124 | |
Vincent Coubard |
0:f2542974c862 | 125 | memset(&attr_md, 0, sizeof(attr_md)); |
Vincent Coubard |
0:f2542974c862 | 126 | |
Vincent Coubard |
0:f2542974c862 | 127 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
Vincent Coubard |
0:f2542974c862 | 128 | BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); |
Vincent Coubard |
0:f2542974c862 | 129 | |
Vincent Coubard |
0:f2542974c862 | 130 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
Vincent Coubard |
0:f2542974c862 | 131 | attr_md.rd_auth = 0; |
Vincent Coubard |
0:f2542974c862 | 132 | attr_md.wr_auth = 0; |
Vincent Coubard |
0:f2542974c862 | 133 | attr_md.vlen = 1; |
Vincent Coubard |
0:f2542974c862 | 134 | |
Vincent Coubard |
0:f2542974c862 | 135 | memset(&attr_char_value, 0, sizeof(attr_char_value)); |
Vincent Coubard |
0:f2542974c862 | 136 | |
Vincent Coubard |
0:f2542974c862 | 137 | attr_char_value.p_uuid = &char_uuid; |
Vincent Coubard |
0:f2542974c862 | 138 | attr_char_value.p_attr_md = &attr_md; |
Vincent Coubard |
0:f2542974c862 | 139 | attr_char_value.init_len = sizeof(uint16_t); |
Vincent Coubard |
0:f2542974c862 | 140 | attr_char_value.init_offs = 0; |
Vincent Coubard |
0:f2542974c862 | 141 | attr_char_value.max_len = sizeof(uint16_t); |
Vincent Coubard |
0:f2542974c862 | 142 | attr_char_value.p_value = (uint8_t *)&p_dfu_init->revision; |
Vincent Coubard |
0:f2542974c862 | 143 | |
Vincent Coubard |
0:f2542974c862 | 144 | return sd_ble_gatts_characteristic_add(p_dfu->service_handle, |
Vincent Coubard |
0:f2542974c862 | 145 | &char_md, |
Vincent Coubard |
0:f2542974c862 | 146 | &attr_char_value, |
Vincent Coubard |
0:f2542974c862 | 147 | &p_dfu->dfu_rev_handles); |
Vincent Coubard |
0:f2542974c862 | 148 | } |
Vincent Coubard |
0:f2542974c862 | 149 | |
Vincent Coubard |
0:f2542974c862 | 150 | |
Vincent Coubard |
0:f2542974c862 | 151 | /**@brief Function for adding DFU Control Point characteristic to the BLE Stack. |
Vincent Coubard |
0:f2542974c862 | 152 | * |
Vincent Coubard |
0:f2542974c862 | 153 | * @param[in] p_dfu DFU Service structure. |
Vincent Coubard |
0:f2542974c862 | 154 | * |
Vincent Coubard |
0:f2542974c862 | 155 | * @return NRF_SUCCESS on success. Otherwise an error code. |
Vincent Coubard |
0:f2542974c862 | 156 | */ |
Vincent Coubard |
0:f2542974c862 | 157 | static uint32_t dfu_ctrl_pt_add(ble_dfu_t * const p_dfu) |
Vincent Coubard |
0:f2542974c862 | 158 | { |
Vincent Coubard |
0:f2542974c862 | 159 | ble_gatts_char_md_t char_md; |
Vincent Coubard |
0:f2542974c862 | 160 | ble_gatts_attr_t attr_char_value; |
Vincent Coubard |
0:f2542974c862 | 161 | ble_uuid_t char_uuid; |
Vincent Coubard |
0:f2542974c862 | 162 | ble_gatts_attr_md_t attr_md; |
Vincent Coubard |
0:f2542974c862 | 163 | |
Vincent Coubard |
0:f2542974c862 | 164 | memset(&char_md, 0, sizeof(char_md)); |
Vincent Coubard |
0:f2542974c862 | 165 | |
Vincent Coubard |
0:f2542974c862 | 166 | char_md.char_props.write = 1; |
Vincent Coubard |
0:f2542974c862 | 167 | char_md.char_props.notify = 1; |
Vincent Coubard |
0:f2542974c862 | 168 | char_md.p_char_user_desc = NULL; |
Vincent Coubard |
0:f2542974c862 | 169 | char_md.p_char_pf = NULL; |
Vincent Coubard |
0:f2542974c862 | 170 | char_md.p_user_desc_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 171 | char_md.p_cccd_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 172 | char_md.p_sccd_md = NULL; |
Vincent Coubard |
0:f2542974c862 | 173 | |
Vincent Coubard |
0:f2542974c862 | 174 | char_uuid.type = p_dfu->uuid_type; |
Vincent Coubard |
0:f2542974c862 | 175 | char_uuid.uuid = BLE_DFU_CTRL_PT_UUID; |
Vincent Coubard |
0:f2542974c862 | 176 | |
Vincent Coubard |
0:f2542974c862 | 177 | memset(&attr_md, 0, sizeof(attr_md)); |
Vincent Coubard |
0:f2542974c862 | 178 | |
Vincent Coubard |
0:f2542974c862 | 179 | BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.read_perm); |
Vincent Coubard |
0:f2542974c862 | 180 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
Vincent Coubard |
0:f2542974c862 | 181 | |
Vincent Coubard |
0:f2542974c862 | 182 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
Vincent Coubard |
0:f2542974c862 | 183 | attr_md.rd_auth = 0; |
Vincent Coubard |
0:f2542974c862 | 184 | attr_md.wr_auth = 1; |
Vincent Coubard |
0:f2542974c862 | 185 | attr_md.vlen = 1; |
Vincent Coubard |
0:f2542974c862 | 186 | |
Vincent Coubard |
0:f2542974c862 | 187 | memset(&attr_char_value, 0, sizeof(attr_char_value)); |
Vincent Coubard |
0:f2542974c862 | 188 | |
Vincent Coubard |
0:f2542974c862 | 189 | attr_char_value.p_uuid = &char_uuid; |
Vincent Coubard |
0:f2542974c862 | 190 | attr_char_value.p_attr_md = &attr_md; |
Vincent Coubard |
0:f2542974c862 | 191 | attr_char_value.init_len = 0; |
Vincent Coubard |
0:f2542974c862 | 192 | attr_char_value.init_offs = 0; |
Vincent Coubard |
0:f2542974c862 | 193 | attr_char_value.max_len = BLE_L2CAP_MTU_DEF; |
Vincent Coubard |
0:f2542974c862 | 194 | attr_char_value.p_value = NULL; |
Vincent Coubard |
0:f2542974c862 | 195 | |
Vincent Coubard |
0:f2542974c862 | 196 | return sd_ble_gatts_characteristic_add(p_dfu->service_handle, |
Vincent Coubard |
0:f2542974c862 | 197 | &char_md, |
Vincent Coubard |
0:f2542974c862 | 198 | &attr_char_value, |
Vincent Coubard |
0:f2542974c862 | 199 | &p_dfu->dfu_ctrl_pt_handles); |
Vincent Coubard |
0:f2542974c862 | 200 | } |
Vincent Coubard |
0:f2542974c862 | 201 | |
Vincent Coubard |
0:f2542974c862 | 202 | |
Vincent Coubard |
0:f2542974c862 | 203 | /**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the S110 SoftDevice. |
Vincent Coubard |
0:f2542974c862 | 204 | * |
Vincent Coubard |
0:f2542974c862 | 205 | * @param[in] p_dfu DFU Service Structure. |
Vincent Coubard |
0:f2542974c862 | 206 | * @param[in] p_ble_evt Pointer to the event received from BLE stack. |
Vincent Coubard |
0:f2542974c862 | 207 | */ |
Vincent Coubard |
0:f2542974c862 | 208 | static void on_connect(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) |
Vincent Coubard |
0:f2542974c862 | 209 | { |
Vincent Coubard |
0:f2542974c862 | 210 | p_dfu->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; |
Vincent Coubard |
0:f2542974c862 | 211 | } |
Vincent Coubard |
0:f2542974c862 | 212 | |
Vincent Coubard |
0:f2542974c862 | 213 | |
Vincent Coubard |
0:f2542974c862 | 214 | /**@brief Function for checking if the CCCD of DFU Control point is configured for Notification. |
Vincent Coubard |
0:f2542974c862 | 215 | * |
Vincent Coubard |
0:f2542974c862 | 216 | * @details This function checks if the CCCD of DFU Control Point characteristic is configured |
Vincent Coubard |
0:f2542974c862 | 217 | * for Notification by the DFU Controller. |
Vincent Coubard |
0:f2542974c862 | 218 | * |
Vincent Coubard |
0:f2542974c862 | 219 | * @param[in] p_dfu DFU Service structure. |
Vincent Coubard |
0:f2542974c862 | 220 | * |
Vincent Coubard |
0:f2542974c862 | 221 | * @return True if the CCCD of DFU Control Point characteristic is configured for Notification. |
Vincent Coubard |
0:f2542974c862 | 222 | * False otherwise. |
Vincent Coubard |
0:f2542974c862 | 223 | */ |
Vincent Coubard |
0:f2542974c862 | 224 | static bool is_cccd_configured(ble_dfu_t * p_dfu) |
Vincent Coubard |
0:f2542974c862 | 225 | { |
Vincent Coubard |
0:f2542974c862 | 226 | // Check if the CCCDs are configured. |
Vincent Coubard |
0:f2542974c862 | 227 | uint8_t cccd_val_buf[BLE_CCCD_VALUE_LEN]; |
Vincent Coubard |
0:f2542974c862 | 228 | ble_gatts_value_t gatts_value; |
Vincent Coubard |
0:f2542974c862 | 229 | |
Vincent Coubard |
0:f2542974c862 | 230 | // Initialize value struct. |
Vincent Coubard |
0:f2542974c862 | 231 | memset(&gatts_value, 0, sizeof(gatts_value)); |
Vincent Coubard |
0:f2542974c862 | 232 | |
Vincent Coubard |
0:f2542974c862 | 233 | gatts_value.len = BLE_CCCD_VALUE_LEN; |
Vincent Coubard |
0:f2542974c862 | 234 | gatts_value.offset = 0; |
Vincent Coubard |
0:f2542974c862 | 235 | gatts_value.p_value = cccd_val_buf; |
Vincent Coubard |
0:f2542974c862 | 236 | |
Vincent Coubard |
0:f2542974c862 | 237 | // Check the CCCD Value of DFU Control Point. |
Vincent Coubard |
0:f2542974c862 | 238 | uint32_t err_code = sd_ble_gatts_value_get(p_dfu->conn_handle, |
Vincent Coubard |
0:f2542974c862 | 239 | p_dfu->dfu_ctrl_pt_handles.cccd_handle, |
Vincent Coubard |
0:f2542974c862 | 240 | &gatts_value); |
Vincent Coubard |
0:f2542974c862 | 241 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 242 | { |
Vincent Coubard |
0:f2542974c862 | 243 | if (p_dfu->error_handler != NULL) |
Vincent Coubard |
0:f2542974c862 | 244 | { |
Vincent Coubard |
0:f2542974c862 | 245 | p_dfu->error_handler(err_code); |
Vincent Coubard |
0:f2542974c862 | 246 | } |
Vincent Coubard |
0:f2542974c862 | 247 | return false; |
Vincent Coubard |
0:f2542974c862 | 248 | } |
Vincent Coubard |
0:f2542974c862 | 249 | |
Vincent Coubard |
0:f2542974c862 | 250 | return ble_srv_is_notification_enabled(cccd_val_buf); |
Vincent Coubard |
0:f2542974c862 | 251 | } |
Vincent Coubard |
0:f2542974c862 | 252 | |
Vincent Coubard |
0:f2542974c862 | 253 | |
Vincent Coubard |
0:f2542974c862 | 254 | /**@brief Function for handling a Write event on the Control Point characteristic. |
Vincent Coubard |
0:f2542974c862 | 255 | * |
Vincent Coubard |
0:f2542974c862 | 256 | * @param[in] p_dfu DFU Service Structure. |
Vincent Coubard |
0:f2542974c862 | 257 | * @param[in] p_ble_write_evt Pointer to the write event received from BLE stack. |
Vincent Coubard |
0:f2542974c862 | 258 | * |
Vincent Coubard |
0:f2542974c862 | 259 | * @return NRF_SUCCESS on successful processing of control point write. Otherwise an error code. |
Vincent Coubard |
0:f2542974c862 | 260 | */ |
Vincent Coubard |
0:f2542974c862 | 261 | static uint32_t on_ctrl_pt_write(ble_dfu_t * p_dfu, ble_gatts_evt_write_t * p_ble_write_evt) |
Vincent Coubard |
0:f2542974c862 | 262 | { |
Vincent Coubard |
0:f2542974c862 | 263 | ble_gatts_rw_authorize_reply_params_t write_authorize_reply; |
Vincent Coubard |
0:f2542974c862 | 264 | |
Vincent Coubard |
0:f2542974c862 | 265 | write_authorize_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE; |
Vincent Coubard |
0:f2542974c862 | 266 | |
Vincent Coubard |
0:f2542974c862 | 267 | if (!is_cccd_configured(p_dfu)) |
Vincent Coubard |
0:f2542974c862 | 268 | { |
Vincent Coubard |
0:f2542974c862 | 269 | // Send an error response to the peer indicating that the CCCD is improperly configured. |
Vincent Coubard |
0:f2542974c862 | 270 | write_authorize_reply.params.write.gatt_status = |
Vincent Coubard |
0:f2542974c862 | 271 | BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR; |
Vincent Coubard |
0:f2542974c862 | 272 | |
Vincent Coubard |
0:f2542974c862 | 273 | return (sd_ble_gatts_rw_authorize_reply(p_dfu->conn_handle, &write_authorize_reply)); |
Vincent Coubard |
0:f2542974c862 | 274 | |
Vincent Coubard |
0:f2542974c862 | 275 | } |
Vincent Coubard |
0:f2542974c862 | 276 | else |
Vincent Coubard |
0:f2542974c862 | 277 | { |
Vincent Coubard |
0:f2542974c862 | 278 | uint32_t err_code; |
Vincent Coubard |
0:f2542974c862 | 279 | |
Vincent Coubard |
0:f2542974c862 | 280 | write_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS; |
Vincent Coubard |
0:f2542974c862 | 281 | |
Vincent Coubard |
0:f2542974c862 | 282 | err_code = (sd_ble_gatts_rw_authorize_reply(p_dfu->conn_handle, &write_authorize_reply)); |
Vincent Coubard |
0:f2542974c862 | 283 | |
Vincent Coubard |
0:f2542974c862 | 284 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 285 | { |
Vincent Coubard |
0:f2542974c862 | 286 | return err_code; |
Vincent Coubard |
0:f2542974c862 | 287 | } |
Vincent Coubard |
0:f2542974c862 | 288 | } |
Vincent Coubard |
0:f2542974c862 | 289 | |
Vincent Coubard |
0:f2542974c862 | 290 | ble_dfu_evt_t ble_dfu_evt; |
Vincent Coubard |
0:f2542974c862 | 291 | |
Vincent Coubard |
0:f2542974c862 | 292 | switch (p_ble_write_evt->data[0]) |
Vincent Coubard |
0:f2542974c862 | 293 | { |
Vincent Coubard |
0:f2542974c862 | 294 | case OP_CODE_START_DFU: |
Vincent Coubard |
0:f2542974c862 | 295 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_START; |
Vincent Coubard |
0:f2542974c862 | 296 | |
Vincent Coubard |
0:f2542974c862 | 297 | if (p_ble_write_evt->len < PKT_START_DFU_PARAM_LEN) |
Vincent Coubard |
0:f2542974c862 | 298 | { |
Vincent Coubard |
0:f2542974c862 | 299 | return ble_dfu_response_send(p_dfu, |
Vincent Coubard |
0:f2542974c862 | 300 | (ble_dfu_procedure_t) p_ble_write_evt->data[0], |
Vincent Coubard |
0:f2542974c862 | 301 | BLE_DFU_RESP_VAL_OPER_FAILED); |
Vincent Coubard |
0:f2542974c862 | 302 | } |
Vincent Coubard |
0:f2542974c862 | 303 | |
Vincent Coubard |
0:f2542974c862 | 304 | ble_dfu_evt.evt.ble_dfu_pkt_write.len = 1; |
Vincent Coubard |
0:f2542974c862 | 305 | ble_dfu_evt.evt.ble_dfu_pkt_write.p_data = &(p_ble_write_evt->data[1]); |
Vincent Coubard |
0:f2542974c862 | 306 | |
Vincent Coubard |
0:f2542974c862 | 307 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 308 | break; |
Vincent Coubard |
0:f2542974c862 | 309 | |
Vincent Coubard |
0:f2542974c862 | 310 | case OP_CODE_RECEIVE_INIT: |
Vincent Coubard |
0:f2542974c862 | 311 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_RECEIVE_INIT_DATA; |
Vincent Coubard |
0:f2542974c862 | 312 | |
Vincent Coubard |
0:f2542974c862 | 313 | if (p_ble_write_evt->len < PKT_INIT_DFU_PARAM_LEN) |
Vincent Coubard |
0:f2542974c862 | 314 | { |
Vincent Coubard |
0:f2542974c862 | 315 | return ble_dfu_response_send(p_dfu, |
Vincent Coubard |
0:f2542974c862 | 316 | (ble_dfu_procedure_t) p_ble_write_evt->data[0], |
Vincent Coubard |
0:f2542974c862 | 317 | BLE_DFU_RESP_VAL_OPER_FAILED); |
Vincent Coubard |
0:f2542974c862 | 318 | } |
Vincent Coubard |
0:f2542974c862 | 319 | |
Vincent Coubard |
0:f2542974c862 | 320 | ble_dfu_evt.evt.ble_dfu_pkt_write.len = 1; |
Vincent Coubard |
0:f2542974c862 | 321 | ble_dfu_evt.evt.ble_dfu_pkt_write.p_data = &(p_ble_write_evt->data[1]); |
Vincent Coubard |
0:f2542974c862 | 322 | |
Vincent Coubard |
0:f2542974c862 | 323 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 324 | break; |
Vincent Coubard |
0:f2542974c862 | 325 | |
Vincent Coubard |
0:f2542974c862 | 326 | case OP_CODE_RECEIVE_FW: |
Vincent Coubard |
0:f2542974c862 | 327 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_RECEIVE_APP_DATA; |
Vincent Coubard |
0:f2542974c862 | 328 | |
Vincent Coubard |
0:f2542974c862 | 329 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 330 | break; |
Vincent Coubard |
0:f2542974c862 | 331 | |
Vincent Coubard |
0:f2542974c862 | 332 | case OP_CODE_VALIDATE: |
Vincent Coubard |
0:f2542974c862 | 333 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_VALIDATE; |
Vincent Coubard |
0:f2542974c862 | 334 | |
Vincent Coubard |
0:f2542974c862 | 335 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 336 | break; |
Vincent Coubard |
0:f2542974c862 | 337 | |
Vincent Coubard |
0:f2542974c862 | 338 | case OP_CODE_ACTIVATE_N_RESET: |
Vincent Coubard |
0:f2542974c862 | 339 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_ACTIVATE_N_RESET; |
Vincent Coubard |
0:f2542974c862 | 340 | |
Vincent Coubard |
0:f2542974c862 | 341 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 342 | break; |
Vincent Coubard |
0:f2542974c862 | 343 | |
Vincent Coubard |
0:f2542974c862 | 344 | case OP_CODE_SYS_RESET: |
Vincent Coubard |
0:f2542974c862 | 345 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_SYS_RESET; |
Vincent Coubard |
0:f2542974c862 | 346 | |
Vincent Coubard |
0:f2542974c862 | 347 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 348 | break; |
Vincent Coubard |
0:f2542974c862 | 349 | |
Vincent Coubard |
0:f2542974c862 | 350 | case OP_CODE_PKT_RCPT_NOTIF_REQ: |
Vincent Coubard |
0:f2542974c862 | 351 | if (p_ble_write_evt->len < PKT_RCPT_NOTIF_REQ_LEN) |
Vincent Coubard |
0:f2542974c862 | 352 | { |
Vincent Coubard |
0:f2542974c862 | 353 | return (ble_dfu_response_send(p_dfu, |
Vincent Coubard |
0:f2542974c862 | 354 | BLE_DFU_PKT_RCPT_REQ_PROCEDURE, |
Vincent Coubard |
0:f2542974c862 | 355 | BLE_DFU_RESP_VAL_NOT_SUPPORTED)); |
Vincent Coubard |
0:f2542974c862 | 356 | } |
Vincent Coubard |
0:f2542974c862 | 357 | |
Vincent Coubard |
0:f2542974c862 | 358 | ble_dfu_evt.evt.pkt_rcpt_notif_req.num_of_pkts = |
Vincent Coubard |
0:f2542974c862 | 359 | uint16_decode(&(p_ble_write_evt->data[1])); |
Vincent Coubard |
0:f2542974c862 | 360 | |
Vincent Coubard |
0:f2542974c862 | 361 | if (ble_dfu_evt.evt.pkt_rcpt_notif_req.num_of_pkts == 0) |
Vincent Coubard |
0:f2542974c862 | 362 | { |
Vincent Coubard |
0:f2542974c862 | 363 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_PKT_RCPT_NOTIF_DISABLED; |
Vincent Coubard |
0:f2542974c862 | 364 | } |
Vincent Coubard |
0:f2542974c862 | 365 | else |
Vincent Coubard |
0:f2542974c862 | 366 | { |
Vincent Coubard |
0:f2542974c862 | 367 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_PKT_RCPT_NOTIF_ENABLED; |
Vincent Coubard |
0:f2542974c862 | 368 | } |
Vincent Coubard |
0:f2542974c862 | 369 | |
Vincent Coubard |
0:f2542974c862 | 370 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 371 | |
Vincent Coubard |
0:f2542974c862 | 372 | break; |
Vincent Coubard |
0:f2542974c862 | 373 | |
Vincent Coubard |
0:f2542974c862 | 374 | case OP_CODE_IMAGE_SIZE_REQ: |
Vincent Coubard |
0:f2542974c862 | 375 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_BYTES_RECEIVED_SEND; |
Vincent Coubard |
0:f2542974c862 | 376 | |
Vincent Coubard |
0:f2542974c862 | 377 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 378 | break; |
Vincent Coubard |
0:f2542974c862 | 379 | |
Vincent Coubard |
0:f2542974c862 | 380 | default: |
Vincent Coubard |
0:f2542974c862 | 381 | // Unsupported op code. |
Vincent Coubard |
0:f2542974c862 | 382 | return ble_dfu_response_send(p_dfu, |
Vincent Coubard |
0:f2542974c862 | 383 | (ble_dfu_procedure_t) p_ble_write_evt->data[0], |
Vincent Coubard |
0:f2542974c862 | 384 | BLE_DFU_RESP_VAL_NOT_SUPPORTED); |
Vincent Coubard |
0:f2542974c862 | 385 | } |
Vincent Coubard |
0:f2542974c862 | 386 | return NRF_SUCCESS; |
Vincent Coubard |
0:f2542974c862 | 387 | } |
Vincent Coubard |
0:f2542974c862 | 388 | |
Vincent Coubard |
0:f2542974c862 | 389 | |
Vincent Coubard |
0:f2542974c862 | 390 | /**@brief Function for handling the @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST event from the S110 |
Vincent Coubard |
0:f2542974c862 | 391 | * Stack. |
Vincent Coubard |
0:f2542974c862 | 392 | * |
Vincent Coubard |
0:f2542974c862 | 393 | * @param[in] p_dfu DFU Service Structure. |
Vincent Coubard |
0:f2542974c862 | 394 | * @param[in] p_ble_evt Pointer to the event received from BLE stack. |
Vincent Coubard |
0:f2542974c862 | 395 | */ |
Vincent Coubard |
0:f2542974c862 | 396 | static void on_rw_authorize_req(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) |
Vincent Coubard |
0:f2542974c862 | 397 | { |
Vincent Coubard |
0:f2542974c862 | 398 | ble_gatts_evt_rw_authorize_request_t * p_authorize_request; |
Vincent Coubard |
0:f2542974c862 | 399 | |
Vincent Coubard |
0:f2542974c862 | 400 | p_authorize_request = &(p_ble_evt->evt.gatts_evt.params.authorize_request); |
Vincent Coubard |
0:f2542974c862 | 401 | |
Vincent Coubard |
0:f2542974c862 | 402 | if ( |
Vincent Coubard |
0:f2542974c862 | 403 | (p_authorize_request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) |
Vincent Coubard |
0:f2542974c862 | 404 | && |
Vincent Coubard |
0:f2542974c862 | 405 | (p_authorize_request->request.write.handle == p_dfu->dfu_ctrl_pt_handles.value_handle) |
Vincent Coubard |
0:f2542974c862 | 406 | && |
Vincent Coubard |
0:f2542974c862 | 407 | (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op != BLE_GATTS_OP_PREP_WRITE_REQ) |
Vincent Coubard |
0:f2542974c862 | 408 | && |
Vincent Coubard |
0:f2542974c862 | 409 | (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) |
Vincent Coubard |
0:f2542974c862 | 410 | && |
Vincent Coubard |
0:f2542974c862 | 411 | (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL) |
Vincent Coubard |
0:f2542974c862 | 412 | ) |
Vincent Coubard |
0:f2542974c862 | 413 | { |
Vincent Coubard |
0:f2542974c862 | 414 | uint32_t err_code; |
Vincent Coubard |
0:f2542974c862 | 415 | |
Vincent Coubard |
0:f2542974c862 | 416 | err_code = on_ctrl_pt_write(p_dfu, &(p_authorize_request->request.write)); |
Vincent Coubard |
0:f2542974c862 | 417 | |
Vincent Coubard |
0:f2542974c862 | 418 | if (err_code != NRF_SUCCESS && p_dfu->error_handler != NULL) |
Vincent Coubard |
0:f2542974c862 | 419 | { |
Vincent Coubard |
0:f2542974c862 | 420 | p_dfu->error_handler(err_code); |
Vincent Coubard |
0:f2542974c862 | 421 | } |
Vincent Coubard |
0:f2542974c862 | 422 | } |
Vincent Coubard |
0:f2542974c862 | 423 | } |
Vincent Coubard |
0:f2542974c862 | 424 | |
Vincent Coubard |
0:f2542974c862 | 425 | |
Vincent Coubard |
0:f2542974c862 | 426 | /**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the S110 SoftDevice. |
Vincent Coubard |
0:f2542974c862 | 427 | * |
Vincent Coubard |
0:f2542974c862 | 428 | * @param[in] p_dfu DFU Service Structure. |
Vincent Coubard |
0:f2542974c862 | 429 | * @param[in] p_ble_evt Pointer to the event received from BLE stack. |
Vincent Coubard |
0:f2542974c862 | 430 | */ |
Vincent Coubard |
0:f2542974c862 | 431 | static void on_write(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) |
Vincent Coubard |
0:f2542974c862 | 432 | { |
Vincent Coubard |
0:f2542974c862 | 433 | if (p_ble_evt->evt.gatts_evt.params.write.handle == p_dfu->dfu_pkt_handles.value_handle) |
Vincent Coubard |
0:f2542974c862 | 434 | { |
Vincent Coubard |
0:f2542974c862 | 435 | // DFU Packet written |
Vincent Coubard |
0:f2542974c862 | 436 | |
Vincent Coubard |
0:f2542974c862 | 437 | ble_dfu_evt_t ble_dfu_evt; |
Vincent Coubard |
0:f2542974c862 | 438 | |
Vincent Coubard |
0:f2542974c862 | 439 | ble_dfu_evt.ble_dfu_evt_type = BLE_DFU_PACKET_WRITE; |
Vincent Coubard |
0:f2542974c862 | 440 | ble_dfu_evt.evt.ble_dfu_pkt_write.len = p_ble_evt->evt.gatts_evt.params.write.len; |
Vincent Coubard |
0:f2542974c862 | 441 | ble_dfu_evt.evt.ble_dfu_pkt_write.p_data = p_ble_evt->evt.gatts_evt.params.write.data; |
Vincent Coubard |
0:f2542974c862 | 442 | |
Vincent Coubard |
0:f2542974c862 | 443 | p_dfu->evt_handler(p_dfu, &ble_dfu_evt); |
Vincent Coubard |
0:f2542974c862 | 444 | } |
Vincent Coubard |
0:f2542974c862 | 445 | } |
Vincent Coubard |
0:f2542974c862 | 446 | |
Vincent Coubard |
0:f2542974c862 | 447 | |
Vincent Coubard |
0:f2542974c862 | 448 | /**@brief Function for handling the BLE_GAP_EVT_DISCONNECTED event from the S110 SoftDevice. |
Vincent Coubard |
0:f2542974c862 | 449 | * |
Vincent Coubard |
0:f2542974c862 | 450 | * @param[in] p_dfu DFU Service Structure. |
Vincent Coubard |
0:f2542974c862 | 451 | * @param[in] p_ble_evt Pointer to the event received from BLE stack. |
Vincent Coubard |
0:f2542974c862 | 452 | */ |
Vincent Coubard |
0:f2542974c862 | 453 | static void on_disconnect(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) |
Vincent Coubard |
0:f2542974c862 | 454 | { |
Vincent Coubard |
0:f2542974c862 | 455 | p_dfu->conn_handle = BLE_CONN_HANDLE_INVALID; |
Vincent Coubard |
0:f2542974c862 | 456 | } |
Vincent Coubard |
0:f2542974c862 | 457 | |
Vincent Coubard |
0:f2542974c862 | 458 | |
Vincent Coubard |
0:f2542974c862 | 459 | uint32_t ble_dfu_init(ble_dfu_t * p_dfu, ble_dfu_init_t * p_dfu_init) |
Vincent Coubard |
0:f2542974c862 | 460 | { |
Vincent Coubard |
0:f2542974c862 | 461 | if ((p_dfu == NULL) || (p_dfu_init == NULL) || (p_dfu_init->evt_handler == NULL)) |
Vincent Coubard |
0:f2542974c862 | 462 | { |
Vincent Coubard |
0:f2542974c862 | 463 | return NRF_ERROR_NULL; |
Vincent Coubard |
0:f2542974c862 | 464 | } |
Vincent Coubard |
0:f2542974c862 | 465 | |
Vincent Coubard |
0:f2542974c862 | 466 | p_dfu->conn_handle = BLE_CONN_HANDLE_INVALID; |
Vincent Coubard |
0:f2542974c862 | 467 | |
Vincent Coubard |
0:f2542974c862 | 468 | ble_uuid_t service_uuid; |
Vincent Coubard |
0:f2542974c862 | 469 | uint32_t err_code; |
Vincent Coubard |
0:f2542974c862 | 470 | |
Vincent Coubard |
0:f2542974c862 | 471 | const ble_uuid128_t base_uuid128 = |
Vincent Coubard |
0:f2542974c862 | 472 | { |
Vincent Coubard |
0:f2542974c862 | 473 | { |
Vincent Coubard |
0:f2542974c862 | 474 | 0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, |
Vincent Coubard |
0:f2542974c862 | 475 | 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00 |
Vincent Coubard |
0:f2542974c862 | 476 | } |
Vincent Coubard |
0:f2542974c862 | 477 | }; |
Vincent Coubard |
0:f2542974c862 | 478 | |
Vincent Coubard |
0:f2542974c862 | 479 | service_uuid.uuid = BLE_DFU_SERVICE_UUID; |
Vincent Coubard |
0:f2542974c862 | 480 | |
Vincent Coubard |
0:f2542974c862 | 481 | err_code = sd_ble_uuid_vs_add(&base_uuid128, &(service_uuid.type)); |
Vincent Coubard |
0:f2542974c862 | 482 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 483 | { |
Vincent Coubard |
0:f2542974c862 | 484 | return err_code; |
Vincent Coubard |
0:f2542974c862 | 485 | } |
Vincent Coubard |
0:f2542974c862 | 486 | |
Vincent Coubard |
0:f2542974c862 | 487 | err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, |
Vincent Coubard |
0:f2542974c862 | 488 | &service_uuid, |
Vincent Coubard |
0:f2542974c862 | 489 | &(p_dfu->service_handle)); |
Vincent Coubard |
0:f2542974c862 | 490 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 491 | { |
Vincent Coubard |
0:f2542974c862 | 492 | return err_code; |
Vincent Coubard |
0:f2542974c862 | 493 | } |
Vincent Coubard |
0:f2542974c862 | 494 | |
Vincent Coubard |
0:f2542974c862 | 495 | p_dfu->uuid_type = service_uuid.type; |
Vincent Coubard |
0:f2542974c862 | 496 | |
Vincent Coubard |
0:f2542974c862 | 497 | err_code = dfu_pkt_char_add(p_dfu); |
Vincent Coubard |
0:f2542974c862 | 498 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 499 | { |
Vincent Coubard |
0:f2542974c862 | 500 | return err_code; |
Vincent Coubard |
0:f2542974c862 | 501 | } |
Vincent Coubard |
0:f2542974c862 | 502 | |
Vincent Coubard |
0:f2542974c862 | 503 | err_code = dfu_ctrl_pt_add(p_dfu); |
Vincent Coubard |
0:f2542974c862 | 504 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 505 | { |
Vincent Coubard |
0:f2542974c862 | 506 | return err_code; |
Vincent Coubard |
0:f2542974c862 | 507 | } |
Vincent Coubard |
0:f2542974c862 | 508 | |
Vincent Coubard |
0:f2542974c862 | 509 | err_code = dfu_rev_char_add(p_dfu, p_dfu_init); |
Vincent Coubard |
0:f2542974c862 | 510 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
0:f2542974c862 | 511 | { |
Vincent Coubard |
0:f2542974c862 | 512 | return err_code; |
Vincent Coubard |
0:f2542974c862 | 513 | } |
Vincent Coubard |
0:f2542974c862 | 514 | |
Vincent Coubard |
0:f2542974c862 | 515 | p_dfu->evt_handler = p_dfu_init->evt_handler; |
Vincent Coubard |
0:f2542974c862 | 516 | |
Vincent Coubard |
0:f2542974c862 | 517 | if (p_dfu_init->error_handler != NULL) |
Vincent Coubard |
0:f2542974c862 | 518 | { |
Vincent Coubard |
0:f2542974c862 | 519 | p_dfu->error_handler = p_dfu_init->error_handler; |
Vincent Coubard |
0:f2542974c862 | 520 | } |
Vincent Coubard |
0:f2542974c862 | 521 | |
Vincent Coubard |
0:f2542974c862 | 522 | m_is_dfu_service_initialized = true; |
Vincent Coubard |
0:f2542974c862 | 523 | |
Vincent Coubard |
0:f2542974c862 | 524 | return NRF_SUCCESS; |
Vincent Coubard |
0:f2542974c862 | 525 | } |
Vincent Coubard |
0:f2542974c862 | 526 | |
Vincent Coubard |
0:f2542974c862 | 527 | |
Vincent Coubard |
0:f2542974c862 | 528 | void ble_dfu_on_ble_evt(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) |
Vincent Coubard |
0:f2542974c862 | 529 | { |
Vincent Coubard |
0:f2542974c862 | 530 | if ((p_dfu == NULL) || (p_ble_evt == NULL)) |
Vincent Coubard |
0:f2542974c862 | 531 | { |
Vincent Coubard |
0:f2542974c862 | 532 | return; |
Vincent Coubard |
0:f2542974c862 | 533 | } |
Vincent Coubard |
0:f2542974c862 | 534 | |
Vincent Coubard |
0:f2542974c862 | 535 | if (p_dfu->evt_handler != NULL) |
Vincent Coubard |
0:f2542974c862 | 536 | { |
Vincent Coubard |
0:f2542974c862 | 537 | switch (p_ble_evt->header.evt_id) |
Vincent Coubard |
0:f2542974c862 | 538 | { |
Vincent Coubard |
0:f2542974c862 | 539 | case BLE_GAP_EVT_CONNECTED: |
Vincent Coubard |
0:f2542974c862 | 540 | on_connect(p_dfu, p_ble_evt); |
Vincent Coubard |
0:f2542974c862 | 541 | break; |
Vincent Coubard |
0:f2542974c862 | 542 | |
Vincent Coubard |
0:f2542974c862 | 543 | case BLE_GATTS_EVT_WRITE: |
Vincent Coubard |
0:f2542974c862 | 544 | on_write(p_dfu, p_ble_evt); |
Vincent Coubard |
0:f2542974c862 | 545 | break; |
Vincent Coubard |
0:f2542974c862 | 546 | |
Vincent Coubard |
0:f2542974c862 | 547 | case BLE_GAP_EVT_DISCONNECTED: |
Vincent Coubard |
0:f2542974c862 | 548 | on_disconnect(p_dfu, p_ble_evt); |
Vincent Coubard |
0:f2542974c862 | 549 | break; |
Vincent Coubard |
0:f2542974c862 | 550 | |
Vincent Coubard |
0:f2542974c862 | 551 | case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: |
Vincent Coubard |
0:f2542974c862 | 552 | on_rw_authorize_req(p_dfu, p_ble_evt); |
Vincent Coubard |
0:f2542974c862 | 553 | break; |
Vincent Coubard |
0:f2542974c862 | 554 | |
Vincent Coubard |
0:f2542974c862 | 555 | default: |
Vincent Coubard |
0:f2542974c862 | 556 | // No implementation needed. |
Vincent Coubard |
0:f2542974c862 | 557 | break; |
Vincent Coubard |
0:f2542974c862 | 558 | } |
Vincent Coubard |
0:f2542974c862 | 559 | } |
Vincent Coubard |
0:f2542974c862 | 560 | } |
Vincent Coubard |
0:f2542974c862 | 561 | |
Vincent Coubard |
0:f2542974c862 | 562 | |
Vincent Coubard |
0:f2542974c862 | 563 | uint32_t ble_dfu_bytes_rcvd_report(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd) |
Vincent Coubard |
0:f2542974c862 | 564 | { |
Vincent Coubard |
0:f2542974c862 | 565 | if (p_dfu == NULL) |
Vincent Coubard |
0:f2542974c862 | 566 | { |
Vincent Coubard |
0:f2542974c862 | 567 | return NRF_ERROR_NULL; |
Vincent Coubard |
0:f2542974c862 | 568 | } |
Vincent Coubard |
0:f2542974c862 | 569 | |
Vincent Coubard |
0:f2542974c862 | 570 | if ((p_dfu->conn_handle == BLE_CONN_HANDLE_INVALID) || !m_is_dfu_service_initialized) |
Vincent Coubard |
0:f2542974c862 | 571 | { |
Vincent Coubard |
0:f2542974c862 | 572 | return NRF_ERROR_INVALID_STATE; |
Vincent Coubard |
0:f2542974c862 | 573 | } |
Vincent Coubard |
0:f2542974c862 | 574 | |
Vincent Coubard |
0:f2542974c862 | 575 | ble_gatts_hvx_params_t hvx_params; |
Vincent Coubard |
0:f2542974c862 | 576 | uint16_t index = 0; |
Vincent Coubard |
0:f2542974c862 | 577 | |
Vincent Coubard |
0:f2542974c862 | 578 | // Encode the Op Code. |
Vincent Coubard |
0:f2542974c862 | 579 | m_notif_buffer[index++] = OP_CODE_RESPONSE; |
Vincent Coubard |
0:f2542974c862 | 580 | |
Vincent Coubard |
0:f2542974c862 | 581 | // Encode the Reqest Op Code. |
Vincent Coubard |
0:f2542974c862 | 582 | m_notif_buffer[index++] = OP_CODE_IMAGE_SIZE_REQ; |
Vincent Coubard |
0:f2542974c862 | 583 | |
Vincent Coubard |
0:f2542974c862 | 584 | // Encode the Response Value. |
Vincent Coubard |
0:f2542974c862 | 585 | m_notif_buffer[index++] = (uint8_t)BLE_DFU_RESP_VAL_SUCCESS; |
Vincent Coubard |
0:f2542974c862 | 586 | |
Vincent Coubard |
0:f2542974c862 | 587 | index += uint32_encode(num_of_firmware_bytes_rcvd, &m_notif_buffer[index]); |
Vincent Coubard |
0:f2542974c862 | 588 | |
Vincent Coubard |
0:f2542974c862 | 589 | memset(&hvx_params, 0, sizeof(hvx_params)); |
Vincent Coubard |
0:f2542974c862 | 590 | |
Vincent Coubard |
0:f2542974c862 | 591 | hvx_params.handle = p_dfu->dfu_ctrl_pt_handles.value_handle; |
Vincent Coubard |
0:f2542974c862 | 592 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; |
Vincent Coubard |
0:f2542974c862 | 593 | hvx_params.offset = 0; |
Vincent Coubard |
0:f2542974c862 | 594 | hvx_params.p_len = &index; |
Vincent Coubard |
0:f2542974c862 | 595 | hvx_params.p_data = m_notif_buffer; |
Vincent Coubard |
0:f2542974c862 | 596 | |
Vincent Coubard |
0:f2542974c862 | 597 | return sd_ble_gatts_hvx(p_dfu->conn_handle, &hvx_params); |
Vincent Coubard |
0:f2542974c862 | 598 | } |
Vincent Coubard |
0:f2542974c862 | 599 | |
Vincent Coubard |
0:f2542974c862 | 600 | |
Vincent Coubard |
0:f2542974c862 | 601 | uint32_t ble_dfu_pkts_rcpt_notify(ble_dfu_t * p_dfu, uint32_t num_of_firmware_bytes_rcvd) |
Vincent Coubard |
0:f2542974c862 | 602 | { |
Vincent Coubard |
0:f2542974c862 | 603 | if (p_dfu == NULL) |
Vincent Coubard |
0:f2542974c862 | 604 | { |
Vincent Coubard |
0:f2542974c862 | 605 | return NRF_ERROR_NULL; |
Vincent Coubard |
0:f2542974c862 | 606 | } |
Vincent Coubard |
0:f2542974c862 | 607 | |
Vincent Coubard |
0:f2542974c862 | 608 | if ((p_dfu->conn_handle == BLE_CONN_HANDLE_INVALID) || !m_is_dfu_service_initialized) |
Vincent Coubard |
0:f2542974c862 | 609 | { |
Vincent Coubard |
0:f2542974c862 | 610 | return NRF_ERROR_INVALID_STATE; |
Vincent Coubard |
0:f2542974c862 | 611 | } |
Vincent Coubard |
0:f2542974c862 | 612 | |
Vincent Coubard |
0:f2542974c862 | 613 | ble_gatts_hvx_params_t hvx_params; |
Vincent Coubard |
0:f2542974c862 | 614 | uint16_t index = 0; |
Vincent Coubard |
0:f2542974c862 | 615 | |
Vincent Coubard |
0:f2542974c862 | 616 | m_notif_buffer[index++] = OP_CODE_PKT_RCPT_NOTIF; |
Vincent Coubard |
0:f2542974c862 | 617 | |
Vincent Coubard |
0:f2542974c862 | 618 | index += uint32_encode(num_of_firmware_bytes_rcvd, &m_notif_buffer[index]); |
Vincent Coubard |
0:f2542974c862 | 619 | |
Vincent Coubard |
0:f2542974c862 | 620 | memset(&hvx_params, 0, sizeof(hvx_params)); |
Vincent Coubard |
0:f2542974c862 | 621 | |
Vincent Coubard |
0:f2542974c862 | 622 | hvx_params.handle = p_dfu->dfu_ctrl_pt_handles.value_handle; |
Vincent Coubard |
0:f2542974c862 | 623 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; |
Vincent Coubard |
0:f2542974c862 | 624 | hvx_params.offset = 0; |
Vincent Coubard |
0:f2542974c862 | 625 | hvx_params.p_len = &index; |
Vincent Coubard |
0:f2542974c862 | 626 | hvx_params.p_data = m_notif_buffer; |
Vincent Coubard |
0:f2542974c862 | 627 | |
Vincent Coubard |
0:f2542974c862 | 628 | return sd_ble_gatts_hvx(p_dfu->conn_handle, &hvx_params); |
Vincent Coubard |
0:f2542974c862 | 629 | } |
Vincent Coubard |
0:f2542974c862 | 630 | |
Vincent Coubard |
0:f2542974c862 | 631 | |
Vincent Coubard |
0:f2542974c862 | 632 | uint32_t ble_dfu_response_send(ble_dfu_t * p_dfu, |
Vincent Coubard |
0:f2542974c862 | 633 | ble_dfu_procedure_t dfu_proc, |
Vincent Coubard |
0:f2542974c862 | 634 | ble_dfu_resp_val_t resp_val) |
Vincent Coubard |
0:f2542974c862 | 635 | { |
Vincent Coubard |
0:f2542974c862 | 636 | if (p_dfu == NULL) |
Vincent Coubard |
0:f2542974c862 | 637 | { |
Vincent Coubard |
0:f2542974c862 | 638 | return NRF_ERROR_NULL; |
Vincent Coubard |
0:f2542974c862 | 639 | } |
Vincent Coubard |
0:f2542974c862 | 640 | |
Vincent Coubard |
0:f2542974c862 | 641 | if ((p_dfu->conn_handle == BLE_CONN_HANDLE_INVALID) || !m_is_dfu_service_initialized) |
Vincent Coubard |
0:f2542974c862 | 642 | { |
Vincent Coubard |
0:f2542974c862 | 643 | return NRF_ERROR_INVALID_STATE; |
Vincent Coubard |
0:f2542974c862 | 644 | } |
Vincent Coubard |
0:f2542974c862 | 645 | |
Vincent Coubard |
0:f2542974c862 | 646 | ble_gatts_hvx_params_t hvx_params; |
Vincent Coubard |
0:f2542974c862 | 647 | uint16_t index = 0; |
Vincent Coubard |
0:f2542974c862 | 648 | |
Vincent Coubard |
0:f2542974c862 | 649 | m_notif_buffer[index++] = OP_CODE_RESPONSE; |
Vincent Coubard |
0:f2542974c862 | 650 | |
Vincent Coubard |
0:f2542974c862 | 651 | // Encode the Request Op code |
Vincent Coubard |
0:f2542974c862 | 652 | m_notif_buffer[index++] = (uint8_t)dfu_proc; |
Vincent Coubard |
0:f2542974c862 | 653 | |
Vincent Coubard |
0:f2542974c862 | 654 | // Encode the Response Value. |
Vincent Coubard |
0:f2542974c862 | 655 | m_notif_buffer[index++] = (uint8_t)resp_val; |
Vincent Coubard |
0:f2542974c862 | 656 | |
Vincent Coubard |
0:f2542974c862 | 657 | memset(&hvx_params, 0, sizeof(hvx_params)); |
Vincent Coubard |
0:f2542974c862 | 658 | |
Vincent Coubard |
0:f2542974c862 | 659 | hvx_params.handle = p_dfu->dfu_ctrl_pt_handles.value_handle; |
Vincent Coubard |
0:f2542974c862 | 660 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; |
Vincent Coubard |
0:f2542974c862 | 661 | hvx_params.offset = 0; |
Vincent Coubard |
0:f2542974c862 | 662 | hvx_params.p_len = &index; |
Vincent Coubard |
0:f2542974c862 | 663 | hvx_params.p_data = m_notif_buffer; |
Vincent Coubard |
0:f2542974c862 | 664 | |
Vincent Coubard |
0:f2542974c862 | 665 | return sd_ble_gatts_hvx(p_dfu->conn_handle, &hvx_params); |
vcoubard | 1:ebc0e0ef0a11 | 666 | } |