a80d54ca28fd08aa6d6cbdfa03fbf087e3f7221f
[platform/core/connectivity/smartcard-service.git] / server / ServerSEService.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
18 /* standard library header */
19 #include <stdio.h>
20 #include <string.h>
21 #include <dirent.h>
22 #include <dlfcn.h>
23 #include <errno.h>
24
25 /* SLP library header */
26
27 /* local header */
28 #include "Debug.h"
29 #include "TerminalInterface.h"
30 #include "ServerResource.h"
31 #include "ServerSEService.h"
32
33 namespace smartcard_service_api
34 {
35 #define OMAPI_SE_PATH "/usr/lib/se"
36
37         ServerSEService::ServerSEService():SEServiceHelper()
38         {
39         }
40
41         ServerSEService::~ServerSEService()
42         {
43         }
44
45         ServerSEService &ServerSEService::getInstance()
46         {
47                 static ServerSEService seService;
48
49                 return seService;
50         }
51
52         Terminal *ServerSEService::createInstance(void *library)
53         {
54                 Terminal *terminal = NULL;
55                 terminal_create_instance_fn createInstance = NULL;
56
57                 /* create se instance */
58                 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
59                 if (createInstance != NULL)
60                 {
61                         terminal = (Terminal *)createInstance();
62                         if (terminal != NULL)
63                         {
64                                 _DBG("terminal [%p]", terminal);
65                         }
66                         else
67                         {
68                                 _ERR("terminal is null");
69                         }
70                 }
71                 else
72                 {
73                         _ERR("create_instance is null [%d]", errno);
74                 }
75
76                 return terminal;
77         }
78
79         bool ServerSEService::appendSELibrary(char *library)
80         {
81                 void *libHandle = NULL;
82                 bool result = false;
83
84                 libHandle = dlopen(library, RTLD_LAZY);
85                 if (libHandle != NULL)
86                 {
87                         Terminal *terminal = NULL;
88
89                         terminal = createInstance(libHandle);
90                         if (terminal != NULL)
91                         {
92                                 _DBG("SE info : [%s] [%s]", library, terminal->getName());
93
94                                 libraries.push_back(libHandle);
95
96                                 pair<string, Terminal *> newPair(terminal->getName(), terminal);
97                                 mapTerminals.insert(newPair);
98
99                                 if (terminal->isSecureElementPresence() == true)
100                                 {
101                                         ServerReader *reader = new ServerReader(this, terminal->getName(), terminal);
102                                         if (reader != NULL)
103                                         {
104                                                 _DBG("register success [%s]", terminal->getName());
105
106                                                 readers.push_back(reader);
107                                         }
108                                         else
109                                         {
110                                                 _ERR("ServerReader alloc failed [%s]", terminal->getName());
111                                                 /* throw exception */
112                                         }
113                                 }
114                                 else
115                                 {
116                                         _DBG("SE is not ready [%s]", terminal->getName());
117                                 }
118
119                                 result = true;
120                         }
121                         else
122                         {
123                                 _ERR("createInstance failed [%s]", library);
124
125                                 dlclose(libHandle);
126                         }
127                 }
128                 else
129                 {
130                         _ERR("it is not se file [%s] [%d]", library, errno);
131                 }
132
133                 return result;
134         }
135
136         int ServerSEService::openSELibraries()
137         {
138                 int result;
139                 DIR *dir = NULL;
140                 struct dirent *entry = NULL;
141
142                 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
143                 {
144                         while ((entry = readdir(dir)) != NULL)
145                         {
146                                 if (strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
147                                 {
148                                         char fullPath[1024] = { 0, };
149
150                                         /* need additional name rule :) */
151                                         /* open each files */
152
153                                         snprintf(fullPath, sizeof(fullPath), "%s/%s", OMAPI_SE_PATH, entry->d_name);
154
155                                         SECURE_LOGD("se name [%s]", fullPath);
156
157                                         result = appendSELibrary(fullPath);
158                                 }
159                         }
160
161                         closedir(dir);
162
163                         result = 0;
164                 }
165                 else
166                 {
167                         result = -1;
168                 }
169
170                 return result;
171         }
172
173         void ServerSEService::closeSELibraries()
174         {
175                 if (libraries.size() > 0)
176                 {
177                         size_t i;
178
179                         for (i = 0; i < libraries.size(); i++)
180                         {
181                                 if (libraries[i] != NULL)
182                                         dlclose(libraries[i]);
183                         }
184                 }
185         }
186
187 #if 0
188         bool ServerSEService::isValidReaderHandle(void *handle)
189         {
190                 bool result = false;
191                 size_t i;
192
193                 for (i = 0; i < readers.size(); i++)
194                 {
195                         if ((void *)readers[i] == handle)
196                         {
197                                 result =  true;
198                                 break;
199                         }
200                 }
201
202                 return false;
203         }
204 #endif
205
206         void ServerSEService::terminalCallback(const void *terminal, int event, int error, void *user_param)
207         {
208                 switch (event)
209                 {
210                 case Terminal::NOTIFY_SE_AVAILABLE :
211                         {
212                                 /* add right se reader */
213 //                              if ((term = ServerResource::getInstance().getTerminal((char *)terminal)) != NULL)
214 //                              {
215 //                                      _DBG("terminal : [%s]", (char *)terminal);
216 //
217 //                                      term->initialize();
218 //                              }
219 //                              else
220 //                              {
221 //                                      _DBG("unknown terminal : [%s]", (char *)terminal);
222 //                              }
223                         }
224                         break;
225
226                 case Terminal::NOTIFY_SE_NOT_AVAILABLE :
227                         {
228                                 /* remove right se reader */
229 //                              if ((term = ServerResource::getInstance().getTerminal((char *)terminal)) != NULL)
230 //                              {
231 //                                      _DBG("terminal : [%s]", (char *)terminal);
232 //
233 //                                      term->finalize();
234 //                              }
235 //                              else
236 //                              {
237 //                                      _DBG("unknown terminal : [%s]", (char *)terminal);
238 //                              }
239                         }
240                         break;
241
242                 default :
243                         break;
244                 }
245         }
246
247 } /* namespace smartcard_service_api */