From 3054b6150429617dff77705f34efb341c17dce1c Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sat, 22 Jun 2013 01:07:16 +0200 Subject: add no such port exception handling --- src/main/java/com/github/jodersky/flow/low/NativeSerial.java | 2 ++ src/main/native/flow.c | 6 ++++-- src/main/native/flow.h | 2 ++ src/main/scala/com/github/jodersky/flow/exceptions.scala | 2 +- src/main/scala/com/github/jodersky/flow/low/Serial.scala | 2 ++ 5 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/java/com/github/jodersky/flow/low/NativeSerial.java b/src/main/java/com/github/jodersky/flow/low/NativeSerial.java index 132c0ef..dcbd2f0 100644 --- a/src/main/java/com/github/jodersky/flow/low/NativeSerial.java +++ b/src/main/java/com/github/jodersky/flow/low/NativeSerial.java @@ -12,6 +12,7 @@ class NativeSerial { final static int E_BUSY = -3; final static int E_INVALID_BAUD = -4; final static int E_INTERRUPT = -5; + final static int E_NO_PORT = -6; /**Opens a serial port and allocates memory for storing configuration. Note: if this function fails, * any internally allocated resources will be freed. @@ -19,6 +20,7 @@ class NativeSerial { * @param baud baud rate * @param serial pointer to memory that will be allocated with a serial structure * @return 0 on success + * @return E_NO_PORT if the given port does not exist * @return E_ACCESS_DENIED if permissions are not sufficient to open port * @return E_BUSY if port is already in use * @return E_INVALID_BAUD if specified baudrate is non-standard diff --git a/src/main/native/flow.c b/src/main/native/flow.c index c8927e9..8cb7def 100644 --- a/src/main/native/flow.c +++ b/src/main/native/flow.c @@ -51,9 +51,11 @@ int serial_open(const char* port_name, int baud, struct serial_config** serial) int fd = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { + int en = errno; DEBUG(perror("obtain file descriptor");); - if (errno == EACCES) return E_ACCESS_DENIED; - else return E_IO; + if (en == EACCES) return E_ACCESS_DENIED; + if (en == ENOENT) return E_NO_PORT; + return E_IO; } if (flock(fd, LOCK_EX | LOCK_NB) < 0) { diff --git a/src/main/native/flow.h b/src/main/native/flow.h index d1823c1..29b8d28 100644 --- a/src/main/native/flow.h +++ b/src/main/native/flow.h @@ -9,6 +9,7 @@ #define E_BUSY -3 // port is busy #define E_INVALID_BAUD -4 // baud rate is not valid #define E_INTERRUPT -5 // not really an error, function call aborted because port is closed +#define E_NO_PORT -6 //contains file descriptors used in managing a serial port struct serial_config { @@ -28,6 +29,7 @@ struct serial_config { * @param baud baud rate * @param serial pointer to memory that will be allocated with a serial structure * @return 0 on success + * @return E_NO_PORT if the given port does not exist * @return E_ACCESS_DENIED if permissions are not sufficient to open port * @return E_BUSY if port is already in use * @return E_INVALID_BAUD if specified baudrate is non-standard diff --git a/src/main/scala/com/github/jodersky/flow/exceptions.scala b/src/main/scala/com/github/jodersky/flow/exceptions.scala index ebcce60..0422018 100644 --- a/src/main/scala/com/github/jodersky/flow/exceptions.scala +++ b/src/main/scala/com/github/jodersky/flow/exceptions.scala @@ -2,7 +2,7 @@ package com.github.jodersky.flow import java.io.IOException -//class NoSuchPortException(message: String) extends IOException(message) +class NoSuchPortException(message: String) extends Exception(message) class PortInUseException(message: String) extends Exception(message) class AccessDeniedException(message: String) extends Exception(message) class IllegalBaudRateException(message: String) extends Exception(message) diff --git a/src/main/scala/com/github/jodersky/flow/low/Serial.scala b/src/main/scala/com/github/jodersky/flow/low/Serial.scala index 0a098c5..fcf4d37 100644 --- a/src/main/scala/com/github/jodersky/flow/low/Serial.scala +++ b/src/main/scala/com/github/jodersky/flow/low/Serial.scala @@ -10,6 +10,7 @@ import com.github.jodersky.flow.IllegalBaudRateException import scala.util.Try import java.util.concurrent.atomic.AtomicBoolean import com.github.jodersky.flow.PortInterruptedException +import com.github.jodersky.flow.NoSuchPortException class Serial private (val port: String, private val pointer: Long) { import Serial._ @@ -60,6 +61,7 @@ object Serial { case E_BUSY => throw new PortInUseException(port) case E_INVALID_BAUD => throw new IllegalBaudRateException("use standard baud rate (see termios.h)") case E_INTERRUPT => throw new PortInterruptedException(port) + case E_NO_PORT => throw new NoSuchPortException(port) case error if error < 0 => throw new IOException(s"unknown error code: ${error}") case success => success } -- cgit v1.2.3