Merge "Flow control for DataControl" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_SystemClient.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                FSys_SettingClient.cpp
20  * @brief               This is the implementation file for _SettingClient class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include "FSys_SystemClient.h"
26
27 #include <FBaseSysLog.h>
28
29 #include <FIo_AppServiceIpcMessages.h>
30 #include "FSys_CommunicationDispatcherClient.h"
31
32 using namespace std;
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Io;
38
39 namespace Tizen { namespace System
40 {
41
42 static const wchar_t* _SYSTEM_SERVICE_ID = L"osp.system.service";
43 static const wchar_t* _SYSTEM_COMMAND_SUPPORTED = L"osp.system.command.system.get.bool";
44
45 static const wchar_t* _SYSTEM_OK = L"osp.system.result.ok";
46 static const wchar_t* _SYSTEM_INVALID_ARG = L"osp.system.result.invalid_arg";
47 static const wchar_t* _SYSTEM_ERROR = L"osp.system.result.error";
48
49 _SystemClient* _SystemClient::__pSystemClient = null;
50
51 _SystemClient*
52 _SystemClient::GetInstance(void)
53 {
54         if(__pSystemClient == null)
55         {
56                 __pSystemClient = new (std::nothrow) _SystemClient();
57         }
58         return __pSystemClient;
59 }
60
61 _SystemClient::_SystemClient()
62         :__pIpcClient(null)
63 {
64         result r = E_SUCCESS;
65         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
66         r = pCommunicationDispatcherClient->RegisterCommunicationListener(_SYSTEM_SERVICE_ID, *this);
67         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
68 }
69
70 _SystemClient::~_SystemClient()
71 {
72 }
73
74 result
75 _SystemClient::ConvertResultCode(String code)
76 {
77         result r = E_SYSTEM;
78         if(code == _SYSTEM_OK)
79         {
80                 r = E_SUCCESS;
81         }
82         else if(code == _SYSTEM_INVALID_ARG)
83         {
84                 r = E_INVALID_ARG;
85         }
86         return r;
87 }
88
89 result
90 _SystemClient::GetValue(const Tizen::Base::String& key, bool& value)
91 {
92         result r = E_SUCCESS;
93         String requestKey(key);
94         String serviceId(_SYSTEM_SERVICE_ID);
95         String commandId(_SYSTEM_COMMAND_SUPPORTED);
96
97         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
98
99         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
100
101         ArrayList requestMessages;
102         ArrayList responseMessages;
103
104         requestMessages.Construct();
105         responseMessages.Construct();
106
107         requestMessages.Add(serviceId);
108         requestMessages.Add(commandId);
109         requestMessages.Add(requestKey);
110
111         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
112
113         r = __pIpcClient->SendRequest(*pMsg);
114         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
115
116         String* pResult = (String*)responseMessages.GetAt(2);
117         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
118
119         r = ConvertResultCode(*pResult);
120         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "Error is occured.");
121
122         String* pValue = (String*)responseMessages.GetAt(3);
123         SysTryReturnResult(NID_SYS, pValue != null, E_SYSTEM, "There is no result value.");
124         if(pValue->GetPointer() == L"1")
125         {
126                 value = true;
127         }
128         else
129         {
130                 value = false;
131         }
132
133         responseMessages.RemoveAll(true);
134         return r;
135 }
136
137 void
138 _SystemClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
139 {
140 }
141
142 } } // Tizen::System