Remove gcov rpm package
[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         /* LCOV_EXCL_START */
29         ServerChannel::ServerChannel(ServerSession *session, void *caller,
30                 int channelNum, Terminal *terminal) :
31                 Channel(session), terminal(terminal), caller(caller),
32                 privilege(true)
33         {
34                 this->channelNum = channelNum;
35         }
36
37         ServerChannel::~ServerChannel()
38         {
39                 if (isClosed() == false)
40                 {
41                         closeSync();
42                 }
43         }
44
45         void ServerChannel::closeSync()
46         {
47                 ByteArray command, result;
48                 APDUHelper apdu;
49                 int rv;
50
51                 if (isClosed() == false && isBasicChannel() == false) {
52                         /* close channel */
53                         command = apdu.generateAPDU(APDUHelper::COMMAND_CLOSE_LOGICAL_CHANNEL, channelNum, ByteArray::EMPTY);
54
55                         _DBG("command [%zu] : %s", command.size(), command.toString().c_str());
56
57                         rv = terminal->transmitSync(command, result);
58
59                         if (rv == 0 && result.size() >= 2) {
60                                 ResponseHelper resp(result);
61
62                                 if (resp.getStatus() >= 0) {
63                                         _DBG("close success");
64                                 } else {
65                                         _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
66                                 }
67                         } else {
68                                 _ERR("select apdu is failed, rv [%d], length [%zu]", rv, result.size());
69                         }
70
71                         channelNum = -1;
72                 }
73         }
74
75         int ServerChannel::transmitSync(const ByteArray &command, ByteArray &result)
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 [%zu] : %s", cmd.size(), cmd.toString().c_str());
104
105                 ret = terminal->transmitSync(cmd, result);
106
107                 return ret;
108         }
109
110         /* LCOV_EXCL_STOP */
111 } /* namespace smartcard_service_api */