toyota.c 3.0 KB

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