diff --git a/hal/src/main/native/athena/CANAPI.cpp b/hal/src/main/native/athena/CANAPI.cpp index 40332ef169..c69bc74826 100644 --- a/hal/src/main/native/athena/CANAPI.cpp +++ b/hal/src/main/native/athena/CANAPI.cpp @@ -7,6 +7,7 @@ #include #include +#include #include "HALInitializer.h" #include "hal/CAN.h" @@ -26,8 +27,9 @@ struct CANStorage { HAL_CANManufacturer manufacturer; HAL_CANDeviceType deviceType; uint8_t deviceId; - wpi::mutex mapMutex; + wpi::mutex periodicSendsMutex; wpi::SmallDenseMap periodicSends; + wpi::mutex receivesMutex; wpi::SmallDenseMap receives; }; } // namespace @@ -89,7 +91,7 @@ void HAL_CleanCAN(HAL_CANHandle handle) { return; } - std::scoped_lock lock(data->mapMutex); + std::scoped_lock lock(data->periodicSendsMutex); for (auto&& i : data->periodicSends) { int32_t s = 0; @@ -108,12 +110,8 @@ void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t* data, } auto id = CreateCANId(can.get(), apiId); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = -1; } @@ -127,12 +125,8 @@ void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t* data, } auto id = CreateCANId(can.get(), apiId); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, data, length, repeatMs, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = repeatMs; } @@ -148,12 +142,8 @@ void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId, uint8_t data[8]; std::memset(data, 0, sizeof(data)); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = -1; } @@ -166,13 +156,9 @@ void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId, } auto id = CreateCANId(can.get(), apiId); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, nullptr, 0, HAL_CAN_SEND_PERIOD_STOP_REPEATING, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = -1; } @@ -191,7 +177,7 @@ void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t* data, HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); if (*status == 0) { - std::scoped_lock lock(can->mapMutex); + std::scoped_lock lock(can->receivesMutex); auto& msg = can->receives[messageId]; msg.length = dataSize; msg.lastTimeStamp = ts; @@ -216,7 +202,7 @@ void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t* data, uint32_t ts = 0; HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); - std::scoped_lock lock(can->mapMutex); + std::scoped_lock lock(can->receivesMutex); if (*status == 0) { // fresh update auto& msg = can->receives[messageId]; @@ -253,7 +239,7 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, uint32_t ts = 0; HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); - std::scoped_lock lock(can->mapMutex); + std::scoped_lock lock(can->receivesMutex); if (*status == 0) { // fresh update auto& msg = can->receives[messageId]; diff --git a/hal/src/main/native/sim/CANAPI.cpp b/hal/src/main/native/sim/CANAPI.cpp index 5a4324ae59..4689d1f4e4 100644 --- a/hal/src/main/native/sim/CANAPI.cpp +++ b/hal/src/main/native/sim/CANAPI.cpp @@ -26,8 +26,9 @@ struct CANStorage { HAL_CANManufacturer manufacturer; HAL_CANDeviceType deviceType; uint8_t deviceId; - wpi::mutex mapMutex; + wpi::mutex periodicSendsMutex; wpi::SmallDenseMap periodicSends; + wpi::mutex receivesMutex; wpi::SmallDenseMap receives; }; } // namespace @@ -97,7 +98,7 @@ void HAL_CleanCAN(HAL_CANHandle handle) { return; } - std::scoped_lock lock(data->mapMutex); + std::scoped_lock lock(data->periodicSendsMutex); for (auto&& i : data->periodicSends) { int32_t s = 0; @@ -116,12 +117,8 @@ void HAL_WriteCANPacket(HAL_CANHandle handle, const uint8_t* data, } auto id = CreateCANId(can.get(), apiId); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = -1; } @@ -135,12 +132,8 @@ void HAL_WriteCANPacketRepeating(HAL_CANHandle handle, const uint8_t* data, } auto id = CreateCANId(can.get(), apiId); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, data, length, repeatMs, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = repeatMs; } @@ -156,12 +149,8 @@ void HAL_WriteCANRTRFrame(HAL_CANHandle handle, int32_t length, int32_t apiId, uint8_t data[8]; std::memset(data, 0, sizeof(data)); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, data, length, HAL_CAN_SEND_PERIOD_NO_REPEAT, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = -1; } @@ -174,13 +163,9 @@ void HAL_StopCANPacketRepeating(HAL_CANHandle handle, int32_t apiId, } auto id = CreateCANId(can.get(), apiId); + std::scoped_lock lock(can->periodicSendsMutex); HAL_CAN_SendMessage(id, nullptr, 0, HAL_CAN_SEND_PERIOD_STOP_REPEATING, status); - - if (*status != 0) { - return; - } - std::scoped_lock lock(can->mapMutex); can->periodicSends[apiId] = -1; } @@ -199,7 +184,7 @@ void HAL_ReadCANPacketNew(HAL_CANHandle handle, int32_t apiId, uint8_t* data, HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); if (*status == 0) { - std::scoped_lock lock(can->mapMutex); + std::scoped_lock lock(can->receivesMutex); auto& msg = can->receives[messageId]; msg.length = dataSize; msg.lastTimeStamp = ts; @@ -224,7 +209,7 @@ void HAL_ReadCANPacketLatest(HAL_CANHandle handle, int32_t apiId, uint8_t* data, uint32_t ts = 0; HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); - std::scoped_lock lock(can->mapMutex); + std::scoped_lock lock(can->receivesMutex); if (*status == 0) { // fresh update auto& msg = can->receives[messageId]; @@ -261,7 +246,7 @@ void HAL_ReadCANPacketTimeout(HAL_CANHandle handle, int32_t apiId, uint32_t ts = 0; HAL_CAN_ReceiveMessage(&messageId, 0x1FFFFFFF, data, &dataSize, &ts, status); - std::scoped_lock lock(can->mapMutex); + std::scoped_lock lock(can->receivesMutex); if (*status == 0) { // fresh update auto& msg = can->receives[messageId];