SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
HostPortUDP.h
Go to the documentation of this file.
1
2#ifndef HOSTPORTUDP_H
3#define HOSTPORTUDP_H
4
5#include <memory>
6#include <string>
7#include "sockpp/udp_socket.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 static constexpr unsigned int MAX_PACKET_SIZE = 256;
177
178private:
179 //static constexpr unsigned int TX_BUF_SIZE = 1024; //!< Size of the output buffer.
180 sockpp::socket_initializer sockInit;
181 static constexpr unsigned int MASK = 0xFF;
182 sockpp::udp_socket client;
183 unsigned int _port;
184 std::string _ip;
185 unsigned int _terminator;
186 unsigned int _header;
187 unsigned int _timeout;
188 int _enable = 1; //<! Enable value for the socket.
189 //unsigned char _tx_buf[TX_BUF_SIZE]; //!< Tx buffer.
190 bool _isFirstRead = false;
191 bool init(const std::string& ipaddr, unsigned short port, unsigned int timeout);
192};
193
194#endif
A class for host communication via UDP protocol using the broadcast IP.
Definition: HostPortUDP.h:20
static constexpr unsigned int TIMEOUT
Default timeout.
Definition: HostPortUDP.h:175
int _enable
Definition: HostPortUDP.h:188
static constexpr unsigned int MAX_PACKET_SIZE
Maximum packet size in bytes.
Definition: HostPortUDP.h:176
bool setTimeout(unsigned int timeout)
Set the timeout.
unsigned int getHeader(void)
Get the header.
bool init(const std::string &ipaddr, unsigned short port, unsigned int timeout)
Private initialization function.
static constexpr unsigned int HEADER
Default header.
Definition: HostPortUDP.h:173
unsigned int _timeout
Timeout.
Definition: HostPortUDP.h:187
bool flush(void)
Flush the port.
unsigned int _terminator
Terminator bytes.
Definition: HostPortUDP.h:185
HostPortUDP()
Constructor.
bool setIP(const std::string &ipaddr)
Set the IP address.
unsigned int _port
Socket port.
Definition: HostPortUDP.h:183
unsigned int getTimeout(void)
Get the timeout.
static constexpr unsigned int TERMINATOR
Default terminator.
Definition: HostPortUDP.h:174
static constexpr unsigned int MASK
Mask for parsing.
Definition: HostPortUDP.h:181
std::string getIP(void)
Get the IP address.
sockpp::socket_initializer sockInit
Definition: HostPortUDP.h:180
bool setTerminator(unsigned int terminator)
Set the terminator.
unsigned int _header
Header bytes.
Definition: HostPortUDP.h:186
bool read(unsigned char *packetPtr, unsigned int size)
Read data buffer.
unsigned short getPort(void)
Get the serial port.
~HostPortUDP()
Desctructor.
bool write(unsigned char *packetPtr, unsigned int size)
Write data buffer.
unsigned int getTerminator(void)
Get the terminator.
std::string _ip
IP address.
Definition: HostPortUDP.h:184
bool isInit(void)
Check if host port is open.
bool setPort(unsigned short port)
Set the serial port.
bool begin(const std::string &ipaddr, unsigned short port, unsigned int header, unsigned int terminator)
Begin the host port.
bool _isFirstRead
True if firt packer read.
Definition: HostPortUDP.h:190
bool restart(void)
Restart the host port.
bool setHeader(unsigned int header)
Set the header.
sockpp::udp_socket client
Socket object.
Definition: HostPortUDP.h:182
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.
bool close(void)
Close the host port.