Files
smod_atsc_app/atsc_app/task/sntp_task.c
T

130 lines
2.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
@STAY_FINAL_PROC:
*/
#include "lwip/ip_addr.h"
#include "sntp.h"
#include "type.h"
#include "sntp_task.h"
#include "lcd_task.h"
#include "os_task.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "dns.h"
extern void SNTP_REQ(void);
extern void delay_ms(uint32 nms);
int getNowTime(void)
{
//atsc_para.system_para.sntp_date
#if 0
time_t now = 0;
struct tm timeinfo = { 0 };
char strftime_buf[72];
time(&now); //获取网络时间, 64bit的秒计数
localtime_r(&now, &timeinfo); //转换成具体的时间参数
// strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
return timeinfo;
#endif
return 0;
}
void sntpService(uint8 *pAddr)
{
ip_addr_t sip;
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sip.addr = pAddr[0]<<24|pAddr[1]<<16|pAddr[2]<<8|pAddr[3]<<0;
sntp_setserver(0, &sip);
sntp_init();
}
void stopSNTP(void)
{
sntp_stop();
}
int sntp_enable(uint8 en, uint8 *pAddr)
{
if(en>1)
en = 0;
if(NULL ==pAddr)
return -1;
stopSNTP();
if(1 == en)
sntpService(pAddr);
return 0;
}
void dnsStart(uint8 *pdns)
{
ip_addr_t serverip;
serverip.addr =pdns[0]<<24|pdns[1]<<16|pdns[2]<<8|pdns[3]<<0;
dns_setserver(0, &serverip);
dns_init();
}
void sntp_restart(void)
{
static uint8 cnt = 0;
if(cnt++>10)
{
cnt = 0;
sntp_enable(1, atsc_para.system_para.sntp_sip);
}
}
void sntp_task(void * pvParameters)
{
MSG *pmsg = NULL;
config_time_msg(MSG_SEC);
sntpService(atsc_para.system_para.sntp_sip);
while((pmsg = get_msg(OS_WAIT_FOREVER))!=NULL)
{
switch(pmsg->type)
{
case MSG_SEC:
task_is_ok(); //任务正常工作
sntp_restart();
break;
default:
break;
}
if(MSG_NEED_TO_FREE(pmsg))
free_msg(pmsg);
}
}