IUser.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.Exceptions;
  4. using Ryujinx.HLE.HOS.Ipc;
  5. using Ryujinx.HLE.HOS.Kernel.Common;
  6. using Ryujinx.HLE.HOS.Kernel.Threading;
  7. using Ryujinx.HLE.HOS.Services.Hid;
  8. using Ryujinx.HLE.HOS.Services.Hid.HidServer;
  9. using Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager;
  10. using System;
  11. using System.Buffers.Binary;
  12. using System.Globalization;
  13. using System.Runtime.InteropServices;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
  17. {
  18. class IUser : IpcService
  19. {
  20. private ulong _appletResourceUserId;
  21. private ulong _mcuVersionData;
  22. private byte[] _mcuData;
  23. private State _state = State.NonInitialized;
  24. private KEvent _availabilityChangeEvent;
  25. private CancellationTokenSource _cancelTokenSource;
  26. public IUser() { }
  27. [Command(0)]
  28. // Initialize(u64, u64, pid, buffer<unknown, 5>)
  29. public ResultCode Initialize(ServiceCtx context)
  30. {
  31. _appletResourceUserId = context.RequestData.ReadUInt64();
  32. _mcuVersionData = context.RequestData.ReadUInt64();
  33. long inputPosition = context.Request.SendBuff[0].Position;
  34. long inputSize = context.Request.SendBuff[0].Size;
  35. _mcuData = new byte[inputSize];
  36. context.Memory.Read((ulong)inputPosition, _mcuData);
  37. // TODO: The mcuData buffer seems to contains entries with a size of 0x40 bytes each. Usage of the data needs to be determined.
  38. // TODO: Handle this in a controller class directly.
  39. // Every functions which use the Handle call nn::hid::system::GetXcdHandleForNpadWithNfc().
  40. NfpDevice devicePlayer1 = new NfpDevice
  41. {
  42. NpadIdType = NpadIdType.Player1,
  43. Handle = HidUtils.GetIndexFromNpadIdType(NpadIdType.Player1),
  44. State = NfpDeviceState.Initialized
  45. };
  46. context.Device.System.NfpDevices.Add(devicePlayer1);
  47. // TODO: It mounts 0x8000000000000020 save data and stores a random generate value inside. Usage of the data needs to be determined.
  48. _state = State.Initialized;
  49. return ResultCode.Success;
  50. }
  51. [Command(1)]
  52. // Finalize()
  53. public ResultCode Finalize(ServiceCtx context)
  54. {
  55. if (_state == State.Initialized)
  56. {
  57. if (_cancelTokenSource != null)
  58. {
  59. _cancelTokenSource.Cancel();
  60. }
  61. // NOTE: All events are destroyed here.
  62. context.Device.System.NfpDevices.Clear();
  63. _state = State.NonInitialized;
  64. }
  65. return ResultCode.Success;
  66. }
  67. [Command(2)]
  68. // ListDevices() -> (u32, buffer<unknown, 0xa>)
  69. public ResultCode ListDevices(ServiceCtx context)
  70. {
  71. if (context.Request.RecvListBuff.Count == 0)
  72. {
  73. return ResultCode.WrongArgument;
  74. }
  75. long outputPosition = context.Request.RecvListBuff[0].Position;
  76. long outputSize = context.Request.RecvListBuff[0].Size;
  77. if (context.Device.System.NfpDevices.Count == 0)
  78. {
  79. return ResultCode.DeviceNotFound;
  80. }
  81. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  82. if (CheckNfcIsEnabled() == ResultCode.Success)
  83. {
  84. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  85. {
  86. context.Memory.Write((ulong)(outputPosition + (i * sizeof(long))), (uint)context.Device.System.NfpDevices[i].Handle);
  87. }
  88. context.ResponseData.Write(context.Device.System.NfpDevices.Count);
  89. }
  90. else
  91. {
  92. context.ResponseData.Write(0);
  93. }
  94. return ResultCode.Success;
  95. }
  96. [Command(3)]
  97. // StartDetection(bytes<8, 4>)
  98. public ResultCode StartDetection(ServiceCtx context)
  99. {
  100. ResultCode resultCode = CheckNfcIsEnabled();
  101. if (resultCode != ResultCode.Success)
  102. {
  103. return resultCode;
  104. }
  105. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  106. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  107. {
  108. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  109. {
  110. context.Device.System.NfpDevices[i].State = NfpDeviceState.SearchingForTag;
  111. break;
  112. }
  113. }
  114. _cancelTokenSource = new CancellationTokenSource();
  115. Task.Run(() =>
  116. {
  117. while (true)
  118. {
  119. if (_cancelTokenSource.Token.IsCancellationRequested)
  120. {
  121. break;
  122. }
  123. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  124. {
  125. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagFound)
  126. {
  127. context.Device.System.NfpDevices[i].SignalActivate();
  128. Thread.Sleep(50); // NOTE: Simulate amiibo scanning delay.
  129. context.Device.System.NfpDevices[i].SignalDeactivate();
  130. break;
  131. }
  132. }
  133. }
  134. }, _cancelTokenSource.Token);
  135. return ResultCode.Success;
  136. }
  137. [Command(4)]
  138. // StopDetection(bytes<8, 4>)
  139. public ResultCode StopDetection(ServiceCtx context)
  140. {
  141. ResultCode resultCode = CheckNfcIsEnabled();
  142. if (resultCode != ResultCode.Success)
  143. {
  144. return resultCode;
  145. }
  146. if (_cancelTokenSource != null)
  147. {
  148. _cancelTokenSource.Cancel();
  149. }
  150. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  151. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  152. {
  153. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  154. {
  155. context.Device.System.NfpDevices[i].State = NfpDeviceState.Initialized;
  156. break;
  157. }
  158. }
  159. return ResultCode.Success;
  160. }
  161. [Command(5)]
  162. // Mount(bytes<8, 4>, u32, u32)
  163. public ResultCode Mount(ServiceCtx context)
  164. {
  165. ResultCode resultCode = CheckNfcIsEnabled();
  166. if (resultCode != ResultCode.Success)
  167. {
  168. return resultCode;
  169. }
  170. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  171. UserManager.DeviceType deviceType = (UserManager.DeviceType)context.RequestData.ReadUInt32();
  172. MountTarget mountTarget = (MountTarget)context.RequestData.ReadUInt32();
  173. if (deviceType != 0)
  174. {
  175. return ResultCode.WrongArgument;
  176. }
  177. if (((uint)mountTarget & 3) == 0)
  178. {
  179. return ResultCode.WrongArgument;
  180. }
  181. // TODO: Found how the MountTarget is handled.
  182. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  183. {
  184. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  185. {
  186. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  187. {
  188. resultCode = ResultCode.TagNotFound;
  189. }
  190. else
  191. {
  192. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagFound)
  193. {
  194. // NOTE: This mount the amiibo data, which isn't needed in our case.
  195. context.Device.System.NfpDevices[i].State = NfpDeviceState.TagMounted;
  196. resultCode = ResultCode.Success;
  197. }
  198. else
  199. {
  200. resultCode = ResultCode.WrongDeviceState;
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. return resultCode;
  207. }
  208. [Command(6)]
  209. // Unmount(bytes<8, 4>)
  210. public ResultCode Unmount(ServiceCtx context)
  211. {
  212. ResultCode resultCode = CheckNfcIsEnabled();
  213. if (resultCode != ResultCode.Success)
  214. {
  215. return resultCode;
  216. }
  217. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  218. if (context.Device.System.NfpDevices.Count == 0)
  219. {
  220. return ResultCode.DeviceNotFound;
  221. }
  222. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  223. {
  224. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  225. {
  226. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  227. {
  228. resultCode = ResultCode.TagNotFound;
  229. }
  230. else
  231. {
  232. // NOTE: This mount the amiibo data, which isn't needed in our case.
  233. context.Device.System.NfpDevices[i].State = NfpDeviceState.TagFound;
  234. resultCode = ResultCode.Success;
  235. }
  236. break;
  237. }
  238. }
  239. return resultCode;
  240. }
  241. [Command(7)]
  242. // OpenApplicationArea(bytes<8, 4>, u32)
  243. public ResultCode OpenApplicationArea(ServiceCtx context)
  244. {
  245. ResultCode resultCode = CheckNfcIsEnabled();
  246. if (resultCode != ResultCode.Success)
  247. {
  248. return resultCode;
  249. }
  250. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  251. if (context.Device.System.NfpDevices.Count == 0)
  252. {
  253. return ResultCode.DeviceNotFound;
  254. }
  255. uint applicationAreaId = context.RequestData.ReadUInt32();
  256. bool isOpened = false;
  257. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  258. {
  259. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  260. {
  261. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  262. {
  263. resultCode = ResultCode.TagNotFound;
  264. }
  265. else
  266. {
  267. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  268. {
  269. isOpened = VirtualAmiibo.OpenApplicationArea(context.Device.System.NfpDevices[i].AmiiboId, applicationAreaId);
  270. resultCode = ResultCode.Success;
  271. }
  272. else
  273. {
  274. resultCode = ResultCode.WrongDeviceState;
  275. }
  276. }
  277. break;
  278. }
  279. }
  280. if (!isOpened)
  281. {
  282. resultCode = ResultCode.ApplicationAreaIsNull;
  283. }
  284. return resultCode;
  285. }
  286. [Command(8)]
  287. // GetApplicationArea(bytes<8, 4>) -> (u32, buffer<unknown, 6>)
  288. public ResultCode GetApplicationArea(ServiceCtx context)
  289. {
  290. ResultCode resultCode = CheckNfcIsEnabled();
  291. if (resultCode != ResultCode.Success)
  292. {
  293. return resultCode;
  294. }
  295. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  296. if (context.Device.System.NfpDevices.Count == 0)
  297. {
  298. return ResultCode.DeviceNotFound;
  299. }
  300. long outputPosition = context.Request.ReceiveBuff[0].Position;
  301. long outputSize = context.Request.ReceiveBuff[0].Size;
  302. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  303. uint size = 0;
  304. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  305. {
  306. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  307. {
  308. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  309. {
  310. resultCode = ResultCode.TagNotFound;
  311. }
  312. else
  313. {
  314. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  315. {
  316. byte[] applicationArea = VirtualAmiibo.GetApplicationArea(context.Device.System.NfpDevices[i].AmiiboId);
  317. context.Memory.Write((ulong)outputPosition, applicationArea);
  318. size = (uint)applicationArea.Length;
  319. resultCode = ResultCode.Success;
  320. }
  321. else
  322. {
  323. resultCode = ResultCode.WrongDeviceState;
  324. }
  325. }
  326. }
  327. }
  328. if (resultCode != ResultCode.Success)
  329. {
  330. return resultCode;
  331. }
  332. if (size == 0)
  333. {
  334. return ResultCode.ApplicationAreaIsNull;
  335. }
  336. context.ResponseData.Write(size);
  337. return ResultCode.Success;
  338. }
  339. [Command(9)]
  340. // SetApplicationArea(bytes<8, 4>, buffer<unknown, 5>)
  341. public ResultCode SetApplicationArea(ServiceCtx context)
  342. {
  343. ResultCode resultCode = CheckNfcIsEnabled();
  344. if (resultCode != ResultCode.Success)
  345. {
  346. return resultCode;
  347. }
  348. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  349. if (context.Device.System.NfpDevices.Count == 0)
  350. {
  351. return ResultCode.DeviceNotFound;
  352. }
  353. long inputPosition = context.Request.SendBuff[0].Position;
  354. long inputSize = context.Request.SendBuff[0].Size;
  355. byte[] applicationArea = new byte[inputSize];
  356. context.Memory.Read((ulong)inputPosition, applicationArea);
  357. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  358. {
  359. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  360. {
  361. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  362. {
  363. resultCode = ResultCode.TagNotFound;
  364. }
  365. else
  366. {
  367. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  368. {
  369. VirtualAmiibo.SetApplicationArea(context.Device.System.NfpDevices[i].AmiiboId, applicationArea);
  370. resultCode = ResultCode.Success;
  371. }
  372. else
  373. {
  374. resultCode = ResultCode.WrongDeviceState;
  375. }
  376. }
  377. break;
  378. }
  379. }
  380. return resultCode;
  381. }
  382. [Command(10)]
  383. // Flush(bytes<8, 4>)
  384. public ResultCode Flush(ServiceCtx context)
  385. {
  386. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  387. if (context.Device.System.NfpDevices.Count == 0)
  388. {
  389. return ResultCode.DeviceNotFound;
  390. }
  391. // NOTE: Since we handle amiibo through VirtualAmiibo, we don't have to flush anything in our case.
  392. return ResultCode.Success;
  393. }
  394. [Command(11)]
  395. // Restore(bytes<8, 4>)
  396. public ResultCode Restore(ServiceCtx context)
  397. {
  398. throw new ServiceNotImplementedException(this, context);
  399. }
  400. [Command(12)]
  401. // CreateApplicationArea(bytes<8, 4>, u32, buffer<unknown, 5>)
  402. public ResultCode CreateApplicationArea(ServiceCtx context)
  403. {
  404. ResultCode resultCode = CheckNfcIsEnabled();
  405. if (resultCode != ResultCode.Success)
  406. {
  407. return resultCode;
  408. }
  409. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  410. if (context.Device.System.NfpDevices.Count == 0)
  411. {
  412. return ResultCode.DeviceNotFound;
  413. }
  414. uint applicationAreaId = context.RequestData.ReadUInt32();
  415. long inputPosition = context.Request.SendBuff[0].Position;
  416. long inputSize = context.Request.SendBuff[0].Size;
  417. byte[] applicationArea = new byte[inputSize];
  418. context.Memory.Read((ulong)inputPosition, applicationArea);
  419. bool isCreated = false;
  420. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  421. {
  422. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  423. {
  424. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  425. {
  426. resultCode = ResultCode.TagNotFound;
  427. }
  428. else
  429. {
  430. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  431. {
  432. isCreated = VirtualAmiibo.CreateApplicationArea(context.Device.System.NfpDevices[i].AmiiboId, applicationAreaId, applicationArea);
  433. resultCode = ResultCode.Success;
  434. }
  435. else
  436. {
  437. resultCode = ResultCode.WrongDeviceState;
  438. }
  439. }
  440. break;
  441. }
  442. }
  443. if (!isCreated)
  444. {
  445. resultCode = ResultCode.ApplicationAreaIsNull;
  446. }
  447. return resultCode;
  448. }
  449. [Command(13)]
  450. // GetTagInfo(bytes<8, 4>) -> buffer<unknown<0x58>, 0x1a>
  451. public ResultCode GetTagInfo(ServiceCtx context)
  452. {
  453. ResultCode resultCode = CheckNfcIsEnabled();
  454. if (resultCode != ResultCode.Success)
  455. {
  456. return resultCode;
  457. }
  458. if (context.Request.RecvListBuff.Count == 0)
  459. {
  460. return ResultCode.WrongArgument;
  461. }
  462. long outputPosition = context.Request.RecvListBuff[0].Position;
  463. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(Marshal.SizeOf(typeof(TagInfo)));
  464. MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf(typeof(TagInfo)));
  465. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  466. if (context.Device.System.NfpDevices.Count == 0)
  467. {
  468. return ResultCode.DeviceNotFound;
  469. }
  470. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  471. {
  472. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  473. {
  474. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  475. {
  476. resultCode = ResultCode.TagNotFound;
  477. }
  478. else
  479. {
  480. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted || context.Device.System.NfpDevices[i].State == NfpDeviceState.TagFound)
  481. {
  482. byte[] Uuid = VirtualAmiibo.GenerateUuid(context.Device.System.NfpDevices[i].AmiiboId, context.Device.System.NfpDevices[i].UseRandomUuid);
  483. if (Uuid.Length > AmiiboConstants.UuidMaxLength)
  484. {
  485. throw new ArgumentOutOfRangeException();
  486. }
  487. TagInfo tagInfo = new TagInfo
  488. {
  489. UuidLength = (byte)Uuid.Length,
  490. Reserved1 = new Array21<byte>(),
  491. Protocol = uint.MaxValue, // All Protocol
  492. TagType = uint.MaxValue, // All Type
  493. Reserved2 = new Array6<byte>()
  494. };
  495. Uuid.CopyTo(tagInfo.Uuid.ToSpan());
  496. context.Memory.Write((ulong)outputPosition, tagInfo);
  497. resultCode = ResultCode.Success;
  498. }
  499. else
  500. {
  501. resultCode = ResultCode.WrongDeviceState;
  502. }
  503. }
  504. break;
  505. }
  506. }
  507. return resultCode;
  508. }
  509. [Command(14)]
  510. // GetRegisterInfo(bytes<8, 4>) -> buffer<unknown<0x100>, 0x1a>
  511. public ResultCode GetRegisterInfo(ServiceCtx context)
  512. {
  513. ResultCode resultCode = CheckNfcIsEnabled();
  514. if (resultCode != ResultCode.Success)
  515. {
  516. return resultCode;
  517. }
  518. if (context.Request.RecvListBuff.Count == 0)
  519. {
  520. return ResultCode.WrongArgument;
  521. }
  522. long outputPosition = context.Request.RecvListBuff[0].Position;
  523. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(Marshal.SizeOf(typeof(RegisterInfo)));
  524. MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf(typeof(RegisterInfo)));
  525. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  526. if (context.Device.System.NfpDevices.Count == 0)
  527. {
  528. return ResultCode.DeviceNotFound;
  529. }
  530. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  531. {
  532. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  533. {
  534. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  535. {
  536. resultCode = ResultCode.TagNotFound;
  537. }
  538. else
  539. {
  540. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  541. {
  542. RegisterInfo registerInfo = VirtualAmiibo.GetRegisterInfo(context.Device.System.NfpDevices[i].AmiiboId);
  543. context.Memory.Write((ulong)outputPosition, registerInfo);
  544. resultCode = ResultCode.Success;
  545. }
  546. else
  547. {
  548. resultCode = ResultCode.WrongDeviceState;
  549. }
  550. }
  551. break;
  552. }
  553. }
  554. return resultCode;
  555. }
  556. [Command(15)]
  557. // GetCommonInfo(bytes<8, 4>) -> buffer<unknown<0x40>, 0x1a>
  558. public ResultCode GetCommonInfo(ServiceCtx context)
  559. {
  560. ResultCode resultCode = CheckNfcIsEnabled();
  561. if (resultCode != ResultCode.Success)
  562. {
  563. return resultCode;
  564. }
  565. if (context.Request.RecvListBuff.Count == 0)
  566. {
  567. return ResultCode.WrongArgument;
  568. }
  569. long outputPosition = context.Request.RecvListBuff[0].Position;
  570. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(Marshal.SizeOf(typeof(CommonInfo)));
  571. MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf(typeof(CommonInfo)));
  572. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  573. if (context.Device.System.NfpDevices.Count == 0)
  574. {
  575. return ResultCode.DeviceNotFound;
  576. }
  577. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  578. {
  579. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  580. {
  581. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  582. {
  583. resultCode = ResultCode.TagNotFound;
  584. }
  585. else
  586. {
  587. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  588. {
  589. CommonInfo commonInfo = VirtualAmiibo.GetCommonInfo(context.Device.System.NfpDevices[i].AmiiboId);
  590. context.Memory.Write((ulong)outputPosition, commonInfo);
  591. resultCode = ResultCode.Success;
  592. }
  593. else
  594. {
  595. resultCode = ResultCode.WrongDeviceState;
  596. }
  597. }
  598. break;
  599. }
  600. }
  601. return resultCode;
  602. }
  603. [Command(16)]
  604. // GetModelInfo(bytes<8, 4>) -> buffer<unknown<0x40>, 0x1a>
  605. public ResultCode GetModelInfo(ServiceCtx context)
  606. {
  607. ResultCode resultCode = CheckNfcIsEnabled();
  608. if (resultCode != ResultCode.Success)
  609. {
  610. return resultCode;
  611. }
  612. if (context.Request.RecvListBuff.Count == 0)
  613. {
  614. return ResultCode.WrongArgument;
  615. }
  616. long outputPosition = context.Request.RecvListBuff[0].Position;
  617. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(Marshal.SizeOf(typeof(ModelInfo)));
  618. MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf(typeof(ModelInfo)));
  619. uint deviceHandle = (uint)context.RequestData.ReadUInt64();
  620. if (context.Device.System.NfpDevices.Count == 0)
  621. {
  622. return ResultCode.DeviceNotFound;
  623. }
  624. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  625. {
  626. if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
  627. {
  628. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
  629. {
  630. resultCode = ResultCode.TagNotFound;
  631. }
  632. else
  633. {
  634. if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
  635. {
  636. ModelInfo modelInfo = new ModelInfo
  637. {
  638. Reserved = new Array57<byte>()
  639. };
  640. modelInfo.CharacterId = BinaryPrimitives.ReverseEndianness(ushort.Parse(context.Device.System.NfpDevices[i].AmiiboId.Substring(0, 4), NumberStyles.HexNumber));
  641. modelInfo.CharacterVariant = byte.Parse(context.Device.System.NfpDevices[i].AmiiboId.Substring(4, 2), NumberStyles.HexNumber);
  642. modelInfo.Series = byte.Parse(context.Device.System.NfpDevices[i].AmiiboId.Substring(12, 2), NumberStyles.HexNumber);
  643. modelInfo.ModelNumber = ushort.Parse(context.Device.System.NfpDevices[i].AmiiboId.Substring(8, 4), NumberStyles.HexNumber);
  644. modelInfo.Type = byte.Parse(context.Device.System.NfpDevices[i].AmiiboId.Substring(6, 2), NumberStyles.HexNumber);
  645. context.Memory.Write((ulong)outputPosition, modelInfo);
  646. resultCode = ResultCode.Success;
  647. }
  648. else
  649. {
  650. resultCode = ResultCode.WrongDeviceState;
  651. }
  652. }
  653. break;
  654. }
  655. }
  656. return resultCode;
  657. }
  658. [Command(17)]
  659. // AttachActivateEvent(bytes<8, 4>) -> handle<copy>
  660. public ResultCode AttachActivateEvent(ServiceCtx context)
  661. {
  662. uint deviceHandle = context.RequestData.ReadUInt32();
  663. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  664. {
  665. if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
  666. {
  667. context.Device.System.NfpDevices[i].ActivateEvent = new KEvent(context.Device.System.KernelContext);
  668. if (context.Process.HandleTable.GenerateHandle(context.Device.System.NfpDevices[i].ActivateEvent.ReadableEvent, out int activateEventHandle) != KernelResult.Success)
  669. {
  670. throw new InvalidOperationException("Out of handles!");
  671. }
  672. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(activateEventHandle);
  673. return ResultCode.Success;
  674. }
  675. }
  676. return ResultCode.DeviceNotFound;
  677. }
  678. [Command(18)]
  679. // AttachDeactivateEvent(bytes<8, 4>) -> handle<copy>
  680. public ResultCode AttachDeactivateEvent(ServiceCtx context)
  681. {
  682. uint deviceHandle = context.RequestData.ReadUInt32();
  683. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  684. {
  685. if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
  686. {
  687. context.Device.System.NfpDevices[i].DeactivateEvent = new KEvent(context.Device.System.KernelContext);
  688. if (context.Process.HandleTable.GenerateHandle(context.Device.System.NfpDevices[i].DeactivateEvent.ReadableEvent, out int deactivateEventHandle) != KernelResult.Success)
  689. {
  690. throw new InvalidOperationException("Out of handles!");
  691. }
  692. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(deactivateEventHandle);
  693. return ResultCode.Success;
  694. }
  695. }
  696. return ResultCode.DeviceNotFound;
  697. }
  698. [Command(19)]
  699. // GetState() -> u32
  700. public ResultCode GetState(ServiceCtx context)
  701. {
  702. context.ResponseData.Write((int)_state);
  703. return ResultCode.Success;
  704. }
  705. [Command(20)]
  706. // GetDeviceState(bytes<8, 4>) -> u32
  707. public ResultCode GetDeviceState(ServiceCtx context)
  708. {
  709. uint deviceHandle = context.RequestData.ReadUInt32();
  710. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  711. {
  712. if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
  713. {
  714. if (context.Device.System.NfpDevices[i].State > NfpDeviceState.Finalized)
  715. {
  716. throw new ArgumentOutOfRangeException();
  717. }
  718. context.ResponseData.Write((uint)context.Device.System.NfpDevices[i].State);
  719. return ResultCode.Success;
  720. }
  721. }
  722. context.ResponseData.Write((uint)NfpDeviceState.Unavailable);
  723. return ResultCode.DeviceNotFound;
  724. }
  725. [Command(21)]
  726. // GetNpadId(bytes<8, 4>) -> u32
  727. public ResultCode GetNpadId(ServiceCtx context)
  728. {
  729. uint deviceHandle = context.RequestData.ReadUInt32();
  730. for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
  731. {
  732. if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
  733. {
  734. context.ResponseData.Write((uint)HidUtils.GetNpadIdTypeFromIndex(context.Device.System.NfpDevices[i].Handle));
  735. return ResultCode.Success;
  736. }
  737. }
  738. return ResultCode.DeviceNotFound;
  739. }
  740. [Command(22)]
  741. // GetApplicationAreaSize() -> u32
  742. public ResultCode GetApplicationAreaSize(ServiceCtx context)
  743. {
  744. context.ResponseData.Write(AmiiboConstants.ApplicationAreaSize);
  745. return ResultCode.Success;
  746. }
  747. [Command(23)] // 3.0.0+
  748. // AttachAvailabilityChangeEvent() -> handle<copy>
  749. public ResultCode AttachAvailabilityChangeEvent(ServiceCtx context)
  750. {
  751. _availabilityChangeEvent = new KEvent(context.Device.System.KernelContext);
  752. if (context.Process.HandleTable.GenerateHandle(_availabilityChangeEvent.ReadableEvent, out int availabilityChangeEventHandle) != KernelResult.Success)
  753. {
  754. throw new InvalidOperationException("Out of handles!");
  755. }
  756. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(availabilityChangeEventHandle);
  757. return ResultCode.Success;
  758. }
  759. [Command(24)] // 3.0.0+
  760. // RecreateApplicationArea(bytes<8, 4>, u32, buffer<unknown, 5>)
  761. public ResultCode RecreateApplicationArea(ServiceCtx context)
  762. {
  763. throw new ServiceNotImplementedException(this, context);
  764. }
  765. private ResultCode CheckNfcIsEnabled()
  766. {
  767. // TODO: Call nn::settings::detail::GetNfcEnableFlag when it will be implemented.
  768. return true ? ResultCode.Success : ResultCode.NfcDisabled;
  769. }
  770. }
  771. }