protocol_metakom.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "protocol_metakom.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <core/check.h>
  5. #define METAKOM_DATA_SIZE 4
  6. #define METAKOM_PERIOD_SAMPLE_COUNT 10
  7. typedef enum {
  8. METAKOM_WAIT_PERIOD_SYNC,
  9. METAKOM_WAIT_START_BIT,
  10. METAKOM_WAIT_START_WORD,
  11. METAKOM_READ_WORD,
  12. METAKOM_READ_STOP_WORD,
  13. } MetakomState;
  14. typedef enum {
  15. METAKOM_BIT_WAIT_FRONT_HIGH,
  16. METAKOM_BIT_WAIT_FRONT_LOW,
  17. } MetakomBitState;
  18. struct ProtocolMetakom {
  19. PulseProtocol* protocol;
  20. // high + low period time
  21. uint32_t period_time;
  22. uint32_t low_time_storage;
  23. uint8_t period_sample_index;
  24. uint32_t period_sample_data[METAKOM_PERIOD_SAMPLE_COUNT];
  25. // ready flag
  26. // TODO: atomic access
  27. bool ready;
  28. uint8_t tmp_data;
  29. uint8_t tmp_counter;
  30. uint32_t key_data;
  31. uint8_t key_data_index;
  32. MetakomBitState bit_state;
  33. MetakomState state;
  34. };
  35. static void metakom_pulse(void* context, bool polarity, uint32_t length);
  36. static void metakom_reset(void* context);
  37. static void metakom_get_data(void* context, uint8_t* data, size_t length);
  38. static bool metakom_decoded(void* context);
  39. ProtocolMetakom* protocol_metakom_alloc() {
  40. ProtocolMetakom* metakom = malloc(sizeof(ProtocolMetakom));
  41. metakom_reset(metakom);
  42. metakom->protocol = pulse_protocol_alloc();
  43. pulse_protocol_set_context(metakom->protocol, metakom);
  44. pulse_protocol_set_pulse_cb(metakom->protocol, metakom_pulse);
  45. pulse_protocol_set_reset_cb(metakom->protocol, metakom_reset);
  46. pulse_protocol_set_get_data_cb(metakom->protocol, metakom_get_data);
  47. pulse_protocol_set_decoded_cb(metakom->protocol, metakom_decoded);
  48. return metakom;
  49. }
  50. void protocol_metakom_free(ProtocolMetakom* metakom) {
  51. furi_assert(metakom);
  52. pulse_protocol_free(metakom->protocol);
  53. free(metakom);
  54. }
  55. PulseProtocol* protocol_metakom_get_protocol(ProtocolMetakom* metakom) {
  56. furi_assert(metakom);
  57. return metakom->protocol;
  58. }
  59. static void metakom_get_data(void* context, uint8_t* data, size_t length) {
  60. furi_assert(context);
  61. furi_check(length >= METAKOM_DATA_SIZE);
  62. ProtocolMetakom* metakom = context;
  63. memcpy(data, &metakom->key_data, METAKOM_DATA_SIZE);
  64. }
  65. static bool metakom_decoded(void* context) {
  66. furi_assert(context);
  67. ProtocolMetakom* metakom = context;
  68. bool decoded = metakom->ready;
  69. return decoded;
  70. }
  71. static void metakom_reset(void* context) {
  72. furi_assert(context);
  73. ProtocolMetakom* metakom = context;
  74. metakom->ready = false;
  75. metakom->period_sample_index = 0;
  76. metakom->period_time = 0;
  77. metakom->tmp_counter = 0;
  78. metakom->tmp_data = 0;
  79. for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
  80. metakom->period_sample_data[i] = 0;
  81. };
  82. metakom->state = METAKOM_WAIT_PERIOD_SYNC;
  83. metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
  84. metakom->key_data = 0;
  85. metakom->key_data_index = 0;
  86. metakom->low_time_storage = 0;
  87. }
  88. static bool metakom_parity_check(uint8_t data) {
  89. uint8_t ones_count = 0;
  90. bool result;
  91. for(uint8_t i = 0; i < 8; i++) {
  92. if((data >> i) & 0b00000001) {
  93. ones_count++;
  94. }
  95. }
  96. result = (ones_count % 2 == 0);
  97. return result;
  98. }
  99. static bool metakom_process_bit(
  100. ProtocolMetakom* metakom,
  101. bool polarity,
  102. uint32_t time,
  103. uint32_t* high_time,
  104. uint32_t* low_time) {
  105. bool result = false;
  106. switch(metakom->bit_state) {
  107. case METAKOM_BIT_WAIT_FRONT_LOW:
  108. if(polarity == false) {
  109. *low_time = metakom->low_time_storage;
  110. *high_time = time;
  111. result = true;
  112. metakom->bit_state = METAKOM_BIT_WAIT_FRONT_HIGH;
  113. }
  114. break;
  115. case METAKOM_BIT_WAIT_FRONT_HIGH:
  116. if(polarity == true) {
  117. metakom->low_time_storage = time;
  118. metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
  119. }
  120. break;
  121. }
  122. return result;
  123. }
  124. static void metakom_pulse(void* context, bool polarity, uint32_t time) {
  125. furi_assert(context);
  126. ProtocolMetakom* metakom = context;
  127. if(metakom->ready) return;
  128. uint32_t high_time = 0;
  129. uint32_t low_time = 0;
  130. switch(metakom->state) {
  131. case METAKOM_WAIT_PERIOD_SYNC:
  132. if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
  133. metakom->period_sample_data[metakom->period_sample_index] = high_time + low_time;
  134. metakom->period_sample_index++;
  135. if(metakom->period_sample_index == METAKOM_PERIOD_SAMPLE_COUNT) {
  136. for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
  137. metakom->period_time += metakom->period_sample_data[i];
  138. };
  139. metakom->period_time /= METAKOM_PERIOD_SAMPLE_COUNT;
  140. metakom->state = METAKOM_WAIT_START_BIT;
  141. }
  142. }
  143. break;
  144. case METAKOM_WAIT_START_BIT:
  145. if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
  146. metakom->tmp_counter++;
  147. if(high_time > metakom->period_time) {
  148. metakom->tmp_counter = 0;
  149. metakom->state = METAKOM_WAIT_START_WORD;
  150. }
  151. if(metakom->tmp_counter > 40) {
  152. metakom_reset(metakom);
  153. }
  154. }
  155. break;
  156. case METAKOM_WAIT_START_WORD:
  157. if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
  158. if(low_time < (metakom->period_time / 2)) {
  159. metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
  160. } else {
  161. metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
  162. }
  163. metakom->tmp_counter++;
  164. if(metakom->tmp_counter == 3) {
  165. if(metakom->tmp_data == 0b010) {
  166. metakom->tmp_counter = 0;
  167. metakom->tmp_data = 0;
  168. metakom->state = METAKOM_READ_WORD;
  169. } else {
  170. metakom_reset(metakom);
  171. }
  172. }
  173. }
  174. break;
  175. case METAKOM_READ_WORD:
  176. if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
  177. if(low_time < (metakom->period_time / 2)) {
  178. metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
  179. } else {
  180. metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
  181. }
  182. metakom->tmp_counter++;
  183. if(metakom->tmp_counter == 8) {
  184. if(metakom_parity_check(metakom->tmp_data)) {
  185. metakom->key_data = (metakom->key_data << 8) | metakom->tmp_data;
  186. metakom->key_data_index++;
  187. metakom->tmp_data = 0;
  188. metakom->tmp_counter = 0;
  189. if(metakom->key_data_index == 4) {
  190. // check for stop bit
  191. if(high_time > metakom->period_time) {
  192. metakom->state = METAKOM_READ_STOP_WORD;
  193. } else {
  194. metakom_reset(metakom);
  195. }
  196. }
  197. } else {
  198. metakom_reset(metakom);
  199. }
  200. }
  201. }
  202. break;
  203. case METAKOM_READ_STOP_WORD:
  204. if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
  205. if(low_time < (metakom->period_time / 2)) {
  206. metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
  207. } else {
  208. metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
  209. }
  210. metakom->tmp_counter++;
  211. if(metakom->tmp_counter == 3) {
  212. if(metakom->tmp_data == 0b010) {
  213. metakom->ready = true;
  214. } else {
  215. metakom_reset(metakom);
  216. }
  217. }
  218. }
  219. break;
  220. }
  221. }