xremote_cross_remote_item.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. if(!flipper_format_read_string(ff, "name", item->name)) break;
  66. if(!flipper_format_read_string(ff, "filename", item->filename)) break;
  67. success = true;
  68. } while(false);
  69. furi_string_free(buf);
  70. return success;
  71. }
  72. static bool xremote_cross_remote_item_read_ir_signal_raw(CrossRemoteItem* item, FlipperFormat* ff) {
  73. uint32_t timings_size, frequency;
  74. float duty_cycle;
  75. bool success = flipper_format_read_uint32(ff, "frequency", &frequency, 1) &&
  76. flipper_format_read_float(ff, "duty_cycle", &duty_cycle, 1) &&
  77. flipper_format_get_value_count(ff, "data", &timings_size);
  78. if(!success || timings_size > MAX_TIMINGS_AMOUNT) {
  79. return false;
  80. }
  81. uint32_t* timings = malloc(sizeof(uint32_t) * timings_size);
  82. success = flipper_format_read_uint32(ff, "data", timings, timings_size);
  83. if(success) {
  84. xremote_ir_signal_set_raw_signal(
  85. item->ir_signal, timings, timings_size, frequency, duty_cycle);
  86. }
  87. free(timings);
  88. return success;
  89. }
  90. static bool xremote_cross_remote_item_read_message(CrossRemoteItem* item, FlipperFormat* ff) {
  91. FuriString* buf = furi_string_alloc();
  92. bool success = false;
  93. do {
  94. if(!flipper_format_read_string(ff, "protocol", buf)) break;
  95. InfraredMessage message;
  96. message.protocol = infrared_get_protocol_by_name(furi_string_get_cstr(buf));
  97. success = flipper_format_read_hex(ff, "address", (uint8_t*)&message.address, 4) &&
  98. flipper_format_read_hex(ff, "command", (uint8_t*)&message.command, 4) &&
  99. xremote_ir_signal_is_message_valid(&message);
  100. if(!success) break;
  101. xremote_ir_signal_set_message(item->ir_signal, &message);
  102. } while(false);
  103. furi_string_free(buf);
  104. return success;
  105. }
  106. static bool xremote_cross_remote_item_read_ir(CrossRemoteItem* item, FlipperFormat* ff) {
  107. FuriString* buf;
  108. bool success = false;
  109. buf = furi_string_alloc();
  110. item->type = XRemoteRemoteItemTypeInfrared;
  111. item->time = 0;
  112. do {
  113. if(!flipper_format_read_string(ff, "name", item->name)) break;
  114. if(!flipper_format_read_uint32(ff, "time", &item->time, 1)) item->time = 1000;
  115. if(!flipper_format_read_string(ff, "type", buf)) break;
  116. if(furi_string_equal(buf, "raw")) {
  117. if(!xremote_cross_remote_item_read_ir_signal_raw(item, ff)) break;
  118. } else if(furi_string_equal(buf, "parsed")) {
  119. if(!xremote_cross_remote_item_read_message(item, ff)) break;
  120. } else {
  121. break;
  122. }
  123. success = true;
  124. } while(false);
  125. furi_string_free(buf);
  126. return success;
  127. }
  128. static bool xremote_cross_remote_item_read_pause(CrossRemoteItem* item, FlipperFormat* ff) {
  129. bool success = false;
  130. item->type = XRemoteRemoteItemTypePause;
  131. do {
  132. if(!flipper_format_read_string(ff, "name", item->name)) break;
  133. if(!flipper_format_read_uint32(ff, "time", &item->time, 1)) break;
  134. success = true;
  135. } while(false);
  136. return success;
  137. }
  138. bool xremote_cross_remote_item_read(CrossRemoteItem* item, FlipperFormat* ff) {
  139. FuriString* type = furi_string_alloc();
  140. bool success = false;
  141. do {
  142. if(!flipper_format_read_string(ff, "remote_type", type)) break;
  143. if(furi_string_equal(type, "IR")) {
  144. success = xremote_cross_remote_item_read_ir(item, ff);
  145. } else if(furi_string_equal(type, "SG")) {
  146. success = xremote_cross_remote_item_read_sg(item, ff);
  147. } else if(furi_string_equal(type, "PAUSE")) {
  148. success = xremote_cross_remote_item_read_pause(item, ff);
  149. } else {
  150. break;
  151. }
  152. success = true;
  153. } while(false);
  154. furi_string_free(type);
  155. return success;
  156. }
  157. void xremote_cross_remote_item_free(CrossRemoteItem* item) {
  158. furi_string_free(item->name);
  159. furi_string_free(item->filename);
  160. //Determine type before free
  161. xremote_ir_signal_free(item->ir_signal);
  162. xremote_sg_remote_free(item->sg_signal);
  163. free(item);
  164. }
  165. void xremote_cross_remote_item_set_type(CrossRemoteItem* item, int type) {
  166. item->type = type;
  167. }
  168. void xremote_cross_remote_item_set_name(CrossRemoteItem* item, const char* name) {
  169. furi_string_set(item->name, name);
  170. }
  171. void xremote_cross_remote_item_set_time(CrossRemoteItem* item, uint32_t time) {
  172. item->time = time;
  173. }
  174. void xremote_cross_remote_item_set_ir_signal(CrossRemoteItem* item, InfraredSignal* signal) {
  175. xremote_ir_signal_set_signal(item->ir_signal, signal);
  176. }
  177. void xremote_cross_remote_item_set_sg_signal(CrossRemoteItem* item, SubGhzRemote* subghz) {
  178. item->sg_signal = subghz;
  179. }
  180. const char* xremote_cross_remote_item_get_name(CrossRemoteItem* item) {
  181. return furi_string_get_cstr(item->name);
  182. }
  183. InfraredSignal* xremote_cross_remote_item_get_ir_signal(CrossRemoteItem* item) {
  184. return item->ir_signal;
  185. }
  186. SubGhzRemote* xremote_cross_remote_item_get_sg_signal(CrossRemoteItem* item) {
  187. return item->sg_signal;
  188. }
  189. uint32_t xremote_cross_remote_item_get_time(CrossRemoteItem* item) {
  190. return item->time;
  191. }
  192. bool xremote_cross_remote_item_ir_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name, uint32_t time) {
  193. if(!flipper_format_write_comment_cstr(ff, "") ||
  194. !flipper_format_write_string_cstr(ff, "remote_type", "IR") ||
  195. !flipper_format_write_string_cstr(ff, "name", name) ||
  196. !flipper_format_write_uint32(ff, "time", &time, 1)) {
  197. return false;
  198. } else if(signal->is_raw) {
  199. return xremote_ir_signal_save_raw(&signal->payload.raw, ff);
  200. } else {
  201. return xremote_ir_signal_save_message(&signal->payload.message, ff);
  202. }
  203. }
  204. bool xremote_cross_remote_item_pause_save(FlipperFormat* ff, uint32_t time, const char* name) {
  205. if(!flipper_format_write_comment_cstr(ff, "") ||
  206. !flipper_format_write_string_cstr(ff, "remote_type", "PAUSE") ||
  207. !flipper_format_write_string_cstr(ff, "name", name) ||
  208. !flipper_format_write_uint32(ff, "time", &time, 1)) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. bool xremote_cross_remote_item_sg_signal_save(SubGhzRemote* remote, FlipperFormat* ff, const char* name) {
  214. if(!flipper_format_write_comment_cstr(ff, "") ||
  215. !flipper_format_write_string_cstr(ff, "remote_type", "SG") ||
  216. !flipper_format_write_string_cstr(ff, "name", name) ||
  217. !flipper_format_write_string_cstr(ff, "filename", xremote_sg_remote_get_filename(remote))) {
  218. return false;
  219. }
  220. return xremote_sg_signal_save_data(remote, ff);
  221. }