Skip to content

Implementing efl services #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ BasedOnStyle: Google
ColumnLimit: 119
IndentWidth: 4
DerivePointerAlignment: false
NamespaceIndentation: Inner
NamespaceIndentation: All
2 changes: 1 addition & 1 deletion libeiffel
33 changes: 33 additions & 0 deletions source/core/plugin_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "plugin_handler.hpp"

#include <eiffel/user.h>

namespace efl::core {

PluginHandler::PluginHandler() {}

PluginHandler::~PluginHandler() {}

ams::Result PluginHandler::GetPluginMeta(SlPluginMeta* out_pluginMeta, PluginName& name) {
auto& activePluginMetas = GetInstance().m_ActivePluginMetas;
auto foundPluginEntry = activePluginMetas.find(name.data());
if (foundPluginEntry != activePluginMetas.end()) {
*out_pluginMeta = foundPluginEntry->second;
} else {
return EFL_U_RESULT_PLUGIN_NOT_ACTIVE;
}
return 0;
}

ams::Result PluginHandler::GetPluginSharedMemInfo(SlPluginSharedMemInfo* out_sharedMemInfo, PluginName& name) {
auto& activePluginSharedMemInfos = GetInstance().m_ActivePluginSharedMemInfos;
auto foundPluginEntry = activePluginSharedMemInfos.find(name.data());
if (foundPluginEntry == activePluginSharedMemInfos.end()) {
return EFL_U_RESULT_SHARED_MEM_NOT_REGISTERED;
}
*out_sharedMemInfo = foundPluginEntry->second;
activePluginSharedMemInfos.erase(foundPluginEntry); // temp: keep it after proper cleanup impl
return 0;
}

} // namespace efl::core
41 changes: 41 additions & 0 deletions source/core/plugin_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <eiffel/sl.h>

#include <map>
#include <stratosphere.hpp>
#include <string>

namespace efl::core {

using PluginName = std::array<char, sizeof(SlPluginName)>;

class PluginHandler {
private:
PluginHandler();
PluginHandler(const PluginHandler&) = delete;
~PluginHandler();
static inline auto& GetInstance() {
static PluginHandler s_Instance;
return s_Instance;
}

std::map<std::string, SlPluginMeta> m_ActivePluginMetas;
std::map<std::string, SlPluginSharedMemInfo> m_ActivePluginSharedMemInfos;

// TODO: need to clean everything when game is closed

public:
static inline void SetPluginMeta(SlPluginMeta& pluginMeta) {
GetInstance().m_ActivePluginMetas[pluginMeta.name] = pluginMeta;
}

static inline void SetPluginSharedMem(SlPluginName name, SlPluginSharedMemInfo& sharedMemInfo) {
GetInstance().m_ActivePluginSharedMemInfos[name] = sharedMemInfo;
}

static ams::Result GetPluginMeta(SlPluginMeta* out_pluginMeta, PluginName& name);
static ams::Result GetPluginSharedMemInfo(SlPluginSharedMemInfo* out_sharedMemInfo, PluginName& name);
};

} // namespace efl::core
9 changes: 5 additions & 4 deletions source/ipc/server.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "server.hpp"

#include "sl_service.hpp"
#include "user_service.hpp"

namespace efl {
namespace ipc {
namespace efl::ipc {

Server::Server() {
R_ABORT_UNLESS(
m_serverManager.RegisterServer<SlService>(SlService::SERVICE_NAME, EIFFEL_SERVICE_MAX_SESSIONS));
R_ABORT_UNLESS(
m_serverManager.RegisterServer<UserService>(UserService::SERVICE_NAME, EIFFEL_SERVICE_MAX_SESSIONS));
}

}; // namespace ipc
}; // namespace efl
}; // namespace efl::ipc
8 changes: 3 additions & 5 deletions source/ipc/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

#include <stratosphere.hpp>

