Remove white space
[platform/framework/native/appfw.git] / src / system / FSys_RuntimeInfoImpl.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                FSysRuntimeInfo.cpp
19  * @brief               This is the implementation file for _RuntimeInfoImpl class.
20  *
21  *
22  * This file contains the implementation of _RuntimeInfoImpl class.
23  */
24
25 #include <unique_ptr.h>
26 #include <pthread.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/vfs.h>
32 #include <app_storage.h>
33 #include <devman.h>
34 #include <runtime_info.h>
35 #include <vconf.h>
36
37 #include <FIo.h>
38 #include <FAppApp.h>
39 #include <FBaseRtEvent.h>
40 #include <FBaseRtIEventArg.h>
41 #include <FBaseRtIEventListener.h>
42 #include <FBaseRtThread.h>
43 #include <FBaseSysLog.h>
44 #include <FApp_AppInfo.h>
45 #include <FBase_StringConverter.h>
46
47 #include "FSys_EnvironmentImpl.h"
48 #include "FSys_RuntimeInfoImpl.h"
49 #include "FSys_RuntimeClient.h"
50 #include "FSys_RuntimeClientEx.h"
51
52 using namespace Tizen::App;
53 using namespace Tizen::Base;
54 using namespace Tizen::Base::Runtime;
55
56 using namespace std;
57
58 namespace Tizen { namespace System
59 {
60
61 #ifndef VCONFKEY_SERVICE_READY
62 #define VCONFKEY_SERVICE_READY  "memory/deviced/boot_power_on"
63 #endif
64
65 static const wchar_t* _ALLOCATED_MEMORY = L"AllocatedMemory";
66 static const wchar_t* _MEMORY_ALLOCATED = L"http://tizen.org/runtime/memory.allocated";
67 static const wchar_t* _MEMORY_ALLOCATED_SELF = L"http://tizen.org/runtime/memory.allocated.self";
68
69 static const wchar_t* _AVAILABLE_EXTERNAL_STORAGE = L"AvailableExternalStorage";
70 static const wchar_t* _STORAGE_AVAILABLE_EXTERNAL = L"http://tizen.org/runtime/storage.available.external";
71
72 static const wchar_t* _AVAILABLE_INTERNAL_STORAGE = L"AvailableInternalStorage";
73 static const wchar_t* _STORAGE_AVAILABLE_INTERNAL = L"http://tizen.org/runtime/storage.available.internal";
74
75 static const wchar_t* _AVAILABLE_MEDIA_STORAGE = L"AvailableMediaStorage";
76 static const wchar_t* _STORAGE_AVAILABLE_INTERNAL_MEDIA = L"http://tizen.org/runtime/storage.available.internal.media";
77
78 static const wchar_t* _BATTERY_LEVEL = L"BatteryLevel";
79 static const wchar_t* _CHARGING = L"IsCharging";
80
81 static const wchar_t* _CPU_CORE_ALL_USAGE = L"http://tizen.org/runtime/cpu.core.all.usage";
82
83 static const wchar_t* _MAX_ALLOCATABLE_MEMORY = L"MaxAllocatableMemory";
84 static const wchar_t* _AVAILABLE_VIDEO_MEMORY = L"AvailableVideoMemory";
85 static const wchar_t* _ALLOCATED_VIDEO_MEMORY = L"AllocatedVideoMemory";
86 static const wchar_t* _AVAILABLE_MEMORY = L"AvailableMemory";
87 static const wchar_t* _MEMORY_AVAILABLE = L"http://tizen.org/runtime/memory.available";
88 static const wchar_t* _MEMORY_AVAILABLE_VIDEO = L"http://tizen.org/runtime/memory.available.video";
89 static const wchar_t* _MEMORY_ALLOCATED_VIDEO = L"http://tizen.org/runtime/memory.allocated.video";
90
91 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_APPLICATION = L"http://tizen.org/runtime/storage.allocated.internal.application";
92 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_AUDIO = L"http://tizen.org/runtime/storage.allocated.internal.audio";
93 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_VIDEO = L"http://tizen.org/runtime/storage.allocated.internal.video";
94 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_IMAGE = L"http://tizen.org/runtime/storage.allocated.internal.image";
95 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_DOWNLOAD = L"http://tizen.org/runtime/storage.allocated.internal.download";
96 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_APPLICATION = L"http://tizen.org/runtime/storage.allocated.external.application";
97 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_AUDIO = L"http://tizen.org/runtime/storage.allocated.external.audio";
98 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_VIDEO = L"http://tizen.org/runtime/storage.allocated.external.video";
99 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_IMAGE = L"http://tizen.org/runtime/storage.allocated.external.image";
100 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_DOWNLOAD = L"http://tizen.org/runtime/storage.allocated.external.download";
101
102 static const wchar_t _BOOT_STATUS_KEY[] = L"http://tizen.org/runtime/system.status";
103 static const wchar_t _BOOT_STATUS_INITIALIZING[] = L"initializing";
104 static const wchar_t _BOOT_STATUS_READY[]  = L"ready";
105
106
107 static const int _RUNTIME_INFO_EVENT_TYPE_INT = 101;
108 static const int _RUNTIME_INFO_EVENT_TYPE_LONGLONG = 102;
109
110
111 static const char* const _PROC_SYSTEM_MEMORY_INFO = "/proc/meminfo";
112 static const char* const _PROC_PROCESS_MEMORY_INFO = "/proc/self/status";
113 static const char* const _PROC_STAT = "/proc/stat";
114
115 static const char* const _PROC_KEY_SYSTEM_MEMORY_TOTAL = "MemTotal";
116 static const char* const _PROC_KEY_SYSTEM_MEMORY_FREE = "MemFree";
117 static const char* const _PROC_KEY_PROCESS_MEMORY = "VmSize";
118
119 static const char* const _ROOT_PATH = "/";
120
121 static const int _MAX_BUFFER_LENGTH = 1024;
122
123 static const int _CPU_USAGE_EVALUATION_INTERVAL = 1000000; //1 sec
124
125 typedef struct {
126     char cpu[20];
127     unsigned long long user;
128     unsigned long long system;
129     unsigned long long nice;
130     unsigned long long idle;
131     unsigned long long wait;
132     unsigned long long hi;
133     unsigned long long si;
134     unsigned long long zero;
135 } procstat_t;
136
137 bool read_proc_stat(procstat_t *p)
138 {
139         FILE *fp = null;
140         int ret = 0;
141
142         fp = fopen(_PROC_STAT, "r");
143         if(fp == null)
144         {
145                 return false;
146         }
147
148         ret = fscanf(fp, "%s %llu %llu %llu %llu %llu %llu %llu %llu",
149         p->cpu,
150         &p->user,
151         &p->system,
152         &p->nice,
153         &p->idle,
154         &p->wait,
155         &p->hi,
156         &p->si,
157         &p->zero);
158
159         if(ret < 1)
160         {
161                 if(fp != null)
162                 {
163                         fclose(fp);
164                 }
165                 return false;
166         }
167
168         if(fp != null)
169         {
170                 fclose(fp);
171         }
172
173         return true;
174 }
175  
176 void diff_proc_stat(procstat_t *a, procstat_t *b, procstat_t *d)
177 {
178         d->user     = a->user   - b->user;
179         d->system   = a->system - b->system;
180         d->nice     = a->nice   - b->nice;
181         d->idle     = a->idle   - b->idle;
182         d->wait     = a->wait   - b->wait;
183         d->hi       = a->hi     - b->hi;
184         d->si       = a->si     - b->si;
185         d->zero     = a->zero   - b->zero;
186 }
187
188 class _RuntimeInfoEventArg : public IEventArg
189 {
190 public:
191         _RuntimeInfoEventArg()
192         : Type(0)
193         , intValue(0)
194         , longLongValue(0)
195         , errorCode(E_SUCCESS)
196         , DirectoryPath(null)
197         , pListener(null)
198         {
199         }
200
201         ~_RuntimeInfoEventArg()
202         {
203                 if(DirectoryPath != null)
204                 {
205                         delete [] DirectoryPath;
206                 }
207         }
208
209         int Type;
210         int intValue;
211         long long longLongValue;
212         result errorCode;
213         char* DirectoryPath;
214         IEventListener* pListener;
215 };
216
217 class _RuntimeInfoEvent : public Event
218 {
219 protected:
220         void FireImpl(IEventListener& listener, const IEventArg& arg)
221         {
222                 const _RuntimeInfoEventArg* runtimeArg = dynamic_cast<const _RuntimeInfoEventArg*> (&arg);
223
224                 if(runtimeArg == null)
225                 {
226                         SysLogException(NID_SYS, E_SYSTEM, "Argument is null.");
227                         return;
228                 }
229
230                 switch(runtimeArg->Type)
231                 {
232                 case _RUNTIME_INFO_EVENT_TYPE_INT:
233                 {
234                         IRuntimeInfoGetIntAsyncResultListener* pIntListener = dynamic_cast<IRuntimeInfoGetIntAsyncResultListener*>(&listener);
235
236                         if(pIntListener == runtimeArg->pListener)
237                         {                               
238                                 pIntListener->OnResultReceivedForGetValueAsync(runtimeArg->intValue, runtimeArg->errorCode);
239                                 RemoveListener(listener);
240                         }
241                 }
242                 break;
243                 case _RUNTIME_INFO_EVENT_TYPE_LONGLONG:
244                 {
245                         IRuntimeInfoGetLonglongAsyncResultListener* pLonglongListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*>(&listener);
246                         if(pLonglongListener == runtimeArg->pListener)
247                         {
248                                 pLonglongListener->OnResultReceivedForGetValueAsync(runtimeArg->longLongValue, runtimeArg->errorCode);
249                                 RemoveListener(listener);
250                         }
251                 }
252                 break;
253                 }
254         }
255 };
256
257 _RuntimeInfoEvent runtimeInfoEvent;
258
259 _RuntimeInfoImpl::_RuntimeInfoImpl(void)
260 {
261 }
262
263 _RuntimeInfoImpl::~_RuntimeInfoImpl(void)
264 {
265 }
266
267 result
268 _RuntimeInfoImpl::GetValue(const String& key, String& value)
269 {
270         if (key == _BOOT_STATUS_KEY)
271         {
272                 int bootReady = -1;
273                 const int errorCode = vconf_get_int(VCONFKEY_SERVICE_READY, &bootReady);
274                 if (errorCode != 0)
275                 {
276                         value = (bootReady == 1 ) ? _BOOT_STATUS_READY : _BOOT_STATUS_INITIALIZING;
277
278                         return E_SUCCESS;
279                 }
280
281                 return E_SYSTEM;
282         }
283         else
284         {
285                 return E_OBJ_NOT_FOUND;
286         }
287 }
288
289 result
290 _RuntimeInfoImpl::GetValue(const String& key, int& value)
291 {
292         int total_size = 0;
293         int free_size =0;
294         result r = E_OBJ_NOT_FOUND;
295
296         if (key == _BATTERY_LEVEL)
297         {
298                 return GetBatteryLevel(value);
299         }
300         else if (key == _ALLOCATED_MEMORY || key == _ALLOCATED_VIDEO_MEMORY)
301         {
302                 r = GetFromProc(_PROC_PROCESS_MEMORY_INFO, _PROC_KEY_PROCESS_MEMORY, free_size);
303                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
304                 value = free_size * 1024;
305         }
306         else if (key == _AVAILABLE_MEMORY || key == _AVAILABLE_VIDEO_MEMORY || key == _MAX_ALLOCATABLE_MEMORY)
307         {
308                 r = GetFromProc(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_FREE, free_size);
309                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
310                 value = free_size * 1024;
311         }
312         return r;
313 }
314
315 result
316 _RuntimeInfoImpl::GetValue(const String& key, long long& value)
317 {
318
319         long long total_size = 0;
320         long long free_size =0;
321         result r = E_OBJ_NOT_FOUND;
322
323         if (key == _MEMORY_AVAILABLE || key == _MEMORY_AVAILABLE_VIDEO)
324         {
325                 r = GetFromProcLonglong(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_FREE, free_size);
326                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
327                 value = free_size * 1024;
328         }
329         else if (key == _MEMORY_ALLOCATED || key == _MEMORY_ALLOCATED_VIDEO)
330         {
331                 r = GetFromProcLonglong(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_TOTAL, total_size);
332                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
333                 r = GetFromProcLonglong(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_FREE, free_size);
334                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
335                 value = (total_size - free_size) * 1024;
336         }
337         else if (key == _MEMORY_ALLOCATED_SELF)
338         {   
339                 r = GetFromProcLonglong(_PROC_PROCESS_MEMORY_INFO, _PROC_KEY_PROCESS_MEMORY, free_size);
340                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
341                 value = free_size * 1024;
342         } 
343         else if (key == _AVAILABLE_INTERNAL_STORAGE)
344         {
345                 return GetAvailableInternalStorage(value);
346         }
347         else if (key == _AVAILABLE_EXTERNAL_STORAGE)
348         {
349                 return GetAvailableExternalStorage(value);
350         }
351         else if (key == _AVAILABLE_MEDIA_STORAGE)
352         {
353                 return GetAvailableMediaStorage(value);
354         }
355         else if(key == _STORAGE_AVAILABLE_INTERNAL)
356         {
357                 return GetAvailableInternalStorage(value);
358         }
359         else if(key == _STORAGE_AVAILABLE_EXTERNAL)
360         {
361                 return GetAvailableExternalStorage(value);
362         }
363         else if(key == _STORAGE_AVAILABLE_INTERNAL_MEDIA)
364         {
365                 return GetAvailableMediaStorage(value);
366         }
367         else
368         {
369                 SysLogException(NID_SYS, r, "Key[%ls] is not valid.", key.GetPointer());
370         }
371         return r;
372 }
373
374 long long
375 _RuntimeInfoImpl::GetDirectorySize(const char* path)
376 {
377         if(path == null)
378         {
379                 return 0;
380         }
381
382         char command[512] = {0,};
383         char fileName[512] = {0,};
384         long long size = 0;
385         int ret = 0;
386
387         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
388         if(pApp == null)
389         {
390                 return 0;
391         }
392
393         String appId(pApp->GetAppId());
394         unique_ptr <char> appIdPath(_StringConverter::CopyToCharArrayN(appId));
395
396         ret = sprintf(fileName, "/tmp/size_of_directory_%s.tmp", appIdPath.get());
397         if(ret < 1)
398         {
399                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write file path.");
400                 return 0;
401         }
402
403         ret = sprintf(command, "rm -rf %s", fileName);
404         if(ret < 1)
405         {
406                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write remove command.");
407                 return 0;
408         }
409
410         ret = system(command);
411
412         if(ret == -1)
413         {
414                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
415         }
416
417         ret = sprintf(command, "du -skb -P %s >> %s", path, fileName);
418         if(ret < 1)
419         {
420                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write du command.");
421                 return 0;
422         }
423
424         ret = system(command);
425         if(ret == -1)
426         {
427                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
428                 return 0;
429         }
430
431         FILE* pFile = null;
432         pFile = fopen(fileName, "r");
433         if(pFile == null)
434         {
435                 return 0;
436         }
437
438         ret = fscanf(pFile, "%lld", &size);
439         if(ret < 1)
440         {
441                 if(pFile != null)
442                 {
443                         fclose(pFile);
444                 }
445
446                 return 0;
447         }
448
449         if(pFile != null)
450         {
451                 fclose(pFile);
452         }
453
454         ret = sprintf(command, "rm -rf %s", fileName);
455         if(ret < 1)
456         {
457                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write remove command.");
458                 return 0;
459         }
460
461         ret = system(command);
462         if(ret == -1)
463         {
464                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
465         }
466
467
468         return size;
469 }
470
471 result
472 _RuntimeInfoImpl::GetValue(const String& key, double& value)
473 {
474         return E_OBJ_NOT_FOUND;
475 }
476
477 result
478 _RuntimeInfoImpl::GetValue(const String& key, bool& value)
479 {
480         if (key == _CHARGING)
481         {
482                 return IsChargingMode(value);
483         }
484         else
485         {
486                 return E_OBJ_NOT_FOUND;
487         }
488 }
489
490 result
491 _RuntimeInfoImpl::GetValue(const String& key, UuId& value)
492 {
493         return E_OBJ_NOT_FOUND;
494 }
495
496 result
497 _RuntimeInfoImpl::GetBatteryLevel(int& value)
498 {
499         int batteryValue = -1;
500         batteryValue = device_get_battery_pct();
501         SysTryReturnResult(NID_SYS, !(batteryValue < 0 || value > 100), E_SYSTEM, "[E_SYSTEM] device_get_battery_pct return %d", batteryValue);
502         value = batteryValue;
503
504         return E_SUCCESS;
505 }
506
507 result
508 _RuntimeInfoImpl::IsChargingMode(bool& value)
509 {
510         bool chargeState = false;
511         int ret = 0;
512         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
513         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] runtime_info_get_value_bool returns %d.", ret);
514         value = chargeState;
515         return E_SUCCESS;
516
517 }
518
519 result
520 _RuntimeInfoImpl::GetAvailableInternalStorage(long long& value)
521 {
522         long long total = 0;
523         result r = E_SUCCESS;
524
525         r = GetCapacity(_ROOT_PATH, total, value);
526         SysTryReturn(NID_SYS, !IsFailed(r), E_SYSTEM, r, "[E_SYSTEM] system error has occurred. ");
527
528         return E_SUCCESS;
529 }
530 result
531 _RuntimeInfoImpl::GetAvailableExternalStorage(long long& value)
532 {
533         int sdCardStatus = 0;
534         int ret = -1;
535         long long total = 0;
536
537         ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &sdCardStatus);
538         SysTryReturn(NID_SYS, !(ret < 0), E_SYSTEM, E_SYSTEM, "[E_SYSTEM] vconf_get_int VCONFKEY_SYSMAN_MMC_STATUS failed");
539
540         if(sdCardStatus == 0)
541         {
542                 value = 0;
543                 return E_SUCCESS;
544         }
545         char* directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetExternalStoragePath());
546         SysTryReturnResult(NID_SYS, directoryPath != null, E_SYSTEM, "It is failed to get external directory path.");
547         result r = GetCapacity(directoryPath, total, value);
548         delete [] directoryPath;
549         return r ;
550 }
551
552 result
553 _RuntimeInfoImpl::GetAvailableMediaStorage(long long& value)
554 {
555         long long total = 0;
556
557         char* directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetMediaPath());
558         SysTryReturnResult(NID_SYS, directoryPath != null, E_SYSTEM, "It is failed to get media directory path.");
559         result r = GetCapacity(directoryPath, total, value);
560         delete [] directoryPath;
561         return r;
562 }
563
564 result
565 _RuntimeInfoImpl::GetCapacity(const char* path, long long& totalSize, long long& availableSize)
566 {
567         struct statfs fs;
568         if (statfs(path, &fs) < 0)
569         {
570                 SysLogException(NID_SYS, E_SYSTEM, "[E_SYSTEM] statfs() failed");
571                 return E_SYSTEM;
572         }
573
574         totalSize = static_cast< long long >(fs.f_bsize) * static_cast< long long >(fs.f_blocks);
575         availableSize = static_cast< long long >(fs.f_bsize) * static_cast< long long >(fs.f_bavail);
576
577         return E_SUCCESS;
578 }
579
580 result
581 _RuntimeInfoImpl::GetFromProcLonglong(const char* path, const char* key, long long& value)
582 {
583         FILE* pStream = null;
584         char line[_MAX_BUFFER_LENGTH] = {0, };
585         char fieldName[_MAX_BUFFER_LENGTH] = {0, };
586         long long keyValue = 0;
587         pStream = fopen(path, "r");
588         SysTryReturnResult(NID_SYS, pStream != null, E_SYSTEM, "%s open is failed.", path);
589
590         while (fgets(line, sizeof(line), pStream))
591         {
592                 if (sscanf(line, "%s %lld %*s", fieldName, &keyValue) == 2)
593                 {
594                         if (strncmp(fieldName, key, static_cast< int >(strlen(key))) == 0)
595                         {
596                                 value = keyValue;
597                                 fclose(pStream);
598                                 return E_SUCCESS;
599                         }
600                 }
601         }
602         fclose(pStream);
603
604         SysLogException(NID_SYS, E_OBJ_NOT_FOUND, "it can't find %s field", key);
605         return E_OBJ_NOT_FOUND;
606 }
607
608 result
609 _RuntimeInfoImpl::GetFromProc(const char* path, const char* key, int& value)
610 {
611         FILE* pStream = null;
612         char line[_MAX_BUFFER_LENGTH] = {0, };
613         char fieldName[_MAX_BUFFER_LENGTH] = {0, };
614         int keyValue = 0;
615         pStream = fopen(path, "r");
616         SysTryReturnResult(NID_SYS, pStream != null, E_SYSTEM, "%s open is failed.", path);
617
618         while (fgets(line, sizeof(line), pStream))
619         {
620                 if (sscanf(line, "%s %d %*s", fieldName, &keyValue) == 2)
621                 {
622                         if (strncmp(fieldName, key, static_cast< int >(strlen(key))) == 0)
623                         {
624                                 value = keyValue;
625                                 fclose(pStream);
626                                 return E_SUCCESS;
627                         }
628                 }
629         }
630         fclose(pStream);
631
632         SysLogException(NID_SYS, E_OBJ_NOT_FOUND, "it can't find %s field", key);
633         return E_OBJ_NOT_FOUND;
634 }
635
636 void*
637 _RuntimeInfoImpl::GetDirectorySizeAsync(void* data)
638 {       
639         if(data == null)
640         {               
641                 return null;
642         }
643         _RuntimeInfoEventArg* pEventArg = (_RuntimeInfoEventArg*)data;
644         long long size = _RuntimeInfoImpl::GetDirectorySize(pEventArg->DirectoryPath);
645
646         if(pEventArg == null)
647         {
648                 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _RuntimeInfoEventArg");
649                 return null;
650         }
651         pEventArg->Type = _RUNTIME_INFO_EVENT_TYPE_LONGLONG;
652         pEventArg->longLongValue = size;
653         SysLog(NID_SYS, "Fire event %x %s %lld", pEventArg, pEventArg->DirectoryPath, size);
654         runtimeInfoEvent.Fire(*pEventArg);
655         
656         return null;
657 }
658
659 void*
660 _RuntimeInfoImpl::GetCpuUsageAsync(void* data)
661 {
662         result r = E_SUCCESS;
663
664         int value = 0;
665         procstat_t first, second, diffrence;
666         unsigned long long total, usage;
667
668         if(read_proc_stat(&first) != false)
669         {
670                 usleep(_CPU_USAGE_EVALUATION_INTERVAL);
671                 if(read_proc_stat(&second) != false)
672                 {
673                         diff_proc_stat(&second, &first, &diffrence);
674                         total = diffrence.user + diffrence.system 
675                                 + diffrence.nice + diffrence.idle 
676                                 + diffrence.wait + diffrence.hi 
677                                 + diffrence.si;
678                         usage = 10000 - (diffrence.idle * 10000 / total);
679                         value = usage/100;
680                         r = E_SUCCESS;
681                 }
682                 else
683                 {
684                         r = E_SYSTEM;
685                 }
686         }
687         else
688         {
689                 r = E_SYSTEM;
690         }
691
692         _RuntimeInfoEventArg* pEventArg = (_RuntimeInfoEventArg*) data;
693         if(pEventArg == null)
694         {
695                 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _RuntimeInfoEventArg");
696                 return null;
697         }
698         pEventArg->errorCode = r;
699         pEventArg->Type = _RUNTIME_INFO_EVENT_TYPE_INT;
700         pEventArg->intValue = value;
701
702         runtimeInfoEvent.Fire(*pEventArg);
703
704         return null;
705 }
706
707 result
708 _RuntimeInfoImpl::GetValueAsync(const String& key, IRuntimeInfoGetIntAsyncResultListener* listener)
709 {
710         result r = E_SUCCESS;
711
712         SysTryReturnResult(NID_SYS, listener != null, E_INVALID_ARG, "listener is null");
713
714         if (key == _CPU_CORE_ALL_USAGE)
715         {
716                 if(listener != null)
717                 {
718                         runtimeInfoEvent.AddListener(*listener);
719                 }
720
721                 int thr_id = 0;
722                 pthread_t p_thread;
723                 _RuntimeInfoEventArg* pEventArg = new (std::nothrow) _RuntimeInfoEventArg();
724                 pEventArg->pListener = listener;
725                 thr_id = pthread_create(&p_thread, null, GetCpuUsageAsync, pEventArg);
726                 pthread_detach(p_thread);
727         }
728         else
729         {
730                 SysLogException(NID_SYS, E_INVALID_ARG, "Required key[%ls] is not valid", key.GetPointer());
731                 r = E_INVALID_ARG;
732         }
733         return r;
734 }
735
736 result
737 _RuntimeInfoImpl::GetValueAsync(const String& key, IRuntimeInfoGetLonglongAsyncResultListener* listener)
738 {
739         int thr_id = 0;
740         pthread_t p_thread;
741         result r = E_SUCCESS;
742         String directoryPath;
743
744         SysTryReturnResult(NID_SYS, listener != null, E_INVALID_ARG, "listener is null");
745
746         if(key == _STORAGE_ALLOCATED_INTERNAL_APPLICATION)
747         {
748                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_APPLICATIONS));
749         }       
750         else if (key == _STORAGE_ALLOCATED_INTERNAL_AUDIO)
751         {
752                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_SOUNDS));
753         }
754         else if (key == _STORAGE_ALLOCATED_INTERNAL_VIDEO)
755         {
756                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_VIDEOS));
757         }
758         else if (key == _STORAGE_ALLOCATED_INTERNAL_IMAGE)
759         {
760                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_IMAGES));
761         }
762         else if (key == _STORAGE_ALLOCATED_INTERNAL_DOWNLOAD)
763         {
764                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_DOWNLOADS));
765         }
766         else if (key == _STORAGE_ALLOCATED_EXTERNAL_APPLICATION)
767         {
768                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_APPLICATIONS));
769         }
770         else if (key == _STORAGE_ALLOCATED_EXTERNAL_AUDIO)
771         {
772                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_SOUNDS));
773         }
774         else if (key == _STORAGE_ALLOCATED_EXTERNAL_VIDEO)
775         {
776                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_VIDEOS));
777         }
778         else if (key == _STORAGE_ALLOCATED_EXTERNAL_IMAGE)
779         {
780                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_IMAGES));
781         }
782         else if (key == _STORAGE_ALLOCATED_EXTERNAL_DOWNLOAD)
783         {
784                 directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_DOWNLOADS));
785         }
786         else
787         {
788                 SysLogException(NID_SYS, E_INVALID_ARG, "Required key[%ls] is not valid", key.GetPointer());
789                 r = E_INVALID_ARG;
790         }
791
792         SysLog(NID_SYS, "%s", GetErrorMessage(r));
793         SysLog(NID_SYS, "%ls", directoryPath.GetPointer());
794         if(r == E_SUCCESS)
795         {
796 //              if(Tizen::Io::File::IsFileExist(L"/opt/usr/etc/common_service_for_runtime") == true)
797                 {
798                         SysLog(NID_SYS, "Runtime is serviced by common-service.");
799                         _RuntimeClientEx* pRuntimeClientEx = _RuntimeClientEx::GetInstance();
800                         pRuntimeClientEx->GetDirectorySizeValueAsync(directoryPath, listener);
801                 }
802 /*              else
803                 {
804                         SysLog(NID_SYS, "Runtime is serviced by app-service.");
805                         _RuntimeClient* pRuntimeClient = _RuntimeClient::GetInstance();
806                         pRuntimeClient->GetDirectorySizeValueAsync(directoryPath, listener);
807                 }
808 */
809         }
810
811         return r;
812 }
813
814 _RuntimeInfoImpl*
815 _RuntimeInfoImpl::GetInstance(RuntimeInfo& runtimeinfo)
816 {
817         return runtimeinfo.__pRuntimeInfoImpl;
818 }
819
820 const _RuntimeInfoImpl*
821 _RuntimeInfoImpl::GetInstance(const RuntimeInfo& runtimeinfo)
822 {
823         return runtimeinfo.__pRuntimeInfoImpl;
824 }
825
826 } } //Tizen::System
827