subghz_protocol_princeton.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "subghz_protocol_princeton.h"
  2. /*
  3. * Help
  4. * https://phreakerclub.com/447
  5. *
  6. */
  7. #define SUBGHZ_PT_SHORT 450
  8. #define SUBGHZ_PT_LONG (SUBGHZ_PT_SHORT * 3)
  9. #define SUBGHZ_PT_GUARD (SUBGHZ_PT_SHORT * 30)
  10. struct SubGhzEncoderPrinceton {
  11. uint32_t key;
  12. uint16_t te;
  13. size_t repeat;
  14. size_t front;
  15. };
  16. struct SubGhzDecoderPrinceton {
  17. SubGhzProtocolCommon common;
  18. uint16_t te;
  19. };
  20. SubGhzEncoderPrinceton* subghz_encoder_princeton_alloc() {
  21. SubGhzEncoderPrinceton* instance = furi_alloc(sizeof(SubGhzEncoderPrinceton));
  22. return instance;
  23. }
  24. void subghz_encoder_princeton_free(SubGhzEncoderPrinceton* instance) {
  25. furi_assert(instance);
  26. free(instance);
  27. }
  28. void subghz_encoder_princeton_set_te(SubGhzEncoderPrinceton* instance, void* decoder){
  29. SubGhzDecoderPrinceton* pricenton = decoder;
  30. if((pricenton->te) !=0){
  31. instance->te = pricenton->te;
  32. }else{
  33. instance->te = SUBGHZ_PT_SHORT;
  34. }
  35. }
  36. void subghz_encoder_princeton_reset(SubGhzEncoderPrinceton* instance, uint32_t key, size_t repeat) {
  37. furi_assert(instance);
  38. instance->te = SUBGHZ_PT_SHORT;
  39. instance->key = key;
  40. instance->repeat = repeat;
  41. instance->front = 48;
  42. }
  43. size_t subghz_encoder_princeton_get_repeat_left(SubGhzEncoderPrinceton* instance) {
  44. furi_assert(instance);
  45. return instance->repeat;
  46. }
  47. LevelDuration subghz_encoder_princeton_yield(void* context) {
  48. SubGhzEncoderPrinceton* instance = context;
  49. if(instance->repeat == 0) return level_duration_reset();
  50. size_t bit = instance->front / 2;
  51. bool level = !(instance->front % 2);
  52. LevelDuration ret;
  53. if(bit < 24) {
  54. uint8_t byte = bit / 8;
  55. uint8_t bit_in_byte = bit % 8;
  56. bool value = (((uint8_t*)&instance->key)[2 - byte] >> (7 - bit_in_byte)) & 1;
  57. if(value) {
  58. ret = level_duration_make(level, level ? instance->te * 3 : instance->te);
  59. } else {
  60. ret = level_duration_make(level, level ? instance->te : instance->te * 3);
  61. }
  62. } else {
  63. ret = level_duration_make(level, level ? instance->te : instance->te * 30);
  64. }
  65. instance->front++;
  66. if(instance->front == 50) {
  67. instance->repeat--;
  68. instance->front = 0;
  69. }
  70. return ret;
  71. }
  72. SubGhzDecoderPrinceton* subghz_decoder_princeton_alloc(void) {
  73. SubGhzDecoderPrinceton* instance = furi_alloc(sizeof(SubGhzDecoderPrinceton));
  74. instance->common.name = "Princeton";
  75. instance->common.code_min_count_bit_for_found = 24;
  76. instance->common.te_shot = 450; //150;
  77. instance->common.te_long = 1350; //450;
  78. instance->common.te_delta = 200; //50;
  79. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_decoder_princeton_to_str;
  80. instance->common.to_save_string =
  81. (SubGhzProtocolCommonGetStrSave)subghz_decoder_princeton_to_save_str;
  82. instance->common.to_load_protocol=
  83. (SubGhzProtocolCommonLoad)subghz_decoder_princeton_to_load_protocol;
  84. return instance;
  85. }
  86. void subghz_decoder_princeton_free(SubGhzDecoderPrinceton* instance) {
  87. furi_assert(instance);
  88. free(instance);
  89. }
  90. /** Send bit
  91. *
  92. * @param instance - SubGhzDecoderPrinceton instance
  93. * @param bit - bit
  94. */
  95. void subghz_decoder_princeton_send_bit(SubGhzDecoderPrinceton* instance, uint8_t bit) {
  96. if(bit) {
  97. //send bit 1
  98. SUBGHZ_TX_PIN_LOW();
  99. delay_us(instance->common.te_long);
  100. SUBGHZ_TX_PIN_HIGTH();
  101. delay_us(instance->common.te_shot);
  102. } else {
  103. //send bit 0
  104. SUBGHZ_TX_PIN_LOW();
  105. delay_us(instance->common.te_shot);
  106. SUBGHZ_TX_PIN_HIGTH();
  107. delay_us(instance->common.te_long);
  108. }
  109. }
  110. void subghz_decoder_princeton_send_key(
  111. SubGhzDecoderPrinceton* instance,
  112. uint64_t key,
  113. uint8_t bit,
  114. uint8_t repeat) {
  115. while(repeat--) {
  116. SUBGHZ_TX_PIN_LOW();
  117. //Send start bit
  118. subghz_decoder_princeton_send_bit(instance, 1);
  119. //Send header
  120. delay_us(instance->common.te_shot * 33); //+2 interval v bit 1
  121. //Send key data
  122. for(uint8_t i = bit; i > 0; i--) {
  123. subghz_decoder_princeton_send_bit(instance, bit_read(key, i - 1));
  124. }
  125. }
  126. }
  127. void subghz_decoder_princeton_reset(SubGhzDecoderPrinceton* instance) {
  128. instance->common.parser_step = 0;
  129. }
  130. void subghz_decoder_princeton_parse(
  131. SubGhzDecoderPrinceton* instance,
  132. bool level,
  133. uint32_t duration) {
  134. switch(instance->common.parser_step) {
  135. case 0:
  136. if((!level) && (DURATION_DIFF(duration, instance->common.te_shot * 36) <
  137. instance->common.te_delta * 36)) {
  138. //Found Preambula
  139. instance->common.parser_step = 1;
  140. instance->common.code_found = 0;
  141. instance->common.code_count_bit = 0;
  142. } else {
  143. instance->common.parser_step = 0;
  144. }
  145. break;
  146. case 1:
  147. //save duration
  148. if(level) {
  149. instance->common.te_last = duration;
  150. instance->common.parser_step = 2;
  151. }
  152. break;
  153. case 2:
  154. if(!level) {
  155. if(duration >= (instance->common.te_shot * 10 + instance->common.te_delta)) {
  156. instance->common.parser_step = 1;
  157. if(instance->common.code_count_bit >=
  158. instance->common.code_min_count_bit_for_found) {
  159. if(instance->common.code_last_found == instance->common.code_found) {
  160. //instance->te = (instance->te+instance->common.te_last)/2; //Option 1 TE averaging
  161. if(instance->te > instance->common.te_last)
  162. instance->te = instance->common.te_last; //Option 2 TE averaging
  163. } else {
  164. instance->te = instance->common.te_last;
  165. }
  166. instance->common.code_last_found = instance->common.code_found;
  167. instance->common.code_last_count_bit = instance->common.code_count_bit;
  168. instance->common.serial = instance->common.code_found >> 4;
  169. instance->common.btn = (uint8_t)instance->common.code_found & 0x00000F;
  170. if(instance->common.callback)
  171. instance->common.callback(
  172. (SubGhzProtocolCommon*)instance, instance->common.context);
  173. }
  174. instance->common.code_found = 0;
  175. instance->common.code_count_bit = 0;
  176. break;
  177. }
  178. if((DURATION_DIFF(instance->common.te_last, instance->common.te_shot) <
  179. instance->common.te_delta) &&
  180. (DURATION_DIFF(duration, instance->common.te_long) <
  181. instance->common.te_delta * 3)) {
  182. subghz_protocol_common_add_bit(&instance->common, 0);
  183. instance->common.parser_step = 1;
  184. } else if(
  185. (DURATION_DIFF(instance->common.te_last, instance->common.te_long) <
  186. instance->common.te_delta * 3) &&
  187. (DURATION_DIFF(duration, instance->common.te_shot) < instance->common.te_delta)) {
  188. subghz_protocol_common_add_bit(&instance->common, 1);
  189. instance->common.parser_step = 1;
  190. } else {
  191. instance->common.parser_step = 0;
  192. }
  193. } else {
  194. instance->common.parser_step = 0;
  195. }
  196. break;
  197. }
  198. }
  199. void subghz_decoder_princeton_to_str(SubGhzDecoderPrinceton* instance, string_t output) {
  200. uint32_t code_found_hi = instance->common.code_last_found >> 32;
  201. uint32_t code_found_lo = instance->common.code_last_found & 0x00000000ffffffff;
  202. uint64_t code_found_reverse = subghz_protocol_common_reverse_key(
  203. instance->common.code_last_found, instance->common.code_last_count_bit);
  204. uint32_t code_found_reverse_hi = code_found_reverse >> 32;
  205. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  206. string_cat_printf(
  207. output,
  208. "%s %d Bit te %dus\r\n"
  209. " KEY:0x%lX%08lX\r\n"
  210. " YEK:0x%lX%08lX\r\n"
  211. " SN:0x%05lX BTN:%02X\r\n",
  212. instance->common.name,
  213. instance->common.code_last_count_bit,
  214. instance->te,
  215. code_found_hi,
  216. code_found_lo,
  217. code_found_reverse_hi,
  218. code_found_reverse_lo,
  219. instance->common.serial,
  220. instance->common.btn);
  221. }
  222. void subghz_decoder_princeton_to_save_str(SubGhzDecoderPrinceton* instance, string_t output) {
  223. string_printf(
  224. output,
  225. "Protocol: %s\n"
  226. "Bit: %d\n"
  227. "Te: %d\n"
  228. "Key: %08lX\n",
  229. instance->common.name,
  230. instance->common.code_last_count_bit,
  231. instance->te,
  232. (uint32_t)(instance->common.code_last_found & 0x00000000ffffffff));
  233. }
  234. bool subghz_decoder_princeton_to_load_protocol(FileWorker* file_worker, SubGhzDecoderPrinceton* instance){
  235. bool loaded = false;
  236. string_t temp_str;
  237. string_init(temp_str);
  238. int res = 0;
  239. int data = 0;
  240. do {
  241. // Read and parse bit data from 2nd line
  242. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  243. break;
  244. }
  245. res = sscanf(string_get_cstr(temp_str), "Bit: %d\n", &data);
  246. if(res != 1) {
  247. break;
  248. }
  249. instance->common.code_last_count_bit = (uint8_t)data;
  250. // Read and parse te data from 3nd line
  251. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  252. break;
  253. }
  254. res = sscanf(string_get_cstr(temp_str), "Te: %d\n", &data);
  255. if(res != 1) {
  256. break;
  257. }
  258. instance->te = (uint16_t)data;
  259. // Read and parse key data from 4nd line
  260. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  261. break;
  262. }
  263. uint32_t temp_key = 0;
  264. res = sscanf(string_get_cstr(temp_str), "Key: %08lX\n", &temp_key);
  265. if(res != 1) {
  266. break;
  267. }
  268. instance->common.code_last_found = (uint64_t)temp_key;
  269. instance->common.serial = instance->common.code_last_found >> 4;
  270. instance->common.btn = (uint8_t)instance->common.code_last_found & 0x00000F;
  271. loaded = true;
  272. } while(0);
  273. string_clear(temp_str);
  274. return loaded;
  275. }