acarsdec

an ACARS decoder
git clone git://r-36.net/acarsdec
Log | Files | Refs | README

commit 1248597a6b7689167d766aa71e298a7c5c7aa70e
parent 62f33d87852de094a7ddc6da2656f80be6a87143
Author: Christoph Lohmann <20h@r-36.net>
Date:   Sat, 27 Oct 2012 07:09:35 +0200

Reformatting the serv.c file.

Diffstat:
serv.c | 471++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 239 insertions(+), 232 deletions(-)

diff --git a/serv.c b/serv.c @@ -1,8 +1,8 @@ /* * Copyright (c) 2007 by Thierry Leconte (F4DWV) - * (c) 2010 by Christoph Lohmann <20h@r-36.net> + * (c) 2010 by Christoph Lohmann <20h@r-36.net> * - * $Id: serv.c,v 1.2 2007/04/22 16:14:41 f4dwv Exp $ + * $Id: serv.c,v 1.2 2007/04/22 16:14:41 f4dwv Exp $ * * This code is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License version 2 @@ -34,278 +34,285 @@ static int sa, sc; int init_serv(short port) { - struct sockaddr_in locaddr, remaddr; - socklen_t len; - char c; - int res; - - sa = socket(PF_INET, SOCK_STREAM, 0); - if (sa < 0) { - fprintf(stderr, "socket : %s\n", strerror(errno)); - return -1; - } - - res = 1; - res = setsockopt(sa, SOL_SOCKET, SO_REUSEADDR, &res, sizeof(res)); - if (res) { - fprintf(stderr, "reuseaddr : %s\n", strerror(errno)); - return -1; - } - - memset(&locaddr, 0, sizeof(locaddr)); - locaddr.sin_family = AF_INET; - locaddr.sin_port = htons(port); - locaddr.sin_addr.s_addr = htonl(INADDR_ANY); - - len = sizeof(locaddr); - res = bind(sa, (struct sockaddr *) &locaddr, len); - if (res) { - fprintf(stderr, "bind : %s\n", strerror(errno)); - return -1; - } - - res = listen(sa, 1); - if (res) { - fprintf(stderr, "listen : %s\n", strerror(errno)); - return -1; - } - - memset(&remaddr, 0, sizeof(remaddr)); - len = sizeof(remaddr); - sc = accept(sa, (struct sockaddr *) &remaddr, &len); - if (sc < 0) { - fprintf(stderr, "accept : %s\n", strerror(errno)); - return -1; - } - - do { - res = read(sc, &c, 1); - } while (res == 1 && c != '\n'); - - - return 0; + struct sockaddr_in locaddr, remaddr; + socklen_t len; + char c; + int res; + + sa = socket(PF_INET, SOCK_STREAM, 0); + if (sa < 0) { + fprintf(stderr, "socket : %s\n", strerror(errno)); + return -1; + } + + res = 1; + res = setsockopt(sa, SOL_SOCKET, SO_REUSEADDR, &res, sizeof(res)); + if (res) { + fprintf(stderr, "reuseaddr : %s\n", strerror(errno)); + return -1; + } + + memset(&locaddr, 0, sizeof(locaddr)); + locaddr.sin_family = AF_INET; + locaddr.sin_port = htons(port); + locaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + len = sizeof(locaddr); + res = bind(sa, (struct sockaddr *) &locaddr, len); + if (res) { + fprintf(stderr, "bind : %s\n", strerror(errno)); + return -1; + } + + res = listen(sa, 1); + if (res) { + fprintf(stderr, "listen : %s\n", strerror(errno)); + return -1; + } + + memset(&remaddr, 0, sizeof(remaddr)); + len = sizeof(remaddr); + sc = accept(sa, (struct sockaddr *) &remaddr, &len); + if (sc < 0) { + fprintf(stderr, "accept : %s\n", strerror(errno)); + return -1; + } + + do { + res = read(sc, &c, 1); + } while (res == 1 && c != '\n'); + + + return 0; } /* convert ACARS position reports to APRS position */ static void toaprs(int la, char lac, int ln, char lnc, int prec, char *out) { - int lad, lnd; - float lam, lnm; + int lad, lnd; + float lam, lnm; - lad = la / 10000; - lnd = ln / 10000; - lam = (float) (la - (lad * 10000)) * 60.0 / 10000.0; - lnm = (float) (ln - (lnd * 10000)) * 60.0 / 10000.0; + lad = la / 10000; + lnd = ln / 10000; + lam = (float) (la - (lad * 10000)) * 60.0 / 10000.0; + lnm = (float) (ln - (lnd * 10000)) * 60.0 / 10000.0; - switch (prec) { + switch (prec) { case 0: - sprintf(out, "%02d%02.0f. %c/%03d%02.0f. %c^", lad, lam, lac, lnd, lnm, lnc); + sprintf(out, "%02d%02.0f. %c/%03d%02.0f. %c^", + lad, lam, lac, lnd, lnm, lnc); break; case 1: - sprintf(out, "%02d%04.1f %c/%03d%04.1f %c^", lad, lam, lac, lnd, lnm, lnc); + sprintf(out, "%02d%04.1f %c/%03d%04.1f %c^", + lad, lam, lac, lnd, lnm, lnc); break; case 2: default: - sprintf(out, "%02d%05.2f%c/%03d%05.2f%c^", lad, lam, lac, lnd, lnm, lnc); + sprintf(out, "%02d%05.2f%c/%03d%05.2f%c^", + lad, lam, lac, lnd, lnm, lnc); break; - } + } } int posconv(char *txt, unsigned char *label, char *pos) { - char lac, lnc; - int la, ln; - char las[7], lns[7]; - int n; - char *p; - -/*try different heuristics */ - - n = sscanf(txt, "#M1BPOS%c%05d%c%063d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - n = sscanf(txt, "#M1AAEP%c%06d%c%07d", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - toaprs(la, lac, ln, lnc, 2, pos); - return 0;; - } - - if (strncmp(txt, "#M1B", 4) == 0) { - if ((p = strstr(txt, "/FPO")) != NULL) { - n = sscanf(p, "/FPO%c%05d%c%06d", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') - && (lnc == 'E' || lnc == 'W')) { + char lac, lnc; + int la, ln; + char las[7], lns[7]; + int n; + char *p; + + /*try different heuristics */ + + n = sscanf(txt, "#M1BPOS%c%05d%c%063d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { la *= 10; ln *= 10; toaprs(la, lac, ln, lnc, 1, pos); return 0;; - } } - if ((p = strstr(txt, "/PS")) != NULL) { - n = sscanf(p, "/PS%c%05d%c%06d", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') - && (lnc == 'E' || lnc == 'W')) { + n = sscanf(txt, "#M1AAEP%c%06d%c%07d", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + toaprs(la, lac, ln, lnc, 2, pos); + return 0;; + } + + if (strncmp(txt, "#M1B", 4) == 0) { + if ((p = strstr(txt, "/FPO")) != NULL) { + n = sscanf(p, "/FPO%c%05d%c%06d", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') + && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + } + if ((p = strstr(txt, "/PS")) != NULL) { + n = sscanf(p, "/PS%c%05d%c%06d", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') + && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + } + } + + n = sscanf(txt, "FST01%*8s%c%06d%c%07d", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + toaprs(la, lac, ln, lnc, 2, pos); + return 0;; + } + + n = sscanf(txt, "(2%c%5c%c%6c", &lac, las, &lnc, lns); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + las[5] = 0; + lns[6] = 0; + la = 10 * atoi(las); + ln = 10 * atoi(lns); + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + n = sscanf(txt, "(:2%c%5c%c%6c", &lac, las, &lnc, lns); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + las[5] = 0; + lns[6] = 0; + la = 10 * atoi(las); + ln = 10 * atoi(lns); + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + + n = sscanf(txt, "(2%*4s%c%5c%c%6c", &lac, las, &lnc, lns); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + las[5] = 0; + lns[6] = 0; + la = 10 * atoi(las); + ln = 10 * atoi(lns); + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + n = sscanf(txt, "LAT %c%3c.%3c/LON %c%3c.%3c", &lac, las, &(las[3]), + &lnc, lns, &(lns[3])); + if (n == 6 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + las[6] = 0; + lns[6] = 0; + la = 10 * atoi(las); + ln = 10 * atoi(lns); + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + + n = sscanf(txt, "#DFB(POS-%*6s-%04d%c%05d%c/", &la, &lac, &ln, &lnc); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 100; + ln *= 100; + toaprs(la, lac, ln, lnc, 0, pos); + return 0;; + } + + n = sscanf(txt, "#DFB*POS\a%*8s%c%04d%c%05d/", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 100; + ln *= 100; + toaprs(la, lac, ln, lnc, 0, pos); + return 0;; + } + + n = sscanf(txt, "POS%c%05d%c%06d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + n = sscanf(txt, "POS%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + n = sscanf(txt, "RCL%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + n = sscanf(txt, "TWX%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { la *= 10; ln *= 10; toaprs(la, lac, ln, lnc, 1, pos); return 0;; - } } - } - - n = sscanf(txt, "FST01%*8s%c%06d%c%07d", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - toaprs(la, lac, ln, lnc, 2, pos); - return 0;; - } - - n = sscanf(txt, "(2%c%5c%c%6c", &lac, las, &lnc, lns); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - las[5] = 0; - lns[6] = 0; - la = 10 * atoi(las); - ln = 10 * atoi(lns); - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "(:2%c%5c%c%6c", &lac, las, &lnc, lns); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - las[5] = 0; - lns[6] = 0; - la = 10 * atoi(las); - ln = 10 * atoi(lns); - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - - n = sscanf(txt, "(2%*4s%c%5c%c%6c", &lac, las, &lnc, lns); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - las[5] = 0; - lns[6] = 0; - la = 10 * atoi(las); - ln = 10 * atoi(lns); - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "LAT %c%3c.%3c/LON %c%3c.%3c", &lac, las, &(las[3]), - &lnc, lns, &(lns[3])); - if (n == 6 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - las[6] = 0; - lns[6] = 0; - la = 10 * atoi(las); - ln = 10 * atoi(lns); - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - - n = sscanf(txt, "#DFB(POS-%*6s-%04d%c%05d%c/", &la, &lac, &ln, &lnc); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 100; - ln *= 100; - toaprs(la, lac, ln, lnc, 0, pos); - return 0;; - } - - n = sscanf(txt, "#DFB*POS\a%*8s%c%04d%c%05d/", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 100; - ln *= 100; - toaprs(la, lac, ln, lnc, 0, pos); - return 0;; - } - - n = sscanf(txt, "POS%c%05d%c%06d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "POS%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "RCL%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "TWX%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "CLA%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - n = sscanf(txt, "%c%05d/%c%06d,", &lac, &la, &lnc, &ln); - if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { - la *= 10; - ln *= 10; - toaprs(la, lac, ln, lnc, 1, pos); - return 0;; - } - - return 1; + + n = sscanf(txt, "CLA%*2s,%c%05d%c%06d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + n = sscanf(txt, "%c%05d/%c%06d,", &lac, &la, &lnc, &ln); + if (n == 4 && (lac == 'N' || lac == 'S') && (lnc == 'E' || lnc == 'W')) { + la *= 10; + ln *= 10; + toaprs(la, lac, ln, lnc, 1, pos); + return 0;; + } + + return 1; } int send_mesg(msg_t * msg) { - char apstr[512]; - char txt[512]; - char pos[64]; - unsigned char *ind; + char apstr[512]; + char txt[512]; + char pos[64]; + unsigned char *ind; - if(msg->label[0]=='_' && msg->label[1]==0x7f) - return 0; + if(msg->label[0]=='_' && msg->label[1]==0x7f) + return 0; - strcpy(txt,msg->txt); - for(ind = (unsigned char *)&txt; *ind != 0 ;ind++) { - if(*ind==0x0a || *ind == 0x0d) *ind=' '; - } + strcpy(txt,msg->txt); + for(ind = (unsigned char *)&txt; *ind != 0 ;ind++) { + if(*ind==0x0a || *ind == 0x0d) *ind=' '; + } - ind = msg->addr; - while (*ind == '.' && *ind != 0) - ind++; + ind = msg->addr; + while (*ind == '.' && *ind != 0) + ind++; - if (posconv(msg->txt, msg->label, pos)) - sprintf(apstr, "%s>ACARS:>Fid:%s Lbl:%s %s\n", ind, msg->fid,msg->label,txt); - else - sprintf(apstr, "%s>ACARS:!%sFid:%s Lbl:%s %s\n", ind, pos,msg->fid,msg->label,txt); + if (posconv(msg->txt, msg->label, pos)) { + sprintf(apstr, "%s>ACARS:>Fid:%s Lbl:%s %s\n", + ind, msg->fid,msg->label,txt); + } else { + sprintf(apstr, "%s>ACARS:!%sFid:%s Lbl:%s %s\n", + ind, pos,msg->fid,msg->label,txt); + } - write(sc, apstr, strlen(apstr)); + write(sc, apstr, strlen(apstr)); - return 0; + return 0; } void end_serv(void) { - close(sc); - close(sa); + close(sc); + close(sa); } +