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