esd_manager.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file esd manager interface
  8. */
  9. #define SYS_LOG_NO_NEWLINE
  10. #ifdef SYS_LOG_DOMAIN
  11. #undef SYS_LOG_DOMAIN
  12. #endif
  13. #define SYS_LOG_DOMAIN "esd_manager"
  14. #include <os_common_api.h>
  15. #include <mem_manager.h>
  16. #include <msg_manager.h>
  17. #include <string.h>
  18. #ifdef CONFIG_ESD_MANAGER
  19. #include "esd_manager.h"
  20. #endif
  21. #ifdef CONFIG_PLAYTTS
  22. #include "tts_manager.h"
  23. #endif
  24. #ifdef CONFIG_THREAD_TIMER
  25. #include "thread_timer.h"
  26. #endif
  27. #define ESD_BK_FLAG 0x12345678
  28. #define BK_INFO_SIZE 250
  29. #define BK_INFO_FINISED_FLAG 0xAA
  30. OS_MUTEX_DEFINE(esd_manager_mutex);
  31. static struct thread_timer esd_reset_timer;
  32. typedef struct {
  33. uint32_t flag;
  34. uint8_t reset_flag;
  35. uint8_t bk_off;
  36. uint8_t bk_info[BK_INFO_SIZE];
  37. } esd_bk_info_t;/* max length is 0x100 bytes */
  38. static __in_section_unique(ESD_DATA) esd_bk_info_t gloable_esd_bk_info;
  39. static void _esd_manager_reset_timer(struct thread_timer *ttimer, void *expiry_fn_arg)
  40. {
  41. os_mutex_lock(&esd_manager_mutex, OS_FOREVER);
  42. if (esd_manager_check_esd()) {
  43. #ifdef CONFIG_PLAYTTS
  44. tts_manager_unlock();
  45. #endif
  46. esd_manager_reset_finished();
  47. }
  48. os_mutex_unlock(&esd_manager_mutex);
  49. }
  50. void esd_manager_init(void)
  51. {
  52. esd_bk_info_t *esd_bk_info = &gloable_esd_bk_info;
  53. /* print_buffer(&gloable_esd_bk_info, 1, sizeof(esd_bk_info_t), 16, 0); */
  54. if (esd_bk_info->flag == ESD_BK_FLAG) {
  55. esd_bk_info->reset_flag = 1;
  56. } else {
  57. esd_bk_info->reset_flag = 0;
  58. esd_bk_info->bk_off = 0;
  59. memset(&gloable_esd_bk_info, 0, sizeof(esd_bk_info_t));
  60. }
  61. thread_timer_init(&esd_reset_timer, _esd_manager_reset_timer, NULL);
  62. thread_timer_start(&esd_reset_timer, 20000, 0);
  63. SYS_LOG_INF("0x%x flag %d\n", esd_bk_info->flag, esd_bk_info->reset_flag);
  64. }
  65. bool esd_manager_check_esd(void)
  66. {
  67. esd_bk_info_t *esd_bk_info = &gloable_esd_bk_info;
  68. return (esd_bk_info->reset_flag == 1);
  69. }
  70. void esd_manager_reset_finished(void)
  71. {
  72. esd_bk_info_t *esd_bk_info = &gloable_esd_bk_info;
  73. esd_bk_info->reset_flag = 0;
  74. SYS_LOG_INF("esd finished\n");
  75. }
  76. int esd_manager_save_scene(int tag, uint8_t *value, int len)
  77. {
  78. int i = 0;
  79. int tmp_tag, tmp_len = 0;
  80. esd_bk_info_t *esd_bk_info = &gloable_esd_bk_info;
  81. if (esd_bk_info->bk_off + len + 2 > BK_INFO_SIZE) {
  82. return -ENOMEM;
  83. }
  84. os_mutex_lock(&esd_manager_mutex, OS_FOREVER);
  85. for (i = 0; i < BK_INFO_SIZE - 2;) {
  86. tmp_tag = esd_bk_info->bk_info[i];
  87. tmp_len = esd_bk_info->bk_info[i + 1];
  88. if (tmp_tag == tag && tmp_len >= len) {
  89. memcpy(&esd_bk_info->bk_info[i + 2], value, len);
  90. goto exit;
  91. }
  92. if (tmp_tag == BK_INFO_FINISED_FLAG) {
  93. break;
  94. }
  95. i += tmp_len + 2;
  96. }
  97. esd_bk_info->bk_info[esd_bk_info->bk_off] = tag;
  98. esd_bk_info->bk_info[esd_bk_info->bk_off + 1] = len;
  99. memcpy(&esd_bk_info->bk_info[esd_bk_info->bk_off + 2], value, len);
  100. esd_bk_info->bk_off += len + 2;
  101. esd_bk_info->bk_info[esd_bk_info->bk_off] = BK_INFO_FINISED_FLAG;
  102. esd_bk_info->flag = ESD_BK_FLAG;
  103. /* print_buffer(&gloable_esd_bk_info, 1, sizeof(esd_bk_info_t), 16, 0); */
  104. /* SYS_LOG_INF("tag %d value %x len %d\n", tag, value[0], len); */
  105. exit:
  106. os_mutex_unlock(&esd_manager_mutex);
  107. return 0;
  108. }
  109. int esd_manager_restore_scene(int tag, uint8_t *value, int len)
  110. {
  111. int result = -1;
  112. int i = 0;
  113. int tmp_tag, tmp_len = 0;
  114. esd_bk_info_t *esd_bk_info = &gloable_esd_bk_info;
  115. os_mutex_lock(&esd_manager_mutex, OS_FOREVER);
  116. for (i = 0; i < BK_INFO_SIZE - 2;) {
  117. tmp_tag = esd_bk_info->bk_info[i];
  118. tmp_len = esd_bk_info->bk_info[i + 1];
  119. if (tmp_tag == tag && tmp_len <= len) {
  120. memcpy(value, &esd_bk_info->bk_info[i + 2], tmp_len);
  121. result = 0;
  122. break;
  123. }
  124. if (tmp_tag == BK_INFO_FINISED_FLAG) {
  125. break;
  126. }
  127. i += tmp_len + 2;
  128. }
  129. /* SYS_LOG_INF("tag %d value %x len %d\n", tag, value[0], len); */
  130. /* print_buffer(&gloable_esd_bk_info, 1, sizeof(esd_bk_info_t), 16, 0); */
  131. os_mutex_unlock(&esd_manager_mutex);
  132. return result;
  133. }
  134. void esd_manager_deinit(void)
  135. {
  136. #if CONFIG_SYS_LOG_DEFAULT_LEVEL > 2
  137. esd_bk_info_t *esd_bk_info = &gloable_esd_bk_info;
  138. #endif
  139. memset(&gloable_esd_bk_info, 0, sizeof(esd_bk_info_t));
  140. #if CONFIG_SYS_LOG_DEFAULT_LEVEL > 2
  141. SYS_LOG_INF("0x%x flag %d\n", esd_bk_info->flag, esd_bk_info->reset_flag);
  142. #endif
  143. }