subghz_protocol.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <furi.h>
  9. #include <m-string.h>
  10. #include <filesystem-api.h>
  11. #define FILE_BUFFER_SIZE 64
  12. struct SubGhzProtocol {
  13. SubGhzProtocolCame* came;
  14. SubGhzProtocolKeeloq* keeloq;
  15. SubGhzProtocolNiceFlo* nice_flo;
  16. SubGhzProtocolNiceFlorS* nice_flor_s;
  17. SubGhzProtocolPrinceton* princeton;
  18. SubGhzProtocolTextCallback text_callback;
  19. void* text_callback_context;
  20. };
  21. static void subghz_protocol_came_rx_callback(SubGhzProtocolCommon* parser, void* context) {
  22. SubGhzProtocol* instance = context;
  23. string_t output;
  24. string_init(output);
  25. subghz_protocol_common_to_str((SubGhzProtocolCommon*)parser, output);
  26. if (instance->text_callback) {
  27. instance->text_callback(output, instance->text_callback_context);
  28. } else {
  29. printf(string_get_cstr(output));
  30. }
  31. string_clear(output);
  32. }
  33. SubGhzProtocol* subghz_protocol_alloc() {
  34. SubGhzProtocol* instance = furi_alloc(sizeof(SubGhzProtocol));
  35. instance->came = subghz_protocol_came_alloc();
  36. instance->keeloq = subghz_protocol_keeloq_alloc();
  37. instance->princeton = subghz_protocol_princeton_alloc();
  38. instance->nice_flo = subghz_protocol_nice_flo_alloc();
  39. instance->nice_flor_s = subghz_protocol_nice_flor_s_alloc();
  40. return instance;
  41. }
  42. void subghz_protocol_free(SubGhzProtocol* instance) {
  43. furi_assert(instance);
  44. subghz_protocol_came_free(instance->came);
  45. subghz_protocol_keeloq_free(instance->keeloq);
  46. subghz_protocol_princeton_free(instance->princeton);
  47. subghz_protocol_nice_flo_free(instance->nice_flo);
  48. subghz_protocol_nice_flor_s_free(instance->nice_flor_s);
  49. free(instance);
  50. }
  51. void subghz_protocol_enable_dump(SubGhzProtocol* instance, SubGhzProtocolTextCallback callback, void* context) {
  52. furi_assert(instance);
  53. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->came, subghz_protocol_came_rx_callback, instance);
  54. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->keeloq, subghz_protocol_came_rx_callback, instance);
  55. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->princeton, subghz_protocol_came_rx_callback, instance);
  56. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flo, subghz_protocol_came_rx_callback, instance);
  57. subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flor_s, subghz_protocol_came_rx_callback, instance);
  58. instance->text_callback = callback;
  59. instance->text_callback_context = context;
  60. }
  61. static void subghz_protocol_load_keeloq_file_process_line(SubGhzProtocol* instance, string_t line) {
  62. uint64_t key = 0;
  63. uint16_t type = 0;
  64. char skey[17] = {0};
  65. char name[65] = {0};
  66. int ret = sscanf(string_get_cstr(line), "%16s:%hu:%64s", skey, &type, name);
  67. key = strtoull(skey, NULL, 16);
  68. if (ret == 3) {
  69. subghz_protocol_keeloq_add_manafacture_key(instance->keeloq, name, key, type);
  70. } else {
  71. printf("Failed to load line: %s\r\n", string_get_cstr(line));
  72. }
  73. }
  74. void subghz_protocol_load_nice_flor_s_file(SubGhzProtocol* instance, const char* file_name) {
  75. subghz_protocol_nice_flor_s_name_file(instance->nice_flor_s, file_name);
  76. }
  77. void subghz_protocol_load_keeloq_file(SubGhzProtocol* instance, const char* file_name) {
  78. File manufacture_keys_file;
  79. FS_Api* fs_api = furi_record_open("sdcard");
  80. fs_api->file.open(&manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING);
  81. string_t line;
  82. string_init(line);
  83. if(manufacture_keys_file.error_id == FSE_OK) {
  84. printf("Loading manufacture keys file %s\r\n", file_name);
  85. char buffer[FILE_BUFFER_SIZE];
  86. uint16_t ret;
  87. do {
  88. ret = fs_api->file.read(&manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
  89. for (uint16_t i=0; i < ret; i++) {
  90. if (buffer[i] == '\n' && string_size(line) > 0) {
  91. subghz_protocol_load_keeloq_file_process_line(instance, line);
  92. string_clean(line);
  93. } else {
  94. string_push_back(line, buffer[i]);
  95. }
  96. }
  97. } while(ret > 0);
  98. } else {
  99. printf("Manufacture keys file is not found: %s\r\n", file_name);
  100. }
  101. string_clear(line);
  102. fs_api->file.close(&manufacture_keys_file);
  103. furi_record_close("sdcard");
  104. }
  105. void subghz_protocol_reset(SubGhzProtocol* instance) {
  106. }
  107. void subghz_protocol_parse(SubGhzProtocol* instance, LevelPair data) {
  108. subghz_protocol_came_parse(instance->came, data);
  109. subghz_protocol_keeloq_parse(instance->keeloq, data);
  110. subghz_protocol_princeton_parse(instance->princeton, data);
  111. subghz_protocol_nice_flo_parse(instance->nice_flo, data);
  112. subghz_protocol_nice_flor_s_parse(instance->nice_flor_s, data);
  113. }