aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/BluetoothManager/BtmUser/IBtmUserCore.cs
blob: d4e23b93d9a2cc5d2b464814f32a0618714094e1 (plain)
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
126
127
128
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.Horizon.Common;

namespace Ryujinx.HLE.HOS.Services.BluetoothManager.BtmUser
{
    class IBtmUserCore : IpcService
    {
        public KEvent _bleScanEvent;
        public int _bleScanEventHandle;

        public KEvent _bleConnectionEvent;
        public int _bleConnectionEventHandle;

        public KEvent _bleServiceDiscoveryEvent;
        public int _bleServiceDiscoveryEventHandle;

        public KEvent _bleMtuConfigEvent;
        public int _bleMtuConfigEventHandle;

        public IBtmUserCore() { }

        [CommandCmif(0)] // 5.0.0+
        // AcquireBleScanEvent() -> (byte<1>, handle<copy>)
        public ResultCode AcquireBleScanEvent(ServiceCtx context)
        {
            Result result = Result.Success;

            if (_bleScanEventHandle == 0)
            {
                _bleScanEvent = new KEvent(context.Device.System.KernelContext);

                result = context.Process.HandleTable.GenerateHandle(_bleScanEvent.ReadableEvent, out _bleScanEventHandle);

                if (result != Result.Success)
                {
                    // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
                    Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
                }
            }

            context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleScanEventHandle);

            context.ResponseData.Write(result == Result.Success ? 1 : 0);

            return ResultCode.Success;
        }

        [CommandCmif(17)] // 5.0.0+
        // AcquireBleConnectionEvent() -> (byte<1>, handle<copy>)
        public ResultCode AcquireBleConnectionEvent(ServiceCtx context)
        {
            Result result = Result.Success;

            if (_bleConnectionEventHandle == 0)
            {
                _bleConnectionEvent = new KEvent(context.Device.System.KernelContext);

                result = context.Process.HandleTable.GenerateHandle(_bleConnectionEvent.ReadableEvent, out _bleConnectionEventHandle);

                if (result != Result.Success)
                {
                    // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
                    Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
                }
            }

            context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleConnectionEventHandle);

            context.ResponseData.Write(result == Result.Success ? 1 : 0);

            return ResultCode.Success;
        }

        [CommandCmif(26)] // 5.0.0+
        // AcquireBleServiceDiscoveryEvent() -> (byte<1>, handle<copy>)
        public ResultCode AcquireBleServiceDiscoveryEvent(ServiceCtx context)
        {
            Result result = Result.Success;

            if (_bleServiceDiscoveryEventHandle == 0)
            {
                _bleServiceDiscoveryEvent = new KEvent(context.Device.System.KernelContext);

                result = context.Process.HandleTable.GenerateHandle(_bleServiceDiscoveryEvent.ReadableEvent, out _bleServiceDiscoveryEventHandle);

                if (result != Result.Success)
                {
                    // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
                    Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
                }
            }

            context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleServiceDiscoveryEventHandle);

            context.ResponseData.Write(result == Result.Success ? 1 : 0);

            return ResultCode.Success;
        }

        [CommandCmif(33)] // 5.0.0+
        // AcquireBleMtuConfigEvent() -> (byte<1>, handle<copy>)
        public ResultCode AcquireBleMtuConfigEvent(ServiceCtx context)
        {
            Result result = Result.Success;

            if (_bleMtuConfigEventHandle == 0)
            {
                _bleMtuConfigEvent = new KEvent(context.Device.System.KernelContext);

                result = context.Process.HandleTable.GenerateHandle(_bleMtuConfigEvent.ReadableEvent, out _bleMtuConfigEventHandle);

                if (result != Result.Success)
                {
                    // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
                    Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
                }
            }

            context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleMtuConfigEventHandle);

            context.ResponseData.Write(result == Result.Success ? 1 : 0);

            return ResultCode.Success;
        }
    }
}