From 551486ffff618df381bf7d5a18c067a0c48eeba6 Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Fri, 27 Nov 2020 07:26:50 +0100 Subject: [PATCH] api: Add explicit close methods to classes This is needed for bindings to languages which perform implicit and lazy object cleanups. The explicit close methods allow to release resources when they are no longer required, permitting deterministic reuse. One example is node-red-node-intel-gpio which will use the new calls on node closing. Fixes #1044. Signed-off-by: Jan Kiszka --- api/mraa/aio.hpp | 11 +++++++++++ api/mraa/gpio.hpp | 11 +++++++++++ api/mraa/i2c.hpp | 12 ++++++++++++ api/mraa/iio.hpp | 13 ++++++++++++- api/mraa/led.hpp | 12 ++++++++++++ api/mraa/pwm.hpp | 11 +++++++++++ api/mraa/spi.hpp | 12 ++++++++++++ api/mraa/uart.hpp | 12 ++++++++++++ api/mraa/uart_ow.hpp | 12 ++++++++++++ 9 files changed, 105 insertions(+), 1 deletion(-) diff --git a/api/mraa/aio.hpp b/api/mraa/aio.hpp index f880b64..2db385f 100644 --- a/api/mraa/aio.hpp +++ b/api/mraa/aio.hpp @@ -75,8 +75,19 @@ class Aio * Aio destructor */ ~Aio() + { + if (m_aio != NULL) { + mraa_aio_close(m_aio); + } + } + /** + * Closes AIO explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_aio_close(m_aio); + m_aio = NULL; } /** * Read a value from the AIO pin. By default mraa will shift diff --git a/api/mraa/gpio.hpp b/api/mraa/gpio.hpp index 07ca03e..fad439e 100644 --- a/api/mraa/gpio.hpp +++ b/api/mraa/gpio.hpp @@ -143,8 +143,19 @@ class Gpio * the owner */ ~Gpio() + { + if (m_gpio != NULL) { + mraa_gpio_close(m_gpio); + } + } + /** + * Closes Gpio explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_gpio_close(m_gpio); + m_gpio = NULL; } /** * Set the edge mode for ISR diff --git a/api/mraa/i2c.hpp b/api/mraa/i2c.hpp index cc26b15..cf52b77 100644 --- a/api/mraa/i2c.hpp +++ b/api/mraa/i2c.hpp @@ -83,8 +83,20 @@ class I2c * slaves. */ ~I2c() + { + if (m_i2c != NULL) { + mraa_i2c_stop(m_i2c); + } + } + + /** + * Closes I2c explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_i2c_stop(m_i2c); + m_i2c = NULL; } /** diff --git a/api/mraa/iio.hpp b/api/mraa/iio.hpp index f9d8ff4..c1f2c67 100644 --- a/api/mraa/iio.hpp +++ b/api/mraa/iio.hpp @@ -131,9 +131,20 @@ class Iio */ ~Iio() { - mraa_iio_close(m_iio); + if (m_iio != NULL) { + mraa_iio_close(m_iio); + } } + /** + * Closes Iio explicitly, prior to implicit closing on object destruction + */ + void + close() + { + mraa_iio_close(m_iio); + m_iio = NULL; + } /** * Get device name diff --git a/api/mraa/led.hpp b/api/mraa/led.hpp index 8adc789..903823f 100644 --- a/api/mraa/led.hpp +++ b/api/mraa/led.hpp @@ -89,8 +89,20 @@ class Led * LED object destructor */ ~Led() + { + if (m_led != NULL) { + mraa_led_close(m_led); + } + } + + /* + * Closes LED explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_led_close(m_led); + m_led = NULL; } /** diff --git a/api/mraa/pwm.hpp b/api/mraa/pwm.hpp index 4bdfa71..66da1f3 100644 --- a/api/mraa/pwm.hpp +++ b/api/mraa/pwm.hpp @@ -86,8 +86,19 @@ class Pwm * Pwm destructor */ ~Pwm() + { + if (m_pwm != NULL) { + mraa_pwm_close(m_pwm); + } + } + /* + * Closes Pwm explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_pwm_close(m_pwm); + m_pwm = NULL; } /** * Set the output duty-cycle percentage, as a float diff --git a/api/mraa/spi.hpp b/api/mraa/spi.hpp index 26ead24..aa9d69d 100644 --- a/api/mraa/spi.hpp +++ b/api/mraa/spi.hpp @@ -105,8 +105,20 @@ class Spi * Closes spi bus */ ~Spi() + { + if (m_spi != NULL) { + mraa_spi_stop(m_spi); + } + } + + /** + * Closes Spi explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_spi_stop(m_spi); + m_spi = NULL; } /** diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index e9624db..056b690 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -95,8 +95,20 @@ class Uart * Uart destructor */ ~Uart() + { + if (m_uart != NULL) { + mraa_uart_stop(m_uart); + } + } + + /* + * Closes Uart explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_uart_stop(m_uart); + m_uart = NULL; } /** diff --git a/api/mraa/uart_ow.hpp b/api/mraa/uart_ow.hpp index db44e9d..e64a14a 100644 --- a/api/mraa/uart_ow.hpp +++ b/api/mraa/uart_ow.hpp @@ -95,8 +95,20 @@ class UartOW * Uart destructor */ ~UartOW() + { + if (m_uart != NULL) { + mraa_uart_ow_stop(m_uart); + } + } + + /* + * Closes UartOW explicitly, prior to implicit closing on object destruction + */ + void + close() { mraa_uart_ow_stop(m_uart); + m_uart = NULL; } /**