Simple Network Time Protocol Client. This lib feature: Access to SNTP server, and get Epoch (Unix) time.
SimpleNTP.cpp@0:2be905de8e28, 2015-11-25 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Wed Nov 25 10:03:10 2015 +0000
- Revision:
- 0:2be905de8e28
- Child:
- 1:b5a2e4532331
Simple Network Time Protocol Client.; This lib feature: Access to SNTP server, and get Epoch (Unix) time.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkinoriHashimoto | 0:2be905de8e28 | 1 | #include "SimpleNTP.h" |
AkinoriHashimoto | 0:2be905de8e28 | 2 | |
AkinoriHashimoto | 0:2be905de8e28 | 3 | SimpleNTP::Result SimpleNTP::setNTPServer(string _server, unsigned short _port) |
AkinoriHashimoto | 0:2be905de8e28 | 4 | { |
AkinoriHashimoto | 0:2be905de8e28 | 5 | server= _server; |
AkinoriHashimoto | 0:2be905de8e28 | 6 | port= _port; |
AkinoriHashimoto | 0:2be905de8e28 | 7 | |
AkinoriHashimoto | 0:2be905de8e28 | 8 | socket.set_blocking(false, 1500); |
AkinoriHashimoto | 0:2be905de8e28 | 9 | if(socket.init() == -1) |
AkinoriHashimoto | 0:2be905de8e28 | 10 | return ERR_SocketInit; |
AkinoriHashimoto | 0:2be905de8e28 | 11 | |
AkinoriHashimoto | 0:2be905de8e28 | 12 | if(serverNTP.set_address(server.c_str(), port) == -1) |
AkinoriHashimoto | 0:2be905de8e28 | 13 | return ERR_SetNTPAddr; |
AkinoriHashimoto | 0:2be905de8e28 | 14 | |
AkinoriHashimoto | 0:2be905de8e28 | 15 | return SUCCESS; |
AkinoriHashimoto | 0:2be905de8e28 | 16 | } |
AkinoriHashimoto | 0:2be905de8e28 | 17 | |
AkinoriHashimoto | 0:2be905de8e28 | 18 | long SimpleNTP::getNetworkTime() |
AkinoriHashimoto | 0:2be905de8e28 | 19 | { |
AkinoriHashimoto | 0:2be905de8e28 | 20 | // long timeEpoch= -1; |
AkinoriHashimoto | 0:2be905de8e28 | 21 | int idx, bytes; |
AkinoriHashimoto | 0:2be905de8e28 | 22 | // LeapIndicator:0(0x00), VersionNumber:4(0x100), Mode:3(0x011); |
AkinoriHashimoto | 0:2be905de8e28 | 23 | // -> 0b0010 0011 = 0x23 |
AkinoriHashimoto | 0:2be905de8e28 | 24 | char req[49]; |
AkinoriHashimoto | 0:2be905de8e28 | 25 | for(idx= 0; idx < 48; idx++) |
AkinoriHashimoto | 0:2be905de8e28 | 26 | req[idx]= 0x00; |
AkinoriHashimoto | 0:2be905de8e28 | 27 | req[48]= NULL; |
AkinoriHashimoto | 0:2be905de8e28 | 28 | req[0]= 0x23; |
AkinoriHashimoto | 0:2be905de8e28 | 29 | bytes= socket.sendTo(serverNTP, (char *)req, 48); |
AkinoriHashimoto | 0:2be905de8e28 | 30 | if(bytes == -1) |
AkinoriHashimoto | 0:2be905de8e28 | 31 | return -1; |
AkinoriHashimoto | 0:2be905de8e28 | 32 | |
AkinoriHashimoto | 0:2be905de8e28 | 33 | // NTP response is max 68 Byte (EXPECTED). |
AkinoriHashimoto | 0:2be905de8e28 | 34 | char resSNTP[70]; |
AkinoriHashimoto | 0:2be905de8e28 | 35 | bytes= socket.receiveFrom(serverNTP, resSNTP, sizeof(resSNTP)); |
AkinoriHashimoto | 0:2be905de8e28 | 36 | if(bytes == -1) |
AkinoriHashimoto | 0:2be905de8e28 | 37 | return -1; |
AkinoriHashimoto | 0:2be905de8e28 | 38 | |
AkinoriHashimoto | 0:2be905de8e28 | 39 | // RFC2030; Transmit TimeStamp[40-48Byte]:BigEndian 32bit unsigned long (dec), + 32bit under floating-point. |
AkinoriHashimoto | 0:2be905de8e28 | 40 | unsigned long timeNTP= 0; |
AkinoriHashimoto | 0:2be905de8e28 | 41 | for(idx = 0; idx < 4; idx++) |
AkinoriHashimoto | 0:2be905de8e28 | 42 | timeNTP= (timeNTP << 8) | resSNTP[40+ idx]; |
AkinoriHashimoto | 0:2be905de8e28 | 43 | |
AkinoriHashimoto | 0:2be905de8e28 | 44 | // 25567day * 24h * 60m * 60s = 2208988800 s. w/o leap seconds(DEFINED). |
AkinoriHashimoto | 0:2be905de8e28 | 45 | // Epoch: 1970/01/01~. NTP:1900/01/01~ |
AkinoriHashimoto | 0:2be905de8e28 | 46 | // timeEpoch= timeNTP- 2208988800; |
AkinoriHashimoto | 0:2be905de8e28 | 47 | return timeNTP- 2208988800;//timeEpoch; |
AkinoriHashimoto | 0:2be905de8e28 | 48 | } |
AkinoriHashimoto | 0:2be905de8e28 | 49 | |
AkinoriHashimoto | 0:2be905de8e28 | 50 | |
AkinoriHashimoto | 0:2be905de8e28 | 51 | // EOF |