update the latest source
[platform/core/connectivity/smartcard-service.git] / server / ServerChannel.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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         }
34
35         ServerChannel::~ServerChannel()
36         {
37                 if (isClosed() == false)
38                 {
39                         closeSync();
40                 }
41         }
42
43         void ServerChannel::closeSync()
44         {
45                 ByteArray command, result;
46                 APDUHelper apdu;
47                 int rv;
48
49                 if (isBasicChannel() == false)
50                 {
51                         /* close channel */
52                         command = apdu.generateAPDU(APDUHelper::COMMAND_CLOSE_LOGICAL_CHANNEL, channelNum, ByteArray::EMPTY);
53                         rv = terminal->transmitSync(command, result);
54
55                         if (rv == 0 && result.getLength() >= 2)
56                         {
57                                 ResponseHelper resp(result);
58
59                                 if (resp.getStatus() == 0)
60                                 {
61                                         SCARD_DEBUG("close success");
62                                 }
63                                 else
64                                 {
65                                         SCARD_DEBUG_ERR("status word [%d][ 0x%02X 0x%02X ]", resp.getStatus(), result[result.getLength() - 2], result[result.getLength() - 1]);
66                                 }
67                         }
68                         else
69                         {
70                                 SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", rv, result.getLength());
71                         }
72                 }
73
74                 channelNum = -1;
75         }
76
77         int ServerChannel::getChannelNumber()
78         {
79                 return channelNum;
80         }
81
82         int ServerChannel::transmitSync(ByteArray command, ByteArray &result)
83         {
84                 APDUCommand helper;
85
86                 if (session != NULL) /* admin channel */
87                 {
88                         helper.setCommand(command);
89
90                         /* filter command */
91                         if ((helper.getINS() == APDUCommand::INS_SELECT_FILE && helper.getP1() == APDUCommand::P1_SELECT_BY_DF_NAME) ||
92                                 (helper.getINS() == APDUCommand::INS_MANAGE_CHANNEL))
93                         {
94                                 return -4; /* security reason */
95                         }
96
97                         /* insert channel ID */
98                         helper.setChannel(0, channelNum);
99
100                         helper.getBuffer(command);
101                 }
102
103                 SCARD_DEBUG("command [%d] : %s", command.getLength(), command.toString());
104
105                 return terminal->transmitSync(command, result);
106         }
107
108 } /* namespace smartcard_service_api */