action.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "quac.h"
  2. #include "item.h"
  3. #include "action_i.h"
  4. #define MAX_FILE_LEN (size_t)256
  5. void action_ql_resolve(
  6. void* context,
  7. const FuriString* action_path,
  8. FuriString* new_path,
  9. FuriString* error) {
  10. UNUSED(error);
  11. App* app = (App*)context;
  12. File* file_link = storage_file_alloc(app->storage);
  13. do {
  14. if(!storage_file_open(
  15. file_link, furi_string_get_cstr(action_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  16. ACTION_SET_ERROR(
  17. "Quac Link: Failed to open file %s", furi_string_get_cstr(action_path));
  18. break;
  19. }
  20. furi_string_reset(new_path);
  21. const size_t file_size = storage_file_size(file_link);
  22. size_t bytes_read = 0;
  23. while(bytes_read < file_size) {
  24. char buffer[65] = {0};
  25. const size_t chunk_size = MIN(file_size - bytes_read, sizeof(buffer) - 1);
  26. size_t chunk_read = storage_file_read(file_link, buffer, chunk_size);
  27. if(chunk_read != chunk_size) break;
  28. bytes_read += chunk_read;
  29. furi_string_cat_str(new_path, buffer);
  30. }
  31. furi_string_trim(new_path);
  32. if(bytes_read != file_size) {
  33. ACTION_SET_ERROR(
  34. "Quac Link: Error reading link file %s", furi_string_get_cstr(action_path));
  35. break;
  36. }
  37. if(!storage_file_exists(app->storage, furi_string_get_cstr(new_path))) {
  38. ACTION_SET_ERROR(
  39. "Quac Link: Linked file does not exist! %s", furi_string_get_cstr(new_path));
  40. break;
  41. }
  42. } while(false);
  43. storage_file_close(file_link);
  44. storage_file_free(file_link);
  45. }
  46. void action_tx(void* context, Item* item, FuriString* error) {
  47. // FURI_LOG_I(TAG, "action_run: %s : %s", furi_string_get_cstr(item->name), item->ext);
  48. FuriString* path = furi_string_alloc_set(item->path);
  49. if(item->is_link) {
  50. // This is a Quac link, open the file and pull the real filename
  51. action_ql_resolve(context, item->path, path, error);
  52. if(furi_string_size(error)) {
  53. furi_string_free(path);
  54. return;
  55. }
  56. FURI_LOG_I(TAG, "Resolved Quac link file to: %s", furi_string_get_cstr(path));
  57. }
  58. if(!strcmp(item->ext, ".sub")) {
  59. action_subghz_tx(context, path, error);
  60. } else if(!strcmp(item->ext, ".ir")) {
  61. action_ir_tx(context, path, error);
  62. } else if(!strcmp(item->ext, ".rfid")) {
  63. action_rfid_tx(context, path, error);
  64. } else if(!strcmp(item->ext, ".nfc")) {
  65. action_nfc_tx(context, path, error);
  66. } else if(!strcmp(item->ext, ".ibtn")) {
  67. action_ibutton_tx(context, path, error);
  68. } else if(!strcmp(item->ext, ".qpl")) {
  69. action_qpl_tx(context, path, error);
  70. } else {
  71. FURI_LOG_E(TAG, "Unknown item file type! %s", item->ext);
  72. }
  73. furi_string_free(path);
  74. }