Skip to content

Commit

Permalink
add sbs out
Browse files Browse the repository at this point in the history
  • Loading branch information
TLeconte committed Feb 28, 2023
1 parent 214cad1 commit 7926dc0
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 84 deletions.
3 changes: 2 additions & 1 deletion acars.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef struct {
char gin[5];
char woff[5];
char won[5];
// not really oooi
// not really oooi
float lon,lat;
int epu;
int alt;
Expand All @@ -48,6 +48,7 @@ struct flight_s {
uint32_t addr;
char reg[9];
char fid[7];
int gnd;
struct timeval ts,tl;
int nbm;
int rt,gt;
Expand Down
10 changes: 7 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ int jsonout = 0;
int routeout = 0;
int regout = 0;

char *netOutputRawaddr = NULL;
char *netOutJsonAddr = NULL;
char *netOutSbsAddr = NULL;

char *idstation = NULL ;
FILE *logfd;
Expand Down Expand Up @@ -121,7 +122,7 @@ int main(int argc, char **argv)
nbch = 0;
logfd = stdout;

while ((c = getopt(argc, argv, "vqrp:g:k:l:JRj:i:GEUb:a")) != EOF) {
while ((c = getopt(argc, argv, "vqrp:g:k:l:JRj:s:i:GEUb:a")) != EOF) {
switch (c) {
case 'v':
verbose = 2;
Expand Down Expand Up @@ -156,7 +157,10 @@ int main(int argc, char **argv)
break;
#endif
case 'j':
netOutputRawaddr = optarg;
netOutJsonAddr = optarg;
break;
case 's':
netOutSbsAddr = optarg;
break;
case 'J':
jsonout = 1;
Expand Down
148 changes: 102 additions & 46 deletions out.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,41 @@ extern int jsonout;
extern int routeout;
extern int regout;
extern int verbose;
extern char *netOutputRawaddr;

extern char *netOutJsonAddr;
extern char *netOutSbsAddr;

extern int outxid(flight_t *fl, unsigned char *p, int len);
extern int outacars(flight_t *fl, unsigned char *txt, int len);

cJSON *json_obj=NULL;
static int sockfd=-1;
static const int mdly=1800;

static int sock_json=-1;
static int sock_sbs=-1;


static char *outbuff=NULL,*ptroutbuff;

int initNetOutput(char *Rawaddr)
static int initNetOutput(int json_sbs)
{
char *raddr,*addr;
char *port;
struct addrinfo hints, *servinfo, *p;
int rv;

netOutputRawaddr = Rawaddr;
raddr=strdup(Rawaddr);
int sockfd;

memset(&hints, 0, sizeof hints);

if(json_sbs) {
if(netOutJsonAddr==NULL) return -1;
raddr=strdup(netOutJsonAddr);
hints.ai_socktype = SOCK_DGRAM;
} else {
if(netOutSbsAddr==NULL) return -1;
raddr=strdup(netOutSbsAddr);
hints.ai_socktype = SOCK_STREAM;
}

if (raddr[0] == '[') {
hints.ai_family = AF_INET6;
addr = raddr + 1;
Expand All @@ -86,13 +99,14 @@ int initNetOutput(char *Rawaddr)
}
}

hints.ai_socktype = SOCK_DGRAM;

if ((rv = getaddrinfo(addr, port, &hints, &servinfo)) != 0) {
fprintf(stderr, "Invalid/unknown address %s\n", addr);
free(raddr);
return -1;
}

sockfd=-1;
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
Expand All @@ -101,7 +115,6 @@ int initNetOutput(char *Rawaddr)
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
if(verbose > 1) fprintf(stderr, "failed to connect\n");
close(sockfd);
sockfd=-1;
continue;
}
break;
Expand All @@ -110,33 +123,73 @@ int initNetOutput(char *Rawaddr)
free(raddr);
freeaddrinfo(servinfo);

if (sockfd == -1 ) {
return -1;
}
if(json_sbs)
sock_json=sockfd;
else
sock_sbs=sockfd;

return 0;
if (sockfd == -1 )
return -1;
else
return 0;
}

static int Netwrite(const void *buf, size_t count) {
static int Netwrite(const void *buf, size_t count,int json_sbs) {
int res;
int *sockfd;

if(json_sbs)
sockfd=&sock_json;
else
sockfd=&sock_sbs;

if(sockfd == -1) {
if (!netOutputRawaddr) {
return -1;
}
initNetOutput(netOutputRawaddr);
if(*sockfd == -1) {
initNetOutput(json_sbs);
}
if(sockfd == -1) return -1;
if(*sockfd == -1) return -1;

res = write(sockfd, buf, count);
res = write(*sockfd, buf, count);
if (res != count) {
close(sockfd);
sockfd = -1;
close(*sockfd);
*sockfd=-1;
}
return res;
}

static void outsbs(msgblk_t * blk,flight_t *fl)
{
char outbuff[1024],*p;
struct timespec now;
struct tm stTime_receive, stTime_now;

if(fl->reg[0]==0 && fl->oooi.epu==0) return;

clock_gettime(CLOCK_REALTIME, &now);
gmtime_r(&now.tv_sec, &stTime_now);
gmtime_r(&blk->tv.tv_sec, &stTime_receive);

p=outbuff;

if(fl->oooi.epu) {
p+=sprintf(outbuff, "MSG,3,1,1,%06X,1,", fl->addr&0Xffffff);
} else {
p+=sprintf(outbuff, "MSG,1,1,1,%06X,1,", fl->addr&0Xffffff);
}
p += sprintf(p, "%04d/%02d/%02d,", (stTime_receive.tm_year + 1900), (stTime_receive.tm_mon + 1), stTime_receive.tm_mday);
p += sprintf(p, "%02d:%02d:%02d.%03u,", stTime_receive.tm_hour, stTime_receive.tm_min, stTime_receive.tm_sec, (unsigned) (blk->tv.tv_usec/1000));
p += sprintf(p, "%04d/%02d/%02d,", (stTime_now.tm_year + 1900), (stTime_now.tm_mon + 1), stTime_now.tm_mday);
p += sprintf(p, "%02d:%02d:%02d.%03u", stTime_now.tm_hour, stTime_now.tm_min, stTime_now.tm_sec, (unsigned) (now.tv_nsec / 1000000U));
if(fl->reg[0]) p += sprintf(p, ",%s", fl->reg); else p += sprintf(p, ",");
if(fl->oooi.alt) p += sprintf(p, ",%d", fl->oooi.alt); else p += sprintf(p, ",");
p += sprintf(p, ",,");
if(fl->oooi.epu) p += sprintf(p, ",%1.6f,%1.6f", fl->oooi.lat, fl->oooi.lon); else p += sprintf(p, ",,");
p += sprintf(p, ",,,,,,");
if(fl->gnd) p += sprintf(p, "-1");
p += sprintf(p, "\r\n");
Netwrite(outbuff, strlen(outbuff),0);
//printf("%s",outbuff);
//fflush(stdout);
}

static void outjson()
{
Expand All @@ -158,9 +211,9 @@ static void outjson()
fflush(logfd);
}

if (netOutputRawaddr) {
ok=Netwrite(jsonbuf, lp+1);
}
if (netOutJsonAddr) {
Netwrite(jsonbuf, lp+1,1);
}
}

static void buildjsonobj(unsigned int faddr,unsigned int taddr,int fromair,int isresponse,int isonground,msgblk_t * blk)
Expand Down Expand Up @@ -204,6 +257,7 @@ static flight_t *addFlight(uint32_t addr, struct timeval tv)
{
static flight_t *flight_head=NULL;
flight_t *fl,*fld,*flp;
const static int mdly=1800;

/* find aircraft */
fl=flight_head;
Expand All @@ -224,6 +278,7 @@ static flight_t *addFlight(uint32_t addr, struct timeval tv)
}

fl->tl=tv;
fl->oooi.epu=fl->oooi.alt=0;
fl->nbm+=1;

if(flp) {
Expand Down Expand Up @@ -328,7 +383,6 @@ void vout(char *format, ...)

}


void dumpdata(unsigned char *p, int len)
{
int i, k;
Expand Down Expand Up @@ -432,20 +486,20 @@ static void outlinkctrl(unsigned char lc, int rep)

vout( "Frame-");
if (lc & 1) {
if (lc & 2) {
vout( "U: ");
vout( "%s\n",
Ufrm[rep][((lc >> 3) & 0x1c) |
((lc >> 2) & 0x3)]);
} else {
vout( "S: ");
vout( "Nr:%01d %s\n", (lc >> 5) & 0x7,
Sfrm[(lc >> 2) & 0x3]);
}
if (lc & 2) {
vout( "U: ");
vout( "%s\n",
Ufrm[rep][((lc >> 3) & 0x1c) |
((lc >> 2) & 0x3)]);
} else {
vout( "S: ");
vout( "Nr:%01d %s\n", (lc >> 5) & 0x7,
Sfrm[(lc >> 2) & 0x3]);
}
} else {
vout( "I: ");
vout( "Ns:%01d Nr:%01d\n", (lc >> 1) & 0x7,
(lc >> 5) & 0x7);
vout( "I: ");
vout( "Ns:%01d Nr:%01d\n", (lc >> 1) & 0x7,
(lc >> 5) & 0x7);
}
}

Expand All @@ -460,11 +514,9 @@ static void printdate(struct timeval tv)
tmp.tm_hour, tmp.tm_min, tmp.tm_sec,(int)tv.tv_usec/1000);
}


void out(msgblk_t * blk, unsigned char *hdata, int l)
{
int rep = (hdata[5] & 2) >> 1;
int gnd = hdata[1] & 2;
unsigned int faddr,taddr;
int dec,fromair;
flight_t *fl=NULL;
Expand All @@ -474,12 +526,15 @@ void out(msgblk_t * blk, unsigned char *hdata, int l)

fromair=((faddr >> 24)==1);

/* filter bad message */
if(!grndmess && !fromair) return;
if(!emptymess && l<=13) return;
if(!undecmess && fromair && ( (faddr & 0xffffff) == 0 || (faddr & 0xffffff) == 0xffffff)) return;

if(fromair)
if(fromair) {
fl=addFlight(faddr,blk->tv);
fl->gnd = hdata[1] & 2;
}

if(verbose) {

Expand All @@ -491,15 +546,15 @@ void out(msgblk_t * blk, unsigned char *hdata, int l)

vout( "%s from ", rep ? "Response" : "Command");
outaddr(faddr);
vout( "(%s) to ", gnd ? "on ground" : "airborne");
vout( "(%s) to ", fl->gnd ? "on ground" : "airborne");
outaddr(taddr);
vout( "\n");

outlinkctrl(hdata[9], rep);
}

if((jsonout || netOutputRawaddr) && !routeout)
buildjsonobj(faddr,taddr,fromair,rep,gnd,blk);
if((jsonout || netOutJsonAddr) && !routeout)
buildjsonobj(faddr,taddr,fromair,rep,fl->gnd,blk);

dec=0;

Expand All @@ -526,6 +581,7 @@ void out(msgblk_t * blk, unsigned char *hdata, int l)
if(fl) {
if(routeout) routejson(fl,blk->tv);
if(regout) airreg(fl,blk->tv);
if (netOutSbsAddr) outsbs(blk,fl);
}

if(json_obj)
Expand Down
6 changes: 6 additions & 0 deletions outacars.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ int outacars(flight_t *fl,unsigned char *txt, int len)
if(oooi.gin[0]) memcpy(fl->oooi.gin,oooi.gin,5);
if(oooi.woff[0]) memcpy(fl->oooi.woff,oooi.woff,5);
if(oooi.won[0]) memcpy(fl->oooi.won,oooi.won,5);
if(oooi.epu) {
fl->oooi.epu=oooi.epu;
fl->oooi.lat=oooi.lat;
fl->oooi.lat=oooi.lon;
}
fl->oooi.alt=oooi.alt;
}


Expand Down
Loading

0 comments on commit 7926dc0

Please sign in to comment.