Skip to content

Commit

Permalink
Bugfix XcpEthServerShutdown incorrect thread termination
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerZ committed Sep 12, 2024
1 parent afded27 commit e7adc13
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ Xcode_build/
Environment
CustomProperties.json

*.json
/.vscode/settings.json
/.vscode/tasks.json
6 changes: 6 additions & 0 deletions CPP_Demo/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
cd build
make
cd ..

6 changes: 6 additions & 0 deletions C_Demo/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
cd build
make
cd ..

4 changes: 4 additions & 0 deletions C_Demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions XCPlite/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
cd build
make
cd ..

21 changes: 10 additions & 11 deletions XCPlite/main_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

2 changes: 2 additions & 0 deletions src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ 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

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

Expand Down
4 changes: 2 additions & 2 deletions src/xcpCanServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 19 additions & 2 deletions src/xcpEthServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/xcpLite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
1 change: 1 addition & 0 deletions src/xcpLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e7adc13

Please sign in to comment.