subbrute_radio_device_loader.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "subbrute_radio_device_loader.h"
  2. #include <furi/core/check.h>
  3. #include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  4. #include "applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h"
  5. #define TAG "SubBruteRadioDeviceLoader"
  6. /**
  7. * @brief This function is responsible for powering on the radio device loader.
  8. *
  9. * This function is responsible for powering on the radio device loader.
  10. * It attempts to enable the OTG (On-The-Go) power.
  11. * The function uses a @c `while` loop with a maximum of 5 attempts to enable the OTG power.
  12. * If it succeeds, it will break out of the loop.
  13. * If the maximum number of attempts is reached and the OTG power is not enabled,
  14. * the function checks the USB voltage using @c `furi_hal_power_get_usb_voltage()`.
  15. *
  16. * If the voltage is below 4.5 volts, an error message is logged using @c `FURI_LOG_E()`
  17. * with a message includes the value of @c `furi_hal_power_check_otg_fault()`
  18. * (converted to 1 if true, 0 if false),
  19. * which checks the OTG fault status using the BQ2589 chip.
  20. *
  21. */
  22. static void subbrute_radio_device_loader_power_on() {
  23. uint8_t attempts = 5;
  24. while(--attempts > 0) {
  25. if(furi_hal_power_enable_otg()) {
  26. break;
  27. }
  28. }
  29. if(attempts == 0) {
  30. if(furi_hal_power_get_usb_voltage() < 4.5f) {
  31. FURI_LOG_E(
  32. TAG,
  33. "Error power otg enable. BQ2589 check otg fault = %d",
  34. furi_hal_power_check_otg_fault() ? 1 : 0);
  35. }
  36. }
  37. }
  38. /**
  39. * @brief Turns off the power of the radio device loader.
  40. *
  41. * This function checks if OTG (On-The-Go) power is enabled, and if so, it disables it.
  42. *
  43. * @note This function is static and used internally by the radio device loader module.
  44. *
  45. */
  46. static void subbrute_radio_device_loader_power_off() {
  47. if(furi_hal_power_is_otg_enabled()) {
  48. furi_hal_power_disable_otg();
  49. }
  50. }
  51. /**
  52. * @brief Checks if a radio device of the given name is connected externally.
  53. *
  54. * This function checks if a radio device with the specified name is connected externally.
  55. * If the OTG power is not enabled, it attempts to power on the radio device loader.
  56. * It then retrieves the device information using the provided name and checks if it is connected.
  57. * Finally, if the OTG power was initially disabled, it powers off the radio device loader.
  58. *
  59. * @param[in] name The name of the radio device to check.
  60. *
  61. * @return true if the device is connected externally, false otherwise.
  62. *
  63. */
  64. static bool subbrute_radio_device_loader_is_connect_external(const char* name) {
  65. bool is_connect = false;
  66. bool is_otg_enabled = furi_hal_power_is_otg_enabled();
  67. if(!is_otg_enabled) {
  68. subbrute_radio_device_loader_power_on();
  69. }
  70. const SubGhzDevice* device = subghz_devices_get_by_name(name);
  71. if(device) {
  72. is_connect = subghz_devices_is_connect(device);
  73. }
  74. if(!is_otg_enabled) {
  75. subbrute_radio_device_loader_power_off();
  76. }
  77. return is_connect;
  78. }
  79. const SubGhzDevice* subbrute_radio_device_loader_set(
  80. const SubGhzDevice* current_radio_device,
  81. SubGhzRadioDeviceType radio_device_type) {
  82. const SubGhzDevice* radio_device;
  83. if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
  84. subbrute_radio_device_loader_is_connect_external(SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
  85. subbrute_radio_device_loader_power_on();
  86. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
  87. subghz_devices_begin(radio_device);
  88. } else if(current_radio_device == NULL) {
  89. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  90. } else {
  91. subbrute_radio_device_loader_end(current_radio_device);
  92. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  93. }
  94. return radio_device;
  95. }
  96. void subbrute_radio_device_loader_end(const SubGhzDevice* radio_device) {
  97. furi_assert(radio_device);
  98. subbrute_radio_device_loader_power_off();
  99. if(radio_device != subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME)) {
  100. subghz_devices_end(radio_device);
  101. }
  102. }