-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.cpp
114 lines (76 loc) · 2.6 KB
/
watcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <string>
#include "inc/token.h"
#include "inc/ep_pkcs15.h"
#include "inc/watcher.h"
#include "epToken/client_handler.h"
#include "include/cef_v8.h"
#include "inc/ep_thread_ctx.h"
#if defined(WIN32)
#include <windows.h>
#include <process.h>
#endif
namespace epsilon {
CefRefPtr<CefProcessMessage> Msg(const std::string &kMessageName, const char *reader = NULL) {
CefRefPtr<CefProcessMessage> response = CefProcessMessage::Create(kMessageName);
if(reader) {
CefRefPtr<CefDictionaryValue> args = CefDictionaryValue::Create();
args->SetString("reader", reader);
response->GetArgumentList()->SetDictionary(0, args);
}
return response;
}
// Bring in platform-specific definitions.
#if defined(WIN32)
void WatchToken(void *lpParam) {
volatile bool out_ = true;
volatile DWORD sleep_ = 1000;
sc_context *ctx = NULL;
ClientHandler *handler = static_cast<ClientHandler *>(lpParam);
ASSERT(handler != NULL);
while(handler->app_running_) {
if(TokenContext::isTokenPresent(&ctx)) {
ASSERT(ctx != NULL);
if(out_) {
unsigned int evt;
sc_reader *found = NULL;
if(sc_wait_for_event(ctx, SC_EVENT_READER_ATTACHED, &found, &evt, 0, NULL) == 0) {
sc_ctx_detect_readers(ctx);
/* Waiting for a card to be inserted */
if(sc_wait_for_event(ctx, SC_EVENT_CARD_INSERTED, &found, &evt, 0, NULL) == 0) {
/* Card was inserted */
if(evt & SC_EVENT_CARD_INSERTED) {
CefRefPtr<CefBrowser> browser = handler->GetBrowser();
ASSERT(browser.get());
browser->SendProcessMessage(PID_RENDERER, Msg(E_TOKEN_INSERTED, found->name));
sleep_ = 2500;
out_ = false;
}
}
}
}
sc_release_context(ctx);
ctx = NULL;
} else {
if(out_ == false) {
/* Card was removed */
CefRefPtr<CefBrowser> browser = handler->GetBrowser();
ASSERT(browser.get());
browser->SendProcessMessage(PID_RENDERER, Msg(E_TOKEN_REMOVED));
sleep_ = 1000;
out_ = true;
}
}
sleep : Sleep(sleep_);
}
_endthread();
if(handler->m_watcherHnd) {
//CloseHandle(handler->m_watcherHnd);
handler->m_watcherHnd = NULL;
}
//return 0;
}
void TokenWatcher::Init(ClientHandler *handler) {
handler->m_watcherHnd = (void *)_beginthread(WatchToken, 0, (void *)handler);
}
#endif
}