Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ICAO registration country flags #101

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
207 changes: 207 additions & 0 deletions country_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
enum blockSize {
BLK_1024 = 1024,
BLK_4096 = 4096,
BLK_32768 = 32768,
BLK_262144 = 262144,
BLK_1048576= 1048576
};


struct countryCodes {
char *code;
uint32_t len;
uint32_t icao_addr;
uint32_t icao_mask;
} countryList[] = {
{"ZW",BLK_1024,0x4000,0xfffc00},
{"BW",BLK_1024,0x30000,0xfffc00},
{"KM",BLK_1024,0x35000,0xfffc00},
{"GW",BLK_1024,0x48000,0xfffc00},
{"LS",BLK_1024,0x4a000,0xfffc00},
{"MV",BLK_1024,0x5a000,0xfffc00},
{"MR",BLK_1024,0x5e000,0xfffc00},
{"MU",BLK_1024,0x60000,0xfffc00},
{"QA",BLK_1024,0x6a000,0xfffc00},
{"SC",BLK_1024,0x74000,0xfffc00},
{"SL",BLK_1024,0x76000,0xfffc00},
{"SZ",BLK_1024,0x7a000,0xfffc00},
{"BJ",BLK_1024,0x94000,0xfffc00},
{"CV",BLK_1024,0x96000,0xfffc00},
{"DJ",BLK_1024,0x98000,0xfffc00},
{"ST",BLK_1024,0x9e000,0xfffc00},
{"BB",BLK_1024,0xaa000,0xfffc00},
{"BZ",BLK_1024,0xab000,0xfffc00},
{"VC",BLK_1024,0xbc000,0xfffc00},
{"AG",BLK_1024,0xca000,0xfffc00},
{"GD",BLK_1024,0xcc000,0xfffc00},
{"NA",BLK_1024,0x201000,0xfffc00},
{"ER",BLK_1024,0x202000,0xfffc00},
{"CY",BLK_1024,0x4c8000,0xfffc00},
{"LU",BLK_1024,0x4d0000,0xfffc00},
{"MT",BLK_1024,0x4d2000,0xfffc00},
{"MC",BLK_1024,0x4d4000,0xfffc00},
{"SM",BLK_1024,0x500000,0xfffc00},
{"AL",BLK_1024,0x501000,0xfffc00},
{"HR",BLK_1024,0x501c00,0xfffc00},
{"LV",BLK_1024,0x502c00,0xfffc00},
{"LT",BLK_1024,0x503c00,0xfffc00},
{"MD",BLK_1024,0x504c00,0xfffc00},
{"SK",BLK_1024,0x505c00,0xfffc00},
{"SI",BLK_1024,0x506c00,0xfffc00},
{"UZ",BLK_1024,0x507c00,0xfffc00},
{"BY",BLK_1024,0x510000,0xfffc00},
{"EE",BLK_1024,0x511000,0xfffc00},
{"MK",BLK_1024,0x512000,0xfffc00},
{"BA",BLK_1024,0x513000,0xfffc00},
{"GE",BLK_1024,0x514000,0xfffc00},
{"TJ",BLK_1024,0x515000,0xfffc00},
{"AM",BLK_1024,0x600000,0xfffc00},
{"AZ",BLK_1024,0x600800,0xfffc00},
{"KG",BLK_1024,0x601000,0xfffc00},
{"TM",BLK_1024,0x601800,0xfffc00},
{"BT",BLK_1024,0x680000,0xfffc00},
{"FM",BLK_1024,0x681000,0xfffc00},
{"MN",BLK_1024,0x682000,0xfffc00},
{"KZ",BLK_1024,0x683000,0xfffc00},
{"PW",BLK_1024,0x684000,0xfffc00},
{"OM",BLK_1024,0x70c000,0xfffc00},
{"BN",BLK_1024,0x895000,0xfffc00},
{"SB",BLK_1024,0x897000,0xfffc00},
{"00",BLK_1024,0x899000,0xfffc00},
{"MH",BLK_1024,0x900000,0xfffc00},
{"CK",BLK_1024,0x901000,0xfffc00},
{"WS",BLK_1024,0x902000,0xfffc00},
{"NR",BLK_1024,0xc8a000,0xfffc00},
{"LC",BLK_1024,0xc8c000,0xfffc00},
{"TO",BLK_1024,0xc8d000,0xfffc00},
{"KI",BLK_1024,0xc8e000,0xfffc00},
{"VU",BLK_1024,0xc90000,0xfffc00},
{"00",BLK_1024,0xf09000,0xfffc00},
{"MZ",BLK_4096,0x6000,0xfff000},
{"BI",BLK_4096,0x32000,0xfff000},
{"CM",BLK_4096,0x34000,0xfff000},
{"CG",BLK_4096,0x36000,0xfff000},
{"CI",BLK_4096,0x38000,0xfff000},
{"GA",BLK_4096,0x3e000,0xfff000},
{"ET",BLK_4096,0x40000,0xfff000},
{"GQ",BLK_4096,0x42000,0xfff000},
{"GH",BLK_4096,0x44000,0xfff000},
{"GN",BLK_4096,0x46000,0xfff000},
{"KE",BLK_4096,0x4c000,0xfff000},
{"LR",BLK_4096,0x50000,0xfff000},
{"MG",BLK_4096,0x54000,0xfff000},
{"MW",BLK_4096,0x58000,0xfff000},
{"ML",BLK_4096,0x5c000,0xfff000},
{"NE",BLK_4096,0x62000,0xfff000},
{"NG",BLK_4096,0x64000,0xfff000},
{"UG",BLK_4096,0x68000,0xfff000},
{"CF",BLK_4096,0x6c000,0xfff000},
{"RW",BLK_4096,0x6e000,0xfff000},
{"SN",BLK_4096,0x70000,0xfff000},
{"SO",BLK_4096,0x78000,0xfff000},
{"SD",BLK_4096,0x7c000,0xfff000},
{"TZ",BLK_4096,0x80000,0xfff000},
{"TD",BLK_4096,0x84000,0xfff000},
{"TG",BLK_4096,0x88000,0xfff000},
{"ZM",BLK_4096,0x8a000,0xfff000},
{"CD",BLK_4096,0x8c000,0xfff000},
{"AO",BLK_4096,0x90000,0xfff000},
{"GM",BLK_4096,0x9a000,0xfff000},
{"BF",BLK_4096,0x9c000,0xfff000},
{"BS",BLK_4096,0xa8000,0xfff000},
{"CO",BLK_4096,0xac000,0xfff000},
{"CR",BLK_4096,0xae000,0xfff000},
{"CU",BLK_4096,0xb0000,0xfff000},
{"SV",BLK_4096,0xb2000,0xfff000},
{"GT",BLK_4096,0xb4000,0xfff000},
{"GY",BLK_4096,0xb6000,0xfff000},
{"HT",BLK_4096,0xb8000,0xfff000},
{"HN",BLK_4096,0xba000,0xfff000},
{"JM",BLK_4096,0xbe000,0xfff000},
{"NI",BLK_4096,0xc0000,0xfff000},
{"PA",BLK_4096,0xc2000,0xfff000},
{"DO",BLK_4096,0xc4000,0xfff000},
{"TT",BLK_4096,0xc6000,0xfff000},
{"SR",BLK_4096,0xc8000,0xfff000},
{"IE",BLK_4096,0x4ca000,0xfff000},
{"IS",BLK_4096,0x4cc000,0xfff000},
{"AF",BLK_4096,0x700000,0xfff000},
{"BD",BLK_4096,0x702000,0xfff000},
{"MM",BLK_4096,0x704000,0xfff000},
{"KW",BLK_4096,0x706000,0xfff000},
{"LA",BLK_4096,0x708000,0xfff000},
{"NP",BLK_4096,0x70a000,0xfff000},
{"KH",BLK_4096,0x70e000,0xfff000},
{"YE",BLK_4096,0x890000,0xfff000},
{"BH",BLK_4096,0x894000,0xfff000},
{"AE",BLK_4096,0x896000,0xfff000},
{"PG",BLK_4096,0x898000,0xfff000},
{"FJ",BLK_4096,0xc88000,0xfff000},
{"CL",BLK_4096,0xe80000,0xfff000},
{"EC",BLK_4096,0xe84000,0xfff000},
{"PY",BLK_4096,0xe88000,0xfff000},
{"PE",BLK_4096,0xe8c000,0xfff000},
{"UY",BLK_4096,0xe90000,0xfff000},
{"BO",BLK_4096,0xe94000,0xfff000},
{"ZA",BLK_32768,0x8000,0xff8000},
{"EG",BLK_32768,0x10000,0xff8000},
{"LY",BLK_32768,0x18000,0xff8000},
{"MA",BLK_32768,0x20000,0xff8000},
{"TN",BLK_32768,0x28000,0xff8000},
{"DZ",BLK_32768,0xa0000,0xff8000},
{"MX",BLK_32768,0xd0000,0xff8000},
{"VE",BLK_32768,0xd8000,0xff8000},
{"AT",BLK_32768,0x440000,0xff8000},
{"BE",BLK_32768,0x448000,0xff8000},
{"BG",BLK_32768,0x450000,0xff8000},
{"DK",BLK_32768,0x458000,0xff8000},
{"FI",BLK_32768,0x460000,0xff8000},
{"GR",BLK_32768,0x468000,0xff8000},
{"HU",BLK_32768,0x470000,0xff8000},
{"NO",BLK_32768,0x478000,0xff8000},
{"NL",BLK_32768,0x480000,0xff8000},
{"PL",BLK_32768,0x488000,0xff8000},
{"PT",BLK_32768,0x490000,0xff8000},
{"CZ",BLK_32768,0x498000,0xff8000},
{"RO",BLK_32768,0x4a0000,0xff8000},
{"SE",BLK_32768,0x4a8000,0xff8000},
{"CH",BLK_32768,0x4b0000,0xff8000},
{"TR",BLK_32768,0x4b8000,0xff8000},
{"YU",BLK_32768,0x4c0000,0xff8000},
{"UA",BLK_32768,0x508000,0xff8000},
{"SA",BLK_32768,0x710000,0xff8000},
{"KR",BLK_32768,0x718000,0xff8000},
{"KP",BLK_32768,0x720000,0xff8000},
{"IQ",BLK_32768,0x728000,0xff8000},
{"IR",BLK_32768,0x730000,0xff8000},
{"IL",BLK_32768,0x738000,0xff8000},
{"JO",BLK_32768,0x740000,0xff8000},
{"LB",BLK_32768,0x748000,0xff8000},
{"MY",BLK_32768,0x750000,0xff8000},
{"PH",BLK_32768,0x758000,0xff8000},
{"PK",BLK_32768,0x760000,0xff8000},
{"SG",BLK_32768,0x768000,0xff8000},
{"LK",BLK_32768,0x770000,0xff8000},
{"SY",BLK_32768,0x778000,0xff8000},
{"TH",BLK_32768,0x880000,0xff8000},
{"VN",BLK_32768,0x888000,0xff8000},
{"ID",BLK_32768,0x8a0000,0xff8000},
{"NZ",BLK_32768,0xc80000,0xff8000},
{"00",BLK_32768,0xf00000,0xff8000},
{"IT",BLK_262144,0x300000,0xfc0000},
{"ES",BLK_262144,0x340000,0xfc0000},
{"FR",BLK_262144,0x380000,0xfc0000},
{"DE",BLK_262144,0x3c0000,0xfc0000},
{"GB",BLK_262144,0x400000,0xfc0000},
{"CN",BLK_262144,0x780000,0xfc0000},
{"AU",BLK_262144,0x7c0000,0xfc0000},
{"IN",BLK_262144,0x800000,0xfc0000},
{"JP",BLK_262144,0x840000,0xfc0000},
{"CA",BLK_262144,0xc00000,0xfc0000},
{"AR",BLK_262144,0xe00000,0xfc0000},
{"BR",BLK_262144,0xe40000,0xfc0000},
{"RU",BLK_1048576,0x100000,0xf00000},
{"US",BLK_1048576,0xa00000,0xf00000},
};

