diff --git a/dump.c b/dump.c index 79242ab..9de1892 100644 --- a/dump.c +++ b/dump.c @@ -1,676 +1,179 @@ -/*---------------------------------------------------------------------------*\ - - FILE........: dump.c - AUTHOR......: David Rowe - DATE CREATED: 25/8/09 - - Routines to dump data to text files for Octave analysis. - -\*---------------------------------------------------------------------------*/ - -/* - All rights reserved. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License version 2.1, as - published by the Free Software Foundation. This program is - distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public - License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program; if not, see . -*/ - -#include "defines.h" -#include "comp.h" -#include "dump.h" -#include -#include -#include -#include -#include - -#ifdef __EMBEDDED__ -#include "gdb_stdio.h" -#define fprintf gdb_stdio_fprintf -#define fopen gdb_stdio_fopen -#define fclose gdb_stdio_fclose -#endif - -#ifdef DUMP -static int dumpon = 0; - -static FILE *fsn = NULL; -static FILE *fsw = NULL; -static FILE *few = NULL; -static FILE *fsw_ = NULL; -static FILE *fsoftdec = NULL; -static FILE *fmodel = NULL; -static FILE *fqmodel = NULL; -static FILE *fpwb = NULL; -static FILE *fpw = NULL; -static FILE *frw = NULL; -static FILE *flsp = NULL; -static FILE *fweights = NULL; -static FILE *flsp_ = NULL; -static FILE *fmel = NULL; -static FILE *fmel_indexes = NULL; -static FILE *fphase = NULL; -static FILE *fphase_ = NULL; -static FILE *ffw = NULL; -static FILE *fe = NULL; -static FILE *fsq = NULL; -static FILE *fdec = NULL; -static FILE *fsnr = NULL; -static FILE *flpcsnr = NULL; -static FILE *fak = NULL; -static FILE *fak_ = NULL; -static FILE *fbg = NULL; -static FILE *fE = NULL; -static FILE *frk = NULL; -static FILE *fhephase = NULL; - -static char prefix[MAX_STR]; - -void dump_on(char p[]) { - dumpon = 1; - strcpy(prefix, p); -} - -void dump_off(){ - if (fsn != NULL) - fclose(fsn); - if (fsw != NULL) - fclose(fsw); - if (fsw_ != NULL) - fclose(fsw_); - if (few != NULL) - fclose(few); - if (fmodel != NULL) - fclose(fmodel); - if (fsoftdec != NULL) - fclose(fsoftdec); - if (fqmodel != NULL) - fclose(fqmodel); - if (fpwb != NULL) - fclose(fpwb); - if (fpw != NULL) - fclose(fpw); - if (frw != NULL) - fclose(frw); - if (flsp != NULL) - fclose(flsp); - if (fweights != NULL) - fclose(fweights); - if (flsp_ != NULL) - fclose(flsp_); - if (fmel != NULL) - fclose(fmel); - if (fmel_indexes != NULL) - fclose(fmel_indexes); - if (fphase != NULL) - fclose(fphase); - if (fphase_ != NULL) - fclose(fphase_); - if (ffw != NULL) - fclose(ffw); - if (fe != NULL) - fclose(fe); - if (fsq != NULL) - fclose(fsq); - if (fdec != NULL) - fclose(fdec); - if (fsnr != NULL) - fclose(fsnr); - if (flpcsnr != NULL) - fclose(flpcsnr); - if (fak != NULL) - fclose(fak); - if (fak_ != NULL) - fclose(fak_); - if (fbg != NULL) - fclose(fbg); - if (fE != NULL) - fclose(fE); - if (frk != NULL) - fclose(frk); - if (fhephase != NULL) - fclose(fhephase); -} - -void dump_Sn(int m_pitch, float Sn[]) { - int i; - char s[MAX_STR]; - - if (!dumpon) return; - - if (fsn == NULL) { - sprintf(s,"%s_sn.txt", prefix); - fsn = fopen(s, "wt"); - assert(fsn != NULL); - } - - /* split across two lines to avoid max line length problems */ - /* reconstruct in Octave */ - - for(i=0; iWo, model->L); - for(l=1; l<=model->L; l++) { - sprintf(s,"%12f ",model->A[l]); - strcat(line, s); - assert(strlen(line) < MAX_STR*10); - } - for(l=model->L+1; l<=MAX_AMP; l++) { - sprintf(s,"%12f ", 0.0); - strcat(line,s); - assert(strlen(line) < MAX_STR*10); - } - - sprintf(s,"%d\n",model->voiced); - strcat(line,s); - fprintf(fmodel,"%s",line); -} - -void dump_quantised_model(MODEL *model) { - int l; - char s[MAX_STR]; - char line[4096]; - - if (!dumpon) return; - - if (fqmodel == NULL) { - sprintf(s,"%s_qmodel.txt", prefix); - fqmodel = fopen(s, "wt"); - assert(fqmodel != NULL); - } - - sprintf(line,"%12f %12d ", model->Wo, model->L); - for(l=1; l<=model->L; l++) { - sprintf(s,"%12f ",model->A[l]); - strcat(line, s); - assert(strlen(line) < 4096); - } - for(l=model->L+1; l<=MAX_AMP; l++) { - sprintf(s,"%12f ", 0.0); - strcat(line, s); - assert(strlen(line) < 4096); - } - - sprintf(s,"%d\n",model->voiced); - strcat(line, s); - fprintf(fqmodel, "%s", line); -} - -void dump_phase(float phase[], int L) { - int l; - char s[MAX_STR]; - - if (!dumpon) return; - - if (fphase == NULL) { - sprintf(s,"%s_phase.txt", prefix); - fphase = fopen(s, "wt"); - assert(fphase != NULL); - } - - for(l=1; l<=L; l++) - fprintf(fphase,"%f\t",phase[l]); - for(l=L+1; l<=MAX_AMP; l++) - fprintf(fphase,"%f\t",0.0); - fprintf(fphase,"\n"); -} - -void dump_phase_(float phase_[], int L) { - int l; - char s[MAX_STR]; - - if (!dumpon) return; - - if (fphase_ == NULL) { - sprintf(s,"%s_phase_.txt", prefix); - fphase_ = fopen(s, "wt"); - assert(fphase_ != NULL); - } - - for(l=1; l<=L; l++) - fprintf(fphase_,"%f\t",phase_[l]); - for(l=L+1; l +#include +#include +#include +#include + +//internet +#include +#include + +//port +uint16_t port_num=17000; //default port + +//UDP packet +uint8_t bits[54]; + +//internet +struct sockaddr_in si_me, si_other; +int s, i, slen = sizeof(si_other) , rcv_len; + +//CRC +uint16_t CRC_LUT[256]; +uint16_t poly=0x5935; + +//test +//FILE *fp; + +//stream info +struct moip_packet +{ + uint16_t sid; + uint8_t src[10]; + uint8_t dst[10]; + uint16_t type; + uint8_t nonce[14]; + uint16_t fn; + uint8_t payload[16]; + uint16_t crc_udp; +} packet; + +void CRC_Init(uint16_t *crc_table, uint16_t poly) +{ + uint16_t remainder; + + for(uint16_t dividend=0; dividend<256; dividend++) + { + remainder=dividend<<8; + + for(uint8_t bit=8; bit>0; bit--) + { + if(remainder&(1<<15)) + remainder=(remainder<<1)^poly; + else + remainder=(remainder<<1); + } + + crc_table[dividend]=remainder; + } +} + +uint16_t CRC_M17(uint16_t* crc_table, const uint8_t* message, uint16_t nBytes) +{ + uint8_t data; + uint16_t remainder=0xFFFF; + + for(uint16_t byte=0; byte>8); + remainder=crc_table[data]^(remainder<<8); + } + + return(remainder); +} + +uint8_t* decode_callsign_base40(uint64_t encoded, uint8_t *callsign) +{ + if(encoded >= 262144000000000) + { + *callsign=0; + return callsign; + } + + uint8_t *p = callsign; + + for (; encoded>0; p++) + { + *p = "xABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/."[encoded % 40]; + encoded/=40; + } + + *p = 0; + + return callsign; +} + +// ./this.out port +int main(int argc, char *argv[]) +{ + if(argc==2) + { + port_num=atoi(argv[1]); + } + else + { + fprintf(stderr, "Not enough args\n"); + return 1; + } + + //init + memset(bits, 0, 54); + CRC_Init(CRC_LUT, poly); + + s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if(s==-1) + { + fprintf(stderr, "Socket error\n"); + return 1; + } + + //zero out the structure + memset((char*)&si_me, 0, sizeof(si_me)); + + si_me.sin_family = AF_INET; + si_me.sin_port = htons(port_num); + si_me.sin_addr.s_addr = htonl(INADDR_ANY); + + if(bind(s, (struct sockaddr*)&si_me, sizeof(si_me))==-1) + { + fprintf(stderr, "Can't bind to port %d\n", port_num); + return 1; + } + + printf("Listening for M17 frames on port %d:\n", port_num); + printf("Src\t\tDst\t\tType\tPld\n"); + + while(1) + { + //receive packet via UDP + if((rcv_len=recvfrom(s, bits, 54, 0, (struct sockaddr*)&si_other, &slen))==-1) + { + fprintf(stderr, "What the hell?\n"); + return 1; + } + + if(rcv_len==54) + { + packet.sid=(bits[4]<<8)|bits[5]; + uint64_t tmp; + tmp=(bits[6]<<(5*8))|(bits[7]<<(4*8))|(bits[8]<<(3*8))|(bits[9]<<(2*8))|(bits[10]<<(1*8))|bits[11]; + decode_callsign_base40(tmp, packet.src); + tmp=(bits[12]<<(5*8))|(bits[13]<<(4*8))|(bits[14]<<(3*8))|(bits[15]<<(2*8))|(bits[16]<<(1*8))|bits[17]; + decode_callsign_base40(tmp, packet.dst); + packet.type=(bits[18]<<8)|bits[19]; + + memcpy(packet.nonce, &bits[20], 14); + packet.fn=bits[34]<<8|bits[35]; + memcpy(packet.payload, &bits[36], 16); + packet.crc_udp=bits[52]<<8|bits[53]; + + //info + printf("%s\t\t%s\t\t%04X\t", packet.src, packet.dst, packet.type); + for(uint8_t i=0; i<128/8; i++) + { + printf("%02X", packet.payload[i]); + if(i==(128/8-1)) + printf("\n"); + } + + /*fp=fopen("out.raw", "a"); + fwrite(speech_buff, 2, 160, fp); + fclose(fp);*/ + } + } + + + return 0; +}