xremote_cross_remote_item.c 9.0 KB

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