IUserLocalCommunicationService.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Ryujinx.HLE.HOS.Ipc;
  2. using Ryujinx.HLE.HOS.Kernel.Common;
  3. using Ryujinx.HLE.HOS.Services.Ldn.Types;
  4. using System;
  5. using System.Net;
  6. namespace Ryujinx.HLE.HOS.Services.Ldn.UserServiceCreator
  7. {
  8. class IUserLocalCommunicationService : IpcService
  9. {
  10. // TODO(Ac_K): Determine what the hardcoded unknown value is.
  11. private const int UnknownValue = 90;
  12. private NetworkInterface _networkInterface;
  13. private int _stateChangeEventHandle = 0;
  14. public IUserLocalCommunicationService(ServiceCtx context)
  15. {
  16. _networkInterface = new NetworkInterface(context.Device.System);
  17. }
  18. [Command(0)]
  19. // GetState() -> s32 state
  20. public ResultCode GetState(ServiceCtx context)
  21. {
  22. if (_networkInterface.NifmState != ResultCode.Success)
  23. {
  24. context.ResponseData.Write((int)NetworkState.Error);
  25. return ResultCode.Success;
  26. }
  27. ResultCode result = _networkInterface.GetState(out NetworkState state);
  28. if (result == ResultCode.Success)
  29. {
  30. context.ResponseData.Write((int)state);
  31. }
  32. return result;
  33. }
  34. [Command(100)]
  35. // AttachStateChangeEvent() -> handle<copy>
  36. public ResultCode AttachStateChangeEvent(ServiceCtx context)
  37. {
  38. if (_stateChangeEventHandle == 0)
  39. {
  40. if (context.Process.HandleTable.GenerateHandle(_networkInterface.StateChangeEvent.ReadableEvent, out _stateChangeEventHandle) != KernelResult.Success)
  41. {
  42. throw new InvalidOperationException("Out of handles!");
  43. }
  44. }
  45. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_stateChangeEventHandle);
  46. // Return ResultCode.InvalidArgument if handle is null, doesn't occur in our case since we already throw an Exception.
  47. return ResultCode.Success;
  48. }
  49. [Command(400)]
  50. // InitializeOld(u64, pid)
  51. public ResultCode InitializeOld(ServiceCtx context)
  52. {
  53. return _networkInterface.Initialize(UnknownValue, 0, null, null);
  54. }
  55. [Command(401)]
  56. // Finalize()
  57. public ResultCode Finalize(ServiceCtx context)
  58. {
  59. return _networkInterface.Finalize();
  60. }
  61. [Command(402)] // 7.0.0+
  62. // Initialize(u64 ip_addresses, u64, pid)
  63. public ResultCode Initialize(ServiceCtx context)
  64. {
  65. // TODO(Ac_K): Determine what addresses are.
  66. IPAddress unknownAddress1 = new IPAddress(context.RequestData.ReadUInt32());
  67. IPAddress unknownAddress2 = new IPAddress(context.RequestData.ReadUInt32());
  68. return _networkInterface.Initialize(UnknownValue, version: 1, unknownAddress1, unknownAddress2);
  69. }
  70. }
  71. }