citroen.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Citroen TPMS. Usually 443.92 Mhz FSK.
  5. *
  6. * Preamble of ~14 high/low 52 us pulses
  7. * Sync of high 100us pulse then 50us low
  8. * Then Manchester bits, 10 bytes total.
  9. * Simple XOR checksum. */
  10. #include "../../app.h"
  11. static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
  12. /* We consider a preamble of 17 symbols. They are more, but the decoding
  13. * is more likely to happen if we don't pretend to receive from the
  14. * very start of the message. */
  15. uint32_t sync_len = 17;
  16. const char *sync_pattern = "10101010101010110";
  17. if (numbits-sync_len < 8*10) return false; /* Expect 10 bytes. */
  18. uint64_t off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_pattern);
  19. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  20. FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");
  21. info->start_off = off;
  22. off += sync_len; /* Skip preamble + sync. */
  23. uint8_t raw[10];
  24. uint32_t decoded =
  25. convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
  26. "01","10"); /* Manchester. */
  27. FURI_LOG_E(TAG, "Citroen TPMS decoded bits: %lu", decoded);
  28. if (decoded < 8*10) return false; /* Require the full 10 bytes. */
  29. /* Check the CRC. It's a simple XOR of bytes 1-9, the first byte
  30. * is not included. The meaning of the first byte is unknown and
  31. * we don't display it. */
  32. uint8_t crc = 0;
  33. for (int j = 1; j < 10; j++) crc ^= raw[j];
  34. if (crc != 0) return false; /* Require sane checksum. */
  35. info->pulses_count = (off+8*10*2) - info->start_off;
  36. int repeat = raw[5] & 0xf;
  37. float kpa = (float)raw[6]*1.364;
  38. int temp = raw[7]-50;
  39. int battery = raw[8]; /* This may be the battery. It's not clear. */
  40. fieldset_add_bytes(info->fieldset,"Tire ID",raw+1,4*2);
  41. fieldset_add_float(info->fieldset,"Pressure kpa",kpa,2);
  42. fieldset_add_int(info->fieldset,"Temperature C",temp,8);
  43. fieldset_add_uint(info->fieldset,"Repeat",repeat,4);
  44. fieldset_add_uint(info->fieldset,"Battery",battery,8);
  45. return true;
  46. }
  47. ProtoViewDecoder CitroenTPMSDecoder = {
  48. .name = "Citroen TPMS",
  49. .decode = decode,
  50. .get_fields = NULL,
  51. .build_message = NULL
  52. };