Network Services

Dependents:   PwrCond_mbed5

Fork of W5500Interface_K22F by Andrew McCartney

Committer:
dgriffin65
Date:
Thu Jun 15 20:29:03 2017 +0000
Revision:
15:14382459c8b7
Converted to a single library

Who changed what in which revision?

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