test
Fork of nRF51822 by
Embed:
(wiki syntax)
Show/hide line numbers
dfu_app_handler.c
00001 /* 00002 * Copyright (c) Nordic Semiconductor ASA 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without modification, 00006 * are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, this 00009 * list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, this 00012 * list of conditions and the following disclaimer in the documentation and/or 00013 * other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other 00016 * contributors to this software may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00023 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00024 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00027 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 * 00031 */ 00032 00033 #include <string.h> 00034 #include "dfu_app_handler.h " 00035 #include "bootloader_util.h " 00036 #include "nrf.h" 00037 #include "nrf_sdm.h" 00038 #include "ble_gatts.h" 00039 #include "app_error.h" 00040 #include "dfu_ble_svc.h " 00041 #include "device_manager.h " 00042 00043 #define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */ 00044 #define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */ 00045 00046 static void dfu_app_reset_prepare(void); /**< Forward declare of default reset handler. */ 00047 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 cleanup of service and memory prior to reset. */ 00048 #if NEEDED 00049 static dfu_ble_peer_data_t m_peer_data; /**< Peer data to be used for data exchange when reseting into DFU mode. */ 00050 static dm_handle_t m_dm_handle; /**< Device Manager handle with instance id's of current BLE connection. */ 00051 static bool m_dm_handle_valid = false; /**< Variable indicating if the Device Manager handle is valid. */ 00052 #endif /* NEEDED */ 00053 00054 00055 /**@brief Default reset prepare handler if application hasn't registered a handler. 00056 */ 00057 static void dfu_app_reset_prepare(void) 00058 { 00059 // Reset prepare should be handled by application. 00060 // This function can be extended to include default handling if application does not implement 00061 // own handler. 00062 } 00063 00064 00065 /**@brief Function for disabling all interrupts before jumping from bootloader to application. 00066 */ 00067 static void interrupts_disable(void) 00068 { 00069 uint32_t interrupt_setting_mask; 00070 uint32_t irq = 0; // We start from first interrupt, i.e. interrupt 0. 00071 00072 // Fetch the current interrupt settings. 00073 interrupt_setting_mask = NVIC->ISER[0]; 00074 00075 for (; irq < MAX_NUMBER_INTERRUPTS; irq++) 00076 { 00077 if (interrupt_setting_mask & (IRQ_ENABLED << irq)) 00078 { 00079 // The interrupt was enabled, and hence disable it. 00080 NVIC_DisableIRQ((IRQn_Type )irq); 00081 } 00082 } 00083 } 00084 00085 #if NEEDED 00086 /**@brief Function for providing peer information to DFU for re-establishing bonded connection in 00087 * DFU mode. 00088 */ 00089 static void dfu_app_set_peer_data(void) 00090 { 00091 uint32_t err_code; 00092 dm_sec_keyset_t key_set; 00093 00094 /** [DFU bond sharing] */ 00095 err_code = dm_distributed_keys_get(&m_dm_handle, &key_set); 00096 APP_ERROR_CHECK(err_code); 00097 00098 m_peer_data.addr = key_set.keys_central.p_id_key->id_addr_info; 00099 m_peer_data.enc_key = *((ble_gap_enc_key_t *)(key_set.keys_central.enc_key.p_enc_key)); 00100 m_peer_data.irk = key_set.keys_central.p_id_key->id_info; 00101 00102 err_code = dfu_ble_svc_set_peer_data(&m_peer_data); 00103 APP_ERROR_CHECK(err_code); 00104 /** [DFU bond sharing] */ 00105 } 00106 #endif /* NEEDED */ 00107 00108 00109 /**@brief Function for preparing the reset, disabling SoftDevice and jump to the bootloader. 00110 */ 00111 void bootloader_start(void) 00112 { 00113 m_reset_prepare(); 00114 00115 uint32_t err_code = sd_power_gpregret_set(BOOTLOADER_DFU_START); 00116 APP_ERROR_CHECK(err_code); 00117 00118 err_code = sd_softdevice_disable(); 00119 APP_ERROR_CHECK(err_code); 00120 00121 err_code = sd_softdevice_vector_table_base_set(NRF_UICR->BOOTLOADERADDR); 00122 APP_ERROR_CHECK(err_code); 00123 00124 // Commenting out the following block because it brings in unwanted dependencies from bonding. 00125 // TODO: discuss this with Nordic. 00126 // if (m_dm_handle_valid) 00127 // { 00128 // dfu_app_set_peer_data(); 00129 // } 00130 00131 NVIC_ClearPendingIRQ(SWI2_IRQn ); 00132 interrupts_disable(); 00133 bootloader_util_app_start(NRF_UICR->BOOTLOADERADDR); 00134 } 00135 00136 00137 void dfu_app_on_dfu_evt(ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt) 00138 { 00139 switch (p_evt->ble_dfu_evt_type) 00140 { 00141 case BLE_DFU_START: 00142 // Starting the bootloader - will cause reset. 00143 bootloader_start(); 00144 break; 00145 00146 case BLE_DFU_VALIDATE: 00147 case BLE_DFU_ACTIVATE_N_RESET: 00148 case BLE_DFU_SYS_RESET: 00149 case BLE_DFU_RECEIVE_INIT_DATA: 00150 case BLE_DFU_RECEIVE_APP_DATA: 00151 case BLE_DFU_PACKET_WRITE: 00152 case BLE_DFU_PKT_RCPT_NOTIF_ENABLED: 00153 case BLE_DFU_PKT_RCPT_NOTIF_DISABLED: 00154 case BLE_DFU_BYTES_RECEIVED_SEND: 00155 default: 00156 { 00157 // Unsupported event received from DFU Service. 00158 // Send back BLE_DFU_RESP_VAL_NOT_SUPPORTED message to peer. 00159 uint32_t err_code = ble_dfu_response_send(p_dfu, 00160 BLE_DFU_START_PROCEDURE, 00161 BLE_DFU_RESP_VAL_NOT_SUPPORTED); 00162 APP_ERROR_CHECK(err_code); 00163 } 00164 break; 00165 } 00166 } 00167 00168 00169 void dfu_app_reset_prepare_set(dfu_app_reset_prepare_t reset_prepare_func) 00170 { 00171 m_reset_prepare = reset_prepare_func; 00172 } 00173 00174 #if NEEDED 00175 void dfu_app_set_dm_handle(dm_handle_t const * p_dm_handle) 00176 { 00177 m_dm_handle_valid = false; 00178 00179 if (p_dm_handle != NULL) 00180 { 00181 m_dm_handle = *p_dm_handle; 00182 m_dm_handle_valid = true; 00183 } 00184 } 00185 #endif /* NEEDED */
Generated on Tue Jul 12 2022 21:00:16 by 1.7.2