AP_Scripting: fix PWMSource deletion crash

This commit is contained in:
Iampete1 2023-02-20 13:59:22 +00:00 committed by Andrew Tridgell
parent 2bb8294685
commit 54a9239e62
6 changed files with 53 additions and 12 deletions

View File

@ -244,6 +244,15 @@ void AP_Scripting::thread(void) {
}
num_i2c_devices = 0;
// clear allocated PWM sources
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_PWM_SOURCE; i++) {
if (_pwm_source[i] != nullptr) {
delete _pwm_source[i];
_pwm_source[i] = nullptr;
}
}
num_pwm_source = 0;
bool cleared = false;
while(true) {
// 1hz check if we should restart

View File

@ -28,6 +28,8 @@
#define SCRIPTING_MAX_NUM_I2C_DEVICE 4
#endif
#define SCRIPTING_MAX_NUM_PWM_SOURCE 4
class AP_Scripting
{
public:
@ -89,6 +91,10 @@ public:
};
ObjectBuffer<struct scripting_mission_cmd> * mission_data;
// PWMSource storage
uint8_t num_pwm_source;
AP_HAL::PWMSource *_pwm_source[SCRIPTING_MAX_NUM_PWM_SOURCE];
private:
bool repl_start(void);

View File

@ -309,24 +309,24 @@ function motor_factor_table_ud:roll(index, value) end
-- desc
---@class PWMSource_ud
local PWMSource_ud = {}
---@class AP_HAL__PWMSource_ud
local AP_HAL__PWMSource_ud = {}
---@return PWMSource_ud
---@return AP_HAL__PWMSource_ud
function PWMSource() end
-- desc
---@return integer
function PWMSource_ud:get_pwm_avg_us() end
function AP_HAL__PWMSource_ud:get_pwm_avg_us() end
-- desc
---@return integer
function PWMSource_ud:get_pwm_us() end
function AP_HAL__PWMSource_ud:get_pwm_us() end
-- desc
---@param pin_number integer
---@return boolean
function PWMSource_ud:set_pin(pin_number) end
function AP_HAL__PWMSource_ud:set_pin(pin_number) end
-- desc

View File

@ -478,10 +478,11 @@ ap_object AP_HAL::AnalogSource method voltage_average float
ap_object AP_HAL::AnalogSource method voltage_latest float
ap_object AP_HAL::AnalogSource method voltage_average_ratiometric float
userdata AP_HAL::PWMSource rename PWMSource
userdata AP_HAL::PWMSource method set_pin boolean uint8_t'skip_check "Scripting"'literal
userdata AP_HAL::PWMSource method get_pwm_us uint16_t
userdata AP_HAL::PWMSource method get_pwm_avg_us uint16_t
global manual PWMSource lua_get_PWMSource 0
ap_object AP_HAL::PWMSource method set_pin boolean uint8_t'skip_check "Scripting"'literal
ap_object AP_HAL::PWMSource method get_pwm_us uint16_t
ap_object AP_HAL::PWMSource method get_pwm_avg_us uint16_t
singleton hal.gpio rename gpio
singleton hal.gpio literal

View File

@ -500,6 +500,8 @@ int lua_get_CAN_device2(lua_State *L) {
directory listing, return table of files in a directory
*/
int lua_dirlist(lua_State *L) {
binding_argcheck(L, 1);
struct dirent *entry;
int i;
const char *path = luaL_checkstring(L, 1);
@ -529,8 +531,9 @@ int lua_dirlist(lua_State *L) {
remove a file
*/
int lua_removefile(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
return luaL_fileresult(L, remove(filename) == 0, filename);
binding_argcheck(L, 1);
const char *filename = luaL_checkstring(L, 1);
return luaL_fileresult(L, remove(filename) == 0, filename);
}
// Manual binding to allow SRV_Channels table to see safety state
@ -540,3 +543,24 @@ int SRV_Channels_get_safety_state(lua_State *L) {
lua_pushboolean(L, data);
return 1;
}
int lua_get_PWMSource(lua_State *L) {
binding_argcheck(L, 0);
static_assert(SCRIPTING_MAX_NUM_PWM_SOURCE >= 0, "There cannot be a negative number of PWMSources");
if (AP::scripting()->num_pwm_source >= SCRIPTING_MAX_NUM_PWM_SOURCE) {
return luaL_argerror(L, 1, "no PWMSources available");
}
AP::scripting()->_pwm_source[AP::scripting()->num_pwm_source] = new AP_HAL::PWMSource;
if (AP::scripting()->_pwm_source[AP::scripting()->num_pwm_source] == nullptr) {
return luaL_argerror(L, 1, "PWMSources device nullptr");
}
new_AP_HAL__PWMSource(L);
*((AP_HAL::PWMSource**)luaL_checkudata(L, -1, "AP_HAL::PWMSource")) = AP::scripting()->_pwm_source[AP::scripting()->num_pwm_source];
AP::scripting()->num_pwm_source++;
return 1;
}

View File

@ -13,3 +13,4 @@ int lua_get_CAN_device2(lua_State *L);
int lua_dirlist(lua_State *L);
int lua_removefile(lua_State *L);
int SRV_Channels_get_safety_state(lua_State *L);
int lua_get_PWMSource(lua_State *L);