Skip to content

Commit

Permalink
Adding getErrno() and clearErrno() functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemarple committed Jan 29, 2012
1 parent 0487435 commit 997914b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
30 changes: 25 additions & 5 deletions software/RNXV/RNXV.cpp
@@ -1,6 +1,7 @@
#include "RNXV.h"

// Use the readLine() function from IniFile
#include "SD.h"
#include "IniFile.h"

const uint8_t RNXV::unconnectedPin = RNXV_UNCONNECTED_PIN;
Expand Down Expand Up @@ -115,8 +116,11 @@ void RNXV::setGpio4Pin(uint8_t pin)

bool RNXV::isAssociated(void) const
{
if (gpio4Pin == unconnectedPin)
return false;
if (gpio4Pin == unconnectedPin) {
errno = errorGpio4PinNotSet;
return true;
}
errno = exitOk;
return digitalRead(gpio4Pin);
}

Expand Down Expand Up @@ -158,7 +162,7 @@ bool RNXV::isConnected(void) const
{
if (gpio6Pin == unconnectedPin) {
errno = errorGpio6PinNotSet;
return false;
return true;
}
errno = exitOk;
return digitalRead(gpio6Pin);
Expand Down Expand Up @@ -230,7 +234,7 @@ bool RNXV::sendCommandsFromFile(const char* filename, char* buffer,
return true;
}

bool RNXV::connect(const char* hostname, uint16_t port) const
bool RNXV::connect(const char* hostname, uint16_t port, uint16_t timeout_ms) const
{
uart.print("open ");
uart.print(hostname);
Expand All @@ -247,12 +251,21 @@ bool RNXV::connect(const char* hostname, uint16_t port) const
console->print(char(uart.read()));
console->println();
}
unsigned long t = millis();
while (!isConnected())
if (millis() - t > timeout_ms) {
errno = errorTimeout;
return false;
}

errno = exitOk;
return true;
}

bool RNXV::connect(const IPAddress& ip, uint16_t port) const
bool RNXV::connect(const IPAddress& ip, uint16_t port, uint16_t timeout_ms) const
{
if (!isAssociated())
return false; // errno already set
uart.print("open ");
uart.print(ip);
uart.print(' ');
Expand All @@ -268,6 +281,13 @@ bool RNXV::connect(const IPAddress& ip, uint16_t port) const
console->print(char(uart.read()));
console->println();
}
unsigned long t = millis();
while (!isConnected())
if (millis() - t > timeout_ms) {
errno = errorTimeout;
return false;
}

errno = exitOk;
return true;
}
Expand Down
24 changes: 21 additions & 3 deletions software/RNXV/RNXV.h
Expand Up @@ -34,6 +34,10 @@
#define RNXV_DEFAULT_CMD_PIN RNXV_UNCONNECTED_PIN
#endif

#ifndef RNXV_TIMEOUT_MS
#define RNXV_TIMEOUT_MS 4000
#endif

class RNXV {
public:
enum returnValues {
Expand All @@ -55,6 +59,14 @@ class RNXV {
static const uint8_t unconnectedPin;
RNXV(Stream& uartStream);

inline returnValues getErrno(void) const {
return errno;
}

inline void clearErrno(void) const {
errno = exitOk;
}

inline bool getDebug(void) const {
return debug;
}
Expand Down Expand Up @@ -88,6 +100,10 @@ class RNXV {
uart.println(arg);
}

inline void flush(void) const {
uart.flush();
}

bool commandMode(void) const;
bool uartCommandMode(void) const;
bool gpioCommandMode(void) const;
Expand Down Expand Up @@ -139,8 +155,10 @@ class RNXV {
bool sendCommand(const char* cmd) const;
bool sendCommandsFromFile(const char* filename, char* buffer,
int bufferLen) const;
bool connect(const char* hostname, uint16_t port) const;
bool connect(const IPAddress& ip, uint16_t port) const;
bool connect(const char* hostname, uint16_t port,
uint16_t timeout_ms = RNXV_TIMEOUT_MS) const;
bool connect(const IPAddress& ip, uint16_t port,
uint16_t timeout_ms = RNXV_TIMEOUT_MS) const;
bool stop(void) const;

// For Ethernet compatibility
Expand All @@ -156,7 +174,7 @@ class RNXV {
void showPinStatus(void) const;

private:
mutable uint8_t errno; // Error number
mutable returnValues errno; // Error number
uint8_t rtsPin;
uint8_t ctsPin;
uint8_t gpio4Pin;
Expand Down

0 comments on commit 997914b

Please sign in to comment.