IApplicationFunctions.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using LibHac;
  2. using LibHac.Account;
  3. using LibHac.Common;
  4. using LibHac.Fs;
  5. using LibHac.Ns;
  6. using Ryujinx.Common;
  7. using Ryujinx.Common.Logging;
  8. using Ryujinx.HLE.HOS.Ipc;
  9. using Ryujinx.HLE.HOS.Kernel.Common;
  10. using Ryujinx.HLE.HOS.Kernel.Memory;
  11. using Ryujinx.HLE.HOS.Kernel.Threading;
  12. using Ryujinx.HLE.HOS.Services.Am.AppletAE.Storage;
  13. using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
  14. using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService;
  15. using Ryujinx.HLE.HOS.SystemState;
  16. using System;
  17. using System.Numerics;
  18. using static LibHac.Fs.ApplicationSaveDataManagement;
  19. using AccountUid = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  20. using ApplicationId = LibHac.Ncm.ApplicationId;
  21. namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy
  22. {
  23. class IApplicationFunctions : IpcService
  24. {
  25. private ulong _defaultSaveDataSize = 200000000;
  26. private ulong _defaultJournalSaveDataSize = 200000000;
  27. private KEvent _gpuErrorDetectedSystemEvent;
  28. private KEvent _friendInvitationStorageChannelEvent;
  29. private KEvent _notificationStorageChannelEvent;
  30. private KEvent _healthWarningDisappearedSystemEvent;
  31. private int _gpuErrorDetectedSystemEventHandle;
  32. private int _friendInvitationStorageChannelEventHandle;
  33. private int _notificationStorageChannelEventHandle;
  34. private int _healthWarningDisappearedSystemEventHandle;
  35. private HorizonClient _horizon;
  36. public IApplicationFunctions(Horizon system)
  37. {
  38. // TODO: Find where they are signaled.
  39. _gpuErrorDetectedSystemEvent = new KEvent(system.KernelContext);
  40. _friendInvitationStorageChannelEvent = new KEvent(system.KernelContext);
  41. _notificationStorageChannelEvent = new KEvent(system.KernelContext);
  42. _healthWarningDisappearedSystemEvent = new KEvent(system.KernelContext);
  43. _horizon = system.LibHacHorizonManager.AmClient;
  44. }
  45. [CommandHipc(1)]
  46. // PopLaunchParameter(LaunchParameterKind kind) -> object<nn::am::service::IStorage>
  47. public ResultCode PopLaunchParameter(ServiceCtx context)
  48. {
  49. LaunchParameterKind kind = (LaunchParameterKind)context.RequestData.ReadUInt32();
  50. byte[] storageData;
  51. switch (kind)
  52. {
  53. case LaunchParameterKind.UserChannel:
  54. storageData = context.Device.Configuration.UserChannelPersistence.Pop();
  55. break;
  56. case LaunchParameterKind.PreselectedUser:
  57. // Only the first 0x18 bytes of the Data seems to be actually used.
  58. storageData = StorageHelper.MakeLaunchParams(context.Device.System.AccountManager.LastOpenedUser);
  59. break;
  60. case LaunchParameterKind.Unknown:
  61. throw new NotImplementedException("Unknown LaunchParameterKind.");
  62. default:
  63. return ResultCode.ObjectInvalid;
  64. }
  65. if (storageData == null)
  66. {
  67. return ResultCode.NotAvailable;
  68. }
  69. MakeObject(context, new AppletAE.IStorage(storageData));
  70. return ResultCode.Success;
  71. }
  72. [CommandHipc(12)] // 4.0.0+
  73. // CreateApplicationAndRequestToStart(u64 title_id)
  74. public ResultCode CreateApplicationAndRequestToStart(ServiceCtx context)
  75. {
  76. ulong titleId = context.RequestData.ReadUInt64();
  77. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { titleId });
  78. if (titleId == 0)
  79. {
  80. context.Device.UiHandler.ExecuteProgram(context.Device, ProgramSpecifyKind.RestartProgram, titleId);
  81. }
  82. else
  83. {
  84. throw new NotImplementedException();
  85. }
  86. return ResultCode.Success;
  87. }
  88. [CommandHipc(20)]
  89. // EnsureSaveData(nn::account::Uid) -> u64
  90. public ResultCode EnsureSaveData(ServiceCtx context)
  91. {
  92. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  93. // Mask out the low nibble of the program ID to get the application ID
  94. ApplicationId applicationId = new ApplicationId(context.Device.Application.TitleId & ~0xFul);
  95. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.Application.ControlData;
  96. ref ApplicationControlProperty control = ref controlHolder.Value;
  97. if (LibHac.Common.Utilities.IsZeros(controlHolder.ByteSpan))
  98. {
  99. // If the current application doesn't have a loaded control property, create a dummy one
  100. // and set the savedata sizes so a user savedata will be created.
  101. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  102. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  103. control.UserAccountSaveDataSize = 0x4000;
  104. control.UserAccountSaveDataJournalSize = 0x4000;
  105. Logger.Warning?.Print(LogClass.ServiceAm,
  106. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  107. }
  108. HorizonClient hos = context.Device.System.LibHacHorizonManager.AmClient;
  109. Result result = EnsureApplicationSaveData(hos.Fs, out long requiredSize, applicationId, ref control, ref userId);
  110. context.ResponseData.Write(requiredSize);
  111. return (ResultCode)result.Value;
  112. }
  113. [CommandHipc(21)]
  114. // GetDesiredLanguage() -> nn::settings::LanguageCode
  115. public ResultCode GetDesiredLanguage(ServiceCtx context)
  116. {
  117. // This seems to be calling ns:am GetApplicationDesiredLanguage followed by ConvertApplicationLanguageToLanguageCode
  118. // Calls are from a IReadOnlyApplicationControlDataInterface object
  119. // ConvertApplicationLanguageToLanguageCode compares language code strings and returns the index
  120. // TODO: When above calls are implemented, switch to using ns:am
  121. long desiredLanguageCode = context.Device.System.State.DesiredLanguageCode;
  122. int supportedLanguages = (int)context.Device.Application.ControlData.Value.SupportedLanguages;
  123. int firstSupported = BitOperations.TrailingZeroCount(supportedLanguages);
  124. if (firstSupported > (int)SystemState.TitleLanguage.BrazilianPortuguese)
  125. {
  126. Logger.Warning?.Print(LogClass.ServiceAm, "Application has zero supported languages");
  127. context.ResponseData.Write(desiredLanguageCode);
  128. return ResultCode.Success;
  129. }
  130. // If desired language is not supported by application, use first supported language from TitleLanguage.
  131. // TODO: In the future, a GUI could enable user-specified search priority
  132. if (((1 << (int)context.Device.System.State.DesiredTitleLanguage) & supportedLanguages) == 0)
  133. {
  134. SystemLanguage newLanguage = Enum.Parse<SystemLanguage>(Enum.GetName(typeof(SystemState.TitleLanguage), firstSupported));
  135. desiredLanguageCode = SystemStateMgr.GetLanguageCode((int)newLanguage);
  136. Logger.Info?.Print(LogClass.ServiceAm, $"Application doesn't support configured language. Using {newLanguage}");
  137. }
  138. context.ResponseData.Write(desiredLanguageCode);
  139. return ResultCode.Success;
  140. }
  141. [CommandHipc(22)]
  142. // SetTerminateResult(u32)
  143. public ResultCode SetTerminateResult(ServiceCtx context)
  144. {
  145. Result result = new Result(context.RequestData.ReadUInt32());
  146. Logger.Info?.Print(LogClass.ServiceAm, $"Result = 0x{result.Value:x8} ({result.ToStringWithName()}).");
  147. return ResultCode.Success;
  148. }
  149. [CommandHipc(23)]
  150. // GetDisplayVersion() -> nn::oe::DisplayVersion
  151. public ResultCode GetDisplayVersion(ServiceCtx context)
  152. {
  153. // This should work as DisplayVersion U8Span always gives a 0x10 size byte array.
  154. // If an NACP isn't found, the buffer will be all '\0' which seems to be the correct implementation.
  155. context.ResponseData.Write(context.Device.Application.ControlData.Value.DisplayVersion);
  156. return ResultCode.Success;
  157. }
  158. [CommandHipc(25)] // 3.0.0+
  159. // ExtendSaveData(u8 save_data_type, nn::account::Uid, u64 save_size, u64 journal_size) -> u64 result_code
  160. public ResultCode ExtendSaveData(ServiceCtx context)
  161. {
  162. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadUInt64();
  163. Uid userId = context.RequestData.ReadStruct<Uid>();
  164. ulong saveDataSize = context.RequestData.ReadUInt64();
  165. ulong journalSize = context.RequestData.ReadUInt64();
  166. // NOTE: Service calls nn::fs::ExtendApplicationSaveData.
  167. // Since LibHac currently doesn't support this method, we can stub it for now.
  168. _defaultSaveDataSize = saveDataSize;
  169. _defaultJournalSaveDataSize = journalSize;
  170. context.ResponseData.Write((uint)ResultCode.Success);
  171. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { saveDataType, userId, saveDataSize, journalSize });
  172. return ResultCode.Success;
  173. }
  174. [CommandHipc(26)] // 3.0.0+
  175. // GetSaveDataSize(u8 save_data_type, nn::account::Uid) -> (u64 save_size, u64 journal_size)
  176. public ResultCode GetSaveDataSize(ServiceCtx context)
  177. {
  178. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadUInt64();
  179. Uid userId = context.RequestData.ReadStruct<Uid>();
  180. // NOTE: Service calls nn::fs::FindSaveDataWithFilter with SaveDataType = 1 hardcoded.
  181. // Then it calls nn::fs::GetSaveDataAvailableSize and nn::fs::GetSaveDataJournalSize to get the sizes.
  182. // Since LibHac currently doesn't support the 2 last methods, we can hardcode the values to 200mb.
  183. context.ResponseData.Write(_defaultSaveDataSize);
  184. context.ResponseData.Write(_defaultJournalSaveDataSize);
  185. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { saveDataType, userId });
  186. return ResultCode.Success;
  187. }
  188. [CommandHipc(27)] // 5.0.0+
  189. // CreateCacheStorage(u16 index, s64 save_size, s64 journal_size) -> (u32 storageTarget, u64 requiredSize)
  190. public ResultCode CreateCacheStorage(ServiceCtx context)
  191. {
  192. ushort index = (ushort)context.RequestData.ReadUInt64();
  193. long saveSize = context.RequestData.ReadInt64();
  194. long journalSize = context.RequestData.ReadInt64();
  195. // Mask out the low nibble of the program ID to get the application ID
  196. ApplicationId applicationId = new ApplicationId(context.Device.Application.TitleId & ~0xFul);
  197. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.Application.ControlData;
  198. Result result = _horizon.Fs.CreateApplicationCacheStorage(out long requiredSize,
  199. out CacheStorageTargetMedia storageTarget, applicationId, ref controlHolder.Value, index, saveSize,
  200. journalSize);
  201. if (result.IsFailure()) return (ResultCode)result.Value;
  202. context.ResponseData.Write((ulong)storageTarget);
  203. context.ResponseData.Write(requiredSize);
  204. return ResultCode.Success;
  205. }
  206. [CommandHipc(30)]
  207. // BeginBlockingHomeButtonShortAndLongPressed()
  208. public ResultCode BeginBlockingHomeButtonShortAndLongPressed(ServiceCtx context)
  209. {
  210. // NOTE: This set two internal fields at offsets 0x89 and 0x8B to value 1 then it signals an internal event.
  211. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  212. return ResultCode.Success;
  213. }
  214. [CommandHipc(31)]
  215. // EndBlockingHomeButtonShortAndLongPressed()
  216. public ResultCode EndBlockingHomeButtonShortAndLongPressed(ServiceCtx context)
  217. {
  218. // NOTE: This set two internal fields at offsets 0x89 and 0x8B to value 0 then it signals an internal event.
  219. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  220. return ResultCode.Success;
  221. }
  222. [CommandHipc(32)] // 2.0.0+
  223. // BeginBlockingHomeButton(u64 nano_second)
  224. public ResultCode BeginBlockingHomeButton(ServiceCtx context)
  225. {
  226. ulong nanoSeconds = context.RequestData.ReadUInt64();
  227. // NOTE: This set two internal fields at offsets 0x89 to value 1 and 0x90 to value of "nanoSeconds" then it signals an internal event.
  228. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { nanoSeconds });
  229. return ResultCode.Success;
  230. }
  231. [CommandHipc(33)] // 2.0.0+
  232. // EndBlockingHomeButton()
  233. public ResultCode EndBlockingHomeButton(ServiceCtx context)
  234. {
  235. // NOTE: This set two internal fields at offsets 0x89 and 0x90 to value 0 then it signals an internal event.
  236. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  237. return ResultCode.Success;
  238. }
  239. [CommandHipc(40)]
  240. // NotifyRunning() -> b8
  241. public ResultCode NotifyRunning(ServiceCtx context)
  242. {
  243. context.ResponseData.Write(true);
  244. return ResultCode.Success;
  245. }
  246. [CommandHipc(50)] // 2.0.0+
  247. // GetPseudoDeviceId() -> nn::util::Uuid
  248. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  249. {
  250. context.ResponseData.Write(0L);
  251. context.ResponseData.Write(0L);
  252. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  253. return ResultCode.Success;
  254. }
  255. [CommandHipc(60)] // 2.0.0+
  256. // SetMediaPlaybackStateForApplication(bool enabled)
  257. public ResultCode SetMediaPlaybackStateForApplication(ServiceCtx context)
  258. {
  259. bool enabled = context.RequestData.ReadBoolean();
  260. // NOTE: Service stores the "enabled" value in a private field, when enabled is false, it stores nn::os::GetSystemTick() too.
  261. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { enabled });
  262. return ResultCode.Success;
  263. }
  264. [CommandHipc(66)] // 3.0.0+
  265. // InitializeGamePlayRecording(u64, handle<copy>)
  266. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  267. {
  268. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  269. return ResultCode.Success;
  270. }
  271. [CommandHipc(67)] // 3.0.0+
  272. // SetGamePlayRecordingState(u32)
  273. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  274. {
  275. int state = context.RequestData.ReadInt32();
  276. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { state });
  277. return ResultCode.Success;
  278. }
  279. [CommandHipc(90)] // 4.0.0+
  280. // EnableApplicationCrashReport(u8)
  281. public ResultCode EnableApplicationCrashReport(ServiceCtx context)
  282. {
  283. bool applicationCrashReportEnabled = context.RequestData.ReadBoolean();
  284. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { applicationCrashReportEnabled });
  285. return ResultCode.Success;
  286. }
  287. [CommandHipc(100)] // 5.0.0+
  288. // InitializeApplicationCopyrightFrameBuffer(s32 width, s32 height, handle<copy, transfer_memory> transfer_memory, u64 transfer_memory_size)
  289. public ResultCode InitializeApplicationCopyrightFrameBuffer(ServiceCtx context)
  290. {
  291. int width = context.RequestData.ReadInt32();
  292. int height = context.RequestData.ReadInt32();
  293. ulong transferMemorySize = context.RequestData.ReadUInt64();
  294. int transferMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  295. ulong transferMemoryAddress = context.Process.HandleTable.GetObject<KTransferMemory>(transferMemoryHandle).Address;
  296. ResultCode resultCode = ResultCode.InvalidParameters;
  297. if (((transferMemorySize & 0x3FFFF) == 0) && width <= 1280 && height <= 720)
  298. {
  299. resultCode = InitializeApplicationCopyrightFrameBufferImpl(transferMemoryAddress, transferMemorySize, width, height);
  300. }
  301. if (transferMemoryHandle != 0)
  302. {
  303. context.Device.System.KernelContext.Syscall.CloseHandle(transferMemoryHandle);
  304. }
  305. return resultCode;
  306. }
  307. private ResultCode InitializeApplicationCopyrightFrameBufferImpl(ulong transferMemoryAddress, ulong transferMemorySize, int width, int height)
  308. {
  309. if ((transferMemorySize & 0x3FFFF) != 0)
  310. {
  311. return ResultCode.InvalidParameters;
  312. }
  313. ResultCode resultCode;
  314. // if (_copyrightBuffer == null)
  315. {
  316. // TODO: Initialize buffer and object.
  317. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { transferMemoryAddress, transferMemorySize, width, height });
  318. resultCode = ResultCode.Success;
  319. }
  320. return resultCode;
  321. }
  322. [CommandHipc(101)] // 5.0.0+
  323. // SetApplicationCopyrightImage(buffer<bytes, 0x45> frame_buffer, s32 x, s32 y, s32 width, s32 height, s32 window_origin_mode)
  324. public ResultCode SetApplicationCopyrightImage(ServiceCtx context)
  325. {
  326. ulong frameBufferPos = context.Request.SendBuff[0].Position;
  327. ulong frameBufferSize = context.Request.SendBuff[0].Size;
  328. int x = context.RequestData.ReadInt32();
  329. int y = context.RequestData.ReadInt32();
  330. int width = context.RequestData.ReadInt32();
  331. int height = context.RequestData.ReadInt32();
  332. uint windowOriginMode = context.RequestData.ReadUInt32();
  333. ResultCode resultCode = ResultCode.InvalidParameters;
  334. if (((y | x) >= 0) && width >= 1 && height >= 1)
  335. {
  336. ResultCode result = SetApplicationCopyrightImageImpl(x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode);
  337. if (result != ResultCode.Success)
  338. {
  339. resultCode = result;
  340. }
  341. else
  342. {
  343. resultCode = ResultCode.Success;
  344. }
  345. }
  346. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { frameBufferPos, frameBufferSize, x, y, width, height, windowOriginMode });
  347. return resultCode;
  348. }
  349. private ResultCode SetApplicationCopyrightImageImpl(int x, int y, int width, int height, ulong frameBufferPos, ulong frameBufferSize, uint windowOriginMode)
  350. {
  351. /*
  352. if (_copyrightBuffer == null)
  353. {
  354. return ResultCode.NullCopyrightObject;
  355. }
  356. */
  357. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode });
  358. return ResultCode.Success;
  359. }
  360. [CommandHipc(102)] // 5.0.0+
  361. // SetApplicationCopyrightVisibility(bool visible)
  362. public ResultCode SetApplicationCopyrightVisibility(ServiceCtx context)
  363. {
  364. bool visible = context.RequestData.ReadBoolean();
  365. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { visible });
  366. // NOTE: It sets an internal field and return ResultCode.Success in all case.
  367. return ResultCode.Success;
  368. }
  369. [CommandHipc(110)] // 5.0.0+
  370. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  371. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  372. {
  373. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  374. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  375. }
  376. [CommandHipc(111)] // 6.0.0+
  377. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  378. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  379. {
  380. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  381. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  382. }
  383. [CommandHipc(120)] // 5.0.0+
  384. // ExecuteProgram(ProgramSpecifyKind kind, u64 value)
  385. public ResultCode ExecuteProgram(ServiceCtx context)
  386. {
  387. ProgramSpecifyKind kind = (ProgramSpecifyKind)context.RequestData.ReadUInt32();
  388. // padding
  389. context.RequestData.ReadUInt32();
  390. ulong value = context.RequestData.ReadUInt64();
  391. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { kind, value });
  392. context.Device.UiHandler.ExecuteProgram(context.Device, kind, value);
  393. return ResultCode.Success;
  394. }
  395. [CommandHipc(121)] // 5.0.0+
  396. // ClearUserChannel()
  397. public ResultCode ClearUserChannel(ServiceCtx context)
  398. {
  399. context.Device.Configuration.UserChannelPersistence.Clear();
  400. return ResultCode.Success;
  401. }
  402. [CommandHipc(122)] // 5.0.0+
  403. // UnpopToUserChannel(object<nn::am::service::IStorage> input_storage)
  404. public ResultCode UnpopToUserChannel(ServiceCtx context)
  405. {
  406. AppletAE.IStorage data = GetObject<AppletAE.IStorage>(context, 0);
  407. context.Device.Configuration.UserChannelPersistence.Push(data.Data);
  408. return ResultCode.Success;
  409. }
  410. [CommandHipc(123)] // 5.0.0+
  411. // GetPreviousProgramIndex() -> s32 program_index
  412. public ResultCode GetPreviousProgramIndex(ServiceCtx context)
  413. {
  414. int previousProgramIndex = context.Device.Configuration.UserChannelPersistence.PreviousIndex;
  415. context.ResponseData.Write(previousProgramIndex);
  416. Logger.Stub?.PrintStub(LogClass.ServiceAm, new { previousProgramIndex });
  417. return ResultCode.Success;
  418. }
  419. [CommandHipc(130)] // 8.0.0+
  420. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  421. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  422. {
  423. if (_gpuErrorDetectedSystemEventHandle == 0)
  424. {
  425. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out _gpuErrorDetectedSystemEventHandle) != KernelResult.Success)
  426. {
  427. throw new InvalidOperationException("Out of handles!");
  428. }
  429. }
  430. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_gpuErrorDetectedSystemEventHandle);
  431. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  432. // A separate thread is setup where event-waiting is handled.
  433. // When the Event is signaled, official sw will assert.
  434. return ResultCode.Success;
  435. }
  436. [CommandHipc(140)] // 9.0.0+
  437. // GetFriendInvitationStorageChannelEvent() -> handle<copy>
  438. public ResultCode GetFriendInvitationStorageChannelEvent(ServiceCtx context)
  439. {
  440. if (_friendInvitationStorageChannelEventHandle == 0)
  441. {
  442. if (context.Process.HandleTable.GenerateHandle(_friendInvitationStorageChannelEvent.ReadableEvent, out _friendInvitationStorageChannelEventHandle) != KernelResult.Success)
  443. {
  444. throw new InvalidOperationException("Out of handles!");
  445. }
  446. }
  447. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_friendInvitationStorageChannelEventHandle);
  448. return ResultCode.Success;
  449. }
  450. [CommandHipc(141)] // 9.0.0+
  451. // TryPopFromFriendInvitationStorageChannel() -> object<nn::am::service::IStorage>
  452. public ResultCode TryPopFromFriendInvitationStorageChannel(ServiceCtx context)
  453. {
  454. // NOTE: IStorage are pushed in the channel with IApplicationAccessor PushToFriendInvitationStorageChannel
  455. // If _friendInvitationStorageChannelEvent is signaled, the event is cleared.
  456. // If an IStorage is available, returns it with ResultCode.Success.
  457. // If not, just returns ResultCode.NotAvailable. Since we don't support friend feature for now, it's fine to do the same.
  458. Logger.Stub?.PrintStub(LogClass.ServiceAm);
  459. return ResultCode.NotAvailable;
  460. }
  461. [CommandHipc(150)] // 9.0.0+
  462. // GetNotificationStorageChannelEvent() -> handle<copy>
  463. public ResultCode GetNotificationStorageChannelEvent(ServiceCtx context)
  464. {
  465. if (_notificationStorageChannelEventHandle == 0)
  466. {
  467. if (context.Process.HandleTable.GenerateHandle(_notificationStorageChannelEvent.ReadableEvent, out _notificationStorageChannelEventHandle) != KernelResult.Success)
  468. {
  469. throw new InvalidOperationException("Out of handles!");
  470. }
  471. }
  472. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_notificationStorageChannelEventHandle);
  473. return ResultCode.Success;
  474. }
  475. [CommandHipc(160)] // 9.0.0+
  476. // GetHealthWarningDisappearedSystemEvent() -> handle<copy>
  477. public ResultCode GetHealthWarningDisappearedSystemEvent(ServiceCtx context)
  478. {
  479. if (_healthWarningDisappearedSystemEventHandle == 0)
  480. {
  481. if (context.Process.HandleTable.GenerateHandle(_healthWarningDisappearedSystemEvent.ReadableEvent, out _healthWarningDisappearedSystemEventHandle) != KernelResult.Success)
  482. {
  483. throw new InvalidOperationException("Out of handles!");
  484. }
  485. }
  486. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_healthWarningDisappearedSystemEventHandle);
  487. return ResultCode.Success;
  488. }
  489. }
  490. }