-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatarefsViaUsb.cpp
294 lines (260 loc) · 8.39 KB
/
DatarefsViaUsb.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
#include "DatarefsViaUsb.h"
#ifndef XPLM300
#error This is made to be compiled against the XPLM300 SDK
#endif
std::list<connection_t> connections;
static void Send(HANDLE handle, uint8 data[], uint8 dataLength) {
DWORD dNoOfBytesWritten = 0;
WriteFile(handle, data, dataLength, &dNoOfBytesWritten, NULL);
}
static void SendInit(HANDLE handle) {
uint8 data[1] = { INIT };
Send(handle, data, 1);
}
static void SendDatarefUpdate(HANDLE handle, uint8 datarefIndex, dataref_t *dataref) {
uint8 data[BUFFER_SIZE];
data[0] = UPDATE;
data[1] = datarefIndex;
memcpy(&data[2], &(dataref->value), sizeof(dataref->value));
uint8 dataLength = 2 + sizeof(dataref->value);
Send(handle, data, dataLength);
}
void Connect(LPCSTR portName) {
HANDLE hComm = CreateFile(portName, //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE)
printf("Error in opening serial port\n");
else {
printf("opening serial port successful\n");
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
BOOL Status = GetCommState(hComm, &dcbSerialParams);
dcbSerialParams.BaudRate = CBR_115200; // Setting BaudRate
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
SetCommState(hComm, &dcbSerialParams);
COMMTIMEOUTS timeouts = { 0, //interval timeout. 0 = not used
0, // read multiplier
1, // read constant (milliseconds)
0, // Write multiplier
0 // Write Constant
};
SetCommTimeouts(hComm, &timeouts);
connection_t conn = { portName, hComm, {}, {0}, 0, false };
connections.push_back(conn);
SendInit(conn.handle);
}
}
static void Disconnect() {
std::list<connection_t>::iterator conn = connections.begin();
while (conn != connections.end()) {
CloseHandle(conn->handle);
conn++;
}
connections.clear();
}
static datarefValue_t ReadDatarefValue(dataref_t *dataref) {
switch (dataref->type) {
case INT_4BYTES:
return XPLMGetDatai(dataref->handle);
break;
case FLOAT_4BYTES:
float value = XPLMGetDataf(dataref->handle);
datarefValue_t result = 0;
memcpy((uint8*)&result, (uint8*)&value, sizeof(datarefValue_t));
return result;
break;
}
}
static void WriteDatarefValue(XPLMDataRef handle, datatype_t type, datarefValue_t value) {
if (XPLMCanWriteDataRef(handle)) {
switch (type) {
case INT_4BYTES:
XPLMSetDatai(handle, value);
break;
case FLOAT_4BYTES:
float floatValue = 0.0f;
memcpy((uint8*)&floatValue, (uint8*)&value, sizeof(datarefValue_t));
XPLMSetDataf(handle, floatValue);
break;
}
}
else {
// oops
}
}
static void UpdateDatarefs(connection_t *conn) {
std::list<dataref_t>::iterator dataref = conn->datarefs.begin();
uint8 datarefIndex = 0;
while (dataref != conn->datarefs.end()) {
bool updateRequired = false;
if (dataref->handle == NULL) {
dataref->handle = XPLMFindDataRef(dataref->label);
updateRequired = true;
}
if (dataref->handle != NULL) {
datarefValue_t value = ReadDatarefValue(&(*dataref));
if (value != dataref->value) {
updateRequired = true;
}
if (updateRequired) {
dataref->value = value;
SendDatarefUpdate(conn->handle, datarefIndex, &(*dataref));
}
}
dataref++;
datarefIndex++;
}
}
static sint16 findNullTerminator(uint8* ptr, uint8 maxLength) {
for (uint8 i = 0; i < maxLength; i++) {
if (*ptr == 0) {
return i;
}
ptr++;
}
return -1;
}
static void HandleInput(connection_t *conn) {
DWORD bytesRead = 0;
ReadFile(conn->handle, &(conn->buffer[conn->buffer_usage]), BUFFER_SIZE-conn->buffer_usage, &bytesRead, NULL);
if (bytesRead > 0) {
conn->buffer_usage += bytesRead;
if (!conn->initialized) {
if ((messageFromClient_t)conn->buffer[0] != SUBSCRIBE) {
SendInit(conn->handle);
}
conn->initialized = true;
}
bool continueProcessing = false;
do {
switch ((messageFromClient_t)conn->buffer[0]) {
case SUBSCRIBE:
if(conn->buffer_usage > 3) {
sint16 terminatorIdx = findNullTerminator(&(conn->buffer[2]), conn->buffer_usage - 2);
if (terminatorIdx >= 0) {
dataref_t dataref = {};
dataref.type = (datatype_t)conn->buffer[1];
strncpy(dataref.label, (const char*)&(conn->buffer[2]), min(terminatorIdx + 1, MAX_DATAREF_LABEL_LENGTH));
dataref.handle = XPLMFindDataRef(dataref.label);
if (dataref.handle != NULL) {
dataref.value = ReadDatarefValue(&dataref);
conn->datarefs.push_back(dataref);
SendDatarefUpdate(conn->handle, conn->datarefs.size() - 1, &dataref);
}
memmove(&(conn->buffer), &(conn->buffer[3 + terminatorIdx]), conn->buffer_usage - terminatorIdx - 3);
conn->buffer_usage -= terminatorIdx + 3;
continueProcessing = conn->buffer_usage > 0;
}
else {
//did not yet receive the end of the dataref label (no null terminator)
continueProcessing = false;
}
}
else {
continueProcessing = false;
}
break;
case COMMAND:
case BEGIN_COMMAND:
case END_COMMAND:
if (conn->buffer_usage > 2) {
sint16 terminatorIdx = findNullTerminator(&(conn->buffer[1]), conn->buffer_usage - 1);
if (terminatorIdx >= 0) {
XPLMCommandRef cmdHandle = XPLMFindCommand((const char*)&conn->buffer[1]);
if (cmdHandle != NULL) {
switch ((messageFromClient_t)conn->buffer[0]) {
case COMMAND:
XPLMCommandOnce(cmdHandle);
break;
case BEGIN_COMMAND:
XPLMCommandBegin(cmdHandle);
break;
case END_COMMAND:
XPLMCommandEnd(cmdHandle);
break;
}
}
memmove(&(conn->buffer), &(conn->buffer[2 + terminatorIdx]), conn->buffer_usage - terminatorIdx - 2);
conn->buffer_usage -= terminatorIdx + 2;
continueProcessing = conn->buffer_usage > 0;
}
else {
//did not yet receive the end of the command label (no null terminator)
continueProcessing = false;
}
break;
}
else {
continueProcessing = false;
}
case SET_DATAREF:
if (conn->buffer_usage > 2+sizeof(datarefValue_t)) {
sint16 terminatorIdx = findNullTerminator(&(conn->buffer[2+sizeof(datarefValue_t)]), conn->buffer_usage - 2 - sizeof(datarefValue_t));
if (terminatorIdx >= 0) {
datatype_t type = (datatype_t)conn->buffer[1];
datarefValue_t value = 0;
memcpy((uint8*)&value, &conn->buffer[2], sizeof(datarefValue_t));
XPLMDataRef datarefHandle = XPLMFindDataRef((const char*)&conn->buffer[2 + sizeof(datarefValue_t)]);
if (datarefHandle != NULL) {
WriteDatarefValue(datarefHandle, type, value);
}
memmove(&(conn->buffer), &(conn->buffer[3 + sizeof(datarefValue_t) + terminatorIdx]), conn->buffer_usage - terminatorIdx - 3 - sizeof(datarefValue_t));
conn->buffer_usage -= terminatorIdx + 3 + sizeof(datarefValue_t);
continueProcessing = conn->buffer_usage > 0;
}
else {
//did not yet receive the end of the dataref label (no null terminator)
continueProcessing = false;
}
break;
}
else {
continueProcessing = false;
}
}
} while (continueProcessing);
}
}
PLUGIN_API float MyFlightLoopCallback(
float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop,
int inCounter,
void *inRefcon) {
std::list<connection_t>::iterator conn = connections.begin();
while (conn != connections.end()) {
UpdateDatarefs(&(*conn));
HandleInput(&(*conn));
conn++;
}
return 0.01f;
}
PLUGIN_API int XPluginStart(
char * outName,
char * outSig,
char * outDesc)
{
strcpy(outName, "DatarefsViaUsb");
strcpy(outSig, "DatarefsViaUsb");
strcpy(outDesc, "Plugin to make Datarefs available via an USB Connection");
return true;
}
PLUGIN_API void XPluginStop(void)
{
}
PLUGIN_API void XPluginDisable(void) {
XPLMUnregisterFlightLoopCallback(MyFlightLoopCallback, NULL);
Disconnect();
}
PLUGIN_API int XPluginEnable(void) {
Connect("\\\\.\\COM6"); //TODO: Allow to define connections via GUI
XPLMRegisterFlightLoopCallback(MyFlightLoopCallback, 0.01f, NULL);
return 1;
}
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void * inParam) { }