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