pocsag_pager_history.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "pocsag_pager_history.h"
  2. #include <flipper_format/flipper_format_i.h>
  3. #include <lib/toolbox/stream/stream.h>
  4. #include <lib/subghz/receiver.h>
  5. #include "protocols/pcsg_generic.h"
  6. #include <furi.h>
  7. #define PCSG_HISTORY_MAX 50
  8. #define TAG "PCSGHistory"
  9. typedef struct {
  10. FuriString* item_str;
  11. FlipperFormat* flipper_string;
  12. uint8_t type;
  13. SubGhzRadioPreset* preset;
  14. } PCSGHistoryItem;
  15. ARRAY_DEF(PCSGHistoryItemArray, PCSGHistoryItem, M_POD_OPLIST)
  16. #define M_OPL_PCSGHistoryItemArray_t() ARRAY_OPLIST(PCSGHistoryItemArray, M_POD_OPLIST)
  17. typedef struct {
  18. PCSGHistoryItemArray_t data;
  19. } PCSGHistoryStruct;
  20. struct PCSGHistory {
  21. uint32_t last_update_timestamp;
  22. uint16_t last_index_write;
  23. uint8_t code_last_hash_data;
  24. FuriString* tmp_string;
  25. PCSGHistoryStruct* history;
  26. };
  27. PCSGHistory* pcsg_history_alloc(void) {
  28. PCSGHistory* instance = malloc(sizeof(PCSGHistory));
  29. instance->tmp_string = furi_string_alloc();
  30. instance->history = malloc(sizeof(PCSGHistoryStruct));
  31. PCSGHistoryItemArray_init(instance->history->data);
  32. return instance;
  33. }
  34. void pcsg_history_free(PCSGHistory* instance) {
  35. furi_assert(instance);
  36. furi_string_free(instance->tmp_string);
  37. for
  38. M_EACH(item, instance->history->data, PCSGHistoryItemArray_t) {
  39. furi_string_free(item->item_str);
  40. furi_string_free(item->preset->name);
  41. free(item->preset);
  42. flipper_format_free(item->flipper_string);
  43. item->type = 0;
  44. }
  45. PCSGHistoryItemArray_clear(instance->history->data);
  46. free(instance->history);
  47. free(instance);
  48. }
  49. uint32_t pcsg_history_get_frequency(PCSGHistory* instance, uint16_t idx) {
  50. furi_assert(instance);
  51. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  52. return item->preset->frequency;
  53. }
  54. SubGhzRadioPreset* pcsg_history_get_radio_preset(PCSGHistory* instance, uint16_t idx) {
  55. furi_assert(instance);
  56. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  57. return item->preset;
  58. }
  59. const char* pcsg_history_get_preset(PCSGHistory* instance, uint16_t idx) {
  60. furi_assert(instance);
  61. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  62. return furi_string_get_cstr(item->preset->name);
  63. }
  64. void pcsg_history_reset(PCSGHistory* instance) {
  65. furi_assert(instance);
  66. furi_string_reset(instance->tmp_string);
  67. for
  68. M_EACH(item, instance->history->data, PCSGHistoryItemArray_t) {
  69. furi_string_free(item->item_str);
  70. furi_string_free(item->preset->name);
  71. free(item->preset);
  72. flipper_format_free(item->flipper_string);
  73. item->type = 0;
  74. }
  75. PCSGHistoryItemArray_reset(instance->history->data);
  76. instance->last_index_write = 0;
  77. instance->code_last_hash_data = 0;
  78. }
  79. uint16_t pcsg_history_get_item(PCSGHistory* instance) {
  80. furi_assert(instance);
  81. return instance->last_index_write;
  82. }
  83. uint8_t pcsg_history_get_type_protocol(PCSGHistory* instance, uint16_t idx) {
  84. furi_assert(instance);
  85. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  86. return item->type;
  87. }
  88. const char* pcsg_history_get_protocol_name(PCSGHistory* instance, uint16_t idx) {
  89. furi_assert(instance);
  90. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  91. flipper_format_rewind(item->flipper_string);
  92. if(!flipper_format_read_string(item->flipper_string, "Protocol", instance->tmp_string)) {
  93. FURI_LOG_E(TAG, "Missing Protocol");
  94. furi_string_reset(instance->tmp_string);
  95. }
  96. return furi_string_get_cstr(instance->tmp_string);
  97. }
  98. FlipperFormat* pcsg_history_get_raw_data(PCSGHistory* instance, uint16_t idx) {
  99. furi_assert(instance);
  100. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  101. if(item->flipper_string) {
  102. return item->flipper_string;
  103. } else {
  104. return NULL;
  105. }
  106. }
  107. bool pcsg_history_get_text_space_left(PCSGHistory* instance, FuriString* output) {
  108. furi_assert(instance);
  109. if(instance->last_index_write == PCSG_HISTORY_MAX) {
  110. if(output != NULL) furi_string_printf(output, "Memory is FULL");
  111. return true;
  112. }
  113. if(output != NULL)
  114. furi_string_printf(output, "%02u/%02u", instance->last_index_write, PCSG_HISTORY_MAX);
  115. return false;
  116. }
  117. void pcsg_history_get_text_item_menu(PCSGHistory* instance, FuriString* output, uint16_t idx) {
  118. PCSGHistoryItem* item = PCSGHistoryItemArray_get(instance->history->data, idx);
  119. furi_string_set(output, item->item_str);
  120. }
  121. PCSGHistoryStateAddKey
  122. pcsg_history_add_to_history(PCSGHistory* instance, void* context, SubGhzRadioPreset* preset) {
  123. furi_assert(instance);
  124. furi_assert(context);
  125. if(instance->last_index_write >= PCSG_HISTORY_MAX) return PCSGHistoryStateAddKeyOverflow;
  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 PCSGHistoryStateAddKeyTimeOut;
  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. FlipperFormat* fff = flipper_format_string_alloc();
  136. subghz_protocol_decoder_base_serialize(decoder_base, fff, preset);
  137. do {
  138. if(!flipper_format_rewind(fff)) {
  139. FURI_LOG_E(TAG, "Rewind error");
  140. break;
  141. }
  142. } while(false);
  143. flipper_format_free(fff);
  144. PCSGHistoryItem* item = PCSGHistoryItemArray_push_raw(instance->history->data);
  145. item->preset = malloc(sizeof(SubGhzRadioPreset));
  146. item->type = decoder_base->protocol->type;
  147. item->preset->frequency = preset->frequency;
  148. item->preset->name = furi_string_alloc();
  149. furi_string_set(item->preset->name, preset->name);
  150. item->preset->data = preset->data;
  151. item->preset->data_size = preset->data_size;
  152. item->item_str = furi_string_alloc();
  153. item->flipper_string = flipper_format_string_alloc();
  154. subghz_protocol_decoder_base_serialize(decoder_base, item->flipper_string, preset);
  155. do {
  156. if(!flipper_format_rewind(item->flipper_string)) {
  157. FURI_LOG_E(TAG, "Rewind error");
  158. break;
  159. }
  160. if(!flipper_format_read_string(item->flipper_string, "Protocol", instance->tmp_string)) {
  161. FURI_LOG_E(TAG, "Missing Protocol");
  162. break;
  163. }
  164. if(!flipper_format_rewind(item->flipper_string)) {
  165. FURI_LOG_E(TAG, "Rewind error");
  166. break;
  167. }
  168. FuriString* temp_ric = furi_string_alloc();
  169. if(!flipper_format_read_string(item->flipper_string, "Ric", temp_ric)) {
  170. FURI_LOG_E(TAG, "Missing Ric");
  171. break;
  172. }
  173. FuriString* temp_message = furi_string_alloc();
  174. if(!flipper_format_read_string(item->flipper_string, "Message", temp_message)) {
  175. FURI_LOG_E(TAG, "Missing Message");
  176. break;
  177. }
  178. furi_string_printf(
  179. item->item_str,
  180. "%s%s",
  181. furi_string_get_cstr(temp_ric),
  182. furi_string_get_cstr(temp_message));
  183. furi_string_free(temp_message);
  184. furi_string_free(temp_ric);
  185. } while(false);
  186. instance->last_index_write++;
  187. return PCSGHistoryStateAddKeyNewDada;
  188. return PCSGHistoryStateAddKeyUnknown;
  189. }