xremote_cross_remote_item.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "xremote_cross_remote_item.h"
  2. CrossRemoteItem* xremote_cross_remote_item_alloc() {
  3. CrossRemoteItem* item = malloc(sizeof(CrossRemoteItem));
  4. item->name = furi_string_alloc();
  5. item->filename = furi_string_alloc();
  6. item->time = 0;
  7. item->type = 0;
  8. item->ir_signal = xremote_ir_signal_alloc();
  9. item->sg_signal = xremote_sg_remote_alloc();
  10. return item;
  11. }
  12. static inline bool xremote_ir_signal_save_message(InfraredMessage* message, FlipperFormat* ff) {
  13. const char* protocol_name = infrared_get_protocol_name(message->protocol);
  14. return flipper_format_write_string_cstr(ff, "type", "parsed") &&
  15. flipper_format_write_string_cstr(ff, "protocol", protocol_name) &&
  16. flipper_format_write_hex(ff, "address", (uint8_t*)&message->address, 4) &&
  17. flipper_format_write_hex(ff, "command", (uint8_t*)&message->command, 4);
  18. }
  19. static inline bool xremote_ir_signal_save_raw(InfraredRawSignal* raw, FlipperFormat* ff) {
  20. furi_assert(raw->timings_size <= MAX_TIMINGS_AMOUNT);
  21. return flipper_format_write_string_cstr(ff, "type", "raw") &&
  22. flipper_format_write_uint32(ff, "frequency", &raw->frequency, 1) &&
  23. flipper_format_write_float(ff, "duty_cycle", &raw->duty_cycle, 1) &&
  24. flipper_format_write_uint32(ff, "data", raw->timings, raw->timings_size);
  25. }
  26. static inline bool xremote_sg_signal_save_data(SubGhzRemote* remote, FlipperFormat* ff) {
  27. UNUSED(remote);
  28. UNUSED(ff);
  29. return true;
  30. }
  31. static bool xremote_ir_signal_is_message_valid(InfraredMessage* message) {
  32. if(!infrared_is_protocol_valid(message->protocol)) {
  33. FURI_LOG_E(TAG, "Unknown protocol");
  34. return false;
  35. }
  36. uint32_t address_length = infrared_get_protocol_address_length(message->protocol);
  37. uint32_t address_mask = (1UL << address_length) - 1;
  38. if(message->address != (message->address & address_mask)) {
  39. FURI_LOG_E(
  40. TAG,
  41. "Address is out of range (mask 0x%08lX): 0x%lX\r\n",
  42. address_mask,
  43. message->address);
  44. return false;
  45. }
  46. uint32_t command_length = infrared_get_protocol_command_length(message->protocol);
  47. uint32_t command_mask = (1UL << command_length) - 1;
  48. if(message->command != (message->command & command_mask)) {
  49. FURI_LOG_E(
  50. TAG,
  51. "Command is out of range (mask 0x%08lX): 0x%lX\r\n",
  52. command_mask,
  53. message->command);
  54. return false;
  55. }
  56. return true;
  57. }
  58. static bool xremote_cross_remote_item_read_sg(CrossRemoteItem* item, FlipperFormat* ff) {
  59. FuriString* buf;
  60. bool success = false;
  61. buf = furi_string_alloc();
  62. item->type = XRemoteRemoteItemTypeSubGhz;
  63. item->time = 0;
  64. do {
  65. success = flipper_format_read_string(ff, "name", item->name) &&
  66. flipper_format_read_string(ff, "filename", item->filename);
  67. if(!success) break;
  68. } while(false);
  69. furi_string_free(buf);
  70. FURI_LOG_D(TAG, "final name: %s", furi_string_get_cstr(item->name));
  71. FURI_LOG_D(TAG, "final filename: %s", furi_string_get_cstr(item->filename));
  72. return success;
  73. }
  74. static bool xremote_cross_remote_item_read_ir_signal_raw(CrossRemoteItem* item, FlipperFormat* ff) {
  75. uint32_t timings_size, frequency;
  76. float duty_cycle;
  77. bool success = flipper_format_read_uint32(ff, "frequency", &frequency, 1) &&
  78. flipper_format_read_float(ff, "duty_cycle", &duty_cycle, 1) &&
  79. flipper_format_get_value_count(ff, "data", &timings_size);
  80. if(!success || timings_size > MAX_TIMINGS_AMOUNT) {
  81. return false;
  82. }
  83. uint32_t* timings = malloc(sizeof(uint32_t) * timings_size);
  84. success = flipper_format_read_uint32(ff, "data", timings, timings_size);
  85. if(success) {
  86. xremote_ir_signal_set_raw_signal(
  87. item->ir_signal, timings, timings_size, frequency, duty_cycle);
  88. }
  89. free(timings);
  90. return success;
  91. }
  92. static bool xremote_cross_remote_item_read_message(CrossRemoteItem* item, FlipperFormat* ff) {
  93. FuriString* buf = furi_string_alloc();
  94. bool success = false;
  95. do {
  96. if(!flipper_format_read_string(ff, "protocol", buf)) break;
  97. InfraredMessage message;
  98. message.protocol = infrared_get_protocol_by_name(furi_string_get_cstr(buf));
  99. success = flipper_format_read_hex(ff, "address", (uint8_t*)&message.address, 4) &&
  100. flipper_format_read_hex(ff, "command", (uint8_t*)&message.command, 4) &&
  101. xremote_ir_signal_is_message_valid(&message);
  102. if(!success) break;
  103. xremote_ir_signal_set_message(item->ir_signal, &message);
  104. } while(false);
  105. furi_string_free(buf);
  106. return success;
  107. }
  108. static bool xremote_cross_remote_item_read_ir(CrossRemoteItem* item, FlipperFormat* ff) {
  109. FuriString* buf;
  110. bool success = false;
  111. buf = furi_string_alloc();
  112. item->type = XRemoteRemoteItemTypeInfrared;
  113. item->time = 0;
  114. do {
  115. if(!flipper_format_read_string(ff, "name", item->name)) break;
  116. if(!flipper_format_read_uint32(ff, "time", &item->time, 1)) item->time = 1000;
  117. if(!flipper_format_read_string(ff, "type", buf)) break;
  118. if(furi_string_equal(buf, "raw")) {
  119. if(!xremote_cross_remote_item_read_ir_signal_raw(item, ff)) break;
  120. } else if(furi_string_equal(buf, "parsed")) {
  121. if(!xremote_cross_remote_item_read_message(item, ff)) break;
  122. } else {
  123. break;
  124. }
  125. success = true;
  126. } while(false);
  127. furi_string_free(buf);
  128. return success;
  129. }
  130. static bool xremote_cross_remote_item_read_pause(CrossRemoteItem* item, FlipperFormat* ff) {
  131. bool success = false;
  132. item->type = XRemoteRemoteItemTypePause;
  133. do {
  134. if(!flipper_format_read_string(ff, "name", item->name)) break;
  135. if(!flipper_format_read_uint32(ff, "time", &item->time, 1)) break;
  136. success = true;
  137. } while(false);
  138. return success;
  139. }
  140. bool xremote_cross_remote_item_read(CrossRemoteItem* item, FlipperFormat* ff) {
  141. FuriString* type = furi_string_alloc();
  142. bool success = false;
  143. do {
  144. if(!flipper_format_read_string(ff, "remote_type", type)) break;
  145. if(furi_string_equal(type, "IR")) {
  146. success = xremote_cross_remote_item_read_ir(item, ff);
  147. } else if(furi_string_equal(type, "SG")) {
  148. success = xremote_cross_remote_item_read_sg(item, ff);
  149. } else if(furi_string_equal(type, "PAUSE")) {
  150. success = xremote_cross_remote_item_read_pause(item, ff);
  151. } else {
  152. break;
  153. }
  154. success = true;
  155. } while(false);
  156. furi_string_free(type);
  157. return success;
  158. }
  159. void xremote_cross_remote_item_free(CrossRemoteItem* item) {
  160. furi_string_free(item->name);
  161. furi_string_free(item->filename);
  162. //Determine type before free
  163. xremote_ir_signal_free(item->ir_signal);
  164. xremote_sg_remote_free(item->sg_signal);
  165. free(item);
  166. }
  167. void xremote_cross_remote_item_set_type(CrossRemoteItem* item, int type) {
  168. item->type = type;
  169. }
  170. void xremote_cross_remote_item_set_name(CrossRemoteItem* item, const char* name) {
  171. furi_string_set(item->name, name);
  172. }
  173. void xremote_cross_remote_item_set_filename(CrossRemoteItem* item, const char* filename) {
  174. furi_string_set(item->filename, filename);
  175. }
  176. void xremote_cross_remote_item_set_time(CrossRemoteItem* item, uint32_t time) {
  177. item->time = time;
  178. }
  179. void xremote_cross_remote_item_set_ir_signal(CrossRemoteItem* item, InfraredSignal* signal) {
  180. xremote_ir_signal_set_signal(item->ir_signal, signal);
  181. }
  182. void xremote_cross_remote_item_set_sg_signal(CrossRemoteItem* item, SubGhzRemote* subghz) {
  183. item->sg_signal = subghz;
  184. }
  185. const char* xremote_cross_remote_item_get_name(CrossRemoteItem* item) {
  186. return furi_string_get_cstr(item->name);
  187. }
  188. const char* xremote_cross_remote_item_get_filename(CrossRemoteItem* item) {
  189. return furi_string_get_cstr(item->filename);
  190. }
  191. InfraredSignal* xremote_cross_remote_item_get_ir_signal(CrossRemoteItem* item) {
  192. return item->ir_signal;
  193. }
  194. SubGhzRemote* xremote_cross_remote_item_get_sg_signal(CrossRemoteItem* item) {
  195. return item->sg_signal;
  196. }
  197. uint32_t xremote_cross_remote_item_get_time(CrossRemoteItem* item) {
  198. return item->time;
  199. }
  200. bool xremote_cross_remote_item_ir_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name, uint32_t time) {
  201. if(!flipper_format_write_comment_cstr(ff, "") ||
  202. !flipper_format_write_string_cstr(ff, "remote_type", "IR") ||
  203. !flipper_format_write_string_cstr(ff, "name", name) ||
  204. !flipper_format_write_uint32(ff, "time", &time, 1)) {
  205. return false;
  206. } else if(signal->is_raw) {
  207. return xremote_ir_signal_save_raw(&signal->payload.raw, ff);
  208. } else {
  209. return xremote_ir_signal_save_message(&signal->payload.message, ff);
  210. }
  211. }
  212. bool xremote_cross_remote_item_pause_save(FlipperFormat* ff, uint32_t time, const char* name) {
  213. if(!flipper_format_write_comment_cstr(ff, "") ||
  214. !flipper_format_write_string_cstr(ff, "remote_type", "PAUSE") ||
  215. !flipper_format_write_string_cstr(ff, "name", name) ||
  216. !flipper_format_write_uint32(ff, "time", &time, 1)) {
  217. return false;
  218. }
  219. return true;
  220. }
  221. bool xremote_cross_remote_item_sg_signal_save(SubGhzRemote* remote, FlipperFormat* ff, const char* name, const char* filename) {
  222. if(!flipper_format_write_comment_cstr(ff, "") ||
  223. !flipper_format_write_string_cstr(ff, "remote_type", "SG") ||
  224. !flipper_format_write_string_cstr(ff, "name", name) ||
  225. !flipper_format_write_string_cstr(ff, "filename", filename)) {
  226. return false;
  227. }
  228. return xremote_sg_signal_save_data(remote, ff);
  229. }