pcsg_generic.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "pcsg_generic.h"
  2. #include <lib/toolbox/stream/stream.h>
  3. #include <lib/flipper_format/flipper_format_i.h>
  4. #include "../helpers/pocsag_pager_types.h"
  5. #define TAG "PCSGBlockGeneric"
  6. void pcsg_block_generic_get_preset_name(const char* preset_name, FuriString* preset_str) {
  7. const char* preset_name_temp;
  8. if(!strcmp(preset_name, "AM270")) {
  9. preset_name_temp = "FuriHalSubGhzPresetOok270Async";
  10. } else if(!strcmp(preset_name, "AM650")) {
  11. preset_name_temp = "FuriHalSubGhzPresetOok650Async";
  12. } else if(!strcmp(preset_name, "FM238")) {
  13. preset_name_temp = "FuriHalSubGhzPreset2FSKDev238Async";
  14. } else if(!strcmp(preset_name, "FM476")) {
  15. preset_name_temp = "FuriHalSubGhzPreset2FSKDev476Async";
  16. } else {
  17. preset_name_temp = "FuriHalSubGhzPresetCustom";
  18. }
  19. furi_string_set(preset_str, preset_name_temp);
  20. }
  21. SubGhzProtocolStatus pcsg_block_generic_serialize(
  22. PCSGBlockGeneric* instance,
  23. FlipperFormat* flipper_format,
  24. SubGhzRadioPreset* preset) {
  25. furi_assert(instance);
  26. SubGhzProtocolStatus res = SubGhzProtocolStatusError;
  27. FuriString* temp_str;
  28. temp_str = furi_string_alloc();
  29. do {
  30. stream_clean(flipper_format_get_raw_stream(flipper_format));
  31. if(!flipper_format_write_header_cstr(
  32. flipper_format, PCSG_KEY_FILE_TYPE, PCSG_KEY_FILE_VERSION)) {
  33. FURI_LOG_E(TAG, "Unable to add header");
  34. break;
  35. }
  36. if(!flipper_format_write_uint32(flipper_format, "Frequency", &preset->frequency, 1)) {
  37. FURI_LOG_E(TAG, "Unable to add Frequency");
  38. break;
  39. }
  40. pcsg_block_generic_get_preset_name(furi_string_get_cstr(preset->name), temp_str);
  41. if(!flipper_format_write_string_cstr(
  42. flipper_format, "Preset", furi_string_get_cstr(temp_str))) {
  43. FURI_LOG_E(TAG, "Unable to add Preset");
  44. break;
  45. }
  46. if(!strcmp(furi_string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
  47. if(!flipper_format_write_string_cstr(
  48. flipper_format, "Custom_preset_module", "CC1101")) {
  49. FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
  50. break;
  51. }
  52. if(!flipper_format_write_hex(
  53. flipper_format, "Custom_preset_data", preset->data, preset->data_size)) {
  54. FURI_LOG_E(TAG, "Unable to add Custom_preset_data");
  55. break;
  56. }
  57. }
  58. if(!flipper_format_write_string_cstr(flipper_format, "Protocol", instance->protocol_name)) {
  59. FURI_LOG_E(TAG, "Unable to add Protocol");
  60. break;
  61. }
  62. if(!flipper_format_write_string(flipper_format, "Ric", instance->result_ric)) {
  63. FURI_LOG_E(TAG, "Unable to add Ric");
  64. break;
  65. }
  66. if(!flipper_format_write_string(flipper_format, "Message", instance->result_msg)) {
  67. FURI_LOG_E(TAG, "Unable to add Message");
  68. break;
  69. }
  70. res = SubGhzProtocolStatusOk;
  71. } while(false);
  72. furi_string_free(temp_str);
  73. return res;
  74. }
  75. SubGhzProtocolStatus
  76. pcsg_block_generic_deserialize(PCSGBlockGeneric* instance, FlipperFormat* flipper_format) {
  77. furi_assert(instance);
  78. SubGhzProtocolStatus res = SubGhzProtocolStatusError;
  79. FuriString* temp_data = furi_string_alloc();
  80. FuriString* temp_data2 = furi_string_alloc();
  81. do {
  82. if(!flipper_format_rewind(flipper_format)) {
  83. FURI_LOG_E(TAG, "Rewind error");
  84. break;
  85. }
  86. if(!flipper_format_read_string(flipper_format, "Ric", temp_data2)) {
  87. FURI_LOG_E(TAG, "Missing Ric");
  88. break;
  89. }
  90. if(instance->result_ric != NULL) {
  91. furi_string_set(instance->result_ric, temp_data2);
  92. } else {
  93. instance->result_ric = furi_string_alloc_set(temp_data2);
  94. }
  95. if(!flipper_format_read_string(flipper_format, "Message", temp_data)) {
  96. FURI_LOG_E(TAG, "Missing Message");
  97. break;
  98. }
  99. if(instance->result_msg != NULL) {
  100. furi_string_set(instance->result_msg, temp_data);
  101. } else {
  102. instance->result_msg = furi_string_alloc_set(temp_data);
  103. }
  104. res = SubGhzProtocolStatusOk;
  105. } while(0);
  106. furi_string_free(temp_data);
  107. furi_string_free(temp_data2);
  108. return res;
  109. }