xremote_sg_remote.c 2.0 KB

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