pvchat.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "../app.h"
  2. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  3. * See the LICENSE file for information about the license.
  4. *
  5. * ----------------------------------------------------------
  6. * ProtoView chat protocol. This is just a fun test protocol
  7. * that can be used between two Flippers in order to send
  8. * and receive text messages.
  9. * ----------------------------------------------------------
  10. *
  11. * Protocol description
  12. * ====================
  13. *
  14. * "1" represents a pulse of one-third bit time (100us)
  15. * "0" represents a gap of one-third bit time (100us)
  16. *
  17. * The message starts with a preamble + a sync pattern:
  18. *
  19. * preamble = 10101010101010101010101010101010
  20. * sync = 1100110011001100
  21. *
  22. * The a variable amount of bytes follow, where each bit
  23. * is encoded in the following way:
  24. *
  25. * zero 100 (100 us pulse, 200 us gap)
  26. * one 110 (200 us pulse, 100 us gap)
  27. *
  28. * Bytes are sent MSB first, so receiving, in sequence, bits
  29. * 11100001, means byte E1.
  30. *
  31. * This is the data format:
  32. *
  33. * +-+------+-------+
  34. * |L|Sender|Message|
  35. * +-+------+-------+
  36. * | | \_Message, terminated by 0xFF + 0xAA + 1 byte of checksum
  37. * | |
  38. * | \_ L bytes of sender name
  39. * \
  40. * \_ Length of sender in bytes
  41. *
  42. *
  43. * Checksum = sum of bytes modulo 256, with checksum set
  44. * to 0 for the computation.
  45. */
  46. static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
  47. const char *sync_pattern = "1010101010101010" "1100110011001100";
  48. uint8_t sync_len = 32;
  49. // This is a variable length message, however the minimum length
  50. // requires a sender len byte (of value zero) and the terminator
  51. // FF 00 plus checksum: a total of 4 bytes.
  52. if (numbits-sync_len < 8*4) return false;
  53. uint64_t off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_pattern);
  54. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  55. FURI_LOG_E(TAG, "Chat preamble+sync found");
  56. info->start_off = off;
  57. off += sync_len; /* Skip preamble and sync. */
  58. uint8_t raw[64] = {(uint8_t)'.'};
  59. uint32_t decoded =
  60. convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
  61. "100","110"); /* PWM */
  62. FURI_LOG_E(TAG, "Chat decoded bits: %lu", decoded);
  63. if (decoded < 8*4) return false; /* Min message len. */
  64. // The message needs to have a two bytes terminator before
  65. // the checksum.
  66. uint32_t j;
  67. for (j = 0; j < sizeof(raw)-2; j++)
  68. if (raw[j] == 0xff && raw[j+1] == 0xaa) break;
  69. if (j == sizeof(raw)-1) return false; // No terminator found.
  70. uint32_t datalen = j+3; // If the terminator was found at j, then
  71. // we need to sum three more bytes to have
  72. // the len: FF itself, AA, checksum.
  73. info->pulses_count = 8*3*datalen;
  74. // Check if the control sum matches.
  75. if (sum_bytes(raw,datalen-1,0) != raw[datalen-1]) return false;
  76. // Check if the length of the sender looks sane
  77. uint8_t senderlen = raw[0];
  78. if (senderlen >= sizeof(raw)) return false; // Overflow
  79. fieldset_add_str(info->fieldset,"sender",(char*)raw+1,senderlen);
  80. fieldset_add_str(info->fieldset,"message",(char*)raw+1+senderlen,
  81. datalen-senderlen-3);
  82. return true;
  83. }
  84. /* Give fields and defaults for the signal creator. */
  85. static void get_fields(ProtoViewFieldSet *fieldset) {
  86. fieldset_add_str(fieldset,"sender","Carol",5);
  87. fieldset_add_str(fieldset,"message","Anyone hearing?",15);
  88. }
  89. /* Create a signal. */
  90. static void build_message(RawSamplesBuffer *samples, ProtoViewFieldSet *fs)
  91. {
  92. uint32_t te = 100; /* Short pulse duration in microseconds.
  93. Our protocol needs three symbol times to send
  94. a bit, so 300 us per bit = 3.33 kBaud. */
  95. // Preamble: 16 alternating 100us pulse/gap pairs.
  96. for (int j = 0; j < 16; j++) {
  97. raw_samples_add(samples,true,te);
  98. raw_samples_add(samples,false,te);
  99. }
  100. // Sync: 4 alternating 200 us pulse/gap pairs.
  101. for (int j = 0; j < 4; j++) {
  102. raw_samples_add(samples,true,te*2);
  103. raw_samples_add(samples,false,te*2);
  104. }
  105. // Data: build the array.
  106. uint32_t datalen = 1 + fs->fields[0]->len + // Userlen + Username
  107. fs->fields[1]->len + 3; // Message + FF + 00 + CRC
  108. uint8_t *data = malloc(datalen), *p = data;
  109. *p++ = fs->fields[0]->len;
  110. memcpy(p,fs->fields[0]->str,fs->fields[0]->len);
  111. p += fs->fields[0]->len;
  112. memcpy(p,fs->fields[1]->str,fs->fields[1]->len);
  113. p += fs->fields[1]->len;
  114. *p++ = 0xff;
  115. *p++ = 0xaa;
  116. *p = sum_bytes(data,datalen-1,0);
  117. FURI_LOG_E(TAG,"SUM: %lu", (unsigned long)*p);
  118. // Emit bits
  119. for (uint32_t j = 0; j < datalen*8; j++) {
  120. if (bitmap_get(data,datalen,j)) {
  121. raw_samples_add(samples,true,te*2);
  122. raw_samples_add(samples,false,te);
  123. } else {
  124. raw_samples_add(samples,true,te);
  125. raw_samples_add(samples,false,te*2);
  126. }
  127. }
  128. free(data);
  129. }
  130. ProtoViewDecoder ProtoViewChatDecoder = {
  131. .name = "ProtoView chat",
  132. .decode = decode,
  133. .get_fields = get_fields,
  134. .build_message = build_message
  135. };