-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholympusportmanager.cpp
More file actions
375 lines (326 loc) · 10.6 KB
/
olympusportmanager.cpp
File metadata and controls
375 lines (326 loc) · 10.6 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "olympusportmanager.h"
extern OlympusPortManager *g_olympusPM;
OlympusPortManager *g_olympusPM = nullptr;
bool loadLibraries(QLibrary &pm,
QLibrary &pd,
QLibrary &fsi,
QLibrary >)
{
int count = 0;
if (pm.load()) count++;
if (pd.load()) count++;
if (fsi.load()) count++;
if (gt.load()) count++;
if (count < 4) {
qCritical() << "SDK Loading Error:\n"
<< "Please check that the following files are in the installation directory:\n"
<< " - msl_pm.dll\n"
<< " - msl_pd_1394.dll\n"
<< " - fsi1394.dll\n"
<< " - gt_log.dll\n"
<< " - gtlib.config\n"
<< "If any of these files are missing, please locate them in the IX3 SDK.";
return false;
}
qDebug() << "[PM] Libraries were loaded";
return true;
}
bool loadFunctionPointers(QLibrary &pm,
ptr_Initialize &ptr_init,
ptr_EnumInterface &ptr_enumIf,
ptr_GetInterfaceInfo &ptr_getInfo,
ptr_OpenInterface &ptr_openIf,
ptr_SendCommand &ptr_sendCmd,
ptr_RegisterCallback &ptr_reCb,
ptr_CloseInterface &ptr_closeIf)
{
ptr_init = (ptr_Initialize)pm.resolve("MSL_PM_Initialize");
ptr_enumIf = (ptr_EnumInterface)pm.resolve("MSL_PM_EnumInterface");
ptr_getInfo = (ptr_GetInterfaceInfo)pm.resolve("MSL_PM_GetInterfaceInfo");
ptr_openIf = (ptr_OpenInterface)pm.resolve("MSL_PM_OpenInterface");
ptr_sendCmd = (ptr_SendCommand)pm.resolve("MSL_PM_SendCommand");
ptr_reCb = (ptr_RegisterCallback)pm.resolve("MSL_PM_RegisterCallback");
ptr_closeIf = (ptr_CloseInterface)pm.resolve("MSL_PM_CloseInterface");
if (!ptr_init || !ptr_getInfo || !ptr_enumIf
|| !ptr_openIf || !ptr_sendCmd
|| !ptr_reCb || !ptr_closeIf) {
qCritical() << "SDK Loading Error:\n"
<< "Please confirm that the following DLLs are intact.\n"
<< " - msl_pm.dll\n"
<< " - msl_pd_1394.dll\n"
<< " - fsi1394.dll\n"
<< " - gt_log.dll";
return false;
}
qDebug() << "[PM] Function pointers were loaded";
return true;
}
// COMMAND: call back entry from SDK port manager
int CALLBACK CommandCallback(ULONG MsgId, ULONG wParam, ULONG lParam,
PVOID pv, PVOID pContext, PVOID pCaller)
{
UNREFERENCED_PARAMETER(MsgId);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
UNREFERENCED_PARAMETER(pContext);
UNREFERENCED_PARAMETER(pCaller);
UNREFERENCED_PARAMETER(pv);
OlympusPortManager *olympusPM = (OlympusPortManager *)pContext;
if (olympusPM) {
// Extract string from struct
QString response = QString(QLatin1String((char *)olympusPM->m_Cmd.m_Rsp));
qDebug() << "[PM] Response < " << response;
if (response.contains("L +")) {
if (olympusPM->pendingLog) {
olympusPM->logged = !olympusPM->logged;
qDebug() << "[PM] Internal Logged Flag: " << olympusPM->isLogged();
olympusPM->pendingLog = false;
}
}
if (olympusPM->pendingMessage.socket) {
olympusPM->pendingMessage.response = response;
olympusPM->sendResponse();
}
olympusPM->waitingResponse = false;
}
else {
qCritical() << "[PM] The callback pointer is invalid, program is quiting now!";
g_olympusPM->pendingMessage.response = "Error:-8071:IX3-TPC is not responding. "
"Please make sure that IX3-TPC is ready.";
g_olympusPM->sendResponse();
g_olympusPM->closeInterface();
emit g_olympusPM->emergencyStop();
}
return 0;
}
// NOTIFICATION: call back entry from SDK port manager
int CALLBACK NotifyCallback(ULONG MsgId, ULONG wParam, ULONG lParam,
PVOID pv, PVOID pContext, PVOID pCaller)
{
UNREFERENCED_PARAMETER(MsgId);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
UNREFERENCED_PARAMETER(pContext);
UNREFERENCED_PARAMETER(pCaller);
UNREFERENCED_PARAMETER(pv);
char *notify = (char *)pv;
OlympusPortManager *olympusPM = (OlympusPortManager *)pContext;
QString rsp(notify);
rsp.remove("\r\n", Qt::CaseInsensitive);
qDebug() << "[PM] Notify < " << rsp;
emit olympusPM->sendNotify(rsp);
return 0;
}
// ERROR NOTIFICATON: call back entry from SDK port manager
int CALLBACK ErrorCallback(ULONG MsgId, ULONG wParam, ULONG lParam,
PVOID pv, PVOID pContext, PVOID pCaller)
{
UNREFERENCED_PARAMETER(MsgId);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
UNREFERENCED_PARAMETER(pContext);
UNREFERENCED_PARAMETER(pCaller);
UNREFERENCED_PARAMETER(pv);
return 0;
}
bool OlympusPortManager::sendCommand(QString command)
{
if (waitingResponse) {
QTimer::singleShot(50, this, [this, command]() {
this->sendCommand(command);
});
return false;
}
qDebug() << "[PM] Command > " << command;
waitingResponse = true;
if (command.contains("L ")) {
pendingLog = true;
}
// command initiate
int len;
memset(&m_Cmd, 0x00, sizeof(MDK_MSL_CMD));
// add a new line to command string
command.append("\r\n");
len = command.length();
// QString to BYTE( unsigned char* )
QByteArray tmp = command.toLatin1();
// write to memory
memcpy(m_Cmd.m_Cmd, tmp.data(), len);
// set parameters for command
// copy from sample source code
m_Cmd.m_CmdSize = 0;
m_Cmd.m_Callback = (void *)CommandCallback;
m_Cmd.m_Context = NULL; // this pointer passed by pv
m_Cmd.m_Timeout = 10000; // (ms)
m_Cmd.m_Sync = FALSE;
m_Cmd.m_Command = TRUE; // TRUE: Command , FALSE: it means QUERY form ('?').
// send command
return this->ptr_sendCmd(this->pInterface, &m_Cmd);
}
OlympusPortManager::OlympusPortManager(QObject *parent)
: QObject{parent}
{
g_olympusPM = this;
pInterface = nullptr;
logged = false;
clearPendingMessage();
QObject::connect(this, SIGNAL(emergencyStop()), this, SLOT(receiveEmergencyStop()));
}
OlympusPortManager::~OlympusPortManager()
{
g_olympusPM = nullptr;
qDebug() << "[PM] The destructor is called.";
}
bool OlympusPortManager::initialize()
{
QLibrary pm("msl_pm.dll");
QLibrary pd("msl_pd_1394.dll");
QLibrary fsi("fsi1394.dll");
QLibrary gt("gt_log.dll");
// If failed to load libraries
if (!loadLibraries(pm, pd, fsi, gt)) return false;
// If failed to load function pointers
if (!loadFunctionPointers(pm, ptr_init,
ptr_enumIf,
ptr_getInfo,
ptr_openIf,
ptr_sendCmd,
ptr_reCb,
ptr_closeIf)) return false;
ptr_init();
return true;
}
bool OlympusPortManager::isConnected()
{
return pInterface != nullptr;
}
bool OlympusPortManager::isLogged()
{
return logged;
}
bool OlympusPortManager::isBusy()
{
return waitingResponse;
}
void OlympusPortManager::waitWithoutBlocking(int time)
{
QEventLoop loop;
QTimer::singleShot(time, &loop, SLOT(quit())); // time unit: msec
loop.exec();
}
int OlympusPortManager::enumerateInterface()
{
if (pInterface) return 0;
numInterface = ptr_enumIf();
return numInterface;
}
bool OlympusPortManager::openInterface(int interfaceIndex)
{
if (interfaceIndex+1 > numInterface) {
// if connected
qWarning() << "The interface" << interfaceIndex
<< "is not within the range" << numInterface-1;
return false;
}
else {
// Get the interface pointer
if (pInterface) {
qWarning() << "Request to open a new interface before close the older";
return false;
}
else {
ptr_getInfo(interfaceIndex, &pInterface);
// Open interface
if (ptr_openIf(pInterface)) {
// Register callback functions
if (ptr_reCb(this->pInterface, CommandCallback,
NotifyCallback, ErrorCallback, this)) {
return true;
}
else {
closeInterface();
qWarning() << "Could not register callbacks on interface" << interfaceIndex;
return false;
}
}
else {
pInterface = nullptr;
qWarning() << "Could not open interface" << interfaceIndex;
return false;
}
}
}
}
void OlympusPortManager::closeInterface()
{
if (pInterface) {
if (logged && !pendingLog) {
pendingLog = true;
sendCommand("L 0,0");
waitWithoutBlocking(100);
closeInterface();
}
else if (pendingLog) {
waitWithoutBlocking(100);
closeInterface();
}
else {
ptr_closeIf(pInterface);
pInterface = nullptr;
}
}
}
void OlympusPortManager::receiveRequest(MessageSocket ms)
{
if (waitingResponse) {
QTimer::singleShot(50, this, [this, ms]() {
this->receiveRequest(ms);
});
}
QString command = ms.request.trimmed();
pendingMessage = ms;
if (pInterface != nullptr) {
sendCommand(command);
}
else {
pendingMessage.response = "Error:-8070:No interface is opened.";
sendResponse();
}
}
void OlympusPortManager::sendResponse()
{
if (pendingMessage.socket) {
emit sendMessageSocket(pendingMessage);
}
else {
qWarning() << "PM Warning: MessageSocket do not have a correct socket";
}
clearPendingMessage();
}
void OlympusPortManager::clearPendingMessage()
{
pendingMessage.request = QString();
pendingMessage.response = QString();
pendingMessage.socket = nullptr;
}
void OlympusPortManager::logIn()
{
if (!logged) {
pendingLog = true;
sendCommand("L 1,0");
}
}
void OlympusPortManager::logOut()
{
if (logged) {
pendingLog = true;
sendCommand("L 0,0");
}
}
void OlympusPortManager::receiveEmergencyStop()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
std::cout << "Press any key to exit..." << std::endl;
_getch();
emit sendGlobalQuit();
}