2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "SEService.h"
25 #include "APDUHelper.h"
26 #include "ClientChannel.h"
28 using namespace smartcard_service_api;
30 typedef struct _user_context_t
32 Session *clientSession;
33 SEServiceHelper *clientService;
34 Channel *clientChannel;
39 GMainLoop *loop = NULL;
40 user_context_t user_context = { 0, };
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);
50 class TestEventHandler : public SEServiceListener
52 void serviceConnected(SEServiceHelper *service, void *userData)
55 testConnectedCallback(service, userData);
59 void eventHandler(SEServiceHelper *service, char *seName, int event, void *userData)
63 SCARD_DEBUG("event occured service [%p], seName[%p], event [%d]", service, seName, event);
68 void errorHandler(SEServiceHelper *service, int error, void *userData)
72 SCARD_DEBUG("error occured service [%p], error [%d]", service, error);
78 TestEventHandler testEventHandler;
80 void testConnectedCallback(SEServiceHelper *service, void *userData)
82 vector<ReaderHelper *> readers;
83 user_context_t *context = (user_context_t *)userData;
89 SCARD_DEBUG("callback called, service [%p]", service);
91 context->clientService = service;
93 readers = service->getReaders();
95 if (readers.size() > 0)
97 Reader *reader = NULL;
99 reader = (Reader *)readers[0];
101 SCARD_DEBUG("reader [%p]", reader);
103 Session *session = (Session *)reader->openSessionSync();
106 SCARD_DEBUG("session [%p]", session);
109 temp = session->getATRSync();
110 SCARD_DEBUG("atr[%d] : %s", temp.getLength(), temp.toString());
112 unsigned char MF[] = { 0xA0, 0x00, 0x00, 0x00, 0x63, 0x50, 0x4B, 0x43, 0x53, 0x2D, 0x31, 0x35 };
115 aid.setBuffer(MF, sizeof(MF));
116 ClientChannel *channel = (ClientChannel *)session->openLogicalChannelSync(aid);
119 SCARD_DEBUG("channel [%p]", channel);
121 ByteArray data, command;
122 int fid = 0x00003150;
124 response = channel->getSelectResponse();
125 SCARD_DEBUG("response : %s", response.toString());
127 SCARD_DEBUG("isBasicChannel() = %s", channel->isBasicChannel() ? "Basic" : "Logical");
128 SCARD_DEBUG("isClosed() = %s", channel->isClosed() ? "Closed" : "Opened");
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);
134 SCARD_DEBUG("error : %d, response : %s", error, response.toString());
136 channel->closeSync();
140 SCARD_DEBUG_ERR("openLogicalChannelSync failed");
143 session->closeSync();
147 SCARD_DEBUG_ERR("openSessionSync failed");
154 SCARD_DEBUG_ERR("reader is empty");
159 SCARD_DEBUG_ERR("service is NULL");
162 g_main_loop_quit(loop);
167 int main(int argv, char *args[])
169 SEService *service = new SEService((void *)&user_context, &testEventHandler);
171 loop = g_main_new(TRUE);
172 g_main_loop_run(loop);