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