namespace efl {
namespace ipc {
namespace efl::ipc {

class Server {
private:
static constexpr auto NUM_SERVERS = 1;
static constexpr auto NUM_SERVERS = 2;
static constexpr auto EIFFEL_SERVICE_MAX_SESSIONS = 5;

Server();
Expand All @@ -23,5 +22,4 @@ namespace ipc {
static inline void Loop() { GetInstance().m_serverManager.LoopProcess(); }
};

} // namespace ipc
} // namespace efl
} // namespace efl::ipc
23 changes: 18 additions & 5 deletions source/ipc/sl_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@

#include "../util.hpp"

namespace efl {
namespace ipc {
namespace efl::ipc {

SlService::SlService() {}

SlService::~SlService() {}

ams::Result SlService::Log(const ams::sf::InBuffer& module_name, efl::logger::LogLevel level,
const ams::sf::InBuffer& buf) {
LOG("[%s][%s] %s", module_name.GetPointer(), logger::GetLogLevelString(level), buf.GetPointer());
// TODO: impl proper handling of logs
printf("LOG: [%s][%s] %s", module_name.GetPointer(), logger::GetLogLevelString(level),
buf.GetPointer()); // this is just for debug
return 0;
};

}; // namespace ipc
}; // namespace efl
ams::Result SlService::RegisterPlugin(SlPluginMeta meta) {
core::PluginHandler::SetPluginMeta(meta);
LOG("%s", meta.name);
return 0;
};

ams::Result SlService::RegisterSharedMem(EiffelSlRegisterSharedMemIn in, ams::sf::CopyHandle handle) {
auto sharedMemInfo = SlPluginSharedMemInfo{handle.GetValue(), in.size, in.perm};
core::PluginHandler::SetPluginSharedMem(in.name, sharedMemInfo);
LOG("%s - size 0x%lx perm: %x", in.name, in.size, in.perm);
return 0;
};

}; // namespace efl::ipc
16 changes: 11 additions & 5 deletions source/ipc/sl_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,37 @@

#include <stratosphere.hpp>

#include "../core/plugin_handler.hpp"
#include "../logger/types.hpp"

namespace efl {
namespace ipc {
namespace efl::ipc {

class SlService final : public ams::sf::IServiceObject {
protected:
enum class CommandId {
Log = EFL_SL_CMD_LOG,
RegisterPlugin = EFL_SL_CMD_REGISTER_PLUGIN,
RegisterSharedMem = EFL_SL_CMD_REGISTER_SHARED_MEM,
};

public:
explicit SlService();
virtual ~SlService();

private:
ams::Result Log(const ams::sf::InBuffer&, efl::logger::LogLevel, const ams::sf::InBuffer&);
ams::Result Log(const ams::sf::InBuffer& module_name, efl::logger::LogLevel level,
const ams::sf::InBuffer& buf);
ams::Result RegisterPlugin(SlPluginMeta meta);
ams::Result RegisterSharedMem(EiffelSlRegisterSharedMemIn in, ams::sf::CopyHandle handle);

public:
DEFINE_SERVICE_DISPATCH_TABLE{
MAKE_SERVICE_COMMAND_META(Log),
MAKE_SERVICE_COMMAND_META(RegisterPlugin),
MAKE_SERVICE_COMMAND_META(RegisterSharedMem),
};

static constexpr auto SERVICE_NAME = ams::sm::ServiceName::Encode(EIFFEL_SKYLINE_SERVICE_NAME);
};

}; // namespace ipc
}; // namespace efl
}; // namespace efl::ipc
28 changes: 28 additions & 0 deletions source/ipc/user_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "user_service.hpp"

#include "../util.hpp"

namespace efl::ipc {

UserService::UserService() {}

UserService::~UserService() {}

ams::Result UserService::GetPluginMeta(ams::sf::Out<SlPluginMeta> out, core::PluginName name) {
LOG("%s", name.data());
return core::PluginHandler::GetPluginMeta(out.GetPointer(), name);
};

// TODO: use copy handle instead when cleaning up handles is implemented
ams::Result UserService::GetPluginSharedMem(ams::sf::OutMoveHandle out_handle,
ams::sf::Out<SlPluginSharedMemInfo> out, core::PluginName name) {
LOG("%s", name.data());
auto rc = core::PluginHandler::GetPluginSharedMemInfo(out.GetPointer(), name);
if (R_SUCCEEDED(rc)) {
*(out_handle.GetHandlePointer()) = out.GetPointer()->handle;
}
out.GetPointer()->handle = INVALID_HANDLE; // for libeiffel to fill in
return rc;
};

}; // namespace efl::ipc
36 changes: 36 additions & 0 deletions source/ipc/user_service.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <eiffel/user.h>

#include <stratosphere.hpp>

#include "../core/plugin_handler.hpp"

namespace efl::ipc {

class UserService final : public ams::sf::IServiceObject {
protected:
enum class CommandId {
GetPluginMeta = EFL_U_CMD_GET_PLUGIN_META,
GetPluginSharedMem = EFL_U_CMD_GET_PLUGIN_SHARED_MEM,
};

public:
explicit UserService();
virtual ~UserService();

private:
ams::Result GetPluginMeta(ams::sf::Out<SlPluginMeta> out, core::PluginName name);
ams::Result GetPluginSharedMem(ams::sf::OutMoveHandle out_handle, ams::sf::Out<SlPluginSharedMemInfo> out,
core::PluginName name);

public:
DEFINE_SERVICE_DISPATCH_TABLE{
MAKE_SERVICE_COMMAND_META(GetPluginMeta),
MAKE_SERVICE_COMMAND_META(GetPluginSharedMem),
};

static constexpr auto SERVICE_NAME = ams::sm::ServiceName::Encode(EIFFEL_USER_SERVICE_NAME);
};

}; // namespace efl::ipc