Merge branch 'systime-fix' into v1.2.0

This commit is contained in:
Debaucher 2022-06-13 14:33:32 +08:00
commit 58a71d349d
1 changed files with 25 additions and 27 deletions

View File

@ -15,13 +15,13 @@
#include <errno.h>
pthread_mutex_t lock;
u_int8_t g_Flag;
u_int8_t g_Flag; // 控制启用常驻定时器还是临时定时器
u_int8_t g_Quit;
u_int8_t g_Quit; //退出信号
sem_t g_Wait;
u_int8_t g_TimeChanged;
u_int8_t g_TimeSync;
u_int8_t g_TimeChanged; // 发生了时间变更
u_int8_t g_TimeSync; // 需要进行对时
void sig_Handler(int sig)
{
@ -40,10 +40,7 @@ static void *printClock(void *ptr)
time(&current);
now = localtime(&current);
// printf("%04d/%02d/%02d %02d:%02d:%02d\n", now->tm_year + 1900, now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
// printf("g_TimeChanged is %d\n", g_TimeChanged);
// printf("%d",now->tm_sec);
// 如果时间发生改变发送TimeVhangeSignal信号
// 如果时间发生改变发送TimeChangeSignal信号
if (g_TimeChanged == 1)
{
char *buf = calloc(1, 128);
@ -63,9 +60,10 @@ static void *printClock(void *ptr)
dbus_connection_send(conn, msg, &serial);
dbus_connection_flush(conn);
dbus_message_unref(msg);
free(buf);
}
// 非整点情况
// 非整点情况
if (now->tm_sec != 0)
{
pthread_mutex_lock(&lock);
@ -108,6 +106,7 @@ static void *printClock(void *ptr)
pthread_mutex_unlock(&lock);
sem_post(&g_Wait);
}
free(buf);
}
return NULL;
@ -117,26 +116,29 @@ static void *printClock(void *ptr)
void *startBroadcastSystemTimePerMin(void *tmp)
{
DBusConnection *conn = tmp;
size_t timerID = -1;
size_t periodicTimerID = 0;
while (!g_Quit)
{
sem_wait(&g_Wait);
if (g_TimeChanged || g_TimeSync)
{
printf("Get Time Changed signal or mis-synced. stop timerID %zd\n", timerID);
kdk_timer_stop(timerID);
// 若临时定时器已启动,则不做处理
// 时钟发生变化需要进行对时调整关闭常驻定时器timerID启动临时定时器
printf("Get Time Changed signal or mis-synced. stop timerID %zd\n", periodicTimerID);
kdk_timer_stop(periodicTimerID);
g_TimeChanged = 0;
g_TimeSync = 0;
timerID = -1;
periodicTimerID = 0;
}
if (!g_Flag)
kdk_timer_start(200, printClock, KTIMER_SINGLESHOT, KTIMER_RELATIVE, conn, 0);
else
{
timerID = kdk_timer_start(1000 * 60, printClock, KTIMER_PERIODIC, KTIMER_RELATIVE, conn, 0);
printf("start periodic timer with ID %zd\n", timerID);
// 当启动常驻定时器时,临时定时器肯定不需要再存在了
periodicTimerID = kdk_timer_start(1000 * 60, printClock, KTIMER_PERIODIC, KTIMER_RELATIVE, conn, 0);
printf("start periodic timer with ID %zd\n", periodicTimerID);
}
}
@ -162,6 +164,7 @@ int monitorSystemTimeChange()
u_int64_t dep;
ssize_t ret = read(fd, &dep, sizeof(u_int64_t));
close(fd);
if (ret == -1 && errno == ECANCELED)
return 1;
@ -176,9 +179,12 @@ void *actionTimeChanged(void *ptr)
if (monitorSystemTimeChange() == 1)
{
printf("System Time Changed.\n");
g_TimeChanged = 1;
g_Flag = 0;
printClock(conn);
if (g_Flag)
{
g_TimeChanged = 1;
g_Flag = 0;
printClock(conn);
}
}
}
@ -227,15 +233,7 @@ const char *server_introspection_xml =
" </interface>\n"
" <interface name='org.freedesktop.DBus.Properties'>\n"
" <method name='Get'>\n"
" <arg name='interface' type='s' direction='in' />\n"
" <arg name='property' type='s' direction='in' />\n"
" <arg name='value' type='s' direction='out' />\n"
" </method>\n"
" <method name='GetAll'>\n"
" <arg name='interface' type='s' direction='in'/>\n"
" <arg name='properties' type='a{sv}' direction='out'/>\n"
" </method>\n"
" <method name=timerfd"
" </interface>\n"
" <interface name='com.kylin.kysdk.TimeInterface'>\n"