/ src / Ryujinx.HLE / HOS / Services / BluetoothManager / BtmUser / IBtmUserCore.cs
IBtmUserCore.cs
  1  using Ryujinx.Common.Logging;
  2  using Ryujinx.HLE.HOS.Ipc;
  3  using Ryujinx.HLE.HOS.Kernel.Threading;
  4  using Ryujinx.Horizon.Common;
  5  
  6  namespace Ryujinx.HLE.HOS.Services.BluetoothManager.BtmUser
  7  {
  8      class IBtmUserCore : IpcService
  9      {
 10          public KEvent _bleScanEvent;
 11          public int _bleScanEventHandle;
 12  
 13          public KEvent _bleConnectionEvent;
 14          public int _bleConnectionEventHandle;
 15  
 16          public KEvent _bleServiceDiscoveryEvent;
 17          public int _bleServiceDiscoveryEventHandle;
 18  
 19          public KEvent _bleMtuConfigEvent;
 20          public int _bleMtuConfigEventHandle;
 21  
 22          public IBtmUserCore() { }
 23  
 24          [CommandCmif(0)] // 5.0.0+
 25          // AcquireBleScanEvent() -> (byte<1>, handle<copy>)
 26          public ResultCode AcquireBleScanEvent(ServiceCtx context)
 27          {
 28              Result result = Result.Success;
 29  
 30              if (_bleScanEventHandle == 0)
 31              {
 32                  _bleScanEvent = new KEvent(context.Device.System.KernelContext);
 33  
 34                  result = context.Process.HandleTable.GenerateHandle(_bleScanEvent.ReadableEvent, out _bleScanEventHandle);
 35  
 36                  if (result != Result.Success)
 37                  {
 38                      // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
 39                      Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
 40                  }
 41              }
 42  
 43              context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleScanEventHandle);
 44  
 45              context.ResponseData.Write(result == Result.Success ? 1 : 0);
 46  
 47              return ResultCode.Success;
 48          }
 49  
 50          [CommandCmif(17)] // 5.0.0+
 51          // AcquireBleConnectionEvent() -> (byte<1>, handle<copy>)
 52          public ResultCode AcquireBleConnectionEvent(ServiceCtx context)
 53          {
 54              Result result = Result.Success;
 55  
 56              if (_bleConnectionEventHandle == 0)
 57              {
 58                  _bleConnectionEvent = new KEvent(context.Device.System.KernelContext);
 59  
 60                  result = context.Process.HandleTable.GenerateHandle(_bleConnectionEvent.ReadableEvent, out _bleConnectionEventHandle);
 61  
 62                  if (result != Result.Success)
 63                  {
 64                      // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
 65                      Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
 66                  }
 67              }
 68  
 69              context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleConnectionEventHandle);
 70  
 71              context.ResponseData.Write(result == Result.Success ? 1 : 0);
 72  
 73              return ResultCode.Success;
 74          }
 75  
 76          [CommandCmif(26)] // 5.0.0+
 77          // AcquireBleServiceDiscoveryEvent() -> (byte<1>, handle<copy>)
 78          public ResultCode AcquireBleServiceDiscoveryEvent(ServiceCtx context)
 79          {
 80              Result result = Result.Success;
 81  
 82              if (_bleServiceDiscoveryEventHandle == 0)
 83              {
 84                  _bleServiceDiscoveryEvent = new KEvent(context.Device.System.KernelContext);
 85  
 86                  result = context.Process.HandleTable.GenerateHandle(_bleServiceDiscoveryEvent.ReadableEvent, out _bleServiceDiscoveryEventHandle);
 87  
 88                  if (result != Result.Success)
 89                  {
 90                      // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
 91                      Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
 92                  }
 93              }
 94  
 95              context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleServiceDiscoveryEventHandle);
 96  
 97              context.ResponseData.Write(result == Result.Success ? 1 : 0);
 98  
 99              return ResultCode.Success;
100          }
101  
102          [CommandCmif(33)] // 5.0.0+
103          // AcquireBleMtuConfigEvent() -> (byte<1>, handle<copy>)
104          public ResultCode AcquireBleMtuConfigEvent(ServiceCtx context)
105          {
106              Result result = Result.Success;
107  
108              if (_bleMtuConfigEventHandle == 0)
109              {
110                  _bleMtuConfigEvent = new KEvent(context.Device.System.KernelContext);
111  
112                  result = context.Process.HandleTable.GenerateHandle(_bleMtuConfigEvent.ReadableEvent, out _bleMtuConfigEventHandle);
113  
114                  if (result != Result.Success)
115                  {
116                      // NOTE: We use a Logging instead of an exception because the call return a boolean if succeed or not.
117                      Logger.Error?.Print(LogClass.ServiceBsd, "Out of handles!");
118                  }
119              }
120  
121              context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_bleMtuConfigEventHandle);
122  
123              context.ResponseData.Write(result == Result.Success ? 1 : 0);
124  
125              return ResultCode.Success;
126          }
127      }
128  }