refactor/bringup: Bringup full project
moving SPIT-App parts to main app LiveShowDemoProject added Debugging motor movements
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
/*
|
||||
* SPITSocket is the base class for WindowsSocket, UnixSocket,.
|
||||
* Used by SPIT_Client and SAD_Client.
|
||||
* File: SPITSocket.h
|
||||
@@ -16,17 +16,17 @@
|
||||
#ifndef SPITSOCKET_H
|
||||
#define SPITSOCKET_H
|
||||
|
||||
#include <string>
|
||||
#include "esp_netif.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include "Task.hpp"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -35,133 +35,132 @@ string createGUID();
|
||||
/** Returns the name of the client's host computer*/
|
||||
string getClientHostName();
|
||||
|
||||
class SPITSocket{
|
||||
public:
|
||||
static const int FAMILY_IP4 = AF_INET;
|
||||
class SPITSocket {
|
||||
public:
|
||||
static const int FAMILY_IP4 = AF_INET;
|
||||
|
||||
/**
|
||||
* IPAddress class is used to fetch all available IP addresses of the computer
|
||||
* This is for internal usage
|
||||
*/
|
||||
struct IPAddress {
|
||||
public:
|
||||
IPAddress(const string& p_IPAddress, const string& p_SubnetMask)
|
||||
: m_IPAddress(p_IPAddress), m_SubnetMask(p_SubnetMask) { }
|
||||
const string m_IPAddress;
|
||||
const string m_SubnetMask;
|
||||
/**
|
||||
* IPAddress class is used to fetch all available IP addresses of the computer
|
||||
* This is for internal usage
|
||||
*/
|
||||
struct IPAddress {
|
||||
public:
|
||||
IPAddress(const string &p_IPAddress, const string &p_SubnetMask)
|
||||
: m_IPAddress(p_IPAddress), m_SubnetMask(p_SubnetMask) {}
|
||||
const string m_IPAddress;
|
||||
const string m_SubnetMask;
|
||||
|
||||
/**
|
||||
* Calculates the broadcast address from a ip4 address
|
||||
* @param p_IPAddress the ip4 address
|
||||
* @return returns the broadcast address
|
||||
*/
|
||||
string getBroadcastIP4() const {
|
||||
struct in_addr broadcast;
|
||||
unsigned long ulIPAddress;
|
||||
unsigned long ulSubnetMask;
|
||||
/**
|
||||
* Calculates the broadcast address from a ip4 address
|
||||
* @param p_IPAddress the ip4 address
|
||||
* @return returns the broadcast address
|
||||
*/
|
||||
string getBroadcastIP4() const {
|
||||
struct in_addr broadcast;
|
||||
unsigned long ulIPAddress;
|
||||
unsigned long ulSubnetMask;
|
||||
|
||||
ulIPAddress = inet_addr(m_IPAddress.data());
|
||||
ulSubnetMask = inet_addr(m_SubnetMask.data());
|
||||
broadcast.s_addr = ulIPAddress | ~ulSubnetMask;
|
||||
ulIPAddress = inet_addr(m_IPAddress.data());
|
||||
ulSubnetMask = inet_addr(m_SubnetMask.data());
|
||||
broadcast.s_addr = ulIPAddress | ~ulSubnetMask;
|
||||
|
||||
return inet_ntoa(broadcast);
|
||||
}
|
||||
};
|
||||
const IPAddress mOwnIp;
|
||||
static IPAddress getAdaptersInfo();
|
||||
bool createSocket_UDP();
|
||||
static const int INVALID_SOCKET = -1;
|
||||
return inet_ntoa(broadcast);
|
||||
}
|
||||
};
|
||||
const IPAddress mOwnIp;
|
||||
static IPAddress getAdaptersInfo();
|
||||
bool createSocket_UDP();
|
||||
static const int INVALID_SOCKET = -1;
|
||||
|
||||
private:
|
||||
int m_UDPSocketIP4;
|
||||
private:
|
||||
int m_UDPSocketIP4;
|
||||
|
||||
public:
|
||||
//-------- class --------------
|
||||
SPITSocket(IPAddress ip) : mOwnIp(ip) {
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
|
||||
m_ServerIP = "";
|
||||
m_ServerPort = "";
|
||||
m_ClientIP = "";
|
||||
m_ClientPort = "";
|
||||
m_BoundClientPort = "";
|
||||
m_BoundClientFamily = 0;
|
||||
m_IsBound = false;
|
||||
createSocket_UDP();
|
||||
public:
|
||||
//-------- class --------------
|
||||
SPITSocket(IPAddress ip) : mOwnIp(ip) {
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
|
||||
}
|
||||
virtual ~SPITSocket() {
|
||||
ESP_LOGD("UDP", "Shutting down ...");
|
||||
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
shutdown(m_UDPSocketIP4, 2);
|
||||
close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
fos::Task::delayMs(100);
|
||||
m_ServerIP = "";
|
||||
m_ServerPort = "";
|
||||
m_ClientIP = "";
|
||||
m_ClientPort = "";
|
||||
m_BoundClientPort = "";
|
||||
m_BoundClientFamily = 0;
|
||||
m_IsBound = false;
|
||||
}
|
||||
private:
|
||||
string m_ServerIP;
|
||||
string m_ServerPort;
|
||||
string m_ClientIP;
|
||||
string m_ClientPort; //port given by the user maybe "0"
|
||||
string m_BoundClientPort; //port set after bind
|
||||
int m_BoundClientFamily;
|
||||
bool m_IsBound;
|
||||
createSocket_UDP();
|
||||
}
|
||||
virtual ~SPITSocket() {
|
||||
ESP_LOGD("UDP", "Shutting down ...");
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the ip address and port for server and client.
|
||||
* @param p_ServerIP
|
||||
* @param p_ServerPort
|
||||
* @param p_ClientIP
|
||||
* @param p_ClientPort
|
||||
*/
|
||||
void setConnectionParams(string p_ServerIP, string p_ServerPort, string p_ClientIP, string p_ClientPort) {
|
||||
m_ServerIP = p_ServerIP;
|
||||
m_ServerPort = p_ServerPort;
|
||||
m_ClientIP = p_ClientIP;
|
||||
m_ClientPort = p_ClientPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the server as set by setConnectionParams
|
||||
* @param p_MessageBytes byte buffer containing the message
|
||||
* @param p_MessageLength length of the messaeg
|
||||
* @return
|
||||
*/
|
||||
int sendMessage(const char* p_MessageBytes, int p_MessageLength) {
|
||||
if (m_ServerPort.length() <= 0)
|
||||
return -1;
|
||||
int intResult;
|
||||
{
|
||||
if (m_IsBound == false)
|
||||
return -1;
|
||||
intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength);
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
shutdown(m_UDPSocketIP4, 2);
|
||||
close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
fos::Task::delayMs(100);
|
||||
m_IsBound = false;
|
||||
}
|
||||
|
||||
int receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP);
|
||||
int setBroadcast(bool p_Broadcast);
|
||||
int bindSocket(string p_ServerIP, string p_ClientIP, string p_ClientPort);
|
||||
|
||||
// * Internal usage - don't use this manually
|
||||
// * Use sendMessage instead
|
||||
int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_Port, const char* p_MessageBytes, int p_MessageLength);
|
||||
|
||||
string getServerIP() { return m_ServerIP; }
|
||||
string getServerPort(){ return m_ServerPort; }
|
||||
string getClientIP() { return m_ClientIP; }
|
||||
string getClientPort() { return m_ClientPort; }
|
||||
string getBoundClientPort() { return m_BoundClientPort; }
|
||||
void setBoundClientPort(string p_Port) { m_BoundClientPort = p_Port; }
|
||||
void setBoundClientFamily(int p_Family) { m_BoundClientFamily = p_Family; }
|
||||
int getBoundClientFamily() { return m_BoundClientFamily; }
|
||||
void setIsBound(bool p_IsBound) { m_IsBound = p_IsBound; }
|
||||
bool getIsBound() { return m_IsBound; }
|
||||
private:
|
||||
string m_ServerIP;
|
||||
string m_ServerPort;
|
||||
string m_ClientIP;
|
||||
string m_ClientPort; // port given by the user maybe "0"
|
||||
string m_BoundClientPort; // port set after bind
|
||||
int m_BoundClientFamily;
|
||||
bool m_IsBound;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the ip address and port for server and client.
|
||||
* @param p_ServerIP
|
||||
* @param p_ServerPort
|
||||
* @param p_ClientIP
|
||||
* @param p_ClientPort
|
||||
*/
|
||||
void setConnectionParams(string p_ServerIP, string p_ServerPort, string p_ClientIP, string p_ClientPort) {
|
||||
m_ServerIP = p_ServerIP;
|
||||
m_ServerPort = p_ServerPort;
|
||||
m_ClientIP = p_ClientIP;
|
||||
m_ClientPort = p_ClientPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the server as set by setConnectionParams
|
||||
* @param p_MessageBytes byte buffer containing the message
|
||||
* @param p_MessageLength length of the messaeg
|
||||
* @return
|
||||
*/
|
||||
int sendMessage(const char *p_MessageBytes, int p_MessageLength) {
|
||||
if (m_ServerPort.length() <= 0)
|
||||
return -1;
|
||||
int intResult;
|
||||
{
|
||||
if (m_IsBound == false)
|
||||
return -1;
|
||||
intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength);
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
int receiveMessage(int p_InetFamily, char *p_Buffer, int p_BufferLength, string *p_ServerIP);
|
||||
int setBroadcast(bool p_Broadcast);
|
||||
int bindSocket(string p_ServerIP, string p_ClientIP, string p_ClientPort);
|
||||
|
||||
// * Internal usage - don't use this manually
|
||||
// * Use sendMessage instead
|
||||
int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_Port, const char *p_MessageBytes, int p_MessageLength);
|
||||
|
||||
string getServerIP() { return m_ServerIP; }
|
||||
string getServerPort() { return m_ServerPort; }
|
||||
string getClientIP() { return m_ClientIP; }
|
||||
string getClientPort() { return m_ClientPort; }
|
||||
string getBoundClientPort() { return m_BoundClientPort; }
|
||||
void setBoundClientPort(string p_Port) { m_BoundClientPort = p_Port; }
|
||||
void setBoundClientFamily(int p_Family) { m_BoundClientFamily = p_Family; }
|
||||
int getBoundClientFamily() { return m_BoundClientFamily; }
|
||||
void setIsBound(bool p_IsBound) { m_IsBound = p_IsBound; }
|
||||
bool getIsBound() { return m_IsBound; }
|
||||
};
|
||||
|
||||
#endif /* SPITSOCKET_H */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user