2021-02-11 07:52:12 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-02-11 07:52:12 +01:00
|
|
|
*
|
|
|
|
* 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 "htclow_channel.hpp"
|
|
|
|
|
|
|
|
namespace ams::htclow {
|
|
|
|
|
|
|
|
Result Channel::Open(const Module *module, ChannelId id) {
|
|
|
|
/* Check we're not already initialized. */
|
|
|
|
AMS_ASSERT(!module->GetBase()->_is_initialized);
|
|
|
|
|
|
|
|
/* Set our channel type. */
|
|
|
|
m_channel = {
|
|
|
|
._is_initialized = true,
|
|
|
|
._module_id = module->GetBase()->_id,
|
|
|
|
._channel_id = id,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Open the channel. */
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(m_manager->Open(impl::ConvertChannelType(m_channel)));
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::Close() {
|
|
|
|
m_manager->Close(impl::ConvertChannelType(m_channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelState Channel::GetChannelState() {
|
|
|
|
return m_manager->GetChannelState(impl::ConvertChannelType(m_channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
os::EventType *Channel::GetChannelStateEvent() {
|
|
|
|
return m_manager->GetChannelStateEvent(impl::ConvertChannelType(m_channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::Connect() {
|
|
|
|
const auto channel = impl::ConvertChannelType(m_channel);
|
|
|
|
|
|
|
|
/* Begin the flush. */
|
2022-03-06 21:08:20 +01:00
|
|
|
u32 task_id{};
|
2021-02-11 07:52:12 +01:00
|
|
|
R_TRY(m_manager->ConnectBegin(std::addressof(task_id), channel));
|
|
|
|
|
|
|
|
/* Wait for the task to finish. */
|
|
|
|
this->WaitEvent(m_manager->GetTaskEvent(task_id), false);
|
|
|
|
|
|
|
|
/* End the flush. */
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(m_manager->ConnectEnd(channel, task_id));
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::Flush() {
|
|
|
|
/* Begin the flush. */
|
2022-03-06 21:08:20 +01:00
|
|
|
u32 task_id{};
|
2021-02-11 07:52:12 +01:00
|
|
|
R_TRY(m_manager->FlushBegin(std::addressof(task_id), impl::ConvertChannelType(m_channel)));
|
|
|
|
|
|
|
|
/* Wait for the task to finish. */
|
|
|
|
this->WaitEvent(m_manager->GetTaskEvent(task_id), true);
|
|
|
|
|
|
|
|
/* End the flush. */
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(m_manager->FlushEnd(task_id));
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::Shutdown() {
|
|
|
|
m_manager->Shutdown(impl::ConvertChannelType(m_channel));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::Receive(s64 *out, void *dst, s64 size, ReceiveOption option) {
|
|
|
|
/* Check pre-conditions. */
|
|
|
|
AMS_ABORT_UNLESS(util::IsIntValueRepresentable<size_t>(size));
|
|
|
|
|
|
|
|
/* Determine the minimum allowable receive size. */
|
|
|
|
s64 min_size;
|
|
|
|
switch (option) {
|
|
|
|
case htclow::ReceiveOption_NonBlocking: min_size = 0; break;
|
|
|
|
case htclow::ReceiveOption_ReceiveAnyData: min_size = 1; break;
|
|
|
|
case htclow::ReceiveOption_ReceiveAllData: min_size = size; break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Repeatedly receive. */
|
|
|
|
s64 received = 0;
|
|
|
|
do {
|
|
|
|
s64 cur_received;
|
|
|
|
const Result result = this->ReceiveInternal(std::addressof(cur_received), static_cast<u8 *>(dst) + received, size - received, option);
|
|
|
|
|
|
|
|
if (R_FAILED(result)) {
|
|
|
|
if (htclow::ResultChannelReceiveBufferEmpty::Includes(result)) {
|
|
|
|
R_UNLESS(option != htclow::ReceiveOption_NonBlocking, htclow::ResultNonBlockingReceiveFailed());
|
|
|
|
}
|
|
|
|
if (htclow::ResultChannelNotExist::Includes(result)) {
|
|
|
|
*out = received;
|
|
|
|
}
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(result);
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
received += static_cast<size_t>(cur_received);
|
|
|
|
} while (received < min_size);
|
|
|
|
|
|
|
|
/* Set output size. */
|
|
|
|
AMS_ASSERT(received <= size);
|
|
|
|
*out = received;
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
2021-02-13 07:07:34 +01:00
|
|
|
Result Channel::Send(s64 *out, const void *src, s64 size) {
|
2021-02-11 07:52:12 +01:00
|
|
|
/* Check pre-conditions. */
|
|
|
|
AMS_ASSERT(util::IsIntValueRepresentable<size_t>(size));
|
|
|
|
|
|
|
|
/* Convert channel. */
|
|
|
|
const auto channel = impl::ConvertChannelType(m_channel);
|
|
|
|
|
|
|
|
/* Loop sending. */
|
|
|
|
s64 total_sent;
|
|
|
|
size_t cur_sent;
|
|
|
|
for (total_sent = 0; total_sent < size; total_sent += cur_sent) {
|
|
|
|
AMS_ASSERT(util::IsIntValueRepresentable<size_t>(size - total_sent));
|
|
|
|
|
|
|
|
/* Begin the send. */
|
2022-03-06 21:08:20 +01:00
|
|
|
u32 task_id{};
|
2021-02-11 07:52:12 +01:00
|
|
|
const auto begin_result = m_manager->SendBegin(std::addressof(task_id), std::addressof(cur_sent), static_cast<const u8 *>(src) + total_sent, size - total_sent, channel);
|
|
|
|
if (R_FAILED(begin_result)) {
|
|
|
|
if (total_sent != 0) {
|
|
|
|
break;
|
|
|
|
} else {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(begin_result);
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the task to finish. */
|
|
|
|
this->WaitEvent(m_manager->GetTaskEvent(task_id), true);
|
|
|
|
|
|
|
|
/* Finish the send. */
|
|
|
|
R_ABORT_UNLESS(m_manager->SendEnd(task_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set output size. */
|
|
|
|
AMS_ASSERT(total_sent <= size);
|
|
|
|
*out = total_sent;
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::SetConfig(const ChannelConfig &config) {
|
|
|
|
m_manager->SetConfig(impl::ConvertChannelType(m_channel), config);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::SetReceiveBuffer(void *buf, size_t size) {
|
|
|
|
m_manager->SetReceiveBuffer(impl::ConvertChannelType(m_channel), buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::SetSendBuffer(void *buf, size_t size) {
|
|
|
|
m_manager->SetSendBuffer(impl::ConvertChannelType(m_channel), buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::SetSendBufferWithData(const void *buf, size_t size) {
|
|
|
|
m_manager->SetSendBufferWithData(impl::ConvertChannelType(m_channel), buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::WaitReceive(s64 size) {
|
|
|
|
/* Check pre-conditions. */
|
|
|
|
AMS_ASSERT(util::IsIntValueRepresentable<size_t>(size));
|
|
|
|
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(this->WaitReceiveInternal(size, nullptr));
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::WaitReceive(s64 size, os::EventType *event) {
|
|
|
|
/* Check pre-conditions. */
|
|
|
|
AMS_ASSERT(util::IsIntValueRepresentable<size_t>(size));
|
|
|
|
AMS_ASSERT(event != nullptr);
|
|
|
|
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(this->WaitReceiveInternal(size, event));
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::WaitEvent(os::EventType *event, bool) {
|
|
|
|
return os::WaitEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::ReceiveInternal(s64 *out, void *dst, s64 size, ReceiveOption option) {
|
|
|
|
const auto channel = impl::ConvertChannelType(m_channel);
|
|
|
|
const bool blocking = option != ReceiveOption_NonBlocking;
|
|
|
|
|
|
|
|
/* Begin the receive. */
|
2022-03-06 21:08:20 +01:00
|
|
|
u32 task_id{};
|
2021-02-11 07:52:12 +01:00
|
|
|
R_TRY(m_manager->ReceiveBegin(std::addressof(task_id), channel, blocking ? 1 : 0));
|
|
|
|
|
|
|
|
/* Wait for the task to finish. */
|
|
|
|
this->WaitEvent(m_manager->GetTaskEvent(task_id), false);
|
|
|
|
|
|
|
|
/* Receive the data. */
|
|
|
|
size_t received;
|
|
|
|
AMS_ASSERT(util::IsIntValueRepresentable<size_t>(size));
|
|
|
|
R_TRY(m_manager->ReceiveEnd(std::addressof(received), dst, static_cast<size_t>(size), channel, task_id));
|
|
|
|
|
|
|
|
/* Set the output size. */
|
|
|
|
AMS_ASSERT(util::IsIntValueRepresentable<s64>(received));
|
|
|
|
*out = static_cast<s64>(received);
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Channel::WaitReceiveInternal(s64 size, os::EventType *event) {
|
|
|
|
const auto channel = impl::ConvertChannelType(m_channel);
|
|
|
|
|
|
|
|
/* Begin the wait. */
|
2022-03-06 21:08:20 +01:00
|
|
|
u32 task_id{};
|
2021-02-11 07:52:12 +01:00
|
|
|
R_TRY(m_manager->WaitReceiveBegin(std::addressof(task_id), channel, size));
|
|
|
|
|
|
|
|
|
|
|
|
/* Perform the wait. */
|
|
|
|
if (event != nullptr) {
|
|
|
|
if (os::WaitAny(event, m_manager->GetTaskEvent(task_id)) == 0) {
|
|
|
|
m_manager->WaitReceiveEnd(task_id);
|
2022-03-26 08:14:36 +01:00
|
|
|
R_THROW(htclow::ResultChannelWaitCancelled());
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->WaitEvent(m_manager->GetTaskEvent(task_id), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End the wait. */
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(m_manager->WaitReceiveEnd(task_id));
|
2021-02-11 07:52:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|