API
MagAOX::tty::telnetConn Struct Reference

A Telnet connection manager, wrapping libtelnet. More...

#include <telnetConn.hpp>

Collaboration diagram for MagAOX::tty::telnetConn:
Collaboration graph

Public Member Functions

 ~telnetConn ()
 D'tor, conducts connection cleanup. More...
 
int connect (const std::string &host, const std::string &port)
 Connect to the device. More...
 
int login (const std::string &username, const std::string &password)
 Manage the login process on this device. More...
 
int noLogin ()
 Set flags as if we're logged in, used when device doesn't require it. More...
 
int write (const std::string &buffWrite, int timeoutWrite)
 Write to a telnet connection. More...
 
int read (const std::string &eot, int timeoutRead, bool clear=true)
 Read from a telnet connection, until end-of-transmission string is read. More...
 
int read (int timeoutRead, bool clear=true)
 Read from a telnet connection, until m_prompt is read. More...
 
int writeRead (const std::string &strWrite, bool swallowEcho, int timeoutWrite, int timeoutRead)
 Write to a telnet connection, then get the reply. More...
 

Static Public Member Functions

static int send (int sock, const char *buffer, size_t size)
 Internal send for use by event_handler. More...
 
static void event_handler (telnet_t *telnet, telnet_event_t *ev, void *user_data)
 Event handler callback for libtelnet processing. More...
 

Public Attributes

int m_sock {0}
 The socket file descriptor. More...
 
telnet_t * m_telnet {nullptr}
 libtelnet telnet_t structure More...
 
std::string m_usernamePrompt {"Username:"}
 The device's username entry prompt, used for managing login. More...
 
std::string m_passwordPrompt {"Password:"}
 The device's password entry prompt, used for managing login. More...
 
std::string m_prompt {"$> "}
 The device's prompt, used for detecting end of transmission. More...
 
int m_loggedin {0}
 Flag denoting the login state. More...
 
int m_EHError {0}
 Used to indicate an error occurred in the event handler callback. More...
 
std::string m_strRead
 The accumulated string read from the device. More...
 

Detailed Description

A Telnet connection manager, wrapping libtelnet.

Establishes the connection to the server, and initializes the libtelnet structure, including registering the event handler callback.

Errors encountered during telnet event handling are indicated by an internal flag, which must be checked each time a libtelnet function is called. If it is nonzero an error has occurred.

Responses from the server are accumulated in the m_strRead member. It is typically cleared before reading, but this can be suppressed when desired.

Because of the way event handling is managed, and the class-global error and response accumulation this is not thread-safe. Any calls to this class methods should be mutex-ed.

Definition at line 80 of file telnetConn.hpp.

Constructor & Destructor Documentation

◆ ~telnetConn()

MagAOX::tty::telnetConn::~telnetConn ( )

D'tor, conducts connection cleanup.

Definition at line 24 of file telnetConn.cpp.

Member Function Documentation

◆ connect()

int MagAOX::tty::telnetConn::connect ( const std::string &  host,
const std::string &  port 
)

Connect to the device.

Parameters
[in]hostThe host specification (i.p. address)
[in]portthe port on the host.

Definition at line 31 of file telnetConn.cpp.

Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), and MagAOX::app::trippLitePDU::devConnect().

◆ event_handler()

void MagAOX::tty::telnetConn::event_handler ( telnet_t *  telnet,
telnet_event_t *  ev,
void *  user_data 
)
static

Event handler callback for libtelnet processing.

Resets the internal m_EHError value to TTY_E_NOERROR on entry. Will set it to an error flag if an error is encountered, so this flag should be checked after any call to a libtelnet function.

Warning
this makes telnetConn not thread safe

Definition at line 378 of file telnetConn.cpp.

Referenced by connect().

◆ login()

int MagAOX::tty::telnetConn::login ( const std::string &  username,
const std::string &  password 
)

Manage the login process on this device.

Parameters
[in]passwordThe username [in] The password.

Definition at line 90 of file telnetConn.cpp.

Referenced by MagAOX::app::trippLitePDU::devLogin().

◆ noLogin()

int MagAOX::tty::telnetConn::noLogin ( )

Set flags as if we're logged in, used when device doesn't require it.

Definition at line 176 of file telnetConn.cpp.

Referenced by MagAOX::app::picoMotorCtrl::appLogic(), and MagAOX::app::siglentSDG::appLogic().

◆ read() [1/2]

int MagAOX::tty::telnetConn::read ( const std::string &  eot,
int  timeoutRead,
bool  clear = true 
)

Read from a telnet connection, until end-of-transmission string is read.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[in]eotthe end-of-transmission indicator
[in]timeoutReadThe timeout in milliseconds.
[in]clear[optional] whether or not to clear the strRead buffer

Definition at line 223 of file telnetConn.cpp.

Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), MagAOX::app::trippLitePDU::devStatus(), read(), MagAOX::app::siglentSDG::writeCommand(), writeRead(), and MagAOX::app::siglentSDG::writeRead().

◆ read() [2/2]

int MagAOX::tty::telnetConn::read ( int  timeoutRead,
bool  clear = true 
)

Read from a telnet connection, until m_prompt is read.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[in]timeoutReadThe timeout in milliseconds.
[in]clear[optional] whether or not to clear the strRead buffer

