Skip to content

Commit 73193e8

Browse files
committed
Last fixes + Suggestions
1 parent 311acd1 commit 73193e8

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/main/fc/fc_msp.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,27 +4201,32 @@ static void readMspSimulatorValues(sbuf_t *src, const int dataSize, const uint8_
42014201

42024202
static mspResult_e mspProcessSimulatorCommand(sbuf_t *dst, sbuf_t *src, const int dataSize)
42034203
{
4204+
if (dataSize < 2) {
4205+
return MSP_RESULT_ERROR;
4206+
}
4207+
42044208
const uint8_t simMspVersion = sbufReadU8(src); // Get the Simulator MSP version
4205-
// Check the MSP version of simulator
42064209
if (simMspVersion != SIMULATOR_MSP_VERSION_2 && simMspVersion != SIMULATOR_MSP_VERSION_3) {
42074210
return MSP_RESULT_ERROR;
42084211
}
4209-
4210-
// Backward compatibility for HITL Plugin 1.X
4211-
simulatorData.flags = sbufReadU8(src);
42124212

42134213
if (simMspVersion == SIMULATOR_MSP_VERSION_3) {
4214-
simulatorData.flags |= ((uint16_t)sbufReadU8(src)) << 8;
4214+
if (dataSize < 3) {
4215+
return MSP_RESULT_ERROR;
4216+
}
4217+
simulatorData.flags = sbufReadU16(src);
4218+
} else {
4219+
simulatorData.flags = sbufReadU8(src);
42154220
}
4216-
4217-
// In case of SITL mode, we do not read any input from the simulator (done vis DREFs)
4221+
4222+
const int remainingPayload = (int)sbufBytesRemaining(src);
4223+
42184224
if (!SIMULATOR_HAS_OPTION(HITL_SITL_MODE)) {
4219-
// Check if simulator is disabled and was previously enabled (flags != 0)
42204225
if (!SIMULATOR_HAS_OPTION(HITL_ENABLE) && simulatorData.flags) {
42214226
fcReboot(false);
42224227
return MSP_RESULT_NO_REPLY;
42234228
} else {
4224-
readMspSimulatorValues(src, dataSize, simMspVersion);
4229+
readMspSimulatorValues(src, remainingPayload, simMspVersion);
42254230
}
42264231
}
42274232

src/main/target/SITL/sim/xplane.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static bool receiveSingleDref(dref_t in_dref, float* value)
303303
return false;
304304
}
305305

306-
if (strncmp((char*)buf, "RREF", 4) != 0) {
306+
if (recvLen < 5 || strncmp((char*)buf, "RREF", 4) != 0) {
307307
return false;
308308
}
309309

@@ -572,12 +572,12 @@ static void exchangeDataWithXPlane(void)
572572
);
573573

574574
if (inavXitlDrefVersion >= XITL_DREF_VERSION) {
575-
if (rangefinderAltitude == 0xff) {
575+
if (rangefinderAltitude == 0xffff) {
576576
fakeRangefindersSetData(-1);
577577
} else {
578578
fakeRangefindersSetData(rangefinderAltitude);
579579
}
580-
} else if (inavXitlDrefVersion < XITL_DREF_VERSION) {
580+
} else {
581581
// Use AGL from X-Plane as rangefinder input
582582
const int32_t altitideOverGround = (int32_t)roundf(agl * 100);
583583
if (altitideOverGround > 0 &&
@@ -625,7 +625,7 @@ static void exchangeDataWithXPlane(void)
625625
constrainToInt16(magY * 1024.0f),
626626
constrainToInt16(magZ * 1024.0f)
627627
);
628-
} else if (inavXitlDrefVersion >= XITL_DREF_VERSION) {
628+
} else {
629629
fakeBattSensorSetVbat(16.8f * 100);
630630

631631
fpQuaternion_t quat;
@@ -653,7 +653,6 @@ static void* listenWorker(void* arg)
653653
switch (connectionState) {
654654
case CONNECTING:
655655

656-
657656
// First register all DREFs with 0 freq to clear previous ones
658657
registerCommonDrefs(0);
659658
registerInavXitlDrefs(0);

0 commit comments

Comments
 (0)