tio: implement SdCardObserver (finishes sysmodule)
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include "tio_file_server_packet.hpp"
|
||||
#include "tio_file_server_htcs_server.hpp"
|
||||
#include "tio_file_server_processor.hpp"
|
||||
#include "tio_sd_card_observer.hpp"
|
||||
|
||||
namespace ams::tio {
|
||||
|
||||
@@ -36,10 +37,12 @@ namespace ams::tio {
|
||||
constexpr const char HtcsPortName[] = "iywys@$TioServer_FileServer";
|
||||
|
||||
alignas(os::ThreadStackAlignment) u8 g_server_stack[os::MemoryPageSize];
|
||||
alignas(os::ThreadStackAlignment) u8 g_observer_stack[os::MemoryPageSize];
|
||||
alignas(os::ThreadStackAlignment) u8 g_dispatch_stacks[NumDispatchThreads][os::MemoryPageSize];
|
||||
|
||||
constinit FileServerHtcsServer g_file_server_htcs_server;
|
||||
constinit FileServerProcessor g_file_server_processor(g_file_server_htcs_server);
|
||||
constinit SdCardObserver g_sd_card_observer;
|
||||
|
||||
constinit os::ThreadType g_file_server_dispatch_threads[NumDispatchThreads];
|
||||
|
||||
@@ -51,6 +54,10 @@ namespace ams::tio {
|
||||
constinit uintptr_t g_free_mq_storage[NumDispatchThreads];
|
||||
constinit uintptr_t g_dispatch_mq_storage[NumDispatchThreads];
|
||||
|
||||
void OnSdCardInsertionChanged(bool inserted) {
|
||||
g_file_server_processor.SetInserted(inserted);
|
||||
}
|
||||
|
||||
void OnFileServerHtcsSocketAccepted(int fd) {
|
||||
/* Service requests, while we can. */
|
||||
while (true) {
|
||||
@@ -110,10 +117,12 @@ namespace ams::tio {
|
||||
/* Initialize the htcs server. */
|
||||
g_file_server_htcs_server.Initialize(HtcsPortName, g_server_stack, sizeof(g_server_stack), OnFileServerHtcsSocketAccepted);
|
||||
|
||||
/* TODO: Initialize SD card observer. */
|
||||
/* Initialize SD card observer. */
|
||||
g_sd_card_observer.Initialize(g_observer_stack, sizeof(g_observer_stack));
|
||||
g_sd_card_observer.SetCallback(OnSdCardInsertionChanged);
|
||||
|
||||
/* Initialize the command processor. */
|
||||
g_file_server_processor.SetInserted(false);
|
||||
g_file_server_processor.SetInserted(g_sd_card_observer.IsSdCardInserted());
|
||||
g_file_server_processor.SetRequestBufferSize(RequestBufferSize);
|
||||
|
||||
/* Initialize the dispatch message queues. */
|
||||
|
||||
64
stratosphere/TioServer/source/tio_sd_card_observer.cpp
Normal file
64
stratosphere/TioServer/source/tio_sd_card_observer.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2018-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>
|
||||
#include "tio_sd_card_observer.hpp"
|
||||
|
||||
namespace ams::tio {
|
||||
|
||||
void SdCardObserver::Initialize(void *thread_stack, size_t thread_stack_size) {
|
||||
/* Setup our thread. */
|
||||
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_thread), ThreadEntry, this, thread_stack, thread_stack_size, AMS_GET_SYSTEM_THREAD_PRIORITY(TioServer, SdCardObserver)));
|
||||
|
||||
/* Set our thread name pointer. */
|
||||
os::SetThreadNamePointer(std::addressof(m_thread), AMS_GET_SYSTEM_THREAD_NAME(TioServer, SdCardObserver));
|
||||
|
||||
/* Set our initial insertion state. */
|
||||
m_inserted = fs::IsSdCardInserted();
|
||||
}
|
||||
|
||||
void SdCardObserver::SetCallback(SdCardInsertionCallback callback) {
|
||||
/* Check that we don't already have a callback. */
|
||||
AMS_ABORT_UNLESS(m_callback == nullptr);
|
||||
|
||||
/* Set our callback. */
|
||||
m_callback = callback;
|
||||
}
|
||||
|
||||
void SdCardObserver::ThreadFunc() {
|
||||
/* Open detection event notifier. */
|
||||
std::unique_ptr<fs::IEventNotifier> notifier;
|
||||
R_ABORT_UNLESS(fs::OpenSdCardDetectionEventNotifier(std::addressof(notifier)));
|
||||
|
||||
/* Bind the detection event. */
|
||||
os::SystemEventType event;
|
||||
R_ABORT_UNLESS(notifier->BindEvent(std::addressof(event), os::EventClearMode_AutoClear));
|
||||
|
||||
/* Loop, waiting for insertion events. */
|
||||
while (true) {
|
||||
/* Wait for an event. */
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
|
||||
/* Update our insertion state. */
|
||||
m_inserted = fs::IsSdCardInserted();
|
||||
|
||||
/* Invoke our callback. */
|
||||
if (m_callback) {
|
||||
m_callback(m_inserted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
43
stratosphere/TioServer/source/tio_sd_card_observer.hpp
Normal file
43
stratosphere/TioServer/source/tio_sd_card_observer.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2018-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/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::tio {
|
||||
|
||||
using SdCardInsertionCallback = void(*)(bool inserted);
|
||||
|
||||
class SdCardObserver {
|
||||
private:
|
||||
bool m_inserted;
|
||||
SdCardInsertionCallback m_callback;
|
||||
os::ThreadType m_thread;
|
||||
public:
|
||||
constexpr SdCardObserver() : m_inserted(false), m_callback(nullptr), m_thread{} { /* ... */ }
|
||||
|
||||
bool IsSdCardInserted() const { return m_inserted; }
|
||||
private:
|
||||
static void ThreadEntry(void *arg) {
|
||||
static_cast<SdCardObserver *>(arg)->ThreadFunc();
|
||||
}
|
||||
|
||||
void ThreadFunc();
|
||||
public:
|
||||
void Initialize(void *thread_stack, size_t thread_stack_size);
|
||||
void SetCallback(SdCardInsertionCallback callback);
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user