Skip to content

Commit

Permalink
Fix the help texts and the NTP server module
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fb-martin committed Nov 11, 2019
1 parent 3149ae9 commit a3eae28
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 61 deletions.
27 changes: 17 additions & 10 deletions hc_broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,38 @@ void hc_broadcast_send (const char *data, int length) {
}


const char *hc_broadcast_format (struct sockaddr_in *addr) {
static char formatted[80];
snprintf (formatted, sizeof(formatted), "%d.%d.%d.%d:%d",
addr->sin_addr.s_addr & 0xff,
(addr->sin_addr.s_addr >> 8) & 0xff,
(addr->sin_addr.s_addr >> 16) & 0xff,
(addr->sin_addr.s_addr >> 24) & 0xff,
addr->sin_port);
return formatted;
}

void hc_broadcast_reply
(const char *data, int length, in_addr_t destination) {
(const char *data, int length, struct sockaddr_in *destination) {

if (udpserver < 0) return;
netaddress.sin_addr.s_addr = destination;

sendto (udpserver, data, length, 0,
(struct sockaddr *)&netaddress, sizeof(netaddress));
(struct sockaddr *)destination, sizeof(struct sockaddr_in));
}

int hc_broadcast_receive (char *buffer, int size, in_addr_t *source) {
int hc_broadcast_receive (char *buffer, int size,
struct sockaddr_in *source) {

int length;
struct sockaddr_in srcaddr;
socklen_t srclength = sizeof(srcaddr);
socklen_t srclength = sizeof(struct sockaddr_in);


if (udpserver < 0) return 0;

length = recvfrom (udpserver, buffer, size, 0,
(struct sockaddr *)&srcaddr, &srclength);
(struct sockaddr *)source, &srclength);

if (length > 0) {
*source = srcaddr.sin_addr.s_addr;
}
return length;
}

6 changes: 4 additions & 2 deletions hc_broadcast.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ int hc_broadcast_open (const char *service);
void hc_broadcast_send (const char *data, int length);

void hc_broadcast_reply
(const char *data, int length, in_addr_t destination);
(const char *data, int length, struct sockaddr_in *destination);

int hc_broadcast_receive
(char *buffer, int size, in_addr_t *source);
(char *buffer, int size, struct sockaddr_in *source);

const char *hc_broadcast_format (struct sockaddr_in *addr);

30 changes: 23 additions & 7 deletions hc_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static int clockPrecision;
static int clockShowDrift = 0;

static int clockSynchronized = 0;
static struct timeval clockReference = {0, 0};

static hc_clock_status *hc_clock_status_db = 0;
static int *hc_clock_drift_db = 0;
Expand Down Expand Up @@ -122,7 +123,7 @@ void hc_clock_initialize (int argc, const char **argv) {
}

