2021-12-01 18:44:39 +03:00
|
|
|
#include "subghz_tx_rx_worker.h"
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
#define TAG "SubGhzTxRxWorker"
|
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
#define SUBGHZ_TXRX_WORKER_BUF_SIZE 2048
|
2021-12-01 18:44:39 +03:00
|
|
|
//you can not set more than 62 because it will not fit into the FIFO CC1101
|
2021-12-08 16:42:01 +03:00
|
|
|
#define SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE 60
|
2021-12-01 18:44:39 +03:00
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
#define SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF 40
|
2021-12-01 18:44:39 +03:00
|
|
|
|
|
|
|
struct SubGhzTxRxWorker {
|
|
|
|
FuriThread* thread;
|
2022-10-07 15:27:11 +03:00
|
|
|
FuriStreamBuffer* stream_tx;
|
|
|
|
FuriStreamBuffer* stream_rx;
|
2021-12-01 18:44:39 +03:00
|
|
|
|
|
|
|
volatile bool worker_running;
|
|
|
|
volatile bool worker_stoping;
|
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
SubGhzTxRxWorkerStatus status;
|
2021-12-01 18:44:39 +03:00
|
|
|
|
|
|
|
uint32_t frequency;
|
2023-06-30 17:03:36 +03:00
|
|
|
const SubGhzDevice* device;
|
|
|
|
const GpioPin* device_data_gpio;
|
2021-12-01 18:44:39 +03:00
|
|
|
|
|
|
|
SubGhzTxRxWorkerCallbackHaveRead callback_have_read;
|
|
|
|
void* context_have_read;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool subghz_tx_rx_worker_write(SubGhzTxRxWorker* instance, uint8_t* data, size_t size) {
|
|
|
|
furi_assert(instance);
|
|
|
|
bool ret = false;
|
2022-10-07 15:27:11 +03:00
|
|
|
size_t stream_tx_free_byte = furi_stream_buffer_spaces_available(instance->stream_tx);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(size && (stream_tx_free_byte >= size)) {
|
2022-10-07 15:27:11 +03:00
|
|
|
if(furi_stream_buffer_send(
|
2021-12-08 16:42:01 +03:00
|
|
|
instance->stream_tx, data, size, SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF) ==
|
2021-12-01 18:44:39 +03:00
|
|
|
size) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t subghz_tx_rx_worker_available(SubGhzTxRxWorker* instance) {
|
|
|
|
furi_assert(instance);
|
2022-10-07 15:27:11 +03:00
|
|
|
return furi_stream_buffer_bytes_available(instance->stream_rx);
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t subghz_tx_rx_worker_read(SubGhzTxRxWorker* instance, uint8_t* data, size_t size) {
|
|
|
|
furi_assert(instance);
|
2022-10-07 15:27:11 +03:00
|
|
|
return furi_stream_buffer_receive(instance->stream_rx, data, size, 0);
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_tx_rx_worker_set_callback_have_read(
|
|
|
|
SubGhzTxRxWorker* instance,
|
|
|
|
SubGhzTxRxWorkerCallbackHaveRead callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(callback);
|
|
|
|
furi_assert(context);
|
|
|
|
instance->callback_have_read = callback;
|
|
|
|
instance->context_have_read = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_tx_rx_worker_rx(SubGhzTxRxWorker* instance, uint8_t* data, uint8_t* size) {
|
2021-12-08 16:42:01 +03:00
|
|
|
uint8_t timeout = 100;
|
2021-12-01 18:44:39 +03:00
|
|
|
bool ret = false;
|
2021-12-08 16:42:01 +03:00
|
|
|
if(instance->status != SubGhzTxRxWorkerStatusRx) {
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_set_rx(instance->device);
|
2021-12-08 16:42:01 +03:00
|
|
|
instance->status = SubGhzTxRxWorkerStatusRx;
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_tick(1);
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
//waiting for reception to complete
|
2023-06-30 17:03:36 +03:00
|
|
|
while(furi_hal_gpio_read(instance->device_data_gpio)) {
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_tick(1);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(!--timeout) {
|
|
|
|
FURI_LOG_W(TAG, "RX cc1101_g0 timeout");
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_flush_rx(instance->device);
|
|
|
|
subghz_devices_set_rx(instance->device);
|
2021-12-01 18:44:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 17:03:36 +03:00
|
|
|
if(subghz_devices_rx_pipe_not_empty(instance->device)) {
|
2021-12-08 16:42:01 +03:00
|
|
|
FURI_LOG_I(
|
2022-05-11 12:45:01 +03:00
|
|
|
TAG,
|
|
|
|
"RSSI: %03.1fdbm LQI: %d",
|
2023-06-30 17:03:36 +03:00
|
|
|
(double)subghz_devices_get_rssi(instance->device),
|
|
|
|
subghz_devices_get_lqi(instance->device));
|
|
|
|
if(subghz_devices_is_rx_data_crc_valid(instance->device)) {
|
|
|
|
subghz_devices_read_packet(instance->device, data, size);
|
2021-12-01 18:44:39 +03:00
|
|
|
ret = true;
|
|
|
|
}
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_flush_rx(instance->device);
|
|
|
|
subghz_devices_set_rx(instance->device);
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_tx_rx_worker_tx(SubGhzTxRxWorker* instance, uint8_t* data, size_t size) {
|
2021-12-08 16:42:01 +03:00
|
|
|
uint8_t timeout = 200;
|
|
|
|
if(instance->status != SubGhzTxRxWorkerStatusIDLE) {
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_idle(instance->device);
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_write_packet(instance->device, data, size);
|
|
|
|
subghz_devices_set_tx(instance->device); //start send
|
2021-12-08 16:42:01 +03:00
|
|
|
instance->status = SubGhzTxRxWorkerStatusTx;
|
2023-06-30 17:03:36 +03:00
|
|
|
while(!furi_hal_gpio_read(
|
|
|
|
instance->device_data_gpio)) { // Wait for GDO0 to be set -> sync transmitted
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_tick(1);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(!--timeout) {
|
|
|
|
FURI_LOG_W(TAG, "TX !cc1101_g0 timeout");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 17:03:36 +03:00
|
|
|
while(furi_hal_gpio_read(
|
|
|
|
instance->device_data_gpio)) { // Wait for GDO0 to be cleared -> end of packet
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_tick(1);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(!--timeout) {
|
|
|
|
FURI_LOG_W(TAG, "TX cc1101_g0 timeout");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_idle(instance->device);
|
2021-12-08 16:42:01 +03:00
|
|
|
instance->status = SubGhzTxRxWorkerStatusIDLE;
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
/** Worker thread
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return exit code
|
|
|
|
*/
|
|
|
|
static int32_t subghz_tx_rx_worker_thread(void* context) {
|
|
|
|
SubGhzTxRxWorker* instance = context;
|
2023-06-30 17:03:36 +03:00
|
|
|
furi_assert(instance->device);
|
2021-12-01 18:44:39 +03:00
|
|
|
FURI_LOG_I(TAG, "Worker start");
|
|
|
|
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_begin(instance->device);
|
|
|
|
instance->device_data_gpio = subghz_devices_get_data_gpio(instance->device);
|
|
|
|
subghz_devices_reset(instance->device);
|
|
|
|
subghz_devices_idle(instance->device);
|
|
|
|
subghz_devices_load_preset(instance->device, FuriHalSubGhzPresetGFSK9_99KbAsync, NULL);
|
2021-12-01 18:44:39 +03:00
|
|
|
|
2023-06-30 17:03:36 +03:00
|
|
|
furi_hal_gpio_init(instance->device_data_gpio, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
|
|
|
|
|
|
|
subghz_devices_set_frequency(instance->device, instance->frequency);
|
|
|
|
subghz_devices_flush_rx(instance->device);
|
2021-12-01 18:44:39 +03:00
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
uint8_t data[SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE + 1] = {0};
|
2021-12-01 18:44:39 +03:00
|
|
|
size_t size_tx = 0;
|
|
|
|
uint8_t size_rx[1] = {0};
|
|
|
|
uint8_t timeout_tx = 0;
|
|
|
|
bool callback_rx = false;
|
|
|
|
|
|
|
|
while(instance->worker_running) {
|
|
|
|
//transmit
|
2022-10-07 15:27:11 +03:00
|
|
|
size_tx = furi_stream_buffer_bytes_available(instance->stream_tx);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(size_tx > 0 && !timeout_tx) {
|
2021-12-08 16:42:01 +03:00
|
|
|
timeout_tx = 10; //20ms
|
|
|
|
if(size_tx > SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE) {
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_receive(
|
2021-12-01 18:44:39 +03:00
|
|
|
instance->stream_tx,
|
|
|
|
&data,
|
2021-12-08 16:42:01 +03:00
|
|
|
SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE,
|
|
|
|
SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF);
|
|
|
|
subghz_tx_rx_worker_tx(instance, data, SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE);
|
2021-12-01 18:44:39 +03:00
|
|
|
} else {
|
2023-08-22 20:56:27 +03:00
|
|
|
//TODO FL-3554: checking that it managed to write all the data to the TX buffer
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_receive(
|
2021-12-08 16:42:01 +03:00
|
|
|
instance->stream_tx, &data, size_tx, SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF);
|
2021-12-01 18:44:39 +03:00
|
|
|
subghz_tx_rx_worker_tx(instance, data, size_tx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//recive
|
|
|
|
if(subghz_tx_rx_worker_rx(instance, data, size_rx)) {
|
2022-10-07 15:27:11 +03:00
|
|
|
if(furi_stream_buffer_spaces_available(instance->stream_rx) >= size_rx[0]) {
|
2021-12-01 18:44:39 +03:00
|
|
|
if(instance->callback_have_read &&
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_bytes_available(instance->stream_rx) == 0) {
|
2021-12-01 18:44:39 +03:00
|
|
|
callback_rx = true;
|
|
|
|
}
|
2023-08-22 20:56:27 +03:00
|
|
|
//TODO FL-3554: checking that it managed to write all the data to the RX buffer
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_send(
|
2021-12-01 18:44:39 +03:00
|
|
|
instance->stream_rx,
|
|
|
|
&data,
|
|
|
|
size_rx[0],
|
2021-12-08 16:42:01 +03:00
|
|
|
SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF);
|
2021-12-01 18:44:39 +03:00
|
|
|
if(callback_rx) {
|
|
|
|
instance->callback_have_read(instance->context_have_read);
|
|
|
|
callback_rx = false;
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-22 20:56:27 +03:00
|
|
|
//TODO FL-3555: RX buffer overflow
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(timeout_tx) timeout_tx--;
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_tick(1);
|
2021-12-01 18:44:39 +03:00
|
|
|
}
|
|
|
|
|
2023-06-30 17:03:36 +03:00
|
|
|
subghz_devices_sleep(instance->device);
|
|
|
|
subghz_devices_end(instance->device);
|
2021-12-01 18:44:39 +03:00
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "Worker stop");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubGhzTxRxWorker* subghz_tx_rx_worker_alloc() {
|
2022-02-18 22:53:46 +03:00
|
|
|
SubGhzTxRxWorker* instance = malloc(sizeof(SubGhzTxRxWorker));
|
2021-12-01 18:44:39 +03:00
|
|
|
|
2022-11-23 15:49:17 +03:00
|
|
|
instance->thread =
|
|
|
|
furi_thread_alloc_ex("SubGhzTxRxWorker", 2048, subghz_tx_rx_worker_thread, instance);
|
2021-12-01 18:44:39 +03:00
|
|
|
instance->stream_tx =
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_alloc(sizeof(uint8_t) * SUBGHZ_TXRX_WORKER_BUF_SIZE, sizeof(uint8_t));
|
2021-12-01 18:44:39 +03:00
|
|
|
instance->stream_rx =
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_alloc(sizeof(uint8_t) * SUBGHZ_TXRX_WORKER_BUF_SIZE, sizeof(uint8_t));
|
2021-12-01 18:44:39 +03:00
|
|
|
|
2021-12-08 16:42:01 +03:00
|
|
|
instance->status = SubGhzTxRxWorkerStatusIDLE;
|
2021-12-01 18:44:39 +03:00
|
|
|
instance->worker_stoping = true;
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_tx_rx_worker_free(SubGhzTxRxWorker* instance) {
|
|
|
|
furi_assert(instance);
|
2021-12-08 16:42:01 +03:00
|
|
|
furi_assert(!instance->worker_running);
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_free(instance->stream_tx);
|
|
|
|
furi_stream_buffer_free(instance->stream_rx);
|
2021-12-01 18:44:39 +03:00
|
|
|
furi_thread_free(instance->thread);
|
|
|
|
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2023-06-30 17:03:36 +03:00
|
|
|
bool subghz_tx_rx_worker_start(
|
|
|
|
SubGhzTxRxWorker* instance,
|
|
|
|
const SubGhzDevice* device,
|
|
|
|
uint32_t frequency) {
|
2021-12-01 18:44:39 +03:00
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(!instance->worker_running);
|
|
|
|
bool res = false;
|
2022-10-07 15:27:11 +03:00
|
|
|
furi_stream_buffer_reset(instance->stream_tx);
|
|
|
|
furi_stream_buffer_reset(instance->stream_rx);
|
2021-12-01 18:44:39 +03:00
|
|
|
|
|
|
|
instance->worker_running = true;
|
|
|
|
|
2022-08-11 12:21:56 +03:00
|
|
|
if(furi_hal_region_is_frequency_allowed(frequency)) {
|
2021-12-01 18:44:39 +03:00
|
|
|
instance->frequency = frequency;
|
2023-06-30 17:03:36 +03:00
|
|
|
instance->device = device;
|
2021-12-01 18:44:39 +03:00
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:28:31 +03:00
|
|
|
furi_thread_start(instance->thread);
|
|
|
|
|
2021-12-01 18:44:39 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_tx_rx_worker_stop(SubGhzTxRxWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(instance->worker_running);
|
|
|
|
|
|
|
|
instance->worker_running = false;
|
|
|
|
|
|
|
|
furi_thread_join(instance->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_tx_rx_worker_is_running(SubGhzTxRxWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->worker_running;
|
|
|
|
}
|