int main(void) { int i, len, n; const int num_tests = 500000; printf("Encode test #1:\n"); len = sizeof(enc_test_1); n = encode_4b6b(enc_test_1, result, len); print_bytes(result, n); printf("Encode test #2:\n"); len = sizeof(enc_test_2); n = encode_4b6b(enc_test_2, result, len); print_bytes(result, n); printf("Decode test #1:\n"); len = sizeof(dec_test_1); n = decode_4b6b(dec_test_1, result, len); if (n >= 0) { print_bytes(result, n); } else { printf("Decoding error!\n"); return 1; } printf("Decode test #2:\n"); len = sizeof(dec_test_2); n = decode_4b6b(dec_test_2, result, len); if (n >= 0) { print_bytes(result, n); } else { printf("Decoding error!\n"); return 1; } printf("Inverse test:\n"); for (i = 0; i < num_tests; ++i) { randomly_fill(test, 128); n = encode_4b6b(test, tmp, 128); len = decode_4b6b(tmp, result, n); if (len != 128) { printf("Decoding error!\n"); return 1; } if (memcmp(result, test, 128) != 0) { printf("Decoded to wrong value!\n"); return 1; } } return 0; }
int main(int argc, char **argv) { int n, len; if (argc <= 1 || argc >= BUF_SIZE) { fprintf(stderr, "Usage: %s bytes in hex ...\n", argv[0]); return 1; } --argc; ++argv; for (n = 0; n < argc; ++n) { int v; char *endp; v = strtol(*argv, &endp, 16); if (*endp != '\0' || v < 0 || v > 0xFF) { fprintf(stderr, "Argument '%s' is not a hex byte\n", *argv); return 1; } input[n] = v; ++argv; } len = decode_4b6b_length(n); memset(output, 0, len); if (decode_4b6b(input, output, n)) printf("Decoding FAILED\n"); print_bytes(output, len); return 0; }
bool receiveMedtronicMessage (uint8_t message[], size_t * const length) { size_t i = 0; uint8_t lastData = 0xFF; RFST = RFST_SIDLE; RFST = RFST_SRX; PKTLEN = 0xFF; enableTimerInt(); for( ; i<128 && lastData != 0x00; ++i ) { while (!RFTXRXIF) { usbUartProcess(); usbReceiveData(); if (RFIF & 0x40) { RFIF &= 0xBF; lastData = 0xFF; i = 0; RFST = RFST_SIDLE; RFST = RFST_SRX; resetTimerCounter(); } } stopTimerInt (); rfMessage[i] = RFD; lastData = rfMessage[i]; TCON &= ~0x02; } rfLength = i-1; RFST = RFST_SIDLE; //P1_1 = ~P1_1; decode_4b6b( rfMessage, rfLength, message, length ); if( check_crc8( message, *length - 1 ) ) { return false; } if( check_crc16( message, *length -2 ) ) { return false; } if( check_crc8( message, *length - 2 ) ) { return false; } if( check_crc16( message, *length - 3 ) ) { return false; } return true; }
uint8_t *recv_packet(int timeout) { __xdata static uint8_t bytes[256]; int length, n; uint8_t crc; length = radio_receive(packet, sizeof(packet), timeout); if (length == 0) { #if VERBOSE print_time(); printf("Receive timeout\n"); #endif return 0; } #if VERBOSE print_time(); printf("Received %d-byte packet:\n", length); print_bytes(packet, length); #endif memset(bytes, 0, sizeof(bytes)); n = decode_4b6b(packet, bytes, length); #if VERBOSE printf("4b/6b decoding%s:\n", n == -1 ? " FAILED" : ""); #else print_time(); if (n == -1) { printf("4b/6b decoding failed\n"); return 0; } printf("< "); #endif print_bytes(bytes, n); if (n < 2) return 0; crc = crc8(bytes, n - 1); if (bytes[n - 1] != crc) printf("CRC should be %02X\n", crc); return bytes; }