diff --git a/acars.h b/acars.h index 1fbc49d..ffd0cb3 100644 --- a/acars.h +++ b/acars.h @@ -17,25 +17,25 @@ * */ typedef struct { - char mode; - char reg[8]; - char ack; - char label[3]; - char bid; - char no[5]; - char fid[7]; - char bs, be; - char txt[17000]; + char mode; + char reg[8]; + char ack; + char label[3]; + char bid; + char no[5]; + char fid[7]; + char bs, be; + char txt[17000]; } acarsmsg_t; typedef struct { - char da[5]; - char sa[5]; - char eta[5]; - char gout[5]; - char gin[5]; - char woff[5]; - char won[5]; + char da[5]; + char sa[5]; + char eta[5]; + char gout[5]; + char gin[5]; + char woff[5]; + char won[5]; } oooi_t; extern void outacars(unsigned char *txt, int len); diff --git a/crc.c b/crc.c index 3004599..5694d18 100644 --- a/crc.c +++ b/crc.c @@ -1,35 +1,34 @@ const unsigned short crc_ccitt_table[256] = { - 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, - 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, - 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, - 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, - 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, - 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, - 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, - 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, - 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, - 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, - 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, - 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, - 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, - 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, - 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, - 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, - 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, - 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, - 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, - 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, - 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, - 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, - 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, - 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, - 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, - 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, - 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, - 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, - 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, - 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, - 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, - 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 + 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, + 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, + 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, + 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, + 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, + 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, + 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, + 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, + 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, + 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, + 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, + 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, + 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, + 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, + 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, + 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, + 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, + 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, + 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, + 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, + 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, + 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, + 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, + 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, + 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, + 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, + 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, + 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, + 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, + 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, + 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, + 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 }; - diff --git a/d8psk.c b/d8psk.c index e6a08e4..72d5d76 100644 --- a/d8psk.c +++ b/d8psk.c @@ -28,360 +28,381 @@ static struct timespec ftime; static unsigned int ink; -int initD8psk(channel_t *ch) +int initD8psk(channel_t * ch) { - ch->ink = 0; - ch->Phidx = 0; - ch->df = 0; - ch->perr = 100; - ch->P1 = 0; + ch->ink = 0; + ch->Phidx = 0; + ch->df = 0; + ch->perr = 100; + ch->P1 = 0; - return 0; + return 0; } unsigned int reversebits(const unsigned int bits, const int n) { - int i; - unsigned int in, out; - - in = bits; - out = 0; - for (i = 0; i < n; i++) { - out <<= 1; - out |= in & 1; - in >>= 1; - } - return out; + int i; + unsigned int in, out; + + in = bits; + out = 0; + for (i = 0; i < n; i++) { + out <<= 1; + out |= in & 1; + in >>= 1; + } + return out; } -static inline float descrambler(channel_t *ch,const float V) +static inline float descrambler(channel_t * ch, const float V) { - int b; + int b; - b = (ch->scrambler ^ (ch->scrambler >> 14)) & 1; - ch->scrambler = (ch->scrambler << 1) | b; - if (b) - return (1.0 - V); - else - return V; + b = (ch->scrambler ^ (ch->scrambler >> 14)) & 1; + ch->scrambler = (ch->scrambler << 1) | b; + if (b) + return (1.0 - V); + else + return V; } -static void putbit(channel_t *ch,const float SV) +static void putbit(channel_t * ch, const float SV) { - float V; - - V = descrambler(ch,SV); - - switch (ch->state) { - case WSYNC: - /* ???? */ - return; - case GETHEAD: - { - unsigned int bits, len; - - if(ch->nbits<3) V=0; - viterbi_add(V, ch->nbits); - ch->nbits++; - if (ch->nbits < 25) - return; - - viterbi_end(&bits); - - bits >>= 5; /* remove FEC */ - - len = reversebits(bits, 17); - - ch->nbrow=ch->blk->nbrow = len / 1992 + 1; - ch->nlbyte=ch->blk->nlbyte = (len % 1992 + 7) / 8; - - if (len < 12*8) { - if(verbose>1)fprintf(stderr,"error too short %d #%d\n",len,ch->chn+1); - ch->state = WSYNC; - return; - } - - if (ch->blk->nbrow > 8) { - if(verbose>1)fprintf(stderr,"error too long %d #%d\n",ch->blk->nbrow,ch->chn); - ch->state = WSYNC; - return; - } - - if(verbose>1) - fprintf(stderr,"#%d %d row %d %d\n",ch->chn+1,len,ch->nbrow,ch->nlbyte); - - ch->state = GETDATA; - ch->nrow = ch->nbyte = 0; - ch->nbits = ch->bits = 0; - - return; - } - case GETDATA: - { - if (V > 0.5) { - ch->bits |= 1 << ch->nbits; - } - - ch->nbits++; - if (ch->nbits < 8) return; - - ch->blk->data[ch->nrow][ch->nbyte] = ch->bits; - //fprintf(stderr,"data %d %d %x\n",ch->nrow,ch->nbyte,ch->bits); - - ch->nbits = 0; - ch->bits = 0; - ch->nrow++; - - if (ch->nrow == ch->nbrow ) { - ch->nrow = 0; - ch->nbyte++; - } - - if(ch->nlbyte) - while(ch->nrow == ch->nbrow-1 && ch->nbyte >= ch->nlbyte && ch->nbyte < 249) { - ch->blk->data[ch->nrow][ch->nbyte]=0; - //fprintf(stderr,"data0 %d %d\n",ch->nrow,ch->nbyte); - ch->nrow = 0; - ch->nbyte++; + float V; + + V = descrambler(ch, SV); + + switch (ch->state) { + case WSYNC: + /* ???? */ + return; + case GETHEAD: + { + unsigned int bits, len; + + if (ch->nbits < 3) + V = 0; + viterbi_add(V, ch->nbits); + ch->nbits++; + if (ch->nbits < 25) + return; + + viterbi_end(&bits); + + bits >>= 5; /* remove FEC */ + + len = reversebits(bits, 17); + + ch->nbrow = ch->blk->nbrow = len / 1992 + 1; + ch->nlbyte = ch->blk->nlbyte = (len % 1992 + 7) / 8; + + if (len < 12 * 8) { + if (verbose > 1) + fprintf(stderr, + "error too short %d #%d\n", len, + ch->chn + 1); + ch->state = WSYNC; + return; + } + + if (ch->blk->nbrow > 8) { + if (verbose > 1) + fprintf(stderr, + "error too long %d #%d\n", + ch->blk->nbrow, ch->chn); + ch->state = WSYNC; + return; + } + + if (verbose > 1) + fprintf(stderr, "#%d %d row %d %d\n", + ch->chn + 1, len, ch->nbrow, + ch->nlbyte); + + ch->state = GETDATA; + ch->nrow = ch->nbyte = 0; + ch->nbits = ch->bits = 0; + + return; } - - if(ch->nbyte == 249) { - ch->state = GETFEC; - ch->nrow=ch->nbyte = 0; - - if(ch->nlbyte<=2) { - ch->nlbyte=0; - ch->nbrow--; - } else - if(ch->nlbyte<=30) - ch->nlbyte=2; - else - if(ch->nlbyte<=67) - ch->nlbyte=4; - else - ch->nlbyte=0; - - } - return; - } - case GETFEC: - { - if (V > 0.5) { - ch->bits |= 1 << ch->nbits; - } - - ch->nbits++; - if (ch->nbits < 8) return; - - ch->blk->data[ch->nrow][ch->nbyte+249] = ch->bits; - //fprintf(stderr,"data fec %d %d %x\n",ch->nrow,ch->nbyte+249,ch->bits); - - ch->nbits = 0; - ch->bits = 0; - ch->nrow++; - - if (ch->nrow == ch->nbrow ) { - ch->nrow = 0; - ch->nbyte++; - } - - if(ch->nlbyte) - while(ch->nrow == ch->nbrow-1 && ch->nbyte >= ch->nlbyte && ch->nbyte < 6) { - ch->blk->data[ch->nrow][ch->nbyte+249]=0; - //fprintf(stderr,"data fec0 %d %d\n",ch->nrow,ch->nbyte+249); - ch->nrow = 0; - ch->nbyte++; + case GETDATA: + { + if (V > 0.5) { + ch->bits |= 1 << ch->nbits; + } + + ch->nbits++; + if (ch->nbits < 8) + return; + + ch->blk->data[ch->nrow][ch->nbyte] = ch->bits; + //fprintf(stderr,"data %d %d %x\n",ch->nrow,ch->nbyte,ch->bits); + + ch->nbits = 0; + ch->bits = 0; + ch->nrow++; + + if (ch->nrow == ch->nbrow) { + ch->nrow = 0; + ch->nbyte++; + } + + if (ch->nlbyte) + while (ch->nrow == ch->nbrow - 1 + && ch->nbyte >= ch->nlbyte + && ch->nbyte < 249) { + ch->blk->data[ch->nrow][ch->nbyte] = 0; + //fprintf(stderr,"data0 %d %d\n",ch->nrow,ch->nbyte); + ch->nrow = 0; + ch->nbyte++; + } + + if (ch->nbyte == 249) { + ch->state = GETFEC; + ch->nrow = ch->nbyte = 0; + + if (ch->nlbyte <= 2) { + ch->nlbyte = 0; + ch->nbrow--; + } else if (ch->nlbyte <= 30) + ch->nlbyte = 2; + else if (ch->nlbyte <= 67) + ch->nlbyte = 4; + else + ch->nlbyte = 0; + + } + return; + } + case GETFEC: + { + if (V > 0.5) { + ch->bits |= 1 << ch->nbits; + } + + ch->nbits++; + if (ch->nbits < 8) + return; + + ch->blk->data[ch->nrow][ch->nbyte + 249] = ch->bits; + //fprintf(stderr,"data fec %d %d %x\n",ch->nrow,ch->nbyte+249,ch->bits); + + ch->nbits = 0; + ch->bits = 0; + ch->nrow++; + + if (ch->nrow == ch->nbrow) { + ch->nrow = 0; + ch->nbyte++; + } + + if (ch->nlbyte) + while (ch->nrow == ch->nbrow - 1 + && ch->nbyte >= ch->nlbyte + && ch->nbyte < 6) { + ch->blk->data[ch->nrow][ch->nbyte + + 249] = 0; + //fprintf(stderr,"data fec0 %d %d\n",ch->nrow,ch->nbyte+249); + ch->nrow = 0; + ch->nbyte++; + } + + if (ch->nbyte == 6) { + + decodeVdlm2(ch); + + ch->state = WSYNC; + } + return; } - - if (ch->nbyte == 6 ) { - - decodeVdlm2(ch); - - ch->state = WSYNC; - } - return; } - } } -static inline void putgreycode(channel_t *ch,const float v) +static inline void putgreycode(channel_t * ch, const float v) { - int i = (int)roundf(128.0 * v / M_PI + 128.0); - putbit(ch,Grey1[i]); - putbit(ch,Grey2[i]); - putbit(ch,Grey3[i]); + int i = (int)roundf(128.0 * v / M_PI + 128.0); + putbit(ch, Grey1[i]); + putbit(ch, Grey2[i]); + putbit(ch, Grey3[i]); } -static inline float filteredphase(channel_t *ch) +static inline float filteredphase(channel_t * ch) { - int i,k; - complex float S = 0; - - for (i=ch->clk,k=ch->ink; i< MFLTLEN;i=i+4,k=(k+1)%MBUFLEN) { - S += ch->Inbuff[k] * mflt[i]; - } - /* phase */ - return cargf(S); -} + int i, k; + complex float S = 0; + for (i = ch->clk, k = ch->ink; i < MFLTLEN; + i = i + 4, k = (k + 1) % MBUFLEN) { + S += ch->Inbuff[k] * mflt[i]; + } + /* phase */ + return cargf(S); +} -static inline void demodD8psk(channel_t *ch,const complex float E) +static inline void demodD8psk(channel_t * ch, const complex float E) { - int i, k; - complex float S; - float P; - float d[4]; - - ch->Inbuff[ch->ink] = E; - ch->ink = (ch->ink + 1) % MBUFLEN; - - ch->clk+=4; - - - if (ch->state == WSYNC) { - - float fr, M; - float Pr[NBPH], Pu, Pv; - int l; - float p, err; - - if(ch->clk<8) return; - ch->clk-=8; - - P=filteredphase(ch); - - ch->Phidx = (ch->Phidx + 1) % (NBPH * D8DWN); - ch->Ph[ch->Phidx] = P; - - /* detect sync */ - - Pu = 0; - M = Pv = Pr[0] = ch->Ph[(ch->Phidx + D8DWN) % (NBPH * D8DWN)] - SW[0]; - for (l = 1; l < NBPH; l++) { - float Pc, Pd; - Pc = ch->Ph[(ch->Phidx + (l + 1) * D8DWN) % (NBPH * D8DWN)] - SW[l]; - /* unwarp */ - Pd = Pc - Pv; - Pv = Pc; - if (Pd > M_PI) { - Pu -= 2 * M_PI; - } else if (Pd < -M_PI) { - Pu += 2 * M_PI; - } - Pr[l] = Pc + Pu; - M += Pr[l]; - } - M /= NBPH; - fr = 0; - for (l = 0; l < NBPH; l++) { - Pr[l] -= M; - fr += Pr[l] * (l - (NBPH - 1) / 2); - } - fr /= 408; - err = 0; - for (l = 0; l < NBPH; l++) { - float e; - e = Pr[l] - (l - (NBPH - 1) / 2) * fr; - err += e * e; - } - - //fprintf(stderr,"err %f %f\n",ch->perr,fr); - if (ch->perr < 4.0 && err > ch->perr) { - float of; - double du; - clock_gettime(CLOCK_REALTIME, &(ch->blk->ts)); - ch->state = GETHEAD; - ch->nbits = 0; - ch->scrambler = 0x4D4B; - viterbi_init(); - ch->df = ch->pfr; - ch->blk->ppm=10500*ch->df/(2*M_PI*ch->Fr)*1e6; - of = 4*(ch->p2err - 4 * ch->perr + 3 * err) / (ch->p2err - 2 * ch->perr + err); - ch->clk = (int)roundf(of); - ch->P1 = filteredphase(ch); - - //fprintf(stderr,"head %f %d %f %f\n",of,ch->clk, err,ch->df); - ch->perr = ch->p2err = 500; - } else { - ch->p2err = ch->perr; - ch->perr = err; - ch->pfr = fr; - } - } else { - int v; - float D, err; - - if (ch->clk <32) return; - ch->clk -=32; - - P=filteredphase(ch); - - D = (P - ch->P1) - ch->df; - if (D > M_PI) - D -= 2 * M_PI; - if (D < -M_PI) - D += 2 * M_PI; - - putgreycode(ch,D); - - ch->P1 = P; - } - - //SndWrite(d); + int i, k; + complex float S; + float P; + float d[4]; + + ch->Inbuff[ch->ink] = E; + ch->ink = (ch->ink + 1) % MBUFLEN; + + ch->clk += 4; + + if (ch->state == WSYNC) { + + float fr, M; + float Pr[NBPH], Pu, Pv; + int l; + float p, err; + + if (ch->clk < 8) + return; + ch->clk -= 8; + + P = filteredphase(ch); + + ch->Phidx = (ch->Phidx + 1) % (NBPH * D8DWN); + ch->Ph[ch->Phidx] = P; + + /* detect sync */ + + Pu = 0; + M = Pv = Pr[0] = + ch->Ph[(ch->Phidx + D8DWN) % (NBPH * D8DWN)] - SW[0]; + for (l = 1; l < NBPH; l++) { + float Pc, Pd; + Pc = ch->Ph[(ch->Phidx + (l + 1) * D8DWN) % + (NBPH * D8DWN)] - SW[l]; + /* unwarp */ + Pd = Pc - Pv; + Pv = Pc; + if (Pd > M_PI) { + Pu -= 2 * M_PI; + } else if (Pd < -M_PI) { + Pu += 2 * M_PI; + } + Pr[l] = Pc + Pu; + M += Pr[l]; + } + M /= NBPH; + fr = 0; + for (l = 0; l < NBPH; l++) { + Pr[l] -= M; + fr += Pr[l] * (l - (NBPH - 1) / 2); + } + fr /= 408; + err = 0; + for (l = 0; l < NBPH; l++) { + float e; + e = Pr[l] - (l - (NBPH - 1) / 2) * fr; + err += e * e; + } + + //fprintf(stderr,"err %f %f\n",ch->perr,fr); + if (ch->perr < 4.0 && err > ch->perr) { + float of; + double du; + clock_gettime(CLOCK_REALTIME, &(ch->blk->ts)); + ch->state = GETHEAD; + ch->nbits = 0; + ch->scrambler = 0x4D4B; + viterbi_init(); + ch->df = ch->pfr; + ch->blk->ppm = + 10500 * ch->df / (2 * M_PI * ch->Fr) * 1e6; + of = 4 * (ch->p2err - 4 * ch->perr + + 3 * err) / (ch->p2err - 2 * ch->perr + err); + ch->clk = (int)roundf(of); + ch->P1 = filteredphase(ch); + + //fprintf(stderr,"head %f %d %f %f\n",of,ch->clk, err,ch->df); + ch->perr = ch->p2err = 500; + } else { + ch->p2err = ch->perr; + ch->perr = err; + ch->pfr = fr; + } + } else { + int v; + float D, err; + + if (ch->clk < 32) + return; + ch->clk -= 32; + + P = filteredphase(ch); + + D = (P - ch->P1) - ch->df; + if (D > M_PI) + D -= 2 * M_PI; + if (D < -M_PI) + D += 2 * M_PI; + + putgreycode(ch, D); + + ch->P1 = P; + } + + //SndWrite(d); } -extern pthread_barrier_t Bar1,Bar2; -extern complex float Cbuff[RTLINBUFSZ/2]; +extern pthread_barrier_t Bar1, Bar2; +extern complex float Cbuff[RTLINBUFSZ / 2]; void *rcv_thread(void *arg) { - thread_param_t *param=(thread_param_t*)arg; - channel_t ch; - - ch.chn=param->chn; - ch.Fr=param->Fr; - - int clk=0; - int nf=0; - int no,swf; - float Fo; - complex float D=0; - complex float wf[SDRINRATE/STEPRATE]; - - initD8psk(&ch); - initVdlm2(&ch); - - /* pre compute local Osc */ - swf=abs(SDRINRATE/ch.Fr); - Fo=(float)ch.Fr/(float)(SDRINRATE) * 2.0 * M_PI; - for(no=0;no< swf;no++) { - wf[no]=cexpf(-no*Fo*I); - } - no=0; - - pthread_barrier_wait(&Bar1); - do { - int i; - - pthread_barrier_wait(&Bar2); - - for (i = 0; i < RTLINBUFSZ/2;i++) { - - D+=Cbuff[i]*wf[no]; nf++; - - no=(no+1)%swf; - - /* rought downsample */ - clk+=21; - if(clk>=SDRCLK) { - clk%=SDRCLK; - D/=nf; - demodD8psk(&ch,D); - D=0;nf=0; - } - } - pthread_barrier_wait(&Bar1); - } while(1); + thread_param_t *param = (thread_param_t *) arg; + channel_t ch; + + ch.chn = param->chn; + ch.Fr = param->Fr; + + int clk = 0; + int nf = 0; + int no, swf; + float Fo; + complex float D = 0; + complex float wf[SDRINRATE / STEPRATE]; + + initD8psk(&ch); + initVdlm2(&ch); + + /* pre compute local Osc */ + swf = abs(SDRINRATE / ch.Fr); + Fo = (float)ch.Fr / (float)(SDRINRATE) * 2.0 * M_PI; + for (no = 0; no < swf; no++) { + wf[no] = cexpf(-no * Fo * I); + } + no = 0; + + pthread_barrier_wait(&Bar1); + do { + int i; + + pthread_barrier_wait(&Bar2); + + for (i = 0; i < RTLINBUFSZ / 2; i++) { + + D += Cbuff[i] * wf[no]; + nf++; + + no = (no + 1) % swf; + + /* rought downsample */ + clk += 21; + if (clk >= SDRCLK) { + clk %= SDRCLK; + D /= nf; + demodD8psk(&ch, D); + D = 0; + nf = 0; + } + } + pthread_barrier_wait(&Bar1); + } while (1); } diff --git a/d8psk.h b/d8psk.h index 87a8b72..3dcb6bc 100644 --- a/d8psk.h +++ b/d8psk.h @@ -18,136 +18,232 @@ */ static const float SW[NBPH] = { - 2 * M_PI / 8, 3 * M_PI / 8, 10 * M_PI / 8, 15 * M_PI / 8, - 8 * M_PI / 8, 9 * M_PI / 8, 12 * M_PI / 8, 9 * M_PI / 8, - 2 * M_PI / 8, 5 * M_PI / 8, 4 * M_PI / 8, 9 * M_PI / 8, - 4 * M_PI / 8, M_PI / 8, -4 * M_PI / 8, -5 * M_PI / 8, - 2 * M_PI / 8 + 2 * M_PI / 8, 3 * M_PI / 8, 10 * M_PI / 8, 15 * M_PI / 8, + 8 * M_PI / 8, 9 * M_PI / 8, 12 * M_PI / 8, 9 * M_PI / 8, + 2 * M_PI / 8, 5 * M_PI / 8, 4 * M_PI / 8, 9 * M_PI / 8, + 4 * M_PI / 8, M_PI / 8, -4 * M_PI / 8, -5 * M_PI / 8, + 2 * M_PI / 8 }; static const float mflt[MFLTLEN] = { - -0.0063474526, -0.0147744088, -0.0251715417, -0.0372531112, - -0.0505438764, -0.0643762574, -0.0778990609, -0.0900984580, - -0.0998311862, -0.1058691815, -0.1069540690, -0.1018592183, - -0.0894564364, -0.0687838818, -0.0391114778, +0.0000000000, - +0.0486498533, +0.1065617468, +0.1730641128, +0.2470886715, - +0.3271881497, +0.4115732615, +0.4981679546, +0.5846808858, - +0.6686901328, +0.7477373336, +0.8194268281, +0.8815249907, - +0.9320548266, +0.9693810568, +0.9922813460, +1.0000000000, - +0.9922813460, +0.9693810568, +0.9320548266, +0.8815249907, - +0.8194268281, +0.7477373336, +0.6686901328, +0.5846808858, - +0.4981679546, +0.4115732615, +0.3271881497, +0.2470886715, - +0.1730641128, +0.1065617468, +0.0486498533, +0.0000000000, - -0.0391114778, -0.0687838818, -0.0894564364, -0.1018592183, - -0.1069540690, -0.1058691815, -0.0998311862, -0.0900984580, - -0.0778990609, -0.0643762574, -0.0505438764, -0.0372531112, - -0.0251715417, -0.0147744088, -0.0063474526 - }; + -0.0063474526, -0.0147744088, -0.0251715417, -0.0372531112, + -0.0505438764, -0.0643762574, -0.0778990609, -0.0900984580, + -0.0998311862, -0.1058691815, -0.1069540690, -0.1018592183, + -0.0894564364, -0.0687838818, -0.0391114778, +0.0000000000, + +0.0486498533, +0.1065617468, +0.1730641128, +0.2470886715, + +0.3271881497, +0.4115732615, +0.4981679546, +0.5846808858, + +0.6686901328, +0.7477373336, +0.8194268281, +0.8815249907, + +0.9320548266, +0.9693810568, +0.9922813460, +1.0000000000, + +0.9922813460, +0.9693810568, +0.9320548266, +0.8815249907, + +0.8194268281, +0.7477373336, +0.6686901328, +0.5846808858, + +0.4981679546, +0.4115732615, +0.3271881497, +0.2470886715, + +0.1730641128, +0.1065617468, +0.0486498533, +0.0000000000, + -0.0391114778, -0.0687838818, -0.0894564364, -0.1018592183, + -0.1069540690, -0.1058691815, -0.0998311862, -0.0900984580, + -0.0778990609, -0.0643762574, -0.0505438764, -0.0372531112, + -0.0251715417, -0.0147744088, -0.0063474526 +}; static const float Grey1[257] = { - 0.500000, - 0.547114, 0.593379, 0.638007, 0.680323, 0.719801, 0.756080, 0.788964, 0.818405, - 0.844475, 0.867341, 0.887234, 0.904421, 0.919182, 0.931801, 0.942548, 0.951672, - 0.959400, 0.965936, 0.971456, 0.976114, 0.980044, 0.983359, 0.986153, 0.988508, - 0.990492, 0.992162, 0.993565, 0.994741, 0.995724, 0.996542, 0.997220, 0.997777, - 0.998233, 0.998604, 0.998902, 0.999140, 0.999330, 0.999479, 0.999596, 0.999687, - 0.999758, 0.999813, 0.999856, 0.999888, 0.999913, 0.999933, 0.999947, 0.999959, - 0.999968, 0.999975, 0.999980, 0.999984, 0.999987, 0.999990, 0.999992, 0.999994, - 0.999995, 0.999996, 0.999996, 0.999997, 0.999997, 0.999998, 0.999998, 0.999998, - 0.999998, 0.999998, 0.999997, 0.999997, 0.999996, 0.999996, 0.999995, 0.999994, - 0.999992, 0.999990, 0.999987, 0.999984, 0.999980, 0.999975, 0.999968, 0.999959, - 0.999947, 0.999933, 0.999913, 0.999888, 0.999856, 0.999813, 0.999758, 0.999687, - 0.999596, 0.999479, 0.999330, 0.999140, 0.998902, 0.998604, 0.998233, 0.997777, - 0.997220, 0.996542, 0.995724, 0.994741, 0.993565, 0.992162, 0.990492, 0.988508, - 0.986153, 0.983359, 0.980044, 0.976114, 0.971456, 0.965936, 0.959400, 0.951672, - 0.942548, 0.931801, 0.919182, 0.904421, 0.887234, 0.867341, 0.844475, 0.818405, - 0.788964, 0.756080, 0.719801, 0.680323, 0.638007, 0.593379, 0.547114, 0.500000, - 0.452886, 0.406621, 0.361993, 0.319677, 0.280199, 0.243920, 0.211036, 0.181595, - 0.155525, 0.132659, 0.112766, 0.095579, 0.080818, 0.068199, 0.057452, 0.048328, - 0.040600, 0.034064, 0.028544, 0.023886, 0.019956, 0.016641, 0.013847, 0.011492, - 0.009508, 0.007838, 0.006435, 0.005259, 0.004276, 0.003458, 0.002780, 0.002223, - 0.001767, 0.001396, 0.001098, 0.000860, 0.000670, 0.000521, 0.000404, 0.000313, - 0.000242, 0.000187, 0.000144, 0.000112, 0.000087, 0.000067, 0.000053, 0.000041, - 0.000032, 0.000025, 0.000020, 0.000016, 0.000013, 0.000010, 0.000008, 0.000006, - 0.000005, 0.000004, 0.000004, 0.000003, 0.000003, 0.000002, 0.000002, 0.000002, - 0.000002, 0.000002, 0.000003, 0.000003, 0.000004, 0.000004, 0.000005, 0.000006, - 0.000008, 0.000010, 0.000013, 0.000016, 0.000020, 0.000025, 0.000032, 0.000041, - 0.000053, 0.000067, 0.000087, 0.000112, 0.000144, 0.000187, 0.000242, 0.000313, - 0.000404, 0.000521, 0.000670, 0.000860, 0.001098, 0.001396, 0.001767, 0.002223, - 0.002780, 0.003458, 0.004276, 0.005259, 0.006435, 0.007838, 0.009508, 0.011492, - 0.013847, 0.016641, 0.019956, 0.023886, 0.028544, 0.034064, 0.040600, 0.048328, - 0.057452, 0.068199, 0.080818, 0.095579, 0.112766, 0.132659, 0.155525, 0.181595, - 0.211036, 0.243920, 0.280199, 0.319677, 0.361993, 0.406621, 0.452886, 0.500000 + 0.500000, + 0.547114, 0.593379, 0.638007, 0.680323, 0.719801, 0.756080, 0.788964, + 0.818405, + 0.844475, 0.867341, 0.887234, 0.904421, 0.919182, 0.931801, 0.942548, + 0.951672, + 0.959400, 0.965936, 0.971456, 0.976114, 0.980044, 0.983359, 0.986153, + 0.988508, + 0.990492, 0.992162, 0.993565, 0.994741, 0.995724, 0.996542, 0.997220, + 0.997777, + 0.998233, 0.998604, 0.998902, 0.999140, 0.999330, 0.999479, 0.999596, + 0.999687, + 0.999758, 0.999813, 0.999856, 0.999888, 0.999913, 0.999933, 0.999947, + 0.999959, + 0.999968, 0.999975, 0.999980, 0.999984, 0.999987, 0.999990, 0.999992, + 0.999994, + 0.999995, 0.999996, 0.999996, 0.999997, 0.999997, 0.999998, 0.999998, + 0.999998, + 0.999998, 0.999998, 0.999997, 0.999997, 0.999996, 0.999996, 0.999995, + 0.999994, + 0.999992, 0.999990, 0.999987, 0.999984, 0.999980, 0.999975, 0.999968, + 0.999959, + 0.999947, 0.999933, 0.999913, 0.999888, 0.999856, 0.999813, 0.999758, + 0.999687, + 0.999596, 0.999479, 0.999330, 0.999140, 0.998902, 0.998604, 0.998233, + 0.997777, + 0.997220, 0.996542, 0.995724, 0.994741, 0.993565, 0.992162, 0.990492, + 0.988508, + 0.986153, 0.983359, 0.980044, 0.976114, 0.971456, 0.965936, 0.959400, + 0.951672, + 0.942548, 0.931801, 0.919182, 0.904421, 0.887234, 0.867341, 0.844475, + 0.818405, + 0.788964, 0.756080, 0.719801, 0.680323, 0.638007, 0.593379, 0.547114, + 0.500000, + 0.452886, 0.406621, 0.361993, 0.319677, 0.280199, 0.243920, 0.211036, + 0.181595, + 0.155525, 0.132659, 0.112766, 0.095579, 0.080818, 0.068199, 0.057452, + 0.048328, + 0.040600, 0.034064, 0.028544, 0.023886, 0.019956, 0.016641, 0.013847, + 0.011492, + 0.009508, 0.007838, 0.006435, 0.005259, 0.004276, 0.003458, 0.002780, + 0.002223, + 0.001767, 0.001396, 0.001098, 0.000860, 0.000670, 0.000521, 0.000404, + 0.000313, + 0.000242, 0.000187, 0.000144, 0.000112, 0.000087, 0.000067, 0.000053, + 0.000041, + 0.000032, 0.000025, 0.000020, 0.000016, 0.000013, 0.000010, 0.000008, + 0.000006, + 0.000005, 0.000004, 0.000004, 0.000003, 0.000003, 0.000002, 0.000002, + 0.000002, + 0.000002, 0.000002, 0.000003, 0.000003, 0.000004, 0.000004, 0.000005, + 0.000006, + 0.000008, 0.000010, 0.000013, 0.000016, 0.000020, 0.000025, 0.000032, + 0.000041, + 0.000053, 0.000067, 0.000087, 0.000112, 0.000144, 0.000187, 0.000242, + 0.000313, + 0.000404, 0.000521, 0.000670, 0.000860, 0.001098, 0.001396, 0.001767, + 0.002223, + 0.002780, 0.003458, 0.004276, 0.005259, 0.006435, 0.007838, 0.009508, + 0.011492, + 0.013847, 0.016641, 0.019956, 0.023886, 0.028544, 0.034064, 0.040600, + 0.048328, + 0.057452, 0.068199, 0.080818, 0.095579, 0.112766, 0.132659, 0.155525, + 0.181595, + 0.211036, 0.243920, 0.280199, 0.319677, 0.361993, 0.406621, 0.452886, + 0.500000 }; static const float Grey2[257] = { - 0.999998, - 0.999998, 0.999998, 0.999997, 0.999997, 0.999996, 0.999996, 0.999995, 0.999994, - 0.999992, 0.999990, 0.999987, 0.999984, 0.999980, 0.999975, 0.999968, 0.999959, - 0.999947, 0.999933, 0.999913, 0.999888, 0.999856, 0.999813, 0.999758, 0.999687, - 0.999596, 0.999479, 0.999330, 0.999140, 0.998902, 0.998604, 0.998233, 0.997777, - 0.997220, 0.996542, 0.995724, 0.994741, 0.993565, 0.992162, 0.990492, 0.988508, - 0.986153, 0.983359, 0.980044, 0.976114, 0.971456, 0.965936, 0.959400, 0.951672, - 0.942548, 0.931801, 0.919182, 0.904421, 0.887234, 0.867341, 0.844475, 0.818405, - 0.788964, 0.756080, 0.719801, 0.680323, 0.638007, 0.593379, 0.547114, 0.500000, - 0.452886, 0.406621, 0.361993, 0.319677, 0.280199, 0.243920, 0.211036, 0.181595, - 0.155525, 0.132659, 0.112766, 0.095579, 0.080818, 0.068199, 0.057452, 0.048328, - 0.040600, 0.034064, 0.028544, 0.023886, 0.019956, 0.016641, 0.013847, 0.011492, - 0.009508, 0.007838, 0.006435, 0.005259, 0.004276, 0.003458, 0.002780, 0.002223, - 0.001767, 0.001396, 0.001098, 0.000860, 0.000670, 0.000521, 0.000404, 0.000313, - 0.000242, 0.000187, 0.000144, 0.000112, 0.000087, 0.000067, 0.000053, 0.000041, - 0.000032, 0.000025, 0.000020, 0.000016, 0.000013, 0.000010, 0.000008, 0.000006, - 0.000005, 0.000004, 0.000004, 0.000003, 0.000003, 0.000002, 0.000002, 0.000002, - 0.000002, 0.000002, 0.000003, 0.000003, 0.000004, 0.000004, 0.000005, 0.000006, - 0.000008, 0.000010, 0.000013, 0.000016, 0.000020, 0.000025, 0.000032, 0.000041, - 0.000053, 0.000067, 0.000087, 0.000112, 0.000144, 0.000187, 0.000242, 0.000313, - 0.000404, 0.000521, 0.000670, 0.000860, 0.001098, 0.001396, 0.001767, 0.002223, - 0.002780, 0.003458, 0.004276, 0.005259, 0.006435, 0.007838, 0.009508, 0.011492, - 0.013847, 0.016641, 0.019956, 0.023886, 0.028544, 0.034064, 0.040600, 0.048328, - 0.057452, 0.068199, 0.080818, 0.095579, 0.112766, 0.132659, 0.155525, 0.181595, - 0.211036, 0.243920, 0.280199, 0.319677, 0.361993, 0.406621, 0.452886, 0.500000, - 0.547114, 0.593379, 0.638007, 0.680323, 0.719801, 0.756080, 0.788964, 0.818405, - 0.844475, 0.867341, 0.887234, 0.904421, 0.919182, 0.931801, 0.942548, 0.951672, - 0.959400, 0.965936, 0.971456, 0.976114, 0.980044, 0.983359, 0.986153, 0.988508, - 0.990492, 0.992162, 0.993565, 0.994741, 0.995724, 0.996542, 0.997220, 0.997777, - 0.998233, 0.998604, 0.998902, 0.999140, 0.999330, 0.999479, 0.999596, 0.999687, - 0.999758, 0.999813, 0.999856, 0.999888, 0.999913, 0.999933, 0.999947, 0.999959, - 0.999968, 0.999975, 0.999980, 0.999984, 0.999987, 0.999990, 0.999992, 0.999994, - 0.999995, 0.999996, 0.999996, 0.999997, 0.999997, 0.999998, 0.999998, 0.999998 + 0.999998, + 0.999998, 0.999998, 0.999997, 0.999997, 0.999996, 0.999996, 0.999995, + 0.999994, + 0.999992, 0.999990, 0.999987, 0.999984, 0.999980, 0.999975, 0.999968, + 0.999959, + 0.999947, 0.999933, 0.999913, 0.999888, 0.999856, 0.999813, 0.999758, + 0.999687, + 0.999596, 0.999479, 0.999330, 0.999140, 0.998902, 0.998604, 0.998233, + 0.997777, + 0.997220, 0.996542, 0.995724, 0.994741, 0.993565, 0.992162, 0.990492, + 0.988508, + 0.986153, 0.983359, 0.980044, 0.976114, 0.971456, 0.965936, 0.959400, + 0.951672, + 0.942548, 0.931801, 0.919182, 0.904421, 0.887234, 0.867341, 0.844475, + 0.818405, + 0.788964, 0.756080, 0.719801, 0.680323, 0.638007, 0.593379, 0.547114, + 0.500000, + 0.452886, 0.406621, 0.361993, 0.319677, 0.280199, 0.243920, 0.211036, + 0.181595, + 0.155525, 0.132659, 0.112766, 0.095579, 0.080818, 0.068199, 0.057452, + 0.048328, + 0.040600, 0.034064, 0.028544, 0.023886, 0.019956, 0.016641, 0.013847, + 0.011492, + 0.009508, 0.007838, 0.006435, 0.005259, 0.004276, 0.003458, 0.002780, + 0.002223, + 0.001767, 0.001396, 0.001098, 0.000860, 0.000670, 0.000521, 0.000404, + 0.000313, + 0.000242, 0.000187, 0.000144, 0.000112, 0.000087, 0.000067, 0.000053, + 0.000041, + 0.000032, 0.000025, 0.000020, 0.000016, 0.000013, 0.000010, 0.000008, + 0.000006, + 0.000005, 0.000004, 0.000004, 0.000003, 0.000003, 0.000002, 0.000002, + 0.000002, + 0.000002, 0.000002, 0.000003, 0.000003, 0.000004, 0.000004, 0.000005, + 0.000006, + 0.000008, 0.000010, 0.000013, 0.000016, 0.000020, 0.000025, 0.000032, + 0.000041, + 0.000053, 0.000067, 0.000087, 0.000112, 0.000144, 0.000187, 0.000242, + 0.000313, + 0.000404, 0.000521, 0.000670, 0.000860, 0.001098, 0.001396, 0.001767, + 0.002223, + 0.002780, 0.003458, 0.004276, 0.005259, 0.006435, 0.007838, 0.009508, + 0.011492, + 0.013847, 0.016641, 0.019956, 0.023886, 0.028544, 0.034064, 0.040600, + 0.048328, + 0.057452, 0.068199, 0.080818, 0.095579, 0.112766, 0.132659, 0.155525, + 0.181595, + 0.211036, 0.243920, 0.280199, 0.319677, 0.361993, 0.406621, 0.452886, + 0.500000, + 0.547114, 0.593379, 0.638007, 0.680323, 0.719801, 0.756080, 0.788964, + 0.818405, + 0.844475, 0.867341, 0.887234, 0.904421, 0.919182, 0.931801, 0.942548, + 0.951672, + 0.959400, 0.965936, 0.971456, 0.976114, 0.980044, 0.983359, 0.986153, + 0.988508, + 0.990492, 0.992162, 0.993565, 0.994741, 0.995724, 0.996542, 0.997220, + 0.997777, + 0.998233, 0.998604, 0.998902, 0.999140, 0.999330, 0.999479, 0.999596, + 0.999687, + 0.999758, 0.999813, 0.999856, 0.999888, 0.999913, 0.999933, 0.999947, + 0.999959, + 0.999968, 0.999975, 0.999980, 0.999984, 0.999987, 0.999990, 0.999992, + 0.999994, + 0.999995, 0.999996, 0.999996, 0.999997, 0.999997, 0.999998, 0.999998, + 0.999998 }; static const float Grey3[257] = { - 0.004445, - 0.004547, 0.004854, 0.005374, 0.006118, 0.007105, 0.008359, 0.009911, 0.011804, - 0.014088, 0.016828, 0.020100, 0.023997, 0.028631, 0.034132, 0.040652, 0.048369, - 0.057484, 0.068224, 0.080838, 0.095595, 0.112778, 0.132668, 0.155533, 0.181602, - 0.211040, 0.243923, 0.280202, 0.319679, 0.361994, 0.406622, 0.452887, 0.500000, - 0.547113, 0.593378, 0.638006, 0.680321, 0.719798, 0.756077, 0.788960, 0.818398, - 0.844467, 0.867332, 0.887222, 0.904405, 0.919162, 0.931776, 0.942516, 0.951631, - 0.959348, 0.965868, 0.971369, 0.976003, 0.979900, 0.983172, 0.985912, 0.988196, - 0.990089, 0.991641, 0.992895, 0.993882, 0.994626, 0.995146, 0.995453, 0.995555, - 0.995453, 0.995146, 0.994626, 0.993882, 0.992895, 0.991641, 0.990089, 0.988196, - 0.985912, 0.983172, 0.979900, 0.976003, 0.971369, 0.965868, 0.959348, 0.951631, - 0.942516, 0.931776, 0.919162, 0.904405, 0.887222, 0.867332, 0.844467, 0.818398, - 0.788960, 0.756077, 0.719798, 0.680321, 0.638006, 0.593378, 0.547113, 0.500000, - 0.452887, 0.406622, 0.361994, 0.319679, 0.280202, 0.243923, 0.211040, 0.181602, - 0.155533, 0.132668, 0.112778, 0.095595, 0.080838, 0.068224, 0.057484, 0.048369, - 0.040652, 0.034132, 0.028631, 0.023997, 0.020100, 0.016828, 0.014088, 0.011804, - 0.009911, 0.008359, 0.007105, 0.006118, 0.005374, 0.004854, 0.004547, 0.004445, - 0.004547, 0.004854, 0.005374, 0.006118, 0.007105, 0.008359, 0.009911, 0.011804, - 0.014088, 0.016828, 0.020100, 0.023997, 0.028631, 0.034132, 0.040652, 0.048369, - 0.057484, 0.068224, 0.080838, 0.095595, 0.112778, 0.132668, 0.155533, 0.181602, - 0.211040, 0.243923, 0.280202, 0.319679, 0.361994, 0.406622, 0.452887, 0.500000, - 0.547113, 0.593378, 0.638006, 0.680321, 0.719798, 0.756077, 0.788960, 0.818398, - 0.844467, 0.867332, 0.887222, 0.904405, 0.919162, 0.931776, 0.942516, 0.951631, - 0.959348, 0.965868, 0.971369, 0.976003, 0.979900, 0.983172, 0.985912, 0.988196, - 0.990089, 0.991641, 0.992895, 0.993882, 0.994626, 0.995146, 0.995453, 0.995555, - 0.995453, 0.995146, 0.994626, 0.993882, 0.992895, 0.991641, 0.990089, 0.988196, - 0.985912, 0.983172, 0.979900, 0.976003, 0.971369, 0.965868, 0.959348, 0.951631, - 0.942516, 0.931776, 0.919162, 0.904405, 0.887222, 0.867332, 0.844467, 0.818398, - 0.788960, 0.756077, 0.719798, 0.680321, 0.638006, 0.593378, 0.547113, 0.500000, - 0.452887, 0.406622, 0.361994, 0.319679, 0.280202, 0.243923, 0.211040, 0.181602, - 0.155533, 0.132668, 0.112778, 0.095595, 0.080838, 0.068224, 0.057484, 0.048369, - 0.040652, 0.034132, 0.028631, 0.023997, 0.020100, 0.016828, 0.014088, 0.011804, - 0.009911, 0.008359, 0.007105, 0.006118, 0.005374, 0.004854, 0.004547, 0.004445 + 0.004445, + 0.004547, 0.004854, 0.005374, 0.006118, 0.007105, 0.008359, 0.009911, + 0.011804, + 0.014088, 0.016828, 0.020100, 0.023997, 0.028631, 0.034132, 0.040652, + 0.048369, + 0.057484, 0.068224, 0.080838, 0.095595, 0.112778, 0.132668, 0.155533, + 0.181602, + 0.211040, 0.243923, 0.280202, 0.319679, 0.361994, 0.406622, 0.452887, + 0.500000, + 0.547113, 0.593378, 0.638006, 0.680321, 0.719798, 0.756077, 0.788960, + 0.818398, + 0.844467, 0.867332, 0.887222, 0.904405, 0.919162, 0.931776, 0.942516, + 0.951631, + 0.959348, 0.965868, 0.971369, 0.976003, 0.979900, 0.983172, 0.985912, + 0.988196, + 0.990089, 0.991641, 0.992895, 0.993882, 0.994626, 0.995146, 0.995453, + 0.995555, + 0.995453, 0.995146, 0.994626, 0.993882, 0.992895, 0.991641, 0.990089, + 0.988196, + 0.985912, 0.983172, 0.979900, 0.976003, 0.971369, 0.965868, 0.959348, + 0.951631, + 0.942516, 0.931776, 0.919162, 0.904405, 0.887222, 0.867332, 0.844467, + 0.818398, + 0.788960, 0.756077, 0.719798, 0.680321, 0.638006, 0.593378, 0.547113, + 0.500000, + 0.452887, 0.406622, 0.361994, 0.319679, 0.280202, 0.243923, 0.211040, + 0.181602, + 0.155533, 0.132668, 0.112778, 0.095595, 0.080838, 0.068224, 0.057484, + 0.048369, + 0.040652, 0.034132, 0.028631, 0.023997, 0.020100, 0.016828, 0.014088, + 0.011804, + 0.009911, 0.008359, 0.007105, 0.006118, 0.005374, 0.004854, 0.004547, + 0.004445, + 0.004547, 0.004854, 0.005374, 0.006118, 0.007105, 0.008359, 0.009911, + 0.011804, + 0.014088, 0.016828, 0.020100, 0.023997, 0.028631, 0.034132, 0.040652, + 0.048369, + 0.057484, 0.068224, 0.080838, 0.095595, 0.112778, 0.132668, 0.155533, + 0.181602, + 0.211040, 0.243923, 0.280202, 0.319679, 0.361994, 0.406622, 0.452887, + 0.500000, + 0.547113, 0.593378, 0.638006, 0.680321, 0.719798, 0.756077, 0.788960, + 0.818398, + 0.844467, 0.867332, 0.887222, 0.904405, 0.919162, 0.931776, 0.942516, + 0.951631, + 0.959348, 0.965868, 0.971369, 0.976003, 0.979900, 0.983172, 0.985912, + 0.988196, + 0.990089, 0.991641, 0.992895, 0.993882, 0.994626, 0.995146, 0.995453, + 0.995555, + 0.995453, 0.995146, 0.994626, 0.993882, 0.992895, 0.991641, 0.990089, + 0.988196, + 0.985912, 0.983172, 0.979900, 0.976003, 0.971369, 0.965868, 0.959348, + 0.951631, + 0.942516, 0.931776, 0.919162, 0.904405, 0.887222, 0.867332, 0.844467, + 0.818398, + 0.788960, 0.756077, 0.719798, 0.680321, 0.638006, 0.593378, 0.547113, + 0.500000, + 0.452887, 0.406622, 0.361994, 0.319679, 0.280202, 0.243923, 0.211040, + 0.181602, + 0.155533, 0.132668, 0.112778, 0.095595, 0.080838, 0.068224, 0.057484, + 0.048369, + 0.040652, 0.034132, 0.028631, 0.023997, 0.020100, 0.016828, 0.014088, + 0.011804, + 0.009911, 0.008359, 0.007105, 0.006118, 0.005374, 0.004854, 0.004547, + 0.004445 }; diff --git a/ggrey.c b/ggrey.c index 7f34777..dd35e21 100644 --- a/ggrey.c +++ b/ggrey.c @@ -21,82 +21,83 @@ double BESSI0(double X) { - double Y, P1, P2, P3, P4, P5, P6, P7, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, AX, BX; - P1 = 1.0; - P2 = 3.5156229; - P3 = 3.0899424; - P4 = 1.2067492; - P5 = 0.2659732; - P6 = 0.360768e-1; - P7 = 0.45813e-2; - Q1 = 0.39894228; - Q2 = 0.1328592e-1; - Q3 = 0.225319e-2; - Q4 = -0.157565e-2; - Q5 = 0.916281e-2; - Q6 = -0.2057706e-1; - Q7 = 0.2635537e-1; - Q8 = -0.1647633e-1; - Q9 = 0.392377e-2; - if (fabs(X) < 3.75) { - Y = (X / 3.75) * (X / 3.75); - return (P1 + Y * (P2 + Y * (P3 + Y * (P4 + Y * (P5 + Y * (P6 + Y * P7)))))); - } else { - AX = fabs(X); - Y = 3.75 / AX; - BX = exp(AX) / sqrt(AX); - AX = Q1 + Y * (Q2 + Y * (Q3 + Y * (Q4 + Y * (Q5 + Y * (Q6 + Y * (Q7 + Y * (Q8 + Y * Q9))))))); - return (AX * BX); - } + double Y, P1, P2, P3, P4, P5, P6, P7, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, + Q9, AX, BX; + P1 = 1.0; + P2 = 3.5156229; + P3 = 3.0899424; + P4 = 1.2067492; + P5 = 0.2659732; + P6 = 0.360768e-1; + P7 = 0.45813e-2; + Q1 = 0.39894228; + Q2 = 0.1328592e-1; + Q3 = 0.225319e-2; + Q4 = -0.157565e-2; + Q5 = 0.916281e-2; + Q6 = -0.2057706e-1; + Q7 = 0.2635537e-1; + Q8 = -0.1647633e-1; + Q9 = 0.392377e-2; + if (fabs(X) < 3.75) { + Y = (X / 3.75) * (X / 3.75); + return (P1 + Y * (P2 + Y * (P3 + Y * (P4 + Y * (P5 + Y * (P6 + Y * P7)))))); + } else { + AX = fabs(X); + Y = 3.75 / AX; + BX = exp(AX) / sqrt(AX); + AX = Q1 + Y * (Q2 + Y * (Q3 + Y * (Q4 + Y * (Q5 + Y * (Q6 + Y * (Q7 + Y * (Q8 + Y * Q9))))))); + return (AX * BX); + } } double vonmises(double x, double k) { - return exp(k * cos(x)) / (2 * M_PI * BESSI0(k)); + return exp(k * cos(x)) / (2 * M_PI * BESSI0(k)); } int main() { - int i; - const int K = 10; + int i; + const int K = 10; - for (i = -128; i <= 128; i++) { - double pb, p1, pt; + for (i = -128; i <= 128; i++) { + double pb, p1, pt; - p1 = pt = 0; + p1 = pt = 0; - pb = vonmises(M_PI / 8 - i * M_PI / 128, K); - pt += pb; + pb = vonmises(M_PI / 8 - i * M_PI / 128, K); + pt += pb; - pb = vonmises(3 * M_PI / 8 - i * M_PI / 128, K); - pt += pb; + pb = vonmises(3 * M_PI / 8 - i * M_PI / 128, K); + pt += pb; - pb = vonmises(5 * M_PI / 8 - i * M_PI / 128, K); - pt += pb; + pb = vonmises(5 * M_PI / 8 - i * M_PI / 128, K); + pt += pb; - pb = vonmises(7 * M_PI / 8 - i * M_PI / 128, K); - pt += pb; + pb = vonmises(7 * M_PI / 8 - i * M_PI / 128, K); + pt += pb; - pb = vonmises(-M_PI / 8 - i * M_PI / 128, K); - pt += pb; - p1 += pb; + pb = vonmises(-M_PI / 8 - i * M_PI / 128, K); + pt += pb; + p1 += pb; - pb = vonmises(-3 * M_PI / 8 - i * M_PI / 128, K); - pt += pb; - p1 += pb; + pb = vonmises(-3 * M_PI / 8 - i * M_PI / 128, K); + pt += pb; + p1 += pb; - pb = vonmises(-5 * M_PI / 8 - i * M_PI / 128, K); - pt += pb; - p1 += pb; + pb = vonmises(-5 * M_PI / 8 - i * M_PI / 128, K); + pt += pb; + p1 += pb; - pb = vonmises(-7 * M_PI / 8 - i * M_PI / 128, K); - pt += pb; - p1 += pb; + pb = vonmises(-7 * M_PI / 8 - i * M_PI / 128, K); + pt += pb; + p1 += pb; - printf("%f,", p1 / pt); - if ((i & 7) == 0) - printf("\n"); - } + printf("%f,", p1 / pt); + if ((i & 7) == 0) + printf("\n"); + } } diff --git a/main.c b/main.c index d044200..80dfa42 100644 --- a/main.c +++ b/main.c @@ -39,107 +39,110 @@ int ppm = 0; int nbch; thread_param_t tparam[MAXNBCHANNELS]; -pthread_barrier_t Bar1,Bar2; +pthread_barrier_t Bar1, Bar2; FILE *logfd; static void usage(void) { - fprintf(stderr, "vdlm2dec V1.0 Copyright (c) 2016 Thierry Leconte \n\n"); - fprintf(stderr, "Usage: vdlm2dec [-v|V] [-l logfile] "); + fprintf(stderr, + "vdlm2dec V1.0 Copyright (c) 2016 Thierry Leconte \n\n"); + fprintf(stderr, "Usage: vdlm2dec [-v|V] [-l logfile] "); #ifdef WITH_RTL - fprintf(stderr, " [-g gain] [-r rtldevicenumber] "); + fprintf(stderr, " [-g gain] [-r rtldevicenumber] "); #endif - fprintf(stderr, " Frequency(Mhz)\n"); + fprintf(stderr, " Frequency(Mhz)\n"); #ifdef WITH_RTL - fprintf(stderr, " -r rtldevicenumber :\tdecode from rtl dongle number rtldevicenumber \n"); - fprintf(stderr, " -g gain :\t\tset rtl preamp gain in tenth of db (ie -g 90 for +9db).\n"); - fprintf(stderr, " -p ppm :\t\tppm frequency correction\n"); + fprintf(stderr, + " -r rtldevicenumber :\tdecode from rtl dongle number rtldevicenumber \n"); + fprintf(stderr, + " -g gain :\t\tset rtl preamp gain in tenth of db (ie -g 90 for +9db).\n"); + fprintf(stderr, " -p ppm :\t\tppm frequency correction\n"); #endif - fprintf(stderr, " -v :\t\t\tverbose\n"); - fprintf(stderr, " -q :\t\t\tquiet\n"); - fprintf(stderr, " -l logfile :\t\toutput log (stderr by default)\n"); - exit(1); + fprintf(stderr, " -v :\t\t\tverbose\n"); + fprintf(stderr, " -q :\t\t\tquiet\n"); + fprintf(stderr, " -l logfile :\t\toutput log (stderr by default)\n"); + exit(1); } static void sighandler(int signum) { - stopVdlm2(); - exit(1); + stopVdlm2(); + exit(1); } int main(int argc, char **argv) { - int c; - int res, n; - struct sigaction sigact; - char *fname; - int rdev; - double freq; - - nbch=0; - logfd = stderr; - - while ((c = getopt(argc, argv, "vqrp:g:l:")) != EOF) { - switch (c) { - case 'v': - verbose = 2; - break; - case 'q': - verbose = 0; - break; - case 'l': - logfd = fopen(optarg, "w+"); - if (logfd == NULL) { - fprintf(stderr, "unable top open %s\n", optarg); - exit(1); - } - break; + int c; + int res, n; + struct sigaction sigact; + char *fname; + int rdev; + double freq; + + nbch = 0; + logfd = stderr; + + while ((c = getopt(argc, argv, "vqrp:g:l:")) != EOF) { + switch (c) { + case 'v': + verbose = 2; + break; + case 'q': + verbose = 0; + break; + case 'l': + logfd = fopen(optarg, "w+"); + if (logfd == NULL) { + fprintf(stderr, "unable top open %s\n", optarg); + exit(1); + } + break; #ifdef WITH_RTL - case 'r': - res = initRtl(argv, optind,tparam); - break; - case 'g': - gain = atoi(optarg); - break; - case 'p': - ppm = atoi(optarg); - break; + case 'r': + res = initRtl(argv, optind, tparam); + break; + case 'g': + gain = atoi(optarg); + break; + case 'p': + ppm = atoi(optarg); + break; #endif - default: - usage(); - return (1); - } - } - - if (res) { - fprintf(stderr, "Unable to init input\n"); - exit(res); - } - - sigact.sa_handler = sighandler; - sigemptyset(&sigact.sa_mask); - sigact.sa_flags = 0; - sigaction(SIGINT, &sigact, NULL); - sigaction(SIGTERM, &sigact, NULL); - sigaction(SIGQUIT, &sigact, NULL); - - pthread_barrier_init(&Bar1,NULL,nbch+1); - pthread_barrier_init(&Bar2,NULL,nbch+1); - - for (n = 0; n < nbch; n++) { - pthread_t th; - pthread_create(&th, NULL, rcv_thread, &(tparam[n])); - } + default: + usage(); + return (1); + } + } + + if (res) { + fprintf(stderr, "Unable to init input\n"); + exit(res); + } + + sigact.sa_handler = sighandler; + sigemptyset(&sigact.sa_mask); + sigact.sa_flags = 0; + sigaction(SIGINT, &sigact, NULL); + sigaction(SIGTERM, &sigact, NULL); + sigaction(SIGQUIT, &sigact, NULL); + + pthread_barrier_init(&Bar1, NULL, nbch + 1); + pthread_barrier_init(&Bar2, NULL, nbch + 1); + + for (n = 0; n < nbch; n++) { + pthread_t th; + pthread_create(&th, NULL, rcv_thread, &(tparam[n])); + } #if DEBUG - initSndWrite(); + initSndWrite(); #endif - runRtlSample(); + runRtlSample(); - stopVdlm2(); + stopVdlm2(); - exit(1); + exit(1); } diff --git a/out.c b/out.c index 3e023f2..2e3a10f 100644 --- a/out.c +++ b/out.c @@ -26,108 +26,117 @@ void dumpdata(unsigned char *p, int len) { - int i,k; - - for (i = 0; i < len; i+=16) { - for (k = 0; k<16 ; k++) - if(i+k=' ' && p[i+k]<='~') - fprintf(logfd, "%c", p[i+k]); - else - fprintf(logfd, "."); - fprintf(logfd, "|\n"); - } + int i, k; + + for (i = 0; i < len; i += 16) { + for (k = 0; k < 16; k++) + if (i + k < len) + fprintf(logfd, "%02hhx ", p[i + k]); + else + fprintf(logfd, " "); + fprintf(logfd, " |"); + for (k = 0; k < 16; k++) + if (i + k < len && p[i + k] >= ' ' && p[i + k] <= '~') + fprintf(logfd, "%c", p[i + k]); + else + fprintf(logfd, "."); + fprintf(logfd, "|\n"); + } } static void outaddr(unsigned char *hdata) { - unsigned int addr = - (reversebits(hdata[0] >> 2, 6) << 21) | (reversebits(hdata[2] >> 1, 7) << 14) | - (reversebits(hdata[2] >> 1, 7) << 7) | (reversebits(hdata[3] >> 1, 7)); + unsigned int addr = + (reversebits(hdata[0] >> 2, 6) << 21) | + (reversebits(hdata[2] >> 1, 7) << 14) | + (reversebits(hdata[2] >> 1, 7) << 7) | + (reversebits(hdata[3] >> 1, 7)); - unsigned int type = addr >>24; - - addr=addr&0xffffff; + unsigned int type = addr >> 24; - if(type==7) - fprintf(logfd, "All ",addr); - else - fprintf(logfd, "%06x ",addr); + addr = addr & 0xffffff; -} + if (type == 7) + fprintf(logfd, "All ", addr); + else + fprintf(logfd, "%06x ", addr); -static const char *Sfrm [4]={"RR", "RNR","REJ","SREJ"}; -static const char *Ufrm [2][32]={ -{"UI", "SIM","0x02","SARM","UP","0x05","0x06","SABM", -"DISC","0x09","0x0a","SARME","0x0c","0x0d","0x0e","SABME", -"SNRM","0x11","0x12","RSET","0x14","0x15","0x16","XID", -"0x18","0x19","0x1a","SNRME","TEST","0x1d","0x1e","0x1f" -}, -{"UI", "RIM","0x02","DM","0x04","0x05","0x06","0x07", -"RD","0x09","0x0a","0x0b","UA","0x0d","0x0e","0x0f", -"0x10","FRMR","0x12","0x13","0x14","0x15","0x16","XID", -"0x18","0x19","0x1a","0x1b","TEST","0x1d","0x1e","0x1f" } + +static const char *Sfrm[4] = { "RR", "RNR", "REJ", "SREJ" }; + +static const char *Ufrm[2][32] = { + {"UI", "SIM", "0x02", "SARM", "UP", "0x05", "0x06", "SABM", + "DISC", "0x09", "0x0a", "SARME", "0x0c", "0x0d", "0x0e", "SABME", + "SNRM", "0x11", "0x12", "RSET", "0x14", "0x15", "0x16", "XID", + "0x18", "0x19", "0x1a", "SNRME", "TEST", "0x1d", "0x1e", "0x1f"}, + {"UI", "RIM", "0x02", "DM", "0x04", "0x05", "0x06", "0x07", + "RD", "0x09", "0x0a", "0x0b", "UA", "0x0d", "0x0e", "0x0f", + "0x10", "FRMR", "0x12", "0x13", "0x14", "0x15", "0x16", "XID", + "0x18", "0x19", "0x1a", "0x1b", "TEST", "0x1d", "0x1e", "0x1f"} }; -static void outlinkctrl(unsigned char lc,int rep) + +static void outlinkctrl(unsigned char lc, int rep) { - fprintf(logfd, "Frame-"); - if(lc&1) { - if(lc&2) { - fprintf(logfd, "U: "); - fprintf(logfd, "%s ", (lc & 0x10) ? "P/F" : "-/-"); - fprintf(logfd, "%s\n", Ufrm[rep][((lc >> 3) & 0x1c) | ((lc >> 2) & 0x3)]); + fprintf(logfd, "Frame-"); + if (lc & 1) { + if (lc & 2) { + fprintf(logfd, "U: "); + fprintf(logfd, "%s ", (lc & 0x10) ? "P/F" : "-/-"); + fprintf(logfd, "%s\n", + Ufrm[rep][((lc >> 3) & 0x1c) | + ((lc >> 2) & 0x3)]); + } else { + fprintf(logfd, "S: "); + fprintf(logfd, "%s ", (lc & 0x10) ? "P/F" : "-/-"); + fprintf(logfd, "Nr:%01d %s\n", (lc >> 5) & 0x7, + Sfrm[(lc >> 2) & 0x3]); + } } else { - fprintf(logfd, "S: "); - fprintf(logfd, "%s ", (lc & 0x10) ? "P/F" : "-/-"); - fprintf(logfd, "Nr:%01d %s\n", (lc >> 5) & 0x7, Sfrm[(lc >> 2) & 0x3]); + fprintf(logfd, "I: "); + fprintf(logfd, "%s ", (lc & 0x10) ? "P/F" : "-/-"); + fprintf(logfd, "Ns:%01d Nr:%01d\n", (lc >> 1) & 0x7, + (lc >> 5) & 0x7); } - } else { - fprintf(logfd, "I: "); - fprintf(logfd, "%s ", (lc & 0x10) ? "P/F" : "-/-"); - fprintf(logfd, "Ns:%01d Nr:%01d\n", (lc >> 1) & 0x7, (lc >> 5) & 0x7); - } } -void out(msgblk_t *blk,unsigned char *hdata,int l) +void out(msgblk_t * blk, unsigned char *hdata, int l) { - int i,d; - int rep = (hdata[5] & 2)>>1; - int gnd = hdata[1] & 2; - - fprintf(logfd, "-----------------------------------------------------------------------\n"); - fprintf(logfd, "#%1d %s ", blk->chn+1,rep ? "Response":"Command"); - fprintf(logfd, "from %s: ",gnd ? "Ground":"Aircraft"); outaddr(&(hdata[5])); - fprintf(logfd, "to: ");outaddr(&(hdata[1])); - fprintf(logfd, "\n"); - - outlinkctrl(hdata[9],rep); - - d = 0; - - if (l == 13) { - d = 1; - } - - if (l >= 16 && hdata[10] == 0xff && hdata[11] == 0xff && hdata[12] == 1) { - outacars(&(hdata[13]), l - 16); - d = 1; - } - if (l >= 14 && hdata[10] == 0x82) { - outxid(&(hdata[11]), l - 14); - d = 1; - } - - if (d == 0) { - fprintf(logfd, "unknown data\n"); - dumpdata(&(hdata[10]), l - 13); - } - fflush(logfd); + int i, d; + int rep = (hdata[5] & 2) >> 1; + int gnd = hdata[1] & 2; + + fprintf(logfd, + "-----------------------------------------------------------------------\n"); + fprintf(logfd, "#%1d %s ", blk->chn + 1, rep ? "Response" : "Command"); + fprintf(logfd, "from %s: ", gnd ? "Ground" : "Aircraft"); + outaddr(&(hdata[5])); + fprintf(logfd, "to: "); + outaddr(&(hdata[1])); + fprintf(logfd, "\n"); + + outlinkctrl(hdata[9], rep); + + d = 0; + + if (l == 13) { + d = 1; + } + + if (l >= 16 && hdata[10] == 0xff && hdata[11] == 0xff && hdata[12] == 1) { + outacars(&(hdata[13]), l - 16); + d = 1; + } + if (l >= 14 && hdata[10] == 0x82) { + outxid(&(hdata[11]), l - 14); + d = 1; + } + + if (d == 0) { + fprintf(logfd, "unknown data\n"); + dumpdata(&(hdata[10]), l - 13); + } + fflush(logfd); } diff --git a/outacars.c b/outacars.c index 90bdf9a..8745fc0 100644 --- a/outacars.c +++ b/outacars.c @@ -31,105 +31,105 @@ extern int verbose; static void printmsg(acarsmsg_t * msg) { - if (msg->mode < 0x5d) { - fprintf(logfd, "Aircraft reg: %s ", msg->reg); - fprintf(logfd, "Flight id: %s", msg->fid); - fprintf(logfd, "\n"); - } - fprintf(logfd, "Mode: %1c ", msg->mode); - fprintf(logfd, "Msg. label: %s\n", msg->label); - fprintf(logfd, "Block id: %c ", msg->bid); - fprintf(logfd, "Ack: %c\n", msg->ack); - fprintf(logfd, "Msg. no: %s\n", msg->no); - fprintf(logfd, "Message :\n%s\n", msg->txt); - if (msg->be == 0x17) - fprintf(logfd, "Block End\n"); + if (msg->mode < 0x5d) { + fprintf(logfd, "Aircraft reg: %s ", msg->reg); + fprintf(logfd, "Flight id: %s", msg->fid); + fprintf(logfd, "\n"); + } + fprintf(logfd, "Mode: %1c ", msg->mode); + fprintf(logfd, "Msg. label: %s\n", msg->label); + fprintf(logfd, "Block id: %c ", msg->bid); + fprintf(logfd, "Ack: %c\n", msg->ack); + fprintf(logfd, "Msg. no: %s\n", msg->no); + fprintf(logfd, "Message :\n%s\n", msg->txt); + if (msg->be == 0x17) + fprintf(logfd, "Block End\n"); } void outacars(unsigned char *txt, int len) { - acarsmsg_t msg; - int i, k, j; - unsigned int crc; - - fprintf(logfd, "ACARS\n"); - - crc = 0; - /* test crc, set le and remove parity */ - for (i = 0; i < len - 1; i++) { - update_crc(crc, txt[i]); - txt[i] &= 0x7f; - } - if (crc) { - fprintf(logfd, "crc error\n"); - } - - /* fill msg struct */ - - k = 0; - msg.mode = txt[k]; - k++; - - for (i = 0, j = 0; i < 7; i++, k++) { - if (txt[k] != '.') { - msg.reg[j] = txt[k]; - j++; - } - } - msg.reg[j] = '\0'; - - /* ACK/NAK */ - msg.ack = txt[k]; - if (msg.ack == 0x15) - msg.ack = '!'; - k++; - - msg.label[0] = txt[k]; - k++; - msg.label[1] = txt[k]; - if (msg.label[1] == 0x7f) - msg.label[1] = 'd'; - k++; - msg.label[2] = '\0'; - - msg.bid = txt[k]; - if (msg.bid == 0) - msg.bid = ' '; - k++; - - /* txt start */ - msg.bs = txt[k]; - k++; - - msg.no[0] = '\0'; - msg.fid[0] = '\0'; - msg.txt[0] = '\0'; - - if (msg.bs != 0x03) { - if (msg.mode <= 'Z' && msg.bid <= '9') { - /* message no */ - for (i = 0; i < 4 && k < len - 4; i++, k++) { - msg.no[i] = txt[k]; - } - msg.no[i] = '\0'; - - /* Flight id */ - for (i = 0; i < 6 && k < len - 4; i++, k++) { - msg.fid[i] = txt[k]; - } - msg.fid[i] = '\0'; - } - - /* Message txt */ - for (i = 0; (k < len - 4); i++, k++) - msg.txt[i] = txt[k]; - msg.txt[i] = 0; - } - msg.be = txt[k]; - - if (verbose) { - printmsg(&msg); - } + acarsmsg_t msg; + int i, k, j; + unsigned int crc; + + fprintf(logfd, "ACARS\n"); + + crc = 0; + /* test crc, set le and remove parity */ + for (i = 0; i < len - 1; i++) { + update_crc(crc, txt[i]); + txt[i] &= 0x7f; + } + if (crc) { + fprintf(logfd, "crc error\n"); + } + + /* fill msg struct */ + + k = 0; + msg.mode = txt[k]; + k++; + + for (i = 0, j = 0; i < 7; i++, k++) { + if (txt[k] != '.') { + msg.reg[j] = txt[k]; + j++; + } + } + msg.reg[j] = '\0'; + + /* ACK/NAK */ + msg.ack = txt[k]; + if (msg.ack == 0x15) + msg.ack = '!'; + k++; + + msg.label[0] = txt[k]; + k++; + msg.label[1] = txt[k]; + if (msg.label[1] == 0x7f) + msg.label[1] = 'd'; + k++; + msg.label[2] = '\0'; + + msg.bid = txt[k]; + if (msg.bid == 0) + msg.bid = ' '; + k++; + + /* txt start */ + msg.bs = txt[k]; + k++; + + msg.no[0] = '\0'; + msg.fid[0] = '\0'; + msg.txt[0] = '\0'; + + if (msg.bs != 0x03) { + if (msg.mode <= 'Z' && msg.bid <= '9') { + /* message no */ + for (i = 0; i < 4 && k < len - 4; i++, k++) { + msg.no[i] = txt[k]; + } + msg.no[i] = '\0'; + + /* Flight id */ + for (i = 0; i < 6 && k < len - 4; i++, k++) { + msg.fid[i] = txt[k]; + } + msg.fid[i] = '\0'; + } + + /* Message txt */ + for (i = 0; (k < len - 4); i++, k++) + msg.txt[i] = txt[k]; + msg.txt[i] = 0; + } + msg.be = txt[k]; + + if (verbose) { + printmsg(&msg); + } } diff --git a/outxid.c b/outxid.c index ee282d0..35e690f 100644 --- a/outxid.c +++ b/outxid.c @@ -29,164 +29,176 @@ extern int verbose; void outpublicgr(unsigned char *p, int len) { - int i; + int i; - i = 0; - do { - short len = p[i + 1]; + i = 0; + do { + short len = p[i + 1]; - switch (p[i]) { - case 0: - break; - case 0x01: - fprintf(logfd, "Parameter set ID\n"); - break; - case 0x02: - fprintf(logfd, "Procedure classes\n"); - break; - case 0x03: - fprintf(logfd, "HDLC options\n"); - break; - case 0x09: - fprintf(logfd, "Timer T1 downlink\n"); - break; - default: - fprintf(logfd, "unknown public id %02x\n", p[i]); - if (verbose > 1) { - dumpdata(&(p[i]), len + 2); - } - break; - } - i += 2 + len; - } while (i < len); + switch (p[i]) { + case 0: + break; + case 0x01: + fprintf(logfd, "Parameter set ID\n"); + break; + case 0x02: + fprintf(logfd, "Procedure classes\n"); + break; + case 0x03: + fprintf(logfd, "HDLC options\n"); + break; + case 0x09: + fprintf(logfd, "Timer T1 downlink\n"); + break; + default: + fprintf(logfd, "unknown public id %02x\n", p[i]); + if (verbose > 1) { + dumpdata(&(p[i]), len + 2); + } + break; + } + i += 2 + len; + } while (i < len); } void outprivategr(unsigned char *p, int len) { - int i; + int i; - i = 0; - do { - short len = p[i + 1]; + i = 0; + do { + short len = p[i + 1]; - switch (p[i]) { - case 0: - break; - case 0x01: - fprintf(logfd, "Connection management: "); - if(p[i + 2]&1) - fprintf(logfd, "HO "); - else - if(p[i + 2]&2) - fprintf(logfd, "LCR "); - else - fprintf(logfd, "LE "); - if(p[i + 2]&4) - fprintf(logfd, "GDA:"); - else - fprintf(logfd, "VDA:"); - if(p[i + 2]&8) - fprintf(logfd, "ESS\n"); - else - fprintf(logfd, "ESN\n"); - break; - case 0x02: - fprintf(logfd, "Signal quality %01d\n", p[i + 2]); - break; - case 0x03: - fprintf(logfd, "XID sequencing %1d:%1d\n", p[i + 2] >> 4, p[i + 2] & 0x7); - break; - case 0x04: - fprintf(logfd, "Specific options: ", p[i + 2]); - if(p[i + 2]&1) - fprintf(logfd, "GDA:"); - else - fprintf(logfd, "VDA:"); - if(p[i + 2]&2) - fprintf(logfd, "ESS:"); - else - fprintf(logfd, "ESN:"); - if(p[i + 2]&4) - fprintf(logfd, "IHS:"); - else - fprintf(logfd, "IHN:"); - if(p[i + 2]&8) - fprintf(logfd, "BHS:"); - else - fprintf(logfd, "BHN:"); - if(p[i + 2]&0x10) - fprintf(logfd, "BCS\n"); - else - fprintf(logfd, "BCN\n"); - break; - case 0x05: - fprintf(logfd, "Expedited subnetwork connection %02x\n", p[i + 2]); - break; - case 0x06: - fprintf(logfd, "LCR cause %02x\n", p[i + 2]); - break; - case 0x81: - fprintf(logfd, "Modulation support %02x\n", p[i + 2]); - break; - case 0x82:{ - unsigned int addr; - addr = - (reversebits(p[i + 2] >> 2, 6) << 21) | (reversebits(p[i + 3] >> 1, 7) << 14) | - (reversebits(p[i + 4] >> 1, 7) << 7) | (reversebits(p[i + 5] >> 1, 7)); - fprintf(logfd, "Acceptable alternative ground station %06x\n", addr & 0xffffff); - break; - } - case 0x83:{ - oooi_t oooi; - memset(&oooi, 0, sizeof(oooi)); - memcpy(oooi.da, &(p[i + 2]), 4); - fprintf(logfd, "Destination airport %s\n", oooi.da); - break; - } - case 0x84:{ - int alt; - short lat, lon; - lat = (((uint32_t)(p[i + 2]) << 8) | (p[i + 3]&0xf0)) ; - lon = (((uint32_t)(p[i + 3]&0xf) << 12) | (uint32_t)(p[i + 4])<<4) ; - alt = p[i + 5] * 1000; - fprintf(logfd, "Aircraft Position %4.1f %5.1f alt:%d\n" ,(float)lat/160.0, (float)lon/160.0, alt); - break; - } - default: - fprintf(logfd, "unknown private id %02x\n", p[i]); - if (verbose > 1) { - dumpdata(&(p[i]), len + 2); - } + switch (p[i]) { + case 0: + break; + case 0x01: + fprintf(logfd, "Connection management: "); + if (p[i + 2] & 1) + fprintf(logfd, "HO "); + else if (p[i + 2] & 2) + fprintf(logfd, "LCR "); + else + fprintf(logfd, "LE "); + if (p[i + 2] & 4) + fprintf(logfd, "GDA:"); + else + fprintf(logfd, "VDA:"); + if (p[i + 2] & 8) + fprintf(logfd, "ESS\n"); + else + fprintf(logfd, "ESN\n"); + break; + case 0x02: + fprintf(logfd, "Signal quality %01d\n", p[i + 2]); + break; + case 0x03: + fprintf(logfd, "XID sequencing %1d:%1d\n", + p[i + 2] >> 4, p[i + 2] & 0x7); + break; + case 0x04: + fprintf(logfd, "Specific options: ", p[i + 2]); + if (p[i + 2] & 1) + fprintf(logfd, "GDA:"); + else + fprintf(logfd, "VDA:"); + if (p[i + 2] & 2) + fprintf(logfd, "ESS:"); + else + fprintf(logfd, "ESN:"); + if (p[i + 2] & 4) + fprintf(logfd, "IHS:"); + else + fprintf(logfd, "IHN:"); + if (p[i + 2] & 8) + fprintf(logfd, "BHS:"); + else + fprintf(logfd, "BHN:"); + if (p[i + 2] & 0x10) + fprintf(logfd, "BCS\n"); + else + fprintf(logfd, "BCN\n"); + break; + case 0x05: + fprintf(logfd, "Expedited subnetwork connection %02x\n", + p[i + 2]); + break; + case 0x06: + fprintf(logfd, "LCR cause %02x\n", p[i + 2]); + break; + case 0x81: + fprintf(logfd, "Modulation support %02x\n", p[i + 2]); + break; + case 0x82:{ + unsigned int addr; + addr = + (reversebits(p[i + 2] >> 2, 6) << 21) | + (reversebits(p[i + 3] >> 1, 7) << 14) | + (reversebits(p[i + 4] >> 1, 7) << 7) | + (reversebits(p[i + 5] >> 1, 7)); + fprintf(logfd, + "Acceptable alternative ground station %06x\n", + addr & 0xffffff); + break; + } + case 0x83:{ + oooi_t oooi; + memset(&oooi, 0, sizeof(oooi)); + memcpy(oooi.da, &(p[i + 2]), 4); + fprintf(logfd, "Destination airport %s\n", + oooi.da); + break; + } + case 0x84:{ + int alt; + short lat, lon; + lat = + (((uint32_t) (p[i + 2]) << 8) | + (p[i + 3] & 0xf0)); + lon = + (((uint32_t) (p[i + 3] & 0xf) << 12) | + (uint32_t) (p[i + 4]) << 4); + alt = p[i + 5] * 1000; + fprintf(logfd, + "Aircraft Position %4.1f %5.1f alt:%d\n", + (float)lat / 160.0, (float)lon / 160.0, + alt); + break; + } + default: + fprintf(logfd, "unknown private id %02x\n", p[i]); + if (verbose > 1) { + dumpdata(&(p[i]), len + 2); + } - break; - } - i += 2 + len; - } while (i < len); + break; + } + i += 2 + len; + } while (i < len); } void outxid(unsigned char *p, int len) { - int i; + int i; - i = 0; - do { - short glen = p[i + 1] * 256 + p[i + 2]; + i = 0; + do { + short glen = p[i + 1] * 256 + p[i + 2]; - if (p[i] == 0x80) { - outpublicgr(&(p[i + 3]), glen); - i += 3 + glen; - continue; - } - if (p[i] == 0xf0) { - outprivategr(&(p[i + 3]), glen); - i += 3 + glen; - continue; - } - if (verbose > 1) { - fprintf(logfd, "unknown group %02x\n", p[i]); - dumpdata(&(p[i]), glen + 3); - } - i += 3 + glen; - } while (i < len); + if (p[i] == 0x80) { + outpublicgr(&(p[i + 3]), glen); + i += 3 + glen; + continue; + } + if (p[i] == 0xf0) { + outprivategr(&(p[i + 3]), glen); + i += 3 + glen; + continue; + } + if (verbose > 1) { + fprintf(logfd, "unknown group %02x\n", p[i]); + dumpdata(&(p[i]), glen + 3); + } + i += 3 + glen; + } while (i < len); } - diff --git a/rs.c b/rs.c index 5446bb0..bfcfd4b 100644 --- a/rs.c +++ b/rs.c @@ -5,55 +5,71 @@ static inline int mod255(int x) { - while (x >= 255) { - x -= 255; - x = (x >> 8) + (x & 255); - } - return x; + while (x >= 255) { + x -= 255; + x = (x >> 8) + (x & 255); + } + return x; } #define MODNN(x) mod255(x) static const unsigned char alpha_to[] = { - 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x87, 0x89, 0x95, 0xad, 0xdd, 0x3d, 0x7a, 0xf4, - 0x6f, 0xde, 0x3b, 0x76, 0xec, 0x5f, 0xbe, 0xfb, 0x71, 0xe2, 0x43, 0x86, 0x8b, 0x91, 0xa5, 0xcd, - 0x1d, 0x3a, 0x74, 0xe8, 0x57, 0xae, 0xdb, 0x31, 0x62, 0xc4, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, 0x67, - 0xce, 0x1b, 0x36, 0x6c, 0xd8, 0x37, 0x6e, 0xdc, 0x3f, 0x7e, 0xfc, 0x7f, 0xfe, 0x7b, 0xf6, 0x6b, - 0xd6, 0x2b, 0x56, 0xac, 0xdf, 0x39, 0x72, 0xe4, 0x4f, 0x9e, 0xbb, 0xf1, 0x65, 0xca, 0x13, 0x26, - 0x4c, 0x98, 0xb7, 0xe9, 0x55, 0xaa, 0xd3, 0x21, 0x42, 0x84, 0x8f, 0x99, 0xb5, 0xed, 0x5d, 0xba, - 0xf3, 0x61, 0xc2, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, - 0x47, 0x8e, 0x9b, 0xb1, 0xe5, 0x4d, 0x9a, 0xb3, 0xe1, 0x45, 0x8a, 0x93, 0xa1, 0xc5, 0x0d, 0x1a, - 0x34, 0x68, 0xd0, 0x27, 0x4e, 0x9c, 0xbf, 0xf9, 0x75, 0xea, 0x53, 0xa6, 0xcb, 0x11, 0x22, 0x44, - 0x88, 0x97, 0xa9, 0xd5, 0x2d, 0x5a, 0xb4, 0xef, 0x59, 0xb2, 0xe3, 0x41, 0x82, 0x83, 0x81, 0x85, - 0x8d, 0x9d, 0xbd, 0xfd, 0x7d, 0xfa, 0x73, 0xe6, 0x4b, 0x96, 0xab, 0xd1, 0x25, 0x4a, 0x94, 0xaf, - 0xd9, 0x35, 0x6a, 0xd4, 0x2f, 0x5e, 0xbc, 0xff, 0x79, 0xf2, 0x63, 0xc6, 0x0b, 0x16, 0x2c, 0x58, - 0xb0, 0xe7, 0x49, 0x92, 0xa3, 0xc1, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0xc7, 0x09, 0x12, 0x24, - 0x48, 0x90, 0xa7, 0xc9, 0x15, 0x2a, 0x54, 0xa8, 0xd7, 0x29, 0x52, 0xa4, 0xcf, 0x19, 0x32, 0x64, - 0xc8, 0x17, 0x2e, 0x5c, 0xb8, 0xf7, 0x69, 0xd2, 0x23, 0x46, 0x8c, 0x9f, 0xb9, 0xf5, 0x6d, 0xda, - 0x33, 0x66, 0xcc, 0x1f, 0x3e, 0x7c, 0xf8, 0x77, 0xee, 0x5b, 0xb6, 0xeb, 0x51, 0xa2, 0xc3, 0x00, + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, + 0x87, 0x89, 0x95, 0xad, 0xdd, 0x3d, 0x7a, 0xf4, + 0x6f, 0xde, 0x3b, 0x76, 0xec, 0x5f, 0xbe, 0xfb, + 0x71, 0xe2, 0x43, 0x86, 0x8b, 0x91, 0xa5, 0xcd, + 0x1d, 0x3a, 0x74, 0xe8, 0x57, 0xae, 0xdb, 0x31, + 0x62, 0xc4, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, 0x67, + 0xce, 0x1b, 0x36, 0x6c, 0xd8, 0x37, 0x6e, 0xdc, + 0x3f, 0x7e, 0xfc, 0x7f, 0xfe, 0x7b, 0xf6, 0x6b, + 0xd6, 0x2b, 0x56, 0xac, 0xdf, 0x39, 0x72, 0xe4, + 0x4f, 0x9e, 0xbb, 0xf1, 0x65, 0xca, 0x13, 0x26, + 0x4c, 0x98, 0xb7, 0xe9, 0x55, 0xaa, 0xd3, 0x21, + 0x42, 0x84, 0x8f, 0x99, 0xb5, 0xed, 0x5d, 0xba, + 0xf3, 0x61, 0xc2, 0x03, 0x06, 0x0c, 0x18, 0x30, + 0x60, 0xc0, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, + 0x47, 0x8e, 0x9b, 0xb1, 0xe5, 0x4d, 0x9a, 0xb3, + 0xe1, 0x45, 0x8a, 0x93, 0xa1, 0xc5, 0x0d, 0x1a, + 0x34, 0x68, 0xd0, 0x27, 0x4e, 0x9c, 0xbf, 0xf9, + 0x75, 0xea, 0x53, 0xa6, 0xcb, 0x11, 0x22, 0x44, + 0x88, 0x97, 0xa9, 0xd5, 0x2d, 0x5a, 0xb4, 0xef, + 0x59, 0xb2, 0xe3, 0x41, 0x82, 0x83, 0x81, 0x85, + 0x8d, 0x9d, 0xbd, 0xfd, 0x7d, 0xfa, 0x73, 0xe6, + 0x4b, 0x96, 0xab, 0xd1, 0x25, 0x4a, 0x94, 0xaf, + 0xd9, 0x35, 0x6a, 0xd4, 0x2f, 0x5e, 0xbc, 0xff, + 0x79, 0xf2, 0x63, 0xc6, 0x0b, 0x16, 0x2c, 0x58, + 0xb0, 0xe7, 0x49, 0x92, 0xa3, 0xc1, 0x05, 0x0a, + 0x14, 0x28, 0x50, 0xa0, 0xc7, 0x09, 0x12, 0x24, + 0x48, 0x90, 0xa7, 0xc9, 0x15, 0x2a, 0x54, 0xa8, + 0xd7, 0x29, 0x52, 0xa4, 0xcf, 0x19, 0x32, 0x64, + 0xc8, 0x17, 0x2e, 0x5c, 0xb8, 0xf7, 0x69, 0xd2, + 0x23, 0x46, 0x8c, 0x9f, 0xb9, 0xf5, 0x6d, 0xda, + 0x33, 0x66, 0xcc, 0x1f, 0x3e, 0x7c, 0xf8, 0x77, + 0xee, 0x5b, 0xb6, 0xeb, 0x51, 0xa2, 0xc3, 0x00 }; static const unsigned char index_of[] = { - 255, 0, 1, 99, 2, 198, 100, 106, 3, 205, 199, 188, 101, 126, 107, 42, - 4, 141, 206, 78, 200, 212, 189, 225, 102, 221, 127, 49, 108, 32, 43, 243, - 5, 87, 142, 232, 207, 172, 79, 131, 201, 217, 213, 65, 190, 148, 226, 180, - 103, 39, 222, 240, 128, 177, 50, 53, 109, 69, 33, 18, 44, 13, 244, 56, - 6, 155, 88, 26, 143, 121, 233, 112, 208, 194, 173, 168, 80, 117, 132, 72, - 202, 252, 218, 138, 214, 84, 66, 36, 191, 152, 149, 249, 227, 94, 181, 21, - 104, 97, 40, 186, 223, 76, 241, 47, 129, 230, 178, 63, 51, 238, 54, 16, - 110, 24, 70, 166, 34, 136, 19, 247, 45, 184, 14, 61, 245, 164, 57, 59, - 7, 158, 156, 157, 89, 159, 27, 8, 144, 9, 122, 28, 234, 160, 113, 90, - 209, 29, 195, 123, 174, 10, 169, 145, 81, 91, 118, 114, 133, 161, 73, 235, - 203, 124, 253, 196, 219, 30, 139, 210, 215, 146, 85, 170, 67, 11, 37, 175, - 192, 115, 153, 119, 150, 92, 250, 82, 228, 236, 95, 74, 182, 162, 22, 134, - 105, 197, 98, 254, 41, 125, 187, 204, 224, 211, 77, 140, 242, 31, 48, 220, - 130, 171, 231, 86, 179, 147, 64, 216, 52, 176, 239, 38, 55, 12, 17, 68, - 111, 120, 25, 154, 71, 116, 167, 193, 35, 83, 137, 251, 20, 93, 248, 151, - 46, 75, 185, 96, 15, 237, 62, 229, 246, 135, 165, 23, 58, 163, 60, 183, + 255, 0, 1, 99, 2, 198, 100, 106, 3, 205, 199, 188, 101, 126, 107, 42, + 4, 141, 206, 78, 200, 212, 189, 225, 102, 221, 127, 49, 108, 32, 43, 243, + 5, 87, 142, 232, 207, 172, 79, 131, 201, 217, 213, 65, 190, 148, 226, 180, + 103, 39, 222, 240, 128, 177, 50, 53, 109, 69, 33, 18, 44, 13, 244, 56, + 6, 155, 88, 26, 143, 121, 233, 112, 208, 194, 173, 168, 80, 117, 132, 72, + 202, 252, 218, 138, 214, 84, 66, 36, 191, 152, 149, 249, 227, 94, 181, 21, + 104, 97, 40, 186, 223, 76, 241, 47, 129, 230, 178, 63, 51, 238, 54, 16, + 110, 24, 70, 166, 34, 136, 19, 247, 45, 184, 14, 61, 245, 164, 57, 59, + 7, 158, 156, 157, 89, 159, 27, 8, 144, 9, 122, 28, 234, 160, 113, 90, + 209, 29, 195, 123, 174, 10, 169, 145, 81, 91, 118, 114, 133, 161, 73, 235, + 203, 124, 253, 196, 219, 30, 139, 210, 215, 146, 85, 170, 67, 11, 37, 175, + 192, 115, 153, 119, 150, 92, 250, 82, 228, 236, 95, 74, 182, 162, 22, 134, + 105, 197, 98, 254, 41, 125, 187, 204, 224, 211, 77, 140, 242, 31, 48, 220, + 130, 171, 231, 86, 179, 147, 64, 216, 52, 176, 239, 38, 55, 12, 17, 68, + 111, 120, 25, 154, 71, 116, 167, 193, 35, 83, 137, 251, 20, 93, 248, 151, + 46, 75, 185, 96, 15, 237, 62, 229, 246, 135, 165, 23, 58, 163, 60, 183, }; static const unsigned char poly[] = { - 225, 156, 176, 244, 186, 176, 0, + 225, 156, 176, 244, 186, 176, 0, }; #define MM 8 @@ -70,193 +86,211 @@ static const unsigned char poly[] = { int rs(unsigned char *data, int *eras_pos, int no_eras) { - int deg_lambda, el, deg_omega; - int i, j, r, k; - unsigned char u, q, tmp, num1, num2, den, discr_r; - unsigned char lambda[NROOTS + 1], s[NROOTS]; /* Err+Eras Locator poly - * and syndrome poly */ - unsigned char b[NROOTS + 1], t[NROOTS + 1], omega[NROOTS + 1]; - unsigned char root[NROOTS], reg[NROOTS + 1], loc[NROOTS]; - int syn_error, count; + int deg_lambda, el, deg_omega; + int i, j, r, k; + unsigned char u, q, tmp, num1, num2, den, discr_r; + unsigned char lambda[NROOTS + 1], s[NROOTS]; /* Err+Eras Locator poly + * and syndrome poly */ + unsigned char b[NROOTS + 1], t[NROOTS + 1], omega[NROOTS + 1]; + unsigned char root[NROOTS], reg[NROOTS + 1], loc[NROOTS]; + int syn_error, count; - /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */ - for (i = 0; i < NROOTS; i++) - s[i] = data[0]; + /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */ + for (i = 0; i < NROOTS; i++) + s[i] = data[0]; - for (j = 1; j < NN; j++) { - for (i = 0; i < NROOTS; i++) { - if (s[i] == 0) { - s[i] = data[j]; - } else { - s[i] = data[j] ^ ALPHA_TO[MODNN(INDEX_OF[s[i]] + (FCR + i) * PRIM)]; - } - } - } + for (j = 1; j < NN; j++) { + for (i = 0; i < NROOTS; i++) { + if (s[i] == 0) { + s[i] = data[j]; + } else { + s[i] = + data[j] ^ + ALPHA_TO[MODNN + (INDEX_OF[s[i]] + + (FCR + i) * PRIM)]; + } + } + } - /* Convert syndromes to index form, checking for nonzero condition */ - syn_error = 0; - for (i = 0; i < NROOTS; i++) { - syn_error |= s[i]; - s[i] = INDEX_OF[s[i]]; - } + /* Convert syndromes to index form, checking for nonzero condition */ + syn_error = 0; + for (i = 0; i < NROOTS; i++) { + syn_error |= s[i]; + s[i] = INDEX_OF[s[i]]; + } - if (!syn_error) { - /* if syndrome is zero, data[] is a codeword and there are no - * errors to correct. So return data[] unmodified - */ - count = 0; - goto finish; - } - memset(&lambda[1], 0, NROOTS * sizeof(lambda[0])); - lambda[0] = 1; + if (!syn_error) { + /* if syndrome is zero, data[] is a codeword and there are no + * errors to correct. So return data[] unmodified + */ + count = 0; + goto finish; + } + memset(&lambda[1], 0, NROOTS * sizeof(lambda[0])); + lambda[0] = 1; - if (no_eras > 0) { - /* Init lambda to be the erasure locator polynomial */ - lambda[1] = ALPHA_TO[MODNN(PRIM * (NN - 1 - eras_pos[0]))]; - for (i = 1; i < no_eras; i++) { - u = MODNN(PRIM * (NN - 1 - eras_pos[i])); - for (j = i + 1; j > 0; j--) { - tmp = INDEX_OF[lambda[j - 1]]; - if (tmp != A0) - lambda[j] ^= ALPHA_TO[MODNN(u + tmp)]; - } - } + if (no_eras > 0) { + /* Init lambda to be the erasure locator polynomial */ + lambda[1] = ALPHA_TO[MODNN(PRIM * (NN - 1 - eras_pos[0]))]; + for (i = 1; i < no_eras; i++) { + u = MODNN(PRIM * (NN - 1 - eras_pos[i])); + for (j = i + 1; j > 0; j--) { + tmp = INDEX_OF[lambda[j - 1]]; + if (tmp != A0) + lambda[j] ^= ALPHA_TO[MODNN(u + tmp)]; + } + } - } - for (i = 0; i < NROOTS + 1; i++) - b[i] = INDEX_OF[lambda[i]]; + } + for (i = 0; i < NROOTS + 1; i++) + b[i] = INDEX_OF[lambda[i]]; - /* - * Begin Berlekamp-Massey algorithm to determine error+erasure - * locator polynomial - */ - r = no_eras; - el = no_eras; - while (++r <= NROOTS) { /* r is the step number */ - /* Compute discrepancy at the r-th step in poly-form */ - discr_r = 0; - for (i = 0; i < r; i++) { - if ((lambda[i] != 0) && (s[r - i - 1] != A0)) { - discr_r ^= ALPHA_TO[MODNN(INDEX_OF[lambda[i]] + s[r - i - 1])]; - } - } - discr_r = INDEX_OF[discr_r]; /* Index form */ - if (discr_r == A0) { - /* 2 lines below: B(x) <-- x*B(x) */ - memmove(&b[1], b, NROOTS * sizeof(b[0])); - b[0] = A0; - } else { - /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ - t[0] = lambda[0]; - for (i = 0; i < NROOTS; i++) { - if (b[i] != A0) - t[i + 1] = lambda[i + 1] ^ ALPHA_TO[MODNN(discr_r + b[i])]; - else - t[i + 1] = lambda[i + 1]; - } - if (2 * el <= r + no_eras - 1) { - el = r + no_eras - el; - /* - * 2 lines below: B(x) <-- inv(discr_r) * - * lambda(x) - */ - for (i = 0; i <= NROOTS; i++) - b[i] = (lambda[i] == 0) ? A0 : MODNN(INDEX_OF[lambda[i]] - discr_r + NN); - } else { - /* 2 lines below: B(x) <-- x*B(x) */ - memmove(&b[1], b, NROOTS * sizeof(b[0])); - b[0] = A0; - } - memcpy(lambda, t, (NROOTS + 1) * sizeof(t[0])); - } - } + /* + * Begin Berlekamp-Massey algorithm to determine error+erasure + * locator polynomial + */ + r = no_eras; + el = no_eras; + while (++r <= NROOTS) { /* r is the step number */ + /* Compute discrepancy at the r-th step in poly-form */ + discr_r = 0; + for (i = 0; i < r; i++) { + if ((lambda[i] != 0) && (s[r - i - 1] != A0)) { + discr_r ^= + ALPHA_TO[MODNN + (INDEX_OF[lambda[i]] + + s[r - i - 1])]; + } + } + discr_r = INDEX_OF[discr_r]; /* Index form */ + if (discr_r == A0) { + /* 2 lines below: B(x) <-- x*B(x) */ + memmove(&b[1], b, NROOTS * sizeof(b[0])); + b[0] = A0; + } else { + /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ + t[0] = lambda[0]; + for (i = 0; i < NROOTS; i++) { + if (b[i] != A0) + t[i + 1] = + lambda[i + + 1] ^ ALPHA_TO[MODNN(discr_r + + b[i])]; + else + t[i + 1] = lambda[i + 1]; + } + if (2 * el <= r + no_eras - 1) { + el = r + no_eras - el; + /* + * 2 lines below: B(x) <-- inv(discr_r) * + * lambda(x) + */ + for (i = 0; i <= NROOTS; i++) + b[i] = + (lambda[i] == + 0) ? A0 : MODNN(INDEX_OF[lambda[i]] + - discr_r + NN); + } else { + /* 2 lines below: B(x) <-- x*B(x) */ + memmove(&b[1], b, NROOTS * sizeof(b[0])); + b[0] = A0; + } + memcpy(lambda, t, (NROOTS + 1) * sizeof(t[0])); + } + } - /* Convert lambda to index form and compute deg(lambda(x)) */ - deg_lambda = 0; - for (i = 0; i < NROOTS + 1; i++) { - lambda[i] = INDEX_OF[lambda[i]]; - if (lambda[i] != A0) - deg_lambda = i; - } - /* Find roots of the error+erasure locator polynomial by Chien search */ - memcpy(®[1], &lambda[1], NROOTS * sizeof(reg[0])); - count = 0; /* Number of roots of lambda(x) */ - for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) { - q = 1; /* lambda[0] is always 0 */ - for (j = deg_lambda; j > 0; j--) { - if (reg[j] != A0) { - reg[j] = MODNN(reg[j] + j); - q ^= ALPHA_TO[reg[j]]; - } - } - if (q != 0) - continue; /* Not a root */ - /* store root (index-form) and error location number */ - root[count] = i; - loc[count] = k; - /* If we've already found max possible roots, - * abort the search to save time - */ - if (++count == deg_lambda) - break; - } - if (deg_lambda != count) { - /* - * deg(lambda) unequal to number of roots => uncorrectable - * error detected - */ - count = -1; - goto finish; - } - /* - * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo - * x**NROOTS). in index form. Also find deg(omega). - */ - deg_omega = 0; - for (i = 0; i < NROOTS; i++) { - tmp = 0; - j = (deg_lambda < i) ? deg_lambda : i; - for (; j >= 0; j--) { - if ((s[i - j] != A0) && (lambda[j] != A0)) - tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])]; - } - if (tmp != 0) - deg_omega = i; - omega[i] = INDEX_OF[tmp]; - } - omega[NROOTS] = A0; + /* Convert lambda to index form and compute deg(lambda(x)) */ + deg_lambda = 0; + for (i = 0; i < NROOTS + 1; i++) { + lambda[i] = INDEX_OF[lambda[i]]; + if (lambda[i] != A0) + deg_lambda = i; + } + /* Find roots of the error+erasure locator polynomial by Chien search */ + memcpy(®[1], &lambda[1], NROOTS * sizeof(reg[0])); + count = 0; /* Number of roots of lambda(x) */ + for (i = 1, k = IPRIM - 1; i <= NN; i++, k = MODNN(k + IPRIM)) { + q = 1; /* lambda[0] is always 0 */ + for (j = deg_lambda; j > 0; j--) { + if (reg[j] != A0) { + reg[j] = MODNN(reg[j] + j); + q ^= ALPHA_TO[reg[j]]; + } + } + if (q != 0) + continue; /* Not a root */ + /* store root (index-form) and error location number */ + root[count] = i; + loc[count] = k; + /* If we've already found max possible roots, + * abort the search to save time + */ + if (++count == deg_lambda) + break; + } + if (deg_lambda != count) { + /* + * deg(lambda) unequal to number of roots => uncorrectable + * error detected + */ + count = -1; + goto finish; + } + /* + * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo + * x**NROOTS). in index form. Also find deg(omega). + */ + deg_omega = 0; + for (i = 0; i < NROOTS; i++) { + tmp = 0; + j = (deg_lambda < i) ? deg_lambda : i; + for (; j >= 0; j--) { + if ((s[i - j] != A0) && (lambda[j] != A0)) + tmp ^= ALPHA_TO[MODNN(s[i - j] + lambda[j])]; + } + if (tmp != 0) + deg_omega = i; + omega[i] = INDEX_OF[tmp]; + } + omega[NROOTS] = A0; - /* - * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = - * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form - */ - for (j = count - 1; j >= 0; j--) { - num1 = 0; - for (i = deg_omega; i >= 0; i--) { - if (omega[i] != A0) - num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])]; - } - num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)]; - den = 0; + /* + * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = + * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form + */ + for (j = count - 1; j >= 0; j--) { + num1 = 0; + for (i = deg_omega; i >= 0; i--) { + if (omega[i] != A0) + num1 ^= ALPHA_TO[MODNN(omega[i] + i * root[j])]; + } + num2 = ALPHA_TO[MODNN(root[j] * (FCR - 1) + NN)]; + den = 0; - /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ - for (i = min(deg_lambda, NROOTS - 1) & ~1; i >= 0; i -= 2) { - if (lambda[i + 1] != A0) - den ^= ALPHA_TO[MODNN(lambda[i + 1] + i * root[j])]; - } - if (den == 0) { - count = -1; - goto finish; - } - /* Apply error to data */ - if (num1 != 0) { - data[loc[j]] ^= ALPHA_TO[MODNN(INDEX_OF[num1] + INDEX_OF[num2] + NN - INDEX_OF[den])]; - } - } + /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ + for (i = min(deg_lambda, NROOTS - 1) & ~1; i >= 0; i -= 2) { + if (lambda[i + 1] != A0) + den ^= + ALPHA_TO[MODNN + (lambda[i + 1] + i * root[j])]; + } + if (den == 0) { + count = -1; + goto finish; + } + /* Apply error to data */ + if (num1 != 0) { + data[loc[j]] ^= + ALPHA_TO[MODNN + (INDEX_OF[num1] + INDEX_OF[num2] + NN - + INDEX_OF[den])]; + } + } finish: - if (eras_pos != NULL) { - for (i = 0; i < count; i++) - eras_pos[i] = loc[i]; - } - return count; + if (eras_pos != NULL) { + for (i = 0; i < count; i++) + eras_pos[i] = loc[i]; + } + return count; } diff --git a/rtl.c b/rtl.c index 9fd5eb2..38fb694 100644 --- a/rtl.c +++ b/rtl.c @@ -28,7 +28,7 @@ #include "vdlm2.h" extern int nbch; -extern pthread_barrier_t Bar1,Bar2; +extern pthread_barrier_t Bar1, Bar2; static rtlsdr_dev_t *dev = NULL; static int status = 0; @@ -175,7 +175,7 @@ int nearest_gain(int target_gain) return close_gain; } -int initRtl(char **argv, int optind, thread_param_t *param) +int initRtl(char **argv, int optind, thread_param_t * param) { int r, n; int dev_index; @@ -213,7 +213,8 @@ int initRtl(char **argv, int optind, thread_param_t *param) Fd[nbch] = (int)(1000000 * atof(argF)); optind++; if (Fd[nbch] < 118000000 || Fd[nbch] > 138000000) { - fprintf(stderr, "WARNING: Invalid frequency %d\n", Fd[nbch]); + fprintf(stderr, "WARNING: Invalid frequency %d\n", + Fd[nbch]); continue; } param[nbch].chn = nbch; @@ -262,7 +263,7 @@ int initRtl(char **argv, int optind, thread_param_t *param) return 0; } -complex float Cbuff[RTLINBUFSZ/2]; +complex float Cbuff[RTLINBUFSZ / 2]; void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx) { int i; @@ -273,12 +274,14 @@ void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx) } pthread_barrier_wait(&Bar1); - + for (i = 0; i < RTLINBUFSZ;) { float r, g; - r = (float)rtlinbuff[i] - (float)127.5; i++; - g = (float)rtlinbuff[i] - (float)127.5; i++; - Cbuff[i/2]=r+g*I; + r = (float)rtlinbuff[i] - (float)127.5; + i++; + g = (float)rtlinbuff[i] - (float)127.5; + i++; + Cbuff[i / 2] = r + g * I; } pthread_barrier_wait(&Bar2); diff --git a/vdlm2.c b/vdlm2.c index d548a37..02c4fa3 100644 --- a/vdlm2.c +++ b/vdlm2.c @@ -26,177 +26,180 @@ #include "acars.h" #include "crc.h" -#define PPPINITFCS16 0xffff /* Initial FCS value */ -#define PPPGOODFCS16 0xf0b8 /* Good final FCS value */ +#define PPPINITFCS16 0xffff /* Initial FCS value */ +#define PPPGOODFCS16 0xf0b8 /* Good final FCS value */ static pthread_mutex_t blkmtx; static pthread_cond_t blkwcd; static msgblk_t *blkq_s = NULL; static msgblk_t *blkq_e = NULL; -static void check_frame(msgblk_t *blk,unsigned char *hdata,int l) +static void check_frame(msgblk_t * blk, unsigned char *hdata, int l) { - int i,d; - unsigned short crc; + int i, d; + unsigned short crc; - if (l < 13) { - if (verbose > 1) - fprintf(logfd, "#%d error too short\n",blk->chn+1); + if (l < 13) { + if (verbose > 1) + fprintf(logfd, "#%d error too short\n", blk->chn + 1); return; - } - - /* crc */ - crc = PPPINITFCS16; - for (i = 1; i < l-1; i++) { - update_crc(crc, hdata[i]); - } - if (crc != PPPGOODFCS16) { - if (verbose > 1) - fprintf(logfd, "#%derror crc\n",blk->chn+1); + } + + /* crc */ + crc = PPPINITFCS16; + for (i = 1; i < l - 1; i++) { + update_crc(crc, hdata[i]); + } + if (crc != PPPGOODFCS16) { + if (verbose > 1) + fprintf(logfd, "#%derror crc\n", blk->chn + 1); return; - } + } - out(blk,hdata,l); + out(blk, hdata, l); } static int set_eras(int *eras_pos, int nb) { - int i, nbera = 0; - - if (nb <= 67) { - nbera = 2; - eras_pos[0] = 253; - eras_pos[1] = 254; - } - if (nb <= 30) { - nbera = 4; - eras_pos[0] = 251; - eras_pos[1] = 252; - eras_pos[2] = 253; - eras_pos[3] = 254; - } - - return nbera; + int i, nbera = 0; + + if (nb <= 67) { + nbera = 2; + eras_pos[0] = 253; + eras_pos[1] = 254; + } + if (nb <= 30) { + nbera = 4; + eras_pos[0] = 251; + eras_pos[1] = 252; + eras_pos[2] = 253; + eras_pos[3] = 254; + } + + return nbera; } static void *blk_thread(void *arg) { - do { - msgblk_t *blk; - int i, n, k, r, s, t; - unsigned char hdata[65*249]; - int nbera; - int eras_pos[6]; - - pthread_mutex_lock(&blkmtx); - while (blkq_e == NULL) - pthread_cond_wait(&blkwcd, &blkmtx); - - blk = blkq_e; - blkq_e = blk->prev; - if (blkq_e == NULL) - blkq_s = NULL; - pthread_mutex_unlock(&blkmtx); - - k = s = t = 0; - hdata[k] = 0; - for (r = 0; r < blk->nbrow; r++) { - int by; - - if (r == blk->nbrow - 1) { - by = blk->nlbyte; - nbera = set_eras(eras_pos, by); - } else { - by = 249;nbera=0; - } - - /* reed solomon FEC */ - rs(blk->data[r], eras_pos, nbera); - - /* HDLC bit un stuffing */ - for (i = 0; i < by; i++) { - - for (n = 0; n < 8; n++) { - if (blk->data[r][i] & (1 << n)) { - hdata[k] |= 1 << s; - t++; - } else { - if (t == 5) { - t = 0; - continue; - } - t = 0; - } - s++; - if (s == 8) { - s = 0; - if(hdata[k]==0x7e) { - if(k==0) { - k++;hdata[k] = 0; - } else - if(k==1) { - hdata[1] = 0; - } else - if(k>1) { - check_frame(blk,hdata,k+1); - k++;hdata[k] = 0; - } - } else - if(k>0) { - k++;hdata[k] = 0; - } - } - } - } - - } - - free(blk); - } while (1); - - return NULL; + do { + msgblk_t *blk; + int i, n, k, r, s, t; + unsigned char hdata[65 * 249]; + int nbera; + int eras_pos[6]; + + pthread_mutex_lock(&blkmtx); + while (blkq_e == NULL) + pthread_cond_wait(&blkwcd, &blkmtx); + + blk = blkq_e; + blkq_e = blk->prev; + if (blkq_e == NULL) + blkq_s = NULL; + pthread_mutex_unlock(&blkmtx); + + k = s = t = 0; + hdata[k] = 0; + for (r = 0; r < blk->nbrow; r++) { + int by; + + if (r == blk->nbrow - 1) { + by = blk->nlbyte; + nbera = set_eras(eras_pos, by); + } else { + by = 249; + nbera = 0; + } + + /* reed solomon FEC */ + rs(blk->data[r], eras_pos, nbera); + + /* HDLC bit un stuffing */ + for (i = 0; i < by; i++) { + + for (n = 0; n < 8; n++) { + if (blk->data[r][i] & (1 << n)) { + hdata[k] |= 1 << s; + t++; + } else { + if (t == 5) { + t = 0; + continue; + } + t = 0; + } + s++; + if (s == 8) { + s = 0; + if (hdata[k] == 0x7e) { + if (k == 0) { + k++; + hdata[k] = 0; + } else if (k == 1) { + hdata[1] = 0; + } else if (k > 1) { + check_frame(blk, + hdata, + k + + 1); + k++; + hdata[k] = 0; + } + } else if (k > 0) { + k++; + hdata[k] = 0; + } + } + } + } + + } + + free(blk); + } while (1); + + return NULL; } - -int initVdlm2(channel_t *ch) +int initVdlm2(channel_t * ch) { - pthread_t th; + pthread_t th; - ch->state = WSYNC; - ch->blk = calloc(sizeof(msgblk_t), 1); - ch->blk->chn=ch->chn; + ch->state = WSYNC; + ch->blk = calloc(sizeof(msgblk_t), 1); + ch->blk->chn = ch->chn; - if(ch->chn==0) { - pthread_mutex_init(&blkmtx, NULL); - pthread_cond_init(&blkwcd, NULL); + if (ch->chn == 0) { + pthread_mutex_init(&blkmtx, NULL); + pthread_cond_init(&blkwcd, NULL); - pthread_create(&th, NULL, blk_thread, NULL); - } + pthread_create(&th, NULL, blk_thread, NULL); + } - return 0; + return 0; } void stopVdlm2(void) { - int c; - for (c = 0; blkq_e && c < 5; c++) - sleep(1); + int c; + for (c = 0; blkq_e && c < 5; c++) + sleep(1); } -void decodeVdlm2(channel_t *ch) +void decodeVdlm2(channel_t * ch) { - pthread_mutex_lock(&blkmtx); - ch->blk->prev = NULL; - if (blkq_s) - blkq_s->prev = ch->blk; - blkq_s = ch->blk; - if (blkq_e == NULL) - blkq_e = blkq_s; - pthread_cond_signal(&blkwcd); - pthread_mutex_unlock(&blkmtx); - - ch->blk = calloc(sizeof(msgblk_t), 1); - ch->blk->chn=ch->chn; - - return; + pthread_mutex_lock(&blkmtx); + ch->blk->prev = NULL; + if (blkq_s) + blkq_s->prev = ch->blk; + blkq_s = ch->blk; + if (blkq_e == NULL) + blkq_e = blkq_s; + pthread_cond_signal(&blkwcd); + pthread_mutex_unlock(&blkmtx); + + ch->blk = calloc(sizeof(msgblk_t), 1); + ch->blk->chn = ch->chn; + + return; } diff --git a/vdlm2.h b/vdlm2.h index 67d4e15..eefb1cf 100644 --- a/vdlm2.h +++ b/vdlm2.h @@ -34,43 +34,43 @@ #define MFLTLEN 65 #define MBUFLEN 17 typedef struct mskblk_s { - struct mskblk_s *prev; - int chn; - struct timespec ts; - float ppm; - int nbrow, nlbyte; - unsigned char data[65][255]; + struct mskblk_s *prev; + int chn; + struct timespec ts; + float ppm; + int nbrow, nlbyte; + unsigned char data[65][255]; } msgblk_t; typedef struct { - int chn; - int Fr; + int chn; + int Fr; } thread_param_t; #define NBPH 17 #define D8DWN 4 typedef struct { - complex float Inbuff[MBUFLEN]; - float Ph[NBPH * D8DWN]; - - int chn; - int Fr; - int ink,Phidx; - float df; - int clk; - float p2err, perr; - float pfr; - float P1; - - unsigned int scrambler; - unsigned int nbits; - unsigned int nbyte; - unsigned int nrow,nbrow; - unsigned int nlbyte; - unsigned char bits; - - enum { WSYNC, GETHEAD, GETDATA, GETFEC } state; - msgblk_t *blk; + complex float Inbuff[MBUFLEN]; + float Ph[NBPH * D8DWN]; + + int chn; + int Fr; + int ink, Phidx; + float df; + int clk; + float p2err, perr; + float pfr; + float P1; + + unsigned int scrambler; + unsigned int nbits; + unsigned int nbyte; + unsigned int nrow, nbrow; + unsigned int nlbyte; + unsigned char bits; + + enum { WSYNC, GETHEAD, GETDATA, GETFEC } state; + msgblk_t *blk; } channel_t; @@ -83,7 +83,7 @@ extern int verbose; extern FILE *logfd; #ifdef WITH_RTL -extern int initRtl(char **argv,int optind,thread_param_t* param); +extern int initRtl(char **argv, int optind, thread_param_t * param); extern int runRtlSample(void); #endif @@ -92,15 +92,16 @@ extern void in_callback(unsigned char *rtlinbuff, uint32_t nread, void *ctx); extern int initFile(char *file); extern int runFileSample(void); -extern int initD8psk(channel_t *ch); +extern int initD8psk(channel_t * ch); extern void *rcv_thread(void *arg); -extern int initVdlm2(channel_t *ch); +extern int initVdlm2(channel_t * ch); extern void stopVdlm2(void); -extern void decodeVdlm2(channel_t *ch); +extern void decodeVdlm2(channel_t * ch); extern void init_crc_tab(void); -extern unsigned short update_crc(const unsigned short crc, const unsigned char c); +extern unsigned short update_crc(const unsigned short crc, + const unsigned char c); extern void viterbi_init(void); extern void viterbi_add(float V, int n); @@ -111,5 +112,4 @@ extern unsigned int reversebits(const unsigned int bits, const int n); extern unsigned short pppfcs16(unsigned char *cp, int len); extern int rs(unsigned char *data, int *eras_pos, int no_eras); -extern void out(msgblk_t *blk,unsigned char *hdata,int l); - +extern void out(msgblk_t * blk, unsigned char *hdata, int l); diff --git a/viterbi.c b/viterbi.c index 0028999..9c1c430 100644 --- a/viterbi.c +++ b/viterbi.c @@ -27,90 +27,71 @@ static int Bk[NBITS + 1][NBSTATES]; static int B[NBITS + 1][NBSTATES]; const int H[NBITS] = { - 0b00110, 0b00111, 0b01001, 0b01010, 0b01011, - 0b01100, 0b01110, 0b01111, 0b10001, 0b10011, - 0b10101, 0b10110, 0b11000, 0b11001, 0b11010, - 0b11011, 0b11100, 0b11101, 0b11110, 0b11111, - 0b10000, 0b01000, 0b00100, 0b00010, 0b00001 + 0b00110, 0b00111, 0b01001, 0b01010, 0b01011, + 0b01100, 0b01110, 0b01111, 0b10001, 0b10011, + 0b10101, 0b10110, 0b11000, 0b11001, 0b11010, + 0b11011, 0b11100, 0b11101, 0b11110, 0b11111, + 0b10000, 0b01000, 0b00100, 0b00010, 0b00001 }; void viterbi_init(void) { - int s; - Pb[0][0] = 1.0; - for (s = 1; s < NBSTATES; s++) - Pb[0][s] = 0; + int s; + Pb[0][0] = 1.0; + for (s = 1; s < NBSTATES; s++) + Pb[0][s] = 0; } void viterbi_add(float V, int n) { - int s; - - for (s = 0; s < NBSTATES; s++) - Pb[n + 1][s] = 0; - - for (s = 0; s < NBSTATES; s++) { - double np; - int ns; - - if (Pb[n][s] == 0.0) - continue; - - /* 1 */ - np = Pb[n][s] * V; - ns = s ^ H[n]; - if (np > Pb[n + 1][ns]) { - Pb[n + 1][ns] = np; - Bk[n + 1][ns] = s; - B[n + 1][ns] = 1; - } - /* 0 */ - np = Pb[n][s] * (1.0 - V); - if (np > Pb[n + 1][s]) { - Pb[n + 1][s] = np; - Bk[n + 1][s] = s; - B[n + 1][s] = 0; - } - - } + int s; + + for (s = 0; s < NBSTATES; s++) + Pb[n + 1][s] = 0; + + for (s = 0; s < NBSTATES; s++) { + double np; + int ns; + + if (Pb[n][s] == 0.0) + continue; + + /* 1 */ + np = Pb[n][s] * V; + ns = s ^ H[n]; + if (np > Pb[n + 1][ns]) { + Pb[n + 1][ns] = np; + Bk[n + 1][ns] = s; + B[n + 1][ns] = 1; + } + /* 0 */ + np = Pb[n][s] * (1.0 - V); + if (np > Pb[n + 1][s]) { + Pb[n + 1][s] = np; + Bk[n + 1][s] = s; + B[n + 1][s] = 0; + } + + } } float viterbi_end(unsigned int *bits) { - int n; - int s; - int b; - - s = 0; - *bits = 0; - b = 1; - for (n = NBITS; n > 0; n--) { - if (B[n][s]) - *bits |= b; - s = Bk[n][s]; - b <<= 1; - } - return Pb[NBITS][0]; + int n; + int s; + int b; + + s = 0; + *bits = 0; + b = 1; + for (n = NBITS; n > 0; n--) { + if (B[n][s]) + *bits |= b; + s = Bk[n][s]; + b <<= 1; + } + return Pb[NBITS][0]; } -/* -main() -{ -const float V[25]= { -0,0.7,0.05,0.95, 0,0.7,0.95,0.95, 0.2,0.4,0.95,0.05, 0.95,0.05,0.95,0.95, 0.95,0.05,0,0.05, -0.7,0.95,0.05,0,0.6}; -int k; -int b; -float p; - -viterbi_init(); - -for(k=0;k<25;k++) - viterbi_add(V[k],k); - -p=viterbi_end(&b); -fprintf(stderr,"%0x %f\n,",b,p); -} -*/