Skip to content

Commit

Permalink
Sync with xcp-lite
Browse files Browse the repository at this point in the history
XcpEthServer optional, XcpTlTransmitQueuePeek and XcpTlTransmitQueueNext
Option for forcefull or gracefull thread termination
Segment_size parameters removed
SendCallback removed
Type of BOOL changed to uint8_t to simplify FFI
XcpReset
  • Loading branch information
RainerZ committed Sep 12, 2024
1 parent 8b275c8 commit d1adee9
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 268 deletions.
4 changes: 2 additions & 2 deletions src/dbg_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
//-------------------------------------------------------------------------------
// Debug print

#if OPTION_ENABLE_DBG_PRINTS
#ifdef XCP_ENABLE_DBG_PRINTS

extern unsigned int gDebugLevel;
extern uint8_t gDebugLevel;
#define DBG_LEVEL gDebugLevel

/*
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
#include <vector>
#endif

#define BOOL int
#define BOOL uint8_t
#define FALSE 0
#define TRUE 1

Expand Down
18 changes: 15 additions & 3 deletions src/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "platform.h"
#include "dbg_print.h"

#ifndef __MAIN_CFG_H__
#error "Include dependency error!"
#endif

#if defined(_WIN) // Windows // Windows needs to link with Ws2_32.lib

#pragma comment(lib, "ws2_32.lib")
Expand Down Expand Up @@ -189,6 +193,7 @@ void mutexDestroy(MUTEX* m) {
// Sockets
/**************************************************************************/

#if defined(XCPTL_ENABLE_UDP) || defined(XCPTL_ENABLE_TCP)

#ifdef _LINUX

Expand Down Expand Up @@ -237,14 +242,17 @@ BOOL socketBind(SOCKET sock, uint8_t* addr, uint16_t port) {
return TRUE;
}


// Shutdown socket
// Block rx and tx direction
BOOL socketShutdown(SOCKET sock) {
if (sock != INVALID_SOCKET) {
shutdown(sock, SHUT_RDWR);
}
return TRUE;
}

