Skip to content

Correct CRC and better error handling #41

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 62 additions & 33 deletions predict.c
Original file line number Diff line number Diff line change
Expand Up @@ -2179,41 +2179,57 @@ char *Abbreviate(char *string, int n)
return temp;
}

char KepCheck(char *line1, char *line2)
#include <stdbool.h>

static bool checksum_tle(const char *tle_0, const char *tle_1)
{
/* This function scans line 1 and line 2 of a NASA 2-Line element
set and returns a 1 if the element set appears to be valid or
a 0 if it does not. If the data survives this torture test,
it's a pretty safe bet we're looking at a valid 2-line
element set and not just some random text that might pass
as orbital data based on a simple checksum calculation alone. */
int i, check_a, check_b;

int x;
unsigned sum1, sum2;
if(strlen(tle_0) < 69)
{
return false;
}

/* Compute checksum for each line */
if(strlen(tle_1) < 69)
{
return false;
}

for (x=0, sum1=0, sum2=0; x<=67; sum1+=val[(int)line1[x]], sum2+=val[(int)line2[x]], x++);
check_a = 0;
check_b = 0;

/* Perform a "torture test" on the data */
for(i=0; i<68; i++)
{
if(isdigit(tle_0[i]))
{
check_a += tle_0[i] - '0';
}
else if(tle_0[i] == '-')
{
check_a += 1;
}

if(isdigit(tle_1[i]))
{
check_b += tle_1[i] - '0';
}
else if(tle_1[i] == '-')
{
check_b += 1;
}
}

x=(val[(int)line1[68]]^(sum1%10)) | (val[(int)line2[68]]^(sum2%10)) |
(line1[0]^'1') | (line1[1]^' ') | (line1[7]^'U') |
(line1[8]^' ') | (line1[17]^' ') | (line1[23]^'.') |
(line1[32]^' ') | (line1[34]^'.') | (line1[43]^' ') |
(line1[52]^' ') | (line1[61]^' ') | (line1[62]^'0') |
(line1[63]^' ') | (line2[0]^'2') | (line2[1]^' ') |
(line2[7]^' ') | (line2[11]^'.') | (line2[16]^' ') |
(line2[20]^'.') | (line2[25]^' ') | (line2[33]^' ') |
(line2[37]^'.') | (line2[42]^' ') | (line2[46]^'.') |
(line2[51]^' ') | (line2[54]^'.') | (line1[2]^line2[2]) |
(line1[3]^line2[3]) | (line1[4]^line2[4]) |
(line1[5]^line2[5]) | (line1[6]^line2[6]) |
(isdigit(line1[68]) ? 0 : 1) | (isdigit(line2[68]) ? 0 : 1) |
(isdigit(line1[18]) ? 0 : 1) | (isdigit(line1[19]) ? 0 : 1) |
(isdigit(line2[31]) ? 0 : 1) | (isdigit(line2[32]) ? 0 : 1);
if((check_a % 10) != (tle_0[68] - '0'))
{
return false;
}

if((check_b % 10) != (tle_1[68] - '0'))
{
return false;
}

return (x ? 0 : 1);
return true;
}

void InternalUpdate(int x)
Expand Down Expand Up @@ -2448,7 +2464,7 @@ char ReadTLE(char *line0, char *line1, char *line2)
a = ((la == 0) || (la >= sizeof(sat.name)));
b = ((lb == 0) || (lb >= sizeof(sat.line1)));
c = ((lc == 0) || (lc >= sizeof(sat.line2)));
d = !KepCheck(line1, line2);
d = !checksum_tle(line1, line2);
error_flags = (a << 3) | (b << 2) | (c << 1) | (d << 0);

if (error_flags == 0)
Expand Down Expand Up @@ -3462,7 +3478,7 @@ PyObject * PythonifyObservation(observation * obs) {
char load(PyObject *args)
{
//TODO: Not threadsafe, detect and raise warning?
int x;
int x, tle_error;
char *env=NULL;

/* Set up translation table for computing TLE checksums */
Expand All @@ -3481,9 +3497,22 @@ char load(PyObject *args)
return -1;
};

if (ReadTLE(tle0,tle1,tle2) != 0)
{
PyErr_SetString(PredictException, "Unable to process TLE");
tle_error = ReadTLE(tle0,tle1,tle2);

if (tle_error & 8) {
PyErr_SetString(PredictException, "Unable to process TLE: Name line missing or too long.");
return -1;
} else if (tle_error & 4) {
PyErr_SetString(PredictException, "Unable to process TLE: Line 1 missing or too long.");
return -1;
} else if (tle_error & 2) {
PyErr_SetString(PredictException, "Unable to process TLE: Line 2 missing or too long.");
return -1;
} else if (tle_error & 1) {
PyErr_SetString(PredictException, "Unable to process TLE: Checksum error.");
return -1;
} else if (tle_error != 0) {
PyErr_SetString(PredictException, "Unable to process TLE: Unknown error.");
return -1;
}

Expand Down