dfu_app_handler.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "dfu_app_handler.h"
  13. #include <string.h>
  14. #include "bootloader_util.h"
  15. #include "nrf.h"
  16. #include "nrf_sdm.h"
  17. #include "ble_gatt.h"
  18. #include "ble_gatts.h"
  19. #include "app_error.h"
  20. #include "dfu_ble_svc.h"
  21. #include "device_manager.h"
  22. #include "nrf_delay.h"
  23. #define IRQ_ENABLED 0x01 /**< Field that identifies if an interrupt is enabled. */
  24. #define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */
  25. static void dfu_app_reset_prepare(void); /**< Forward declaration of default reset handler. */
  26. 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. */
  27. static dfu_ble_peer_data_t m_peer_data; /**< Peer data to be used for data exchange when resetting into DFU mode. */
  28. static dm_handle_t m_dm_handle; /**< Device Manager handle with instance IDs of current BLE connection. */
  29. /**@brief Function for reset_prepare handler if the application has not registered a handler.
  30. */
  31. static void dfu_app_reset_prepare(void)
  32. {
  33. // Reset prepare should be handled by application.
  34. // This function can be extended to include default handling if application does not implement
  35. // own handler.
  36. }
  37. /**@brief Function for disabling all interrupts before jumping from bootloader to application.
  38. */
  39. static void interrupts_disable(void)
  40. {
  41. uint32_t interrupt_setting_mask;
  42. uint32_t irq;
  43. // Fetch the current interrupt settings.
  44. interrupt_setting_mask = NVIC->ISER[0];
  45. // Loop from interrupt 0 for disabling of all interrupts.
  46. for (irq = 0; irq < MAX_NUMBER_INTERRUPTS; irq++)
  47. {
  48. if (interrupt_setting_mask & (IRQ_ENABLED << irq))
  49. {
  50. // The interrupt was enabled, hence disable it.
  51. NVIC_DisableIRQ((IRQn_Type)irq);
  52. }
  53. }
  54. }
  55. /**@brief Function for providing peer information to DFU for re-establishing a bonded connection in
  56. * DFU mode.
  57. *
  58. * @param[in] conn_handle Connection handle for the connection requesting DFU mode.
  59. */
  60. static void dfu_app_peer_data_set(uint16_t conn_handle)
  61. {
  62. uint32_t err_code;
  63. dm_sec_keyset_t key_set;
  64. uint32_t app_context_data = 0;
  65. dm_application_context_t app_context;
  66. /** [DFU bond sharing] */
  67. err_code = dm_handle_get(conn_handle, &m_dm_handle);
  68. if (err_code == NRF_SUCCESS)
  69. {
  70. err_code = dm_distributed_keys_get(&m_dm_handle, &key_set);
  71. if (err_code == NRF_SUCCESS)
  72. {
  73. APP_ERROR_CHECK(err_code);
  74. m_peer_data.addr = key_set.keys_central.p_id_key->id_addr_info;
  75. m_peer_data.irk = key_set.keys_central.p_id_key->id_info;
  76. m_peer_data.enc_key.enc_info = key_set.keys_periph.enc_key.p_enc_key->enc_info;
  77. m_peer_data.enc_key.master_id = key_set.keys_periph.enc_key.p_enc_key->master_id;
  78. err_code = dfu_ble_svc_peer_data_set(&m_peer_data);
  79. APP_ERROR_CHECK(err_code);
  80. app_context_data = (DFU_APP_ATT_TABLE_CHANGED << DFU_APP_ATT_TABLE_POS);
  81. app_context.len = sizeof(app_context_data);
  82. app_context.p_data = (uint8_t *)&app_context_data;
  83. app_context.flags = 0;
  84. err_code = dm_application_context_set(&m_dm_handle, &app_context);
  85. APP_ERROR_CHECK(err_code);
  86. }
  87. else
  88. {
  89. // Keys were not available, thus we have a non-encrypted connection.
  90. err_code = dm_peer_addr_get(&m_dm_handle, &m_peer_data.addr);
  91. APP_ERROR_CHECK(err_code);
  92. err_code = dfu_ble_svc_peer_data_set(&m_peer_data);
  93. APP_ERROR_CHECK(err_code);
  94. }
  95. }
  96. /** [DFU bond sharing] */
  97. }
  98. /**@brief Function for preparing the reset, disabling SoftDevice, and jumping to the bootloader.
  99. *
  100. * @param[in] conn_handle Connection handle for peer requesting to enter DFU mode.
  101. */
  102. static void bootloader_start(uint16_t conn_handle)
  103. {
  104. uint32_t err_code;
  105. uint16_t sys_serv_attr_len = sizeof(m_peer_data.sys_serv_attr);
  106. err_code = sd_ble_gatts_sys_attr_get(conn_handle,
  107. m_peer_data.sys_serv_attr,
  108. &sys_serv_attr_len,
  109. BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
  110. if (err_code != NRF_SUCCESS)
  111. {
  112. // Any error at this stage means the system service attributes could not be fetched.
  113. // This means the service changed indication cannot be sent in DFU mode, but connection
  114. // is still possible to establish.
  115. }
  116. m_reset_prepare();
  117. err_code = sd_power_gpregret_set(BOOTLOADER_DFU_START);
  118. APP_ERROR_CHECK(err_code);
  119. err_code = sd_softdevice_disable();
  120. APP_ERROR_CHECK(err_code);
  121. err_code = sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]);
  122. APP_ERROR_CHECK(err_code);
  123. dfu_app_peer_data_set(conn_handle);
  124. NVIC_ClearPendingIRQ(SWI2_IRQn);
  125. interrupts_disable();
  126. bootloader_util_app_start(NRF_UICR->NRFFW[0]);
  127. }
  128. void dfu_app_on_dfu_evt(ble_dfu_t * p_dfu, ble_dfu_evt_t * p_evt)
  129. {
  130. switch (p_evt->ble_dfu_evt_type)
  131. {
  132. case BLE_DFU_START:
  133. // Starting the bootloader - will cause reset.
  134. bootloader_start(p_dfu->conn_handle);
  135. break;
  136. default:
  137. {
  138. // Unsupported event received from DFU Service.
  139. // Send back BLE_DFU_RESP_VAL_NOT_SUPPORTED message to peer.
  140. uint32_t err_code = ble_dfu_response_send(p_dfu,
  141. BLE_DFU_START_PROCEDURE,
  142. BLE_DFU_RESP_VAL_NOT_SUPPORTED);
  143. APP_ERROR_CHECK(err_code);
  144. }
  145. break;
  146. }
  147. }
  148. void dfu_app_reset_prepare_set(dfu_app_reset_prepare_t reset_prepare_func)
  149. {
  150. m_reset_prepare = reset_prepare_func;
  151. }
  152. void dfu_app_dm_appl_instance_set(dm_application_instance_t app_instance)
  153. {
  154. uint32_t err_code;
  155. err_code = dm_application_instance_set(&app_instance, &m_dm_handle);
  156. APP_ERROR_CHECK(err_code);
  157. }