background_dfu_operation.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) 2017 - 2019, 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. /** @file
  41. *
  42. * @defgroup background_dfu_operation background_dfu_operation.c
  43. * @{
  44. * @ingroup background_dfu
  45. * @brief Background DFU operations implementation.
  46. *
  47. */
  48. #include "background_dfu_operation.h"
  49. #include "sdk_config.h"
  50. #include "nrf_dfu_req_handler.h"
  51. #define NRF_LOG_MODULE_NAME background_dfu
  52. #define NRF_LOG_LEVEL BACKGROUND_DFU_CONFIG_LOG_LEVEL
  53. #define NRF_LOG_INFO_COLOR BACKGROUND_DFU_CONFIG_INFO_COLOR
  54. #define NRF_LOG_DEBUG_COLOR BACKGROUND_DFU_CONFIG_DEBUG_COLOR
  55. #include "nrf_log.h"
  56. ret_code_t background_dfu_op_select(uint32_t object_type,
  57. nrf_dfu_response_callback_t callback,
  58. void * p_context)
  59. {
  60. nrf_dfu_request_t dfu_req;
  61. memset(&dfu_req, 0, sizeof(dfu_req));
  62. dfu_req.request = NRF_DFU_OP_OBJECT_SELECT;
  63. dfu_req.select.object_type = object_type;
  64. dfu_req.p_context = p_context;
  65. dfu_req.callback.response = callback;
  66. return nrf_dfu_req_handler_on_req(&dfu_req);
  67. }
  68. ret_code_t background_dfu_op_create(uint32_t object_type,
  69. uint32_t object_size,
  70. nrf_dfu_response_callback_t callback,
  71. void * p_context)
  72. {
  73. nrf_dfu_request_t dfu_req;
  74. memset(&dfu_req, 0, sizeof(dfu_req));
  75. dfu_req.request = NRF_DFU_OP_OBJECT_CREATE;
  76. dfu_req.create.object_size = object_size;
  77. dfu_req.create.object_type = object_type;
  78. dfu_req.p_context = p_context;
  79. dfu_req.callback.response = callback;
  80. return nrf_dfu_req_handler_on_req(&dfu_req);
  81. }
  82. ret_code_t background_dfu_op_write(const uint8_t * p_payload,
  83. uint16_t payload_length,
  84. nrf_dfu_response_callback_t response_callback,
  85. nrf_dfu_flash_callback_t write_callback,
  86. void * p_context)
  87. {
  88. nrf_dfu_request_t dfu_req;
  89. memset(&dfu_req, 0, sizeof(dfu_req));
  90. dfu_req.request = NRF_DFU_OP_OBJECT_WRITE;
  91. dfu_req.write.p_data = (uint8_t *)p_payload;
  92. dfu_req.write.len = payload_length;
  93. dfu_req.p_context = p_context;
  94. dfu_req.callback.response = response_callback;
  95. dfu_req.callback.write = write_callback;
  96. return nrf_dfu_req_handler_on_req(&dfu_req);
  97. }
  98. ret_code_t background_dfu_op_crc(nrf_dfu_response_callback_t callback,
  99. void * p_context)
  100. {
  101. nrf_dfu_request_t dfu_req;
  102. memset(&dfu_req, 0, sizeof(dfu_req));
  103. dfu_req.request = NRF_DFU_OP_CRC_GET;
  104. dfu_req.p_context = p_context;
  105. dfu_req.callback.response = callback;
  106. return nrf_dfu_req_handler_on_req(&dfu_req);
  107. }
  108. ret_code_t background_dfu_op_execute(nrf_dfu_response_callback_t callback,
  109. void * p_context)
  110. {
  111. nrf_dfu_request_t dfu_req;
  112. memset(&dfu_req, 0, sizeof(dfu_req));
  113. dfu_req.request = NRF_DFU_OP_OBJECT_EXECUTE;
  114. dfu_req.p_context = p_context;
  115. dfu_req.callback.response = callback;
  116. return nrf_dfu_req_handler_on_req(&dfu_req);
  117. }