Merge "Alarm refactoring : remove IPC between appfw and app-service" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_SystemTimeImpl.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_SystemTimeImpl.cpp
20  * @brief               This is the implementation file for _SystemTimeImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <new>
25 #include <sys/sysinfo.h>
26 #include <sys/time.h>
27 #include <time.h>
28 #include <vconf.h>
29
30 #include <FBaseDateTime.h>
31 #include <FBaseResult.h>
32 #include <FBaseColArrayList.h>
33 #include <FBaseSysLog.h>
34
35 #include <FLcl_LocaleManagerImpl.h>
36 #include <FSys_Types.h>
37 #include <FSys_SystemTimeImpl.h>
38 #include <FIo_AppServiceIpcMessages.h>
39 #include <FIo_IpcClient.h>
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Io;
44 using namespace Tizen::Locales;
45
46 namespace Tizen { namespace System
47 {
48
49 _SystemTimeImpl::_SystemTimeImpl(void)
50 {
51 }
52
53 _SystemTimeImpl::~_SystemTimeImpl(void)
54 {
55 }
56
57 result
58 _SystemTimeImpl::GetUptime(TimeSpan& uptime)
59 {
60         int ret = 0;
61         long long ticks = 0;
62         struct timespec timeSpec;
63
64         ret = clock_gettime(CLOCK_MONOTONIC_RAW, &timeSpec);
65         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the time ");
66
67         ticks = (long long) timeSpec.tv_sec * 1000ll;
68         ticks += (long long) timeSpec.tv_nsec / 1000000ll;
69         uptime = TimeSpan(ticks);
70
71         return E_SUCCESS;
72 }
73
74 result
75 _SystemTimeImpl::GetCurrentTime(TimeMode timeMode, DateTime& currentTime)
76 {
77         struct tm* pGmTime = null;
78         time_t currTime = 0;
79         TimeZone timeZone;
80         DateTime tempTime;
81         struct timeval currentTimeVal;
82
83         _LocaleManagerImpl* plm = null;
84
85         time(&currTime);
86         gettimeofday(&currentTimeVal, null);
87
88         pGmTime = gmtime(&currTime);
89         SysTryReturnResult(NID_SYS, null != pGmTime, E_SYSTEM, "Failed to convert the time value to UTC time");
90
91         plm = new (std::nothrow) _LocaleManagerImpl();
92         SysTryReturnResult(NID_SYS, null != plm, E_SYSTEM, "_LocaleManagerImpl instance must not be null");
93
94         timeZone = plm->GetSystemTimeZone();
95         delete plm;
96         String timeZoneId = timeZone.GetId();
97         int rawOffset = timeZone.GetRawOffset();
98         int dstSavings = timeZone.GetDstSavings();
99         SysLog(NID_SYS, "Time zone: %S, %d, %d", timeZoneId.GetPointer(), rawOffset, dstSavings);
100
101
102         tempTime.SetValue(pGmTime->tm_year + 1900, pGmTime->tm_mon + 1, pGmTime->tm_mday, pGmTime->tm_hour, pGmTime->tm_min, pGmTime->tm_sec, currentTimeVal.tv_usec / 1000);
103
104         SysLog(NID_SYS, "Original Time %d %d/%d, %d:%d:%d:%d", tempTime.GetYear(), tempTime.GetMonth(), tempTime.GetDay(), tempTime.GetHour(), tempTime.GetMinute(), tempTime.GetSecond(), tempTime.GetMillisecond());
105
106         switch (timeMode)
107         {
108         case UTC_TIME:
109                 currentTime.SetValue(tempTime);
110                 break;
111
112         case STANDARD_TIME:
113                 currentTime = timeZone.UtcTimeToStandardTime(tempTime);
114                 break;
115
116         case WALL_TIME:
117                 currentTime = timeZone.UtcTimeToWallTime(tempTime);
118                 break;
119
120         default:
121                 break;
122         }
123
124         SysLog(NID_SYS, "Current Time %d %d/%d, %d:%d:%d", currentTime.GetYear(), currentTime.GetMonth(), currentTime.GetDay(), currentTime.GetHour(), currentTime.GetMinute(), currentTime.GetSecond());
125
126         return E_SUCCESS;
127 }
128
129 result
130 _SystemTimeImpl::GetCurrentTime(DateTime& currentTime)
131 {
132         return GetCurrentTime(UTC_TIME, currentTime);
133 }
134
135 result
136 _SystemTimeImpl::SetCurrentTime(const DateTime& currentTime)
137 {
138         result r = E_SUCCESS;
139
140         ArrayList requestMessage;
141         ArrayList responseMessage;
142         char datetime[32] = {0,};
143
144         LocaleManager localeManager;
145         localeManager.Construct();
146         TimeZone timeZone = localeManager.GetSystemTimeZone();
147
148         DateTime wallTime = timeZone.UtcTimeToWallTime(currentTime);
149
150         std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient());
151         r = pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID);
152         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to create IpcClient", GetErrorMessage(r));
153
154         requestMessage.Construct();
155         responseMessage.Construct();
156
157         String serviceId = _SYSTEM_SERVICE_ID;
158         String commandId = _SYSTEM_COMMAND_CHANGE_TIME ;
159         String dateTime;
160
161         sprintf(datetime, "%03d %02d %02d %02d:%02d:%02d:%03d", wallTime.GetYear() - 1900, wallTime.GetMonth()-1, wallTime.GetDay(), wallTime.GetHour(), wallTime.GetMinute(), wallTime.GetSecond(), wallTime.GetMillisecond());
162         dateTime.Append(datetime);
163
164         requestMessage.Add(serviceId);
165         requestMessage.Add(commandId);
166         requestMessage.Add(dateTime);
167
168         std::unique_ptr<IoService_Request> pMsg (new (std::nothrow) IoService_Request(requestMessage, &responseMessage));
169         SysTryReturnResult(NID_SYS, pMsg != null, E_SYSTEM, "[E_SYSTEM] fail to create Ipc message");
170
171         r = pIpcClient->SendRequest(pMsg.get());
172         SysTryReturn(NID_SYS, r == E_SUCCESS, E_SYSTEM, r, "[%s] Fail to ipc message", GetErrorMessage(r));
173
174         String* pResult = (String*)responseMessage.GetAt(_SYSTEM_RESPONSE_DATA);
175
176         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "[E_SYSTEM] fail to receive Ipc response");
177         SysTryReturnResult(NID_SYS, *pResult != _SYSTEM_RESULT_PRIVILEGED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] applciation does not have proper privilege.");
178         SysTryReturnResult(NID_SYS, *pResult == _SYSTEM_RESULT_OK, E_SYSTEM, "[E_SYSTEM] fail to set time");
179
180         vconf_sync_key(VCONFKEY_SYSTEM_TIME_CHANGED);
181         
182         return r;
183 }
184 result
185 _SystemTimeImpl::GetTicks(long long& ticks)
186 {
187         int ret = 0;
188         struct timeval timeValue;
189
190         ret = gettimeofday(&timeValue, null);
191         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get the date and Time");
192
193         ticks = (long long) timeValue.tv_sec * 1000ll;
194         ticks += (long long) timeValue.tv_usec / 1000ll;
195
196         return E_SUCCESS;
197 }
198
199 result
200 _SystemTimeImpl::GetNanoTicks(long long& nanoTicks)
201 {
202         int ret = 0;
203         struct timespec realTime;
204
205         ret = clock_gettime(CLOCK_MONOTONIC_RAW, &realTime);
206
207         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "Failed to get clock information");
208
209         nanoTicks = (long long) realTime.tv_sec * 1000000000;
210         nanoTicks += (long long) realTime.tv_nsec;
211
212         return E_SUCCESS;
213 }
214
215 _SystemTimeImpl*
216 _SystemTimeImpl::GetInstance(SystemTime& systemtime)
217 {
218         return systemtime.__pSystemTimeImpl;
219 }
220
221 const _SystemTimeImpl*
222 _SystemTimeImpl::GetInstance(const SystemTime& systemtime)
223 {
224         return systemtime.__pSystemTimeImpl;
225 }
226
227
228 } } // Tizen::System