update the latest source
[framework/system/smartcard-service.git] / server / ServerDispatcher.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 /* standard library header */
19 #include <stdio.h>
20 #include <string.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "ServerDispatcher.h"
27 #include "ServerResource.h"
28 #include "ServerSEService.h"
29 #include "ServerChannel.h"
30 #include "ServerSession.h"
31 #include "ServerReader.h"
32
33 namespace smartcard_service_api
34 {
35         ServerDispatcher::ServerDispatcher():DispatcherHelper()
36         {
37                 SCARD_BEGIN();
38
39                 runDispatcherThread();
40
41                 SCARD_END();
42         }
43
44         ServerDispatcher::~ServerDispatcher()
45         {
46         }
47
48         ServerDispatcher *ServerDispatcher::getInstance()
49         {
50                 static ServerDispatcher instance;
51
52                 return &instance;
53         }
54
55         void *ServerDispatcher::dispatcherThreadFunc(DispatcherMsg *msg, void *data)
56         {
57                 int socket = -1;
58                 ServerResource *resource = NULL;
59
60                 if (data == NULL)
61                 {
62                         SCARD_DEBUG_ERR("dispatcher instance is null");
63                         return NULL;
64                 }
65
66                 if (msg == NULL)
67                 {
68                         SCARD_DEBUG_ERR("message is null");
69                         return NULL;
70                 }
71
72                 resource = &ServerResource::getInstance();
73                 socket = msg->getPeerSocket();
74
75                 switch (msg->message)
76                 {
77                 /* handle message */
78                 case Message::MSG_REQUEST_READERS :
79                         {
80                                 SCARD_DEBUG("[MSG_REQUEST_READERS]");
81
82 #if 0
83                                 seService->dispatcherCallback(msg, msg->getPeerSocket());
84 #else
85                                 int count = 0;
86                                 Message response(*msg);
87                                 ByteArray info;
88                                 ClientInstance *instance = NULL;
89
90                                 if ((instance = resource->getClient(socket)) != NULL)
91                                 {
92                                         /* update client PID */
93                                         if (instance->getPID() == -1)
94                                         {
95                                                 instance->setPID(msg->error);
96                                                 SCARD_DEBUG_ERR("update PID [%d]", msg->error);
97                                         }
98
99                                         /* create service */
100                                         if (resource->getService(socket, (unsigned int)msg->userParam) == NULL)
101                                         {
102                                                 if (resource->createService(socket, (unsigned int)msg->userParam) == true)
103                                                 {
104                                                         SCARD_DEBUG_ERR("client added : context [%d]", (unsigned int)msg->userParam);
105                                                 }
106                                                 else
107                                                 {
108                                                         SCARD_DEBUG_ERR("createClient failed");
109
110                                                         response.param1 = 0;
111                                                         response.error = -1;
112
113                                                         /* response to client */
114                                                         ServerIPC::getInstance()->sendMessage(socket, &response);
115
116                                                         return NULL;
117                                                 }
118                                         }
119
120                                         if ((count = resource->getReadersInformation(info)) > 0)
121                                         {
122                                                 response.param1 = count;
123                                                 response.param2 = 0;
124                                                 response.error = 0;
125                                                 response.data = info;
126                                         }
127                                         else
128                                         {
129                                                 SCARD_DEBUG("no secure elements");
130
131                                                 response.error = -1;
132                                         }
133                                 }
134                                 else
135                                 {
136                                         SCARD_DEBUG("client doesn't exist, socket [%d]", socket);
137
138                                         response.error = -1;
139                                 }
140
141                                 /* response to client */
142                                 ServerIPC::getInstance()->sendMessage(socket, &response);
143 #endif
144                         }
145                         break;
146
147                 case Message::MSG_REQUEST_SHUTDOWN :
148 #if 0
149                         {
150                                 Message response(*msg);
151
152                                 SCARD_DEBUG("[MSG_REQUEST_SHUTDOWN]");
153
154                                 if (msg->param1 != 0)
155                                 {
156                                         ServerChannel *channel = NULL;
157
158                                         channel = (ServerChannel *)msg->param1;
159
160                                         channel->closeSync();
161                                 }
162
163                                 /* response to client */
164                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
165                         }
166 #else
167                         {
168                                 Message response(*msg);
169
170                                 SCARD_DEBUG("[MSG_REQUEST_SHUTDOWN]");
171
172                                 response.error = 0;
173
174                                 resource->removeService(socket, msg->error/* service context */);
175
176                                 /* response to client */
177                                 ServerIPC::getInstance()->sendMessage(socket, &response);
178                         }
179 #endif
180                         break;
181
182                 case Message::MSG_REQUEST_OPEN_SESSION :
183 #if 0
184                         {
185                                 Message response(*msg);
186                                 ServerReader *reader = NULL;
187                                 ServerSession *session = NULL;
188
189                                 SCARD_DEBUG("[MSG_REQUEST_OPEN_SESSION]");
190
191                                 if (msg->param1 != 0)
192                                 {
193                                         reader = (ServerReader *)msg->param1;
194
195                                         session = reader->openSessionSync(msg->data, msg->caller);
196                                 }
197
198                                 /* TODO : attach atr??? */
199                                 response.param1 = (unsigned int)session;
200
201                                 /* response to client */
202                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
203                         }
204 #else
205                         {
206                                 Message response(*msg);
207                                 unsigned int handle = -1;
208
209                                 SCARD_DEBUG("[MSG_REQUEST_OPEN_SESSION]");
210
211                                 response.param1 = -1;
212                                 response.error = -1;
213
214                                 if (resource->isValidReaderHandle(msg->param1))
215                                 {
216                                         handle = resource->createSession(socket, msg->error/* service context */, msg->param1, msg->data, msg->caller);
217                                         if (handle != IntegerHandle::INVALID_HANDLE)
218                                         {
219                                                 response.param1 = handle;
220                                                 response.error = 0;
221                                         }
222                                         else
223                                         {
224                                                 SCARD_DEBUG_ERR("createSession failed [%d]", handle);
225                                         }
226                                 }
227                                 else
228                                 {
229                                         SCARD_DEBUG_ERR("request invalid reader handle [%d]", msg->param1);
230                                 }
231
232                                 /* response to client */
233                                 ServerIPC::getInstance()->sendMessage(socket, &response);
234                         }
235 #endif
236                         break;
237
238                 case Message::MSG_REQUEST_CLOSE_SESSION :
239 #if 0
240                         {
241                                 Message response(*msg);
242                                 ServerSession *session = NULL;
243
244                                 SCARD_DEBUG("[MSG_REQUEST_CLOSE_SESSION]");
245
246                                 if (msg->param1 != 0)
247                                 {
248                                         session = (ServerSession *)msg->param1;
249
250                                         session->closeSync();
251                                 }
252
253                                 /* response to client */
254                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
255                         }
256 #else
257                         {
258                                 Message response(*msg);
259
260                                 SCARD_DEBUG("[MSG_REQUEST_CLOSE_SESSION]");
261
262                                 response.param1 = -1;
263                                 response.error = -1;
264
265                                 if (resource->isValidSessionHandle(socket, msg->error/* service context */, msg->param1))
266                                 {
267                                         resource->removeSession(socket, msg->error/* service context */, msg->param1);
268                                 }
269
270                                 /* response to client */
271                                 ServerIPC::getInstance()->sendMessage(socket, &response);
272                         }
273 #endif
274                         break;
275
276                 case Message::MSG_REQUEST_OPEN_CHANNEL :
277 #if 0
278                         {
279                                 Message response(*msg);
280                                 ServerSession *session = NULL;
281
282                                 SCARD_DEBUG("[MSG_REQUEST_OPEN_CHANNEL]");
283
284                                 if (/* check valid session handle */msg->param2 != 0)
285                                 {
286                                         ServerChannel *channel = NULL;
287
288                                         session = (ServerSession *)msg->param2;
289
290                                         if (msg->param1 == 0)
291                                                 channel = (ServerChannel *)session->openBasicChannelSync(msg->data, msg->caller);
292                                         else
293                                                 channel = (ServerChannel *)session->openLogicalChannelSync(msg->data, msg->caller);
294
295                                         if (channel != NULL)
296                                         {
297                                                 response.param1 = (unsigned int)channel;
298                                                 response.param2 = channel->getChannelID();
299                                                 response.error = 0;
300                                                 response.data = channel->getSelectResponse();
301                                         }
302                                         else
303                                         {
304                                                 SCARD_DEBUG_ERR("channel is null.");
305
306                                                 /* set error value */
307                                                 response.param1 = 0;
308                                                 response.param2 = 0;
309                                                 response.error = -4;
310                                                 response.data.releaseBuffer();
311                                         }
312                                 }
313                                 else
314                                 {
315                                         SCARD_DEBUG_ERR("session is invalid");
316
317                                         response.param1 = 0;
318                                         response.param2 = 0;
319                                         response.error = -1;
320                                         response.data.releaseBuffer();
321                                 }
322
323                                 /* response to client */
324                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
325                         }
326 #else
327                         {
328                                 Message response(*msg);
329                                 unsigned int channelID = -1;
330
331                                 SCARD_DEBUG("[MSG_REQUEST_OPEN_CHANNEL]");
332
333                                 response.param1 = 0;
334                                 response.param2 = 0;
335                                 response.error = -1;
336                                 response.data.releaseBuffer();
337
338                                 channelID = resource->createChannel(socket, msg->error/* service context */, msg->param2, msg->param1, msg->data);
339                                 if (channelID != IntegerHandle::INVALID_HANDLE)
340                                 {
341                                         ServerChannel *temp = (ServerChannel *)resource->getChannel(socket, msg->error/* service context */, channelID);
342
343                                         if (temp != NULL)
344                                         {
345                                                 response.param1 = channelID;
346                                                 response.param2 = temp->channelNum;
347                                                 response.error = 0;
348                                                 response.data = temp->selectResponse;
349                                         }
350                                         else
351                                         {
352                                                 SCARD_DEBUG_ERR("IS IT POSSIBLE??????????????????");
353                                         }
354                                 }
355                                 else
356                                 {
357                                         SCARD_DEBUG_ERR("channel is null.");
358
359                                         /* set error value */
360                                         response.error = -4;
361                                 }
362
363                                 /* response to client */
364                                 ServerIPC::getInstance()->sendMessage(socket, &response);
365                         }
366 #endif
367                         break;
368
369                 case Message::MSG_REQUEST_GET_CHANNEL_COUNT :
370                         {
371                                 Message response(*msg);
372
373                                 SCARD_DEBUG("[MSG_REQUEST_GET_CHANNEL_COUNT]");
374
375                                 response.error = 0;
376
377                                 response.param1 = resource->getChannelCount(socket, msg->error/* service context */, msg->param1);
378
379                                 /* response to client */
380                                 ServerIPC::getInstance()->sendMessage(socket, &response);
381                         }
382                         break;
383
384                 case Message::MSG_REQUEST_CLOSE_CHANNEL :
385 #if 0
386                         {
387                                 Message response(*msg);
388
389                                 SCARD_DEBUG("[MSG_REQUEST_CLOSE_CHANNEL]");
390
391                                 if (msg->param1 != 0)
392                                 {
393                                         ServerChannel *channel = NULL;
394
395                                         channel = (ServerChannel *)msg->param1;
396
397                                         channel->closeSync();
398                                 }
399
400                                 /* response to client */
401                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
402                         }
403 #else
404                         {
405                                 Message response(*msg);
406
407                                 SCARD_DEBUG("[MSG_REQUEST_CLOSE_CHANNEL]");
408
409                                 response.error = 0;
410
411                                 if (resource->getChannel(socket, msg->error/* service context */, msg->param1) != NULL)
412                                 {
413                                         resource->removeChannel(socket, msg->error/* service context */, msg->param1);
414                                 }
415
416                                 /* response to client */
417                                 ServerIPC::getInstance()->sendMessage(socket, &response);
418                         }
419 #endif
420                         break;
421
422                 case Message::MSG_REQUEST_GET_ATR :
423 #if 0
424                         {
425                                 Message response(*msg);
426
427                                 SCARD_DEBUG("[MSG_REQUEST_GET_ATR]");
428
429                                 if (msg->param1 != 0)
430                                 {
431                                         ServerChannel *channel = NULL;
432
433                                         channel = (ServerChannel *)msg->param1;
434
435                                         channel->closeSync();
436                                 }
437
438                                 /* response to client */
439                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
440                         }
441 #else
442                         {
443                                 int rv;
444                                 Message response(*msg);
445                                 ByteArray result;
446                                 ServiceInstance *client = NULL;
447
448                                 SCARD_DEBUG("[MSG_REQUEST_GET_ATR]");
449
450                                 response.param1 = 0;
451                                 response.param2 = 0;
452                                 response.error = -1;
453
454                                 if ((client = resource->getService(socket, msg->error/* service context */)) != NULL)
455                                 {
456                                         Terminal *terminal = NULL;
457
458                                         if ((terminal = client->getTerminal(msg->param1)) != NULL)
459                                         {
460                                                 if ((rv = terminal->getATRSync(result)) == 0)
461                                                 {
462                                                         response.data = result;
463                                                         response.error = 0;
464                                                 }
465                                                 else
466                                                 {
467                                                         SCARD_DEBUG_ERR("transmit failed [%d]", rv);
468
469                                                         response.error = rv;
470                                                 }
471                                         }
472                                         else
473                                         {
474                                                 SCARD_DEBUG_ERR("getTerminal failed : socket [%d], context [%d], session [%d]", socket, msg->error/* service context */, msg->param1);
475                                         }
476                                 }
477                                 else
478                                 {
479                                         SCARD_DEBUG_ERR("getClient failed : socket [%d], context [%d], session [%d]", socket, msg->error/* service context */, msg->param1);
480                                 }
481
482                                 /* response to client */
483                                 ServerIPC::getInstance()->sendMessage(socket, &response);
484                         }
485 #endif
486                         break;
487
488                 case Message::MSG_REQUEST_TRANSMIT :
489 #if 0
490                         {
491                                 Message response(*msg);
492                                 ByteArray result;
493                                 int rv;
494
495                                 SCARD_DEBUG("[MSG_REQUEST_TRANSMIT]");
496
497                                 if (msg->param1 != 0)
498                                 {
499                                         ServerChannel *channel = NULL;
500
501                                         channel = (ServerChannel *)msg->param1;
502
503                                         if ((rv = channel->transmitSync(msg->data, result)) == 0)
504                                         {
505                                                 response.data = result;
506                                         }
507                                         else
508                                         {
509                                                 SCARD_DEBUG_ERR("transmit failed [%d]", rv);
510                                         }
511                                 }
512
513 //                              if (resource->isValidChannelHandle((void *)msg->param1))
514 //                              {
515 //                              }
516
517                                 /* response to client */
518                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
519                         }
520 #else
521                         {
522                                 int rv;
523                                 Message response(*msg);
524                                 ByteArray result;
525                                 Channel *channel = NULL;
526
527                                 SCARD_DEBUG("[MSG_REQUEST_TRANSMIT]");
528
529                                 response.param1 = 0;
530                                 response.param2 = 0;
531                                 response.error = -1;
532
533                                 if ((channel = resource->getChannel(socket, msg->error/* service context */, msg->param1)) != NULL)
534                                 {
535                                         if ((rv = channel->transmitSync(msg->data, result)) == 0)
536                                         {
537                                                 response.data = result;
538                                                 response.error = 0;
539                                         }
540                                         else
541                                         {
542                                                 SCARD_DEBUG_ERR("transmit failed [%d]", rv);
543
544                                                 response.error = rv;
545                                         }
546                                 }
547                                 else
548                                 {
549                                         SCARD_DEBUG_ERR("invalid handle : socket [%d], context [%d], channel [%d]", socket, msg->error/* service context */, msg->param1);
550                                 }
551
552                                 /* response to client */
553                                 ServerIPC::getInstance()->sendMessage(socket, &response);
554                         }
555 #endif
556                         break;
557
558                 case Message::MSG_OPERATION_RELEASE_CLIENT :
559 #if 0
560                         {
561                                 Message response(*msg);
562
563                                 SCARD_DEBUG("[MSG_REQUEST_CLOSE_CHANNEL]");
564
565                                 if (msg->param1 != 0)
566                                 {
567                                         ServerChannel *channel = NULL;
568
569                                         channel = (ServerChannel *)msg->param1;
570
571                                         channel->closeSync();
572                                 }
573
574                                 /* response to client */
575                                 ServerIPC::getInstance()->sendMessage(msg->getPeerSocket(), &response);
576                         }
577 #else
578                         {
579                                 SCARD_DEBUG("[MSG_OPERATION_RELEASE_CLIENT]");
580
581                                 resource->removeClient(msg->param1);
582                         }
583 #endif
584                         break;
585
586                 default :
587                         SCARD_DEBUG("unknown message [%s], socket [%d]", msg->toString(), socket);
588                         break;
589                 }
590
591                 return NULL;
592         }
593
594 } /* namespace smartcard_service_api */