xremote_cross_remote.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "xremote_cross_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_cross_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, uint32_t timing) {
  73. CrossRemoteItem* item = xremote_cross_remote_item_alloc();
  74. xremote_cross_remote_item_set_type(item, XRemoteRemoteItemTypeInfrared);
  75. xremote_cross_remote_item_set_name(item, name);
  76. xremote_cross_remote_item_set_time(item, timing);
  77. xremote_cross_remote_item_set_ir_signal(item, signal);
  78. CrossRemoteItemArray_push_back(remote->items, item);
  79. return true;
  80. }
  81. bool xremote_cross_remote_add_pause(CrossRemote* remote, int time) {
  82. CrossRemoteItem* item = xremote_cross_remote_item_alloc();
  83. xremote_cross_remote_item_set_type(item, XRemoteRemoteItemTypePause);
  84. char name[9];
  85. snprintf(name, 9, CROSS_REMOTE_PAUSE_NAME, time);
  86. xremote_cross_remote_item_set_name(item, name);
  87. xremote_cross_remote_item_set_time(item, time);
  88. CrossRemoteItemArray_push_back(remote->items, item);
  89. return true;
  90. }
  91. bool xremote_cross_remote_add_subghz(CrossRemote* remote, SubGhzRemote* subghz) {
  92. UNUSED(subghz);
  93. CrossRemoteItem* item = xremote_cross_remote_item_alloc();
  94. xremote_cross_remote_item_set_type(item, XRemoteRemoteItemTypeSubGhz);
  95. xremote_cross_remote_item_set_name(item, xremote_sg_remote_get_name(subghz));
  96. xremote_cross_remote_item_set_sg_signal(item, subghz);
  97. CrossRemoteItemArray_push_back(remote->items, item);
  98. return true;
  99. }
  100. size_t xremote_cross_remote_get_item_count(CrossRemote* remote) {
  101. return CrossRemoteItemArray_size(remote->items);
  102. }
  103. CrossRemoteItem* xremote_cross_remote_get_item(CrossRemote* remote, size_t index) {
  104. furi_assert(index < CrossRemoteItemArray_size(remote->items));
  105. return *CrossRemoteItemArray_get(remote->items, index);
  106. }
  107. void xremote_cross_remote_remove_item(CrossRemote* remote, size_t index) {
  108. CrossRemoteItemArray_erase(remote->items, index);
  109. }
  110. void xremote_cross_remote_rename_item(CrossRemote* remote, size_t index, const char* name) {
  111. CrossRemoteItem* item = xremote_cross_remote_get_item(remote, index);
  112. xremote_cross_remote_item_set_name(item, name);
  113. }
  114. static void xremote_cross_remote_set_name(CrossRemote* remote, const char* name) {
  115. furi_string_set(remote->name, name);
  116. }
  117. static void xremote_cross_remote_set_path(CrossRemote* remote, const char* path) {
  118. furi_string_set(remote->path, path);
  119. }
  120. bool xremote_cross_remote_load(CrossRemote* remote, FuriString* path) {
  121. Storage* storage = furi_record_open(RECORD_STORAGE);
  122. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  123. FuriString* buf;
  124. buf = furi_string_alloc();
  125. FURI_LOG_I(TAG, "loading file: \'%s\'", furi_string_get_cstr(path));
  126. bool success = false;
  127. do {
  128. // File not found
  129. if(!flipper_format_buffered_file_open_existing(ff, furi_string_get_cstr(path))) break;
  130. uint32_t version;
  131. // Read Version & Type
  132. if(!flipper_format_read_header(ff, buf, &version)) break;
  133. if(!furi_string_equal(buf, XREMOTE_FILE_TYPE) || (version != XREMOTE_FILE_VERSION)) break;
  134. // Init Remote
  135. path_extract_filename(path, buf, true);
  136. xremote_cross_remote_clear_items(remote);
  137. xremote_cross_remote_set_name(remote, furi_string_get_cstr(buf));
  138. xremote_cross_remote_set_path(remote, furi_string_get_cstr(path));
  139. // Load Items
  140. for(bool can_read = true; can_read;) {
  141. CrossRemoteItem* item = xremote_cross_remote_item_alloc();
  142. can_read = xremote_cross_remote_item_read(item, ff);
  143. if(can_read) {
  144. CrossRemoteItemArray_push_back(remote->items, item);
  145. } else {
  146. xremote_cross_remote_item_free(item);
  147. }
  148. }
  149. success = true;
  150. } while(false);
  151. furi_string_free(buf);
  152. flipper_format_buffered_file_close(ff);
  153. flipper_format_free(ff);
  154. furi_record_close(RECORD_STORAGE);
  155. return success;
  156. }
  157. static bool xremote_cross_remote_store(CrossRemote* remote) {
  158. Storage* storage = furi_record_open(RECORD_STORAGE);
  159. FlipperFormat* ff = flipper_format_file_alloc(storage);
  160. const char* path = furi_string_get_cstr(remote->path);
  161. FURI_LOG_I(TAG, "Storing file: \'%s\'", path);
  162. bool success = flipper_format_file_open_always(ff, path) &&
  163. flipper_format_write_header_cstr(ff, XREMOTE_FILE_TYPE, XREMOTE_FILE_VERSION);
  164. // save Items
  165. if(success) {
  166. CrossRemoteItemArray_it_t it;
  167. for(CrossRemoteItemArray_it(it, remote->items); !CrossRemoteItemArray_end_p(it);
  168. CrossRemoteItemArray_next(it)) {
  169. CrossRemoteItem* item = *CrossRemoteItemArray_cref(it);
  170. success = false;
  171. if(item->type == XRemoteRemoteItemTypeInfrared) {
  172. success = xremote_cross_remote_item_ir_signal_save(
  173. xremote_cross_remote_item_get_ir_signal(item),
  174. ff,
  175. xremote_cross_remote_item_get_name(item),
  176. xremote_cross_remote_item_get_time(item));
  177. } else if(item->type == XRemoteRemoteItemTypePause) {
  178. success = xremote_cross_remote_item_pause_save(ff, item->time, xremote_cross_remote_item_get_name(item));
  179. } else if(item->type == XRemoteRemoteItemTypeSubGhz) {
  180. success = xremote_cross_remote_item_sg_signal_save(
  181. xremote_cross_remote_item_get_sg_signal(item),
  182. ff,
  183. xremote_cross_remote_item_get_name(item));
  184. }
  185. if(!success) {
  186. break;
  187. }
  188. }
  189. }
  190. flipper_format_free(ff);
  191. furi_record_close(RECORD_STORAGE);
  192. return success;
  193. }
  194. bool xremote_cross_remote_save_new(CrossRemote* remote, const char* name) {
  195. FuriString *new_name, *new_path;
  196. new_name = furi_string_alloc_set(name);
  197. new_path = furi_string_alloc_set(XREMOTE_APP_FOLDER);
  198. xremote_cross_remote_find_vacant_remote_name(new_name, furi_string_get_cstr(new_path));
  199. furi_string_cat_printf(
  200. new_path, "/%s%s", furi_string_get_cstr(new_name), XREMOTE_APP_EXTENSION);
  201. xremote_cross_remote_set_name(remote, furi_string_get_cstr(new_name));
  202. xremote_cross_remote_set_path(remote, furi_string_get_cstr(new_path));
  203. furi_string_free(new_name);
  204. furi_string_free(new_path);
  205. return xremote_cross_remote_store(remote);
  206. }
  207. static void xremote_cross_remote_reset(CrossRemote* remote) {
  208. furi_string_reset(remote->name);
  209. furi_string_reset(remote->path);
  210. CrossRemoteItemArray_clear(remote->items);
  211. remote->transmitting = 0;
  212. }
  213. bool xremote_cross_remote_delete(CrossRemote* remote) {
  214. Storage* storage = furi_record_open(RECORD_STORAGE);
  215. FS_Error status = storage_common_remove(storage, furi_string_get_cstr(remote->path));
  216. xremote_cross_remote_reset(remote);
  217. furi_record_close(RECORD_STORAGE);
  218. return (status == FSE_OK || status == FSE_NOT_EXIST);
  219. }