Definition at line 287 of file telnetConn.cpp.

◆ send()

int MagAOX::tty::telnetConn::send ( int  sock,
const char *  buffer,
size_t  size 
)
static

Internal send for use by event_handler.

Definition at line 353 of file telnetConn.cpp.

Referenced by event_handler().

◆ write()

int MagAOX::tty::telnetConn::write ( const std::string &  buffWrite,
int  timeoutWrite 
)

Write to a telnet connection.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONWRITEPOLL if the poll times out.
TTY_E_ERRORONWRITEPOLL if an error is returned by poll.
TTY_E_TIMEOUTONWRITE if a timeout occurs during the write.
TTY_E_ERRORONWRITE if an error occurs writing to the file.
Parameters
[in]buffWriteThe characters to write to the telnet.
[in]timeoutWriteThe timeout in milliseconds.

Definition at line 182 of file telnetConn.cpp.

Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), MagAOX::app::picoMotorCtrl::channelThreadExec(), login(), MagAOX::app::siglentSDG::writeCommand(), writeRead(), and MagAOX::app::siglentSDG::writeRead().

◆ writeRead()

int MagAOX::tty::telnetConn::writeRead ( const std::string &  strWrite,
bool  swallowEcho,
int  timeoutWrite,
int  timeoutRead 
)

Write to a telnet connection, then get the reply.

The read is conducted until the m_prompt string is received. Echo characters are swallowed if desired.

Returns
TTY_E_NOERROR on success
TTY_E_TIMEOUTONWRITEPOLL if the poll times out.
TTY_E_ERRORONWRITEPOLL if an error is returned by poll.
TTY_E_TIMEOUTONWRITE if a timeout occurs during the write.
TTY_E_ERRORONWRITE if an error occurs writing to the file.
TTY_E_TIMEOUTONREADPOLL if the poll times out.
TTY_E_ERRORONREADPOLL if an error is returned by poll.
TTY_E_TIMEOUTONREAD if a timeout occurs during the read.
TTY_E_ERRORONREAD if an error occurs reading from the file.
Parameters
[in]strWriteThe characters to write to the telnet.
[in]swallowEchoIf true, strWrite.size() characters are read after the write
[in]timeoutWriteThe write timeout in milliseconds.
[in]timeoutReadThe read timeout in milliseconds.

Definition at line 294 of file telnetConn.cpp.

Referenced by MagAOX::app::trippLitePDU::devPostLogin(), MagAOX::app::trippLitePDU::devStatus(), MagAOX::app::trippLitePDU::turnOutletOff(), MagAOX::app::trippLitePDU::turnOutletOn(), and MagAOX::app::siglentSDG::writeRead().

Member Data Documentation

◆ m_EHError

int MagAOX::tty::telnetConn::m_EHError {0}

Used to indicate an error occurred in the event handler callback.

Definition at line 107 of file telnetConn.hpp.

Referenced by event_handler(), login(), read(), and writeRead().

◆ m_loggedin

int MagAOX::tty::telnetConn::m_loggedin {0}

Flag denoting the login state.

Used to manage different behaviors in the libtelnet event handler.

  • TELNET_WAITING_USER: waiting on m_usernamePrompt
  • TELNET_GOT_USER: got m_usernamePrompt
  • TELNET_WAITING_PASS: waiting on m_passwordPrompt
  • TELNET_GOT_PASS: got m_passwordPrompt
  • TELNET_WAITING_PROMPT: waiting on m_prompt
  • TELNET_LOGGED_IN: logged in

Definition at line 104 of file telnetConn.hpp.

Referenced by connect(), event_handler(), login(), and noLogin().

◆ m_passwordPrompt

std::string MagAOX::tty::telnetConn::m_passwordPrompt {"Password:"}

The device's password entry prompt, used for managing login.

Definition at line 90 of file telnetConn.hpp.

Referenced by event_handler().

◆ m_prompt

std::string MagAOX::tty::telnetConn::m_prompt {"$> "}

◆ m_sock

int MagAOX::tty::telnetConn::m_sock {0}

The socket file descriptor.

Definition at line 82 of file telnetConn.hpp.

Referenced by ~telnetConn(), connect(), event_handler(), login(), read(), write(), and writeRead().

◆ m_strRead

std::string MagAOX::tty::telnetConn::m_strRead

The accumulated string read from the device.

This needs to be clear()-ed when expecting a new response to start.

Warning
This makes telnetConn NOT threadsafe.

Definition at line 113 of file telnetConn.hpp.

Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), MagAOX::app::trippLitePDU::devStatus(), event_handler(), read(), writeRead(), and MagAOX::app::siglentSDG::writeRead().

◆ m_telnet

telnet_t* MagAOX::tty::telnetConn::m_telnet {nullptr}

libtelnet telnet_t structure

Definition at line 84 of file telnetConn.hpp.

Referenced by ~telnetConn(), connect(), login(), read(), write(), and writeRead().

◆ m_usernamePrompt

std::string MagAOX::tty::telnetConn::m_usernamePrompt {"Username:"}

The device's username entry prompt, used for managing login.

Definition at line 87 of file telnetConn.hpp.

Referenced by MagAOX::app::trippLitePDU::devLogin(), and event_handler().


The documentation for this struct was generated from the following files: