commit ceb8470d0ef87be8950b8be6dd222a485e6ce7cf
parent 79279b1c9882cd7a42f4dff6e518b085c3607d7b
Author: Christoph Lohmann <20h@r-36.net>
Date:   Sat, 27 Oct 2012 00:46:01 +0200
Making acarsdec work again.
Diffstat:
| acarsdec.h | | | 4 | ++-- | 
| getmesg.c | | | 29 | +++++++++++++++-------------- | 
| input.c | | | 40 | +++++++++++++++++++++------------------- | 
| main.c | | | 37 | ++++++++++++++++++------------------- | 
4 files changed, 56 insertions(+), 54 deletions(-)
diff --git a/acarsdec.h b/acarsdec.h
@@ -18,7 +18,7 @@ extern void resetbits(int ch);
 extern int getbit(short in, unsigned char *outbits, int ch);
 
 extern void init_mesg(void);
-extern int getmesg(unsigned char r, msg_t * msg, int ch);
+extern int getmesg(unsigned char r, msg_t *msg, int ch);
 
 extern int init_serv(short port);
 extern int send_mesg(msg_t *msg);
@@ -29,7 +29,7 @@ enum {
 	IN_FILE = 0,
 	IN_ALSA = 1,
 	IN_STDIN = 2,
-	
+
 	OUT_NET = 0x01,
 	OUT_PRINT = 0x02,
 	OUT_PROTO = 0x04,
diff --git a/getmesg.c b/getmesg.c
@@ -58,7 +58,7 @@ static void update_crc(unsigned short *crc, unsigned char ch)
 	}
 }
 
-static int build_mesg(char *txt, int len, msg_t * msg)
+static int build_mesg(char *txt, int len, msg_t *msg)
 {
 	int i, k;
 	char r;
@@ -66,18 +66,8 @@ static int build_mesg(char *txt, int len, msg_t * msg)
 	/* remove special chars */
 	for (i = 0; i < len; i++) {
 		r = txt[i];
-		switch(r) {
-		case 0x0d:
-			r = 176;
-			break;
-		case 0x0a:
-			r = 178;
-			break;
-		default:
-			if (r < ' ')
-				r = 0xa4;
-			break;
-		};
+		if (r < ' ' && r != 0x0d && r != 0x0a)
+			r = ' ';
 		txt[i] = r;
 	}
 	txt[i] = '\0';
@@ -127,12 +117,23 @@ void init_mesg(void)
 	mstat[0].state = mstat[1].state = HEADL;
 }
 
