Browse Source

time接口使用错误,改正
程序中还有多处线程不安全接口需修改调用

guohui 1 year ago
parent
commit
a8e6e7f3ae
5 changed files with 153 additions and 26 deletions
  1. 5 6
      pro/src/app.c
  2. 4 4
      pro/src/appweb_handle.c
  3. 1 1
      pro/src/cascade_slave_tcp.h
  4. 134 12
      pro/src/common.c
  5. 9 3
      pro/src/common.h

+ 5 - 6
pro/src/app.c

@@ -1784,7 +1784,7 @@ static void* ntp_thread(void *arg)
 {
     int r=0,flag=0;
     NTPClient  *pc=NULL;
-    time_t time1=0,time2=0,ntp_time;
+    time_t time1=0,time2=0,utc_time;
     thread_handle_t *h=(thread_handle_t*)arg;
     GlobalDeviceManager *dm=(GlobalDeviceManager*)h->arg;;
     NTPManager *pntp=&_globalNTPManager;
@@ -1823,13 +1823,12 @@ static void* ntp_thread(void *arg)
                 pc = createNTPClient(pntp->ntpaddress, pntp->port);
                 if (pc)
                 {
-                    if (getCurrentTimeFromNTP(pc, &ntp_time) == 0)
+                    if (getCurrentTimeFromNTP(pc, &utc_time) == 0)
                     {
                         time_t host_time = time(NULL);
-                        log_d("host_time: %ld, ntp_time: %ld", host_time, ntp_time);
-                        struct tm *ptm = localtime(&ntp_time);
-                       // set_time(ptm);
-                        set_time_zone(ptm, pntp->tzone);
+                        log_d("host_time: %ld, ntp_time: %ld", host_time, utc_time);
+                        //set_utctime(utc_time);
+                        set_utctime_zone(utc_time, pntp->tzone);
                     }
                     else
                     {

+ 4 - 4
pro/src/appweb_handle.c

@@ -4032,7 +4032,6 @@ static void serviceManagement(void *conn)
         }
         if (_globalNTPManager.mode == 0)
         {
-            time_t t;
             memset(tempstr, 0, sizeof(tempstr));
             p = strstr(_globalNTPManager.time, "-");
             if (p == NULL)
@@ -4043,7 +4042,7 @@ static void serviceManagement(void *conn)
             memset(tempstr, 0, sizeof(tempstr));
             memcpy(tempstr, _globalNTPManager.time, p - _globalNTPManager.time);
             struct tm tm_new;
-            tm_new.tm_year = atoi(tempstr) - 1900;
+            tm_new.tm_year = atoi(tempstr);
             char* p1 = strstr(&p[1], "-");
             if (p1 == NULL)
             {
@@ -4053,7 +4052,7 @@ static void serviceManagement(void *conn)
             int nSize= p1 - p - 1;
             memset(tempstr, 0, sizeof(tempstr));
             memcpy(tempstr, &p[1],nSize);
-            tm_new.tm_mon = atoi(tempstr) - 1;
+            tm_new.tm_mon = atoi(tempstr);
             p = p1;
             p1 = strstr(&p[2], " ");//个位数第一位有可能是空格所以需要找空格后一个
             if (p1 == NULL)
@@ -4100,11 +4099,12 @@ static void serviceManagement(void *conn)
             memset(tempstr, 0, sizeof(tempstr));
             memcpy(tempstr, &p[0], 2);
             tm_new.tm_sec = atoi(tempstr);
-            t = mktime(&tm_new);
+
             set_time_zone(&tm_new, _globalNTPManager.tzone);
         }
         else if (_globalNTPManager.mode == 1)
         {
+            set_zone(_globalNTPManager.tzone);
             ntp_sync_flag = 1;
         }
 

+ 1 - 1
pro/src/cascade_slave_tcp.h

@@ -8,7 +8,7 @@
 
 
 #define CASCADE_SLAVE_CONNECTION                5
-#define CASCADE_SLAVE_PORT                      6666
+#define CASCADE_SLAVE_PORT                      502
 
 
 

+ 134 - 12
pro/src/common.c

@@ -8,6 +8,8 @@
 #include <net/if.h>
 #include <arpa/inet.h>
 #include <sys/vfs.h>
+#include <sys/ioctl.h>
+#include <linux/rtc.h>
 
 #undef LOG_TAG
 #define LOG_TAG    "main"
@@ -273,14 +275,86 @@ void get_disk_occupy(char ** reused)
 }
 
 
-int set_time(struct tm* time)
+static int set_rtc(time_t t)
 {
-    char buf[256];
+    int r=0,fd;
+    struct rtc_time *rtc;
 
-    sprintf(buf, "date -s \"%04d-%02d-%02d %02d:%02d:%02d\" && hwclock -w -f /dev/rtc0 ", time->tm_year + 1900, time->tm_mon + 1, 
-                                                  time->tm_mday, time->tm_hour, time->tm_min, time->tm_sec);
-    log_i("set_time: %s", buf);
-    return system(buf);
+    struct tm *tm = gmtime(&t);
+    fd = open("/dev/rtc0", O_RDONLY);
+    if (fd < 0) {
+        perror("open rtc failed\n");
+        return -1;
+    }
+ 
+    rtc = (struct rtc_time*)tm;
+    if (ioctl(fd, RTC_SET_TIME, rtc) < 0) {
+        perror("ioctl RTC_SET_TIME failed\n");
+        r = -1;
+    }
+    else {
+        printf("set rtc ok!\n");
+    }
+    close(fd);
+    
+    return r;
+}
+
+#define MY_MINUTE  60
+#define MY_HOUR    (60*MY_MINUTE)
+#define MY_DAY     (24*MY_HOUR)
+#define MY_YEAR    (365*MY_DAY)
+static int my_month[12] = {
+    0,
+    MY_DAY*(31),
+    MY_DAY*(31+29),
+    MY_DAY*(31+29+31),
+    MY_DAY*(31+29+31+30),
+    MY_DAY*(31+29+31+30+31),
+    MY_DAY*(31+29+31+30+31+30),
+    MY_DAY*(31+29+31+30+31+30+31),
+    MY_DAY*(31+29+31+30+31+30+31+31),
+    MY_DAY*(31+29+31+30+31+30+31+31+30),
+    MY_DAY*(31+29+31+30+31+30+31+31+30+31),
+    MY_DAY*(31+29+31+30+31+30+31+31+30+31+30)
+};
+time_t mktime2(struct tm *t, int zone)
+{
+    time_t res,year;
+
+    year = t->tm_year - 70;
+    /* magic offsets (y+1) needed to get leapyears right.*/
+    res = MY_YEAR*year + MY_DAY*((year+1)/4);
+    res += my_month[t->tm_mon];
+    /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
+    if (t->tm_mon>1 && ((year+2)%4))
+        res -= MY_DAY;
+    res += MY_DAY*(t->tm_mday-1);
+    res += MY_HOUR*t->tm_hour;
+    res += MY_MINUTE*t->tm_min;
+    res += t->tm_sec;
+
+    res -= zone*3600;
+
+    return res;
+}
+
+
+int set_time(struct tm *t, int zone)
+{
+    if(!t) {
+        return -1;
+    }
+    t->tm_year -= 1900;
+    t->tm_mon -= 1;
+    time_t utc = mktime2(t, zone);
+    return stime(&utc);
+}
+
+
+int set_utctime(time_t t)
+{
+    return stime(&t);
 }
 
 
@@ -288,6 +362,7 @@ int set_zone(int zone)
 {
     char buf[256];
 
+#if 1
     switch(zone) {
         case -11:
             sprintf(buf, "ln -sf /usr/share/zoneinfo/Pacific/Midway /etc/localtime");
@@ -329,7 +404,7 @@ int set_zone(int zone)
             sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime");
             break;
         case 2:
-            sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Athens /etc/localtime");
+            sprintf(buf, "ln -sf /usr/share/zoneinfo/EET /etc/localtime");
             break;
         case 3:
             sprintf(buf, "ln -sf /usr/share/zoneinfo/Europe/Moscow /etc/localtime");
@@ -364,28 +439,75 @@ int set_zone(int zone)
         default:
             return -1;
     }
+#else
+    sprintf(buf, "ln -sf /usr/share/zoneinfo/Etc/GMT%s%d /etc/localtime", (zone<0)?"-":"+", abs(zone));
+#endif
+
     log_i("set_zone: %s", buf);
     return system(buf);
 }
 
 
-int set_time_zone(struct tm* time, int zone)
+void print_time(char *s, struct tm *tm)
+{
+    printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+}
+
+void print_utc(char *s, time_t t)
+{
+    struct tm *tm=gmtime(&t);
+    print_time(s, tm);
+}
+
+
+int set_time_zone(struct tm *t, int zone)
 {
     int r;
 
+    if(!t) {
+        return -1;
+    }
+
     r = set_zone(zone);
     if(r) {
-        log_e("___ set_zone failed\n");
+        log_e("__1__ set_zone failed\n");
     }
-    sleep(1);
-    r = set_time(time);
+
+    print_time("111", t);
+    r = set_time(t, zone);
     if(r) {
-        log_e("___ set_time failed\n");
+        log_e("__1__ set_time failed\n");
     }
+    print_utc("222", time(NULL));
 
     return r;
 }
 
+
+int set_utctime_zone(time_t t, int zone)
+{
+    int r;
+
+    r = set_zone(zone);
+    if(r) {
+        log_e("__2__ set_zone failed\n");
+    }
+
+    r = set_utctime(t);
+    if(r) {
+        log_e("__2__ set_time failed\n");
+    }
+    
+    return r;
+}
+
+int get_time_str(char *s, int len, time_t t)
+{
+    struct tm *tm=localtime(&t);
+    return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d",tm->tm_year,tm->tm_mon,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
+}
+
+
 char* getUserNamebyToken(char* Token)
 {
     char* userName = NULL;

+ 9 - 3
pro/src/common.h

@@ -1077,9 +1077,15 @@ char* get_mac_address(const char *iface_name) ;
 void get_disk_occupy(char ** reused);
 
 int set_zone(int zone);
-int set_time(struct tm* time);
-int set_time_zone(struct tm* time, int zone);
-
+int set_time(struct tm *t, int zone);
+int set_utctime(time_t t);
+int set_time_zone(struct tm *t, int zone);
+int set_utctime_zone(time_t t, int zone);
+
+time_t mktime2(struct tm *t, int zone);
+int get_time_str(char *s, int len, time_t t);
+void print_time(char *s, struct tm *tm);
+void print_utc(char *s, time_t t);
 
 char* getUserNamebyToken(char* Token);
 int is_process_running(const char *process_name);