xremote_remote_item.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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(item->ir_signal, timings, timings_size, frequency, duty_cycle);
  119. }
  120. free(timings);
  121. return success;
  122. }
  123. bool xremote_remote_item_read_message(CrossRemoteItem* item, FlipperFormat* ff) {
  124. FuriString* buf = furi_string_alloc();
  125. bool success = false;
  126. do {
  127. if(!flipper_format_read_string(ff, "protocol", buf)) break;
  128. InfraredMessage message;
  129. message.protocol = infrared_get_protocol_by_name(furi_string_get_cstr(buf));
  130. success = flipper_format_read_hex(ff, "address", (uint8_t*)&message.address, 4) &&
  131. flipper_format_read_hex(ff, "command", (uint8_t*)&message.command, 4) &&
  132. xremote_ir_signal_is_message_valid(&message);
  133. if(!success) break;
  134. xremote_ir_signal_set_message(item->ir_signal, &message);
  135. } while(false);
  136. furi_string_free(buf);
  137. return success;
  138. }
  139. void xremote_remote_item_free(CrossRemoteItem* item) {
  140. furi_string_free(item->name);
  141. //Determine type before free
  142. //xremote_ir_signal_free(item->ir_signal);
  143. //xremote_sg_remote_free(item->sg_signal);
  144. free(item);
  145. }
  146. void xremote_remote_item_set_type(CrossRemoteItem* item, int type) {
  147. item->type = type;
  148. }
  149. void xremote_remote_item_set_name(CrossRemoteItem* item, const char* name) {
  150. furi_string_set(item->name, name);
  151. }
  152. void xremote_remote_item_set_time(CrossRemoteItem* item, int32_t time) {
  153. item->time = time;
  154. }
  155. void xremote_remote_item_set_ir_signal(CrossRemoteItem* item, InfraredSignal* signal) {
  156. xremote_ir_signal_set_signal(item->ir_signal, signal);
  157. }
  158. void xremote_remote_item_set_sg_signal(CrossRemoteItem* item, SubGhzRemote* subghz) {
  159. item->sg_signal = subghz;
  160. }
  161. const char* xremote_remote_item_get_name(CrossRemoteItem* item) {
  162. return furi_string_get_cstr(item->name);
  163. }
  164. InfraredSignal* xremote_remote_item_get_ir_signal(CrossRemoteItem* item) {
  165. return item->ir_signal;
  166. }
  167. SubGhzRemote* xremote_remote_item_get_sg_signal(CrossRemoteItem* item) {
  168. return item->sg_signal;
  169. }
  170. bool xremote_ir_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name) {
  171. if(!flipper_format_write_comment_cstr(ff, "") ||
  172. !flipper_format_write_string_cstr(ff, "remote_type", "IR") ||
  173. !flipper_format_write_string_cstr(ff, "name", name)) {
  174. return false;
  175. } else if(signal->is_raw) {
  176. return xremote_ir_signal_save_raw(&signal->payload.raw, ff);
  177. } else {
  178. return xremote_ir_signal_save_message(&signal->payload.message, ff);
  179. }
  180. }
  181. bool xremote_pause_save(FlipperFormat* ff, int32_t time, const char* name) {
  182. if(!flipper_format_write_comment_cstr(ff, "") ||
  183. !flipper_format_write_string_cstr(ff, "remote_type", "PAUSE") ||
  184. !flipper_format_write_string_cstr(ff, "name", name) ||
  185. !flipper_format_write_int32(ff, "time", &time, 1)) {
  186. return false;
  187. }
  188. return true;
  189. }
  190. bool xremote_sg_signal_save(SubGhzRemote* remote, FlipperFormat* ff, const char* name) {
  191. if(!flipper_format_write_comment_cstr(ff, "") ||
  192. !flipper_format_write_string_cstr(ff, "remote_type", "SG") ||
  193. !flipper_format_write_string_cstr(ff, "name", name)) {
  194. return false;
  195. }
  196. return xremote_sg_signal_save_data(remote, ff);
  197. }