2023-06-07 02:56:50 -03:00
|
|
|
#include "AP_Scripting_config.h"
|
|
|
|
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
|
2018-10-03 17:37:43 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
2019-04-29 04:42:26 -03:00
|
|
|
#include <AP_HAL/HAL.h>
|
2023-12-25 22:21:11 -04:00
|
|
|
#include <AP_Networking/AP_Networking_Config.h>
|
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
#include <AP_HAL/utility/Socket.h>
|
|
|
|
#endif
|
2020-02-09 09:40:26 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2022-12-18 20:58:35 -04:00
|
|
|
#include <AP_Filesystem/AP_Filesystem.h>
|
2018-10-03 17:37:43 -03:00
|
|
|
|
|
|
|
#include "lua_bindings.h"
|
|
|
|
|
2019-04-29 04:42:26 -03:00
|
|
|
#include "lua_boxed_numerics.h"
|
2020-05-10 06:37:10 -03:00
|
|
|
#include <AP_Scripting/lua_generated_bindings.h>
|
2019-03-14 04:38:12 -03:00
|
|
|
|
2021-03-03 03:43:45 -04:00
|
|
|
#include <AP_Scripting/AP_Scripting.h>
|
2021-10-05 19:51:21 -03:00
|
|
|
#include <string.h>
|
2021-03-03 03:43:45 -04:00
|
|
|
|
2023-12-07 14:36:19 -04:00
|
|
|
#include "lua/src/lauxlib.h"
|
2023-12-03 01:43:29 -04:00
|
|
|
|
2020-02-24 20:22:10 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2022-12-01 16:47:21 -04:00
|
|
|
extern "C" {
|
|
|
|
#include "lua/src/lmem.h"
|
|
|
|
}
|
|
|
|
|
2019-04-29 04:42:26 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// millis
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_millis(lua_State *L) {
|
2022-08-10 19:17:34 -03:00
|
|
|
binding_argcheck(L, 0);
|
2019-04-29 04:42:26 -03:00
|
|
|
|
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = AP_HAL::millis();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-09 20:49:40 -03:00
|
|
|
// micros
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_micros(lua_State *L) {
|
2022-08-10 19:17:34 -03:00
|
|
|
binding_argcheck(L, 0);
|
2020-05-09 20:49:40 -03:00
|
|
|
|
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = AP_HAL::micros();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-09 18:45:30 -04:00
|
|
|
|
2023-09-21 22:24:30 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2023-03-09 18:45:30 -04:00
|
|
|
int lua_mavlink_init(lua_State *L) {
|
2023-06-18 09:56:28 -03:00
|
|
|
|
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "mavlink") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, 2+arg_offset);
|
2023-03-09 18:45:30 -04:00
|
|
|
WITH_SEMAPHORE(AP::scripting()->mavlink_data.sem);
|
|
|
|
// get the depth of receive queue
|
2023-06-18 09:56:28 -03:00
|
|
|
const uint32_t queue_size = get_uint32(L, 1+arg_offset, 0, 25);
|
2023-03-09 18:45:30 -04:00
|
|
|
// get number of msgs to accept
|
2023-06-18 09:56:28 -03:00
|
|
|
const uint32_t num_msgs = get_uint32(L, 2+arg_offset, 0, 25);
|
2023-03-09 18:45:30 -04:00
|
|
|
|
|
|
|
struct AP_Scripting::mavlink &data = AP::scripting()->mavlink_data;
|
|
|
|
if (data.rx_buffer == nullptr) {
|
2024-05-26 22:24:14 -03:00
|
|
|
data.rx_buffer = NEW_NOTHROW ObjectBuffer<struct AP_Scripting::mavlink_msg>(queue_size);
|
2023-03-09 18:45:30 -04:00
|
|
|
if (data.rx_buffer == nullptr) {
|
|
|
|
return luaL_error(L, "Failed to allocate mavlink rx buffer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data.accept_msg_ids == nullptr) {
|
2024-05-26 22:24:14 -03:00
|
|
|
data.accept_msg_ids = NEW_NOTHROW uint32_t[num_msgs];
|
2023-03-09 18:45:30 -04:00
|
|
|
if (data.accept_msg_ids == nullptr) {
|
|
|
|
return luaL_error(L, "Failed to allocate mavlink rx registry");
|
|
|
|
}
|
|
|
|
data.accept_msg_ids_size = num_msgs;
|
2023-05-15 20:42:39 -03:00
|
|
|
memset(data.accept_msg_ids, UINT32_MAX, sizeof(int) * num_msgs);
|
2023-03-09 18:45:30 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-03 06:45:13 -03:00
|
|
|
int lua_mavlink_receive_chan(lua_State *L) {
|
2023-06-18 09:56:28 -03:00
|
|
|
|
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "mavlink") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, arg_offset);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
|
|
|
struct AP_Scripting::mavlink_msg msg;
|
2023-03-09 18:45:30 -04:00
|
|
|
ObjectBuffer<struct AP_Scripting::mavlink_msg> *rx_buffer = AP::scripting()->mavlink_data.rx_buffer;
|
2020-02-24 20:22:10 -04:00
|
|
|
|
2023-03-09 18:45:30 -04:00
|
|
|
if (rx_buffer == nullptr) {
|
|
|
|
return luaL_error(L, "Never subscribed to a message");
|
2020-02-24 20:22:10 -04:00
|
|
|
}
|
|
|
|
|
2023-03-09 18:45:30 -04:00
|
|
|
if (rx_buffer->pop(msg)) {
|
2020-02-24 20:22:10 -04:00
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
luaL_addlstring(&b, (char *)&msg.msg, sizeof(msg.msg));
|
|
|
|
luaL_pushresult(&b);
|
|
|
|
lua_pushinteger(L, msg.chan);
|
2023-06-18 09:56:28 -03:00
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = msg.timestamp_ms;
|
2023-05-03 06:45:13 -03:00
|
|
|
return 3;
|
2020-02-24 20:22:10 -04:00
|
|
|
} else {
|
|
|
|
// no MAVLink to handle, just return no results
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 06:45:13 -03:00
|
|
|
int lua_mavlink_register_rx_msgid(lua_State *L) {
|
2023-06-18 09:56:28 -03:00
|
|
|
|
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "mavlink") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, 1+arg_offset);
|
|
|
|
|
|
|
|
const uint32_t msgid = get_uint32(L, 1+arg_offset, 0, (1 << 24) - 1);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
|
|
|
struct AP_Scripting::mavlink &data = AP::scripting()->mavlink_data;
|
|
|
|
|
|
|
|
// check that we aren't currently watching this ID
|
2023-03-09 18:45:30 -04:00
|
|
|
for (uint8_t i = 0; i < data.accept_msg_ids_size; i++) {
|
|
|
|
if (data.accept_msg_ids[i] == msgid) {
|
2020-02-24 20:22:10 -04:00
|
|
|
lua_pushboolean(L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
2023-03-09 18:45:30 -04:00
|
|
|
for (i = 0; i < data.accept_msg_ids_size; i++) {
|
2023-05-15 20:42:39 -03:00
|
|
|
if (data.accept_msg_ids[i] == UINT32_MAX) {
|
2020-02-24 20:22:10 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:45:30 -04:00
|
|
|
if (i >= data.accept_msg_ids_size) {
|
2020-02-24 20:22:10 -04:00
|
|
|
return luaL_error(L, "Out of MAVLink ID's to monitor");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(data.sem);
|
|
|
|
data.accept_msg_ids[i] = msgid;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-03 06:45:13 -03:00
|
|
|
int lua_mavlink_send_chan(lua_State *L) {
|
2023-06-18 09:56:28 -03:00
|
|
|
|
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "mavlink") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, 3+arg_offset);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
2023-06-18 09:56:28 -03:00
|
|
|
const mavlink_channel_t chan = (mavlink_channel_t)get_uint32(L, 1+arg_offset, 0, MAVLINK_COMM_NUM_BUFFERS - 1);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
2023-06-18 09:56:28 -03:00
|
|
|
const uint32_t msgid = get_uint32(L, 2+arg_offset, 0, (1 << 24) - 1);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
2023-06-18 09:56:28 -03:00
|
|
|
const char *packet = luaL_checkstring(L, 3+arg_offset);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
2023-03-09 18:45:30 -04:00
|
|
|
// FIXME: The data that's in this mavlink_msg_entry_t should be provided from the script, which allows
|
|
|
|
// sending entirely new messages as outputs. At the moment we can only encode messages that
|
|
|
|
// are known at compile time. This is fine as a starting point as this is symmetrical to the
|
|
|
|
// decoding side of the scripting support
|
|
|
|
const mavlink_msg_entry_t *entry = mavlink_get_msg_entry(msgid);
|
|
|
|
if (entry == nullptr) {
|
|
|
|
return luaL_error(L, "Unknown MAVLink message ID (%d)", msgid);
|
2020-02-24 20:22:10 -04:00
|
|
|
}
|
2023-05-14 03:08:36 -03:00
|
|
|
|
|
|
|
WITH_SEMAPHORE(comm_chan_lock(chan));
|
2023-03-09 18:45:30 -04:00
|
|
|
if (comm_get_txspace(chan) >= (GCS_MAVLINK::packet_overhead_chan(chan) + entry->max_msg_len)) {
|
|
|
|
_mav_finalize_message_chan_send(chan,
|
|
|
|
entry->msgid,
|
|
|
|
packet,
|
|
|
|
entry->min_msg_len,
|
|
|
|
entry->max_msg_len,
|
|
|
|
entry->crc_extra);
|
2020-02-24 20:22:10 -04:00
|
|
|
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
} else {
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-09 20:49:40 -03:00
|
|
|
|
2023-06-18 09:59:45 -03:00
|
|
|
int lua_mavlink_block_command(lua_State *L) {
|
|
|
|
|
|
|
|
// Allow : and . access
|
2023-06-18 10:00:58 -03:00
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "mavlink") != NULL) ? 1 : 0;
|
2023-06-18 09:59:45 -03:00
|
|
|
|
|
|
|
binding_argcheck(L, 1+arg_offset);
|
|
|
|
|
|
|
|
const uint16_t id = get_uint16_t(L, 1+arg_offset);
|
|
|
|
|
|
|
|
// Check if ID is already registered
|
|
|
|
if (AP::scripting()->is_handling_command(id)) {
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new list item
|
2024-05-26 22:24:14 -03:00
|
|
|
AP_Scripting::command_block_list *new_item = NEW_NOTHROW AP_Scripting::command_block_list;
|
2023-06-18 09:59:45 -03:00
|
|
|
if (new_item == nullptr) {
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
new_item->id = id;
|
|
|
|
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(AP::scripting()->mavlink_command_block_list_sem);
|
|
|
|
new_item->next = AP::scripting()->mavlink_command_block_list;
|
|
|
|
AP::scripting()->mavlink_command_block_list = new_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-09-21 22:24:30 -03:00
|
|
|
#endif // HAL_GCS_ENABLED
|
2023-06-18 09:59:45 -03:00
|
|
|
|
2023-10-04 14:01:20 -03:00
|
|
|
#if AP_MISSION_ENABLED
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_mission_receive(lua_State *L) {
|
2022-08-10 19:17:34 -03:00
|
|
|
binding_argcheck(L, 0);
|
2021-02-25 21:09:17 -04:00
|
|
|
|
|
|
|
ObjectBuffer<struct AP_Scripting::scripting_mission_cmd> *input = AP::scripting()->mission_data;
|
|
|
|
|
|
|
|
if (input == nullptr) {
|
|
|
|
// no mission items ever received
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AP_Scripting::scripting_mission_cmd cmd;
|
|
|
|
|
|
|
|
if (!input->pop(cmd)) {
|
|
|
|
// no new item
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_uint32_t(L);
|
|
|
|
*check_uint32_t(L, -1) = cmd.time_ms;
|
|
|
|
|
|
|
|
lua_pushinteger(L, cmd.p1);
|
|
|
|
lua_pushnumber(L, cmd.content_p1);
|
|
|
|
lua_pushnumber(L, cmd.content_p2);
|
|
|
|
lua_pushnumber(L, cmd.content_p3);
|
|
|
|
|
|
|
|
return 5;
|
|
|
|
}
|
2023-10-04 14:01:20 -03:00
|
|
|
#endif // AP_MISSION_ENABLED
|
2021-02-25 21:09:17 -04:00
|
|
|
|
2023-09-21 22:24:30 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2022-04-30 13:17:40 -03:00
|
|
|
int AP_Logger_Write(lua_State *L) {
|
2020-02-09 09:40:26 -04:00
|
|
|
AP_Logger * AP_logger = AP_Logger::get_singleton();
|
|
|
|
if (AP_logger == nullptr) {
|
|
|
|
return luaL_argerror(L, 1, "logger not supported on this firmware");
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "logger") != NULL) ? 1 : 0;
|
|
|
|
|
2020-02-09 09:40:26 -04:00
|
|
|
// check we have at least 4 arguments passed in
|
2022-09-03 13:01:36 -03:00
|
|
|
const int args = lua_gettop(L) - arg_offset;
|
2020-02-09 09:40:26 -04:00
|
|
|
if (args < 4) {
|
|
|
|
return luaL_argerror(L, args, "too few arguments");
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
const char * name = luaL_checkstring(L, 1 + arg_offset);
|
|
|
|
const char * labels = luaL_checkstring(L, 2 + arg_offset);
|
|
|
|
const char * fmt = luaL_checkstring(L, 3 + arg_offset);
|
2020-02-09 09:40:26 -04:00
|
|
|
|
|
|
|
// cheack the name, labels and format are not too long
|
|
|
|
if (strlen(name) >= LS_NAME_SIZE) {
|
|
|
|
return luaL_error(L, "Name must be 4 or less chars long");
|
|
|
|
}
|
2020-07-25 09:13:36 -03:00
|
|
|
uint8_t length = strlen(labels);
|
2020-02-09 09:40:26 -04:00
|
|
|
if (length >= (LS_LABELS_SIZE - 7)) { // need 7 chars to add 'TimeUS,'
|
|
|
|
return luaL_error(L, "labels must be less than 58 chars long");
|
|
|
|
}
|
|
|
|
// Count the number of commas
|
|
|
|
uint8_t commas = 1;
|
|
|
|
for (uint8_t i=0; i<length; i++) {
|
|
|
|
if (labels[i] == ',') {
|
|
|
|
commas++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length = strlen(fmt);
|
|
|
|
if (length >= (LS_FORMAT_SIZE - 1)) { // need 1 char to add timestamp
|
|
|
|
return luaL_error(L, "format must be less than 15 chars long");
|
|
|
|
}
|
|
|
|
|
2020-07-25 09:13:36 -03:00
|
|
|
// check the number of arguments matches the number of values in the label
|
|
|
|
if (length != commas) {
|
|
|
|
return luaL_argerror(L, args, "label does not match format");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool have_units = false;
|
|
|
|
if (args - 5 == length) {
|
|
|
|
// check if there are enough arguments for units and multiplyers
|
|
|
|
have_units = true;
|
|
|
|
} else if (args - 3 != length) {
|
|
|
|
// check the number of arguments matches the length of the foramt string
|
2020-02-09 09:40:26 -04:00
|
|
|
return luaL_argerror(L, args, "format does not match No. of arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend timestamp to format and labels
|
|
|
|
char label_cat[LS_LABELS_SIZE];
|
|
|
|
strcpy(label_cat,"TimeUS,");
|
|
|
|
strcat(label_cat,labels);
|
|
|
|
char fmt_cat[LS_FORMAT_SIZE];
|
|
|
|
strcpy(fmt_cat,"Q");
|
|
|
|
strcat(fmt_cat,fmt);
|
|
|
|
|
2020-07-25 09:13:36 -03:00
|
|
|
// Need to declare these here so they don't go out of scope
|
|
|
|
char units_cat[LS_FORMAT_SIZE];
|
|
|
|
char multipliers_cat[LS_FORMAT_SIZE];
|
|
|
|
|
|
|
|
uint8_t field_start = 4;
|
|
|
|
struct AP_Logger::log_write_fmt *f;
|
|
|
|
if (!have_units) {
|
|
|
|
// ask for a mesage type
|
2022-10-26 13:01:59 -03:00
|
|
|
f = AP_logger->msg_fmt_for_name(name, label_cat, nullptr, nullptr, fmt_cat, true, true);
|
2020-07-25 09:13:36 -03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// read in units and multiplers strings
|
|
|
|
field_start += 2;
|
2022-09-03 13:01:36 -03:00
|
|
|
const char * units = luaL_checkstring(L, 4 + arg_offset);
|
|
|
|
const char * multipliers = luaL_checkstring(L, 5 + arg_offset);
|
2020-07-25 09:13:36 -03:00
|
|
|
|
|
|
|
if (length != strlen(units)) {
|
|
|
|
return luaL_error(L, "units must be same length as format");
|
|
|
|
}
|
|
|
|
if (length != strlen(multipliers)) {
|
|
|
|
return luaL_error(L, "multipliers must be same length as format");
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend timestamp to units and multiplyers
|
|
|
|
strcpy(units_cat,"s");
|
|
|
|
strcat(units_cat,units);
|
|
|
|
|
|
|
|
strcpy(multipliers_cat,"F");
|
|
|
|
strcat(multipliers_cat,multipliers);
|
|
|
|
|
|
|
|
// ask for a mesage type
|
2022-10-26 13:01:59 -03:00
|
|
|
f = AP_logger->msg_fmt_for_name(name, label_cat, units_cat, multipliers_cat, fmt_cat, true, true);
|
2020-07-25 09:13:36 -03:00
|
|
|
}
|
|
|
|
|
2020-02-09 09:40:26 -04:00
|
|
|
if (f == nullptr) {
|
|
|
|
// unable to map name to a messagetype; could be out of
|
|
|
|
// msgtypes, could be out of slots, ...
|
|
|
|
return luaL_argerror(L, args, "could not map message type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// work out how long the block will be
|
|
|
|
int16_t msg_len = AP_logger->Write_calc_msg_len(fmt_cat);
|
|
|
|
if (msg_len == -1) {
|
|
|
|
return luaL_argerror(L, args, "unknown format");
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:47:21 -04:00
|
|
|
// note that luaM_malloc will never return null, it will fault instead
|
|
|
|
char *buffer = (char*)luaM_malloc(L, msg_len);
|
2020-02-09 09:40:26 -04:00
|
|
|
|
|
|
|
// add logging headers
|
2022-10-16 19:40:38 -03:00
|
|
|
uint8_t offset = 0;
|
|
|
|
buffer[offset++] = HEAD_BYTE1;
|
|
|
|
buffer[offset++] = HEAD_BYTE2;
|
|
|
|
buffer[offset++] = f->msg_type;
|
2020-02-09 09:40:26 -04:00
|
|
|
|
|
|
|
// timestamp is always first value
|
|
|
|
const uint64_t now = AP_HAL::micros64();
|
2022-10-16 19:40:38 -03:00
|
|
|
memcpy(&buffer[offset], &now, sizeof(uint64_t));
|
|
|
|
offset += sizeof(uint64_t);
|
2020-02-09 09:40:26 -04:00
|
|
|
|
2020-07-25 09:13:36 -03:00
|
|
|
for (uint8_t i=field_start; i<=args; i++) {
|
2020-02-09 09:40:26 -04:00
|
|
|
uint8_t charlen = 0;
|
2020-07-25 09:13:36 -03:00
|
|
|
uint8_t index = have_units ? i-5 : i-3;
|
2022-09-03 13:01:36 -03:00
|
|
|
uint8_t arg_index = i + arg_offset;
|
2020-07-25 09:13:36 -03:00
|
|
|
switch(fmt_cat[index]) {
|
2022-12-26 21:31:48 -04:00
|
|
|
// logger variable types not available to scripting
|
2020-02-09 09:40:26 -04:00
|
|
|
// 'd': double
|
|
|
|
// 'q': int64_t
|
2024-06-14 15:56:00 -03:00
|
|
|
// 'a': int16_t[32]
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'b': { // int8_t
|
|
|
|
int isnum;
|
|
|
|
const lua_Integer tmp1 = lua_tointegerx(L, arg_index, &isnum);
|
|
|
|
if (!isnum || (tmp1 < INT8_MIN) || (tmp1 > INT8_MAX)) {
|
|
|
|
luaM_free(L, buffer);
|
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
int8_t tmp = static_cast<int8_t>(tmp1);
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(int8_t));
|
|
|
|
offset += sizeof(int8_t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h': // int16_t
|
|
|
|
case 'c': { // int16_t * 100
|
|
|
|
int isnum;
|
|
|
|
const lua_Integer tmp1 = lua_tointegerx(L, arg_index, &isnum);
|
|
|
|
if (!isnum || (tmp1 < INT16_MIN) || (tmp1 > INT16_MAX)) {
|
|
|
|
luaM_free(L, buffer);
|
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
int16_t tmp = static_cast<int16_t>(tmp1);
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(int16_t));
|
|
|
|
offset += sizeof(int16_t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'H': // uint16_t
|
|
|
|
case 'C': { // uint16_t * 100
|
|
|
|
int isnum;
|
|
|
|
const lua_Integer tmp1 = lua_tointegerx(L, arg_index, &isnum);
|
|
|
|
if (!isnum || (tmp1 < 0) || (tmp1 > UINT16_MAX)) {
|
|
|
|
luaM_free(L, buffer);
|
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
uint16_t tmp = static_cast<uint16_t>(tmp1);
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(uint16_t));
|
|
|
|
offset += sizeof(uint16_t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'i': // int32_t
|
|
|
|
case 'L': // int32_t (lat/long)
|
|
|
|
case 'e': { // int32_t * 100
|
2022-10-16 19:40:38 -03:00
|
|
|
int isnum;
|
|
|
|
const lua_Integer tmp1 = lua_tointegerx(L, arg_index, &isnum);
|
|
|
|
if (!isnum) {
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2022-10-16 19:40:38 -03:00
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
const int32_t tmp = tmp1;
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(int32_t));
|
|
|
|
offset += sizeof(int32_t);
|
2020-02-09 09:40:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'f': { // float
|
2022-10-16 19:40:38 -03:00
|
|
|
int isnum;
|
|
|
|
const lua_Number tmp1 = lua_tonumberx(L, arg_index, &isnum);
|
|
|
|
if (!isnum) {
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2022-10-16 19:40:38 -03:00
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
const float tmp = tmp1;
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(float));
|
|
|
|
offset += sizeof(float);
|
2020-02-09 09:40:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'n': { // char[4]
|
2020-02-09 09:40:26 -04:00
|
|
|
charlen = 4;
|
|
|
|
break;
|
|
|
|
}
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'M': // uint8_t (flight mode)
|
|
|
|
case 'B': { // uint8_t
|
2022-10-16 19:40:38 -03:00
|
|
|
int isnum;
|
2024-06-17 17:54:20 -03:00
|
|
|
lua_Integer tmp1 = lua_tointegerx(L, arg_index, &isnum);
|
2022-10-16 19:40:38 -03:00
|
|
|
if (!isnum || (tmp1 < 0) || (tmp1 > UINT8_MAX)) {
|
2024-06-17 17:54:20 -03:00
|
|
|
// Also allow boolean
|
|
|
|
if (!isnum && lua_isboolean(L, arg_index)) {
|
|
|
|
tmp1 = lua_toboolean(L, arg_index);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
luaM_free(L, buffer);
|
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
2022-10-16 19:40:38 -03:00
|
|
|
}
|
2020-02-09 09:40:26 -04:00
|
|
|
uint8_t tmp = static_cast<uint8_t>(tmp1);
|
2022-10-16 19:40:38 -03:00
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(uint8_t));
|
|
|
|
offset += sizeof(uint8_t);
|
2020-02-09 09:40:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'I': // uint32_t
|
|
|
|
case 'E': { // uint32_t * 100
|
2022-10-16 19:40:38 -03:00
|
|
|
uint32_t tmp;
|
|
|
|
void * ud = luaL_testudata(L, arg_index, "uint32_t");
|
|
|
|
if (ud != nullptr) {
|
|
|
|
tmp = *static_cast<uint32_t *>(ud);
|
|
|
|
} else {
|
|
|
|
int success;
|
|
|
|
const lua_Integer v_int = lua_tointegerx(L, arg_index, &success);
|
|
|
|
if (success) {
|
|
|
|
tmp = v_int;
|
|
|
|
} else {
|
|
|
|
const lua_Number v_float = lua_tonumberx(L, arg_index, &success);
|
|
|
|
if (!success || (v_float < 0) || (v_float > float(UINT32_MAX))) {
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2022-10-16 19:40:38 -03:00
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
tmp = v_float;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(uint32_t));
|
|
|
|
offset += sizeof(uint32_t);
|
2020-02-09 09:40:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2024-06-14 15:56:00 -03:00
|
|
|
case 'Q': { // uint64_t
|
|
|
|
void * ud = luaL_testudata(L, arg_index, "uint64_t");
|
|
|
|
if (ud == nullptr) {
|
|
|
|
luaM_free(L, buffer);
|
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
|
|
|
}
|
|
|
|
uint64_t tmp = *static_cast<uint64_t *>(ud);
|
|
|
|
memcpy(&buffer[offset], &tmp, sizeof(uint64_t));
|
|
|
|
offset += sizeof(uint64_t);
|
|
|
|
break;
|
|
|
|
}
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'N': { // char[16]
|
2020-02-09 09:40:26 -04:00
|
|
|
charlen = 16;
|
|
|
|
break;
|
|
|
|
}
|
2022-12-26 21:31:48 -04:00
|
|
|
case 'Z': { // char[64]
|
2020-02-09 09:40:26 -04:00
|
|
|
charlen = 64;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2022-10-16 19:40:38 -03:00
|
|
|
luaL_error(L, "%c unsupported format",fmt_cat[index]);
|
|
|
|
// no return
|
2020-02-09 09:40:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (charlen != 0) {
|
2022-10-16 19:40:38 -03:00
|
|
|
size_t slen;
|
|
|
|
const char *tmp = lua_tolstring(L, arg_index, &slen);
|
|
|
|
if (tmp == nullptr) {
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2022-10-16 19:40:38 -03:00
|
|
|
luaL_argerror(L, arg_index, "argument out of range");
|
|
|
|
// no return
|
2020-02-09 09:40:26 -04:00
|
|
|
}
|
2022-10-16 19:40:38 -03:00
|
|
|
if (slen > charlen) {
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2022-10-16 19:40:38 -03:00
|
|
|
luaL_error(L, "arg %d too long for %c format",arg_index,fmt_cat[index]);
|
|
|
|
// no return
|
2021-10-05 19:51:21 -03:00
|
|
|
}
|
2022-10-16 19:40:38 -03:00
|
|
|
memcpy(&buffer[offset], tmp, slen);
|
|
|
|
memset(&buffer[offset+slen], 0, charlen-slen);
|
|
|
|
offset += charlen;
|
2020-02-09 09:40:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_logger->Safe_Write_Emit_FMT(f);
|
|
|
|
|
2022-10-16 19:40:38 -03:00
|
|
|
AP_logger->WriteBlock(buffer,msg_len);
|
|
|
|
|
2022-12-01 16:47:21 -04:00
|
|
|
luaM_free(L, buffer);
|
2020-02-09 09:40:26 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-09-21 22:24:30 -03:00
|
|
|
#endif // HAL_LOGGING_ENABLED
|
2020-02-09 09:40:26 -04:00
|
|
|
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_get_i2c_device(lua_State *L) {
|
2021-03-03 03:43:45 -04:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "i2c") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
const int args = lua_gettop(L) - arg_offset;
|
2021-03-03 03:43:45 -04:00
|
|
|
if (args < 2) {
|
|
|
|
return luaL_argerror(L, args, "require i2c bus and address");
|
|
|
|
}
|
|
|
|
if (args > 4) {
|
|
|
|
return luaL_argerror(L, args, "too many arguments");
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:55:22 -03:00
|
|
|
const lua_Integer bus_in = get_integer(L, 1 + arg_offset, 0, 4);
|
2021-03-03 03:43:45 -04:00
|
|
|
const uint8_t bus = static_cast<uint8_t>(bus_in);
|
|
|
|
|
2022-10-15 21:55:22 -03:00
|
|
|
const lua_Integer address_in = get_integer(L, 2 + arg_offset, 0, 128);
|
2021-03-03 03:43:45 -04:00
|
|
|
const uint8_t address = static_cast<uint8_t>(address_in);
|
|
|
|
|
|
|
|
// optional arguments, use the same defaults as the hal get_device function
|
|
|
|
uint32_t bus_clock = 400000;
|
|
|
|
bool use_smbus = false;
|
|
|
|
|
|
|
|
if (args > 2) {
|
2022-09-03 13:01:36 -03:00
|
|
|
bus_clock = coerce_to_uint32_t(L, 3 + arg_offset);
|
2021-03-03 03:43:45 -04:00
|
|
|
|
|
|
|
if (args > 3) {
|
2022-09-03 13:01:36 -03:00
|
|
|
use_smbus = static_cast<bool>(lua_toboolean(L, 4 + arg_offset));
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
2021-03-03 03:43:45 -04:00
|
|
|
static_assert(SCRIPTING_MAX_NUM_I2C_DEVICE >= 0, "There cannot be a negative number of I2C devices");
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->num_i2c_devices >= SCRIPTING_MAX_NUM_I2C_DEVICE) {
|
2022-02-05 08:21:06 -04:00
|
|
|
return luaL_argerror(L, 1, "no i2c devices available");
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
|
2024-05-26 22:24:14 -03:00
|
|
|
scripting->_i2c_dev[scripting->num_i2c_devices] = NEW_NOTHROW AP_HAL::OwnPtr<AP_HAL::I2CDevice>;
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->_i2c_dev[scripting->num_i2c_devices] == nullptr) {
|
2021-07-23 08:15:21 -03:00
|
|
|
return luaL_argerror(L, 1, "i2c device nullptr");
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
*scripting->_i2c_dev[scripting->num_i2c_devices] = std::move(hal.i2c_mgr->get_device(bus, address, bus_clock, use_smbus));
|
2021-03-03 03:43:45 -04:00
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->_i2c_dev[scripting->num_i2c_devices] == nullptr || scripting->_i2c_dev[scripting->num_i2c_devices]->get() == nullptr) {
|
2021-07-23 08:15:21 -03:00
|
|
|
return luaL_argerror(L, 1, "i2c device nullptr");
|
2021-03-03 03:43:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
new_AP_HAL__I2CDevice(L);
|
2023-03-03 05:45:19 -04:00
|
|
|
*((AP_HAL::I2CDevice**)luaL_checkudata(L, -1, "AP_HAL::I2CDevice")) = scripting->_i2c_dev[scripting->num_i2c_devices]->get();
|
2021-03-03 03:43:45 -04:00
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
scripting->num_i2c_devices++;
|
2021-03-03 03:43:45 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-10 13:08:12 -03:00
|
|
|
int AP_HAL__I2CDevice_read_registers(lua_State *L) {
|
|
|
|
const int args = lua_gettop(L);
|
|
|
|
bool multi_register;
|
|
|
|
if (args == 2) {
|
|
|
|
multi_register = false;
|
|
|
|
} else if (args == 3) {
|
|
|
|
multi_register = true;
|
|
|
|
} else {
|
|
|
|
return luaL_argerror(L, args, "expected 1 or 2 arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL::I2CDevice * ud = *check_AP_HAL__I2CDevice(L, 1);
|
|
|
|
|
2022-11-02 12:24:20 -03:00
|
|
|
const uint8_t first_reg = get_uint8_t(L, 2);
|
2022-09-10 13:08:12 -03:00
|
|
|
|
|
|
|
uint8_t recv_length = 1;
|
|
|
|
if (multi_register) {
|
2022-11-02 12:24:20 -03:00
|
|
|
recv_length = get_uint8_t(L, 3);
|
2022-09-10 13:08:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t data[recv_length];
|
|
|
|
|
|
|
|
ud->get_semaphore()->take_blocking();
|
|
|
|
const bool success = static_cast<bool>(ud->read_registers(first_reg, data, recv_length));
|
|
|
|
ud->get_semaphore()->give();
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
if (!multi_register) {
|
|
|
|
lua_pushinteger(L, data[0]);
|
|
|
|
} else {
|
|
|
|
// push to table
|
|
|
|
lua_newtable(L);
|
|
|
|
for (uint8_t i=0; i < recv_length; i++) {
|
|
|
|
lua_pushinteger(L, i+1);
|
|
|
|
lua_pushinteger(L, data[i]);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:27:54 -03:00
|
|
|
#if AP_SCRIPTING_CAN_SENSOR_ENABLED
|
2022-04-30 13:17:40 -03:00
|
|
|
int lua_get_CAN_device(lua_State *L) {
|
2021-07-23 08:15:21 -03:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "CAN") != NULL) ? 1 : 0;
|
2021-07-23 08:15:21 -03:00
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
binding_argcheck(L, 1 + arg_offset);
|
|
|
|
|
2022-10-15 21:55:22 -03:00
|
|
|
const uint32_t raw_buffer_len = get_uint32(L, 1 + arg_offset, 1, 25);
|
2021-07-23 08:15:21 -03:00
|
|
|
const uint32_t buffer_len = static_cast<uint32_t>(raw_buffer_len);
|
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
|
|
|
if (scripting->_CAN_dev == nullptr) {
|
2024-05-26 22:24:14 -03:00
|
|
|
scripting->_CAN_dev = NEW_NOTHROW ScriptingCANSensor(AP_CAN::Protocol::Scripting);
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->_CAN_dev == nullptr) {
|
2021-07-23 08:15:21 -03:00
|
|
|
return luaL_argerror(L, 1, "CAN device nullptr");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:27:23 -03:00
|
|
|
if (!scripting->_CAN_dev->initialized()) {
|
|
|
|
// Driver not initialized, probably because there is no can driver set to scripting
|
|
|
|
// Return nil
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:15:21 -03:00
|
|
|
new_ScriptingCANBuffer(L);
|
2023-03-03 05:45:19 -04:00
|
|
|
*((ScriptingCANBuffer**)luaL_checkudata(L, -1, "ScriptingCANBuffer")) = scripting->_CAN_dev->add_buffer(buffer_len);
|
2021-07-23 08:15:21 -03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2022-07-01 23:23:58 -03:00
|
|
|
|
|
|
|
int lua_get_CAN_device2(lua_State *L) {
|
|
|
|
|
2022-09-03 13:01:36 -03:00
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "CAN") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, 1 + arg_offset);
|
2022-07-01 23:23:58 -03:00
|
|
|
|
2022-10-15 21:55:22 -03:00
|
|
|
const uint32_t raw_buffer_len = get_uint32(L, 1 + arg_offset, 1, 25);
|
2022-07-01 23:23:58 -03:00
|
|
|
const uint32_t buffer_len = static_cast<uint32_t>(raw_buffer_len);
|
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
|
|
|
if (scripting->_CAN_dev2 == nullptr) {
|
2024-05-26 22:24:14 -03:00
|
|
|
scripting->_CAN_dev2 = NEW_NOTHROW ScriptingCANSensor(AP_CAN::Protocol::Scripting2);
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->_CAN_dev2 == nullptr) {
|
2022-07-01 23:23:58 -03:00
|
|
|
return luaL_argerror(L, 1, "CAN device nullptr");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:27:23 -03:00
|
|
|
if (!scripting->_CAN_dev2->initialized()) {
|
|
|
|
// Driver not initialized, probably because there is no can driver set to scripting 2
|
|
|
|
// Return nil
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-01 23:23:58 -03:00
|
|
|
new_ScriptingCANBuffer(L);
|
2023-03-03 05:45:19 -04:00
|
|
|
*((ScriptingCANBuffer**)luaL_checkudata(L, -1, "ScriptingCANBuffer")) = scripting->_CAN_dev2->add_buffer(buffer_len);
|
2022-07-01 23:23:58 -03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2023-10-04 13:27:54 -03:00
|
|
|
#endif // AP_SCRIPTING_CAN_SENSOR_ENABLED
|
2022-12-18 20:58:35 -04:00
|
|
|
|
2024-06-16 16:33:23 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
|
|
|
int lua_serial_find_serial(lua_State *L) {
|
|
|
|
// Allow : and . access
|
|
|
|
const int arg_offset = (luaL_testudata(L, 1, "serial") != NULL) ? 1 : 0;
|
|
|
|
|
|
|
|
binding_argcheck(L, 1 + arg_offset);
|
|
|
|
|
|
|
|
uint8_t instance = get_uint8_t(L, 1 + arg_offset);
|
|
|
|
|
|
|
|
AP_SerialManager *mgr = &AP::serialmanager();
|
|
|
|
AP_HAL::UARTDriver *driver_stream = mgr->find_serial(
|
|
|
|
AP_SerialManager::SerialProtocol_Scripting, instance);
|
|
|
|
|
|
|
|
if (driver_stream == nullptr) { // not found
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_AP_Scripting_SerialAccess(L);
|
|
|
|
check_AP_Scripting_SerialAccess(L, -1)->stream = driver_stream;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif // HAL_GCS_ENABLED
|
|
|
|
|
2024-06-16 18:52:37 -03:00
|
|
|
int lua_serial_readstring(lua_State *L) {
|
|
|
|
binding_argcheck(L, 2);
|
|
|
|
|
|
|
|
AP_Scripting_SerialAccess * port = check_AP_Scripting_SerialAccess(L, 1);
|
|
|
|
|
|
|
|
// create a buffer sized to hold the number of bytes the user wants to read
|
|
|
|
luaL_Buffer b;
|
|
|
|
const uint16_t req_bytes = get_uint16_t(L, 2);
|
|
|
|
uint8_t *data = (uint8_t *)luaL_buffinitsize(L, &b, req_bytes);
|
|
|
|
|
|
|
|
// read up to that number of bytes
|
|
|
|
const ssize_t read_bytes = port->read(data, req_bytes);
|
|
|
|
if (read_bytes < 0) {
|
|
|
|
return 0; // error, return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// push the buffer as a string, truncated to the number of bytes actually read
|
|
|
|
luaL_pushresultsize(&b, read_bytes);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-18 20:58:35 -04:00
|
|
|
/*
|
|
|
|
directory listing, return table of files in a directory
|
|
|
|
*/
|
|
|
|
int lua_dirlist(lua_State *L) {
|
2023-02-20 09:59:22 -04:00
|
|
|
binding_argcheck(L, 1);
|
|
|
|
|
2022-12-18 20:58:35 -04:00
|
|
|
struct dirent *entry;
|
|
|
|
int i;
|
|
|
|
const char *path = luaL_checkstring(L, 1);
|
|
|
|
|
|
|
|
/* open directory */
|
|
|
|
auto dir = AP::FS().opendir(path);
|
|
|
|
if (dir == nullptr) { /* error opening the directory? */
|
|
|
|
lua_pushnil(L); /* return nil and ... */
|
|
|
|
lua_pushstring(L, strerror(errno)); /* error message */
|
|
|
|
return 2; /* number of results */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create result table */
|
|
|
|
lua_newtable(L);
|
|
|
|
i = 1;
|
|
|
|
while ((entry = AP::FS().readdir(dir)) != nullptr) {
|
|
|
|
lua_pushnumber(L, i++); /* push key */
|
|
|
|
lua_pushstring(L, entry->d_name); /* push value */
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
AP::FS().closedir(dir);
|
|
|
|
return 1; /* table is already on top */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
remove a file
|
|
|
|
*/
|
|
|
|
int lua_removefile(lua_State *L) {
|
2023-02-20 09:59:22 -04:00
|
|
|
binding_argcheck(L, 1);
|
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
|
|
|
return luaL_fileresult(L, remove(filename) == 0, filename);
|
2022-12-18 20:58:35 -04:00
|
|
|
}
|
2023-02-02 12:51:03 -04:00
|
|
|
|
|
|
|
// Manual binding to allow SRV_Channels table to see safety state
|
|
|
|
int SRV_Channels_get_safety_state(lua_State *L) {
|
|
|
|
binding_argcheck(L, 1);
|
|
|
|
const bool data = hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_ARMED;
|
|
|
|
lua_pushboolean(L, data);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-20 09:59:22 -04:00
|
|
|
|
|
|
|
int lua_get_PWMSource(lua_State *L) {
|
|
|
|
binding_argcheck(L, 0);
|
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
2023-02-20 09:59:22 -04:00
|
|
|
static_assert(SCRIPTING_MAX_NUM_PWM_SOURCE >= 0, "There cannot be a negative number of PWMSources");
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->num_pwm_source >= SCRIPTING_MAX_NUM_PWM_SOURCE) {
|
2023-02-20 09:59:22 -04:00
|
|
|
return luaL_argerror(L, 1, "no PWMSources available");
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:24:14 -03:00
|
|
|
scripting->_pwm_source[scripting->num_pwm_source] = NEW_NOTHROW AP_HAL::PWMSource;
|
2023-03-03 05:45:19 -04:00
|
|
|
if (scripting->_pwm_source[scripting->num_pwm_source] == nullptr) {
|
2023-02-20 09:59:22 -04:00
|
|
|
return luaL_argerror(L, 1, "PWMSources device nullptr");
|
|
|
|
}
|
|
|
|
|
|
|
|
new_AP_HAL__PWMSource(L);
|
2023-03-03 05:45:19 -04:00
|
|
|
*((AP_HAL::PWMSource**)luaL_checkudata(L, -1, "AP_HAL::PWMSource")) = scripting->_pwm_source[scripting->num_pwm_source];
|
2023-02-20 09:59:22 -04:00
|
|
|
|
2023-03-03 05:45:19 -04:00
|
|
|
scripting->num_pwm_source++;
|
2023-02-20 09:59:22 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-09 00:47:50 -04:00
|
|
|
|
2023-12-03 01:43:29 -04:00
|
|
|
#if AP_NETWORKING_ENABLED
|
|
|
|
/*
|
|
|
|
allocate a SocketAPM
|
|
|
|
*/
|
|
|
|
int lua_get_SocketAPM(lua_State *L) {
|
|
|
|
binding_argcheck(L, 1);
|
|
|
|
const uint8_t datagram = get_uint8_t(L, 1);
|
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
2024-05-26 22:24:14 -03:00
|
|
|
auto *sock = NEW_NOTHROW SocketAPM(datagram);
|
2023-12-03 01:43:29 -04:00
|
|
|
if (sock == nullptr) {
|
|
|
|
return luaL_argerror(L, 1, "SocketAPM device nullptr");
|
|
|
|
}
|
2024-01-03 19:23:57 -04:00
|
|
|
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_NET_SOCKET; i++) {
|
|
|
|
if (scripting->_net_sockets[i] == nullptr) {
|
|
|
|
scripting->_net_sockets[i] = sock;
|
|
|
|
new_SocketAPM(L);
|
|
|
|
*((SocketAPM**)luaL_checkudata(L, -1, "SocketAPM")) = scripting->_net_sockets[i];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2023-12-03 01:43:29 -04:00
|
|
|
|
2024-01-03 19:23:57 -04:00
|
|
|
return luaL_argerror(L, 1, "no sockets available");
|
2023-12-03 01:43:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
socket close
|
|
|
|
*/
|
|
|
|
int SocketAPM_close(lua_State *L) {
|
|
|
|
binding_argcheck(L, 1);
|
|
|
|
|
|
|
|
SocketAPM *ud = *check_SocketAPM(L, 1);
|
|
|
|
|
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
|
|
|
// clear allocated socket
|
|
|
|
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_NET_SOCKET; i++) {
|
|
|
|
if (scripting->_net_sockets[i] == ud) {
|
|
|
|
ud->close();
|
|
|
|
delete ud;
|
|
|
|
scripting->_net_sockets[i] = nullptr;
|
2024-02-08 17:00:17 -04:00
|
|
|
*check_SocketAPM(L, 1) = nullptr;
|
2023-12-03 01:43:29 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-07 14:36:19 -04:00
|
|
|
/*
|
|
|
|
socket sendfile, for offloading file send to AP_Networking
|
|
|
|
*/
|
|
|
|
int SocketAPM_sendfile(lua_State *L) {
|
|
|
|
binding_argcheck(L, 2);
|
|
|
|
|
|
|
|
SocketAPM *ud = *check_SocketAPM(L, 1);
|
|
|
|
|
|
|
|
auto *p = (luaL_Stream *)luaL_checkudata(L, 2, LUA_FILEHANDLE);
|
|
|
|
int fd = p->f->fd;
|
|
|
|
|
2024-01-03 19:23:57 -04:00
|
|
|
bool ret = fd != -1 && AP::network().sendfile(ud, fd);
|
|
|
|
if (ret) {
|
|
|
|
// the fd is no longer valid. The lua script must
|
|
|
|
// still call close() to release the memory from the
|
|
|
|
// socket
|
|
|
|
p->f->fd = -1;
|
2023-12-07 14:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L, ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-12-03 01:43:29 -04:00
|
|
|
/*
|
|
|
|
receive from a socket to a lua string
|
|
|
|
*/
|
|
|
|
int SocketAPM_recv(lua_State *L) {
|
|
|
|
binding_argcheck(L, 2);
|
|
|
|
|
|
|
|
SocketAPM * ud = *check_SocketAPM(L, 1);
|
|
|
|
|
|
|
|
const uint16_t count = get_uint16_t(L, 2);
|
|
|
|
uint8_t *data = (uint8_t*)malloc(count);
|
|
|
|
if (data == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto ret = ud->recv(data, count, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// push to lua string
|
|
|
|
lua_pushlstring(L, (const char *)data, ret);
|
|
|
|
free(data);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
TCP socket accept() call
|
|
|
|
*/
|
|
|
|
int SocketAPM_accept(lua_State *L) {
|
|
|
|
binding_argcheck(L, 1);
|
|
|
|
|
|
|
|
SocketAPM * ud = *check_SocketAPM(L, 1);
|
|
|
|
|
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
|
2024-01-03 19:23:57 -04:00
|
|
|
// find an empty slot
|
|
|
|
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_NET_SOCKET; i++) {
|
|
|
|
if (scripting->_net_sockets[i] == nullptr) {
|
|
|
|
scripting->_net_sockets[i] = ud->accept(0);
|
|
|
|
if (scripting->_net_sockets[i] == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
new_SocketAPM(L);
|
|
|
|
*((SocketAPM**)luaL_checkudata(L, -1, "SocketAPM")) = scripting->_net_sockets[i];
|
|
|
|
return 1;
|
|
|
|
}
|
2023-12-03 01:43:29 -04:00
|
|
|
}
|
|
|
|
|
2024-01-03 19:23:57 -04:00
|
|
|
// out of socket slots, return nil, caller can retry
|
|
|
|
return 0;
|
2023-12-03 01:43:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AP_NETWORKING_ENABLED
|
|
|
|
|
|
|
|
|
2023-03-09 00:47:50 -04:00
|
|
|
int lua_get_current_ref()
|
|
|
|
{
|
|
|
|
auto *scripting = AP::scripting();
|
|
|
|
return scripting->get_current_ref();
|
|
|
|
}
|
2023-06-07 02:56:50 -03:00
|
|
|
|
2024-02-22 21:22:03 -04:00
|
|
|
// This is used when loading modules with require, lua must only look in enabled directory's
|
|
|
|
const char* lua_get_modules_path()
|
|
|
|
{
|
|
|
|
#define LUA_PATH_ROMFS "@ROMFS/scripts/modules/?.lua;" "@ROMFS/scripts/modules/?/init.lua"
|
|
|
|
#define LUA_PATH_SCRIPTS LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua"
|
|
|
|
|
|
|
|
uint16_t dir_disable = AP_Scripting::get_singleton()->get_disabled_dir();
|
|
|
|
dir_disable &= uint16_t(AP_Scripting::SCR_DIR::SCRIPTS) | uint16_t(AP_Scripting::SCR_DIR::ROMFS);
|
|
|
|
if (dir_disable == 0) {
|
|
|
|
// Both directory's are enabled, return both, ROMFS takes priority
|
|
|
|
return LUA_PATH_ROMFS ";" LUA_PATH_SCRIPTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::SCRIPTS)) == 0) {
|
|
|
|
// Only scripts enabled
|
|
|
|
return LUA_PATH_SCRIPTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::ROMFS)) == 0) {
|
|
|
|
// Only ROMFS enabled
|
|
|
|
return LUA_PATH_ROMFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing enabled?
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-10-01 14:31:49 -03:00
|
|
|
// Simple print to GCS or over CAN
|
|
|
|
int lua_print(lua_State *L) {
|
|
|
|
// Only support a single argument
|
|
|
|
binding_argcheck(L, 1);
|
|
|
|
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_DEBUG, "%s", luaL_checkstring(L, 1));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-26 01:05:45 -04:00
|
|
|
#if (!defined(HAL_BUILD_AP_PERIPH) || defined(HAL_PERIPH_ENABLE_RANGEFINDER))
|
|
|
|
int lua_range_finder_handle_script_msg(lua_State *L) {
|
|
|
|
// Arg 1 => self (an instance of rangefinder_backend)
|
|
|
|
// Arg 2 => a float distance or a RangeFinder_State user data
|
|
|
|
binding_argcheck(L, 2);
|
|
|
|
|
|
|
|
// check_AP_RangeFinder_Backend aborts if not found. No need to check for null
|
|
|
|
AP_RangeFinder_Backend * ud = *check_AP_RangeFinder_Backend(L, 1);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
// Check to see if the first argument is the state structure.
|
|
|
|
const void *state_arg = luaL_testudata(L, 2, "RangeFinder_State");
|
|
|
|
if (state_arg != nullptr) {
|
|
|
|
result = ud->handle_script_msg(*static_cast<const RangeFinder::RangeFinder_State *>(state_arg));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Otherwise assume the argument is a number and set the measurement.
|
|
|
|
result = ud->handle_script_msg(luaL_checknumber(L, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L, result);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-02-06 18:35:42 -04:00
|
|
|
/*
|
|
|
|
lua wants to abort, and doesn't have access to a panic function
|
|
|
|
*/
|
|
|
|
void lua_abort()
|
|
|
|
{
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
|
|
|
#if AP_SIM_ENABLED
|
|
|
|
AP_HAL::panic("lua_abort called");
|
|
|
|
#else
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
AP_HAL::panic("lua_abort called");
|
|
|
|
}
|
|
|
|
// abort while flying, all we can do is loop
|
|
|
|
while (true) {
|
|
|
|
hal.scheduler->delay(1000);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-07 02:56:50 -03:00
|
|
|
#endif // AP_SCRIPTING_ENABLED
|