Private
Public Access
2
0

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:
Brendan Le Foll
2014-10-16 14:53:56 +01:00
parent 88341c7e56
commit 54deb01796
8 changed files with 63 additions and 10 deletions

View File

@@ -24,6 +24,7 @@
#pragma once
#include <stdexcept>
#include "aio.h"
namespace mraa {
@@ -45,6 +46,9 @@ class Aio {
*/
Aio(unsigned int 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

View File

@@ -25,6 +25,7 @@
#pragma once
#include "gpio.h"
#include <stdexcept>
namespace mraa {
@@ -80,12 +81,20 @@ class Gpio {
* you so this may not always work as expected.
*/
Gpio(int pin, bool owner=true, bool raw=false) {
if (raw)
if (raw) {
m_gpio = mraa_gpio_init_raw(pin);
else
}
else {
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);
}
}
/**
* Gpio object destructor, this will only unexport the gpio if we where

View File

@@ -25,6 +25,7 @@
#pragma once
#include "i2c.h"
#include <stdexcept>
namespace mraa {
@@ -48,10 +49,15 @@ class I2c {
* @param raw Whether to disable pinmapper for your board
*/
I2c(int bus, bool raw=false) {
if (raw)
if (raw) {
m_i2c = mraa_i2c_init_raw(bus);
else
}
else {
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

View File

@@ -25,6 +25,7 @@
#pragma once
#include "pwm.h"
#include <stdexcept>
namespace mraa {
@@ -47,12 +48,20 @@ class Pwm {
* if the pinmapper exported it
*/
Pwm(int pin, int chipid=-1, bool owner = true) {
if (chipid == -1)
if (chipid == -1) {
m_pwm = mraa_pwm_init(pin);
else
}
else {
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);
}
}
/**
* Pwm destructor

View File

@@ -25,6 +25,7 @@
#pragma once
#include "spi.h"
#include <stdexcept>
namespace mraa {
@@ -44,6 +45,10 @@ class Spi {
*/
Spi(int bus) {
m_spi = mraa_spi_init(bus);
if (m_spi == NULL) {
throw std::invalid_argument("Error initialising SPI bus");
}
}
/**
* Closes spi bus

View File

@@ -25,6 +25,7 @@
#pragma once
#include "uart.h"
#include <stdexcept>
namespace mraa {
@@ -43,6 +44,10 @@ class Uart {
*/
Uart(int uart) {
m_uart = mraa_uart_init(uart);
if (m_uart == NULL) {
throw std::invalid_argument("Error initialising UART");
}
}
/**
* Uart destructor

View File

@@ -25,5 +25,9 @@
import mraa
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?")

View File

@@ -1,5 +1,6 @@
%include stdint.i
%include std_string.i
%include exception.i
#ifdef DOXYGEN
%include common_hpp_doc.i
@@ -26,6 +27,16 @@
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) unsigned char* = char*;
%apply (char *STRING, size_t LENGTH) { (char *data, size_t length) };