ws_generic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "ws_generic.h"
  2. #include <lib/toolbox/stream/stream.h>
  3. #include <lib/flipper_format/flipper_format_i.h>
  4. #include "../helpers/weather_station_types.h"
  5. #define TAG "WSBlockGeneric"
  6. void ws_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. bool ws_block_generic_serialize(
  22. WSBlockGeneric* instance,
  23. FlipperFormat* flipper_format,
  24. SubGhzRadioPreset* preset) {
  25. furi_assert(instance);
  26. bool res = false;
  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, WS_KEY_FILE_TYPE, WS_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. ws_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. uint32_t temp_data = instance->id;
  63. if(!flipper_format_write_uint32(flipper_format, "Id", &temp_data, 1)) {
  64. FURI_LOG_E(TAG, "Unable to add Id");
  65. break;
  66. }
  67. temp_data = instance->data_count_bit;
  68. if(!flipper_format_write_uint32(flipper_format, "Bit", &temp_data, 1)) {
  69. FURI_LOG_E(TAG, "Unable to add Bit");
  70. break;
  71. }
  72. uint8_t key_data[sizeof(uint64_t)] = {0};
  73. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  74. key_data[sizeof(uint64_t) - i - 1] = (instance->data >> i * 8) & 0xFF;
  75. }
  76. if(!flipper_format_write_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
  77. FURI_LOG_E(TAG, "Unable to add Data");
  78. break;
  79. }
  80. temp_data = instance->battery_low;
  81. if(!flipper_format_write_uint32(flipper_format, "Batt", &temp_data, 1)) {
  82. FURI_LOG_E(TAG, "Unable to add Battery_low");
  83. break;
  84. }
  85. temp_data = instance->humidity;
  86. if(!flipper_format_write_uint32(flipper_format, "Hum", &temp_data, 1)) {
  87. FURI_LOG_E(TAG, "Unable to add Humidity");
  88. break;
  89. }
  90. temp_data = instance->channel;
  91. if(!flipper_format_write_uint32(flipper_format, "Ch", &temp_data, 1)) {
  92. FURI_LOG_E(TAG, "Unable to add Channel");
  93. break;
  94. }
  95. temp_data = instance->btn;
  96. if(!flipper_format_write_uint32(flipper_format, "Btn", &temp_data, 1)) {
  97. FURI_LOG_E(TAG, "Unable to add Btn");
  98. break;
  99. }
  100. float temp = instance->temp;
  101. if(!flipper_format_write_float(flipper_format, "Temp", &temp, 1)) {
  102. FURI_LOG_E(TAG, "Unable to add Temperature");
  103. break;
  104. }
  105. res = true;
  106. } while(false);
  107. furi_string_free(temp_str);
  108. return res;
  109. }
  110. bool ws_block_generic_deserialize(WSBlockGeneric* instance, FlipperFormat* flipper_format) {
  111. furi_assert(instance);
  112. bool res = false;
  113. uint32_t temp_data = 0;
  114. do {
  115. if(!flipper_format_rewind(flipper_format)) {
  116. FURI_LOG_E(TAG, "Rewind error");
  117. break;
  118. }
  119. if(!flipper_format_read_uint32(flipper_format, "Id", (uint32_t*)&temp_data, 1)) {
  120. FURI_LOG_E(TAG, "Missing Id");
  121. break;
  122. }
  123. instance->id = (uint32_t)temp_data;
  124. if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
  125. FURI_LOG_E(TAG, "Missing Bit");
  126. break;
  127. }
  128. instance->data_count_bit = (uint8_t)temp_data;
  129. uint8_t key_data[sizeof(uint64_t)] = {0};
  130. if(!flipper_format_read_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
  131. FURI_LOG_E(TAG, "Missing Data");
  132. break;
  133. }
  134. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  135. instance->data = instance->data << 8 | key_data[i];
  136. }
  137. if(!flipper_format_read_uint32(flipper_format, "Batt", (uint32_t*)&temp_data, 1)) {
  138. FURI_LOG_E(TAG, "Missing Battery_low");
  139. break;
  140. }
  141. instance->battery_low = (uint8_t)temp_data;
  142. if(!flipper_format_read_uint32(flipper_format, "Hum", (uint32_t*)&temp_data, 1)) {
  143. FURI_LOG_E(TAG, "Missing Humidity");
  144. break;
  145. }
  146. instance->humidity = (uint8_t)temp_data;
  147. if(!flipper_format_read_uint32(flipper_format, "Ch", (uint32_t*)&temp_data, 1)) {
  148. FURI_LOG_E(TAG, "Missing Channel");
  149. break;
  150. }
  151. instance->channel = (uint8_t)temp_data;
  152. if(!flipper_format_read_uint32(flipper_format, "Btn", (uint32_t*)&temp_data, 1)) {
  153. FURI_LOG_E(TAG, "Missing Btn");
  154. break;
  155. }
  156. instance->btn = (uint8_t)temp_data;
  157. float temp;
  158. if(!flipper_format_read_float(flipper_format, "Temp", (float*)&temp, 1)) {
  159. FURI_LOG_E(TAG, "Missing Temperature");
  160. break;
  161. }
  162. instance->temp = temp;
  163. res = true;
  164. } while(0);
  165. return res;
  166. }
  167. float ws_block_generic_fahrenheit_to_celsius(float fahrenheit) {
  168. return (fahrenheit - 32.0f) / 1.8f;
  169. }