2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSTimer
21 /// Timer class for setting timers.
23 //////////////////////////////////////////////////////////////////////////////////////////////////
25 #include <native_service/ns_timer_if.hpp>
26 #include <native_service/ns_message_center_if.h>
29 //////////////////////////////////////////////////////////////////////////////////////////////
31 //////////////////////////////////////////////////////////////////////////////////////////////
35 m_bRepeatTimer(FALSE) {
38 //////////////////////////////////////////////////////////////////////////////////////////////
40 //////////////////////////////////////////////////////////////////////////////////////////////
42 if (NULL != m_hTimer) {
43 NS_TimerDelete(m_hTimer);
49 //////////////////////////////////////////////////////////////////////////////////////////////
51 //////////////////////////////////////////////////////////////////////////////////////////////
52 EFrameworkunifiedStatus NSTimer::SetNotifyMethod(UI_16 notifyCmdId, PCSTR notifyToAppName) {
53 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
54 NSTimerInfo l_tTimerInfo = {};
56 if ((NULL != notifyToAppName) && (strlen(notifyToAppName) <= MAX_SERVICE_NAME)) {
57 HANDLE l_hSenderHandle = McOpenSender(notifyToAppName);
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);
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;
72 McClose(l_hSenderHandle);
74 // just return fail. Don't reset any previous notify method
75 l_eStatus = eFrameworkunifiedStatusFail;
78 l_eStatus = eFrameworkunifiedStatusInvldParam;
84 //////////////////////////////////////////////////////////////////////////////////////////////
86 //////////////////////////////////////////////////////////////////////////////////////////////
87 VOID NSTimer::SetRepeatTimer(BOOL repeatTimer) {
88 m_bRepeatTimer = repeatTimer;
91 //////////////////////////////////////////////////////////////////////////////////////////////
93 //////////////////////////////////////////////////////////////////////////////////////////////
94 VOID NSTimer::SetTime(UI_32 seconds, UI_64 msecs) {
95 m_u64TimeInterval = msecs + seconds * 1000ULL; // internally, set interval as milliseconds
98 //////////////////////////////////////////////////////////////////////////////////////////////
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;
106 //////////////////////////////////////////////////////////////////////////////////////////////
108 //////////////////////////////////////////////////////////////////////////////////////////////
109 EFrameworkunifiedStatus NSTimer::Start(UI_32 seconds, UI_64 msecs) {
110 this->SetTime(seconds, msecs);
111 return (this->Start());
114 //////////////////////////////////////////////////////////////////////////////////////////////
116 //////////////////////////////////////////////////////////////////////////////////////////////
117 EFrameworkunifiedStatus NSTimer::Start() {
118 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
119 if (NULL != m_hTimer) {
120 NSTimerInfo l_tTimerInfo = {};
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
125 if (TRUE == m_bRepeatTimer) {
126 l_tTimerInfo.rpt_sec = l_tTimerInfo.t_sec;
127 l_tTimerInfo.rpt_nsec = l_tTimerInfo.t_nsec;
129 l_eStatus = NS_TimerSetTime(m_hTimer, l_tTimerInfo);
131 l_eStatus = eFrameworkunifiedStatusFail;
137 //////////////////////////////////////////////////////////////////////////////////////////////
139 //////////////////////////////////////////////////////////////////////////////////////////////
140 EFrameworkunifiedStatus NSTimer::Stop() {
141 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
143 if (NULL != m_hTimer) {
144 NSTimerInfo l_tTimerInfo = {};
145 l_eStatus = NS_TimerSetTime(m_hTimer, l_tTimerInfo);
147 l_eStatus = eFrameworkunifiedStatusFail;
153 //////////////////////////////////////////////////////////////////////////////////////////////
155 //////////////////////////////////////////////////////////////////////////////////////////////
156 BOOL NSTimer::IsRunning() {
157 BOOL l_bIsRunning = FALSE;
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)) {
170 //////////////////////////////////////////////////////////////////////////////////////////////
172 //////////////////////////////////////////////////////////////////////////////////////////////
173 UI_64 NSTimer::GetInterval() {
174 return m_u64TimeInterval;