Adjust comment
[platform/core/connectivity/smartcard-service.git] / test-client / test-client-sync.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 #include <stdio.h>
18 #include <string.h>
19 #include <glib.h>
20
21 #include "Debug.h"
22 #include "SEService.h"
23 #include "Reader.h"
24 #include "Session.h"
25 #include "APDUHelper.h"
26 #include "ClientChannel.h"
27
28 using namespace smartcard_service_api;
29
30 typedef struct _user_context_t
31 {
32         Session *clientSession;
33         SEServiceHelper *clientService;
34         Channel *clientChannel;
35 }
36 user_context_t;
37
38 /* global variable */
39 GMainLoop *loop = NULL;
40 user_context_t user_context = { 0, };
41
42 void testCloseCallback(int error, void *userData);
43 void testTransmitCallback(unsigned char *buffer, unsigned int length, int error, void *userData);
44 void testOpenChannelCallback(Channel *channel, int error, void *userData);
45 void testGetATRCallback(unsigned char *atr, unsigned int length, int error, void *userData);
46 void testCloseSessionCallback(int error, void *userData);
47 void testOpenSessionCallback(SessionHelper *session, int error, void *userData);
48 void testConnectedCallback(SEServiceHelper *service, void *context);
49
50 class TestEventHandler : public SEServiceListener
51 {
52         void serviceConnected(SEServiceHelper *service, void *userData)
53         {
54                 SCARD_BEGIN();
55                 testConnectedCallback(service, userData);
56                 SCARD_END();
57         }
58
59         void eventHandler(SEServiceHelper *service, char *seName, int event, void *userData)
60         {
61                 SCARD_BEGIN();
62
63                 SCARD_DEBUG("event occured service [%p], seName[%p], event [%d]", service, seName, event);
64
65                 SCARD_END();
66         }
67
68         void errorHandler(SEServiceHelper *service, int error, void *userData)
69         {
70                 SCARD_BEGIN();
71
72                 SCARD_DEBUG("error occured service [%p], error [%d]", service, error);
73
74                 SCARD_END();
75         }
76 };
77
78 TestEventHandler testEventHandler;
79
80 void testConnectedCallback(SEServiceHelper *service, void *userData)
81 {
82         vector<ReaderHelper *> readers;
83         user_context_t *context = (user_context_t *)userData;
84
85         SCARD_BEGIN();
86
87         if (service != NULL)
88         {
89                 SCARD_DEBUG("callback called, service [%p]", service);
90
91                 context->clientService = service;
92
93                 readers = service->getReaders();
94
95                 if (readers.size() > 0)
96                 {
97                         Reader *reader = NULL;
98
99                         reader = (Reader *)readers[0];
100
101                         SCARD_DEBUG("reader [%p]", reader);
102
103                         Session *session = (Session *)reader->openSessionSync();
104                         if (session != NULL)
105                         {
106                                 SCARD_DEBUG("session [%p]", session);
107
108                                 ByteArray temp;
109                                 temp = session->getATRSync();
110                                 SCARD_DEBUG("atr[%d] : %s", temp.getLength(), temp.toString());
111
112                                 unsigned char MF[] = { 0xA0, 0x00, 0x00, 0x00, 0x63, 0x50, 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
113                                 ByteArray aid;
114
115                                 aid.setBuffer(MF, sizeof(MF));
116                                 ClientChannel *channel = (ClientChannel *)session->openLogicalChannelSync(aid);
117                                 if (channel != NULL)
118                                 {
119                                         SCARD_DEBUG("channel [%p]", channel);
120                                         ByteArray response;
121                                         ByteArray data, command;
122                                         int fid = 0x00003150;
123
124                                         response = channel->getSelectResponse();
125                                         SCARD_DEBUG("response : %s", response.toString());
126
127                                         SCARD_DEBUG("isBasicChannel() = %s", channel->isBasicChannel() ? "Basic" : "Logical");
128                                         SCARD_DEBUG("isClosed() = %s", channel->isClosed() ? "Closed" : "Opened");
129
130                                         data.setBuffer((unsigned char *)&fid, 2);
131                                         command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_ID, 0, data);
132                                         int error = channel->transmitSync(command, response);
133
134                                         SCARD_DEBUG("error : %d, response : %s", error, response.toString());
135
136                                         channel->closeSync();
137                                 }
138                                 else
139                                 {
140                                         SCARD_DEBUG_ERR("openLogicalChannelSync failed");
141                                 }
142
143                                 session->closeSync();
144                         }
145                         else
146                         {
147                                 SCARD_DEBUG_ERR("openSessionSync failed");
148                         }
149
150                         service->shutdown();
151                 }
152                 else
153                 {
154                         SCARD_DEBUG_ERR("reader is empty");
155                 }
156         }
157         else
158         {
159                 SCARD_DEBUG_ERR("service is NULL");
160         }
161
162         g_main_loop_quit(loop);
163
164         SCARD_END();
165 }
166
167 int main(int argv, char *args[])
168 {
169         SEService *service = new SEService((void *)&user_context, &testEventHandler);
170
171         loop = g_main_new(TRUE);
172         g_main_loop_run(loop);
173
174         if (service != NULL)
175                 delete service;
176
177         return 0;
178 }