math.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #define bit_read(value, bit) (((value) >> (bit)) & 0x01)
  6. #define bit_set(value, bit) ((value) |= (1UL << (bit)))
  7. #define bit_clear(value, bit) ((value) &= ~(1UL << (bit)))
  8. #define bit_write(value, bit, bitvalue) (bitvalue ? bit_set(value, bit) : bit_clear(value, bit))
  9. #define DURATION_DIFF(x, y) ((x < y) ? (y - x) : (x - y))
  10. #define abs(x) ((x) > 0 ? (x) : -(x))
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * Flip the data bitwise.
  16. * @param key In data
  17. * @param count_bit number of data bits
  18. * @return Reverse data
  19. **/
  20. uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t count_bit);
  21. /**
  22. * Get parity the data bitwise.
  23. * @param key In data
  24. * @param count_bit number of data bits
  25. * @return parity
  26. **/
  27. uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t count_bit);
  28. /**
  29. * CRC-4.
  30. * @param message array of bytes to check
  31. * @param nBytes number of bytes in message
  32. * @param polynomial CRC polynomial
  33. * @param init starting crc value
  34. * @return CRC value
  35. **/
  36. uint8_t subghz_protocol_blocks_crc4(
  37. uint8_t const message[],
  38. unsigned nBytes,
  39. uint8_t polynomial,
  40. uint8_t init);
  41. /**
  42. * CRC-7.
  43. * @param message array of bytes to check
  44. * @param nBytes number of bytes in message
  45. * @param polynomial CRC polynomial
  46. * @param init starting crc value
  47. * @return CRC value
  48. **/
  49. uint8_t subghz_protocol_blocks_crc7(
  50. uint8_t const message[],
  51. unsigned nBytes,
  52. uint8_t polynomial,
  53. uint8_t init);
  54. /**
  55. * Generic Cyclic Redundancy Check CRC-8.
  56. * Example polynomial: 0x31 = x8 + x5 + x4 + 1 (x8 is implicit)
  57. * Example polynomial: 0x80 = x8 + x7 (a normal bit-by-bit parity XOR)
  58. * @param message array of bytes to check
  59. * @param nBytes number of bytes in message
  60. * @param polynomial byte is from x^7 to x^0 (x^8 is implicitly one)
  61. * @param init starting crc value
  62. * @return CRC value
  63. **/
  64. uint8_t subghz_protocol_blocks_crc8(
  65. uint8_t const message[],
  66. unsigned nBytes,
  67. uint8_t polynomial,
  68. uint8_t init);
  69. /**
  70. * "Little-endian" Cyclic Redundancy Check CRC-8 LE
  71. * Input and output are reflected, i.e. least significant bit is shifted in first.
  72. * @param message array of bytes to check
  73. * @param nBytes number of bytes in message
  74. * @param polynomial CRC polynomial
  75. * @param init starting crc value
  76. * @return CRC value
  77. **/
  78. uint8_t subghz_protocol_blocks_crc8le(
  79. uint8_t const message[],
  80. unsigned nBytes,
  81. uint8_t polynomial,
  82. uint8_t init);
  83. /**
  84. * CRC-16 LSB.
  85. * Input and output are reflected, i.e. least significant bit is shifted in first.
  86. * Note that poly and init already need to be reflected.
  87. * @param message array of bytes to check
  88. * @param nBytes number of bytes in message
  89. * @param polynomial CRC polynomial
  90. * @param init starting crc value
  91. * @return CRC value
  92. **/
  93. uint16_t subghz_protocol_blocks_crc16lsb(
  94. uint8_t const message[],
  95. unsigned nBytes,
  96. uint16_t polynomial,
  97. uint16_t init);
  98. /**
  99. * CRC-16.
  100. * @param message array of bytes to check
  101. * @param nBytes number of bytes in message
  102. * @param polynomial CRC polynomial
  103. * @param init starting crc value
  104. * @return CRC value
  105. **/
  106. uint16_t subghz_protocol_blocks_crc16(
  107. uint8_t const message[],
  108. unsigned nBytes,
  109. uint16_t polynomial,
  110. uint16_t init);
  111. /**
  112. * Digest-8 by "LFSR-based Toeplitz hash".
  113. * @param message bytes of message data
  114. * @param bytes number of bytes to digest
  115. * @param gen key stream generator, needs to includes the MSB if the LFSR is rolling
  116. * @param key initial key
  117. * @return digest value
  118. **/
  119. uint8_t subghz_protocol_blocks_lfsr_digest8(
  120. uint8_t const message[],
  121. unsigned bytes,
  122. uint8_t gen,
  123. uint8_t key);
  124. /**
  125. * Digest-8 by "LFSR-based Toeplitz hash", byte reflect, bit reflect.
  126. * @param message bytes of message data
  127. * @param bytes number of bytes to digest
  128. * @param gen key stream generator, needs to includes the MSB if the LFSR is rolling
  129. * @param key initial key
  130. * @return digest value
  131. **/
  132. uint8_t subghz_protocol_blocks_lfsr_digest8_reflect(
  133. uint8_t const message[],
  134. int bytes,
  135. uint8_t gen,
  136. uint8_t key);
  137. /**
  138. * Digest-16 by "LFSR-based Toeplitz hash".
  139. * @param message bytes of message data
  140. * @param bytes number of bytes to digest
  141. * @param gen key stream generator, needs to includes the MSB if the LFSR is rolling
  142. * @param key initial key
  143. * @return digest value
  144. **/
  145. uint16_t subghz_protocol_blocks_lfsr_digest16(
  146. uint8_t const message[],
  147. unsigned bytes,
  148. uint16_t gen,
  149. uint16_t key);
  150. #ifdef __cplusplus
  151. }
  152. #endif