OS5.15 complaint NTP library

Dependents:   Firebase-Example TCP-NTP-Server TCP-NTP-Server-OS5depreciated

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NTPclient.h Source File

NTPclient.h

00001 
00002 #include "mbed.h"
00003  
00004 #ifndef NTPCLIENT_H
00005 #define NTPCLIENT_H
00006 
00007 #include <stdint.h>
00008 #include "UDPSocket.h"
00009 #include "NetworkInterface.h"
00010 
00011 #define NTP_DEFAULT_PORT 123
00012 #define NTP_DEFAULT_TIMEOUT 4000
00013 #define NTP_DEFAULT_TIMEZONE_OFFSET 0
00014 
00015 class NTPclient
00016 {
00017 public:  
00018     NTPclient(NetworkInterface & _m_intf);
00019     // Get current time in seconds (blocking)
00020     // Update the time using the server host
00021     // Blocks until completion
00022     // NTPpool, NTP server IPv4 address or hostname (will be resolved via DNS)
00023     // tzoffset, offset in seconds (3600 add 1 hour, -3600 subtract 1 hour)
00024     // dst, adjust for DST 1= enabled, 0=dissabled 
00025     // setRTC, set system RTC 1= enabled, 0=dissabled 
00026 
00027     uint32_t getNTP(const char* NTPpool, uint32_t tzoffset, bool dst, bool setRTC); 
00028 
00029 private:
00030     struct NTPPacket
00031     {
00032     //We are in LE Network is BE
00033     //LSb first
00034     unsigned mode : 3;
00035     unsigned vn : 3;
00036     unsigned li : 2;
00037 
00038     uint8_t stratum;
00039     uint8_t poll;
00040     uint8_t precision;
00041     
00042     //32 bits header
00043     uint32_t rootDelay;
00044     uint32_t rootDispersion;
00045     uint32_t refId;
00046     uint32_t refTm_s;
00047     uint32_t refTm_f;
00048     uint32_t origTm_s;
00049     uint32_t origTm_f;
00050     uint32_t rxTm_s;
00051     uint32_t rxTm_f;
00052     uint32_t txTm_s;
00053     uint32_t txTm_f;
00054   } __attribute__ ((packed));
00055   
00056   NetworkInterface & m_intf;  // WiFi interface  
00057   UDPSocket m_sock;
00058 };
00059 #endif /* NTPCLIENT_H_ */