From a277077a9a23bd11d0ac129d3103409f4a08d68f Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Sat, 27 Jul 2024 03:23:58 -0400 Subject: [PATCH 1/2] remove periodic packet function from sim CANAPI --- hal/src/main/native/sim/CANAPI.cpp | 65 +----------------------------- 1 file changed, 2 insertions(+), 63 deletions(-) diff --git a/hal/src/main/native/sim/CANAPI.cpp b/hal/src/main/native/sim/CANAPI.cpp index 0eae18aaa75..81858434a7d 100644 --- a/hal/src/main/native/sim/CANAPI.cpp +++ b/hal/src/main/native/sim/CANAPI.cpp @@ -63,7 +63,7 @@ static int32_t CreateCANId(CANStorage* storage, int32_t apiId) { createdId |= (storage->deviceId & 0x3F); return createdId; } - +extern "C" { uint32_t HAL_GetCANPacketBaseTime() { int status = 0; auto basetime = HAL_GetFPGATime(&status); @@ -289,65 +289,4 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, } } } - -void HAL_ReadCANPeriodicPacket(HAL_CANHandle handle, int32_t apiId, - uint8_t* data, int32_t* length, - uint64_t* receivedTimestamp, int32_t timeoutMs, - int32_t periodMs, int32_t* status) { - auto can = canHandles->Get(handle); - if (!can) { - *status = HAL_HANDLE_ERROR; - return; - } - - uint32_t messageId = CreateCANId(can.get(), apiId); - - { - std::scoped_lock lock(can->mapMutex); - auto i = can->receives.find(messageId); - if (i != can->receives.end()) { - // Found, check if new enough - uint32_t now = HAL_GetCANPacketBaseTime(); - if (now - i->second.lastTimeStamp < static_cast(periodMs)) { - *status = 0; - // Read the data from the stored message into the output - std::memcpy(data, i->second.data, i->second.length); - *length = i->second.length; - *receivedTimestamp = i->second.lastTimeStamp; - return; - } - } - } - - uint8_t dataSize = 0; - uint32_t ts = 0; - HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); - - std::scoped_lock lock(can->mapMutex); - if (*status == 0) { - // fresh update - auto& msg = can->receives[messageId]; - msg.length = dataSize; - *length = dataSize; - msg.lastTimeStamp = ts; - *receivedTimestamp = ts; - // The NetComm call placed in data, copy into the msg - std::memcpy(msg.data, data, dataSize); - } else { - auto i = can->receives.find(messageId); - if (i != can->receives.end()) { - // Found, check if new enough - uint32_t now = HAL_GetCANPacketBaseTime(); - if (now - i->second.lastTimeStamp > static_cast(timeoutMs)) { - // Timeout, return bad status - *status = HAL_CAN_TIMEOUT; - return; - } - // Read the data from the stored message into the output - std::memcpy(data, i->second.data, i->second.length); - *length = i->second.length; - *receivedTimestamp = i->second.lastTimeStamp; - *status = 0; - } - } -} +} // extern "C" \ No newline at end of file From 07994db2b18748493f65db9c250d170aed269049 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Sat, 27 Jul 2024 03:54:53 -0400 Subject: [PATCH 2/2] format --- hal/src/main/native/sim/CANAPI.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hal/src/main/native/sim/CANAPI.cpp b/hal/src/main/native/sim/CANAPI.cpp index 81858434a7d..5a4324ae590 100644 --- a/hal/src/main/native/sim/CANAPI.cpp +++ b/hal/src/main/native/sim/CANAPI.cpp @@ -64,7 +64,7 @@ static int32_t CreateCANId(CANStorage* storage, int32_t apiId) { return createdId; } extern "C" { -uint32_t HAL_GetCANPacketBaseTime() { +uint32_t HAL_GetCANPacketBaseTime(void) { int status = 0; auto basetime = HAL_GetFPGATime(&status); // us to ms @@ -289,4 +289,4 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, } } } -} // extern "C" \ No newline at end of file +} // extern "C"