123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- #include "iot_sntp.h"
- #include <stdint.h>
- #include <stddef.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdio.h>
- #include <unistd.h>
- #include "ohos_init.h"
- #include "ohos_types.h"
- #include "cmsis_os2.h"
- #include "lwip/sockets.h"
- #include "kv_store.h"
- #ifndef NTP_SERVER_IP
- #define NTP_SERVER_IP "120.24.166.46"
- #endif
- #ifndef NTP_SERVER_PORT
- #define NTP_SERVER_PORT 123
- #endif
- #define SECONDS_FROM_1900_TO_1970 (2208988800L)
- #pragma pack(1)
- typedef struct {
- uint32_t low;
- uint32_t high;
- }TimeStamp;
- typedef struct {
- uint8_t liVnMode;
- uint8_t stratum;
- uint8_t poll;
- uint8_t precision;
- uint32_t rootDelay;
- uint32_t rootDispersion;
- uint8_t referenceID[4];
- TimeStamp refecenceTimestamp;
- TimeStamp originateTimestamp;
- TimeStamp receiveTimestamp;
- TimeStamp transmitTimestamp;
- }SntpPacket;
- #pragma pack()
- #define CN_TIMEZONE_MAXIMUM (11)
- #define CN_TIMEZONE_MINIMUM (-11)
- #define CN_SNTP_RETRYTIMES 10
- int SntpGetRtcTime(int localTimeZone, struct tm *rtcTime)
- {
- int ret = -1;
- int sockfd = -1;
- struct sockaddr_in server;
- socklen_t addrLen;
- fd_set set;
- struct timeval timeout;
- struct timeval tv;
- int nRet;
- SntpPacket sntpPacket;
- struct tm *time = NULL;
- int tryTimes = CN_SNTP_RETRYTIMES;
-
- if ((localTimeZone > CN_TIMEZONE_MAXIMUM) || (localTimeZone < CN_TIMEZONE_MINIMUM) ||\
- rtcTime == NULL) {
- return ret;
- }
-
- bzero(&server, sizeof(server));
- server.sin_family = AF_INET;
- server.sin_port = htons(NTP_SERVER_PORT);
- addrLen = sizeof(server);
- if (inet_aton(NTP_SERVER_IP, &server.sin_addr) <= 0) {
- printf("inet_pton error\n");
- return ret;
- }
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- return ret;
- }
-
- do{
- tryTimes--;
-
- bzero((char * )&sntpPacket, sizeof(sntpPacket));
- sntpPacket.liVnMode = 0x1b;
- if (sendto(sockfd, (char *) &sntpPacket, sizeof(sntpPacket), 0,
- (struct sockaddr*) &server, addrLen) < 0) {
- printf("sendto error!\n");
- continue;
- }
-
- FD_ZERO(&set);
- FD_SET(sockfd, &set);
- timeout.tv_sec = 0;
- timeout.tv_usec = 500000;
- nRet = select(sockfd + 1, &set, NULL, NULL, &timeout);
- if (nRet < 0) {
- printf("select error!\n");
- continue;
- } else if (nRet == 0) {
- printf("time out!\n");
- continue;
- } else {
- if (FD_ISSET(sockfd, &set)) {
- if (recvfrom(sockfd, (char *) &sntpPacket, sizeof(sntpPacket),
- 0, (struct sockaddr*) &server, &addrLen) < 0) {
- printf("recv error!\n");
- continue;
- } else {
- tv.tv_usec = 0;
- tv.tv_sec = ntohl(sntpPacket.transmitTimestamp.low);
- tv.tv_sec -= SECONDS_FROM_1900_TO_1970;
- tv.tv_sec += (localTimeZone * 3600);
- if((time = gmtime_r((const long int *) &tv.tv_sec, rtcTime)) != NULL) {
- ret = 0;
- break;
- }
- }
- }
- }
- } while (tryTimes > 0);
- closesocket(sockfd);
- return ret;
- }
|