subghz_keystore.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "subghz_keystore.h"
  2. #include <furi.h>
  3. #include <storage/storage.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 = storage_file_alloc(furi_record_open("storage"));
  44. string_t line;
  45. string_init(line);
  46. if(storage_file_open(manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING)) {
  47. printf("Loading manufacture keys file %s\r\n", file_name);
  48. char buffer[FILE_BUFFER_SIZE];
  49. uint16_t ret;
  50. do {
  51. ret = storage_file_read(manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
  52. for (uint16_t i=0; i < ret; i++) {
  53. if (buffer[i] == '\n' && string_size(line) > 0) {
  54. subghz_keystore_process_line(instance, line);
  55. string_clean(line);
  56. } else {
  57. string_push_back(line, buffer[i]);
  58. }
  59. }
  60. } while(ret > 0);
  61. } else {
  62. printf("Manufacture keys file is not found: %s\r\n", file_name);
  63. }
  64. string_clear(line);
  65. storage_file_close(manufacture_keys_file);
  66. storage_file_free(manufacture_keys_file);
  67. furi_record_close("storage");
  68. }
  69. SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
  70. furi_assert(instance);
  71. return &instance->data;
  72. }