nrf_dfu_svci_handler.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <string.h>
  41. #include "nrf_svci_async_handler.h"
  42. #include "app_error.h"
  43. #include "nrf_nvmc.h"
  44. #include "nrf_dfu_types.h"
  45. #include "nrf_dfu_ble_svci_bond_sharing.h"
  46. #include "nrf_log.h"
  47. #include "nrf_dfu_settings.h"
  48. #include "sdk_config.h"
  49. #if (NRF_DFU_TRANSPORT_BLE && NRF_DFU_BLE_REQUIRES_BONDS)
  50. NRF_SVCI_ASYNC_HANDLER_CREATE(NRF_DFU_SVCI_SET_PEER_DATA,
  51. nrf_dfu_set_peer_data, nrf_dfu_peer_data_t, nrf_dfu_peer_data_state_t);
  52. static uint32_t nrf_dfu_set_peer_data_handler(nrf_dfu_set_peer_data_svci_async_t * p_async)
  53. {
  54. VERIFY_PARAM_NOT_NULL(p_async);
  55. p_async->async_func = nrf_dfu_set_peer_data_on_call;
  56. p_async->sys_evt_handler = nrf_dfu_set_peer_data_on_sys_evt;
  57. p_async->state = DFU_PEER_DATA_STATE_INITIALIZED;
  58. return NRF_SUCCESS;
  59. }
  60. static uint32_t nrf_dfu_set_peer_data_on_call(nrf_dfu_peer_data_t * p_data,
  61. nrf_dfu_peer_data_state_t * p_state)
  62. {
  63. uint32_t ret_val = NRF_ERROR_BUSY;
  64. VERIFY_PARAM_NOT_NULL(p_state);
  65. switch (*p_state)
  66. {
  67. case DFU_PEER_DATA_STATE_INVALID:
  68. return NRF_ERROR_INVALID_STATE;
  69. case DFU_PEER_DATA_STATE_INITIALIZED:
  70. ret_val = nrf_dfu_settings_peer_data_write(p_data);
  71. if (ret_val == NRF_SUCCESS)
  72. {
  73. *p_state = DFU_PEER_DATA_STATE_WRITE_REQUESTED;
  74. }
  75. break;
  76. case DFU_PEER_DATA_STATE_WRITE_REQUESTED:
  77. return NRF_ERROR_BUSY;
  78. case DFU_PEER_DATA_STATE_WRITE_FINISHED:
  79. return NRF_ERROR_INVALID_STATE;
  80. case DFU_PEER_DATA_STATE_WRITE_FAILED:
  81. return NRF_ERROR_INVALID_STATE;
  82. }
  83. return ret_val;
  84. }
  85. static uint32_t nrf_dfu_set_peer_data_on_sys_evt(uint32_t sys_event, nrf_dfu_peer_data_state_t * p_state)
  86. {
  87. uint32_t ret_val = NRF_ERROR_INVALID_STATE;
  88. VERIFY_PARAM_NOT_NULL(p_state);
  89. if (*p_state == DFU_PEER_DATA_STATE_WRITE_REQUESTED)
  90. {
  91. switch (sys_event)
  92. {
  93. case NRF_EVT_FLASH_OPERATION_ERROR:
  94. return NRF_ERROR_BUSY;
  95. case NRF_EVT_FLASH_OPERATION_SUCCESS:
  96. ret_val = NRF_SUCCESS;
  97. (*p_state) = DFU_PEER_DATA_STATE_WRITE_FINISHED;
  98. break;
  99. default:
  100. // Event not intended for us
  101. break;
  102. }
  103. }
  104. return ret_val;
  105. }
  106. #elif (NRF_DFU_TRANSPORT_BLE && !NRF_DFU_BLE_REQUIRES_BONDS)
  107. NRF_SVCI_ASYNC_HANDLER_CREATE(NRF_DFU_SVCI_SET_ADV_NAME,
  108. nrf_dfu_set_adv_name, nrf_dfu_adv_name_t, nrf_dfu_set_adv_name_state_t);
  109. static uint32_t nrf_dfu_set_adv_name_handler(nrf_dfu_set_adv_name_svci_async_t * p_async)
  110. {
  111. VERIFY_PARAM_NOT_NULL(p_async);
  112. p_async->async_func = nrf_dfu_set_adv_name_on_call;
  113. p_async->sys_evt_handler = nrf_dfu_set_adv_name_on_sys_evt;
  114. p_async->state = DFU_ADV_NAME_STATE_INITIALIZED;
  115. return NRF_SUCCESS;
  116. }
  117. static uint32_t nrf_dfu_set_adv_name_on_call(nrf_dfu_adv_name_t * p_adv_name,
  118. nrf_dfu_set_adv_name_state_t * p_state)
  119. {
  120. uint32_t ret_val = NRF_ERROR_BUSY;
  121. VERIFY_PARAM_NOT_NULL(p_state);
  122. switch (*p_state)
  123. {
  124. case DFU_ADV_NAME_STATE_INVALID:
  125. return NRF_ERROR_INVALID_STATE;
  126. case DFU_ADV_NAME_STATE_INITIALIZED:
  127. ret_val = nrf_dfu_settings_adv_name_write(p_adv_name);
  128. if (ret_val == NRF_SUCCESS)
  129. {
  130. *p_state = DFU_ADV_NAME_STATE_WRITE_REQUESTED;
  131. }
  132. break;
  133. case DFU_ADV_NAME_STATE_WRITE_REQUESTED:
  134. return NRF_ERROR_BUSY;
  135. case DFU_ADV_NAME_STATE_WRITE_FINISHED:
  136. return NRF_ERROR_INVALID_STATE;
  137. case DFU_ADV_NAME_STATE_WRITE_FAILED:
  138. return NRF_ERROR_INVALID_STATE;
  139. }
  140. return ret_val;
  141. }
  142. static uint32_t nrf_dfu_set_adv_name_on_sys_evt(uint32_t sys_event, nrf_dfu_set_adv_name_state_t * p_state)
  143. {
  144. uint32_t ret_val = NRF_ERROR_INVALID_STATE;
  145. VERIFY_PARAM_NOT_NULL(p_state);
  146. if (*p_state == DFU_ADV_NAME_STATE_WRITE_REQUESTED)
  147. {
  148. switch (sys_event)
  149. {
  150. case NRF_EVT_FLASH_OPERATION_ERROR:
  151. return NRF_ERROR_BUSY;
  152. case NRF_EVT_FLASH_OPERATION_SUCCESS:
  153. ret_val = NRF_SUCCESS;
  154. (*p_state) = DFU_ADV_NAME_STATE_WRITE_FINISHED;
  155. break;
  156. default:
  157. // Event not intended for us
  158. break;
  159. }
  160. }
  161. return ret_val;
  162. }
  163. #endif // NRF_DFU_TRANSPORT_BLE && !NRF_DFU_BLE_REQUIRES_BONDS