refactor: simplifying project until runs on embedded system

This commit is contained in:
2025-03-26 21:28:50 +01:00
parent c13310d540
commit 171a48eb71
28 changed files with 1260 additions and 3208 deletions
File diff suppressed because it is too large Load Diff
-219
View File
@@ -1,219 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SAD_Client is used for ServerAutoDetection (SAD).
*
* File: ServerAutoDetetc_Client.cpp
* Author: Hagi
*
* Created on 16. April 2019, 08:57
*/
#include "SAD_Client.h"
//----- initialze static members -----
bool SAD_Client::s_StopSAD = true;
fos::Mutex SAD_Client::s_MutexSAD;
fos::Semaphore SAD_Client::s_ConditionStopSAD;
fos::Task* SAD_Client::s_ThreadSend = NULL;
fos::Task* SAD_Client::s_ThreadReceiveIP4 = NULL;
SAD_ClientInterface* SAD_Client::s_SADClientInterface = NULL;
fos::Mutex SAD_Client::s_MutexSocket;
SPITSocket* SAD_Client::s_SPITSocket = NULL;
void SAD_Client::startAutoDetectServer(SAD_ClientInterface* p_SADClientInterface, SPITSocket* p_SpitSocket) {
if (p_SADClientInterface == NULL) return;
if (p_SpitSocket == NULL) return;
s_SADClientInterface = p_SADClientInterface;
{
fos::MutexGuard o_UniqueLock(s_MutexSAD);
if (s_StopSAD == false) return;
s_StopSAD = false;
SAD_DetectedServer::clearDetectedServers();
}
s_SPITSocket = p_SpitSocket;
s_ThreadSend = new fos::Task(SAD_Client::sendAutoDetectServer, "sendDetect");
s_ThreadReceiveIP4 = new fos::Task(SAD_Client::receiveAutoDetectServer_IP4, "getDetect");
}
void SAD_Client::stopAutoDetectServer(SAD_ClientInterface* p_SADClientInterface) {
if (s_SADClientInterface != p_SADClientInterface) return;
{
fos::MutexGuard o_UniqueLock(s_MutexSAD);
if (s_StopSAD == true) return;
s_StopSAD = true;
}
s_ConditionStopSAD.give();
if (s_ThreadSend != NULL) {
delete s_ThreadSend;
s_ThreadSend = NULL;
}
//wait for receiving threads
if (s_ThreadReceiveIP4 != NULL) {
delete s_ThreadReceiveIP4;
s_ThreadReceiveIP4 = NULL;
}
{
fos::MutexGuard o_LockSocket(s_MutexSocket);
if (s_SPITSocket != NULL) {
delete s_SPITSocket;
s_SPITSocket = NULL;
}
}
{
fos::MutexGuard o_UniqueLock(s_MutexSAD);
SAD_DetectedServer::clearDetectedServers();
}
}
void SAD_Client::sendAutoDetectServer() {
SPITSocket::IPAddress* o_IPAddress;
string stringBroadcastIP4;
string stringDatagram;
int intDatagramLength;
bool boolDone;
stringDatagram = SERVERAUTODETECT_PRECODE;
stringDatagram.append(SERVERAUTODETECT_SEPERATOR);
stringDatagram.append(SERVERAUTODETECT_TASKID);
intDatagramLength = stringDatagram.length();
while(s_StopSAD == false) {
//---- IP4 ---
cycleStart();
{
fos::MutexGuard o_LockSocket(s_MutexSocket);
if (s_SPITSocket == NULL) {
return;
}
s_SPITSocket->getAdaptersInfo();
for (int i = 0; i < SPITSocket::getIPAddressCount(); i++) {
o_IPAddress = SPITSocket::getIPAddress(i);
if (o_IPAddress == NULL) break;
stringBroadcastIP4 = s_SPITSocket->getBroadcastIP4(o_IPAddress);
if (s_SPITSocket->sendMessageTo(SPITSocket::FAMILY_IP4, stringBroadcastIP4, SERVERAUTODETECT_PORT, stringDatagram.data(), intDatagramLength) < 0) {
}
else {
}
}
}
{
if (s_StopSAD == true) {
return;
}
//wait for 3000 milliseconds or
//s_StopAutoDetectServer == true see stopAutoDetectServer
fos::MutexGuard o_LockWait(s_MutexSAD);
boolDone = s_ConditionStopSAD.take(3000);
if (not boolDone) // timeout occurred
{
s_StopSAD = true;
}
if (s_StopSAD == true) {
return;
}
}
cycleEnd();
}
int intTest = 0;
}
void SAD_Client::receiveAutoDetectServer_IP4() {
char buffer[512+1];
int intReceivedByteCount;
string stringMessage;
string stringServerIP;
int intIndex = 0;
int intPos = 0;
int intOldPos = 0;
string stringPart;
string stringParts[9];
//sendAutoDetectServer will bind the socket to a port
//wait a while to be sendAutoDetectServer first
fos::Task::delayMs(500);
while (s_StopSAD == false) {
{
fos::MutexGuard o_LockIP4(s_MutexSocket);
if (s_SPITSocket == NULL) {
return;
}
}
intReceivedByteCount = s_SPITSocket->receiveMessage(SPITSocket::FAMILY_IP4, buffer, 512, &stringServerIP);
if (intReceivedByteCount <= 0) {
if (s_StopSAD == false) {
//To reduce the processor load if a socket error occurs, we let the thread sleep a bit.
fos::Task::delayMs(100);
}
continue;
}
if (s_StopSAD == true) {
return;
}
//add null chararcter
if (intReceivedByteCount < 512) {
buffer[intReceivedByteCount] = '\0';
}
else {
buffer[512] = '\0';
}
stringMessage = buffer;
//Check if precode is ok
if (stringMessage.find(SERVERAUTODETECT_PRECODE.data(),0) != 0) {
return;
}
intIndex = 0;
intPos = 0;
intOldPos = 0;
while(intIndex < 8) {
intPos = stringMessage.find(SERVERAUTODETECT_SEPERATOR.data(),intOldPos);
if (intPos == string::npos) {
stringPart = "";
}
else {
stringPart = stringMessage.substr(intOldPos, intPos-intOldPos);
intOldPos = intPos + SERVERAUTODETECT_SEPERATOR.length();
}
stringParts[intIndex] = stringPart;
intIndex++;
}
stringParts[8] = stringServerIP;
SAD_DetectedServer::addDetectedServer(true, stringParts[SAD_DetectedServer::PARAMINDEX_ID], stringParts);
}
int inTest = 0;
}
void SAD_Client::cycleStart() {
{
fos::MutexGuard o_UniqueLock(s_MutexSAD);
if (s_StopSAD == true) return;
}
// printf("SAD CYCLE START\n");
SAD_DetectedServer::cycleStart();
}
void SAD_Client::cycleEnd() {
{
fos::MutexGuard o_UniqueLock(s_MutexSAD);
if (s_StopSAD == true) return;
}
SAD_DetectedServer::cycleEnd(s_SADClientInterface);
// printf("SAD CYCLE END\n");
}
+11 -49
View File
@@ -12,9 +12,11 @@
* Created on 4. Mai 2019, 13:51
*/
#include "SAD_DetectedServer.h"
#include "SAD_DetectedServer.hpp"
#include "esp_log.h"
static const char * TAG = "SpitSrv";
fos::Mutex SAD_DetectedServer::s_MutexDetectedServer;
unordered_map<string, SAD_DetectedServer*> SAD_DetectedServer::s_DetectedServers;
SAD_DetectedServer::~SAD_DetectedServer() {
@@ -27,47 +29,19 @@ SAD_DetectedServer::SAD_DetectedServer() {
m_ClientIP = "";
m_Port = "";
m_Additions = "";
m_IPIsLoopback = false;
m_IPIsIP4 = false;
m_IPIsIP6 = false;
}
void SAD_DetectedServer::setID(string p_ID) {
m_ID = p_ID;
}
void SAD_DetectedServer::setName(string p_Name) {
m_Name = p_Name;
}
void SAD_DetectedServer::setComputerName(string p_ComputerName) {
m_ComputerName = p_ComputerName;
}
void SAD_DetectedServer::setIP(string p_IP) {
m_IP = p_IP;
}
void SAD_DetectedServer::setClientIP(string p_IP) {
m_ClientIP = p_IP;
}
void SAD_DetectedServer::setPort(string p_Port) {
m_Port = p_Port;
}
void SAD_DetectedServer::setAdditions(string p_Additions) {
m_Additions = p_Additions;
}
void SAD_DetectedServer::cycleStart() {
{
fos::MutexGuard o_lock(s_MutexDetectedServer);
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
i->second->startCycleSAD();
}
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
i->second->startCycleSAD();
}
}
void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
void SAD_DetectedServer::cycleEnd(I_SAD_Client& sADClientInterface) {
list<SAD_DetectedServer*> o_LostServers;
list<SAD_DetectedServer*> o_NewServers;
SAD_DetectedServer* ptrDetectedServer;
{
fos::MutexGuard o_lock(s_MutexDetectedServer);
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
ptrDetectedServer = i->second;
if (ptrDetectedServer->getFoundAgain() == false) {
@@ -80,7 +54,6 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
ptrDetectedServer->endCycleSAD();
}
else if (ptrDetectedServer->getChanged()) {
// p_SADClientInterface->
ptrDetectedServer->endCycleSAD();
}
else {
@@ -91,12 +64,10 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
//Notify client interface about lost servers
for (auto i = o_LostServers.begin(); i != o_LostServers.end(); i++) {
ptrDetectedServer = *i;
if (p_SADClientInterface != NULL) {
p_SADClientInterface->eventServerLost(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
sADClientInterface.eventServerLost(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
ptrDetectedServer->getComputerName(), ptrDetectedServer->getIP(), ptrDetectedServer->getPort(), ptrDetectedServer->getAdditions(),
ptrDetectedServer->getClientIP(),
ptrDetectedServer->getIpIsLoopback(), ptrDetectedServer->getIpIsIP4(), ptrDetectedServer->getIpIsIP6());
}
ptrDetectedServer->getIpIsIP4());
}
//Remove lost servers
for (auto i = o_LostServers.begin(); i != o_LostServers.end(); i++) {
@@ -107,12 +78,10 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
for (auto i = o_NewServers.begin(); i != o_NewServers.end(); i++) {
ptrDetectedServer = *i;
if (p_SADClientInterface != NULL) {
p_SADClientInterface->eventServerDetected(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
sADClientInterface.eventServerDetected(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
ptrDetectedServer->getComputerName(), ptrDetectedServer->getIP(), ptrDetectedServer->getPort(), ptrDetectedServer->getAdditions(),
ptrDetectedServer->getClientIP(),
ptrDetectedServer->getIpIsLoopback(), ptrDetectedServer->getIpIsIP4(), ptrDetectedServer->getIpIsIP6());
}
ptrDetectedServer->getIpIsIP4());
}
o_NewServers.clear();
@@ -126,7 +95,6 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, string p_ServerParams[]) {
SAD_DetectedServer* ptrDetectedServer;
{
fos::MutexGuard o_Lock(s_MutexDetectedServer);
auto i = s_DetectedServers.find(p_ServerID);
if (i != s_DetectedServers.end()) {
ptrDetectedServer = i->second;
@@ -138,7 +106,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
}
ptrDetectedServer->setFoundAgain();
if (ptrDetectedServer->getIpIsLoopback() == false) {
//second prefer IP4
if (p_IsIP4 == true) {
ptrDetectedServer->setIP(p_ServerParams[PARAMINDEX_IP]);
@@ -147,10 +114,8 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
}
else if (ptrDetectedServer->getIpIsIP4() == false){
ptrDetectedServer->setIP(p_ServerParams[PARAMINDEX_IP]);
ptrDetectedServer->setIPIsIP6(true);
ptrDetectedServer->setClientIP(p_ServerParams[PARAMINDEX_CLIENTIP]);
}
}
ptrDetectedServer->setName(p_ServerParams[PARAMINDEX_NAME]);
ptrDetectedServer->setComputerName(p_ServerParams[PARAMINDEX_COMPUTERNAME]);
ptrDetectedServer->setPort(p_ServerParams[PARAMINDEX_PORT]);
@@ -158,7 +123,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
s_DetectedServers[ptrDetectedServer->getID()] = ptrDetectedServer;
}
}
/**
* Used by SAD_Client to remove a SPITServer - don't use this manually
@@ -167,7 +131,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer* p_DetectedServer) {
SAD_DetectedServer* o_DetectedServer;
{
fos::MutexGuard o_Lock(s_MutexDetectedServer);
auto i = s_DetectedServers.find(p_DetectedServer->getID());
if (i != s_DetectedServers.end()) {
o_DetectedServer = i->second;
@@ -183,7 +146,6 @@ void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer* p_DetectedServ
void SAD_DetectedServer::clearDetectedServers() {
SAD_DetectedServer* ptrDetectedServer;
{
fos::MutexGuard o_Lock(s_MutexDetectedServer);
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
ptrDetectedServer = i->second;
i = s_DetectedServers.erase(i);
+1 -17
View File
@@ -13,15 +13,13 @@
*/
#include "SPITObject.h"
#include "_SPITMACROS.h"
#include "SPITSocket.h"
unordered_map<string, SPITObject*> SPITObject::s_SPITObjects;
unordered_map<string, SPITObject*> SPITObject::s_SPITObjectsRemote;
fos::Mutex SPITObject::s_MutexSPITObject;
SPITObject* SPITObject::getSPITObject(string p_ID) {
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
auto i = s_SPITObjects.find(p_ID);
if (i == s_SPITObjects.end()) {
return NULL;
@@ -37,7 +35,6 @@
*/
const SPITObject& SPITObject::getSPITObjectRef(string p_ID) {
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
auto i = s_SPITObjects.find(p_ID);
if (i == s_SPITObjects.end()) {
throw "SPITObject no SPITObject with given id";
@@ -48,7 +45,6 @@
SPITObject* SPITObject::getSPITObjectRemote(string p_ID) {
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
auto i = s_SPITObjectsRemote.find(p_ID);
if (i == s_SPITObjectsRemote.end()) {
return NULL;
@@ -64,7 +60,6 @@
*/
const SPITObject& SPITObject::getSPITObjectRemoteRef(string p_ID) {
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
auto i = s_SPITObjectsRemote.find(p_ID);
if (i == s_SPITObjectsRemote.end()) {
throw "Remote SPITObject no SPITObject with given id";
@@ -76,7 +71,6 @@
bool SPITObject::addSPITObject(SPITObject* p_ptrSPITObject) {
if (p_ptrSPITObject == NULL) return false;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
s_SPITObjects[p_ptrSPITObject->getID()] = p_ptrSPITObject;
return true;
}
@@ -84,7 +78,6 @@
bool SPITObject::addSPITObjectRemote(SPITObject* p_ptrSPITObject) {
if (p_ptrSPITObject == NULL) return false;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
s_SPITObjectsRemote[p_ptrSPITObject->getID()] = p_ptrSPITObject;
return true;
}
@@ -92,7 +85,6 @@
bool SPITObject::removeSPITObject(SPITObject* p_ptrSPITObject) {
if (p_ptrSPITObject == NULL) return false;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
auto i = s_SPITObjects.find(p_ptrSPITObject->getID());
if (i == s_SPITObjects.end()) {
return false;
@@ -104,7 +96,6 @@
bool SPITObject::removeSPITObjectRemote(SPITObject* p_ptrSPITObject) {
if (p_ptrSPITObject == NULL) return false;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
auto i = s_SPITObjectsRemote.find(p_ptrSPITObject->getID());
if (i == s_SPITObjectsRemote.end()) {
return false;
@@ -123,7 +114,6 @@
SPITObject* SPITObject::getSPITObject(int p_Index) {
SPITObject* ptrSPITObject = NULL;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
if (p_Index >= 0 && p_Index < s_SPITObjects.size()) {
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjects.begin(), p_Index);
ptrSPITObject = i->second;
@@ -143,7 +133,6 @@
const SPITObject& SPITObject::getSPITObjectRef(int p_Index) {
SPITObject* ptrSPITObject = NULL;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
if (p_Index >= 0 && p_Index < s_SPITObjects.size()) {
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjects.begin(), p_Index);
ptrSPITObject = i->second;
@@ -157,7 +146,6 @@
SPITObject* SPITObject::getSPITObjectRemote(int p_Index) {
SPITObject* ptrSPITObject = NULL;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) {
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjectsRemote.begin(), p_Index);
ptrSPITObject = i->second;
@@ -177,7 +165,6 @@
const SPITObject& SPITObject::getSPITObjectRemoteRef(int p_Index) {
SPITObject* ptrSPITObject = NULL;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) {
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjectsRemote.begin(), p_Index);
ptrSPITObject = i->second;
@@ -192,7 +179,6 @@
void SPITObject::clearSPITObjects() {
SPITObject* ptrSPITObject;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
ptrSPITObject = i->second;
i = s_SPITObjects.erase(i);
@@ -206,7 +192,6 @@
void SPITObject::clearSPITObjectsRemote() {
SPITObject* ptrSPITObject;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
for (auto i = s_SPITObjectsRemote.begin(); i != s_SPITObjectsRemote.end(); i++) {
ptrSPITObject = i->second;
i = s_SPITObjectsRemote.erase(i);
@@ -227,7 +212,6 @@
void SPITObject::setConfirmedFalse() {
SPITObject* ptrSPITObject;
{
fos::MutexGuard o_LockObject(s_MutexSPITObject);
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
ptrSPITObject = i->second;
ptrSPITObject->setConfirmed(false);
-7
View File
@@ -14,12 +14,10 @@
#include "SPITPlayer.h"
fos::Mutex SPITPlayer::s_Mutex;
unordered_map<string, SPITPlayer*> SPITPlayer::s_SPITPlayers;
SPITPlayer* SPITPlayer::getSPITPlayer(string p_PlayerID) {
{
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
auto i = s_SPITPlayers.find(p_PlayerID);
if (i == s_SPITPlayers.end()) {
s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID);
@@ -33,7 +31,6 @@ SPITPlayer* SPITPlayer::getSPITPlayer(string p_PlayerID) {
void SPITPlayer::clearPlayers() {
SPITPlayer* ptrSPITPlayer;
{
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
for (auto i = s_SPITPlayers.begin(); i != s_SPITPlayers.end(); i++) {
ptrSPITPlayer = i->second;
i = s_SPITPlayers.erase(i);
@@ -54,7 +51,6 @@ SPITPlayer::SPITPlayer(string p_PlayerID) {
SPITPlayer::~SPITPlayer() {
{
fos::MutexGuard o_LockPlayer(s_Mutex);
PlayObject* ptrPlayObject;
for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) {
ptrPlayObject = i->second;
@@ -92,7 +88,6 @@ unordered_map<string, SPITPlayer::PlayObject*>* SPITPlayer::getPlayObjects() {
SPITPlayer::PlayObject* SPITPlayer::addSPITObject_Playing (string p_SPITObjectID) {
PlayObject* ptrPlayObject;
{
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
auto i = m_PlayObjects.find(p_SPITObjectID);
if (i == m_PlayObjects.end()) {
ptrPlayObject = new PlayObject(this, p_SPITObjectID);
@@ -111,7 +106,6 @@ SPITPlayer::PlayObject* SPITPlayer::addSPITObject_Playing (string p_SPITObjectID
void SPITPlayer::startPlaySequence() {
PlayObject* ptrPlayObject;
{
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) {
ptrPlayObject = i->second;
ptrPlayObject->startPlaySequence();
@@ -131,7 +125,6 @@ SPITPlayer::PlayObject* SPITPlayer::getPlayObject(string p_SPITObjectID) {
void SPITPlayer::endPlaySequence() {
PlayObject* ptrPlayObject;
{
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) {
ptrPlayObject = i->second;
if (ptrPlayObject->getIsPlayed() == false) {
+267 -183
View File
@@ -4,198 +4,282 @@
* and open the template in the editor.
*/
/*
* SPITSOcket is the base class for WindowsSocket and UnixSocket.
* File: SPITSocket.cpp
/*
* Unix implementation of a SPITSocket.
*
* File: UnixSocket.cpp
* Author: Hagi
*
* Created on 12. April 2019, 15:40
*
* Created on 12. April 2019, 15:58
*/
#include "SPITSocket.h"
#include "Task.hpp"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp/uuid.h"
#include <unistd.h>
#include <string>
using namespace std;
//----- initialize static members -----
fos::Mutex SPITSocket::s_MutexIPAddresses;
unordered_map<string, SPITSocket::IPAddress*> SPITSocket::s_IPAddresses;
int SPITSocket::FAMILY_IP4 = 2;
int SPITSocket::FAMILY_IP6 = 23;
/**
* Creates socket. If there exist some, they will be closed.
* @return
*/
bool SPITSocket::createSocket_UDP() {
bool boolSuccess = true;
int intResult = -1;
int intOptionValue4 = 1;
// ip4
if (m_UDPSocketIP4 != INVALID_SOCKET) {
close(m_UDPSocketIP4);
m_UDPSocketIP4 = INVALID_SOCKET;
}
m_UDPSocketIP4 = socket(AF_INET, SOCK_DGRAM, 0);
if (m_UDPSocketIP4 == INVALID_SOCKET) {
boolSuccess = false;
} else {
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_REUSEADDR, (void *)&intOptionValue4, sizeof(intOptionValue4));
}
// // set the m_UDPSocketIP4 in NON BLOCKING mode because
// // if the UDPSockets are in blocking mode the receiveMessage does not return in every constellation
// int intFlags;
// intFlags = fcntl(m_UDPSocketIP4, F_GETFL);
// fcntl(m_UDPSocketIP4, F_SETFL, intFlags | O_NONBLOCK);
struct timeval tv;
tv.tv_sec = 1; // seconds
tv.tv_usec = 0; // microseconds (0.0 sec)
// This sets the receive timeout
setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
SPITSocket::SPITSocket() {
m_ServerIP = "";
m_ServerPort = "";
m_ClientIP = "";
m_ClientPort = "";
m_BoundClientPort = "";
m_BoundClientFamily = 0;
m_IsBound = false;
if (intResult < 0) {
boolSuccess = false;
}
return boolSuccess;
}
/**
* Binds the socket to the client's ip address and port. The socket option SO_REUSEADDR is set to true.
* @param p_ClientIP ip address of the client
* @param p_ClientPort port of the client.
* if p_Port == "0" any free port will be used
* @return -1 id bind fails, o if bind is ok
*
*/
int SPITSocket::bindSocket(string p_ClientIP, string p_ClientPort) {
addrinfo *clientAI;
struct sockaddr_in *ptrIP4SocketAddress;
struct sockaddr_in o_IP4SocketAddress;
int intSocklen;
string stringClientPort;
if (p_ClientPort.length() <= 0)
stringClientPort = "0";
else
stringClientPort = p_ClientPort;
// int intResult = SOCKET_ERROR;
int intResult = 0;
if (getaddrinfo(p_ClientIP.data(), p_ClientPort.data(), NULL, &clientAI) != 0) {
return -1;
}
switch (clientAI->ai_family) {
case AF_INET:
if (m_UDPSocketIP4 == INVALID_SOCKET) {
break;
}
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in *>(clientAI->ai_addr);
intSocklen = sizeof(*ptrIP4SocketAddress);
// copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
memset(&o_IP4SocketAddress, 0, intSocklen);
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
// o_IP4SocketAddress.sin_zero = ptrIP4SocketAddress->sin_zero;
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
intResult = bind(m_UDPSocketIP4, (const sockaddr *)(ptrIP4SocketAddress), (socklen_t)intSocklen);
if (intResult < 0) {
break;
}
setBoundClientFamily(AF_INET);
if (intResult == 0) {
if (getsockname(m_UDPSocketIP4, (sockaddr *)ptrIP4SocketAddress, (socklen_t *)&intSocklen) != -1) {
setBoundClientPort(to_string((int)ptrIP4SocketAddress->sin_port));
}
}
break;
default:
setBoundClientFamily(0);
intResult = -1;
break;
}
freeaddrinfo(clientAI);
if (intResult == -1) {
setIsBound(false);
} else {
setIsBound(true);
}
return intResult;
}
/**
* Set whether the sockat are brodcast sockets or not. Set the sockopt SO_BROADCAST to 0 or 1
* @param p_Broadcast true SO_BROADCAST is set
* false = SO_BROADCAST is not set
* @return 0 = ok, -1 = error
*/
int SPITSocket::setBroadcast(bool p_Broadcast) {
int intResult;
int intOptionValue = 1;
u_char charOptionValue = 0x01;
// IP4
if (p_Broadcast) {
intOptionValue = 1;
} else {
intOptionValue = 0;
}
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_BROADCAST, &intOptionValue, sizeof(int));
// intResult = setsockopt(m_UDPSocketIP4, IPPROTO_IP, IP_MULTICAST_LOOP, (void*)&charOptionValue, sizeof(charOptionValue));
return intResult;
}
/**
*
* @param p_InetFamily AF_INET or AF_INET6
* @param p_ServerIP the ip address of the server
* @param p_ServerPort the port of the server
* @param p_MessageBytes buffer containing the message
* @param p_MessageLength the length of the message
* @return -1 send fails
* 0 send is done
*/
int SPITSocket::sendMessageTo(int p_InetFamily, string p_ServerIP, string p_ServerPort, const char *p_MessageBytes, int p_MessageLength) {
if (m_UDPSocketIP4 == INVALID_SOCKET)
return -1;
struct addrinfo *ptrAddressInfo;
struct sockaddr_in *ptrIP4SocketAddress;
struct sockaddr_in o_IP4SocketAddress;
int intSocklen;
int intResult = -1;
// prepare AddressInfo
if (getaddrinfo(p_ServerIP.data(), p_ServerPort.data(), NULL, &ptrAddressInfo) != 0) {
return -1;
}
switch (ptrAddressInfo->ai_family) {
case AF_INET:
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in *>(ptrAddressInfo->ai_addr);
intSocklen = sizeof(*ptrIP4SocketAddress);
if (m_UDPSocketIP4 != INVALID_SOCKET) {
// copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
memset(&o_IP4SocketAddress, 0, intSocklen);
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
intResult = sendto(m_UDPSocketIP4, p_MessageBytes, p_MessageLength, 0, (const sockaddr *)&o_IP4SocketAddress, (socklen_t)intSocklen);
if (intResult < 0) {
printf("UnixSocket::sendMessageTo IP4 sendto %s FAILED %s\n", p_ServerIP.data(), strerror(errno));
printf("UnixSocket::sendMessageTo ret=%d %s / %s / %d\n", intResult,
p_ServerPort.data(), p_MessageBytes, p_MessageLength);
}
freeaddrinfo(ptrAddressInfo);
}
break;
default:
intResult = -1;
}
if (intResult == -1) {
return -1;
}
return 0;
}
/**
* Call this for receiving a message
* This will block until a message is received or the underlying socket is closed
* @param p_InetFamily either AF_INET or AF_INET6<br>
* The SPITSocket creates two udp sockets, one for IP4 and one for IP6
* This parameter decides from which socket should a message recieved
* @param p_Buffer the buffer containing the received message later.
* @param p_BufferLength the size of the buffer
* @param p_ServerIP a pointer to a string, when a messages is received the string will contain the ip address of the sender/server
* if p_ServerIP is NULL - no detection of the server will be done.
* @return
*/
int SPITSocket::receiveMessage(int p_InetFamily, char *p_Buffer, int p_BufferLength, string *p_ServerIP) {
int intResult = -1;
// ADDRINFO *AI;
struct sockaddr_in o_IP4SocketAddress;
int intSocklen;
switch (p_InetFamily) {
case AF_INET:
if (m_UDPSocketIP4 == INVALID_SOCKET) {
break;
}
intSocklen = sizeof(o_IP4SocketAddress);
memset(&o_IP4SocketAddress, 0, intSocklen);
intResult = recvfrom(m_UDPSocketIP4, (void *)p_Buffer, p_BufferLength, 0 , (sockaddr *)&o_IP4SocketAddress, (socklen_t *)&intSocklen);
if (intResult >= 0) {
if (p_ServerIP != NULL) {
char bufferIP4[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(o_IP4SocketAddress.sin_addr), bufferIP4, intSocklen);
*p_ServerIP = bufferIP4;
}
}
break;
}
return intResult;
}
SPITSocket::~SPITSocket() {
{
fos::MutexGuard o_LockSocket(m_MutexSocket);
m_IsBound = false;
SPITSocket::IPAddress SPITSocket::getAdaptersInfo() {
// iterate over active interfaces, and print out IPs
esp_netif_t *netif = NULL;
while ((netif = esp_netif_next_unsafe(netif)) != NULL) {
ESP_LOGI("SPIT", "Connected to %s", esp_netif_get_desc(netif));
esp_netif_ip_info_t ip;
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
ESP_LOGI("SPIT", "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
ESP_LOGI("SPIT", "- IPv4 netmask: " IPSTR ",", IP2STR(&ip.netmask));
char buffer[64];
sprintf(buffer, IPSTR, IP2STR(&ip.ip));
string stringIP4 = buffer;
sprintf(buffer, IPSTR, IP2STR(&ip.netmask));
string stringSubnet = buffer;
return {stringIP4, stringSubnet};
}
return {"0.0.0.0", "0.0.0.0"};
}
string createGUID() {
// uuid_t id;
// uuid_generate(id);
// char *guid_string = new char[100];
// uuid_unparse(id, guid_string);
// string stringGUID = guid_string;
// return stringGUID;
uuid_t uu;
char uu_str[UUID_STR_LEN];
uuid_generate(uu);
uuid_unparse(uu, uu_str);
string stringGUID = uu_str;
return stringGUID;
}
/** The name of the client's host computer*/
string getClientHostName() {
string stringHostName = "";
const char * hostname;
esp_netif_get_hostname(esp_netif_get_default_netif(), &hostname);
if (hostname != nullptr) {
stringHostName = hostname;
}
}
void SPITSocket::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;
}
string SPITSocket::getServerIP() {
return m_ServerIP;
}
string SPITSocket::getServerPort(){
return m_ServerPort;
}
string SPITSocket::getClientIP() {
return m_ClientIP;
}
string SPITSocket::getClientPort() {
return m_ClientPort;
}
string SPITSocket::getBoundClientPort() {
return m_BoundClientPort;
}
void SPITSocket::setBoundClientPort(string p_Port) {
m_BoundClientPort = p_Port;
}
int SPITSocket::getBoundClientFamily() {
return m_BoundClientFamily;
}
void SPITSocket::setBoundClientFamily(int p_Family) {
m_BoundClientFamily = p_Family;
}
void SPITSocket::setIsBound(bool p_IsBound) {
{
fos::MutexGuard o_LockSocket(m_MutexSocket);
m_IsBound = p_IsBound;
}
}
bool SPITSocket::getIsBound() {
return m_IsBound;
}
int SPITSocket::sendMessage(const char* p_MessageBytes, int p_MessageLength) {
if (m_ServerPort.length() <= 0) return -1;
int intResult;
{
fos::MutexGuard o_LockSocket(m_MutexSocket);
if (m_IsBound == false) return -1;
intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength);
}
return intResult;
}
//---------- IP Adresses ---------
void SPITSocket::addIPAddress(string p_IPAddress, string p_SubnetMask) {
IPAddress* o_IPAddress;
{
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
auto i = s_IPAddresses.find(p_IPAddress);
if (i != s_IPAddresses.end()) {
o_IPAddress = i->second;
}
else {
o_IPAddress = new IPAddress();
}
o_IPAddress->setIPAddress(p_IPAddress);
o_IPAddress->setSubnetMask(p_SubnetMask);
o_IPAddress->setFound(true);
s_IPAddresses[o_IPAddress->getIPAddress()] = o_IPAddress;
}
}
void SPITSocket::removeIPAddress(string p_IPAddress) {
IPAddress* o_IPAddress;
{
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
auto i = s_IPAddresses.find(p_IPAddress);
if (i != s_IPAddresses.end()) {
o_IPAddress = i->second;
i = s_IPAddresses.erase(i);
delete o_IPAddress;
}
}
}
int SPITSocket::getIPAddressCount() {
return s_IPAddresses.size();
}
SPITSocket::IPAddress* SPITSocket::getIPAddress(int p_Index) {
IPAddress* ptr_IPAddress = NULL;
{
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
if (p_Index <= s_IPAddresses.size()) {
unordered_map<string, IPAddress*>::iterator i = next(s_IPAddresses.begin(), p_Index);
ptr_IPAddress = i->second;
}
return ptr_IPAddress;
}
}
void SPITSocket::clearIPAddresses() {
IPAddress* o_IPAddress;
{
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) {
o_IPAddress = i->second;
i = s_IPAddresses.erase(i);
delete o_IPAddress;
if (i == s_IPAddresses.end()) {
break;
}
}
}
}
void SPITSocket::resetFoundIPAddresses() {
IPAddress* o_IPAddress;
{
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) {
o_IPAddress = i->second;
o_IPAddress->setFound(false);
}
}
}
void SPITSocket::removeNotFoundIPAddresses() {
IPAddress* o_IPAddress;
{
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) {
o_IPAddress = i->second;
if (o_IPAddress->getFound() == false) {
i = s_IPAddresses.erase(i);
delete o_IPAddress;
if (i == s_IPAddresses.end()) {
break;
}
}
}
}
}
//------------ IPAddress -------------------
SPITSocket::IPAddress::IPAddress() {
m_IPAddress = "";
m_SubnetMask = "";
m_Found = false;
}
SPITSocket::IPAddress::~IPAddress() {
m_Found = false;
}
void SPITSocket::IPAddress::setIPAddress(const string& p_IPAddress) {
m_IPAddress = p_IPAddress;
}
void SPITSocket::IPAddress::setSubnetMask(const string& p_SubnetMask) {
m_SubnetMask = p_SubnetMask;
}
void SPITSocket::IPAddress::setFound(bool p_Found) {
m_Found = p_Found;
return stringHostName;;
}
+3 -17
View File
@@ -14,15 +14,15 @@
#include "SPITType.h"
#include "_SPITMACROS.h"
#include "esp_log.h"
static const char * TAG = "SPIT";
unordered_map<string, SPITType*> SPITType::s_SPITTypes;
unordered_map<string, SPITType*> SPITType::s_SPITTypesRemote;
fos::Mutex SPITType::s_MutexSPITType;
SPITType* SPITType::getSPITType(string p_ID) {
{
fos::MutexGuard o_LockType(s_MutexSPITType);
auto i = s_SPITTypes.find(p_ID);
if (i == s_SPITTypes.end()) {
return NULL;
@@ -38,7 +38,6 @@
*/
const SPITType& SPITType::getSPITTypeRef(string p_ID) {
{
fos::MutexGuard o_LockType(s_MutexSPITType);
auto i = s_SPITTypes.find(p_ID);
if (i == s_SPITTypes.end()) {
throw "SPITType no SPITType with given ID";
@@ -49,7 +48,6 @@
SPITType* SPITType::getSPITTypeRemote(string p_ID) {
{
fos::MutexGuard o_LockType(s_MutexSPITType);
auto i = s_SPITTypesRemote.find(p_ID);
if (i == s_SPITTypesRemote.end()) {
return NULL;
@@ -65,7 +63,6 @@
*/
const SPITType& SPITType::getSPITTypeRemoteRef(string p_ID) {
{
fos::MutexGuard o_LockType(s_MutexSPITType);
auto i = s_SPITTypesRemote.find(p_ID);
if (i == s_SPITTypesRemote.end()) {
throw "Remote SPITType no SPITType with given ID";
@@ -77,7 +74,6 @@
bool SPITType::addSPITType(SPITType* p_ptrSpitType) {
if (p_ptrSpitType == NULL) return false;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
s_SPITTypes[p_ptrSpitType->getID()] = p_ptrSpitType;
return true;
}
@@ -85,7 +81,6 @@
bool SPITType::addSPITTypeRemote(SPITType* p_ptrSpitType) {
if (p_ptrSpitType == NULL) return false;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
s_SPITTypesRemote[p_ptrSpitType->getID()] = p_ptrSpitType;
return true;
}
@@ -93,7 +88,6 @@
bool SPITType::removeSPITType(SPITType* p_ptrSpitType) {
if (p_ptrSpitType == NULL) return false;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
auto i = s_SPITTypes.find(p_ptrSpitType->getID());
if (i == s_SPITTypes.end()) {
return false;
@@ -105,7 +99,6 @@
bool SPITType::removeSPITTypeRemote(SPITType* p_ptrSpitType) {
if (p_ptrSpitType == NULL) return false;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
auto i = s_SPITTypesRemote.find(p_ptrSpitType->getID());
if (i == s_SPITTypesRemote.end()) {
return false;
@@ -124,7 +117,6 @@
SPITType* SPITType::getSPITType(int p_Index) {
SPITType* ptrSPITType = NULL;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
if (p_Index < s_SPITTypes.size()) {
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypes.begin(), p_Index);
ptrSPITType = i->second;
@@ -141,7 +133,6 @@
const SPITType& SPITType::getSPITTypeRef(int p_Index) {
SPITType* ptrSPITType = NULL;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
if (p_Index >= 0 && p_Index < s_SPITTypes.size()) {
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypes.begin(), p_Index);
ptrSPITType = i->second;
@@ -155,7 +146,6 @@
SPITType* SPITType::getSPITTypeRemote(int p_Index) {
SPITType* ptrSPITType = NULL;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
if (p_Index < s_SPITTypesRemote.size()) {
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypesRemote.begin(), p_Index);
ptrSPITType = i->second;
@@ -172,7 +162,6 @@
const SPITType& SPITType::getSPITTypeRemoteRef(int p_Index) {
SPITType* ptrSPITType = NULL;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
if (p_Index >= 0 && p_Index < s_SPITTypesRemote.size()) {
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypesRemote.begin(), p_Index);
ptrSPITType = i->second;
@@ -187,7 +176,6 @@
void SPITType::clearSPITTypes() {
SPITType* ptrSPITType;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
for (auto i = s_SPITTypes.begin(); i != s_SPITTypes.end(); i++) {
ptrSPITType = i->second;
i = s_SPITTypes.erase(i);
@@ -201,7 +189,6 @@
void SPITType::clearSPITTypesRemote() {
SPITType* ptrSPITType;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
for (auto i = s_SPITTypesRemote.begin(); i != s_SPITTypesRemote.end(); i++) {
ptrSPITType = i->second;
i = s_SPITTypesRemote.erase(i);
@@ -220,7 +207,6 @@
void SPITType::setConfirmedFalse() {
SPITType* ptrSPITType;
{
fos::MutexGuard o_LockType(s_MutexSPITType);
for (auto i = s_SPITTypes.begin(); i != s_SPITTypes.end(); i++) {
ptrSPITType = i->second;
ptrSPITType->setConfirmed(false);
File diff suppressed because it is too large Load Diff
-84
View File
@@ -1,84 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SPITHandler lists runnable instances and processes them in order.
* File: SPIT_Handler.cpp
* Author: Hagi
*
* Created on 24. April 2019, 11:18
*/
#include "SPIT_Handler.h"
#include "esp_log.h"
fos::Mutex SPIT_Handler::s_MutexLock;
fos::Semaphore SPIT_Handler::s_ConditionVariable(1000);
list<SPIT_Handler::Runnable*> SPIT_Handler::s_Runnables;
bool SPIT_Handler::s_Close = false;
fos::Task* SPIT_Handler::s_HandlerThread = nullptr;
SPIT_Handler::Runnable::~Runnable() {}
void SPIT_Handler::addRunnable(Runnable* p_Runnable) {
if(s_HandlerThread == nullptr) {
s_HandlerThread = new fos::Task(SPIT_Handler::run, "spitHandler");
}
if (p_Runnable == NULL) return;
{
fos::MutexGuard o_Unique_Lock(s_MutexLock);
if (s_Close == true) return;
s_Runnables.push_back(p_Runnable);
}
s_ConditionVariable.give();
}
void SPIT_Handler::closeHandler() {
if (s_Close == true) return;
{
fos::MutexGuard o_Unique_Lock(s_MutexLock);
if (s_Close == true) return;
s_Close = true;
}
s_ConditionVariable.give();
}
void SPIT_Handler::run() {
Runnable* o_Runnable;
bool boolWait = false;
while (s_Close == false) {
boolWait = false;
{
fos::MutexGuard o_Lock(s_MutexLock);
if (s_Runnables.empty()) {
s_ConditionVariable.take();
}
if (s_Close == true) {
return;
}
if (s_Runnables.empty()) {
continue;
}
else {
o_Runnable = s_Runnables.front();
s_Runnables.erase(s_Runnables.begin());
}
}
if (o_Runnable != NULL) {
try {
o_Runnable->run();
delete o_Runnable;
}
catch(...) {
ESP_LOGW("SPIT", "ERROR SPITHandler o_Runable->run()");
}
}
}
}
-377
View File
@@ -1,377 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Unix implementation of a SPITSocket.
*
* File: UnixSocket.cpp
* Author: Hagi
*
* Created on 12. April 2019, 15:58
*/
#include "_SPITMACROS.h"
#include "UnixSocket.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "Task.hpp"
UnixSocket::UnixSocket() : SPITSocket(){
SPITSocket::FAMILY_IP4 = AF_INET;
SPITSocket::FAMILY_IP6 = AF_INET6;
m_UDPSocketIP4 = INVALID_SOCKET;
m_UDPSocketIP6 = INVALID_SOCKET;
createSocket_UDP();
}
UnixSocket::~UnixSocket() {
int intResultIP4;
int intResultIP6;
if (m_UDPSocketIP4 != INVALID_SOCKET) {
// intResultIP4 = shutdown(m_UDPSocketIP4, 2);
intResultIP4 = close(m_UDPSocketIP4);
m_UDPSocketIP4 = INVALID_SOCKET;
}
if (m_UDPSocketIP6 != INVALID_SOCKET) {
// intResultIP6 = shutdown(m_UDPSocketIP6, 2);
intResultIP6 = close(m_UDPSocketIP6);
m_UDPSocketIP6 = INVALID_SOCKET;
}
fos::Task::delayMs(100);
}
int UnixSocket::init() {
return 0;
}
int UnixSocket::cleanUp() {
return 0;
}
/**
* Creates two new sockets.<br>
* One socket for IP4 and one for IP6.<br>
* If there exist some, they will be closed.<br>
* @return
*/
bool UnixSocket::createSocket_UDP() {
bool boolSuccess = true;
int intResult = -1;
int intOptionValue4 = 1;
int intOptionValue6 = 1;
u_char charOptionValue = 0x01;
//ip4
if (m_UDPSocketIP4 != INVALID_SOCKET) {
close(m_UDPSocketIP4);
m_UDPSocketIP4 = INVALID_SOCKET;
}
if (m_UDPSocketIP6 != INVALID_SOCKET) {
close(m_UDPSocketIP6);
m_UDPSocketIP6 = INVALID_SOCKET;
}
m_UDPSocketIP4 = socket(AF_INET,SOCK_DGRAM, 0);
if (m_UDPSocketIP4 == INVALID_SOCKET) {
boolSuccess = false;
}
else {
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_REUSEADDR, (void*)&intOptionValue4, sizeof(intOptionValue4));
}
m_UDPSocketIP6 = socket(AF_INET6,SOCK_DGRAM, 0);
if (m_UDPSocketIP6 == INVALID_SOCKET) {
boolSuccess = false;
close(m_UDPSocketIP4);
m_UDPSocketIP4 = INVALID_SOCKET;
}
else {
intResult = setsockopt(m_UDPSocketIP6, SOL_SOCKET, SO_REUSEADDR, (void*)&intOptionValue6, sizeof(intOptionValue6));
}
//set the m_UDPSocketIP4 and m_UDPSocketIP6 in NON BLOCKING mode because
//if the UDPSockets are in blocking mode the receiveMessage does not return in every constellation
int intFlags;
intFlags = fcntl(m_UDPSocketIP4, F_GETFL);
fcntl(m_UDPSocketIP4, F_SETFL, intFlags | O_NONBLOCK);
intFlags = fcntl(m_UDPSocketIP6, F_GETFL);
fcntl(m_UDPSocketIP6, F_SETFL, intFlags | O_NONBLOCK);
if (intResult < 0) {
boolSuccess = false;
}
return boolSuccess;
}
/**
* Binds the socket to the client's ip address and port.<br>
* The socket option SO_REUSEADDR is set to true.<br>
* @param p_ClientIP ip address of the client
* @param p_ClientPort port of the client.<br>
* if p_Port == "0" any free port will be used<br>
* @return -1 id bind fails, o if bind is ok
*
*/
int UnixSocket::bindSocket(string p_ClientIP, string p_ClientPort) {
addrinfo *clientAI;
struct sockaddr_in* ptrIP4SocketAddress;
struct sockaddr_in o_IP4SocketAddress;
struct sockaddr_in6* ptrIP6SocketAddress;
struct sockaddr_in6 o_IP6SocketAddress;
int intSocklen;
string stringClientPort;
if (p_ClientPort.length() <= 0) stringClientPort = "0";
else stringClientPort = p_ClientPort;
//int intResult = SOCKET_ERROR;
int intResult = 0;
if (getaddrinfo(p_ClientIP.data(), p_ClientPort.data(), NULL, &clientAI) != 0) {
return -1;
}
switch(clientAI->ai_family) {
case AF_INET:
if (m_UDPSocketIP4 == INVALID_SOCKET) {
break;
}
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in*>(clientAI->ai_addr);
intSocklen = sizeof (*ptrIP4SocketAddress);
//copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
memset(&o_IP4SocketAddress, 0, intSocklen);
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
//o_IP4SocketAddress.sin_zero = ptrIP4SocketAddress->sin_zero;
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
intResult = bind(m_UDPSocketIP4, (const sockaddr*)(ptrIP4SocketAddress),(socklen_t)intSocklen);
if (intResult < 0) {
break;
}
setBoundClientFamily(AF_INET);
if (intResult == 0) {
if (getsockname(m_UDPSocketIP4, (sockaddr*)ptrIP4SocketAddress, (socklen_t*)&intSocklen) != -1) {
setBoundClientPort(to_string((int)ptrIP4SocketAddress->sin_port));
}
}
break;
case AF_INET6:
if (m_UDPSocketIP6 == INVALID_SOCKET) {
break;
}
ptrIP6SocketAddress = reinterpret_cast<sockaddr_in6*>(clientAI->ai_addr);
intSocklen = sizeof (*ptrIP6SocketAddress);
//copy the ptrIP6SocketAddress, may be not filled correct by getaddrinfo
o_IP6SocketAddress.sin6_addr = ptrIP6SocketAddress->sin6_addr;
o_IP6SocketAddress.sin6_family = ptrIP6SocketAddress->sin6_family;
o_IP6SocketAddress.sin6_flowinfo = ptrIP6SocketAddress->sin6_flowinfo;
o_IP6SocketAddress.sin6_port = ptrIP6SocketAddress->sin6_port;
o_IP6SocketAddress.sin6_scope_id = ptrIP6SocketAddress->sin6_scope_id;
intResult = bind(m_UDPSocketIP6, (const sockaddr*)(ptrIP6SocketAddress), (socklen_t)intSocklen);
if (intResult < 0) {
break;
}
setBoundClientFamily(AF_INET6);
if (intResult == 0) {
if (getsockname(m_UDPSocketIP6, (sockaddr*)ptrIP6SocketAddress, (socklen_t*)&intSocklen) != -1) {
setBoundClientPort(to_string((int)ptrIP6SocketAddress->sin6_port));
}
}
break;
default:
setBoundClientFamily(0);
intResult = -1;
break;
}
freeaddrinfo(clientAI);
if (intResult == -1) {
setIsBound(false);
}
else {
setIsBound(true);
}
return intResult;
}
/**
* Set the sockopt SO_BROADCAST to 0 or 1
* @param p_Broadcast true SO_BROADCAST is set<br>
* false = SO_BROADCAST is not set
* @return 0 = ok, -1 = error
*/
int UnixSocket::setBroadcast(bool p_Broadcast) {
int intResult;
int intOptionValue = 1;
u_char charOptionValue = 0x01;
//IP4
if (p_Broadcast) {
intOptionValue = 1;
}
else {
intOptionValue = 0;
}
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_BROADCAST, &intOptionValue, sizeof(int));
// intResult = setsockopt(m_UDPSocketIP4, IPPROTO_IP, IP_MULTICAST_LOOP, (void*)&charOptionValue, sizeof(charOptionValue));
//IP6
// intResult = setsockopt(m_UDPSocketIP6, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (void*)&charOptionValue, sizeof(charOptionValue));
return intResult;
}
/**
*
* @param p_InetFamily AF_INET or AF_INET6
* @param p_ServerIP the ip address of the server
* @param p_ServerPort the port of the server
* @param p_MessageBytes buffer containing the message
* @param p_MessageLength the length of the message
* @return -1 send fails<br>
* 0 send is done
*/
int UnixSocket::sendMessageTo(int p_InetFamily, string p_ServerIP, string p_ServerPort, const char* p_MessageBytes, int p_MessageLength) {
if (m_UDPSocketIP4 == INVALID_SOCKET && m_UDPSocketIP6 == INVALID_SOCKET) return -1;
struct addrinfo* ptrAddressInfo;
struct sockaddr_in* ptrIP4SocketAddress;
struct sockaddr_in o_IP4SocketAddress;
struct sockaddr_in6* ptrIP6SocketAddress;
struct sockaddr_in6 o_IP6SocketAddress;
int intSocklen;
int intResult = -1;
// prepare AddressInfo
if (getaddrinfo(p_ServerIP.data(), p_ServerPort.data(), NULL, &ptrAddressInfo) != 0) {
return -1;
}
switch(ptrAddressInfo->ai_family) {
case AF_INET:
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in*>(ptrAddressInfo->ai_addr);
intSocklen = sizeof(*ptrIP4SocketAddress);
if (m_UDPSocketIP4 != INVALID_SOCKET) {
//copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
memset(&o_IP4SocketAddress, 0, intSocklen);
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
intResult = sendto(m_UDPSocketIP4, p_MessageBytes, p_MessageLength, 0, (const sockaddr*)&o_IP4SocketAddress, (socklen_t)intSocklen);
if (intResult < 0) printf("UnixSocket::sendMessageTo IP4 sendto %s FAILED %s\n", p_ServerIP.data(), strerror(errno));
freeaddrinfo(ptrAddressInfo);
}
break;
case AF_INET6:
ptrIP6SocketAddress = reinterpret_cast<sockaddr_in6*>(ptrAddressInfo->ai_addr);
intSocklen = sizeof(*ptrIP6SocketAddress);
if (m_UDPSocketIP6 != INVALID_SOCKET) {
//copy the ptrIP6SocketAddress, may be not filled correct by getaddrinfo
o_IP6SocketAddress.sin6_addr = ptrIP6SocketAddress->sin6_addr;
o_IP6SocketAddress.sin6_family = ptrIP6SocketAddress->sin6_family;
o_IP6SocketAddress.sin6_flowinfo = ptrIP6SocketAddress->sin6_flowinfo;
o_IP6SocketAddress.sin6_port = ptrIP6SocketAddress->sin6_port;
o_IP6SocketAddress.sin6_scope_id = ptrIP6SocketAddress->sin6_scope_id;
intResult = sendto(m_UDPSocketIP6, p_MessageBytes, p_MessageLength, 0, (const sockaddr*)&o_IP6SocketAddress, (socklen_t)intSocklen);
if (intResult < 0) printf("UnixSocket::sendMessageTo IP6 sendto %s FAILED %s\n", p_ServerIP.data(), strerror(errno));
freeaddrinfo(ptrAddressInfo);
}
break;
default:
intResult = -1;
}
if (intResult == -1) {
return -1;
}
return 0;
}
/**
* Starts receiving a message
* @param p_InetFamily AF_INET or AF_INET6
* @param p_Buffer a char* buffer large enough to hold the expected data
* @param p_BufferLength the length of the buffer
* @param p_ServerIP = NULL, the ip address is not determined<br>
* != NULL after receiving a message p_ServerID contains the ip address of the sender/server
* @return
*/
int UnixSocket::receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP) {
int intResult = -1;
// ADDRINFO *AI;
struct sockaddr_in o_IP4SocketAddress;
struct sockaddr_in6 o_IP6SocketAddress;
int intSocklen;
switch(p_InetFamily) {
case AF_INET:
if (m_UDPSocketIP4 == INVALID_SOCKET) {
break;
}
intSocklen = sizeof(o_IP4SocketAddress);
memset(&o_IP4SocketAddress, 0, intSocklen);
intResult = recvfrom(m_UDPSocketIP4, (void*)p_Buffer, p_BufferLength, 0, (sockaddr*)&o_IP4SocketAddress, (socklen_t*)&intSocklen);
if (intResult >= 0) {
if (p_ServerIP != NULL) {
char bufferIP4[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(o_IP4SocketAddress.sin_addr), bufferIP4, intSocklen);
*p_ServerIP = bufferIP4;
}
}
break;
case AF_INET6:
if (m_UDPSocketIP6 == INVALID_SOCKET) {
break;
}
intSocklen = sizeof(o_IP6SocketAddress);
memset(&o_IP6SocketAddress, 0, intSocklen);
intResult = recvfrom(m_UDPSocketIP6, (void*)p_Buffer, p_BufferLength, 0, (sockaddr*)&o_IP6SocketAddress, (socklen_t*)&intSocklen);
if (intResult >= 0) {
if (p_ServerIP != NULL) {
char bufferIP6[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(o_IP6SocketAddress.sin6_addr), bufferIP6, intSocklen);
*p_ServerIP = bufferIP6;
}
}
break;
}
return intResult;
}
/**
* Calculates the broadcast address from a ip4 address.<br>
* @param p_IPAddress the ip4 address
* @return returns the broadcast address
*/
string UnixSocket::getBroadcastIP4(SPITSocket::IPAddress* p_IPAddress) {
struct in_addr broadcast;
unsigned long ulIPAddress;
unsigned long ulSubnetMask;
ulIPAddress = inet_addr(p_IPAddress->getIPAddress().data());
ulSubnetMask = inet_addr(p_IPAddress->getSubnetMask().data());
broadcast.s_addr = ulIPAddress | ~ulSubnetMask;
return inet_ntoa(broadcast);
}
void UnixSocket::getAdaptersInfo() {
resetFoundIPAddresses();
// iterate over active interfaces, and print out IPs
esp_netif_t *netif = NULL;
while ((netif = esp_netif_next_unsafe(netif)) != NULL) {
ESP_LOGI("SPIT", "Connected to %s", esp_netif_get_desc(netif));
esp_netif_ip_info_t ip;
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
ESP_LOGI("SPIT", "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
ESP_LOGI("SPIT", "- IPv4 netmask: " IPSTR ",", IP2STR(&ip.netmask));
char buffer[64];
sprintf(buffer, IPSTR, IP2STR(&ip.ip));
string stringIP4 = buffer;
sprintf(buffer, IPSTR, IP2STR(&ip.netmask));
string stringSubnet = buffer;
addIPAddress(stringIP4, stringSubnet);
}
removeNotFoundIPAddresses();
}
-82
View File
@@ -1,82 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SPITWatchdog sends watchdog messages to the server from time to time.
* If there is no response from the server an eventWatchdogLost is triggered.
*
* File: Watchdog.cpp
* Author: Hagi
*
* Created on 22. April 2019, 13:02
*/
#include "Watchdog.h"
int Watchdog::WATCHDOGTIME = 3000;
bool Watchdog::s_StopWatchdog = true;
Watchdog::Watchdog(Listener* p_Listener) {
s_StopWatchdog = true;
m_Listener = p_Listener;
m_WatchdogThread = NULL;
}
Watchdog::~Watchdog() {
stopWatchdog();
}
void Watchdog::startWatchdog() {
if (s_StopWatchdog == false) return;
{
fos::Mutex o_UniqueLock(m_MutexLock);
s_StopWatchdog = false;
}
m_WatchdogThread = new fos::TaskT<Watchdog&>(Watchdog::runWatchdog, *this, "spitWD");
}
void Watchdog::stopWatchdog() {
if (m_WatchdogThread != NULL) {
{
fos::Mutex o_UniqueLock(m_MutexLock);
if (s_StopWatchdog == true) {
return;
}
s_StopWatchdog = true;
}
m_ConditionVariable.give();
delete m_WatchdogThread;
m_WatchdogThread = NULL;
}
}
void Watchdog::answerWatchdog() {
m_Answered = true;
}
void Watchdog::runWatchdog(Watchdog& wd) {
bool boolDone;
while (wd.s_StopWatchdog == false) {
wd.m_Answered = false;
wd.m_Listener->sendWatchdog();
{
fos::Mutex o_UniqueLock(wd.m_MutexLock);
boolDone = wd.m_ConditionVariable.take(WATCHDOGTIME);
if (wd.s_StopWatchdog == true) {
return;
}
}
if (boolDone) {
return;
}
if (wd.m_Answered == false) {
wd.stopWatchdog();
wd.m_Listener->eventWatchdogLost();
return;
}
}
}
-36
View File
@@ -1,36 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "_SPITMACROS.h"
#include "esp/uuid.h"
#include "esp_netif.h"
string createGUID() {
// uuid_t id;
// uuid_generate(id);
// char *guid_string = new char[100];
// uuid_unparse(id, guid_string);
// string stringGUID = guid_string;
// return stringGUID;
uuid_t uu;
char uu_str[UUID_STR_LEN];
uuid_generate(uu);
uuid_unparse(uu, uu_str);
string stringGUID = uu_str;
return stringGUID;
}
/** The name of the client's host computer*/
string getClientHostName() {
string stringHostName = "";
const char * hostname;
esp_netif_get_hostname(esp_netif_get_default_netif(), &hostname);
if (hostname != nullptr) {
stringHostName = hostname;
}
return stringHostName;;
}