ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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