xremote_remote.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "xremote_remote.h"
  2. ARRAY_DEF(CrossRemoteItemArray, CrossRemoteItem*, M_PTR_OPLIST);
  3. struct CrossRemote {
  4. FuriString* name;
  5. FuriString* path;
  6. CrossRemoteItemArray_t items;
  7. int transmitting;
  8. };
  9. static void xremote_cross_remote_clear_items(CrossRemote* remote) {
  10. CrossRemoteItemArray_it_t it;
  11. for(CrossRemoteItemArray_it(it, remote->items); !CrossRemoteItemArray_end_p(it);
  12. CrossRemoteItemArray_next(it)) {
  13. xremote_remote_item_free(*CrossRemoteItemArray_cref(it));
  14. }
  15. CrossRemoteItemArray_reset(remote->items);
  16. }
  17. static void xremote_cross_remote_find_vacant_remote_name(FuriString* name, const char* path) {
  18. Storage* storage = furi_record_open(RECORD_STORAGE);
  19. FuriString* base_path;
  20. base_path = furi_string_alloc_set(path);
  21. if(furi_string_end_with(base_path, XREMOTE_APP_EXTENSION)) {
  22. size_t filename_start = furi_string_search_rchar(base_path, '/');
  23. furi_string_left(base_path, filename_start);
  24. }
  25. furi_string_printf(
  26. base_path, "%s/%s%s", path, furi_string_get_cstr(name), XREMOTE_APP_EXTENSION);
  27. FS_Error status = storage_common_stat(storage, furi_string_get_cstr(base_path), NULL);
  28. if(status == FSE_OK) {
  29. // If name is taken, try another name2, name3 etc
  30. size_t dot = furi_string_search_rchar(base_path, '.');
  31. furi_string_left(base_path, dot);
  32. FuriString* path_temp;
  33. path_temp = furi_string_alloc();
  34. uint32_t i = 1;
  35. do {
  36. furi_string_printf(
  37. path_temp, "%s%lu%s", furi_string_get_cstr(base_path), ++i, XREMOTE_APP_EXTENSION);
  38. status = storage_common_stat(storage, furi_string_get_cstr(path_temp), NULL);
  39. } while(status == FSE_OK);
  40. furi_string_free(path_temp);
  41. if(status == FSE_NOT_EXIST) {
  42. furi_string_cat_printf(name, "%lu", i);
  43. }
  44. }
  45. furi_string_free(base_path);
  46. furi_record_close(RECORD_STORAGE);
  47. }
  48. CrossRemote* xremote_cross_remote_alloc() {
  49. CrossRemote* remote = malloc(sizeof(CrossRemote));
  50. CrossRemoteItemArray_init(remote->items);
  51. remote->name = furi_string_alloc();
  52. remote->path = furi_string_alloc();
  53. remote->transmitting = 0;
  54. return remote;
  55. }
  56. void xremote_cross_remote_set_transmitting(CrossRemote* remote, int status) {
  57. remote->transmitting = status;
  58. }
  59. int xremote_cross_remote_get_transmitting(CrossRemote* remote) {
  60. return remote->transmitting;
  61. }
  62. void xremote_cross_remote_free(CrossRemote* remote) {
  63. furi_string_free(remote->name);
  64. furi_string_free(remote->path);
  65. xremote_cross_remote_clear_items(remote);
  66. CrossRemoteItemArray_clear(remote->items);
  67. free(remote);
  68. }
  69. const char* xremote_cross_remote_get_name(CrossRemote* remote) {
  70. return furi_string_get_cstr(remote->name);
  71. }
  72. bool xremote_cross_remote_add_ir_item(CrossRemote* remote, const char* name, InfraredSignal* signal) {
  73. CrossRemoteItem* item = xremote_remote_item_alloc();
  74. xremote_remote_item_set_type(item, XRemoteRemoteItemTypeInfrared);
  75. xremote_remote_item_set_name(item, name);
  76. xremote_remote_item_set_ir_signal(item, signal);
  77. CrossRemoteItemArray_push_back(remote->items, item);
  78. return true;
  79. }
  80. bool xremote_cross_remote_add_pause(CrossRemote* remote, int time) {
  81. CrossRemoteItem* item = xremote_remote_item_alloc();
  82. xremote_remote_item_set_type(item, XRemoteRemoteItemTypePause);
  83. char name[9];
  84. snprintf(name, 9, CROSS_REMOTE_PAUSE_NAME, time);
  85. xremote_remote_item_set_name(item, name);
  86. xremote_remote_item_set_time(item, time);
  87. CrossRemoteItemArray_push_back(remote->items, item);
  88. return true;
  89. }
  90. bool xremote_cross_remote_add_subghz(CrossRemote* remote, SubGhzRemote* subghz) {
  91. UNUSED(subghz);
  92. CrossRemoteItem* item = xremote_remote_item_alloc();
  93. xremote_remote_item_set_type(item, XRemoteRemoteItemTypeSubGhz);
  94. xremote_remote_item_set_name(item, xremote_sg_remote_get_name(subghz));
  95. xremote_remote_item_set_sg_signal(item, subghz);
  96. CrossRemoteItemArray_push_back(remote->items, item);
  97. return true;
  98. }
  99. size_t xremote_cross_remote_get_item_count(CrossRemote* remote) {
  100. return CrossRemoteItemArray_size(remote->items);
  101. }
  102. CrossRemoteItem* xremote_cross_remote_get_item(CrossRemote* remote, size_t index) {
  103. furi_assert(index < CrossRemoteItemArray_size(remote->items));
  104. return *CrossRemoteItemArray_get(remote->items, index);
  105. }
  106. void xremote_cross_remote_remove_item(CrossRemote* remote, size_t index) {
  107. CrossRemoteItemArray_erase(remote->items, index);
  108. }
  109. void xremote_cross_remote_rename_item(CrossRemote* remote, size_t index, const char* name) {
  110. CrossRemoteItem* item = xremote_cross_remote_get_item(remote, index);
  111. xremote_remote_item_set_name(item, name);
  112. }
  113. static void xremote_cross_remote_set_name(CrossRemote* remote, const char* name) {
  114. furi_string_set(remote->name, name);
  115. }
  116. static void xremote_cross_remote_set_path(CrossRemote* remote, const char* path) {
  117. furi_string_set(remote->path, path);
  118. }
  119. bool xremote_cross_remote_load(CrossRemote* remote, FuriString* path) {
  120. Storage* storage = furi_record_open(RECORD_STORAGE);
  121. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  122. FuriString* buf;
  123. buf = furi_string_alloc();
  124. FURI_LOG_I(TAG, "loading file: \'%s\'", furi_string_get_cstr(path));
  125. bool success = false;
  126. do {
  127. // File not found
  128. if(!flipper_format_buffered_file_open_existing(ff, furi_string_get_cstr(path))) break;
  129. uint32_t version;
  130. // Read Version & Type
  131. if(!flipper_format_read_header(ff, buf, &version)) break;
  132. if(!furi_string_equal(buf, XREMOTE_FILE_TYPE) || (version != XREMOTE_FILE_VERSION)) break;
  133. // Init Remote
  134. path_extract_filename(path, buf, true);
  135. xremote_cross_remote_clear_items(remote);
  136. xremote_cross_remote_set_name(remote, furi_string_get_cstr(buf));
  137. xremote_cross_remote_set_path(remote, furi_string_get_cstr(path));
  138. // Load Items
  139. for(bool can_read = true; can_read;) {
  140. CrossRemoteItem* item = xremote_remote_item_alloc();
  141. can_read = xremote_remote_item_read(item, ff);
  142. if(can_read) {
  143. CrossRemoteItemArray_push_back(remote->items, item);
  144. } else {
  145. xremote_remote_item_free(item);
  146. }
  147. }
  148. success = true;
  149. } while(false);
  150. furi_string_free(buf);
  151. flipper_format_buffered_file_close(ff);
  152. flipper_format_free(ff);
  153. furi_record_close(RECORD_STORAGE);
  154. return success;
  155. }
  156. static bool xremote_cross_remote_store(CrossRemote* remote) {
  157. Storage* storage = furi_record_open(RECORD_STORAGE);
  158. FlipperFormat* ff = flipper_format_file_alloc(storage);
  159. const char* path = furi_string_get_cstr(remote->path);
  160. FURI_LOG_I(TAG, "Storing file: \'%s\'", path);
  161. bool success = flipper_format_file_open_always(ff, path) &&
  162. flipper_format_write_header_cstr(ff, XREMOTE_FILE_TYPE, XREMOTE_FILE_VERSION);
  163. // save Items
  164. if(success) {
  165. CrossRemoteItemArray_it_t it;
  166. for(CrossRemoteItemArray_it(it, remote->items); !CrossRemoteItemArray_end_p(it);
  167. CrossRemoteItemArray_next(it)) {
  168. CrossRemoteItem* item = *CrossRemoteItemArray_cref(it);
  169. success = false;
  170. if(item->type == XRemoteRemoteItemTypeInfrared) {
  171. success = xremote_ir_signal_save(
  172. xremote_remote_item_get_ir_signal(item),
  173. ff,
  174. xremote_remote_item_get_name(item));
  175. } else if(item->type == XRemoteRemoteItemTypePause) {
  176. success = xremote_pause_save(ff, item->time, xremote_remote_item_get_name(item));
  177. } else if(item->type == XRemoteRemoteItemTypeSubGhz) {
  178. success = xremote_sg_signal_save(
  179. xremote_remote_item_get_sg_signal(item),
  180. ff,
  181. xremote_remote_item_get_name(item));
  182. }
  183. if(!success) {
  184. break;
  185. }
  186. }
  187. }
  188. flipper_format_free(ff);
  189. furi_record_close(RECORD_STORAGE);
  190. return success;
  191. }
  192. bool xremote_cross_remote_save_new(CrossRemote* remote, const char* name) {
  193. FuriString *new_name, *new_path;
  194. new_name = furi_string_alloc_set(name);
  195. new_path = furi_string_alloc_set(XREMOTE_APP_FOLDER);
  196. xremote_cross_remote_find_vacant_remote_name(new_name, furi_string_get_cstr(new_path));
  197. furi_string_cat_printf(
  198. new_path, "/%s%s", furi_string_get_cstr(new_name), XREMOTE_APP_EXTENSION);
  199. xremote_cross_remote_set_name(remote, furi_string_get_cstr(new_name));
  200. xremote_cross_remote_set_path(remote, furi_string_get_cstr(new_path));
  201. furi_string_free(new_name);
  202. furi_string_free(new_path);
  203. return xremote_cross_remote_store(remote);
  204. }
  205. static void xremote_cross_remote_reset(CrossRemote* remote) {
  206. furi_string_reset(remote->name);
  207. furi_string_reset(remote->path);
  208. CrossRemoteItemArray_clear(remote->items);
  209. remote->transmitting = 0;
  210. }
  211. bool xremote_cross_remote_delete(CrossRemote* remote) {
  212. Storage* storage = furi_record_open(RECORD_STORAGE);
  213. FS_Error status = storage_common_remove(storage, furi_string_get_cstr(remote->path));
  214. xremote_cross_remote_reset(remote);
  215. furi_record_close(RECORD_STORAGE);
  216. return (status == FSE_OK || status == FSE_NOT_EXIST);
  217. }