diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/ConsoleRestore.cpp b/trunk/ConsoleDataMigration/ConsoleRestore/ConsoleRestore.cpp index 9bd511c..72ecb34 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/ConsoleRestore.cpp +++ b/trunk/ConsoleDataMigration/ConsoleRestore/ConsoleRestore.cpp @@ -148,8 +148,8 @@ void FinalizeAll() nn::ptm::CTR::FinalizeForSystemMenu(); nn::cfg::CTR::system::Finalize(); nn::cfg::CTR::init::Finalize(); - //nn::hid::Finalize(); - //nn::fs::Finalize(); + nn::hid::Finalize(); + nn::fs::Finalize(); nn::applet::PrepareToCloseApplication(); nn::applet::CloseApplication(); diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp b/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp index 777251a..799307b 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp +++ b/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp @@ -114,6 +114,29 @@ bool CheckAndReadAPSetting(::std::vector& operationMessage) return s_ReadSettingSuccess; } +void PutAliveMessage(::std::vector& operationMessage, const char* str) +{ + std::string message = std::string(str); + static u8 i = 0; + if (i < 0xff / 4) + { + operationMessage.push_back(message + std::string(" /")); + } + else if (i < 0xff * 2 / 4) + { + operationMessage.push_back(message + std::string(" |")); + } + else if (i < 0xff * 3 / 4) + { + operationMessage.push_back(message + std::string(" \\")); + } + else + { + operationMessage.push_back(message + std::string(" -")); + } + i += 4; +} + void ControlState(::std::vector& operationMessage, bool& nextStep, bool& continueRestore) { // 状態遷移Controller @@ -324,24 +347,7 @@ void ControlState(::std::vector& operationMessage, bool& nextStep, // 動いていることを表示 { - static u8 i = 0; - if( i < 0xff / 4) - { - operationMessage.push_back(::std::string("Updating /")); - } - else if(i < 0xff * 2 / 4) - { - operationMessage.push_back(::std::string("Updating |")); - } - else if(i < 0xff * 3 / 4) - { - operationMessage.push_back(::std::string("Updating \\")); - } - else - { - operationMessage.push_back(::std::string("Updating -")); - } - i++; + PutAliveMessage(operationMessage, "Updating"); } @@ -482,9 +488,23 @@ void ControlState(::std::vector& operationMessage, bool& nextStep, AdjustTime(); init = false; } + + // 動いていることを表示 + { + PutAliveMessage(operationMessage, "Sync Clock"); + } + if(IsTimeAdjustFinished()) { - s_RestoreState = WAIT_SD_EJECT; + if(IsTimeAdjustSuccessed()) + { + s_RestoreState = WAIT_SD_EJECT; + } + else + { + s_RestoreState = FAIL; + } + } } break; diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.cpp b/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.cpp index e114529..5589ef0 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.cpp +++ b/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.cpp @@ -34,6 +34,7 @@ const size_t NTP_THREAD_STACK_SIZE = 0x1000; nn::os::Thread s_NtpThread; nn::os::StackBuffer s_NtpThreadStack; bool s_IsNtpThreadFinish = false; +bool s_NtpSyncSuccessed = false; struct NTP_Packet{ // NTPパケット @@ -53,59 +54,55 @@ NTP_Packet s_NTPSendPacket; // 送信するNTPパケット NTP_Packet s_NTPRecvPacket; // 受信するNTPパケット const u32 NTP_PORT_NUM = 123; - nn::Result InitializeNetwork(void) +nn::Result InitializeNetwork(void) +{ + nn::Result result; + nn::ac::Config config; + + result = nn::ac::Initialize(); + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + + // 接続要求用のパラメータを作成 + result = nn::ac::CreateDefaultConfig(&config); + if (result.IsFailure()) { - nn::Result result; - nn::ac::Config config; - - nn::fs::Initialize(); - result = nn::ac::Initialize(); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - // 接続要求用のパラメータを作成 - result = nn::ac::CreateDefaultConfig( &config ); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return result; - } - - // デバッグ用に接続テストを無効化 - nn::ac::DebugSetNetworkArea( &config, nn::ac::NETWORK_AREA_LAN ); - - // 接続要求を発行 - result = nn::ac::Connect( config ); - if (result.IsFailure()) - { - COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return result; - } - - return nn::ResultSuccess(); + return result; } - nn::Result FinalizeNetwork(void) + // デバッグ用に接続テストを無効化 + nn::ac::DebugSetNetworkArea(&config, nn::ac::NETWORK_AREA_LAN); + + // 接続要求を発行 + result = nn::ac::Connect(config); + if (result.IsFailure()) { - nn::Result result; - - // 接続要求用のパラメータを作成 - result = nn::ac::Close(); - NN_UTIL_RETURN_IF_FAILED(result); - - result = nn::ac::Finalize(); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - nn::fs::Finalize(); - - return nn::ResultSuccess(); + return result; } + + return nn::ResultSuccess(); } -u32 GetNtpTime() +nn::Result FinalizeNetwork(void) { nn::Result result; - u32 ntp_time; + // 接続要求用のパラメータを作成 + result = nn::ac::Close(); + NN_UTIL_RETURN_IF_FAILED(result); + + result = nn::ac::Finalize(); + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + + return nn::ResultSuccess(); +} + +bool GetNtpTime(u32* ntpTime) +{ + nn::Result result; + + bool retval = true; NN_LOG("Initializing network.\n"); // 本体に書き込まれているネットワーク設定を使ってネットワーク接続を初期化 @@ -157,7 +154,6 @@ u32 GetNtpTime() #ifndef NN_SWITCH_DISABLE_DEBUG_PRINT nn::socket::DumpRoutingTable(); #endif - NN_UNUSED_VAR(ret); } { @@ -191,6 +187,7 @@ u32 GetNtpTime() if (serverHostent == NULL) { COMMON_LOGGER("Error: GetHostByName %s\n", GetNtpServerName()); + retval = false; } else { @@ -217,6 +214,7 @@ u32 GetNtpTime() &serverSockAddrIn)) < 0) { COMMON_LOGGER("Error: Failed Send to Server, %d\n", ret); + retval = false; } NN_LOG("SendTo finished\n"); @@ -228,6 +226,7 @@ u32 GetNtpTime() if ((ret = nn::socket::Poll(&pollFd, 1, TIMEOUT_MILLISECOND)) < 0) { COMMON_LOGGER("Error: recv error, %d\n", ret); + retval = false; } NN_LOG("Poll Finished\n"); @@ -236,15 +235,17 @@ u32 GetNtpTime() { case nn::socket::POLLERR: // ソケットにエラーが発生しました。 COMMON_LOGGER("Error: POLLERR %s %d\n", __FILE__, __LINE__); + retval = false; break; case nn::socket::POLLHUP: // ストリーム・ソケットが未接続です。 COMMON_LOGGER("Error: POLLHUP %s %d\n", __FILE__, __LINE__); + retval = false; break; case nn::socket::POLLNVAL: // 不正なソケット記述子です。 COMMON_LOGGER("Error: POLLNVAL %s %d\n", __FILE__, __LINE__); + retval = false; break; default: - COMMON_LOGGER("Poll: No Error\n"); break; } @@ -254,15 +255,15 @@ u32 GetNtpTime() if ((ret = nn::socket::RecvFrom(socket, reinterpret_cast (&s_NTPRecvPacket), sizeof(s_NTPRecvPacket), nn::socket::MSG_DONTWAIT, &serverSockAddrIn)) < 0) { - NN_LOG("Error: RecvFrom, %d\n", ret); + COMMON_LOGGER("Error: RecvFrom, %d\n", ret); + retval = false; } NN_LOG("RecvFrom finished\n"); // NTPサーバから取得した時刻を現地時間に変換する - ntp_time = nn::socket::NtoHl(s_NTPRecvPacket.transmitTimestampSeconds) - 2208988800; /* 1970/01/01 からの秒数に変換 */ - - NN_LOG("ntp_time = %d\n", ntp_time); + *ntpTime = nn::socket::NtoHl(s_NTPRecvPacket.transmitTimestampSeconds) - 2208988800; /* 1970/01/01 からの秒数に変換 */ + NN_LOG("ntp_time = %d\n", ntpTime); nn::socket::Close(socket); NN_UNUSED_VAR(ret); @@ -272,16 +273,13 @@ u32 GetNtpTime() // ソケットライブラリの終了 result = nn::socket::Finalize(); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - - // ソケットライブラリを Finalize すればワーク領域を再利用できます。 - delete[] pWorkMemory; } NN_LOG("Finalizing network.\n"); result = FinalizeNetwork(); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); - return ntp_time; + return retval; } void RestoreCurrentInternetSetting() @@ -301,36 +299,46 @@ void RestoreCurrentInternetSetting() COMMON_LOGGER_RESULT_IF_FAILED(result); } +} + void NtpThreadFunc() { // NTP時間を取得する - u32 ntpTime = GetNtpTime(); - - // タイムゾーンを考慮してDateTimeに変換する - TimeZone timeZone = GetTimeZone(); - - // 1970/01/01 - nn::fnd::DateTime utc = nn::fnd::DateTime(1970, 1, 1, 0, 0, 0, 0); - nn::fnd::DateTime current = utc + nn::fnd::TimeSpan::FromSeconds(ntpTime); - - if(timeZone.isMinus) + u32 ntpTime; + if (GetNtpTime(&ntpTime)) { - current -= (nn::fnd::TimeSpan::FromHours(timeZone.hour) + nn::fnd::TimeSpan::FromMinutes(timeZone.minutes)); + // タイムゾーンを考慮してDateTimeに変換する + TimeZone timeZone = GetTimeZone(); + + // 1970/01/01 + nn::fnd::DateTime utc = nn::fnd::DateTime(1970, 1, 1, 0, 0, 0, 0); + nn::fnd::DateTime current = utc + nn::fnd::TimeSpan::FromSeconds(ntpTime); + + if (timeZone.isMinus) + { + current -= (nn::fnd::TimeSpan::FromHours(timeZone.hour) + nn::fnd::TimeSpan::FromMinutes(timeZone.minutes)); + } + else + { + current += nn::fnd::TimeSpan::FromHours(timeZone.hour) + nn::fnd::TimeSpan::FromMinutes(timeZone.minutes); + } + + // SWCを書き込む + nn::ptm::CTR::SetUserTime(current); + + COMMON_LOGGER("Set User Time %04d/%02d/%02d %02d:%02d:%02d\n", + current.GetYear(), current.GetMonth(), current.GetDay(), current.GetHour(), current.GetMinute(), current.GetSecond()); + + // インターネット設定を元に戻す + RestoreCurrentInternetSetting(); + s_NtpSyncSuccessed = true; } else { - current += nn::fnd::TimeSpan::FromHours(timeZone.hour) + nn::fnd::TimeSpan::FromMinutes(timeZone.minutes); + COMMON_LOGGER("Failed Get Ntp Time\n"); + s_NtpSyncSuccessed = false; } - // SWCを書き込む - nn::ptm::CTR::SetUserTime(current); - - COMMON_LOGGER("Set User Time %04d/%02d/%02d %02d:%02d:%02d\n", - current.GetYear(), current.GetMonth(), current.GetDay(), current.GetHour(), current.GetMinute(), current.GetSecond()); - - // インターネット設定を元に戻す - RestoreCurrentInternetSetting(); - s_IsNtpThreadFinish = true; } @@ -339,6 +347,11 @@ bool IsTimeAdjustFinished() return s_IsNtpThreadFinish; } +bool IsTimeAdjustSuccessed() +{ + return s_NtpSyncSuccessed; +} + void AdjustTime() { nn::Result result; diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.h b/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.h index 9c5d3e1..7c7bcf1 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.h +++ b/trunk/ConsoleDataMigration/ConsoleRestore/NtpClient.h @@ -21,6 +21,7 @@ namespace ConsoleRestore u32 AdjustTime(); bool IsTimeAdjustFinished(); +bool IsTimeAdjustSuccessed(); } diff --git a/trunk/ConsoleDataMigration/common/CommonLogger.cpp b/trunk/ConsoleDataMigration/common/CommonLogger.cpp index c2e5f21..c75a924 100644 --- a/trunk/ConsoleDataMigration/common/CommonLogger.cpp +++ b/trunk/ConsoleDataMigration/common/CommonLogger.cpp @@ -66,6 +66,7 @@ void CommonLogger::Print(const char* fmt, ...) void CommonLogger::PrintResult(const char* fmt, ...) { + nn::os::CriticalSection::ScopedLock lock(m_CriticalSection); va_list vlist; va_start(vlist, fmt);