Initial Commit
[apps/agl-service-unicens.git] / ucs2-lib / inc / ucs_timer.h
1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491                                                                            */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
4 /*                                                                                                */
5 /* This program is free software: you can redistribute it and/or modify                           */
6 /* it under the terms of the GNU General Public License as published by                           */
7 /* the Free Software Foundation, either version 2 of the License, or                              */
8 /* (at your option) any later version.                                                            */
9 /*                                                                                                */
10 /* This program is distributed in the hope that it will be useful,                                */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
13 /* GNU General Public License for more details.                                                   */
14 /*                                                                                                */
15 /* You should have received a copy of the GNU General Public License                              */
16 /* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
17 /*                                                                                                */
18 /* You may also obtain this software under a propriety license from Microchip.                    */
19 /* Please contact Microchip for further information.                                              */
20 /*------------------------------------------------------------------------------------------------*/
21
22 /*!
23  * \file
24  * \brief Internal header file of the timer management module.
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup G_TIMER
28  * @{
29  */
30
31 #ifndef UCS_TIMER_H
32 #define UCS_TIMER_H
33
34 /*------------------------------------------------------------------------------------------------*/
35 /* Includes                                                                                       */
36 /*------------------------------------------------------------------------------------------------*/
37 #include "ucs_rules.h"
38 #include "ucs_dl.h"
39 #include "ucs_scheduler.h"
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45
46 /*------------------------------------------------------------------------------------------------*/
47 /* Types                                                                                          */
48 /*------------------------------------------------------------------------------------------------*/
49 /*! \brief Function signature used for timer handler 
50  *  \param args Void pointer to optional data
51  */
52 typedef void (*Tm_Handler_t)(void *args);
53
54 /*------------------------------------------------------------------------------------------------*/
55 /* Structures                                                                                     */
56 /*------------------------------------------------------------------------------------------------*/
57 /*! \brief  Initialization structure of the timer management module. */
58 typedef struct Tm_InitData_
59 {
60     /*! \brief Observer used to request current tick count value */
61     CSingleObserver *get_tick_count_obs_ptr;
62     /*! \brief Observer used to start application timer for delayed TM service calls */
63     CSingleObserver *set_application_timer_obs_ptr;
64     /*! \brief UNICENS instance ID */
65     /*uint8_t ucs_inst_id;*/
66
67 } Tm_InitData_t;
68
69 /*! \brief   Class structure of the a timer object. */
70 typedef struct CTimer_
71 {
72     /*! \brief Node of the doubly linked (timer-) list */
73     CDlNode node;
74     /*! \brief Handler function which is invoked when the timer expires */
75     Tm_Handler_t handler_fptr;
76     /*! \brief Reference to optional parameter */
77     void *args_ptr;
78     /*! \brief The Timeout value before the timer expires for the first time, in milliseconds */
79     uint16_t elapse;
80     /*! \brief The period of the timer, in milliseconds */
81     uint16_t period;
82     /*! \brief Delta time related to next timer in list */
83     uint16_t delta;
84     /*! \brief Flag which signals that the timer is in use */
85     bool in_use;
86     /*! \brief Flag to check if timer object has changed within timer handler callback function */
87     bool changed;
88
89 } CTimer;
90
91 /*! \brief   Class structure of the timer management */
92 typedef struct CTimerManagement_
93 {
94     /*! \brief Doubly linked list to manage the active timers */
95     CDlList timer_list;
96     /*! \brief Subject to request current tick count */
97     CSingleSubject get_tick_count_subject;
98     /*! \brief Subject to start the application timer which triggers a UCS service call */
99     CSingleSubject set_application_timer_subject;
100     /*! \brief Service instance to add the timer management to the scheduler */
101     CService tm_srv;
102     /*! \brief Last tick count value (saved at TM service) */
103     uint16_t last_tick_count;
104     /*! \brief Signals that the application timer callbacks are used */
105     bool delayed_tm_service_enabled;
106     /*! \brief Indicates that the application timer must be started */
107     bool set_service_timer;
108     /*! \brief UNICENS instance ID */
109     void * ucs_user_ptr;
110
111 } CTimerManagement;
112
113 /*------------------------------------------------------------------------------------------------*/
114 /* Prototypes of class CTimerManagement                                                           */
115 /*------------------------------------------------------------------------------------------------*/
116 extern void Tm_Ctor(CTimerManagement *self, CScheduler *scd, const Tm_InitData_t *init_ptr, void * ucs_user_ptr);
117 extern void Tm_SetTimer(CTimerManagement *self, CTimer *timer_ptr, Tm_Handler_t handler_fptr, 
118                         void *args_ptr, uint16_t elapse, uint16_t period);
119 extern void Tm_ClearTimer(CTimerManagement *self, CTimer *timer_ptr);
120 extern void Tm_CheckForNextService(CTimerManagement *self);
121 extern void Tm_TriggerService(CTimerManagement *self);
122 extern void Tm_StopService(CTimerManagement *self);
123
124 /*------------------------------------------------------------------------------------------------*/
125 /* Prototypes of class CTimer                                                                     */
126 /*------------------------------------------------------------------------------------------------*/
127 extern void T_Ctor(CTimer *self);
128 extern bool T_IsTimerInUse(CTimer *self);
129
130 #ifdef __cplusplus
131 }   /* extern "C" */
132 #endif
133
134 #endif  /* #ifndef UCS_TIMER_H */
135
136 /*!
137  * @}
138  * \endcond
139  */
140
141 /*------------------------------------------------------------------------------------------------*/
142 /* End of file                                                                                    */
143 /*------------------------------------------------------------------------------------------------*/
144