app_mw_nrf_soc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Copyright (c) 2014 - 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 "nrf_soc.h"
  41. #include <stdint.h>
  42. #include <string.h>
  43. #include "ser_sd_transport.h"
  44. #include "nrf_soc_app.h"
  45. #include "nrf_error_soc.h"
  46. #include "app_error.h"
  47. #include "ble_serialization.h"
  48. #include "ser_app_power_system_off.h"
  49. static void * mp_out_param;
  50. static void tx_buf_alloc(uint8_t * * p_data, uint16_t * p_len)
  51. {
  52. uint32_t err_code;
  53. do
  54. {
  55. err_code = ser_sd_transport_tx_alloc(p_data, p_len);
  56. }
  57. while (err_code != NRF_SUCCESS);
  58. *p_data[0] = SER_PKT_TYPE_CMD;
  59. *p_len -= 1;
  60. }
  61. uint32_t sd_power_system_off(void)
  62. {
  63. uint8_t * p_buffer;
  64. uint32_t buffer_length = 0;
  65. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  66. const uint32_t err_code = power_system_off_req_enc(&(p_buffer[1]), &buffer_length);
  67. APP_ERROR_CHECK(err_code);
  68. ser_app_power_system_off_set();
  69. //@note: Increment buffer length as internally managed packet type field must be included.
  70. return ser_sd_transport_cmd_write(p_buffer,
  71. (++buffer_length),
  72. NULL);
  73. }
  74. /**@brief Command response callback function for @ref sd_temp_get BLE command.
  75. *
  76. * Callback for decoding the output parameters and the command response return code.
  77. *
  78. * @param[in] p_buffer Pointer to begin of command response buffer.
  79. * @param[in] length Length of data in bytes.
  80. *
  81. * @return Decoded command response return code.
  82. */
  83. static uint32_t mw_temp_get_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  84. {
  85. uint32_t result_code;
  86. const uint32_t err_code = temp_get_rsp_dec(p_buffer,
  87. length,
  88. &result_code,
  89. (int32_t * *) &mp_out_param);
  90. APP_ERROR_CHECK(err_code);
  91. return result_code;
  92. }
  93. uint32_t sd_temp_get(int32_t * p_temp)
  94. {
  95. uint8_t * p_buffer;
  96. uint32_t buffer_length = 0;
  97. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  98. mp_out_param = p_temp;
  99. const uint32_t err_code = temp_get_req_enc(p_temp,
  100. &(p_buffer[1]),
  101. &buffer_length);
  102. APP_ERROR_CHECK(err_code);
  103. //@note: Increment buffer length as internally managed packet type field must be included.
  104. return ser_sd_transport_cmd_write(p_buffer,
  105. (++buffer_length),
  106. mw_temp_get_rsp_dec);
  107. }
  108. /**@brief Command response callback function for @ref sd_ecb_block_encrypt BLE command.
  109. *
  110. * Callback for decoding the output parameters and the command response return code.
  111. *
  112. * @param[in] p_buffer Pointer to begin of command response buffer.
  113. * @param[in] length Length of data in bytes.
  114. *
  115. * @return Decoded command response return code.
  116. */
  117. static uint32_t mw_ecb_block_encrypt_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  118. {
  119. uint32_t result_code;
  120. const uint32_t err_code = ecb_block_encrypt_rsp_dec(p_buffer,
  121. length,
  122. (nrf_ecb_hal_data_t * *)&mp_out_param,
  123. &result_code);
  124. APP_ERROR_CHECK(err_code);
  125. return result_code;
  126. }
  127. uint32_t sd_ecb_block_encrypt(nrf_ecb_hal_data_t * p_ecb_data)
  128. {
  129. uint8_t * p_buffer;
  130. uint32_t buffer_length = 0;
  131. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  132. mp_out_param = p_ecb_data;
  133. const uint32_t err_code = ecb_block_encrypt_req_enc(p_ecb_data,
  134. &(p_buffer[1]),
  135. &buffer_length);
  136. APP_ERROR_CHECK(err_code);
  137. //@note: Increment buffer length as internally managed packet type field must be included.
  138. return ser_sd_transport_cmd_write(p_buffer,
  139. (++buffer_length),
  140. mw_ecb_block_encrypt_rsp_dec);
  141. }