exception: Add exceptions when context creation fails
* Exceptions only fired in constructor when it would initialise with a NULL context causing segfaults if used any further * Adds exception.i requirement to mraa.i for node.js and python support Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include "aio.h"
|
#include "aio.h"
|
||||||
|
|
||||||
namespace mraa {
|
namespace mraa {
|
||||||
@@ -45,6 +46,9 @@ class Aio {
|
|||||||
*/
|
*/
|
||||||
Aio(unsigned int pin) {
|
Aio(unsigned int pin) {
|
||||||
m_aio = mraa_aio_init(pin);
|
m_aio = mraa_aio_init(pin);
|
||||||
|
if (m_aio == NULL) {
|
||||||
|
throw std::invalid_argument("Invalid AIO pin specified - do you have an ADC?");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Aio destructor
|
* Aio destructor
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace mraa {
|
namespace mraa {
|
||||||
|
|
||||||
@@ -80,13 +81,21 @@ class Gpio {
|
|||||||
* you so this may not always work as expected.
|
* you so this may not always work as expected.
|
||||||
*/
|
*/
|
||||||
Gpio(int pin, bool owner=true, bool raw=false) {
|
Gpio(int pin, bool owner=true, bool raw=false) {
|
||||||
if (raw)
|
if (raw) {
|
||||||
m_gpio = mraa_gpio_init_raw(pin);
|
m_gpio = mraa_gpio_init_raw(pin);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
m_gpio = mraa_gpio_init(pin);
|
m_gpio = mraa_gpio_init(pin);
|
||||||
if (!owner)
|
}
|
||||||
|
|
||||||
|
if (m_gpio == NULL) {
|
||||||
|
throw std::invalid_argument("Invalid GPIO pin specified");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!owner) {
|
||||||
mraa_gpio_owner(m_gpio, 0);
|
mraa_gpio_owner(m_gpio, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Gpio object destructor, this will only unexport the gpio if we where
|
* Gpio object destructor, this will only unexport the gpio if we where
|
||||||
* the owner
|
* the owner
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace mraa {
|
namespace mraa {
|
||||||
|
|
||||||
@@ -48,11 +49,16 @@ class I2c {
|
|||||||
* @param raw Whether to disable pinmapper for your board
|
* @param raw Whether to disable pinmapper for your board
|
||||||
*/
|
*/
|
||||||
I2c(int bus, bool raw=false) {
|
I2c(int bus, bool raw=false) {
|
||||||
if (raw)
|
if (raw) {
|
||||||
m_i2c = mraa_i2c_init_raw(bus);
|
m_i2c = mraa_i2c_init_raw(bus);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
m_i2c = mraa_i2c_init(bus);
|
m_i2c = mraa_i2c_init(bus);
|
||||||
}
|
}
|
||||||
|
if (m_i2c == NULL) {
|
||||||
|
throw std::invalid_argument("Invalid i2c bus");
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Closes the I2c Bus used. This does not guarrantee the bus will not
|
* Closes the I2c Bus used. This does not guarrantee the bus will not
|
||||||
* be usable by anyone else or communicates this disconnect to any
|
* be usable by anyone else or communicates this disconnect to any
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "pwm.h"
|
#include "pwm.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace mraa {
|
namespace mraa {
|
||||||
|
|
||||||
@@ -47,13 +48,21 @@ class Pwm {
|
|||||||
* if the pinmapper exported it
|
* if the pinmapper exported it
|
||||||
*/
|
*/
|
||||||
Pwm(int pin, int chipid=-1, bool owner = true) {
|
Pwm(int pin, int chipid=-1, bool owner = true) {
|
||||||
if (chipid == -1)
|
if (chipid == -1) {
|
||||||
m_pwm = mraa_pwm_init(pin);
|
m_pwm = mraa_pwm_init(pin);
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
m_pwm = mraa_pwm_init_raw(pin, chipid);
|
m_pwm = mraa_pwm_init_raw(pin, chipid);
|
||||||
if (!owner)
|
}
|
||||||
|
|
||||||
|
if (m_pwm == NULL) {
|
||||||
|
throw std::invalid_argument("Error initialising PWM on pin");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!owner) {
|
||||||
mraa_pwm_owner(m_pwm, 0);
|
mraa_pwm_owner(m_pwm, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Pwm destructor
|
* Pwm destructor
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spi.h"
|
#include "spi.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace mraa {
|
namespace mraa {
|
||||||
|
|
||||||
@@ -44,6 +45,10 @@ class Spi {
|
|||||||
*/
|
*/
|
||||||
Spi(int bus) {
|
Spi(int bus) {
|
||||||
m_spi = mraa_spi_init(bus);
|
m_spi = mraa_spi_init(bus);
|
||||||
|
|
||||||
|
if (m_spi == NULL) {
|
||||||
|
throw std::invalid_argument("Error initialising SPI bus");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Closes spi bus
|
* Closes spi bus
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace mraa {
|
namespace mraa {
|
||||||
|
|
||||||
@@ -43,6 +44,10 @@ class Uart {
|
|||||||
*/
|
*/
|
||||||
Uart(int uart) {
|
Uart(int uart) {
|
||||||
m_uart = mraa_uart_init(uart);
|
m_uart = mraa_uart_init(uart);
|
||||||
|
|
||||||
|
if (m_uart == NULL) {
|
||||||
|
throw std::invalid_argument("Error initialising UART");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Uart destructor
|
* Uart destructor
|
||||||
|
|||||||
@@ -25,5 +25,9 @@
|
|||||||
import mraa
|
import mraa
|
||||||
|
|
||||||
print (mraa.getVersion())
|
print (mraa.getVersion())
|
||||||
x = mraa.Aio(0)
|
|
||||||
print (x.read())
|
try:
|
||||||
|
x = mraa.Aio(0)
|
||||||
|
print (x.read())
|
||||||
|
except:
|
||||||
|
print ("Are you sure you have an ADC?")
|
||||||
|
|||||||
11
src/mraa.i
11
src/mraa.i
@@ -1,5 +1,6 @@
|
|||||||
%include stdint.i
|
%include stdint.i
|
||||||
%include std_string.i
|
%include std_string.i
|
||||||
|
%include exception.i
|
||||||
|
|
||||||
#ifdef DOXYGEN
|
#ifdef DOXYGEN
|
||||||
%include common_hpp_doc.i
|
%include common_hpp_doc.i
|
||||||
@@ -26,6 +27,16 @@
|
|||||||
mraa_init();
|
mraa_init();
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
%exception {
|
||||||
|
try {
|
||||||
|
$action
|
||||||
|
} catch(const std::invalid_argument& e) {
|
||||||
|
SWIG_exception(SWIG_ValueError, e.what());
|
||||||
|
} catch(...) {
|
||||||
|
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
%typemap(in) uint8_t = char;
|
%typemap(in) uint8_t = char;
|
||||||
%typemap(in) unsigned char* = char*;
|
%typemap(in) unsigned char* = char*;
|
||||||
%apply (char *STRING, size_t LENGTH) { (char *data, size_t length) };
|
%apply (char *STRING, size_t LENGTH) { (char *data, size_t length) };
|
||||||
|
|||||||
Reference in New Issue
Block a user