AP_Scripting: clear serial device sim buffers appropriately

Ensures the script won't process data created before it started, and
that the protocol won't process data created after the script stopped.
This commit is contained in:
Thomas Watson 2024-06-01 23:23:25 -05:00 committed by Peter Barker
parent 4cb684e8a9
commit 46e204dd6c
3 changed files with 30 additions and 0 deletions

View File

@ -311,6 +311,11 @@ void AP_Scripting::thread(void) {
GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Scripting: %s", "Unable to allocate memory");
_init_failed = true;
} else {
#if AP_SCRIPTING_SERIALDEVICE_ENABLED
// clear data in serial buffers that the script wasn't ready to
// receive
_serialdevice.clear();
#endif
// run won't return while scripting is still active
lua->run();
@ -345,6 +350,11 @@ void AP_Scripting::thread(void) {
}
}
#endif // AP_NETWORKING_ENABLED
#if AP_SCRIPTING_SERIALDEVICE_ENABLED
// clear data in serial buffers that hasn't been transmitted
_serialdevice.clear();
#endif
// Clear blocked commands
{

View File

@ -35,6 +35,13 @@ void AP_Scripting_SerialDevice::init(void)
}
}
void AP_Scripting_SerialDevice::clear(void)
{
for (auto &p : ports) {
p.clear();
}
}
/*
initialise port
*/
@ -43,6 +50,17 @@ void AP_Scripting_SerialDevice::Port::init(void)
begin(1000000, 0, 0); // assume 1MBaud rate even though it's a bit meaningless
}
void AP_Scripting_SerialDevice::Port::clear(void)
{
WITH_SEMAPHORE(sem);
if (readbuffer) {
readbuffer->clear();
}
if (writebuffer) {
writebuffer->clear();
}
}
size_t AP_Scripting_SerialDevice::Port::device_write(const uint8_t *buffer, size_t size)
{
WITH_SEMAPHORE(sem);

View File

@ -19,12 +19,14 @@ public:
AP_Int8 enable;
void init(void);
void clear(void);
public:
class Port : public AP_SerialManager::RegisteredPort {
public:
friend class AP_Scripting_SerialDevice;
void init(void);
void clear(void);
size_t device_write(const uint8_t *buffer, size_t size);
ssize_t device_read(uint8_t *buffer, uint16_t count);