AP_Scripting: introduce serial device simulation support

Allows a script to simulate a device attached via any serial protocol.
The script can read and write data and have it handled according to the
protocol as if exchanged over a serial port. The script can then do
protocol translation, data filtering and validation,
hardware-in-the-loop simulation, experimentation, etc., especially in
combination with the scripting protocol which lets the script itself
handle an attached device and so interpose any communication.
This commit is contained in:
Thomas Watson authored and Peter Barker committed 2024-06-27 12:00:18 +10:00
1 parent 4df2a1bce7
commit a077e4a3ed
10 files changed
+358 -9

No files matched your search

@@ -1,15 +1,27 @@
/*
generic object to allow a script to use a serial stream
generic object to allow a script to use a serial driver stream from both
driver and device perspectives
*/
#include "AP_Scripting_config.h"
#include "AP_Scripting.h"
#include "AP_Scripting_SerialAccess.h"
#if AP_SCRIPTING_ENABLED
#if AP_SCRIPTING_SERIALDEVICE_ENABLED
#define check_is_device_port() (is_device_port)
#define ON_DEVICE_PORT(func, ...) (((AP_Scripting_SerialDevice::Port*)stream)->device_##func (__VA_ARGS__))
#else
#define check_is_device_port() (false)
#define ON_DEVICE_PORT(...) (0) // not executed
#endif
void AP_Scripting_SerialAccess::begin(uint32_t baud)
{
stream->begin(baud);
if (!check_is_device_port()) {
stream->begin(baud);
}
}
size_t AP_Scripting_SerialAccess::write(uint8_t c)
@@ -19,7 +31,10 @@ size_t AP_Scripting_SerialAccess::write(uint8_t c)
size_t AP_Scripting_SerialAccess::write(const uint8_t *buffer, size_t size)
{
return stream->write(buffer, size);
if (!check_is_device_port()) {
return stream->write(buffer, size);
}
return ON_DEVICE_PORT(write, buffer, size);
}
int16_t AP_Scripting_SerialAccess::read(void)
@@ -33,17 +48,25 @@ int16_t AP_Scripting_SerialAccess::read(void)
ssize_t AP_Scripting_SerialAccess::read(uint8_t* buffer, uint16_t count)
{
return stream->read(buffer, count);
if (!check_is_device_port()) {
return stream->read(buffer, count);
}
return ON_DEVICE_PORT(read, buffer, count);
}
uint32_t AP_Scripting_SerialAccess::available(void)
{
return stream->available();
if (!check_is_device_port()) {
return stream->available();
}
return ON_DEVICE_PORT(available);
}
void AP_Scripting_SerialAccess::set_flow_control(enum AP_HAL::UARTDriver::flow_control fcs)
{
stream->set_flow_control(fcs);
if (!check_is_device_port()) {
stream->set_flow_control(fcs);
}
}
#endif // AP_SCRIPTING_ENABLED