Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: SCardReconnect #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/pcsclite.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ CardReader.prototype.connect = function(options, cb) {
}
};

CardReader.prototype.reconnect = function(options, cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
}

options = options || {};
options.share_mode = options.share_mode || this.SCARD_SHARE_EXCLUSIVE;
options.protocol = options.protocol || this.SCARD_PROTOCOL_T0 | this.SCARD_PROTOCOL_T1;
if (typeof options.initialization !== 'number') {
options.initialization = this.SCARD_UNPOWER_CARD;
}

this._reconnect(options.share_mode, options.protocol, options.initialization, cb);
};

CardReader.prototype.disconnect = function(disposition, cb) {
if (typeof disposition === 'function') {
cb = disposition;
Expand Down
113 changes: 112 additions & 1 deletion src/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void CardReader::init(Handle<Object> target) {
// Prototype
NanSetPrototypeTemplate(tpl, "get_status", NanNew<FunctionTemplate>(GetStatus));
NanSetPrototypeTemplate(tpl, "_connect", NanNew<FunctionTemplate>(Connect));
NanSetPrototypeTemplate(tpl, "_reconnect", NanNew<FunctionTemplate>(Reconnect));
NanSetPrototypeTemplate(tpl, "_disconnect", NanNew<FunctionTemplate>(Disconnect));
NanSetPrototypeTemplate(tpl, "_transmit", NanNew<FunctionTemplate>(Transmit));
NanSetPrototypeTemplate(tpl, "_control", NanNew<FunctionTemplate>(Control));
Expand Down Expand Up @@ -52,8 +53,8 @@ void CardReader::init(Handle<Object> target) {
// Disconnect disposition
NanSetPrototypeTemplate(tpl, "SCARD_LEAVE_CARD", NanNew(SCARD_LEAVE_CARD));
NanSetPrototypeTemplate(tpl, "SCARD_RESET_CARD", NanNew(SCARD_RESET_CARD));
NanSetPrototypeTemplate(tpl, "SCARD_STATE_CHANGED", NanNew(SCARD_STATE_CHANGED));
NanSetPrototypeTemplate(tpl, "SCARD_UNPOWER_CARD", NanNew(SCARD_UNPOWER_CARD));
NanSetPrototypeTemplate(tpl, "SCARD_EJECT_CARD", NanNew(SCARD_EJECT_CARD));

NanAssignPersistent<Function>(constructor, tpl->GetFunction());
target->Set(NanNew("CardReader"), tpl->GetFunction());
Expand Down Expand Up @@ -151,6 +152,52 @@ NAN_METHOD(CardReader::Connect) {
NanReturnUndefined();
}

NAN_METHOD(CardReader::Reconnect) {

NanScope();

// The second argument is the length of the data to be received
if (!args[0]->IsUint32()) {
return NanThrowError("First argument must be an integer");
}

if (!args[1]->IsUint32()) {
return NanThrowError("Second argument must be an integer");
}

if (!args[2]->IsUint32()) {
return NanThrowError("Third argument must be an integer");
}

if (!args[3]->IsFunction()) {
return NanThrowError("Fourth argument must be a callback function");
}

ReconnectInput* ri = new ReconnectInput();
ri->share_mode = args[0]->Uint32Value();
ri->pref_protocol = args[1]->Uint32Value();
ri->action = args[2]->Uint32Value();
Local<Function> cb = Local<Function>::Cast(args[3]);

// This creates our work request, including the libuv struct.
Baton* baton = new Baton();
baton->request.data = baton;
NanAssignPersistent(baton->callback, cb);
baton->reader = ObjectWrap::Unwrap<CardReader>(args.This());
baton->input = ri;

// Schedule our work request with libuv. Here you can specify the functions
// that should be executed in the threadpool and back in the main thread
// after the threadpool function completed.
int status = uv_queue_work(uv_default_loop(),
&baton->request,
DoReconnect,
reinterpret_cast<uv_after_work_cb>(AfterReconnect));
assert(status == 0);

NanReturnUndefined();
}

NAN_METHOD(CardReader::Disconnect) {

NanScope();
Expand Down Expand Up @@ -447,6 +494,70 @@ void CardReader::AfterConnect(uv_work_t* req, int status) {
delete baton;
}

void CardReader::DoReconnect(uv_work_t* req) {

Baton* baton = static_cast<Baton*>(req->data);
ReconnectInput* ri = static_cast<ReconnectInput*>(baton->input);

DWORD card_protocol;
LONG result = SCARD_S_SUCCESS;
CardReader* obj = baton->reader;

/* Lock mutex */
pthread_mutex_lock(&obj->m_mutex);

/* Reconnect */
if (obj->m_card_handle) {
result = SCardReconnect(obj->m_card_handle,
ri->share_mode,
ri->pref_protocol,
ri->action,
&card_protocol);
}

/* Unlock the mutex */
pthread_mutex_unlock(&obj->m_mutex);

ReconnectResult *rr = new ReconnectResult();
rr->result = result;
if (!result) {
rr->card_protocol = card_protocol;
}

baton->result = rr;
}

void CardReader::AfterReconnect(uv_work_t* req, int status) {

NanScope();
Baton* baton = static_cast<Baton*>(req->data);
ReconnectInput *ri = static_cast<ReconnectInput*>(baton->input);
ReconnectResult *rr = static_cast<ReconnectResult*>(baton->result);

if (rr->result) {
Local<Value> err = NanError(error_msg("SCardReconnect", rr->result).c_str());
// Prepare the parameters for the callback function.
const unsigned argc = 1;
Handle<Value> argv[argc] = { err };
NanCallback(NanNew(baton->callback)).Call(argc, argv);
} else {
NanObjectWrapHandle(baton->reader)->Set(NanNew(connected_symbol), NanTrue());
const unsigned argc = 2;
Handle<Value> argv[argc] = {
NanNull(),
NanNew<Number>(rr->card_protocol)
};

NanCallback(NanNew(baton->callback)).Call(argc, argv);
}

// The callback is a permanent handle, so we have to dispose of it manually.
NanDisposePersistent(baton->callback);
delete ri;
delete rr;
delete baton;
}

void CardReader::DoDisconnect(uv_work_t* req) {

Baton* baton = static_cast<Baton*>(req->data);
Expand Down
14 changes: 14 additions & 0 deletions src/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class CardReader: public node::ObjectWrap {
DWORD card_protocol;
};

struct ReconnectInput {
DWORD share_mode;
DWORD pref_protocol;
DWORD action;
};

struct ReconnectResult {
LONG result;
DWORD card_protocol;
};

struct TransmitInput {
DWORD card_protocol;
LPBYTE in_data;
Expand Down Expand Up @@ -94,6 +105,7 @@ class CardReader: public node::ObjectWrap {
static NAN_METHOD(New);
static NAN_METHOD(GetStatus);
static NAN_METHOD(Connect);
static NAN_METHOD(Reconnect);
static NAN_METHOD(Disconnect);
static NAN_METHOD(Transmit);
static NAN_METHOD(Control);
Expand All @@ -102,12 +114,14 @@ class CardReader: public node::ObjectWrap {
static void HandleReaderStatusChange(uv_async_t *handle, int status);
static void* HandlerFunction(void* arg);
static void DoConnect(uv_work_t* req);
static void DoReconnect(uv_work_t* req);
static void DoDisconnect(uv_work_t* req);
static void DoTransmit(uv_work_t* req);
static void DoControl(uv_work_t* req);
static void CloseCallback(uv_handle_t *handle);

static void AfterConnect(uv_work_t* req, int status);
static void AfterReconnect(uv_work_t* req, int status);
static void AfterDisconnect(uv_work_t* req, int status);
static void AfterTransmit(uv_work_t* req, int status);
static void AfterControl(uv_work_t* req, int status);
Expand Down