Skip to content

Commit e7adc13

Browse files
committed
Bugfix XcpEthServerShutdown incorrect thread termination
1 parent afded27 commit e7adc13

File tree

11 files changed

+63
-16
lines changed

11 files changed

+63
-16
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ Xcode_build/
5353
Environment
5454
CustomProperties.json
5555

56-
*.json
56+
/.vscode/settings.json
57+
/.vscode/tasks.json

CPP_Demo/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
3+
cd build
4+
make
5+
cd ..
6+

C_Demo/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
3+
cd build
4+
make
5+
cd ..
6+

C_Demo/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ int main(int argc, char* argv[]) {
9090
if (!checkKeyboard()) break;
9191
}
9292

93+
// Terminate task
94+
sleepMs(1000);
95+
cancel_thread(t2);
96+
9397
// Stop the XCP server
9498
XcpEthServerShutdown();
9599
socketCleanup();

XCPlite/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
3+
cd build
4+
make
5+
cd ..
6+

XCPlite/main_cfg.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
#define ON 1
3939
#define OFF 0
4040

41-
42-
// Debug prints
43-
#define OPTION_ENABLE_DBG_PRINTS ON
44-
#define OPTION_DEBUG_LEVEL 2
41+
// Set clock resolution (for clock function in platform.c)
42+
#define CLOCK_USE_APP_TIME_US
43+
//#define CLOCK_USE_UTC_TIME_NS
4544

4645
// A2L generation
4746
#define OPTION_ENABLE_A2L_GEN ON // Enable A2L generation
@@ -50,16 +49,16 @@
5049
#define OPTION_A2L_FILE_NAME "XCPlite.a2l" // A2L filename
5150
#endif
5251

53-
54-
// Set clock resolution (for clock function in platform.c)
55-
#define CLOCK_USE_APP_TIME_US
56-
//#define CLOCK_USE_UTC_TIME_NS
57-
58-
5952
// Ethernet Transport Layer
6053
#define OPTION_USE_TCP OFF
6154
#define OPTION_MTU 1500 // Ethernet MTU
6255
#define OPTION_SERVER_PORT 5555 // Default UDP port
6356
#define OPTION_SERVER_ADDR {127,0,0,1} // IP addr to bind, 0.0.0.0 = ANY
64-
//#define OPTION_SERVER_ADDR {192,168,179,2} // IP addr to bind, 0.0.0.0 = ANY
57+
58+
// Shutdown options for the XCP server
59+
//#define OPTION_SERVER_FORCEFULL_TERMINATION ON
60+
61+
// Debug prints
62+
#define OPTION_ENABLE_DBG_PRINTS ON
63+
#define OPTION_DEBUG_LEVEL 2
6564

src/platform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ typedef HANDLE tXcpThread;
7272
#define create_thread(h,t) *h = CreateThread(0, 0, t, NULL, 0, NULL)
7373
#define join_thread(h) WaitForSingleObject(h, INFINITE);
7474
#define terminate_thread(h) { TerminateThread(h,0); WaitForSingleObject(h,1000); CloseHandle(h); }
75+
#define cancel_thread terminate_thread
7576

7677
#elif defined(_LINUX) // Linux
7778

7879
typedef pthread_t tXcpThread;
7980
#define create_thread(h,t) pthread_create(h, NULL, t, NULL);
8081
#define join_thread(h) pthread_join(h,NULL);
8182
#define detach_thread(h) { pthread_detach(h); pthread_cancel(h); }
83+
#define cancel_thread detach_thread
8284

8385
#endif
8486

src/xcpCanServer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ BOOL XcpCanServerInit(BOOL useCANFD, uint16_t croId, uint16_t dtoId, uint32_t bi
5858
if (gXcpServer.isInit) return FALSE;
5959
XCP_DBG_PRINT1("\nStart XCP server\n");
6060

61-
gXcpServer.TransmitThreadRunning = 0;
62-
gXcpServer.ReceiveThreadRunning = 0;
61+
gXcpServer.TransmitThreadRunning = FALSE;
62+
gXcpServer.ReceiveThreadRunning = FALSE;
6363

6464
// Initialize XCP protocol layer
6565
XcpInit();

src/xcpEthServer.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ BOOL XcpEthServerInit(const uint8_t* addr, uint16_t port, BOOL useTCP, uint16_t
6161
// Init network sockets
6262
if (!socketStartup()) return FALSE;
6363

64-
gXcpServer.TransmitThreadRunning = 0;
65-
gXcpServer.ReceiveThreadRunning = 0;
64+
gXcpServer.TransmitThreadRunning = FALSE;
65+
gXcpServer.ReceiveThreadRunning = FALSE;
6666

6767
// Initialize XCP protocol layer if not already done
6868
XcpInit();
@@ -84,6 +84,20 @@ BOOL XcpEthServerInit(const uint8_t* addr, uint16_t port, BOOL useTCP, uint16_t
8484

8585
BOOL XcpEthServerShutdown() {
8686

87+
#if OPTION_SERVER_FORCEFULL_TERMINATION
88+
// Forcefull termination
89+
if (gXcpServer.isInit) {
90+
DBG_PRINT3("Disconnect, cancel threads and shutdown XCP!\n");
91+
XcpDisconnect();
92+
cancel_thread(gXcpServer.ReceiveThreadHandle);
93+
cancel_thread(gXcpServer.TransmitThreadHandle);
94+
XcpTlShutdown();
95+
gXcpServer.isInit = FALSE;
96+
socketCleanup();
97+
XcpReset();
98+
}
99+
#else
100+
// Gracefull termination
87101
if (gXcpServer.isInit) {
88102
XcpDisconnect();
89103
gXcpServer.ReceiveThreadRunning = FALSE;
@@ -93,11 +107,14 @@ BOOL XcpEthServerShutdown() {
93107
join_thread(gXcpServer.TransmitThreadHandle);
94108
gXcpServer.isInit = FALSE;
95109
socketCleanup();
110+
XcpReset();
96111
}
112+
#endif
97113
return TRUE;
98114
}
99115

100116

117+
101118
// XCP server unicast command receive thread
102119
#if defined(_WIN) // Windows
103120
DWORD WINAPI XcpServerReceiveThread(LPVOID par)

src/xcpLite.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,11 @@ void XcpDisconnect( void )
857857
gXcp.SessionStatus &= (uint16_t)(~SS_CONNECTED);
858858
}
859859

860+
// Reset XCP kernel states
861+
void XcpReset() {
862+
memset(&gXcp, 0, sizeof(gXcp));
863+
}
864+
860865
// Transmit command response
861866
static void XcpSendResponse() {
862867

src/xcpLite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef struct {
7373
extern void XcpInit(void);
7474
extern void XcpStart(void);
7575
extern void XcpDisconnect();
76+
extern void XcpReset();
7677

7778
/* Trigger a XCP data acquisition or stimulation event */
7879
extern void XcpEvent(uint16_t event);

0 commit comments

Comments
 (0)