Init basesystem source codes.
[staging/basesystem.git] / systemservice / interface_unified / library / src / ss_system_timer.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_SystemManagerIf
19 /// \brief    This file provides support for the System Manager client interface.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "system_service/ss_system_timer.h"
24 #include <native_service/ns_timer_if.h>
25 #include <native_service/ns_message_center_if.h>
26 #include <utility>
27 #include "ss_system_timer_local.h"
28
29 Timer::Timer() :
30   m_bInit(FALSE), m_hApp(NULL), m_hSnd(NULL), m_hTmr(NULL) {
31   bzero(&m_tTi, sizeof(m_tTi));
32 }
33
34 Timer::Timer(HANDLE hdl, UI_32 id, CbFuncPtr CbFn):
35   m_bInit(FALSE), m_hApp(NULL), m_hSnd(NULL), m_hTmr(NULL) {
36   bzero(&m_tTi, sizeof(m_tTi));
37   Initialize(hdl, id, CbFn);
38 }
39
40 Timer::~Timer() {
41   if (m_bInit) {
42     Shutdown();
43   }
44 }  // LCOV_EXCL_BR_LINE 10:Because destructor
45
46 BOOL Timer::Initialize(HANDLE hApp, UI_32 id, CbFuncPtr CbFn) {
47   if (!m_bInit && hApp && id && CbFn) {
48     m_hApp = hApp;
49     m_tTi.iCmd = id;
50     m_tTi.t_sec = 0;
51     m_tTi.t_nsec = 0;
52     m_tTi.rpt_sec = 0;
53     m_tTi.rpt_nsec = 0;
54     m_CbFn = CbFn;
55     m_hSnd = McOpenSender(FrameworkunifiedGetAppName(m_hApp));
56     m_hTmr = NS_TimerCreate(m_tTi, CALLBACK_MESSAGE, m_hSnd);
57     m_bInit = TRUE;
58   }
59   return m_bInit;
60 }
61
62 void Timer::Reinitialize(UI_32 id) {
63   if (!m_bInit && id) {
64     m_tTi.iCmd = id;
65
66     m_tTi.t_sec = 0;
67     m_tTi.t_nsec = 0;
68     m_tTi.rpt_sec = 0;
69     m_tTi.rpt_nsec = 0;
70
71     m_hSnd = McOpenSender(FrameworkunifiedGetAppName(m_hApp));
72     m_hTmr = NS_TimerCreate(m_tTi, CALLBACK_MESSAGE, m_hSnd);
73     m_bInit = TRUE;
74   }
75   return;
76 }
77
78 void Timer::Shutdown() {
79   if (m_bInit) {
80     Stop();
81     NS_TimerDelete(m_hTmr);
82     m_hTmr = NULL;
83     McClose(m_hSnd);
84     m_hSnd = NULL;
85     m_bInit = FALSE;
86   }
87 }
88
89 BOOL Timer::SetTime(UI_32 ss, UI_32 sms, UI_32 rs, UI_32 rms) {
90   if (m_bInit) {
91     m_tTi.t_sec = ss;
92     m_tTi.t_nsec = (ss || sms) ? sms*NS_PER_MS : 1;  // LCOV_EXCL_BR_LINE 11:Unexpected branch
93     m_tTi.rpt_sec = rs;
94     m_tTi.rpt_nsec = rms*NS_PER_MS;
95   }
96   return m_bInit;
97 }
98
99 BOOL Timer::Start(UI_32 ss, UI_32 sms, UI_32 rs, UI_32 rms) {
100   if (m_bInit) {
101     SetTime(ss, sms, rs, rms);
102     FrameworkunifiedAttachCallbackToDispatcher(m_hApp, TIMER_SERVICE_NAME, m_tTi.iCmd, m_CbFn);
103     NS_TimerSetTime(m_hTmr, m_tTi);
104   }
105   return m_bInit;
106 }
107
108 BOOL Timer::Start() {
109   if (m_bInit) {
110     FrameworkunifiedAttachCallbackToDispatcher(m_hApp, TIMER_SERVICE_NAME, m_tTi.iCmd, m_CbFn);
111     m_tTi.t_nsec = (m_tTi.t_sec || m_tTi.t_nsec) ? m_tTi.t_nsec : 1;  // LCOV_EXCL_BR_LINE 11:Unexpected branch
112     NS_TimerSetTime(m_hTmr, m_tTi);
113   }
114   return m_bInit;
115 }
116
117 BOOL Timer::Stop() {
118   if (m_bInit) {
119     NSTimerInfo ti = m_tTi;
120     ti.t_sec = 0;
121     ti.t_nsec = 0;
122     ti.rpt_sec = 0;
123     ti.rpt_nsec = 0;
124     NS_TimerSetTime(m_hTmr, ti);
125     FrameworkunifiedDetachCallbackFromDispatcher(m_hApp, TIMER_SERVICE_NAME, ti.iCmd);
126   }
127   return m_bInit;
128 }
129
130 // LCOV_EXCL_START 6:Because the condition cannot be set
131 TimerCtrl::TimerCtrl()
132   : m_hApp(NULL), m_nTimersMax(DEFAULT_NTIMERS) {
133 }
134
135 TimerCtrl::TimerCtrl(UI_32 ntimers)
136   : m_hApp(NULL), m_nTimersMax(ntimers) {
137 }
138
139 void TimerCtrl::Initialize(HANDLE hApp) {
140   if ( NULL != hApp ) {
141     m_hApp = hApp;
142     if (m_aTimers.empty() && m_rTimers.empty()) {
143       for (UI_32 i = DEFAULT_TIMERID; i < DEFAULT_TIMERID+m_nTimersMax; ++i) {
144         m_aTimers.push_back(i);
145       }
146     }
147   }
148 }
149
150 void TimerCtrl::Shutdown() {
151   m_aTimers.clear();
152   while (m_rTimers.begin() != m_rTimers.end()) {
153     Timer* p_timer = m_rTimers.begin()->second;
154     p_timer->Shutdown();
155     delete p_timer;
156     m_rTimers.erase(m_rTimers.begin());
157   }
158 }
159
160 UI_32 TimerCtrl::CreateTimer(CbFuncPtr CbFn) {
161   if (!m_aTimers.empty()) {
162     UI_32 id = m_aTimers.front();
163     m_aTimers.pop_front();
164     Timer* p_timer = new Timer(m_hApp, id, CbFn);
165     m_rTimers.insert(std::pair<UI_32, Timer*>(id, p_timer));
166     return id;
167   }
168   return 0;
169 }
170
171 UI_32 TimerCtrl::DeleteTimer(UI_32 id) {
172   if (m_rTimers.find(id) != m_rTimers.end()) {
173     Timer* p_timer = m_rTimers.find(id)->second;
174     p_timer->Shutdown();
175     delete p_timer;
176     m_rTimers.erase(m_rTimers.find(id));
177     m_aTimers.push_back(id);
178     return id;
179   }
180   return 0;
181 }
182
183 UI_32 TimerCtrl::SetTimer(UI_32 id, UI_32 ss, UI_32 sms, UI_32 rs, UI_32 rms) {
184   if (m_rTimers.find(id) != m_rTimers.end()) {
185     Timer* p_timer = m_rTimers.find(id)->second;
186     p_timer->SetTime(ss, sms, rs, rms);
187     return id;
188   }
189   return 0;
190 }
191 UI_32 TimerCtrl::StartTimer(UI_32 id, UI_32 ss, UI_32 sms, UI_32 rs, UI_32 rms) {
192   if (m_rTimers.find(id) != m_rTimers.end()) {
193     Timer* p_timer = m_rTimers.find(id)->second;
194     p_timer->Start(ss, sms, rs, rms);
195     return id;
196   }
197   return 0;
198 }
199
200 UI_32 TimerCtrl::StartTimerMulti(UI_32 id, UI_32 ss, UI_32 sms, UI_32 rs, UI_32 rms, UI_32 subId) {
201   if (m_rTimers.find(id) != m_rTimers.end()) {
202     Timer* p_timer = m_rTimers.find(id)->second;
203     p_timer->Shutdown();
204     p_timer->Reinitialize(id + subId);
205     p_timer->Start(ss, sms, rs, rms);
206     return id;
207   }
208   return 0;
209 }
210
211 UI_32 TimerCtrl::StartTimer(UI_32 id) {
212   if (m_rTimers.find(id) != m_rTimers.end()) {
213     Timer* p_timer = m_rTimers.find(id)->second;
214     p_timer->Start();
215     return id;
216   }
217   return 0;
218 }
219
220 UI_32 TimerCtrl::StopTimer(UI_32 id) {
221   if (m_rTimers.find(id) != m_rTimers.end()) {
222     Timer* p_timer = m_rTimers.find(id)->second;
223     p_timer->Stop();
224     return id;
225   }
226   return 0;
227 }
228 // LCOV_EXCL_STOP