Station API
NTPClient.h@1:a22e390c70b3, 2011-12-12 (annotated)
- Committer:
- yamaguch
- Date:
- Mon Dec 12 02:33:21 2011 +0000
- Revision:
- 1:a22e390c70b3
- Child:
- 2:a9d1a9c92927
0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 1:a22e390c70b3 | 1 | /* |
yamaguch | 1:a22e390c70b3 | 2 | Copyright (c) 2011, Senio Networks, Inc. |
yamaguch | 1:a22e390c70b3 | 3 | |
yamaguch | 1:a22e390c70b3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yamaguch | 1:a22e390c70b3 | 5 | of this software and associated documentation files (the "Software"), to deal |
yamaguch | 1:a22e390c70b3 | 6 | in the Software without restriction, including without limitation the rights |
yamaguch | 1:a22e390c70b3 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yamaguch | 1:a22e390c70b3 | 8 | copies of the Software, and to permit persons to whom the Software is |
yamaguch | 1:a22e390c70b3 | 9 | furnished to do so, subject to the following conditions: |
yamaguch | 1:a22e390c70b3 | 10 | |
yamaguch | 1:a22e390c70b3 | 11 | The above copyright notice and this permission notice shall be included in |
yamaguch | 1:a22e390c70b3 | 12 | all copies or substantial portions of the Software. |
yamaguch | 1:a22e390c70b3 | 13 | |
yamaguch | 1:a22e390c70b3 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yamaguch | 1:a22e390c70b3 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yamaguch | 1:a22e390c70b3 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yamaguch | 1:a22e390c70b3 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yamaguch | 1:a22e390c70b3 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yamaguch | 1:a22e390c70b3 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yamaguch | 1:a22e390c70b3 | 20 | THE SOFTWARE. |
yamaguch | 1:a22e390c70b3 | 21 | */ |
yamaguch | 1:a22e390c70b3 | 22 | |
yamaguch | 1:a22e390c70b3 | 23 | #include "SimpleSocket.h" |
yamaguch | 1:a22e390c70b3 | 24 | |
yamaguch | 1:a22e390c70b3 | 25 | class NTPClient { |
yamaguch | 1:a22e390c70b3 | 26 | public: |
yamaguch | 1:a22e390c70b3 | 27 | NTPClient(char *server = "pool.ntp.org", float timeout = 3.0) |
yamaguch | 1:a22e390c70b3 | 28 | : server(server), timeout(timeout) {} |
yamaguch | 1:a22e390c70b3 | 29 | |
yamaguch | 1:a22e390c70b3 | 30 | bool setTime() { |
yamaguch | 1:a22e390c70b3 | 31 | Timer timer; |
yamaguch | 1:a22e390c70b3 | 32 | timer.start(); |
yamaguch | 1:a22e390c70b3 | 33 | while (timer.read() < timeout) { |
yamaguch | 1:a22e390c70b3 | 34 | DatagramSocket datagram; |
yamaguch | 1:a22e390c70b3 | 35 | char buf[48] = {0x23}; // 00100011 LI(0), Version(4), Mode(3: Client) |
yamaguch | 1:a22e390c70b3 | 36 | datagram.write(buf, sizeof(buf)); |
yamaguch | 1:a22e390c70b3 | 37 | datagram.send(server, 123); |
yamaguch | 1:a22e390c70b3 | 38 | |
yamaguch | 1:a22e390c70b3 | 39 | if (datagram.receive(0, timeout) > 0) { |
yamaguch | 1:a22e390c70b3 | 40 | if (datagram.read(buf, sizeof(buf))) { |
yamaguch | 1:a22e390c70b3 | 41 | unsigned long seconds = 0; |
yamaguch | 1:a22e390c70b3 | 42 | for (int i = 40; i <= 43; i++) |
yamaguch | 1:a22e390c70b3 | 43 | seconds = (seconds << 8) | buf[i]; |
yamaguch | 1:a22e390c70b3 | 44 | set_time(time_t(seconds - 2208988800ULL)); |
yamaguch | 1:a22e390c70b3 | 45 | return true; |
yamaguch | 1:a22e390c70b3 | 46 | } |
yamaguch | 1:a22e390c70b3 | 47 | } |
yamaguch | 1:a22e390c70b3 | 48 | } |
yamaguch | 1:a22e390c70b3 | 49 | |
yamaguch | 1:a22e390c70b3 | 50 | return false; |
yamaguch | 1:a22e390c70b3 | 51 | } |
yamaguch | 1:a22e390c70b3 | 52 | |
yamaguch | 1:a22e390c70b3 | 53 | private: |
yamaguch | 1:a22e390c70b3 | 54 | char *server; |
yamaguch | 1:a22e390c70b3 | 55 | float timeout; |
yamaguch | 1:a22e390c70b3 | 56 | }; |