Skip to content
Open
Show file tree
Hide file tree
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
94 changes: 32 additions & 62 deletions Firmware/src/ntp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef struct{
uint8_t vn:3; // vn. Three bits. Version number of the protocol.
uint8_t li:2; // li. Two bits. Leap indicator.
}ntp_flags_t;

typedef union {
uint32_t data;
uint8_t byte[4];
Expand All @@ -24,12 +24,12 @@ typedef struct
ntp_flags_t flags;
uint8_t stratum; // Eight bits. Stratum level of the local clock.
uint8_t poll; // Eight bits. Maximum interval between successive messages.
uint8_t precision; // Eight bits. Precision of the local clock.
int8_t precision; // Eight bits signed. Precision of the local clock.

uint32_t rootDelay; // 32 bits. Total round trip delay time.
uint32_t rootDispersion; // 32 bits. Max error aloud from primary clock source.
uint32_t rootDispersion; // 32 bits. Max error allowed from primary clock source.
refID_t refId; // 32 bits. Reference clock identifier.


uint32_t refTm_s; // 32 bits. Reference time-stamp seconds.
uint32_t refTm_f; // 32 bits. Reference time-stamp fraction of a second.
Expand All @@ -43,7 +43,7 @@ typedef struct
uint32_t txTm_s; // 32 bits and the most important field the client cares about. Transmit time-stamp seconds.
uint32_t txTm_f; // 32 bits. Transmit time-stamp fraction of a second.

} ntp_packet_t;
} ntp_packet_t;

uint8_t __calloverhead = 0;

Expand All @@ -52,7 +52,7 @@ uint32_t(*fnc_read_utc)(void) = NULL;
uint32_t(*fnc_read_subsecond)(void) = NULL;

NTP_Server::NTP_Server( ){

}

NTP_Server::~NTP_Server(){
Expand All @@ -75,14 +75,14 @@ uint8_t DeterminePrecision( void ){
double runtime = ( ((double)(end)-(double)(start) ) / ( (double)(1000000.0) * (double)(1024) ) )+((double)(1)) ; // One second as we don't keep track on the fractions
__calloverhead = log2(runtime);


} else {
/* this is a bad one ! */
configASSERT( fnc_read_utc != NULL );
}
return 0;


}

bool NTP_Server::begin(uint16_t port , uint32_t(*fnc_getutc_time)(void) , uint32_t(*fnc_get_subsecond)(void) ){
Expand Down Expand Up @@ -118,11 +118,11 @@ void NTP_Server::processUDPPacket(AsyncUDPPacket& packet) {
uint32_t processing_start = 0;
uint32_t millisec = 0;
ntp_packet_t ntp_req;

if(NULL != fnc_read_subsecond){
millisec = fnc_read_subsecond();
}


if(fnc_read_utc!=NULL){
processing_start=fnc_read_utc()+NTP_TIMESTAMP_DELTA;
Expand All @@ -133,67 +133,37 @@ void NTP_Server::processUDPPacket(AsyncUDPPacket& packet) {
/* this is not what we want ! */
return;
}

memcpy(&ntp_req, packet.data(), sizeof(ntp_packet_t));

/* Next is to swap the data arround */

ntp_req.rootDelay = ntohl( ntp_req.rootDelay );
ntp_req.rootDispersion = ntohl( ntp_req.rootDispersion );
ntp_req.refId.data = ntohl( ntp_req.refId.data );

ntp_req.refTm_s = ntohl(ntp_req.refTm_s );
ntp_req.refTm_f = ntohl(ntp_req.refTm_f );

ntp_req.origTm_s = ntohl( ntp_req.txTm_s );
ntp_req.origTm_f = ntohl( ntp_req.txTm_f );


ntp_req.rxTm_s = ntohl( ntp_req.rxTm_s );
ntp_req.rxTm_f = ntohl( ntp_req.rxTm_f );

ntp_req.txTm_s = ntohl( ntp_req.txTm_s );
ntp_req.txTm_f = ntohl( ntp_req.txTm_f );

ntp_req.flags.li = 0; //NONE
ntp_req.flags.vn = 4; // NTP Version 4
ntp_req.flags.mode = 4; // Server
ntp_req.stratum = 1;
// We don't touch ntp_req.poll

strncpy(ntp_req.refId.c_str ,"PPS", sizeof(ntp_req.refId.c_str) );
ntp_req.stratum = 1;
// We don't touch ntp_req.poll
ntp_req.precision = __calloverhead;
ntp_req.rootDelay=1;
ntp_req.rootDispersion=1;
/* We don't touch the originate timestamp */

ntp_req.rxTm_s= processing_start;
ntp_req.rxTm_f= 0;
/* UNIX Start is 1.1.1970 and GPS Start is 1.1.1900 */
//ntp_req.refTm_s= ntp_req.refTm_s + NTP_TIMESTAMP_DELTA; // We need to add 70 Years
ntp_req.refTm_s = processing_start;
ntp_req.refTm_f = 0;

ntp_req.txTm_s = fnc_read_utc()+NTP_TIMESTAMP_DELTA;
ntp_req.txTm_f = 0;

ntp_req.rootDelay = htonl( ntp_req.rootDelay );
ntp_req.rootDelay=1;
ntp_req.rootDispersion=1;
strncpy(ntp_req.refId.c_str ,"PPS", sizeof(ntp_req.refId.c_str) );
// Set to network byte order
ntp_req.rootDelay = htonl( ntp_req.rootDelay );
ntp_req.rootDispersion = htonl( ntp_req.rootDispersion );

ntp_req.refTm_s = htonl(ntp_req.refTm_s );
ntp_req.refTm_f = htonl(ntp_req.refTm_f );
ntp_req.origTm_s = htonl( ntp_req.origTm_s );
ntp_req.origTm_f = htonl( ntp_req.origTm_f );

ntp_req.rxTm_s = htonl( ntp_req.rxTm_s );
//ntp_req.rxTm_f = htonl( ntp_req.rxTm_f );
ntp_req.txTm_s = htonl( ntp_req.txTm_s );
ntp_req.rxTm_f = htonl(millisec * 4294967);
ntp_req.txTm_f = htonl(millisec * 4294967);
ntp_req.refId.data = ntohl( ntp_req.refId.data );

ntp_req.origTm_s = ntp_req.txTm_s;
ntp_req.origTm_f = ntp_req.txTm_f;

ntp_req.txTm_f = htonl( ntp_req.txTm_f );
ntp_req.rxTm_s = htonl(processing_start);
ntp_req.rxTm_f = htonl(millisec * 4294967);

ntp_req.refTm_s = ntp_req.rxTm_s;
ntp_req.refTm_f = ntp_req.rxTm_f;

/* UNIX Start is 1.1.1970 and GPS Start is 1.1.1900 */
ntp_req.txTm_s = fnc_read_utc()+NTP_TIMESTAMP_DELTA; // We need to add 70 Years
ntp_req.txTm_s = htonl(ntp_req.txTm_s);
ntp_req.txTm_f = htonl(millisec * 4294967);

packet.write((uint8_t*)&ntp_req, sizeof(ntp_packet_t));


}
3 changes: 2 additions & 1 deletion Firmware/src/ntp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class NTP_Server {
public:
NTP_Server( );
~NTP_Server();

bool begin(uint16_t port , uint32_t(*fnc_getutc_time)(void) , uint32_t(*fnc_get_subsecond)(void) );
private:
static void processUDPPacket(AsyncUDPPacket& packet);

};
Loading