xremote_remote_item.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "xremote_remote_item.h"
  2. CrossRemoteItem* xremote_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. /*return flipper_format_write_uint32(ff, "frequency", xremote_sg_remote_get_frequency(remote)) &&
  30. flipper_format_write_string_cstr(ff, "preset", xremote_sg_remote_get_preset(remote));*/
  31. }
  32. static bool xremote_ir_signal_is_message_valid(InfraredMessage* message) {
  33. if(!infrared_is_protocol_valid(message->protocol)) {
  34. FURI_LOG_E(TAG, "Unknown protocol");
  35. return false;
  36. }
  37. uint32_t address_length = infrared_get_protocol_address_length(message->protocol);
  38. uint32_t address_mask = (1UL << address_length) - 1;
  39. if(message->address != (message->address & address_mask)) {
  40. FURI_LOG_E(
  41. TAG,
  42. "Address is out of range (mask 0x%08lX): 0x%lX\r\n",
  43. address_mask,
  44. message->address);
  45. return false;
  46. }
  47. uint32_t command_length = infrared_get_protocol_command_length(message->protocol);
  48. uint32_t command_mask = (1UL << command_length) - 1;
  49. if(message->command != (message->command & command_mask)) {
  50. FURI_LOG_E(
  51. TAG,
  52. "Command is out of range (mask 0x%08lX): 0x%lX\r\n",
  53. command_mask,
  54. message->command);
  55. return false;
  56. }
  57. return true;
  58. }
  59. static bool xremote_remote_item_read_sg(CrossRemoteItem* item, FlipperFormat* ff) {
  60. FuriString* buf;
  61. bool success = false;
  62. buf = furi_string_alloc();
  63. item->type = XRemoteRemoteItemTypeSubGhz;
  64. item->time = 0;
  65. do {
  66. if(!flipper_format_read_string(ff, "name", item->name)) break;
  67. success = true;
  68. } while(false);
  69. furi_string_free(buf);
  70. return success;
  71. }
  72. bool xremote_remote_item_read(CrossRemoteItem* item, FlipperFormat* ff) {
  73. FuriString* type = furi_string_alloc();
  74. bool success = false;
  75. do {
  76. if(!flipper_format_read_string(ff, "remote_type", type)) break;
  77. if(furi_string_equal(type, "IR")) {
  78. success = xremote_remote_item_read_ir(item, ff);
  79. } else if(furi_string_equal(type, "SG")) {
  80. success = xremote_remote_item_read_sg(item, ff);
  81. } else if(furi_string_equal(type, "PAUSE")) {
  82. success = xremote_remote_item_read_pause(item, ff);
  83. } else {
  84. break;
  85. }
  86. success = true;
  87. } while(false);
  88. furi_string_free(type);
  89. return success;
  90. }
  91. bool xremote_remote_item_read_ir(CrossRemoteItem* item, FlipperFormat* ff) {
  92. FuriString* buf;
  93. bool success = false;
  94. buf = furi_string_alloc();
  95. item->type = XRemoteRemoteItemTypeInfrared;
  96. item->time = 0;
  97. do {
  98. if(!flipper_format_read_string(ff, "name", item->name)) break;
  99. if(!flipper_format_read_string(ff, "type", buf)) break;
  100. if(furi_string_equal(buf, "raw")) {
  101. if(!xremote_remote_item_read_ir_signal_raw(item, ff)) break;
  102. } else if(furi_string_equal(buf, "parsed")) {
  103. if(!xremote_remote_item_read_message(item, ff)) break;
  104. } else {
  105. break;
  106. }
  107. success = true;
  108. } while(false);
  109. furi_string_free(buf);
  110. return success;
  111. }
  112. bool xremote_remote_item_read_pause(CrossRemoteItem* item, FlipperFormat* ff) {
  113. bool success = false;
  114. item->type = XRemoteRemoteItemTypePause;
  115. do {
  116. if(!flipper_format_read_string(ff, "name", item->name)) break;
  117. if(!flipper_format_read_int32(ff, "time", &item->time, 1)) break;
  118. success = true;
  119. } while(false);
  120. return success;
  121. }
  122. bool xremote_remote_item_read_ir_signal_raw(CrossRemoteItem* item, FlipperFormat* ff) {
  123. uint32_t timings_size, frequency;
  124. float duty_cycle;
  125. bool success = flipper_format_read_uint32(ff, "frequency", &frequency, 1) &&
  126. flipper_format_read_float(ff, "duty_cycle", &duty_cycle, 1) &&
  127. flipper_format_get_value_count(ff, "data", &timings_size);
  128. if(!success || timings_size > MAX_TIMINGS_AMOUNT) {
  129. return false;
  130. }
  131. uint32_t* timings = malloc(sizeof(uint32_t) * timings_size);
  132. success = flipper_format_read_uint32(ff, "data", timings, timings_size);
  133. if(success) {
  134. xremote_ir_signal_set_raw_signal(
  135. item->ir_signal, timings, timings_size, frequency, duty_cycle);
  136. }
  137. free(timings);
  138. return success;
  139. }
  140. bool xremote_remote_item_read_message(CrossRemoteItem* item, FlipperFormat* ff) {
  141. FuriString* buf = furi_string_alloc();
  142. bool success = false;
  143. do {
  144. if(!flipper_format_read_string(ff, "protocol", buf)) break;
  145. InfraredMessage message;
  146. message.protocol = infrared_get_protocol_by_name(furi_string_get_cstr(buf));
  147. success = flipper_format_read_hex(ff, "address", (uint8_t*)&message.address, 4) &&
  148. flipper_format_read_hex(ff, "command", (uint8_t*)&message.command, 4) &&
  149. xremote_ir_signal_is_message_valid(&message);
  150. if(!success) break;
  151. xremote_ir_signal_set_message(item->ir_signal, &message);
  152. } while(false);
  153. furi_string_free(buf);
  154. return success;
  155. }
  156. void xremote_remote_item_free(CrossRemoteItem* item) {
  157. furi_string_free(item->name);
  158. //Determine type before free
  159. //xremote_ir_signal_free(item->ir_signal);
  160. //xremote_sg_remote_free(item->sg_signal);
  161. free(item);
  162. }
  163. void xremote_remote_item_set_type(CrossRemoteItem* item, int type) {
  164. item->type = type;
  165. }
  166. void xremote_remote_item_set_name(CrossRemoteItem* item, const char* name) {
  167. furi_string_set(item->name, name);
  168. }
  169. void xremote_remote_item_set_time(CrossRemoteItem* item, int32_t time) {
  170. item->time = time;
  171. }
  172. void xremote_remote_item_set_ir_signal(CrossRemoteItem* item, InfraredSignal* signal) {
  173. xremote_ir_signal_set_signal(item->ir_signal, signal);
  174. }
  175. void xremote_remote_item_set_sg_signal(CrossRemoteItem* item, SubGhzRemote* subghz) {
  176. item->sg_signal = subghz;
  177. }
  178. const char* xremote_remote_item_get_name(CrossRemoteItem* item) {
  179. return furi_string_get_cstr(item->name);
  180. }
  181. InfraredSignal* xremote_remote_item_get_ir_signal(CrossRemoteItem* item) {
  182. return item->ir_signal;
  183. }
  184. SubGhzRemote* xremote_remote_item_get_sg_signal(CrossRemoteItem* item) {
  185. return item->sg_signal;
  186. }
  187. bool xremote_ir_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name) {
  188. if(!flipper_format_write_comment_cstr(ff, "") ||
  189. !flipper_format_write_string_cstr(ff, "remote_type", "IR") ||
  190. !flipper_format_write_string_cstr(ff, "name", name)) {
  191. return false;
  192. } else if(signal->is_raw) {
  193. return xremote_ir_signal_save_raw(&signal->payload.raw, ff);
  194. } else {
  195. return xremote_ir_signal_save_message(&signal->payload.message, ff);
  196. }
  197. }
  198. bool xremote_pause_save(FlipperFormat* ff, int32_t time, const char* name) {
  199. if(!flipper_format_write_comment_cstr(ff, "") ||
  200. !flipper_format_write_string_cstr(ff, "remote_type", "PAUSE") ||
  201. !flipper_format_write_string_cstr(ff, "name", name) ||
  202. !flipper_format_write_int32(ff, "time", &time, 1)) {
  203. return false;
  204. }
  205. return true;
  206. }
  207. bool xremote_sg_signal_save(SubGhzRemote* remote, FlipperFormat* ff, const char* name) {
  208. if(!flipper_format_write_comment_cstr(ff, "") ||
  209. !flipper_format_write_string_cstr(ff, "remote_type", "SG") ||
  210. !flipper_format_write_string_cstr(ff, "name", name)) {
  211. return false;
  212. }
  213. return xremote_sg_signal_save_data(remote, ff);
  214. }