mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 16:51:16 +08:00
* fix(heater): open and use the PX4IO fd only on the work queue thread
File descriptors on NuttX are per task group. Since the multi-instance
rework (#26325) the PX4IO heater fd was opened in the constructor, i.e.
in the shell task running `heater start`, but used in Run() on
wq:lp_default. There the fd number refers to whatever that task group has
open, so the heater ioctls silently failed (heater never driven on
PX4IO boards) or hit an unrelated file. On a Cube Orange+ this ended in a
hardfault in fat_ioctl() when the number matched an SD card file.
stop() also issued an ioctl from the shell, and the exit path closed the
fd before turning the heater off with it.
Assisted-by: Claude:claude-opus-5-5
* fix(cyphal): close the CAN fd on the work queue thread
The CAN socket/device is opened in Run() on wq:uavcan, but was only
closed from the destructor, which runs in the shell calling
`cyphal stop`. File descriptors are per task group on NuttX, so that
close did nothing and the SocketCAN socket stayed bound, keeping
received frames queued in the shared IOB pool. The NuttX CAN device was
never closed at all.
Close the interface in Run()'s exit branch and make close() idempotent.
Assisted-by: Claude:claude-opus-5-5
* fix(uavcannode): close the DroneCAN shell pipes on the work queue thread
The shell pipes are created from a service callback on wq:uavcan, but on
`uavcannode stop` they were closed by the destructor in the shell task.
File descriptors are per task group on NuttX, so the work queue ends
stayed open and the spawned shell task never got EOF.
Assisted-by: Claude:claude-opus-5-5
* fix(distance_sensor): close serial ports on the work queue thread
CM8JL65, uLanding, LeddarOne, Sony AS-DT1 and Lightware serial open
their UART in Run() on the work queue, but stop() closed it from the
shell calling `<driver> stop`. File descriptors are per task group on
NuttX, so the UART stayed open in the work queue group, leaking it on
every start/stop cycle. stop() also deleted the driver without waiting
for a Run() in progress.
stop() now asks Run() to close the port and waits for it. Error paths
reset the fd so a stale number is not reused, and ports opened in the
shell (LeddarOne init probe, Sony -p mode) are closed there.
Assisted-by: Claude:claude-opus-5-5
* fix(uwb_sr150): schedule only after init succeeded
On init failure the instance is deleted in the shell, whose destructor
closes the UART. If Run() had already opened it on the work queue, that
closed an unrelated fd number, as file descriptors are per task group
on NuttX.
Assisted-by: Claude:claude-opus-5-5
* fix(sagetech_mxs): schedule only after init succeeded
On init failure the instance is deleted in the shell, whose destructor
closes the serial port. If Run() had already opened it on the work
queue, that closed an unrelated fd number, as file descriptors are per
task group on NuttX.
Assisted-by: Claude:claude-opus-5-5
---------
(cherry picked from commit 35223a381e)
Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
1 parent
530ef17a55
commit
e0e68e494d
19 files changed
+250
-43
No files matched your search
@@ -84,6 +84,13 @@ CanardHandle::CanardHandle(uint32_t node_id, const size_t capacity, const size_t
|
||||
|
||||
}
|
||||
|
||||
void CanardHandle::close()
|
||||
{
|
||||
if (_can_interface) {
|
||||
_can_interface->close();
|
||||
}
|
||||
}
|
||||
|
||||
CanardHandle::~CanardHandle()
|
||||
{
|
||||
if (_can_interface) {
|
||||
|
||||
@@ -56,6 +56,10 @@ public:
|
||||
void receive();
|
||||
void transmit();
|
||||
|
||||
/// Close the CAN interface. File descriptors are per task group on NuttX,
|
||||
/// so this must be called from the thread that opened it (the work queue).
|
||||
void close();
|
||||
|
||||
int32_t TxPush(const CanardMicrosecond tx_deadline_usec,
|
||||
const CanardTransferMetadata *const metadata,
|
||||
const size_t payload_size,
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
|
||||
#include <canard.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "CanardInterface.hpp"
|
||||
|
||||
@@ -61,6 +62,18 @@ public:
|
||||
/// The return value is number of bytes received, negative value on error.
|
||||
int16_t receive(CanardRxFrame *rxf);
|
||||
|
||||
/// Close the CAN device
|
||||
int close() override
|
||||
{
|
||||
if (_fd < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int ret = ::close(_fd);
|
||||
_fd = -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
int _fd{-1};
|
||||
bool _can_fd{false};
|
||||
|
||||
@@ -65,7 +65,13 @@ public:
|
||||
/// Close socket connection
|
||||
int close()
|
||||
{
|
||||
return ::close(_fd);
|
||||
if (_fd < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int ret = ::close(_fd);
|
||||
_fd = -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Send a CanardFrame to the CanardSocketInstance socket
|
||||
|
||||
@@ -225,6 +225,10 @@ void CyphalNode::Run()
|
||||
if (_instance && _task_should_exit.load()) {
|
||||
ScheduleClear();
|
||||
|
||||
// The CAN fd belongs to the work queue task group, so close it here
|
||||
// and not in the destructor, which runs in the shell calling stop.
|
||||
_canard_handle.close();
|
||||
|
||||
if (_initialized) {
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ int
|
||||
CM8JL65::open_serial_port(const speed_t speed)
|
||||
{
|
||||
// File descriptor initialized?
|
||||
if (_file_descriptor > 0) {
|
||||
if (_file_descriptor >= 0) {
|
||||
// PX4_INFO("serial port already open");
|
||||
return PX4_OK;
|
||||
}
|
||||
@@ -315,7 +315,7 @@ CM8JL65::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("CFG: %d ISPD", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ CM8JL65::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("CFG: %d OSPD", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -333,7 +333,7 @@ CM8JL65::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("baud %d ATTR", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -351,6 +351,13 @@ CM8JL65::print_info()
|
||||
void
|
||||
CM8JL65::Run()
|
||||
{
|
||||
if (_stop_requested.load()) {
|
||||
ScheduleClear();
|
||||
close_serial_port();
|
||||
_stopped.store(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure the serial port is open.
|
||||
open_serial_port();
|
||||
|
||||
@@ -361,6 +368,9 @@ CM8JL65::Run()
|
||||
void
|
||||
CM8JL65::start()
|
||||
{
|
||||
_stop_requested.store(false);
|
||||
_stopped.store(false);
|
||||
|
||||
// Schedule the driver at regular intervals.
|
||||
ScheduleOnInterval(CM8JL65_MEASURE_INTERVAL);
|
||||
|
||||
@@ -370,9 +380,24 @@ CM8JL65::start()
|
||||
void
|
||||
CM8JL65::stop()
|
||||
{
|
||||
// Clear the work queue schedule.
|
||||
ScheduleClear();
|
||||
// The serial port is opened in Run() on the work queue. File descriptors are
|
||||
// per task group on NuttX, so it must also be closed there and not from the
|
||||
// shell calling stop. Let Run() close it and wait until it has.
|
||||
_stop_requested.store(true);
|
||||
ScheduleNow();
|
||||
|
||||
// Ensure the serial port is closed.
|
||||
::close(_file_descriptor);
|
||||
for (int i = 0; i < 100 && !_stopped.load(); i++) {
|
||||
px4_usleep(10000);
|
||||
}
|
||||
|
||||
ScheduleClear();
|
||||
}
|
||||
|
||||
void
|
||||
CM8JL65::close_serial_port()
|
||||
{
|
||||
if (_file_descriptor >= 0) {
|
||||
::close(_file_descriptor);
|
||||
_file_descriptor = -1;
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,8 @@
|
||||
#include <drivers/rangefinder/PX4Rangefinder.hpp>
|
||||
#include <perf/perf_counter.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
|
||||
|
||||
@@ -146,6 +148,11 @@ private:
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* Closes the serial port, must be called from the thread that opened it.
|
||||
*/
|
||||
void close_serial_port();
|
||||
|
||||
|
||||
PX4Rangefinder _px4_rangefinder;
|
||||
|
||||
@@ -155,6 +162,9 @@ private:
|
||||
|
||||
int _file_descriptor{-1};
|
||||
|
||||
px4::atomic_bool _stop_requested{false};
|
||||
px4::atomic_bool _stopped{false};
|
||||
|
||||
uint8_t _linebuf[25] {};
|
||||
|
||||
uint16_t _crc16{0};
|
||||
|
||||
@@ -168,7 +168,7 @@ LeddarOne::init()
|
||||
if (collect() == PX4_OK) {
|
||||
// The file descriptor can only be accessed by the process that opened it,
|
||||
// so closing here allows the port to be opened from scheduled work queue.
|
||||
stop();
|
||||
close_serial_port();
|
||||
return PX4_OK;
|
||||
}
|
||||
}
|
||||
@@ -178,6 +178,7 @@ LeddarOne::init()
|
||||
}
|
||||
|
||||
PX4_ERR("No readings from LeddarOne");
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -203,7 +204,7 @@ int
|
||||
LeddarOne::open_serial_port(const speed_t speed)
|
||||
{
|
||||
// File descriptor already initialized?
|
||||
if (_file_descriptor > 0) {
|
||||
if (_file_descriptor >= 0) {
|
||||
// PX4_INFO("serial port already open");
|
||||
return PX4_OK;
|
||||
}
|
||||
@@ -224,8 +225,7 @@ LeddarOne::open_serial_port(const speed_t speed)
|
||||
// Store the current port configuration. attributes.
|
||||
if (tcgetattr(_file_descriptor, &uart_config)) {
|
||||
PX4_ERR("Unable to get termios from %s.", _serial_port);
|
||||
::close(_file_descriptor);
|
||||
_file_descriptor = -1;
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ LeddarOne::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("CFG: %d ISPD", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ LeddarOne::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("CFG: %d OSPD", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -264,7 +264,7 @@ LeddarOne::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("baud %d ATTR", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -286,6 +286,13 @@ LeddarOne::print_info()
|
||||
void
|
||||
LeddarOne::Run()
|
||||
{
|
||||
if (_stop_requested.load()) {
|
||||
ScheduleClear();
|
||||
close_serial_port();
|
||||
_stopped.store(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure the serial port is open.
|
||||
open_serial_port();
|
||||
|
||||
@@ -295,6 +302,9 @@ LeddarOne::Run()
|
||||
void
|
||||
LeddarOne::start()
|
||||
{
|
||||
_stop_requested.store(false);
|
||||
_stopped.store(false);
|
||||
|
||||
// Schedule the driver at regular intervals.
|
||||
ScheduleOnInterval(LEDDAR_ONE_MEASURE_INTERVAL, LEDDAR_ONE_MEASURE_INTERVAL);
|
||||
}
|
||||
@@ -302,10 +312,24 @@ LeddarOne::start()
|
||||
void
|
||||
LeddarOne::stop()
|
||||
{
|
||||
// Ensure the serial port is closed.
|
||||
::close(_file_descriptor);
|
||||
_file_descriptor = -1;
|
||||
// The serial port is opened in Run() on the work queue. File descriptors are
|
||||
// per task group on NuttX, so it must also be closed there and not from the
|
||||
// shell calling stop. Let Run() close it and wait until it has.
|
||||
_stop_requested.store(true);
|
||||
ScheduleNow();
|
||||
|
||||
for (int i = 0; i < 100 && !_stopped.load(); i++) {
|
||||
px4_usleep(10000);
|
||||
}
|
||||
|
||||
// Clear the work queue schedule.
|
||||
ScheduleClear();
|
||||
}
|
||||
|
||||
void
|
||||
LeddarOne::close_serial_port()
|
||||
{
|
||||
if (_file_descriptor >= 0) {
|
||||
::close(_file_descriptor);
|
||||
_file_descriptor = -1;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@
|
||||
#include <lib/drivers/rangefinder/PX4Rangefinder.hpp>
|
||||
#include <lib/perf/perf_counter.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <px4_platform_common/defines.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
|
||||
@@ -117,6 +119,11 @@ public:
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* Closes the serial port, must be called from the thread that opened it.
|
||||
*/
|
||||
void close_serial_port();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
@@ -150,6 +157,9 @@ private:
|
||||
|
||||
int _file_descriptor{-1};
|
||||
|
||||
px4::atomic_bool _stop_requested{false};
|
||||
px4::atomic_bool _stopped{false};
|
||||
|
||||
uint8_t _buffer[sizeof(reading_msg)];
|
||||
uint8_t _buffer_len{0};
|
||||
|
||||
|
||||
@@ -250,6 +250,8 @@ void LightwareLaserSerial::start()
|
||||
{
|
||||
/* reset the report ring and state machine */
|
||||
_collect_phase = false;
|
||||
_stop_requested.store(false);
|
||||
_stopped.store(false);
|
||||
|
||||
/* schedule a cycle to start things */
|
||||
ScheduleNow();
|
||||
@@ -257,12 +259,28 @@ void LightwareLaserSerial::start()
|
||||
|
||||
void LightwareLaserSerial::stop()
|
||||
{
|
||||
// The serial port is opened in Run() on the work queue. File descriptors are
|
||||
// per task group on NuttX, so it must also be closed there and not from the
|
||||
// shell calling stop. Let Run() close it and wait until it has.
|
||||
_stop_requested.store(true);
|
||||
ScheduleNow();
|
||||
|
||||
for (int i = 0; i < 100 && !_stopped.load(); i++) {
|
||||
px4_usleep(10000);
|
||||
}
|
||||
|
||||
ScheduleClear();
|
||||
_uart.close();
|
||||
}
|
||||
|
||||
void LightwareLaserSerial::Run()
|
||||
{
|
||||
if (_stop_requested.load()) {
|
||||
ScheduleClear();
|
||||
_uart.close();
|
||||
_stopped.store(true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_uart.isOpen()) {
|
||||
if (!_uart.setPort(_port)) {
|
||||
PX4_ERR("failed to configure port %s", _port);
|
||||
|
||||
@@ -42,6 +42,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <px4_platform_common/Serial.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <lib/drivers/rangefinder/PX4Rangefinder.hpp>
|
||||
@@ -72,6 +74,9 @@ private:
|
||||
PX4Rangefinder _px4_rangefinder;
|
||||
device::Serial _uart;
|
||||
|
||||
px4::atomic_bool _stop_requested{false};
|
||||
px4::atomic_bool _stopped{false};
|
||||
|
||||
char _port[20] {};
|
||||
int _interval{100000};
|
||||
bool _collect_phase{false};
|
||||
|
||||
@@ -139,6 +139,9 @@ int AS_DT1::init()
|
||||
PX4_WARN("flshow: no complete response after %u tries", static_cast<unsigned>(FLSHOW_RETRY_LIMIT));
|
||||
}
|
||||
|
||||
// Opened in the shell task, so close it here as well.
|
||||
close_port();
|
||||
|
||||
} else {
|
||||
start();
|
||||
}
|
||||
@@ -226,18 +229,36 @@ void AS_DT1::print_info()
|
||||
|
||||
void AS_DT1::start()
|
||||
{
|
||||
_stop_requested.store(false);
|
||||
_stopped.store(false);
|
||||
_current_baud = ASDT1_DESIRED_BAUD;
|
||||
ScheduleNow();
|
||||
}
|
||||
|
||||
void AS_DT1::stop()
|
||||
{
|
||||
// The serial port is opened in Run() on the work queue. File descriptors are
|
||||
// per task group on NuttX, so it must also be closed there and not from the
|
||||
// shell calling stop. Let Run() close it and wait until it has.
|
||||
_stop_requested.store(true);
|
||||
ScheduleNow();
|
||||
|
||||
for (int i = 0; i < 100 && !_stopped.load(); i++) {
|
||||
px4_usleep(10000);
|
||||
}
|
||||
|
||||
ScheduleClear();
|
||||
close_port();
|
||||
}
|
||||
|
||||
void AS_DT1::Run()
|
||||
{
|
||||
if (_stop_requested.load()) {
|
||||
ScheduleClear();
|
||||
close_port();
|
||||
_stopped.store(true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_fd < 0) {
|
||||
if (open_port() != PX4_OK) {
|
||||
return;
|
||||
|
||||
@@ -41,6 +41,8 @@
|
||||
|
||||
#include <px4_platform_common/module.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <uORB/Publication.hpp>
|
||||
@@ -169,6 +171,9 @@ private:
|
||||
uORB::Publication<obstacle_distance_s> _obstacle_distance_pub{ORB_ID(obstacle_distance)};
|
||||
|
||||
int _fd{-1};
|
||||
|
||||
px4::atomic_bool _stop_requested{false};
|
||||
px4::atomic_bool _stopped{false};
|
||||
int _read_interval_us{2000};
|
||||
char _device[20] {};
|
||||
bool _print_config_only{false};
|
||||
|
||||
@@ -146,7 +146,7 @@ int AerotennaULanding::collect()
|
||||
int AerotennaULanding::open_serial_port(const speed_t speed)
|
||||
{
|
||||
// File descriptor initialized?
|
||||
if (_file_descriptor > 0) {
|
||||
if (_file_descriptor >= 0) {
|
||||
PX4_DEBUG("serial port already open");
|
||||
return PX4_OK;
|
||||
}
|
||||
@@ -164,6 +164,7 @@ int AerotennaULanding::open_serial_port(const speed_t speed)
|
||||
|
||||
if (!isatty(_file_descriptor)) {
|
||||
PX4_WARN("not a serial device");
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -188,7 +189,7 @@ int AerotennaULanding::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("CFG: %d ISPD", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -197,7 +198,7 @@ int AerotennaULanding::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("CFG: %d OSPD", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -206,7 +207,7 @@ int AerotennaULanding::open_serial_port(const speed_t speed)
|
||||
|
||||
if (termios_state < 0) {
|
||||
PX4_ERR("baud %d ATTR", termios_state);
|
||||
::close(_file_descriptor);
|
||||
close_serial_port();
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
@@ -216,6 +217,13 @@ int AerotennaULanding::open_serial_port(const speed_t speed)
|
||||
|
||||
void AerotennaULanding::Run()
|
||||
{
|
||||
if (_stop_requested.load()) {
|
||||
ScheduleClear();
|
||||
close_serial_port();
|
||||
_stopped.store(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure the serial port is open.
|
||||
open_serial_port();
|
||||
|
||||
@@ -224,19 +232,36 @@ void AerotennaULanding::Run()
|
||||
|
||||
void AerotennaULanding::start()
|
||||
{
|
||||
_stop_requested.store(false);
|
||||
_stopped.store(false);
|
||||
|
||||
// Schedule the driver at regular intervals.
|
||||
ScheduleOnInterval(ULANDING_MEASURE_INTERVAL, 0);
|
||||
}
|
||||
|
||||
void AerotennaULanding::stop()
|
||||
{
|
||||
// Ensure the serial port is closed.
|
||||
::close(_file_descriptor);
|
||||
// The serial port is opened in Run() on the work queue. File descriptors are
|
||||
// per task group on NuttX, so it must also be closed there and not from the
|
||||
// shell calling stop. Let Run() close it and wait until it has.
|
||||
_stop_requested.store(true);
|
||||
ScheduleNow();
|
||||
|
||||
for (int i = 0; i < 100 && !_stopped.load(); i++) {
|
||||
px4_usleep(10000);
|
||||
}
|
||||
|
||||
// Clear the work queue schedule.
|
||||
ScheduleClear();
|
||||
}
|
||||
|
||||
void AerotennaULanding::close_serial_port()
|
||||
{
|
||||
if (_file_descriptor >= 0) {
|
||||
::close(_file_descriptor);
|
||||
_file_descriptor = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void AerotennaULanding::print_info()
|
||||
{
|
||||
perf_print_counter(_sample_perf);
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
#include <lib/perf/perf_counter.h>
|
||||
#include <lib/drivers/rangefinder/PX4Rangefinder.hpp>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <px4_platform_common/defines.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <lib/perf/perf_counter.h>
|
||||
@@ -107,12 +109,20 @@ private:
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* Closes the serial port, must be called from the thread that opened it.
|
||||
*/
|
||||
void close_serial_port();
|
||||
|
||||
PX4Rangefinder _px4_rangefinder;
|
||||
|
||||
char _port[20] {};
|
||||
|
||||
int _file_descriptor{-1};
|
||||
|
||||
px4::atomic_bool _stop_requested{false};
|
||||
px4::atomic_bool _stopped{false};
|
||||
|
||||
uint8_t _buffer[ULANDING_BUFFER_LENGTH] {};
|
||||
|
||||
perf_counter_t _comms_errors{perf_alloc(PC_COUNT, MODULE_NAME": com_err")};
|
||||
|
||||
@@ -107,8 +107,6 @@ Heater::Heater(uint8_t instance) :
|
||||
ModuleParams(nullptr),
|
||||
_instance(instance)
|
||||
{
|
||||
initialize_heater_io();
|
||||
|
||||
char name[32];
|
||||
|
||||
// Dynamically locate the parameter handle corresponding to the current instance
|
||||
@@ -363,6 +361,9 @@ bool Heater::initialize_topics()
|
||||
(unsigned)_instance, use_hygro ? "hygrometer" : "accel",
|
||||
selected_instance, (unsigned long)_sensor_device_id);
|
||||
|
||||
// Called from Run(), so the PX4IO fd is opened on the work queue thread.
|
||||
initialize_heater_io();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -371,18 +372,22 @@ bool Heater::initialize_topics()
|
||||
void Heater::Run()
|
||||
{
|
||||
if (_should_exit) {
|
||||
#if defined(HEATER_PX4IO)
|
||||
|
||||
// must be closed from wq thread
|
||||
if (_io_fd >= 0) {
|
||||
px4_close(_io_fd);
|
||||
}
|
||||
|
||||
#endif
|
||||
ScheduleClear();
|
||||
|
||||
// Ensure heating is turned off (avoid leaving it in the ON position).
|
||||
heater_off();
|
||||
disable_heater();
|
||||
|
||||
#if defined(HEATER_PX4IO)
|
||||
|
||||
// File descriptors are per task group, so the PX4IO fd is only valid
|
||||
// on the work queue thread: open, use and close it only from here.
|
||||
if (_io_fd >= 0) {
|
||||
px4_close(_io_fd);
|
||||
_io_fd = -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
delete Heater::g_heater[_instance - 1];
|
||||
Heater::g_heater[_instance - 1] = nullptr;
|
||||
@@ -562,8 +567,9 @@ int Heater::start()
|
||||
|
||||
void Heater::stop()
|
||||
{
|
||||
// Turning the heater off is done in Run() on the work queue thread,
|
||||
// which owns the PX4IO file descriptor.
|
||||
_should_exit = true;
|
||||
heater_off();
|
||||
ScheduleNow();
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +110,6 @@ int SagetechMXS::task_spawn(int argc, char *argv[])
|
||||
|
||||
bool SagetechMXS::init()
|
||||
{
|
||||
ScheduleOnInterval(UPDATE_INTERVAL_US); // 50Hz
|
||||
mxs_state.initialized = false;
|
||||
mxs_state.init_failed = false;
|
||||
|
||||
@@ -132,6 +131,11 @@ bool SagetechMXS::init()
|
||||
list_size_allocated = _adsb_list_max.get();
|
||||
}
|
||||
|
||||
// Schedule only once init succeeded: on failure the instance is deleted in
|
||||
// the shell, which must not happen after Run() opened the serial port on the
|
||||
// work queue (file descriptors are per task group on NuttX).
|
||||
ScheduleOnInterval(UPDATE_INTERVAL_US); // 50Hz
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -709,6 +709,14 @@ void UavcanNode::Run()
|
||||
|
||||
if (_task_should_exit.load()) {
|
||||
ScheduleClear();
|
||||
|
||||
#if defined(CONFIG_UAVCANNODE_COMMAND_SHELL)
|
||||
// The shell pipes were opened on this work queue and file descriptors
|
||||
// are per task group, so close them here rather than in the destructor
|
||||
// (which runs in the shell calling stop). Closing gives the shell EOF.
|
||||
close_shell();
|
||||
#endif // CONFIG_UAVCANNODE_COMMAND_SHELL
|
||||
|
||||
_instance = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,9 +253,11 @@ int UWB_SR150::task_spawn(int argc, char *argv[])
|
||||
desc.object.store(instance);
|
||||
desc.task_id = task_id_is_work_queue;
|
||||
|
||||
instance->ScheduleOnInterval(5000_us);
|
||||
|
||||
// Initialize before scheduling: on failure the instance is deleted here in
|
||||
// the shell, which must not happen after Run() opened the UART on the
|
||||
// work queue (file descriptors are per task group on NuttX).
|
||||
if (instance->init()) {
|
||||
instance->ScheduleOnInterval(5000_us);
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user