Skip to content

Commit

Permalink
Reworked Eth implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed May 23, 2021
1 parent fe11413 commit 2ed68fb
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 210 deletions.
4 changes: 3 additions & 1 deletion inc/xcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ extern "C"
* Interface defaults.
*/
#define XCP_ETH_DEFAULT_PORT (5555)
#define XCP_SOCKET_CAN_DEFAULT_IF ("vcan0")
#define XCP_SOCKET_CAN_DEFAULT_IF ("vcan0")

#define XCP_ETH_HEADER_SIZE (4)

/*
** Global Types.
Expand Down
120 changes: 94 additions & 26 deletions src/tl/eth/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
#define SOCKET_ERROR (-1)
#endif

static void XcpTl_Accept(void);
static int XcpTl_ReadHeader(uint16_t * len, uint16_t * counter);
static int XcpTl_ReadData(uint8_t * data, uint8_t len);

XcpTl_ConnectionType XcpTl_Connection;

static uint8_t XcpTl_RxBuffer[XCP_COMM_BUFLEN];


void * XcpTl_Thread(void * param)
{
Expand All @@ -46,9 +51,8 @@ void * XcpTl_Thread(void * param)

void XcpTl_MainFunction(void)
{
if (XcpTl_FrameAvailable(0, 1000) > 0) {
XcpTl_RxHandler();
}
XcpTl_Accept();
XcpTl_RxHandler();
}

void *get_in_addr(struct sockaddr *sa)
Expand All @@ -59,31 +63,95 @@ void *get_in_addr(struct sockaddr *sa)
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int16_t XcpTl_FrameAvailable(uint32_t sec, uint32_t usec)
static int XcpTl_ReadHeader(uint16_t * len, uint16_t * counter)
{
struct timeval timeout;
fd_set fds;
int16_t res;

timeout.tv_sec = sec;
timeout.tv_usec = usec;

FD_ZERO(&fds);
FD_SET(XcpTl_Connection.boundSocket, &fds);

// Return value:
// -1: error occurred
// 0: timed out
// > 0: data ready to be read
if (((XcpTl_Connection.socketType == SOCK_STREAM) && (!XcpTl_Connection.connected)) || (XcpTl_Connection.socketType == SOCK_DGRAM)) {
res = select(0, &fds, 0, 0, &timeout);
if (res == SOCKET_ERROR) {
XcpHw_ErrorMsg("XcpTl_FrameAvailable:select()", WSAGetLastError());
uint8_t header_buffer[8];
uint8_t bytes_remaining = XCP_ETH_HEADER_SIZE;
uint8_t offset = 0;
uint8_t nbytes = 0;

XCP_FOREVER {
nbytes = recv(XcpTl_Connection.connectedSocket, (char*)header_buffer + offset, bytes_remaining, 0);
if (nbytes < 1) {
return nbytes;
}
bytes_remaining -= nbytes;
if (bytes_remaining == 0) {
break;
}
offset += nbytes;
}
*len = XCP_MAKEWORD(header_buffer[offset + 1], header_buffer[offset + 0]);
*counter = XCP_MAKEWORD(header_buffer[offset + 3], header_buffer[offset + 2]);
return 1;
}

static int XcpTl_ReadData(uint8_t * data, uint8_t len)
{
uint8_t bytes_remaining = len;
uint8_t offset = 0;
uint8_t nbytes = 0;

XCP_FOREVER {
nbytes = recv(XcpTl_Connection.connectedSocket, (char*)data + offset, bytes_remaining, 0);
if (nbytes < 1) {
return nbytes;
}
bytes_remaining -= nbytes;
if (bytes_remaining == 0) {
break;
}
offset += nbytes;
}
}

void XcpTl_RxHandler(void)
{
int res;
uint16_t dlc = 0U;
uint16_t counter = 0U;

ZeroMemory(XcpTl_RxBuffer, XCP_COMM_BUFLEN);

XCP_FOREVER {
res = XcpTl_ReadHeader(&dlc, &counter);
if (res == -1) {
XcpHw_ErrorMsg("XcpTl_RxHandler:XcpTl_ReadHeader()", WSAGetLastError());
exit(2);
} else if (res == 0) {
return;
}
res = XcpTl_ReadData(&XcpTl_RxBuffer[0], dlc);
if (res == -1) {
XcpHw_ErrorMsg("XcpTl_RxHandler:XcpTl_ReadData()", WSAGetLastError());
exit(2);
} else if (res == 0) {
return;
}
return res;
} else {
return 1;
XcpUtl_MemCopy(Xcp_CtoIn.data, XcpTl_RxBuffer, dlc);
Xcp_DispatchCommand(&Xcp_CtoIn);
}
}

static void XcpTl_Accept(void)
{
socklen_t FromLen = 0;
struct sockaddr_storage From;

XcpUtl_ZeroMem(XcpTl_RxBuffer, XCP_COMM_BUFLEN);

if (XcpTl_Connection.socketType == SOCK_STREAM) {
if (!XcpTl_Connection.connected) {
FromLen = sizeof(From);
XcpTl_Connection.connectedSocket = accept(XcpTl_Connection.boundSocket, (struct sockaddr *)&XcpTl_Connection.currentAddress, &FromLen);
if (XcpTl_Connection.connectedSocket == -1) {
XcpHw_ErrorMsg("XcpTl_RxHandler::accept()", errno);
//WSACleanup();
exit(1);
return;
}
}

}
}

Expand Down Expand Up @@ -114,7 +182,7 @@ bool XcpTl_VerifyConnection(void)

void XcpTl_PrintConnectionInformation(void)
{
printf("XCPonEth -- Listening on port %s / %s [%s]\n\r",
printf("XCPonEth -- Listening on port %u / %s [%s]\n\r",
XCP_ETH_DEFAULT_PORT,
Xcp_Options.tcp ? "TCP" : "UDP",
Xcp_Options.ipv6 ? "IPv6" : "IPv4"
Expand Down
74 changes: 0 additions & 74 deletions src/tl/eth/linuxeth.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
#include "xcp_eth.h"


unsigned char XcpTl_RxBuffer[XCP_COMM_BUFLEN];
socklen_t addrSize = sizeof(struct sockaddr_storage);

static XcpTl_ConnectionType XcpTl_Connection;

static bool Xcp_EnableSocketOption(int sock, int option);
static bool Xcp_DisableSocketOption(int sock, int option);
static void * XcpTl_WorkerThread(void * param);

static bool Xcp_EnableSocketOption(int sock, int option)
{
Expand Down Expand Up @@ -123,78 +121,6 @@ void XcpTl_DeInit(void)
close(XcpTl_Connection.boundSocket);
}

void XcpTl_RxHandler(void)
{
int recv_len = 0;
uint16_t dlc = 0;
socklen_t FromLen = 0;
struct sockaddr_storage From;
char hostname[NI_MAXHOST];

XcpUtl_ZeroMem(XcpTl_RxBuffer, XCP_COMM_BUFLEN);

if (XcpTl_Connection.socketType == SOCK_STREAM) {
if (!XcpTl_Connection.connected) {
FromLen = sizeof(From);
XcpTl_Connection.connectedSocket = accept(XcpTl_Connection.boundSocket, (struct sockaddr *)&XcpTl_Connection.currentAddress, &FromLen);
if (XcpTl_Connection.connectedSocket == -1) {
XcpHw_ErrorMsg("XcpTl_RxHandler::accept()", errno);
//WSACleanup();
exit(1);
return;
}
inet_ntop(XcpTl_Connection.currentAddress.ss_family, get_in_addr((struct sockaddr *)&XcpTl_Connection.currentAddress), hostname, sizeof(hostname));
//printf("server: got connection from %s\n", hostname);
}
recv_len = recv(XcpTl_Connection.connectedSocket, (char*)XcpTl_RxBuffer, XCP_COMM_BUFLEN, 0);
if (recv_len == -1) {
if (errno != EAGAIN) {
XcpHw_ErrorMsg("XcpTl_RxHandler::recv()", errno);
close(XcpTl_Connection.connectedSocket);
exit(1);
return;
}
}
if (recv_len == 0) {
DBG_PRINT1("Client closed connection\n\r");
close(XcpTl_Connection.connectedSocket);
Xcp_Disconnect();
return;
}
} else {
recv_len = recvfrom(XcpTl_Connection.boundSocket, (char*)XcpTl_RxBuffer, XCP_COMM_BUFLEN, 0,
(struct sockaddr *)&XcpTl_Connection.currentAddress, &addrSize
);
if (recv_len == -1)
{
XcpHw_ErrorMsg("XcpTl_RxHandler:recvfrom()", errno);
fflush(stdout);
exit(1);
}
//printf("Received %d bytes from client: ", recv_len);
//Xcp_Hexdump(XcpTl_RxBuffer, recv_len);
}
if (recv_len > 0) {
#if XCP_TRANSPORT_LAYER_LENGTH_SIZE == 1
dlc = (uint16_t)XcpTl_RxBuffer[0];
#elif XCP_TRANSPORT_LAYER_LENGTH_SIZE == 2
dlc = XCP_MAKEWORD(XcpTl_RxBuffer[0], XcpTl_RxBuffer[1]);
//dlc = (uint16_t)*(XcpTl_RxBuffer + 0);
#endif // XCP_TRANSPORT_LAYER_LENGTH_SIZE
if (!XcpTl_Connection.connected || (XcpTl_VerifyConnection())) {
Xcp_CtoIn.len = dlc;
XcpUtl_MemCopy(Xcp_CtoIn.data, XcpTl_RxBuffer + XCP_TRANSPORT_LAYER_BUFFER_OFFSET, recv_len - XCP_TRANSPORT_LAYER_BUFFER_OFFSET);
Xcp_DispatchCommand(&Xcp_CtoIn);
}
if (recv_len < 5) {
DBG_PRINT2("Error: frame to short: %d\n\r", recv_len);
} else {

}
fflush(stdout);
}
}

void XcpTl_Send(uint8_t const * buf, uint16_t len)
{

Expand Down
109 changes: 0 additions & 109 deletions src/tl/eth/wineth.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
void XcpHw_ErrorMsg(char * const function, int errorCode);


uint8_t XcpTl_RxBuffer[XCP_COMM_BUFLEN];

extern XcpTl_ConnectionType XcpTl_Connection;

void Xcp_DispatchCommand(Xcp_PduType const * const pdu);
Expand Down Expand Up @@ -182,112 +180,6 @@ void XcpTl_DeInit(void)
WSACleanup();
}

int XcpTl_Read(uint8_t * buffer, size_t offset)
{
int nbytes;

if (XcpTl_Connection.socketType == SOCK_STREAM) {
if (!XcpTl_Connection.connected) {
XcpTl_Connection.connectedSocket = accept(XcpTl_Connection.boundSocket, (LPSOCKADDR)&XcpTl_Connection.currentAddress, NULL);
if (XcpTl_Connection.connectedSocket == INVALID_SOCKET) {
XcpHw_ErrorMsg("XcpTl_RxHandler::accept()", WSAGetLastError());
exit(1);
}

}
nbytes = recv(XcpTl_Connection.connectedSocket, (char*)buffer + offset, XCP_COMM_BUFLEN, 0);
if (nbytes == SOCKET_ERROR) {
XcpHw_ErrorMsg("XcpTl_RxHandler::recv()", WSAGetLastError());
closesocket(XcpTl_Connection.connectedSocket);
exit(1);
}
printf("len: %u\n\r", nbytes);
XcpUtl_Hexdump(XcpTl_RxBuffer + offset, nbytes);

if (nbytes == 0) {
DBG_PRINT1("Client closed connection\n\r");
closesocket(XcpTl_Connection.connectedSocket);
Xcp_Disconnect();
}
} else {
nbytes = recvfrom(XcpTl_Connection.boundSocket, (char*)buffer + offset, XCP_COMM_BUFLEN, 0,
(LPSOCKADDR)&XcpTl_Connection.currentAddress, NULL
);
if (nbytes == SOCKET_ERROR)
{
XcpHw_ErrorMsg("XcpTl_RxHandler:recvfrom()", WSAGetLastError());
exit(1);
}
}
return nbytes;
}

void XcpTl_RxHandler(void)
{
int nbytes;
uint16_t dlc = 0U;
uint16_t counter = 0U;
static uint16_t offset = 0U;
static uint16_t bytes_to_read = 0U;
static uint8_t state = 0;

ZeroMemory(XcpTl_RxBuffer, XCP_COMM_BUFLEN);

XCP_FOREVER {
nbytes = XcpTl_Read(XcpTl_RxBuffer, 0);
if (nbytes == 0) {
return;
} else if (nbytes > 0) {
printf("nbytes: %u\n", nbytes);
offset += nbytes;
if (nbytes >= 2) {
dlc = MAKEWORD(XcpTl_RxBuffer[0], XcpTl_RxBuffer[1]);
Xcp_CtoIn.len = dlc;
printf("\tdlc: %u\n\r", dlc);

if ((dlc + 4) <= nbytes) {
printf("COMPLETE frame!!\n");
XcpUtl_MemCopy(Xcp_CtoIn.data, XcpTl_RxBuffer + XCP_TRANSPORT_LAYER_BUFFER_OFFSET, nbytes - XCP_TRANSPORT_LAYER_BUFFER_OFFSET);
Xcp_DispatchCommand(&Xcp_CtoIn);
}
}
} else {

}
if (nbytes < XCP_COMM_BUFLEN) {
break;
}
}
#if 0
if (nbytes > 0) {
printf("len: %u\n\r", nbytes);
XcpUtl_Hexdump(XcpTl_RxBuffer + offset, nbytes);
offset += nbytes;
if (state == 1) {
if (offset == 2) {

state = 2;
bytes_to_read = dlc + 2; /* Consider counter. */
} else {
bytes_to_read = 1; /* we got only a single byte. */
}
printf("\t\tcontinue with [%d] bytes\n\r",bytes_to_read);
} else if (state == 2) {
bytes_to_read -= nbytes;
if (bytes_to_read == 0) {
if (!XcpTl_Connection.connected || (XcpTl_VerifyConnection())) {

XcpUtl_Hexdump(XcpTl_RxBuffer, nbytes);

}
}
}
} else {

}
#endif
}


void XcpTl_Send(uint8_t const * buf, uint16_t len)
{
Expand All @@ -305,4 +197,3 @@ void XcpTl_Send(uint8_t const * buf, uint16_t len)
}
XCP_TL_LEAVE_CRITICAL();
}

0 comments on commit 2ed68fb

Please sign in to comment.