generic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, string_t 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. string_set(preset_str, preset_name_temp);
  19. }
  20. bool subghz_block_generic_serialize(
  21. SubGhzBlockGeneric* instance,
  22. FlipperFormat* flipper_format,
  23. SubGhzPresetDefinition* preset) {
  24. furi_assert(instance);
  25. bool res = false;
  26. string_t temp_str;
  27. string_init(temp_str);
  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(string_get_cstr(preset->name), temp_str);
  40. if(!flipper_format_write_string_cstr(flipper_format, "Preset", string_get_cstr(temp_str))) {
  41. FURI_LOG_E(TAG, "Unable to add Preset");
  42. break;
  43. }
  44. if(!strcmp(string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
  45. if(!flipper_format_write_string_cstr(
  46. flipper_format, "Custom_preset_module", "CC1101")) {
  47. FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
  48. break;
  49. }
  50. if(!flipper_format_write_hex(
  51. flipper_format, "Custom_preset_data", preset->data, preset->data_size)) {
  52. FURI_LOG_E(TAG, "Unable to add Custom_preset_data");
  53. break;
  54. }
  55. }
  56. if(!flipper_format_write_string_cstr(flipper_format, "Protocol", instance->protocol_name)) {
  57. FURI_LOG_E(TAG, "Unable to add Protocol");
  58. break;
  59. }
  60. uint32_t temp = instance->data_count_bit;
  61. if(!flipper_format_write_uint32(flipper_format, "Bit", &temp, 1)) {
  62. FURI_LOG_E(TAG, "Unable to add Bit");
  63. break;
  64. }
  65. uint8_t key_data[sizeof(uint64_t)] = {0};
  66. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  67. key_data[sizeof(uint64_t) - i - 1] = (instance->data >> i * 8) & 0xFF;
  68. }
  69. if(!flipper_format_write_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  70. FURI_LOG_E(TAG, "Unable to add Key");
  71. break;
  72. }
  73. res = true;
  74. } while(false);
  75. string_clear(temp_str);
  76. return res;
  77. }
  78. bool subghz_block_generic_deserialize(SubGhzBlockGeneric* instance, FlipperFormat* flipper_format) {
  79. furi_assert(instance);
  80. bool res = false;
  81. string_t temp_str;
  82. string_init(temp_str);
  83. uint32_t temp_data = 0;
  84. do {
  85. if(!flipper_format_rewind(flipper_format)) {
  86. FURI_LOG_E(TAG, "Rewind error");
  87. break;
  88. }
  89. if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
  90. FURI_LOG_E(TAG, "Missing Bit");
  91. break;
  92. }
  93. instance->data_count_bit = (uint8_t)temp_data;
  94. uint8_t key_data[sizeof(uint64_t)] = {0};
  95. if(!flipper_format_read_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  96. FURI_LOG_E(TAG, "Missing Key");
  97. break;
  98. }
  99. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  100. instance->data = instance->data << 8 | key_data[i];
  101. }
  102. res = true;
  103. } while(0);
  104. string_clear(temp_str);
  105. return res;
  106. }