diff --git a/impl/src/layer1/packet_demod.c b/impl/src/layer1/packet_demod.c deleted file mode 100644 index 354775f..0000000 --- a/impl/src/layer1/packet_demod.c +++ /dev/null @@ -1,120 +0,0 @@ -#include - -#include "correlator.h" -#include "preamble.h" -#include "config.h" - -#include "packet_demod.h" - -result_t packet_demod_init(packet_demod_ctx_t *ctx) -{ - ctx->pkt_bytes = NULL; - ctx->pkt_symbols = NULL; - - ctx->length = 0; - - if(!correlator_init(&ctx->correlator, preamble_get_symbols(), preamble_get_symbol_count())) { - return ERR_NO_MEM; - } - - ctx->fec = fec_create(CHANNEL_CODE, NULL); - ctx->modem = modem_create(MODULATION); - - ctx->hdr_fec = fec_create(HEADER_CHANNEL_CODE, NULL); - ctx->hdr_modem = modem_create(HEADER_MODULATION); - - ctx->state = SEARCHING_PREAMBLE; - - return OK; -} - - -result_t packet_demod_free(packet_demod_ctx_t *ctx) -{ - switch(ctx->state) { - case DATA_DEMODULATED: - case DATA_DECODED: - free(ctx->pkt_bytes); - ctx->pkt_bytes = NULL; - break; - - case SYMBOLS_SET: - free(ctx->pkt_symbols); - ctx->pkt_symbols = NULL; - break; - - case SEARCHING_PREAMBLE: - case HEADER_PARSED: - // nothing to do - break; - } - - ctx->state = SEARCHING_PREAMBLE; - ctx->length = 0; - - fec_destroy(ctx->fec); - modem_destroy(ctx->modem); - - fec_destroy(ctx->hdr_fec); - modem_destroy(ctx->hdr_modem); - - return OK; -} - - -bool packet_demod_search_preamble(packet_demod_ctx_t *ctx, float complex sample, float *magnitude) -{ - float mag = cabsf(correlator_step(&ctx->correlator, sample)); - - if(magnitude != NULL) { - *magnitude = mag; - } - - return (mag > ((float)preamble_get_symbol_count() / 2)); -} - - -result_t packet_demod_get_result_b(packet_demod_ctx_t *ctx, unsigned char *data, size_t *length) -{ - if(!((ctx->state == DATA_DECODED) || (ctx->state == DATA_DEMODULATED))) { - return ERR_INVALID_STATE; - } - - if(!data) { - // data is NULL, so we only return the required length - *length = ctx->length; - return OK; - } - - if(*length < ctx->length) { - return ERR_NO_MEM; - } - - *length = ctx->length; - memcpy(data, ctx->pkt_bytes, *length * sizeof(unsigned char)); - - return OK; -} - - -result_t packet_demod_get_result_cf(packet_demod_ctx_t *ctx, float complex *data, size_t *length) -{ - if(ctx->state != SYMBOLS_SET) { - return ERR_INVALID_STATE; - } - - if(!data) { - // data is NULL, so we only return the required length - *length = ctx->length; - return OK; - } - - if(*length < ctx->length) { - return ERR_NO_MEM; - } - - *length = ctx->length; - memcpy(data, ctx->pkt_symbols, *length * sizeof(float complex)); - - return OK; -} diff --git a/impl/src/layer1/packet_demod.h b/impl/src/layer1/packet_demod.h deleted file mode 100644 index c1cccdf..0000000 --- a/impl/src/layer1/packet_demod.h +++ /dev/null @@ -1,179 +0,0 @@ -#ifndef LAYER1_PACKET_DEMOD_H -#define LAYER1_PACKET_DEMOD_H - -#include -#include - -#include - -#include "correlator.h" - -#include "results.h" - -typedef enum -{ - SEARCHING_PREAMBLE, - HEADER_PARSED, - SYMBOLS_SET, - DATA_DEMODULATED, - DATA_DECODED, -} packet_demod_state_t; - -typedef struct -{ - unsigned char *pkt_bytes; - float complex *pkt_symbols; - - packet_demod_state_t state; - - correlator_ctx_t correlator; - - modem modem; - fec fec; - - modem hdr_modem; - fec hdr_fec; - - uint16_t raw_data_crc; - - size_t length; -} packet_demod_ctx_t; - - -/*!\brief Initialize the packet modulator context. - * - * \param[inout] ctx The context to initialize. - * \returns An result code (see results.h). - */ -result_t packet_demod_init(packet_demod_ctx_t *ctx); - -/*!\brief Free all resources in the given context. - * - * \param[inout] ctx The context to free. - * \returns An result code (see results.h). - */ -result_t packet_demod_free(packet_demod_ctx_t *ctx); - -/*!\brief Search for the preamble. - * - * Does a single correlation step and returns whether the correlation factor - * was high enough to indicate the presence of a preamble. - * - * \param[inout] ctx The context to use for this operation. - * \param[in] sample The received sample. - * \param[out] magnitude The magnitude of the correlation (optional, may be NULL). - * \returns Whether the magnitude was high enough to indicate a received preamble. - */ -bool packet_demod_search_preamble(packet_demod_ctx_t *ctx, float complex sample, float *magnitude); - -/*!\brief Set the synchronized packet samples for demodulation. - * - * This is can only be called after a header has been successfully parsed with - * \ref packet_demod_decode_header(). - * - * The stored symbols can be retrieved for verification using \ref - * packet_demod_get_result_cf(). - * - * \param[inout] ctx The context to use for this operation. - * \param[in] symbols The complex symbols of the packet. The required - * number of symbols is determined by the last - * successful call to \ref - * packet_demod_decode_header(). - * \param[in] length The length of the symbols array. - * \returns An result code (see results.h). - */ -result_t packet_demod_set_symbols(packet_demod_ctx_t *ctx, const float complex *symbols, size_t length); - -/*!\brief Decode the channel code of the demodulated symbols - * - * This can only be called after \ref packet_demod_demodulate(). - * - * The resulting data can be retrieved for verification using \ref - * packet_demod_get_result_b(). - * - * \param[inout] ctx The context to use for this operation. - * \returns An result code (see results.h). - */ -result_t packet_demod_decode(packet_demod_ctx_t *ctx); - -/*!\brief Demodulate the packet data. - * - * This can only be called after \ref packet_demod_set_symbols(). - * - * The resulting data can be retrieved for verification using \ref - * packet_demod_get_result_b(). - * - * \param[inout] ctx The context to use for this operation. - * \returns An result code (see results.h). - */ -result_t packet_demod_demodulate(packet_demod_ctx_t *ctx); - -/*!\brief Demodulate and decode the header. - * - * If the header is decoded successfully, the packet size and CRC are - * extracted. The CRC is stored in dhe packet demod context, the length is - * written to the location specified by \ref packet_length. - * - * This function works in any state and resets the demodulation chain if a - * header is successfully decoded. - * - * \param[inout] ctx The context to use for this operation. - * \param[in] symbols The complex header symbols. The length of this array - * must be at least the value returned by \ref - * packet_demod_get_header_length(). - * \param[out] packet_length Will be set to the number of symbol required - * to decode the packet. - * \returns An result code (see results.h). - */ -result_t packet_demod_decode_header(packet_demod_ctx_t *ctx, const float complex *symbols, size_t *packet_length); - -/*!\brief Get the length of the header in symbols. - * - * \param[in] ctx The context to use for this operation. - * \param[out] header_length Will be set to the number of symbol required - * to decode the header. - * \returns An result code (see results.h). - */ -result_t packet_demod_get_header_length(packet_demod_ctx_t *ctx, size_t *header_length); - -/*!\brief Get the result data as raw bytes. - * - * Raw bytes can be retrieved after \ref packet_demod_demodulate() and \ref - * packet_demod_decode(). 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. - */ -result_t packet_demod_get_result_b(packet_demod_ctx_t *ctx, unsigned char *data, size_t *length); - -/*!\brief Get the result data as complex numbers. - * - * Complex numbers can be retrieved after \ref packet_demod_set_symbols(). 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. - */ -result_t packet_demod_get_result_cf(packet_demod_ctx_t *ctx, float complex *data, size_t *length); - -#endif // LAYER1_PACKET_DEMOD_H