1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-07-20 19:02:55 +00:00

Adapt to gnss-sdr coding style

This commit is contained in:
Carles Fernandez 2017-03-28 19:32:42 +02:00
parent fa4eb25920
commit cff63b378a
6 changed files with 561 additions and 504 deletions

View File

@ -73,7 +73,8 @@ u32 getbitu(const u8 *buff, u32 pos, u8 len)
{
u32 bits = 0;
u32 i=0;
for ( i= pos; i < pos + len; i++) {
for (i = pos; i < pos + len; i++)
{
bits = (bits << 1) +
((buff[i/8] >> (7 - i%8)) & 1u);
}
@ -118,8 +119,9 @@ void setbitu(u8 *buff, u32 pos, u32 len, u32 data)
if (len <= 0 || 32 < len)
return;
u32 i=0;
for (i = pos; i < pos + len; i++, mask >>= 1) {
u32 i = 0;
for (i = pos; i < pos + len; i++, mask >>= 1)
{
if (data & mask)
buff[i/8] |= 1u << (7 - i % 8);
else
@ -153,35 +155,40 @@ void setbits(u8 *buff, u32 pos, u32 len, s32 data)
*/
void bitshl(void *buf, u32 size, u32 shift)
{
if (shift > size * CHAR_BIT) {
if (shift > size * CHAR_BIT)
{
/* Quick check: if the shift is larger, than the buffer, zero the data */
memset(buf, 0, size);
return;
}
unsigned char *dst = buf; /* Destination byte. */
const unsigned char *src = dst + shift / CHAR_BIT; /* First source byte,
* possibly incomplete. */
const unsigned char *src = dst + shift / CHAR_BIT; /* First source byte, possibly incomplete. */
u32 copy_bits = size * CHAR_BIT - shift; /* Number of bits to move */
u32 byte_shift = copy_bits % CHAR_BIT; /* Shift of data */
u32 full_bytes = copy_bits / CHAR_BIT; /* Number of bytes to move */
if (0 == byte_shift) {
if (0 == byte_shift)
{
/* When moving data in character boundaries, use built-in functions: move
* data, and then zero the tail. */
memmove(dst, src, full_bytes);
memset(dst + full_bytes, 0, size - full_bytes);
} else {
}
else
{
/* Create an accumulator: it will hold a value of two consecutive bytes */
u32 acc = *src++;
u32 i=0;
for (i = 0; i < full_bytes; ++i) {
u32 i = 0;
for (i = 0; i < full_bytes; ++i)
{
acc = (acc << CHAR_BIT) | *src++;
*dst++ = acc >> byte_shift;
}
*dst++ = acc << CHAR_BIT >> byte_shift;
if (full_bytes + 1 < size) {
if (full_bytes + 1 < size)
{
memset(dst, 0, size - full_bytes - 1);
}
}
@ -208,14 +215,16 @@ void bitcopy(void *dst, u32 dst_index, const void *src, u32 src_index,
{
u32 limit1 = count / 32;
u32 limit2 = count % 32;
u32 idx=0;
for (idx = 0; idx < limit1; ++idx) {
u32 idx = 0;
for (idx = 0; idx < limit1; ++idx)
{
u32 tmp = getbitu(src, src_index, 32);
setbitu(dst, dst_index, 32, tmp);
src_index += 32;
dst_index += 32;
}
if (0 != limit2) {
if (0 != limit2)
{
u32 tmp = getbitu(src, src_index, limit2);
setbitu(dst, dst_index, limit2, tmp);
}
@ -232,7 +241,7 @@ void bitcopy(void *dst, u32 dst_index, const void *src, u32 src_index,
u8 count_bits_u64(u64 v, u8 bv)
{
u8 r = 0;
int i =0;
int i = 0;
for (i = 0; i < 16; i++)
r += bitn[(v >> (i*4)) & 0xf];
return bv == 1 ? r : 64 - r;
@ -249,7 +258,7 @@ int i =0;
u8 count_bits_u32(u32 v, u8 bv)
{
u8 r = 0;
int i=0;
int i = 0;
for (i = 0; i < 8; i++)
r += bitn[(v >> (i*4)) & 0xf];
return bv == 1 ? r : 32 - r;
@ -266,7 +275,7 @@ u8 count_bits_u32(u32 v, u8 bv)
u8 count_bits_u16(u16 v, u8 bv)
{
u8 r = 0;
int i =0;
int i = 0;
for (i= 0; i < 4; i++)
r += bitn[(v >> (i*4)) & 0xf];
return bv == 1 ? r : 16 - r;
@ -283,7 +292,7 @@ int i =0;
u8 count_bits_u8(u8 v, u8 bv)
{
u8 r = 0;
int i=0;
int i = 0;
for (i = 0; i < 2; i++)
r += bitn[(v >> (i*4)) & 0xf];
return bv == 1 ? r : 8 - r;

View File

@ -90,6 +90,7 @@ static u32 _cnav_compute_crc(cnav_v27_part_t *part)
return crc;
}
/**
* Extracts CRC-24Q from a CNAV message buffer.
* CRC-24Q value is the last 24 bits from 300 bits message buffer.
@ -104,12 +105,14 @@ static u32 _cnav_extract_crc(const cnav_v27_part_t *part)
{
u32 crc = getbitu(part->decoded, GPS_CNAV_MSG_DATA_LENGTH,
GPS_CNAV_MSG_CRC_LENGTH);
if (part->invert) {
if (part->invert)
{
crc ^= 0xFFFFFF;
}
return crc;
}
/**
* Helper to rescan for preamble in the received buffer.
* Occasionally there could be a false lock on message contents if it the
@ -128,13 +131,15 @@ static void _cnav_rescan_preamble(cnav_v27_part_t *part)
{
part->preamble_seen = false;
if (part->n_decoded > GPS_CNAV_PREAMBLE_LENGTH + 1) {
size_t i=0;
size_t j=0;
for (i = 1, j = part->n_decoded - GPS_CNAV_PREAMBLE_LENGTH;
i < j; ++i) {
if (part->n_decoded > GPS_CNAV_PREAMBLE_LENGTH + 1)
{
size_t i = 0;
size_t j = 0;
for (i = 1, j = part->n_decoded - GPS_CNAV_PREAMBLE_LENGTH; i < j; ++i)
{
u32 c = getbitu(part->decoded, i, GPS_CNAV_PREAMBLE_LENGTH);
if (GPS_CNAV_PREAMBLE1 == c || GPS_CNAV_PREAMBLE2 == c) {
if (GPS_CNAV_PREAMBLE1 == c || GPS_CNAV_PREAMBLE2 == c)
{
part->preamble_seen = true;
part->invert = (GPS_CNAV_PREAMBLE2 == c);
/* We shift the accumulated bits to the beginning of the buffer */
@ -144,13 +149,15 @@ static void _cnav_rescan_preamble(cnav_v27_part_t *part)
}
}
}
if (!part->preamble_seen && part->n_decoded >= GPS_CNAV_PREAMBLE_LENGTH) {
if (!part->preamble_seen && part->n_decoded >= GPS_CNAV_PREAMBLE_LENGTH)
{
bitshl(part->decoded, sizeof(part->decoded),
part->n_decoded - GPS_CNAV_PREAMBLE_LENGTH + 1);
part->n_decoded = GPS_CNAV_PREAMBLE_LENGTH - 1;
}
}
/**
* Feed a symbol into Viterbi decoder instance.
*
@ -169,14 +176,17 @@ static void _cnav_add_symbol(cnav_v27_part_t *part, u8 ch)
{
part->symbols[part->n_symbols++] = ch;
if (part->init) {
if (part->init)
{
/* Initial step - load more symbols without decoding. */
if (part->n_symbols < (GPS_L2C_V27_INIT_BITS + GPS_L2C_V27_DECODE_BITS) * 2) {
if (part->n_symbols < (GPS_L2C_V27_INIT_BITS + GPS_L2C_V27_DECODE_BITS) * 2)
{
return;
}
part->init = false;
}
else if (part->n_symbols < GPS_L2C_V27_DECODE_BITS * 2) {
else if (part->n_symbols < GPS_L2C_V27_DECODE_BITS * 2)
{
/* Wait until decoding block is accumulated */
return;
}
@ -217,30 +227,38 @@ static void _cnav_add_symbol(cnav_v27_part_t *part, u8 ch)
*/
bool retry = true;
while (retry) {
while (retry)
{
retry = false;
if (!part->preamble_seen) {
if (!part->preamble_seen)
{
/* Rescan for preamble if possible. The first bit is ignored. */
_cnav_rescan_preamble(part);
}
if (part->preamble_seen && GPS_CNAV_MSG_LENGTH <= part->n_decoded) {
if (part->preamble_seen && GPS_CNAV_MSG_LENGTH <= part->n_decoded)
{
/* We have collected 300 bits starting from message preamble. Now try
* to compute CRC-24Q */
u32 crc = _cnav_compute_crc(part);
u32 crc2 = _cnav_extract_crc(part);
if (part->message_lock) {
if (part->message_lock)
{
/* We have message lock */
part->crc_ok = (crc == crc2);
if (part->crc_ok) {
if (part->crc_ok)
{
/* Reset message lock counter */
part->n_crc_fail = 0;
} else {
}
else
{
/* Increment message lock counter */
part->n_crc_fail++;
if (part->n_crc_fail > GPS_CNAV_LOCK_MAX_CRC_FAILS) {
if (part->n_crc_fail > GPS_CNAV_LOCK_MAX_CRC_FAILS)
{
/* CRC has failed too many times - drop the lock. */
part->n_crc_fail = 0;
part->message_lock = false;
@ -249,12 +267,16 @@ static void _cnav_add_symbol(cnav_v27_part_t *part, u8 ch)
retry = true;
}
}
} else if (crc == crc2) {
}
else if (crc == crc2)
{
/* CRC match - message can be decoded */
part->message_lock = true;
part->crc_ok = true;
part->n_crc_fail = 0;
} else {
}
else
{
/* There is no message lock and the CRC check fails. Assume there is
* false positive lock - rescan for preamble. */
part->crc_ok = false;
@ -271,6 +293,7 @@ static void _cnav_add_symbol(cnav_v27_part_t *part, u8 ch)
}
}
/**
* Invert message bits in the buffer.
*
@ -283,11 +306,13 @@ static void _cnav_add_symbol(cnav_v27_part_t *part, u8 ch)
static void _cnav_msg_invert(cnav_v27_part_t *part)
{
size_t i = 0;
for (i = 0; i < sizeof(part->decoded); i++) {
for (i = 0; i < sizeof(part->decoded); i++)
{
part->decoded[i] ^= 0xFFu;
}
}
/**
* Performs CNAV message decoding.
*
@ -311,10 +336,13 @@ static void _cnav_msg_invert(cnav_v27_part_t *part)
static bool _cnav_msg_decode(cnav_v27_part_t *part, cnav_msg_t *msg, u32 *delay)
{
bool res = false;
if (GPS_CNAV_MSG_LENGTH <= part->n_decoded) {
if (part->crc_ok) {
if (GPS_CNAV_MSG_LENGTH <= part->n_decoded)
{
if (part->crc_ok)
{
/* CRC is OK */
if (part->invert) {
if (part->invert)
{
_cnav_msg_invert(part);
}
@ -326,14 +354,16 @@ static bool _cnav_msg_decode(cnav_v27_part_t *part, cnav_msg_t *msg, u32 *delay)
/* copy RAW message for GNSS-SDR */
memcpy(msg->raw_msg,part->decoded,GPS_L2C_V27_DECODE_BITS + GPS_L2C_V27_DELAY_BITS);
*delay = (part->n_decoded - GPS_CNAV_MSG_LENGTH + GPS_L2C_V27_DELAY_BITS)
* 2 + part->n_symbols;
*delay = (part->n_decoded - GPS_CNAV_MSG_LENGTH + GPS_L2C_V27_DELAY_BITS) * 2 + part->n_symbols;
if (part->invert) {
if (part->invert)
{
_cnav_msg_invert(part);
}
res = true;
} else {
}
else
{
/* CRC mismatch - no decoding */
}
bitshl(part->decoded, sizeof(part->decoded), GPS_CNAV_MSG_LENGTH);
@ -343,6 +373,7 @@ static bool _cnav_msg_decode(cnav_v27_part_t *part, cnav_msg_t *msg, u32 *delay)
return res;
}
/**
* Initialize CNAV decoder.
*
@ -401,13 +432,15 @@ bool cnav_msg_decoder_add_symbol(cnav_msg_decoder_t *dec,
_cnav_add_symbol(&dec->part1, symbol);
_cnav_add_symbol(&dec->part2, symbol);
if (dec->part1.message_lock) {
if (dec->part1.message_lock)
{
/* Flush data in decoder. */
dec->part2.n_decoded = 0;
dec->part2.n_symbols = 0;
return _cnav_msg_decode(&dec->part1, msg, pdelay);
}
if (dec->part2.message_lock) {
if (dec->part2.message_lock)
{
/* Flush data in decoder. */
dec->part1.n_decoded = 0;
dec->part1.n_symbols = 0;
@ -417,6 +450,7 @@ bool cnav_msg_decoder_add_symbol(cnav_msg_decoder_t *dec,
return false;
}
/**
* Provides a singleton polynomial object.
*
@ -432,7 +466,8 @@ const v27_poly_t *cnav_msg_decoder_get_poly(void)
static v27_poly_t instance;
static bool initialized = false;
if (!initialized) {
if (!initialized)
{
/* Coefficients for polynomial object */
const signed char coeffs[2] = { GPS_L2C_V27_POLY_A, GPS_L2C_V27_POLY_B };

View File

@ -91,8 +91,8 @@ static const u32 crc24qtab[256] = {
*/
u32 crc24q(const u8 *buf, u32 len, u32 crc)
{
u32 i=0;
for ( i= 0; i < len; i++)
u32 i = 0;
for (i = 0; i < len; i++)
crc = ((crc << 8) & 0xFFFFFF) ^ crc24qtab[((crc >> 16) ^ buf[i]) & 0xff];
return crc;
}
@ -118,17 +118,20 @@ u32 crc24q_bits(u32 crc, const u8 *buf, u32 n_bits, bool invert)
u8 b = 0;
u32 shift = 8 - n_bits % 8;
u32 i=0;
for ( i= 0; i < n_bits / 8; ++i) {
u32 i = 0;
for (i = 0; i < n_bits / 8; ++i)
{
acc = (acc << 8) | *buf++;
if (invert) {
if (invert)
{
acc ^= 0xFFu;
}
b = (acc >> shift) & 0xFFu;
crc = ((crc << 8) & 0xFFFFFFu) ^ crc24qtab[((crc >> 16) ^ b) & 0xFFu];
}
acc = (acc << 8) | *buf;
if (invert) {
if (invert)
{
acc ^= 0xFFu;
}
b = (acc >> shift) & 0xFFu;

View File

@ -42,6 +42,7 @@ static inline int parity(int x)
return (0x6996 >> x) & 1;
}
/** Initialize a v27_poly_t struct for use with a v27_t decoder.
*
* \param poly Structure to initialize.
@ -51,12 +52,14 @@ void v27_poly_init(v27_poly_t *poly, const signed char polynomial[2])
{
int state;
for(state = 0; state < 32; state++) {
for(state = 0; state < 32; state++)
{
poly->c0[state] = (polynomial[0] < 0) ^ parity((2*state) & abs(polynomial[0])) ? 255 : 0;
poly->c1[state] = (polynomial[1] < 0) ^ parity((2*state) & abs(polynomial[1])) ? 255 : 0;
}
}
/** Initialize a v27_t struct for Viterbi decoding.
*
* \param v Structure to initialize
@ -86,9 +89,10 @@ void v27_init(v27_t *v, v27_decision_t *decisions, unsigned int decisions_count,
v->old_metrics[initial_state & 63] = 0; /* Bias known start state */
}
/* C-language butterfly */
#define BFLY(i) {\
unsigned int metric,m0,m1,decision;\
unsigned int metric,m0,m1,decision;\
metric = (v->poly->c0[i] ^ sym0) + (v->poly->c1[i] ^ sym1);\
m0 = v->old_metrics[i] + metric;\
m1 = v->old_metrics[i+32] + (510 - metric);\
@ -115,7 +119,8 @@ void v27_update(v27_t *v, const unsigned char *syms, int nbits)
unsigned int *tmp;
int normalize = 0;
while(nbits--) {
while(nbits--)
{
v27_decision_t *d = &v->decisions[v->decisions_index];
d->w[0] = d->w[1] = 0;
@ -156,16 +161,18 @@ void v27_update(v27_t *v, const unsigned char *syms, int nbits)
BFLY(31);
/* Normalize metrics if they are nearing overflow */
if(v->new_metrics[0] > (1<<30)) {
if(v->new_metrics[0] > (1 << 30))
{
int i;
unsigned int minmetric = 1<<31;
unsigned int minmetric = 1 << 31;
for(i=0; i<64; i++) {
for(i = 0; i < 64; i++)
{
if(v->new_metrics[i] < minmetric)
minmetric = v->new_metrics[i];
}
for(i=0; i<64; i++)
for(i = 0; i < 64; i++)
v->new_metrics[i] -= minmetric;
normalize += minmetric;
@ -182,6 +189,7 @@ void v27_update(v27_t *v, const unsigned char *syms, int nbits)
}
}
/** Retrieve the most likely output bit sequence with known final state from
* a v27_t decoder.
*
@ -199,22 +207,24 @@ void v27_chainback_fixed(v27_t *v, unsigned char *data, unsigned int nbits,
final_state %= 64;
final_state <<= 2;
while(nbits-- != 0) {
while(nbits-- != 0)
{
/* Decrement decision index */
decisions_index = (decisions_index == 0) ?
v->decisions_count-1 : decisions_index-1;
v27_decision_t *d = &v->decisions[decisions_index];
k = (d->w[(final_state>>2)/32] >> ((final_state>>2)%32)) & 1;
k = (d->w[(final_state >> 2) / 32] >> ((final_state >> 2) % 32)) & 1;
/* The store into data[] only needs to be done every 8 bits.
* But this avoids a conditional branch, and the writes will
* combine in the cache anyway
*/
data[nbits>>3] = final_state = (final_state >> 1) | (k << 7);
data[nbits >> 3] = final_state = (final_state >> 1) | (k << 7);
}
}
/** Retrieve the most likely output bit sequence with unknown final state from
* a v27_t decoder.
*
@ -229,7 +239,7 @@ void v27_chainback_likely(v27_t *v, unsigned char *data, unsigned int nbits)
int i;
unsigned int best_metric = 0xffffffff;
unsigned char best_state = 0;
for(i=0; i<64; i++)
for(i = 0; i < 64; i++)
{
if(v->new_metrics[i] < best_metric)
{