static void hc_clock_force (const struct timeval *gps,
const struct timeval *local) {
const struct timeval *local, int latency) {

struct timeval now;
struct timeval corrected = *gps;
Expand All @@ -133,7 +134,7 @@ static void hc_clock_force (const struct timeval *gps,
// current time, as estimated using the local clock.
//
corrected.tv_sec += (now.tv_sec - local->tv_sec);
corrected.tv_usec += (now.tv_usec - local->tv_usec);
corrected.tv_usec += (now.tv_usec - local->tv_usec) + (latency * 1000);
if (corrected.tv_usec > 1000000) {
corrected.tv_sec += 1;
corrected.tv_usec -= 1000000;
Expand All @@ -149,6 +150,7 @@ static void hc_clock_force (const struct timeval *gps,
if (settimeofday (&corrected, NULL) != 0) {
printf ("settimeofday() error %d\n", errno);
}
clockReference = now;
}

static void hc_clock_adjust (time_t drift) {
Expand All @@ -160,11 +162,14 @@ static void hc_clock_adjust (time_t drift) {
if (adjtime (&delta, NULL) != 0) {
printf ("adjtime() error %d\n", errno);
}
gettimeofday (&clockReference, NULL);
}

void hc_clock_synchronize(const struct timeval *gps,
const struct timeval *local, int latency) {

static int FirstCall = 1;

if (hc_clock_drift_db == 0) return;
if (hc_clock_status_db == 0) return;

Expand All @@ -179,13 +184,21 @@ void hc_clock_synchronize(const struct timeval *gps,

if (clockShowDrift || hc_test_mode()) {
printf ("[%d] %8.3f\n", local->tv_sec%120, drift/1000.0);
if (hc_test_mode()) return;
if (hc_test_mode()) {
if (absdrift < clockPrecision) {
clockSynchronized = hc_clock_status_db->synchronized = 1;
} else {
clockSynchronized = hc_clock_status_db->synchronized = 0;
}
return;
}
}

if (absdrift >= 10000) {
if (FirstCall || absdrift >= 10000) {
// Too much of a difference: force system time.
hc_clock_force (gps, local);
hc_clock_force (gps, local, latency);
hc_clock_start_learning();
FirstCall = 0;
return;
}

Expand All @@ -205,8 +218,7 @@ void hc_clock_synchronize(const struct timeval *gps,
printf ("Average drift: %d ms\n", drift);

if (absdrift < clockPrecision) {
clockSynchronized = 1;
hc_clock_status_db->synchronized = 1;
clockSynchronized = hc_clock_status_db->synchronized = 1;
} else {
// GPS and local system time have drifted apart
// by a small difference: adjust the time progressively.
Expand All @@ -225,3 +237,7 @@ int hc_clock_synchronized (void) {
return clockSynchronized;
}

void hc_clock_reference (struct timeval *reference) {
*reference = clockReference;
}

1 change: 1 addition & 0 deletions hc_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void hc_clock_initialize (int argc, const char **argv);
void hc_clock_synchronize (const struct timeval *gps,
const struct timeval *local, int latency);
int hc_clock_synchronized (void);
void hc_clock_reference (struct timeval *reference);

/* Live database.
*/
Expand Down
17 changes: 10 additions & 7 deletions hc_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static const char *hc_http_status (const char *method, const char *uri,
static hc_clock_status *clock_db = 0;
char latitude[20];
char longitude[20];
const char *date = "010100";

if (nmea_db == 0) {
nmea_db = (hc_nmea_status *) hc_http_attach (HC_NMEA_STATUS);
Expand Down Expand Up @@ -102,20 +103,18 @@ static const char *hc_http_status (const char *method, const char *uri,
nmea_db->longitude, nmea_db->hemisphere[1]);
}

if (nmea_db->date[0] > 0) date = nmea_db->date;

snprintf (JsonBuffer, sizeof(JsonBuffer),
"{\"gps\":{\"fix\":%s"
",\"time\":[%c%c,%c%c,%c%c],\"date\":[%d,%c%c,%c%c]"
",\"time\":\"%s\",\"date\":\"%4d%2.2s%2.2s\""
",\"latitude\":%s,\"longitude\":%s}"
",\"clock\":{\"synchronized\":%s"
",\"precision\":%d,\"drift\":%d,\"timestamp\":%zd.%03d}"
",\"learn\":{\"count\":%d,\"accumulator\":%d}}",
nmea_db->fix?"true":"false",
nmea_db->time[0], nmea_db->time[1],
nmea_db->time[2], nmea_db->time[3],
nmea_db->time[4], nmea_db->time[5],
2000 + (nmea_db->date[4]-'0')*10 + (nmea_db->date[5]-'0'),
nmea_db->date[2], nmea_db->date[3],
nmea_db->date[0], nmea_db->date[1],
nmea_db->time,
2000 + (date[4]-'0')*10 + (date[5]-'0'), date+2, date,
latitude, longitude,
clock_db->synchronized?"true":"false",
clock_db->precision,
Expand Down Expand Up @@ -160,6 +159,10 @@ static const char *hc_http_clockdrift (const char *method, const char *uri,
return JsonBuffer;
}

const char *hc_http_help (int level) {
return echttp_help(level);
}

void hc_http (int argc, const char **argv) {
parent = getppid();
if (echttp_open (argc, argv) <= 0) exit(1);
Expand Down
2 changes: 2 additions & 0 deletions hc_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
const char *hc_http_help (int level);

void hc_http (int argc, const char **argv);

8 changes: 4 additions & 4 deletions hc_nmea.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ const char *hc_nmea_help (int level) {

static const char *nmeaHelp[] = {
" [-gps=DEV] [-latency=N] [-burst] [-privacy]",
"-gps=DEV: TTY device from which to read the NMEA data.\n"
"-latency=N: delay between the GPS fix and the 1st NMEA sentence.\n"
"-show-nmea: trace NMEA sentences.\n"
"-burst: Use burst start as the GPS timing reference\n"
"-gps=DEV: TTY device from which to read the NMEA data.",
"-latency=N: delay between the GPS fix and the 1st NMEA sentence.",
"-show-nmea: trace NMEA sentences.",
"-burst: Use burst start as the GPS timing reference",
"-privacy: do not export location",
NULL
};
Expand Down
Loading

0 comments on commit a3eae28

Please sign in to comment.