2022-02-27 20:21:14 +01:00
|
|
|
#include <linux/if.h>
|
2021-10-17 19:26:38 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2022-01-30 20:05:20 +01:00
|
|
|
#include <math.h>
|
2021-10-17 19:26:38 +02:00
|
|
|
#include <liquid/liquid.h>
|
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
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-02-13 21:29:35 +01:00
|
|
|
#define RESULT_CHECK(stmt) { \
|
|
|
|
result_t res = stmt; \
|
|
|
|
if(res != OK) { \
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "Error %d in %s:%d!", 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-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-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:
|
2022-02-16 21:46:56 +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");
|
|
|
|
//fprintf(stderr, "%s\n", packet_data);
|
|
|
|
//fprintf(stderr, "=======================\n");
|
2022-02-16 20:52:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RX_EVT_PACKET_RECEIVED:
|
2022-02-27 20:21:14 +01:00
|
|
|
fprintf(stderr, "=== DECODED PAYLOAD (%4zu bytes) ===\n", packet_len);
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "%s\n", packet_data);
|
2022-02-27 20:21:14 +01:00
|
|
|
fprintf(stderr, "====================================\n");
|
|
|
|
|
|
|
|
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:
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "Found preamble!\n");
|
2022-02-16 20:52:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-12 21:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-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-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
|
|
|
// channel emulation
|
2021-10-17 19:26:38 +02:00
|
|
|
channel_cccf channel = channel_cccf_create();
|
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
float snr = 15.0f;
|
2021-10-17 19:26:38 +02:00
|
|
|
channel_cccf_add_awgn(channel, -snr, snr);
|
2022-01-30 20:23:41 +01:00
|
|
|
channel_cccf_add_carrier_offset(channel, 0.20f, 1.00f);
|
2022-02-27 20:21:14 +01:00
|
|
|
//channel_cccf_add_shadowing(channel, 1.00f, 0.1f);
|
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-02-27 20:21:14 +01:00
|
|
|
uint8_t packetbuf[2048];
|
|
|
|
while(m_running) {
|
|
|
|
int ret = read(m_tunfd, packetbuf, sizeof(packetbuf));
|
|
|
|
if(ret < 0) {
|
|
|
|
perror("read");
|
|
|
|
break;
|
|
|
|
} else if(ret == 0) {
|
|
|
|
// no more data
|
|
|
|
break;
|
|
|
|
}
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
fprintf(stderr, "Transmitting packet with %d bytes.\n", ret);
|
2022-02-27 16:59:14 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
RESULT_CHECK(layer1_tx_reset(&tx));
|
2022-02-27 16:59:14 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
// ** Modulate packet **
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
RESULT_CHECK(layer1_tx_add_packet_to_burst(&tx, packetbuf, ret));
|
|
|
|
RESULT_CHECK(layer1_tx_finalize_burst(&tx));
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
size_t burst_len = layer1_tx_get_sample_count(&tx);
|
|
|
|
const float complex *whole_burst = layer1_tx_get_sample_data(&tx);
|
2022-01-30 20:05:20 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
dump_array_cf(whole_burst, burst_len, 1.0f, "/tmp/tx.cpx");
|
|
|
|
|
|
|
|
// ** Apply channel distortions **
|
2022-01-30 20:05:20 +01:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
float complex msg_received[burst_len];
|
|
|
|
|
|
|
|
channel_cccf_execute_block(channel, whole_burst, burst_len, msg_received);
|
|
|
|
|
|
|
|
// scale the entire signal to give the AGC something to do
|
|
|
|
for(size_t i = 0; i < burst_len; i++) {
|
|
|
|
msg_received[i] *= 0.02f;
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_array_cf(msg_received, burst_len, 1.0f, "/tmp/rx.cpx");
|
|
|
|
|
|
|
|
// ** Receive signal **
|
|
|
|
|
|
|
|
RESULT_CHECK(layer1_rx_process(&rx, msg_received, burst_len));
|
|
|
|
}
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-27 20:21:14 +01:00
|
|
|
// ** Cleanup **
|
2021-10-17 19:26:38 +02:00
|
|
|
|
|
|
|
channel_cccf_destroy(channel);
|
|
|
|
|
2022-02-16 20:52:46 +01:00
|
|
|
layer1_tx_shutdown(&tx);
|
|
|
|
layer1_rx_shutdown(&rx);
|
|
|
|
|
2022-02-16 21:46:56 +01:00
|
|
|
fprintf(stderr, "Done.\n");
|
2021-10-17 19:26:38 +02:00
|
|
|
}
|