47b41e9a75d41bc39138d287f6c2e330626bfef7
[staging/basesystem.git] / nsframework / framework_unified / client / NS_Timer / src / ns_timer_class.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSTimer
19 /// \brief
20 ///
21 /// Timer class for setting timers.
22 ///
23 //////////////////////////////////////////////////////////////////////////////////////////////////
24
25 #include <native_service/ns_timer_if.hpp>
26 #include <native_service/ns_message_center_if.h>
27 #include <string.h>
28
29 //////////////////////////////////////////////////////////////////////////////////////////////
30 ///// Constructor
31 //////////////////////////////////////////////////////////////////////////////////////////////
32 NSTimer::NSTimer():
33   m_hTimer(NULL),
34   m_u64TimeInterval(0),
35   m_bRepeatTimer(FALSE) {
36 }
37
38 //////////////////////////////////////////////////////////////////////////////////////////////
39 ///// Destructor
40 //////////////////////////////////////////////////////////////////////////////////////////////
41 NSTimer::~NSTimer() {
42   if (NULL != m_hTimer) {
43     NS_TimerDelete(m_hTimer);
44     m_hTimer = NULL;
45   }
46 }
47
48
49 //////////////////////////////////////////////////////////////////////////////////////////////
50 ///// SetNotifyMethod
51 //////////////////////////////////////////////////////////////////////////////////////////////
52 EFrameworkunifiedStatus NSTimer::SetNotifyMethod(UI_16 notifyCmdId, PCSTR notifyToAppName) {
53   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
54   NSTimerInfo l_tTimerInfo = {};
55
56   if ((NULL != notifyToAppName) && (strlen(notifyToAppName) <= MAX_SERVICE_NAME)) {
57     HANDLE l_hSenderHandle = McOpenSender(notifyToAppName);
58
59     if (NULL != l_hSenderHandle) {  // reset previous notify method, and set new
60       // resets previous timer timeout action if any
61       if (NULL != m_hTimer) {
62         NS_TimerDelete(m_hTimer);
63         m_hTimer = NULL;
64       }
65
66       l_tTimerInfo.iCmd = notifyCmdId;
67       // Create timer. Do not start.
68       if (NULL == (m_hTimer = NS_TimerCreate(l_tTimerInfo, CALLBACK_MESSAGE, l_hSenderHandle))) {
69         l_eStatus = eFrameworkunifiedStatusFail;
70       }
71
72       McClose(l_hSenderHandle);
73     } else {
74       // just return fail. Don't reset any previous notify method
75       l_eStatus = eFrameworkunifiedStatusFail;
76     }
77   } else {
78     l_eStatus = eFrameworkunifiedStatusInvldParam;
79   }
80
81   return l_eStatus;
82 }
83
84 //////////////////////////////////////////////////////////////////////////////////////////////
85 ///// SetRepeatTimer
86 //////////////////////////////////////////////////////////////////////////////////////////////
87 VOID NSTimer::SetRepeatTimer(BOOL repeatTimer) {
88   m_bRepeatTimer = repeatTimer;
89 }
90
91 //////////////////////////////////////////////////////////////////////////////////////////////
92 ///// SetTime
93 //////////////////////////////////////////////////////////////////////////////////////////////
94 VOID NSTimer::SetTime(UI_32 seconds, UI_64 msecs) {
95   m_u64TimeInterval = msecs + seconds * 1000ULL;  // internally, set interval as milliseconds
96 }
97
98 //////////////////////////////////////////////////////////////////////////////////////////////
99 ///// SetTime
100 //////////////////////////////////////////////////////////////////////////////////////////////
101 VOID NSTimer::SetTime(UI_32 hrs, UI_32 mins, UI_32 seconds, UI_64 msecs) {
102   // internally, set interval as milliseconds
103   m_u64TimeInterval = msecs + seconds * 1000ULL + mins * 60000ULL + hrs * 3600000ULL;
104 }
105
106 //////////////////////////////////////////////////////////////////////////////////////////////
107 ///// Start
108 //////////////////////////////////////////////////////////////////////////////////////////////
109 EFrameworkunifiedStatus NSTimer::Start(UI_32 seconds, UI_64 msecs) {
110   this->SetTime(seconds, msecs);
111   return (this->Start());
112 }
113
114 //////////////////////////////////////////////////////////////////////////////////////////////
115 ///// Start
116 //////////////////////////////////////////////////////////////////////////////////////////////
117 EFrameworkunifiedStatus NSTimer::Start() {
118   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
119   if (NULL != m_hTimer) {
120     NSTimerInfo l_tTimerInfo = {};
121
122     l_tTimerInfo.t_sec = WholeSeconds(static_cast<UI_32>(m_u64TimeInterval));  /// Converts to seconds
123     l_tTimerInfo.t_nsec = MSToNS(RemainderMs(static_cast<UI_32>(m_u64TimeInterval)));  /// Converts to nano seconds
124
125     if (TRUE == m_bRepeatTimer) {
126       l_tTimerInfo.rpt_sec = l_tTimerInfo.t_sec;
127       l_tTimerInfo.rpt_nsec = l_tTimerInfo.t_nsec;
128     }
129     l_eStatus = NS_TimerSetTime(m_hTimer, l_tTimerInfo);
130   } else {
131     l_eStatus = eFrameworkunifiedStatusFail;
132   }
133
134   return l_eStatus;
135 }
136
137 //////////////////////////////////////////////////////////////////////////////////////////////
138 ///// Stop
139 //////////////////////////////////////////////////////////////////////////////////////////////
140 EFrameworkunifiedStatus NSTimer::Stop() {
141   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
142
143   if (NULL != m_hTimer) {
144     NSTimerInfo l_tTimerInfo = {};
145     l_eStatus = NS_TimerSetTime(m_hTimer, l_tTimerInfo);
146   } else {
147     l_eStatus = eFrameworkunifiedStatusFail;
148   }
149
150   return l_eStatus;
151 }
152
153 //////////////////////////////////////////////////////////////////////////////////////////////
154 ///// IsRunning
155 //////////////////////////////////////////////////////////////////////////////////////////////
156 BOOL NSTimer::IsRunning() {
157   BOOL l_bIsRunning = FALSE;
158
159   if (NULL != m_hTimer) {
160     NSTimerInfo l_tTimerInfo = {};
161     NS_TimerGetTime(m_hTimer, &l_tTimerInfo);
162     if ((0 != l_tTimerInfo.t_sec) || (0 != l_tTimerInfo.t_nsec) || (0 != l_tTimerInfo.rpt_sec) ||
163         (0 != l_tTimerInfo.rpt_nsec)) {
164       l_bIsRunning = TRUE;
165     }
166   }
167   return l_bIsRunning;
168 }
169
170 //////////////////////////////////////////////////////////////////////////////////////////////
171 ///// GetInterval
172 //////////////////////////////////////////////////////////////////////////////////////////////
173 UI_64 NSTimer::GetInterval() {
174   return m_u64TimeInterval;
175 }
176
177 // EOF