2022-02-13 20:01:33 +01:00
|
|
|
#ifndef LAYER1_PACKET_MOD_H
|
|
|
|
#define LAYER1_PACKET_MOD_H
|
2021-10-17 19:26:38 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <complex.h>
|
|
|
|
|
|
|
|
#include <liquid/liquid.h>
|
|
|
|
|
2022-02-17 21:39:18 +01:00
|
|
|
#include "modcod.h"
|
|
|
|
|
2021-10-17 19:26:38 +02:00
|
|
|
#include "results.h"
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
NOT_STARTED,
|
|
|
|
DATA_RAW,
|
|
|
|
DATA_CODED,
|
|
|
|
DATA_MODULATED,
|
|
|
|
HEADER_ADDED,
|
|
|
|
PREAMBLE_ADDED,
|
|
|
|
} packet_mod_state_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned char *pkt_bytes;
|
|
|
|
float complex *pkt_symbols;
|
|
|
|
|
|
|
|
packet_mod_state_t state;
|
|
|
|
|
2022-01-23 16:28:53 +01:00
|
|
|
modem hdr_modem;
|
|
|
|
fec hdr_fec;
|
|
|
|
|
2021-10-17 19:26:38 +02:00
|
|
|
uint16_t raw_data_crc;
|
2022-02-11 17:56:26 +01:00
|
|
|
uint16_t raw_data_len;
|
2021-10-17 19:26:38 +02:00
|
|
|
|
2022-02-17 21:39:18 +01:00
|
|
|
uint8_t modcod;
|
|
|
|
|
2021-10-17 19:26:38 +02:00
|
|
|
size_t length;
|
|
|
|
} packet_mod_ctx_t;
|
|
|
|
|
|
|
|
|
2021-10-17 22:51:10 +02:00
|
|
|
/*!\brief Initialize the packet modulator context.
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to initialize.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_init(packet_mod_ctx_t *ctx);
|
2021-10-17 22:51:10 +02:00
|
|
|
|
|
|
|
/*!\brief Free all resources in the given context.
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to free.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_free(packet_mod_ctx_t *ctx);
|
|
|
|
|
2022-02-17 21:39:18 +01:00
|
|
|
/*!\brief Set the modulation and code to use for the next packet.
|
|
|
|
*
|
|
|
|
* Must be called before setting the raw data.
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to use for this operation.
|
|
|
|
* \param[in] modcod The modulation/code to use.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
|
|
|
result_t packet_mod_set_modcod(packet_mod_ctx_t *ctx, modcod_t modcod);
|
|
|
|
|
2021-10-17 22:51:10 +02:00
|
|
|
/*!\brief Set the raw packet data.
|
|
|
|
*
|
|
|
|
* In this step, the CRC of the data is calculated.
|
|
|
|
*
|
|
|
|
* This is the first step in packet processing. The context must be freshly
|
|
|
|
* initialized.
|
|
|
|
*
|
|
|
|
* The stored data can be retrieved for verification using \ref
|
|
|
|
* packet_mod_get_result_b().
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to use for this operation.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_set_data(packet_mod_ctx_t *ctx, const unsigned char *data, size_t length);
|
2021-10-17 22:51:10 +02:00
|
|
|
|
|
|
|
/*!\brief Channel-code the stored packet data.
|
|
|
|
*
|
|
|
|
* This can only be called after \ref packet_mod_set_data().
|
|
|
|
*
|
|
|
|
* The resulting data can be retrieved for verification using \ref
|
|
|
|
* packet_mod_get_result_b().
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to use for this operation.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_encode(packet_mod_ctx_t *ctx);
|
2021-10-17 22:51:10 +02:00
|
|
|
|
|
|
|
/*!\brief Modulate the packet data.
|
|
|
|
*
|
|
|
|
* This can be called after \ref packet_mod_set_data() or \ref
|
|
|
|
* packet_mod_encode().
|
|
|
|
*
|
|
|
|
* The resulting data can be retrieved for verification using \ref
|
|
|
|
* packet_mod_get_result_cf().
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to use for this operation.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_modulate(packet_mod_ctx_t *ctx);
|
2021-10-17 22:51:10 +02:00
|
|
|
|
|
|
|
/*!\brief Add a channel-coded and modulated packet header.
|
|
|
|
*
|
|
|
|
* The header contains the length of the data part (in symbols) and the CRC of
|
|
|
|
* the original raw data. The channel coding scheme and modulation are
|
|
|
|
* different than for the actual packet data, see \ref HEADER_CHANNEL_CODING
|
|
|
|
* and \ref HEADER_MODULATION. These settings should be chosen to be slightly
|
|
|
|
* more robust than the actual data encoding and modulation.
|
|
|
|
*
|
|
|
|
* This can only be called after \ref packet_mod_modulate().
|
|
|
|
*
|
|
|
|
* The resulting data can be retrieved for verification using \ref
|
|
|
|
* packet_mod_get_result_cf().
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to use for this operation.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_add_header(packet_mod_ctx_t *ctx);
|
2021-10-17 22:51:10 +02:00
|
|
|
|
|
|
|
/*!\brief Add the preamble to the packet.
|
|
|
|
*
|
|
|
|
* The preamble is one cycle of a m-sequence, modulated using BPSK. Adding such
|
|
|
|
* a preamble allows to find the packet start time and further signal
|
|
|
|
* parameters such as phase offset using cross-correlation at the receiver.
|
|
|
|
*
|
|
|
|
* This can only be called after \ref packet_mod_add_header().
|
|
|
|
*
|
|
|
|
* The resulting data can be retrieved using \ref packet_mod_get_result_cf().
|
|
|
|
*
|
|
|
|
* \param[inout] ctx The context to use for this operation.
|
|
|
|
* \returns An result code (see results.h).
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_add_preamble(packet_mod_ctx_t *ctx);
|
|
|
|
|
2021-10-17 22:51:10 +02:00
|
|
|
/*!\brief Get the result data as raw bytes.
|
|
|
|
*
|
|
|
|
* Raw bytes can be retrieved after \ref packet_mod_set_data() and \ref
|
|
|
|
* packet_mod_encode(). In all other states, this returns ERR_INVALID_STATE.
|
|
|
|
*
|
|
|
|
* \param[in] ctx The context to use for this operation.
|
|
|
|
* \param[out] data A pointer to the memory location where the data should be written.
|
|
|
|
* \param[inout length A pointer to the data length (in array items). The
|
|
|
|
* value shall be set to the size of the data buffer.
|
|
|
|
* After the call, it is set to the required buffer size
|
|
|
|
* (if \ref data is NULL or the length is not sufficient)
|
|
|
|
* or the number of items actually written to \ref data.
|
|
|
|
* \retval OK If the data was copied successfully or \ref data was
|
|
|
|
* NULL. In the latter case, only \ref length is modified
|
|
|
|
* to the required buffer size.
|
|
|
|
* \retval ERR_NO_MEM If the \ref length was not sufficient for the data.
|
|
|
|
* \retval ERR_INVALID_STATE If byte data is unavailable in the current state.
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_get_result_b(packet_mod_ctx_t *ctx, unsigned char *data, size_t *length);
|
2021-10-17 22:51:10 +02:00
|
|
|
|
|
|
|
/*!\brief Get the result data as complex numbers.
|
|
|
|
*
|
|
|
|
* Complex numbers can be retrieved after \ref packet_mod_modulate(), \ref
|
|
|
|
* packet_mod_add_header() and \ref packet_mod_add_preamble(). In all other
|
|
|
|
* states, this returns ERR_INVALID_STATE.
|
|
|
|
*
|
|
|
|
* \param[in] ctx The context to use for this operation.
|
|
|
|
* \param[out] data A pointer to the memory location where the data should be written.
|
|
|
|
* \param[inout length A pointer to the data length (in array items). The
|
|
|
|
* value shall be set to the size of the data buffer.
|
|
|
|
* After the call, it is set to the required buffer size
|
|
|
|
* (if \ref data is NULL or the length is not sufficient)
|
|
|
|
* or the number of items actually written to \ref data.
|
|
|
|
* \retval OK If the data was copied successfully or \ref data was
|
|
|
|
* NULL. In the latter case, only \ref length is modified
|
|
|
|
* to the required buffer size.
|
|
|
|
* \retval ERR_NO_MEM If the \ref length was not sufficient for the data.
|
|
|
|
* \retval ERR_INVALID_STATE If byte data is unavailable in the current state.
|
|
|
|
*/
|
2021-10-17 19:26:38 +02:00
|
|
|
result_t packet_mod_get_result_cf(packet_mod_ctx_t *ctx, float complex *data, size_t *length);
|
|
|
|
|
2022-02-13 20:01:33 +01:00
|
|
|
#endif // LAYER1_PACKET_MOD_H
|