diff --git a/Host/Source/LibOpenBLT/port/linux/critutil.c b/Host/Source/LibOpenBLT/port/linux/critutil.c index 1f446b9d..705432ab 100644 --- a/Host/Source/LibOpenBLT/port/linux/critutil.c +++ b/Host/Source/LibOpenBLT/port/linux/critutil.c @@ -40,10 +40,10 @@ * Local data declarations ****************************************************************************************/ /** \brief Flag to determine if the critical section object was already initialized. */ -static volatile bool criticalSectionInitialized = false; +static bool criticalSectionInitialized = false; /** \brief Crital section nesting counter. ***/ -static volatile uint32_t criticalSectionNesting; +static uint32_t criticalSectionNesting; /** \brief Critical section object. */ static pthread_mutex_t mtxCritSect; diff --git a/Host/Source/LibOpenBLT/port/windows/critutil.c b/Host/Source/LibOpenBLT/port/windows/critutil.c index 7b57ba12..9e0130c2 100644 --- a/Host/Source/LibOpenBLT/port/windows/critutil.c +++ b/Host/Source/LibOpenBLT/port/windows/critutil.c @@ -40,13 +40,13 @@ * Local data declarations ****************************************************************************************/ /** \brief Flag to determine if the critical section object was already initialized. */ -static volatile bool criticalSectionInitialized = false; +static bool criticalSectionInitialized = false; /** \brief Crital section nesting counter. ***/ -static volatile uint32_t criticalSectionNesting; +static uint32_t criticalSectionNesting; /** \brief Critical section object. */ -static volatile CRITICAL_SECTION criticalSection; +static CRITICAL_SECTION criticalSection; /************************************************************************************//** diff --git a/Host/Source/LibOpenBLT/xcptpcan.c b/Host/Source/LibOpenBLT/xcptpcan.c index 0a042cd2..69947371 100644 --- a/Host/Source/LibOpenBLT/xcptpcan.c +++ b/Host/Source/LibOpenBLT/xcptpcan.c @@ -87,6 +87,18 @@ static const tCanEvents canEvents = /** \brief The settings to use in this transport layer. */ static tXcpTpCanSettings tpCanSettings; +/** \brief Flag to indicate that a response packet was received via CAN. Made volatile + * because it is shared with an event callback function that could be called from + * a different thread. + */ +static volatile bool tpCanResponseMessageReceived; + +/** \brief Buffer for storing the CAN message with response packet data. Made volatile + * because it is shared with an event callback function that could be called from + * a different thread. + */ +static volatile tCanMsg tpCanResponseMessage; + /***********************************************************************************//** ** \brief Obtains a pointer to the transport layer structure, so that it can be @@ -306,6 +318,14 @@ static bool XcpTpCanSendPacket(tXcpTransportPacket const * txPacket, { canMsg.data[idx] = txPacket->data[idx]; } + /* Enter critical section. */ + UtilCriticalSectionEnter(); + /* Reset packet received flag before transmitting the packet, to be able to detect + * its response packet. + */ + tpCanResponseMessageReceived = false; + /* Exit critical section. */ + UtilCriticalSectionExit(); /* Submit the packet for transmission on the CAN bus. */ if (!CanTransmit(&canMsg)) { @@ -321,9 +341,36 @@ static bool XcpTpCanSendPacket(tXcpTransportPacket const * txPacket, */ while (UtilTimeGetSystemTimeMs() < responseTimeoutTime) { - /* ##Vg TODO Implement packet reception. */ - break; + /* Enter critical section. */ + UtilCriticalSectionEnter(); + /* Response received? */ + if (tpCanResponseMessageReceived) + { + /* Copy the response packet. */ + rxPacket->len = tpCanResponseMessage.dlc; + for (uint8_t idx = 0; idx < rxPacket->len; idx++) + { + rxPacket->data[idx] = tpCanResponseMessage.data[idx]; + } + /* Exit critical section. */ + UtilCriticalSectionExit(); + /* Response packet receive so no need to continue loop. */ + break; + } + /* Exit critical section. */ + UtilCriticalSectionExit(); + /* Wait a little bit to not starve the CPU. */ + UtilTimeDelayMs(1); } + /* Enter critical section. */ + UtilCriticalSectionEnter(); + /* Check if a timeout occurred and no response was received. */ + if (!tpCanResponseMessageReceived) + { + result = false; + } + /* Exit critical section. */ + UtilCriticalSectionExit(); } } } @@ -364,7 +411,19 @@ static void XcpTpCanEventMessageReceived(tCanMsg const * msg) /* Check if the identifier matches the one for XCP on CAN. */ if (msg->id == tpCanRxId) { - /* ##Vg TODO process CAN message reception. */ + /* Enter critical section. */ + UtilCriticalSectionEnter(); + /* Copy to the packet response message buffer. */ + tpCanResponseMessage.id = msg->id; + tpCanResponseMessage.dlc = msg->dlc; + for (uint8_t idx = 0; idx < msg->dlc; idx++) + { + tpCanResponseMessage.data[idx] = msg->data[idx]; + } + /* Set packet received flag. */ + tpCanResponseMessageReceived = true; + /* Exit critical section. */ + UtilCriticalSectionExit(); } } /*** end of XcpTpCanEventMessageReceived ***/ diff --git a/Host/libopenblt.dll b/Host/libopenblt.dll index 7f9b29fb..a8e61e10 100644 Binary files a/Host/libopenblt.dll and b/Host/libopenblt.dll differ