Skip to content

Commit

Permalink
Windows build break fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerZ committed Sep 29, 2024
2 parents ab03169 + 6229a1d commit b4c738b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 39 deletions.
21 changes: 8 additions & 13 deletions xcplib/src/xcpEthTl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ static struct {
SOCKET MulticastSock;
#endif

#if defined(_WIN) // Windows
HANDLE queue_event;
uint64_t queue_event_time;
#endif

} gXcpTl;
#endif

Expand Down Expand Up @@ -154,10 +149,9 @@ void XcpEthTlSendMulticastCrm(const uint8_t* packet, uint16_t packet_size, const

//------------------------------------------------------------------------------

static int handleXcpCommand(int n, tXcpCtoMessage *p, uint8_t *srcAddr, uint16_t srcPort) {
static int handleXcpCommand(tXcpCtoMessage *p, uint8_t *srcAddr, uint16_t srcPort) {

int connected;
(void)n;

// gXcpTl.LastCrmCtr = p->ctr;
connected = XcpIsConnected();
Expand Down Expand Up @@ -193,8 +187,8 @@ static int handleXcpCommand(int n, tXcpCtoMessage *p, uint8_t *srcAddr, uint16_t
}
}
#endif // UDP

XcpCommand((const uint32_t*)&p->packet[0], p->dlc); // Handle command
if (p->dlc>XCPTL_MAX_CTO_SIZE) return 0;
XcpCommand((const uint32_t*)&p->packet[0], (uint8_t)p->dlc); // Handle command
}

/* Not connected yet */
Expand All @@ -209,7 +203,7 @@ static int handleXcpCommand(int n, tXcpCtoMessage *p, uint8_t *srcAddr, uint16_t
}
#endif // UDP
XcpTlResetTransmitQueue();
XcpCommand((const uint32_t*)&p->packet[0],p->dlc); // Handle CONNECT command
XcpCommand((const uint32_t*)&p->packet[0],(uint8_t)p->dlc); // Handle CONNECT command
}
else {
DBG_PRINT_WARNING("WARNING: handleXcpCommand: no valid CONNECT command\n");
Expand Down Expand Up @@ -268,7 +262,7 @@ BOOL XcpEthTlHandleCommands(uint32_t timeout_ms) {
n = socketRecv(gXcpTl.Sock, (uint8_t*)&msgBuf.packet, msgBuf.dlc, TRUE); // packet, recv blocking
if (n > 0) {
if (n == msgBuf.dlc) {
return handleXcpCommand(n, &msgBuf, NULL, 0);
return handleXcpCommand(&msgBuf, NULL, 0);
}
else {
socketShutdown(gXcpTl.Sock); // Let the receive thread terminate without error message
Expand Down Expand Up @@ -303,7 +297,7 @@ BOOL XcpEthTlHandleCommands(uint32_t timeout_ms) {
DBG_PRINT_ERROR("ERROR: corrupt message received!\n");
return FALSE; // Error
}
return handleXcpCommand(n, &msgBuf, srcAddr, srcPort);
return handleXcpCommand(&msgBuf, srcAddr, srcPort);
}
}
#endif // UDP
Expand All @@ -327,7 +321,8 @@ static int handleXcpMulticastCommand(int n, tXcpCtoMessage* p, uint8_t* dstAddr,

// Valid socket data received, at least transport layer header and 1 byte
if (n >= XCPTL_TRANSPORT_LAYER_HEADER_SIZE + 1 && p->dlc <= n- XCPTL_TRANSPORT_LAYER_HEADER_SIZE) {
XcpCommand((const uint32_t*)&p->packet[0],p->dlc); // Handle command
if (p->dlc >= XCPTL_MAX_CTO_SIZE) return 0; // Error
XcpCommand((const uint32_t*)&p->packet[0],(uint8_t)p->dlc); // Handle command
}
else {
printf("MULTICAST ignored\n");
Expand Down
13 changes: 6 additions & 7 deletions xcplib/src/xcpLite.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ static tXcpData gXcp = { 0 };
#define CRM_WORD(x) (gXcp.Crm.w[x])
#define CRM_DWORD(x) (gXcp.Crm.dw[x])

static uint8_t XcpAsyncCommand( BOOL async, const uint32_t* cmdBuf, uint16_t cmdLen );
static uint8_t XcpAsyncCommand( BOOL async, const uint32_t* cmdBuf, uint8_t cmdLen );


/****************************************************************************/
Expand Down Expand Up @@ -686,8 +686,7 @@ static uint8_t XcpSetDaqListMode(uint16_t daq, uint16_t event, uint8_t mode, uin
// Check all DAQ lists with same event have the same address extension
uint8_t ext = DaqListAddrExt(daq);
for (uint16_t daq0=0;daq0<gXcp.Daq.DaqCount;daq0++) {
uint16_t event0 = DaqListEventChannel(daq0);
if (event0==event) {
if (DaqListEventChannel(daq0)==event) {
uint8_t ext0 = DaqListAddrExt(daq0);
if (ext != ext0) return CRC_DAQ_CONFIG; // Error address extension not unique
}
Expand Down Expand Up @@ -1047,7 +1046,7 @@ static void XcpSendMulticastResponse( const tXcpCto* crm, uint8_t crmLen, uint8_
// Push XCP command which can not be executes in this context for later async execution
#ifdef XCP_ENABLE_DYN_ADDRESSING

static uint8_t XcpPushCommand( const tXcpCto* cmdBuf, uint16_t cmdLen) {
static uint8_t XcpPushCommand( const tXcpCto* cmdBuf, uint8_t cmdLen) {

#if defined(XCP_ENABLE_MULTITHREAD_CAL_EVENTS)
mutexLock(&gXcp.CmdPendingMutex);
Expand All @@ -1074,11 +1073,11 @@ static uint8_t XcpPushCommand( const tXcpCto* cmdBuf, uint16_t cmdLen) {
#endif // XCP_ENABLE_DYN_ADDRESSING

// Handles incoming XCP commands
uint8_t XcpCommand( const uint32_t* cmdBuf, uint16_t cmdLen ) {
uint8_t XcpCommand( const uint32_t* cmdBuf, uint8_t cmdLen ) {
return XcpAsyncCommand(FALSE, cmdBuf, cmdLen);
}
// Handles incoming or asyncronous XCP commands
static uint8_t XcpAsyncCommand( BOOL async, const uint32_t* cmdBuf, uint16_t cmdLen )
static uint8_t XcpAsyncCommand( BOOL async, const uint32_t* cmdBuf, uint8_t cmdLen )
{
#define CRO ((tXcpCto*)cmdBuf)
#define CRO_LEN (cmdLen)
Expand Down Expand Up @@ -1903,7 +1902,7 @@ void XcpPrint( const char *str ) {
crm.b[0] = PID_SERV; /* Event*/
crm.b[1] = 0x01; /* Eventcode SERV_TEXT */
uint8_t i;
uint16_t l = strlen(str);
uint16_t l = (uint16_t)strlen(str);
for (i = 0; i < l && i < XCPTL_MAX_CTO_SIZE-4; i++) crm.b[i+2] = str[i];
crm.b[i+2] = '\n';
crm.b[i+3] = 0;
Expand Down
2 changes: 1 addition & 1 deletion xcplib/src/xcpLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extern void XcpStart();
extern void XcpReset();

/* XCP command processor */
extern uint8_t XcpCommand( const uint32_t* pCommand, uint16_t len );
extern uint8_t XcpCommand( const uint32_t* pCommand, uint8_t len );

/* Disconnect, stop DAQ, flush queue */
extern void XcpDisconnect();
Expand Down
11 changes: 10 additions & 1 deletion xcplib/src/xcpTl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#include "xcpTlQueue.h"


#if defined(_WIN) // Windows
static struct {
HANDLE queue_event;
uint64_t queue_event_time;
} gXcpTl;
#endif


BOOL XcpTlInit() {

XcpTlInitTransmitQueue();
Expand Down Expand Up @@ -76,6 +84,7 @@ uint8_t XcpTlCommand( uint16_t msgLen, const uint8_t* msgBuf) {

/* Connected */
if (connected) {
if (p->dlc>XCPTL_MAX_CTO_SIZE) return CRC_CMD_SYNTAX;
return XcpCommand((const uint32_t*)&p->packet[0], p->dlc); // Handle command
}

Expand All @@ -84,7 +93,7 @@ uint8_t XcpTlCommand( uint16_t msgLen, const uint8_t* msgBuf) {
/* Check for CONNECT command ? */
if (p->dlc == 2 && p->packet[0] == CC_CONNECT) {
XcpTlResetTransmitQueue();
return XcpCommand((const uint32_t*)&p->packet[0],p->dlc); // Handle CONNECT command
return XcpCommand((const uint32_t*)&p->packet[0],(uint8_t)p->dlc); // Handle CONNECT command
}
else {
DBG_PRINTF_WARNING("WARNING: XcpTlCommand: no valid CONNECT command, dlc=%u, data=%02X\n", p->dlc, p->packet[0]);
Expand Down
53 changes: 36 additions & 17 deletions xcplib/src/xcpTlQueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,31 @@
#include "dbg_print.h"
#include "xcpLite.h"


#ifndef _WIN

#include <stdatomic.h>

// Use spinlock instead of mutex for producer lock
//#define USE_SPINLOCK
#define USE_SPINLOCK

#else

#ifdef _WIN32_
#error "Windows32 not implemented yet"
#else


#define atomic_uint_fast64_t uint64_t
#define atomic_store(a,b) (*a)=(b)
#define atomic_load(a) (*a)
#define atomic_load_explicit(a,b) (*a)
#define atomic_fetch_add(a,b) { mutexLock(&gXcpTlQueue.mutex); (*a)+=(b); mutexUnlock(&gXcpTlQueue.mutex);}

#endif

#endif


// Queue entry states
#define RESERVED 0 // Reserved by producer
Expand Down Expand Up @@ -63,8 +84,8 @@ void XcpTlInitTransmitQueue() {
gXcpTlQueue.tail_len = 0;
#ifdef USE_SPINLOCK
assert(atomic_is_lock_free(&lock)!=0);
#endif
assert(atomic_is_lock_free(&gXcpTlQueue.head)!=0);
#endif
}

void XcpTlResetTransmitQueue() {
Expand Down Expand Up @@ -167,10 +188,10 @@ void XcpTlFlushTransmitBuffer() {


// Get transmit queue level in bytes
static int32_t XcpTlGetTransmitQueueLevel() {
static uint32_t XcpTlGetTransmitQueueLevel() {
uint64_t head = atomic_load(&gXcpTlQueue.head);
uint64_t tail = atomic_load(&gXcpTlQueue.tail);
return head-tail;
return (uint32_t)(head-tail);
}

// Wait (sleep) until transmit queue is empty
Expand Down Expand Up @@ -204,31 +225,29 @@ const uint8_t * XcpTlTransmitQueuePeekMsg( uint16_t* msg_len ) {

uint64_t head = atomic_load(&gXcpTlQueue.head);
uint64_t tail = atomic_load(&gXcpTlQueue.tail);

if (head == tail) return NULL; // Queue is empty

uint32_t level = head-tail;
assert(level <= MPSC_QUEUE_SIZE); // Overrun not handled
DBG_PRINTF5("XcpTlTransmitQueuePeekMsg: level = %u, ctr=%u\n", level, gXcpTlQueue.ctr );
assert(head-tail<=MPSC_QUEUE_SIZE); // Overrun not handled
uint32_t level = (uint32_t)(head-tail);
DBG_PRINTF5("XcpTlTransmitQueuePeekMsg: level=%u, ctr=%u\n", level, gXcpTlQueue.ctr );

uint32_t tail_offset = tail % MPSC_QUEUE_SIZE;
tXcpDtoMessage *entry = (tXcpDtoMessage *)(gXcpTlQueue.buffer + tail_offset);
tXcpDtoMessage *entry1 = (tXcpDtoMessage *)(gXcpTlQueue.buffer + tail_offset);

if (gXcpTlQueue.tail_len==0) {

uint16_t ctr = entry->ctr; // entry ctr may be concurrently changed by producer, when committed
if (ctr==RESERVED) return NULL; // Not commited yet
assert(ctr==COMMITTED);
assert(entry->dlc<=XCPTL_MAX_DTO_SIZE); // Max DTO size
uint16_t ctr1 = entry1->ctr; // entry ctr may be concurrently changed by producer, when committed
if (ctr1==RESERVED) return NULL; // Not commited yet
assert(ctr1==COMMITTED);
assert(entry1->dlc<=XCPTL_MAX_DTO_SIZE); // Max DTO size

if (gXcpTlQueue.overruns) { // Add the number of overruns
DBG_PRINTF3("XcpTlTransmitQueuePeekMsg: overruns=%u\n", gXcpTlQueue.overruns);
gXcpTlQueue.ctr += gXcpTlQueue.overruns;
gXcpTlQueue.overruns = 0;
}

entry->ctr = gXcpTlQueue.ctr++; // Set the transport layer packet counter
uint16_t len = entry->dlc + XCPTL_TRANSPORT_LAYER_HEADER_SIZE;
entry1->ctr = gXcpTlQueue.ctr++; // Set the transport layer packet counter
uint16_t len = entry1->dlc + XCPTL_TRANSPORT_LAYER_HEADER_SIZE;

// Check for more packets to concatenate in a meassage segment
uint16_t len1 = len;
Expand Down Expand Up @@ -259,7 +278,7 @@ const uint8_t * XcpTlTransmitQueuePeekMsg( uint16_t* msg_len ) {

//DBG_PRINTF5("XcpTlTransmitQueuePeekMsg: msg_len = %u\n", gXcpTlQueue.tail_len );
*msg_len = gXcpTlQueue.tail_len;
return (uint8_t*)entry;
return (uint8_t*)entry1;
}


Expand Down

0 comments on commit b4c738b

Please sign in to comment.