subghz_history.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "subghz_history.h"
  2. #include <lib/subghz/receiver.h>
  3. #include <lib/subghz/protocols/came.h>
  4. #include <furi.h>
  5. #define SUBGHZ_HISTORY_MAX 50
  6. #define TAG "SubGhzHistory"
  7. typedef struct {
  8. FuriString* item_str;
  9. FlipperFormat* flipper_string;
  10. uint8_t type;
  11. SubGhzRadioPreset* preset;
  12. } SubGhzHistoryItem;
  13. ARRAY_DEF(SubGhzHistoryItemArray, SubGhzHistoryItem, M_POD_OPLIST)
  14. #define M_OPL_SubGhzHistoryItemArray_t() ARRAY_OPLIST(SubGhzHistoryItemArray, M_POD_OPLIST)
  15. typedef struct {
  16. SubGhzHistoryItemArray_t data;
  17. } SubGhzHistoryStruct;
  18. struct SubGhzHistory {
  19. uint32_t last_update_timestamp;
  20. uint16_t last_index_write;
  21. uint8_t code_last_hash_data;
  22. FuriString* tmp_string;
  23. SubGhzHistoryStruct* history;
  24. };
  25. SubGhzHistory* subghz_history_alloc(void) {
  26. SubGhzHistory* instance = malloc(sizeof(SubGhzHistory));
  27. instance->tmp_string = furi_string_alloc();
  28. instance->history = malloc(sizeof(SubGhzHistoryStruct));
  29. SubGhzHistoryItemArray_init(instance->history->data);
  30. return instance;
  31. }
  32. void subghz_history_free(SubGhzHistory* instance) {
  33. furi_assert(instance);
  34. furi_string_free(instance->tmp_string);
  35. for
  36. M_EACH(item, instance->history->data, SubGhzHistoryItemArray_t) {
  37. furi_string_free(item->item_str);
  38. furi_string_free(item->preset->name);
  39. free(item->preset);
  40. flipper_format_free(item->flipper_string);
  41. item->type = 0;
  42. }
  43. SubGhzHistoryItemArray_clear(instance->history->data);
  44. free(instance->history);
  45. free(instance);
  46. }
  47. uint32_t subghz_history_get_frequency(SubGhzHistory* instance, uint16_t idx) {
  48. furi_assert(instance);
  49. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  50. return item->preset->frequency;
  51. }
  52. SubGhzRadioPreset* subghz_history_get_radio_preset(SubGhzHistory* instance, uint16_t idx) {
  53. furi_assert(instance);
  54. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  55. return item->preset;
  56. }
  57. const char* subghz_history_get_preset(SubGhzHistory* instance, uint16_t idx) {
  58. furi_assert(instance);
  59. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  60. return furi_string_get_cstr(item->preset->name);
  61. }
  62. void subghz_history_reset(SubGhzHistory* instance) {
  63. furi_assert(instance);
  64. furi_string_reset(instance->tmp_string);
  65. for
  66. M_EACH(item, instance->history->data, SubGhzHistoryItemArray_t) {
  67. furi_string_free(item->item_str);
  68. furi_string_free(item->preset->name);
  69. free(item->preset);
  70. flipper_format_free(item->flipper_string);
  71. item->type = 0;
  72. }
  73. SubGhzHistoryItemArray_reset(instance->history->data);
  74. instance->last_index_write = 0;
  75. instance->code_last_hash_data = 0;
  76. }
  77. uint16_t subghz_history_get_item(SubGhzHistory* instance) {
  78. furi_assert(instance);
  79. return instance->last_index_write;
  80. }
  81. uint8_t subghz_history_get_type_protocol(SubGhzHistory* instance, uint16_t idx) {
  82. furi_assert(instance);
  83. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  84. return item->type;
  85. }
  86. const char* subghz_history_get_protocol_name(SubGhzHistory* instance, uint16_t idx) {
  87. furi_assert(instance);
  88. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  89. flipper_format_rewind(item->flipper_string);
  90. if(!flipper_format_read_string(item->flipper_string, "Protocol", instance->tmp_string)) {
  91. FURI_LOG_E(TAG, "Missing Protocol");
  92. furi_string_reset(instance->tmp_string);
  93. }
  94. return furi_string_get_cstr(instance->tmp_string);
  95. }
  96. FlipperFormat* subghz_history_get_raw_data(SubGhzHistory* instance, uint16_t idx) {
  97. furi_assert(instance);
  98. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  99. if(item->flipper_string) {
  100. return item->flipper_string;
  101. } else {
  102. return NULL;
  103. }
  104. }
  105. bool subghz_history_get_text_space_left(SubGhzHistory* instance, FuriString* output) {
  106. furi_assert(instance);
  107. if(instance->last_index_write == SUBGHZ_HISTORY_MAX) {
  108. if(output != NULL) furi_string_printf(output, "Memory is FULL");
  109. return true;
  110. }
  111. if(output != NULL)
  112. furi_string_printf(output, "%02u/%02u", instance->last_index_write, SUBGHZ_HISTORY_MAX);
  113. return false;
  114. }
  115. void subghz_history_get_text_item_menu(SubGhzHistory* instance, FuriString* output, uint16_t idx) {
  116. SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
  117. furi_string_set(output, item->item_str);
  118. }
  119. bool subghz_history_add_to_history(
  120. SubGhzHistory* instance,
  121. void* context,
  122. SubGhzRadioPreset* preset) {
  123. furi_assert(instance);
  124. furi_assert(context);
  125. if(instance->last_index_write >= SUBGHZ_HISTORY_MAX) return false;
  126. SubGhzProtocolDecoderBase* decoder_base = context;
  127. if((instance->code_last_hash_data ==
  128. subghz_protocol_decoder_base_get_hash_data(decoder_base)) &&
  129. ((furi_get_tick() - instance->last_update_timestamp) < 500)) {
  130. instance->last_update_timestamp = furi_get_tick();
  131. return false;
  132. }
  133. instance->code_last_hash_data = subghz_protocol_decoder_base_get_hash_data(decoder_base);
  134. instance->last_update_timestamp = furi_get_tick();
  135. FuriString* text;
  136. text = furi_string_alloc();
  137. SubGhzHistoryItem* item = SubGhzHistoryItemArray_push_raw(instance->history->data);
  138. item->preset = malloc(sizeof(SubGhzRadioPreset));
  139. item->type = decoder_base->protocol->type;
  140. item->preset->frequency = preset->frequency;
  141. item->preset->name = furi_string_alloc();
  142. furi_string_set(item->preset->name, preset->name);
  143. item->preset->data = preset->data;
  144. item->preset->data_size = preset->data_size;
  145. item->item_str = furi_string_alloc();
  146. item->flipper_string = flipper_format_string_alloc();
  147. subghz_protocol_decoder_base_serialize(decoder_base, item->flipper_string, preset);
  148. do {
  149. if(!flipper_format_rewind(item->flipper_string)) {
  150. FURI_LOG_E(TAG, "Rewind error");
  151. break;
  152. }
  153. if(!flipper_format_read_string(item->flipper_string, "Protocol", instance->tmp_string)) {
  154. FURI_LOG_E(TAG, "Missing Protocol");
  155. break;
  156. }
  157. if(!strcmp(furi_string_get_cstr(instance->tmp_string), "KeeLoq")) {
  158. furi_string_set(instance->tmp_string, "KL ");
  159. if(!flipper_format_read_string(item->flipper_string, "Manufacture", text)) {
  160. FURI_LOG_E(TAG, "Missing Protocol");
  161. break;
  162. }
  163. furi_string_cat(instance->tmp_string, text);
  164. } else if(!strcmp(furi_string_get_cstr(instance->tmp_string), "Star Line")) {
  165. furi_string_set(instance->tmp_string, "SL ");
  166. if(!flipper_format_read_string(item->flipper_string, "Manufacture", text)) {
  167. FURI_LOG_E(TAG, "Missing Protocol");
  168. break;
  169. }
  170. furi_string_cat(instance->tmp_string, text);
  171. }
  172. if(!flipper_format_rewind(item->flipper_string)) {
  173. FURI_LOG_E(TAG, "Rewind error");
  174. break;
  175. }
  176. uint8_t key_data[sizeof(uint64_t)] = {0};
  177. if(!flipper_format_read_hex(item->flipper_string, "Key", key_data, sizeof(uint64_t))) {
  178. FURI_LOG_E(TAG, "Missing Key");
  179. break;
  180. }
  181. uint64_t data = 0;
  182. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  183. data = (data << 8) | key_data[i];
  184. }
  185. if(!(uint32_t)(data >> 32)) {
  186. furi_string_printf(
  187. item->item_str,
  188. "%s %lX",
  189. furi_string_get_cstr(instance->tmp_string),
  190. (uint32_t)(data & 0xFFFFFFFF));
  191. } else {
  192. furi_string_printf(
  193. item->item_str,
  194. "%s %lX%08lX",
  195. furi_string_get_cstr(instance->tmp_string),
  196. (uint32_t)(data >> 32),
  197. (uint32_t)(data & 0xFFFFFFFF));
  198. }
  199. } while(false);
  200. furi_string_free(text);
  201. instance->last_index_write++;
  202. return true;
  203. }