renault.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Renault tires TPMS. Usually 443.92 Mhz FSK.
  5. *
  6. * Preamble + sync + Manchester bits. ~48us short pulse.
  7. * 9 Bytes in total not counting the preamble. */
  8. #include "../../app.h"
  9. #define USE_TEST_VECTOR 0
  10. static const char* test_vector =
  11. "...01010101010101010110" // Preamble + sync
  12. /* The following is Marshal encoded, so each two characters are
  13. * actaully one bit. 01 = 0, 10 = 1. */
  14. "010110010110" // Flags.
  15. "10011001101010011001" // Pressure, multiply by 0.75 to obtain kpa.
  16. // 244 kpa here.
  17. "1010010110011010" // Temperature, subtract 30 to obtain celsius. 22C here.
  18. "1001010101101001"
  19. "0101100110010101"
  20. "1001010101100110" // Tire ID. 0x7AD779 here.
  21. "0101010101010101"
  22. "0101010101010101" // Two FF bytes (usually). Unknown.
  23. "0110010101010101"; // CRC8 with (poly 7, initialization 0).
  24. static bool decode(uint8_t* bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo* info) {
  25. if(USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
  26. bitmap_set_pattern(bits, numbytes, 0, test_vector);
  27. numbits = strlen(test_vector);
  28. }
  29. if(numbits - 12 < 9 * 8) return false;
  30. const char* sync_pattern = "01010101010101010110";
  31. uint64_t off = bitmap_seek_bits(bits, numbytes, 0, numbits, sync_pattern);
  32. if(off == BITMAP_SEEK_NOT_FOUND) return false;
  33. FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");
  34. info->start_off = off;
  35. off += 20; /* Skip preamble. */
  36. uint8_t raw[9];
  37. uint32_t decoded = convert_from_line_code(
  38. raw, sizeof(raw), bits, numbytes, off, "01", "10"); /* Manchester. */
  39. FURI_LOG_E(TAG, "Renault TPMS decoded bits: %lu", decoded);
  40. if(decoded < 8 * 9) return false; /* Require the full 9 bytes. */
  41. if(crc8(raw, 8, 0, 7) != raw[8]) return false; /* Require sane CRC. */
  42. info->pulses_count = (off + 8 * 9 * 2) - info->start_off;
  43. uint8_t flags = raw[0] >> 2;
  44. float kpa = 0.75 * ((uint32_t)((raw[0] & 3) << 8) | raw[1]);
  45. int temp = raw[2] - 30;
  46. fieldset_add_bytes(info->fieldset, "Tire ID", raw + 3, 3 * 2);
  47. fieldset_add_float(info->fieldset, "Pressure kpa", kpa, 2);
  48. fieldset_add_int(info->fieldset, "Temperature C", temp, 8);
  49. fieldset_add_hex(info->fieldset, "Flags", flags, 6);
  50. fieldset_add_bytes(info->fieldset, "Unknown1", raw + 6, 2);
  51. fieldset_add_bytes(info->fieldset, "Unknown2", raw + 7, 2);
  52. return true;
  53. }
  54. /* Give fields and defaults for the signal creator. */
  55. static void get_fields(ProtoViewFieldSet* fieldset) {
  56. uint8_t default_id[3] = {0xAB, 0xCD, 0xEF};
  57. fieldset_add_bytes(fieldset, "Tire ID", default_id, 3 * 2);
  58. fieldset_add_float(fieldset, "Pressure kpa", 123, 2);
  59. fieldset_add_int(fieldset, "Temperature C", 20, 8);
  60. // We don't know what flags are, but 1B is a common value.
  61. fieldset_add_hex(fieldset, "Flags", 0x1b, 6);
  62. fieldset_add_bytes(fieldset, "Unknown1", (uint8_t*)"\xff", 2);
  63. fieldset_add_bytes(fieldset, "Unknown2", (uint8_t*)"\xff", 2);
  64. }
  65. /* Create a Renault TPMS signal, according to the fields provided. */
  66. static void build_message(RawSamplesBuffer* samples, ProtoViewFieldSet* fieldset) {
  67. uint32_t te = 50; // Short pulse duration in microseconds.
  68. // Preamble + sync
  69. const char* psync = "01010101010101010101010101010110";
  70. const char* p = psync;
  71. while(*p) {
  72. raw_samples_add_or_update(samples, *p == '1', te);
  73. p++;
  74. }
  75. // Data, 9 bytes
  76. uint8_t data[9] = {0};
  77. unsigned int raw_pressure = fieldset->fields[1]->fvalue * 4 / 3;
  78. data[0] = fieldset->fields[3]->uvalue << 2; // Flags
  79. data[0] |= (raw_pressure >> 8) & 3; // Pressure kpa high 2 bits
  80. data[1] = raw_pressure & 0xff; // Pressure kpa low 8 bits
  81. data[2] = fieldset->fields[2]->value + 30; // Temperature C
  82. memcpy(data + 3, fieldset->fields[0]->bytes, 6); // ID, 24 bits.
  83. data[6] = fieldset->fields[4]->bytes[0]; // Unknown 1
  84. data[7] = fieldset->fields[5]->bytes[0]; // Unknown 2
  85. data[8] = crc8(data, 8, 0, 7);
  86. // Generate Manchester code for each bit
  87. for(uint32_t j = 0; j < 9 * 8; j++) {
  88. if(bitmap_get(data, sizeof(data), j)) {
  89. raw_samples_add_or_update(samples, true, te);
  90. raw_samples_add_or_update(samples, false, te);
  91. } else {
  92. raw_samples_add_or_update(samples, false, te);
  93. raw_samples_add_or_update(samples, true, te);
  94. }
  95. }
  96. }
  97. ProtoViewDecoder RenaultTPMSDecoder = {
  98. .name = "Renault TPMS",
  99. .decode = decode,
  100. .get_fields = get_fields,
  101. .build_message = build_message};