xremote_remote_item.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. bool xremote_remote_item_read(CrossRemoteItem* item, FlipperFormat* ff) {
  60. FuriString* type = furi_string_alloc();
  61. bool success = false;
  62. do {
  63. if(!flipper_format_read_string(ff, "remote_type", type)) break;
  64. if(furi_string_equal(type, "IR")) {
  65. success = xremote_remote_item_read_ir(item, ff);
  66. } else if(furi_string_equal(type, "PAUSE")) {
  67. success = xremote_remote_item_read_pause(item, ff);
  68. } else {
  69. break;
  70. }
  71. success = true;
  72. } while(false);
  73. return success;
  74. }
  75. bool xremote_remote_item_read_ir(CrossRemoteItem* item, FlipperFormat* ff) {
  76. FuriString* buf;
  77. bool success = false;
  78. buf = furi_string_alloc();
  79. item->type = XRemoteRemoteItemTypeInfrared;
  80. item->time = 0;
  81. do {
  82. if(!flipper_format_read_string(ff, "name", item->name)) break;
  83. if(!flipper_format_read_string(ff, "type", buf)) break;
  84. if(furi_string_equal(buf, "raw")) {
  85. if(!xremote_remote_item_read_ir_signal_raw(item, ff)) break;
  86. } else if(furi_string_equal(buf, "parsed")) {
  87. if(!xremote_remote_item_read_message(item, ff)) break;
  88. } else {
  89. break;
  90. }
  91. success = true;
  92. } while(false);
  93. furi_string_free(buf);
  94. return success;
  95. }
  96. bool xremote_remote_item_read_pause(CrossRemoteItem* item, FlipperFormat* ff) {
  97. bool success = false;
  98. item->type = XRemoteRemoteItemTypePause;
  99. do {
  100. if(!flipper_format_read_string(ff, "name", item->name)) break;
  101. if(!flipper_format_read_int32(ff, "time", &item->time, 1)) break;
  102. success = true;
  103. } while(false);
  104. return success;
  105. }
  106. bool xremote_remote_item_read_ir_signal_raw(CrossRemoteItem* item, FlipperFormat* ff) {
  107. uint32_t timings_size, frequency;
  108. float duty_cycle;
  109. bool success = flipper_format_read_uint32(ff, "frequency", &frequency, 1) &&
  110. flipper_format_read_float(ff, "duty_cycle", &duty_cycle, 1) &&
  111. flipper_format_get_value_count(ff, "data", &timings_size);
  112. if(!success || timings_size > MAX_TIMINGS_AMOUNT) {
  113. return false;
  114. }
  115. uint32_t* timings = malloc(sizeof(uint32_t) * timings_size);
  116. success = flipper_format_read_uint32(ff, "data", timings, timings_size);
  117. if(success) {
  118. xremote_ir_signal_set_raw_signal(
  119. item->ir_signal, timings, timings_size, frequency, duty_cycle);
  120. }
  121. free(timings);
  122. return success;
  123. }
  124. bool xremote_remote_item_read_message(CrossRemoteItem* item, FlipperFormat* ff) {
  125. FuriString* buf = furi_string_alloc();
  126. bool success = false;
  127. do {
  128. if(!flipper_format_read_string(ff, "protocol", buf)) break;
  129. InfraredMessage message;
  130. message.protocol = infrared_get_protocol_by_name(furi_string_get_cstr(buf));
  131. success = flipper_format_read_hex(ff, "address", (uint8_t*)&message.address, 4) &&
  132. flipper_format_read_hex(ff, "command", (uint8_t*)&message.command, 4) &&
  133. xremote_ir_signal_is_message_valid(&message);
  134. if(!success) break;
  135. xremote_ir_signal_set_message(item->ir_signal, &message);
  136. } while(false);
  137. furi_string_free(buf);
  138. return success;
  139. }
  140. void xremote_remote_item_free(CrossRemoteItem* item) {
  141. furi_string_free(item->name);
  142. //Determine type before free
  143. //xremote_ir_signal_free(item->ir_signal);
  144. //xremote_sg_remote_free(item->sg_signal);
  145. free(item);
  146. }
  147. void xremote_remote_item_set_type(CrossRemoteItem* item, int type) {
  148. item->type = type;
  149. }
  150. void xremote_remote_item_set_name(CrossRemoteItem* item, const char* name) {
  151. furi_string_set(item->name, name);
  152. }
  153. void xremote_remote_item_set_time(CrossRemoteItem* item, int32_t time) {
  154. item->time = time;
  155. }
  156. void xremote_remote_item_set_ir_signal(CrossRemoteItem* item, InfraredSignal* signal) {
  157. xremote_ir_signal_set_signal(item->ir_signal, signal);
  158. }
  159. void xremote_remote_item_set_sg_signal(CrossRemoteItem* item, SubGhzRemote* subghz) {
  160. item->sg_signal = subghz;
  161. }
  162. const char* xremote_remote_item_get_name(CrossRemoteItem* item) {
  163. return furi_string_get_cstr(item->name);
  164. }
  165. InfraredSignal* xremote_remote_item_get_ir_signal(CrossRemoteItem* item) {
  166. return item->ir_signal;
  167. }
  168. SubGhzRemote* xremote_remote_item_get_sg_signal(CrossRemoteItem* item) {
  169. return item->sg_signal;
  170. }
  171. bool xremote_ir_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name) {
  172. if(!flipper_format_write_comment_cstr(ff, "") ||
  173. !flipper_format_write_string_cstr(ff, "remote_type", "IR") ||
  174. !flipper_format_write_string_cstr(ff, "name", name)) {
  175. return false;
  176. } else if(signal->is_raw) {
  177. return xremote_ir_signal_save_raw(&signal->payload.raw, ff);
  178. } else {
  179. return xremote_ir_signal_save_message(&signal->payload.message, ff);
  180. }
  181. }
  182. bool xremote_pause_save(FlipperFormat* ff, int32_t time, const char* name) {
  183. if(!flipper_format_write_comment_cstr(ff, "") ||
  184. !flipper_format_write_string_cstr(ff, "remote_type", "PAUSE") ||
  185. !flipper_format_write_string_cstr(ff, "name", name) ||
  186. !flipper_format_write_int32(ff, "time", &time, 1)) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. bool xremote_sg_signal_save(SubGhzRemote* remote, FlipperFormat* ff, const char* name) {
  192. if(!flipper_format_write_comment_cstr(ff, "") ||
  193. !flipper_format_write_string_cstr(ff, "remote_type", "SG") ||
  194. !flipper_format_write_string_cstr(ff, "name", name)) {
  195. return false;
  196. }
  197. return xremote_sg_signal_save_data(remote, ff);
  198. }