From 57856fa2a69b5961840cbb67aebef102060bf94e Mon Sep 17 00:00:00 2001 From: staphen Date: Fri, 4 Jun 2021 12:47:32 -0400 Subject: [PATCH] 3DS SOC service initialization functions --- Source/platform/ctr/sockets.cpp | 66 +++++++++++++++++++++++++++++++++ Source/platform/ctr/sockets.hpp | 8 ++++ 2 files changed, 74 insertions(+) create mode 100644 Source/platform/ctr/sockets.cpp create mode 100644 Source/platform/ctr/sockets.hpp diff --git a/Source/platform/ctr/sockets.cpp b/Source/platform/ctr/sockets.cpp new file mode 100644 index 000000000..2f63ac075 --- /dev/null +++ b/Source/platform/ctr/sockets.cpp @@ -0,0 +1,66 @@ +#include +#include <3ds.h> +#include "utils/log.hpp" + +namespace devilution { + +constexpr auto SOC_ALIGN = 0x1000; +constexpr auto SOC_BUFFERSIZE = 0x100000; +static u32 *socBuffer; +static bool initialized; + +static bool waitForWifi() +{ + // 100 ms + constexpr s64 sleepNano = 100 * 1000 * 1000; + + // 5 sec + constexpr int loopCount = 5 * 1000 / 100; + + uint32_t wifi = 0; + for (int i = 0; i < loopCount; ++i) { + if (R_SUCCEEDED(ACU_GetWifiStatus(&wifi)) && wifi) + return true; + + svcSleepThread(sleepNano); + } + + return false; +} + +void n3ds_socExit() +{ + if (socBuffer == nullptr) + return; + + socExit(); + free(socBuffer); + socBuffer = nullptr; +} + +void n3ds_socInit() +{ + if (!waitForWifi()) { + LogError("n3ds_socInit: Wifi off"); + return; + } + + socBuffer = (u32 *)memalign(SOC_ALIGN, SOC_BUFFERSIZE); + if (socBuffer == nullptr) { + LogError("n3ds_socInit: memalign() failed"); + return; + } + + Result result = socInit(socBuffer, SOC_BUFFERSIZE); + if (!R_SUCCEEDED(result)) { + LogError("n3ds_socInit: socInit() failed"); + free(socBuffer); + return; + } + + if (!initialized) + atexit([]() { n3ds_socExit(); }); + initialized = true; +} + +} // namespace devilution diff --git a/Source/platform/ctr/sockets.hpp b/Source/platform/ctr/sockets.hpp new file mode 100644 index 000000000..fdc5cb55a --- /dev/null +++ b/Source/platform/ctr/sockets.hpp @@ -0,0 +1,8 @@ +#pragma once + +namespace devilution { + +void n3ds_socInit(); +void n3ds_socExit(); + +} // namespace devilution