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.
18 /* standard library header */
20 /* SLP library header */
24 #include "ServiceInstance.h"
25 #include "ClientInstance.h"
26 #include "ServerResource.h"
28 namespace smartcard_service_api
30 unsigned int ServiceInstance::openSession(Terminal *terminal, vector<ByteArray> &certHashes, void *caller)
32 unsigned int handle = IntegerHandle::assignHandle();
34 ServerSession *session = new ServerSession((ServerReader *)0, certHashes, caller, terminal);
36 mapSessions.insert(make_pair(handle, make_pair(session, terminal)));
41 ServerSession *ServiceInstance::getSession(unsigned int session)
43 ServerSession *result = NULL;
44 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
46 if ((item = mapSessions.find(session)) != mapSessions.end())
48 /*if (item->second.first == session)*/
49 result = item->second.first;
55 Terminal *ServiceInstance::getTerminal(unsigned int session)
57 Terminal *result = NULL;
58 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
60 if ((item = mapSessions.find(session)) != mapSessions.end())
62 result = item->second.second;
68 void ServiceInstance::closeSession(unsigned int session)
70 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
72 if ((item = mapSessions.find(session)) != mapSessions.end())
74 closeChannelsBySession(session);
76 item->second.first->closeSync();
78 mapSessions.erase(item);
80 IntegerHandle::releaseHandle(session);
84 void ServiceInstance::closeSessions()
86 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
90 for (item = mapSessions.begin(); item != mapSessions.end(); item++)
92 item->second.first->closeSync();
94 IntegerHandle::releaseHandle(item->first);
100 unsigned int ServiceInstance::openChannel(unsigned int session, int channelNum, ByteArray response)
102 Terminal *terminal = getTerminal(session);
103 ServerChannel *channel = NULL;
104 unsigned int handle = -1;
106 /* create ServerChannel */
107 channel = new ServerChannel((ServerSession *)session, (void *)parent->getPID(), channelNum, terminal);
110 handle = IntegerHandle::assignHandle();
111 mapChannels.insert(make_pair(handle, make_pair(session, channel)));
113 if (response != ByteArray::EMPTY)
114 channel->selectResponse = response;
118 SCARD_DEBUG_ERR("alloc failed");
124 ServerChannel *ServiceInstance::getChannel(/*unsigned int session, */unsigned int channel)
126 ServerChannel *result = NULL;
127 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
129 if ((item = mapChannels.find(channel)) != mapChannels.end())
131 /*if (item->second.first == session)*/
132 result = item->second.second;
138 unsigned int ServiceInstance::getChannelCountBySession(unsigned int session)
140 unsigned int channelCount = 0;
141 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
143 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
145 if (item->second.first == session)
152 void ServiceInstance::closeChannel(unsigned int channel)
154 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
156 if ((item = mapChannels.find(channel)) != mapChannels.end())
158 /* destroy ServerChannel */
159 delete item->second.second;
161 mapChannels.erase(item);
163 IntegerHandle::releaseHandle(channel);
167 void ServiceInstance::closeChannelsBySession(unsigned int session)
170 vector<unsigned int> list;
171 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
173 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
175 if (item->second.first == session)
176 list.push_back(item->second.first);
179 for (i = 0; i < list.size(); i++)
181 closeChannel(list[i]);
185 void ServiceInstance::closeChannels()
187 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
189 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
191 /* destroy ServerChannel */
192 delete item->second.second;
194 IntegerHandle::releaseHandle(item->first);
200 } /* namespace smartcard_service_api */