IUser.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using Ryujinx.HLE.Exceptions;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using Ryujinx.HLE.HOS.Services.Hid;
  6. using Ryujinx.HLE.HOS.Services.Hid.HidServer;
  7. using Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager;
  8. using System;
  9. using System.Collections.Generic;
  10. namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
  11. {
  12. class IUser : IpcService
  13. {
  14. private State _state = State.NonInitialized;
  15. private KEvent _availabilityChangeEvent;
  16. private int _availabilityChangeEventHandle = 0;
  17. private List<Device> _devices = new List<Device>();
  18. public IUser() { }
  19. [Command(0)]
  20. // Initialize(u64, u64, pid, buffer<unknown, 5>)
  21. public ResultCode Initialize(ServiceCtx context)
  22. {
  23. long appletResourceUserId = context.RequestData.ReadInt64();
  24. long mcuVersionData = context.RequestData.ReadInt64();
  25. long inputPosition = context.Request.SendBuff[0].Position;
  26. long inputSize = context.Request.SendBuff[0].Size;
  27. byte[] unknownBuffer = new byte[inputSize];
  28. context.Memory.Read((ulong)inputPosition, unknownBuffer);
  29. // NOTE: appletResourceUserId, mcuVersionData and the buffer are stored inside an internal struct.
  30. // The buffer seems to contains entries with a size of 0x40 bytes each.
  31. // Sadly, this internal struct doesn't seems to be used in retail.
  32. // TODO: Add an instance of nn::nfc::server::Manager when it will be implemented.
  33. // Add an instance of nn::nfc::server::SaveData when it will be implemented.
  34. // TODO: When we will be able to add multiple controllers add one entry by controller here.
  35. Device device1 = new Device
  36. {
  37. NpadIdType = NpadIdType.Player1,
  38. Handle = HidUtils.GetIndexFromNpadIdType(NpadIdType.Player1),
  39. State = DeviceState.Initialized
  40. };
  41. _devices.Add(device1);
  42. _state = State.Initialized;
  43. return ResultCode.Success;
  44. }
  45. [Command(1)]
  46. // Finalize()
  47. public ResultCode Finalize(ServiceCtx context)
  48. {
  49. // TODO: Call StopDetection() and Unmount() when they will be implemented.
  50. // Remove the instance of nn::nfc::server::Manager when it will be implemented.
  51. // Remove the instance of nn::nfc::server::SaveData when it will be implemented.
  52. _devices.Clear();
  53. _state = State.NonInitialized;
  54. return ResultCode.Success;
  55. }
  56. [Command(2)]
  57. // ListDevices() -> (u32, buffer<unknown, 0xa>)
  58. public ResultCode ListDevices(ServiceCtx context)
  59. {
  60. if (context.Request.RecvListBuff.Count == 0)
  61. {
  62. return ResultCode.DevicesBufferIsNull;
  63. }
  64. long outputPosition = context.Request.RecvListBuff[0].Position;
  65. long outputSize = context.Request.RecvListBuff[0].Size;
  66. if (_devices.Count == 0)
  67. {
  68. return ResultCode.DeviceNotFound;
  69. }
  70. for (int i = 0; i < _devices.Count; i++)
  71. {
  72. context.Memory.Write((ulong)(outputPosition + (i * sizeof(long))), (uint)_devices[i].Handle);
  73. }
  74. context.ResponseData.Write(_devices.Count);
  75. return ResultCode.Success;
  76. }
  77. [Command(3)]
  78. // StartDetection(bytes<8, 4>)
  79. public ResultCode StartDetection(ServiceCtx context)
  80. {
  81. throw new ServiceNotImplementedException(this, context);
  82. }
  83. [Command(4)]
  84. // StopDetection(bytes<8, 4>)
  85. public ResultCode StopDetection(ServiceCtx context)
  86. {
  87. throw new ServiceNotImplementedException(this, context);
  88. }
  89. [Command(5)]
  90. // Mount(bytes<8, 4>, u32, u32)
  91. public ResultCode Mount(ServiceCtx context)
  92. {
  93. throw new ServiceNotImplementedException(this, context);
  94. }
  95. [Command(6)]
  96. // Unmount(bytes<8, 4>)
  97. public ResultCode Unmount(ServiceCtx context)
  98. {
  99. throw new ServiceNotImplementedException(this, context);
  100. }
  101. [Command(7)]
  102. // OpenApplicationArea(bytes<8, 4>, u32)
  103. public ResultCode OpenApplicationArea(ServiceCtx context)
  104. {
  105. throw new ServiceNotImplementedException(this, context);
  106. }
  107. [Command(8)]
  108. // GetApplicationArea(bytes<8, 4>) -> (u32, buffer<unknown, 6>)
  109. public ResultCode GetApplicationArea(ServiceCtx context)
  110. {
  111. throw new ServiceNotImplementedException(this, context);
  112. }
  113. [Command(9)]
  114. // SetApplicationArea(bytes<8, 4>, buffer<unknown, 5>)
  115. public ResultCode SetApplicationArea(ServiceCtx context)
  116. {
  117. throw new ServiceNotImplementedException(this, context);
  118. }
  119. [Command(10)]
  120. // Flush(bytes<8, 4>)
  121. public ResultCode Flush(ServiceCtx context)
  122. {
  123. throw new ServiceNotImplementedException(this, context);
  124. }
  125. [Command(11)]
  126. // Restore(bytes<8, 4>)
  127. public ResultCode Restore(ServiceCtx context)
  128. {
  129. throw new ServiceNotImplementedException(this, context);
  130. }
  131. [Command(12)]
  132. // CreateApplicationArea(bytes<8, 4>, u32, buffer<unknown, 5>)
  133. public ResultCode CreateApplicationArea(ServiceCtx context)
  134. {
  135. throw new ServiceNotImplementedException(this, context);
  136. }
  137. [Command(13)]
  138. // GetTagInfo(bytes<8, 4>) -> buffer<unknown<0x58>, 0x1a>
  139. public ResultCode GetTagInfo(ServiceCtx context)
  140. {
  141. throw new ServiceNotImplementedException(this, context);
  142. }
  143. [Command(14)]
  144. // GetRegisterInfo(bytes<8, 4>) -> buffer<unknown<0x100>, 0x1a>
  145. public ResultCode GetRegisterInfo(ServiceCtx context)
  146. {
  147. throw new ServiceNotImplementedException(this, context);
  148. }
  149. [Command(15)]
  150. // GetCommonInfo(bytes<8, 4>) -> buffer<unknown<0x40>, 0x1a>
  151. public ResultCode GetCommonInfo(ServiceCtx context)
  152. {
  153. throw new ServiceNotImplementedException(this, context);
  154. }
  155. [Command(16)]
  156. // GetModelInfo(bytes<8, 4>) -> buffer<unknown<0x40>, 0x1a>
  157. public ResultCode GetModelInfo(ServiceCtx context)
  158. {
  159. throw new ServiceNotImplementedException(this, context);
  160. }
  161. [Command(17)]
  162. // AttachActivateEvent(bytes<8, 4>) -> handle<copy>
  163. public ResultCode AttachActivateEvent(ServiceCtx context)
  164. {
  165. uint deviceHandle = context.RequestData.ReadUInt32();
  166. for (int i = 0; i < _devices.Count; i++)
  167. {
  168. if ((uint)_devices[i].Handle == deviceHandle)
  169. {
  170. if (_devices[i].ActivateEventHandle == 0)
  171. {
  172. _devices[i].ActivateEvent = new KEvent(context.Device.System.KernelContext);
  173. if (context.Process.HandleTable.GenerateHandle(_devices[i].ActivateEvent.ReadableEvent, out _devices[i].ActivateEventHandle) != KernelResult.Success)
  174. {
  175. throw new InvalidOperationException("Out of handles!");
  176. }
  177. }
  178. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_devices[i].ActivateEventHandle);
  179. return ResultCode.Success;
  180. }
  181. }
  182. return ResultCode.DeviceNotFound;
  183. }
  184. [Command(18)]
  185. // AttachDeactivateEvent(bytes<8, 4>) -> handle<copy>
  186. public ResultCode AttachDeactivateEvent(ServiceCtx context)
  187. {
  188. uint deviceHandle = context.RequestData.ReadUInt32();
  189. for (int i = 0; i < _devices.Count; i++)
  190. {
  191. if ((uint)_devices[i].Handle == deviceHandle)
  192. {
  193. if (_devices[i].DeactivateEventHandle == 0)
  194. {
  195. _devices[i].DeactivateEvent = new KEvent(context.Device.System.KernelContext);
  196. if (context.Process.HandleTable.GenerateHandle(_devices[i].DeactivateEvent.ReadableEvent, out _devices[i].DeactivateEventHandle) != KernelResult.Success)
  197. {
  198. throw new InvalidOperationException("Out of handles!");
  199. }
  200. }
  201. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_devices[i].DeactivateEventHandle);
  202. return ResultCode.Success;
  203. }
  204. }
  205. return ResultCode.DeviceNotFound;
  206. }
  207. [Command(19)]
  208. // GetState() -> u32
  209. public ResultCode GetState(ServiceCtx context)
  210. {
  211. context.ResponseData.Write((int)_state);
  212. return ResultCode.Success;
  213. }
  214. [Command(20)]
  215. // GetDeviceState(bytes<8, 4>) -> u32
  216. public ResultCode GetDeviceState(ServiceCtx context)
  217. {
  218. uint deviceHandle = context.RequestData.ReadUInt32();
  219. for (int i = 0; i < _devices.Count; i++)
  220. {
  221. if ((uint)_devices[i].Handle == deviceHandle)
  222. {
  223. context.ResponseData.Write((uint)_devices[i].State);
  224. return ResultCode.Success;
  225. }
  226. }
  227. context.ResponseData.Write((uint)DeviceState.Unavailable);
  228. return ResultCode.DeviceNotFound;
  229. }
  230. [Command(21)]
  231. // GetNpadId(bytes<8, 4>) -> u32
  232. public ResultCode GetNpadId(ServiceCtx context)
  233. {
  234. uint deviceHandle = context.RequestData.ReadUInt32();
  235. for (int i = 0; i < _devices.Count; i++)
  236. {
  237. if ((uint)_devices[i].Handle == deviceHandle)
  238. {
  239. context.ResponseData.Write((uint)HidUtils.GetNpadIdTypeFromIndex(_devices[i].Handle));
  240. return ResultCode.Success;
  241. }
  242. }
  243. return ResultCode.DeviceNotFound;
  244. }
  245. [Command(22)]
  246. // GetApplicationAreaSize(bytes<8, 4>) -> u32
  247. public ResultCode GetApplicationAreaSize(ServiceCtx context)
  248. {
  249. throw new ServiceNotImplementedException(this, context);
  250. }
  251. [Command(23)] // 3.0.0+
  252. // AttachAvailabilityChangeEvent() -> handle<copy>
  253. public ResultCode AttachAvailabilityChangeEvent(ServiceCtx context)
  254. {
  255. if (_availabilityChangeEventHandle == 0)
  256. {
  257. _availabilityChangeEvent = new KEvent(context.Device.System.KernelContext);
  258. if (context.Process.HandleTable.GenerateHandle(_availabilityChangeEvent.ReadableEvent, out _availabilityChangeEventHandle) != KernelResult.Success)
  259. {
  260. throw new InvalidOperationException("Out of handles!");
  261. }
  262. }
  263. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_availabilityChangeEventHandle);
  264. return ResultCode.Success;
  265. }
  266. [Command(24)] // 3.0.0+
  267. // RecreateApplicationArea(bytes<8, 4>, u32, buffer<unknown, 5>)
  268. public ResultCode RecreateApplicationArea(ServiceCtx context)
  269. {
  270. throw new ServiceNotImplementedException(this, context);
  271. }
  272. }
  273. }