Acquisition for GNSS1A1

Dependencies:   F7_Ethernet X_NUCLEO_IKS01A2 mbed-rtos mbed

Fork of Test2Boards by Simone Mentasti

Committer:
nirnakern
Date:
Mon Oct 01 14:33:56 2018 +0000
Revision:
3:0fb5321d73fb
Parent:
1:ef1bbf9b6205
First Commit;

Who changed what in which revision?

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