From 0e89a6b8bd5ffb02f6e364e34678628de79ab378 Mon Sep 17 00:00:00 2001 From: acfloria Date: Tue, 13 Mar 2018 15:05:35 +0100 Subject: [PATCH] IridiumSBD: Do not allow stopping the driver when the device is used to prevent hardfault --- .../telemetry/iridiumsbd/IridiumSBD.cpp | 19 +++++++++++++ src/drivers/telemetry/iridiumsbd/IridiumSBD.h | 28 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp b/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp index 665d1ad09b..01e9eee837 100644 --- a/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp +++ b/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp @@ -91,6 +91,11 @@ int IridiumSBD::stop() return PX4_ERROR; } + if (IridiumSBD::instance->cdev_used) { + PX4_WARN("device is used. Stop all users (MavLink)"); + return PX4_ERROR; + } + PX4_WARN("stopping..."); IridiumSBD::instance->task_should_exit = true; @@ -922,6 +927,20 @@ void IridiumSBD::publish_telemetry_status() } } +int IridiumSBD::open_first(struct file *filep) +{ + cdev_used = true; + return CDev::open_first(filep); +} + +int IridiumSBD::close_last(struct file *filep) +{ + cdev_used = false; + return CDev::close_last(filep); +} + + + int iridiumsbd_main(int argc, char *argv[]) { if (!strcmp(argv[1], "start")) { diff --git a/src/drivers/telemetry/iridiumsbd/IridiumSBD.h b/src/drivers/telemetry/iridiumsbd/IridiumSBD.h index 2d7acc96f5..2deaa6441c 100644 --- a/src/drivers/telemetry/iridiumsbd/IridiumSBD.h +++ b/src/drivers/telemetry/iridiumsbd/IridiumSBD.h @@ -261,6 +261,32 @@ private: */ void publish_telemetry_status(void); + /** + * Notification of the first open of CDev. + * + * This function is called when the device open count transitions from zero + * to one. The driver lock is held for the duration of the call. + * + * Notes that CDev is used and blocks stopping the driver. + * + * @param filep Pointer to the NuttX file structure. + * @return OK if the open should proceed, -errno otherwise. + */ + virtual int open_first(struct file *filep) override; + + /** + * Notification of the last close of CDev. + * + * This function is called when the device open count transitions from + * one to zero. The driver lock is held for the duration of the call. + * + * Notes that CDev is not used anymore and allows stopping the driver. + * + * @param filep Pointer to the NuttX file structure. + * @return OK if the open should return OK, -errno otherwise. + */ + virtual int close_last(struct file *filep) override; + static IridiumSBD *instance; static int task_handle; bool task_should_exit = false; @@ -295,6 +321,8 @@ private: bool rx_read_pending = false; bool tx_session_pending = false; + bool cdev_used = false; + hrt_abstime last_write_time = 0; hrt_abstime last_read_time = 0; hrt_abstime last_heartbeat = 0;