2020-04-14 00:07:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-04-14 00:07:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2020-04-14 00:07:37 +00:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
extern TimeServiceType __nx_time_service_type;
|
|
|
|
|
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
#endif
|
2020-04-14 00:07:37 +00:00
|
|
|
|
|
|
|
namespace ams::time {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum InitializeMode {
|
|
|
|
InitializeMode_None,
|
|
|
|
InitializeMode_Normal,
|
|
|
|
InitializeMode_Menu,
|
|
|
|
InitializeMode_System,
|
|
|
|
InitializeMode_Repair,
|
|
|
|
InitializeMode_SystemUser,
|
|
|
|
};
|
|
|
|
|
2021-09-12 02:32:14 +00:00
|
|
|
constinit u32 g_initialize_count = 0;
|
|
|
|
constinit InitializeMode g_initialize_mode = InitializeMode_None;
|
2020-04-14 00:07:37 +00:00
|
|
|
|
2021-09-12 02:32:14 +00:00
|
|
|
constinit os::SdkMutex g_initialize_mutex;
|
2020-04-14 00:07:37 +00:00
|
|
|
|
|
|
|
Result InitializeImpl(InitializeMode mode) {
|
|
|
|
std::scoped_lock lk(g_initialize_mutex);
|
|
|
|
|
|
|
|
if (g_initialize_count > 0) {
|
|
|
|
AMS_ABORT_UNLESS(mode == g_initialize_mode);
|
|
|
|
g_initialize_count++;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2020-04-14 00:07:37 +00:00
|
|
|
switch (mode) {
|
|
|
|
case InitializeMode_Normal: __nx_time_service_type = ::TimeServiceType_User; break;
|
|
|
|
case InitializeMode_Menu: __nx_time_service_type = ::TimeServiceType_Menu; break;
|
|
|
|
case InitializeMode_System: __nx_time_service_type = ::TimeServiceType_System; break;
|
|
|
|
case InitializeMode_Repair: __nx_time_service_type = ::TimeServiceType_Repair; break;
|
|
|
|
case InitializeMode_SystemUser: __nx_time_service_type = ::TimeServiceType_SystemUser; break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
R_TRY(::timeInitialize());
|
2022-03-06 20:08:20 +00:00
|
|
|
#else
|
2022-03-27 21:36:31 +00:00
|
|
|
// TODO: Real AMS_ABORT("TODO");
|
2022-03-06 20:08:20 +00:00
|
|
|
#endif
|
2020-04-14 00:07:37 +00:00
|
|
|
|
|
|
|
g_initialize_count++;
|
|
|
|
g_initialize_mode = mode;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Initialize() {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(InitializeImpl(InitializeMode_Normal));
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result InitializeForSystem() {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(InitializeImpl(InitializeMode_System));
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result InitializeForSystemUser() {
|
2021-09-12 02:32:14 +00:00
|
|
|
if (hos::GetVersion() >= hos::Version_9_0_0) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(InitializeImpl(InitializeMode_SystemUser));
|
2021-09-12 02:32:14 +00:00
|
|
|
} else {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(InitializeImpl(InitializeMode_Normal));
|
2021-09-12 02:32:14 +00:00
|
|
|
}
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Finalize() {
|
|
|
|
std::scoped_lock lk(g_initialize_mutex);
|
|
|
|
|
|
|
|
if (g_initialize_count > 0) {
|
|
|
|
if ((--g_initialize_count) == 0) {
|
2022-03-06 20:08:20 +00:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2020-04-14 00:07:37 +00:00
|
|
|
::timeExit();
|
2022-03-06 20:08:20 +00:00
|
|
|
#else
|
2022-03-27 21:36:31 +00:00
|
|
|
// TODO: Real AMS_ABORT("TODO");
|
2022-03-06 20:08:20 +00:00
|
|
|
#endif
|
2020-04-14 00:07:37 +00:00
|
|
|
g_initialize_mode = InitializeMode_None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsInitialized() {
|
|
|
|
std::scoped_lock lk(g_initialize_mutex);
|
|
|
|
|
|
|
|
return g_initialize_count > 0;
|
|
|
|
}
|
|
|
|
|
2021-09-12 02:32:14 +00:00
|
|
|
bool IsValidDate(int year, int month, int day) {
|
|
|
|
return impl::util::IsValidDate(year, month, day);
|
|
|
|
}
|
|
|
|
|
2020-04-14 00:07:37 +00:00
|
|
|
Result GetElapsedSecondsBetween(s64 *out, const SteadyClockTimePoint &from, const SteadyClockTimePoint &to) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(impl::util::GetSpanBetween(out, from, to));
|
2020-04-14 00:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|