ant_state_indicator.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_STATE_INDICATOR)
  42. #include "ant_state_indicator.h"
  43. #include "bsp.h"
  44. #include "app_error.h"
  45. #include "nrf_sdh.h"
  46. #include "nrf_sdh_ant.h"
  47. #include "nrf_pwr_mgmt.h"
  48. static uint8_t m_channel = UINT8_MAX; /**< ANT channel number linked to indication. */
  49. static uint8_t m_channel_type; /**< Type of linked ANT channel. */
  50. ret_code_t ant_state_indicator_init( uint8_t channel, uint8_t channel_type)
  51. {
  52. m_channel = channel;
  53. m_channel_type = channel_type;
  54. return bsp_indication_set(BSP_INDICATE_IDLE);
  55. }
  56. ret_code_t ant_state_indicator_channel_opened(void)
  57. {
  58. ret_code_t err_code = NRF_SUCCESS;
  59. switch (m_channel_type)
  60. {
  61. case CHANNEL_TYPE_SLAVE:
  62. /* fall through */
  63. case CHANNEL_TYPE_SLAVE_RX_ONLY:
  64. err_code = bsp_indication_set(BSP_INDICATE_SCANNING);
  65. break;
  66. case CHANNEL_TYPE_MASTER:
  67. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  68. break;
  69. }
  70. return err_code;
  71. }
  72. /**
  73. * @brief Function for handling ANT events.
  74. *
  75. * @param[in] p_ant_evt Event received from the ANT stack.
  76. * @param[in] p_context Context.
  77. */
  78. static void ant_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  79. {
  80. ret_code_t err_code = NRF_SUCCESS;
  81. if (m_channel != p_ant_evt->channel)
  82. {
  83. return;
  84. }
  85. switch (m_channel_type)
  86. {
  87. case CHANNEL_TYPE_SLAVE:
  88. /* fall through */
  89. case CHANNEL_TYPE_SLAVE_RX_ONLY:
  90. switch (p_ant_evt->event)
  91. {
  92. case EVENT_RX:
  93. err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  94. break;
  95. case EVENT_RX_FAIL:
  96. err_code = bsp_indication_set(BSP_INDICATE_RCV_ERROR);
  97. break;
  98. case EVENT_RX_FAIL_GO_TO_SEARCH:
  99. err_code = bsp_indication_set(BSP_INDICATE_SCANNING);
  100. break;
  101. case EVENT_CHANNEL_CLOSED:
  102. nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
  103. break;
  104. case EVENT_RX_SEARCH_TIMEOUT:
  105. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  106. break;
  107. }
  108. break;
  109. }
  110. APP_ERROR_CHECK(err_code);
  111. }
  112. NRF_SDH_ANT_OBSERVER(m_ant_observer, ANT_STATE_INDICATOR_ANT_OBSERVER_PRIO, ant_evt_handler, NULL);
  113. /**
  114. * @brief Function for shutdown events.
  115. *
  116. * @param[in] event Shutdown type.
  117. */
  118. static bool m_shutdown_handler(nrf_pwr_mgmt_evt_t event)
  119. {
  120. ret_code_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  121. APP_ERROR_CHECK(err_code);
  122. return true;
  123. }
  124. NRF_PWR_MGMT_HANDLER_REGISTER(m_shutdown_handler, ANT_STATE_INDICATOR_CONFIG_SHUTDOWN_HANDLER_PRIORITY);
  125. #endif // NRF_MODULE_ENABLED(ANT_STATE_INDICATOR)