I would like to implement a Cyclic Redundancy Check (CRC) in C++. Specifically, I want to obtain a boolean return value for CRC validation for every packet processed. Could you please provide a sample code or guidance on how to achieve this?
Here is the current code I am working on:
while (true)
{
ouster::LidarScan profile_scan(sensor_info_.format.columns_per_frame, sensor_info_.format.pixels_per_column, sensor_info_.format.udp_profile_lidar);
while (true)
{
ouster::sensor::client_state client_state = ouster::sensor::poll_client(*client_ptr_);
if (client_state & ouster::sensor::LIDAR_DATA)
{
auto packet_buffer = std::make_unique<uint8_t[]>(udp_buffer_size_);
if (ouster::sensor::read_lidar_packet(*client_ptr_, packet_buffer.get(), *packet_format_ptr_))
{
// Is it possible to perform CRC validation here?
if ((*batch_to_scan_converter_ptr_)(packet_buffer.get(), profile_scan))
{
if (profile_scan.complete(sensor_info_.format.column_window))
break;
}
}
}
}
ouster::LidarScan::Points pc_ouster = ouster::cartesian(profile_scan, xyz_lut_);
}
We don’t have an API that let’s you know that if there were CRC errors that took place, the CRC check is performed by the data link layer if there is a mismatch then the packet will be discarded and won’t be handed to your application. Since UDP isn’t reliable the packet won’t be sent again by the sender, so in this case you would completely miss this packet the LidarScan/Frame would be missing some measurements (16 consecutive measurements for every missing packet).
What you could try is probably check if the system provides with an API that tells you whether there were some failed CRC checks. For example I know that the ethtool display some stats about failed crc checks (try ethtool -s <interface>) this would give you an indication if there were errors present since the link is up.
@Samahu
Thank you for your reply. Your response helped me understand. While the CRC error cannot be detected with the API, what can be detected with if (client_state & ouster::sensor::CLIENT_ERROR), if (client_state & ouster::sensor::TIMEOUT), and if (client_state & ouster::sensor::EXIT)?
EXIT means stop signal was requested for client
TIMEOUT no packet data received within the timeout period (this doesn’t mean there is an error, just no data coming, which can happen due to a connection loss or sensor powered down, …)
CLIENT_ERROR this indicates there is a problem when accessing the socket, like when the socket was forcefully closed while the program is still running
There is now an API for both reading and computing the CRC64 from LidarPackets in the SDK. Generally you shouldn’t need to worry about this since the low level network packets have a CRC that is verified by the network stack, but since we provide it in the packets we decided to offer an API for it.