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