return clientDispatcher;
}
- bool ClientDispatcher::addSEService(void *context, SEService *service)
+ bool ClientDispatcher::addSEService(unsigned int handle, SEService *service)
{
bool result = true;
- map<void *, SEService *>::iterator item;
+ map<unsigned int, SEService *>::iterator item;
_BEGIN();
- if ((item = mapSESerivces.find(context)) == mapSESerivces.end())
+ if ((item = mapSESerivces.find(handle)) == mapSESerivces.end())
{
- mapSESerivces.insert(make_pair(context, service));
+ mapSESerivces.insert(make_pair(handle, service));
}
else
{
- _DBG("SEService [%p] exists", context);
+ _DBG("SEService [%p] exists", handle);
}
_END();
return result;
}
- void ClientDispatcher::removeSEService(void *context)
+ void ClientDispatcher::removeSEService(unsigned int handle)
{
- map<void *, SEService *>::iterator item;
+ map<unsigned int, SEService *>::iterator item;
_BEGIN();
- if ((item = mapSESerivces.find(context)) != mapSESerivces.end())
+ if ((item = mapSESerivces.find(handle)) != mapSESerivces.end())
{
mapSESerivces.erase(item);
}
case Message::MSG_NOTIFY_SE_INSERTED :
case Message::MSG_NOTIFY_SE_REMOVED :
{
- map<void *, SEService *>::iterator item;
+ map<unsigned int, SEService *>::iterator item;
for (item = mapSESerivces.begin(); item != mapSESerivces.end(); item++)
{
case Message::MSG_OPERATION_RELEASE_CLIENT :
{
- map<void *, SEService *>::iterator item;
+ map<unsigned int, SEService *>::iterator item;
for (item = mapSESerivces.begin(); item != mapSESerivces.end(); item++)
{
namespace smartcard_service_api
{
- SEService::SEService() :
- SEServiceHelper()
+ SEService::SEService() : SEServiceHelper(),
+ handle(-1), context(NULL),
+ handler(NULL), listener(NULL)
{
- this->context = NULL;
- this->handler = NULL;
- this->listener = NULL;
- connected = false;
}
SEService::SEService(void *user_data, serviceConnected handler)
throw(ErrorIO &, ErrorIllegalParameter &) :
- SEServiceHelper()
+ SEServiceHelper(), handle(-1),
+ listener(NULL)
{
- this->listener = NULL;
- connected = false;
-
initialize(user_data, handler);
}
SEService::SEService(void *user_data, SEServiceListener *listener)
throw(ErrorIO &, ErrorIllegalParameter &) :
- SEServiceHelper()
+ SEServiceHelper(), handle(-1),
+ handler(NULL)
{
- this->handler = NULL;
- connected = false;
-
initialize(user_data, listener);
}
Message msg;
msg.message = Message::MSG_REQUEST_SHUTDOWN;
+ msg.param1 = (unsigned long)handle;
msg.error = (unsigned long)this; /* using error to context */
msg.caller = (void *)this;
msg.callback = (void *)NULL;
Message msg;
msg.message = Message::MSG_REQUEST_SHUTDOWN;
+ msg.param1 = (unsigned long)handle;
msg.error = (unsigned long)this; /* using error to context */
msg.caller = (void *)this;
msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
rv = waitTimedCondition(0);
if (rv == 0)
{
- ClientDispatcher::getInstance().removeSEService(context);
+ ClientDispatcher::getInstance().removeSEService(handle);
connected = false;
}
return result;
}
- clientDispatcher->addSEService(context, this);
-
{
/* send message to load se */
Message msg;
_INFO("[MSG_REQUEST_READERS]");
service->connected = true;
+ service->handle = (unsigned int)msg->param2;
+
+ ClientDispatcher::getInstance().addSEService(service->handle, service);
/* parse message data */
service->parseReaderInformation(msg->param1, msg->data);
{
service->listener->errorHandler(service, msg->error, service->context);
- ClientDispatcher::getInstance().removeSEService(service->context);
+ ClientDispatcher::getInstance().removeSEService(service->handle);
service->connected = false;
}
else
class ClientDispatcher: public DispatcherHelper
{
private:
- map<void *, SEService *> mapSESerivces;
+ map<unsigned int, SEService *> mapSESerivces;
ClientDispatcher();
~ClientDispatcher();
public:
static ClientDispatcher &getInstance();
- bool addSEService(void *context, SEService *service);
- void removeSEService(void *context);
+ bool addSEService(unsigned int handle, SEService *service);
+ void removeSEService(unsigned int handle);
};
} /* namespace open_mobile_api */
{
typedef void (*serviceConnected)(SEServiceHelper *service, void *context);
- class SEService: public SEServiceHelper
+ class SEService : public SEServiceHelper
{
private:
- int pid;
+ unsigned int handle;
void *context;
serviceConnected handler;
SEServiceListener *listener;
this->pid = pid;
}
- ServiceInstance *ClientInstance::createService(unsigned int context)
+ ServiceInstance *ClientInstance::createService()
{
ServiceInstance *result = NULL;
- if ((result = getService(context)) == NULL)
+ result = new ServiceInstance(this);
+ if (result != NULL)
{
- result = new ServiceInstance(this, context);
- if (result != NULL)
- {
- mapServices.insert(make_pair(context, result));
- }
- else
- {
- _ERR("alloc failed");
- }
+ mapServices.insert(make_pair(result->getHandle(), result));
}
else
{
- _ERR("service already exist [%d]", context);
+ _ERR("alloc failed");
}
return result;
}
- ServiceInstance *ClientInstance::getService(unsigned int context)
+ ServiceInstance *ClientInstance::getService(unsigned int handle)
{
ServiceInstance *result = NULL;
map<unsigned int, ServiceInstance *>::iterator item;
- if ((item = mapServices.find(context)) != mapServices.end())
+ if ((item = mapServices.find(handle)) != mapServices.end())
{
result = item->second;
}
return result;
}
- void ClientInstance::removeService(unsigned int context)
+ void ClientInstance::removeService(unsigned int handle)
{
map<unsigned int, ServiceInstance *>::iterator item;
- if ((item = mapServices.find(context)) != mapServices.end())
+ if ((item = mapServices.find(handle)) != mapServices.end())
{
delete item->second;
mapServices.erase(item);
Message response(*msg);
ByteArray info;
ClientInstance *instance = NULL;
+ ServiceInstance *service = NULL;
response.param1 = 0;
response.param2 = 0;
}
/* create service */
- if (resource->createService(socket, (unsigned int)msg->userParam) != NULL)
+ if ((service = resource->createService(socket)) != NULL)
{
response.error = SCARD_ERROR_OK;
+ response.param2 = service->getHandle();
if ((count = resource->getReadersInformation(info)) > 0)
{
response.error = SCARD_ERROR_OK;
- resource->removeService(socket, msg->error/* service context */);
+ resource->removeService(socket, msg->param1);
/* response to client */
ServerIPC::getInstance()->sendMessage(socket, &response);
mapClients.clear();
}
- ServiceInstance *ServerResource::createService(int socket, unsigned int context)
+ ServiceInstance *ServerResource::createService(int socket)
{
ServiceInstance *result = NULL;
ClientInstance *instance = NULL;
if ((instance = getClient(socket)) != NULL)
{
- if ((result = instance->getService(context)) == NULL)
+ if ((result = instance->createService()) == NULL)
{
- if ((result = instance->createService(context)) == NULL)
- {
- _ERR("ClientInstance::createService failed [%d] [%d]", socket, context);
- }
+ _ERR("ClientInstance::createService failed [%d]", socket);
}
}
else
return result;
}
- ServiceInstance *ServerResource::getService(int socket, unsigned int context)
+ ServiceInstance *ServerResource::getService(int socket, unsigned int handle)
{
ServiceInstance *result = NULL;
ClientInstance *instance = NULL;
if ((instance = getClient(socket)) != NULL)
{
- result = instance->getService(context);
+ result = instance->getService(handle);
}
else
{
return result;
}
- void ServerResource::removeService(int socket, unsigned int context)
+ void ServerResource::removeService(int socket, unsigned int handle)
{
ClientInstance *instance = NULL;
if ((instance = getClient(socket)) != NULL)
{
- instance->removeService(context);
+ instance->removeService(handle);
}
else
{
return result;
}
- unsigned int ServerResource::createSession(int socket, unsigned int context, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
+ unsigned int ServerResource::createSession(int socket, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
{
unsigned int result = -1;
Terminal *temp = NULL;
ServiceInstance *instance = NULL;
- if ((instance = getService(socket, context)) != NULL)
+ if ((instance = getService(socket, handle)) != NULL)
{
if ((temp = getTerminalByReaderID(readerID)) != NULL)
{
}
else
{
- _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+ _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
}
return result;
}
- ServerSession *ServerResource::getSession(int socket, unsigned int context, unsigned int sessionID)
+ ServerSession *ServerResource::getSession(int socket, unsigned int handle, unsigned int sessionID)
{
ServerSession *result = NULL;
ServiceInstance *instance = NULL;
- if ((instance = getService(socket, context)) != NULL)
+ if ((instance = getService(socket, handle)) != NULL)
{
result = instance->getSession(sessionID);
}
else
{
- _ERR("Session doesn't exist : socket [%d], context [%d], handle [%d]", socket, context, sessionID);
+ _ERR("Session doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, sessionID);
}
return result;
}
- unsigned int ServerResource::getChannelCount(int socket, unsigned int context, unsigned int sessionID)
+ unsigned int ServerResource::getChannelCount(int socket, unsigned int handle, unsigned int sessionID)
{
unsigned int result = -1;
ServiceInstance *instance = NULL;
- if ((instance = getService(socket, context)) != NULL)
+ if ((instance = getService(socket, handle)) != NULL)
{
result = instance->getChannelCountBySession(sessionID);
}
else
{
- _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+ _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
}
return result;
}
- void ServerResource::removeSession(int socket, unsigned int context, unsigned int sessionID)
+ void ServerResource::removeSession(int socket, unsigned int handle, unsigned int sessionID)
{
ServiceInstance *instance = NULL;
- if ((instance = getService(socket, context)) != NULL)
+ if ((instance = getService(socket, handle)) != NULL)
{
instance->closeSession(sessionID);
}
else
{
- _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+ _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
}
}
return result;
}
- unsigned int ServerResource::createChannel(int socket, unsigned int context, unsigned int sessionID, int channelType, ByteArray aid)
+ unsigned int ServerResource::createChannel(int socket, unsigned int handle, unsigned int sessionID, int channelType, ByteArray aid)
throw(ExceptionBase &)
{
unsigned int result = -1;
ServiceInstance *service = NULL;
- if ((service = getService(socket, context)) != NULL)
+ if ((service = getService(socket, handle)) != NULL)
{
if (service->isVaildSessionHandle(sessionID) == true)
{
}
else
{
- _ERR("getService is failed [%d] [%d]", socket, context);
+ _ERR("getService is failed [%d] [%d]", socket, handle);
throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
}
return result;
}
- Channel *ServerResource::getChannel(int socket, unsigned int context, unsigned int channelID)
+ Channel *ServerResource::getChannel(int socket, unsigned int handle, unsigned int channelID)
{
Channel *result = NULL;
ServiceInstance *instance = NULL;
- if ((instance = getService(socket, context)) != NULL)
+ if ((instance = getService(socket, handle)) != NULL)
{
result = instance->getChannel(channelID);
}
else
{
- _ERR("Channel doesn't exist : socket [%d], context [%d], handle [%d]", socket, context, channelID);
+ _ERR("Channel doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, channelID);
}
return result;
}
- void ServerResource::removeChannel(int socket, unsigned int context, unsigned int channelID)
+ void ServerResource::removeChannel(int socket, unsigned int handle, unsigned int channelID)
{
ServiceInstance *instance = NULL;
- if ((instance = getService(socket, context)) != NULL)
+ if ((instance = getService(socket, handle)) != NULL)
{
instance->closeChannel(channelID);
}
else
{
- _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+ _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
}
}
return (getTerminalByReaderID(reader) != NULL);
}
- bool ServerResource::isValidSessionHandle(int socket, unsigned int context, unsigned int session)
+ bool ServerResource::isValidSessionHandle(int socket, unsigned int handle, unsigned int session)
{
ServiceInstance *instance = NULL;
- return (((instance = getService(socket, context)) != NULL) && (instance->isVaildSessionHandle(session)));
+ return (((instance = getService(socket, handle)) != NULL) && (instance->isVaildSessionHandle(session)));
}
int ServerResource::getReadersInformation(ByteArray &info)
Message *msg = (Message *)message;
Message response(*msg);
ServerResource &resource = ServerResource::getInstance();
+ ServiceInstance *service;
- if (resource.createService(socket, msg->error) != NULL)
+ if ((service = resource.createService(socket)) != NULL)
{
_ERR("client added : pid [%d]", msg->error);
response.error = SCARD_ERROR_OK;
+ response.param2 = service->getHandle();
if ((count = resource.getReadersInformation(info)) > 0)
{
namespace smartcard_service_api
{
+ ServiceInstance::ServiceInstance(ClientInstance *parent) :
+ parent(parent)
+ {
+ handle = IntegerHandle::assignHandle();
+ }
+
+ ServiceInstance::~ServiceInstance()
+ {
+ closeSessions();
+
+ if (handle != IntegerHandle::INVALID_HANDLE) {
+ IntegerHandle::releaseHandle(handle);
+ handle = IntegerHandle::INVALID_HANDLE;
+ }
+ }
+
unsigned int ServiceInstance::openSession(Terminal *terminal, vector<ByteArray> &certHashes, void *caller)
{
unsigned int handle = IntegerHandle::assignHandle();
void setPID(int pid);
inline int getPID() { return pid; }
- ServiceInstance *createService(unsigned int context);
+ ServiceInstance *createService();
ServiceInstance *getService(unsigned int context);
void removeService(unsigned int context);
void removeServices();
void removeClient(int socket);
void removeClients();
- ServiceInstance *createService(int socket, unsigned int context);
- ServiceInstance *getService(int socket, unsigned int context);
- void removeService(int socket, unsigned int context);
+ ServiceInstance *createService(int socket);
+ ServiceInstance *getService(int socket, unsigned int handle);
+ void removeService(int socket, unsigned int handle);
void removeServices(int socket);
- unsigned int createSession(int socket, unsigned int context, unsigned int readerID, vector<ByteArray> &certHashes, void *caller);
- ServerSession *getSession(int socket, unsigned int context, unsigned int sessionID);
- unsigned int getChannelCount(int socket, unsigned int context, unsigned int sessionID);
- void removeSession(int socket, unsigned int context, unsigned int session);
- bool isValidSessionHandle(int socket, unsigned int context, unsigned int sessionID);
+ unsigned int createSession(int socket, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller);
+ ServerSession *getSession(int socket, unsigned int handle, unsigned int sessionID);
+ unsigned int getChannelCount(int socket, unsigned int handle, unsigned int sessionID);
+ void removeSession(int socket, unsigned int handle, unsigned int session);
+ bool isValidSessionHandle(int socket, unsigned int handle, unsigned int sessionID);
- unsigned int createChannel(int socket, unsigned int context,
+ unsigned int createChannel(int socket, unsigned int handle,
unsigned int sessionID, int channelType, ByteArray aid)
throw(ExceptionBase &);
- Channel *getChannel(int socket, unsigned int context, unsigned int channelID);
- void removeChannel(int socket, unsigned int context, unsigned int channelID);
+ Channel *getChannel(int socket, unsigned int handle, unsigned int channelID);
+ void removeChannel(int socket, unsigned int handle, unsigned int channelID);
void addAccessControlList(Terminal *terminal, AccessControlList *acl);
void addAccessControlList(ServerChannel *channel, AccessControlList *acl);
class ServiceInstance
{
private:
- unsigned int context;
+ unsigned int handle;
ClientInstance *parent;
map<unsigned int, pair<ServerSession *, Terminal *> > mapSessions; /* session unique id <-> terminal instance map */
map<unsigned int, pair<unsigned int, ServerChannel *> > mapChannels; /* channel unique id <-> (session unique id, channel instance) map */
public:
- ServiceInstance(ClientInstance *parent, unsigned int context)
- {
- this->parent = parent;
- this->context = context;
- }
- ~ServiceInstance() { closeSessions(); };
+ ServiceInstance(ClientInstance *parent);
+ ~ServiceInstance();
- inline bool operator ==(const unsigned int &context) const { return (this->context == context); }
+ inline unsigned int getHandle() { return handle; }
+
+ inline bool operator ==(const unsigned int &handle) const { return (this->handle == handle); }
inline bool isVaildSessionHandle(unsigned int handle) { return (mapSessions.find(handle) != mapSessions.end()); }
inline bool isVaildChannelHandle(unsigned int handle) { return (mapChannels.find(handle) != mapChannels.end()); }
inline ClientInstance *getParent() { return parent; }
unsigned int openChannel(unsigned int session, int channelNum, ByteArray response = ByteArray::EMPTY);
ServerChannel *getChannel(/*unsigned int session, */unsigned int channel);
unsigned int getChannelCountBySession(unsigned int session);
+
void closeChannel(unsigned int channel);
void closeChannelsBySession(unsigned int session);
void closeChannels();