xremote_cross_remote_item.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_string(ff, "type", buf)) break;
  113. if(furi_string_equal(buf, "raw")) {
  114. if(!xremote_cross_remote_item_read_ir_signal_raw(item, ff)) break;
  115. } else if(furi_string_equal(buf, "parsed")) {
  116. if(!xremote_cross_remote_item_read_message(item, ff)) break;
  117. } else {
  118. break;
  119. }
  120. success = true;
  121. } while(false);
  122. furi_string_free(buf);
  123. return success;
  124. }
  125. static bool xremote_cross_remote_item_read_pause(CrossRemoteItem* item, FlipperFormat* ff) {
  126. bool success = false;
  127. item->type = XRemoteRemoteItemTypePause;
  128. do {
  129. if(!flipper_format_read_string(ff, "name", item->name)) break;
  130. if(!flipper_format_read_int32(ff, "time", &item->time, 1)) break;
  131. success = true;
  132. } while(false);
  133. return success;
  134. }
  135. bool xremote_cross_remote_item_read(CrossRemoteItem* item, FlipperFormat* ff) {
  136. FuriString* type = furi_string_alloc();
  137. bool success = false;
  138. do {
  139. if(!flipper_format_read_string(ff, "remote_type", type)) break;
  140. if(furi_string_equal(type, "IR")) {
  141. success = xremote_cross_remote_item_read_ir(item, ff);
  142. } else if(furi_string_equal(type, "SG")) {
  143. success = xremote_cross_remote_item_read_sg(item, ff);
  144. } else if(furi_string_equal(type, "PAUSE")) {
  145. success = xremote_cross_remote_item_read_pause(item, ff);
  146. } else {
  147. break;
  148. }
  149. success = true;
  150. } while(false);
  151. furi_string_free(type);
  152. return success;
  153. }
  154. void xremote_cross_remote_item_free(CrossRemoteItem* item) {
  155. furi_string_free(item->name);
  156. //Determine type before free
  157. xremote_ir_signal_free(item->ir_signal);
  158. xremote_sg_remote_free(item->sg_signal);
  159. free(item);
  160. }
  161. void xremote_cross_remote_item_set_type(CrossRemoteItem* item, int type) {
  162. item->type = type;
  163. }
  164. void xremote_cross_remote_item_set_name(CrossRemoteItem* item, const char* name) {
  165. furi_string_set(item->name, name);
  166. }
  167. void xremote_cross_remote_item_set_time(CrossRemoteItem* item, int32_t time) {
  168. item->time = time;
  169. }
  170. void xremote_cross_remote_item_set_ir_signal(CrossRemoteItem* item, InfraredSignal* signal) {
  171. xremote_ir_signal_set_signal(item->ir_signal, signal);
  172. }
  173. void xremote_cross_remote_item_set_sg_signal(CrossRemoteItem* item, SubGhzRemote* subghz) {
  174. item->sg_signal = subghz;
  175. }
  176. const char* xremote_cross_remote_item_get_name(CrossRemoteItem* item) {
  177. return furi_string_get_cstr(item->name);
  178. }
  179. InfraredSignal* xremote_cross_remote_item_get_ir_signal(CrossRemoteItem* item) {
  180. return item->ir_signal;
  181. }
  182. SubGhzRemote* xremote_cross_remote_item_get_sg_signal(CrossRemoteItem* item) {
  183. return item->sg_signal;
  184. }
  185. uint32_t xremote_cross_remote_item_get_time(CrossRemoteItem* item) {
  186. return item->time;
  187. }
  188. bool xremote_cross_remote_item_ir_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name, uint32_t time) {
  189. if(!flipper_format_write_comment_cstr(ff, "") ||
  190. !flipper_format_write_string_cstr(ff, "remote_type", "IR") ||
  191. !flipper_format_write_string_cstr(ff, "name", name) ||
  192. !flipper_format_write_uint32(ff, "time", &time, 1)) {
  193. return false;
  194. } else if(signal->is_raw) {
  195. return xremote_ir_signal_save_raw(&signal->payload.raw, ff);
  196. } else {
  197. return xremote_ir_signal_save_message(&signal->payload.message, ff);
  198. }
  199. }
  200. bool xremote_cross_remote_item_pause_save(FlipperFormat* ff, int32_t time, const char* name) {
  201. if(!flipper_format_write_comment_cstr(ff, "") ||
  202. !flipper_format_write_string_cstr(ff, "remote_type", "PAUSE") ||
  203. !flipper_format_write_string_cstr(ff, "name", name) ||
  204. !flipper_format_write_int32(ff, "time", &time, 1)) {
  205. return false;
  206. }
  207. return true;
  208. }
  209. bool xremote_cross_remote_item_sg_signal_save(SubGhzRemote* remote, FlipperFormat* ff, const char* name) {
  210. if(!flipper_format_write_comment_cstr(ff, "") ||
  211. !flipper_format_write_string_cstr(ff, "remote_type", "SG") ||
  212. !flipper_format_write_string_cstr(ff, "name", name) ||
  213. !flipper_format_write_string_cstr(ff, "filename", xremote_sg_remote_get_filename(remote))) {
  214. return false;
  215. }
  216. return xremote_sg_signal_save_data(remote, ff);
  217. }