[修改] 变更了时钟广播的文件路径
This commit is contained in:
parent
520f4e0694
commit
99ec465c43
|
@ -1,4 +1,4 @@
|
|||
#include "systeminfo.h"
|
||||
#include "systeminfo.hpp"
|
||||
#include <fstream>
|
||||
|
||||
namespace KYSDK_SYSTEM{
|
||||
|
@ -119,9 +119,4 @@ std::string getKernelVersion()
|
|||
return kernelVersion;
|
||||
}
|
||||
|
||||
std::string getCurrentUserName()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
#ifndef KYSDK_SYSTEM_SYSINFO_H__
|
||||
#define KYSDK_SYSTEM_SYSINFO_H__
|
||||
|
||||
#include <string>
|
||||
#include <stdbool.h>
|
||||
|
||||
namespace KYSDK_SYSTEM {
|
||||
|
||||
/**
|
||||
* @brief 获取架构类型
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string getSystemArchitecture();
|
||||
|
||||
/**
|
||||
* @brief 获取操作系统名称
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string getSystemName();
|
||||
|
||||
/**
|
||||
* @brief 获取操作系统版本号
|
||||
*
|
||||
* @param verbose
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string getSystemVersion(bool verbose = false);
|
||||
|
||||
/**
|
||||
* @brief 获取操作系统激活状态
|
||||
*
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
extern bool getSystemActivationStatus();
|
||||
|
||||
/**
|
||||
* @brief 获取操作系统服务序列号
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string getSystemSerialNumber();
|
||||
|
||||
/**
|
||||
* @brief 获取内核版本号
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string getKernelVersion();
|
||||
|
||||
/**
|
||||
* @brief 获取当前用户名
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string getCurrentUserName();
|
||||
}
|
||||
|
||||
#endif // KYSDK_SYSTEM_SYSINFO_H__
|
|
@ -0,0 +1,14 @@
|
|||
FILE=$(file)
|
||||
CFLAGS=$(cflags)
|
||||
CFLAGS += -g -O0 -std=gnu11 -Wall
|
||||
CLIBS=-lpthread -pthread -lrt
|
||||
CLIBS += $(clibs)
|
||||
|
||||
.PHONY:main test
|
||||
all:systemtime.c
|
||||
gcc -o demo systemtime.c ../timer/libkytimer.c $(CFLAGS) $(CLIBS)
|
||||
|
||||
.PHONY:clean
|
||||
clean:
|
||||
-rm main
|
||||
-rm *.o
|
|
@ -0,0 +1,153 @@
|
|||
#include "timebroadcast.h"
|
||||
#include "libkytimer.h"
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <semaphore.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <errno.h>
|
||||
|
||||
pthread_mutex_t lock;
|
||||
u_int8_t g_Flag;
|
||||
|
||||
u_int8_t g_Quit;
|
||||
sem_t g_Wait;
|
||||
|
||||
u_int8_t g_TimeChanged;
|
||||
u_int8_t g_TimeSync;
|
||||
|
||||
void sig_Handler(int sig)
|
||||
{
|
||||
g_Quit = 1;
|
||||
}
|
||||
|
||||
static void* printClock(void* ptr)
|
||||
{
|
||||
struct tm *now;
|
||||
time_t current;
|
||||
time(¤t);
|
||||
now = localtime(¤t);
|
||||
|
||||
// 非整点情况
|
||||
if (now->tm_sec != 0)
|
||||
{
|
||||
pthread_mutex_lock(&lock);
|
||||
|
||||
// 如果出现了非整点的报时,且
|
||||
// 之前处于整分钟报时的情况,说明
|
||||
// 出现了唤醒偏差。重新执行一下对时
|
||||
if(g_Flag == 1)
|
||||
{
|
||||
g_Flag = 0;
|
||||
g_TimeSync = 1;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
sem_post(&g_Wait);
|
||||
}
|
||||
// 整点的情况
|
||||
else
|
||||
{
|
||||
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);
|
||||
if (g_Flag == 0)
|
||||
{
|
||||
pthread_mutex_lock(&lock);
|
||||
g_Flag = (1 & ~g_TimeChanged);
|
||||
pthread_mutex_unlock(&lock);
|
||||
sem_post(&g_Wait);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void startBroadcastSystemTimePerMin()
|
||||
{
|
||||
size_t timerID = -1;
|
||||
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);
|
||||
kyStopTimer(timerID);
|
||||
g_TimeChanged = 0;
|
||||
g_TimeSync = 0;
|
||||
timerID = -1;
|
||||
}
|
||||
|
||||
if (! g_Flag)
|
||||
kyStartTimer(200, printClock, KTIMER_SINGLESHOT, KTIMER_RELATIVE, NULL, 0);
|
||||
else
|
||||
{
|
||||
timerID = kyStartTimer(1000 * 60, printClock, KTIMER_PERIODIC, KTIMER_RELATIVE, NULL, 0);
|
||||
printf("start periodic timer with ID %zd\n", timerID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int monitorSystemTimeChange()
|
||||
{
|
||||
#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
|
||||
|
||||
struct itimerspec its = {.it_value.tv_sec = TIME_T_MAX};
|
||||
int fd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
|
||||
if (fd < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0)
|
||||
{
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
u_int64_t dep;
|
||||
ssize_t ret = read(fd, &dep, sizeof(u_int64_t));
|
||||
if (ret == -1 && errno == ECANCELED)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* actionTimeChanged(void* ptr)
|
||||
{
|
||||
while (!g_Quit)
|
||||
{
|
||||
if (monitorSystemTimeChange() == 1)
|
||||
{
|
||||
printf("System Time Changed.\n");
|
||||
g_TimeChanged = 1;
|
||||
g_Flag = 0;
|
||||
printClock(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
signal(SIGTERM, sig_Handler);
|
||||
signal(SIGQUIT, sig_Handler);
|
||||
signal(SIGKILL, sig_Handler);
|
||||
|
||||
sem_init(&g_Wait, 0, 1);
|
||||
|
||||
pthread_attr_t attr;
|
||||
pthread_t tid;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
pthread_create(&tid, &attr, actionTimeChanged, NULL);
|
||||
|
||||
pthread_mutex_init(&lock, NULL);
|
||||
startBroadcastSystemTimePerMin();
|
||||
pthread_mutex_destroy(&lock);
|
||||
|
||||
sem_destroy(&g_Wait);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef KYSDK_SYSTEM_SYSTIME_TIMEBROADCAST_H__
|
||||
#define KYSDK_SYSTEM_SYSTIME_TIMEBROADCAST_H__
|
||||
|
||||
extern void startBroadcastSystemTimePerMin();
|
||||
|
||||
/**
|
||||
* @brief 当系统时间被修改时,该函数会返回 1
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
extern int monitorSystemTimeChange();
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue