Change log macro
[platform/core/connectivity/smartcard-service.git] / client / ClientChannel.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 /* standard library header */
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "Message.h"
27 #include "ClientIPC.h"
28 #include "ClientChannel.h"
29 #include "ReaderHelper.h"
30 #include "APDUHelper.h"
31
32 #ifndef EXTERN_API
33 #define EXTERN_API __attribute__((visibility("default")))
34 #endif
35
36 namespace smartcard_service_api
37 {
38         ClientChannel::ClientChannel(void *context, Session *session,
39                 int channelNum, ByteArray selectResponse, void *handle)
40                 : Channel(session)
41         {
42                 this->channelNum = -1;
43                 this->handle = NULL;
44                 this->context = NULL;
45
46                 if (handle == NULL)
47                 {
48                         _ERR("ClientIPC::getInstance() failed");
49
50                         return;
51                 }
52
53                 this->channelNum = channelNum;
54                 this->handle = handle;
55                 this->selectResponse = selectResponse;
56                 this->context = context;
57         }
58
59         ClientChannel::~ClientChannel()
60         {
61                 closeSync();
62         }
63
64         void ClientChannel::closeSync()
65                 throw(ExceptionBase &, ErrorIO &, ErrorSecurity &,
66                         ErrorIllegalState &, ErrorIllegalParameter &)
67         {
68 #ifdef CLIENT_IPC_THREAD
69                 if (isClosed() == false)
70                 {
71                         if (getSession()->getReader()->isSecureElementPresent() == true)
72                         {
73                                 Message msg;
74                                 int rv;
75
76                                 /* send message to server */
77                                 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
78                                 msg.param1 = (unsigned long)handle;
79                                 msg.error = (unsigned long)context; /* using error to context */
80                                 msg.caller = (void *)this;
81                                 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
82
83                                 syncLock();
84                                 if (ClientIPC::getInstance().sendMessage(&msg) == true)
85                                 {
86                                         rv = waitTimedCondition(0);
87                                         if (rv < 0)
88                                         {
89                                                 _ERR("closeSync failed [%d]", rv);
90                                                 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
91                                         }
92                                 }
93                                 else
94                                 {
95                                         _ERR("sendMessage failed");
96                                         this->error = SCARD_ERROR_IPC_FAILED;
97                                 }
98                                 syncUnlock();
99
100                                 channelNum = -1;
101
102                                 if (this->error != SCARD_ERROR_OK)
103                                 {
104                                         ThrowError::throwError(this->error);
105                                 }
106                         }
107                         else
108                         {
109                                 /* FIXME */
110                                 _DBG("unavailable channel");
111                         }
112                 }
113 #endif
114         }
115
116         int ClientChannel::close(closeCallback callback, void *userParam)
117         {
118                 int result = SCARD_ERROR_OK;
119
120                 if (isClosed() == false)
121                 {
122                         if (getSession()->getReader()->isSecureElementPresent() == true)
123                         {
124                                 Message msg;
125                                 channelNum = -1;
126
127                                 /* send message to server */
128                                 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
129                                 msg.param1 = (unsigned long)handle;
130                                 msg.error = (unsigned long)context; /* using error to context */
131                                 msg.caller = (void *)this;
132                                 msg.callback = (void *)callback;
133                                 msg.userParam = userParam;
134
135                                 if (ClientIPC::getInstance().sendMessage(&msg) == false)
136                                 {
137                                         result = SCARD_ERROR_IPC_FAILED;
138                                 }
139                         }
140                         else
141                         {
142                                 _ERR("unavailable channel");
143                                 result = SCARD_ERROR_ILLEGAL_STATE;
144                         }
145                 }
146
147                 return result;
148         }
149
150         int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
151                 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
152                         ErrorIllegalParameter &, ErrorSecurity &)
153         {
154                 int rv = SCARD_ERROR_OK;
155                 if (getSession()->getReader()->isSecureElementPresent() == true)
156                 {
157                         Message msg;
158
159 #ifdef CLIENT_IPC_THREAD
160                         /* send message to server */
161                         msg.message = Message::MSG_REQUEST_TRANSMIT;
162                         msg.param1 = (unsigned long)handle;
163                         msg.param2 = 0;
164                         msg.data = command;
165                         msg.error = (unsigned long)context; /* using error to context */
166                         msg.caller = (void *)this;
167                         msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
168
169                         syncLock();
170                         if (ClientIPC::getInstance().sendMessage(&msg) == true)
171                         {
172                                 rv = waitTimedCondition(0);
173                                 if (rv >= 0)
174                                 {
175                                         result = response;
176
177                                         rv = SCARD_ERROR_OK;
178                                 }
179                                 else
180                                 {
181                                         _ERR("timeout");
182
183                                         this->error = SCARD_ERROR_OPERATION_TIMEOUT;
184                                 }
185                         }
186                         else
187                         {
188                                 _ERR("sendMessage failed");
189                         }
190                         syncUnlock();
191
192                         if (this->error != SCARD_ERROR_OK)
193                         {
194                                 ThrowError::throwError(this->error);
195                         }
196 #endif
197                 }
198                 else
199                 {
200                         _ERR("unavailable channel");
201                         throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
202                 }
203
204                 return rv;
205         }
206
207         int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
208         {
209                 int result;
210
211                 if (getSession()->getReader()->isSecureElementPresent() == true)
212                 {
213                         Message msg;
214
215                         /* send message to server */
216                         msg.message = Message::MSG_REQUEST_TRANSMIT;
217                         msg.param1 = (unsigned long)handle;
218                         msg.param2 = 0;
219                         msg.data = command;
220                         msg.error = (unsigned long)context; /* using error to context */
221                         msg.caller = (void *)this;
222                         msg.callback = (void *)callback;
223                         msg.userParam = userParam;
224
225                         if (ClientIPC::getInstance().sendMessage(&msg) == true)
226                         {
227                                 result = SCARD_ERROR_OK;
228                         }
229                         else
230                         {
231                                 result = SCARD_ERROR_IPC_FAILED;
232                         }
233                 }
234                 else
235                 {
236                         _ERR("unavailable channel");
237                         result = SCARD_ERROR_ILLEGAL_STATE;
238                 }
239
240                 return result;
241         }
242
243         bool ClientChannel::dispatcherCallback(void *message)
244         {
245                 Message *msg = (Message *)message;
246                 ClientChannel *channel = NULL;
247                 bool result = false;
248
249                 if (msg == NULL)
250                 {
251                         _ERR("message is null");
252                         return result;
253                 }
254
255                 channel = (ClientChannel *)msg->caller;
256
257                 switch (msg->message)
258                 {
259                 case Message::MSG_REQUEST_TRANSMIT :
260                         {
261                                 /* transmit result */
262                                 _DBG("MSG_REQUEST_TRANSMIT");
263
264                                 if (msg->error == 0 &&
265                                         ResponseHelper::getStatus(msg->data) == 0)
266                                 {
267                                         /* store select response */
268                                         if (msg->data.getAt(1) == APDUCommand::INS_SELECT_FILE)
269                                                 channel->setSelectResponse(msg->data);
270                                 }
271
272                                 if (msg->isSynchronousCall() == true) /* synchronized call */
273                                 {
274                                         /* sync call */
275                                         channel->syncLock();
276
277                                         /* copy result */
278                                         channel->error = msg->error;
279                                         channel->response = msg->data;
280
281                                         channel->signalCondition();
282                                         channel->syncUnlock();
283                                 }
284                                 else if (msg->callback != NULL)
285                                 {
286                                         transmitCallback cb = (transmitCallback)msg->callback;
287
288                                         /* async call */
289                                         cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
290                                 }
291                         }
292                         break;
293
294                 case Message::MSG_REQUEST_CLOSE_CHANNEL :
295                         {
296                                 _DBG("MSG_REQUEST_CLOSE_CHANNEL");
297
298                                 if (msg->isSynchronousCall() == true) /* synchronized call */
299                                 {
300                                         /* sync call */
301                                         channel->syncLock();
302
303                                         channel->error = msg->error;
304
305                                         channel->signalCondition();
306                                         channel->syncUnlock();
307                                 }
308                                 else if (msg->callback != NULL)
309                                 {
310                                         closeCallback cb = (closeCallback)msg->callback;
311
312                                         /* async call */
313                                         cb(msg->error, msg->userParam);
314                                 }
315                         }
316                         break;
317
318                 default:
319                         _DBG("Unknown message : %s", msg->toString());
320                         break;
321                 }
322
323                 return result;
324         }
325 } /* namespace smartcard_service_api */
326
327 /* export C API */
328 #define CHANNEL_EXTERN_BEGIN \
329         if (handle != NULL) \
330         { \
331                 ClientChannel *channel = (ClientChannel *)handle;
332
333 #define CHANNEL_EXTERN_END \
334         } \
335         else \
336         { \
337                 _ERR("Invalid param"); \
338         }
339
340 using namespace smartcard_service_api;
341
342 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
343 {
344         int result = -1;
345
346         CHANNEL_EXTERN_BEGIN;
347         result = channel->close((closeCallback)callback, userParam);
348         CHANNEL_EXTERN_END;
349
350         return result;
351 }
352
353 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
354         unsigned int length, channel_transmit_cb callback, void *userParam)
355 {
356         int result = -1;
357
358         CHANNEL_EXTERN_BEGIN;
359         ByteArray temp;
360
361         temp.setBuffer(command, length);
362         result = channel->transmit(temp, (transmitCallback)callback, userParam);
363         CHANNEL_EXTERN_END;
364
365         return result;
366 }
367
368 EXTERN_API void channel_close_sync(channel_h handle)
369 {
370 #ifdef CLIENT_IPC_THREAD
371         CHANNEL_EXTERN_BEGIN;
372         try
373         {
374                 channel->closeSync();
375         }
376         catch (...)
377         {
378         }
379         CHANNEL_EXTERN_END;
380 #endif
381 }
382
383 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
384         unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
385 {
386         int result = -1;
387
388 #ifdef CLIENT_IPC_THREAD
389         if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
390                 return result;
391
392         CHANNEL_EXTERN_BEGIN;
393         ByteArray temp, resp;
394
395         temp.setBuffer(command, cmd_len);
396
397         try
398         {
399                 result = channel->transmitSync(temp, resp);
400                 if (resp.getLength() > 0)
401                 {
402                         *resp_len = resp.getLength();
403                         *response = (unsigned char *)calloc(1, *resp_len);
404                         memcpy(*response, resp.getBuffer(), *resp_len);
405                 }
406         }
407         catch (...)
408         {
409                 result = -1;
410         }
411         CHANNEL_EXTERN_END;
412 #endif
413
414         return result;
415 }
416
417 EXTERN_API bool channel_is_basic_channel(channel_h handle)
418 {
419         bool result = false;
420
421         CHANNEL_EXTERN_BEGIN;
422         result = channel->isBasicChannel();
423         CHANNEL_EXTERN_END;
424
425         return result;
426 }
427
428 EXTERN_API bool channel_is_closed(channel_h handle)
429 {
430         bool result = false;
431
432         CHANNEL_EXTERN_BEGIN;
433         result = channel->isClosed();
434         CHANNEL_EXTERN_END;
435
436         return result;
437 }
438
439 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
440 {
441         unsigned int result = 0;
442
443         CHANNEL_EXTERN_BEGIN;
444         result = channel->getSelectResponse().getLength();
445         CHANNEL_EXTERN_END;
446
447         return result;
448 }
449
450 EXTERN_API bool channel_get_select_response(channel_h handle,
451         unsigned char *buffer, unsigned int length)
452 {
453         bool result = false;
454
455         if (buffer == NULL || length == 0)
456         {
457                 return result;
458         }
459
460         CHANNEL_EXTERN_BEGIN;
461         ByteArray response;
462
463         response = channel->getSelectResponse();
464         if (response.getLength() > 0)
465         {
466                 memcpy(buffer, response.getBuffer(), MIN(length, response.getLength()));
467                 result = true;
468         }
469         CHANNEL_EXTERN_END;
470
471         return result;
472 }
473
474 EXTERN_API session_h channel_get_session(channel_h handle)
475 {
476         session_h session = NULL;
477
478         CHANNEL_EXTERN_BEGIN;
479         session = channel->getSession();
480         CHANNEL_EXTERN_END;
481
482         return session;
483 }
484
485 EXTERN_API void channel_destroy_instance(channel_h handle)
486 {
487         /* do nothing */
488 }