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