-int getmesg(unsigned char r, msg_t * msg, int ch)
+void
+print_mstat(struct mstat_s *mstat)
+{
+	if(mstat->state > 0) {
+		fprintf(stderr, "mstat: state = %d; ind = %d; crc = %.4x\n",
+				mstat->state, mstat->ind, mstat->crc);
+	}
+}
+
+int getmesg(unsigned char r, msg_t *msg, int ch)
 {
 	struct mstat_s *st;
 
 	st = &(mstat[ch]);
 
+	//print_mstat(st);
+
 	do {
 		switch (st->state) {
 		case HEADL:
diff --git a/input.c b/input.c
@@ -33,21 +33,21 @@ static int initstdin(void)
 {
 	SF_INFO infwav;
 
-/* open wav input file */
 	infwav.format = 0;
 	inwav = sf_open_fd(STDIN_FILENO, SFM_READ, &infwav, SF_TRUE);
 	if (inwav == NULL) {
 		fprintf(stderr, "could not open stdin\n");
-		return (0);
+		return 0;
 	}
 	if (infwav.samplerate != 48000) {
 		fprintf(stderr,
 			"Bad Input File sample rate: %d. Must be 48000\n",
 			infwav.samplerate);
-		return (0);
+		return 0;
 	}
-	nbch=infwav.channels;
-	return (infwav.channels);
+	nbch = infwav.channels;
+
+	return infwav.channels;
 }
 
 static SNDFILE *inwav;
@@ -55,21 +55,22 @@ static int initsnd(char *filename)
 {
 	SF_INFO infwav;
 
-/* open wav input file */
+	/* open wav input file */
 	infwav.format = 0;
 	inwav = sf_open(filename, SFM_READ, &infwav);
 	if (inwav == NULL) {
 		fprintf(stderr, "could not open %s\n", filename);
-		return (0);
+		return 0;
 	}
 	if (infwav.samplerate != 48000) {
 		fprintf(stderr,
 			"Bad Input File sample rate: %d. Must be 48000\n",
 			infwav.samplerate);
-		return (0);
+		return 0;
 	}
-	nbch=infwav.channels;
-	return (infwav.channels);
+	nbch = infwav.channels;
+
+	return infwav.channels;
 }
 
 static snd_pcm_t *capture_handle;
@@ -117,20 +118,21 @@ static int initalsa(char *filename)
 		return 0;
 	}
 
-	if ((err =
-	     snd_pcm_hw_params_set_rate(capture_handle, hw_params, 48000,
+	if ((err = snd_pcm_hw_params_set_rate(capture_handle, hw_params, 48000,
 					0)) < 0) {
 		fprintf(stderr, "cannot set sample rate (%s)\n",
 			snd_strerror(err));
 		return 0;
 	}
 
-	for(nbch=2;nbch>0;nbch--)  {
-		if (snd_pcm_hw_params_set_channels(capture_handle, hw_params, nbch)==0)	
+	for(nbch = 2; nbch > 0; nbch--)  {
+		if (snd_pcm_hw_params_set_channels(capture_handle,
+					hw_params, nbch) == 0) {
 			break;
+		}
 	}
 
-	if (nbch ==0) {
+	if (nbch == 0) {
 		fprintf(stderr, "cannot set number of channels\n");
 		return 0;
 	}
@@ -173,20 +175,20 @@ int getsample(short *sample, int nb)
 
 	switch(source) {
 	case IN_STDIN:
-		r = sf_read_short(inwav, sample, nb);
+		r = sf_read_short(inwav, sample, nb / nbch);
 		break;
 	case IN_FILE:
-		r = sf_read_short(inwav, sample, nb);
+		r = sf_read_short(inwav, sample, nb / nbch);
 		if (r == 0)
 			r = -1;	/* this is the end */
 		break;
 	case IN_ALSA:
-		r = snd_pcm_readi(capture_handle, sample, nb/nbch);
+		r = snd_pcm_readi(capture_handle, sample, nb / nbch);
 		if (r <= 0)
 			fprintf(stderr,
 				"cannot read from interface (%s)\n",
 				snd_strerror(r));
-		r=r*nbch;
+		r = r * nbch;
 		break;
 	}
 	return r;
diff --git a/main.c b/main.c
@@ -56,9 +56,9 @@ static void usage(void)
 			"input alsapcmdevice (ie: hw:0,0)\n");
 	fprintf(stderr, " -t :\t\t\tread from stdin.\n");
 	fprintf(stderr, " [-v] :\t\t\tbe verbose.\n");
-	fprintf(stderr, " [-c] :\t\t\tcolorfull output.\n");
+	fprintf(stderr, " [-c] :\t\t\tcolorful output.\n");
 	fprintf(stderr, " [-p] :\t\t\toutput should be parseable protocol.\n");
-	fprintf(stderr, " [-e] :\t\t\tdo debug?\n");
+	fprintf(stderr, " [-e] :\t\t\tturn on debugging\n");
 	fprintf(stderr, " [-LR] :\t\tdisable left or right channel "
 			"decoding of stereo signal\n");
 	fprintf(stderr, " [-s noport ] :\t\tact as an APRS local server, "
@@ -70,23 +70,23 @@ static void usage(void)
 	exit(1);
 }
 
-void print_mesg(msg_t * msg, int colored, int messgnumb)
+void print_mesg(msg_t *msg, int colored, int messgnumb)
 {
 	time_t t;
 	struct tm *tmp;
 	char pos[128];
 
 	if (colored) {
-		printf("ACARS mode: %s%c%s", RED,msg->mode,NONE);
+		printf("ACARS mode: %s%c%s", RED, msg->mode, NONE);
 		printf(" Aircraft reg: %s%s%s\n", GREEN,msg->addr,NONE);
-		printf("Message label: %s%s%s", CYAN,msg->label,NONE);
-		printf(" Block id: %s%d%s", RED,(int) msg->bid,NONE);
-		printf(" Msg. no: %s%s%s\n", BLUE,msg->no,NONE);
-		printf("Flight id: %s%s%s\n", MAGENTA,msg->fid,NONE);
-		printf("Message content:-\n%s%s%s", YELLOW,msg->txt,NONE);
+		printf("Message label: %s%s%s", CYAN, msg->label, NONE);
+		printf(" Block id: %s%d%s", RED, (int)msg->bid, NONE);
+		printf(" Msg. no: %s%s%s\n", BLUE, msg->no, NONE);
+		printf("Flight id: %s%s%s\n", MAGENTA, msg->fid, NONE);
+		printf("Message content:-\n%s%s%s", YELLOW, msg->txt, NONE);
 		if (posconv(msg->txt, msg->label, pos)==0) {
 			printf("\n%sAPRS : Addr:%s Fid:%s Lbl:%s pos:%s%s\n",
-				RED,msg->addr, msg->fid, msg->label, pos,NONE);
+				RED, msg->addr, msg->fid, msg->label, pos, NONE);
 		}
 	} else {
 		printf("ACARS mode: %c", msg->mode);
@@ -114,7 +114,7 @@ void print_mesg(msg_t * msg, int colored, int messgnumb)
 	fflush(stdout);
 }
 
-void print_proto(msg_t * msg)
+void print_proto(msg_t *msg)
 {
 	time_t t;
 	struct tm *tmp;
@@ -129,7 +129,8 @@ void print_proto(msg_t * msg)
 	printf("MSGNO: %s\n", msg->no);
 	printf("FLIGHTID: %s\n", msg->fid);
 	printf("CONTENT: %s\n.\n", msg->txt);
-    	if (posconv(msg->txt, msg->label, pos)==0) {
+
+	if (posconv(msg->txt, msg->label, pos)==0) {
 		printf("APRS-ADDR: %s\n", msg->addr);
 		printf("APRS-FID: %s\n", msg->fid);
 		printf("APRS-LABEL: %s\n", msg->label);
@@ -162,10 +163,10 @@ int main(int argc, char **argv)
 	msg_t msg[2];
 	int nbit[2] = {0, 0};
 	int nrbit[2] = {8, 8};
-	int nbch=0;
+	int nbch = 0;
 	int esel[2] = {1, 1};
-	short port=0;
-	int debug=0;
+	short port = 0;
+	int debug = 0;
 	int output = 0;
 	int i;
 	int colored = 0;
@@ -239,8 +240,6 @@ int main(int argc, char **argv)
 		int ind, len;
 
 		len = getsample(sample, 4096);
-		if (debug)
-			fprintf(stderr, "Got sample: %d\n", len);
 		if (len <= 0)
 			break;
 
@@ -248,10 +247,10 @@ int main(int argc, char **argv)
 			for (i = 0; i < nbch; i++, ind++) {
 				if (esel[i]) {
 					nbit[i] += getbit(sample[ind], &r[i],
-							0);
+							(i? 1 : 0));
 					if (nbit[i] >= nrbit[i]) {
 						nrbit[i] = getmesg(r[i],
-							       &msg[i], 0);
+							       &msg[i], (i? 1 : 0));
 						nbit[i] = 0;
 						if (nrbit[i] == 0) {
 							do_output(output,