aboutsummaryrefslogtreecommitdiff
path: root/src/core/hle/service/sm/sm.cpp
blob: 3fb87adb35f28f552d5ab61a62fffef49d631818 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <tuple>
#include "common/assert.h"
#include "core/core.h"
#include "core/hle/kernel/client_session.h"
#include "core/hle/result.h"
#include "core/hle/service/sm/sm.h"
#include "core/hle/service/sm/srv.h"

namespace Service::SM {

static Result ValidateServiceName(const std::string& name) {
    R_UNLESS(name.size() > 0 && name.size() <= 8, ResultInvalidNameSize);
    R_UNLESS(name.find('\0') == std::string::npos, ResultNameContainsNul);
    return ResultSuccess;
}

ServiceManager::ServiceManager(Core::System& system) : system(system) {}

void ServiceManager::InstallInterfaces(Core::System& system) {
    ASSERT(system.ServiceManager().srv_interface.expired());

    auto srv = std::make_shared<SRV>(system);
    srv->InstallAsNamedPort(system.Kernel());
    system.ServiceManager().srv_interface = srv;
}

Result ServiceManager::RegisterService(std::shared_ptr<Kernel::ServerPort>* out_server_port,
                                       std::string name, u32 max_sessions) {
    R_TRY(ValidateServiceName(name));
    R_UNLESS(registered_services.find(name) == registered_services.end(), ResultAlreadyRegistered);

    const auto [server_port, client_port] = system.Kernel().CreatePortPair(max_sessions, name);
    registered_services_inverse.emplace(client_port->GetObjectId(), name);
    registered_services.emplace(std::move(name), std::move(client_port));

    *out_server_port = server_port;
    return ResultSuccess;
}

Result ServiceManager::GetServicePort(std::shared_ptr<Kernel::ClientPort>* out_client_port,
                                      const std::string& name) {
    R_TRY(ValidateServiceName(name));

    auto it = registered_services.find(name);
    R_UNLESS(it != registered_services.end(), ResultServiceNotRegistered);

    *out_client_port = it->second;
    return ResultSuccess;
}

Result ServiceManager::ConnectToService(std::shared_ptr<Kernel::ClientSession>* out_client_session,
                                        const std::string& name) {
    std::shared_ptr<Kernel::ClientPort> client_port;
    R_TRY(GetServicePort(std::addressof(client_port), name));
    return client_port->Connect(out_client_session);
}

std::string ServiceManager::GetServiceNameByPortId(u32 port) const {
    if (registered_services_inverse.count(port)) {
        return registered_services_inverse.at(port);
    }

    return "";
}

} // namespace Service::SM