fix(socketcan): restore bounded receive and adapt regression tests

Keep the poll-based receive implementation and explicit timeout overload. Bound default receive to 100 ms for idle-bus shutdown, clear stale output frames, and adapt the test peer to the current API. Validate complete Release all-target build, seven SocketCAN cases, and eleven offline regression executables.
This commit is contained in:
xtkuang 2026-11-19 17:29:28 +08:00
parent 173a5cbf7d
commit 70bd28fe4e
3 changed files with 44 additions and 3 deletions

View File

@ -200,7 +200,8 @@ namespace cmvr {
ErrorCode SocketCanClientRaw::receive(std::vector<CanFrame> *const frames,
int32_t *const frame_num) {
return receive(frames, frame_num, std::chrono::milliseconds{-1});
// Periodically return to CanReceiver stop checks, even on an idle bus.
return receive(frames, frame_num, std::chrono::milliseconds{100});
}
ErrorCode SocketCanClientRaw::receive(std::vector<CanFrame> *const frames,
@ -214,6 +215,7 @@ namespace cmvr {
if (frames == nullptr) {
return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
}
frames->clear();
if (!is_started_) {
CMVR_LOG(ERROR) << "Nvidia can client is not init! Please init first!";
return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;

View File

@ -54,7 +54,7 @@ namespace cmvr {
int32_t *const frame_num) override;
/**
* @brief Receive messages
* @brief Receive messages with a bounded 100 ms wait so receivers can stop
* @param frames The messages to receive.
* @param frame_num The amount of messages to receive.
* @return The status of the receiving action
@ -78,6 +78,7 @@ namespace cmvr {
*/
std::string getErrorString(const int32_t status) override;
private:
friend class SocketCanClientRawTestPeer;
int dev_handler_ = -1;
cmvr::msgs::CANCardParameter::CANChannelId port_;
cmvr::msgs::CANCardParameter::CANInterface interface_;

View File

@ -13,7 +13,6 @@ namespace cmvr::device {
class SocketCanClientRawTestPeer {
public:
static bool attach(SocketCanClientRaw& client, int fd) {
if (!client.configureReceiveTimeout(fd)) return false;
client.dev_handler_ = fd;
client.is_started_ = true;
return true;
@ -92,4 +91,43 @@ TEST_F(SocketCanReceiveTest, ShortFrameIsRejectedAndStopIsIdempotent) {
EXPECT_EQ(count, 0);
}
TEST_F(SocketCanReceiveTest, ExplicitTimeoutUsesOneCallDeadlineAndClearsOldFrames) {
std::vector<CanFrame> frames(2);
int32_t count = 1;
const auto started = std::chrono::steady_clock::now();
EXPECT_EQ(client.receive(&frames, &count, std::chrono::milliseconds(20)), msgs::ErrorCode::OK);
EXPECT_EQ(count, 0);
EXPECT_TRUE(frames.empty());
EXPECT_LT(std::chrono::steady_clock::now() - started, std::chrono::milliseconds(500));
}
TEST_F(SocketCanReceiveTest, RejectsCanFdAndInvalidClassicDlc) {
canfd_frame fd{};
fd.can_id = 0x141;
fd.len = 12;
ASSERT_EQ(::write(peer, &fd, sizeof(fd)), sizeof(fd));
std::vector<CanFrame> frames;
int32_t count = 1;
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
EXPECT_EQ(count, 0);
can_frame classic{};
classic.can_dlc = 9;
ASSERT_EQ(::write(peer, &classic, sizeof(classic)), sizeof(classic));
count = 1;
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
EXPECT_EQ(count, 0);
}
TEST_F(SocketCanReceiveTest, RejectsNullAndInvalidCapacity) {
std::vector<CanFrame> frames;
int32_t count = 1;
EXPECT_EQ(client.receive(nullptr, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
EXPECT_EQ(count, 0);
EXPECT_EQ(client.receive(&frames, nullptr), msgs::ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM);
count = -1;
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM);
EXPECT_EQ(count, 0);
}
} // namespace cmvr::device