Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tuxitheone 0:ecaf3e593122 1 /* NTPClient.h */
Tuxitheone 0:ecaf3e593122 2 /* Copyright (C) 2012 mbed.org, MIT License
Tuxitheone 0:ecaf3e593122 3 *
Tuxitheone 0:ecaf3e593122 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Tuxitheone 0:ecaf3e593122 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Tuxitheone 0:ecaf3e593122 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Tuxitheone 0:ecaf3e593122 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Tuxitheone 0:ecaf3e593122 8 * furnished to do so, subject to the following conditions:
Tuxitheone 0:ecaf3e593122 9 *
Tuxitheone 0:ecaf3e593122 10 * The above copyright notice and this permission notice shall be included in all copies or
Tuxitheone 0:ecaf3e593122 11 * substantial portions of the Software.
Tuxitheone 0:ecaf3e593122 12 *
Tuxitheone 0:ecaf3e593122 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Tuxitheone 0:ecaf3e593122 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Tuxitheone 0:ecaf3e593122 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Tuxitheone 0:ecaf3e593122 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Tuxitheone 0:ecaf3e593122 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Tuxitheone 0:ecaf3e593122 18 */
Tuxitheone 0:ecaf3e593122 19
Tuxitheone 0:ecaf3e593122 20 /** \file
Tuxitheone 0:ecaf3e593122 21 NTP Client header file
Tuxitheone 0:ecaf3e593122 22 */
Tuxitheone 0:ecaf3e593122 23
Tuxitheone 0:ecaf3e593122 24 #ifndef NTPCLIENT_H_
Tuxitheone 0:ecaf3e593122 25 #define NTPCLIENT_H_
Tuxitheone 0:ecaf3e593122 26
Tuxitheone 0:ecaf3e593122 27 #include <cstdint>
Tuxitheone 0:ecaf3e593122 28
Tuxitheone 0:ecaf3e593122 29 using std::uint8_t;
Tuxitheone 0:ecaf3e593122 30 using std::uint16_t;
Tuxitheone 0:ecaf3e593122 31 using std::uint32_t;
Tuxitheone 0:ecaf3e593122 32
Tuxitheone 0:ecaf3e593122 33 #include "UDPSocket.h"
Tuxitheone 0:ecaf3e593122 34
Tuxitheone 0:ecaf3e593122 35 #define NTP_DEFAULT_PORT 123
Tuxitheone 0:ecaf3e593122 36 #define NTP_DEFAULT_TIMEOUT 4000
Tuxitheone 0:ecaf3e593122 37
Tuxitheone 0:ecaf3e593122 38 ///NTP client results
Tuxitheone 0:ecaf3e593122 39 enum NTPResult
Tuxitheone 0:ecaf3e593122 40 {
Tuxitheone 0:ecaf3e593122 41 NTP_DNS, ///<Could not resolve name
Tuxitheone 0:ecaf3e593122 42 NTP_PRTCL, ///<Protocol error
Tuxitheone 0:ecaf3e593122 43 NTP_TIMEOUT, ///<Connection timeout
Tuxitheone 0:ecaf3e593122 44 NTP_CONN, ///<Connection error
Tuxitheone 0:ecaf3e593122 45 NTP_OK = 0, ///<Success
Tuxitheone 0:ecaf3e593122 46 };
Tuxitheone 0:ecaf3e593122 47
Tuxitheone 0:ecaf3e593122 48 /** NTP Client to update the mbed's RTC using a remote time server
Tuxitheone 0:ecaf3e593122 49 *
Tuxitheone 0:ecaf3e593122 50 */
Tuxitheone 0:ecaf3e593122 51 class NTPClient
Tuxitheone 0:ecaf3e593122 52 {
Tuxitheone 0:ecaf3e593122 53 public:
Tuxitheone 0:ecaf3e593122 54 /**
Tuxitheone 0:ecaf3e593122 55 Instantiate the NTP client
Tuxitheone 0:ecaf3e593122 56 */
Tuxitheone 0:ecaf3e593122 57 NTPClient();
Tuxitheone 0:ecaf3e593122 58
Tuxitheone 0:ecaf3e593122 59 /**Get current time (blocking)
Tuxitheone 0:ecaf3e593122 60 Update the time using the server host
Tuxitheone 0:ecaf3e593122 61 Blocks until completion
Tuxitheone 0:ecaf3e593122 62 @param host NTP server IPv4 address or hostname (will be resolved via DNS)
Tuxitheone 0:ecaf3e593122 63 @param port port to use; defaults to 123
Tuxitheone 0:ecaf3e593122 64 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
Tuxitheone 0:ecaf3e593122 65 @return 0 on success, NTP error code (<0) on failure
Tuxitheone 0:ecaf3e593122 66 */
Tuxitheone 0:ecaf3e593122 67 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking
Tuxitheone 0:ecaf3e593122 68
Tuxitheone 0:ecaf3e593122 69 private:
Tuxitheone 0:ecaf3e593122 70 struct NTPPacket //See RFC 4330 for Simple NTP
Tuxitheone 0:ecaf3e593122 71 {
Tuxitheone 0:ecaf3e593122 72 //WARN: We are in LE! Network is BE!
Tuxitheone 0:ecaf3e593122 73 //LSb first
Tuxitheone 0:ecaf3e593122 74 unsigned mode : 3;
Tuxitheone 0:ecaf3e593122 75 unsigned vn : 3;
Tuxitheone 0:ecaf3e593122 76 unsigned li : 2;
Tuxitheone 0:ecaf3e593122 77
Tuxitheone 0:ecaf3e593122 78 uint8_t stratum;
Tuxitheone 0:ecaf3e593122 79 uint8_t poll;
Tuxitheone 0:ecaf3e593122 80 uint8_t precision;
Tuxitheone 0:ecaf3e593122 81 //32 bits header
Tuxitheone 0:ecaf3e593122 82
Tuxitheone 0:ecaf3e593122 83 uint32_t rootDelay;
Tuxitheone 0:ecaf3e593122 84 uint32_t rootDispersion;
Tuxitheone 0:ecaf3e593122 85 uint32_t refId;
Tuxitheone 0:ecaf3e593122 86
Tuxitheone 0:ecaf3e593122 87 uint32_t refTm_s;
Tuxitheone 0:ecaf3e593122 88 uint32_t refTm_f;
Tuxitheone 0:ecaf3e593122 89 uint32_t origTm_s;
Tuxitheone 0:ecaf3e593122 90 uint32_t origTm_f;
Tuxitheone 0:ecaf3e593122 91 uint32_t rxTm_s;
Tuxitheone 0:ecaf3e593122 92 uint32_t rxTm_f;
Tuxitheone 0:ecaf3e593122 93 uint32_t txTm_s;
Tuxitheone 0:ecaf3e593122 94 uint32_t txTm_f;
Tuxitheone 0:ecaf3e593122 95 } __attribute__ ((packed));
Tuxitheone 0:ecaf3e593122 96
Tuxitheone 0:ecaf3e593122 97 UDPSocket m_sock;
Tuxitheone 0:ecaf3e593122 98
Tuxitheone 0:ecaf3e593122 99 };
Tuxitheone 0:ecaf3e593122 100
Tuxitheone 0:ecaf3e593122 101
Tuxitheone 0:ecaf3e593122 102 #endif /* NTPCLIENT_H_ */