// Close socket
// Make addr reusable
BOOL socketClose(SOCKET *sp) {
if (*sp != INVALID_SOCKET) {
close(*sp);
Expand All @@ -257,10 +265,10 @@ BOOL socketClose(SOCKET *sp) {

#ifdef PLATFORM_ENABLE_GET_LOCAL_ADDR

#include <ifaddrs.h>
#ifndef __APPLE__
#include <linux/if_packet.h>
#else
#include <ifaddrs.h>
#include <net/if_dl.h>
#endif

Expand Down Expand Up @@ -458,7 +466,8 @@ BOOL socketBind(SOCKET sock, uint8_t *addr, uint16_t port) {
return TRUE;
}


// Shutdown socket
// Block rx and tx direction
BOOL socketShutdown(SOCKET sock) {

if (sock != INVALID_SOCKET) {
Expand All @@ -467,6 +476,8 @@ BOOL socketShutdown(SOCKET sock) {
return TRUE;
}

// Close socket
// Make addr reusable
BOOL socketClose(SOCKET* sockp) {

if (*sockp != INVALID_SOCKET) {
Expand Down Expand Up @@ -648,6 +659,7 @@ int16_t socketSend(SOCKET sock, const uint8_t* buffer, uint16_t size) {
return (int16_t)send(sock, (const char *)buffer, size, 0);
}

#endif

/**************************************************************************/
// Clock
Expand Down
14 changes: 9 additions & 5 deletions src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
| Code released into public domain, no attribution required
*/


#define PLATFORM_ENABLE_GET_LOCAL_ADDR
#define PLATFORM_ENABLE_KEYBOARD
#ifndef __MAIN_CFG_H__
#error "Include dependency error!"
#endif

//-------------------------------------------------------------------------------
// Keyboard
Expand Down Expand Up @@ -76,21 +76,23 @@ void mutexDestroy(MUTEX* m);
typedef HANDLE tXcpThread;
#define create_thread(h,t) *h = CreateThread(0, 0, t, NULL, 0, NULL)
#define join_thread(h) WaitForSingleObject(h, INFINITE);
#define terminate_thread(h) { TerminateThread(h,0); WaitForSingleObject(h,1000); CloseHandle(h); }
#define cancel_thread(h) { TerminateThread(h,0); WaitForSingleObject(h,1000); CloseHandle(h); }

#elif defined(_LINUX) // Linux

typedef pthread_t tXcpThread;
#define create_thread(h,t) pthread_create(h, NULL, t, NULL);
#define join_thread(h) pthread_join(h,NULL);
#define detach_thread(h) { pthread_detach(h); pthread_cancel(h); }
#define cancel_thread(h) { pthread_detach(h); pthread_cancel(h); }

#endif


//-------------------------------------------------------------------------------
// Platform independant socket functions

#if defined(XCPTL_ENABLE_UDP) || defined(XCPTL_ENABLE_TCP)

#ifdef _LINUX // Linux sockets

#define SOCKET int
Expand Down Expand Up @@ -153,6 +155,8 @@ extern BOOL socketClose(SOCKET* sp);
extern BOOL socketGetLocalAddr(uint8_t* mac, uint8_t* addr);
#endif

#endif

//-------------------------------------------------------------------------------
// Clock

Expand Down
43 changes: 33 additions & 10 deletions src/xcpEthServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include "xcpLite.h"
#include "xcpEthServer.h"

#ifndef __MAIN_CFG_H__
#error "Include dependency error!"
#endif

#if defined(XCPTL_ENABLE_UDP) || defined(XCPTL_ENABLE_TCP)

#if defined(_WIN) // Windows
static DWORD WINAPI XcpServerReceiveThread(LPVOID lpParameter);
Expand All @@ -36,9 +41,9 @@ static struct {
BOOL isInit;

// Threads
tXcpThread DAQThreadHandle;
tXcpThread TransmitThreadHandle;
volatile BOOL TransmitThreadRunning;
tXcpThread CMDThreadHandle;
tXcpThread ReceiveThreadHandle;
volatile BOOL ReceiveThreadRunning;

} gXcpServer;
Expand All @@ -51,7 +56,7 @@ BOOL XcpEthServerStatus() {


// XCP server init
BOOL XcpEthServerInit(const uint8_t* addr, uint16_t port, BOOL useTCP, uint16_t segmentSize)
BOOL XcpEthServerInit(const uint8_t* addr, uint16_t port, BOOL useTCP)
{
int r = 0;

Expand All @@ -61,39 +66,55 @@ BOOL XcpEthServerInit(const uint8_t* addr, uint16_t port, BOOL useTCP, uint16_t
// Init network sockets
if (!socketStartup()) return FALSE;

gXcpServer.TransmitThreadRunning = 0;
gXcpServer.ReceiveThreadRunning = 0;
gXcpServer.TransmitThreadRunning = FALSE;
gXcpServer.ReceiveThreadRunning = FALSE;

// Initialize XCP protocol layer if not already done
XcpInit();

// Initialize XCP transport layer
r = XcpEthTlInit(addr, port, useTCP, segmentSize, TRUE /*blocking rx*/);
r = XcpEthTlInit(addr, port, useTCP, TRUE /*blocking rx*/);
if (!r) return 0;

// Start XCP protocol layer
XcpStart();

// Create threads
create_thread(&gXcpServer.DAQThreadHandle, XcpServerTransmitThread);
create_thread(&gXcpServer.CMDThreadHandle, XcpServerReceiveThread);
create_thread(&gXcpServer.TransmitThreadHandle, XcpServerTransmitThread);
create_thread(&gXcpServer.ReceiveThreadHandle, XcpServerReceiveThread);

gXcpServer.isInit = TRUE;
return TRUE;
}

BOOL XcpEthServerShutdown() {

#ifdef XCP_SERVER_FORCEFULL_TERMINATION
// Forcefull termination
if (gXcpServer.isInit) {
DBG_PRINT3("Disconnect, cancel threads and shutdown XCP!\n");
XcpDisconnect();
cancel_thread(gXcpServer.ReceiveThreadHandle);
cancel_thread(gXcpServer.TransmitThreadHandle);
XcpEthTlShutdown();
gXcpServer.isInit = FALSE;
socketCleanup();
XcpReset();
}
#else
// Gracefull termination
if (gXcpServer.isInit) {
XcpDisconnect();
gXcpServer.ReceiveThreadRunning = FALSE;
gXcpServer.TransmitThreadRunning = FALSE;
XcpEthTlShutdown();
join_thread(gXcpServer.CMDThreadHandle);
join_thread(gXcpServer.DAQThreadHandle);
join_thread(gXcpServer.ReceiveThreadHandle);
join_thread(gXcpServer.TransmitThreadHandle);
gXcpServer.isInit = FALSE;
socketCleanup();
XcpReset();
}
#endif
return TRUE;
}

Expand Down Expand Up @@ -156,3 +177,5 @@ extern void* XcpServerTransmitThread(void* par)
return 0;
}


#endif
5 changes: 4 additions & 1 deletion src/xcpEthServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
/* Copyright(c) Vector Informatik GmbH.All rights reserved.
Licensed under the MIT license.See LICENSE file in the project root for details. */

extern BOOL XcpEthServerInit(const uint8_t *addr, uint16_t port, BOOL useTCP, uint16_t segmentSize);
#if defined(XCPTL_ENABLE_UDP) || defined(XCPTL_ENABLE_TCP)

extern BOOL XcpEthServerInit(const uint8_t *addr, uint16_t port, BOOL useTCP);
extern BOOL XcpEthServerShutdown();
extern BOOL XcpEthServerStatus();

#endif
Loading

0 comments on commit d1adee9

Please sign in to comment.