5e97ce8260a738561988f4d2d538c33817b17f23
[platform/core/connectivity/smartcard-service.git] / server / include / ServerResource.h
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 #ifndef SERVERRESOURCE_H_
18 #define SERVERRESOURCE_H_
19
20 /* standard library header */
21 #ifdef __cplusplus
22 #include <map>
23 #include <vector>
24 #include <set>
25 #endif /* __cplusplus */
26
27 /* SLP library header */
28
29 /* local header */
30 #ifdef __cplusplus
31 #include "Terminal.h"
32 #include "Lock.h"
33 #include "ServerIPC.h"
34 #include "ServerDispatcher.h"
35 #include "ServerReader.h"
36 #include "ServerSession.h"
37 #include "ClientInstance.h"
38 #include "ServiceInstance.h"
39 #endif /* __cplusplus */
40
41 #ifdef __cplusplus
42 using namespace std;
43
44 namespace smartcard_service_api
45 {
46         class IntegerHandle
47         {
48         private:
49                 static unsigned int newHandle;
50                 static set<unsigned int> setHandles;
51                 static PMutex mutexLock;
52
53         public:
54                 static const unsigned int INVALID_HANDLE = (unsigned int)-1;
55
56                 static unsigned int assignHandle();
57                 static void releaseHandle(unsigned int);
58         };
59
60         class ServerResource
61         {
62         private:
63                 /* non-static member */
64                 vector<void *> libraries;
65                 map<unsigned int, Terminal *> mapTerminals; /* terminal unique id <-> terminal instance map */
66                 map<unsigned int, unsigned int> mapReaders; /* reader unique id <-> terminal unique id map */
67                 map<int, ClientInstance *> mapClients; /* client pid <-> client instance map */
68                 map<Terminal *, AccessControlList *> mapACL; /* terminal instance <-> access control instance map */
69                 void *mainLoop;
70                 ServerIPC *serverIPC;
71                 ServerDispatcher *serverDispatcher;
72
73                 ServerResource();
74                 ~ServerResource();
75
76                 Terminal *createInstance(void *library);
77                 bool appendSELibrary(char *library);
78                 void clearSELibraries();
79
80                 static void terminalCallback(void *terminal, int event, int error, void *user_param);
81
82                 int _openLogicalChannel(Terminal *terminal);
83                 int _closeLogicalChannel(Terminal *terminal, int channelNum);
84                 bool _isAuthorizedAccess(ServerChannel *channel, int pid, ByteArray aid, vector<ByteArray> &hashes);
85                 unsigned int _createChannel(Terminal *terminal, ServiceInstance *service, int channelType, unsigned int sessionID, ByteArray aid);
86         public:
87                 /* static member */
88                 static ServerResource &getInstance();
89
90                 /* non-static member */
91                 inline void setMainLoopInstance(void *mainLoop)
92                 {
93                         this->mainLoop = mainLoop;
94                 }
95                 inline void *getMainLoopInstance()
96                 {
97                         return this->mainLoop;
98                 }
99
100                 int loadSecureElements();
101                 void unloadSecureElements();
102
103                 Terminal *getTerminal(unsigned int terminalID);
104                 Terminal *getTerminal(const char *name);
105                 Terminal *getTerminalByReaderID(unsigned int readerID);
106                 unsigned int getTerminalID(const char *name);
107                 int getReadersInformation(ByteArray &info);
108                 bool isValidReaderHandle(unsigned int reader);
109
110                 unsigned int createReader(unsigned int terminalID);
111                 unsigned int getReaderID(const char *name);
112                 void removeReader(unsigned int readerID);
113
114                 bool createClient(void *ioChannel, int socket, int watchID, int state, int pid);
115                 ClientInstance *getClient(int socket);
116                 void setPID(int socket, int pid);
117                 int getClientCount();
118                 void removeClient(int socket);
119                 void removeClients();
120
121                 bool createService(int socket, unsigned int context);
122                 ServiceInstance *getService(int socket, unsigned int context);
123                 void removeService(int socket, unsigned int context);
124                 void removeServices(int socket);
125
126                 unsigned int createSession(int socket, unsigned int context, unsigned int readerID, vector<ByteArray> &certHashes, void *caller);
127                 ServerSession *getSession(int socket, unsigned int context, unsigned int sessionID);
128                 unsigned int getChannelCount(int socket, unsigned int context, unsigned int sessionID);
129                 void removeSession(int socket, unsigned int context, unsigned int session);
130                 bool isValidSessionHandle(int socket, unsigned int context, unsigned int sessionID);
131
132                 unsigned int createChannel(int socket, unsigned int context, unsigned int sessionID, int channelType, ByteArray aid);
133                 Channel *getChannel(int socket, unsigned int context, unsigned int channelID);
134                 void removeChannel(int socket, unsigned int context, unsigned int channelID);
135
136                 AccessControlList *getAccessControlList(Terminal *terminal);
137                 AccessControlList *getAccessControlList(ServerChannel *channel);
138
139                 bool sendMessageToAllClients(Message &msg);
140
141                 friend void terminalCallback(void *terminal, int event, int error, void *user_param);
142         };
143
144 } /* namespace smartcard_service_api */
145 #endif /* __cplusplus */
146
147 /* export C API */
148 #ifdef __cplusplus
149 extern "C"
150 {
151 #endif /* __cplusplus */
152
153 void server_resource_set_main_loop_instance(void *instance);
154
155 #ifdef __cplusplus
156 }
157 #endif /* __cplusplus */
158
159 #endif /* SERVERRESOURCE_H_ */