Modify to work with Ethernet Interface on NUCLEO F746ZG

Fork of NTPClient by Vergil Cola

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NTPClient.h Source File

NTPClient.h

Go to the documentation of this file.
00001 /* NTPClient.h */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 /** \file
00021 NTP Client header file
00022 */
00023 
00024 #ifndef NTPCLIENT_H_
00025 #define NTPCLIENT_H_
00026 
00027 #include <stdint.h>
00028 
00029 //using std::uint8_t;
00030 //using std::uint16_t;
00031 //using std::uint32_t;
00032 
00033 #include "EthernetInterface.h"  //#include "NetworkInterface.h"
00034 #include "UDPSocket.h"
00035 
00036 
00037 #define NTP_DEFAULT_PORT 123
00038 #define NTP_DEFAULT_TIMEOUT 4000
00039 
00040 ///NTP client results
00041 enum NTPResult
00042 {
00043   NTP_DNS, ///<Could not resolve name
00044   NTP_PRTCL, ///<Protocol error
00045   NTP_TIMEOUT, ///<Connection timeout
00046   NTP_CONN, ///<Connection error
00047   NTP_OK = 0, ///<Success
00048 };
00049 
00050 /** NTP Client to update the mbed's RTC using a remote time server
00051 *
00052 */
00053 class NTPClient
00054 {
00055 public:
00056   /**
00057   Instantiate the NTP client
00058   */
00059  // NTPClient();
00060   NTPClient(EthernetInterface & _m_intf);      // NTPClient(NetworkInterface & _m_intf);
00061   /**Get current time (blocking)
00062   Update the time using the server host
00063   Blocks until completion
00064   @param host NTP server IPv4 address or hostname (will be resolved via DNS)
00065   @param port port to use; defaults to 123
00066   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00067   @return 0 on success, NTP error code (<0) on failure
00068   */
00069   NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); 
00070 
00071 private:
00072   struct NTPPacket //See RFC 4330 for Simple NTP
00073   {
00074     //WARN: We are in LE! Network is BE!
00075     //LSb first
00076     unsigned mode : 3;
00077     unsigned vn : 3;
00078     unsigned li : 2;
00079 
00080     uint8_t stratum;
00081     uint8_t poll;
00082     uint8_t precision;
00083     //32 bits header
00084 
00085     uint32_t rootDelay;
00086     uint32_t rootDispersion;
00087     uint32_t refId;
00088 
00089     uint32_t refTm_s;
00090     uint32_t refTm_f;
00091     uint32_t origTm_s;
00092     uint32_t origTm_f;
00093     uint32_t rxTm_s;
00094     uint32_t rxTm_f;
00095     uint32_t txTm_s;
00096     uint32_t txTm_f;
00097   } __attribute__ ((packed));
00098   
00099   NetworkInterface & m_intf;  // WiFi interface  
00100   UDPSocket m_sock;
00101 };
00102 
00103 
00104 #endif /* NTPCLIENT_H_ */