IUser.cs 11 KB

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