2021-04-09 08:06:03 +01: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 <vapours.hpp>
|
|
|
|
#include <stratosphere/tipc/tipc_common.hpp>
|
|
|
|
#include <stratosphere/tipc/tipc_service_object.hpp>
|
2021-10-01 03:00:47 +01:00
|
|
|
#include <stratosphere/tipc/tipc_object_holder.hpp>
|
2021-04-09 08:06:03 +01:00
|
|
|
|
|
|
|
namespace ams::tipc {
|
|
|
|
|
|
|
|
/* TODO: Put this in a better header. */
|
|
|
|
constexpr inline u16 MethodId_Invalid = 0x0;
|
|
|
|
constexpr inline u16 MethodId_CloseSession = 0xF;
|
|
|
|
|
|
|
|
class ObjectManagerBase {
|
2021-04-27 04:05:56 +01:00
|
|
|
protected:
|
2021-04-09 08:06:03 +01:00
|
|
|
struct Entry {
|
2021-10-01 03:00:47 +01:00
|
|
|
util::TypedStorage<ObjectHolder> object;
|
|
|
|
os::MultiWaitHolderType multi_wait_holder;
|
2021-04-09 08:06:03 +01:00
|
|
|
};
|
|
|
|
private:
|
|
|
|
os::SdkMutex m_mutex{};
|
|
|
|
Entry *m_entries_start{};
|
|
|
|
Entry *m_entries_end{};
|
2021-10-01 03:00:47 +01:00
|
|
|
os::MultiWaitType *m_multi_wait{};
|
2021-04-09 08:06:03 +01:00
|
|
|
private:
|
|
|
|
Entry *FindEntry(svc::Handle handle) {
|
|
|
|
for (Entry *cur = m_entries_start; cur != m_entries_end; ++cur) {
|
|
|
|
if (GetReference(cur->object).GetHandle() == handle) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-01 03:00:47 +01:00
|
|
|
Entry *FindEntry(os::MultiWaitHolderType *holder) {
|
2021-04-09 08:06:03 +01:00
|
|
|
for (Entry *cur = m_entries_start; cur != m_entries_end; ++cur) {
|
2021-10-01 03:00:47 +01:00
|
|
|
if (std::addressof(cur->multi_wait_holder) == holder) {
|
2021-04-09 08:06:03 +01:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
constexpr ObjectManagerBase() = default;
|
|
|
|
|
2021-10-01 03:00:47 +01:00
|
|
|
void InitializeImpl(os::MultiWaitType *multi_wait, Entry *entries, size_t max_objects) {
|
|
|
|
/* Set our multi wait. */
|
|
|
|
m_multi_wait = multi_wait;
|
2021-04-09 08:06:03 +01:00
|
|
|
|
|
|
|
/* Setup entry pointers. */
|
|
|
|
m_entries_start = entries;
|
|
|
|
m_entries_end = entries + max_objects;
|
|
|
|
|
|
|
|
/* Construct all entries. */
|
|
|
|
for (size_t i = 0; i < max_objects; ++i) {
|
|
|
|
util::ConstructAt(m_entries_start[i].object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 03:00:47 +01:00
|
|
|
void AddObject(ObjectHolder &object) {
|
2021-04-09 08:06:03 +01:00
|
|
|
/* Lock ourselves. */
|
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
|
|
|
|
/* Find an empty entry. */
|
|
|
|
auto *entry = this->FindEntry(svc::InvalidHandle);
|
|
|
|
AMS_ABORT_UNLESS(entry != nullptr);
|
|
|
|
|
|
|
|
/* Set the entry's object. */
|
|
|
|
GetReference(entry->object) = object;
|
|
|
|
|
|
|
|
/* Setup the entry's holder. */
|
2021-10-01 03:00:47 +01:00
|
|
|
os::InitializeMultiWaitHolder(std::addressof(entry->multi_wait_holder), object.GetHandle());
|
|
|
|
os::LinkMultiWaitHolder(m_multi_wait, std::addressof(entry->multi_wait_holder));
|
2021-04-09 08:06:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseObject(svc::Handle handle) {
|
|
|
|
/* Lock ourselves. */
|
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
|
|
|
|
/* Find the matching entry. */
|
|
|
|
auto *entry = this->FindEntry(handle);
|
|
|
|
AMS_ABORT_UNLESS(entry != nullptr);
|
|
|
|
|
|
|
|
/* Finalize the entry's holder. */
|
2021-10-01 03:00:47 +01:00
|
|
|
os::UnlinkMultiWaitHolder(std::addressof(entry->multi_wait_holder));
|
|
|
|
os::FinalizeMultiWaitHolder(std::addressof(entry->multi_wait_holder));
|
2021-04-09 08:06:03 +01:00
|
|
|
|
|
|
|
/* Destroy the object. */
|
|
|
|
GetReference(entry->object).Destroy();
|
|
|
|
}
|
|
|
|
|
2021-10-01 03:00:47 +01:00
|
|
|
Result ReplyAndReceive(os::MultiWaitHolderType **out_holder, ObjectHolder *out_object, svc::Handle reply_target, os::MultiWaitType *multi_wait) {
|
2021-04-09 08:06:03 +01:00
|
|
|
/* Declare signaled holder for processing ahead of time. */
|
2021-10-01 03:00:47 +01:00
|
|
|
os::MultiWaitHolderType *signaled_holder;
|
2021-04-09 08:06:03 +01:00
|
|
|
|
|
|
|
/* Reply and receive until we get a newly signaled target. */
|
2021-10-01 03:00:47 +01:00
|
|
|
Result result = os::SdkReplyAndReceive(out_holder, reply_target, multi_wait);
|
2021-04-09 08:06:03 +01:00
|
|
|
for (signaled_holder = *out_holder; signaled_holder == nullptr; signaled_holder = *out_holder) {
|
2021-10-01 03:00:47 +01:00
|
|
|
result = os::SdkReplyAndReceive(out_holder, svc::InvalidHandle, multi_wait);
|
2021-04-09 08:06:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the entry matching the signaled holder. */
|
|
|
|
if (auto *entry = this->FindEntry(signaled_holder); entry != nullptr) {
|
|
|
|
/* Get the output object. */
|
|
|
|
*out_object = GetReference(entry->object);
|
|
|
|
*out_holder = nullptr;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Reply(svc::Handle reply_target) {
|
|
|
|
/* Perform the reply. */
|
|
|
|
s32 dummy;
|
|
|
|
R_TRY_CATCH(svc::ReplyAndReceive(std::addressof(dummy), nullptr, 0, reply_target, 0)) {
|
|
|
|
R_CATCH(svc::ResultTimedOut) {
|
|
|
|
/* Timing out is acceptable. */
|
|
|
|
}
|
|
|
|
R_CATCH(svc::ResultSessionClosed) {
|
|
|
|
/* It's okay if we couldn't reply to a closed session. */
|
|
|
|
}
|
|
|
|
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2021-10-01 03:00:47 +01:00
|
|
|
Result ProcessRequest(ObjectHolder &object) {
|
2021-04-09 08:06:03 +01:00
|
|
|
/* Get the method id. */
|
|
|
|
const auto method_id = svc::ipc::MessageBuffer::MessageHeader(svc::ipc::MessageBuffer(svc::ipc::GetMessageBuffer())).GetTag();
|
|
|
|
|
|
|
|
/* Check that the method id is valid. */
|
|
|
|
R_UNLESS(method_id != MethodId_Invalid, tipc::ResultInvalidMethod());
|
|
|
|
|
|
|
|
/* If we're closing the object, do so. */
|
|
|
|
if (method_id == MethodId_CloseSession) {
|
|
|
|
const auto handle = object.GetHandle();
|
|
|
|
|
|
|
|
/* Close the object itself. */
|
|
|
|
this->CloseObject(handle);
|
|
|
|
|
|
|
|
/* Close the object's handle. */
|
|
|
|
/* NOTE: Nintendo does not check that this succeeds. */
|
|
|
|
R_ABORT_UNLESS(svc::CloseHandle(handle));
|
|
|
|
|
|
|
|
/* Return result to signify we closed the object. */
|
|
|
|
return tipc::ResultSessionClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the generic method for the object. */
|
|
|
|
R_TRY(object.GetObject()->ProcessRequest());
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t MaxObjects>
|
|
|
|
class ObjectManager : public ObjectManagerBase {
|
|
|
|
private:
|
|
|
|
Entry m_entries_storage[MaxObjects]{};
|
|
|
|
public:
|
|
|
|
constexpr ObjectManager() = default;
|
|
|
|
|
2021-10-01 03:00:47 +01:00
|
|
|
void Initialize(os::MultiWaitType *multi_wait) {
|
|
|
|
this->InitializeImpl(multi_wait, m_entries_storage, MaxObjects);
|
2021-04-09 08:06:03 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|