toyota.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Toyota tires TPMS. Usually 443.92 Mhz FSK (In Europe).
  5. *
  6. * Preamble + sync + 64 bits of data. ~48us short pulse length.
  7. *
  8. * The preamble + sync is something like:
  9. *
  10. * 10101010101 (preamble) + 001111[1] (sync)
  11. *
  12. * Note: the final [1] means that sometimes it is four 1s, sometimes
  13. * five, depending on the short pulse length detection and the exact
  14. * duration of the high long pulse. After the sync, a differential
  15. * Manchester encoded payload follows. However the Flipper's CC1101
  16. * often can't decode correctly the initial alternating pattern 101010101,
  17. * so what we do is to seek just the sync, that is "001111" or "0011111",
  18. * however we now that it must be followed by one differenitally encoded
  19. * bit, so we can use also the first symbol of data to force a more robust
  20. * detection, and look for one of the following:
  21. *
  22. * [001111]00
  23. * [0011111]00
  24. * [001111]01
  25. * [0011111]01
  26. */
  27. #include "../../app.h"
  28. static bool decode(uint8_t* bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo* info) {
  29. if(numbits - 6 < 64 * 2)
  30. return false; /* Ask for 64 bit of data (each bit
  31. is two symbols in the bitmap). */
  32. char* sync[] = {"00111100", "001111100", "00111101", "001111101", NULL};
  33. int j;
  34. uint32_t off = 0;
  35. for(j = 0; sync[j]; j++) {
  36. off = bitmap_seek_bits(bits, numbytes, 0, numbits, sync[j]);
  37. if(off != BITMAP_SEEK_NOT_FOUND) {
  38. info->start_off = off;
  39. off += strlen(sync[j]) - 2;
  40. break;
  41. }
  42. }
  43. if(off == BITMAP_SEEK_NOT_FOUND) return false;
  44. FURI_LOG_E(TAG, "Toyota TPMS sync[%s] found", sync[j]);
  45. uint8_t raw[9];
  46. uint32_t decoded = convert_from_diff_manchester(raw, sizeof(raw), bits, numbytes, off, true);
  47. FURI_LOG_E(TAG, "Toyota TPMS decoded bits: %lu", decoded);
  48. if(decoded < 8 * 9) return false; /* Require the full 8 bytes. */
  49. if(crc8(raw, 8, 0x80, 7) != raw[8]) return false; /* Require sane CRC. */
  50. /* We detected a valid signal. However now info->start_off is actually
  51. * pointing to the sync part, not the preamble of alternating 0 and 1.
  52. * Protoview decoders get called with some space to the left, in order
  53. * for the decoder itself to fix the signal if neeeded, so that its
  54. * logical representation will be more accurate and better to save
  55. * and retransmit. */
  56. if(info->start_off >= 12) {
  57. info->start_off -= 12;
  58. bitmap_set_pattern(bits, numbytes, info->start_off, "010101010101");
  59. }
  60. info->pulses_count = (off + 8 * 9 * 2) - info->start_off;
  61. float psi = (float)((raw[4] & 0x7f) << 1 | raw[5] >> 7) * 0.25 - 7;
  62. int temp = ((raw[5] & 0x7f) << 1 | raw[6] >> 7) - 40;
  63. fieldset_add_bytes(info->fieldset, "Tire ID", raw, 4 * 2);
  64. fieldset_add_float(info->fieldset, "Pressure psi", psi, 2);
  65. fieldset_add_int(info->fieldset, "Temperature C", temp, 8);
  66. return true;
  67. }
  68. ProtoViewDecoder ToyotaTPMSDecoder =
  69. {.name = "Toyota TPMS", .decode = decode, .get_fields = NULL, .build_message = NULL};