2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
19 /* SLP library header */
23 #include "ServiceInstance.h"
24 #include "ClientInstance.h"
25 #include "ServerResource.h"
27 namespace smartcard_service_api
29 unsigned int ServiceInstance::openSession(Terminal *terminal, vector<ByteArray> &certHashes, void *caller)
31 unsigned int handle = IntegerHandle::assignHandle();
33 ServerSession *session = new ServerSession((ServerReader *)0, certHashes, caller, terminal);
35 mapSessions.insert(make_pair(handle, make_pair(session, terminal)));
40 ServerSession *ServiceInstance::getSession(unsigned int session)
42 ServerSession *result = NULL;
43 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
45 if ((item = mapSessions.find(session)) != mapSessions.end())
47 /*if (item->second.first == session)*/
48 result = item->second.first;
54 Terminal *ServiceInstance::getTerminal(unsigned int session)
56 Terminal *result = NULL;
57 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
59 if ((item = mapSessions.find(session)) != mapSessions.end())
61 result = item->second.second;
67 void ServiceInstance::closeSession(unsigned int session)
69 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
71 if ((item = mapSessions.find(session)) != mapSessions.end())
73 closeChannelsBySession(session);
75 item->second.first->closeSync();
77 mapSessions.erase(item);
79 IntegerHandle::releaseHandle(session);
83 void ServiceInstance::closeSessions()
85 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
89 for (item = mapSessions.begin(); item != mapSessions.end(); item++)
91 item->second.first->closeSync();
93 IntegerHandle::releaseHandle(item->first);
99 unsigned int ServiceInstance::openChannel(unsigned int session, int channelNum, ByteArray response)
101 Terminal *terminal = getTerminal(session);
102 ServerChannel *channel = NULL;
103 unsigned int handle = -1;
105 /* create ServerChannel */
106 channel = new ServerChannel((ServerSession *)session, (void *)parent->getPID(), channelNum, terminal);
109 handle = IntegerHandle::assignHandle();
110 mapChannels.insert(make_pair(handle, make_pair(session, channel)));
112 if (response != ByteArray::EMPTY)
113 channel->selectResponse = response;
117 SCARD_DEBUG_ERR("alloc failed");
123 ServerChannel *ServiceInstance::getChannel(/*unsigned int session, */unsigned int channel)
125 ServerChannel *result = NULL;
126 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
128 if ((item = mapChannels.find(channel)) != mapChannels.end())
130 /*if (item->second.first == session)*/
131 result = item->second.second;
137 unsigned int ServiceInstance::getChannelCountBySession(unsigned int session)
139 unsigned int channelCount = 0;
140 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
142 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
144 if (item->second.first == session)
151 void ServiceInstance::closeChannel(unsigned int channel)
153 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
155 if ((item = mapChannels.find(channel)) != mapChannels.end())
157 /* destroy ServerChannel */
158 delete item->second.second;
160 mapChannels.erase(item);
162 IntegerHandle::releaseHandle(channel);
166 void ServiceInstance::closeChannelsBySession(unsigned int session)
169 vector<unsigned int> list;
170 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
172 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
174 if (item->second.first == session)
175 list.push_back(item->second.first);
178 for (i = 0; i < list.size(); i++)
180 closeChannel(list[i]);
184 void ServiceInstance::closeChannels()
186 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
188 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
190 /* destroy ServerChannel */
191 delete item->second.second;
193 IntegerHandle::releaseHandle(item->first);
199 } /* namespace smartcard_service_api */