time_t parse_date (const char **a) { const char *d = *a; struct tm raw_time; if (skip_str (&d, "Mon ") && skip_str (&d, "Tue ") && skip_str (&d, "Wed ") && skip_str (&d, "Thu ") && skip_str (&d, "Fri ") && skip_str (&d, "Sat ") && skip_str (&d, "Sun ")) { return (time_t)-1; } /* now get the month */ if (((raw_time.tm_mon = month_to_num (d)) == -1) || (d[3] != ' ')) { return (time_t)-1; } d += 4; /* ... the day */ if (((raw_time.tm_mday = asc_to_num (d, 2)) == -1) || (d[2] != ' ')) { return (time_t)-1; } d += 3; /* ... the hours */ if (((raw_time.tm_hour = asc_to_num (d, 2)) == -1) || (d[2] != ':')) { return (time_t)-1; } d += 3; /* ... the minutes */ if (((raw_time.tm_min = asc_to_num (d, 2)) == -1) || (d[2] != ':')) { return (time_t)-1; } d += 3; /* ... the seconds */ if (((raw_time.tm_sec = asc_to_num (d, 2)) == -1) || (d[2] != ' ')) { return (time_t)-1; } d += 3; /* ... and the year */ if (((raw_time.tm_year = asc_to_num (d, 4)) == -1)) { return (time_t)-1; } d += 4; raw_time.tm_year -= 1900; raw_time.tm_isdst = -1; *a = d; return mktime (&raw_time); }
int Exosite_SyncTime() { char day[11]; char time[9]; int http_status = 0; int strLen; char *testStr = "GET /ip HTTP/1.1\r\n"; unsigned char serverAddr[6] = {54, 183, 115, 21, 0, 80}; char strBuf[RX_SIZE]; DateTime datetime; if (!exosite_initialized) { return -1; } long sock = connect_to_exosite_with_server_addr("", serverAddr); if (sock < 0) { return -1; } exoHAL_SocketSend(sock, testStr, strlen(testStr)); sendLine(sock, HOST_LINE, NULL); sendLine(sock, ACCEPT_LINE, "\r\n"); http_status = get_http_status(sock); if (200 != http_status) return -1; strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); exoHAL_SocketClose(sock); if(strLen <= 0) return -1; if(parse_datetime(strBuf, strLen, &datetime) < 0) return -1; sprintf(day, "%s/%02d/%s", datetime.dayStr, month_to_num(datetime.monStr), datetime.yearStr); sprintf(time, "%s:%s:%s", datetime.hourStr, datetime.minStr, datetime.secStr); if(AtLibGs_SetTime(day, time) != ATLIBGS_MSG_ID_OK) return -1; return 0; }