ser_conn_reset_cmd_decoder.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_nvic.h"
  41. #include "ser_conn_reset_cmd_decoder.h"
  42. #include "app_scheduler.h"
  43. #include "nrf_sdh.h"
  44. #include "app_error.h"
  45. #include "ble_serialization.h"
  46. #include "nrf_log.h"
  47. #include "ser_conn_handlers.h"
  48. #include "ser_hal_transport.h"
  49. #include "app_timer.h"
  50. #ifdef SER_PHY_HCI
  51. #include "ser_phy_hci.h"
  52. #endif
  53. APP_TIMER_DEF(reset_timer);
  54. bool m_reset_ongoing;
  55. static bool m_timer_created;
  56. #ifdef SER_PHY_HCI
  57. extern void ser_phy_hci_reset(void);
  58. #endif
  59. static void sd_start_from_app_sched(void * p_event_data, uint16_t event_size)
  60. {
  61. NRF_LOG_DEBUG("sdh enable request.");
  62. (void)nrf_sdh_enable_request();
  63. m_reset_ongoing = false;
  64. }
  65. static void sdh_observer_handler(nrf_sdh_state_evt_t state, void * p_context)
  66. {
  67. if (state == NRF_SDH_EVT_STATE_DISABLED)
  68. {
  69. APP_ERROR_CHECK(app_sched_event_put(NULL, 0, sd_start_from_app_sched));
  70. app_sched_resume();
  71. }
  72. else if (state == NRF_SDH_EVT_STATE_ENABLED)
  73. {
  74. NRF_LOG_DEBUG("sdh enabled.");
  75. }
  76. else
  77. {
  78. /* empty */
  79. }
  80. }
  81. NRF_SDH_STATE_OBSERVER(sdh_observer, 0) =
  82. {
  83. .handler = sdh_observer_handler
  84. };
  85. void timer_handler(void * p_ctx)
  86. {
  87. NRF_LOG_DEBUG("transport reset");
  88. ser_hal_transport_reset();
  89. #ifdef SER_PHY_HCI
  90. ser_phy_hci_reset();
  91. ser_phy_hci_slip_reset();
  92. #endif
  93. }
  94. bool soft_reset_trigger(void)
  95. {
  96. if (m_reset_ongoing)
  97. {
  98. return false;
  99. }
  100. m_reset_ongoing = true;
  101. NRF_LOG_INFO("reset ongoing");
  102. if (!m_timer_created)
  103. {
  104. (void)app_timer_create(&reset_timer, APP_TIMER_MODE_SINGLE_SHOT, timer_handler);
  105. }
  106. (void)app_timer_start(reset_timer, APP_TIMER_TICKS(100), NULL);
  107. (void)nrf_sdh_disable_request();
  108. #ifdef BLE_STACK_SUPPORT_REQD
  109. ser_conn_reset();
  110. #endif
  111. return true;
  112. }
  113. void ser_conn_generic_command_process(uint8_t * p_command, uint16_t command_len)
  114. {
  115. switch (p_command[0])
  116. {
  117. case SER_GENERIC_CMD_RESET:
  118. (void)sd_nvic_SystemReset();
  119. break;
  120. case SER_GENERIC_CMD_SOFT_RESET:
  121. (void)soft_reset_trigger();
  122. break;
  123. default:
  124. break;
  125. }
  126. }