unknown.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "../app.h"
  2. /* Copyright (C) 2023 Salvatore Sanfilippo -- All Rights Reserved
  3. * See the LICENSE file for information about the license.
  4. *
  5. * ----------------------------------------------------------------------------
  6. * The "unknown" decoder fires as the last one, once we are sure no other
  7. * decoder was able to identify the signal. The goal is to detect the
  8. * preamble and line code used in the received signal, then turn the
  9. * decoded bits into bytes.
  10. *
  11. * The techniques used for the detection are described in the comments
  12. * below.
  13. * ----------------------------------------------------------------------------
  14. */
  15. /* Scan the signal bitmap looking for a PWM modulation. In this case
  16. * for PWM we are referring to two exact patterns of high and low
  17. * signal (each bit in the bitmap is worth the smallest gap/pulse duration
  18. * we detected) that repeat each other in a given segment of the message.
  19. *
  20. * This modulation is quite common, for instance sometimes zero and
  21. * one are rappresented by a 700us pulse followed by 350 gap,
  22. * and 350us pulse followed by a 700us gap. So the signal bitmap received
  23. * by the decoder would contain 110 and 100 symbols.
  24. *
  25. * The way this function work is commented inline.
  26. *
  27. * The function returns the number of consecutive symbols found, having
  28. * a symbol length of 'symlen' (3 in the above example), and stores
  29. * in *s1i the offset of the first symbol found, and in *s2i the offset
  30. * of the second symbol. The function can't tell which is one and which
  31. * zero. */
  32. static uint32_t find_pwm(uint8_t *bits, uint32_t numbytes, uint32_t numbits,
  33. uint32_t symlen, uint32_t *s1i, uint32_t *s2i)
  34. {
  35. uint32_t best_count = 0; /* Max number of symbols found in this try. */
  36. uint32_t best_idx1 = 0; /* First symbol offset of longest sequence found.
  37. * This is also the start sequence offset. */
  38. uint32_t best_idx2 = 0; /* Second symbol offset. */
  39. /* Try all the possible symbol offsets that are less of our
  40. * symbol len. This is likely not really useful but we take
  41. * a conservative approach. Because if have have, for instance,
  42. * repeating symbols "100" and "110", they will form a sequence
  43. * that is choerent at different offsets, but out-of-sync.
  44. *
  45. * Anyway at the end of the function we try to fix the sync. */
  46. for (uint32_t off = 0; off < symlen; off++) {
  47. uint32_t c = 0; // Number of contiguous symbols found.
  48. *s1i = off; // Assume we start at one symbol boundaty.
  49. *s2i = UINT32_MAX; // Second symbol first index still unknown.
  50. uint32_t next = off;
  51. /* We scan the whole bitmap in one pass, resetting the state
  52. * each time we find a pattern that is not one of the two
  53. * symbols we found so far. */
  54. while(next < numbits-symlen) {
  55. bool match1 = bitmap_match_bitmap(bits,numbytes,next,
  56. bits,numbytes,*s1i,
  57. symlen);
  58. if (!match1 && *s2i == UINT32_MAX) {
  59. /* It's not the first sybol. We don't know how the
  60. * second look like. Assume we found an occurrence of
  61. * the second symbol. */
  62. *s2i = next;
  63. }
  64. bool match2 = bitmap_match_bitmap(bits,numbytes,next,
  65. bits,numbytes,*s2i,
  66. symlen);
  67. /* One or the other should match. */
  68. if (match1 || match2) {
  69. c++;
  70. if (c > best_count) {
  71. best_count = c;
  72. best_idx1 = *s1i;
  73. best_idx2 = *s2i;
  74. }
  75. next += symlen;
  76. } else {
  77. /* No match. Continue resetting the signal info. */
  78. c = 0; // Start again to count contiguous symbols.
  79. *s1i = next; // First symbol always at start.
  80. *s2i = UINT32_MAX; // Second symbol unknown.
  81. }
  82. }
  83. }
  84. /* We don't know if we are really synchronized with the bits at this point.
  85. * For example if zero bit is 100 and one bit is 110 in a specific
  86. * line code, our detector could randomly believe it's 001 and 101.
  87. * However PWD line codes normally start with a pulse in both symbols.
  88. * If that is the case, let's align. */
  89. uint32_t shift;
  90. for (shift = 0; shift < symlen; shift++) {
  91. if (bitmap_get(bits,numbytes,best_idx1+shift) &&
  92. bitmap_get(bits,numbytes,best_idx2+shift)) break;
  93. }
  94. if (shift != symlen) {
  95. best_idx1 += shift;
  96. best_idx2 += shift;
  97. }
  98. *s1i = best_idx1;
  99. *s2i = best_idx2;
  100. return best_count;
  101. }
  102. /* Find the longest sequence that looks like Manchester coding.
  103. *
  104. * Manchester coding requires each pairs of bits to be either
  105. * 01 or 10. We'll have to try odd and even offsets to be
  106. * sure to find it.
  107. *
  108. * Note that this will also detect differential Manchester, but
  109. * will report it as Manchester. I can't think of any way to
  110. * distinguish between the two line codes, because shifting them
  111. * one symbol will make one to look like the other.
  112. *
  113. * Only option could be to decode the message with both line
  114. * codes and use statistical properties (common byte values)
  115. * to determine what's more likely, but this looks very fragile.
  116. *
  117. * Fortunately differential Manchester is more rarely used,
  118. * so we can assume Manchester most of the times. Yet we are left
  119. * with the indetermination about zero being pulse-gap or gap-pulse
  120. * or the other way around.
  121. *
  122. * If the 'only_raising' parameter is true, the function detects
  123. * only sequences going from gap to pulse: this is useful in order
  124. * to locate preambles of alternating gaps and pulses. */
  125. static uint32_t find_alternating_bits(uint8_t *bits, uint32_t numbytes,
  126. uint32_t numbits, uint32_t *start, bool only_raising)
  127. {
  128. uint32_t best_count = 0; // Max number of symbols found
  129. uint32_t best_off = 0; // Max symbols start offset.
  130. for (int odd = 0; odd < 2; odd++) {
  131. uint32_t count = 0; // Symbols found so far
  132. uint32_t start_off = odd;
  133. uint32_t j = odd;
  134. while (j < numbits-1) {
  135. bool bit1 = bitmap_get(bits,numbytes,j);
  136. bool bit2 = bitmap_get(bits,numbytes,j+1);
  137. if ((!only_raising && bit1 != bit2) ||
  138. (only_raising && !bit1 && bit2))
  139. {
  140. count++;
  141. if (count > best_count) {
  142. best_count = count;
  143. best_off = start_off;
  144. }
  145. } else {
  146. /* End of sequence. Continue with the next
  147. * part of the signal. */
  148. count = 0;
  149. start_off = j + 2;
  150. }
  151. j += 2;
  152. }
  153. }
  154. *start = best_off;
  155. return best_count;
  156. }
  157. /* Wrapper to find Manchester code. */
  158. static uint32_t find_manchester(uint8_t *bits, uint32_t numbytes,
  159. uint32_t numbits, uint32_t *start)
  160. {
  161. return find_alternating_bits(bits,numbytes,numbits,start,false);
  162. }
  163. /* Wrapper to find preamble sections. */
  164. static uint32_t find_preamble(uint8_t *bits, uint32_t numbytes,
  165. uint32_t numbits, uint32_t *start)
  166. {
  167. return find_alternating_bits(bits,numbytes,numbits,start,true);
  168. }
  169. typedef enum {
  170. LineCodeNone,
  171. LineCodeManchester,
  172. LineCodePWM3,
  173. LineCodePWM4,
  174. } LineCodeGuess;
  175. static char *get_linecode_name(LineCodeGuess lc) {
  176. switch(lc) {
  177. case LineCodeNone: return "none";
  178. case LineCodeManchester: return "Manchester";
  179. case LineCodePWM3: return "PWM3";
  180. case LineCodePWM4: return "PWM4";
  181. }
  182. return "unknown";
  183. }
  184. static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
  185. /* No decoder was able to detect this message. Let's try if we can
  186. * find some structure. To start, we'll see if it looks like is
  187. * manchester coded, or PWM with symbol len of 3 or 4. */
  188. /* For PWM, start1 and start2 are the offsets at which the two
  189. * sequences composing the message appear the first time.
  190. * So start1 is also the message start offset. Start2 is not used
  191. * for Manchester, that does not have two separated symbols like
  192. * PWM. */
  193. uint32_t start1 = 0, start2 = 0;
  194. uint32_t msgbits; // Number of message bits in the bitmap, so
  195. // this will be the number of symbols, not actual
  196. // bits after the message is decoded.
  197. uint32_t tmp1, tmp2; // Temp vars to store the start.
  198. uint32_t minbits = 16; // Less than that gets undetected.
  199. uint32_t pwm_len; // Bits per symbol, in the case of PWM.
  200. LineCodeGuess linecode = LineCodeNone;
  201. // Try PWM3
  202. uint32_t pwm3_bits = find_pwm(bits,numbytes,numbits,3,&tmp1,&tmp2);
  203. if (pwm3_bits >= minbits) {
  204. linecode = LineCodePWM3;
  205. start1 = tmp1;
  206. start2 = tmp2;
  207. pwm_len = 3;
  208. msgbits = pwm3_bits*pwm_len;
  209. }
  210. // Try PWM4
  211. uint32_t pwm4_bits = find_pwm(bits,numbytes,numbits,4,&tmp1,&tmp2);
  212. if (pwm4_bits >= minbits && pwm4_bits > pwm3_bits) {
  213. linecode = LineCodePWM4;
  214. start1 = tmp1;
  215. start2 = tmp2;
  216. pwm_len = 4;
  217. msgbits = pwm3_bits*pwm_len;
  218. }
  219. // Try Manchester
  220. uint32_t manchester_bits = find_manchester(bits,numbytes,numbits,&tmp1);
  221. if (manchester_bits > minbits &&
  222. manchester_bits > pwm3_bits &&
  223. manchester_bits > pwm4_bits)
  224. {
  225. linecode = LineCodeManchester;
  226. start1 = tmp1;
  227. msgbits = pwm3_bits*2;
  228. }
  229. if (linecode == LineCodeNone) return false;
  230. /* Often there is a preamble before the signal. We'll try to find
  231. * it, and if it is not too far away from our signal, we'll claim
  232. * our signal starts at the preamble. */
  233. uint32_t preamble_len = find_preamble(bits,numbytes,numbits,&tmp1);
  234. uint32_t min_preamble_len = 10;
  235. uint32_t max_preamble_distance = 32;
  236. uint32_t preamble_start;
  237. bool preamble_found = false;
  238. if (preamble_len >= min_preamble_len && // Not too short.
  239. tmp1 < start1 && // Should be before the data.
  240. start1-tmp1 <= max_preamble_distance) // Not too far.
  241. {
  242. preamble_start = tmp1;
  243. preamble_found = true;
  244. }
  245. info->start_off = preamble_found ? preamble_start : start1;
  246. info->pulses_count = (start1+msgbits) - info->start_off;
  247. info->pulses_count += 20; /* Add a few more, so that if the user resends
  248. * the message, it is more likely we will
  249. * transfer all that is needed, like a message
  250. * terminator (that we don't detect). */
  251. /* We think there is a message and we know where it starts and the
  252. * line code used. We can turn it into bits and bytes. */
  253. uint32_t decoded;
  254. uint8_t data[32];
  255. uint32_t datalen;
  256. char symbol1[5], symbol2[5];
  257. if (linecode == LineCodePWM3 || linecode == LineCodePWM4) {
  258. bitmap_to_string(symbol1,bits,numbytes,start1,pwm_len);
  259. bitmap_to_string(symbol2,bits,numbytes,start2,pwm_len);
  260. } else if (linecode == LineCodeManchester) {
  261. memcpy(symbol1,"01",3);
  262. memcpy(symbol2,"10",3);
  263. }
  264. decoded = convert_from_line_code(data,sizeof(data),bits,numbytes,start1,
  265. symbol1,symbol2);
  266. datalen = (decoded+7)/8;
  267. char *linecode_name = get_linecode_name(linecode);
  268. fieldset_add_str(info->fieldset,"line code",linecode_name,strlen(linecode_name));
  269. fieldset_add_uint(info->fieldset,"preamble len",preamble_len,8);
  270. fieldset_add_str(info->fieldset,"first symbol",symbol1,strlen(symbol1));
  271. fieldset_add_str(info->fieldset,"second symbol",symbol2,strlen(symbol2));
  272. fieldset_add_uint(info->fieldset,"data bits",decoded,8);
  273. for (uint32_t j = 0; j < datalen; j++) {
  274. char label[16];
  275. snprintf(label,sizeof(label),"data[%lu]",j);
  276. fieldset_add_bytes(info->fieldset,label,data+j,2);
  277. }
  278. return true;
  279. }
  280. ProtoViewDecoder UnknownDecoder = {
  281. .name = "Unknown",
  282. .decode = decode,
  283. .get_fields = NULL,
  284. .build_message = NULL
  285. };