packet_mod: implement FEC and modulation for the header

This commit is contained in:
Thomas Kolb 2022-01-23 16:28:53 +01:00
parent 9a1107ff38
commit a1f62a3b0a
2 changed files with 39 additions and 22 deletions

View file

@ -15,6 +15,9 @@ result_t packet_mod_init(packet_mod_ctx_t *ctx)
ctx->fec = fec_create(CHANNEL_CODE, NULL); ctx->fec = fec_create(CHANNEL_CODE, NULL);
ctx->modem = modem_create(MODULATION); ctx->modem = modem_create(MODULATION);
ctx->hdr_fec = fec_create(HEADER_CHANNEL_CODE, NULL);
ctx->hdr_modem = modem_create(HEADER_MODULATION);
ctx->state = NOT_STARTED; ctx->state = NOT_STARTED;
return OK; return OK;
@ -48,6 +51,9 @@ result_t packet_mod_free(packet_mod_ctx_t *ctx)
fec_destroy(ctx->fec); fec_destroy(ctx->fec);
modem_destroy(ctx->modem); modem_destroy(ctx->modem);
fec_destroy(ctx->hdr_fec);
modem_destroy(ctx->hdr_modem);
return OK; return OK;
} }
@ -153,18 +159,16 @@ result_t packet_mod_add_header(packet_mod_ctx_t *ctx)
header[2] = (ctx->raw_data_crc >> 8) & 0xFF; header[2] = (ctx->raw_data_crc >> 8) & 0xFF;
header[3] = (ctx->raw_data_crc >> 0) & 0xFF; header[3] = (ctx->raw_data_crc >> 0) & 0xFF;
// the header is coded and modulated differently than the data. // note: the header is coded and modulated differently than the data.
fec hdr_fec = fec_create(HEADER_CHANNEL_CODE, NULL);
modem hdr_modem = modem_create(HEADER_MODULATION);
// encode the header // encode the header
unsigned int hdr_enc_length = fec_get_enc_msg_length(HEADER_CHANNEL_CODE, sizeof(header)); unsigned int hdr_enc_length = fec_get_enc_msg_length(HEADER_CHANNEL_CODE, sizeof(header));
unsigned char header_encoded[hdr_enc_length]; unsigned char header_encoded[hdr_enc_length];
ERR_CHECK_LIQUID(fec_encode(hdr_fec, sizeof(header), header, header_encoded)); ERR_CHECK_LIQUID(fec_encode(ctx->hdr_fec, sizeof(header), header, header_encoded));
// modulate the header // modulate the header
unsigned int bps = modem_get_bps(hdr_modem); unsigned int bps = modem_get_bps(ctx->hdr_modem);
unsigned int nsyms = (hdr_enc_length * 8 + bps/2) / bps; unsigned int nsyms = (hdr_enc_length * 8 + bps/2) / bps;
unsigned char header_sym_idcs[nsyms]; unsigned char header_sym_idcs[nsyms];
@ -173,7 +177,7 @@ result_t packet_mod_add_header(packet_mod_ctx_t *ctx)
ERR_CHECK_LIQUID(liquid_repack_bytes(header_encoded, 8, hdr_enc_length, header_sym_idcs, bps, nsyms, &nsyms)); ERR_CHECK_LIQUID(liquid_repack_bytes(header_encoded, 8, hdr_enc_length, header_sym_idcs, bps, nsyms, &nsyms));
for(size_t i = 0; i < nsyms; i++) { for(size_t i = 0; i < nsyms; i++) {
ERR_CHECK_LIQUID(modem_modulate(hdr_modem, header_sym_idcs[i], &header_mod[i])); ERR_CHECK_LIQUID(modem_modulate(ctx->hdr_modem, header_sym_idcs[i], &header_mod[i]));
} }
// concatenate the modulated header and message // concatenate the modulated header and message
@ -228,6 +232,29 @@ result_t packet_mod_add_preamble(packet_mod_ctx_t *ctx)
} }
result_t packet_mod_get_result_b(packet_mod_ctx_t *ctx, unsigned char *data, size_t *length)
{
if(!((ctx->state == DATA_RAW) || (ctx->state == DATA_CODED))) {
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_mod_get_result_cf(packet_mod_ctx_t *ctx, float complex *data, size_t *length) result_t packet_mod_get_result_cf(packet_mod_ctx_t *ctx, float complex *data, size_t *length)
{ {
if(!((ctx->state == DATA_MODULATED) || (ctx->state == HEADER_ADDED) || (ctx->state == PREAMBLE_ADDED))) { if(!((ctx->state == DATA_MODULATED) || (ctx->state == HEADER_ADDED) || (ctx->state == PREAMBLE_ADDED))) {

View file

@ -1,5 +1,5 @@
#ifndef PACKET_MOD #ifndef PACKET_MOD_H
#define PACKET_MOD #define PACKET_MOD_H
#include <stdlib.h> #include <stdlib.h>
#include <complex.h> #include <complex.h>
@ -28,6 +28,9 @@ typedef struct
modem modem; modem modem;
fec fec; fec fec;
modem hdr_modem;
fec hdr_fec;
uint16_t raw_data_crc; uint16_t raw_data_crc;
size_t length; size_t length;
@ -121,19 +124,6 @@ result_t packet_mod_add_header(packet_mod_ctx_t *ctx);
*/ */
result_t packet_mod_add_preamble(packet_mod_ctx_t *ctx); result_t packet_mod_add_preamble(packet_mod_ctx_t *ctx);
/*!\brief Dump the internal data to the given file.
*
* The format of the resulting file depends on the current state of the packet
* modulation context.
*
* \param[in] ctx The context to use for this operation.
* \param[in] filename Name of the file to write to.
* \returns An result code (see results.h).
*/
result_t packet_mod_dump(packet_mod_ctx_t *ctx, const char *filename);
/*!\brief Get the result data as raw bytes. /*!\brief Get the result data as raw bytes.
* *
* Raw bytes can be retrieved after \ref packet_mod_set_data() and \ref * Raw bytes can be retrieved after \ref packet_mod_set_data() and \ref
@ -175,4 +165,4 @@ result_t packet_mod_get_result_b(packet_mod_ctx_t *ctx, unsigned char *data, siz
*/ */
result_t packet_mod_get_result_cf(packet_mod_ctx_t *ctx, float complex *data, size_t *length); result_t packet_mod_get_result_cf(packet_mod_ctx_t *ctx, float complex *data, size_t *length);
#endif // PACKET_MOD #endif // PACKET_MOD_H