2022-02-27 20:21:14 +01:00
|
|
|
#include <linux/if.h>
|
2021-10-17 19:26:38 +02:00
|
|
|
#include <stdio.h>
|
2022-03-05 21:38:05 +01:00
|
|
|
#include <stdlib.h>
|
2021-10-17 19:26:38 +02:00
|
|
|
#include <string.h>
|
2022-02-27 22:01:51 +01:00
|
|
|
#include <ctype.h>
|
2024-01-02 23:49:55 +01:00
|
|
|
#include <time.h>
|
2022-02-27 22:01:51 +01:00
|
|
|
|
2021-10-17 19:26:38 +02:00
|
|
|
#include <liquid/liquid.h>
|
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
#include <sys/poll.h>
|
2022-02-27 20:21:14 +01:00
|
|
|
#include <unistd.h>
|
2022-03-05 21:38:05 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
2022-02-27 20:21:14 +01:00
|
|
|
|
2021-10-17 19:26:38 +02:00
|
|
|
#include "utils.h"
|
2022-02-13 21:29:35 +01:00
|
|
|
#include "layer1/tx.h"
|
2022-02-16 20:52:46 +01:00
|
|
|
#include "layer1/rx.h"
|
2022-02-13 21:29:35 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
#include "layer2/tundev.h"
|
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
#include "sdr/sdr.h"
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2022-02-13 21:29:35 +01:00
|
|
|
#define RESULT_CHECK(stmt) { \
|
|
|
|
result_t res = stmt; \
|
|
|
|
if(res != OK) { \
|
2022-04-03 18:11:52 +02:00
|
|
|
fprintf(stderr, "Error %d in %s:%d!\n", res, __FILE__, __LINE__); \
|
2022-02-13 21:29:35 +01:00
|
|
|
exit(1); \
|
|
|
|
} \
|
|
|
|
}
|
2022-02-01 21:35:43 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
static int m_tunfd = -1;
|
|
|
|
static bool m_running = true;
|
|
|
|
|
2022-05-07 21:17:53 +02:00
|
|
|
static double next_tx_switch_time = 0.0;
|
|
|
|
|
2024-01-02 19:04:19 +01:00
|
|
|
static struct {
|
|
|
|
size_t preambles_found;
|
|
|
|
size_t successful_decodes;
|
|
|
|
size_t failed_decodes;
|
|
|
|
} m_stats;
|
|
|
|
|
2022-05-07 21:17:53 +02:00
|
|
|
|
|
|
|
static void block_tx_for(unsigned offset_ms)
|
|
|
|
{
|
|
|
|
next_tx_switch_time = get_hires_time() + (double)offset_ms * 0.001;
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:18:24 +01:00
|
|
|
void print_complex_array(const char *varname, float complex const *array, size_t len)
|
|
|
|
{
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "%s=np.array([%f%+fj", varname, crealf(array[0]), cimagf(array[0]));
|
2022-02-03 22:18:24 +01:00
|
|
|
for(size_t k = 1; k < len; k++) {
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, ", %f%+fj", crealf(array[k]), cimagf(array[k]));
|
2022-02-03 22:18:24 +01:00
|
|
|
}
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "])\n");
|
2022-02-03 22:18:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-27 22:01:51 +01:00
|
|
|
void hexdump(const uint8_t *data, size_t len)
|
|
|
|
{
|
|
|
|
static const char lut[16] = "0123456789ABCDEF";
|
|
|
|
static const size_t BYTES_PER_LINE = 16;
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
while(pos < len) {
|
|
|
|
size_t bytes_in_line = len - pos;
|
|
|
|
if(bytes_in_line > BYTES_PER_LINE) {
|
|
|
|
bytes_in_line = BYTES_PER_LINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < BYTES_PER_LINE; i++) {
|
|
|
|
if(i >= bytes_in_line) {
|
|
|
|
fputs(" ", stderr);
|
|
|
|
} else {
|
|
|
|
uint8_t byte = data[pos+i];
|
|
|
|
|
|
|
|
fprintf(stderr, "%c%c ", lut[byte >> 4], lut[byte & 0x0F]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs(" ", stderr);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < bytes_in_line; i++) {
|
|
|
|
uint8_t byte = data[pos+i];
|
|
|
|
|
|
|
|
if(isprint(byte)) {
|
|
|
|
putc(byte, stderr);
|
|
|
|
} else {
|
|
|
|
putc('.', stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
putc('\n', stderr);
|
|
|
|
pos += bytes_in_line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-16 20:52:46 +01:00
|
|
|
void cb_rx(rx_evt_t evt, uint8_t *packet_data, size_t packet_len)
|
2022-02-12 21:58:53 +01:00
|
|
|
{
|
2022-02-27 20:21:14 +01:00
|
|
|
int ret;
|
|
|
|
|
2022-02-16 20:52:46 +01:00
|
|
|
switch(evt)
|
|
|
|
{
|
|
|
|
case RX_EVT_CHECKSUM_ERROR:
|
2024-01-02 19:04:19 +01:00
|
|
|
//fprintf(stderr, "Received a message of %zu bytes, but decoding failed.\n", packet_len);
|
2022-02-27 20:21:14 +01:00
|
|
|
//fprintf(stderr, "=== FAILED PAYLOAD ===\n");
|
2022-02-27 22:01:51 +01:00
|
|
|
//hexdump(packet_data, packet_len);
|
2022-02-27 20:21:14 +01:00
|
|
|
//fprintf(stderr, "=======================\n");
|
2024-01-02 19:04:19 +01:00
|
|
|
m_stats.failed_decodes++;
|
2022-02-16 20:52:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RX_EVT_PACKET_RECEIVED:
|
2024-01-02 19:04:19 +01:00
|
|
|
//fprintf(stderr, "A message of %zu bytes was decoded successfully.\n", packet_len);
|
2022-05-07 21:17:53 +02:00
|
|
|
//fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len);
|
|
|
|
//hexdump(packet_data, packet_len < 64 ? packet_len : 64);
|
|
|
|
//fprintf(stderr, "====================================\n");
|
|
|
|
|
2024-01-02 19:04:19 +01:00
|
|
|
m_stats.successful_decodes++;
|
|
|
|
|
2022-05-07 21:17:53 +02:00
|
|
|
block_tx_for(TX_SWITCH_BACKOFF_END_OF_PACKET_MS);
|
2022-02-27 20:21:14 +01:00
|
|
|
|
|
|
|
ret = write(m_tunfd, packet_data, packet_len);
|
|
|
|
if(ret < 0) {
|
|
|
|
perror("write");
|
|
|
|
}
|
2022-02-16 20:52:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RX_EVT_PREAMBLE_FOUND:
|
2024-01-02 19:04:19 +01:00
|
|
|
//fprintf(stderr, "Found preamble!\n");
|
2022-05-07 21:17:53 +02:00
|
|
|
block_tx_for(TX_SWITCH_BACKOFF_PREAMBLE_MS);
|
2024-01-02 19:04:19 +01:00
|
|
|
m_stats.preambles_found++;
|
2022-02-16 20:52:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-12 21:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
static int debug_fd;
|
|
|
|
|
|
|
|
|
2022-04-07 21:17:37 +02:00
|
|
|
#define CHUNKSIZE_BB 16384
|
2022-03-05 21:38:05 +01:00
|
|
|
#define CHUNKSIZE_RF (CHUNKSIZE_BB * SDR_OVERSAMPLING)
|
|
|
|
static result_t transmit_in_chunks(sdr_ctx_t *sdr, const float complex *samples, size_t len)
|
|
|
|
{
|
|
|
|
size_t transmitted = 0;
|
|
|
|
unsigned retry_counter = 0;
|
|
|
|
|
|
|
|
float complex rf_samples[CHUNKSIZE_RF];
|
|
|
|
|
|
|
|
while(transmitted < len) {
|
|
|
|
size_t to_transmit_bb = len - transmitted;
|
|
|
|
if(to_transmit_bb > CHUNKSIZE_BB) {
|
|
|
|
to_transmit_bb = CHUNKSIZE_BB;
|
|
|
|
}
|
|
|
|
|
2024-01-02 23:49:55 +01:00
|
|
|
size_t to_transmit_rf = CHUNKSIZE_RF;
|
2022-03-05 21:38:05 +01:00
|
|
|
|
2024-01-02 23:49:55 +01:00
|
|
|
RESULT_CHECK(sdr_baseband_to_rf(sdr, samples + transmitted, to_transmit_bb, rf_samples, &to_transmit_rf));
|
2022-03-05 21:38:05 +01:00
|
|
|
|
|
|
|
result_t result = sdr_transmit(sdr, rf_samples, to_transmit_rf, 100000);
|
|
|
|
|
|
|
|
if(result != OK) {
|
|
|
|
retry_counter++;
|
|
|
|
fprintf(stderr, "sdr_transmit failed %d times\n", retry_counter);
|
|
|
|
|
|
|
|
if(retry_counter > 3) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 22:01:26 +02:00
|
|
|
//write(debug_fd, rf_samples, to_transmit_rf*sizeof(rf_samples[0]));
|
2022-03-05 21:38:05 +01:00
|
|
|
|
|
|
|
fprintf(stderr, "t");
|
|
|
|
|
|
|
|
transmitted += to_transmit_bb;
|
|
|
|
|
|
|
|
retry_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-17 19:26:38 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
2022-02-13 21:29:35 +01:00
|
|
|
layer1_tx_t tx;
|
2022-02-27 20:21:14 +01:00
|
|
|
layer1_rx_t rx;
|
2022-02-13 21:29:35 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
sdr_ctx_t sdr;
|
|
|
|
|
2022-05-07 20:10:55 +02:00
|
|
|
bool on_air = true;
|
2022-03-05 21:38:05 +01:00
|
|
|
|
|
|
|
debug_fd = open("/tmp/dump.cf32", O_CREAT | O_WRONLY | O_TRUNC);
|
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
// ** Initialize **
|
2022-02-13 21:29:35 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
char devname[IFNAMSIZ] = "hamnet70";
|
|
|
|
m_tunfd = tundev_open(devname);
|
2022-02-13 21:29:35 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
if(m_tunfd < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-02-13 21:29:35 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
RESULT_CHECK(sdr_init(&sdr));
|
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
RESULT_CHECK(layer1_tx_init(&tx));
|
|
|
|
RESULT_CHECK(layer1_rx_init(&rx, cb_rx));
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
// ** Process packets **
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
struct pollfd pfd;
|
|
|
|
memset(&pfd, 0, sizeof(pfd));
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
pfd.fd = m_tunfd;
|
|
|
|
pfd.events = POLLIN;
|
2022-02-27 16:59:14 +01:00
|
|
|
|
2022-05-07 20:10:55 +02:00
|
|
|
// start in TX mode to work around SoapyHackRF not setting the correct frequency.
|
|
|
|
RESULT_CHECK(sdr_start_tx(&sdr, 1));
|
2022-02-27 16:59:14 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
unsigned rx_retries = 0;
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2023-05-17 22:28:18 +02:00
|
|
|
double old = get_hires_time();
|
|
|
|
size_t total_samples = 0;
|
2024-01-02 19:04:19 +01:00
|
|
|
double next_stats_print_time = old + 0.5;
|
2023-05-17 22:28:18 +02:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
while(m_running) {
|
2022-05-07 21:17:53 +02:00
|
|
|
double now = get_hires_time();
|
|
|
|
|
|
|
|
if((now > next_tx_switch_time) && (on_air || !layer1_rx_is_busy(&rx))) {
|
2022-03-05 21:38:05 +01:00
|
|
|
int ret = poll(&pfd, 1, 0);
|
|
|
|
if(ret < 0) {
|
|
|
|
perror("poll");
|
|
|
|
break;
|
|
|
|
} else if(ret > 0) {
|
|
|
|
// there is a packet to be read.
|
|
|
|
uint8_t packetbuf[2048];
|
|
|
|
ret = read(m_tunfd, packetbuf, sizeof(packetbuf));
|
|
|
|
if(ret < 0) {
|
|
|
|
perror("read");
|
|
|
|
break;
|
|
|
|
} else if(ret == 0) {
|
|
|
|
// no more data
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Transmitting packet with %d bytes.\n", ret);
|
|
|
|
|
|
|
|
RESULT_CHECK(layer1_tx_reset(&tx));
|
|
|
|
|
|
|
|
// ** Modulate packet **
|
|
|
|
|
|
|
|
RESULT_CHECK(layer1_tx_add_packet_to_burst(&tx, packetbuf, ret));
|
|
|
|
RESULT_CHECK(layer1_tx_finalize_burst(&tx));
|
|
|
|
|
|
|
|
size_t burst_len = layer1_tx_get_sample_count(&tx);
|
|
|
|
const float complex *whole_burst = layer1_tx_get_sample_data(&tx);
|
|
|
|
|
|
|
|
dump_array_cf(whole_burst, burst_len, 1.0f, "/tmp/tx.cpx");
|
|
|
|
|
2024-01-03 00:33:54 +01:00
|
|
|
// ensure that the buffer is full before TX is turned on to avoid transmitting empty buffers
|
|
|
|
RESULT_CHECK(transmit_in_chunks(&sdr, whole_burst, burst_len));
|
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
if(!on_air) {
|
2024-01-02 23:49:55 +01:00
|
|
|
RESULT_CHECK(sdr_stop_rx(&sdr));
|
2022-04-03 18:11:52 +02:00
|
|
|
RESULT_CHECK(sdr_start_tx(&sdr, burst_len * SDR_OVERSAMPLING));
|
2022-03-05 21:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_air = true;
|
|
|
|
} else if(on_air) { // ret == 0
|
2024-01-03 00:25:55 +01:00
|
|
|
RESULT_CHECK(sdr_flush_tx_buffer(&sdr));
|
2022-04-03 18:11:52 +02:00
|
|
|
|
2024-01-02 23:49:55 +01:00
|
|
|
RESULT_CHECK(sdr_stop_tx(&sdr));
|
|
|
|
RESULT_CHECK(sdr_start_rx(&sdr));
|
2022-04-03 18:11:52 +02:00
|
|
|
on_air = false;
|
2022-05-07 21:17:53 +02:00
|
|
|
|
|
|
|
block_tx_for(TX_SWITCH_BACKOFF_AFTER_RX_ON);
|
2022-03-05 21:38:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-30 20:05:20 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
if(!on_air) {
|
|
|
|
// ** Receive signal **
|
2022-02-27 20:21:14 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
float complex rf_samples[CHUNKSIZE_RF];
|
|
|
|
float complex bb_samples[CHUNKSIZE_BB];
|
2022-01-30 20:05:20 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
size_t n_rf_samples = CHUNKSIZE_RF;
|
2024-01-02 23:49:55 +01:00
|
|
|
size_t n_bb_samples = CHUNKSIZE_BB;
|
2022-02-27 20:21:14 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
if(sdr_receive(&sdr, rf_samples, &n_rf_samples, 100000) != OK) {
|
|
|
|
rx_retries++;
|
|
|
|
fprintf(stderr, "sdr_receive() failed %d times.\n", rx_retries);
|
|
|
|
if(rx_retries >= 3) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 20:21:14 +01:00
|
|
|
|
2024-01-02 23:49:55 +01:00
|
|
|
if(n_rf_samples == 0) {
|
|
|
|
struct timespec ts_1ms = {0, 1000000};
|
|
|
|
nanosleep(&ts_1ms, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-17 22:28:18 +02:00
|
|
|
total_samples += n_rf_samples;
|
|
|
|
|
2023-05-20 22:01:26 +02:00
|
|
|
double new = get_hires_time();
|
2024-01-02 19:04:19 +01:00
|
|
|
if(new >= next_stats_print_time) {
|
2023-05-20 22:01:26 +02:00
|
|
|
double rate = total_samples / (new - old);
|
|
|
|
fprintf(stderr, "\nEstimated rate: %.3f MS/s\n", rate / 1e6);
|
2024-01-02 19:04:19 +01:00
|
|
|
fprintf(stderr, "Receiver statistics:\n");
|
|
|
|
fprintf(stderr, " Preambles found: %8zd\n", m_stats.preambles_found);
|
|
|
|
fprintf(stderr, " Successful decodes: %8zd (%6.2f %%)\n",
|
|
|
|
m_stats.successful_decodes, m_stats.successful_decodes * 100.0f / m_stats.preambles_found);
|
|
|
|
fprintf(stderr, " Failed decodes: %8zd (%6.2f %%)\n",
|
|
|
|
m_stats.failed_decodes, m_stats.failed_decodes * 100.0f / m_stats.preambles_found);
|
|
|
|
next_stats_print_time += 0.5;
|
2023-05-20 22:01:26 +02:00
|
|
|
|
|
|
|
total_samples = 0;
|
|
|
|
old = new;
|
|
|
|
}
|
2023-05-17 22:28:18 +02:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
rx_retries = 0;
|
2022-02-27 20:21:14 +01:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
fprintf(stderr, "r");
|
2022-02-27 20:21:14 +01:00
|
|
|
|
2024-01-02 23:49:55 +01:00
|
|
|
RESULT_CHECK(sdr_rf_to_baseband(&sdr, rf_samples, n_rf_samples, bb_samples, &n_bb_samples));
|
2022-03-05 21:38:05 +01:00
|
|
|
|
|
|
|
RESULT_CHECK(layer1_rx_process(&rx, bb_samples, n_bb_samples));
|
|
|
|
} else {
|
|
|
|
rx_retries = 0;
|
|
|
|
}
|
2022-02-27 20:21:14 +01:00
|
|
|
}
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
close(debug_fd);
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
// ** Cleanup **
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-16 20:52:46 +01:00
|
|
|
layer1_tx_shutdown(&tx);
|
|
|
|
layer1_rx_shutdown(&rx);
|
|
|
|
|
2022-03-05 21:38:05 +01:00
|
|
|
sdr_destroy(&sdr);
|
|
|
|
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "Done.\n");
|
2021-10-17 19:26:38 +02:00
|
|
|
}
|