xremote_cross_remote_item.c 9.7 KB

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