SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
HostPortTCP Class Reference

A class for host communication via TCP/IP protocol. More...

#include <HostPortTCP.h>

Public Member Functions

 HostPortTCP ()
 Constructor. More...
 
 ~HostPortTCP ()
 Desctructor. More...
 
bool begin (const std::string &ipaddr, unsigned short port)
 Begin the host port. More...
 
bool begin (const std::string &ipaddr, unsigned short port, unsigned int header, unsigned int terminator)
 Begin the host port. More...
 
bool begin (const std::string &ipaddr, unsigned short port, unsigned int header, unsigned int terminator, unsigned int timeout)
 Begin the host port. More...
 
bool read (unsigned char *packetPtr, unsigned int size)
 Read data buffer. More...
 
bool write (unsigned char *packetPtr, unsigned int size)
 Write data buffer. More...
 
bool close (void)
 Close the host port. More...
 
bool restart (void)
 Restart the host port. More...
 
bool flush (void)
 Flush the port. More...
 
bool setPort (unsigned short port)
 Set the serial port. More...
 
bool setIP (const std::string &ipaddr)
 Set the IP address. More...
 
bool setHeader (unsigned int header)
 Set the header. More...
 
bool setTerminator (unsigned int terminator)
 Set the terminator. More...
 
bool setTimeout (unsigned int timeout)
 Set the timeout. More...
 
unsigned short getPort (void)
 Get the serial port. More...
 
std::string getIP (void)
 Get the IP address. More...
 
unsigned int getHeader (void)
 Get the header. More...
 
unsigned int getTerminator (void)
 Get the terminator. More...
 
unsigned int getTimeout (void)
 Get the timeout. More...
 
bool isInit (void)
 Check if host port is open. More...
 
 operator bool ()
 Check if host port is open. More...
 

Static Public Attributes

static constexpr unsigned int HEADER = 0xFF812345
 Default header. More...
 
static constexpr unsigned int TERMINATOR = 0xFF8CABDE
 Default terminator. More...
 
static constexpr unsigned int TIMEOUT = 1000
 Default timeout. More...
 

Private Member Functions

bool init (const std::string &ipaddr, unsigned short port, unsigned int timeout)
 Private initialization function. More...
 

Private Attributes

sockpp::socket_initializer sockInit
 
sockpp::tcp_connector client
 Socket object. More...
 
unsigned int _port
 Socket port. More...
 
std::string _ip
 IP address. More...
 
unsigned int _terminator
 Terminator bytes. More...
 
unsigned int _header
 Header bytes. More...
 
unsigned int _timeout
 Timeout. More...
 
bool _isFirstRead = false
 True if firt packer read. More...
 

Static Private Attributes

static constexpr unsigned int MASK = 0xFF
 Mask for parsing. More...
 

Detailed Description

A class for host communication via TCP/IP protocol.

Class for host communication via TCP/IP protocol with a specified communication protocol. Data packet consists of

  • 4-bytes header
  • data (with specified length)
  • 4-bytes terminator No check sum is performed.
    Author
    Stefano Lovato
    Date
    2022

Constructor & Destructor Documentation

◆ HostPortTCP()

HostPortTCP::HostPortTCP ( )

Constructor.

Default constructor.

◆ ~HostPortTCP()

HostPortTCP::~HostPortTCP ( )

Desctructor.

Default destructor. It automatically destroy the serial object.

Member Function Documentation

◆ begin() [1/3]

bool HostPortTCP::begin ( const std::string &  ipaddr,
unsigned short  port 
)

Begin the host port.

Function to begin the host port communication with default header, terminator, and timeout.

Parameters
ipaddrThe IP address to use.
portThe socket port to use.
Returns
true if success, false otherwise.

◆ begin() [2/3]

bool HostPortTCP::begin ( const std::string &  ipaddr,
unsigned short  port,
unsigned int  header,
unsigned int  terminator 
)

Begin the host port.

Function to begin the host port communication with given header and terminator and default timeout.

Parameters
ipaddrThe IP address to use.
portThe socket port to use.
headerThe header to use (as a HEX number of 4-bytes).
terminatorThe terminator to use (as a HEX number of 4-bytes).
Returns
true if success, false otherwise.

◆ begin() [3/3]

bool HostPortTCP::begin ( const std::string &  ipaddr,
unsigned short  port,
unsigned int  header,
unsigned int  terminator,
unsigned int  timeout 
)

Begin the host port.

Function to begin the host port communication with given header, terminator and timeout.

