toyota.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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) return false; /* Ask for 64 bit of data (each bit
  30. is two symbols in the bitmap). */
  31. char *sync[] = {
  32. "00111100",
  33. "001111100",
  34. "00111101",
  35. "001111101",
  36. NULL
  37. };
  38. int j;
  39. uint32_t off = 0;
  40. for (j = 0; sync[j]; j++) {
  41. off = bitmap_seek_bits(bits,numbytes,0,numbits,sync[j]);
  42. if (off != BITMAP_SEEK_NOT_FOUND) {
  43. info->start_off = off;
  44. off += strlen(sync[j])-2;
  45. break;
  46. }
  47. }
  48. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  49. FURI_LOG_E(TAG, "Toyota TPMS sync[%s] found", sync[j]);
  50. uint8_t raw[9];
  51. uint32_t decoded =
  52. convert_from_diff_manchester(raw,sizeof(raw),bits,numbytes,off,true);
  53. FURI_LOG_E(TAG, "Toyota TPMS decoded bits: %lu", decoded);
  54. if (decoded < 8*9) return false; /* Require the full 8 bytes. */
  55. if (crc8(raw,8,0x80,7) != raw[8]) return false; /* Require sane CRC. */
  56. /* We detected a valid signal. However now info->start_off is actually
  57. * pointing to the sync part, not the preamble of alternating 0 and 1.
  58. * Protoview decoders get called with some space to the left, in order
  59. * for the decoder itself to fix the signal if neeeded, so that its
  60. * logical representation will be more accurate and better to save
  61. * and retransmit. */
  62. if (info->start_off >= 12) {
  63. info->start_off -= 12;
  64. bitmap_set_pattern(bits,numbytes,info->start_off,"010101010101");
  65. }
  66. info->pulses_count = (off+8*9*2) - info->start_off;
  67. float psi = (float)((raw[4]&0x7f)<<1 | raw[5]>>7) * 0.25 - 7;
  68. int temp = ((raw[5]&0x7f)<<1 | raw[6]>>7) - 40;
  69. fieldset_add_bytes(info->fieldset,"Tire ID",raw,4*2);
  70. fieldset_add_float(info->fieldset,"Pressure psi",psi,2);
  71. fieldset_add_int(info->fieldset,"Temperature C",temp,8);
  72. return true;
  73. }
  74. ProtoViewDecoder ToyotaTPMSDecoder = {
  75. .name = "Toyota TPMS",
  76. .decode = decode,
  77. .get_fields = NULL,
  78. .build_message = NULL
  79. };