From e7adc1387b87a50e690c47dbd6f0d6e14f039f9e Mon Sep 17 00:00:00 2001 From: Rainer Zaiser Date: Thu, 12 Sep 2024 10:25:46 +0200 Subject: [PATCH] Bugfix XcpEthServerShutdown incorrect thread termination --- .gitignore | 3 ++- CPP_Demo/build.sh | 6 ++++++ C_Demo/build.sh | 6 ++++++ C_Demo/main.c | 4 ++++ XCPlite/build.sh | 6 ++++++ XCPlite/main_cfg.h | 21 ++++++++++----------- src/platform.h | 2 ++ src/xcpCanServer.c | 4 ++-- src/xcpEthServer.c | 21 +++++++++++++++++++-- src/xcpLite.c | 5 +++++ src/xcpLite.h | 1 + 11 files changed, 63 insertions(+), 16 deletions(-) create mode 100755 CPP_Demo/build.sh create mode 100755 C_Demo/build.sh create mode 100755 XCPlite/build.sh diff --git a/.gitignore b/.gitignore index cad6508..ab45b47 100644 --- a/.gitignore +++ b/.gitignore @@ -53,4 +53,5 @@ Xcode_build/ Environment CustomProperties.json -*.json +/.vscode/settings.json +/.vscode/tasks.json diff --git a/CPP_Demo/build.sh b/CPP_Demo/build.sh new file mode 100755 index 0000000..2b02bca --- /dev/null +++ b/CPP_Demo/build.sh @@ -0,0 +1,6 @@ + +cmake -DCMAKE_BUILD_TYPE=Release -S . -B build +cd build +make +cd .. + diff --git a/C_Demo/build.sh b/C_Demo/build.sh new file mode 100755 index 0000000..2b02bca --- /dev/null +++ b/C_Demo/build.sh @@ -0,0 +1,6 @@ + +cmake -DCMAKE_BUILD_TYPE=Release -S . -B build +cd build +make +cd .. + diff --git a/C_Demo/main.c b/C_Demo/main.c index 2429be2..32a1aba 100644 --- a/C_Demo/main.c +++ b/C_Demo/main.c @@ -90,6 +90,10 @@ int main(int argc, char* argv[]) { if (!checkKeyboard()) break; } + // Terminate task + sleepMs(1000); + cancel_thread(t2); + // Stop the XCP server XcpEthServerShutdown(); socketCleanup(); diff --git a/XCPlite/build.sh b/XCPlite/build.sh new file mode 100755 index 0000000..2b02bca --- /dev/null +++ b/XCPlite/build.sh @@ -0,0 +1,6 @@ + +cmake -DCMAKE_BUILD_TYPE=Release -S . -B build +cd build +make +cd .. + diff --git a/XCPlite/main_cfg.h b/XCPlite/main_cfg.h index 2916884..0d33ad2 100644 --- a/XCPlite/main_cfg.h +++ b/XCPlite/main_cfg.h @@ -38,10 +38,9 @@ #define ON 1 #define OFF 0 - -// Debug prints -#define OPTION_ENABLE_DBG_PRINTS ON -#define OPTION_DEBUG_LEVEL 2 +// Set clock resolution (for clock function in platform.c) +#define CLOCK_USE_APP_TIME_US +//#define CLOCK_USE_UTC_TIME_NS // A2L generation #define OPTION_ENABLE_A2L_GEN ON // Enable A2L generation @@ -50,16 +49,16 @@ #define OPTION_A2L_FILE_NAME "XCPlite.a2l" // A2L filename #endif - -// Set clock resolution (for clock function in platform.c) -#define CLOCK_USE_APP_TIME_US -//#define CLOCK_USE_UTC_TIME_NS - - // Ethernet Transport Layer #define OPTION_USE_TCP OFF #define OPTION_MTU 1500 // Ethernet MTU #define OPTION_SERVER_PORT 5555 // Default UDP port #define OPTION_SERVER_ADDR {127,0,0,1} // IP addr to bind, 0.0.0.0 = ANY -//#define OPTION_SERVER_ADDR {192,168,179,2} // IP addr to bind, 0.0.0.0 = ANY + +// Shutdown options for the XCP server +//#define OPTION_SERVER_FORCEFULL_TERMINATION ON + +// Debug prints +#define OPTION_ENABLE_DBG_PRINTS ON +#define OPTION_DEBUG_LEVEL 2 diff --git a/src/platform.h b/src/platform.h index b7f4e5d..3ff2579 100644 --- a/src/platform.h +++ b/src/platform.h @@ -72,6 +72,7 @@ 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 terminate_thread #elif defined(_LINUX) // Linux @@ -79,6 +80,7 @@ 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 detach_thread #endif diff --git a/src/xcpCanServer.c b/src/xcpCanServer.c index 1aae341..2f89c55 100644 --- a/src/xcpCanServer.c +++ b/src/xcpCanServer.c @@ -58,8 +58,8 @@ BOOL XcpCanServerInit(BOOL useCANFD, uint16_t croId, uint16_t dtoId, uint32_t bi if (gXcpServer.isInit) return FALSE; XCP_DBG_PRINT1("\nStart XCP server\n"); - gXcpServer.TransmitThreadRunning = 0; - gXcpServer.ReceiveThreadRunning = 0; + gXcpServer.TransmitThreadRunning = FALSE; + gXcpServer.ReceiveThreadRunning = FALSE; // Initialize XCP protocol layer XcpInit(); diff --git a/src/xcpEthServer.c b/src/xcpEthServer.c index f6d464e..e6d90b5 100644 --- a/src/xcpEthServer.c +++ b/src/xcpEthServer.c @@ -61,8 +61,8 @@ 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(); @@ -84,6 +84,20 @@ BOOL XcpEthServerInit(const uint8_t* addr, uint16_t port, BOOL useTCP, uint16_t BOOL XcpEthServerShutdown() { +#if OPTION_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); + XcpTlShutdown(); + gXcpServer.isInit = FALSE; + socketCleanup(); + XcpReset(); + } +#else + // Gracefull termination if (gXcpServer.isInit) { XcpDisconnect(); gXcpServer.ReceiveThreadRunning = FALSE; @@ -93,11 +107,14 @@ BOOL XcpEthServerShutdown() { join_thread(gXcpServer.TransmitThreadHandle); gXcpServer.isInit = FALSE; socketCleanup(); + XcpReset(); } +#endif return TRUE; } + // XCP server unicast command receive thread #if defined(_WIN) // Windows DWORD WINAPI XcpServerReceiveThread(LPVOID par) diff --git a/src/xcpLite.c b/src/xcpLite.c index caa9439..0f6234d 100644 --- a/src/xcpLite.c +++ b/src/xcpLite.c @@ -857,6 +857,11 @@ void XcpDisconnect( void ) gXcp.SessionStatus &= (uint16_t)(~SS_CONNECTED); } +// Reset XCP kernel states +void XcpReset() { + memset(&gXcp, 0, sizeof(gXcp)); +} + // Transmit command response static void XcpSendResponse() { diff --git a/src/xcpLite.h b/src/xcpLite.h index a5be6d3..bf3229e 100644 --- a/src/xcpLite.h +++ b/src/xcpLite.h @@ -73,6 +73,7 @@ typedef struct { extern void XcpInit(void); extern void XcpStart(void); extern void XcpDisconnect(); +extern void XcpReset(); /* Trigger a XCP data acquisition or stimulation event */ extern void XcpEvent(uint16_t event);