SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
HostPortTCP.h
Go to the documentation of this file.
1
2#ifndef HOSTPORTTCP_H
3#define HOSTPORTTCP_H
4
5#include <memory>
6#include <string>
7#include "sockpp/tcp_connector.h"
8#include "sockpp/version.h"
9
21public:
22
27
32
39 bool begin(const std::string& ipaddr, unsigned short port);
40
49 bool begin(const std::string& ipaddr, unsigned short port, unsigned int header, unsigned int terminator);
50
60 bool begin(const std::string& ipaddr, unsigned short port, unsigned int header, unsigned int terminator, unsigned int timeout);
61
69 bool read(unsigned char* packetPtr, unsigned int size);
70
78 bool write(unsigned char* packetPtr, unsigned int size);
79
85 bool close(void);
86
92 bool restart(void);
93
99 bool flush(void);
100
105 bool setPort(unsigned short port);
106
111 bool setIP(const std::string& ipaddr);
112
117 bool setHeader(unsigned int header); //set the header
118
123 bool setTerminator(unsigned int terminator);
124
129 bool setTimeout(unsigned int timeout);
130
135 unsigned short getPort(void);
136
141 std::string getIP(void);
142
147 unsigned int getHeader(void);
148
153 unsigned int getTerminator(void);
154
159 unsigned int getTimeout(void);
160
165 bool isInit(void); //check if is init
166
171 operator bool() { return isInit(); }
172
173 static constexpr unsigned int HEADER = 0xFF812345;
174 static constexpr unsigned int TERMINATOR = 0xFF8CABDE;
175 static constexpr unsigned int TIMEOUT = 1000;
176
177private:
178 //static constexpr unsigned int TX_BUF_SIZE = 1024; //!< Size of the output buffer.
179 sockpp::socket_initializer sockInit;
180 static constexpr unsigned int MASK = 0xFF;
181 sockpp::tcp_connector client;
182 unsigned int _port;
183 std::string _ip;
184 unsigned int _terminator;
185 unsigned int _header;
186 unsigned int _timeout;
187 //unsigned char _tx_buf[TX_BUF_SIZE]; //!< Tx buffer.
188 bool _isFirstRead = false;
189 bool init(const std::string& ipaddr, unsigned short port, unsigned int timeout);
190};
191
192#endif
A class for host communication via TCP/IP protocol.
Definition: HostPortTCP.h:20
bool restart(void)
Restart the host port.
bool setTerminator(unsigned int terminator)
Set the terminator.
bool isInit(void)
Check if host port is open.
std::string _ip
IP address.
Definition: HostPortTCP.h:183
static constexpr unsigned int HEADER
Default header.
Definition: HostPortTCP.h:173
bool begin(const std::string &ipaddr, unsigned short port)
Begin the host port.
bool begin(const std::string &ipaddr, unsigned short port, unsigned int header, unsigned int terminator, unsigned int timeout)
Begin the host port.
std::string getIP(void)
Get the IP address.
bool flush(void)
Flush the port.
bool setHeader(unsigned int header)
Set the header.
HostPortTCP()
Constructor.
unsigned int _header
Header bytes.
Definition: HostPortTCP.h:185
unsigned int getTimeout(void)
Get the timeout.
unsigned int _terminator
Terminator bytes.
Definition: HostPortTCP.h:184
bool init(const std::string &ipaddr, unsigned short port, unsigned int timeout)
Private initialization function.
~HostPortTCP()
Desctructor.
unsigned int _port
Socket port.
Definition: HostPortTCP.h:182
bool setTimeout(unsigned int timeout)
Set the timeout.
unsigned short getPort(void)
Get the serial port.
unsigned int _timeout
Timeout.
Definition: HostPortTCP.h:186
bool begin(const std::string &ipaddr, unsigned short port, unsigned int header, unsigned int terminator)
Begin the host port.
unsigned int getHeader(void)
Get the header.
bool _isFirstRead
True if firt packer read.
Definition: HostPortTCP.h:188
sockpp::tcp_connector client
Socket object.
Definition: HostPortTCP.h:181
bool close(void)
Close the host port.
sockpp::socket_initializer sockInit
Definition: HostPortTCP.h:179
static constexpr unsigned int TERMINATOR
Default terminator.
Definition: HostPortTCP.h:174
unsigned int getTerminator(void)
Get the terminator.
bool setIP(const std::string &ipaddr)
Set the IP address.
static constexpr unsigned int TIMEOUT
Default timeout.
Definition: HostPortTCP.h:175
bool setPort(unsigned short port)
Set the serial port.
static constexpr unsigned int MASK
Mask for parsing.
Definition: HostPortTCP.h:180
bool write(unsigned char *packetPtr, unsigned int size)
Write data buffer.
bool read(unsigned char *packetPtr, unsigned int size)
Read data buffer.