hamnet70/impl/test/layer1/test_loopback.c

155 lines
3.6 KiB
C
Raw Normal View History

/*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Copyright (C) 2024 Thomas Kolb
*/
2023-05-12 13:46:02 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
2023-05-12 13:46:02 +02:00
#include <liquid/liquid.h>
#include "layer1/tx.h"
#include "layer1/rx.h"
2024-05-30 10:44:42 +02:00
#include "logger.h"
2023-05-12 13:46:02 +02:00
2024-05-30 10:44:42 +02:00
#include "utils.h"
#include "config.h"
2023-05-12 13:46:02 +02:00
#define RESULT_CHECK(stmt) { \
result_t res = stmt; \
if(res != OK) { \
2024-05-30 10:44:42 +02:00
LOG(LVL_ERR, "Error %d in %s:%d!", res, __FILE__, __LINE__); \
2023-05-12 13:46:02 +02:00
exit(1); \
} \
}
void print_complex_array(const char *varname, float complex const *array, size_t len)
{
fprintf(stderr, "%s=np.array([%f%+fj", varname, crealf(array[0]), cimagf(array[0]));
for(size_t k = 1; k < len; k++) {
fprintf(stderr, ", %f%+fj", crealf(array[k]), cimagf(array[k]));
}
fprintf(stderr, "])\n");
}
void cb_rx(rx_evt_t evt, const layer1_rx_t *rx, uint8_t *packet_data, size_t packet_len)
2023-05-12 13:46:02 +02:00
{
(void)rx;
unsigned int data_size;
2023-05-12 13:46:02 +02:00
switch(evt)
{
case RX_EVT_DECODE_FAILED:
2024-05-30 10:44:42 +02:00
LOG(LVL_ERR, "Received a message of %zu bytes, but decoding failed.", packet_len);
LOG(LVL_INFO, "=== FAILED PAYLOAD ===");
2023-05-12 13:46:02 +02:00
hexdump(packet_data, packet_len);
2024-05-30 10:44:42 +02:00
LOG(LVL_INFO, "=======================");
2023-05-12 13:46:02 +02:00
break;
case RX_EVT_PACKET_RECEIVED:
data_size = packet_len - crc_sizeof_key(PAYLOAD_CRC_SCHEME);
2024-05-30 10:44:42 +02:00
LOG(LVL_INFO, "=== DECODED PAYLOAD (%4zu bytes) ===", packet_len);
hexdump(packet_data, packet_len);
2024-05-30 10:44:42 +02:00
LOG(LVL_INFO, "====================================");
// FIXME: move to layer 2
if(!crc_check_key(PAYLOAD_CRC_SCHEME, packet_data, data_size)) {
LOG(LVL_WARN, "payload CRC check failed!");
break;
}
LOG(LVL_INFO, "A message of %zu bytes was decoded successfully.", packet_len);
2023-05-12 13:46:02 +02:00
break;
case RX_EVT_HEADER_ERROR:
2024-05-30 10:44:42 +02:00
LOG(LVL_ERR, "Header decoding failed!");
break;
2023-05-12 13:46:02 +02:00
case RX_EVT_PREAMBLE_FOUND:
2024-05-30 10:44:42 +02:00
LOG(LVL_INFO, "Found preamble!");
2023-05-12 13:46:02 +02:00
break;
case RX_EVT_PACKET_DEBUG_INFO_COMPLETE:
// FIXME: print debug info
break;
2023-05-12 13:46:02 +02:00
}
}
int main(void)
{
layer1_tx_t tx;
layer1_rx_t rx;
// ** Initialize **
2024-05-30 10:44:42 +02:00
logger_init();
srand(time(NULL));
2023-05-12 13:46:02 +02:00
RESULT_CHECK(layer1_tx_init(&tx));
RESULT_CHECK(layer1_rx_init(&rx, cb_rx));
// ** Generate packets **
RESULT_CHECK(layer1_tx_reset(&tx));
uint8_t packetbuf1[64] = "Hello World! 1234567890";
size_t crc_size = crc_sizeof_key(PAYLOAD_CRC_SCHEME);
size_t l = strlen((char*)packetbuf1);
crc_append_key(PAYLOAD_CRC_SCHEME, packetbuf1, l);
LOG(LVL_INFO, "Transmitting packet with %zd bytes.", l + crc_size);
2023-05-12 13:46:02 +02:00
RESULT_CHECK(layer1_tx_add_packet_to_burst(&tx, packetbuf1, l + crc_size));
2023-05-12 13:46:02 +02:00
uint8_t packetbuf2[128] = "This is just a test message. Just writing some stuff here to create a longer packet.";
l = strlen((char*)packetbuf2);
2023-05-12 13:46:02 +02:00
crc_append_key(PAYLOAD_CRC_SCHEME, packetbuf2, l);
2023-05-12 13:46:02 +02:00
LOG(LVL_INFO, "Transmitting packet with %zd bytes.", l + crc_size);
2023-05-12 13:46:02 +02:00
RESULT_CHECK(layer1_tx_add_packet_to_burst(&tx, packetbuf2, l + crc_size));
2023-05-12 13:46:02 +02:00
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, 10e-6f, "/tmp/tx.cpx");
// ** Simulate channel effects **
channel_cccf ch = channel_cccf_create();
channel_cccf_add_awgn(ch, -30, 30);
2024-05-30 10:44:42 +02:00
//channel_cccf_add_carrier_offset(ch, 0.05f, 1.23f);
2023-05-12 13:46:02 +02:00
float complex whole_burst_ch[burst_len];
channel_cccf_execute_block(ch, (float complex*)whole_burst, burst_len, whole_burst_ch);
dump_array_cf(whole_burst_ch, burst_len, 10e-6f, "/tmp/rx.cpx");
// ** Receive signal **
RESULT_CHECK(layer1_rx_process(&rx, whole_burst_ch, burst_len));
// ** Cleanup **
layer1_tx_shutdown(&tx);
layer1_rx_shutdown(&rx);
2024-05-30 10:44:42 +02:00
LOG(LVL_INFO, "Done.");
2023-05-12 13:46:02 +02:00
}