Fix problems
[platform/core/connectivity/smartcard-service.git] / client / ClientIPC.cpp
1 /*
2  * Copyright (c) 2012, 2013 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 #ifndef USE_GDBUS
18 /* standard library header */
19 #include <sys/socket.h>
20 #include <unistd.h>
21
22 /* SLP library header */
23 #ifdef SECURITY_SERVER
24 #include "security-server.h"
25 #endif
26
27 /* local header */
28 #include "Debug.h"
29 #include "ClientIPC.h"
30 #include "DispatcherMsg.h"
31
32 namespace smartcard_service_api
33 {
34         ClientIPC::ClientIPC() : IPCHelper()
35         {
36 #ifdef USE_AUTOSTART
37                 _launch_daemon();
38 #endif
39 #ifdef SECURITY_SERVER
40                 int length;
41
42                 if ((length = security_server_get_cookie_size()) > 0)
43                 {
44                         uint8_t *buffer = NULL;
45
46                         buffer = new uint8_t[length];
47                         if (buffer != NULL)
48                         {
49                                 int error;
50
51                                 if ((error = security_server_request_cookie(buffer, length))
52                                         == SECURITY_SERVER_API_SUCCESS)
53                                 {
54                                         cookie.assign(buffer, length);
55
56                                         _DBG("cookie : %s", cookie.toString().c_str());
57                                 }
58                                 else
59                                 {
60                                         _ERR("security_server_request_cookie failed [%d]", error);
61                                 }
62
63                                 delete []buffer;
64                         }
65                 }
66                 else
67                 {
68                         _ERR("security_server_get_cookie_size failed");
69                 }
70 #endif
71         }
72
73         ClientIPC::~ClientIPC()
74         {
75         }
76
77         ClientIPC &ClientIPC::getInstance()
78         {
79                 static ClientIPC clientIPC;
80
81                 return clientIPC;
82         }
83
84 #ifdef USE_AUTOSTART
85         void ClientIPC::_launch_daemon()
86         {
87                 DBusGConnection *connection;
88                 GError *error = NULL;
89
90                 _BEGIN();
91
92                 dbus_g_thread_init();
93
94                 g_type_init();
95
96                 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
97                 if (error == NULL)
98                 {
99                         DBusGProxy *proxy;
100
101                         proxy = dbus_g_proxy_new_for_name(connection, "org.tizen.smartcard_service",
102                                 "/org/tizen/smartcard_service", "org.tizen.smartcard_service");
103                         if (proxy != NULL)
104                         {
105                                 gint result = 0;
106
107                                 if (dbus_g_proxy_call(proxy, "launch", &error, G_TYPE_INVALID,
108                                         G_TYPE_INT, &result, G_TYPE_INVALID) == false)
109                                 {
110                                         _ERR("org_tizen_smartcard_service_launch failed");
111                                         if (error != NULL)
112                                         {
113                                                 _ERR("message : [%s]", error->message);
114                                                 g_error_free(error);
115                                         }
116                                 }
117                         }
118                         else
119                         {
120                                 _ERR("ERROR: Can't make dbus proxy");
121                         }
122                 }
123                 else
124                 {
125                         _ERR("ERROR: Can't get on system bus [%s]", error->message);
126                         g_error_free(error);
127                 }
128
129                 _END();
130         }
131 #endif
132
133         bool ClientIPC::sendMessage(const Message &msg)
134         {
135                 ByteArray stream;
136                 unsigned int length;
137
138                 if (ipcSocket == -1)
139                         return false;
140
141 #ifdef SECURITY_SERVER
142                 stream = cookie + msg.serialize();
143 #else
144                 stream = msg.serialize();
145 #endif
146                 length = stream.size();
147
148                 _DBG(">>>[SEND]>>> socket [%d], msg [%d], length [%d]",
149                         ipcSocket, msg.message, stream.size());
150
151                 return IPCHelper::sendMessage(ipcSocket, stream);
152         }
153
154         int ClientIPC::handleIOErrorCondition(void *channel, GIOCondition condition)
155         {
156                 _BEGIN();
157
158                 if (dispatcher != NULL)
159                 {
160                         DispatcherMsg dispMsg;
161
162                         /* push or process disconnect message */
163                         dispMsg.message = Message::MSG_OPERATION_RELEASE_CLIENT;
164                         dispMsg.error = -1;
165
166 #ifdef CLIENT_IPC_THREAD
167                         dispatcher->processMessage(&dispMsg);
168 #else
169                         dispatcher->pushMessage(&dispMsg);
170 #endif
171                 }
172
173                 _END();
174
175                 return FALSE;
176         }
177
178         int ClientIPC::handleInvalidSocketCondition(void *channel, GIOCondition condition)
179         {
180                 _BEGIN();
181
182                 /* finalize context */
183                 destroyConnectSocket();
184
185                 _END();
186
187                 return FALSE;
188         }
189
190         int ClientIPC::handleIncomingCondition(void *channel, GIOCondition condition)
191         {
192                 int result = FALSE;
193
194                 _BEGIN();
195
196 #ifndef CLIENT_IPC_THREAD
197                 if (channel == ioChannel)
198                 {
199 #endif
200                         Message *msg = NULL;
201
202                         _DBG("message from server to client socket");
203
204                         /* read message */
205                         msg = retrieveMessage();
206                         if (msg != NULL)
207                         {
208                                 DispatcherMsg dispMsg(*msg);
209
210                                 /* set peer socket */
211                                 dispMsg.setPeerSocket(ipcSocket);
212
213                                 /* push to dispatcher */
214                                 if (dispatcher != NULL)
215                                 {
216 #ifdef CLIENT_IPC_THREAD
217                                         dispatcher->processMessage(&dispMsg);
218 #else
219                                         dispatcher->pushMessage(&dispMsg);
220 #endif
221                                 }
222
223                                 delete msg;
224                         }
225                         else
226                         {
227                                 /* clear client connection */
228 #ifdef CLIENT_IPC_THREAD
229                                 handleIOErrorCondition(channel, condition);
230                                 result = -1;
231 #endif
232                         }
233
234 #ifndef CLIENT_IPC_THREAD
235                 }
236                 else
237                 {
238                         _ERR("Unknown channel event [%p]", channel);
239                 }
240 #endif
241
242                 _END();
243
244                 return result;
245         }
246 } /* namespace open_mobile_api */
247 #endif /* USE_GDBUS */