NTP(Network Time Protocol) Client Library.

Fork of NTPClientLib by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NTPClient.h Source File

NTPClient.h

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