1
0
Fork 0
mirror of https://github.com/EdgeTX/edgetx.git synced 2025-07-23 16:25:12 +03:00

Lua setTelemetryValue() function fixes (#4158)

* Protect subId, prevent endless loop in sensor discovery if more than 7 is passed.
* Documentation updated
* Add additional parameter checks
This commit is contained in:
3djc 2016-12-23 19:32:39 +01:00 committed by Damjan Adamic
parent 1b43d8993e
commit bab5fc339e

View file

@ -791,7 +791,7 @@ static int luaPlayTone(lua_State * L)
}
/*luadoc
@function luaPlayHaptic(duration, pause [, flags])
@function playHaptic(duration, pause [, flags])
Generate haptic feedback
@ -1017,14 +1017,14 @@ static int luaDefaultStick(lua_State * L)
return 1;
}
/* luadoc
@function setTelemetryValue(id, subID, instance, value [, unit] [, precision [, name])
/*luadoc
@function setTelemetryValue(id, subID, instance, value [, unit [, precision [, name]]])
@param id Id of the sensor
@param id Id of the sensor, valid range is from 0 to 0xFFFF
@param subID subID of the sensor, usually 0
@param subID subID of the sensor, usually 0, valid range is from 0 to 7
@param instance instance of the sensor (SensorID)
@param instance instance of the sensor (SensorID), valid range is from 0 to 0xFF
@param value fed to the sensor
@ -1047,20 +1047,23 @@ static int luaDefaultStick(lua_State * L)
@retval true, if the sensor was just added. In this case the value is ignored (subsequent call will set the value)
@notice All three parameters `id`, `subID` and `instance` can't be zero at the same time. At least one of them
must be different from zero.
@status current Introduced in 2.2.0
*/
static int luaSetTelemetryValue(lua_State * L)
{
uint16_t id = luaL_checkinteger(L, 1);
uint8_t subId = luaL_checkinteger(L, 2);
uint8_t instance = luaL_checkinteger(L, 3);
uint16_t id = luaL_checkunsigned(L, 1);
uint8_t subId = luaL_checkunsigned(L, 2) & 0x7;
uint8_t instance = luaL_checkunsigned(L, 3);
int32_t value = luaL_checkinteger(L, 4);
uint32_t unit = luaL_optinteger(L, 5, 0);
uint32_t prec = luaL_optinteger(L, 6, 0);
uint32_t unit = luaL_optunsigned(L, 5, 0);
uint32_t prec = luaL_optunsigned(L, 6, 0);
char zname[4];
const char* name = luaL_optstring(L, 7, NULL);
if (name != NULL) {
if (name != NULL && strlen(name) > 0) {
str2zchar(zname, name, 4);
} else {
zname[0] = hex2zchar((id & 0xf000) >> 12);
@ -1068,7 +1071,7 @@ static int luaSetTelemetryValue(lua_State * L)
zname[2] = hex2zchar((id & 0x00f0) >> 4);
zname[3] = hex2zchar((id & 0x000f) >> 0);
}
if (id | subId | instance) {
int index = setTelemetryValue(TELEM_PROTO_LUA, id, subId, instance, value, unit, prec);
if (index >= 0) {
TelemetrySensor &telemetrySensor = g_model.telemetrySensors[index];
@ -1081,6 +1084,9 @@ static int luaSetTelemetryValue(lua_State * L)
lua_pushboolean(L, false);
}
return 1;
}
lua_pushboolean(L, false);
return 1;
}
/*luadoc