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