1 #include "linkkit/infra/infra_config.h"
2 
3 #ifdef INFRA_TIMER
4 /*
5  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
6  */
7 #include "linkkit/infra/infra_types.h"
8 #include "linkkit/infra/infra_timer.h"
9 #include "linkkit/wrappers/wrappers.h"
10 
iotx_time_start(iotx_time_t * timer)11 void iotx_time_start(iotx_time_t *timer)
12 {
13     if (!timer) {
14         return;
15     }
16 
17     timer->time = HAL_UptimeMs();
18 }
19 
utils_time_spend(iotx_time_t * start)20 uint32_t utils_time_spend(iotx_time_t *start)
21 {
22     uint32_t now, res;
23 
24     if (!start) {
25         return 0;
26     }
27 
28     now = HAL_UptimeMs();
29     res = now - start->time;
30     return res;
31 }
32 
iotx_time_left(iotx_time_t * end)33 uint32_t iotx_time_left(iotx_time_t *end)
34 {
35     uint32_t now, res;
36 
37     if (!end) {
38         return 0;
39     }
40 
41     if (utils_time_is_expired(end)) {
42         return 0;
43     }
44 
45     now = HAL_UptimeMs();
46     res = end->time - now;
47     return res;
48 }
49 
utils_time_is_expired(iotx_time_t * timer)50 uint32_t utils_time_is_expired(iotx_time_t *timer)
51 {
52     uint32_t cur_time;
53 
54     if (!timer) {
55         return 1;
56     }
57 
58     cur_time = HAL_UptimeMs();
59     /*
60      *  WARNING: Do NOT change the following code until you know exactly what it
61      * do!
62      *
63      *  check whether it reach destination time or not.
64      */
65     if ((cur_time - timer->time) < (UINT32_MAX / 2)) {
66         return 1;
67     } else {
68         return 0;
69     }
70 }
71 
iotx_time_init(iotx_time_t * timer)72 void iotx_time_init(iotx_time_t *timer)
73 {
74     if (!timer) {
75         return;
76     }
77 
78     timer->time = 0;
79 }
80 
utils_time_countdown_ms(iotx_time_t * timer,uint32_t millisecond)81 void utils_time_countdown_ms(iotx_time_t *timer, uint32_t millisecond)
82 {
83     if (!timer) {
84         return;
85     }
86 
87     timer->time = HAL_UptimeMs() + millisecond;
88 }
89 
utils_time_get_ms(void)90 uint32_t utils_time_get_ms(void)
91 {
92     return HAL_UptimeMs();
93 }
94 #endif
95