python-bchlib icon indicating copy to clipboard operation
python-bchlib copied to clipboard

BCH source code, always error -EBADMSG

Open giabattag opened this issue 3 years ago • 1 comments

Good morning,

First of all, thank you for the great library! It is very well made and simple to use.

I am not really an expert with ecc (and maybe also with C programming) and I am having some problems with the functions in the C src code (I am implementing this lib in an STM32F407). I am replicating the example: I send a message, encode it, make a bitflip on the packet and then decode it and correct the errors. Nevertheless, I always get error -EBADMSG (even if I don't make any bitflip).

I am using a bch with m=9, t=24, generator polynomial = 545, which seems to work fine on the Python side (my data has a size of more or less 30 bytes).

I found out that the function "decode_bch" stops at line 1094 "if (err != nroots)".

The only thing I changed is the function htonl(x), as I don't have "arpa/inet.h", and I swapped it with the macro htonl(x) __bswap32(_x) in "machine/endian.h": might that be the problem, as it is used in encode_bch?

It might also be an input problem: for example, what's the difference between recv_ecc and calc_ecc? I'll paste you here my code where I call encode_bch and decode_bch, too (I debugged to verify the correct copying of the bits and data, ecc are not modified between the encode and the decode).

// ENCODE void BCH_Encode(uint8_t* data) { // here data is the raw message without ecc struct bch_control* bch = init_bch(9, 24, (unsigned int) GF9); uint8_t ecc[bch->ecc_bytes];

encode_bch(bch, data, sizeof(data), ecc);

memcpy(&data[sizeof(data)-1],ecc,(bch->ecc_bytes)*sizeof(uint8_t)); // here i add the ecc to data

}

// DECODE int BCH_Decode(uint8_t* packet, const unsigned int psize) {

struct bch_control* bch = init_bch(9, 24, (unsigned int) GF9);
unsigned int errloc[24];
uint8_t data[psize-(bch->ecc_bytes)];
uint8_t ecc[bch->ecc_bytes];
unsigned int dsize = sizeof(data);

int err;
printf("%d\n", psize-(bch->ecc_bytes));

bitflips(packet);              // bitflip creation

// separate data and ecc

memcpy(data,packet,(psize-(bch->ecc_bytes))*sizeof(uint8_t));
printf("%s\n", data);

for (int i=0; i<bch->ecc_bytes; i++) {
	ecc[i] = packet[i+dsize];
}

    err = decode_bch(bch, data, dsize, ecc, NULL,  NULL, errloc);

printf("Error: %d\n",err);

if (err != -EBADMSG) {                                 // always have ==
        correct_bch(packet, errloc, psize);
         memmove(data,packet,(psize-(bch->ecc_bytes)));
         memmove(ecc,packet + (psize-(bch->ecc_bytes)-1),(bch->ecc_bytes)*sizeof(uint8_t));

         printf("Message: %s\n", (char*) data);

           memset(packet,'\0',psize);
           memmove(packet,data,psize);
           return 0;
} else {
	return 1; // ERROR_FAILED_DECODE
}

}

Sorry for the long message, I hope I explained clearly my problem. Thank you in advance!

giabattag avatar Mar 04 '22 15:03 giabattag

Problem solved. I had to set ecc to 0 before using the encode

giabattag avatar Mar 11 '22 08:03 giabattag