1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-24 00:35:18 +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

@ -586,11 +586,11 @@ This is just a hardware pass/fail measure and does not represent the quality of
*/ */
static int luaGetRAS(lua_State * L) static int luaGetRAS(lua_State * L)
{ {
if (IS_SWR_VALUE_VALID()) { if (IS_SWR_VALUE_VALID()) {
lua_pushinteger(L, telemetryData.swr.value); lua_pushinteger(L, telemetryData.swr.value);
} }
else { else {
lua_pushnil(L); lua_pushnil(L);
} }
return 1; return 1;
} }
@ -791,7 +791,7 @@ static int luaPlayTone(lua_State * L)
} }
/*luadoc /*luadoc
@function luaPlayHaptic(duration, pause [, flags]) @function playHaptic(duration, pause [, flags])
Generate haptic feedback Generate haptic feedback
@ -1017,14 +1017,14 @@ static int luaDefaultStick(lua_State * L)
return 1; return 1;
} }
/* luadoc /*luadoc
@function setTelemetryValue(id, subID, instance, value [, unit] [, precision [, name]) @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 @param value fed to the sensor
@ -1040,27 +1040,30 @@ static int luaDefaultStick(lua_State * L)
@param precision the precision of the sensor @param precision the precision of the sensor
* `0 or not present` no decimal precision. * `0 or not present` no decimal precision.
* `!= 0` value is divided by 10^precision, e.g. value=1000, prec=2 => 10.00. * `!= 0` value is divided by 10^precision, e.g. value=1000, prec=2 => 10.00.
@param name (string) Name of the sensor if it does not yet exist (4 chars). @param name (string) Name of the sensor if it does not yet exist (4 chars).
* `not present` Name defaults to the Id. * `not present` Name defaults to the Id.
* `present` Sensor takes name of the argument. Argument must have name surrounded by quotes: e.g., "Name" * `present` Sensor takes name of the argument. Argument must have name surrounded by quotes: e.g., "Name"
@retval true, if the sensor was just added. In this case the value is ignored (subsequent call will set the value) @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 @status current Introduced in 2.2.0
*/ */
static int luaSetTelemetryValue(lua_State * L) static int luaSetTelemetryValue(lua_State * L)
{ {
uint16_t id = luaL_checkinteger(L, 1); uint16_t id = luaL_checkunsigned(L, 1);
uint8_t subId = luaL_checkinteger(L, 2); uint8_t subId = luaL_checkunsigned(L, 2) & 0x7;
uint8_t instance = luaL_checkinteger(L, 3); uint8_t instance = luaL_checkunsigned(L, 3);
int32_t value = luaL_checkinteger(L, 4); int32_t value = luaL_checkinteger(L, 4);
uint32_t unit = luaL_optinteger(L, 5, 0); uint32_t unit = luaL_optunsigned(L, 5, 0);
uint32_t prec = luaL_optinteger(L, 6, 0); uint32_t prec = luaL_optunsigned(L, 6, 0);
char zname[4]; char zname[4];
const char* name = luaL_optstring(L, 7, NULL); const char* name = luaL_optstring(L, 7, NULL);
if (name != NULL) { if (name != NULL && strlen(name) > 0) {
str2zchar(zname, name, 4); str2zchar(zname, name, 4);
} else { } else {
zname[0] = hex2zchar((id & 0xf000) >> 12); zname[0] = hex2zchar((id & 0xf000) >> 12);
@ -1068,18 +1071,21 @@ static int luaSetTelemetryValue(lua_State * L)
zname[2] = hex2zchar((id & 0x00f0) >> 4); zname[2] = hex2zchar((id & 0x00f0) >> 4);
zname[3] = hex2zchar((id & 0x000f) >> 0); zname[3] = hex2zchar((id & 0x000f) >> 0);
} }
if (id | subId | instance) {
int index = setTelemetryValue(TELEM_PROTO_LUA, id, subId, instance, value, unit, prec); int index = setTelemetryValue(TELEM_PROTO_LUA, id, subId, instance, value, unit, prec);
if (index >= 0) { if (index >= 0) {
TelemetrySensor &telemetrySensor = g_model.telemetrySensors[index]; TelemetrySensor &telemetrySensor = g_model.telemetrySensors[index];
telemetrySensor.id = id; telemetrySensor.id = id;
telemetrySensor.subId = subId; telemetrySensor.subId = subId;
telemetrySensor.instance = instance; telemetrySensor.instance = instance;
telemetrySensor.init(zname, unit, prec); telemetrySensor.init(zname, unit, prec);
lua_pushboolean(L, true); lua_pushboolean(L, true);
} else { } else {
lua_pushboolean(L, false); lua_pushboolean(L, false);
}
return 1;
} }
lua_pushboolean(L, false);
return 1; return 1;
} }
@ -1388,6 +1394,6 @@ const luaR_value_entry opentxConstants[] = {
{"UNIT_GPS", UNIT_GPS}, {"UNIT_GPS", UNIT_GPS},
{"UNIT_BITFIELD", UNIT_BITFIELD}, {"UNIT_BITFIELD", UNIT_BITFIELD},
{"UNIT_TEXT", UNIT_TEXT}, {"UNIT_TEXT", UNIT_TEXT},
#endif #endif
{ NULL, 0 } /* sentinel */ { NULL, 0 } /* sentinel */
}; };