1 /*
2  * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _KERNEL_OS_RTTHREAD_OS_TIMER_H_
31 #define _KERNEL_OS_RTTHREAD_OS_TIMER_H_
32 
33 #include "_os_common.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @brief Timer type definition
41  *     - one shot timer: Timer will be in the dormant state after it expires.
42  *     - periodic timer: Timer will auto-reload after it expires.
43  */
44 typedef enum {
45     OS_TIMER_ONCE       = 0, /* one shot timer */
46     OS_TIMER_PERIODIC   = 1  /* periodic timer */
47 } OS_TimerType;
48 
49 /** @brief Timer expire callback function definition */
50 typedef void (*OS_TimerCallback_t)(void *arg);
51 
52 /** @brief Timer handle definition */
53 typedef rt_timer_t OS_TimerHandle_t;
54 
55 typedef struct OS_Timer
56 {
57     rt_timer_t  handle;
58 } OS_Timer_t;
59 
60 
61 OS_Status OS_TimerCreate(OS_Timer_t *timer, OS_TimerType type,
62                          OS_TimerCallback_t cb, void *arg, OS_Time_t periodMS);
63 OS_Status OS_TimerDelete(OS_Timer_t *timer);
64 OS_Status OS_TimerStart(OS_Timer_t *timer);
65 OS_Status OS_TimerChangePeriod(OS_Timer_t *timer, OS_Time_t periodMS);
66 OS_Status OS_TimerStop(OS_Timer_t *timer);
67 
68 /**
69  * @brief Check whether the timer object is valid or not
70  * @param[in] timer Pointer to the timer object
71  * @return 1 on valid, 0 on invalid
72  */
OS_TimerIsValid(OS_Timer_t * timer)73 static inline int OS_TimerIsValid(OS_Timer_t *timer)
74 {
75     return (timer->handle != OS_INVALID_HANDLE);
76 }
77 
78 /**
79  * @brief Set the timer object to invalid state
80  * @param[in] timer Pointer to the timer object
81  * @return None
82  */
OS_TimerSetInvalid(OS_Timer_t * timer)83 static inline void OS_TimerSetInvalid(OS_Timer_t *timer)
84 {
85     timer->handle = OS_INVALID_HANDLE;
86 }
87 
88 /**
89  * @brief Check whether the timer is active or not
90  *
91  * A timer is inactive when it is in one of the following cases:
92  *     - The timer has been created, but not started.
93  *     - The timer is a one shot timer that has not been restarted since it
94  *       expired.
95  *
96  * @param[in] timer Pointer to the timer object
97  * @return 1 on active, 0 on inactive
98  */
OS_TimerIsActive(OS_Timer_t * timer)99 static inline int OS_TimerIsActive(OS_Timer_t *timer)
100 {
101     return ((timer->handle->parent.flag & RT_TIMER_FLAG_ACTIVATED) ? 1 : 0);
102 }
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif /* _KERNEL_OS_RTTHREAD_OS_TIMER_H_ */
109