alarm_main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief alarm app main.
  9. */
  10. #include "alarm.h"
  11. #include "tts_manager.h"
  12. #ifdef CONFIG_PROPERTY
  13. #include "property_manager.h"
  14. #endif
  15. #include <os_common_api.h>
  16. static struct alarm_app_t *p_alarm = NULL;
  17. os_delayed_work alarm_callback_work;
  18. static void _alarm_isr_work(struct k_work *work);
  19. OS_DELAY_WORK_DEFINE(alarm_callback_work, _alarm_isr_work);
  20. void alarm_event_cb(void)
  21. {
  22. SYS_LOG_INF("\n");
  23. os_delayed_work_submit(&alarm_callback_work, 0);
  24. }
  25. void alarm_start_msg_send(void)
  26. {
  27. struct app_msg msg = { 0 };
  28. msg.type = MSG_ALARM_EVENT;
  29. msg.cmd= MSG_ALARM_PROC_START;
  30. SYS_LOG_INF("start by alarm\n");
  31. send_async_msg(app_manager_get_current_app(), &msg);
  32. }
  33. void alarm_exit_msg_send(void)
  34. {
  35. struct app_msg msg = { 0 };
  36. msg.type = MSG_ALARM_EVENT;
  37. msg.cmd= MSG_ALARM_PROC_EXIT;
  38. SYS_LOG_INF("exit alarm\n");
  39. send_async_msg(app_manager_get_current_app(), &msg);
  40. }
  41. static void _alarm_isr_work(struct k_work *work)
  42. {
  43. #ifdef CONFIG_LAUNCHER_APP
  44. alarm_start_msg_send();
  45. #else
  46. struct app_msg msg = { 0 };
  47. if (memcmp(app_manager_get_current_app(), APP_ID_ALARM, strlen(APP_ID_ALARM)) == 0) {
  48. SYS_LOG_INF("prev alarm is playing\n");
  49. } else if (memcmp(app_manager_get_current_app(), APP_ID_BTCALL, strlen(APP_ID_BTCALL)) == 0) {
  50. SYS_LOG_INF("in btcall\n");
  51. } else {
  52. if (p_alarm) {/*exit alarm display*/
  53. msg.type = MSG_INPUT_EVENT;
  54. msg.cmd = MSG_ALARM_ENTRY_EXIT;
  55. msg.reserve = 0x01;
  56. send_async_msg(APP_ID_MAIN, &msg);
  57. }
  58. msg.type = MSG_START_APP;
  59. msg.ptr = APP_ID_ALARM;
  60. msg.reserve = APP_SWITCH_CURR;
  61. send_async_msg(APP_ID_MAIN, &msg);
  62. SYS_LOG_INF("start by alarm\n");
  63. }
  64. #endif
  65. }
  66. static int _alarm_init(void)
  67. {
  68. if (p_alarm)
  69. return 0;
  70. p_alarm = app_mem_malloc(sizeof(struct alarm_app_t));
  71. if (!p_alarm) {
  72. SYS_LOG_ERR("malloc failed!\n");
  73. return -ENOMEM;
  74. }
  75. memset(p_alarm, 0, sizeof(struct alarm_app_t));
  76. #ifdef CONFIG_UI_MANAGER
  77. alarm_view_init();
  78. #endif
  79. alarm_manager_init();
  80. alarm_thread_timer_init();
  81. //system_registry_alarm_callback(alarm_event_cb);
  82. //os_delayed_work_init(&alarm_callback_work, _alarm_isr_work);
  83. SYS_LOG_INF("ok\n");
  84. return 0;
  85. }
  86. void alarm_exit(bool need_switch_last)
  87. {
  88. if (!p_alarm)
  89. goto exit;
  90. #ifdef CONFIG_THREAD_TIMER
  91. if (thread_timer_is_running(&p_alarm->monitor_timer))
  92. thread_timer_stop(&p_alarm->monitor_timer);
  93. if (thread_timer_is_running(&p_alarm->play_timer))
  94. thread_timer_stop(&p_alarm->play_timer);
  95. #endif
  96. alarm_ring_stop(p_alarm, false);
  97. #ifdef CONFIG_LOCAL_PLAYER
  98. alarm_exit_iterator();
  99. #endif
  100. #ifdef CONFIG_UI_MANAGER
  101. alarm_view_deinit();
  102. #endif
  103. app_mem_free(p_alarm);
  104. p_alarm = NULL;
  105. #ifdef CONFIG_PROPERTY
  106. property_flush(NULL);
  107. #endif
  108. if (alarm_wakeup_source_check()) {
  109. find_and_set_alarm();
  110. sys_event_send_message(MSG_POWER_OFF);
  111. }
  112. exit:
  113. find_and_set_alarm();
  114. if (memcmp(app_manager_get_current_app(), APP_ID_ALARM, strlen(APP_ID_ALARM)) == 0) {
  115. app_manager_thread_exit(APP_ID_ALARM);
  116. } else {
  117. #ifdef CONFIG_LAUNCHER_APP
  118. if (need_switch_last)
  119. alarm_switch_to_last();
  120. #endif
  121. }
  122. SYS_LOG_INF("ok\n");
  123. }
  124. struct alarm_app_t *alarm_get_app(void)
  125. {
  126. return p_alarm;
  127. }
  128. void alarm_need_exit(void)
  129. {
  130. if (memcmp(app_manager_get_current_app(), APP_ID_ALARM, strlen(APP_ID_ALARM)) && p_alarm) {
  131. struct app_msg msg = { 0 };
  132. msg.type = MSG_INPUT_EVENT;
  133. msg.cmd = MSG_ALARM_ENTRY_EXIT;
  134. msg.reserve = 0x01;
  135. send_async_msg(APP_ID_MAIN, &msg);
  136. }
  137. }
  138. void alarm_entry_exit(uint8_t force_exit)
  139. {
  140. struct app_msg msg = { 0 };
  141. SYS_LOG_INF("force_exit=%d, p_alarm= %p\n", force_exit, p_alarm);
  142. if (memcmp(app_manager_get_current_app(), APP_ID_ALARM, strlen(APP_ID_ALARM)) == 0) {
  143. /*TBD KEY_TYPE_DOUBLE_CLICK not exit app,when alarm playing*/
  144. return;
  145. } else {
  146. if (p_alarm) {
  147. alarm_exit(true);
  148. return;
  149. } else {
  150. if (force_exit || (memcmp(app_manager_get_current_app(), APP_ID_BTCALL, strlen(APP_ID_BTCALL)) == 0))
  151. return;
  152. _alarm_init();
  153. }
  154. msg.cmd = MSG_ALARM_CHANGE_MODE;
  155. msg.type = MSG_INPUT_EVENT;
  156. msg.reserve = ALARM_PLAYER;
  157. send_async_msg(APP_ID_MAIN, &msg);
  158. }
  159. }
  160. static bool _alarm_disk_ready_check(void)
  161. {
  162. if (fs_manager_get_volume_state("/USB:")) {
  163. strncpy(p_alarm->dir, "/USB:/", sizeof(p_alarm->dir));
  164. } else if (fs_manager_get_volume_state("/SD:")) {
  165. strncpy(p_alarm->dir, "/SD:/", sizeof(p_alarm->dir));
  166. } else if (fs_manager_get_volume_state("/NAND:")) {
  167. strncpy(p_alarm->dir, "/NAND:/", sizeof(p_alarm->dir));
  168. } else if (fs_manager_get_volume_state("/NOR:")) {
  169. strncpy(p_alarm->dir, "/NOR:/", sizeof(p_alarm->dir));
  170. } else {
  171. #ifdef CONFIG_DISK_MUSIC_APP
  172. strncpy(p_alarm->dir, "/NOR:/", sizeof(p_alarm->dir));
  173. #else
  174. alarm_disk_check_timer_start(p_alarm);
  175. return false;
  176. #endif
  177. }
  178. return true;
  179. }
  180. #ifdef CONFIG_LAUNCHER_APP
  181. void alarm_proc_start(void)
  182. {
  183. struct app_msg msg = { 0 };
  184. SYS_LOG_INF("p_alarm %p\n", p_alarm);
  185. if (p_alarm)
  186. return;
  187. if (_alarm_init()) {
  188. alarm_exit(true);
  189. SYS_LOG_ERR("init failed\n");
  190. return;
  191. }
  192. if (_alarm_disk_ready_check()) {
  193. msg.type = MSG_ALARM_EVENT;
  194. msg.cmd = MSG_ALARM_RING_PLAY_INIT;
  195. send_async_msg(app_manager_get_current_app(), &msg);
  196. }
  197. }
  198. extern uint8_t alarm_snooze_times;
  199. void alarm_snooze(bool need_switch_last)
  200. {
  201. if (p_alarm == NULL) {
  202. p_alarm = app_mem_malloc(sizeof(struct alarm_app_t));
  203. if (!p_alarm) {
  204. SYS_LOG_ERR("malloc failed!\n");
  205. return;
  206. }
  207. }
  208. if (alarm_snooze_times < ALARM_SNOOSE_TIMS) {
  209. alarm_snooze_times++;
  210. alarm_set_snooze(p_alarm, ALARM_SNOOSE_TIMEOUT);
  211. }
  212. alarm_exit(need_switch_last);
  213. }
  214. #endif
  215. static void _alarm_main_loop(void *parama1, void *parama2, void *parama3)
  216. {
  217. struct app_msg msg = { 0 };
  218. bool terminated = false;
  219. if (_alarm_init()) {
  220. alarm_exit(false);
  221. SYS_LOG_ERR("init failed\n");
  222. return;
  223. }
  224. if (_alarm_disk_ready_check()) {
  225. msg.type = MSG_ALARM_EVENT;
  226. msg.cmd = MSG_ALARM_RING_PLAY_INIT;
  227. send_async_msg(APP_ID_ALARM, &msg);
  228. }
  229. app_switch_lock(1);
  230. while (!terminated) {
  231. if (receive_msg(&msg, thread_timer_next_timeout())) {
  232. SYS_LOG_INF("type %d, cmd %d\n", msg.type, msg.cmd);
  233. switch (msg.type) {
  234. case MSG_INPUT_EVENT:
  235. alarm_input_event_proc(&msg);
  236. break;
  237. case MSG_ALARM_EVENT:
  238. alarm_event_proc(&msg);
  239. break;
  240. case MSG_TTS_EVENT:
  241. alarm_tts_event_proc(&msg);
  242. break;
  243. case MSG_EXIT_APP:
  244. alarm_exit(false);
  245. terminated = true;
  246. break;
  247. default:
  248. break;
  249. }
  250. if (msg.callback)
  251. msg.callback(&msg, 0, NULL);
  252. }
  253. if (!terminated)
  254. thread_timer_handle_expired();
  255. }
  256. if (alarm_wakeup_source_check()) {
  257. sys_event_send_message(MSG_POWER_OFF);
  258. }
  259. }
  260. APP_DEFINE(alarm, share_stack_area, sizeof(share_stack_area),
  261. CONFIG_APP_PRIORITY, FOREGROUND_APP, NULL, NULL, NULL,
  262. _alarm_main_loop, NULL);