rosalina: ntp: use PTMSYSM_SetRtcTime

This commit is contained in:
TuxSH 2020-05-18 22:43:00 +01:00
parent 704e08dc23
commit 0471002d4c
3 changed files with 13 additions and 47 deletions

View File

@ -30,4 +30,4 @@
#include <time.h>
Result ntpGetTimeStamp(time_t *outTimestamp);
Result ntpSetTimeDate(const struct tm *localt);
Result ntpSetTimeDate(time_t timestamp);

View File

@ -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);
}
}

View File

@ -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, &lt);
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;
}