Call open/close functions of terminal instance before openning channel
[platform/core/connectivity/smartcard-service.git] / server / ServerSession.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 #include <stdio.h>
19 #include <dlfcn.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "ServerSession.h"
26 #include "ServerReader.h"
27 #include "ServerChannel.h"
28 #include "APDUHelper.h"
29 #include "GPACE.h"
30
31 namespace smartcard_service_api
32 {
33         ServerSession::ServerSession(ServerReader *reader,
34                 const vector<ByteArray> &certHashes,
35                 void *caller, Terminal *terminal) : SessionHelper(reader)
36         {
37                 this->terminal = NULL;
38
39                 if (terminal == NULL)
40                 {
41                         _ERR("invalid param");
42
43                         return;
44                 }
45
46                 this->terminal = terminal;
47                 this->certHashes = certHashes;
48         }
49
50         ServerSession::~ServerSession()
51         {
52                 if (isClosed() == false)
53                         closeSync();
54         }
55
56         const ByteArray ServerSession::getATRSync()
57                 throw(ErrorIO &, ErrorIllegalState &)
58         {
59                 /* call get atr to terminal */
60                 if (atr.isEmpty()) {
61                         if (terminal != NULL) {
62                                 if (terminal->open() == true) {
63                                         int error = terminal->getATRSync(atr);
64
65                                         if (error < SCARD_ERROR_OK) {
66                                                 _ERR("getATRSync failed, [%d]", error);
67                                         }
68
69                                         terminal->close();
70                                 } else {
71                                         _ERR("terminal->open failed");
72                                 }
73                         } else {
74                                 _ERR("terminal is null.");
75                         }
76                 }
77
78                 return atr;
79         }
80
81         void ServerSession::closeSync()
82                 throw(ErrorIO &, ErrorIllegalState &)
83         {
84                 if (isClosed() == false)
85                 {
86                         closed = true;
87                         closeChannels();
88                 }
89         }
90
91         void ServerSession::closeChannels()
92                 throw(ErrorIO &, ErrorIllegalState &)
93         {
94                 size_t i;
95
96                 for (i = 0; i < channels.size(); i++)
97                 {
98                         if (channels[i] != NULL)
99                                 channels[i]->closeSync();
100                 }
101
102                 channels.clear();
103         }
104
105         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid)
106                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
107         {
108                 return openBasicChannelSync(aid, NULL);
109         }
110
111         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, void *caller)
112                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
113         {
114                 ServerChannel *channel = NULL;
115                 return channel;
116         }
117
118         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length)
119                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
120         {
121                 ByteArray temp(aid, length);
122
123                 return openBasicChannelSync(temp);
124         }
125
126         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, void *caller)
127                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
128         {
129                 ByteArray temp(aid, length);
130
131                 return openBasicChannelSync(temp, caller);
132         }
133
134         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid)
135                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
136         {
137                 return openLogicalChannelSync(aid, NULL);
138         }
139
140         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, void *caller)
141                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
142         {
143                 ServerChannel *channel = NULL;
144                 return channel;
145         }
146
147         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
148                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
149         {
150                 ByteArray temp(aid, length);
151
152                 return openLogicalChannelSync(temp, NULL);
153         }
154
155         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, void *caller)
156                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
157         {
158                 ByteArray temp(aid, length);
159
160                 return openLogicalChannelSync(temp, caller);
161         }
162
163 } /* namespace smartcard_service_api */