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 */
29 #include "TerminalInterface.h"
30 #include "ServerResource.h"
31 #include "ServerSEService.h"
33 namespace smartcard_service_api
35 #define OMAPI_SE_PATH "/usr/lib/se"
37 ServerSEService::ServerSEService():SEServiceHelper()
41 ServerSEService::~ServerSEService()
45 ServerSEService &ServerSEService::getInstance()
47 static ServerSEService seService;
52 Terminal *ServerSEService::createInstance(void *library)
54 Terminal *terminal = NULL;
55 terminal_create_instance_fn createInstance = NULL;
57 /* create se instance */
58 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
59 if (createInstance != NULL)
61 terminal = (Terminal *)createInstance();
64 _DBG("terminal [%p]", terminal);
68 _ERR("terminal is null");
73 _ERR("create_instance is null [%d]", errno);
79 bool ServerSEService::appendSELibrary(char *library)
81 void *libHandle = NULL;
84 libHandle = dlopen(library, RTLD_LAZY);
85 if (libHandle != NULL)
87 Terminal *terminal = NULL;
89 terminal = createInstance(libHandle);
92 _DBG("SE info : [%s] [%s]", library, terminal->getName());
94 libraries.push_back(libHandle);
96 pair<string, Terminal *> newPair(terminal->getName(), terminal);
97 mapTerminals.insert(newPair);
99 if (terminal->isSecureElementPresence() == true)
101 ServerReader *reader = new ServerReader(this, terminal->getName(), terminal);
104 _DBG("register success [%s]", terminal->getName());
106 readers.push_back(reader);
110 _ERR("ServerReader alloc failed [%s]", terminal->getName());
111 /* throw exception */
116 _DBG("SE is not ready [%s]", terminal->getName());
123 _ERR("createInstance failed [%s]", library);
130 _ERR("it is not se file [%s] [%d]", library, errno);
136 int ServerSEService::openSELibraries()
140 struct dirent *entry = NULL;
142 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
144 while ((entry = readdir(dir)) != NULL)
146 if (strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
148 char fullPath[1024] = { 0, };
150 /* need additional name rule :) */
151 /* open each files */
153 snprintf(fullPath, sizeof(fullPath), "%s/%s", OMAPI_SE_PATH, entry->d_name);
155 SECURE_LOGD("se name [%s]", fullPath);
157 result = appendSELibrary(fullPath);
173 void ServerSEService::closeSELibraries()
175 if (libraries.size() > 0)
179 for (i = 0; i < libraries.size(); i++)
181 if (libraries[i] != NULL)
182 dlclose(libraries[i]);
188 bool ServerSEService::isValidReaderHandle(void *handle)
193 for (i = 0; i < readers.size(); i++)
195 if ((void *)readers[i] == handle)
206 void ServerSEService::terminalCallback(const void *terminal, int event, int error, void *user_param)
210 case Terminal::NOTIFY_SE_AVAILABLE :
212 /* add right se reader */
213 // if ((term = ServerResource::getInstance().getTerminal((char *)terminal)) != NULL)
215 // _DBG("terminal : [%s]", (char *)terminal);
217 // term->initialize();
221 // _DBG("unknown terminal : [%s]", (char *)terminal);
226 case Terminal::NOTIFY_SE_NOT_AVAILABLE :
228 /* remove right se reader */
229 // if ((term = ServerResource::getInstance().getTerminal((char *)terminal)) != NULL)
231 // _DBG("terminal : [%s]", (char *)terminal);
237 // _DBG("unknown terminal : [%s]", (char *)terminal);
247 } /* namespace smartcard_service_api */