Tizen 2.1 base
[platform/core/connectivity/smartcard-service.git] / server / ServerChannel.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
19 /* SLP library header */
20
21 /* local header */
22 #include "Debug.h"
23 #include "ServerChannel.h"
24 #include "APDUHelper.h"
25
26 namespace smartcard_service_api
27 {
28         ServerChannel::ServerChannel(ServerSession *session, void *caller, int channelNum, Terminal *terminal):Channel(session)
29         {
30                 this->terminal = terminal;
31                 this->caller = caller;
32                 this->channelNum = channelNum;
33                 this->privilege = true;
34         }
35
36         ServerChannel::~ServerChannel()
37         {
38                 if (isClosed() == false)
39                 {
40                         closeSync();
41                 }
42         }
43
44         void ServerChannel::closeSync()
45         {
46                 ByteArray command, result;
47                 APDUHelper apdu;
48                 int rv;
49
50                 if (isBasicChannel() == false)
51                 {
52                         /* close channel */
53                         command = apdu.generateAPDU(APDUHelper::COMMAND_CLOSE_LOGICAL_CHANNEL, channelNum, ByteArray::EMPTY);
54                         rv = terminal->transmitSync(command, result);
55
56                         if (rv == 0 && result.getLength() >= 2)
57                         {
58                                 ResponseHelper resp(result);
59
60                                 if (resp.getStatus() == 0)
61                                 {
62                                         SCARD_DEBUG("close success");
63                                 }
64                                 else
65                                 {
66                                         SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
67                                 }
68                         }
69                         else
70                         {
71                                 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", rv, result.getLength());
72                         }
73                 }
74
75                 channelNum = -1;
76         }
77
78         int ServerChannel::transmitSync(ByteArray command, ByteArray &result)
79         {
80                 APDUCommand helper;
81
82                 if (isClosed() == true)
83                 {
84                         return -1;
85                 }
86
87                 helper.setCommand(command);
88
89                 /* filter command */
90                 if (privilege == false)
91                 {
92                         if ((helper.getINS() == APDUCommand::INS_SELECT_FILE && helper.getP1() == APDUCommand::P1_SELECT_BY_DF_NAME) ||
93                                 (helper.getINS() == APDUCommand::INS_MANAGE_CHANNEL))
94                         {
95                                 return -4; /* security reason */
96                         }
97                 }
98
99                 /* TODO : insert channel ID using atr information */
100                 helper.setChannel(APDUCommand::CLA_CHANNEL_STANDARD, channelNum);
101
102                 helper.getBuffer(command);
103
104                 SCARD_DEBUG("command [%d] : %s", command.getLength(), command.toString());
105
106                 return terminal->transmitSync(command, result);
107         }
108
109 } /* namespace smartcard_service_api */