watchdog.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "watchdog.h"
  6. #include "bh_platform.h"
  7. #define WATCHDOG_THREAD_PRIORITY 5
  8. /* Queue of watchdog */
  9. static bh_queue *watchdog_queue;
  10. #ifdef WATCHDOG_ENABLED /* TODO */
  11. static void
  12. watchdog_timer_callback(void *timer)
  13. {
  14. watchdog_timer *wd_timer =
  15. app_manager_get_wd_timer_from_timer_handle(timer);
  16. watchdog_timer_stop(wd_timer);
  17. os_mutex_lock(&wd_timer->lock);
  18. if (!wd_timer->is_stopped) {
  19. wd_timer->is_interrupting = true;
  20. bh_post_msg(watchdog_queue, WD_TIMEOUT, wd_timer->module_data,
  21. sizeof(module_data));
  22. }
  23. os_mutex_unlock(&wd_timer->lock);
  24. }
  25. #endif
  26. bool
  27. watchdog_timer_init(module_data *m_data)
  28. {
  29. #ifdef WATCHDOG_ENABLED /* TODO */
  30. watchdog_timer *wd_timer = &m_data->wd_timer;
  31. if (0 != os_mutex_init(&wd_timer->lock))
  32. return false;
  33. if (!(wd_timer->timer_handle =
  34. app_manager_timer_create(watchdog_timer_callback, wd_timer))) {
  35. os_mutex_destroy(&wd_timer->lock);
  36. return false;
  37. }
  38. wd_timer->module_data = m_data;
  39. wd_timer->is_interrupting = false;
  40. wd_timer->is_stopped = false;
  41. #endif
  42. return true;
  43. }
  44. void
  45. watchdog_timer_destroy(watchdog_timer *wd_timer)
  46. {
  47. #ifdef WATCHDOG_ENABLED /* TODO */
  48. app_manager_timer_destroy(wd_timer->timer_handle);
  49. os_mutex_destroy(&wd_timer->lock);
  50. #endif
  51. }
  52. void
  53. watchdog_timer_start(watchdog_timer *wd_timer)
  54. {
  55. os_mutex_lock(&wd_timer->lock);
  56. wd_timer->is_interrupting = false;
  57. wd_timer->is_stopped = false;
  58. app_manager_timer_start(wd_timer->timer_handle,
  59. wd_timer->module_data->timeout);
  60. os_mutex_unlock(&wd_timer->lock);
  61. }
  62. void
  63. watchdog_timer_stop(watchdog_timer *wd_timer)
  64. {
  65. app_manager_timer_stop(wd_timer->timer_handle);
  66. }
  67. #ifdef WATCHDOG_ENABLED /* TODO */
  68. static void
  69. watchdog_queue_callback(void *queue_msg)
  70. {
  71. if (bh_message_type(queue_msg) == WD_TIMEOUT) {
  72. module_data *m_data = (module_data *)bh_message_payload(queue_msg);
  73. if (g_module_interfaces[m_data->module_type]
  74. && g_module_interfaces[m_data->module_type]->module_watchdog_kill) {
  75. g_module_interfaces[m_data->module_type]->module_watchdog_kill(
  76. m_data);
  77. app_manager_post_applets_update_event();
  78. }
  79. }
  80. }
  81. #endif
  82. #ifdef WATCHDOG_ENABLED /* TODO */
  83. static void *
  84. watchdog_thread_routine(void *arg)
  85. {
  86. /* Enter loop run */
  87. bh_queue_enter_loop_run(watchdog_queue, watchdog_queue_callback);
  88. (void)arg;
  89. return NULL;
  90. }
  91. #endif
  92. bool
  93. watchdog_startup()
  94. {
  95. if (!(watchdog_queue = bh_queue_create())) {
  96. app_manager_printf(
  97. "App Manager start failed: create watchdog queue failed.\n");
  98. return false;
  99. }
  100. #if 0
  101. //todo: enable watchdog
  102. /* Start watchdog thread */
  103. if (!jeff_runtime_create_supervisor_thread_with_prio(watchdog_thread_routine, NULL,
  104. WATCHDOG_THREAD_PRIORITY)) {
  105. bh_queue_destroy(watchdog_queue);
  106. return false;
  107. }
  108. #endif
  109. return true;
  110. }
  111. void
  112. watchdog_destroy()
  113. {
  114. bh_queue_exit_loop_run(watchdog_queue);
  115. bh_queue_destroy(watchdog_queue);
  116. }