mirror of
https://github.com/rvtr/TwlToolsRED.git
synced 2025-10-31 06:41:18 -04:00
add NTP service.
git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlToolsRED@67 7061adef-622a-194b-ae81-725974e89856
This commit is contained in:
parent
73f4d2d1a2
commit
2b9795dbf3
@ -19,6 +19,9 @@ static char SSID_STR[256];
|
||||
static int SSID_LEN = 0;
|
||||
static int key_mode = 0; /* 0->str 1->bin */
|
||||
static char KEY_STR[256];
|
||||
static char NTPSRV_STR[256];
|
||||
static int NTPSRV_LEN = 0;
|
||||
static int TIME_ZONE = 0;
|
||||
|
||||
static u8 KEY_BIN[MAX_KEY_BIN_BUF];
|
||||
static int KEY_STR_LEN = 0;
|
||||
@ -75,9 +78,25 @@ u32 GetDNS2(void)
|
||||
return DNS2;
|
||||
}
|
||||
|
||||
int GetTimeZone(void)
|
||||
{
|
||||
return TIME_ZONE;
|
||||
}
|
||||
|
||||
char *GetNTPSRV(void)
|
||||
{
|
||||
if( NTPSRV_LEN ) {
|
||||
return NTPSRV_STR;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *GetWlanSSID(void)
|
||||
{
|
||||
return SSID_STR;
|
||||
if( SSID_LEN ) {
|
||||
return SSID_STR;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *GetWlanKEYSTR(void)
|
||||
@ -253,6 +272,13 @@ BOOL LoadWlanConfigFile(char *path)
|
||||
BOOL dns1_flag;
|
||||
BOOL dns2_flag;
|
||||
|
||||
BOOL ntpsrv_flag;
|
||||
BOOL timezone_flag;
|
||||
int time_zone_minus;
|
||||
int time_zone_hour_min;
|
||||
int time_zone_hour;
|
||||
int time_zone_min;
|
||||
|
||||
BOOL ret_flag = FALSE;
|
||||
u8 hex;
|
||||
char c;
|
||||
@ -281,7 +307,8 @@ BOOL LoadWlanConfigFile(char *path)
|
||||
gw_flag = FALSE;
|
||||
dns1_flag = FALSE;
|
||||
dns2_flag = FALSE;
|
||||
|
||||
ntpsrv_flag = FALSE;
|
||||
timezone_flag = FALSE;
|
||||
|
||||
while( 1 ) {
|
||||
readSize = ReadLine(&f, line_buf, LINE_BUF_SIZE );
|
||||
@ -312,6 +339,77 @@ BOOL LoadWlanConfigFile(char *path)
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( !ntpsrv_flag && !STD_StrNCmp( line_buf, "NTPSRV:" , STD_StrLen("NTPSRV:")) ) {
|
||||
count = STD_StrLen("NTPSRV:");
|
||||
if( line_buf[count] == '\"' /* ‚PŒÂ–Ú */) {
|
||||
count++;
|
||||
count2 = count;
|
||||
while( readSize > count ) {
|
||||
if( line_buf[count] == '\"' /* ‚QŒÂ–Ú */) {
|
||||
NTPSRV_LEN = count - 1;
|
||||
NTPSRV_STR[count - count2] = '\0';
|
||||
ntpsrv_flag = TRUE;
|
||||
break;
|
||||
}
|
||||
NTPSRV_STR[count - count2] = line_buf[count];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( !timezone_flag && !STD_StrNCmp( line_buf, "TIMEZONE:" , STD_StrLen("TIMEZONE:")) ) {
|
||||
count = STD_StrLen("TIMEZONE:");
|
||||
if( line_buf[count] == '\"' /* ‚PŒÂ–Ú */) {
|
||||
count++;
|
||||
count2 = count;
|
||||
|
||||
time_zone_minus = 0;
|
||||
time_zone_hour_min = 0;
|
||||
time_zone_hour = 0;
|
||||
time_zone_min = 0;
|
||||
TIME_ZONE = 0;
|
||||
|
||||
if( line_buf[count] == '+' ) {
|
||||
count++;
|
||||
}
|
||||
else if( line_buf[count] == '-' ) {
|
||||
time_zone_minus = 1;
|
||||
count++;
|
||||
}
|
||||
|
||||
while( readSize > count ) {
|
||||
if( line_buf[count] == '\"' /* ‚QŒÂ–Ú */) {
|
||||
|
||||
TIME_ZONE = time_zone_hour * 60;
|
||||
TIME_ZONE += time_zone_min;
|
||||
|
||||
if( time_zone_minus ) {
|
||||
TIME_ZONE *= -1;
|
||||
}
|
||||
timezone_flag = TRUE;
|
||||
break;
|
||||
}
|
||||
else if( line_buf[count] == ':' ) {
|
||||
time_zone_hour_min = 1;
|
||||
}
|
||||
else if( ('0' <= line_buf[count]) && ( line_buf[count] <= '9') ) {
|
||||
if( time_zone_hour_min == 0) {
|
||||
time_zone_hour *= 10;
|
||||
time_zone_hour += (line_buf[count] - '0');
|
||||
}
|
||||
else {
|
||||
time_zone_min *= 10;
|
||||
time_zone_min += (line_buf[count] - '0');
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* error */
|
||||
TIME_ZONE = 0;
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( !ip_addr_flag && !STD_StrNCmp( line_buf, "IPADDR:" , STD_StrLen("IPADDR:")) ) {
|
||||
count = STD_StrLen("IPADDR:");
|
||||
if( TRUE == Addr_scan(line_buf, readSize, count, &IPADDR) ) {
|
||||
|
||||
@ -25,6 +25,10 @@ u32 GetDNS2(void);
|
||||
|
||||
BOOL GetKeyModeStr(void);
|
||||
|
||||
|
||||
char *GetNTPSRV(void);
|
||||
int GetTimeZone(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -26,7 +26,8 @@ SRCS = main.c mfiler.c key.c font.c text.c mprintf.c logprintf.c \
|
||||
gfx.c hwi.c mynvram.c my_fs_util.c \
|
||||
hatamotolib.cpp miya_mcu.c error_report.c \
|
||||
sitedefs.c wcm_control.c netconnect.c mywlan.c \
|
||||
mynuc.c nuc_error_msg.c stream.c myfilename.c menu_version.c
|
||||
mynuc.c nuc_error_msg.c stream.c myfilename.c menu_version.c \
|
||||
ntp.c
|
||||
|
||||
TARGET_BIN = copy_dst.srl
|
||||
ROM_SPEC = copy_dst.rsf
|
||||
|
||||
@ -53,6 +53,7 @@
|
||||
#include "mfiler.h"
|
||||
#include "menu_version.h"
|
||||
|
||||
#include "ntp.h"
|
||||
|
||||
//================================================================================
|
||||
#define THREAD_COMMAND_NUP_FUNCTION 0
|
||||
@ -146,12 +147,13 @@ static int vram_num_sub = 0;
|
||||
static LCFGTWLHWNormalInfo hwn_info;
|
||||
static LCFGTWLHWSecureInfo hws_info;
|
||||
|
||||
|
||||
|
||||
#define MY_STACK_SIZE (1024*64) /* でかいほうがいい */
|
||||
#define MY_THREAD_PRIO 20
|
||||
//#define MY_THREAD_PRIO 20
|
||||
|
||||
#define MY_THREAD_PRIO 10
|
||||
|
||||
static OSThread MyThread;
|
||||
static u64 MyStack[MY_STACK_SIZE / sizeof(u64)];
|
||||
static u64 MyStack[MY_STACK_SIZE/sizeof(u64)];
|
||||
static void MyThreadProc(void *arg);
|
||||
static void MyThreadProcNuc(void *arg);
|
||||
|
||||
@ -162,12 +164,11 @@ static OSMessageQueue MyMesgQueue_response;
|
||||
|
||||
static void init_my_thread(void)
|
||||
{
|
||||
|
||||
OS_InitMessageQueue(&MyMesgQueue_request, &MyMesgBuffer_request[0], 1);
|
||||
OS_InitMessageQueue(&MyMesgQueue_response, &MyMesgBuffer_response[0], 1);
|
||||
|
||||
OS_CreateThread(&MyThread, MyThreadProc,
|
||||
NULL, MyStack + MY_STACK_SIZE / sizeof(u64),
|
||||
NULL, MyStack + MY_STACK_SIZE /sizeof(u64),
|
||||
MY_STACK_SIZE, MY_THREAD_PRIO);
|
||||
OS_WakeupThreadDirect(&MyThread);
|
||||
}
|
||||
@ -941,13 +942,13 @@ static void MyThreadProcNuc(void *arg)
|
||||
FSFile *log_fd;
|
||||
BOOL ret_flag;
|
||||
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
|
||||
|
||||
while( 1 ) {
|
||||
(void)OS_SendMessage(&MyMesgQueue_response, (OSMessage)0, OS_MESSAGE_NOBLOCK);
|
||||
(void)OS_ReceiveMessage(&MyMesgQueue_request, &message, OS_MESSAGE_BLOCK);
|
||||
|
||||
if( (u32)message == THREAD_COMMAND_REBOOT_FUNCTION ) {
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
|
||||
mprintf("%s Power button pressed!\n",__FUNCTION__);
|
||||
OS_TPrintf("%s Power button pressed!\n",__FUNCTION__);
|
||||
MCU_SetFreeRegister( 0x00 );
|
||||
@ -955,10 +956,14 @@ static void MyThreadProcNuc(void *arg)
|
||||
pushed_power_button = FALSE;
|
||||
}
|
||||
else if( (u32)message != THREAD_COMMAND_NUP_FUNCTION ) {
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_RED );
|
||||
mprintf("%s unknown command!\n",__FUNCTION__);
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
|
||||
continue;
|
||||
}
|
||||
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_WHITE );
|
||||
|
||||
mprintf("-Wireless AP conf. load ");
|
||||
if( TRUE == LoadWlanConfig() ) {
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_GREEN ); /* green */
|
||||
@ -999,6 +1004,66 @@ static void MyThreadProcNuc(void *arg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* miya */
|
||||
OS_TPrintf("ntp start\n");
|
||||
if( TRUE == my_ntp_init() ) {
|
||||
RTCDate rtc_date;
|
||||
RTCTime rtc_time;
|
||||
rtc_date.year = (u32)( my_ntp_get_year() - 2000 );
|
||||
rtc_date.month = (u32)(my_ntp_get_month() + 1 );
|
||||
rtc_date.day = (u32)my_ntp_get_day();
|
||||
|
||||
rtc_time.hour = (u32)my_ntp_get_hour();
|
||||
rtc_time.minute = (u32)my_ntp_get_min();
|
||||
rtc_time.second = (u32)my_ntp_get_sec();
|
||||
|
||||
//RTCDate rtc_date;
|
||||
//RTCTime rtc_time;
|
||||
/*
|
||||
typedef enum RTCWeek
|
||||
{
|
||||
RTC_WEEK_SUNDAY = 0, // 日曜日
|
||||
RTC_WEEK_MONDAY, // 月曜日
|
||||
RTC_WEEK_TUESDAY, // 火曜日
|
||||
RTC_WEEK_WEDNESDAY, // 水曜日
|
||||
RTC_WEEK_THURSDAY, // 木曜日
|
||||
RTC_WEEK_FRIDAY, // 金曜日
|
||||
RTC_WEEK_SATURDAY, // 土曜日
|
||||
RTC_WEEK_MAX
|
||||
}
|
||||
RTCWeek;
|
||||
|
||||
|
||||
typedef struct RTCDate
|
||||
{
|
||||
u32 year; // 年 ( 0 ~ 99 )
|
||||
u32 month; // 月 ( 1 ~ 12 )
|
||||
u32 day; // 日 ( 1 ~ 31 )
|
||||
RTCWeek week; // 曜日
|
||||
|
||||
}
|
||||
RTCDate;
|
||||
|
||||
// 時刻構造体
|
||||
typedef struct RTCTime
|
||||
{
|
||||
u32 hour; // 時 ( 0 ~ 23 )
|
||||
u32 minute; // 分 ( 0 ~ 59 )
|
||||
u32 second; // 秒 ( 0 ~ 59 )
|
||||
|
||||
}
|
||||
RTCTime;
|
||||
*/
|
||||
|
||||
if( RTC_RESULT_SUCCESS != RTC_SetDate( &rtc_date ) ) {
|
||||
}
|
||||
if( RTC_RESULT_SUCCESS != RTC_SetTime( &rtc_time ) ) {
|
||||
}
|
||||
}
|
||||
OS_TPrintf("ntp end\n");
|
||||
|
||||
|
||||
/* NSSL_Init()呼んではダメ! */
|
||||
log_fd = my_nuc_log_start( MyFile_GetNupLogFileName() );
|
||||
ret_flag = my_numc_proc();
|
||||
@ -1771,7 +1836,7 @@ void TwlMain(void)
|
||||
m_set_palette(tc[1], M_TEXT_COLOR_YELLOW );
|
||||
break;
|
||||
default:
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_GREEN );
|
||||
m_set_palette(tc[1], M_TEXT_COLOR_GREEN );
|
||||
break;
|
||||
}
|
||||
mfprintf(tc[1], "%d/5\n\n" , BatterylevelBuf);
|
||||
|
||||
352
build/tools/sctools/copy_dst/src/ntp.c
Normal file
352
build/tools/sctools/copy_dst/src/ntp.c
Normal file
@ -0,0 +1,352 @@
|
||||
#include <twl.h>
|
||||
#include <nitroWiFi.h>
|
||||
|
||||
#include "ntp.h"
|
||||
|
||||
#include "font.h"
|
||||
#include "text.h"
|
||||
#include "mprintf.h"
|
||||
#include "logprintf.h"
|
||||
|
||||
#include "mywlan.h"
|
||||
|
||||
|
||||
#define BUFSIZE 256 /* バッファサイズ */
|
||||
#define RECVSIZE 4096 /* 受信バッファサイズ */
|
||||
#define TIMEOUT 2 /* タイムアウト秒数 */
|
||||
|
||||
struct NTP_Packet{ /* NTPパケット */
|
||||
int Control_Word;
|
||||
int root_delay;
|
||||
int root_dispersion;
|
||||
int reference_identifier;
|
||||
s64 reference_timestamp;
|
||||
s64 originate_timestamp;
|
||||
s64 receive_timestamp;
|
||||
int transmit_timestamp_seconds;
|
||||
int transmit_timestamp_fractions;
|
||||
};
|
||||
|
||||
struct rtc_time {
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
};
|
||||
|
||||
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
|
||||
#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
|
||||
|
||||
static const unsigned char rtc_days_in_month[] = {
|
||||
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
static int rtc_month_days(unsigned int month, unsigned int year)
|
||||
{
|
||||
return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
|
||||
}
|
||||
|
||||
/* Convert seconds since 01-01-1970 00:00:00 to Gregorian date. */
|
||||
static void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
|
||||
{
|
||||
unsigned int month, year;
|
||||
int days;
|
||||
|
||||
days = (int)(time / 86400 );
|
||||
time -= (unsigned int) days * 86400;
|
||||
|
||||
/* day of the week, 1970-01-01 was a Thursday */
|
||||
tm->tm_wday = (days + 4) % 7;
|
||||
|
||||
year = (unsigned int)(1970 + days / 365);
|
||||
days -= (year - 1970) * 365
|
||||
+ LEAPS_THRU_END_OF(year - 1)
|
||||
- LEAPS_THRU_END_OF(1970 - 1);
|
||||
if (days < 0) {
|
||||
year -= 1;
|
||||
days += 365 + LEAP_YEAR(year);
|
||||
}
|
||||
tm->tm_year = (int)(year - 1900);
|
||||
tm->tm_yday = days + 1;
|
||||
|
||||
for (month = 0; month < 11; month++) {
|
||||
int newdays;
|
||||
|
||||
newdays = days - rtc_month_days(month, year);
|
||||
if (newdays < 0)
|
||||
break;
|
||||
days = newdays;
|
||||
}
|
||||
tm->tm_mon = (int)month;
|
||||
tm->tm_mday = days + 1;
|
||||
|
||||
tm->tm_hour = (int)(time / 3600);
|
||||
time -= tm->tm_hour * 3600;
|
||||
tm->tm_min = (int)(time / 60);
|
||||
tm->tm_sec = (int)(time - tm->tm_min * 60);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static char svName[BUFSIZE]; /* サーバのドメイン名 */
|
||||
static unsigned short port = 123; /* サーバのポート番号 */
|
||||
static unsigned short local_port = 1024; /* 受信するポート番号 */
|
||||
static int soc; /* ソケット(Soket Descriptor) */
|
||||
static unsigned long serveraddr; /* サーバのIPアドレス */
|
||||
|
||||
//static struct hostent *serverhostent; /* サーバの情報を指すポインタ */
|
||||
static SOCHostEnt *serverhostent; /* サーバの情報を指すポインタ */
|
||||
|
||||
//static struct sockaddr_in sockname; /* ソケットのアドレス */
|
||||
|
||||
static SOCSockAddrIn sockname;
|
||||
|
||||
// static struct sockaddr_in serversockaddr; /* サーバのアドレス */
|
||||
|
||||
static SOCSockAddrIn serversockaddr; /* サーバのアドレス */
|
||||
|
||||
static int sockaddr_Size; /* サーバのアドレスのサイズ */
|
||||
static struct NTP_Packet NTP_Send; /* 送信するNTPパケット */
|
||||
static struct NTP_Packet NTP_Recv; /* 受信するNTPパケット */
|
||||
|
||||
static u32 ntp_time; /* NTPサーバから取得した時刻 */
|
||||
|
||||
static struct rtc_time tm;
|
||||
|
||||
|
||||
int my_ntp_get_year(void)
|
||||
{
|
||||
return tm.tm_year+1900;
|
||||
}
|
||||
|
||||
int my_ntp_get_month(void)
|
||||
{
|
||||
return tm.tm_mon;
|
||||
}
|
||||
|
||||
int my_ntp_get_day(void)
|
||||
{
|
||||
return tm.tm_mday;
|
||||
}
|
||||
|
||||
int my_ntp_get_weekday(void)
|
||||
{
|
||||
return tm.tm_wday;
|
||||
}
|
||||
|
||||
int my_ntp_get_hour(void)
|
||||
{
|
||||
return tm.tm_hour;
|
||||
}
|
||||
|
||||
int my_ntp_get_min(void)
|
||||
{
|
||||
return tm.tm_min;
|
||||
}
|
||||
|
||||
|
||||
int my_ntp_get_sec(void)
|
||||
{
|
||||
return tm.tm_sec;
|
||||
}
|
||||
|
||||
|
||||
BOOL my_ntp_init(void)
|
||||
{
|
||||
SOCPollFD pfds[1];
|
||||
|
||||
if( GetNTPSRV() == NULL ) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
OS_TPrintf("NTP srv %s\n",GetNTPSRV() );
|
||||
|
||||
|
||||
// STD_StrCpy(svName, "ntp.jst.mfeed.ad.jp" );
|
||||
STD_StrCpy(svName, GetNTPSRV() );
|
||||
//Name: ntp.jst.mfeed.ad.jp
|
||||
//Addresses: 210.173.160.57, 210.173.160.87, 210.173.160.27
|
||||
|
||||
|
||||
/* ソケットを作成する処理 */
|
||||
/* UDPモードでsocにソケットを作成します */
|
||||
soc = SOC_Socket(SOC_PF_INET, SOC_SOCK_DGRAM, 0);
|
||||
if(soc < 0 ){
|
||||
OS_TPrintf("Error: create Socket\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/* 受信ポートを指定する処理 */
|
||||
/* ソケットのアドレスの構造体にサーバのIPアドレスとポート番号を設定します */
|
||||
// SOSockAddrIn;
|
||||
// int SOC_Bind(int s, const void* sockAddr);
|
||||
// SOC_HtoNs(hostshort)
|
||||
|
||||
sockname.family = SOC_AF_INET; /* インターネットの場合 */
|
||||
sockname.addr.addr = SOC_INADDR_ANY; /* 自分のIPアドレスを使うようにする */
|
||||
sockname.port = SOC_HtoNs((unsigned short)local_port); /* 受信するポート番号 */
|
||||
// STD_MemSet((void *)sockname.zero,(int)0,sizeof(sockname.zero));
|
||||
if(SOC_Bind(soc, (void *)&sockname) < 0 ){
|
||||
OS_TPrintf("Error: specify recv port\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/* サーバのIPアドレスを取得する処理 */
|
||||
/* svNameにドットで区切った10進数のIPアドレスが入っている場合、serveraddrに32bit整数のIPアドレスが返ります */
|
||||
// SOCHostEnt* SOC_GetHostByName(const char* name);
|
||||
// typedef struct SOCHostEnt
|
||||
// {
|
||||
// char* name; // official name of host
|
||||
// char ** aliases; // alias list (zero-terminated)
|
||||
// s16 addrType; // always SO_PF_INET
|
||||
// s16 length; // length of address
|
||||
// u8 ** addrList; // list of addresses
|
||||
// } SOCHostEnt;
|
||||
//
|
||||
// int SOC_InetAtoN(const char* cp, SOCInAddr* inp);
|
||||
//
|
||||
// typedef struct SOCInAddr
|
||||
// {
|
||||
// u32 addr;
|
||||
// } SOCInAddr;
|
||||
//
|
||||
// int SOC_Close(int s);
|
||||
|
||||
// serveraddr = (210) | (173 << 8) | (160 << 16) | (57 << 24);
|
||||
|
||||
/* サーバ名(svName)からサーバのホスト情報を取得します */
|
||||
serverhostent = SOC_GetHostByName(svName);
|
||||
if(serverhostent == NULL) {
|
||||
OS_TPrintf("Error: SOC_GetHostByName %s\n",svName);
|
||||
/* ソケットを破棄する */
|
||||
(void)SOC_Close(soc);
|
||||
return FALSE;
|
||||
}
|
||||
else{
|
||||
/* サーバのホスト情報からIPアドレスをserveraddrにコピーします */
|
||||
serveraddr = *((unsigned long *)((serverhostent->addrList)[0]));
|
||||
}
|
||||
|
||||
#if 1
|
||||
OS_TPrintf("serveraddr = %d.%d.%d.%d\n",
|
||||
((serveraddr >> 0) & 0xff),
|
||||
((serveraddr >> 8) & 0xff),
|
||||
((serveraddr >> 16) & 0xff),
|
||||
((serveraddr >> 24) & 0xff) );
|
||||
#endif
|
||||
|
||||
|
||||
/* サーバのアドレスの構造体にサーバのIPアドレスとポート番号を設定します */
|
||||
serversockaddr.family = SOC_AF_INET; /* インターネットの場合 */
|
||||
serversockaddr.addr.addr = serveraddr; /* サーバのIPアドレス */
|
||||
serversockaddr.port = SOC_HtoNs((unsigned short)port); /* ポート番号 */
|
||||
|
||||
/* サーバにパケットを送信する処理 */
|
||||
/* NTPパケットをSNTP用に初期化する */
|
||||
// NTP_Send.Control_Word = htonl(0x0B000000);
|
||||
NTP_Send.Control_Word = SOC_HtoNl(0x0B000000);
|
||||
|
||||
NTP_Send.root_delay = 0;
|
||||
NTP_Send.root_dispersion = 0;
|
||||
NTP_Send.reference_identifier = 0;
|
||||
NTP_Send.reference_timestamp = 0;
|
||||
NTP_Send.originate_timestamp = 0;
|
||||
NTP_Send.receive_timestamp = 0;
|
||||
NTP_Send.transmit_timestamp_seconds = 0;
|
||||
NTP_Send.transmit_timestamp_fractions = 0;
|
||||
|
||||
|
||||
|
||||
// int SOC_SendTo(int s, const void* buf, int len, int flags, const void* sockTo)
|
||||
|
||||
/* サーバを指定してNTPパケットを送信する */
|
||||
if(SOC_SendTo(soc,(const void *)&NTP_Send, sizeof( NTP_Send ),0,(const void *)&serversockaddr) < 0 ) {
|
||||
OS_TPrintf("Error: サーバへの送信失敗\n");
|
||||
/* ソケットを破棄する */
|
||||
(void)SOC_Close(soc);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/* タイムアウトを行う処理 */
|
||||
/* select関数を使ってタイムアウトを設定する */
|
||||
|
||||
//
|
||||
// typedef struct SOCPollFD
|
||||
// {
|
||||
// int fd;
|
||||
// short events; // input event flags
|
||||
// short revents; // output event flags
|
||||
// } SOCPollFD;
|
||||
//
|
||||
// int SOC_Poll(SOCPollFD fds[], unsigned nfds, OSTick timeout);
|
||||
pfds[0].fd = soc;
|
||||
// pfds[0].events = SOC_POLLRDNORM | SOC_POLLWRNORM;
|
||||
pfds[0].events = SOC_POLLRDNORM;
|
||||
if( SOC_Poll( pfds, 1, OS_MilliSecondsToTicks( TIMEOUT * 1000 ) ) < 0 ) {
|
||||
OS_TPrintf("Error: recv error\n");
|
||||
/* ソケットを破棄する */
|
||||
(void)SOC_Close(soc);
|
||||
return FALSE;
|
||||
}
|
||||
switch( pfds[0].revents ) {
|
||||
case SOC_POLLERR: // ソケットにエラーが発生しました。
|
||||
OS_TPrintf("Error: SOC_POLLERR %s %d\n",__FUNCTION__,__LINE__);
|
||||
break;
|
||||
case SOC_POLLHUP: // ストリーム・ソケットが未接続です。
|
||||
OS_TPrintf("Error: SOC_POLLHUP %s %d\n",__FUNCTION__,__LINE__);
|
||||
break;
|
||||
case SOC_POLLNVAL: // 不正なソケット記述子です。
|
||||
OS_TPrintf("Error: SOC_POLLNVAL %s %d\n",__FUNCTION__,__LINE__);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
/* サーバから時刻情報を受信する処理 */
|
||||
/* サーバを指定して受信を行う */
|
||||
// int SOC_RecvFrom(int s, void* buf, int len, int flags, void* sockFrom);
|
||||
sockaddr_Size = sizeof(serversockaddr);
|
||||
if(SOC_RecvFrom(soc, (char *)&NTP_Recv, sizeof(NTP_Recv), 0 ,(void *)&serversockaddr) < 0 ){
|
||||
OS_TPrintf("Error: サーバからの受信失敗\n");
|
||||
/* ソケットを破棄する */
|
||||
(void)SOC_Close(soc);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/* NTPサーバから取得した時刻を現地時間に変換する */
|
||||
// ntp_time = ntohl(NTP_Recv.transmit_timestamp_seconds) - 2208988800; /* 1970/01/01 からの秒数に変換 */
|
||||
ntp_time = SOC_NtoHl(NTP_Recv.transmit_timestamp_seconds) - 2208988800; /* 1970/01/01 からの秒数に変換 */
|
||||
|
||||
OS_TPrintf("ntp_time = %d\n",ntp_time);
|
||||
|
||||
OS_TPrintf("TimeZone %d\n", GetTimeZone());
|
||||
|
||||
ntp_time += (60*GetTimeZone());
|
||||
|
||||
rtc_time_to_tm(ntp_time, &tm);
|
||||
|
||||
OS_TPrintf("Year %d\n", tm.tm_year+1900);
|
||||
OS_TPrintf("Mon %d\n", tm.tm_mon + 1); /* 0 -> 1月 */
|
||||
OS_TPrintf("Day %d\n", tm.tm_mday);
|
||||
OS_TPrintf("Week %d\n", tm.tm_wday);
|
||||
OS_TPrintf("Hour %d\n", tm.tm_hour);
|
||||
OS_TPrintf("Min %d\n", tm.tm_min);
|
||||
OS_TPrintf("Sec %d\n", tm.tm_sec);
|
||||
|
||||
// tm.tm_yday;
|
||||
// tm.tm_isdst;
|
||||
|
||||
/* ソケットを破棄する処理 */
|
||||
(void)SOC_Close(soc);
|
||||
return TRUE;
|
||||
}
|
||||
25
build/tools/sctools/copy_dst/src/ntp.h
Normal file
25
build/tools/sctools/copy_dst/src/ntp.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef _MY_NTP_H_
|
||||
#define _MY_NTP_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
BOOL my_ntp_init(void);
|
||||
|
||||
int my_ntp_get_year(void);
|
||||
int my_ntp_get_month(void);
|
||||
int my_ntp_get_day(void);
|
||||
int my_ntp_get_weekday(void);
|
||||
int my_ntp_get_hour(void);
|
||||
int my_ntp_get_min(void);
|
||||
int my_ntp_get_sec(void);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _MY_NTP_H_ */
|
||||
@ -1062,7 +1062,7 @@ void TwlMain(void)
|
||||
m_set_palette(tc[1], M_TEXT_COLOR_YELLOW );
|
||||
break;
|
||||
default:
|
||||
m_set_palette(tc[0], M_TEXT_COLOR_GREEN );
|
||||
m_set_palette(tc[1], M_TEXT_COLOR_GREEN );
|
||||
break;
|
||||
}
|
||||
mfprintf(tc[1], "%d/5\n\n" , BatterylevelBuf);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user