Update code of system and text for reporting Klocwork.
[platform/framework/native/appfw.git] / src / system / FSys_RuntimeClient.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FSys_RuntimeClient.cpp
19  * @brief               This is the implementation file for _RuntimeClient class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FBaseInteger.h>
25 #include <FBaseLongLong.h>
26 #include <FBaseSysLog.h>
27
28 #include <FApp_AppInfo.h>
29
30 #include <FIo_AppServiceIpcMessages.h>
31 #include "FSys_SystemServiceMessageClient.h"
32 #include "FSys_RuntimeClient.h"
33
34 using namespace std;
35
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Io;
41
42 namespace Tizen { namespace System
43 {
44
45 static const wchar_t* _RUNTIME_SERVICE_ID = L"osp.sys.ipcserver.runtimeinfo";
46 static const wchar_t* _RUNTIME_GET_SIZE = L"osp.system.command.runtime.get.size";
47 static const wchar_t* _RUNTIME_RESULT_SUCCESS = L"osp.system.result.success";
48
49 _RuntimeClient* _RuntimeClient::__pRuntimeClient= null;
50
51 class _RuntimeAsyncEventArg : public IEventArg
52 {
53 public:
54         long long value;
55         result rCode;
56 };
57
58 class _RuntimeAsyncEvent : public Event
59 {
60 protected:
61         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
62         {
63                 IRuntimeInfoGetLonglongAsyncResultListener* pListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*> (&listener);
64                 const _RuntimeAsyncEventArg* pArg = dynamic_cast<const _RuntimeAsyncEventArg*>(&arg);
65
66                 if(pListener == null || pArg == null)
67                 {
68                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listener or arguemnt");
69                         return;
70                 }
71                 pListener->OnResultReceivedForGetValueAsync(pArg->value, pArg->rCode);
72                 RemoveListener(listener);
73         }
74 };
75
76 _RuntimeClient*
77 _RuntimeClient::GetInstance(void)
78 {
79         if(__pRuntimeClient == null)
80         {
81                 __pRuntimeClient = new (std::nothrow) _RuntimeClient();
82         }
83         return __pRuntimeClient;
84 }
85
86 _RuntimeClient::_RuntimeClient()
87         : __msgCount(0)
88         , __pMessageClient(null)
89 {
90         result r = E_SUCCESS;
91         static String RUNTIME_SERVICE_ID(_RUNTIME_SERVICE_ID);
92
93         __pMessageClient = _SystemServiceMessageClient::CreateInstance(RUNTIME_SERVICE_ID);
94         SysTryCatch(NID_SYS, __pMessageClient != null, r = E_SYSTEM, r, "It is failed to create SystemServiceMessageClient.");
95
96         r = __pMessageClient->RegisterListener(RUNTIME_SERVICE_ID, *this);
97         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on MessageClient.");
98
99         __asyncEventList.Construct();
100 CATCH:
101         SetLastResult(r);
102 }
103
104 _RuntimeClient::~_RuntimeClient()
105 {
106         result r = E_SUCCESS;
107         String key(_RUNTIME_SERVICE_ID);
108         r = __pMessageClient->UnregisterListener(key);
109         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on MessageClient.");
110
111         __asyncEventList.RemoveAll(true);
112         delete __pMessageClient;
113 CATCH:
114         SetLastResult(r);
115 }
116
117 result
118 _RuntimeClient::GetDirectorySizeValueAsync(const String& path, IRuntimeInfoGetLonglongAsyncResultListener* listener)
119 {
120         result r = E_SUCCESS;
121
122         ArrayList requestMessages;
123         ArrayList responseMessages;
124
125         requestMessages.Construct();
126         responseMessages.Construct();
127
128         String serviceId(_RUNTIME_SERVICE_ID);
129         String commandId(_RUNTIME_GET_SIZE);
130         String messageId;
131         messageId.Append(__msgCount);
132
133         requestMessages.Add(serviceId);
134         requestMessages.Add(commandId);
135         requestMessages.Add(path);
136         requestMessages.Add(messageId);
137
138         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
139         r = __pMessageClient->GetIpcClient()->SendRequest(*pMsg);
140         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
141
142         String* pResult = (String*)responseMessages.GetAt(2);
143         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
144
145         if(*pResult == _RUNTIME_RESULT_SUCCESS)
146                 r = E_SUCCESS;
147         else
148                 r = E_SYSTEM;
149
150         SysLog(NID_SYS, "r is %s.", GetErrorMessage(r));
151
152         if(r == E_SUCCESS)
153         {
154                 _RuntimeAsyncEvent* pEvent = new (std::nothrow) _RuntimeAsyncEvent();
155                 pEvent->AddListener(*listener);
156
157                 __asyncEventList.Add(new Integer(__msgCount), pEvent);
158
159                 __msgCount++;
160         }
161         return r;
162 }
163
164 void
165 _RuntimeClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
166 {
167         SysLog(NID_SYS, "Receive result");
168         String* pServiceId = (String*)(data.GetAt(0));
169         String* pResponseId = (String*)(data.GetAt(1));
170         String* pMessageId = (String*)(data.GetAt(2));
171         String* pValue = (String*)(data.GetAt(3));
172         String* pResultCode = (String*)(data.GetAt(4));
173
174         SysTryReturnVoidResult(NID_SYS, pServiceId != null && pResponseId != null && pMessageId != null && pValue != null && pResultCode != null, E_SYSTEM, "There is no result data.");
175
176         int msg_id = 0;
177         Integer::Parse(*pMessageId, msg_id);
178
179         Integer msgKey(msg_id);
180
181         _RuntimeAsyncEvent* pEvent = dynamic_cast <_RuntimeAsyncEvent*> (__asyncEventList.GetValue(msgKey));
182         SysTryReturnVoidResult(NID_SYS, pEvent != null, E_SYSTEM, "There is no registered event.");
183
184         _RuntimeAsyncEventArg* pEventArg = new (std::nothrow) _RuntimeAsyncEventArg();
185
186         LongLong::Parse(*pValue, pEventArg->value);
187
188         if(*pResultCode == _RUNTIME_RESULT_SUCCESS)
189         {
190                 pEventArg->rCode = E_SUCCESS;
191         }
192         else
193         {
194                 pEventArg->rCode = E_SYSTEM;
195         }
196
197         pEvent->Fire(*pEventArg);
198 }
199
200 } } // Tizen::System
201