1 /*
2 * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <time.h>
7
8 #include <k_api.h>
9 #include <aos/errno.h>
10 #include <aos/kernel.h>
11
12 #include "rhino_p.h"
13
14 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC == 0)
15 #warning "RHINO_CONFIG_KOBJ_DYN_ALLOC is disabled!"
16 #endif
17
18 #if (RHINO_CONFIG_TIMER > 0)
aos_timer_new(aos_timer_t * timer,void (* fn)(void *,void *),void * arg,uint32_t ms,bool repeat)19 aos_status_t aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *), void *arg,
20 uint32_t ms, bool repeat)
21 {
22 kstat_t ret;
23
24 if (timer == NULL) {
25 return -EINVAL;
26 }
27 sys_time_t round = (repeat == false ? 0 : ms);
28
29 ret = krhino_timer_dyn_create((ktimer_t **)(timer), "AOS", (timer_cb_t)fn, MS2TICK(ms), MS2TICK(round), arg, 1);
30
31
32 return rhino2stderrno(ret);
33 }
aos_timer_is_valid(aos_timer_t * timer)34 bool aos_timer_is_valid(aos_timer_t *timer)
35 {
36 ktimer_t *k_timer;
37
38 if (timer == NULL) {
39 return false;
40 }
41
42 k_timer = (ktimer_t *)*timer;
43
44 if (k_timer == NULL) {
45 return false;
46 }
47
48 if (k_timer->obj_type != RHINO_TIMER_OBJ_TYPE) {
49 return false;
50 }
51
52 return true;
53 }
aos_timer_new_ext(aos_timer_t * timer,void (* fn)(void *,void *),void * arg,uint32_t ms,bool repeat,bool auto_run)54 aos_status_t aos_timer_new_ext(aos_timer_t *timer, void (*fn)(void *, void *), void *arg,
55 uint32_t ms, bool repeat, bool auto_run)
56 {
57 kstat_t ret;
58
59 if (timer == NULL) {
60 return -EINVAL;
61 }
62
63 sys_time_t round = (repeat == 0) ? 0 : ms;
64
65 ret = krhino_timer_dyn_create((ktimer_t **)(timer), "AOS", (timer_cb_t)fn, MS2TICK(ms), MS2TICK(round), arg, (auto_run == true) ? 1 : 0);
66
67 return rhino2stderrno(ret);
68 }
69
aos_timer_create(aos_timer_t * timer,void (* fn)(void *,void *),void * arg,uint32_t ms,uint32_t options)70 aos_status_t aos_timer_create(aos_timer_t *timer, void (*fn)(void *, void *), void *arg,
71 uint32_t ms, uint32_t options)
72 {
73 kstat_t ret;
74 sys_time_t round;
75 if (timer == NULL) {
76 return -EINVAL;
77 }
78
79 round = (options & AOS_TIMER_REPEAT) ? ms : 0;
80
81 ret = krhino_timer_dyn_create((ktimer_t **)(timer), "AOS", (timer_cb_t)fn, MS2TICK(ms), MS2TICK(round), arg, (options & AOS_TIMER_AUTORUN) ? 1 : 0);
82
83 return rhino2stderrno(ret);
84 }
85
aos_timer_free(aos_timer_t * timer)86 void aos_timer_free(aos_timer_t *timer)
87 {
88 if (timer == NULL || *timer == NULL) {
89 return;
90 }
91
92 krhino_timer_dyn_del((ktimer_t *)*timer);
93 *timer = NULL;
94 }
95
aos_timer_start(aos_timer_t * timer)96 aos_status_t aos_timer_start(aos_timer_t *timer)
97 {
98 int ret;
99
100 CHECK_HANDLE(timer);
101
102 ret = krhino_timer_start((ktimer_t *)*timer);
103 return rhino2stderrno(ret);
104 }
105
aos_timer_stop(aos_timer_t * timer)106 aos_status_t aos_timer_stop(aos_timer_t *timer)
107 {
108 int ret;
109
110 CHECK_HANDLE(timer);
111
112 ret = krhino_timer_stop((ktimer_t *)*timer);
113 return rhino2stderrno(ret);
114 }
115
aos_timer_change(aos_timer_t * timer,uint32_t ms)116 aos_status_t aos_timer_change(aos_timer_t *timer, uint32_t ms)
117 {
118 int ret;
119
120 CHECK_HANDLE(timer);
121
122 ret = krhino_timer_change((ktimer_t *)*timer, MS2TICK(ms), MS2TICK(ms));
123 return rhino2stderrno(ret);
124 }
aos_timer_change_once(aos_timer_t * timer,uint32_t ms)125 aos_status_t aos_timer_change_once(aos_timer_t *timer, uint32_t ms)
126 {
127 int ret;
128
129 CHECK_HANDLE(timer);
130
131 ret = krhino_timer_change((ktimer_t *)*timer, MS2TICK(ms), 0);
132
133 return rhino2stderrno(ret);
134 }
135
aos_timer_gettime(aos_timer_t * timer,uint64_t value[4])136 int aos_timer_gettime(aos_timer_t *timer, uint64_t value[4])
137 {
138 ktimer_t *ktimer = NULL;
139 uint64_t init_ms;
140 uint64_t round_ms;
141
142 CHECK_HANDLE(timer);
143 ktimer = (ktimer_t *)*timer;
144 init_ms = (uint64_t)krhino_ticks_to_ms(ktimer->init_count);
145 round_ms = (uint64_t)krhino_ticks_to_ms(ktimer->round_ticks);
146
147 value[0] = init_ms / 1000;
148 value[1] = (init_ms % 1000) * 1000000UL;
149 value[2] = round_ms / 1000;
150 value[3] = (round_ms % 1000) * 1000000UL ;
151
152 return 0;
153 }
154 #endif
155
156
157
158
159
160