SubGhz: fix variable types and CC1101 GPIO initialization optimization (#1931)

* SubGhz: fix variable types
* SubGhz: CC1101 GPIO initialization optimization
* SubGhz: return back gpio init
* SubGhz: cleanup grammar in math and format doxygen comments

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Skorpionm 2022-10-26 23:40:13 +04:00 committed by GitHub
parent ebaa84b0c2
commit aff99a72e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 235 additions and 200 deletions

View File

@ -102,8 +102,8 @@ static bool subghz_tx(SubGhz* subghz, uint32_t frequency) {
furi_assert(subghz->txrx->txrx_state != SubGhzTxRxStateSleep); furi_assert(subghz->txrx->txrx_state != SubGhzTxRxStateSleep);
furi_hal_subghz_idle(); furi_hal_subghz_idle();
furi_hal_subghz_set_frequency_and_path(frequency); furi_hal_subghz_set_frequency_and_path(frequency);
furi_hal_gpio_write(&gpio_cc1101_g0, false);
furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow); furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
furi_hal_gpio_write(&gpio_cc1101_g0, true);
bool ret = furi_hal_subghz_tx(); bool ret = furi_hal_subghz_tx();
subghz->txrx->txrx_state = SubGhzTxRxStateTx; subghz->txrx->txrx_state = SubGhzTxRxStateTx;
return ret; return ret;

View File

@ -1,5 +1,5 @@
entry,status,name,type,params entry,status,name,type,params
Version,+,5.1,, Version,+,6.0,,
Header,+,applications/services/bt/bt_service/bt.h,, Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,, Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,, Header,+,applications/services/cli/cli_vcp.h,,
@ -2272,21 +2272,22 @@ Function,-,subghz_keystore_raw_get_data,_Bool,"const char*, size_t, uint8_t*, si
Function,-,subghz_keystore_save,_Bool,"SubGhzKeystore*, const char*, uint8_t*" Function,-,subghz_keystore_save,_Bool,"SubGhzKeystore*, const char*, uint8_t*"
Function,+,subghz_protocol_blocks_add_bit,void,"SubGhzBlockDecoder*, uint8_t" Function,+,subghz_protocol_blocks_add_bit,void,"SubGhzBlockDecoder*, uint8_t"
Function,+,subghz_protocol_blocks_add_bytes,uint8_t,"const uint8_t[], size_t" Function,+,subghz_protocol_blocks_add_bytes,uint8_t,"const uint8_t[], size_t"
Function,+,subghz_protocol_blocks_crc16,uint16_t,"const uint8_t[], unsigned, uint16_t, uint16_t" Function,+,subghz_protocol_blocks_add_to_128_bit,void,"SubGhzBlockDecoder*, uint8_t, uint64_t*"
Function,+,subghz_protocol_blocks_crc16lsb,uint16_t,"const uint8_t[], unsigned, uint16_t, uint16_t" Function,+,subghz_protocol_blocks_crc16,uint16_t,"const uint8_t[], size_t, uint16_t, uint16_t"
Function,+,subghz_protocol_blocks_crc4,uint8_t,"const uint8_t[], unsigned, uint8_t, uint8_t" Function,+,subghz_protocol_blocks_crc16lsb,uint16_t,"const uint8_t[], size_t, uint16_t, uint16_t"
Function,+,subghz_protocol_blocks_crc7,uint8_t,"const uint8_t[], unsigned, uint8_t, uint8_t" Function,+,subghz_protocol_blocks_crc4,uint8_t,"const uint8_t[], size_t, uint8_t, uint8_t"
Function,+,subghz_protocol_blocks_crc8,uint8_t,"const uint8_t[], unsigned, uint8_t, uint8_t" Function,+,subghz_protocol_blocks_crc7,uint8_t,"const uint8_t[], size_t, uint8_t, uint8_t"
Function,+,subghz_protocol_blocks_crc8le,uint8_t,"const uint8_t[], unsigned, uint8_t, uint8_t" Function,+,subghz_protocol_blocks_crc8,uint8_t,"const uint8_t[], size_t, uint8_t, uint8_t"
Function,+,subghz_protocol_blocks_crc8le,uint8_t,"const uint8_t[], size_t, uint8_t, uint8_t"
Function,+,subghz_protocol_blocks_get_bit_array,_Bool,"uint8_t[], size_t" Function,+,subghz_protocol_blocks_get_bit_array,_Bool,"uint8_t[], size_t"
Function,+,subghz_protocol_blocks_get_hash_data,uint8_t,"SubGhzBlockDecoder*, size_t" Function,+,subghz_protocol_blocks_get_hash_data,uint8_t,"SubGhzBlockDecoder*, size_t"
Function,+,subghz_protocol_blocks_get_parity,uint8_t,"uint64_t, uint8_t" Function,+,subghz_protocol_blocks_get_parity,uint8_t,"uint64_t, uint8_t"
Function,+,subghz_protocol_blocks_get_upload,size_t,"uint8_t[], size_t, LevelDuration*, size_t, uint32_t" Function,+,subghz_protocol_blocks_get_upload,size_t,"uint8_t[], size_t, LevelDuration*, size_t, uint32_t"
Function,+,subghz_protocol_blocks_lfsr_digest16,uint16_t,"const uint8_t[], unsigned, uint16_t, uint16_t" Function,+,subghz_protocol_blocks_lfsr_digest16,uint16_t,"const uint8_t[], size_t, uint16_t, uint16_t"
Function,+,subghz_protocol_blocks_lfsr_digest8,uint8_t,"const uint8_t[], unsigned, uint8_t, uint8_t" Function,+,subghz_protocol_blocks_lfsr_digest8,uint8_t,"const uint8_t[], size_t, uint8_t, uint8_t"
Function,+,subghz_protocol_blocks_lfsr_digest8_reflect,uint8_t,"const uint8_t[], int, uint8_t, uint8_t" Function,+,subghz_protocol_blocks_lfsr_digest8_reflect,uint8_t,"const uint8_t[], size_t, uint8_t, uint8_t"
Function,+,subghz_protocol_blocks_parity8,int,uint8_t Function,+,subghz_protocol_blocks_parity8,uint8_t,uint8_t
Function,+,subghz_protocol_blocks_parity_bytes,int,"const uint8_t[], size_t" Function,+,subghz_protocol_blocks_parity_bytes,uint8_t,"const uint8_t[], size_t"
Function,+,subghz_protocol_blocks_reverse_key,uint64_t,"uint64_t, uint8_t" Function,+,subghz_protocol_blocks_reverse_key,uint64_t,"uint64_t, uint8_t"
Function,+,subghz_protocol_blocks_set_bit_array,void,"_Bool, uint8_t[], size_t, size_t" Function,+,subghz_protocol_blocks_set_bit_array,void,"_Bool, uint8_t[], size_t, size_t"
Function,+,subghz_protocol_blocks_xor_bytes,uint8_t,"const uint8_t[], size_t" Function,+,subghz_protocol_blocks_xor_bytes,uint8_t,"const uint8_t[], size_t"

1 entry status name type params
2 Version + 5.1 6.0
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/cli/cli.h
5 Header + applications/services/cli/cli_vcp.h
2272 Function - subghz_keystore_save _Bool SubGhzKeystore*, const char*, uint8_t*
2273 Function + subghz_protocol_blocks_add_bit void SubGhzBlockDecoder*, uint8_t
2274 Function + subghz_protocol_blocks_add_bytes uint8_t const uint8_t[], size_t
2275 Function + subghz_protocol_blocks_crc16 subghz_protocol_blocks_add_to_128_bit uint16_t void const uint8_t[], unsigned, uint16_t, uint16_t SubGhzBlockDecoder*, uint8_t, uint64_t*
2276 Function + subghz_protocol_blocks_crc16lsb subghz_protocol_blocks_crc16 uint16_t const uint8_t[], unsigned, uint16_t, uint16_t const uint8_t[], size_t, uint16_t, uint16_t
2277 Function + subghz_protocol_blocks_crc4 subghz_protocol_blocks_crc16lsb uint8_t uint16_t const uint8_t[], unsigned, uint8_t, uint8_t const uint8_t[], size_t, uint16_t, uint16_t
2278 Function + subghz_protocol_blocks_crc7 subghz_protocol_blocks_crc4 uint8_t const uint8_t[], unsigned, uint8_t, uint8_t const uint8_t[], size_t, uint8_t, uint8_t
2279 Function + subghz_protocol_blocks_crc8 subghz_protocol_blocks_crc7 uint8_t const uint8_t[], unsigned, uint8_t, uint8_t const uint8_t[], size_t, uint8_t, uint8_t
2280 Function + subghz_protocol_blocks_crc8le subghz_protocol_blocks_crc8 uint8_t const uint8_t[], unsigned, uint8_t, uint8_t const uint8_t[], size_t, uint8_t, uint8_t
2281 Function + subghz_protocol_blocks_crc8le uint8_t const uint8_t[], size_t, uint8_t, uint8_t
2282 Function + subghz_protocol_blocks_get_bit_array _Bool uint8_t[], size_t
2283 Function + subghz_protocol_blocks_get_hash_data uint8_t SubGhzBlockDecoder*, size_t
2284 Function + subghz_protocol_blocks_get_parity uint8_t uint64_t, uint8_t
2285 Function + subghz_protocol_blocks_get_upload size_t uint8_t[], size_t, LevelDuration*, size_t, uint32_t
2286 Function + subghz_protocol_blocks_lfsr_digest16 uint16_t const uint8_t[], unsigned, uint16_t, uint16_t const uint8_t[], size_t, uint16_t, uint16_t
2287 Function + subghz_protocol_blocks_lfsr_digest8 uint8_t const uint8_t[], unsigned, uint8_t, uint8_t const uint8_t[], size_t, uint8_t, uint8_t
2288 Function + subghz_protocol_blocks_lfsr_digest8_reflect uint8_t const uint8_t[], int, uint8_t, uint8_t const uint8_t[], size_t, uint8_t, uint8_t
2289 Function + subghz_protocol_blocks_parity8 int uint8_t uint8_t
2290 Function + subghz_protocol_blocks_parity_bytes int uint8_t const uint8_t[], size_t
2291 Function + subghz_protocol_blocks_reverse_key uint64_t uint64_t, uint8_t
2292 Function + subghz_protocol_blocks_set_bit_array void _Bool, uint8_t[], size_t, size_t
2293 Function + subghz_protocol_blocks_xor_bytes uint8_t const uint8_t[], size_t

View File

@ -7,6 +7,16 @@ void subghz_protocol_blocks_add_bit(SubGhzBlockDecoder* decoder, uint8_t bit) {
decoder->decode_count_bit++; decoder->decode_count_bit++;
} }
void subghz_protocol_blocks_add_to_128_bit(
SubGhzBlockDecoder* decoder,
uint8_t bit,
uint64_t* head_64_bit) {
if(++decoder->decode_count_bit > 64) {
(*head_64_bit) = ((*head_64_bit) << 1) | (decoder->decode_data >> 63);
}
decoder->decode_data = decoder->decode_data << 1 | bit;
}
uint8_t subghz_protocol_blocks_get_hash_data(SubGhzBlockDecoder* decoder, size_t len) { uint8_t subghz_protocol_blocks_get_hash_data(SubGhzBlockDecoder* decoder, size_t len) {
uint8_t hash = 0; uint8_t hash = 0;
uint8_t* p = (uint8_t*)&decoder->decode_data; uint8_t* p = (uint8_t*)&decoder->decode_data;

View File

@ -24,6 +24,17 @@ struct SubGhzBlockDecoder {
*/ */
void subghz_protocol_blocks_add_bit(SubGhzBlockDecoder* decoder, uint8_t bit); void subghz_protocol_blocks_add_bit(SubGhzBlockDecoder* decoder, uint8_t bit);
/**
* Add data to_128 bit when decoding.
* @param decoder Pointer to a SubGhzBlockDecoder instance
* @param head_64_bit Pointer to a head_64_bit
* @param bit data, 1bit
*/
void subghz_protocol_blocks_add_to_128_bit(
SubGhzBlockDecoder* decoder,
uint8_t bit,
uint64_t* head_64_bit);
/** /**
* Getting the hash sum of the last randomly received parcel. * Getting the hash sum of the last randomly received parcel.
* @param decoder Pointer to a SubGhzBlockDecoder instance * @param decoder Pointer to a SubGhzBlockDecoder instance

View File

@ -1,16 +1,16 @@
#include "math.h" #include "math.h"
uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t count_bit) { uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t bit_count) {
uint64_t key_reverse = 0; uint64_t reverse_key = 0;
for(uint8_t i = 0; i < count_bit; i++) { for(uint8_t i = 0; i < bit_count; i++) {
key_reverse = key_reverse << 1 | bit_read(key, i); reverse_key = reverse_key << 1 | bit_read(key, i);
} }
return key_reverse; return reverse_key;
} }
uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t count_bit) { uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t bit_count) {
uint8_t parity = 0; uint8_t parity = 0;
for(uint8_t i = 0; i < count_bit; i++) { for(uint8_t i = 0; i < bit_count; i++) {
parity += bit_read(key, i); parity += bit_read(key, i);
} }
return parity & 0x01; return parity & 0x01;
@ -18,14 +18,14 @@ uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t count_bit) {
uint8_t subghz_protocol_blocks_crc4( uint8_t subghz_protocol_blocks_crc4(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init) { uint8_t init) {
unsigned remainder = init << 4; // LSBs are unused uint8_t remainder = init << 4; // LSBs are unused
unsigned poly = polynomial << 4; uint8_t poly = polynomial << 4;
unsigned bit; uint8_t bit;
while(nBytes--) { while(size--) {
remainder ^= *message++; remainder ^= *message++;
for(bit = 0; bit < 8; bit++) { for(bit = 0; bit < 8; bit++) {
if(remainder & 0x80) { if(remainder & 0x80) {
@ -40,16 +40,15 @@ uint8_t subghz_protocol_blocks_crc4(
uint8_t subghz_protocol_blocks_crc7( uint8_t subghz_protocol_blocks_crc7(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init) { uint8_t init) {
unsigned remainder = init << 1; // LSB is unused uint8_t remainder = init << 1; // LSB is unused
unsigned poly = polynomial << 1; uint8_t poly = polynomial << 1;
unsigned byte, bit;
for(byte = 0; byte < nBytes; ++byte) { for(size_t byte = 0; byte < size; ++byte) {
remainder ^= message[byte]; remainder ^= message[byte];
for(bit = 0; bit < 8; ++bit) { for(uint8_t bit = 0; bit < 8; ++bit) {
if(remainder & 0x80) { if(remainder & 0x80) {
remainder = (remainder << 1) ^ poly; remainder = (remainder << 1) ^ poly;
} else { } else {
@ -62,15 +61,14 @@ uint8_t subghz_protocol_blocks_crc7(
uint8_t subghz_protocol_blocks_crc8( uint8_t subghz_protocol_blocks_crc8(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init) { uint8_t init) {
uint8_t remainder = init; uint8_t remainder = init;
unsigned byte, bit;
for(byte = 0; byte < nBytes; ++byte) { for(size_t byte = 0; byte < size; ++byte) {
remainder ^= message[byte]; remainder ^= message[byte];
for(bit = 0; bit < 8; ++bit) { for(uint8_t bit = 0; bit < 8; ++bit) {
if(remainder & 0x80) { if(remainder & 0x80) {
remainder = (remainder << 1) ^ polynomial; remainder = (remainder << 1) ^ polynomial;
} else { } else {
@ -83,16 +81,15 @@ uint8_t subghz_protocol_blocks_crc8(
uint8_t subghz_protocol_blocks_crc8le( uint8_t subghz_protocol_blocks_crc8le(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init) { uint8_t init) {
uint8_t remainder = subghz_protocol_blocks_reverse_key(init, 8); uint8_t remainder = subghz_protocol_blocks_reverse_key(init, 8);
unsigned byte, bit;
polynomial = subghz_protocol_blocks_reverse_key(polynomial, 8); polynomial = subghz_protocol_blocks_reverse_key(polynomial, 8);
for(byte = 0; byte < nBytes; ++byte) { for(size_t byte = 0; byte < size; ++byte) {
remainder ^= message[byte]; remainder ^= message[byte];
for(bit = 0; bit < 8; ++bit) { for(uint8_t bit = 0; bit < 8; ++bit) {
if(remainder & 1) { if(remainder & 1) {
remainder = (remainder >> 1) ^ polynomial; remainder = (remainder >> 1) ^ polynomial;
} else { } else {
@ -105,15 +102,14 @@ uint8_t subghz_protocol_blocks_crc8le(
uint16_t subghz_protocol_blocks_crc16lsb( uint16_t subghz_protocol_blocks_crc16lsb(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint16_t polynomial, uint16_t polynomial,
uint16_t init) { uint16_t init) {
uint16_t remainder = init; uint16_t remainder = init;
unsigned byte, bit;
for(byte = 0; byte < nBytes; ++byte) { for(size_t byte = 0; byte < size; ++byte) {
remainder ^= message[byte]; remainder ^= message[byte];
for(bit = 0; bit < 8; ++bit) { for(uint8_t bit = 0; bit < 8; ++bit) {
if(remainder & 1) { if(remainder & 1) {
remainder = (remainder >> 1) ^ polynomial; remainder = (remainder >> 1) ^ polynomial;
} else { } else {
@ -126,15 +122,14 @@ uint16_t subghz_protocol_blocks_crc16lsb(
uint16_t subghz_protocol_blocks_crc16( uint16_t subghz_protocol_blocks_crc16(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint16_t polynomial, uint16_t polynomial,
uint16_t init) { uint16_t init) {
uint16_t remainder = init; uint16_t remainder = init;
unsigned byte, bit;
for(byte = 0; byte < nBytes; ++byte) { for(size_t byte = 0; byte < size; ++byte) {
remainder ^= message[byte] << 8; remainder ^= message[byte] << 8;
for(bit = 0; bit < 8; ++bit) { for(uint8_t bit = 0; bit < 8; ++bit) {
if(remainder & 0x8000) { if(remainder & 0x8000) {
remainder = (remainder << 1) ^ polynomial; remainder = (remainder << 1) ^ polynomial;
} else { } else {
@ -147,18 +142,18 @@ uint16_t subghz_protocol_blocks_crc16(
uint8_t subghz_protocol_blocks_lfsr_digest8( uint8_t subghz_protocol_blocks_lfsr_digest8(
uint8_t const message[], uint8_t const message[],
unsigned bytes, size_t size,
uint8_t gen, uint8_t gen,
uint8_t key) { uint8_t key) {
uint8_t sum = 0; uint8_t sum = 0;
for(unsigned k = 0; k < bytes; ++k) { for(size_t byte = 0; byte < size; ++byte) {
uint8_t data = message[k]; uint8_t data = message[byte];
for(int i = 7; i >= 0; --i) { for(int i = 7; i >= 0; --i) {
// XOR key into sum if data bit is set // XOR key into sum if data bit is set
if((data >> i) & 1) sum ^= key; if((data >> i) & 1) sum ^= key;
// roll the key right (actually the lsb is dropped here) // roll the key right (actually the LSB is dropped here)
// and apply the gen (needs to include the dropped lsb as msb) // and apply the gen (needs to include the dropped LSB as MSB)
if(key & 1) if(key & 1)
key = (key >> 1) ^ gen; key = (key >> 1) ^ gen;
else else
@ -170,22 +165,22 @@ uint8_t subghz_protocol_blocks_lfsr_digest8(
uint8_t subghz_protocol_blocks_lfsr_digest8_reflect( uint8_t subghz_protocol_blocks_lfsr_digest8_reflect(
uint8_t const message[], uint8_t const message[],
int bytes, size_t size,
uint8_t gen, uint8_t gen,
uint8_t key) { uint8_t key) {
uint8_t sum = 0; uint8_t sum = 0;
// Process message from last byte to first byte (reflected) // Process message from last byte to first byte (reflected)
for(int k = bytes - 1; k >= 0; --k) { for(int byte = size - 1; byte >= 0; --byte) {
uint8_t data = message[k]; uint8_t data = message[byte];
// Process individual bits of each byte (reflected) // Process individual bits of each byte (reflected)
for(int i = 0; i < 8; ++i) { for(uint8_t i = 0; i < 8; ++i) {
// XOR key into sum if data bit is set // XOR key into sum if data bit is set
if((data >> i) & 1) { if((data >> i) & 1) {
sum ^= key; sum ^= key;
} }
// roll the key left (actually the lsb is dropped here) // roll the key left (actually the LSB is dropped here)
// and apply the gen (needs to include the dropped lsb as msb) // and apply the gen (needs to include the dropped lsb as MSB)
if(key & 0x80) if(key & 0x80)
key = (key << 1) ^ gen; key = (key << 1) ^ gen;
else else
@ -197,18 +192,18 @@ uint8_t subghz_protocol_blocks_lfsr_digest8_reflect(
uint16_t subghz_protocol_blocks_lfsr_digest16( uint16_t subghz_protocol_blocks_lfsr_digest16(
uint8_t const message[], uint8_t const message[],
unsigned bytes, size_t size,
uint16_t gen, uint16_t gen,
uint16_t key) { uint16_t key) {
uint16_t sum = 0; uint16_t sum = 0;
for(unsigned k = 0; k < bytes; ++k) { for(size_t byte = 0; byte < size; ++byte) {
uint8_t data = message[k]; uint8_t data = message[byte];
for(int i = 7; i >= 0; --i) { for(int8_t i = 7; i >= 0; --i) {
// if data bit is set then xor with key // if data bit is set then xor with key
if((data >> i) & 1) sum ^= key; if((data >> i) & 1) sum ^= key;
// roll the key right (actually the lsb is dropped here) // roll the key right (actually the LSB is dropped here)
// and apply the gen (needs to include the dropped lsb as msb) // and apply the gen (needs to include the dropped LSB as MSB)
if(key & 1) if(key & 1)
key = (key >> 1) ^ gen; key = (key >> 1) ^ gen;
else else
@ -218,31 +213,31 @@ uint16_t subghz_protocol_blocks_lfsr_digest16(
return sum; return sum;
} }
uint8_t subghz_protocol_blocks_add_bytes(uint8_t const message[], size_t num_bytes) { uint8_t subghz_protocol_blocks_add_bytes(uint8_t const message[], size_t size) {
int result = 0; uint32_t result = 0;
for(size_t i = 0; i < num_bytes; ++i) { for(size_t i = 0; i < size; ++i) {
result += message[i]; result += message[i];
} }
return (uint8_t)result; return (uint8_t)result;
} }
int subghz_protocol_blocks_parity8(uint8_t byte) { uint8_t subghz_protocol_blocks_parity8(uint8_t byte) {
byte ^= byte >> 4; byte ^= byte >> 4;
byte &= 0xf; byte &= 0xf;
return (0x6996 >> byte) & 1; return (0x6996 >> byte) & 1;
} }
int subghz_protocol_blocks_parity_bytes(uint8_t const message[], size_t num_bytes) { uint8_t subghz_protocol_blocks_parity_bytes(uint8_t const message[], size_t size) {
int result = 0; uint8_t result = 0;
for(size_t i = 0; i < num_bytes; ++i) { for(size_t i = 0; i < size; ++i) {
result ^= subghz_protocol_blocks_parity8(message[i]); result ^= subghz_protocol_blocks_parity8(message[i]);
} }
return result; return result;
} }
uint8_t subghz_protocol_blocks_xor_bytes(uint8_t const message[], size_t num_bytes) { uint8_t subghz_protocol_blocks_xor_bytes(uint8_t const message[], size_t size) {
uint8_t result = 0; uint8_t result = 0;
for(size_t i = 0; i < num_bytes; ++i) { for(size_t i = 0; i < size; ++i) {
result ^= message[i]; result ^= message[i];
} }
return result; return result;

View File

@ -14,183 +14,201 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/**
* Flip the data bitwise. /** Flip the data bitwise
*
* @param key In data * @param key In data
* @param count_bit number of data bits * @param bit_count number of data bits
*
* @return Reverse data * @return Reverse data
**/ */
uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t count_bit); uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t bit_count);
/** /** Get parity the data bitwise
* Get parity the data bitwise. *
* @param key In data * @param key In data
* @param count_bit number of data bits * @param bit_count number of data bits
*
* @return parity * @return parity
**/ */
uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t count_bit); uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t bit_count);
/** /** CRC-4
* CRC-4. *
* @param message array of bytes to check * @param message array of bytes to check
* @param nBytes number of bytes in message * @param size number of bytes in message
* @param polynomial CRC polynomial * @param polynomial CRC polynomial
* @param init starting crc value * @param init starting crc value
*
* @return CRC value * @return CRC value
**/ */
uint8_t subghz_protocol_blocks_crc4( uint8_t subghz_protocol_blocks_crc4(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init); uint8_t init);
/** /** CRC-7
* CRC-7. *
* @param message array of bytes to check * @param message array of bytes to check
* @param nBytes number of bytes in message * @param size number of bytes in message
* @param polynomial CRC polynomial * @param polynomial CRC polynomial
* @param init starting crc value * @param init starting crc value
*
* @return CRC value * @return CRC value
**/ */
uint8_t subghz_protocol_blocks_crc7( uint8_t subghz_protocol_blocks_crc7(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init); uint8_t init);
/** /** Generic Cyclic Redundancy Check CRC-8. Example polynomial: 0x31 = x8 + x5 +
* Generic Cyclic Redundancy Check CRC-8. * x4 + 1 (x8 is implicit) Example polynomial: 0x80 = x8 + x7 (a normal
* Example polynomial: 0x31 = x8 + x5 + x4 + 1 (x8 is implicit) * bit-by-bit parity XOR)
* Example polynomial: 0x80 = x8 + x7 (a normal bit-by-bit parity XOR) *
* @param message array of bytes to check * @param message array of bytes to check
* @param nBytes number of bytes in message * @param size number of bytes in message
* @param polynomial byte is from x^7 to x^0 (x^8 is implicitly one) * @param polynomial byte is from x^7 to x^0 (x^8 is implicitly one)
* @param init starting crc value * @param init starting crc value
*
* @return CRC value * @return CRC value
**/ */
uint8_t subghz_protocol_blocks_crc8( uint8_t subghz_protocol_blocks_crc8(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init); uint8_t init);
/** /** "Little-endian" Cyclic Redundancy Check CRC-8 LE Input and output are
* "Little-endian" Cyclic Redundancy Check CRC-8 LE * reflected, i.e. least significant bit is shifted in first
* Input and output are reflected, i.e. least significant bit is shifted in first. *
* @param message array of bytes to check * @param message array of bytes to check
* @param nBytes number of bytes in message * @param size number of bytes in message
* @param polynomial CRC polynomial * @param polynomial CRC polynomial
* @param init starting crc value * @param init starting crc value
*
* @return CRC value * @return CRC value
**/ */
uint8_t subghz_protocol_blocks_crc8le( uint8_t subghz_protocol_blocks_crc8le(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint8_t polynomial, uint8_t polynomial,
uint8_t init); uint8_t init);
/** /** CRC-16 LSB. Input and output are reflected, i.e. least significant bit is
* CRC-16 LSB. * shifted in first. Note that poly and init already need to be reflected
* Input and output are reflected, i.e. least significant bit is shifted in first. *
* Note that poly and init already need to be reflected.
* @param message array of bytes to check * @param message array of bytes to check
* @param nBytes number of bytes in message * @param size number of bytes in message
* @param polynomial CRC polynomial * @param polynomial CRC polynomial
* @param init starting crc value * @param init starting crc value
*
* @return CRC value * @return CRC value
**/ */
uint16_t subghz_protocol_blocks_crc16lsb( uint16_t subghz_protocol_blocks_crc16lsb(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint16_t polynomial, uint16_t polynomial,
uint16_t init); uint16_t init);
/** /** CRC-16
* CRC-16. *
* @param message array of bytes to check * @param message array of bytes to check
* @param nBytes number of bytes in message * @param size number of bytes in message
* @param polynomial CRC polynomial * @param polynomial CRC polynomial
* @param init starting crc value * @param init starting crc value
*
* @return CRC value * @return CRC value
**/ */
uint16_t subghz_protocol_blocks_crc16( uint16_t subghz_protocol_blocks_crc16(
uint8_t const message[], uint8_t const message[],
unsigned nBytes, size_t size,
uint16_t polynomial, uint16_t polynomial,
uint16_t init); uint16_t init);
/** /** Digest-8 by "LFSR-based Toeplitz hash"
* Digest-8 by "LFSR-based Toeplitz hash". *
* @param message bytes of message data * @param message bytes of message data
* @param bytes number of bytes to digest * @param size number of bytes to digest
* @param gen key stream generator, needs to includes the MSB if the LFSR is rolling * @param gen key stream generator, needs to includes the MSB if the
* LFSR is rolling
* @param key initial key * @param key initial key
*
* @return digest value * @return digest value
**/ */
uint8_t subghz_protocol_blocks_lfsr_digest8( uint8_t subghz_protocol_blocks_lfsr_digest8(
uint8_t const message[], uint8_t const message[],
unsigned bytes, size_t size,
uint8_t gen, uint8_t gen,
uint8_t key); uint8_t key);
/** /** Digest-8 by "LFSR-based Toeplitz hash", byte reflect, bit reflect
* Digest-8 by "LFSR-based Toeplitz hash", byte reflect, bit reflect. *
* @param message bytes of message data * @param message bytes of message data
* @param bytes number of bytes to digest * @param size number of bytes to digest
* @param gen key stream generator, needs to includes the MSB if the LFSR is rolling * @param gen key stream generator, needs to includes the MSB if the
* LFSR is rolling
* @param key initial key * @param key initial key
*
* @return digest value * @return digest value
**/ */
uint8_t subghz_protocol_blocks_lfsr_digest8_reflect( uint8_t subghz_protocol_blocks_lfsr_digest8_reflect(
uint8_t const message[], uint8_t const message[],
int bytes, size_t size,
uint8_t gen, uint8_t gen,
uint8_t key); uint8_t key);
/** /** Digest-16 by "LFSR-based Toeplitz hash"
* Digest-16 by "LFSR-based Toeplitz hash". *
* @param message bytes of message data * @param message bytes of message data
* @param bytes number of bytes to digest * @param size number of bytes to digest
* @param gen key stream generator, needs to includes the MSB if the LFSR is rolling * @param gen key stream generator, needs to includes the MSB if the
* LFSR is rolling
* @param key initial key * @param key initial key
*
* @return digest value * @return digest value
**/ */
uint16_t subghz_protocol_blocks_lfsr_digest16( uint16_t subghz_protocol_blocks_lfsr_digest16(
uint8_t const message[], uint8_t const message[],
unsigned bytes, size_t size,
uint16_t gen, uint16_t gen,
uint16_t key); uint16_t key);
/** /** Compute Addition of a number of bytes
* Compute Addition of a number of bytes. *
* @param message bytes of message data * @param message bytes of message data
* @param num_bytes number of bytes to sum * @param size number of bytes to sum
*
* @return summation value * @return summation value
**/ */
uint8_t subghz_protocol_blocks_add_bytes(uint8_t const message[], size_t num_bytes); uint8_t subghz_protocol_blocks_add_bytes(uint8_t const message[], size_t size);
/** /** Compute bit parity of a single byte (8 bits)
* Compute bit parity of a single byte (8 bits). *
* @param byte single byte to check * @param byte single byte to check
*
* @return 1 odd parity, 0 even parity * @return 1 odd parity, 0 even parity
**/ */
int subghz_protocol_blocks_parity8(uint8_t byte); uint8_t subghz_protocol_blocks_parity8(uint8_t byte);
/** /** Compute bit parity of a number of bytes
* Compute bit parity of a number of bytes. *
* @param message bytes of message data * @param message bytes of message data
* @param num_bytes number of bytes to sum * @param size number of bytes to sum
*
* @return 1 odd parity, 0 even parity * @return 1 odd parity, 0 even parity
**/ */
int subghz_protocol_blocks_parity_bytes(uint8_t const message[], size_t num_bytes); uint8_t subghz_protocol_blocks_parity_bytes(uint8_t const message[], size_t size);
/** /** Compute XOR (byte-wide parity) of a number of bytes
* Compute XOR (byte-wide parity) of a number of bytes. *
* @param message bytes of message data * @param message bytes of message data
* @param num_bytes number of bytes to sum * @param size number of bytes to sum
*
* @return summation value, per bit-position 1 odd parity, 0 even parity * @return summation value, per bit-position 1 odd parity, 0 even parity
**/ */
uint8_t subghz_protocol_blocks_xor_bytes(uint8_t const message[], size_t num_bytes); uint8_t subghz_protocol_blocks_xor_bytes(uint8_t const message[], size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }