diff --git a/src/drivers/cyphal/CanardHandle.cpp b/src/drivers/cyphal/CanardHandle.cpp index 50c575bea5e..c6b3e385175 100644 --- a/src/drivers/cyphal/CanardHandle.cpp +++ b/src/drivers/cyphal/CanardHandle.cpp @@ -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) { diff --git a/src/drivers/cyphal/CanardHandle.hpp b/src/drivers/cyphal/CanardHandle.hpp index 63682442e6d..8e5991cd091 100644 --- a/src/drivers/cyphal/CanardHandle.hpp +++ b/src/drivers/cyphal/CanardHandle.hpp @@ -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, diff --git a/src/drivers/cyphal/CanardNuttXCDev.hpp b/src/drivers/cyphal/CanardNuttXCDev.hpp index e9d345afd09..efbcfbbd08e 100644 --- a/src/drivers/cyphal/CanardNuttXCDev.hpp +++ b/src/drivers/cyphal/CanardNuttXCDev.hpp @@ -36,6 +36,7 @@ #include #include +#include #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}; diff --git a/src/drivers/cyphal/CanardSocketCAN.hpp b/src/drivers/cyphal/CanardSocketCAN.hpp index ba93b48dbe3..6c3b17bcc62 100644 --- a/src/drivers/cyphal/CanardSocketCAN.hpp +++ b/src/drivers/cyphal/CanardSocketCAN.hpp @@ -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 diff --git a/src/drivers/cyphal/Cyphal.cpp b/src/drivers/cyphal/Cyphal.cpp index f6b411ac657..5846c867390 100644 --- a/src/drivers/cyphal/Cyphal.cpp +++ b/src/drivers/cyphal/Cyphal.cpp @@ -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; } diff --git a/src/drivers/distance_sensor/cm8jl65/CM8JL65.cpp b/src/drivers/distance_sensor/cm8jl65/CM8JL65.cpp index 89f71ee1c0b..c896efa5956 100644 --- a/src/drivers/distance_sensor/cm8jl65/CM8JL65.cpp +++ b/src/drivers/distance_sensor/cm8jl65/CM8JL65.cpp @@ -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; + } } diff --git a/src/drivers/distance_sensor/cm8jl65/CM8JL65.hpp b/src/drivers/distance_sensor/cm8jl65/CM8JL65.hpp index aa12c3d1406..1e5be87e82d 100644 --- a/src/drivers/distance_sensor/cm8jl65/CM8JL65.hpp +++ b/src/drivers/distance_sensor/cm8jl65/CM8JL65.hpp @@ -49,6 +49,8 @@ #include #include #include +#include +#include #include @@ -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}; diff --git a/src/drivers/distance_sensor/leddar_one/LeddarOne.cpp b/src/drivers/distance_sensor/leddar_one/LeddarOne.cpp index 5dd1a58d9d7..e3eb65af935 100644 --- a/src/drivers/distance_sensor/leddar_one/LeddarOne.cpp +++ b/src/drivers/distance_sensor/leddar_one/LeddarOne.cpp @@ -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; + } +} diff --git a/src/drivers/distance_sensor/leddar_one/LeddarOne.hpp b/src/drivers/distance_sensor/leddar_one/LeddarOne.hpp index d1841a369ab..f3f5690af46 100644 --- a/src/drivers/distance_sensor/leddar_one/LeddarOne.hpp +++ b/src/drivers/distance_sensor/leddar_one/LeddarOne.hpp @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include #include @@ -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}; diff --git a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp index 7bba405adb5..52bf0e6e054 100644 --- a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp +++ b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.cpp @@ -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); diff --git a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp index 513ab7e98c0..dcb4b1a23ec 100644 --- a/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp +++ b/src/drivers/distance_sensor/lightware_laser_serial/lightware_laser_serial.hpp @@ -42,6 +42,8 @@ #pragma once #include +#include +#include #include #include #include @@ -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}; diff --git a/src/drivers/distance_sensor/sony/sony_asdt1.cpp b/src/drivers/distance_sensor/sony/sony_asdt1.cpp index b0299c663e6..f0e867b5464 100644 --- a/src/drivers/distance_sensor/sony/sony_asdt1.cpp +++ b/src/drivers/distance_sensor/sony/sony_asdt1.cpp @@ -139,6 +139,9 @@ int AS_DT1::init() PX4_WARN("flshow: no complete response after %u tries", static_cast(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; diff --git a/src/drivers/distance_sensor/sony/sony_asdt1.hpp b/src/drivers/distance_sensor/sony/sony_asdt1.hpp index e1195e6a022..4559dab8125 100644 --- a/src/drivers/distance_sensor/sony/sony_asdt1.hpp +++ b/src/drivers/distance_sensor/sony/sony_asdt1.hpp @@ -41,6 +41,8 @@ #include #include +#include +#include #include #include #include @@ -169,6 +171,9 @@ private: uORB::Publication _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}; diff --git a/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.cpp b/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.cpp index ffd2c81a7ab..28b36269a3c 100644 --- a/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.cpp +++ b/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.cpp @@ -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); diff --git a/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.hpp b/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.hpp index 98d1c0ab904..e25bdd837cc 100644 --- a/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.hpp +++ b/src/drivers/distance_sensor/ulanding_radar/AerotennaULanding.hpp @@ -50,6 +50,8 @@ #include #include #include +#include +#include #include #include #include @@ -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")}; diff --git a/src/drivers/heater/heater.cpp b/src/drivers/heater/heater.cpp index 4abf3d665aa..dc3dd4b4403 100644 --- a/src/drivers/heater/heater.cpp +++ b/src/drivers/heater/heater.cpp @@ -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(); } diff --git a/src/drivers/transponder/sagetech_mxs/SagetechMXS.cpp b/src/drivers/transponder/sagetech_mxs/SagetechMXS.cpp index 77e373526dc..f76de28e20b 100644 --- a/src/drivers/transponder/sagetech_mxs/SagetechMXS.cpp +++ b/src/drivers/transponder/sagetech_mxs/SagetechMXS.cpp @@ -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; } diff --git a/src/drivers/uavcannode/UavcanNode.cpp b/src/drivers/uavcannode/UavcanNode.cpp index 49b18d16041..dbeccb6a582 100644 --- a/src/drivers/uavcannode/UavcanNode.cpp +++ b/src/drivers/uavcannode/UavcanNode.cpp @@ -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; } } diff --git a/src/drivers/uwb/uwb_sr150/uwb_sr150.cpp b/src/drivers/uwb/uwb_sr150/uwb_sr150.cpp index c18eee94986..d2c01813d9b 100644 --- a/src/drivers/uwb/uwb_sr150/uwb_sr150.cpp +++ b/src/drivers/uwb/uwb_sr150/uwb_sr150.cpp @@ -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; }