protocol_metakom.c 7.7 KB

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