| | 518 | // Encode subheader for PACKET_RS_512 |
| | 519 | bool PacketEncoder::EncodeSubheader_PACKET_RS_512( |
| | 520 | char* output, |
| | 521 | unsigned int output_length, |
| | 522 | unsigned int &num_written) |
| | 523 | { |
| | 524 | unsigned int nr; |
| | 525 | unsigned int nw; |
| | 526 | |
| | 527 | // 1. push 16 8-bit symbols into buf1 |
| | 528 | // variable num bits |
| | 529 | // ---- ---- |
| | 530 | // packet_id 32 |
| | 531 | // src_id 16 |
| | 532 | // dst_id 16 |
| | 533 | // app_id 16 |
| | 534 | // port_id 16 |
| | 535 | // expansion 64 |
| | 536 | buf1[0] = (packet_id >> 0) & 0x0F; |
| | 537 | buf1[1] = (packet_id >> 4) & 0x0F; |
| | 538 | buf1[2] = (packet_id >> 8) & 0x0F; |
| | 539 | buf1[3] = (packet_id >> 12) & 0x0F; |
| | 540 | |
| | 541 | buf1[4] = (src_id >> 0 ) & 0x0F; |
| | 542 | buf1[5] = (src_id >> 4 ) & 0x0F; |
| | 543 | |
| | 544 | buf1[6] = (dst_id >> 0 ) & 0x0F; |
| | 545 | buf1[7] = (dst_id >> 4 ) & 0x0F; |
| | 546 | |
| | 547 | buf1[8] = (app_id >> 0 ) & 0x0F; |
| | 548 | buf1[9] = (app_id >> 4 ) & 0x0F; |
| | 549 | |
| | 550 | buf1[10] = (port_id >> 0 ) & 0x0F; |
| | 551 | buf1[11] = (port_id >> 4 ) & 0x0F; |
| | 552 | |
| | 553 | buf1[12] = 7; |
| | 554 | buf1[13] = 7; |
| | 555 | buf1[14] = 7; |
| | 556 | buf1[15] = 7; |
| | 557 | |
| | 558 | // 2. compute CRC and append to end of message |
| | 559 | FastCrc crc(0x04c11db7); |
| | 560 | for (unsigned int i=0; i<16; i++) |
| | 561 | crc.PutByte(buf1[i]); |
| | 562 | UINT32 crc_code = crc.Done(); |
| | 563 | |
| | 564 | buf1[16] = (crc_code >> 0 ) & 0x0F; |
| | 565 | buf1[17] = (crc_code >> 4 ) & 0x0F; |
| | 566 | buf1[18] = (crc_code >> 8 ) & 0x0F; |
| | 567 | buf1[19] = (crc_code >> 12 ) & 0x0F; |
| | 568 | |
| | 569 | // 3. repack 20 8-bit symbols (+2 bits) into 27 6-bit symbols |
| | 570 | SigProc::repack_bytes(buf1, 8, 20, buf2, 2, buf_len, &nw); |
| | 571 | printf("repack 8->2: nw=%d\n", nw); |
| | 572 | buf2[640] = 0; |
| | 573 | SigProc::repack_bytes(buf2, 2, 641, buf1, 6, buf_len, &nw); |
| | 574 | printf("repack 2->6: nw=%d\n", nw); |
| | 575 | |
| | 576 | // 4. use 6-bit RS (63,55) punctured to (35,27) |
| | 577 | // 27 symbols (uncoded) |
| | 578 | // -> 35 symbols (coded) |
| | 579 | int symsize = 6; |
| | 580 | int npad = 28; |
| | 581 | int nn = (1<<symsize)-1-npad; // 63-28 = 35 |
| | 582 | int nroots = 8; |
| | 583 | int kk = nn-nroots; // 35-8 = 27 |
| | 584 | void *rs; |
| | 585 | rs = init_rs_char(symsize,0x43,1,1,nroots,0); |
| | 586 | encode_rs_char(rs, (unsigned char*) buf1, (unsigned char*) &buf1[kk]); |
| | 587 | |
| | 588 | // 5. unpack to output buffer |
| | 589 | SigProc::repack_bytes(buf1, 6, 27, output, 1, output_length, &num_written); |
| | 590 | num_written += 3; // pad to 213 |
| | 591 | printf("PACKET_RS_512 subheader: num_written: %d (should be 213)\n", num_written); |
| | 592 | return true; |
| | 593 | } |
| | 744 | return true; |
| | 745 | } |
| | 746 | |
| | 747 | bool PacketDecoder::DecodeSubheader_PACKET_RS_512( |
| | 748 | char * input, |
| | 749 | unsigned int input_length) |
| | 750 | { |
| | 751 | unsigned int nr; |
| | 752 | unsigned int nw; |
| | 753 | |
| | 754 | // 1. pack 210 bits into 35 6-bit symbols to buf2 |
| | 755 | SigProc::repack_bytes(input, 1, 210, buf2, 6, buf_len, &nw); |
| | 756 | |
| | 757 | // 2. use RS 6-bit (63,55) code, punctured to (35,27) |
| | 758 | int symsize = 6; |
| | 759 | int npad = 28; |
| | 760 | int nn = (1<<symsize)-1-npad; // 63-28 = 35 |
| | 761 | int nroots = 8; |
| | 762 | int kk = nn-nroots; // 35-8 = 27 |
| | 763 | void *rs; |
| | 764 | rs = init_rs_char(symsize,0x43,1,1,nroots,0); |
| | 765 | int nerrors(0); |
| | 766 | nerrors = decode_rs_char(rs, (unsigned char*) buf2, 0, 0); |
| | 767 | |
| | 768 | // 3. repack 27 6-bit symbols (-2 bits) to 20 8-bit symbols |
| | 769 | SigProc::repack_bytes(buf2, 6, 27, buf1, 2, buf_len, &nw); |
| | 770 | printf("decode, unpack: nw = %d (should be 81)\n", nw); |
| | 771 | SigProc::repack_bytes(buf1, 2, 80, buf2, 8, buf_len, &nw); |
| | 772 | printf("decode, unpack: nw = %d (should be 20)\n", nw); |
| | 773 | |
| | 774 | // 4. decode and compare CRC |
| | 775 | FastCrc crc(0x04c11db7); |
| | 776 | for (unsigned int i=0; i<16; i++) |
| | 777 | crc.PutByte( buf2[i] ); |
| | 778 | UINT32 crc_code = crc.Done(); |
| | 779 | unsigned long crc_rx(0); |
| | 780 | for (unsigned int i=1; i<4; i++) { |
| | 781 | crc_rx <<= 4; |
| | 782 | crc_rx |= buf2[20-i-1]; |
| | 783 | } |
| | 784 | //printf("crc_rx = %d, crc_code = %d\n", crc_rx, crc_code); |
| | 785 | std::cout << "crc_rc: 0x" << std::hex << crc_rx << ", crc_code = 0x" << crc_code << std::endl; |
| | 786 | |
| | 787 | // 5. decode 16 8-bit symbols... |
| | 788 | |