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