aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon/Sdk/Sm/SmApi.cs
blob: a63a03a5c84cfaaac28f9a6486708764a7069c81 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Ryujinx.Common.Memory;
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Sf.Cmif;
using System;

namespace Ryujinx.Horizon.Sdk.Sm
{
    public class SmApi : IDisposable
    {
        private const string SmName = "sm:";

        private int _portHandle;

        public Result Initialize()
        {
            Result result = HorizonStatic.Syscall.ConnectToNamedPort(out int portHandle, SmName);

            while (result == KernelResult.NotFound)
            {
                HorizonStatic.Syscall.SleepThread(50000000L);
                result = HorizonStatic.Syscall.ConnectToNamedPort(out portHandle, SmName);
            }

            if (result.IsFailure)
            {
                return result;
            }

            _portHandle = portHandle;

            return RegisterClient();
        }

        private Result RegisterClient()
        {
            Span<byte> data = stackalloc byte[8];

            SpanWriter writer = new(data);

            writer.Write(0UL);

            return ServiceUtil.SendRequest(out _, _portHandle, 0, sendPid: true, data);
        }

        public Result GetServiceHandle(out int handle, ServiceName name)
        {
            Span<byte> data = stackalloc byte[8];

            SpanWriter writer = new(data);

            writer.Write(name);

            Result result = ServiceUtil.SendRequest(out CmifResponse response, _portHandle, 1, sendPid: false, data);

            if (result.IsFailure)
            {
                handle = 0;

                return result;
            }

            handle = response.MoveHandles[0];

            return Result.Success;
        }

        public Result RegisterService(out int handle, ServiceName name, int maxSessions, bool isLight)
        {
            Span<byte> data = stackalloc byte[16];

            SpanWriter writer = new(data);

            writer.Write(name);
            writer.Write(isLight ? 1 : 0);
            writer.Write(maxSessions);

            Result result = ServiceUtil.SendRequest(out CmifResponse response, _portHandle, 2, sendPid: false, data);

            if (result.IsFailure)
            {
                handle = 0;

                return result;
            }

            handle = response.MoveHandles[0];

            return Result.Success;
        }

        public Result UnregisterService(ServiceName name)
        {
            Span<byte> data = stackalloc byte[8];

            SpanWriter writer = new(data);

            writer.Write(name);

            return ServiceUtil.SendRequest(out _, _portHandle, 3, sendPid: false, data);
        }

        public Result DetachClient()
        {
            Span<byte> data = stackalloc byte[8];

            SpanWriter writer = new(data);

            writer.Write(0UL);

            return ServiceUtil.SendRequest(out _, _portHandle, 4, sendPid: true, data);
        }

        public void Dispose()
        {
            if (_portHandle != 0)
            {
                HorizonStatic.Syscall.CloseHandle(_portHandle);

                _portHandle = 0;
            }

            GC.SuppressFinalize(this);
        }
    }
}