Store select response in channel instance
[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,
29                 int channelNum, Terminal *terminal)
30                 : Channel(session)
31         {
32                 this->terminal = terminal;
33                 this->caller = caller;
34                 this->channelNum = channelNum;
35                 this->privilege = true;
36         }
37
38         ServerChannel::~ServerChannel()
39         {
40                 if (isClosed() == false)
41                 {
42                         closeSync();
43                 }
44         }
45
46         void ServerChannel::closeSync()
47                 throw(ErrorIO &, ErrorIllegalState &)
48         {
49                 ByteArray command, result;
50                 APDUHelper apdu;
51                 int rv;
52
53                 if (isBasicChannel() == false)
54                 {
55                         /* close channel */
56                         command = apdu.generateAPDU(APDUHelper::COMMAND_CLOSE_LOGICAL_CHANNEL, channelNum, ByteArray::EMPTY);
57                         rv = terminal->transmitSync(command, result);
58
59                         if (rv == 0 && result.getLength() >= 2)
60                         {
61                                 ResponseHelper resp(result);
62
63                                 if (resp.getStatus() == 0)
64                                 {
65                                         SCARD_DEBUG("close success");
66                                 }
67                                 else
68                                 {
69                                         SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
70                                 }
71                         }
72                         else
73                         {
74                                 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", rv, result.getLength());
75                         }
76                 }
77
78                 channelNum = -1;
79         }
80
81         int ServerChannel::transmitSync(ByteArray command, ByteArray &result)
82                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
83         {
84                 int ret = -1;
85                 APDUCommand helper;
86
87                 if (isClosed() == true)
88                 {
89                         return ret;
90                 }
91
92                 helper.setCommand(command);
93
94                 /* filter command */
95                 if (privilege == false)
96                 {
97                         if ((helper.getINS() == APDUCommand::INS_SELECT_FILE &&
98                                 helper.getP1() == APDUCommand::P1_SELECT_BY_DF_NAME) ||
99                                 (helper.getINS() == APDUCommand::INS_MANAGE_CHANNEL))
100                         {
101                                 return -4; /* security reason */
102                         }
103                 }
104
105                 /* TODO : insert channel ID using atr information */
106                 helper.setChannel(APDUCommand::CLA_CHANNEL_STANDARD, channelNum);
107
108                 helper.getBuffer(command);
109
110                 SCARD_DEBUG("command [%d] : %s", command.getLength(), command.toString());
111
112                 ret = terminal->transmitSync(command, result);
113                 if (ret == 0 && ResponseHelper::getStatus(result) == 0)
114                 {
115                         /* store select response */
116                         if (helper.getINS() == APDUCommand::INS_SELECT_FILE)
117                                 setSelectResponse(result);
118                 }
119
120                 return ret;
121         }
122
123 } /* namespace smartcard_service_api */