erpt: reimplement the sysmodule (#875)

* erpt: reimplement the sysmodule

* fatal: update for latest bindings

* erpt: amend logic for culling orphan attachments
This commit is contained in:
SciresM 2020-04-13 17:07:37 -07:00 committed by GitHub
parent eca5ac01b8
commit 79b9e07ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
117 changed files with 6716 additions and 59 deletions

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2019-2020 Atmosphère-NX
*
* 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>
namespace ams::time::impl::util {
Result GetSpanBetween(s64 *out, const SteadyClockTimePoint &from, const SteadyClockTimePoint &to) {
AMS_ASSERT(out != nullptr);
R_UNLESS(out != nullptr, time::ResultInvalidPointer());
R_UNLESS(from.source_id == to.source_id, time::ResultNotComparable());
const bool no_overflow = (from.value >= 0 ? (to.value >= std::numeric_limits<s64>::min() + from.value)
: (to.value <= std::numeric_limits<s64>::max() + from.value));
R_UNLESS(no_overflow, time::ResultOverflowed());
*out = to.value - from.value;
return ResultSuccess();
}
}

View file

@ -0,0 +1,105 @@
/*
* Copyright (c) 2019-2020 Atmosphère-NX
*
* 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>
extern "C" {
extern TimeServiceType __nx_time_service_type;
}
namespace ams::time {
namespace {
enum InitializeMode {
InitializeMode_None,
InitializeMode_Normal,
InitializeMode_Menu,
InitializeMode_System,
InitializeMode_Repair,
InitializeMode_SystemUser,
};
u32 g_initialize_count = 0;
InitializeMode g_initialize_mode = InitializeMode_None;
/* TODO: os::SdkMutex */
os::Mutex g_initialize_mutex(false);
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++;
return ResultSuccess();
}
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());
g_initialize_count++;
g_initialize_mode = mode;
return ResultSuccess();
}
}
Result Initialize() {
return InitializeImpl(InitializeMode_Normal);
}
Result InitializeForSystem() {
return InitializeImpl(InitializeMode_System);
}
Result InitializeForSystemUser() {
return InitializeImpl(InitializeMode_System);
}
Result Finalize() {
std::scoped_lock lk(g_initialize_mutex);
if (g_initialize_count > 0) {
if ((--g_initialize_count) == 0) {
::timeExit();
g_initialize_mode = InitializeMode_None;
}
}
return ResultSuccess();
}
bool IsInitialized() {
std::scoped_lock lk(g_initialize_mutex);
return g_initialize_count > 0;
}
Result GetElapsedSecondsBetween(s64 *out, const SteadyClockTimePoint &from, const SteadyClockTimePoint &to) {
return impl::util::GetSpanBetween(out, from, to);
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2020 Atmosphère-NX
*
* 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>
namespace ams::time {
Result StandardNetworkSystemClock::GetCurrentTime(PosixTime *out) {
static_assert(sizeof(*out) == sizeof(u64));
return ::timeGetCurrentTime(::TimeType_NetworkSystemClock, reinterpret_cast<u64 *>(out));
}
StandardNetworkSystemClock::time_point StandardNetworkSystemClock::now() {
PosixTime posix_time = {};
if (R_FAILED(GetCurrentTime(std::addressof(posix_time)))) {
posix_time.value = 0;
}
return time_point(duration(posix_time.value));
}
std::time_t StandardNetworkSystemClock::to_time_t(const StandardNetworkSystemClock::time_point &t) {
return static_cast<std::time_t>(std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count());
}
StandardNetworkSystemClock::time_point StandardNetworkSystemClock::from_time_t(std::time_t t) {
return time_point(duration(t));
}
/* TODO: Result StandardNetworkSystemClock::GetSystemClockContext(SystemClockContext *out); */
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2019-2020 Atmosphère-NX
*
* 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>
namespace ams::time {
Result GetStandardSteadyClockCurrentTimePoint(SteadyClockTimePoint *out) {
static_assert(sizeof(*out) == sizeof(::TimeSteadyClockTimePoint));
return ::timeGetStandardSteadyClockTimePoint(reinterpret_cast<::TimeSteadyClockTimePoint *>(out));
}
TimeSpan GetStandardSteadyClockInternalOffset() {
static_assert(sizeof(TimeSpanType) == sizeof(s64));
TimeSpanType offset;
R_ABORT_UNLESS(::timeGetStandardSteadyClockInternalOffset(reinterpret_cast<s64 *>(std::addressof(offset))));
return offset;
}
Result StandardSteadyClock::GetCurrentTimePoint(SteadyClockTimePoint *out) {
return GetStandardSteadyClockCurrentTimePoint(out);
}
StandardSteadyClock::time_point StandardSteadyClock::now() {
SteadyClockTimePoint steady_clock_time_point = {0, util::InvalidUuid};
if (R_FAILED(StandardSteadyClock::GetCurrentTimePoint(std::addressof(steady_clock_time_point)))) {
steady_clock_time_point.value = 0;
steady_clock_time_point.source_id = util::InvalidUuid;
}
return StandardSteadyClock::time_point(StandardSteadyClock::duration(steady_clock_time_point.value));
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2020 Atmosphère-NX
*
* 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>
namespace ams::time {
Result StandardUserSystemClock::GetCurrentTime(PosixTime *out) {
static_assert(sizeof(*out) == sizeof(u64));
return ::timeGetCurrentTime(::TimeType_UserSystemClock, reinterpret_cast<u64 *>(out));
}
StandardUserSystemClock::time_point StandardUserSystemClock::now() {
PosixTime posix_time = {};
if (R_FAILED(GetCurrentTime(std::addressof(posix_time)))) {
posix_time.value = 0;
}
return time_point(duration(posix_time.value));
}
std::time_t StandardUserSystemClock::to_time_t(const StandardUserSystemClock::time_point &t) {
return static_cast<std::time_t>(std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count());
}
StandardUserSystemClock::time_point StandardUserSystemClock::from_time_t(std::time_t t) {
return time_point(duration(t));
}
/* TODO: Result StandardUserSystemClock::GetSystemClockContext(SystemClockContext *out); */
}