Parameters
ipaddrThe IP address to use.
portThe socket port to use.
headerThe header to use (as a HEX number of 4-bytes).
terminatorThe terminator to use (as a HEX number of 4-bytes).
timeoutThe timeout of the serial communcication.
Returns
true if success, false otherwise.

◆ close()

bool HostPortTCP::close ( void  )

Close the host port.

Function to close the host port. Host port must be opened before.

Returns
true if success, false otherwise.

◆ flush()

bool HostPortTCP::flush ( void  )

Flush the port.

Function to flush the port. Host port must be opened before.

Returns
true if success, false otherwise.

◆ getHeader()

unsigned int HostPortTCP::getHeader ( void  )

Get the header.

Function to get the header used for the host communication.

Returns
The header.

◆ getIP()

std::string HostPortTCP::getIP ( void  )

Get the IP address.

Function to get the IP address used for the socket communication.

Returns
The baudrate.

◆ getPort()

unsigned short HostPortTCP::getPort ( void  )

Get the serial port.

Function to get the serial port used for the host communication.

Returns
The serial port number.

◆ getTerminator()

unsigned int HostPortTCP::getTerminator ( void  )

Get the terminator.

Function to get the terminator used for the host communication.

Returns
The terminator.

◆ getTimeout()

unsigned int HostPortTCP::getTimeout ( void  )

Get the timeout.

Function to get the timeout used for the host communication.

Returns
The timeout.

◆ init()

bool HostPortTCP::init ( const std::string &  ipaddr,
unsigned short  port,
unsigned int  timeout 
)
private

Private initialization function.

◆ isInit()

bool HostPortTCP::isInit ( void  )

Check if host port is open.

Function to check if the host port has been open successfully.

Returns
true if the host port is open, false otherwise.

◆ operator bool()

HostPortTCP::operator bool ( )
inline

Check if host port is open.

Boolean operator to check if the host port has been open successfully.

Returns
true if the host port is open, false otherwise.

◆ read()

bool HostPortTCP::read ( unsigned char *  packetPtr,
unsigned int  size 
)

Read data buffer.

Function to read a data buffer with given length from the host port. Host port must be opened before.

Parameters
packetPtrThe pointer to the data buffer.
sizeThe size of the data buffer.
Returns
true if success, false otherwise.

◆ restart()

bool HostPortTCP::restart ( void  )

Restart the host port.

Function to restart the host port. Host port must be opened before.

Returns
true if success, false otherwise.

◆ setHeader()

bool HostPortTCP::setHeader ( unsigned int  header)

Set the header.

Function to set the header of the host communication. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setIP()

bool HostPortTCP::setIP ( const std::string &  ipaddr)

Set the IP address.

Function to set the IP address of the socket communiation. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setPort()

bool HostPortTCP::setPort ( unsigned short  port)

Set the serial port.

Function to set the socket port. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setTerminator()

bool HostPortTCP::setTerminator ( unsigned int  terminator)

Set the terminator.

Function to set the terminator of the host communication. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setTimeout()

bool HostPortTCP::setTimeout ( unsigned int  timeout)

Set the timeout.

Function to set the timeout of the host communication. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ write()

bool HostPortTCP::write ( unsigned char *  packetPtr,
unsigned int  size 
)

Write data buffer.

Function to write a data buffer with given length to the host port. Host port must be opened before.

Parameters
packetPtrThe pointer to the data buffer.
sizeThe size of the data buffer.
Returns
true if success, false otherwise.

Member Data Documentation

◆ _header

unsigned int HostPortTCP::_header
private

Header bytes.

◆ _ip

std::string HostPortTCP::_ip
private

IP address.

◆ _isFirstRead

bool HostPortTCP::_isFirstRead = false
private

True if firt packer read.

◆ _port

unsigned int HostPortTCP::_port
private

Socket port.

◆ _terminator

unsigned int HostPortTCP::_terminator
private

Terminator bytes.

◆ _timeout

unsigned int HostPortTCP::_timeout
private

Timeout.

◆ client

sockpp::tcp_connector HostPortTCP::client
private

Socket object.

◆ HEADER

constexpr unsigned int HostPortTCP::HEADER = 0xFF812345
staticconstexpr

Default header.

◆ MASK

constexpr unsigned int HostPortTCP::MASK = 0xFF
staticconstexprprivate

Mask for parsing.

◆ sockInit

sockpp::socket_initializer HostPortTCP::sockInit
private

◆ TERMINATOR

constexpr unsigned int HostPortTCP::TERMINATOR = 0xFF8CABDE
staticconstexpr

Default terminator.

◆ TIMEOUT

constexpr unsigned int HostPortTCP::TIMEOUT = 1000
staticconstexpr

Default timeout.