hamnet70/impl/src/layer1/rx.h

137 lines
3.4 KiB
C

#ifndef LAYER1_RX_H
#define LAYER1_RX_H
#include <liquid/liquid.h>
#include "modcod.h"
#include "correlator.h"
#include "results.h"
typedef enum {
RX_STATE_ACQUISITION,
RX_STATE_HEADER,
RX_STATE_DATA,
} rx_state_t;
typedef enum {
RX_EVT_PACKET_RECEIVED,
RX_EVT_PREAMBLE_FOUND,
RX_EVT_HEADER_ERROR,
RX_EVT_CHECKSUM_ERROR,
} rx_evt_t;
typedef void (*rx_callback_t)(rx_evt_t evt, uint8_t *packet_data, size_t packet_len);
typedef struct
{
// Input channel filter
firfilt_crcf channel_filter;
// AGC
agc_crcf agc;
float noise_floor_level;
// NCOs for carrier frequency offset correction
nco_crcf carrier_coarse_nco;
nco_crcf carrier_fine_nco;
// Forward error correction objects
fec hdr_fec;
// Demodulators
modem hdr_demod;
modem payload_demod;
// symbol timing synchronizer
symsync_crcf symsync;
// preamble correlator
correlator_ctx_t preamble_correlator;
// Receiver state
rx_state_t state;
// Callback function to notify user of certain events
rx_callback_t callback;
// Precalcuated header lengths
unsigned int hdr_len_symbols;
unsigned int hdr_len_enc_bytes;
// Information from the decoded header
uint16_t payload_len_symbols;
uint16_t payload_len_bytes;
uint16_t payload_crc;
uint16_t payload_len_enc_bytes;
modcod_t modcod;
} layer1_rx_t;
/*!
* \brief Initialize the receiver.
*
* \param rx Pointer to the receiver context.
* \param callback Function to be called on receiver events.
* \returns The result of the initialization.
*/
result_t layer1_rx_init(layer1_rx_t *rx, rx_callback_t callback);
/*!
* \brief Shut the receiver down. Frees all memory.
*
* \param rx Pointer to the receiver context.
* \returns The result of the shutdown.
*/
result_t layer1_rx_shutdown(layer1_rx_t *rx);
/*!
* \brief Process the given block of samples.
*
* Runs the whole receiver chain for the given samples. This includes timing-
* and frequency synchronization, preamble search, header demodulation and
* decoding and payload demodulation and decoding.
*
* On specific events, most notably when a packet is successfully decoded, the
* callback function passed to \ref layer1_rx_init() will be called. See \ref
* rx_evt_t for possible events.
*
* \param rx Pointer to the receiver context.
* \param samples Pointer to the samples to process.
* \param sample_count Number of samples to process.
* \returns The result of the shutdown.
*/
result_t layer1_rx_process(layer1_rx_t *rx, const float complex *samples, size_t sample_count);
/*!
* \brief Determine whether the receiver is currently receiving a packet.
*
* This function can be used to identify idle periods where packet transmission
* is possible. Switching to TX while this function returns true will very
* probably result in a collision with packet corruption.
*
* \param rx Pointer to the receiver context.
* \returns Whether the receiver is currently receiving a packet.
*/
bool layer1_rx_is_busy(const layer1_rx_t *rx);
/*!
* \brief Retrieve the coarsely estimated carrier frequency.
*
* The carrier frequency is measured in radians per sample in the RRC-filtered baseband domain, i.e. the sampling rate is SYMBOL_RATE * RRC_SPS.
*
* \param rx Pointer to the receiver context.
* \returns The estimated carrier frequency in the RRC-filtered domain.
*/
float layer1_rx_get_coarse_carrier_frequency(const layer1_rx_t *rx);
#endif // LAYER1_RX_H