|
|
|
|
@@ -1,8 +1,28 @@
|
|
|
|
|
#include "subghz_txrx_i.h"
|
|
|
|
|
|
|
|
|
|
#include <lib/subghz/protocols/protocol_items.h>
|
|
|
|
|
#include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
|
|
|
|
|
#include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
|
|
|
|
|
|
|
|
|
|
#include <lib/subghz/blocks/custom_btn.h>
|
|
|
|
|
|
|
|
|
|
#define TAG "SubGhz"
|
|
|
|
|
|
|
|
|
|
static void subghz_txrx_radio_device_power_on(SubGhzTxRx* instance) {
|
|
|
|
|
UNUSED(instance);
|
|
|
|
|
uint8_t attempts = 0;
|
|
|
|
|
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
|
|
|
|
|
furi_hal_power_enable_otg();
|
|
|
|
|
//CC1101 power-up time
|
|
|
|
|
furi_delay_ms(10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subghz_txrx_radio_device_power_off(SubGhzTxRx* instance) {
|
|
|
|
|
UNUSED(instance);
|
|
|
|
|
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubGhzTxRx* subghz_txrx_alloc() {
|
|
|
|
|
SubGhzTxRx* instance = malloc(sizeof(SubGhzTxRx));
|
|
|
|
|
instance->setting = subghz_setting_alloc();
|
|
|
|
|
@@ -23,16 +43,15 @@ SubGhzTxRx* subghz_txrx_alloc() {
|
|
|
|
|
instance->fff_data = flipper_format_string_alloc();
|
|
|
|
|
|
|
|
|
|
instance->environment = subghz_environment_alloc();
|
|
|
|
|
instance->is_database_loaded = subghz_environment_load_keystore(
|
|
|
|
|
instance->environment, EXT_PATH("subghz/assets/keeloq_mfcodes"));
|
|
|
|
|
subghz_environment_load_keystore(
|
|
|
|
|
instance->environment, EXT_PATH("subghz/assets/keeloq_mfcodes_user"));
|
|
|
|
|
instance->is_database_loaded =
|
|
|
|
|
subghz_environment_load_keystore(instance->environment, SUBGHZ_KEYSTORE_DIR_NAME);
|
|
|
|
|
subghz_environment_load_keystore(instance->environment, SUBGHZ_KEYSTORE_DIR_USER_NAME);
|
|
|
|
|
subghz_environment_set_came_atomo_rainbow_table_file_name(
|
|
|
|
|
instance->environment, EXT_PATH("subghz/assets/came_atomo"));
|
|
|
|
|
instance->environment, SUBGHZ_CAME_ATOMO_DIR_NAME);
|
|
|
|
|
subghz_environment_set_alutech_at_4n_rainbow_table_file_name(
|
|
|
|
|
instance->environment, EXT_PATH("subghz/assets/alutech_at_4n"));
|
|
|
|
|
instance->environment, SUBGHZ_ALUTECH_AT_4N_DIR_NAME);
|
|
|
|
|
subghz_environment_set_nice_flor_s_rainbow_table_file_name(
|
|
|
|
|
instance->environment, EXT_PATH("subghz/assets/nice_flor_s"));
|
|
|
|
|
instance->environment, SUBGHZ_NICE_FLOR_S_DIR_NAME);
|
|
|
|
|
subghz_environment_set_protocol_registry(
|
|
|
|
|
instance->environment, (void*)&subghz_protocol_registry);
|
|
|
|
|
instance->receiver = subghz_receiver_alloc_init(instance->environment);
|
|
|
|
|
@@ -43,18 +62,31 @@ SubGhzTxRx* subghz_txrx_alloc() {
|
|
|
|
|
instance->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
|
|
|
|
|
subghz_worker_set_context(instance->worker, instance->receiver);
|
|
|
|
|
|
|
|
|
|
//set default device External
|
|
|
|
|
subghz_devices_init();
|
|
|
|
|
instance->radio_device_type =
|
|
|
|
|
subghz_txrx_radio_device_set(instance, SubGhzRadioDeviceTypeExternalCC1101);
|
|
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void subghz_txrx_free(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
|
|
if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
|
|
|
|
|
subghz_txrx_radio_device_power_off(instance);
|
|
|
|
|
subghz_devices_end(instance->radio_device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subghz_devices_deinit();
|
|
|
|
|
|
|
|
|
|
subghz_worker_free(instance->worker);
|
|
|
|
|
subghz_receiver_free(instance->receiver);
|
|
|
|
|
subghz_environment_free(instance->environment);
|
|
|
|
|
flipper_format_free(instance->fff_data);
|
|
|
|
|
furi_string_free(instance->preset->name);
|
|
|
|
|
subghz_setting_free(instance->setting);
|
|
|
|
|
|
|
|
|
|
free(instance->preset);
|
|
|
|
|
free(instance);
|
|
|
|
|
}
|
|
|
|
|
@@ -127,29 +159,29 @@ void subghz_txrx_get_frequency_and_modulation(
|
|
|
|
|
|
|
|
|
|
static void subghz_txrx_begin(SubGhzTxRx* instance, uint8_t* preset_data) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
furi_hal_subghz_reset();
|
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
|
furi_hal_subghz_load_custom_preset(preset_data);
|
|
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
subghz_devices_reset(instance->radio_device);
|
|
|
|
|
subghz_devices_idle(instance->radio_device);
|
|
|
|
|
subghz_devices_load_preset(instance->radio_device, FuriHalSubGhzPresetCustom, preset_data);
|
|
|
|
|
instance->txrx_state = SubGhzTxRxStateIDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t subghz_txrx_rx(SubGhzTxRx* instance, uint32_t frequency) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
// TODO
|
|
|
|
|
if(!furi_hal_subghz_is_frequency_valid(frequency)) {
|
|
|
|
|
furi_crash("SubGhz: Incorrect RX frequency.");
|
|
|
|
|
}
|
|
|
|
|
furi_assert(
|
|
|
|
|
instance->txrx_state != SubGhzTxRxStateRx && instance->txrx_state != SubGhzTxRxStateSleep);
|
|
|
|
|
|
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
|
uint32_t value = furi_hal_subghz_set_frequency_and_path(frequency);
|
|
|
|
|
furi_hal_gpio_init(furi_hal_subghz.cc1101_g0_pin, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
furi_hal_subghz_flush_rx();
|
|
|
|
|
subghz_txrx_speaker_on(instance);
|
|
|
|
|
furi_hal_subghz_rx();
|
|
|
|
|
subghz_devices_idle(instance->radio_device);
|
|
|
|
|
|
|
|
|
|
furi_hal_subghz_start_async_rx(subghz_worker_rx_callback, instance->worker);
|
|
|
|
|
uint32_t value = subghz_devices_set_frequency(instance->radio_device, frequency);
|
|
|
|
|
subghz_devices_flush_rx(instance->radio_device);
|
|
|
|
|
subghz_txrx_speaker_on(instance);
|
|
|
|
|
|
|
|
|
|
subghz_devices_start_async_rx(
|
|
|
|
|
instance->radio_device, subghz_worker_rx_callback, instance->worker);
|
|
|
|
|
subghz_worker_start(instance->worker);
|
|
|
|
|
instance->txrx_state = SubGhzTxRxStateRx;
|
|
|
|
|
return value;
|
|
|
|
|
@@ -158,7 +190,7 @@ static uint32_t subghz_txrx_rx(SubGhzTxRx* instance, uint32_t frequency) {
|
|
|
|
|
static void subghz_txrx_idle(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
furi_assert(instance->txrx_state != SubGhzTxRxStateSleep);
|
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
|
subghz_devices_idle(instance->radio_device);
|
|
|
|
|
subghz_txrx_speaker_off(instance);
|
|
|
|
|
instance->txrx_state = SubGhzTxRxStateIDLE;
|
|
|
|
|
}
|
|
|
|
|
@@ -169,31 +201,30 @@ static void subghz_txrx_rx_end(SubGhzTxRx* instance) {
|
|
|
|
|
|
|
|
|
|
if(subghz_worker_is_running(instance->worker)) {
|
|
|
|
|
subghz_worker_stop(instance->worker);
|
|
|
|
|
furi_hal_subghz_stop_async_rx();
|
|
|
|
|
subghz_devices_stop_async_rx(instance->radio_device);
|
|
|
|
|
}
|
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
|
subghz_devices_idle(instance->radio_device);
|
|
|
|
|
subghz_txrx_speaker_off(instance);
|
|
|
|
|
instance->txrx_state = SubGhzTxRxStateIDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void subghz_txrx_sleep(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
furi_hal_subghz_sleep();
|
|
|
|
|
subghz_devices_sleep(instance->radio_device);
|
|
|
|
|
instance->txrx_state = SubGhzTxRxStateSleep;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool subghz_txrx_tx(SubGhzTxRx* instance, uint32_t frequency) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
// TODO
|
|
|
|
|
if(!furi_hal_subghz_is_frequency_valid(frequency)) {
|
|
|
|
|
furi_crash("SubGhz: Incorrect TX frequency.");
|
|
|
|
|
}
|
|
|
|
|
furi_assert(instance->txrx_state != SubGhzTxRxStateSleep);
|
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
|
furi_hal_subghz_set_frequency_and_path(frequency);
|
|
|
|
|
furi_hal_gpio_write(furi_hal_subghz.cc1101_g0_pin, false);
|
|
|
|
|
furi_hal_gpio_init(
|
|
|
|
|
furi_hal_subghz.cc1101_g0_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
bool ret = furi_hal_subghz_tx();
|
|
|
|
|
subghz_devices_idle(instance->radio_device);
|
|
|
|
|
subghz_devices_set_frequency(instance->radio_device, frequency);
|
|
|
|
|
|
|
|
|
|
bool ret = subghz_devices_set_tx(instance->radio_device);
|
|
|
|
|
if(ret) {
|
|
|
|
|
subghz_txrx_speaker_on(instance);
|
|
|
|
|
instance->txrx_state = SubGhzTxRxStateTx;
|
|
|
|
|
@@ -255,8 +286,8 @@ SubGhzTxRxStartTxState subghz_txrx_tx_start(SubGhzTxRx* instance, FlipperFormat*
|
|
|
|
|
|
|
|
|
|
if(ret == SubGhzTxRxStartTxStateOk) {
|
|
|
|
|
//Start TX
|
|
|
|
|
furi_hal_subghz_start_async_tx(
|
|
|
|
|
subghz_transmitter_yield, instance->transmitter);
|
|
|
|
|
subghz_devices_start_async_tx(
|
|
|
|
|
instance->radio_device, subghz_transmitter_yield, instance->transmitter);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ret = SubGhzTxRxStartTxStateErrorParserOthers;
|
|
|
|
|
@@ -299,7 +330,7 @@ static void subghz_txrx_tx_stop(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
furi_assert(instance->txrx_state == SubGhzTxRxStateTx);
|
|
|
|
|
//Stop TX
|
|
|
|
|
furi_hal_subghz_stop_async_tx();
|
|
|
|
|
subghz_devices_stop_async_tx(instance->radio_device);
|
|
|
|
|
subghz_transmitter_stop(instance->transmitter);
|
|
|
|
|
subghz_transmitter_free(instance->transmitter);
|
|
|
|
|
|
|
|
|
|
@@ -312,7 +343,6 @@ static void subghz_txrx_tx_stop(SubGhzTxRx* instance) {
|
|
|
|
|
subghz_txrx_idle(instance);
|
|
|
|
|
subghz_txrx_speaker_off(instance);
|
|
|
|
|
//Todo: Show message
|
|
|
|
|
// notification_message(notifications, &sequence_reset_red);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FlipperFormat* subghz_txrx_get_fff_data(SubGhzTxRx* instance) {
|
|
|
|
|
@@ -362,7 +392,7 @@ void subghz_txrx_hopper_update(SubGhzTxRx* instance) {
|
|
|
|
|
float rssi = -127.0f;
|
|
|
|
|
if(instance->hopper_state != SubGhzHopperStateRSSITimeOut) {
|
|
|
|
|
// See RSSI Calculation timings in CC1101 17.3 RSSI
|
|
|
|
|
rssi = furi_hal_subghz_get_rssi();
|
|
|
|
|
rssi = subghz_devices_get_rssi(instance->radio_device);
|
|
|
|
|
|
|
|
|
|
// Stay if RSSI is high enough
|
|
|
|
|
if(rssi > -90.0f) {
|
|
|
|
|
@@ -419,13 +449,13 @@ void subghz_txrx_hopper_pause(SubGhzTxRx* instance) {
|
|
|
|
|
void subghz_txrx_speaker_on(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
if(instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(&gpio_ibutton);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_ibutton);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(instance->speaker_state == SubGhzSpeakerStateEnable) {
|
|
|
|
|
if(furi_hal_speaker_acquire(30)) {
|
|
|
|
|
if(!instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(&gpio_speaker);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
instance->speaker_state = SubGhzSpeakerStateDisable;
|
|
|
|
|
@@ -436,12 +466,12 @@ void subghz_txrx_speaker_on(SubGhzTxRx* instance) {
|
|
|
|
|
void subghz_txrx_speaker_off(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
if(instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(NULL);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
|
|
|
|
|
}
|
|
|
|
|
if(instance->speaker_state != SubGhzSpeakerStateDisable) {
|
|
|
|
|
if(furi_hal_speaker_is_mine()) {
|
|
|
|
|
if(!instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(NULL);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
|
|
|
|
|
}
|
|
|
|
|
furi_hal_speaker_release();
|
|
|
|
|
if(instance->speaker_state == SubGhzSpeakerStateShutdown)
|
|
|
|
|
@@ -453,12 +483,12 @@ void subghz_txrx_speaker_off(SubGhzTxRx* instance) {
|
|
|
|
|
void subghz_txrx_speaker_mute(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
if(instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(NULL);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
|
|
|
|
|
}
|
|
|
|
|
if(instance->speaker_state == SubGhzSpeakerStateEnable) {
|
|
|
|
|
if(furi_hal_speaker_is_mine()) {
|
|
|
|
|
if(!instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(NULL);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -467,12 +497,12 @@ void subghz_txrx_speaker_mute(SubGhzTxRx* instance) {
|
|
|
|
|
void subghz_txrx_speaker_unmute(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
if(instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(&gpio_ibutton);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_ibutton);
|
|
|
|
|
}
|
|
|
|
|
if(instance->speaker_state == SubGhzSpeakerStateEnable) {
|
|
|
|
|
if(furi_hal_speaker_is_mine()) {
|
|
|
|
|
if(!instance->debug_pin_state) {
|
|
|
|
|
furi_hal_subghz_set_async_mirror_pin(&gpio_speaker);
|
|
|
|
|
subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -547,6 +577,66 @@ void subghz_txrx_set_raw_file_encoder_worker_callback_end(
|
|
|
|
|
context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool subghz_txrx_radio_device_is_connect_external(SubGhzTxRx* instance, const char* name) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
|
|
bool is_connect = false;
|
|
|
|
|
bool is_otg_enabled = furi_hal_power_is_otg_enabled();
|
|
|
|
|
|
|
|
|
|
if(!is_otg_enabled) {
|
|
|
|
|
subghz_txrx_radio_device_power_on(instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is_connect = subghz_devices_is_connect(subghz_devices_get_by_name(name));
|
|
|
|
|
|
|
|
|
|
if(!is_otg_enabled) {
|
|
|
|
|
subghz_txrx_radio_device_power_off(instance);
|
|
|
|
|
}
|
|
|
|
|
return is_connect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubGhzRadioDeviceType
|
|
|
|
|
subghz_txrx_radio_device_set(SubGhzTxRx* instance, SubGhzRadioDeviceType radio_device_type) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
|
|
if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
|
|
|
|
|
subghz_txrx_radio_device_is_connect_external(instance, SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
|
|
|
|
|
subghz_txrx_radio_device_power_on(instance);
|
|
|
|
|
instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
|
|
|
|
|
subghz_devices_begin(instance->radio_device);
|
|
|
|
|
instance->radio_device_type = SubGhzRadioDeviceTypeExternalCC1101;
|
|
|
|
|
} else {
|
|
|
|
|
subghz_txrx_radio_device_power_off(instance);
|
|
|
|
|
if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
|
|
|
|
|
subghz_devices_end(instance->radio_device);
|
|
|
|
|
}
|
|
|
|
|
instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
|
|
|
|
|
instance->radio_device_type = SubGhzRadioDeviceTypeInternal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return instance->radio_device_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubGhzRadioDeviceType subghz_txrx_radio_device_get(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
return instance->radio_device_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float subghz_txrx_radio_device_get_rssi(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
return subghz_devices_get_rssi(instance->radio_device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* subghz_txrx_radio_device_get_name(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
return subghz_devices_get_name(instance->radio_device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool subghz_txrx_radio_device_is_frequecy_valid(SubGhzTxRx* instance, uint32_t frequency) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
return subghz_devices_is_frequency_valid(instance->radio_device, frequency);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void subghz_txrx_set_debug_pin_state(SubGhzTxRx* instance, bool state) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
instance->debug_pin_state = state;
|
|
|
|
|
@@ -557,6 +647,13 @@ bool subghz_txrx_get_debug_pin_state(SubGhzTxRx* instance) {
|
|
|
|
|
return instance->debug_pin_state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void subghz_txrx_reset_dynamic_and_custom_btns(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
subghz_environment_reset_keeloq(instance->environment);
|
|
|
|
|
|
|
|
|
|
subghz_custom_btns_reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubGhzReceiver* subghz_txrx_get_receiver(SubGhzTxRx* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
return instance->receiver;
|
|
|
|
|
|