generic.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "generic.h"
  2. #include <lib/toolbox/stream/stream.h>
  3. #include <lib/flipper_format/flipper_format_i.h>
  4. #define TAG "SubGhzBlockGeneric"
  5. void subghz_block_generic_get_preset_name(const char* preset_name, FuriString* preset_str) {
  6. const char* preset_name_temp;
  7. if(!strcmp(preset_name, "AM270")) {
  8. preset_name_temp = "FuriHalSubGhzPresetOok270Async";
  9. } else if(!strcmp(preset_name, "AM650")) {
  10. preset_name_temp = "FuriHalSubGhzPresetOok650Async";
  11. } else if(!strcmp(preset_name, "FM238")) {
  12. preset_name_temp = "FuriHalSubGhzPreset2FSKDev238Async";
  13. } else if(!strcmp(preset_name, "FM476")) {
  14. preset_name_temp = "FuriHalSubGhzPreset2FSKDev476Async";
  15. } else {
  16. preset_name_temp = "FuriHalSubGhzPresetCustom";
  17. }
  18. furi_string_set(preset_str, preset_name_temp);
  19. }
  20. bool subghz_block_generic_serialize(
  21. SubGhzBlockGeneric* instance,
  22. FlipperFormat* flipper_format,
  23. SubGhzRadioPreset* preset) {
  24. furi_assert(instance);
  25. bool res = false;
  26. FuriString* temp_str;
  27. temp_str = furi_string_alloc();
  28. do {
  29. stream_clean(flipper_format_get_raw_stream(flipper_format));
  30. if(!flipper_format_write_header_cstr(
  31. flipper_format, SUBGHZ_KEY_FILE_TYPE, SUBGHZ_KEY_FILE_VERSION)) {
  32. FURI_LOG_E(TAG, "Unable to add header");
  33. break;
  34. }
  35. if(!flipper_format_write_uint32(flipper_format, "Frequency", &preset->frequency, 1)) {
  36. FURI_LOG_E(TAG, "Unable to add Frequency");
  37. break;
  38. }
  39. subghz_block_generic_get_preset_name(furi_string_get_cstr(preset->name), temp_str);
  40. if(!flipper_format_write_string_cstr(
  41. flipper_format, "Preset", furi_string_get_cstr(temp_str))) {
  42. FURI_LOG_E(TAG, "Unable to add Preset");
  43. break;
  44. }
  45. if(!strcmp(furi_string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
  46. if(!flipper_format_write_string_cstr(
  47. flipper_format, "Custom_preset_module", "CC1101")) {
  48. FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
  49. break;
  50. }
  51. if(!flipper_format_write_hex(
  52. flipper_format, "Custom_preset_data", preset->data, preset->data_size)) {
  53. FURI_LOG_E(TAG, "Unable to add Custom_preset_data");
  54. break;
  55. }
  56. }
  57. if(!flipper_format_write_string_cstr(flipper_format, "Protocol", instance->protocol_name)) {
  58. FURI_LOG_E(TAG, "Unable to add Protocol");
  59. break;
  60. }
  61. uint32_t temp = instance->data_count_bit;
  62. if(!flipper_format_write_uint32(flipper_format, "Bit", &temp, 1)) {
  63. FURI_LOG_E(TAG, "Unable to add Bit");
  64. break;
  65. }
  66. uint8_t key_data[sizeof(uint64_t)] = {0};
  67. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  68. key_data[sizeof(uint64_t) - i - 1] = (instance->data >> i * 8) & 0xFF;
  69. }
  70. if(!flipper_format_write_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  71. FURI_LOG_E(TAG, "Unable to add Key");
  72. break;
  73. }
  74. res = true;
  75. } while(false);
  76. furi_string_free(temp_str);
  77. return res;
  78. }
  79. bool subghz_block_generic_deserialize(SubGhzBlockGeneric* instance, FlipperFormat* flipper_format) {
  80. furi_assert(instance);
  81. bool res = false;
  82. FuriString* temp_str;
  83. temp_str = furi_string_alloc();
  84. uint32_t temp_data = 0;
  85. do {
  86. if(!flipper_format_rewind(flipper_format)) {
  87. FURI_LOG_E(TAG, "Rewind error");
  88. break;
  89. }
  90. if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
  91. FURI_LOG_E(TAG, "Missing Bit");
  92. break;
  93. }
  94. instance->data_count_bit = (uint8_t)temp_data;
  95. uint8_t key_data[sizeof(uint64_t)] = {0};
  96. if(!flipper_format_read_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  97. FURI_LOG_E(TAG, "Missing Key");
  98. break;
  99. }
  100. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  101. instance->data = instance->data << 8 | key_data[i];
  102. }
  103. res = true;
  104. } while(0);
  105. furi_string_free(temp_str);
  106. return res;
  107. }