11.5.1. test_crc.cpp#

 1#include <uhd/utils/safe_main.hpp>
 2#include <boost/crc.hpp>
 3#include <cstring>
 4 
 5 
 6int UHD_SAFE_MAIN(int argc, char *argv[]){
 7    
 8    // Instantiate a crc-32 object
 9    boost::crc_32_type crc32;
10    // Info byte array
11    unsigned char info[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
12	
13    // CRC encode
14    crc32.process_bytes(info, 16);
15
16    // Print codeword
17    std::cout << "Information:\t"; 
18    for (int i=0; i<16; i++) {
19        std::cout << std::uppercase << std::hex << (unsigned short) info[i];
20    } 
21    std::cout << "\nCRC:\t\t" <<  std::uppercase << std::hex << crc32.checksum()  << std::endl;
22    // Append info and CRC to form codeword
23    unsigned char cw[20];
24    memcpy(cw, info, 16);
25    unsigned char crc_pt[4];
26    boost::uint32_t crc_val = crc32.checksum();
27    memcpy(cw+16, &crc_val, 4);
28    std::cout << "Codeword:\t";
29    for (int i=0; i<20; i++) {
30        std::cout << std::uppercase << std::hex << (unsigned short) cw[i];
31    }
32    std::cout << std::endl;
33
34    // CRC decode: No error
35    std::cout << "\nNo TX error case:\n";
36    crc32.reset(); // Reset for new CRC calculation
37    crc32.process_bytes(cw, 16); // Calculate CRC checksum
38
39    // Compare calculated checksum with RX checksum
40    boost::uint32_t rx_crc;
41    memcpy(&rx_crc, cw+16, 4);
42    if (rx_crc == crc32.checksum()) {
43        std::cout << "CRC checked => No error\n";
44    } else {
45        std::cout << "Error detected!\n";
46    }
47    std::cout << "RX CRC:\t\t" << std::uppercase << std::hex << rx_crc << std::endl;
48    std::cout << "Calculated CRC:\t" << std::uppercase << std::hex << crc32.checksum() << std::endl;
49
50    // CRC decode: Double error
51    std::cout << "\nDouble TX error case:\n";
52    crc32.reset();
53    // Add double error
54    cw[2] ^= 0x08;
55    cw[17] ^= 0x10;
56    crc32.process_bytes(cw, 16);
57
58    // Compare calculated checksum with RX checksum
59    memcpy(&rx_crc, cw+16, 4);
60    if (rx_crc == crc32.checksum()) {
61        std::cout << "CRC checked => No error\n";
62    } else {
63        std::cout << "Error detected!\n";
64    }
65    std::cout << "RX CRC:\t\t" << std::uppercase << std::hex << rx_crc << std::endl;
66    std::cout << "Calculated CRC:\t" << std::uppercase << std::hex << crc32.
67    checksum() << std::endl; 
68
69    return EXIT_SUCCESS;
70}
  • In this decoding process here, we enter only the portion o fthe RX codeward byte array corresponding to the information part in the codeword into the class method process_bytes(). This corresponds to the first decoding method discussed before. We have to compare the calculated checksum to the RX one to see whether there is any error.

Experiment

Build and run the example above to see how Boost.CRC does CRC encoding and decoding.