aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Bluetooth/IBluetoothDriver.cs
blob: 8f2642695168008cf29bf58def9e91a7da7ef55b (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
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Bluetooth.BluetoothDriver;
using Ryujinx.HLE.HOS.Services.Settings;
using Ryujinx.Horizon.Common;
using System;

namespace Ryujinx.HLE.HOS.Services.Bluetooth
{
    [Service("btdrv")]
    class IBluetoothDriver : IpcService
    {
#pragma warning disable CS0414, IDE0052 // Remove unread private member
        private string _unknownLowEnergy;
#pragma warning restore CS0414, IDE0052

        public IBluetoothDriver(ServiceCtx context) { }

        [CommandCmif(46)]
        // InitializeBluetoothLe() -> handle<copy>
        public ResultCode InitializeBluetoothLe(ServiceCtx context)
        {
            NxSettings.Settings.TryGetValue("bluetooth_debug!skip_boot", out object debugMode);

            int initializeEventHandle;

            if ((bool)debugMode)
            {
                if (BluetoothEventManager.InitializeBleDebugEventHandle == 0)
                {
                    BluetoothEventManager.InitializeBleDebugEvent = new KEvent(context.Device.System.KernelContext);

                    if (context.Process.HandleTable.GenerateHandle(BluetoothEventManager.InitializeBleDebugEvent.ReadableEvent, out BluetoothEventManager.InitializeBleDebugEventHandle) != Result.Success)
                    {
                        throw new InvalidOperationException("Out of handles!");
                    }
                }

                if (BluetoothEventManager.UnknownBleDebugEventHandle == 0)
                {
                    BluetoothEventManager.UnknownBleDebugEvent = new KEvent(context.Device.System.KernelContext);

                    if (context.Process.HandleTable.GenerateHandle(BluetoothEventManager.UnknownBleDebugEvent.ReadableEvent, out BluetoothEventManager.UnknownBleDebugEventHandle) != Result.Success)
                    {
                        throw new InvalidOperationException("Out of handles!");
                    }
                }

                if (BluetoothEventManager.RegisterBleDebugEventHandle == 0)
                {
                    BluetoothEventManager.RegisterBleDebugEvent = new KEvent(context.Device.System.KernelContext);

                    if (context.Process.HandleTable.GenerateHandle(BluetoothEventManager.RegisterBleDebugEvent.ReadableEvent, out BluetoothEventManager.RegisterBleDebugEventHandle) != Result.Success)
                    {
                        throw new InvalidOperationException("Out of handles!");
                    }
                }

                initializeEventHandle = BluetoothEventManager.InitializeBleDebugEventHandle;
            }
            else
            {
                _unknownLowEnergy = "low_energy";

                if (BluetoothEventManager.InitializeBleEventHandle == 0)
                {
                    BluetoothEventManager.InitializeBleEvent = new KEvent(context.Device.System.KernelContext);

                    if (context.Process.HandleTable.GenerateHandle(BluetoothEventManager.InitializeBleEvent.ReadableEvent, out BluetoothEventManager.InitializeBleEventHandle) != Result.Success)
                    {
                        throw new InvalidOperationException("Out of handles!");
                    }
                }

                if (BluetoothEventManager.UnknownBleEventHandle == 0)
                {
                    BluetoothEventManager.UnknownBleEvent = new KEvent(context.Device.System.KernelContext);

                    if (context.Process.HandleTable.GenerateHandle(BluetoothEventManager.UnknownBleEvent.ReadableEvent, out BluetoothEventManager.UnknownBleEventHandle) != Result.Success)
                    {
                        throw new InvalidOperationException("Out of handles!");
                    }
                }

                if (BluetoothEventManager.RegisterBleEventHandle == 0)
                {
                    BluetoothEventManager.RegisterBleEvent = new KEvent(context.Device.System.KernelContext);

                    if (context.Process.HandleTable.GenerateHandle(BluetoothEventManager.RegisterBleEvent.ReadableEvent, out BluetoothEventManager.RegisterBleEventHandle) != Result.Success)
                    {
                        throw new InvalidOperationException("Out of handles!");
                    }
                }

                initializeEventHandle = BluetoothEventManager.InitializeBleEventHandle;
            }

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

            return ResultCode.Success;
        }
    }
}