Library for ethernet
Dependents: VC0706_FTP_Client_Ethernet_MQTT
Fork of WIZnetInterface by
Socket/SNTPClient.cpp@16:c830d2e7e0f5, 2015-06-24 (annotated)
- Committer:
- eunkyoungkim
- Date:
- Wed Jun 24 02:27:46 2015 +0000
- Revision:
- 16:c830d2e7e0f5
Add SNTP Client
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eunkyoungkim | 16:c830d2e7e0f5 | 1 | #include "SNTPClient.h" |
eunkyoungkim | 16:c830d2e7e0f5 | 2 | |
eunkyoungkim | 16:c830d2e7e0f5 | 3 | #define MAX_TRY_WRITE 20 |
eunkyoungkim | 16:c830d2e7e0f5 | 4 | #define MAX_TRY_READ 10 |
eunkyoungkim | 16:c830d2e7e0f5 | 5 | |
eunkyoungkim | 16:c830d2e7e0f5 | 6 | //Debug is disabled by default |
eunkyoungkim | 16:c830d2e7e0f5 | 7 | #ifdef _SNTP_DEBUG_ |
eunkyoungkim | 16:c830d2e7e0f5 | 8 | #define DBG(x, ...) std::printf("[SNTPClient : DBG]"x"\r\n", ##__VA_ARGS__); |
eunkyoungkim | 16:c830d2e7e0f5 | 9 | #define WARN(x, ...) std::printf("[SNTPClient : WARN]"x"\r\n", ##__VA_ARGS__); |
eunkyoungkim | 16:c830d2e7e0f5 | 10 | #define ERR(x, ...) std::printf("[SNTPClient : ERR]"x"\r\n", ##__VA_ARGS__); |
eunkyoungkim | 16:c830d2e7e0f5 | 11 | #else |
eunkyoungkim | 16:c830d2e7e0f5 | 12 | #define DBG(x, ...) |
eunkyoungkim | 16:c830d2e7e0f5 | 13 | #define WARN(x, ...) |
eunkyoungkim | 16:c830d2e7e0f5 | 14 | #define ERR(x, ...) |
eunkyoungkim | 16:c830d2e7e0f5 | 15 | #endif |
eunkyoungkim | 16:c830d2e7e0f5 | 16 | |
eunkyoungkim | 16:c830d2e7e0f5 | 17 | #define INFO(x, ...) printf("[SNTPClient : INFO]"x"\r\n", ##__VA_ARGS__); |
eunkyoungkim | 16:c830d2e7e0f5 | 18 | |
eunkyoungkim | 16:c830d2e7e0f5 | 19 | SNTPClient::SNTPClient(char * url, uint8_t time_zone) { |
eunkyoungkim | 16:c830d2e7e0f5 | 20 | memcpy(host, url, strlen(url)); |
eunkyoungkim | 16:c830d2e7e0f5 | 21 | host[strlen(url)] = '\0'; |
eunkyoungkim | 16:c830d2e7e0f5 | 22 | port = ntp_port; |
eunkyoungkim | 16:c830d2e7e0f5 | 23 | socket.set_blocking(false, 3000); |
eunkyoungkim | 16:c830d2e7e0f5 | 24 | |
eunkyoungkim | 16:c830d2e7e0f5 | 25 | tz = time_zone; |
eunkyoungkim | 16:c830d2e7e0f5 | 26 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 27 | |
eunkyoungkim | 16:c830d2e7e0f5 | 28 | bool SNTPClient::connect() { |
eunkyoungkim | 16:c830d2e7e0f5 | 29 | socket.init(); |
eunkyoungkim | 16:c830d2e7e0f5 | 30 | socket.bind(0); |
eunkyoungkim | 16:c830d2e7e0f5 | 31 | if(sntp_server.set_address(host, port) < 0) |
eunkyoungkim | 16:c830d2e7e0f5 | 32 | return false; |
eunkyoungkim | 16:c830d2e7e0f5 | 33 | |
eunkyoungkim | 16:c830d2e7e0f5 | 34 | uint32_t ip = str_to_ip(sntp_server.get_address()); |
eunkyoungkim | 16:c830d2e7e0f5 | 35 | NTPformat.dstaddr[0] = (uint8_t)(ip >> 24); |
eunkyoungkim | 16:c830d2e7e0f5 | 36 | NTPformat.dstaddr[1] = (uint8_t)(ip >> 16); |
eunkyoungkim | 16:c830d2e7e0f5 | 37 | NTPformat.dstaddr[2] = (uint8_t)(ip >> 8); |
eunkyoungkim | 16:c830d2e7e0f5 | 38 | NTPformat.dstaddr[3] = (uint8_t)(ip >> 0); |
eunkyoungkim | 16:c830d2e7e0f5 | 39 | DBG("NTP Server: %s\r\n", sntp_server.get_address()); |
eunkyoungkim | 16:c830d2e7e0f5 | 40 | |
eunkyoungkim | 16:c830d2e7e0f5 | 41 | uint8_t Flag; |
eunkyoungkim | 16:c830d2e7e0f5 | 42 | NTPformat.leap = 0; /* leap indicator */ |
eunkyoungkim | 16:c830d2e7e0f5 | 43 | NTPformat.version = 4; /* version number */ |
eunkyoungkim | 16:c830d2e7e0f5 | 44 | NTPformat.mode = 3; /* mode */ |
eunkyoungkim | 16:c830d2e7e0f5 | 45 | NTPformat.stratum = 0; /* stratum */ |
eunkyoungkim | 16:c830d2e7e0f5 | 46 | NTPformat.poll = 0; /* poll interval */ |
eunkyoungkim | 16:c830d2e7e0f5 | 47 | NTPformat.precision = 0; /* precision */ |
eunkyoungkim | 16:c830d2e7e0f5 | 48 | NTPformat.rootdelay = 0; /* root delay */ |
eunkyoungkim | 16:c830d2e7e0f5 | 49 | NTPformat.rootdisp = 0; /* root dispersion */ |
eunkyoungkim | 16:c830d2e7e0f5 | 50 | NTPformat.refid = 0; /* reference ID */ |
eunkyoungkim | 16:c830d2e7e0f5 | 51 | NTPformat.reftime = 0; /* reference time */ |
eunkyoungkim | 16:c830d2e7e0f5 | 52 | NTPformat.org = 0; /* origin timestamp */ |
eunkyoungkim | 16:c830d2e7e0f5 | 53 | NTPformat.rec = 0; /* receive timestamp */ |
eunkyoungkim | 16:c830d2e7e0f5 | 54 | NTPformat.xmt = 1; /* transmit timestamp */ |
eunkyoungkim | 16:c830d2e7e0f5 | 55 | |
eunkyoungkim | 16:c830d2e7e0f5 | 56 | Flag = (NTPformat.leap<<6)+(NTPformat.version<<3)+NTPformat.mode; //one byte Flag |
eunkyoungkim | 16:c830d2e7e0f5 | 57 | memcpy(ntpmessage,(void const*)(&Flag),1); |
eunkyoungkim | 16:c830d2e7e0f5 | 58 | |
eunkyoungkim | 16:c830d2e7e0f5 | 59 | return true; |
eunkyoungkim | 16:c830d2e7e0f5 | 60 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 61 | |
eunkyoungkim | 16:c830d2e7e0f5 | 62 | bool SNTPClient::getTime(datetime *time) { |
eunkyoungkim | 16:c830d2e7e0f5 | 63 | uint16_t startindex = 40; //last 8-byte of data_buf[size is 48 byte] is xmt, so the startindex should be 40 |
eunkyoungkim | 16:c830d2e7e0f5 | 64 | |
eunkyoungkim | 16:c830d2e7e0f5 | 65 | socket.sendTo(sntp_server, (char *)ntpmessage, sizeof(ntpmessage)); |
eunkyoungkim | 16:c830d2e7e0f5 | 66 | |
eunkyoungkim | 16:c830d2e7e0f5 | 67 | char in_buffer[MAX_SNTP_BUF_SIZE]; |
eunkyoungkim | 16:c830d2e7e0f5 | 68 | int n = socket.receiveFrom(sntp_server, in_buffer, sizeof(in_buffer)); |
eunkyoungkim | 16:c830d2e7e0f5 | 69 | |
eunkyoungkim | 16:c830d2e7e0f5 | 70 | if(n <= 0) |
eunkyoungkim | 16:c830d2e7e0f5 | 71 | return false; |
eunkyoungkim | 16:c830d2e7e0f5 | 72 | |
eunkyoungkim | 16:c830d2e7e0f5 | 73 | get_seconds_from_ntp_server((uint8_t *)in_buffer,startindex); |
eunkyoungkim | 16:c830d2e7e0f5 | 74 | |
eunkyoungkim | 16:c830d2e7e0f5 | 75 | time->yy = Nowdatetime.yy; |
eunkyoungkim | 16:c830d2e7e0f5 | 76 | time->mo = Nowdatetime.mo; |
eunkyoungkim | 16:c830d2e7e0f5 | 77 | time->dd = Nowdatetime.dd; |
eunkyoungkim | 16:c830d2e7e0f5 | 78 | time->hh = Nowdatetime.hh; |
eunkyoungkim | 16:c830d2e7e0f5 | 79 | time->mm = Nowdatetime.mm; |
eunkyoungkim | 16:c830d2e7e0f5 | 80 | time->ss = Nowdatetime.ss; |
eunkyoungkim | 16:c830d2e7e0f5 | 81 | |
eunkyoungkim | 16:c830d2e7e0f5 | 82 | return true; |
eunkyoungkim | 16:c830d2e7e0f5 | 83 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 84 | |
eunkyoungkim | 16:c830d2e7e0f5 | 85 | bool SNTPClient::close() { |
eunkyoungkim | 16:c830d2e7e0f5 | 86 | int ret = socket.close(); |
eunkyoungkim | 16:c830d2e7e0f5 | 87 | if (ret < 0) { |
eunkyoungkim | 16:c830d2e7e0f5 | 88 | ERR("Could not close"); |
eunkyoungkim | 16:c830d2e7e0f5 | 89 | return false; |
eunkyoungkim | 16:c830d2e7e0f5 | 90 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 91 | return true; |
eunkyoungkim | 16:c830d2e7e0f5 | 92 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 93 | |
eunkyoungkim | 16:c830d2e7e0f5 | 94 | char* SNTPClient::getHost() { |
eunkyoungkim | 16:c830d2e7e0f5 | 95 | return host; |
eunkyoungkim | 16:c830d2e7e0f5 | 96 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 97 | |
eunkyoungkim | 16:c830d2e7e0f5 | 98 | /* |
eunkyoungkim | 16:c830d2e7e0f5 | 99 | 00)UTC-12:00 Baker Island, Howland Island (both uninhabited) |
eunkyoungkim | 16:c830d2e7e0f5 | 100 | 01) UTC-11:00 American Samoa, Samoa |
eunkyoungkim | 16:c830d2e7e0f5 | 101 | 02) UTC-10:00 (Summer)French Polynesia (most), United States (Aleutian Islands, Hawaii) |
eunkyoungkim | 16:c830d2e7e0f5 | 102 | 03) UTC-09:30 Marquesas Islands |
eunkyoungkim | 16:c830d2e7e0f5 | 103 | 04) UTC-09:00 Gambier Islands;(Summer)United States (most of Alaska) |
eunkyoungkim | 16:c830d2e7e0f5 | 104 | 05) UTC-08:00 (Summer)Canada (most of British Columbia), Mexico (Baja California) |
eunkyoungkim | 16:c830d2e7e0f5 | 105 | 06) UTC-08:00 United States (California, most of Nevada, most of Oregon, Washington (state)) |
eunkyoungkim | 16:c830d2e7e0f5 | 106 | 07) UTC-07:00 Mexico (Sonora), United States (Arizona); (Summer)Canada (Alberta) |
eunkyoungkim | 16:c830d2e7e0f5 | 107 | 08) UTC-07:00 Mexico (Chihuahua), United States (Colorado) |
eunkyoungkim | 16:c830d2e7e0f5 | 108 | 09) UTC-06:00 Costa Rica, El Salvador, Ecuador (Galapagos Islands), Guatemala, Honduras |
eunkyoungkim | 16:c830d2e7e0f5 | 109 | 10) UTC-06:00 Mexico (most), Nicaragua;(Summer)Canada (Manitoba, Saskatchewan), United States (Illinois, most of Texas) |
eunkyoungkim | 16:c830d2e7e0f5 | 110 | 11) UTC-05:00 Colombia, Cuba, Ecuador (continental), Haiti, Jamaica, Panama, Peru |
eunkyoungkim | 16:c830d2e7e0f5 | 111 | 12) UTC-05:00 (Summer)Canada (most of Ontario, most of Quebec) |
eunkyoungkim | 16:c830d2e7e0f5 | 112 | 13) UTC-05:00 United States (most of Florida, Georgia, Massachusetts, most of Michigan, New York, North Carolina, Ohio, Washington D.C.) |
eunkyoungkim | 16:c830d2e7e0f5 | 113 | 14) UTC-04:30 Venezuela |
eunkyoungkim | 16:c830d2e7e0f5 | 114 | 15) UTC-04:00 Bolivia, Brazil (Amazonas), Chile (continental), Dominican Republic, Canada (Nova Scotia), Paraguay, |
eunkyoungkim | 16:c830d2e7e0f5 | 115 | 16) UTC-04:00 Puerto Rico, Trinidad and Tobago |
eunkyoungkim | 16:c830d2e7e0f5 | 116 | 17) UTC-03:30 Canada (Newfoundland) |
eunkyoungkim | 16:c830d2e7e0f5 | 117 | 18) UTC-03:00 Argentina; (Summer) Brazil (Brasilia, Rio de Janeiro, Sao Paulo), most of Greenland, Uruguay |
eunkyoungkim | 16:c830d2e7e0f5 | 118 | 19) UTC-02:00 Brazil (Fernando de Noronha), South Georgia and the South Sandwich Islands |
eunkyoungkim | 16:c830d2e7e0f5 | 119 | 20) UTC-01:00 Portugal (Azores), Cape Verde |
eunkyoungkim | 16:c830d2e7e0f5 | 120 | 21) UTC±00:00 Cote d'Ivoire, Faroe Islands, Ghana, Iceland, Senegal; (Summer) Ireland, Portugal (continental and Madeira) |
eunkyoungkim | 16:c830d2e7e0f5 | 121 | 22) UTC±00:00 Spain (Canary Islands), Morocco, United Kingdom |
eunkyoungkim | 16:c830d2e7e0f5 | 122 | 23) UTC+01:00 Angola, Cameroon, Nigeria, Tunisia; (Summer)Albania, Algeria, Austria, Belgium, Bosnia and Herzegovina, |
eunkyoungkim | 16:c830d2e7e0f5 | 123 | 24) UTC+01:00 Spain (continental), Croatia, Czech Republic, Denmark, Germany, Hungary, Italy, Kinshasa, Kosovo, |
eunkyoungkim | 16:c830d2e7e0f5 | 124 | 25) UTC+01:00 Macedonia, France (metropolitan), the Netherlands, Norway, Poland, Serbia, Slovakia, Slovenia, Sweden, Switzerland |
eunkyoungkim | 16:c830d2e7e0f5 | 125 | 26) UTC+02:00 Libya, Egypt, Malawi, Mozambique, South Africa, Zambia, Zimbabwe, (Summer)Bulgaria, Cyprus, Estonia, |
eunkyoungkim | 16:c830d2e7e0f5 | 126 | 27) UTC+02:00 Finland, Greece, Israel, Jordan, Latvia, Lebanon, Lithuania, Moldova, Palestine, Romania, Syria, Turkey, Ukraine |
eunkyoungkim | 16:c830d2e7e0f5 | 127 | 28) UTC+03:00 Belarus, Djibouti, Eritrea, Ethiopia, Iraq, Kenya, Madagascar, Russia (Kaliningrad Oblast), Saudi Arabia, |
eunkyoungkim | 16:c830d2e7e0f5 | 128 | 29) UTC+03:00 South Sudan, Sudan, Somalia, South Sudan, Tanzania, Uganda, Yemen |
eunkyoungkim | 16:c830d2e7e0f5 | 129 | 30) UTC+03:30 (Summer)Iran |
eunkyoungkim | 16:c830d2e7e0f5 | 130 | 31) UTC+04:00 Armenia, Azerbaijan, Georgia, Mauritius, Oman, Russia (European), Seychelles, United Arab Emirates |
eunkyoungkim | 16:c830d2e7e0f5 | 131 | 32) UTC+04:30 Afghanistan |
eunkyoungkim | 16:c830d2e7e0f5 | 132 | 33) UTC+05:00 Kazakhstan (West), Maldives, Pakistan, Uzbekistan |
eunkyoungkim | 16:c830d2e7e0f5 | 133 | 34) UTC+05:30 India, Sri Lanka |
eunkyoungkim | 16:c830d2e7e0f5 | 134 | 35) UTC+05:45 Nepal |
eunkyoungkim | 16:c830d2e7e0f5 | 135 | 36) UTC+06:00 Kazakhstan (most), Bangladesh, Russia (Ural: Sverdlovsk Oblast, Chelyabinsk Oblast) |
eunkyoungkim | 16:c830d2e7e0f5 | 136 | 37) UTC+06:30 Cocos Islands, Myanmar |
eunkyoungkim | 16:c830d2e7e0f5 | 137 | 38) UTC+07:00 Jakarta, Russia (Novosibirsk Oblast), Thailand, Vietnam |
eunkyoungkim | 16:c830d2e7e0f5 | 138 | 39) UTC+08:00 China, Hong Kong, Russia (Krasnoyarsk Krai), Malaysia, Philippines, Singapore, Taiwan, most of Mongolia, Western Australia |
eunkyoungkim | 16:c830d2e7e0f5 | 139 | 40) UTC+09:00 Korea, East Timor, Russia (Irkutsk Oblast), Japan |
eunkyoungkim | 16:c830d2e7e0f5 | 140 | 41) UTC+09:30 Australia (Northern Territory);(Summer)Australia (South Australia)) |
eunkyoungkim | 16:c830d2e7e0f5 | 141 | 42) UTC+10:00 Russia (Zabaykalsky Krai); (Summer)Australia (New South Wales, Queensland, Tasmania, Victoria) |
eunkyoungkim | 16:c830d2e7e0f5 | 142 | 43) UTC+10:30 Lord Howe Island |
eunkyoungkim | 16:c830d2e7e0f5 | 143 | 44) UTC+11:00 New Caledonia, Russia (Primorsky Krai), Solomon Islands |
eunkyoungkim | 16:c830d2e7e0f5 | 144 | 45) UTC+11:30 Norfolk Island |
eunkyoungkim | 16:c830d2e7e0f5 | 145 | 46) UTC+12:00 Fiji, Russia (Kamchatka Krai);(Summer)New Zealand |
eunkyoungkim | 16:c830d2e7e0f5 | 146 | 47) UTC+12:45 (Summer)New Zealand |
eunkyoungkim | 16:c830d2e7e0f5 | 147 | 48) UTC+13:00 Tonga |
eunkyoungkim | 16:c830d2e7e0f5 | 148 | 49) UTC+14:00 Kiribati (Line Islands) |
eunkyoungkim | 16:c830d2e7e0f5 | 149 | */ |
eunkyoungkim | 16:c830d2e7e0f5 | 150 | void SNTPClient::get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx) |
eunkyoungkim | 16:c830d2e7e0f5 | 151 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 152 | tstamp seconds = 0; |
eunkyoungkim | 16:c830d2e7e0f5 | 153 | uint8_t i=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 154 | for (i = 0; i < 4; i++) |
eunkyoungkim | 16:c830d2e7e0f5 | 155 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 156 | seconds = (seconds << 8) | buf[idx + i]; |
eunkyoungkim | 16:c830d2e7e0f5 | 157 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 158 | switch (tz) // Time Zone |
eunkyoungkim | 16:c830d2e7e0f5 | 159 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 160 | case 0: |
eunkyoungkim | 16:c830d2e7e0f5 | 161 | seconds -= 12*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 162 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 163 | case 1: |
eunkyoungkim | 16:c830d2e7e0f5 | 164 | seconds -= 11*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 165 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 166 | case 2: |
eunkyoungkim | 16:c830d2e7e0f5 | 167 | seconds -= 10*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 168 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 169 | case 3: |
eunkyoungkim | 16:c830d2e7e0f5 | 170 | seconds -= (9*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 171 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 172 | case 4: |
eunkyoungkim | 16:c830d2e7e0f5 | 173 | seconds -= 9*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 174 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 175 | case 5: |
eunkyoungkim | 16:c830d2e7e0f5 | 176 | case 6: |
eunkyoungkim | 16:c830d2e7e0f5 | 177 | seconds -= 8*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 178 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 179 | case 7: |
eunkyoungkim | 16:c830d2e7e0f5 | 180 | case 8: |
eunkyoungkim | 16:c830d2e7e0f5 | 181 | seconds -= 7*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 182 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 183 | case 9: |
eunkyoungkim | 16:c830d2e7e0f5 | 184 | case 10: |
eunkyoungkim | 16:c830d2e7e0f5 | 185 | seconds -= 6*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 186 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 187 | case 11: |
eunkyoungkim | 16:c830d2e7e0f5 | 188 | case 12: |
eunkyoungkim | 16:c830d2e7e0f5 | 189 | case 13: |
eunkyoungkim | 16:c830d2e7e0f5 | 190 | seconds -= 5*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 191 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 192 | case 14: |
eunkyoungkim | 16:c830d2e7e0f5 | 193 | seconds -= (4*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 194 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 195 | case 15: |
eunkyoungkim | 16:c830d2e7e0f5 | 196 | case 16: |
eunkyoungkim | 16:c830d2e7e0f5 | 197 | seconds -= 4*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 198 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 199 | case 17: |
eunkyoungkim | 16:c830d2e7e0f5 | 200 | seconds -= (3*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 201 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 202 | case 18: |
eunkyoungkim | 16:c830d2e7e0f5 | 203 | seconds -= 3*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 204 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 205 | case 19: |
eunkyoungkim | 16:c830d2e7e0f5 | 206 | seconds -= 2*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 207 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 208 | case 20: |
eunkyoungkim | 16:c830d2e7e0f5 | 209 | seconds -= 1*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 210 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 211 | case 21: //? |
eunkyoungkim | 16:c830d2e7e0f5 | 212 | case 22: |
eunkyoungkim | 16:c830d2e7e0f5 | 213 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 214 | case 23: |
eunkyoungkim | 16:c830d2e7e0f5 | 215 | case 24: |
eunkyoungkim | 16:c830d2e7e0f5 | 216 | case 25: |
eunkyoungkim | 16:c830d2e7e0f5 | 217 | seconds += 1*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 218 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 219 | case 26: |
eunkyoungkim | 16:c830d2e7e0f5 | 220 | case 27: |
eunkyoungkim | 16:c830d2e7e0f5 | 221 | seconds += 2*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 222 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 223 | case 28: |
eunkyoungkim | 16:c830d2e7e0f5 | 224 | case 29: |
eunkyoungkim | 16:c830d2e7e0f5 | 225 | seconds += 3*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 226 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 227 | case 30: |
eunkyoungkim | 16:c830d2e7e0f5 | 228 | seconds += (3*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 229 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 230 | case 31: |
eunkyoungkim | 16:c830d2e7e0f5 | 231 | seconds += 4*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 232 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 233 | case 32: |
eunkyoungkim | 16:c830d2e7e0f5 | 234 | seconds += (4*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 235 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 236 | case 33: |
eunkyoungkim | 16:c830d2e7e0f5 | 237 | seconds += 5*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 238 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 239 | case 34: |
eunkyoungkim | 16:c830d2e7e0f5 | 240 | seconds += (5*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 241 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 242 | case 35: |
eunkyoungkim | 16:c830d2e7e0f5 | 243 | seconds += (5*3600+45*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 244 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 245 | case 36: |
eunkyoungkim | 16:c830d2e7e0f5 | 246 | seconds += 6*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 247 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 248 | case 37: |
eunkyoungkim | 16:c830d2e7e0f5 | 249 | seconds += (6*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 250 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 251 | case 38: |
eunkyoungkim | 16:c830d2e7e0f5 | 252 | seconds += 7*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 253 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 254 | case 39: |
eunkyoungkim | 16:c830d2e7e0f5 | 255 | seconds += 8*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 256 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 257 | case 40: |
eunkyoungkim | 16:c830d2e7e0f5 | 258 | seconds += 9*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 259 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 260 | case 41: |
eunkyoungkim | 16:c830d2e7e0f5 | 261 | seconds += (9*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 262 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 263 | case 42: |
eunkyoungkim | 16:c830d2e7e0f5 | 264 | seconds += 10*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 265 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 266 | case 43: |
eunkyoungkim | 16:c830d2e7e0f5 | 267 | seconds += (10*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 268 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 269 | case 44: |
eunkyoungkim | 16:c830d2e7e0f5 | 270 | seconds += 11*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 271 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 272 | case 45: |
eunkyoungkim | 16:c830d2e7e0f5 | 273 | seconds += (11*3600+30*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 274 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 275 | case 46: |
eunkyoungkim | 16:c830d2e7e0f5 | 276 | seconds += 12*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 277 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 278 | case 47: |
eunkyoungkim | 16:c830d2e7e0f5 | 279 | seconds += (12*3600+45*60); |
eunkyoungkim | 16:c830d2e7e0f5 | 280 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 281 | case 48: |
eunkyoungkim | 16:c830d2e7e0f5 | 282 | seconds += 13*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 283 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 284 | case 49: |
eunkyoungkim | 16:c830d2e7e0f5 | 285 | seconds += 14*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 286 | break; |
eunkyoungkim | 16:c830d2e7e0f5 | 287 | |
eunkyoungkim | 16:c830d2e7e0f5 | 288 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 289 | |
eunkyoungkim | 16:c830d2e7e0f5 | 290 | //calculation for date |
eunkyoungkim | 16:c830d2e7e0f5 | 291 | calcdatetime(seconds); |
eunkyoungkim | 16:c830d2e7e0f5 | 292 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 293 | |
eunkyoungkim | 16:c830d2e7e0f5 | 294 | void SNTPClient::calcdatetime(tstamp seconds) |
eunkyoungkim | 16:c830d2e7e0f5 | 295 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 296 | uint8_t yf=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 297 | tstamp n=0,d=0,total_d=0,rz=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 298 | uint16_t y=0,r=0,yr=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 299 | signed long long yd=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 300 | |
eunkyoungkim | 16:c830d2e7e0f5 | 301 | n = seconds; |
eunkyoungkim | 16:c830d2e7e0f5 | 302 | total_d = seconds/(SECS_PERDAY); |
eunkyoungkim | 16:c830d2e7e0f5 | 303 | d=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 304 | uint32_t p_year_total_sec=SECS_PERDAY*365; |
eunkyoungkim | 16:c830d2e7e0f5 | 305 | uint32_t r_year_total_sec=SECS_PERDAY*366; |
eunkyoungkim | 16:c830d2e7e0f5 | 306 | while(n>=p_year_total_sec) |
eunkyoungkim | 16:c830d2e7e0f5 | 307 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 308 | if((EPOCH+r)%400==0 || ((EPOCH+r)%100!=0 && (EPOCH+r)%4==0)) |
eunkyoungkim | 16:c830d2e7e0f5 | 309 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 310 | n = n -(r_year_total_sec); |
eunkyoungkim | 16:c830d2e7e0f5 | 311 | d = d + 366; |
eunkyoungkim | 16:c830d2e7e0f5 | 312 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 313 | else |
eunkyoungkim | 16:c830d2e7e0f5 | 314 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 315 | n = n - (p_year_total_sec); |
eunkyoungkim | 16:c830d2e7e0f5 | 316 | d = d + 365; |
eunkyoungkim | 16:c830d2e7e0f5 | 317 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 318 | r+=1; |
eunkyoungkim | 16:c830d2e7e0f5 | 319 | y+=1; |
eunkyoungkim | 16:c830d2e7e0f5 | 320 | |
eunkyoungkim | 16:c830d2e7e0f5 | 321 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 322 | |
eunkyoungkim | 16:c830d2e7e0f5 | 323 | y += EPOCH; |
eunkyoungkim | 16:c830d2e7e0f5 | 324 | |
eunkyoungkim | 16:c830d2e7e0f5 | 325 | Nowdatetime.yy = y; |
eunkyoungkim | 16:c830d2e7e0f5 | 326 | |
eunkyoungkim | 16:c830d2e7e0f5 | 327 | yd=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 328 | yd = total_d - d; |
eunkyoungkim | 16:c830d2e7e0f5 | 329 | |
eunkyoungkim | 16:c830d2e7e0f5 | 330 | yf=1; |
eunkyoungkim | 16:c830d2e7e0f5 | 331 | while(yd>=28) |
eunkyoungkim | 16:c830d2e7e0f5 | 332 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 333 | |
eunkyoungkim | 16:c830d2e7e0f5 | 334 | if(yf==1 || yf==3 || yf==5 || yf==7 || yf==8 || yf==10 || yf==12) |
eunkyoungkim | 16:c830d2e7e0f5 | 335 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 336 | yd -= 31; |
eunkyoungkim | 16:c830d2e7e0f5 | 337 | if(yd<0)break; |
eunkyoungkim | 16:c830d2e7e0f5 | 338 | rz += 31; |
eunkyoungkim | 16:c830d2e7e0f5 | 339 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 340 | |
eunkyoungkim | 16:c830d2e7e0f5 | 341 | if (yf==2) |
eunkyoungkim | 16:c830d2e7e0f5 | 342 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 343 | if (y%400==0 || (y%100!=0 && y%4==0)) |
eunkyoungkim | 16:c830d2e7e0f5 | 344 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 345 | yd -= 29; |
eunkyoungkim | 16:c830d2e7e0f5 | 346 | if(yd<0)break; |
eunkyoungkim | 16:c830d2e7e0f5 | 347 | rz += 29; |
eunkyoungkim | 16:c830d2e7e0f5 | 348 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 349 | else |
eunkyoungkim | 16:c830d2e7e0f5 | 350 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 351 | yd -= 28; |
eunkyoungkim | 16:c830d2e7e0f5 | 352 | if(yd<0)break; |
eunkyoungkim | 16:c830d2e7e0f5 | 353 | rz += 28; |
eunkyoungkim | 16:c830d2e7e0f5 | 354 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 355 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 356 | if(yf==4 || yf==6 || yf==9 || yf==11 ) |
eunkyoungkim | 16:c830d2e7e0f5 | 357 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 358 | yd -= 30; |
eunkyoungkim | 16:c830d2e7e0f5 | 359 | if(yd<0)break; |
eunkyoungkim | 16:c830d2e7e0f5 | 360 | rz += 30; |
eunkyoungkim | 16:c830d2e7e0f5 | 361 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 362 | yf += 1; |
eunkyoungkim | 16:c830d2e7e0f5 | 363 | |
eunkyoungkim | 16:c830d2e7e0f5 | 364 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 365 | Nowdatetime.mo=yf; |
eunkyoungkim | 16:c830d2e7e0f5 | 366 | yr = total_d-d-rz; |
eunkyoungkim | 16:c830d2e7e0f5 | 367 | |
eunkyoungkim | 16:c830d2e7e0f5 | 368 | yr += 1; |
eunkyoungkim | 16:c830d2e7e0f5 | 369 | |
eunkyoungkim | 16:c830d2e7e0f5 | 370 | Nowdatetime.dd=yr; |
eunkyoungkim | 16:c830d2e7e0f5 | 371 | |
eunkyoungkim | 16:c830d2e7e0f5 | 372 | //calculation for time |
eunkyoungkim | 16:c830d2e7e0f5 | 373 | seconds = seconds%SECS_PERDAY; |
eunkyoungkim | 16:c830d2e7e0f5 | 374 | Nowdatetime.hh = seconds/3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 375 | Nowdatetime.mm = (seconds%3600)/60; |
eunkyoungkim | 16:c830d2e7e0f5 | 376 | Nowdatetime.ss = (seconds%3600)%60; |
eunkyoungkim | 16:c830d2e7e0f5 | 377 | |
eunkyoungkim | 16:c830d2e7e0f5 | 378 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 379 | |
eunkyoungkim | 16:c830d2e7e0f5 | 380 | tstamp SNTPClient::changedatetime_to_seconds(void) |
eunkyoungkim | 16:c830d2e7e0f5 | 381 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 382 | tstamp seconds=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 383 | uint32_t total_day=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 384 | uint16_t i=0,run_year_cnt=0,l=0; |
eunkyoungkim | 16:c830d2e7e0f5 | 385 | |
eunkyoungkim | 16:c830d2e7e0f5 | 386 | l = Nowdatetime.yy;//low |
eunkyoungkim | 16:c830d2e7e0f5 | 387 | |
eunkyoungkim | 16:c830d2e7e0f5 | 388 | |
eunkyoungkim | 16:c830d2e7e0f5 | 389 | for(i=EPOCH;i<l;i++) |
eunkyoungkim | 16:c830d2e7e0f5 | 390 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 391 | if((i%400==0) || ((i%100!=0) && (i%4==0))) |
eunkyoungkim | 16:c830d2e7e0f5 | 392 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 393 | run_year_cnt += 1; |
eunkyoungkim | 16:c830d2e7e0f5 | 394 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 395 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 396 | |
eunkyoungkim | 16:c830d2e7e0f5 | 397 | total_day=(l-EPOCH-run_year_cnt)*365+run_year_cnt*366; |
eunkyoungkim | 16:c830d2e7e0f5 | 398 | |
eunkyoungkim | 16:c830d2e7e0f5 | 399 | for(i=1;i<=Nowdatetime.mo;i++) |
eunkyoungkim | 16:c830d2e7e0f5 | 400 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 401 | if(i==5 || i==7 || i==10 || i==12) |
eunkyoungkim | 16:c830d2e7e0f5 | 402 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 403 | total_day += 30; |
eunkyoungkim | 16:c830d2e7e0f5 | 404 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 405 | if (i==3) |
eunkyoungkim | 16:c830d2e7e0f5 | 406 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 407 | if (l%400==0 && l%100!=0 && l%4==0) |
eunkyoungkim | 16:c830d2e7e0f5 | 408 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 409 | total_day += 29; |
eunkyoungkim | 16:c830d2e7e0f5 | 410 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 411 | else |
eunkyoungkim | 16:c830d2e7e0f5 | 412 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 413 | total_day += 28; |
eunkyoungkim | 16:c830d2e7e0f5 | 414 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 415 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 416 | if(i==2 || i==4 || i==6 || i==8 || i==9 || i==11) |
eunkyoungkim | 16:c830d2e7e0f5 | 417 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 418 | total_day += 31; |
eunkyoungkim | 16:c830d2e7e0f5 | 419 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 420 | } |
eunkyoungkim | 16:c830d2e7e0f5 | 421 | |
eunkyoungkim | 16:c830d2e7e0f5 | 422 | seconds = (total_day+Nowdatetime.dd-1)*24*3600; |
eunkyoungkim | 16:c830d2e7e0f5 | 423 | seconds += Nowdatetime.ss;//seconds |
eunkyoungkim | 16:c830d2e7e0f5 | 424 | seconds += Nowdatetime.mm*60;//minute |
eunkyoungkim | 16:c830d2e7e0f5 | 425 | seconds += Nowdatetime.hh*3600;//hour |
eunkyoungkim | 16:c830d2e7e0f5 | 426 | |
eunkyoungkim | 16:c830d2e7e0f5 | 427 | return seconds; |
eunkyoungkim | 16:c830d2e7e0f5 | 428 | } |