keeloq.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Microchip HCS200/HCS300/HSC301 KeeLoq, rolling code remotes.
  2. *
  3. * Usually 443.92 Mhz OOK, ~200us or ~400us pulse len, depending
  4. * on the configuration.
  5. *
  6. * Preamble: 12 pairs of alternating pulse/gap.
  7. * Sync: long gap of around 10 times the duration of the short-pulse.
  8. * Data: pulse width encoded data. Each bit takes three cycles:
  9. *
  10. * 0 = 110
  11. * 1 = 100
  12. *
  13. * There are a total of 66 bits transmitted.
  14. * 0..31: 32 bits of encrypted rolling code.
  15. * 32..59: Remote ID, 28 bits
  16. * 60..63: Buttons pressed
  17. * 64..64: Low battery if set
  18. * 65..65: Always set to 1
  19. *
  20. * Bits in bytes are inverted: least significant bit is first.
  21. * For some reason there is no checksum whatsoever, so we only decode
  22. * if we find everything well formed.
  23. */
  24. #include "../app.h"
  25. static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
  26. /* In the sync pattern, we require the 12 high/low pulses and at least
  27. * half the gap we expect (5 pulses times, one is the final zero in the
  28. * 24 symbols high/low sequence, then other 4). */
  29. const char *sync_pattern = "101010101010101010101010" "0000";
  30. uint8_t sync_len = 24+4;
  31. if (numbits-sync_len+sync_len < 3*66) return false;
  32. uint64_t off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_pattern);
  33. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  34. off += sync_len;
  35. /* Now there is half the gap left, but we allow from 3 to 7, instead of 5
  36. * symbols of gap, to avoid missing the signal for a matter of wrong
  37. * timing. */
  38. uint8_t gap_len = 0;
  39. while(gap_len <= 7 && bitmap_get(bits,numbytes,off+gap_len) == 0)
  40. gap_len++;
  41. if (gap_len < 3 || gap_len > 7) return false;
  42. off += gap_len;
  43. FURI_LOG_E(TAG, "Keeloq preamble+sync found");
  44. uint8_t raw[9] = {0};
  45. uint32_t decoded =
  46. convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
  47. "110","100"); /* Pulse width modulation. */
  48. FURI_LOG_E(TAG, "Keeloq decoded bits: %lu", decoded);
  49. if (decoded < 66) return false; /* Require the full 66 bits. */
  50. bitmap_reverse_bytes(raw,sizeof(raw)); /* Keeloq is LSB first. */
  51. int buttons = raw[7]>>4;
  52. int s3 = (buttons&1) != 0;
  53. int s0 = (buttons&2) != 0;
  54. int s1 = (buttons&4) != 0;
  55. int s2 = (buttons&8) != 0;
  56. int remote_id = ((raw[7]&0x0f) << 24) |
  57. (raw[6] << 16) |
  58. (raw[5] << 8) |
  59. (raw[4] << 0);
  60. int lowbat = raw[8]&0x80;
  61. snprintf(info->name,sizeof(info->name),"%s","Keeloq remote");
  62. snprintf(info->raw,sizeof(info->raw),"%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  63. raw[0],raw[1],raw[2],raw[3],raw[4],raw[5],
  64. raw[6],raw[7],raw[8]);
  65. snprintf(info->info1,sizeof(info->info1),"Encrpyted %02X%02X%02X%02X",
  66. raw[3],raw[2],raw[1],raw[0]);
  67. snprintf(info->info2,sizeof(info->info2),"ID %08X", remote_id);
  68. snprintf(info->info3,sizeof(info->info3),"s0-s3: %d%d%d%d",
  69. s0,s1,s2,s3);
  70. snprintf(info->info4,sizeof(info->info4),"Low battery? %s",
  71. lowbat ? "yes" : "no");
  72. return true;
  73. }
  74. ProtoViewDecoder KeeloqDecoder = {
  75. "Keeloq", decode
  76. };