subghz_keystore.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "subghz_keystore.h"
  2. #include <furi.h>
  3. #include <filesystem-api.h>
  4. #define FILE_BUFFER_SIZE 64
  5. struct SubGhzKeystore {
  6. SubGhzKeyArray_t data;
  7. };
  8. SubGhzKeystore* subghz_keystore_alloc() {
  9. SubGhzKeystore* instance = furi_alloc(sizeof(SubGhzKeystore));
  10. SubGhzKeyArray_init(instance->data);
  11. return instance;
  12. }
  13. void subghz_keystore_free(SubGhzKeystore* instance) {
  14. furi_assert(instance);
  15. for
  16. M_EACH(manufacture_code, instance->data, SubGhzKeyArray_t) {
  17. string_clear(manufacture_code->name);
  18. manufacture_code->key = 0;
  19. }
  20. SubGhzKeyArray_clear(instance->data);
  21. free(instance);
  22. }
  23. static void subghz_keystore_add_key(SubGhzKeystore* instance, const char* name, uint64_t key, uint16_t type) {
  24. SubGhzKey* manufacture_code = SubGhzKeyArray_push_raw(instance->data);
  25. string_init_set_str(manufacture_code->name, name);
  26. manufacture_code->key = key;
  27. manufacture_code->type = type;
  28. }
  29. static void subghz_keystore_process_line(SubGhzKeystore* instance, string_t line) {
  30. uint64_t key = 0;
  31. uint16_t type = 0;
  32. char skey[17] = {0};
  33. char name[65] = {0};
  34. int ret = sscanf(string_get_cstr(line), "%16s:%hu:%64s", skey, &type, name);
  35. key = strtoull(skey, NULL, 16);
  36. if (ret == 3) {
  37. subghz_keystore_add_key(instance, name, key, type);
  38. } else {
  39. printf("Failed to load line: %s\r\n", string_get_cstr(line));
  40. }
  41. }
  42. void subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
  43. File manufacture_keys_file;
  44. FS_Api* fs_api = furi_record_open("sdcard");
  45. fs_api->file.open(&manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING);
  46. string_t line;
  47. string_init(line);
  48. if(manufacture_keys_file.error_id == FSE_OK) {
  49. printf("Loading manufacture keys file %s\r\n", file_name);
  50. char buffer[FILE_BUFFER_SIZE];
  51. uint16_t ret;
  52. do {
  53. ret = fs_api->file.read(&manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
  54. for (uint16_t i=0; i < ret; i++) {
  55. if (buffer[i] == '\n' && string_size(line) > 0) {
  56. subghz_keystore_process_line(instance, line);
  57. string_clean(line);
  58. } else {
  59. string_push_back(line, buffer[i]);
  60. }
  61. }
  62. } while(ret > 0);
  63. } else {
  64. printf("Manufacture keys file is not found: %s\r\n", file_name);
  65. }
  66. string_clear(line);
  67. fs_api->file.close(&manufacture_keys_file);
  68. furi_record_close("sdcard");
  69. }
  70. SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
  71. furi_assert(instance);
  72. return &instance->data;
  73. }