ant_encrypt_negotiation_slave.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2015 - 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(ANT_ENCRYPT_NEGOTIATION_SLAVE)
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "ant_encrypt_config.h"
  45. #include "ant_interface.h"
  46. #include "ant_parameters.h"
  47. #include "nrf_error.h"
  48. #include "app_error.h"
  49. #include "ant_encrypt_negotiation_slave.h"
  50. /** Number of supported channels. */
  51. #define NUMBER_OF_CHANNELS (NRF_SDH_ANT_TOTAL_CHANNELS_ALLOCATED)
  52. /** Flag to block other channels from attempting to enable encryption while
  53. * another encryption is in the process.
  54. */
  55. static volatile bool m_can_enable_crypto = true;
  56. /** Array to keep track of which channels are currently tracking. */
  57. static ant_encrypt_tracking_state_t m_encrypt_channel_states[NUMBER_OF_CHANNELS];
  58. /** Array for the slave channels' encryption settings. */
  59. static ant_encrypt_channel_settings_t m_slave_channel_conf[MAX_ANT_CHANNELS];
  60. void ant_channel_encryp_tracking_state_set(uint8_t channel_number,
  61. ant_encrypt_tracking_state_t state)
  62. {
  63. m_encrypt_channel_states[channel_number] = state;
  64. }
  65. void ant_channel_encryp_negotiation_slave_init(void)
  66. {
  67. for (uint32_t channel = 0; channel < NUMBER_OF_CHANNELS; channel++)
  68. {
  69. ant_channel_encryp_tracking_state_set(channel, ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  70. }
  71. m_can_enable_crypto = true;
  72. }
  73. ant_encrypt_tracking_state_t ant_channel_encryp_tracking_state_get(uint8_t channel_number)
  74. {
  75. return m_encrypt_channel_states[channel_number];
  76. }
  77. void ant_slave_channel_encrypt_config(uint8_t channel_number,
  78. ant_encrypt_channel_settings_t const * const p_crypto_config)
  79. {
  80. memcpy(&m_slave_channel_conf[channel_number], p_crypto_config,
  81. sizeof(ant_encrypt_channel_settings_t));
  82. }
  83. /**@brief Function for handling ANT RX channel events.
  84. *
  85. * @param[in] p_event_message_buffer The ANT event message buffer.
  86. */
  87. static void ant_slave_encrypt_try_enable(uint8_t ant_channel,
  88. uint8_t ant_message_id)
  89. {
  90. uint32_t err_code;
  91. ant_encrypt_tracking_state_t track_stat;
  92. switch (ant_message_id)
  93. {
  94. // Broadcast data received.
  95. case MESG_BROADCAST_DATA_ID:
  96. track_stat = ant_channel_encryp_tracking_state_get(ant_channel);
  97. // If the encryption has not yet been negotiated for this channel and another channel
  98. // is not currently trying to enable encryption, enable encryption
  99. if ((track_stat != ANT_ENC_CHANNEL_STAT_TRACKING_DECRYPTED)
  100. && (track_stat != ANT_ENC_CHANNEL_STAT_NEGOTIATING)
  101. && m_can_enable_crypto)
  102. {
  103. // Block other channels from trying to enable encryption until this channel
  104. // is finished
  105. m_can_enable_crypto = false;
  106. ant_channel_encryp_tracking_state_set(ant_channel,
  107. ANT_ENC_CHANNEL_STAT_NEGOTIATING);
  108. // Enable encryption on ant_channel
  109. err_code =
  110. ant_channel_encrypt_config_perform(ant_channel,
  111. &m_slave_channel_conf[ant_channel]);
  112. APP_ERROR_CHECK(err_code);
  113. }
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. void ant_slave_encrypt_negotiation(ant_evt_t * p_ant_evt)
  120. {
  121. ant_encrypt_tracking_state_t track_state =
  122. ant_channel_encryp_tracking_state_get(p_ant_evt->channel);
  123. if (track_state == ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED)
  124. return;
  125. switch (p_ant_evt->event)
  126. {
  127. case EVENT_RX_FAIL_GO_TO_SEARCH:
  128. if (track_state == ANT_ENC_CHANNEL_STAT_NEGOTIATING)
  129. {
  130. m_can_enable_crypto = true;
  131. }
  132. ant_channel_encryp_tracking_state_set(p_ant_evt->channel,
  133. ANT_ENC_CHANNEL_STAT_NOT_TRACKING);
  134. break;
  135. case EVENT_RX:
  136. ant_slave_encrypt_try_enable(p_ant_evt->channel,
  137. p_ant_evt->message.ANT_MESSAGE_ucMesgID);
  138. break;
  139. case EVENT_ENCRYPT_NEGOTIATION_SUCCESS:
  140. m_can_enable_crypto = true;
  141. ant_channel_encryp_tracking_state_set(p_ant_evt->channel,
  142. ANT_ENC_CHANNEL_STAT_TRACKING_DECRYPTED);
  143. break;
  144. case EVENT_ENCRYPT_NEGOTIATION_FAIL:
  145. m_can_enable_crypto = true;
  146. ant_channel_encryp_tracking_state_set(p_ant_evt->channel,
  147. ANT_ENC_CHANNEL_STAT_TRACKING_ENCRYPTED);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. #endif // NRF_MODULE_ENABLED(ANT_ENCRYPT_NEGOTIATION_SLAVE)