Update code of system and text for reporting Klocwork.
[platform/framework/native/appfw.git] / src / system / FSys_AlarmImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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  * @file                FSys_AlarmImpl.cpp
19  * @brief               This is the implementation file for _AlarmImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include <FBaseRtIEventArg.h>
28 #include <FBaseSysLog.h>
29
30 #include <FBase_NativeError.h>
31 #include "FSys_AlarmImpl.h"
32 #include "FSys_AlarmManager.h"
33 #include "FSys_SystemTimeImpl.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Io;
39
40 namespace Tizen { namespace System
41 {
42
43 class _AlarmEventArg : public IEventArg
44 {
45 public:
46         _AlarmEventArg()
47         : pAlarm(null)
48         {
49         }
50         Alarm* pAlarm;
51 };
52
53 class _AlarmEvent : public _Event
54 {
55 public:
56         result Construct(void)
57         {
58                 return _Event::Initialize();
59         }
60
61 protected:
62         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
63         {
64                 SysLog(NID_SYS, "Alarm is expired.");
65                 IAlarmEventListener* pListener = dynamic_cast<IAlarmEventListener*> (&listener);
66                 const _AlarmEventArg* pArg = dynamic_cast<const _AlarmEventArg*>(&arg);
67
68                 if(pListener == null || pArg == null)
69                 {
70                         SysLogException(NID_SYS, E_SYSTEM, "listener or arg is null.");
71                         return;
72                 }
73
74                 if(pArg->pAlarm == null)
75                 {
76                         SysLogException(NID_SYS, E_SYSTEM, "Alarm data is invalid.");
77                         return;
78                 }
79
80                 pListener->OnAlarmExpired(*(pArg->pAlarm));
81         }
82 };
83
84 _AlarmImpl::_AlarmImpl(Alarm* pAlarm)
85         : __alarmId(0)
86         , __period(0)
87         , __pEndTime(null)
88         , __isSetEndTime(false)
89         , __pEvent(null)
90         , __pAlarm(pAlarm)
91 {
92
93 }
94
95 _AlarmImpl::~_AlarmImpl(void)
96 {
97         if(__alarmId.value != 0)
98         {
99                 Cancel();
100         }
101
102         if(__pEvent != null)
103         {
104                 delete __pEvent;
105                 __pEvent = null;
106         }
107 }
108
109 result
110 _AlarmImpl::Construct(void)
111 {
112         return E_SUCCESS;
113 }
114
115 result
116 _AlarmImpl::Set(const DateTime& startTime, int period, const DateTime* pEndTime)
117 {
118         result r = E_SUCCESS;
119         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
120         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
121
122         __startTime = startTime;
123         __period = period;
124
125         if(pEndTime != null)
126         {
127                 DateTime* pRequiredEndTime = const_cast <DateTime*> (pEndTime);
128                 __pEndTime = pRequiredEndTime;
129                 __isSetEndTime = true;
130         }
131         else
132         {
133                 __isSetEndTime = false;
134         }
135
136         if(__alarmId.value == 0)
137         {
138                 r = pAlarmManager->RegisterAlarm(this);
139         }
140         else
141         {
142                 r = pAlarmManager->UpdateAlarm(this);
143         }
144
145         if(r != E_SUCCESS && r != E_INVALID_ARG)
146         {
147                 SysLog(NID_SYS, "It is failed to register alarm. [%s]", GetErrorMessage(r));
148                 r = E_SYSTEM;
149         }
150         return r;
151 }
152
153 result
154 _AlarmImpl::Cancel(void)
155 {
156         result r = E_SUCCESS;
157         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
158         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
159
160         r = pAlarmManager->UnregisterAlarm(this);
161
162         if(r != E_SUCCESS && r != E_INVALID_ARG)
163         {
164                 SysLog(NID_SYS, "It is failed to cancel alarm. [%s]", GetErrorMessage(r));
165                 r = E_SYSTEM;
166         }
167
168         __alarmId.value = 0;
169
170         return r;
171 }
172
173 const DateTime
174 _AlarmImpl::GetStartTime(void) const
175 {
176         ClearLastResult();
177         return __startTime;
178 }
179
180 int
181 _AlarmImpl::GetPeriod(void) const
182 {
183         ClearLastResult();
184         return __period.ToInt();
185 }
186
187 const DateTime*
188 _AlarmImpl::GetEndTime(void) const
189 {
190         ClearLastResult();
191         return __pEndTime;
192 }
193
194 result
195 _AlarmImpl::SetAlarmEventListener(IAlarmEventListener* pListener)
196 {
197         if(pListener == null)
198         {
199                 SysLog(NID_SYS, "Listener null set doesn't mean disalarm");
200                 if(__pEvent != null)
201                 {
202                         delete __pEvent;
203                         __pEvent = null;
204                 }
205         }
206         else
207         {
208                 if(__pEvent == null)
209                 {
210                         _AlarmEvent* pAlarmEvent = new (std::nothrow) _AlarmEvent();
211                         SysTryReturnResult(NID_SYS, pAlarmEvent != null, E_OUT_OF_MEMORY, "It is failed to create Event instance");
212                         pAlarmEvent->Construct();
213                         __pEvent = pAlarmEvent;
214                 }
215                 __pEvent->AddListener(*pListener);
216         }
217         return E_SUCCESS;
218 }
219
220 void 
221 _AlarmImpl::OnAlarmExpired(int alarmId)
222 {
223         if(__pEvent != null)
224         {
225                 _AlarmEventArg* pAlarmEventArg= new (std::nothrow) _AlarmEventArg();
226                 if(pAlarmEventArg == null)
227                 {
228                         SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _AlarmEventArg class.");
229                         return;
230                 }
231                 pAlarmEventArg->pAlarm = this->__pAlarm;
232                 if(pAlarmEventArg != null)
233                 {
234                         SysLog(NID_SYS, "AlarmEventArg is ready.");
235                         __pEvent->FireAsync(*pAlarmEventArg);
236                 }
237
238                 SysLog(NID_SYS, "Alarm Expired for alarm id: %d and listener called.", alarmId);
239         }
240         else
241         {
242                 SysLog(NID_SYS, "Reserved alarm is expired. However, Listener point is unavailable.");
243         }
244 }
245
246
247 _AlarmImpl*
248 _AlarmImpl::GetInstance(Alarm* pAlarm)
249 {
250         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
251 }
252
253 const _AlarmImpl*
254 _AlarmImpl::GetInstance(const Alarm* pAlarm)
255 {
256         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
257 }
258
259 } } // Tizen::System