IUser.cs 11 KB

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