renault.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Renault tires TPMS. Usually 443.92 Mhz FSK.
  2. *
  3. * Preamble + sync + Manchester bits. ~48us short pulse.
  4. * 9 Bytes in total not counting the preamble. */
  5. #include "../../app.h"
  6. #define USE_TEST_VECTOR 0
  7. static const char *test_vector =
  8. "...01010101010101010110" // Preamble + sync
  9. /* The following is Marshal encoded, so each two characters are
  10. * actaully one bit. 01 = 0, 10 = 1. */
  11. "010110010110" // Flags.
  12. "10011001101010011001" // Pressure, multiply by 0.75 to obtain kpa.
  13. // 244 kpa here.
  14. "1010010110011010" // Temperature, subtract 30 to obtain celsius. 22C here.
  15. "1001010101101001"
  16. "0101100110010101"
  17. "1001010101100110" // Tire ID. 0x7AD779 here.
  18. "0101010101010101"
  19. "0101010101010101" // Two FF bytes (usually). Unknown.
  20. "0110010101010101"; // CRC8 with (poly 7, initialization 0).
  21. static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
  22. if (USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
  23. bitmap_set_pattern(bits,numbytes,0,test_vector);
  24. numbits = strlen(test_vector);
  25. }
  26. if (numbits-12 < 9*8) return false;
  27. const char *sync_pattern = "01010101010101010110";
  28. uint64_t off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_pattern);
  29. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  30. FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");
  31. info->start_off = off;
  32. off += 20; /* Skip preamble. */
  33. uint8_t raw[9];
  34. uint32_t decoded =
  35. convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
  36. "01","10"); /* Manchester. */
  37. FURI_LOG_E(TAG, "Renault TPMS decoded bits: %lu", decoded);
  38. if (decoded < 8*9) return false; /* Require the full 9 bytes. */
  39. if (crc8(raw,8,0,7) != raw[8]) return false; /* Require sane CRC. */
  40. info->pulses_count = (off+8*9*2) - info->start_off;
  41. float kpa = 0.75 *((uint32_t)((raw[0]&3)<<8) | raw[1]);
  42. int temp = raw[2]-30;
  43. fieldset_add_bytes(info->fieldset,"Tire ID",raw+3,3*2);
  44. fieldset_add_float(info->fieldset,"Pressure kpa",kpa,2);
  45. fieldset_add_int(info->fieldset,"Temperature C",temp,8);
  46. return true;
  47. }
  48. ProtoViewDecoder RenaultTPMSDecoder = {
  49. .name = "Renault TPMS",
  50. .decode = decode,
  51. .get_fields = NULL,
  52. .build_message = NULL
  53. };