Remove license file in file section
[platform/core/connectivity/smartcard-service.git] / client / ClientDispatcher.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 <glib.h>
19
20 /* SLP library header */
21
22 /* local header */
23 #include "Debug.h"
24 #include "ClientDispatcher.h"
25 #include "SEService.h"
26 #include "Reader.h"
27 #include "Session.h"
28 #include "ClientChannel.h"
29
30 namespace smartcard_service_api
31 {
32         ClientDispatcher::ClientDispatcher()
33         {
34         }
35
36         ClientDispatcher::~ClientDispatcher()
37         {
38                 mapSESerivces.clear();
39         }
40
41         ClientDispatcher &ClientDispatcher::getInstance()
42         {
43                 static ClientDispatcher clientDispatcher;
44
45                 return clientDispatcher;
46         }
47
48         bool ClientDispatcher::addSEService(void *context, SEService *service)
49         {
50                 bool result = true;
51                 map<void *, SEService *>::iterator item;
52
53                 SCARD_BEGIN();
54
55                 if ((item = mapSESerivces.find(context)) == mapSESerivces.end())
56                 {
57                         mapSESerivces.insert(make_pair(context, service));
58                 }
59                 else
60                 {
61                         SCARD_DEBUG("SEService [%p] exists", context);
62                 }
63
64                 SCARD_END();
65
66                 return result;
67         }
68
69         void ClientDispatcher::removeSEService(void *context)
70         {
71                 map<void *, SEService *>::iterator item;
72
73                 SCARD_BEGIN();
74
75                 if ((item = mapSESerivces.find(context)) != mapSESerivces.end())
76                 {
77                         mapSESerivces.erase(item);
78                 }
79                 else
80                 {
81                         SCARD_DEBUG("SEService doesn't exist");
82                 }
83
84                 SCARD_END();
85         }
86
87         void *ClientDispatcher::dispatcherThreadFunc(DispatcherMsg *msg, void *data)
88         {
89                 SCARD_BEGIN();
90
91                 if (msg == NULL)
92                         return NULL;
93
94                 /* this messages are response from server */
95                 switch (msg->message)
96                 {
97                 /* SE Service requests */
98                 case Message::MSG_REQUEST_READERS :
99                 case Message::MSG_REQUEST_SHUTDOWN :
100                         {
101                                 if (msg->isSynchronousCall() == false)
102                                 {
103                                         DispatcherMsg *tempMsg = new DispatcherMsg(msg);
104
105                                         /* Asynchronous call */
106                                         g_idle_add((GSourceFunc)&SEService::dispatcherCallback, (gpointer)tempMsg);
107                                 }
108                                 else
109                                 {
110                                         /* Synchronous call */
111                                         SEService::dispatcherCallback(msg);
112                                 }
113                         }
114                         break;
115
116                 /* Reader requests */
117                 case Message::MSG_REQUEST_OPEN_SESSION :
118                         {
119                                 if (msg->isSynchronousCall() == false)
120                                 {
121                                         DispatcherMsg *tempMsg = new DispatcherMsg(msg);
122
123                                         /* Asynchronous call */
124                                         g_idle_add((GSourceFunc)&Reader::dispatcherCallback, (gpointer)tempMsg);
125                                 }
126                                 else
127                                 {
128                                         /* Synchronous call */
129                                         Reader::dispatcherCallback(msg);
130                                 }
131                         }
132                         break;
133
134                 /* Session requests */
135                 case Message::MSG_REQUEST_OPEN_CHANNEL :
136                 case Message::MSG_REQUEST_GET_ATR :
137                 case Message::MSG_REQUEST_CLOSE_SESSION :
138                 case Message::MSG_REQUEST_GET_CHANNEL_COUNT :
139                         {
140                                 if (msg->isSynchronousCall() == false)
141                                 {
142                                         DispatcherMsg *tempMsg = new DispatcherMsg(msg);
143
144                                         /* Asynchronous call */
145                                         g_idle_add((GSourceFunc)&Session::dispatcherCallback, (gpointer)tempMsg);
146                                 }
147                                 else
148                                 {
149                                         /* Synchronous call */
150                                         Session::dispatcherCallback(msg);
151                                 }
152                         }
153                         break;
154
155                 /* ClientChannel requests */
156                 case Message::MSG_REQUEST_TRANSMIT :
157                 case Message::MSG_REQUEST_CLOSE_CHANNEL :
158                         {
159                                 if (msg->isSynchronousCall() == false)
160                                 {
161                                         DispatcherMsg *tempMsg = new DispatcherMsg(msg);
162
163                                         /* Asynchronous call */
164                                         g_idle_add((GSourceFunc)&ClientChannel::dispatcherCallback, (gpointer)tempMsg);
165                                 }
166                                 else
167                                 {
168                                         /* Synchronous call */
169                                         ClientChannel::dispatcherCallback(msg);
170                                 }
171                         }
172                         break;
173
174                 case Message::MSG_NOTIFY_SE_INSERTED :
175                 case Message::MSG_NOTIFY_SE_REMOVED :
176                         {
177                                 map<void *, SEService *>::iterator item;
178
179                                 for (item = mapSESerivces.begin(); item != mapSESerivces.end(); item++)
180                                 {
181                                         DispatcherMsg *tempMsg = new DispatcherMsg(msg);
182
183                                         tempMsg->caller = item->second;
184
185                                         /* Always asynchronous call */
186                                         g_idle_add((GSourceFunc)&SEService::dispatcherCallback, (gpointer)tempMsg);
187                                 }
188                         }
189                         break;
190
191                 case Message::MSG_OPERATION_RELEASE_CLIENT :
192                         {
193                                 map<void *, SEService *>::iterator item;
194
195                                 for (item = mapSESerivces.begin(); item != mapSESerivces.end(); item++)
196                                 {
197                                         DispatcherMsg *tempMsg = new DispatcherMsg(msg);
198
199                                         tempMsg->caller = item->second;
200                                         tempMsg->error = -1;
201
202                                         /* Always asynchronous call */
203                                         g_idle_add((GSourceFunc)&SEService::dispatcherCallback, (gpointer)tempMsg);
204                                 }
205                         }
206                         break;
207
208                 default :
209                         break;
210                 }
211
212                 SCARD_END();
213
214                 return NULL;
215         }
216
217 } /* namespace open_mobile_api */
218