xremote_sg_remote.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. furi_string_set_str(remote->filename, fullPath);
  39. char* dotPosition = strrchr(fileName, '.');
  40. if(dotPosition != NULL) { // check if there is a dot in the file name
  41. *dotPosition = '\0'; // set the dot position to NULL character to truncate the string
  42. }
  43. //remote->name = fileName;
  44. furi_string_set_str(remote->name, fileName);
  45. //furi_string_set_str(remote->filename, fileName);
  46. //free(fileName);
  47. uint32_t version;
  48. if(!flipper_format_read_header(ff, buf, &version)) break;
  49. if(!furi_string_equal(buf, "Flipper SubGhz RAW File") || (version != 1)) break;
  50. success = true;
  51. } while(false);
  52. furi_string_free(buf);
  53. flipper_format_buffered_file_close(ff);
  54. flipper_format_free(ff);
  55. furi_record_close(RECORD_STORAGE);
  56. return success;
  57. }