From 174fd9280f067c92045bb1afe23b90c0a3d64a79 Mon Sep 17 00:00:00 2001 From: kyz Date: Mon, 26 Jun 2023 13:32:40 +0800 Subject: [PATCH] =?utf8?q?=E7=94=A8sysinfo=E8=8E=B7=E5=8F=96=E7=B3=BB?= =?utf8?q?=E7=BB=9F=E8=BF=90=E8=A1=8C=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/inc/device.h | 2 ++ app/src/device_config.c | 1 - app/src/device_ctrl.c | 20 +++++--------- app/src/device_netconfig.c | 56 +++++++++++--------------------------- 4 files changed, 25 insertions(+), 54 deletions(-) diff --git a/app/inc/device.h b/app/inc/device.h index cd2cf64..5cdb340 100644 --- a/app/inc/device.h +++ b/app/inc/device.h @@ -339,5 +339,7 @@ int32_t tuya_bindcode_read( char * code, int32_t max_sz ); int32_t fw_version_read( char * ver, int32_t max_sz ); int32_t hw_version_read( char * ver, int32_t max_sz ); int32_t tuya_productid_read( char * ver, int32_t max_sz ); + +uint32_t device_uptime_sec( void ); #endif //__MG_DEVICE_H__ diff --git a/app/src/device_config.c b/app/src/device_config.c index 82441f2..153b35f 100644 --- a/app/src/device_config.c +++ b/app/src/device_config.c @@ -221,7 +221,6 @@ int32_t read_device_uuid( mg_dev_t *dev ) int32_t mg_device_active( mg_dev_t *dev ) { FILE *fp; - uint32_t now = time(NULL); char key_id[200]; char uuid[100]; char *s; diff --git a/app/src/device_ctrl.c b/app/src/device_ctrl.c index 7467a65..d74f428 100644 --- a/app/src/device_ctrl.c +++ b/app/src/device_ctrl.c @@ -58,7 +58,7 @@ void * btn_light_ctrl_task( void* para ) MG_LOGI( "enter clean mode ..." ); dev->clean_device = 1; - dev->clean_start_time = time(NULL); + dev->clean_start_time = device_uptime_sec(); }else if( dev->clean_device && to_clean_mode_exit ){ @@ -67,9 +67,7 @@ void * btn_light_ctrl_task( void* para ) dev->clean_start_time = 0; }else if( !dev->clean_device && !dev->netconfig && to_net_config ){ MG_LOGI( "enter net config mode ..." ); - //dev->netconfig = 1; - //dev->netconfig_start_time = time(NULL); - printf( "pressed key -- start net config, t:%ld\n", dev->netconfig_start_time ); + printf( "pressed key -- start net config\n" ); netconfig_start( dev ); } @@ -93,7 +91,7 @@ void * btn_light_ctrl_task( void* para ) if( dev->clean_device ){ //检查清洁模式是否超时 - if( dev->clean_start_time && time(NULL) - dev->clean_start_time > CLEANMODE_TIMEOUT_SEC ){ + if( dev->clean_start_time && device_uptime_sec() - dev->clean_start_time > CLEANMODE_TIMEOUT_SEC ){ mg_dev_lock( dev ); dev->clean_device = 0; dev->clean_start_time = 0; @@ -102,12 +100,8 @@ void * btn_light_ctrl_task( void* para ) } }else if( dev->netconfig){ //检查配网超时 - if( dev->netconfig_start_time && time(NULL) - dev->netconfig_start_time > NETCONFIG_TIMEOUT_SEC ){ - printf( "net config, timeout, now:%ld, s:%ld\n", time(NULL), dev->netconfig_start_time ); - //mg_dev_lock( dev ); - //dev->netconfig= 0; - //dev->netconfig_start_time = 0; - //mg_dev_unlock( dev ); + if( dev->netconfig_start_time && device_uptime_sec() - dev->netconfig_start_time > NETCONFIG_TIMEOUT_SEC ){ + printf( "net config, timeout, now:%ld, s:%ld\n", device_uptime_sec(), dev->netconfig_start_time ); netconfig_stop( dev ); } } @@ -176,9 +170,9 @@ void * btn_light_ctrl_task( void* para ) light_mode_set( dev, LIGHT_MODE_TOF_ACTIVE ); } - if( dev->tof && time(NULL ) - tof_check_time > cfg_mg_tof_interval_read( dev ) ){ + if( dev->tof && device_uptime_sec() - tof_check_time > cfg_mg_tof_interval_read( dev ) ){ - tof_check_time = time(NULL); + tof_check_time = device_uptime_sec(); memset( &info, 0, sizeof( info ) ); //检查周期到达,读取tof状态 diff --git a/app/src/device_netconfig.c b/app/src/device_netconfig.c index 1e2c05e..d28b539 100644 --- a/app/src/device_netconfig.c +++ b/app/src/device_netconfig.c @@ -4,6 +4,7 @@ #include #include #include +#include //#include "button.h" //#include "iot.h" //#include "mg_log.h" @@ -14,19 +15,31 @@ extern int32_t mg_device_bind_flag_write( void ); +//使用sysinfo来获取系统运行时间,因使用time(NULL)获取时间会受到ntp的干扰。 +uint32_t device_uptime_sec( void ) +{ + struct sysinfo info; + + if( sysinfo( &info ) ){ + return 0; + } + + return info.uptime; +} + int32_t netconfig_start( mg_dev_t *dev ) { - printf( "netconfig_start,now:%ld, , cfg:%d, s:%ld\n", time(NULL), dev->netconfig, dev->netconfig_start_time ); + printf( "netconfig_start,now:%ld, , cfg:%d, s:%ld\n", device_uptime_sec(), dev->netconfig, dev->netconfig_start_time ); mg_dev_lock( dev ); dev->netconfig = 1; - dev->netconfig_start_time = time(NULL); + dev->netconfig_start_time = device_uptime_sec(); mg_dev_unlock( dev ); return 0; } int32_t netconfig_stop( mg_dev_t *dev ) { - printf( "netconfig_stop,now:%ld, , cfg:%d, s:%ld\n", time(NULL), dev->netconfig, dev->netconfig_start_time ); + printf( "netconfig_stop,now:%ld, , cfg:%d, s:%ld\n", device_uptime_sec(), dev->netconfig, dev->netconfig_start_time ); mg_dev_lock( dev ); dev->netconfig = 0; dev->netconfig_start_time = 0; @@ -169,40 +182,3 @@ void device_netconfig_task_start( mg_dev_t *dev ) } } -#if 0 -int32_t tuya_config_start( mg_dev_t *dev ); -int32_t tuya_config_stop( mg_dev_t *dev ); - - -int32_t wificonfig_start( mg_dev_t *dev ) -{ - mg_dev_lock( dev ); - dev->wifi_config = 1; - dev->wifi_config_timeout = time(NULL)+2*60;//2minutes timeout - mg_dev_unlock( dev ); - //TODO: start ble adver ... - g_ble_adv = 1; - return 0; -} - -int32_t wificonfig_stop( mg_dev_t *dev ) -{ - mg_dev_lock( dev ); - dev->wifi_config = 0; - dev->wifi_config_timeout = 0; - mg_dev_unlock( dev ); - //TODO: stop ble adver ... - g_ble_adv = 0; - return 0; -} - -int32_t wificonfig_success( mg_dev_t *dev ) -{ - return 0; -} - -int32_t wificonfig_fail( mg_dev_t *dev ) -{ - return 0; -} -#endif -- 2.34.1