#define CTALEN() (sizeof(countryList) / sizeof(struct countryCodes))
88 changes: 60 additions & 28 deletions dump1090.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <sys/select.h>
#include "rtl-sdr.h"
#include "anet.h"
#include "country_list.h"

#define MODES_DEFAULT_RATE 2000000
#define MODES_DEFAULT_FREQ 1090000000
Expand Down Expand Up @@ -120,6 +121,7 @@ struct aircraft {
double lat, lon; /* Coordinated obtained from CPR encoded data. */
long long odd_cprtime, even_cprtime;
struct aircraft *next; /* Next aircraft in our linked list. */
struct countryCodes * reg; /* aircraft registretion country */
};

/* Program global state. */
Expand Down Expand Up @@ -1107,6 +1109,17 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) {
mm->phase_corrected = 0; /* Set to 1 by the caller if needed. */
}

/* Extract the country code from the ICAO adress. */
struct countryCodes * icaoToCountyCode(uint32_t addr)
{
unsigned int i;
for (i=0; i < CTALEN(); i++) {
if((addr & countryList[i].icao_mask) == countryList[i].icao_addr)
return &countryList[i];
}
return NULL;
}

/* This function gets a decoded Mode S Message and prints it on the screen
* in a human readable format. */
void displayModesMessage(struct modesMessage *mm) {
Expand Down Expand Up @@ -1582,6 +1595,7 @@ struct aircraft *interactiveCreateAircraft(uint32_t addr) {
a->lon = 0;
a->seen = time(NULL);
a->messages = 0;
a->reg = icaoToCountyCode(addr);
a->next = NULL;
return a;
}
Expand Down Expand Up @@ -1807,8 +1821,8 @@ void interactiveShowData(void) {

printf("\x1b[H\x1b[2J"); /* Clear the screen */
printf(
"Hex Flight Altitude Speed Lat Lon Track Messages Seen %s\n"
"--------------------------------------------------------------------------------\n",
"Hex Reg Flight Altitude Speed Lat Lon Track Messages Seen %s\n"
"-----------------------------------------------------------------------------------\n",
progress);

while(a && count < Modes.interactive_rows) {
Expand All @@ -1820,8 +1834,10 @@ void interactiveShowData(void) {
speed *= 1.852;
}

printf("%-6s %-8s %-9d %-7d %-7.03f %-7.03f %-3d %-9ld %d sec\n",
a->hexaddr, a->flight, altitude, speed,
printf("%-6s %-3s %-8s %-9d %-7d %-7.03f %-7.03f %-3d %-9ld %d sec\n",
a->hexaddr,
a->reg?a->reg->code:" ",
a->flight, altitude, speed,
a->lat, a->lon, a->track, a->messages,
(int)(now - a->seen));
a = a->next;
Expand Down Expand Up @@ -2152,10 +2168,12 @@ char *aircraftsToJson(int *len) {

if (a->lat != 0 && a->lon != 0) {
l = snprintf(p,buflen,
"{\"hex\":\"%s\", \"flight\":\"%s\", \"lat\":%f, "
"{\"hex\":\"%s\", \"reg\":\"%s\",\"flight\":\"%s\", \"lat\":%f, "
"\"lon\":%f, \"altitude\":%d, \"track\":%d, "
"\"speed\":%d},\n",
a->hexaddr, a->flight, a->lat, a->lon, a->altitude, a->track,
a->hexaddr,
a->reg?a->reg->code:"",
a->flight, a->lat, a->lon, a->altitude, a->track,
a->speed);
p += l; buflen -= l;
/* Resize if needed. */
Expand All @@ -2181,8 +2199,36 @@ char *aircraftsToJson(int *len) {
return buf;
}

/* Return contents of a file */
char *getFileContents(const char *filename, int *clen)
{
struct stat sbuf;
int fd = -1;
char *content;

if (stat(filename,&sbuf) != -1 &&
(fd = open(filename,O_RDONLY)) != -1)
{
content = malloc(sbuf.st_size);
if (read(fd,content,sbuf.st_size) == -1) {
snprintf(content,sbuf.st_size,"Error reading from file: %s",
strerror(errno));
}
*clen = sbuf.st_size;
} else {
char buf[128];

*clen = snprintf(buf,sizeof(buf),"Error opening file: %s",
strerror(errno));
content = strdup(buf);
}
if (fd != -1) close(fd);
return (content);
}

#define MODES_CONTENT_TYPE_HTML "text/html;charset=utf-8"
#define MODES_CONTENT_TYPE_JSON "application/json;charset=utf-8"
#define MODES_CONTENT_TYPE_JS "application/javascript;charset=utf-8"

/* Get an HTTP request header and write the response to the client.
* Again here we assume that the socket buffer is enough without doing
Expand Down Expand Up @@ -2223,33 +2269,19 @@ int handleHTTPRequest(struct client *c) {
printf("HTTP requested URL: %s\n\n", url);
}

/* Select the content to send, we have just two so far:
/* Select the content to send, we have three so far:
* "/" -> Our google map application.
* "/data.json" -> Our ajax request to update planes. */
* "/data.json" -> Our ajax request to update planes.
* "/flags.js" -> Javascript containing base64 encoded flags for each ICAO two character code. */
if (strstr(url, "/data.json")) {
content = aircraftsToJson(&clen);
ctype = MODES_CONTENT_TYPE_JSON;
} else if(strstr(url, "/flags.js")) {
/* Add 1 to strip off leading /, images are in flags subdirectory. */
content = getFileContents("flags.js", &clen);
ctype = MODES_CONTENT_TYPE_JS;
} else {
struct stat sbuf;
int fd = -1;

if (stat("gmap.html",&sbuf) != -1 &&
(fd = open("gmap.html",O_RDONLY)) != -1)
{
content = malloc(sbuf.st_size);
if (read(fd,content,sbuf.st_size) == -1) {
snprintf(content,sbuf.st_size,"Error reading from file: %s",
strerror(errno));
}
clen = sbuf.st_size;
} else {
char buf[128];

clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s",
strerror(errno));
content = strdup(buf);
}
if (fd != -1) close(fd);
content = getFileContents("gmap.html", &clen);
ctype = MODES_CONTENT_TYPE_HTML;
}

Expand Down
Loading