toyota_tpms.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 bit 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. off += strlen(sync[j])-2;
  41. break;
  42. }
  43. }
  44. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  45. FURI_LOG_E(TAG, "Toyota TPMS sync[%s] found", sync[j]);
  46. uint8_t raw[8];
  47. uint32_t decoded =
  48. convert_from_diff_manchester(raw,sizeof(raw),bits,numbytes,off,true);
  49. FURI_LOG_E(TAG, "Toyota TPMS decoded bits: %lu", decoded);
  50. if (decoded < 64) return false; /* Require the full 8 bytes. */
  51. float kpa = (float)((raw[4]&0x7f)<<1 | raw[5]>>7) * 0.25 - 7;
  52. int temp = ((raw[5]&0x7f)<<1 | raw[6]>>7) - 40;
  53. snprintf(info->name,sizeof(info->name),"%s","Toyota TPMS");
  54. snprintf(info->raw,sizeof(info->raw),"%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  55. raw[0],raw[1],raw[2],raw[3],raw[4],raw[5],
  56. raw[6],raw[7],raw[8]);
  57. snprintf(info->info1,sizeof(info->info1),"Tire ID %02X%02X%02X%02X",
  58. raw[0],raw[1],raw[2],raw[3]);
  59. snprintf(info->info1,sizeof(info->info1),"Pressure %.2f psi", (double)kpa);
  60. snprintf(info->info2,sizeof(info->info2),"Temperature %d C", temp);
  61. return true;
  62. }
  63. ProtoViewDecoder ToyotaTPMSDecoder = {
  64. "Toyota TPMS", decode
  65. };