Add manifest for appinfo
[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 "Message.h"
30 #include "TerminalInterface.h"
31 #include "ServerSEService.h"
32 #include "ServerIPC.h"
33 #include "ServerResource.h"
34
35 namespace smartcard_service_api
36 {
37 #define OMAPI_SE_PATH "/usr/lib/se"
38
39         ServerSEService::ServerSEService():SEServiceHelper()
40         {
41         }
42
43         ServerSEService::~ServerSEService()
44         {
45         }
46
47         ServerSEService &ServerSEService::getInstance()
48         {
49                 static ServerSEService seService;
50
51                 return seService;
52         }
53
54         Terminal *ServerSEService::createInstance(void *library)
55         {
56                 Terminal *terminal = NULL;
57                 terminal_create_instance_fn createInstance = NULL;
58
59                 /* create se instance */
60                 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
61                 if (createInstance != NULL)
62                 {
63                         terminal = (Terminal *)createInstance();
64                         if (terminal != NULL)
65                         {
66                                 _DBG("terminal [%p]", terminal);
67                         }
68                         else
69                         {
70                                 _ERR("terminal is null");
71                         }
72                 }
73                 else
74                 {
75                         _ERR("create_instance is null [%d]", errno);
76                 }
77
78                 return terminal;
79         }
80
81         bool ServerSEService::appendSELibrary(char *library)
82         {
83                 void *libHandle = NULL;
84                 bool result = false;
85
86                 libHandle = dlopen(library, RTLD_LAZY);
87                 if (libHandle != NULL)
88                 {
89                         Terminal *terminal = NULL;
90
91                         terminal = createInstance(libHandle);
92                         if (terminal != NULL)
93                         {
94                                 _DBG("SE info : [%s] [%s]", library, terminal->getName());
95
96                                 libraries.push_back(libHandle);
97
98                                 pair<char *, Terminal *> newPair(terminal->getName(), terminal);
99                                 mapTerminals.insert(newPair);
100
101                                 if (terminal->isSecureElementPresence() == true)
102                                 {
103                                         ServerReader *reader = new ServerReader(this, terminal->getName(), terminal);
104                                         if (reader != NULL)
105                                         {
106                                                 _DBG("register success [%s]", terminal->getName());
107
108                                                 readers.push_back(reader);
109                                         }
110                                         else
111                                         {
112                                                 _ERR("ServerReader alloc failed [%s]", terminal->getName());
113                                                 /* throw exception */
114                                         }
115                                 }
116                                 else
117                                 {
118                                         _DBG("SE is not ready [%s]", terminal->getName());
119                                 }
120
121                                 result = true;
122                         }
123                         else
124                         {
125                                 _ERR("createInstance failed [%s]", library);
126
127                                 dlclose(libHandle);
128                         }
129                 }
130                 else
131                 {
132                         _ERR("it is not se file [%s] [%d]", library, errno);
133                 }
134
135                 return result;
136         }
137
138         int ServerSEService::openSELibraries()
139         {
140                 int result;
141                 void *libHandle;
142                 DIR *dir = NULL;
143                 struct dirent *entry = NULL;
144
145                 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
146                 {
147                         while ((entry = readdir(dir)) != NULL)
148                         {
149                                 if (strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
150                                 {
151                                         char fullPath[1024] = { 0, };
152
153                                         /* need additional name rule :) */
154                                         /* open each files */
155                                         libHandle = NULL;
156
157                                         snprintf(fullPath, sizeof(fullPath), "%s/%s", OMAPI_SE_PATH, entry->d_name);
158
159                                         SCARD_DEBUG("se name [%s]", fullPath);
160
161                                         result = appendSELibrary(fullPath);
162                                 }
163                         }
164
165                         closedir(dir);
166
167                         result = 0;
168                 }
169                 else
170                 {
171                         result = -1;
172                 }
173
174                 return result;
175         }
176
177         void ServerSEService::closeSELibraries()
178         {
179                 if (libraries.size() > 0)
180                 {
181                         size_t i;
182
183                         for (i = 0; i < libraries.size(); i++)
184                         {
185                                 if (libraries[i] != NULL)
186                                         dlclose(libraries[i]);
187                         }
188                 }
189         }
190
191         bool ServerSEService::dispatcherCallback(void *message, int socket)
192         {
193 #ifndef USE_GDBUS
194                 int count;
195                 ByteArray info;
196                 Message *msg = (Message *)message;
197                 Message response(*msg);
198                 ServerResource &resource = ServerResource::getInstance();
199                 ServiceInstance *service;
200
201                 if ((service = resource.createService(socket)) != NULL)
202                 {
203                         _ERR("client added : pid [%d]", msg->error);
204
205                         response.error = SCARD_ERROR_OK;
206                         response.param2 = service->getHandle();
207
208                         if ((count = resource.getReadersInformation(info)) > 0)
209                         {
210                                 response.param1 = count;
211                                 response.data = info;
212                         }
213                         else
214                         {
215                                 _DBG("no secure elements");
216                                 response.param1 = 0;
217                         }
218                 }
219                 else
220                 {
221                         _ERR("createClient failed");
222
223                         response.error = SCARD_ERROR_OUT_OF_MEMORY;
224                 }
225
226                 /* response to client */
227                 ServerIPC::getInstance()->sendMessage(socket, &response);
228 #endif
229                 return false;
230         }
231
232         void ServerSEService::terminalCallback(void *terminal, int event, int error, void *user_param)
233         {
234                 Message msg;
235 //              Terminal *term = NULL;
236
237                 switch (event)
238                 {
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);
244
245                         ServerResource::getInstance().sendMessageToAllClients(msg);
246                         break;
247
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);
253
254                         ServerResource::getInstance().sendMessageToAllClients(msg);
255                         break;
256
257                 default :
258                         break;
259                 }
260         }
261
262 } /* namespace smartcard_service_api */