diff --git a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp index 4f9f4ea56..14a165feb 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.cpp @@ -15,7 +15,6 @@ */ #include #include "uart_mitm_service.hpp" -#include "../amsmitm_debug.hpp" #include "../amsmitm_fs_utils.hpp" /* TODO: This should really use async fs-writing, there's a slowdown with bluetooth communications with current fs-writing. */ @@ -358,13 +357,13 @@ namespace ams::mitm::uart { return rc; } - Result UartMitmService::CreatePortSession(sf::Out> out) { + Result UartMitmService::CreatePortSession(sf::Out> out) { /* Open a port interface. */ UartPortSession port; R_TRY(uartCreatePortSessionFwd(this->forward_service.get(), &port)); const sf::cmif::DomainObjectId target_object_id{serviceGetObjectId(&port.s)}; - out.SetValue(sf::MakeShared(this->client_info, std::make_unique(port)), target_object_id); + out.SetValue(sf::CreateSharedObjectEmplaced(this->client_info, std::make_unique(port)), target_object_id); return ResultSuccess(); } diff --git a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp index 53e7a0885..0b0b4471b 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uart_mitm_service.hpp @@ -18,29 +18,25 @@ #include "uart_shim.h" +#define AMS_UART_IPORTSESSION_MITM_INTERFACE_INFO(C, H) \ + AMS_SF_METHOD_INFO(C, H, 0, Result, OpenPort, (sf::Out out, u32 port, u32 baud_rate, UartFlowControlMode flow_control_mode, u32 device_variation, bool is_invert_tx, bool is_invert_rx, bool is_invert_rts, bool is_invert_cts, sf::CopyHandle send_handle, sf::CopyHandle receive_handle, u64 send_buffer_length, u64 receive_buffer_length), (out, port, baud_rate, flow_control_mode, device_variation, is_invert_tx, is_invert_rx, is_invert_rts, is_invert_cts, send_handle, receive_handle, send_buffer_length, receive_buffer_length)) \ + AMS_SF_METHOD_INFO(C, H, 1, Result, OpenPortForDev, (sf::Out out, u32 port, u32 baud_rate, UartFlowControlMode flow_control_mode, u32 device_variation, bool is_invert_tx, bool is_invert_rx, bool is_invert_rts, bool is_invert_cts, sf::CopyHandle send_handle, sf::CopyHandle receive_handle, u64 send_buffer_length, u64 receive_buffer_length), (out, port, baud_rate, flow_control_mode, device_variation, is_invert_tx, is_invert_rx, is_invert_rts, is_invert_cts, send_handle, receive_handle, send_buffer_length, receive_buffer_length)) \ + AMS_SF_METHOD_INFO(C, H, 2, Result, GetWritableLength, (sf::Out out), (out)) \ + AMS_SF_METHOD_INFO(C, H, 3, Result, Send, (sf::Out out_size, const sf::InAutoSelectBuffer &data), (out_size, data)) \ + AMS_SF_METHOD_INFO(C, H, 4, Result, GetReadableLength, (sf::Out out), (out)) \ + AMS_SF_METHOD_INFO(C, H, 5, Result, Receive, (sf::Out out_size, const sf::OutAutoSelectBuffer &data), (out_size, data)) \ + AMS_SF_METHOD_INFO(C, H, 6, Result, BindPortEvent, (sf::Out out, sf::OutCopyHandle out_event_handle, UartPortEventType port_event_type, s64 threshold), (out, out_event_handle, port_event_type, threshold)) \ + AMS_SF_METHOD_INFO(C, H, 7, Result, UnbindPortEvent, (sf::Out out, UartPortEventType port_event_type), (out, port_event_type)) + +AMS_SF_DEFINE_INTERFACE(ams::mitm::uart::impl, IPortSession, AMS_UART_IPORTSESSION_MITM_INTERFACE_INFO) + +#define AMS_UART_MITM_INTERFACE_INFO(C, H) \ + AMS_SF_METHOD_INFO(C, H, 6, Result, CreatePortSession, (sf::Out> out), (out)) + +AMS_SF_DEFINE_MITM_INTERFACE(ams::mitm::uart::impl, IUartMitmInterface, AMS_UART_MITM_INTERFACE_INFO) + namespace ams::mitm::uart { - namespace impl { - - #define AMS_UART_IPORTSESSION_MITM_INTERFACE_INFO(C, H) \ - AMS_SF_METHOD_INFO(C, H, 0, Result, OpenPort, (sf::Out out, u32 port, u32 baud_rate, UartFlowControlMode flow_control_mode, u32 device_variation, bool is_invert_tx, bool is_invert_rx, bool is_invert_rts, bool is_invert_cts, sf::CopyHandle send_handle, sf::CopyHandle receive_handle, u64 send_buffer_length, u64 receive_buffer_length)) \ - AMS_SF_METHOD_INFO(C, H, 1, Result, OpenPortForDev, (sf::Out out, u32 port, u32 baud_rate, UartFlowControlMode flow_control_mode, u32 device_variation, bool is_invert_tx, bool is_invert_rx, bool is_invert_rts, bool is_invert_cts, sf::CopyHandle send_handle, sf::CopyHandle receive_handle, u64 send_buffer_length, u64 receive_buffer_length)) \ - AMS_SF_METHOD_INFO(C, H, 2, Result, GetWritableLength, (sf::Out out)) \ - AMS_SF_METHOD_INFO(C, H, 3, Result, Send, (sf::Out out_size, const sf::InAutoSelectBuffer &data)) \ - AMS_SF_METHOD_INFO(C, H, 4, Result, GetReadableLength, (sf::Out out)) \ - AMS_SF_METHOD_INFO(C, H, 5, Result, Receive, (sf::Out out_size, const sf::OutAutoSelectBuffer &data)) \ - AMS_SF_METHOD_INFO(C, H, 6, Result, BindPortEvent, (sf::Out out, sf::OutCopyHandle out_event_handle, UartPortEventType port_event_type, s64 threshold)) \ - AMS_SF_METHOD_INFO(C, H, 7, Result, UnbindPortEvent, (sf::Out out, UartPortEventType port_event_type)) \ - - AMS_SF_DEFINE_INTERFACE(IPortSession, AMS_UART_IPORTSESSION_MITM_INTERFACE_INFO) - - #define AMS_UART_MITM_INTERFACE_INFO(C, H) \ - AMS_SF_METHOD_INFO(C, H, 6, Result, CreatePortSession, (sf::Out> out)) - - AMS_SF_DEFINE_MITM_INTERFACE(IUartMitmInterface, AMS_UART_MITM_INTERFACE_INFO) - - } - class UartPortService { private: sm::MitmProcessInfo client_info; @@ -101,7 +97,7 @@ namespace ams::mitm::uart { return client_info.program_id == ncm::SystemProgramId::Bluetooth; } public: - Result CreatePortSession(sf::Out> out); + Result CreatePortSession(sf::Out> out); }; static_assert(impl::IsIUartMitmInterface); diff --git a/stratosphere/ams_mitm/source/uart_mitm/uartmitm_module.cpp b/stratosphere/ams_mitm/source/uart_mitm/uartmitm_module.cpp index 297e91bbc..dfe75342f 100644 --- a/stratosphere/ams_mitm/source/uart_mitm/uartmitm_module.cpp +++ b/stratosphere/ams_mitm/source/uart_mitm/uartmitm_module.cpp @@ -22,6 +22,11 @@ namespace ams::mitm::uart { namespace { + enum PortIndex { + PortIndex_Mitm, + PortIndex_Count, + }; + constexpr sm::ServiceName UartMitmServiceName = sm::ServiceName::Encode("uart"); struct ServerOptions { @@ -33,7 +38,25 @@ namespace ams::mitm::uart { constexpr size_t MaxServers = 1; constexpr size_t MaxSessions = 10; - sf::hipc::ServerManager g_server_manager; + class ServerManager final : public sf::hipc::ServerManager { + private: + virtual Result OnNeedsToAccept(int port_index, Server *server) override; + }; + + ServerManager g_server_manager; + + Result ServerManager::OnNeedsToAccept(int port_index, Server *server) { + /* Acknowledge the mitm session. */ + std::shared_ptr<::Service> fsrv; + sm::MitmProcessInfo client_info; + server->AcknowledgeMitmSession(std::addressof(fsrv), std::addressof(client_info)); + + switch (port_index) { + case PortIndex_Mitm: + return this->AcceptMitmImpl(server, sf::CreateSharedObjectEmplaced(decltype(fsrv)(fsrv), client_info), fsrv); + AMS_UNREACHABLE_DEFAULT_CASE(); + } + } bool ShouldMitmUart() { u8 en = 0; @@ -59,7 +82,7 @@ namespace ams::mitm::uart { } /* Create mitm servers. */ - R_ABORT_UNLESS((g_server_manager.RegisterMitmServer(UartMitmServiceName))); + R_ABORT_UNLESS((g_server_manager.RegisterMitmServer(PortIndex_Mitm, UartMitmServiceName))); /* Loop forever, servicing our services. */ g_server_manager.LoopProcess();