subghz_protocol.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "subghz_protocol.h"
  2. #include "subghz_protocol_came.h"
  3. #include "subghz_protocol_cfm.h"
  4. #include "subghz_protocol_keeloq.h"
  5. #include "subghz_protocol_nice_flo.h"
  6. #include "subghz_protocol_nice_flor_s.h"
  7. #include "subghz_protocol_princeton.h"
  8. #include "subghz_protocol_gate_tx.h"
  9. #include "subghz_protocol_ido.h"
  10. #include "subghz_protocol_faac_slh.h"
  11. #include "subghz_protocol_nero_sketch.h"
  12. #include <furi.h>
  13. #include <m-string.h>
  14. #include <filesystem-api.h>
  15. #define FILE_BUFFER_SIZE 64
  16. struct SubGhzProtocol {
  17. SubGhzProtocolCame* came;
  18. SubGhzProtocolKeeloq* keeloq;
  19. SubGhzProtocolNiceFlo* nice_flo;
  20. SubGhzProtocolNiceFlorS* nice_flor_s;
  21. SubGhzProtocolPrinceton* princeton;
  22. SubGhzProtocolGateTX* gate_tx;
  23. SubGhzProtocolIDo* ido;
  24. SubGhzProtocolFaacSLH* faac_slh;
  25. SubGhzProtocolNeroSketch* nero_sketch;
  26. SubGhzProtocolTextCallback text_callback;
  27. void* text_callback_context;
  28. SubGhzProtocolCommonCallbackDump parser_callback;
  29. void* parser_callback_context;
  30. };
  31. static void subghz_protocol_text_rx_callback(SubGhzProtocolCommon* parser, void* context) {
  32. SubGhzProtocol* instance = context;
  33. string_t output;
  34. string_init(output);
  35. subghz_protocol_common_to_str((SubGhzProtocolCommon*)parser, output);
  36. if (instance->text_callback) {
  37. instance->text_callback(output, instance->text_callback_context);
  38. } else {
  39. printf(string_get_cstr(output));
  40. }
  41. string_clear(output);
  42. }
  43. static void subghz_protocol_parser_rx_callback(SubGhzProtocolCommon* parser, void* context) {
  44. SubGhzProtocol* instance = context;
  45. if (instance->parser_callback) {
  46. instance->parser_callback(parser, instance->parser_callback_context);
  47. }
  48. }
  49. SubGhzProtocol* subghz_protocol_alloc() {
  50. SubGhzProtocol* instance = furi_alloc(sizeof(SubGhzProtocol));
  51. instance->came = subghz_protocol_came_alloc();
  52. instance->keeloq = subghz_protocol_keeloq_alloc();
  53. instance->princeton = subghz_protocol_princeton_alloc();
  54. instance->nice_flo = subghz_protocol_nice_flo_alloc();
  55. instance->nice_flor_s = subghz_protocol_nice_flor_s_alloc();
  56. instance->gate_tx = subghz_protocol_gate_tx_alloc();
  57. instance->ido = subghz_protocol_ido_alloc();
  58. instance->faac_slh = subghz_protocol_faac_slh_alloc();
  59. instance->nero_sketch = subghz_protocol_nero_sketch_alloc();
  60. return instance;
  61. }
  62. void subghz_protocol_free(SubGhzProtocol* instance) {
  63. furi_assert(instance);
  64. subghz_protocol_came_free(instance->came);
  65. subghz_protocol_keeloq_free(instance->keeloq);
  66. subghz_protocol_princeton_free(instance->princeton);
  67. subghz_protocol_nice_flo_free(instance->nice_flo);
  68. subghz_protocol_nice_flor_s_free(instance->nice_flor_s);
  69. subghz_protocol_gate_tx_free(instance->gate_tx);
  70. subghz_protocol_ido_free(instance->ido);
  71. subghz_protocol_faac_slh_free(instance->faac_slh);
  72. subghz_protocol_nero_sketch_free(instance->nero_sketch);
  73. free(instance);
  74. }
  75. void subghz_protocol_enable_dump_text(SubGhzProtocol* instance, SubGhzProtocolTextCallback callback, void* context) {
  76. furi_assert(instance);
  77. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->came, subghz_protocol_text_rx_callback, instance);
  78. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->keeloq, subghz_protocol_text_rx_callback, instance);
  79. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->princeton, subghz_protocol_text_rx_callback, instance);
  80. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flo, subghz_protocol_text_rx_callback, instance);
  81. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flor_s, subghz_protocol_text_rx_callback, instance);
  82. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->gate_tx, subghz_protocol_text_rx_callback, instance);
  83. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->ido, subghz_protocol_text_rx_callback, instance);
  84. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->faac_slh, subghz_protocol_text_rx_callback, instance);
  85. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nero_sketch, subghz_protocol_text_rx_callback, instance);
  86. instance->text_callback = callback;
  87. instance->text_callback_context = context;
  88. }
  89. void subghz_protocol_enable_dump(SubGhzProtocol* instance, SubGhzProtocolCommonCallbackDump callback, void* context) {
  90. furi_assert(instance);
  91. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->came, subghz_protocol_parser_rx_callback, instance);
  92. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->keeloq, subghz_protocol_parser_rx_callback, instance);
  93. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->princeton, subghz_protocol_parser_rx_callback, instance);
  94. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flo, subghz_protocol_parser_rx_callback, instance);
  95. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flor_s, subghz_protocol_parser_rx_callback, instance);
  96. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->gate_tx, subghz_protocol_parser_rx_callback, instance);
  97. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->ido, subghz_protocol_parser_rx_callback, instance);
  98. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->faac_slh, subghz_protocol_parser_rx_callback, instance);
  99. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nero_sketch, subghz_protocol_parser_rx_callback, instance);
  100. instance->parser_callback = callback;
  101. instance->parser_callback_context = context;
  102. }
  103. static void subghz_protocol_load_keeloq_file_process_line(SubGhzProtocol* instance, string_t line) {
  104. uint64_t key = 0;
  105. uint16_t type = 0;
  106. char skey[17] = {0};
  107. char name[65] = {0};
  108. int ret = sscanf(string_get_cstr(line), "%16s:%hu:%64s", skey, &type, name);
  109. key = strtoull(skey, NULL, 16);
  110. if (ret == 3) {
  111. subghz_protocol_keeloq_add_manafacture_key(instance->keeloq, name, key, type);
  112. } else {
  113. printf("Failed to load line: %s\r\n", string_get_cstr(line));
  114. }
  115. }
  116. void subghz_protocol_load_nice_flor_s_file(SubGhzProtocol* instance, const char* file_name) {
  117. subghz_protocol_nice_flor_s_name_file(instance->nice_flor_s, file_name);
  118. }
  119. void subghz_protocol_load_keeloq_file(SubGhzProtocol* instance, const char* file_name) {
  120. File manufacture_keys_file;
  121. FS_Api* fs_api = furi_record_open("sdcard");
  122. fs_api->file.open(&manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING);
  123. string_t line;
  124. string_init(line);
  125. if(manufacture_keys_file.error_id == FSE_OK) {
  126. printf("Loading manufacture keys file %s\r\n", file_name);
  127. char buffer[FILE_BUFFER_SIZE];
  128. uint16_t ret;
  129. do {
  130. ret = fs_api->file.read(&manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
  131. for (uint16_t i=0; i < ret; i++) {
  132. if (buffer[i] == '\n' && string_size(line) > 0) {
  133. subghz_protocol_load_keeloq_file_process_line(instance, line);
  134. string_clean(line);
  135. } else {
  136. string_push_back(line, buffer[i]);
  137. }
  138. }
  139. } while(ret > 0);
  140. } else {
  141. printf("Manufacture keys file is not found: %s\r\n", file_name);
  142. }
  143. string_clear(line);
  144. fs_api->file.close(&manufacture_keys_file);
  145. furi_record_close("sdcard");
  146. }
  147. void subghz_protocol_reset(SubGhzProtocol* instance) {
  148. subghz_protocol_came_reset(instance->came);
  149. subghz_protocol_keeloq_reset(instance->keeloq);
  150. subghz_protocol_princeton_reset(instance->princeton);
  151. subghz_protocol_nice_flo_reset(instance->nice_flo);
  152. subghz_protocol_nice_flor_s_reset(instance->nice_flor_s);
  153. subghz_protocol_gate_tx_reset(instance->gate_tx);
  154. subghz_protocol_ido_reset(instance->ido);
  155. subghz_protocol_faac_slh_reset(instance->faac_slh);
  156. subghz_protocol_nero_sketch_reset(instance->nero_sketch);
  157. }
  158. void subghz_protocol_parse(SubGhzProtocol* instance, bool level, uint32_t duration) {
  159. subghz_protocol_came_parse(instance->came, level, duration);
  160. subghz_protocol_keeloq_parse(instance->keeloq, level, duration);
  161. subghz_protocol_princeton_parse(instance->princeton, level, duration);
  162. subghz_protocol_nice_flo_parse(instance->nice_flo, level, duration);
  163. subghz_protocol_nice_flor_s_parse(instance->nice_flor_s, level, duration);
  164. subghz_protocol_gate_tx_parse(instance->gate_tx, level, duration);
  165. subghz_protocol_ido_parse(instance->ido, level, duration);
  166. subghz_protocol_faac_slh_parse(instance->faac_slh, level, duration);
  167. subghz_protocol_nero_sketch_parse(instance->nero_sketch, level, duration);
  168. }