subbrute_radio_device_loader.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //#include "../../../lib/subghz/protocols/protocol_items.h"
  2. //#include "../../../firmware//targets/f7/furi_hal/furi_hal_subghz.h"
  3. #include <furi/core/check.h>
  4. //#include "../../../furi/core/check.h"
  5. // furi/core/check.h
  6. #include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
  7. #include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  8. #include <applications/main/subghz/helpers/subghz_types.h>
  9. #include <lib/subghz/devices/devices.h>
  10. #include "subbrute_radio_device_loader.h"
  11. #define TAG "SubBruteRadioDeviceLoader"
  12. static void subbrute_radio_device_loader_power_on() {
  13. uint8_t attempts = 5;
  14. while(--attempts > 0) {
  15. if(furi_hal_power_enable_otg()) {
  16. break;
  17. }
  18. }
  19. if(attempts == 0) {
  20. if(furi_hal_power_get_usb_voltage() < 4.5f) {
  21. FURI_LOG_E(
  22. TAG,
  23. "Error power otg enable. BQ2589 check otg fault = %d",
  24. furi_hal_power_check_otg_fault() ? 1 : 0);
  25. }
  26. }
  27. }
  28. static void subbrute_radio_device_loader_power_off() {
  29. if(furi_hal_power_is_otg_enabled()) {
  30. furi_hal_power_disable_otg();
  31. }
  32. }
  33. bool subbrute_radio_device_loader_is_connect_external(const char* name) {
  34. bool is_connect = false;
  35. bool is_otg_enabled = furi_hal_power_is_otg_enabled();
  36. if(!is_otg_enabled) {
  37. subbrute_radio_device_loader_power_on();
  38. }
  39. const SubGhzDevice* device = subghz_devices_get_by_name(name);
  40. if(device) {
  41. is_connect = subghz_devices_is_connect(device);
  42. }
  43. if(!is_otg_enabled) {
  44. subbrute_radio_device_loader_power_off();
  45. }
  46. return is_connect;
  47. }
  48. const SubGhzDevice* subbrute_radio_device_loader_set(
  49. const SubGhzDevice* current_radio_device,
  50. SubGhzRadioDeviceType radio_device_type) {
  51. const SubGhzDevice* radio_device;
  52. if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
  53. subbrute_radio_device_loader_is_connect_external(SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
  54. subbrute_radio_device_loader_power_on();
  55. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
  56. subghz_devices_begin(radio_device);
  57. } else if(current_radio_device == NULL) {
  58. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  59. } else {
  60. subbrute_radio_device_loader_end(current_radio_device);
  61. radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  62. }
  63. return radio_device;
  64. }
  65. void subbrute_radio_device_loader_end(const SubGhzDevice* radio_device) {
  66. furi_assert(radio_device);
  67. subbrute_radio_device_loader_power_off();
  68. if(radio_device != subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME)) {
  69. subghz_devices_end(radio_device);
  70. }
  71. }