sync with master
[platform/framework/native/appfw.git] / src / system / FSys_AlarmManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_AlarmManager.cpp
20  * @brief               This is the implementation file for _AlarmManager class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <FBaseSysLog.h>
26 #include <FSysSystemTime.h>
27
28 #include <FBase_NativeError.h>
29 #include <FIo_AppServiceIpcMessages.h>
30
31 #include "FSys_AlarmManager.h"
32 #include "FSys_CommunicationDispatcherClient.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Io;
38
39 namespace Tizen { namespace System
40 {
41
42 static const wchar_t* _ALARM_SERVICE_ID = L"osp.alarm.service";
43 static const wchar_t* _ALARM_SET_SINGLEALARM = L"osp.alarm.set.singlealarm";
44 static const wchar_t* _ALARM_SET_REPEATEDALARM = L"osp.alarm.set.repeatedalarm";
45 static const wchar_t* _ALARM_CANCEL = L"osp.alarm.cancel";
46 static const wchar_t* _ALARM_EXPIRED_EVENT = L"osp.alarm.expired.callback";
47 static const wchar_t* _ALARM_RESULT_OK = L"osp.alarm.result.ok";
48
49 const int _ALARM_SERVICE_IPC_MESSAGE_SERVICE_ID = 0;
50 const int _ALARM_SERVICE_IPC_MESSAGE_COMMAND_ID = 1;
51 const int _ALARM_SERVICE_RESPONSE_DATA = 2;
52 const int _ALARM_SERVICE_ALARM_ID = 3;
53
54  _AlarmManager* _AlarmManager::pAlarmManager = null;
55
56 _AlarmManager::_AlarmManager()
57 : __pIpcClient(null)
58 {
59         result r = E_SUCCESS;
60         static String ALARM_SERVICE_ID(_ALARM_SERVICE_ID);
61         __alarmList.Construct();
62
63         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
64         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is It is It is failed to get CommunicationDispatcherClient.");
65
66         r = pCommunicationDispatcherClient->RegisterCommunicationListener(ALARM_SERVICE_ID, *this);
67         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is It is It is failed to register on CommunicationDispatcherClient.");
68
69         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
70
71 CATCH:
72         SetLastResult(r);
73 }
74
75 _AlarmManager::~_AlarmManager()
76 {
77 }
78
79 _AlarmManager*
80 _AlarmManager::GetInstance(void)
81 {
82         if(pAlarmManager == null)
83         {
84                 pAlarmManager = new (std::nothrow) _AlarmManager();
85         }
86         return pAlarmManager;
87 }
88
89 result
90 _AlarmManager::RegisterAlarm(_AlarmImpl* pAlarmImpl)
91 {
92         SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
93         Integer alarmId(pAlarmImpl->__alarmId.value);
94         SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == false, E_OBJ_ALREADY_EXIST, "Required Alarm[%d] is already registered.", pAlarmImpl->__alarmId.value);
95
96         result r = E_SUCCESS;
97         ArrayList requestMessage;
98         ArrayList responseMessage;
99
100         String startTimeStr;
101         String periodStr;
102         String endTimeStr;
103
104         int period = pAlarmImpl->GetPeriod();
105  
106         String serviceId = _ALARM_SERVICE_ID;
107         String commandId = _ALARM_SET_SINGLEALARM;
108         if(period > 0) //If period is 0, alarm is operated as Single alarm.
109         {
110                 commandId = _ALARM_SET_REPEATEDALARM;
111         }
112
113         DateTime currentTime;
114         DateTime startTime = pAlarmImpl->GetStartTime();
115         const DateTime* pEndTime = pAlarmImpl->GetEndTime();
116
117         //Status check
118         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_INVALID_STATE, "This instance has not been constructed as yet.");
119
120         //Argument check
121         r = SystemTime::GetCurrentTime(WALL_TIME, currentTime);
122         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is It is failed to get current time");
123         SysTryReturnResult(NID_SYS, DateTime::Compare(currentTime, startTime) < 0, E_INVALID_ARG, "Designated start time has to be greater then current time");
124         SysTryReturnResult(NID_SYS, period > -1, E_INVALID_ARG, "Period has to greater then -1");
125
126         if(pEndTime != null)
127         {
128                 SysTryReturnResult(NID_SYS, DateTime::Compare(startTime, *pEndTime) < 0, E_INVALID_ARG, "Designated end time is less then start time");
129         }
130         //End Alarm validation check
131
132         //IPC data construction
133         r = requestMessage.Construct();
134         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to create request instance.");
135         r = responseMessage.Construct();
136         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to create response instance.");
137
138         r = requestMessage.Add(serviceId);
139         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add service id.");
140         r = requestMessage.Add(commandId);
141         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add command id.");
142         startTimeStr = startTime.ToString();
143         r = requestMessage.Add(startTimeStr);
144         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add start time value.");
145         periodStr.Append(period);
146         r = requestMessage.Add(periodStr);
147         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add period value.");
148
149         if(pEndTime != null)
150         {
151                 endTimeStr = pEndTime->ToString();
152                 r = requestMessage.Add(endTimeStr);
153                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to add end time value.");
154         }
155
156         std::unique_ptr<IoService_Request> pRequest(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
157
158         //IPC data delivery
159         r = __pIpcClient->SendRequest(pRequest.get());
160         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is It is failed to send ipc message.");
161         SysLog(NID_SYS, "Alarm reqeust is deliverted to osp-app-service successfully.");
162
163         //IPC data response check
164
165         String* pResult = (String*)responseMessage.GetAt(_ALARM_SERVICE_RESPONSE_DATA);
166         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result.");
167         SysTryReturnResult(NID_SYS, *pResult == _ALARM_RESULT_OK, E_SYSTEM, "It is It is failed to register alarm. [%ls]",  pResult->GetPointer());
168
169         String* pAlarmId = (String*)responseMessage.GetAt(_ALARM_SERVICE_ALARM_ID);
170         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no alarmId.");
171         SysLog(NID_SYS, "Reserved alarm id is %ls from osp-app-service.", pAlarmId->GetPointer());
172
173         int registeredAlarmId = 0;
174         Integer::Parse(*pAlarmId, registeredAlarmId);
175
176         pAlarmImpl->__alarmId.value = registeredAlarmId;
177         r = __alarmList.Add(&(pAlarmImpl->__alarmId), pAlarmImpl);
178         return r;
179 }
180
181 result
182 _AlarmManager::UnregisterAlarm(_AlarmImpl* pAlarmImpl)
183 {
184         result r = E_SUCCESS;
185         SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
186
187         Integer alarmId(pAlarmImpl->__alarmId.value);
188         SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == true, E_OBJ_NOT_FOUND, "There is no registered alarm.");
189         r = __alarmList.Remove(alarmId);
190
191         ArrayList requestMessage;
192         ArrayList responseMessage;
193         String serviceId(_ALARM_SERVICE_ID);
194         String commandId(_ALARM_CANCEL);
195
196         //Status check
197         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_INVALID_STATE, "This instance has not been constructed as yet.");
198
199         //IPC data construction
200         r = requestMessage.Construct();
201         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
202         r = responseMessage.Construct();
203         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
204
205         r = requestMessage.Add(serviceId);
206         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
207         r = requestMessage.Add(commandId);
208         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
209
210         String alarmIdStr;
211         alarmIdStr.Append(alarmId.value);
212
213         r = requestMessage.Add(alarmIdStr);
214         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add alarm id value");
215
216         //IPC data delivery
217         std::unique_ptr<IoService_Request> pRequest(new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
218
219         r = __pIpcClient->SendRequest(pRequest.get());
220         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send ipc message.");
221         SysLog(NID_SYS, "Alarm cancel reqeust is deliverted to osp-app-service successfully");
222
223         //IPC data response check
224         String* pResult = (String*)responseMessage.GetAt(_ALARM_SERVICE_RESPONSE_DATA);
225         
226         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "It is failed to receive Ipc response");
227         SysTryReturnResult(NID_SYS, *pResult == _ALARM_RESULT_OK, E_SYSTEM, "It is failed to cancel alarm");
228
229         return r;
230 }
231
232 result
233 _AlarmManager::UpdateAlarm(_AlarmImpl* pAlarmImpl)
234 {
235         result r = E_SUCCESS;
236         SysTryReturnResult(NID_SYS, pAlarmImpl != null, E_INVALID_ARG, "There is no alarmImpl.");
237
238         Integer alarmId(pAlarmImpl->__alarmId.value);
239         SysTryReturnResult(NID_SYS, __alarmList.ContainsKey(alarmId) == true, E_OBJ_NOT_FOUND, "There is no registered alarm.");
240
241         UnregisterAlarm(pAlarmImpl);
242         RegisterAlarm(pAlarmImpl);
243         return r;
244 }
245
246 void
247 _AlarmManager::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
248 {
249         SysLog(NID_SYS, "IPC message is delivered.");
250         String alarmIdStr;
251
252         String* pServiceId = (String*) data.GetAt(_ALARM_SERVICE_IPC_MESSAGE_SERVICE_ID);
253         String* pCommandId = (String*) data.GetAt(_ALARM_SERVICE_IPC_MESSAGE_COMMAND_ID);
254         String* pData = (String*) data.GetAt(_ALARM_SERVICE_RESPONSE_DATA);
255
256         SysLog(NID_SYS, "IPC message is delivered.");
257         String serviceId(_ALARM_SERVICE_ID);
258         String commandId(_ALARM_EXPIRED_EVENT);
259
260         SysTryReturnVoidResult(NID_SYS, pServiceId != null && pCommandId != null && pData != null, E_SYSTEM, "There is no device data.");
261
262         if(*pServiceId == serviceId && *pCommandId == commandId)
263         {
264                 SysLog(NID_SYS, "Alarm expire event is delivered.");
265                 int alarmId = 0;
266                 Integer::Parse(*pData, alarmId);
267
268                 Integer reservedAlarmId(alarmId);
269
270                 _AlarmImpl* pAlarmImpl = (_AlarmImpl*)__alarmList.GetValue(reservedAlarmId);
271                 if(pAlarmImpl != null)
272                 {
273                         SysLog(NID_SYS, "Reserved Alarm[%d] is found.", alarmId);
274                         pAlarmImpl->OnAlarmExpired(alarmId);
275                 }
276         }
277 }
278
279 } } // Tizen::System