-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDGI_DCCpp.cpp
319 lines (256 loc) · 9.22 KB
/
DDGI_DCCpp.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
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
//
// Created by Mathieu Andrade on 23/05/2021.
//
#include <windows.h>
#include <iostream>
#include "DCCpp.h"
#include "DDGI_Generic_lib_def.h"
#include "DCCpp_utils.h"
#include "DCCpp_commands.h"
#include "DCCpp_emul.h"
/*======================================================================*/
/* GLOBAL DLL MAIN */
/*======================================================================*/
bool WINAPI DllMain(HINSTANCE instance, DWORD fdwReason, LPVOID lpvReserved)
{
/* The return value is used for successful DLL_PROCESS_ATTACH */
DCCpp::instance = instance;
DCCpp_utils::enableVisualStyles();
return true;
}
/*======================================================================*/
/* EXPORTABLE FUNCTIONS */
/*======================================================================*/
__declspec(dllexport) HANDLE DDGL_Register(bool bEnglish, const char **descString, const char **version, const char *railProtocols[], const char *feedbackProtocols[])
{
*descString = DLL_NAME;
*version = DLL_VERSION;
railProtocols[0] = DLL_PROTOCOL;
railProtocols[1] = nullptr;
feedbackProtocols[0] = nullptr;
return nullptr;
}
__declspec(dllexport) bool DDGL_StartServer(DGI_SERVER_PARAMS *serverParams)
{
return DCCpp::start(*serverParams);
}
__declspec(dllexport) bool DDGL_StopServer(DGI_SERVER_PARAMS *serverParams)
{
DCCpp::stop();
return true;
}
__declspec(dllexport) bool DDGL_DisplayServer(DGI_SERVER_PARAMS *serverParams)
{
std::stringstream msg;
if (DCCpp::emulation) {
msg << "Mode : Emulation " << '\n';
msg << " " << '\n';
msg << "Version : " << DCCpp::version << '\n';
msg << " " << '\n';
msg << "Nombre de modules S88 : " << DCCpp::detectorsModuleCount << '\n';
MessageBox(DCCpp::wnd, msg.str().c_str(), "Interface DCCPP", MB_APPLMODAL | MB_OK | MB_ICONINFORMATION);
}
else if (DCCpp::comPort != INVALID_HANDLE_VALUE || DCCpp::comPort != nullptr || DCCpp::ws != nullptr)
{
msg << "Mode : " << (DCCpp::usbMode ? "USB" : "WIFI") << '\n';
msg << " " << '\n';
msg << (DCCpp::usbMode ? "COM port : " : "Adress IP : ") << (DCCpp::usbMode ? DCCpp::comNumber : DCCpp::ipAddress) << '\n';
msg << " " << '\n';
msg << "Version : " << DCCpp::version << '\n';
msg << " " << '\n';
msg << "Nombre de modules S88 : " << DCCpp::detectorsModuleCount << '\n';
MessageBox(DCCpp::wnd, msg.str().c_str(), "Interface DCCPP", MB_APPLMODAL | MB_OK | MB_ICONINFORMATION);
}
return true;
}
__declspec(dllexport) bool DDGL_GetNextExpectedResponse(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_RCV_DATA genericRcvData)
{
bool result = false;
if (!DCCpp::listOfFeedbackMsg.empty())
{
// Wait until message was copied
result = DCCpp_utils::copyFbMsgToGenericData(genericRcvData, DCCpp::listOfFeedbackMsg);
if (result)
{
// Wait until message was removed
result = DCCpp_utils::removeFeedbackMsg(DCCpp::listOfFeedbackMsg[0], DCCpp::listOfFeedbackMsg);
}
}
return result;
}
__declspec(dllexport) bool DDGL_GetNextUnExpectedMessage(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_RCV_DATA genericRcvData)
{
bool result = false;
if (!DCCpp::listOfUnexpectedFbMsg.empty())
{
// Wait until message was copied
result = DCCpp_utils::copyFbMsgToGenericData(genericRcvData, DCCpp::listOfUnexpectedFbMsg); // 1977903376
if (result)
{
// Wait until message was removed
result = DCCpp_utils::removeFeedbackMsg(DCCpp::listOfUnexpectedFbMsg[0], DCCpp::listOfUnexpectedFbMsg);
}
}
return result;
}
__declspec(dllexport) bool DDGL_ResumeOperations(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
bool success;
int index;
success = DCCpp_commands::buildCommand(POWER_ON);
if (success)
{
DCCpp::initS88();
// Wait for command station before send locos commands
// If locos speed are send immediately, command station not handle it
Sleep(1000);
DCCpp::restartAllLocos();
index = DCCpp_utils::saveFeedbackMsg(genericData->pCmdTag, DCCpp::listOfFeedbackMsg);
DCCpp::handleStandaloneCommands(CMD_OPERATIONS_RESUMED, index);
}
return success;
}
__declspec(dllexport) bool DDGL_PowerOff(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
int index;
bool success;
success = DCCpp_commands::buildCommand(POWER_OFF);
if (success)
{
index = DCCpp_utils::saveFeedbackMsg(genericData->pCmdTag, DCCpp::listOfFeedbackMsg);
DCCpp::handleStandaloneCommands(CMD_POWER_OFF, index);
}
return success;
}
__declspec(dllexport) bool DDGL_EmergencyStop(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
int index;
bool success;
DCCpp::emergencyStopAllLocos();
success = DCCpp_commands::buildCommand(POWER_OFF);
if (success)
{
index = DCCpp_utils::saveFeedbackMsg(genericData->pCmdTag, DCCpp::listOfFeedbackMsg);
DCCpp::handleStandaloneCommands(CMD_EMERGENCY_STOP, index);
}
return success;
}
__declspec(dllexport) bool DDGL_StopLoco(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
genericData->nData[1] = 0;
return DCCpp::setLocoSpeed(genericData);
}
__declspec(dllexport) bool DDGL_CommandStationVersionRequest(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
DCCpp_commands::buildCommand(CMD_STATION_VERSION_REQUEST);
return true;
}
__declspec(dllexport) bool DDGL_CommandStationStatusRequest(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
int index;
index = DCCpp_utils::saveFeedbackMsg(genericData->pCmdTag, DCCpp::listOfFeedbackMsg);
DCCpp::handleStandaloneCommands(CMD_STATION_STATUS, index);
return true;
}
__declspec(dllexport) bool DDGL_DetectorInfoRequest(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
// If emulation mode is activated, we need to handle manually detector states
// else do nothing
if(DCCpp::emulation) {
DCCpp_emul::emulDetectorUpdate(genericData->nAddress);
}
return true;
}
__declspec(dllexport) bool DDGL_SetAccessoryState(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
int index;
bool success = false;
CMD_ARG args;
args[0] = genericData->nAddress; // Accessory address
args[1] = genericData->nData[0] != 0; // Accessory state
success = DCCpp_commands::buildCommand(ACCESSORY_OPERATION, args);
if (success)
{
index = DCCpp_utils::saveFeedbackMsg(genericData->pCmdTag, DCCpp::listOfFeedbackMsg);
DCCpp::handleStandaloneCommands(CMD_ACCESSORY_ACTION, index);
}
return success;
}
__declspec(dllexport) bool DDGL_LocoInfoRequest(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
// return DCCpp::sendLocoInfo(genericData);
// Work in progress
return true;
}
__declspec(dllexport) bool DDGL_SetSpeed(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return DCCpp::setLocoSpeed(genericData);
}
__declspec(dllexport) bool DDGL_SetF0F4(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return DCCpp::setLocoFunction(genericData, 0x1F);
}
__declspec(dllexport) bool DDGL_SetF5F8(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return DCCpp::setLocoFunction(genericData, 0x01E0);
}
__declspec(dllexport) bool DDGL_SetF9F12(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return DCCpp::setLocoFunction(genericData, 0x1E00);
}
/*
* Not handled functions
*
__declspec(dllexport) bool DDGL_SetF0F12(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
__declspec(dllexport) bool DDGL_DisplayError(DGI_SERVER_PARAMS *serverParams)
{
return true;
}
__declspec(dllexport) bool DDGL_DisplayServerStatus(DGI_SERVER_PARAMS *serverParams)
{
return true;
}
__declspec(dllexport) bool DDGL_EnableErrorDisplay(DGI_SERVER_PARAMS *serverParams)
{
return true;
}
__declspec(dllexport) bool DDGL_DisplayStatistics(DGI_SERVER_PARAMS *serverParams)
{
return true;
}
__declspec(dllexport) bool DDGL_DisplayRcvBuffer(DGI_SERVER_PARAMS *serverParams)
{
return true;
}
__declspec(dllexport) bool DDGL_DisplayCmdFifo(DGI_SERVER_PARAMS *serverParams)
{
return true;
}
__declspec(dllexport) bool DDGL_EnterServiceMode(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
__declspec(dllexport) bool DDGL_DirectCVRead(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
__declspec(dllexport) bool DDGL_ServiceModeResultRequest(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
__declspec(dllexport) bool DDGL_DirectCVWrite(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
__declspec(dllexport) bool DDGL_ExitServiceMode(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
__declspec(dllexport) bool DDGL_AccessoryInfoRequest(pDGI_SERVER_PARAMS serverParams, pDGI_GENERIC_DATA genericData)
{
return true;
}
*/