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