M17_Implementations/SP5WWP/m17-coder/m17-coder-sym.c

254 wiersze
7.6 KiB
C
Czysty Zwykły widok Historia

2022-12-07 16:51:28 +00:00
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include "../inc/m17.h"
2022-12-16 17:58:22 +00:00
#include "golay.h"
2022-12-07 16:51:28 +00:00
struct LSF
{
uint8_t dst[6];
uint8_t src[6];
uint8_t type[2];
uint8_t meta[112/8];
uint8_t crc[2];
} lsf;
2022-12-16 17:58:22 +00:00
uint8_t lich[6]; //48 bits packed raw, unencoded LICH
uint8_t lich_encoded[12]; //96 bits packed, encoded LICH
uint8_t enc_bits[SYM_PER_PLD*2]; //type-2 bits, unpacked
uint8_t rf_bits[SYM_PER_PLD*2]; //type-4 bits, unpacked
uint8_t data[16]; //raw payload, packed bits
uint16_t fn=0; //16-bit Frame Number (for the stream mode)
uint8_t lich_cnt=0; //0..5 LICH counter, derived from the Frame Number
uint8_t got_lsf=0; //have we filled the LSF struct yet?
2022-12-07 16:51:28 +00:00
2022-12-09 09:17:54 +00:00
void send_Preamble(const uint8_t type)
{
float symb;
2022-12-07 16:51:28 +00:00
2022-12-09 09:17:54 +00:00
if(type) //pre-BERT
{
for(uint16_t i=0; i<192/2; i++) //40ms * 4800 = 192
{
symb=-3.0;
write(STDOUT_FILENO, (uint8_t*)&symb, sizeof(float));
symb=+3.0;
write(STDOUT_FILENO, (uint8_t*)&symb, sizeof(float));
}
}
else //pre-LSF
{
for(uint16_t i=0; i<192/2; i++) //40ms * 4800 = 192
{
symb=+3.0;
write(STDOUT_FILENO, (uint8_t*)&symb, sizeof(float));
symb=-3.0;
write(STDOUT_FILENO, (uint8_t*)&symb, sizeof(float));
}
}
}
void send_Syncword(const uint16_t sword)
{
float symb;
2022-12-07 16:51:28 +00:00
2022-12-09 09:17:54 +00:00
for(uint8_t i=0; i<16; i+=2)
{
symb=symbol_map[(sword>>(14-i))&3];
write(STDOUT_FILENO, (uint8_t*)&symb, sizeof(float));
}
}
2022-12-07 16:51:28 +00:00
//main routine
int main(void)
{
2022-12-16 17:58:22 +00:00
//debug
//printf("%06X\n", golay24_encode(1)); //golay encoder codeword test
//printf("%d -> %d -> %d\n", 1, intrl_seq[1], intrl_seq[intrl_seq[1]]); //interleaver bijective reciprocality test, f(f(x))=x
//return 0;
2022-12-07 16:51:28 +00:00
while(1)
{
if(got_lsf) //stream frames
{
//we could discard the data we already have
while(read(STDIN_FILENO, &(lsf.dst), 6)<6);
while(read(STDIN_FILENO, &(lsf.src), 6)<6);
while(read(STDIN_FILENO, &(lsf.type), 2)<2);
while(read(STDIN_FILENO, &(lsf.meta), 14)<14);
while(read(STDIN_FILENO, data, 16)<16);
2022-12-09 09:17:54 +00:00
//send stream frame syncword
send_Syncword(SYNC_STR);
2022-12-16 14:07:45 +00:00
//derive the LICH_CNT from the Frame Number
lich_cnt=fn%6;
2022-12-16 17:58:22 +00:00
//extract LICH from the whole LSF
switch(lich_cnt)
{
case 0:
lich[0]=lsf.dst[0];
lich[1]=lsf.dst[1];
lich[2]=lsf.dst[2];
lich[3]=lsf.dst[3];
lich[4]=lsf.dst[4];
break;
case 1:
lich[0]=lsf.dst[5];
lich[1]=lsf.src[0];
lich[2]=lsf.src[1];
lich[3]=lsf.src[2];
lich[4]=lsf.src[3];
break;
case 2:
lich[0]=lsf.src[4];
lich[1]=lsf.src[5];
lich[2]=lsf.type[0];
lich[3]=lsf.type[1];
lich[4]=lsf.meta[0];
break;
case 3:
lich[0]=lsf.meta[1];
lich[1]=lsf.meta[2];
lich[2]=lsf.meta[3];
lich[3]=lsf.meta[4];
lich[4]=lsf.meta[5];
break;
case 4:
lich[0]=lsf.meta[6];
lich[1]=lsf.meta[7];
lich[2]=lsf.meta[8];
lich[3]=lsf.meta[9];
lich[4]=lsf.meta[10];
break;
case 5:
lich[0]=lsf.meta[11];
lich[1]=lsf.meta[12];
lich[2]=lsf.meta[13];
lich[3]=lsf.crc[0];
lich[4]=lsf.crc[1];
break;
default:
;
break;
}
lich[5]=lich_cnt<<5;
//encode the LICH
uint32_t val;
val=golay24_encode((lich[0]<<4)|(lich[1]>>4));
lich_encoded[0]=(val>>16)&0xFF;
lich_encoded[1]=(val>>8)&0xFF;
lich_encoded[2]=(val>>0)&0xFF;
val=golay24_encode(((lich[1]&0x0F)<<8)|lich[2]);
lich_encoded[3]=(val>>16)&0xFF;
lich_encoded[4]=(val>>8)&0xFF;
lich_encoded[5]=(val>>0)&0xFF;
val=golay24_encode((lich[3]<<4)|(lich[4]>>4));
lich_encoded[6]=(val>>16)&0xFF;
lich_encoded[7]=(val>>8)&0xFF;
lich_encoded[8]=(val>>0)&0xFF;
val=golay24_encode(((lich[4]&0x0F)<<8)|lich[5]);
lich_encoded[9]=(val>>16)&0xFF;
lich_encoded[10]=(val>>8)&0xFF;
lich_encoded[11]=(val>>0)&0xFF;
//unpack LICH (12 bytes)
memset(enc_bits, 0, SYM_PER_PLD*2);
for(uint8_t i=0; i<12; i++)
{
for(uint8_t j=0; j<8; j++)
enc_bits[i*8+j]=(lich_encoded[i]>>(7-j))&1;
}
//reorder bits
for(uint16_t i=0; i<SYM_PER_PLD*2; i++)
rf_bits[i]=enc_bits[intrl_seq[i]];
//randomize
for(uint16_t i=0; i<SYM_PER_PLD*2; i++)
{
if((rand_seq[i/8]>>(7-(i%8)))&1) //flip bit if '1'
{
if(rf_bits[i])
rf_bits[i]=0;
else
rf_bits[i]=1;
}
}
2022-12-09 09:17:54 +00:00
//send dummy symbols (debug)
2022-12-16 17:58:22 +00:00
/*float s=0.0;
for(uint8_t i=0; i<SYM_PER_PLD; i++) //40ms * 4800 - 8 (syncword)
write(STDOUT_FILENO, (uint8_t*)&s, sizeof(float));*/
float s;
for(uint16_t i=0; i<SYM_PER_PLD; i++) //40ms * 4800 - 8 (syncword)
{
s=symbol_map[rf_bits[2*i]*2+rf_bits[2*i+1]];
2022-12-09 09:17:54 +00:00
write(STDOUT_FILENO, (uint8_t*)&s, sizeof(float));
2022-12-16 17:58:22 +00:00
}
2022-12-09 09:17:54 +00:00
/*printf("\tDATA: ");
2022-12-07 16:51:28 +00:00
for(uint8_t i=0; i<16; i++)
printf("%02X", data[i]);
2022-12-09 09:17:54 +00:00
printf("\n");*/
2022-12-16 14:07:45 +00:00
//increment the Frame Number
fn++;
//debug-only
if(fn==6)
return 0;
2022-12-07 16:51:28 +00:00
}
else //LSF
{
while(read(STDIN_FILENO, &(lsf.dst), 6)<6);
while(read(STDIN_FILENO, &(lsf.src), 6)<6);
while(read(STDIN_FILENO, &(lsf.type), 2)<2);
while(read(STDIN_FILENO, &(lsf.meta), 14)<14);
while(read(STDIN_FILENO, data, 16)<16);
got_lsf=1;
2022-12-09 09:17:54 +00:00
//send out the preamble and LSF
send_Preamble(0); //0 - LSF preamble, as opposed to 1 - BERT preamble
//send LSF syncword
send_Syncword(SYNC_LSF);
//send dummy symbols (debug)
float s=0.0;
2022-12-09 09:19:43 +00:00
for(uint8_t i=0; i<184; i++) //40ms * 4800 - 8 (syncword)
2022-12-09 09:17:54 +00:00
write(STDOUT_FILENO, (uint8_t*)&s, sizeof(float));
/*printf("DST: ");
2022-12-07 16:51:28 +00:00
for(uint8_t i=0; i<6; i++)
printf("%02X", lsf.dst[i]);
printf(" SRC: ");
for(uint8_t i=0; i<6; i++)
printf("%02X", lsf.src[i]);
printf(" TYPE: ");
for(uint8_t i=0; i<2; i++)
printf("%02X", lsf.type[i]);
printf(" META: ");
for(uint8_t i=0; i<14; i++)
printf("%02X", lsf.meta[i]);
2022-12-09 09:17:54 +00:00
printf("\n");*/
2022-12-07 16:51:28 +00:00
}
}
return 0;
}