mbed os 5 & accept pointer interface for ethernet and wifi
NTPClient.h@1:b221a8765b3f, 2012-07-27 (annotated)
- Committer:
- donatien
- Date:
- Fri Jul 27 08:49:34 2012 +0000
- Revision:
- 1:b221a8765b3f
- Parent:
- 0:04a82df0f587
- Child:
- 2:9a64a50df235
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:04a82df0f587 | 1 | /* NTPClient.h */ |
donatien | 0:04a82df0f587 | 2 | /* |
donatien | 0:04a82df0f587 | 3 | Copyright (C) 2012 ARM Limited. |
donatien | 0:04a82df0f587 | 4 | |
donatien | 0:04a82df0f587 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
donatien | 0:04a82df0f587 | 6 | this software and associated documentation files (the "Software"), to deal in |
donatien | 0:04a82df0f587 | 7 | the Software without restriction, including without limitation the rights to |
donatien | 0:04a82df0f587 | 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
donatien | 0:04a82df0f587 | 9 | of the Software, and to permit persons to whom the Software is furnished to do |
donatien | 0:04a82df0f587 | 10 | so, subject to the following conditions: |
donatien | 0:04a82df0f587 | 11 | |
donatien | 0:04a82df0f587 | 12 | The above copyright notice and this permission notice shall be included in all |
donatien | 0:04a82df0f587 | 13 | copies or substantial portions of the Software. |
donatien | 0:04a82df0f587 | 14 | |
donatien | 0:04a82df0f587 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:04a82df0f587 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:04a82df0f587 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:04a82df0f587 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:04a82df0f587 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:04a82df0f587 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
donatien | 0:04a82df0f587 | 21 | SOFTWARE. |
donatien | 0:04a82df0f587 | 22 | */ |
donatien | 0:04a82df0f587 | 23 | |
donatien | 0:04a82df0f587 | 24 | #include "core/fwk.h" |
donatien | 0:04a82df0f587 | 25 | |
donatien | 0:04a82df0f587 | 26 | #ifndef NTPCLIENT_H_ |
donatien | 0:04a82df0f587 | 27 | #define NTPCLIENT_H_ |
donatien | 0:04a82df0f587 | 28 | |
donatien | 0:04a82df0f587 | 29 | #define NTP_DEFAULT_PORT 123 |
donatien | 0:04a82df0f587 | 30 | #define NTP_DEFAULT_TIMEOUT 4000 |
donatien | 0:04a82df0f587 | 31 | |
donatien | 0:04a82df0f587 | 32 | /** NTP Client to update the mbed's RTC using a remote time server |
donatien | 0:04a82df0f587 | 33 | * |
donatien | 0:04a82df0f587 | 34 | */ |
donatien | 0:04a82df0f587 | 35 | class NTPClient |
donatien | 0:04a82df0f587 | 36 | { |
donatien | 0:04a82df0f587 | 37 | public: |
donatien | 0:04a82df0f587 | 38 | /** |
donatien | 0:04a82df0f587 | 39 | Instantiate the NTP client |
donatien | 0:04a82df0f587 | 40 | */ |
donatien | 0:04a82df0f587 | 41 | NTPClient(); |
donatien | 0:04a82df0f587 | 42 | |
donatien | 0:04a82df0f587 | 43 | /**Get current time (blocking) |
donatien | 0:04a82df0f587 | 44 | Update the time using the server host |
donatien | 0:04a82df0f587 | 45 | Blocks until completion |
donatien | 0:04a82df0f587 | 46 | @param host NTP server IPv4 address or hostname (will be resolved via DNS) |
donatien | 0:04a82df0f587 | 47 | @param port port to use; defaults to 123 |
donatien | 0:04a82df0f587 | 48 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
donatien | 0:04a82df0f587 | 49 | @return 0 on success, NET error code (<0) on failure |
donatien | 0:04a82df0f587 | 50 | */ |
donatien | 0:04a82df0f587 | 51 | int setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking |
donatien | 0:04a82df0f587 | 52 | |
donatien | 0:04a82df0f587 | 53 | private: |
donatien | 0:04a82df0f587 | 54 | struct NTPPacket //See RFC 4330 for Simple NTP |
donatien | 0:04a82df0f587 | 55 | { |
donatien | 0:04a82df0f587 | 56 | //WARN: We are in LE! Network is BE! |
donatien | 0:04a82df0f587 | 57 | //LSb first |
donatien | 0:04a82df0f587 | 58 | unsigned mode : 3; |
donatien | 0:04a82df0f587 | 59 | unsigned vn : 3; |
donatien | 0:04a82df0f587 | 60 | unsigned li : 2; |
donatien | 0:04a82df0f587 | 61 | |
donatien | 0:04a82df0f587 | 62 | uint8_t stratum; |
donatien | 0:04a82df0f587 | 63 | uint8_t poll; |
donatien | 0:04a82df0f587 | 64 | uint8_t precision; |
donatien | 0:04a82df0f587 | 65 | //32 bits header |
donatien | 0:04a82df0f587 | 66 | |
donatien | 0:04a82df0f587 | 67 | uint32_t rootDelay; |
donatien | 0:04a82df0f587 | 68 | uint32_t rootDispersion; |
donatien | 0:04a82df0f587 | 69 | uint32_t refId; |
donatien | 0:04a82df0f587 | 70 | |
donatien | 0:04a82df0f587 | 71 | uint32_t refTm_s; |
donatien | 0:04a82df0f587 | 72 | uint32_t refTm_f; |
donatien | 0:04a82df0f587 | 73 | uint32_t origTm_s; |
donatien | 0:04a82df0f587 | 74 | uint32_t origTm_f; |
donatien | 0:04a82df0f587 | 75 | uint32_t rxTm_s; |
donatien | 0:04a82df0f587 | 76 | uint32_t rxTm_f; |
donatien | 0:04a82df0f587 | 77 | uint32_t txTm_s; |
donatien | 0:04a82df0f587 | 78 | uint32_t txTm_f; |
donatien | 0:04a82df0f587 | 79 | } __attribute__ ((packed)); |
donatien | 0:04a82df0f587 | 80 | |
donatien | 0:04a82df0f587 | 81 | }; |
donatien | 0:04a82df0f587 | 82 | |
donatien | 0:04a82df0f587 | 83 | |
donatien | 0:04a82df0f587 | 84 | #endif /* NTPCLIENT_H_ */ |