2021-02-09 14:16:43 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
#include "../driver/htc_i_driver.hpp"
|
2021-02-10 07:09:28 +00:00
|
|
|
#include "htc_rpc_task_table.hpp"
|
2021-02-10 08:59:46 +00:00
|
|
|
#include "htc_rpc_task_queue.hpp"
|
|
|
|
#include "htc_rpc_task_id_free_list.hpp"
|
2021-02-09 14:16:43 +00:00
|
|
|
|
|
|
|
namespace ams::htc::server::rpc {
|
|
|
|
|
2021-02-18 07:28:05 +00:00
|
|
|
template<typename T>
|
|
|
|
concept IsRpcTask = std::derived_from<T, Task>;
|
|
|
|
|
|
|
|
struct RpcTaskFunctionTraits {
|
|
|
|
public:
|
|
|
|
template<typename R, typename C, typename... A>
|
2021-02-18 10:49:37 +00:00
|
|
|
static std::tuple<A...> GetSetArgumentsImpl(R(C::*)(A...));
|
|
|
|
template<typename R, typename C, typename... A>
|
|
|
|
static std::tuple<A...> GetGetResultImpl(R(C::*)(A...) const);
|
2021-02-18 07:28:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> requires IsRpcTask<T>
|
2021-02-18 10:49:37 +00:00
|
|
|
using RpcTaskArgumentsType = decltype(RpcTaskFunctionTraits::GetSetArgumentsImpl(&T::SetArguments));
|
2021-02-18 07:28:05 +00:00
|
|
|
|
|
|
|
template<typename T> requires IsRpcTask<T>
|
2021-02-18 10:49:37 +00:00
|
|
|
using RpcTaskResultType = decltype(RpcTaskFunctionTraits::GetGetResultImpl(&T::GetResult));
|
2021-02-18 07:28:05 +00:00
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
concept IsRpcTaskArgumentsType = IsRpcTask<T> && std::same_as<std::tuple<Args...>, RpcTaskArgumentsType<T>>;
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
concept IsRpcTaskResultType = IsRpcTask<T> && std::same_as<std::tuple<Args...>, RpcTaskResultType<T>>;
|
|
|
|
|
2021-02-09 14:16:43 +00:00
|
|
|
class RpcClient {
|
2021-02-10 08:59:46 +00:00
|
|
|
private:
|
|
|
|
/* TODO: where is this value coming from, again? */
|
|
|
|
static constexpr size_t BufferSize = 0xE400;
|
2021-02-09 14:16:43 +00:00
|
|
|
private:
|
2021-02-11 02:54:40 +00:00
|
|
|
mem::StandardAllocator *m_allocator;
|
2021-02-09 14:16:43 +00:00
|
|
|
driver::IDriver *m_driver;
|
|
|
|
htclow::ChannelId m_channel_id;
|
|
|
|
void *m_receive_thread_stack;
|
|
|
|
void *m_send_thread_stack;
|
|
|
|
os::ThreadType m_receive_thread;
|
|
|
|
os::ThreadType m_send_thread;
|
2021-02-10 04:43:40 +00:00
|
|
|
os::SdkMutex &m_mutex;
|
2021-02-10 08:59:46 +00:00
|
|
|
RpcTaskIdFreeList &m_task_id_free_list;
|
|
|
|
RpcTaskTable &m_task_table;
|
|
|
|
bool m_task_active[MaxRpcCount];
|
|
|
|
RpcTaskQueue m_task_queue;
|
2021-02-09 14:16:43 +00:00
|
|
|
bool m_cancelled;
|
|
|
|
bool m_thread_running;
|
2021-02-10 08:59:46 +00:00
|
|
|
os::EventType m_receive_buffer_available_events[MaxRpcCount];
|
|
|
|
os::EventType m_send_buffer_available_events[MaxRpcCount];
|
2021-02-10 10:05:45 +00:00
|
|
|
char m_receive_buffer[BufferSize];
|
|
|
|
char m_send_buffer[BufferSize];
|
2021-02-10 09:29:40 +00:00
|
|
|
private:
|
|
|
|
static void ReceiveThreadEntry(void *arg) { static_cast<RpcClient *>(arg)->ReceiveThread(); }
|
|
|
|
static void SendThreadEntry(void *arg) { static_cast<RpcClient *>(arg)->SendThread(); }
|
|
|
|
|
2021-02-10 10:05:45 +00:00
|
|
|
Result ReceiveThread();
|
|
|
|
Result SendThread();
|
2021-02-09 14:16:43 +00:00
|
|
|
public:
|
|
|
|
RpcClient(driver::IDriver *driver, htclow::ChannelId channel);
|
2021-02-11 02:54:40 +00:00
|
|
|
RpcClient(mem::StandardAllocator *allocator, driver::IDriver *driver, htclow::ChannelId channel);
|
|
|
|
~RpcClient();
|
2021-02-10 09:29:40 +00:00
|
|
|
public:
|
|
|
|
void Open();
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
Result Start();
|
|
|
|
void Cancel();
|
|
|
|
void Wait();
|
|
|
|
|
|
|
|
int WaitAny(htclow::ChannelState state, os::EventType *event);
|
2021-02-10 10:05:45 +00:00
|
|
|
private:
|
|
|
|
Result ReceiveHeader(RpcPacket *header);
|
|
|
|
Result ReceiveBody(char *dst, size_t size);
|
|
|
|
Result SendRequest(const char *src, size_t size);
|
2021-02-18 07:28:05 +00:00
|
|
|
public:
|
|
|
|
void Wait(u32 task_id) {
|
|
|
|
os::WaitEvent(m_task_table.Get<Task>(task_id)->GetEvent());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args> requires IsRpcTaskArgumentsType<T, Args...>
|
|
|
|
Result Begin(u32 *out_task_id, Args... args) {
|
|
|
|
/* Lock ourselves. */
|
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
|
|
|
|
/* Allocate a free task id. */
|
|
|
|
u32 task_id;
|
|
|
|
R_TRY(m_task_id_free_list.Allocate(std::addressof(task_id)));
|
|
|
|
|
|
|
|
/* Create the new task. */
|
|
|
|
T *task = m_task_table.New<T>(task_id);
|
|
|
|
m_task_active[task_id] = true;
|
|
|
|
|
|
|
|
/* Ensure we clean up the task, if we fail after this. */
|
|
|
|
auto task_guard = SCOPE_GUARD {
|
|
|
|
m_task_active[task_id] = false;
|
|
|
|
m_task_table.Delete<T>(task_id);
|
|
|
|
m_task_id_free_list.Free(task_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Set the task arguments. */
|
|
|
|
R_TRY(task->SetArguments(args...));
|
|
|
|
|
|
|
|
/* Clear the task's events. */
|
|
|
|
os::ClearEvent(std::addressof(m_receive_buffer_available_events[task_id]));
|
|
|
|
os::ClearEvent(std::addressof(m_send_buffer_available_events[task_id]));
|
|
|
|
|
|
|
|
/* Add the task to our queue if we can, or cancel it. */
|
|
|
|
if (m_thread_running) {
|
|
|
|
m_task_queue.Add(task_id, PacketCategory::Request);
|
|
|
|
} else {
|
|
|
|
task->Cancel(RpcTaskCancelReason::QueueNotAvailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the output task id. */
|
|
|
|
*out_task_id = task_id;
|
|
|
|
|
|
|
|
/* We succeeded. */
|
|
|
|
task_guard.Cancel();
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args> requires IsRpcTaskResultType<T, Args...>
|
|
|
|
Result End(u32 task_id, Args... args) {
|
|
|
|
/* Lock ourselves. */
|
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
|
|
|
|
/* Get the task. */
|
|
|
|
T *task = m_task_table.Get<T>(task_id);
|
|
|
|
R_UNLESS(task != nullptr, htc::ResultInvalidTaskId());
|
|
|
|
|
|
|
|
/* Ensure the task is freed if it needs to be, when we're done. */
|
|
|
|
auto task_guard = SCOPE_GUARD {
|
|
|
|
m_task_active[task_id] = false;
|
|
|
|
m_task_table.Delete<T>(task_id);
|
|
|
|
m_task_id_free_list.Free(task_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* If the task was cancelled, handle that. */
|
|
|
|
if (task->GetTaskState() == RpcTaskState::Cancelled) {
|
|
|
|
switch (task->GetTaskCancelReason()) {
|
|
|
|
case RpcTaskCancelReason::One:
|
|
|
|
task_guard.Cancel();
|
|
|
|
return htc::ResultUnknown2021();
|
|
|
|
case RpcTaskCancelReason::Two:
|
|
|
|
return htc::ResultCancelled();
|
|
|
|
case RpcTaskCancelReason::QueueNotAvailable:
|
|
|
|
return htc::ResultTaskQueueNotAvailable();
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the task's result. */
|
|
|
|
R_TRY(task->GetResult(args...));
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
2021-02-09 14:16:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|