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.
18 /* standard library header */
25 /* SLP library header */
30 #include "TerminalInterface.h"
31 #include "ServerSEService.h"
32 #include "ServerIPC.h"
33 #include "ServerResource.h"
35 namespace smartcard_service_api
37 #define OMAPI_SE_PATH "/usr/lib/se"
39 ServerSEService::ServerSEService():SEServiceHelper()
43 ServerSEService::~ServerSEService()
47 ServerSEService &ServerSEService::getInstance()
49 static ServerSEService seService;
54 Terminal *ServerSEService::createInstance(void *library)
56 Terminal *terminal = NULL;
57 terminal_create_instance_fn createInstance = NULL;
59 /* create se instance */
60 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
61 if (createInstance != NULL)
63 terminal = (Terminal *)createInstance();
66 _DBG("terminal [%p]", terminal);
70 _ERR("terminal is null");
75 _ERR("create_instance is null [%d]", errno);
81 bool ServerSEService::appendSELibrary(char *library)
83 void *libHandle = NULL;
86 libHandle = dlopen(library, RTLD_LAZY);
87 if (libHandle != NULL)
89 Terminal *terminal = NULL;
91 terminal = createInstance(libHandle);
94 _DBG("SE info : [%s] [%s]", library, terminal->getName());
96 libraries.push_back(libHandle);
98 pair<char *, Terminal *> newPair(terminal->getName(), terminal);
99 mapTerminals.insert(newPair);
101 if (terminal->isSecureElementPresence() == true)
103 ServerReader *reader = new ServerReader(this, terminal->getName(), terminal);
106 _DBG("register success [%s]", terminal->getName());
108 readers.push_back(reader);
112 _ERR("ServerReader alloc failed [%s]", terminal->getName());
113 /* throw exception */
118 _DBG("SE is not ready [%s]", terminal->getName());
125 _ERR("createInstance failed [%s]", library);
132 _ERR("it is not se file [%s] [%d]", library, errno);
138 int ServerSEService::openSELibraries()
143 struct dirent *entry = NULL;
145 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
147 while ((entry = readdir(dir)) != NULL)
149 if (strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
151 char fullPath[1024] = { 0, };
153 /* need additional name rule :) */
154 /* open each files */
157 snprintf(fullPath, sizeof(fullPath), "%s/%s", OMAPI_SE_PATH, entry->d_name);
159 SCARD_DEBUG("se name [%s]", fullPath);
161 result = appendSELibrary(fullPath);
177 void ServerSEService::closeSELibraries()
179 if (libraries.size() > 0)
183 for (i = 0; i < libraries.size(); i++)
185 if (libraries[i] != NULL)
186 dlclose(libraries[i]);
191 bool ServerSEService::dispatcherCallback(void *message, int socket)
196 Message *msg = (Message *)message;
197 Message response(*msg);
198 ServerResource &resource = ServerResource::getInstance();
199 ServiceInstance *service;
201 if ((service = resource.createService(socket)) != NULL)
203 _ERR("client added : pid [%d]", msg->error);
205 response.error = SCARD_ERROR_OK;
206 response.param2 = service->getHandle();
208 if ((count = resource.getReadersInformation(info)) > 0)
210 response.param1 = count;
211 response.data = info;
215 _DBG("no secure elements");
221 _ERR("createClient failed");
223 response.error = SCARD_ERROR_OUT_OF_MEMORY;
226 /* response to client */
227 ServerIPC::getInstance()->sendMessage(socket, &response);
232 void ServerSEService::terminalCallback(void *terminal, int event, int error, void *user_param)
235 // Terminal *term = NULL;
239 case Terminal::NOTIFY_SE_AVAILABLE :
240 /* send all client to refresh reader */
241 msg.message = msg.MSG_NOTIFY_SE_INSERTED;
242 msg.data.setBuffer((unsigned char *)terminal,
243 strlen((char *)terminal) + 1);
245 ServerResource::getInstance().sendMessageToAllClients(msg);
248 case Terminal::NOTIFY_SE_NOT_AVAILABLE :
249 /* send all client to refresh reader */
250 msg.message = msg.MSG_NOTIFY_SE_REMOVED;
251 msg.data.setBuffer((unsigned char *)terminal,
252 strlen((char *)terminal) + 1);
254 ServerResource::getInstance().sendMessageToAllClients(msg);
262 } /* namespace smartcard_service_api */