ftpiiu_plugin/include/platform.h

176 lines
4.1 KiB
C
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2024 Michael Theall
2020-04-05 21:16:16 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that 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 <https://www.gnu.org/licenses/>.
#pragma once
2020-12-12 02:15:33 +01:00
#include "sockAddr.h"
#if defined(__NDS__)
2020-04-17 22:32:39 +02:00
#include <nds.h>
2022-03-31 18:53:47 +02:00
#elif defined(__3DS__)
2020-04-05 21:16:16 +02:00
#include <3ds.h>
2020-04-17 22:32:39 +02:00
#elif defined(__SWITCH__)
#include <switch.h>
2023-11-19 13:55:58 +01:00
#elif defined(__WIIU__)
#include <coreinit/debug.h>
#include <wut.h>
2020-04-05 21:16:16 +02:00
#endif
#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
2020-04-05 21:16:16 +02:00
#if defined(CLASSIC) && !defined(__WIIU__)
2020-04-17 22:32:39 +02:00
extern PrintConsole g_statusConsole;
extern PrintConsole g_logConsole;
extern PrintConsole g_sessionConsole;
#endif
2020-04-05 21:16:16 +02:00
namespace platform
{
2020-04-06 07:36:03 +02:00
/// \brief Initialize platform
2020-04-05 21:16:16 +02:00
bool init ();
2020-04-06 07:36:03 +02:00
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
/// \brief Enable access point
/// \param enable_ Whether to enable access point
/// \param ssid_ SSID
/// \param passphrase_ Passphrase
bool enableAP (bool enable_, std::string const &ssid_, std::string const &passphrase_);
/// \brief Check if SSID is valid
/// \param ssid_ SSID to check
/// \returns empty string on success, error message on failure
char const *validateSSID (std::string const &ssid_);
/// \brief Check if passphrase is valid
/// \param passphrase_ Passphrase to check
/// \returns empty string on success, error message on failure
char const *validatePassphrase (std::string const &passphrase_);
#endif
/// \brief Whether network is visible
bool networkVisible ();
2020-12-12 02:15:33 +01:00
/// \brief Get network address
/// \param[out] addr_ Network address
bool networkAddress (SockAddr &addr_);
/// \brief Get hostname
std::string const &hostname ();
2020-04-06 07:36:03 +02:00
/// \brief Platform loop
2020-04-05 21:16:16 +02:00
bool loop ();
2020-04-06 07:36:03 +02:00
/// \brief Platform render
2020-04-05 21:16:16 +02:00
void render ();
2020-04-06 07:36:03 +02:00
/// \brief Deinitialize platform
2020-04-05 21:16:16 +02:00
void exit ();
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-06 07:36:03 +02:00
/// \brief Steady clock
2020-04-05 21:16:16 +02:00
struct steady_clock
{
2020-04-06 07:36:03 +02:00
/// \brief Type representing number of ticks
using rep = std::uint64_t;
/// \brief Type representing ratio of clock period in seconds
using period = std::ratio<1, SYSCLOCK_ARM11>;
/// \brief Duration type
using duration = std::chrono::duration<rep, period>;
/// \brief Timestamp type
2020-04-05 21:16:16 +02:00
using time_point = std::chrono::time_point<steady_clock>;
2020-04-06 07:36:03 +02:00
/// \brief Whether clock is steady
2020-04-05 21:16:16 +02:00
constexpr static bool is_steady = true;
2020-04-06 07:36:03 +02:00
/// \brief Current timestamp
2020-04-17 22:32:39 +02:00
static time_point now () noexcept;
2020-04-05 21:16:16 +02:00
};
#else
2020-04-06 07:36:03 +02:00
/// \brief Steady clock
2020-04-05 21:16:16 +02:00
using steady_clock = std::chrono::steady_clock;
#endif
#ifndef __NDS__
2020-04-06 07:36:03 +02:00
/// \brief Platform thread
2020-04-05 21:16:16 +02:00
class Thread
{
public:
~Thread ();
Thread ();
2020-04-06 07:36:03 +02:00
/// \brief Parameterized constructor
/// \param func_ Thread entrypoint
2020-04-07 04:17:30 +02:00
Thread (std::function<void ()> &&func_);
2020-04-05 21:16:16 +02:00
Thread (Thread const &that_) = delete;
2020-04-06 07:36:03 +02:00
/// \brief Move constructor
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
Thread (Thread &&that_);
Thread &operator= (Thread const &that_) = delete;
2020-04-06 07:36:03 +02:00
/// \brief Move assignment
/// \param that_ Object to move from
2020-04-05 21:16:16 +02:00
Thread &operator= (Thread &&that_);
2020-04-06 07:36:03 +02:00
/// \brief Join thread
2020-04-05 21:16:16 +02:00
void join ();
2020-04-06 07:36:03 +02:00
/// \brief Suspend current thread
/// \param timeout_ Minimum time to sleep
2020-04-05 21:16:16 +02:00
static void sleep (std::chrono::milliseconds timeout_);
private:
class privateData_t;
2020-04-06 07:36:03 +02:00
/// \brief pimpl
2020-04-05 21:16:16 +02:00
std::unique_ptr<privateData_t> m_d;
};
2020-04-06 07:36:03 +02:00
/// \brief Platform mutex
2020-04-05 21:16:16 +02:00
class Mutex
{
public:
~Mutex ();
Mutex ();
2020-04-06 07:36:03 +02:00
/// \brief Lock mutex
2020-04-05 21:16:16 +02:00
void lock ();
2020-04-06 07:36:03 +02:00
/// \brief Unlock mutex
2020-04-05 21:16:16 +02:00
void unlock ();
private:
class privateData_t;
2020-04-06 07:36:03 +02:00
/// \brief pimpl
2020-04-05 21:16:16 +02:00
std::unique_ptr<privateData_t> m_d;
};
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
}