From 0471002d4cb1fc94e0dbfef626e071018cf796b1 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Mon, 18 May 2020 22:43:00 +0100 Subject: [PATCH] rosalina: ntp: use PTMSYSM_SetRtcTime --- sysmodules/rosalina/include/ntp.h | 2 +- .../rosalina/source/menus/miscellaneous.c | 4 +- sysmodules/rosalina/source/ntp.c | 54 ++++--------------- 3 files changed, 13 insertions(+), 47 deletions(-) diff --git a/sysmodules/rosalina/include/ntp.h b/sysmodules/rosalina/include/ntp.h index d6e4f45..89cff94 100644 --- a/sysmodules/rosalina/include/ntp.h +++ b/sysmodules/rosalina/include/ntp.h @@ -30,4 +30,4 @@ #include Result ntpGetTimeStamp(time_t *outTimestamp); -Result ntpSetTimeDate(const struct tm *localt); +Result ntpSetTimeDate(time_t timestamp); diff --git a/sysmodules/rosalina/source/menus/miscellaneous.c b/sysmodules/rosalina/source/menus/miscellaneous.c index 20f03a3..809275b 100644 --- a/sysmodules/rosalina/source/menus/miscellaneous.c +++ b/sysmodules/rosalina/source/menus/miscellaneous.c @@ -341,7 +341,6 @@ void MiscellaneousMenu_SyncTimeDate(void) bool isSocURegistered; time_t t; - struct tm localt = {0}; res = srvIsServiceRegistered(&isSocURegistered, "soc:U"); cantStart = R_FAILED(res) || !isSocURegistered; @@ -385,8 +384,7 @@ void MiscellaneousMenu_SyncTimeDate(void) { t += 3600 * utcOffset; t += 60 * utcOffsetMinute; - gmtime_r(&t, &localt); - res = ntpSetTimeDate(&localt); + res = ntpSetTimeDate(t); } } diff --git a/sysmodules/rosalina/source/ntp.c b/sysmodules/rosalina/source/ntp.c index 88ac87f..b721834 100644 --- a/sysmodules/rosalina/source/ntp.c +++ b/sysmodules/rosalina/source/ntp.c @@ -40,18 +40,6 @@ #define NTP_IP MAKE_IPV4(51, 137, 137, 111) // time.windows.com #endif -typedef struct RtcTime { - // From 3dbrew - u8 seconds; - u8 minutes; - u8 hours; - u8 dayofweek; - u8 dayofmonth; - u8 month; - u8 year; - u8 leapcount; -} RtcTime; - // From https://github.com/lettier/ntpclient/blob/master/source/c/main.c typedef struct NtpPacket @@ -84,17 +72,6 @@ typedef struct NtpPacket } NtpPacket; // Total: 384 bits or 48 bytes. -void rtcToBcd(u8 *out, const RtcTime *in) -{ - memcpy(out, in, 8); - for (u32 i = 0; i < 8; i++) - { - u8 units = out[i] % 10; - u8 tens = (out[i] - units) / 10; - out[i] = (tens << 4) | units; - } -} - Result ntpGetTimeStamp(time_t *outTimestamp) { Result res = 0; @@ -162,42 +139,33 @@ cleanup: return res; } -Result ntpSetTimeDate(const struct tm *localt) +Result ntpSetTimeDate(time_t timestamp) { - Result res = mcuHwcInit(); + Result res = ptmSysmInit(); if (R_FAILED(res)) return res; - res = cfguInit(); - if (R_FAILED(res)) goto cleanup; + if (R_FAILED(res)) + { + ptmSysmExit(); + return res; + } // First, set the config RTC offset to 0 u8 rtcOff[8] = {0}; res = CFG_SetConfigInfoBlk4(8, 0x30001, rtcOff); if (R_FAILED(res)) goto cleanup; - u8 yr = (u8)(localt->tm_year - 100); // Update the RTC - u8 bcd[8]; - RtcTime lt = { - .seconds = (u8)localt->tm_sec, - .minutes = (u8)localt->tm_min, - .hours = (u8)localt->tm_hour, - .dayofweek = (u8)localt->tm_wday, - .dayofmonth = (u8)localt->tm_mday, - .month = (u8)(localt->tm_mon + 1), - .year = yr, - .leapcount = 0, - }; - rtcToBcd(bcd, <); - - res = MCUHWC_WriteRegister(0x30, bcd, 7); + // 946684800 is the timestamp of 01/01/2000 00:00 relative to the Unix Epoch + s64 msY2k = (timestamp - 946684800) * 1000; + res = PTMSYSM_SetRtcTime(msY2k); if (R_FAILED(res)) goto cleanup; // Save the config changes res = CFG_UpdateConfigSavegame(); cleanup: - mcuHwcExit(); + ptmSysmExit(); cfguExit(); return res; }