xremote_sg_remote.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "xremote_sg_remote.h"
  2. #define TAG "Xremote"
  3. struct SubGhzRemote {
  4. FuriString* name;
  5. FuriString* filename;
  6. FuriString* path;
  7. };
  8. const char* xremote_sg_remote_get_name(SubGhzRemote* remote) {
  9. return furi_string_get_cstr(remote->name);
  10. }
  11. const char* xremote_sg_remote_get_filename(SubGhzRemote* remote) {
  12. return furi_string_get_cstr(remote->filename);
  13. }
  14. SubGhzRemote* xremote_sg_remote_alloc() {
  15. SubGhzRemote* remote = malloc(sizeof(SubGhzRemote));
  16. remote->name = furi_string_alloc();
  17. remote->filename = furi_string_alloc();
  18. remote->path = furi_string_alloc();
  19. return remote;
  20. }
  21. void xremote_sg_remote_free(SubGhzRemote* remote) {
  22. furi_string_free(remote->path);
  23. furi_string_free(remote->name);
  24. furi_string_free(remote->filename);
  25. free(remote);
  26. }
  27. bool xremote_sg_remote_load(SubGhzRemote* remote, FuriString* path) {
  28. Storage* storage = furi_record_open(RECORD_STORAGE);
  29. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  30. FuriString* buf;
  31. buf = furi_string_alloc();
  32. FURI_LOG_I(TAG, "loading SG Remote: \'%s\'", furi_string_get_cstr(path));
  33. bool success = false;
  34. do {
  35. if(!flipper_format_buffered_file_open_existing(ff, furi_string_get_cstr(path))) break;
  36. const char* fullPath = furi_string_get_cstr(path);
  37. char* fileName = strrchr(fullPath, '/') + 1;
  38. char* dotPosition = strrchr(fileName, '.');
  39. if(dotPosition != NULL) { // check if there is a dot in the file name
  40. *dotPosition = '\0'; // set the dot position to NULL character to truncate the string
  41. }
  42. //remote->name = fileName;
  43. furi_string_set_str(remote->name, fileName);
  44. furi_string_set_str(remote->filename, fileName);
  45. uint32_t version;
  46. if(!flipper_format_read_header(ff, buf, &version)) break;
  47. if(!furi_string_equal(buf, "Flipper SubGhz RAW File") || (version != 1)) break;
  48. success = true;
  49. } while(false);
  50. furi_string_free(buf);
  51. flipper_format_buffered_file_close(ff);
  52. flipper_format_free(ff);
  53. furi_record_close(RECORD_STORAGE);
  54. return success;
  55. }