You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1176 lines
27 KiB
1176 lines
27 KiB
#include "deviceNodeEthernet2Serial.h"
|
|
|
|
#include "../../data/protocol/protocol.h"
|
|
#include "hardwareManager.h"
|
|
|
|
// #define DEBUG_EXTRA
|
|
// #define DEBUG_SERIAL_COMMUNICATION
|
|
|
|
#define LOG(X) deviceNode::m_pLog->Log("E2UART", this, X)
|
|
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR(X) \
|
|
{ \
|
|
char _tBfrErrMSG2w34[1024]; \
|
|
sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
|
|
deviceNode::m_pLog->Log("E2UART", this, _LOG_ERROR, _tBfrErrMSG2w34); \
|
|
}
|
|
#else
|
|
#define LOG_ERROR(X) deviceNode::m_pLog->Log("E2UART", this, _LOG_ERROR, X);
|
|
#endif
|
|
|
|
deviceETH2UART::deviceETH2UART(uint32_t nodeID) :
|
|
deviceNode(nodeID),
|
|
hardwareResource(),
|
|
basicTransport(),
|
|
#ifdef SERIAL_PING_PONG
|
|
basicThread()
|
|
#else
|
|
basicDualThread()
|
|
#endif
|
|
{
|
|
// m_pLog = basicLogCollection::getInstance();
|
|
memset(&m_sUartData, 0, sizeof(m_sUartData));
|
|
#ifdef SERIAL_PING_PONG
|
|
#else
|
|
m_nInputBufferSize = 0;
|
|
m_nOutputBufferSize = 0;
|
|
#endif
|
|
m_iDeviceFD = -1;
|
|
m_iMasterFD = -1;
|
|
m_iInFID = -1;
|
|
m_iOutFID = -1;
|
|
hardwareResource::create(m_sNodeData.aux_parameter, HARDWARE_PROVIDER_SERIAL);
|
|
}
|
|
|
|
deviceETH2UART::~deviceETH2UART()
|
|
{
|
|
closePort();
|
|
hardwareManager * tH = hardwareManager::getInstance();
|
|
if (tH)
|
|
{
|
|
tH->resignResource(this);
|
|
}
|
|
}
|
|
|
|
bool deviceETH2UART::create(void)
|
|
{
|
|
DBC_UART_DATA * tDATA = NULL;
|
|
if (m_sResourceSettings.id == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (m_sResourceSettings.aux_id == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
tDATA = deviceNode::m_pDBC->getUartData(m_sResourceSettings.aux_id);
|
|
if (tDATA != NULL)
|
|
{
|
|
memcpy(&m_sUartData, tDATA, sizeof(DBC_UART_DATA));
|
|
#ifdef DEBUG
|
|
LOG(m_sUartData.path);
|
|
#endif
|
|
free(tDATA);
|
|
m_bValid = create_SOL();
|
|
};
|
|
|
|
if (m_bValid)
|
|
{
|
|
hardwareManager * tH = hardwareManager::getInstance();
|
|
if (tH)
|
|
{
|
|
tH->assignResource(this);
|
|
}
|
|
};
|
|
return m_bValid;
|
|
}
|
|
|
|
bool deviceETH2UART::validate(void)
|
|
{
|
|
if (m_bValid)
|
|
{
|
|
m_bValid = pingDevice();
|
|
if (! m_bValid)
|
|
{
|
|
LOG_ERROR("No Ping to device!");
|
|
closePort();
|
|
};
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Check Settings For Changes");
|
|
#endif
|
|
DBC_UART_DATA * tDATA = deviceNode::m_pDBC->getUartData(m_sResourceSettings.aux_id);
|
|
if (tDATA != NULL)
|
|
{
|
|
bool bFlag = true;
|
|
if (strcmp(tDATA->path, m_sUartData.path) != 0)
|
|
{
|
|
bFlag = false;
|
|
}
|
|
// if( bFlag && ( m_sUartData.speed != tDATA->speed ) ) bFlag = false; //Speed for SOL port Erelevant
|
|
if (bFlag && (m_sUartData.mode != tDATA->mode))
|
|
{
|
|
bFlag = false;
|
|
}
|
|
if (bFlag && (m_sUartData.port != tDATA->port))
|
|
{
|
|
bFlag = false;
|
|
}
|
|
if (! bFlag)
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Settings Has been Changed!");
|
|
#endif
|
|
if (m_bValid)
|
|
{
|
|
closePort(); // if port not closed
|
|
}
|
|
// Apply new settings
|
|
memcpy(&m_sUartData, tDATA, sizeof(DBC_UART_DATA));
|
|
#ifdef DEBUG
|
|
LOG("Reconect with new settings");
|
|
#endif
|
|
m_bValid = create_SOL();
|
|
};
|
|
free(tDATA);
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Error Fetch DB Data!");
|
|
};
|
|
// Finally in any case If Not Connected -> try to Connect!
|
|
if (! m_bValid)
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Try to Conncet");
|
|
#endif
|
|
m_bValid = create_SOL();
|
|
};
|
|
if (m_bValid)
|
|
{
|
|
m_bValid = deviceNode::validate();
|
|
}
|
|
return m_bValid;
|
|
}
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
// Define the Packet Constants
|
|
// ping packet size
|
|
#define PING_PKT_S 64
|
|
|
|
// Automatic port number
|
|
#define PORT_NO 0
|
|
|
|
// Automatic port number
|
|
#define PING_SLEEP_RATE 1000000
|
|
|
|
// Gives the timeout delay for receiving packets
|
|
// in seconds
|
|
#define RECV_TIMEOUT 1
|
|
|
|
// ping packet structure
|
|
struct ping_pkt
|
|
{
|
|
struct icmphdr hdr;
|
|
char msg[PING_PKT_S - sizeof(struct icmphdr)];
|
|
};
|
|
|
|
bool deviceETH2UART::pingDevice()
|
|
{
|
|
bool RetVal = false;
|
|
if (m_bValid)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Pinging Device!");
|
|
#endif
|
|
int sockfd;
|
|
int ttl_val = 64, msg_count = 0;//, flag = 1, msg_received_count = 0;
|
|
size_t i;
|
|
struct ping_pkt pckt;
|
|
struct sockaddr_in ping_addr;
|
|
struct sockaddr_in r_addr;
|
|
struct timeval tv_out;
|
|
|
|
tv_out.tv_sec = 0;
|
|
tv_out.tv_usec = 300000; // 1/3 of second
|
|
|
|
ping_addr.sin_family = AF_INET;
|
|
ping_addr.sin_addr.s_addr = inet_addr(m_sUartData.path);
|
|
// ping_addr.sin_port = htons( m_nPort );
|
|
#ifdef DEBUG_EXTRA
|
|
LOG(m_sUartData.path);
|
|
#endif
|
|
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
|
if (sockfd < 0)
|
|
{
|
|
LOG_ERROR("Socket file descriptor not received!");
|
|
return false;
|
|
};
|
|
|
|
// set socket options at ip to TTL and value to 64,
|
|
// change to what you want by setting ttl_val
|
|
if (setsockopt(sockfd, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) != 0)
|
|
{
|
|
LOG_ERROR("Setting socket options to TTL failed!");
|
|
shutdown(sockfd, SHUT_RDWR);
|
|
close(sockfd);
|
|
return false;
|
|
};
|
|
|
|
// setting timeout of recv setting
|
|
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv_out, sizeof tv_out);
|
|
|
|
// flag is whether packet was sent or not
|
|
//flag = 1;
|
|
|
|
// filling packet
|
|
bzero(&pckt, sizeof(pckt));
|
|
|
|
pckt.hdr.type = ICMP_ECHO;
|
|
pckt.hdr.un.echo.id = getpid();
|
|
|
|
for (i = 0; i < sizeof(pckt.msg) - 1; i++)
|
|
{
|
|
pckt.msg[i] = (char)i + '0';
|
|
}
|
|
|
|
pckt.msg[i] = 0;
|
|
pckt.hdr.un.echo.sequence = msg_count++;
|
|
pckt.hdr.checksum = checksumPing(&pckt, sizeof(pckt)); // checksum(&pckt, sizeof(pckt));
|
|
|
|
// send packet
|
|
if (sendto(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr *) &ping_addr, sizeof(ping_addr)) <= 0)
|
|
{
|
|
LOG_ERROR("Packet Sending Failed!");
|
|
shutdown(sockfd, SHUT_RDWR);
|
|
close(sockfd);
|
|
return false;
|
|
};
|
|
|
|
// receive packet
|
|
socklen_t addr_len = sizeof(r_addr);
|
|
struct timeval tv;
|
|
tv.tv_sec = 3; // timeout_in_seconds;
|
|
tv.tv_usec = 0;
|
|
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv, sizeof tv);
|
|
|
|
if (recvfrom(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr *) &r_addr, &addr_len) <= 0)
|
|
{
|
|
LOG_ERROR("Packet receive failed!");
|
|
shutdown(sockfd, SHUT_RDWR);
|
|
close(sockfd);
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Device Is OK!");
|
|
#endif
|
|
RetVal = true;
|
|
};
|
|
shutdown(sockfd, SHUT_RDWR);
|
|
close(sockfd);
|
|
};
|
|
return RetVal;
|
|
}
|
|
|
|
bool deviceETH2UART::create_SOL()
|
|
{
|
|
|
|
/*
|
|
if( m_sUartData.mode == SOL_NOT_DEFINED ) {
|
|
LOG_ERROR("No Connection Type Defined!");
|
|
return false;
|
|
};
|
|
*/
|
|
if (m_sUartData.port == 0)
|
|
{
|
|
LOG_ERROR("No Port Defined!");
|
|
return false;
|
|
};
|
|
if (m_sUartData.speed == 0)
|
|
{
|
|
LOG_ERROR("No Speed Defined!");
|
|
return false;
|
|
};
|
|
if (m_sUartData.path[0] == (char) 0)
|
|
{
|
|
LOG_ERROR("No Adress Defined!");
|
|
return false;
|
|
};
|
|
|
|
switch (m_sUartData.mode)
|
|
{
|
|
case SOL_UDP_CLIENT:
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("PORT IS CLIENT - Create UDP Server");
|
|
#endif
|
|
#ifdef __linux__
|
|
// Creating socket file descriptor
|
|
if ((m_iDeviceFD = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
{
|
|
LOG_ERROR("UDP Socket Creation Failed!");
|
|
return false;
|
|
};
|
|
fcntl(m_iDeviceFD, F_SETFL, O_NONBLOCK);
|
|
// Filling server information
|
|
struct sockaddr_in tServerAddr;
|
|
memset(&tServerAddr, 0, sizeof(tServerAddr));
|
|
tServerAddr.sin_family = AF_INET; // IPv4
|
|
tServerAddr.sin_addr.s_addr = INADDR_ANY;
|
|
tServerAddr.sin_port = htons(m_sUartData.port);
|
|
|
|
// Bind the socket with the server address
|
|
if (bind(m_iDeviceFD, (const struct sockaddr *) &tServerAddr, sizeof(tServerAddr)) < 0)
|
|
{
|
|
LOG_ERROR("UDP Bind Failed");
|
|
return false;
|
|
};
|
|
#else
|
|
WSADATA wsaData;
|
|
// Initialize Winsock version 2.2
|
|
char tBuff[128];
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
|
{
|
|
sprintf(tBuff, "WSAStartup failed with error %ld", WSAGetLastError());
|
|
LOG_ERROR(tBuff);
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
sprintf(tBuff, "The Winsock DLL status is %s.", wsaData.szSystemStatus);
|
|
LOG(tBuff);
|
|
};
|
|
// Set up a SOCKADDR_IN structure that will tell bind that we
|
|
// want to receive datagrams from all interfaces using port 5150.
|
|
// The IPv4 family
|
|
ReceiverAddr.sin_family = AF_INET;
|
|
// Port no. 5150
|
|
ReceiverAddr.sin_port = htons(m_nPort);
|
|
// From all interface (0.0.0.0)
|
|
ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
// Associate the address information with the socket using bind.
|
|
// At this point you can receive datagrams on your bound socket.
|
|
if (bind(m_sSocket, (SOCKADDR *) &ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR)
|
|
{
|
|
sprintf(tBuff, "Server: bind() failed!Error : %ld.", WSAGetLastError());
|
|
// Close the socket
|
|
closesocket(m_sSocket);
|
|
// Do the clean up
|
|
WSACleanup();
|
|
// and exit with error
|
|
LOG_ERROR(tBuff);
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
LOG("UDP Server: bind() is OK!");
|
|
};
|
|
// Some info on the receiver side...
|
|
SOCKADDR_IN tAddr;
|
|
int tLen = sizeof(SOCKADDR_IN);
|
|
getsockname(m_sSocket, (SOCKADDR *) &tAddr, &tLen);
|
|
sprintf(tBuff, "Receiving IP(s) used : %s", inet_ntoa(tAddr.sin_addr));
|
|
LOG(tBuff);
|
|
sprintf(tBuff, "Receiving port used : %d", htons(tAddr.sin_port));
|
|
LOG(tBuff);
|
|
SelectTiming = recvfromTimeOutUDP(m_sSocket, 10, 0);
|
|
#endif
|
|
break;
|
|
};
|
|
case SOL_TCP_CLIENT:
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("PORT IS CLIENT - Create TCP Server");
|
|
#endif
|
|
break;
|
|
};
|
|
case SOL_UDP_SERVER:
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("PORT IS SERVER - Create UDP Client");
|
|
#endif
|
|
#ifdef __linux__
|
|
// Creating socket file descriptor
|
|
if ((m_iDeviceFD = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
{
|
|
LOG_ERROR("Socket Creation Failed!");
|
|
return false;
|
|
};
|
|
fcntl(m_iDeviceFD, F_SETFL, O_NONBLOCK);
|
|
|
|
// Filling server information
|
|
struct sockaddr_in tServerAddr;
|
|
memset(&tServerAddr, 0, sizeof(tServerAddr));
|
|
tServerAddr.sin_family = AF_INET;
|
|
tServerAddr.sin_addr.s_addr = INADDR_ANY;
|
|
tServerAddr.sin_port = htons(m_sUartData.port);
|
|
#else
|
|
|
|
#endif
|
|
break;
|
|
};
|
|
case SOL_TCP_SERVER:
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("PORT IS SERVER - Create TCP Client");
|
|
#endif
|
|
|
|
// socket create and varification
|
|
m_iDeviceFD = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (m_iDeviceFD == -1)
|
|
{
|
|
LOG_ERROR("Socket creation failed!");
|
|
return false;
|
|
}
|
|
#ifdef DEBUG
|
|
else
|
|
{
|
|
LOG("Socket successfully created");
|
|
}
|
|
#endif
|
|
// Set non-blocking
|
|
int arg;
|
|
if ((arg = fcntl(m_iDeviceFD, F_GETFL, NULL)) < 0)
|
|
{
|
|
char tBuf[512];
|
|
sprintf(tBuf, "Error fcntl(..., F_GETFL) (%s)", strerror(errno));
|
|
LOG_ERROR(tBuf);
|
|
return false;
|
|
};
|
|
arg |= O_NONBLOCK;
|
|
if (fcntl(m_iDeviceFD, F_SETFL, arg) < 0)
|
|
{
|
|
char tBuf[512];
|
|
sprintf(tBuf, "Error fcntl(..., F_SETFL) (%s)", strerror(errno));
|
|
LOG_ERROR(tBuf);
|
|
return false;
|
|
};
|
|
|
|
// assign IP, PORT
|
|
struct sockaddr_in tServerAddr;
|
|
memset(&tServerAddr, 0, sizeof(tServerAddr));
|
|
tServerAddr.sin_family = AF_INET;
|
|
tServerAddr.sin_addr.s_addr = inet_addr(m_sUartData.path);
|
|
tServerAddr.sin_port = htons(m_sUartData.port);
|
|
|
|
// connect the client socket to server socket
|
|
int retVal = connect(m_iDeviceFD, (struct sockaddr *) &tServerAddr, sizeof(tServerAddr));
|
|
if (retVal < 0)
|
|
{
|
|
if (errno == EINPROGRESS)
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("EINPROGRESS in connect() - selecting");
|
|
#endif
|
|
do
|
|
{
|
|
fd_set myset;
|
|
struct timeval tv;
|
|
tv.tv_sec = 1; // one second
|
|
tv.tv_usec = 0;
|
|
FD_ZERO(&myset);
|
|
FD_SET(m_iDeviceFD, &myset);
|
|
retVal = select(m_iDeviceFD + 1, NULL, &myset, NULL, &tv);
|
|
if (retVal < 0 && errno != EINTR)
|
|
{
|
|
char cBuffer[512];
|
|
sprintf(cBuffer, "ERROR connect to [%s:%d] %d (%s)", m_sUartData.path, m_sUartData.port, errno, strerror(errno));
|
|
LOG_ERROR(cBuffer);
|
|
|
|
close(m_iDeviceFD);
|
|
m_iDeviceFD = -1;
|
|
|
|
return false;
|
|
}
|
|
else if (retVal > 0)
|
|
{
|
|
int valopt;
|
|
// Socket selected for write
|
|
socklen_t lon = sizeof(int);
|
|
if (getsockopt(m_iDeviceFD, SOL_SOCKET, SO_ERROR, (void *) (&valopt), &lon) < 0)
|
|
{
|
|
char cBuffer[512];
|
|
sprintf(cBuffer, "ERROR connect to [%s:%d] %d (%s) in getsockopt", m_sUartData.path, m_sUartData.port, errno, strerror(errno));
|
|
LOG_ERROR(cBuffer);
|
|
close(m_iDeviceFD);
|
|
m_iDeviceFD = -1;
|
|
|
|
return false;
|
|
};
|
|
// Check the value returned...
|
|
if (valopt)
|
|
{
|
|
char cBuffer[512];
|
|
sprintf(cBuffer, "ERROR in delayed connection() %d - %s\n", valopt, strerror(valopt));
|
|
LOG_ERROR(cBuffer);
|
|
close(m_iDeviceFD);
|
|
m_iDeviceFD = -1;
|
|
|
|
return false;
|
|
};
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Timeout in select() - Cancelling!");
|
|
close(m_iDeviceFD);
|
|
m_iDeviceFD = -1;
|
|
|
|
return false;
|
|
};
|
|
}
|
|
while (1);
|
|
}
|
|
else
|
|
{
|
|
char cBuffer[512];
|
|
sprintf(cBuffer, "ERROR connect to [%s:%d] %d (%s)", m_sUartData.path, m_sUartData.port, errno, strerror(errno));
|
|
LOG_ERROR(cBuffer);
|
|
close(m_iDeviceFD);
|
|
m_iDeviceFD = -1;
|
|
|
|
return false;
|
|
};
|
|
};
|
|
|
|
#ifdef DEBUG
|
|
{
|
|
char cBuffer[512];
|
|
sprintf(cBuffer, "Connected to [%s:%d]", m_sUartData.path, m_sUartData.port);
|
|
LOG(cBuffer);
|
|
}
|
|
#endif
|
|
|
|
setConnectionStatus(CS_CONNECTED);
|
|
break;
|
|
};
|
|
};
|
|
return init();
|
|
}
|
|
|
|
/*
|
|
from https://stackoverflow.com/questions/2597608/c-socket-connection-timeout
|
|
|
|
void connect_w_to(void) {
|
|
int res;
|
|
struct sockaddr_in addr;
|
|
long arg;
|
|
fd_set myset;
|
|
struct timeval tv;
|
|
int valopt;
|
|
socklen_t lon;
|
|
|
|
// Create socket
|
|
soc = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (soc < 0) {
|
|
fprintf(stderr, "Error creating socket (%d %s)\n", errno, strerror(errno));
|
|
exit(0);
|
|
}
|
|
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(2000);
|
|
addr.sin_addr.s_addr = inet_addr("192.168.0.1");
|
|
|
|
// Set non-blocking
|
|
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
|
|
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
|
|
exit(0);
|
|
}
|
|
arg |= O_NONBLOCK;
|
|
if( fcntl(soc, F_SETFL, arg) < 0) {
|
|
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
|
|
exit(0);
|
|
}
|
|
// Trying to connect with timeout
|
|
res = connect(soc, (struct sockaddr *)&addr, sizeof(addr));
|
|
if (res < 0) {
|
|
if (errno == EINPROGRESS) {
|
|
fprintf(stderr, "EINPROGRESS in connect() - selecting\n");
|
|
do {
|
|
tv.tv_sec = 15;
|
|
tv.tv_usec = 0;
|
|
FD_ZERO(&myset);
|
|
FD_SET(soc, &myset);
|
|
res = select(soc+1, NULL, &myset, NULL, &tv);
|
|
if (res < 0 && errno != EINTR) {
|
|
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
|
|
exit(0);
|
|
}
|
|
else if (res > 0) {
|
|
// Socket selected for write
|
|
lon = sizeof(int);
|
|
if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) {
|
|
fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
|
|
exit(0);
|
|
}
|
|
// Check the value returned...
|
|
if (valopt) {
|
|
fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt)
|
|
);
|
|
exit(0);
|
|
}
|
|
break;
|
|
}
|
|
else {
|
|
fprintf(stderr, "Timeout in select() - Cancelling!\n");
|
|
exit(0);
|
|
}
|
|
} while (1);
|
|
}
|
|
else {
|
|
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
|
|
exit(0);
|
|
}
|
|
}
|
|
// Set to blocking mode again...
|
|
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) {
|
|
fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
|
|
exit(0);
|
|
}
|
|
arg &= (~O_NONBLOCK);
|
|
if( fcntl(soc, F_SETFL, arg) < 0) {
|
|
fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
|
|
exit(0);
|
|
}
|
|
// I hope that is all
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
bool deviceETH2UART::closePort()
|
|
{
|
|
//char tBuff[128];
|
|
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
#ifdef SERIAL_PING_PONG
|
|
if (m_bThreadStatus != TH_PRE_INIT)
|
|
#else
|
|
if (m_bThreadAStatus != TH_PRE_INIT && m_bThreadBStatus != TH_PRE_INIT)
|
|
#endif
|
|
{
|
|
uninit();
|
|
};
|
|
if (m_iDeviceFD != -1)
|
|
{
|
|
switch (m_sUartData.mode)
|
|
{
|
|
case SOL_UDP_CLIENT:
|
|
case SOL_TCP_CLIENT:
|
|
case SOL_UDP_SERVER:
|
|
{
|
|
#ifdef __linux__
|
|
if (m_iDeviceFD > -1)
|
|
{
|
|
close(m_iDeviceFD);
|
|
};
|
|
#else
|
|
// When your application is finished receiving datagrams close the socket.
|
|
LOG("Server: Finished receiving.Closing the listening socket...");
|
|
if (closesocket(m_sSocket) != 0)
|
|
{
|
|
sprintf(tBuff, "closesocket() failed!Error code : %ld", WSAGetLastError());
|
|
LOG_ERROR(tBuff);
|
|
}
|
|
else
|
|
{
|
|
LOG("closesocket() is OK...");
|
|
};
|
|
// When your application is finished call WSACleanup.
|
|
LOG("Server: Cleaning up...");
|
|
if (WSACleanup() != 0)
|
|
{
|
|
sprintf(tBuff, "WSACleanup() failed!Error code : %ld", WSAGetLastError());
|
|
LOG_ERROR(tBuff);
|
|
}
|
|
else
|
|
{
|
|
LOG("WSACleanup() is OK");
|
|
};
|
|
#endif
|
|
break;
|
|
};
|
|
case SOL_TCP_SERVER:
|
|
{
|
|
if (m_iDeviceFD > -1)
|
|
{
|
|
close(m_iDeviceFD);
|
|
};
|
|
break;
|
|
};
|
|
};
|
|
m_iDeviceFD = -1;
|
|
};
|
|
return true;
|
|
}
|
|
#ifdef SERIAL_PING_PONG
|
|
void deviceETH2UART::m_fThreadProc(void)
|
|
{
|
|
int index;
|
|
int RetVal;
|
|
m_bThreadStatus = TH_WORKING;
|
|
#ifdef DEBUG
|
|
LOG("Sender Thread Started!");
|
|
#endif
|
|
pthread_setname_np( pthread_self(), "E2S IO" );
|
|
while (m_bThreadStatus < TH_ENDED){
|
|
m_mBufferProtectorMutex.lock();
|
|
if (! m_qIO.empty()){
|
|
SIO_TZ * tIOData;
|
|
tIOData = m_qIO.front();
|
|
m_mBufferProtectorMutex.unlock();
|
|
if( tIOData ){
|
|
// Remove from Queue
|
|
m_mBufferProtectorMutex.lock();
|
|
m_qIO.pop();
|
|
m_mBufferProtectorMutex.unlock();
|
|
|
|
index = 0;
|
|
//Sending Data to line
|
|
while ((index < tIOData->out_size) && (m_bThreadStatus < TH_ENDED)){
|
|
#ifdef DEBUG_COMMUNICATION
|
|
char TT[128];
|
|
sprintf(TT, "Send \t%d bytes for \t%p", ( tIOData->out_size - index ), tIOData->boundParser );
|
|
LOG(TT);
|
|
#endif
|
|
RetVal = sendBlock(tIOData->out_buffer + index, tIOData->out_size - index);
|
|
if (RetVal > 0){
|
|
index += RetVal;
|
|
} else {
|
|
// if 0 or -(error)
|
|
if (RetVal == 0) {
|
|
usleep(100);
|
|
} else {
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
// m_nOutputBufferSize = 0;
|
|
tIOData->out_size = 0;
|
|
m_bThreadStatus = TH_ENDED;
|
|
LOG_ERROR("Connection Droped on Sending Data");
|
|
};
|
|
};
|
|
};
|
|
//Try to receive data from line
|
|
tIOData->out_size = 0;
|
|
bool tDataOnline = false;
|
|
uint8_t tTOCounter = 20;
|
|
do {
|
|
RetVal = recptBlock( tIOData->inp_buffer, &tIOData->inp_size );
|
|
if( RetVal == -1 ){
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
tIOData->inp_size = 0;
|
|
m_bThreadStatus = TH_ENDED;
|
|
LOG_ERROR("Connection Droped on Reception Data");
|
|
} else if( tIOData->inp_size ){
|
|
tDataOnline = true;
|
|
if( tIOData->boundParser == NULL ){
|
|
|
|
} else {
|
|
#ifdef DEBUG_COMMUNICATION
|
|
char tB[128];
|
|
sprintf( tB, "Read \t%d bytes for \t%p", tIOData->inp_size, tIOData->boundParser );
|
|
LOG( tB );
|
|
#endif
|
|
tDataOnline = tIOData->boundParser->parse( tIOData->inp_buffer, tIOData->inp_size );
|
|
};
|
|
} else {
|
|
if (tTOCounter) {
|
|
tTOCounter--;
|
|
SLEEP(5);
|
|
#ifdef DEBUG_COMMUNICATION
|
|
} else {
|
|
LOG_ERROR("PACK TIMEOUT!");
|
|
#endif
|
|
};
|
|
};
|
|
|
|
} while ((m_bThreadStatus < TH_ENDED) && tTOCounter && ! (tDataOnline && RetVal == 0)); // Pack1 conditions -> White Space after Data.
|
|
// Free Package
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Finishing Read Package");
|
|
#endif
|
|
free(tIOData);
|
|
} else {
|
|
// Mutex already unlocked!
|
|
SLEEP(5);
|
|
};
|
|
} else {
|
|
m_mBufferProtectorMutex.unlock();
|
|
SLEEP(5);
|
|
};
|
|
};
|
|
}
|
|
|
|
bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src ){
|
|
if ( m_bThreadStatus != TH_WORKING ) {
|
|
if( m_bThreadStatus < TH_WORKING ) {
|
|
LOG_ERROR("Trying to Send Data Before Thread is Started!");
|
|
} else {
|
|
LOG_ERROR("Trying to Send Data After Thread is Dead!");
|
|
}
|
|
return false;
|
|
};
|
|
if (DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH) {
|
|
LOG_ERROR("Data Too BIG");
|
|
} else {
|
|
SIO_TZ * tData = (SIO_TZ *) malloc(sizeof(SIO_TZ));
|
|
if (tData) {
|
|
memset( tData, 0, sizeof(SIO_TZ) );
|
|
tData->out_size = DataSize;
|
|
tData->boundParser = src;
|
|
memcpy( tData->out_buffer, Data, DataSize );
|
|
m_mBufferProtectorMutex.lock();
|
|
m_qIO.push(tData);
|
|
m_mBufferProtectorMutex.unlock();
|
|
return true;
|
|
} else {
|
|
LOG_ERROR("MemAlloc Error!");
|
|
};
|
|
};
|
|
return false;
|
|
}
|
|
|
|
|
|
#else // !SERIAL_PING_PONG
|
|
void deviceETH2UART::m_fThreadAProc(void)
|
|
{
|
|
int index;
|
|
int RetVal;
|
|
m_bThreadAStatus = TH_WORKING;
|
|
#ifdef DEBUG
|
|
LOG("Sender Thread Started!");
|
|
#endif
|
|
pthread_setname_np( pthread_self(), "E2S Sender" );
|
|
while (m_bThreadAStatus < TH_ENDED)
|
|
{
|
|
m_mBufferProtectorMutex.lock();
|
|
if (m_nOutputBufferSize > 0)
|
|
{
|
|
index = 0;
|
|
while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED))
|
|
{
|
|
RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
|
|
if (RetVal > 0)
|
|
{
|
|
index += RetVal;
|
|
}
|
|
if (RetVal == -1)
|
|
{
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
m_nOutputBufferSize = 0;
|
|
m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
LOG_ERROR("Connection Droped on Sending Data");
|
|
};
|
|
};
|
|
m_nOutputBufferSize = 0;
|
|
m_mBufferProtectorMutex.unlock();
|
|
}
|
|
else
|
|
{
|
|
m_mBufferProtectorMutex.unlock();
|
|
SLEEP(5);
|
|
};
|
|
};
|
|
}
|
|
|
|
void deviceETH2UART::m_fThreadBProc(void)
|
|
{
|
|
m_bThreadBStatus = TH_WORKING;
|
|
int RetVal;
|
|
#ifdef DEBUG
|
|
LOG("Recipient Thread Started!");
|
|
#endif
|
|
pthread_setname_np( pthread_self(), "E2S Receiver" );
|
|
while (m_bThreadBStatus < TH_ENDED)
|
|
{
|
|
RetVal = recptBlock(m_pInputBuffer, &m_nInputBufferSize);
|
|
if (RetVal == -1)
|
|
{
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
m_nInputBufferSize = 0;
|
|
// if(!getStatus()) {
|
|
m_bThreadBStatus = TH_ENDED;
|
|
m_bThreadAStatus = TH_ENDED;
|
|
//} else SLEEP(100);
|
|
LOG_ERROR("Connection Droped on Reception Data");
|
|
}
|
|
else if (m_nInputBufferSize > 0)
|
|
{
|
|
do
|
|
{
|
|
m_mQueueProtectorMutex.lock();
|
|
if (m_vParsers.size())
|
|
{
|
|
for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++)
|
|
{
|
|
(*index)->parse(m_pInputBuffer, m_nInputBufferSize);
|
|
}
|
|
m_nInputBufferSize = 0;
|
|
m_mQueueProtectorMutex.unlock();
|
|
}
|
|
else
|
|
{
|
|
m_nInputBufferSize = 0;
|
|
m_mQueueProtectorMutex.unlock();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG_ERROR("Try to parse data but No Parsers Assigned!");
|
|
#endif
|
|
SLEEP(100);
|
|
};
|
|
}
|
|
while ((m_vParsers.size() == 0) && (m_bThreadBStatus < TH_ENDED) && (m_bThreadBStatus < TH_ENDED));
|
|
}
|
|
else
|
|
{
|
|
SLEEP(5);
|
|
};
|
|
};
|
|
}
|
|
|
|
bool deviceETH2UART::sendData(uint8_t * Data, int DataSize)
|
|
{
|
|
if (m_bThreadAStatus != TH_WORKING)
|
|
{
|
|
if (m_bThreadAStatus < TH_WORKING)
|
|
{
|
|
LOG_ERROR("Trying to Send Data Before Thread is Started!");
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Trying to Send Data After Thread is Dead!");
|
|
}
|
|
return false;
|
|
};
|
|
m_mBufferProtectorMutex.lock();
|
|
if (m_nOutputBufferSize + DataSize > MAXIMAL_SOCKET_IO_BUFFER_LENGTH)
|
|
{
|
|
m_mBufferProtectorMutex.unlock();
|
|
LOG_ERROR("Data Too BIG");
|
|
return false;
|
|
};
|
|
memcpy(m_pOutputBuffer + m_nOutputBufferSize, Data, DataSize);
|
|
m_nOutputBufferSize += DataSize;
|
|
m_mBufferProtectorMutex.unlock();
|
|
return true;
|
|
}
|
|
#endif // !SERIAL_PING_PONG
|
|
|
|
bool deviceETH2UART::reconnect(void)
|
|
{
|
|
closePort();
|
|
m_bValid = create_SOL();
|
|
return m_bValid;
|
|
}
|
|
|
|
int deviceETH2UART::recptBlock(unsigned char * Buffer, int * BufferLength)
|
|
{
|
|
|
|
#ifdef __linux__
|
|
int RetVal = 0;
|
|
#else
|
|
DWORD RetVal = 0;
|
|
#endif
|
|
|
|
if (m_eConnectionStatus == CS_CONNECTED)
|
|
{
|
|
#ifdef __linux__
|
|
if (m_iDeviceFD > -1)
|
|
{
|
|
struct sockaddr ClientAddress;
|
|
socklen_t addr_len = sizeof(ClientAddress); // len is value/resuslt
|
|
// RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_WAITALL, ( struct sockaddr *) &m_sClientAddr, &addr_len );
|
|
RetVal = recvfrom(m_iDeviceFD, (char *) Buffer, MAXIMAL_SOCKET_IO_BUFFER_LENGTH, MSG_DONTWAIT, (struct sockaddr *) &ClientAddress, &addr_len);
|
|
if (RetVal < 1)
|
|
{
|
|
if (errno != EAGAIN && errno != EINTR)
|
|
{
|
|
LOG_ERROR("Peer Closes Connection!");
|
|
shutdown(m_iDeviceFD, 2);
|
|
close(m_iDeviceFD);
|
|
*BufferLength = 0;
|
|
m_iDeviceFD = -1;
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
return -1;
|
|
};
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Not Connected!");
|
|
*BufferLength = 0;
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
return -1;
|
|
};
|
|
#else
|
|
switch (m_eType)
|
|
{
|
|
case SOL_UDP_CLIENT:
|
|
case SOL_TCP_CLIENT:
|
|
case SOL_UDP_SERVER:
|
|
{
|
|
SOCKADDR_IN tAddr;
|
|
int tLen = sizeof(SOCKADDR_IN);
|
|
RetVal = recvfrom(m_sSocket, (char *) Buffer, MAXIMAL_SOCKET_IO_BUFFER_LENGTH, 0, (struct sockaddr *) &tAddr, &tLen);
|
|
if (RetVal == SOCKET_ERROR)
|
|
{
|
|
char cBuffer[165];
|
|
sprintf(cBuffer, "recvfrom() failed with error code : %d", WSAGetLastError());
|
|
LOG_ERROR(cBuffer);
|
|
};
|
|
break;
|
|
};
|
|
};
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
// make prep for connection SOL_TCP_SERVER
|
|
if (m_sUartData.mode == SOL_TCP_CLIENT)
|
|
{
|
|
// ACCEPT
|
|
struct sockaddr ClientAddress;
|
|
socklen_t size = (socklen_t) sizeof(struct sockaddr_in);
|
|
// int ClientSocket;
|
|
m_iDeviceFD = accept(m_iMasterFD, &ClientAddress, &size);
|
|
if (m_iDeviceFD < 0)
|
|
{
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
LOG_ERROR("Connection Accept Failure!");
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("RCPT ON not connected!");
|
|
shutdown(m_iDeviceFD, 2);
|
|
close(m_iDeviceFD);
|
|
*BufferLength = 0;
|
|
m_iDeviceFD = -1;
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
};
|
|
return -1; // Exit after unsuccess reconnect
|
|
};
|
|
|
|
if (RetVal > 0)
|
|
{
|
|
#ifdef DEBUG_SERIAL_COMMUNICATION
|
|
char MsgBuffer[CLOG];
|
|
if (RetVal > 1 || RetVal == 0)
|
|
{
|
|
sprintf(MsgBuffer, "Received %d bytes", RetVal);
|
|
}
|
|
else
|
|
{
|
|
sprintf(MsgBuffer, "Received 1 byte");
|
|
};
|
|
LOG(MsgBuffer);
|
|
for (int i = 0; i < RetVal; i++)
|
|
{
|
|
sprintf(MsgBuffer, "0x%02X", Buffer[i]);
|
|
LOG(MsgBuffer);
|
|
};
|
|
#endif
|
|
*BufferLength = RetVal;
|
|
return RetVal;
|
|
};
|
|
|
|
*BufferLength = 0;
|
|
|
|
if (RetVal == -1)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (RetVal == 0)
|
|
{
|
|
errno = 0;
|
|
return 0;
|
|
};
|
|
|
|
/* r < -1, should never happen. */
|
|
errno = EIO;
|
|
return 0;
|
|
}
|
|
|
|
int deviceETH2UART::sendBlock(unsigned char * Data, int DataSize)
|
|
{
|
|
#ifdef __linux__
|
|
int RetVal = 0;
|
|
#else
|
|
DWORD RetVal = 0;
|
|
#endif
|
|
|
|
if (m_eConnectionStatus == CS_CONNECTED)
|
|
{
|
|
#ifdef __linux__
|
|
if (m_iDeviceFD > -1)
|
|
{
|
|
|
|
#ifdef DEBUG_EXTRA
|
|
char cBuffer[256];
|
|
sprintf(cBuffer, "Write into Port %d bytes", DataSize);
|
|
LOG(cBuffer);
|
|
#endif
|
|
struct sockaddr ClientAddress;
|
|
socklen_t addr_len = sizeof(ClientAddress);
|
|
RetVal = sendto(m_iDeviceFD, (const char *) Data, DataSize, MSG_CONFIRM, (const struct sockaddr *) &ClientAddress, addr_len);
|
|
|
|
if ((RetVal == -1) && (errno != EINTR) && (errno != EWOULDBLOCK))
|
|
{
|
|
LOG_ERROR("Error acures while writing data into the Socket");
|
|
shutdown(m_iDeviceFD, 2);
|
|
close(m_iDeviceFD);
|
|
m_iDeviceFD = -1;
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
return -1;
|
|
};
|
|
|
|
if (RetVal < 0)
|
|
{
|
|
RetVal = 0;
|
|
}
|
|
|
|
#ifdef DEBUG_EXTRA
|
|
sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
|
|
LOG(cBuffer);
|
|
#ifdef DEBUG_SERIAL_COMMUNICATION
|
|
for (int zz = 0; zz < RetVal; zz++)
|
|
{
|
|
sprintf(cBuffer, "SD: 0x%02X", (uint8_t) Data[zz]);
|
|
LOG(cBuffer);
|
|
};
|
|
#endif
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Not Connected!");
|
|
// m_bConnected = false;
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
return -1;
|
|
};
|
|
#else // Windows
|
|
if (sendto(m_sSocket, (char *) Data, DataSize, 0, (struct sockaddr *) &ReceiverAddr, sizeof(ReceiverAddr)) == SOCKET_ERROR)
|
|
{
|
|
char cBuffer[256];
|
|
sprintf(cBuffer, "sendto() failed with error code : %d", WSAGetLastError());
|
|
LOG_ERROR(cBuffer);
|
|
return -1;
|
|
};
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG
|
|
LOG("Port is not Ready to Send");
|
|
#endif
|
|
return -1;
|
|
};
|
|
return RetVal;
|
|
}
|
|
|
|
#undef LOG_ERROR
|
|
#undef LOG
|