Add changelog
[platform/core/connectivity/smartcard-service.git] / server / ServiceInstance.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 "ServiceInstance.h"
24 #include "ClientInstance.h"
25 #include "ServerResource.h"
26
27 namespace smartcard_service_api
28 {
29         unsigned int ServiceInstance::openSession(Terminal *terminal, vector<ByteArray> &certHashes, void *caller)
30         {
31                 unsigned int handle = IntegerHandle::assignHandle();
32
33                 ServerSession *session = new ServerSession((ServerReader *)0, certHashes, caller, terminal);
34
35                 mapSessions.insert(make_pair(handle, make_pair(session, terminal)));
36
37                 return handle;
38         }
39
40         ServerSession *ServiceInstance::getSession(unsigned int session)
41         {
42                 ServerSession *result = NULL;
43                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
44
45                 if ((item = mapSessions.find(session)) != mapSessions.end())
46                 {
47                         /*if (item->second.first == session)*/
48                                 result = item->second.first;
49                 }
50
51                 return result;
52         }
53
54         Terminal *ServiceInstance::getTerminal(unsigned int session)
55         {
56                 Terminal *result = NULL;
57                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
58
59                 if ((item = mapSessions.find(session)) != mapSessions.end())
60                 {
61                         result = item->second.second;
62                 }
63
64                 return result;
65         }
66
67         void ServiceInstance::closeSession(unsigned int session)
68         {
69                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
70
71                 if ((item = mapSessions.find(session)) != mapSessions.end())
72                 {
73                         closeChannelsBySession(session);
74
75                         item->second.first->closeSync();
76
77                         mapSessions.erase(item);
78
79                         IntegerHandle::releaseHandle(session);
80                 }
81         }
82
83         void ServiceInstance::closeSessions()
84         {
85                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
86
87                 closeChannels();
88
89                 for (item = mapSessions.begin(); item != mapSessions.end(); item++)
90                 {
91                         item->second.first->closeSync();
92
93                         IntegerHandle::releaseHandle(item->first);
94                 }
95
96                 mapSessions.clear();
97         }
98
99         unsigned int ServiceInstance::openChannel(unsigned int session, int channelNum, ByteArray response)
100         {
101                 Terminal *terminal = getTerminal(session);
102                 ServerChannel *channel = NULL;
103                 unsigned int handle = -1;
104
105                 /* create ServerChannel */
106                 channel = new ServerChannel((ServerSession *)session, (void *)parent->getPID(), channelNum, terminal);
107                 if (channel != NULL)
108                 {
109                         handle = IntegerHandle::assignHandle();
110                         mapChannels.insert(make_pair(handle, make_pair(session, channel)));
111
112                         if (response != ByteArray::EMPTY)
113                                 channel->selectResponse = response;
114                 }
115                 else
116                 {
117                         SCARD_DEBUG_ERR("alloc failed");
118                 }
119
120                 return handle;
121         }
122
123         ServerChannel *ServiceInstance::getChannel(/*unsigned int session, */unsigned int channel)
124         {
125                 ServerChannel *result = NULL;
126                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
127
128                 if ((item = mapChannels.find(channel)) != mapChannels.end())
129                 {
130                         /*if (item->second.first == session)*/
131                                 result = item->second.second;
132                 }
133
134                 return result;
135         }
136
137         unsigned int ServiceInstance::getChannelCountBySession(unsigned int session)
138         {
139                 unsigned int channelCount = 0;
140                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
141
142                 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
143                 {
144                         if (item->second.first == session)
145                                 channelCount++;
146                 }
147
148                 return channelCount;
149         }
150
151         void ServiceInstance::closeChannel(unsigned int channel)
152         {
153                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
154
155                 if ((item = mapChannels.find(channel)) != mapChannels.end())
156                 {
157                         /* destroy ServerChannel */
158                         delete item->second.second;
159
160                         mapChannels.erase(item);
161
162                         IntegerHandle::releaseHandle(channel);
163                 }
164         }
165
166         void ServiceInstance::closeChannelsBySession(unsigned int session)
167         {
168                 size_t i;
169                 vector<unsigned int> list;
170                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
171
172                 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
173                 {
174                         if (item->second.first == session)
175                                 list.push_back(item->second.first);
176                 }
177
178                 for (i = 0; i < list.size(); i++)
179                 {
180                         closeChannel(list[i]);
181                 }
182         }
183
184         void ServiceInstance::closeChannels()
185         {
186                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
187
188                 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
189                 {
190                         /* destroy ServerChannel */
191                         delete item->second.second;
192
193                         IntegerHandle::releaseHandle(item->first);
194                 }
195
196                 mapChannels.clear();
197         }
198
199 } /* namespace smartcard_service_api */