Nordic stack and drivers for the mbed BLE API
Dependents: BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate writable_gatt ... more
TARGET_MCU_NRF51822/sdk/source/libraries/bootloader_dfu/dfu_app_handler.c@638:c90ae1400bf2, 2016-09-14 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Wed Sep 14 14:39:43 2016 +0100
- Revision:
- 638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
638:c90ae1400bf2 | 1 | /* |
Vincent Coubard |
638:c90ae1400bf2 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Vincent Coubard |
638:c90ae1400bf2 | 3 | * All rights reserved. |
Vincent Coubard |
638:c90ae1400bf2 | 4 | * |
Vincent Coubard |
638:c90ae1400bf2 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Vincent Coubard |
638:c90ae1400bf2 | 6 | * are permitted provided that the following conditions are met: |
Vincent Coubard |
638:c90ae1400bf2 | 7 | * |
Vincent Coubard |
638:c90ae1400bf2 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Vincent Coubard |
638:c90ae1400bf2 | 9 | * list of conditions and the following disclaimer. |
Vincent Coubard |
638:c90ae1400bf2 | 10 | * |
Vincent Coubard |
638:c90ae1400bf2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Vincent Coubard |
638:c90ae1400bf2 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Vincent Coubard |
638:c90ae1400bf2 | 13 | * other materials provided with the distribution. |
Vincent Coubard |
638:c90ae1400bf2 | 14 | * |
Vincent Coubard |
638:c90ae1400bf2 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Vincent Coubard |
638:c90ae1400bf2 | 16 | * contributors to this software may be used to endorse or promote products |
Vincent Coubard |
638:c90ae1400bf2 | 17 | * derived from this software without specific prior written permission. |
Vincent Coubard |
638:c90ae1400bf2 | 18 | * |
Vincent Coubard |
638:c90ae1400bf2 | 19 | * |
Vincent Coubard |
638:c90ae1400bf2 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Vincent Coubard |
638:c90ae1400bf2 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Vincent Coubard |
638:c90ae1400bf2 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Vincent Coubard |
638:c90ae1400bf2 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Vincent Coubard |
638:c90ae1400bf2 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Vincent Coubard |
638:c90ae1400bf2 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Vincent Coubard |
638:c90ae1400bf2 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Vincent Coubard |
638:c90ae1400bf2 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Vincent Coubard |
638:c90ae1400bf2 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Vincent Coubard |
638:c90ae1400bf2 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Vincent Coubard |
638:c90ae1400bf2 | 30 | * |
Vincent Coubard |
638:c90ae1400bf2 | 31 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 32 | |
Vincent Coubard |
638:c90ae1400bf2 | 33 | #include "dfu_app_handler.h" |
Vincent Coubard |
638:c90ae1400bf2 | 34 | #include <string.h> |
Vincent Coubard |
638:c90ae1400bf2 | 35 | #include "bootloader_util.h" |
Vincent Coubard |
638:c90ae1400bf2 | 36 | #include "nrf.h" |
Vincent Coubard |
638:c90ae1400bf2 | 37 | #include "nrf_sdm.h" |
Vincent Coubard |
638:c90ae1400bf2 | 38 | #include "ble_gatt.h" |
Vincent Coubard |
638:c90ae1400bf2 | 39 | #include "ble_gatts.h" |
Vincent Coubard |
638:c90ae1400bf2 | 40 | #include "app_error.h" |
Vincent Coubard |
638:c90ae1400bf2 | 41 | #include "dfu_ble_svc.h" |
Vincent Coubard |
638:c90ae1400bf2 | 42 | #include "device_manager.h" |
Vincent Coubard |
638:c90ae1400bf2 | 43 | #include "nrf_delay.h" |
Vincent Coubard |
638:c90ae1400bf2 | 44 | |
Vincent Coubard |
638:c90ae1400bf2 | 45 | #define IRQ_ENABLED 0x01 /**< Field that identifies if an interrupt is enabled. */ |
Vincent Coubard |
638:c90ae1400bf2 | 46 | #define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */ |
Vincent Coubard |
638:c90ae1400bf2 | 47 | |
Vincent Coubard |
638:c90ae1400bf2 | 48 | static void dfu_app_reset_prepare(void); /**< Forward declaration of default reset handler. */ |
Vincent Coubard |
638:c90ae1400bf2 | 49 | static dfu_app_reset_prepare_t m_reset_prepare = dfu_app_reset_prepare; /**< Callback function to application to prepare for system reset. Allows application to clean up service and memory before reset. */ |
Vincent Coubard |
638:c90ae1400bf2 | 50 | static dfu_ble_peer_data_t m_peer_data; /**< Peer data to be used for data exchange when resetting into DFU mode. */ |
Vincent Coubard |
638:c90ae1400bf2 | 51 | static dm_handle_t m_dm_handle; /**< Device Manager handle with instance IDs of current BLE connection. */ |
Vincent Coubard |
638:c90ae1400bf2 | 52 | |
Vincent Coubard |
638:c90ae1400bf2 | 53 | |
Vincent Coubard |
638:c90ae1400bf2 | 54 | /**@brief Function for reset_prepare handler if the application has not registered a handler. |
Vincent Coubard |
638:c90ae1400bf2 | 55 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 56 | static void dfu_app_reset_prepare(void) |
Vincent Coubard |
638:c90ae1400bf2 | 57 | { |
Vincent Coubard |
638:c90ae1400bf2 | 58 | // Reset prepare should be handled by application. |
Vincent Coubard |
638:c90ae1400bf2 | 59 | // This function can be extended to include default handling if application does not implement |
Vincent Coubard |
638:c90ae1400bf2 | 60 | // own handler. |
Vincent Coubard |
638:c90ae1400bf2 | 61 | } |
Vincent Coubard |
638:c90ae1400bf2 | 62 | |
Vincent Coubard |
638:c90ae1400bf2 | 63 | |
Vincent Coubard |
638:c90ae1400bf2 | 64 | /**@brief Function for disabling all interrupts before jumping from bootloader to application. |
Vincent Coubard |
638:c90ae1400bf2 | 65 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 66 | static void interrupts_disable(void) |
Vincent Coubard |
638:c90ae1400bf2 | 67 | { |
Vincent Coubard |
638:c90ae1400bf2 | 68 | uint32_t interrupt_setting_mask; |
Vincent Coubard |
638:c90ae1400bf2 | 69 | uint32_t irq; |
Vincent Coubard |
638:c90ae1400bf2 | 70 | |
Vincent Coubard |
638:c90ae1400bf2 | 71 | // Fetch the current interrupt settings. |
Vincent Coubard |
638:c90ae1400bf2 | 72 | interrupt_setting_mask = NVIC->ISER[0]; |
Vincent Coubard |
638:c90ae1400bf2 | 73 | |
Vincent Coubard |
638:c90ae1400bf2 | 74 | // Loop from interrupt 0 for disabling of all interrupts. |
Vincent Coubard |
638:c90ae1400bf2 | 75 | for (irq = 0; irq < MAX_NUMBER_INTERRUPTS; irq++) |
Vincent Coubard |
638:c90ae1400bf2 | 76 | { |
Vincent Coubard |
638:c90ae1400bf2 | 77 | if (interrupt_setting_mask & (IRQ_ENABLED << irq)) |
Vincent Coubard |
638:c90ae1400bf2 | 78 | { |
Vincent Coubard |
638:c90ae1400bf2 | 79 | // The interrupt was enabled, hence disable it. |
Vincent Coubard |
638:c90ae1400bf2 | 80 | NVIC_DisableIRQ((IRQn_Type)irq); |
Vincent Coubard |
638:c90ae1400bf2 | 81 | } |
Vincent Coubard |
638:c90ae1400bf2 | 82 | } |
Vincent Coubard |
638:c90ae1400bf2 | 83 | } |
Vincent Coubard |
638:c90ae1400bf2 | 84 | |
Vincent Coubard |
638:c90ae1400bf2 | 85 | |
Vincent Coubard |
638:c90ae1400bf2 | 86 | /**@brief Function for providing peer information to DFU for re-establishing a bonded connection in |
Vincent Coubard |
638:c90ae1400bf2 | 87 | * DFU mode. |
Vincent Coubard |
638:c90ae1400bf2 | 88 | * |
Vincent Coubard |
638:c90ae1400bf2 | 89 | * @param[in] conn_handle Connection handle for the connection requesting DFU mode. |
Vincent Coubard |
638:c90ae1400bf2 | 90 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 91 | static void dfu_app_peer_data_set(uint16_t conn_handle) |
Vincent Coubard |
638:c90ae1400bf2 | 92 | { |
Vincent Coubard |
638:c90ae1400bf2 | 93 | uint32_t err_code; |
Vincent Coubard |
638:c90ae1400bf2 | 94 | dm_sec_keyset_t key_set; |
Vincent Coubard |
638:c90ae1400bf2 | 95 | uint32_t app_context_data = 0; |
Vincent Coubard |
638:c90ae1400bf2 | 96 | dm_application_context_t app_context; |
Vincent Coubard |
638:c90ae1400bf2 | 97 | |
Vincent Coubard |
638:c90ae1400bf2 | 98 | |
Vincent Coubard |
638:c90ae1400bf2 | 99 | /** [DFU bond sharing] */ |
Vincent Coubard |
638:c90ae1400bf2 | 100 | err_code = dm_handle_get(conn_handle, &m_dm_handle); |
Vincent Coubard |
638:c90ae1400bf2 | 101 | if (err_code == NRF_SUCCESS) |
Vincent Coubard |
638:c90ae1400bf2 | 102 | { |
Vincent Coubard |
638:c90ae1400bf2 | 103 | err_code = dm_distributed_keys_get(&m_dm_handle, &key_set); |
Vincent Coubard |
638:c90ae1400bf2 | 104 | if (err_code == NRF_SUCCESS) |
Vincent Coubard |
638:c90ae1400bf2 | 105 | { |
Vincent Coubard |
638:c90ae1400bf2 | 106 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 107 | |
Vincent Coubard |
638:c90ae1400bf2 | 108 | m_peer_data.addr = key_set.keys_central.p_id_key->id_addr_info; |
Vincent Coubard |
638:c90ae1400bf2 | 109 | m_peer_data.irk = key_set.keys_central.p_id_key->id_info; |
Vincent Coubard |
638:c90ae1400bf2 | 110 | m_peer_data.enc_key.enc_info = key_set.keys_periph.enc_key.p_enc_key->enc_info; |
Vincent Coubard |
638:c90ae1400bf2 | 111 | m_peer_data.enc_key.master_id = key_set.keys_periph.enc_key.p_enc_key->master_id; |
Vincent Coubard |
638:c90ae1400bf2 | 112 | |
Vincent Coubard |
638:c90ae1400bf2 | 113 | err_code = dfu_ble_svc_peer_data_set(&m_peer_data); |
Vincent Coubard |
638:c90ae1400bf2 | 114 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 115 | |
Vincent Coubard |
638:c90ae1400bf2 | 116 | app_context_data = (DFU_APP_ATT_TABLE_CHANGED << DFU_APP_ATT_TABLE_POS); |
Vincent Coubard |
638:c90ae1400bf2 | 117 | app_context.len = sizeof(app_context_data); |
Vincent Coubard |
638:c90ae1400bf2 | 118 | app_context.p_data = (uint8_t *)&app_context_data; |
Vincent Coubard |
638:c90ae1400bf2 | 119 | app_context.flags = 0; |
Vincent Coubard |
638:c90ae1400bf2 | 120 | |
Vincent Coubard |
638:c90ae1400bf2 | 121 | err_code = dm_application_context_set(&m_dm_handle, &app_context); |
Vincent Coubard |
638:c90ae1400bf2 | 122 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 123 | } |
Vincent Coubard |
638:c90ae1400bf2 | 124 | else |
Vincent Coubard |
638:c90ae1400bf2 | 125 | { |
Vincent Coubard |
638:c90ae1400bf2 | 126 | // Keys were not available, thus we have a non-encrypted connection. |
Vincent Coubard |
638:c90ae1400bf2 | 127 | err_code = dm_peer_addr_get(&m_dm_handle, &m_peer_data.addr); |
Vincent Coubard |
638:c90ae1400bf2 | 128 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 129 | |
Vincent Coubard |
638:c90ae1400bf2 | 130 | err_code = dfu_ble_svc_peer_data_set(&m_peer_data); |
Vincent Coubard |
638:c90ae1400bf2 | 131 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 132 | } |
Vincent Coubard |
638:c90ae1400bf2 | 133 | } |
Vincent Coubard |
638:c90ae1400bf2 | 134 | /** [DFU bond sharing] */ |
Vincent Coubard |
638:c90ae1400bf2 | 135 | } |
Vincent Coubard |
638:c90ae1400bf2 | 136 | |
Vincent Coubard |
638:c90ae1400bf2 | 137 | |
Vincent Coubard |
638:c90ae1400bf2 | 138 | /**@brief Function for preparing the reset, disabling SoftDevice, and jumping to the bootloader. |
Vincent Coubard |
638:c90ae1400bf2 | 139 | * |
Vincent Coubard |
638:c90ae1400bf2 | 140 | * @param[in] conn_handle Connection handle for peer requesting to enter DFU mode. |
Vincent Coubard |
638:c90ae1400bf2 | 141 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 142 | static void bootloader_start(uint16_t conn_handle) |
Vincent Coubard |
638:c90ae1400bf2 | 143 | { |
Vincent Coubard |
638:c90ae1400bf2 | 144 | uint32_t err_code; |
Vincent Coubard |
638:c90ae1400bf2 | 145 | uint16_t sys_serv_attr_len = sizeof(m_peer_data.sys_serv_attr); |
Vincent Coubard |
638:c90ae1400bf2 | 146 | |
Vincent Coubard |
638:c90ae1400bf2 | 147 | err_code = sd_ble_gatts_sys_attr_get(conn_handle, |
Vincent Coubard |
638:c90ae1400bf2 | 148 | m_peer_data.sys_serv_attr, |
Vincent Coubard |
638:c90ae1400bf2 | 149 | &sys_serv_attr_len, |
Vincent Coubard |
638:c90ae1400bf2 | 150 | BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS); |
Vincent Coubard |
638:c90ae1400bf2 | 151 | if (err_code != NRF_SUCCESS) |
Vincent Coubard |
638:c90ae1400bf2 | 152 | { |
Vincent Coubard |
638:c90ae1400bf2 | 153 | // Any error at this stage means the system service attributes could not be fetched. |
Vincent Coubard |
638:c90ae1400bf2 | 154 | // This means the service changed indication cannot be sent in DFU mode, but connection |
Vincent Coubard |
638:c90ae1400bf2 | 155 | // is still possible to establish. |
Vincent Coubard |
638:c90ae1400bf2 | 156 | } |
Vincent Coubard |
638:c90ae1400bf2 | 157 | |
Vincent Coubard |
638:c90ae1400bf2 | 158 | m_reset_prepare(); |
Vincent Coubard |
638:c90ae1400bf2 | 159 | |
Vincent Coubard |
638:c90ae1400bf2 | 160 | err_code = sd_power_gpregret_set(BOOTLOADER_DFU_START); |
Vincent Coubard |
638:c90ae1400bf2 | 161 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 162 | |
Vincent Coubard |
638:c90ae1400bf2 | 163 | err_code = sd_softdevice_disable(); |
Vincent Coubard |
638:c90ae1400bf2 | 164 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 165 | |
Vincent Coubard |
638:c90ae1400bf2 | 166 | err_code = sd_softdevice_vector_table_base_set(NRF_UICR->BOOTLOADERADDR); |
Vincent Coubard |
638:c90ae1400bf2 | 167 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 168 | |
Vincent Coubard |
638:c90ae1400bf2 | 169 | dfu_app_peer_data_set(conn_handle); |
Vincent Coubard |
638:c90ae1400bf2 | 170 | |
Vincent Coubard |
638:c90ae1400bf2 | 171 | NVIC_ClearPendingIRQ(SWI2_IRQn); |
Vincent Coubard |
638:c90ae1400bf2 | 172 | interrupts_disable(); |
Vincent Coubard |
638:c90ae1400bf2 | 173 | bootloader_util_app_start(NRF_UICR->BOOTLOADERADDR); |
Vincent Coubard |
638:c90ae1400bf2 | 174 | } |
Vincent Coubard |
638:c90ae1400bf2 | 175 | |
Vincent Coubard |
638:c90ae1400bf2 | 176 | |
Vincent Coubard |
638:c90ae1400bf2 | 177 | void dfu_app_on_dfu_evt(ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt) |
Vincent Coubard |
638:c90ae1400bf2 | 178 | { |
Vincent Coubard |
638:c90ae1400bf2 | 179 | switch (p_evt->ble_dfu_evt_type) |
Vincent Coubard |
638:c90ae1400bf2 | 180 | { |
Vincent Coubard |
638:c90ae1400bf2 | 181 | case BLE_DFU_START: |
Vincent Coubard |
638:c90ae1400bf2 | 182 | // Starting the bootloader - will cause reset. |
Vincent Coubard |
638:c90ae1400bf2 | 183 | bootloader_start(p_dfu->conn_handle); |
Vincent Coubard |
638:c90ae1400bf2 | 184 | break; |
Vincent Coubard |
638:c90ae1400bf2 | 185 | |
Vincent Coubard |
638:c90ae1400bf2 | 186 | default: |
Vincent Coubard |
638:c90ae1400bf2 | 187 | { |
Vincent Coubard |
638:c90ae1400bf2 | 188 | // Unsupported event received from DFU Service. |
Vincent Coubard |
638:c90ae1400bf2 | 189 | // Send back BLE_DFU_RESP_VAL_NOT_SUPPORTED message to peer. |
Vincent Coubard |
638:c90ae1400bf2 | 190 | uint32_t err_code = ble_dfu_response_send(p_dfu, |
Vincent Coubard |
638:c90ae1400bf2 | 191 | BLE_DFU_START_PROCEDURE, |
Vincent Coubard |
638:c90ae1400bf2 | 192 | BLE_DFU_RESP_VAL_NOT_SUPPORTED); |
Vincent Coubard |
638:c90ae1400bf2 | 193 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 194 | } |
Vincent Coubard |
638:c90ae1400bf2 | 195 | break; |
Vincent Coubard |
638:c90ae1400bf2 | 196 | } |
Vincent Coubard |
638:c90ae1400bf2 | 197 | } |
Vincent Coubard |
638:c90ae1400bf2 | 198 | |
Vincent Coubard |
638:c90ae1400bf2 | 199 | |
Vincent Coubard |
638:c90ae1400bf2 | 200 | void dfu_app_reset_prepare_set(dfu_app_reset_prepare_t reset_prepare_func) |
Vincent Coubard |
638:c90ae1400bf2 | 201 | { |
Vincent Coubard |
638:c90ae1400bf2 | 202 | m_reset_prepare = reset_prepare_func; |
Vincent Coubard |
638:c90ae1400bf2 | 203 | } |
Vincent Coubard |
638:c90ae1400bf2 | 204 | |
Vincent Coubard |
638:c90ae1400bf2 | 205 | |
Vincent Coubard |
638:c90ae1400bf2 | 206 | void dfu_app_dm_appl_instance_set(dm_application_instance_t app_instance) |
Vincent Coubard |
638:c90ae1400bf2 | 207 | { |
Vincent Coubard |
638:c90ae1400bf2 | 208 | uint32_t err_code; |
Vincent Coubard |
638:c90ae1400bf2 | 209 | |
Vincent Coubard |
638:c90ae1400bf2 | 210 | err_code = dm_application_instance_set(&app_instance, &m_dm_handle); |
Vincent Coubard |
638:c90ae1400bf2 | 211 | APP_ERROR_CHECK(err_code); |
Vincent Coubard |
638:c90ae1400bf2 | 212 | } |