Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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