alarm_manager.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file alarm manager interface
  8. */
  9. #if defined(CONFIG_SYS_LOG)
  10. #define SYS_LOG_NO_NEWLINE
  11. #ifdef SYS_LOG_DOMAIN
  12. #undef SYS_LOG_DOMAIN
  13. #endif
  14. #define SYS_LOG_DOMAIN "alarm_manager"
  15. #include <logging/sys_log.h>
  16. #endif
  17. #ifndef CONFIG_SIMULATOR
  18. #include <drivers/rtc.h>
  19. #include <soc.h>
  20. #include <drivers/alarm.h>
  21. #endif
  22. #include <os_common_api.h>
  23. #include <mem_manager.h>
  24. #include <msg_manager.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "stream.h"
  30. #include "mem_manager.h"
  31. #include <alarm_manager.h>
  32. #ifdef CONFIG_PROPERTY
  33. #include "property_manager.h"
  34. #endif
  35. #define ALARM_DEFAULF_TIME (7*60*60)/*default alarm time 07:00:00*/
  36. #define ALARM_EXIST (1)
  37. #define SN_ALARM_EXIST (2)
  38. static struct alarm_manager al_manager;
  39. alarm_callback call_back;
  40. static uint8_t get_alarm_times;
  41. static void alarm_manager_callback(const void *cb_data);
  42. static int system_add_alarm(const struct device *dev, struct rtc_time *tm, bool is_snooze);
  43. static int system_delete_alarm(const struct device *dev, struct rtc_time *tm);
  44. static int system_get_alarm(struct rtc_time *tm, bool *is_on);
  45. #if CONFIG_ALARM8HZ_ACTS
  46. static int alarm_set_alarm8hz(const struct device *rtc_dev, struct rtc_time *tm);
  47. #else
  48. static int alarm_set_rtc(const struct device *dev, struct rtc_time *tm);
  49. #endif
  50. #ifdef CONFIG_RTC_ACTS
  51. static void tm_to_time(struct rtc_time *tm, uint32_t *time)
  52. {
  53. *time = (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec;
  54. }
  55. static void tm_add_one_day(struct rtc_time *tm)
  56. {
  57. tm->tm_mday += 1;
  58. if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year)) {
  59. /* alarm day 1-31 */
  60. tm->tm_mday = 1;
  61. tm->tm_mon += 1;
  62. if (tm->tm_mon >= 13) {
  63. /* alarm mon 1-12 */
  64. tm->tm_mon = 1;
  65. tm->tm_year += 1;
  66. }
  67. }
  68. }
  69. static void time_to_tm(struct rtc_time *tm, uint32_t time)
  70. {
  71. tm->tm_hour = time / 3600;
  72. tm->tm_min = time / 60 - tm->tm_hour * 60;
  73. tm->tm_sec = time % 60;
  74. }
  75. #endif
  76. int find_and_set_alarm(void)
  77. {
  78. #ifdef CONFIG_RTC_ACTS
  79. int ret = 0;
  80. int i = 0;
  81. int earliest_alarm = -1;
  82. struct alarm_info *alarm = NULL;
  83. struct alarm_info *sn_alarm = NULL;
  84. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  85. uint32_t cur_time = 0;
  86. struct rtc_time tm = {0};
  87. if (!rtc) {
  88. SYS_LOG_ERR("no alarm\n");
  89. return -1;
  90. }
  91. ret = rtc_get_time(rtc, &tm);
  92. print_rtc_time(&tm);
  93. if (ret) {
  94. SYS_LOG_ERR("get curtime error\n");
  95. return -1;
  96. }
  97. tm_to_time(&tm, &cur_time);
  98. /*need to free current alarm*/
  99. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  100. if (al_manager.alarm[i].state == ALARM_STATE_OK) {
  101. al_manager.alarm[i].state = ALARM_STATE_FREE;
  102. break;
  103. }
  104. if (al_manager.sn_alarm[i].state == ALARM_STATE_OK) {
  105. if ((al_manager.sn_alarm[i].alarm_time > cur_time) &&
  106. (al_manager.sn_alarm[i].alarm_time - cur_time < 36000)) {/* fix snooze time is 11:59:00,cur time is 00:00:00*/
  107. al_manager.sn_alarm[i].state = ALARM_STATE_FREE;
  108. } else {
  109. al_manager.sn_alarm[i].state = ALARM_STATE_NULL;
  110. }
  111. break;
  112. }
  113. }
  114. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  115. alarm = &al_manager.alarm[i];
  116. if ((alarm->state == ALARM_STATE_FREE)
  117. && (alarm->alarm_time > cur_time)) {
  118. if (earliest_alarm < 0) {
  119. earliest_alarm = alarm->alarm_time;
  120. } else if (earliest_alarm > alarm->alarm_time) {
  121. earliest_alarm = alarm->alarm_time;
  122. }
  123. }
  124. sn_alarm = &al_manager.sn_alarm[i];
  125. if ((sn_alarm->state == ALARM_STATE_FREE)
  126. && (sn_alarm->alarm_time > cur_time)) {
  127. if (earliest_alarm < 0) {
  128. earliest_alarm = sn_alarm->alarm_time;
  129. } else if (earliest_alarm > sn_alarm->alarm_time) {
  130. earliest_alarm = sn_alarm->alarm_time;
  131. }
  132. }
  133. }
  134. if (earliest_alarm < 0) {
  135. /*if all alarm is earlier than current time*/
  136. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  137. alarm = &al_manager.alarm[i];
  138. if ((alarm->state == ALARM_STATE_FREE)
  139. && (alarm->alarm_time <= cur_time)) {
  140. if (earliest_alarm < 0) {
  141. earliest_alarm = alarm->alarm_time;
  142. } else if (earliest_alarm > alarm->alarm_time) {
  143. earliest_alarm = alarm->alarm_time;
  144. }
  145. }
  146. sn_alarm = &al_manager.sn_alarm[i];
  147. if ((sn_alarm->state == ALARM_STATE_FREE)
  148. && (sn_alarm->alarm_time <= cur_time)) {
  149. if (earliest_alarm < 0) {
  150. earliest_alarm = sn_alarm->alarm_time;
  151. } else if (earliest_alarm > sn_alarm->alarm_time) {
  152. earliest_alarm = sn_alarm->alarm_time;
  153. }
  154. }
  155. }
  156. }
  157. if (earliest_alarm >= 0) {
  158. time_to_tm(&tm, earliest_alarm);
  159. if (earliest_alarm <= cur_time) {
  160. tm_add_one_day(&tm);
  161. }
  162. #if CONFIG_ALARM8HZ_ACTS
  163. ret = alarm_set_alarm8hz(rtc, &tm);
  164. #else
  165. ret = alarm_set_rtc(rtc, &tm);
  166. #endif
  167. if (ret) {
  168. SYS_LOG_ERR("set alarm error ret=%d\n", ret);
  169. return -1;
  170. }
  171. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  172. alarm = &al_manager.alarm[i];
  173. if ((alarm->state == ALARM_STATE_FREE)
  174. && (alarm->alarm_time == earliest_alarm)) {
  175. alarm->state = ALARM_STATE_OK;
  176. break;
  177. }
  178. sn_alarm = &al_manager.sn_alarm[i];
  179. if ((sn_alarm->state == ALARM_STATE_FREE)
  180. && (sn_alarm->alarm_time == earliest_alarm)) {
  181. sn_alarm->state = ALARM_STATE_OK;
  182. break;
  183. }
  184. }
  185. }
  186. #ifdef CONFIG_PROPERTY
  187. ret = property_set(CFG_ALARM_INFO,
  188. (char *)&al_manager, sizeof(struct alarm_manager));
  189. #endif
  190. if (ret < 0) {
  191. SYS_LOG_ERR("failed to set config %s, ret %d\n",
  192. CFG_ALARM_INFO, ret);
  193. return -1;
  194. }
  195. #endif
  196. return 0;
  197. }
  198. static int is_alarm_full(bool is_snooze)
  199. {
  200. int i = 0;
  201. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  202. if (is_snooze) {
  203. if (al_manager.sn_alarm[i].state <= ALARM_STATE_OFF) {
  204. break;
  205. }
  206. } else {
  207. if (al_manager.alarm[i].state <= ALARM_STATE_OFF) {
  208. break;
  209. }
  210. }
  211. }
  212. if (i == MAX_ALARM_SUPPORT) {
  213. return -2;
  214. }
  215. return 0;
  216. }
  217. static int is_alarm_exist(uint32_t time)
  218. {
  219. for (int i = 0; i < MAX_ALARM_SUPPORT; i++) {
  220. if ((al_manager.sn_alarm[i].alarm_time == time)
  221. && (al_manager.sn_alarm[i].state > ALARM_STATE_OFF)) {
  222. return SN_ALARM_EXIST;
  223. }
  224. if ((al_manager.alarm[i].alarm_time == time)
  225. && (al_manager.alarm[i].state > ALARM_STATE_OFF)) {
  226. return ALARM_EXIST;
  227. }
  228. }
  229. return 0;
  230. }
  231. static int delect_sn_alarm(uint32_t time)
  232. {
  233. for (int i = 0; i < MAX_ALARM_SUPPORT; i++) {
  234. if ((al_manager.sn_alarm[i].alarm_time == time)
  235. && (al_manager.sn_alarm[i].state > ALARM_STATE_OFF)) {
  236. al_manager.sn_alarm[i].state = ALARM_STATE_NULL;
  237. break;
  238. }
  239. }
  240. SYS_LOG_INF("time %d\n", time);
  241. return 0;
  242. }
  243. static int is_need_set_alarm(const struct device *dev, uint32_t time)
  244. {
  245. #ifdef CONFIG_RTC_ACTS
  246. int i = 0;
  247. uint32_t cur_time = 0;
  248. struct rtc_time tm = {0};
  249. bool is_snooze_alarm_set = false;
  250. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  251. if (al_manager.alarm[i].state == ALARM_STATE_OK) {
  252. break;
  253. }
  254. if (al_manager.sn_alarm[i].state == ALARM_STATE_OK) {
  255. is_snooze_alarm_set = true;
  256. break;
  257. }
  258. }
  259. if (i == MAX_ALARM_SUPPORT) {
  260. return 1;
  261. }
  262. int ret = rtc_get_time(dev, &tm);
  263. if (ret) {
  264. SYS_LOG_ERR("get curtime error\n");
  265. return 0;
  266. }
  267. tm_to_time(&tm, &cur_time);
  268. if (is_snooze_alarm_set) {
  269. if (time > cur_time) {
  270. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  271. if (al_manager.sn_alarm[i].state == ALARM_STATE_OK) {
  272. if (time < al_manager.sn_alarm[i].alarm_time) {
  273. al_manager.sn_alarm[i].state = ALARM_STATE_FREE;
  274. return 1;
  275. } else if (al_manager.sn_alarm[i].alarm_time < cur_time) {
  276. al_manager.sn_alarm[i].state = ALARM_STATE_FREE;
  277. return 1;
  278. }
  279. break;
  280. }
  281. }
  282. } else if (time < cur_time) {
  283. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  284. if (al_manager.sn_alarm[i].state == ALARM_STATE_OK) {
  285. if ((time < al_manager.sn_alarm[i].alarm_time)
  286. && (al_manager.sn_alarm[i].alarm_time < cur_time)) {
  287. al_manager.sn_alarm[i].state = ALARM_STATE_FREE;
  288. return 1;
  289. }
  290. break;
  291. }
  292. }
  293. }
  294. } else {
  295. if (time > cur_time) {
  296. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  297. if (al_manager.alarm[i].state == ALARM_STATE_OK) {
  298. if (time < al_manager.alarm[i].alarm_time) {
  299. al_manager.alarm[i].state = ALARM_STATE_FREE;
  300. return 1;
  301. } else if (al_manager.alarm[i].alarm_time < cur_time) {
  302. al_manager.alarm[i].state = ALARM_STATE_FREE;
  303. return 1;
  304. }
  305. break;
  306. }
  307. }
  308. } else if (time < cur_time) {
  309. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  310. if (al_manager.alarm[i].state == ALARM_STATE_OK) {
  311. if ((time < al_manager.alarm[i].alarm_time)
  312. && (al_manager.alarm[i].alarm_time < cur_time)) {
  313. al_manager.alarm[i].state = ALARM_STATE_FREE;
  314. return 1;
  315. }
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. #endif
  322. return 0;
  323. }
  324. static int check_alarm_time(int time)
  325. {
  326. if (time < 0 || time >= 86400)
  327. return -EINVAL;
  328. return 0;
  329. }
  330. static void alarm_manager_callback(const void *cb_data)
  331. {
  332. if (call_back != NULL) {
  333. call_back();
  334. }
  335. }
  336. int alarm_manager_init(void)
  337. {
  338. #ifdef CONFIG_RTC_ACTS
  339. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  340. if (!rtc) {
  341. SYS_LOG_ERR("no alarm\n");
  342. return -1;
  343. }
  344. rtc_enable(rtc);
  345. #if 0
  346. struct alarm_time tm;
  347. uint32_t time_stamp = system_clock_init();
  348. alarm_time_to_tm(time_stamp, &tm)
  349. if (alarm_set_time(alarm, &tm)) {
  350. SYS_LOG_ERR("Failed to config RTC alarm\n");
  351. return -1;
  352. }
  353. #endif
  354. memset(&al_manager, 0, sizeof(struct alarm_manager));
  355. #ifdef CONFIG_PROPERTY
  356. int ret = property_get(CFG_ALARM_INFO,
  357. (char *)&al_manager, sizeof(struct alarm_manager));
  358. #endif
  359. for (int i = 0; i < MAX_ALARM_SUPPORT; i++) {
  360. if (al_manager.alarm[i].state == ALARM_STATE_NULL &&
  361. al_manager.alarm[i].alarm_time != ALARM_DEFAULF_TIME) {
  362. al_manager.alarm[i].alarm_time = ALARM_DEFAULF_TIME;
  363. }
  364. SYS_LOG_INF("alarm[%d]:state=%d,alarm_time=%d\n",
  365. i, al_manager.alarm[i].state, al_manager.alarm[i].alarm_time);
  366. }
  367. if (ret < 0) {
  368. SYS_LOG_ERR("failed to get config %s, ret %d\n", CFG_ALARM_INFO, ret);
  369. return -1;
  370. }
  371. #endif
  372. return 0;
  373. }
  374. int alarm_manager_get_time(struct rtc_time *tm)
  375. {
  376. #ifdef CONFIG_RTC_ACTS
  377. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  378. if (!rtc)
  379. return -ENODEV;
  380. int ret = rtc_get_time(rtc, tm);
  381. if (ret) {
  382. SYS_LOG_ERR("get time error ret=%d\n", ret);
  383. return -1;
  384. }
  385. tm->tm_year += 1900;
  386. tm->tm_mon += 1;
  387. return ret;
  388. #else
  389. return 0;
  390. #endif
  391. }
  392. int alarm_manager_set_time(struct rtc_time *tm)
  393. {
  394. #ifdef CONFIG_RTC_ACTS
  395. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  396. if (!rtc) {
  397. SYS_LOG_ERR("no alarm device\n");
  398. return -1;
  399. }
  400. tm->tm_year -= 1900;
  401. tm->tm_mon -= 1;
  402. int ret = rtc_set_time(rtc, tm);
  403. if (ret) {
  404. SYS_LOG_ERR("set time error ret=%d\n", ret);
  405. return -1;
  406. }
  407. print_rtc_time(tm);
  408. return ret;
  409. #else
  410. return 0;
  411. #endif
  412. }
  413. int alarm_manager_get_alarm(struct rtc_time *tm, bool *is_on)
  414. {
  415. int ret = system_get_alarm(tm, is_on);
  416. if (ret) {
  417. SYS_LOG_ERR("get alarm error ret=%d\n", ret);
  418. return -1;
  419. }
  420. return ret;
  421. }
  422. int alarm_manager_set_alarm(struct rtc_time *tm, bool is_snooze)
  423. {
  424. #ifdef CONFIG_RTC_ACTS
  425. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  426. if (!rtc) {
  427. SYS_LOG_ERR("no alarm device\n");
  428. return -1;
  429. }
  430. int ret = system_add_alarm(rtc, tm, is_snooze);
  431. if (ret) {
  432. SYS_LOG_ERR("set alarm error ret=%d\n", ret);
  433. return -1;
  434. }
  435. get_alarm_times--;
  436. return ret;
  437. #else
  438. return 0;
  439. #endif
  440. }
  441. int alarm_manager_delete_alarm(struct rtc_time *tm)
  442. {
  443. #ifdef CONFIG_RTC_ACTS
  444. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  445. if (!rtc) {
  446. SYS_LOG_ERR("no alarm device\n");
  447. return -1;
  448. }
  449. int ret = system_delete_alarm(rtc, tm);
  450. if (ret) {
  451. SYS_LOG_ERR("delete alarm error ret=%d\n", ret);
  452. return -1;
  453. }
  454. print_rtc_time(tm);
  455. return ret;
  456. #else
  457. return 0;
  458. #endif
  459. }
  460. static int alarm_set_state(bool is_snooze, uint32_t time, uint8_t alarm_state)
  461. {
  462. int i = 0;
  463. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  464. if (is_snooze) {
  465. if (al_manager.sn_alarm[i].state <= ALARM_STATE_OFF) {
  466. al_manager.sn_alarm[i].state = alarm_state;
  467. al_manager.sn_alarm[i].alarm_time = time;
  468. break;
  469. }
  470. } else {
  471. if (al_manager.alarm[i].state <= ALARM_STATE_OFF) {
  472. al_manager.alarm[i].state = alarm_state;
  473. al_manager.alarm[i].alarm_time = time;
  474. break;
  475. }
  476. }
  477. }
  478. return 0;
  479. }
  480. #if CONFIG_ALARM8HZ_ACTS
  481. static int alarm_set_alarm8hz(const struct device *rtc_dev, struct rtc_time *tm)
  482. {
  483. const struct device *dev = device_get_binding(CONFIG_ALARM8HZ_0_NAME);
  484. struct alarm_config config = {0};
  485. int ret = 0;
  486. uint32_t cur_time = 0;
  487. uint32_t alarm_time = 0;
  488. struct rtc_time cur_tm = {0};
  489. uint32_t alarm_msec = 0;
  490. if (!dev) {
  491. printk("failed to get alarm8hz device:%s\n", CONFIG_ALARM8HZ_0_NAME);
  492. return -ENXIO;
  493. }
  494. ret = rtc_get_time(rtc_dev, &cur_tm);
  495. if (ret) {
  496. SYS_LOG_ERR("get curtime error\n");
  497. return ret;
  498. }
  499. rtc_tm_to_time(&cur_tm, &cur_time);
  500. rtc_tm_to_time(tm, &alarm_time);
  501. if ((alarm_time < cur_time) || (alarm_time - cur_time > 4294967)/*0xffffffff / 1000*/) {
  502. SYS_LOG_WRN("time invalid cur_time %d alarm %d\n", cur_time, alarm_time);
  503. return -EINVAL;
  504. }
  505. alarm_msec = (alarm_time - cur_time) * 1000;/*Turning milliseconds*/
  506. config.alarm_msec = alarm_msec;
  507. config.cb_fn = alarm_manager_callback;
  508. config.cb_data = dev;
  509. acts_alarm_set_alarm(dev, &config, true);
  510. SYS_LOG_INF(" cur_time %d alarm %d\n", cur_time, alarm_time);
  511. return 0;
  512. }
  513. #else /* CONFIG_ALARM8HZ_ACTS */
  514. static int alarm_set_rtc(const struct device *dev, struct rtc_time *tm)
  515. {
  516. #ifdef CONFIG_RTC_ACTS
  517. struct rtc_alarm_config config = {0};
  518. int ret = 0;
  519. config.alarm_time.tm_year = tm->tm_year;
  520. config.alarm_time.tm_mon = tm->tm_mon;/*don't need to -1*/
  521. config.alarm_time.tm_mday = tm->tm_mday;
  522. config.alarm_time.tm_hour = tm->tm_hour;
  523. config.alarm_time.tm_min = tm->tm_min;
  524. config.alarm_time.tm_sec = tm->tm_sec;
  525. config.cb_fn = alarm_manager_callback;
  526. config.cb_data = NULL;
  527. SYS_LOG_INF("hour=%d,min=%d\n",
  528. config.alarm_time.tm_hour, config.alarm_time.tm_min);
  529. ret = rtc_set_alarm(dev, &config, true);
  530. if (ret) {
  531. SYS_LOG_ERR("set alarm error ret=%d\n", ret);
  532. return -1;
  533. }
  534. #endif
  535. }
  536. #endif /* CONFIG_ALARM8HZ_ACTS */
  537. static int system_add_alarm(const struct device *dev, struct rtc_time *tm, bool is_snooze)
  538. {
  539. #ifdef CONFIG_RTC_ACTS
  540. int ret = 0;
  541. uint32_t time = 0;
  542. tm->tm_mon--;/*need to - 1*/
  543. tm->tm_year -= 1900;/*need to - 1900*/
  544. if (rtc_valid_tm(tm)) {
  545. SYS_LOG_ERR("Bad time structure\n");
  546. return -ENOEXEC;
  547. }
  548. if (is_alarm_full(is_snooze)) {
  549. SYS_LOG_WRN("alarm full!\n");
  550. return -2;
  551. }
  552. tm_to_time(tm, &time);
  553. ret = is_alarm_exist(time);
  554. if (ret) {
  555. SYS_LOG_WRN("alarm existed!\n");
  556. if (ret == SN_ALARM_EXIST && !is_snooze) {
  557. delect_sn_alarm(time);
  558. } else {
  559. return -3;
  560. }
  561. }
  562. if (is_need_set_alarm(dev, time)) {
  563. #if CONFIG_ALARM8HZ_ACTS
  564. ret = alarm_set_alarm8hz(dev, tm);
  565. #else
  566. ret = alarm_set_rtc(dev, tm);
  567. #endif
  568. if (ret)
  569. return -1;
  570. alarm_set_state(is_snooze, time, ALARM_STATE_OK);
  571. } else {
  572. alarm_set_state(is_snooze, time, ALARM_STATE_FREE);
  573. SYS_LOG_INF("add alarm:hour=%d, min=%d\n", tm->tm_hour, tm->tm_min);
  574. }
  575. #ifdef CONFIG_PROPERTY
  576. ret = property_set(CFG_ALARM_INFO,
  577. (char *)&al_manager, sizeof(struct alarm_manager));
  578. if (ret < 0) {
  579. SYS_LOG_ERR("failed to set config %s, ret %d\n", CFG_ALARM_INFO, ret);
  580. return -1;
  581. }
  582. #endif
  583. #endif
  584. return 0;
  585. }
  586. static int system_delete_alarm(const struct device *dev, struct rtc_time *tm)
  587. {
  588. #ifdef CONFIG_RTC_ACTS
  589. int i = 0;
  590. int ret = 0;
  591. uint32_t time = 0;
  592. bool need_delete = false;
  593. tm_to_time(tm, &time);
  594. SYS_LOG_INF("delect time=%d\n", time);
  595. for (int j = 0; j < MAX_ALARM_SUPPORT; j++) {
  596. SYS_LOG_DBG("al_manager.alarm[%d].state=%d"
  597. ",al_manager.alarm[%d].alarm_time=%d\n",
  598. j, al_manager.alarm[j].state, j, al_manager.alarm[j].alarm_time);
  599. }
  600. for (i = 0; i < MAX_ALARM_SUPPORT; i++) {
  601. if ((al_manager.alarm[i].state > ALARM_STATE_OFF)
  602. && (al_manager.alarm[i].alarm_time == time)) {
  603. need_delete = true;
  604. break;
  605. }
  606. }
  607. if (need_delete) {
  608. if (al_manager.alarm[i].state == ALARM_STATE_OK) {
  609. /*need remove alarm from alarm*/
  610. #if CONFIG_ALARM8HZ_ACTS
  611. const struct device *alarm_8hzdev = device_get_binding(CONFIG_ALARM8HZ_0_NAME);
  612. if (alarm_8hzdev == NULL) {
  613. SYS_LOG_ERR("alarm_8hzdev null\n");
  614. return -1;
  615. }
  616. ret = acts_alarm_set_alarm(alarm_8hzdev, NULL, false);
  617. #else
  618. ret = rtc_set_alarm(dev, NULL, false);
  619. #endif
  620. if (ret) {
  621. SYS_LOG_ERR("set alarm error ret=%d\n", ret);
  622. return -1;
  623. }
  624. al_manager.alarm[i].state = ALARM_STATE_OFF;
  625. if (!find_and_set_alarm())
  626. return 0;
  627. } else {
  628. al_manager.alarm[i].state = ALARM_STATE_OFF;
  629. }
  630. #ifdef CONFIG_PROPERTY
  631. ret = property_set(CFG_ALARM_INFO,
  632. (char *)&al_manager, sizeof(struct alarm_manager));
  633. if (ret < 0) {
  634. SYS_LOG_ERR("failed to set config %s, ret %d\n",
  635. CFG_ALARM_INFO, ret);
  636. return -1;
  637. }
  638. #endif
  639. }
  640. #endif
  641. return 0;
  642. }
  643. static int system_get_alarm(struct rtc_time *tm, bool *is_on)
  644. {
  645. #ifdef CONFIG_RTC_ACTS
  646. for (int i = 0; i < MAX_ALARM_SUPPORT; i++) {
  647. if (get_alarm_times >= MAX_ALARM_SUPPORT)
  648. get_alarm_times = 0;
  649. SYS_LOG_INF("get_alarm_times=%d,alarm_time=%d\n",
  650. get_alarm_times, al_manager.alarm[get_alarm_times].alarm_time);
  651. if (check_alarm_time(al_manager.alarm[get_alarm_times].alarm_time)) {
  652. get_alarm_times++;
  653. } else {
  654. time_to_tm(tm, al_manager.alarm[get_alarm_times].alarm_time);
  655. SYS_LOG_INF("hour=%d,min=%d\n", tm->tm_hour, tm->tm_min);
  656. if (al_manager.alarm[get_alarm_times].state) {
  657. *is_on = true;
  658. } else {
  659. *is_on = false;
  660. }
  661. get_alarm_times++;
  662. break;
  663. }
  664. }
  665. #endif
  666. return 0;
  667. }
  668. int system_registry_alarm_callback(alarm_callback callback)
  669. {
  670. call_back = callback;
  671. return 0;
  672. }
  673. extern int sys_pm_get_wakeup_source(union sys_pm_wakeup_src *src);
  674. bool alarm_wakeup_source_check(void)
  675. {
  676. #ifdef CONFIG_RTC_ACTS
  677. union sys_pm_wakeup_src src = {0};
  678. if (!sys_pm_get_wakeup_source(&src)) {
  679. SYS_LOG_INF("alarm=%d\n", src.t.alarm);
  680. if (src.t.alarm) {
  681. return true;
  682. }
  683. }
  684. #endif
  685. return false;
  686. }
  687. struct alarm_manager *alarm_manager_get_exist_alarm(int *alarm_count)
  688. {
  689. struct alarm_manager *alarm_manager = &al_manager;
  690. *alarm_count = MAX_ALARM_SUPPORT;
  691. return alarm_manager;
  692. }
  693. int alarm_manager_update_alarm(uint32_t time_sec, uint8_t alarm_index, int alarm_state)
  694. {
  695. #ifdef CONFIG_RTC_ACTS
  696. struct rtc_time tm;
  697. int ret = 0;
  698. if ((alarm_index >= MAX_ALARM_SUPPORT) || (alarm_state == ALARM_STATE_NULL))
  699. return -EINVAL;
  700. if (alarm_state == ALARM_STATE_OFF) {
  701. if (al_manager.alarm[alarm_index].state <= ALARM_STATE_OFF) {
  702. al_manager.alarm[alarm_index].alarm_time = time_sec;
  703. al_manager.alarm[alarm_index].state = alarm_state;
  704. #ifdef CONFIG_PROPERTY
  705. ret = property_set(CFG_ALARM_INFO,
  706. (char *)&al_manager, sizeof(struct alarm_manager));
  707. if (ret < 0) {
  708. SYS_LOG_ERR("failed to set config %s, ret %d\n", CFG_ALARM_INFO, ret);
  709. return -1;
  710. }
  711. #endif
  712. } else {
  713. time_to_tm(&tm, time_sec);
  714. al_manager.alarm[alarm_index].alarm_time = time_sec;
  715. alarm_manager_delete_alarm(&tm);
  716. }
  717. } else {
  718. al_manager.alarm[alarm_index].alarm_time = time_sec;
  719. al_manager.alarm[alarm_index].state = ALARM_STATE_FREE;
  720. find_and_set_alarm();
  721. }
  722. #endif
  723. return 0;
  724. }