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 "ServerResource.h"
34 namespace smartcard_service_api
36 #define OMAPI_SE_PATH "/usr/lib/se"
38 ServerSEService::ServerSEService():SEServiceHelper()
42 ServerSEService::~ServerSEService()
46 ServerSEService &ServerSEService::getInstance()
48 static ServerSEService seService;
53 Terminal *ServerSEService::createInstance(void *library)
55 Terminal *terminal = NULL;
56 terminal_create_instance_fn createInstance = NULL;
58 /* create se instance */
59 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
60 if (createInstance != NULL)
62 terminal = (Terminal *)createInstance();
65 SCARD_DEBUG("terminal [%p]", terminal);
69 SCARD_DEBUG_ERR("terminal is null");
74 SCARD_DEBUG_ERR("create_instance is null [%d]", errno);
80 bool ServerSEService::appendSELibrary(char *library)
82 void *libHandle = NULL;
85 libHandle = dlopen(library, RTLD_LAZY);
86 if (libHandle != NULL)
88 Terminal *terminal = NULL;
90 terminal = createInstance(libHandle);
93 SCARD_DEBUG("SE info : [%s] [%s]", library, terminal->getName());
95 libraries.push_back(libHandle);
97 pair<char *, Terminal *> newPair(terminal->getName(), terminal);
98 mapTerminals.insert(newPair);
100 if (terminal->isSecureElementPresence() == true)
102 ServerReader *reader = new ServerReader(this, terminal->getName(), terminal);
105 SCARD_DEBUG("register success [%s]", terminal->getName());
107 readers.push_back(reader);
111 SCARD_DEBUG_ERR("ServerReader alloc failed [%s]", terminal->getName());
112 /* throw exception */
117 SCARD_DEBUG("SE is not ready [%s]", terminal->getName());
124 SCARD_DEBUG_ERR("createInstance failed [%s]", library);
131 SCARD_DEBUG_ERR("it is not se file [%s] [%d]", library, errno);
137 int ServerSEService::openSELibraries()
142 struct dirent *entry = NULL;
144 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
146 while ((entry = readdir(dir)) != NULL)
148 if (strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
150 char fullPath[1024] = { 0, };
152 /* need additional name rule :) */
153 /* open each files */
156 snprintf(fullPath, sizeof(fullPath), "%s/%s", OMAPI_SE_PATH, entry->d_name);
158 SCARD_DEBUG("se name [%s]", fullPath);
160 result = appendSELibrary(fullPath);
176 void ServerSEService::closeSELibraries()
178 if (libraries.size() > 0)
182 for (i = 0; i < libraries.size(); i++)
184 if (libraries[i] != NULL)
185 dlclose(libraries[i]);
190 bool ServerSEService::dispatcherCallback(void *message, int socket)
194 Message *msg = (Message *)message;
195 Message response(*msg);
196 ServerResource &resource = ServerResource::getInstance();
198 if (resource.createService(socket, msg->error) != NULL)
200 SCARD_DEBUG_ERR("client added : pid [%d]", msg->error);
202 response.error = SCARD_ERROR_OK;
204 if ((count = resource.getReadersInformation(info)) > 0)
206 response.param1 = count;
207 response.data = info;
211 SCARD_DEBUG("no secure elements");
217 SCARD_DEBUG_ERR("createClient failed");
219 response.error = SCARD_ERROR_OUT_OF_MEMORY;
222 /* response to client */
223 ServerIPC::getInstance()->sendMessage(socket, &response);
228 void ServerSEService::terminalCallback(void *terminal, int event, int error, void *user_param)
231 // Terminal *term = NULL;
235 case Terminal::NOTIFY_SE_AVAILABLE :
236 /* send all client to refresh reader */
237 msg.message = msg.MSG_NOTIFY_SE_INSERTED;
238 msg.data.setBuffer((unsigned char *)terminal,
239 strlen((char *)terminal) + 1);
241 ServerResource::getInstance().sendMessageToAllClients(msg);
244 case Terminal::NOTIFY_SE_NOT_AVAILABLE :
245 /* send all client to refresh reader */
246 msg.message = msg.MSG_NOTIFY_SE_REMOVED;
247 msg.data.setBuffer((unsigned char *)terminal,
248 strlen((char *)terminal) + 1);
250 ServerResource::getInstance().sendMessageToAllClients(msg);
258 } /* namespace smartcard_service_api */