An access controller for man doors at our facility. It receives Wiegand signals from a keypad/card reader and activates a relay to open the door. Access codes are stored in EEPROM. The active code list is updated from TFTP on a local server.
Dependencies: 24LCxx_I2C CardReader USBHOST
NTPClient.h@0:a56239ae90c2, 2017-09-25 (annotated)
- Committer:
- acesrobertm
- Date:
- Mon Sep 25 19:02:40 2017 +0000
- Revision:
- 0:a56239ae90c2
in process of moving networking code to non-blocking format
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acesrobertm | 0:a56239ae90c2 | 1 | /* NTPClient.h */ |
acesrobertm | 0:a56239ae90c2 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
acesrobertm | 0:a56239ae90c2 | 3 | * |
acesrobertm | 0:a56239ae90c2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
acesrobertm | 0:a56239ae90c2 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
acesrobertm | 0:a56239ae90c2 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
acesrobertm | 0:a56239ae90c2 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
acesrobertm | 0:a56239ae90c2 | 8 | * furnished to do so, subject to the following conditions: |
acesrobertm | 0:a56239ae90c2 | 9 | * |
acesrobertm | 0:a56239ae90c2 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
acesrobertm | 0:a56239ae90c2 | 11 | * substantial portions of the Software. |
acesrobertm | 0:a56239ae90c2 | 12 | * |
acesrobertm | 0:a56239ae90c2 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
acesrobertm | 0:a56239ae90c2 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
acesrobertm | 0:a56239ae90c2 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
acesrobertm | 0:a56239ae90c2 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
acesrobertm | 0:a56239ae90c2 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
acesrobertm | 0:a56239ae90c2 | 18 | */ |
acesrobertm | 0:a56239ae90c2 | 19 | |
acesrobertm | 0:a56239ae90c2 | 20 | #ifndef NTPCLIENT_H_ |
acesrobertm | 0:a56239ae90c2 | 21 | #define NTPCLIENT_H_ |
acesrobertm | 0:a56239ae90c2 | 22 | |
acesrobertm | 0:a56239ae90c2 | 23 | #include <cstdint> |
acesrobertm | 0:a56239ae90c2 | 24 | #include "EthernetInterface.h" |
acesrobertm | 0:a56239ae90c2 | 25 | #include "NetworkStack.h" |
acesrobertm | 0:a56239ae90c2 | 26 | #include "UDPSocket.h" |
acesrobertm | 0:a56239ae90c2 | 27 | #include <inet.h> |
acesrobertm | 0:a56239ae90c2 | 28 | |
acesrobertm | 0:a56239ae90c2 | 29 | using std::uint8_t; |
acesrobertm | 0:a56239ae90c2 | 30 | using std::uint16_t; |
acesrobertm | 0:a56239ae90c2 | 31 | using std::uint32_t; |
acesrobertm | 0:a56239ae90c2 | 32 | |
acesrobertm | 0:a56239ae90c2 | 33 | #define NTP_DEFAULT_PORT 123 |
acesrobertm | 0:a56239ae90c2 | 34 | #define NTP_DEFAULT_TIMEOUT 3 |
acesrobertm | 0:a56239ae90c2 | 35 | #define NTP_CLIENT_PORT 0 //Random port |
acesrobertm | 0:a56239ae90c2 | 36 | #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900) |
acesrobertm | 0:a56239ae90c2 | 37 | |
acesrobertm | 0:a56239ae90c2 | 38 | ///NTP client results |
acesrobertm | 0:a56239ae90c2 | 39 | enum NTPResult |
acesrobertm | 0:a56239ae90c2 | 40 | { |
acesrobertm | 0:a56239ae90c2 | 41 | NTP_DNS = -1, ///<Could not resolve name |
acesrobertm | 0:a56239ae90c2 | 42 | NTP_PRTCL = -2, ///<Protocol error |
acesrobertm | 0:a56239ae90c2 | 43 | NTP_TIMEOUT = -3, ///<Connection timeout |
acesrobertm | 0:a56239ae90c2 | 44 | NTP_CONN = -4, ///<Connection error |
acesrobertm | 0:a56239ae90c2 | 45 | NTP_OK = 1, ///<Success |
acesrobertm | 0:a56239ae90c2 | 46 | NTP_WAIT = 0 |
acesrobertm | 0:a56239ae90c2 | 47 | }; |
acesrobertm | 0:a56239ae90c2 | 48 | |
acesrobertm | 0:a56239ae90c2 | 49 | /** NTP Client to update the mbed's RTC using a remote time server |
acesrobertm | 0:a56239ae90c2 | 50 | * |
acesrobertm | 0:a56239ae90c2 | 51 | */ |
acesrobertm | 0:a56239ae90c2 | 52 | class NTPClient |
acesrobertm | 0:a56239ae90c2 | 53 | { |
acesrobertm | 0:a56239ae90c2 | 54 | public: |
acesrobertm | 0:a56239ae90c2 | 55 | NTPClient(); |
acesrobertm | 0:a56239ae90c2 | 56 | NTPResult setTime(EthernetInterface *stack, const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking |
acesrobertm | 0:a56239ae90c2 | 57 | int update(); |
acesrobertm | 0:a56239ae90c2 | 58 | |
acesrobertm | 0:a56239ae90c2 | 59 | private: |
acesrobertm | 0:a56239ae90c2 | 60 | void process_result(); |
acesrobertm | 0:a56239ae90c2 | 61 | void reset(); |
acesrobertm | 0:a56239ae90c2 | 62 | |
acesrobertm | 0:a56239ae90c2 | 63 | struct NTPPacket //See RFC 4330 for Simple NTP |
acesrobertm | 0:a56239ae90c2 | 64 | { |
acesrobertm | 0:a56239ae90c2 | 65 | //WARN: We are in LE! Network is BE! |
acesrobertm | 0:a56239ae90c2 | 66 | //LSb first |
acesrobertm | 0:a56239ae90c2 | 67 | unsigned mode : 3; |
acesrobertm | 0:a56239ae90c2 | 68 | unsigned vn : 3; |
acesrobertm | 0:a56239ae90c2 | 69 | unsigned li : 2; |
acesrobertm | 0:a56239ae90c2 | 70 | |
acesrobertm | 0:a56239ae90c2 | 71 | uint8_t stratum; |
acesrobertm | 0:a56239ae90c2 | 72 | uint8_t poll; |
acesrobertm | 0:a56239ae90c2 | 73 | uint8_t precision; |
acesrobertm | 0:a56239ae90c2 | 74 | //32 bits header |
acesrobertm | 0:a56239ae90c2 | 75 | |
acesrobertm | 0:a56239ae90c2 | 76 | uint32_t rootDelay; |
acesrobertm | 0:a56239ae90c2 | 77 | uint32_t rootDispersion; |
acesrobertm | 0:a56239ae90c2 | 78 | uint32_t refId; |
acesrobertm | 0:a56239ae90c2 | 79 | |
acesrobertm | 0:a56239ae90c2 | 80 | uint32_t refTm_s; |
acesrobertm | 0:a56239ae90c2 | 81 | uint32_t refTm_f; |
acesrobertm | 0:a56239ae90c2 | 82 | uint32_t origTm_s; |
acesrobertm | 0:a56239ae90c2 | 83 | uint32_t origTm_f; |
acesrobertm | 0:a56239ae90c2 | 84 | uint32_t rxTm_s; |
acesrobertm | 0:a56239ae90c2 | 85 | uint32_t rxTm_f; |
acesrobertm | 0:a56239ae90c2 | 86 | uint32_t txTm_s; |
acesrobertm | 0:a56239ae90c2 | 87 | uint32_t txTm_f; |
acesrobertm | 0:a56239ae90c2 | 88 | } __attribute__ ((packed)); |
acesrobertm | 0:a56239ae90c2 | 89 | |
acesrobertm | 0:a56239ae90c2 | 90 | struct NTPPacket pkt; |
acesrobertm | 0:a56239ae90c2 | 91 | UDPSocket m_sock; |
acesrobertm | 0:a56239ae90c2 | 92 | bool mQuerySent; |
acesrobertm | 0:a56239ae90c2 | 93 | bool mResponseReceived; |
acesrobertm | 0:a56239ae90c2 | 94 | bool mConversationComplete; |
acesrobertm | 0:a56239ae90c2 | 95 | int mStartTime; |
acesrobertm | 0:a56239ae90c2 | 96 | int mTimeout; |
acesrobertm | 0:a56239ae90c2 | 97 | SocketAddress* outEndpoint; |
acesrobertm | 0:a56239ae90c2 | 98 | SocketAddress* inEndpoint; |
acesrobertm | 0:a56239ae90c2 | 99 | }; |
acesrobertm | 0:a56239ae90c2 | 100 | |
acesrobertm | 0:a56239ae90c2 | 101 | |
acesrobertm | 0:a56239ae90c2 | 102 | #endif |
acesrobertm | 0:a56239ae90c2 | 103 |