ptm.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/logging/log.h"
  5. #include "core/file_sys/file_backend.h"
  6. #include "core/hle/service/fs/archive.h"
  7. #include "core/hle/service/ptm/ptm.h"
  8. #include "core/hle/service/ptm/ptm_gets.h"
  9. #include "core/hle/service/ptm/ptm_play.h"
  10. #include "core/hle/service/ptm/ptm_sets.h"
  11. #include "core/hle/service/ptm/ptm_sysm.h"
  12. #include "core/hle/service/ptm/ptm_u.h"
  13. #include "core/hle/service/service.h"
  14. #include "core/settings.h"
  15. namespace Service {
  16. namespace PTM {
  17. /// Values for the default gamecoin.dat file
  18. static const GameCoin default_game_coin = {0x4F00, 42, 0, 0, 0, 2014, 12, 29};
  19. /// Id of the SharedExtData archive used by the PTM process
  20. static const std::vector<u8> ptm_shared_extdata_id = {0, 0, 0, 0, 0x0B, 0, 0, 0xF0, 0, 0, 0, 0};
  21. static bool shell_open;
  22. static bool battery_is_charging;
  23. void GetAdapterState(Service::Interface* self) {
  24. u32* cmd_buff = Kernel::GetCommandBuffer();
  25. // TODO(purpasmart96): This function is only a stub,
  26. // it returns a valid result without implementing full functionality.
  27. cmd_buff[1] = RESULT_SUCCESS.raw;
  28. cmd_buff[2] = battery_is_charging ? 1 : 0;
  29. LOG_WARNING(Service_PTM, "(STUBBED) called");
  30. }
  31. void GetShellState(Service::Interface* self) {
  32. u32* cmd_buff = Kernel::GetCommandBuffer();
  33. cmd_buff[1] = RESULT_SUCCESS.raw;
  34. cmd_buff[2] = shell_open ? 1 : 0;
  35. }
  36. void GetBatteryLevel(Service::Interface* self) {
  37. u32* cmd_buff = Kernel::GetCommandBuffer();
  38. // TODO(purpasmart96): This function is only a stub,
  39. // it returns a valid result without implementing full functionality.
  40. cmd_buff[1] = RESULT_SUCCESS.raw;
  41. cmd_buff[2] =
  42. static_cast<u32>(ChargeLevels::CompletelyFull); // Set to a completely full battery
  43. LOG_WARNING(Service_PTM, "(STUBBED) called");
  44. }
  45. void GetBatteryChargeState(Service::Interface* self) {
  46. u32* cmd_buff = Kernel::GetCommandBuffer();
  47. // TODO(purpasmart96): This function is only a stub,
  48. // it returns a valid result without implementing full functionality.
  49. cmd_buff[1] = RESULT_SUCCESS.raw;
  50. cmd_buff[2] = battery_is_charging ? 1 : 0;
  51. LOG_WARNING(Service_PTM, "(STUBBED) called");
  52. }
  53. void GetTotalStepCount(Service::Interface* self) {
  54. u32* cmd_buff = Kernel::GetCommandBuffer();
  55. // TODO: This function is only a stub,
  56. // it returns 0 as the total step count
  57. cmd_buff[1] = RESULT_SUCCESS.raw;
  58. cmd_buff[2] = 0;
  59. LOG_WARNING(Service_PTM, "(STUBBED) called");
  60. }
  61. void GetSoftwareClosedFlag(Service::Interface* self) {
  62. u32* cmd_buff = Kernel::GetCommandBuffer();
  63. cmd_buff[1] = RESULT_SUCCESS.raw;
  64. cmd_buff[2] = 0;
  65. LOG_WARNING(Service_PTM, "(STUBBED) called");
  66. }
  67. void CheckNew3DS(IPC::RequestBuilder& rb) {
  68. const bool is_new_3ds = Settings::values.is_new_3ds;
  69. if (is_new_3ds) {
  70. LOG_CRITICAL(Service_PTM, "The option 'is_new_3ds' is enabled as part of the 'System' "
  71. "settings. Citra does not fully support New 3DS emulation yet!");
  72. }
  73. rb.Push(RESULT_SUCCESS);
  74. rb.Push(is_new_3ds);
  75. LOG_WARNING(Service_PTM, "(STUBBED) called isNew3DS = 0x%08x", static_cast<u32>(is_new_3ds));
  76. }
  77. void CheckNew3DS(Service::Interface* self) {
  78. IPC::RequestBuilder rb(Kernel::GetCommandBuffer(), 0x040A0000);
  79. CheckNew3DS(rb);
  80. }
  81. void Init() {
  82. AddService(new PTM_Gets);
  83. AddService(new PTM_Play);
  84. AddService(new PTM_S);
  85. AddService(new PTM_Sets);
  86. AddService(new PTM_Sysm);
  87. AddService(new PTM_U);
  88. shell_open = true;
  89. battery_is_charging = true;
  90. // Open the SharedExtSaveData archive 0xF000000B and create the gamecoin.dat file if it doesn't
  91. // exist
  92. FileSys::Path archive_path(ptm_shared_extdata_id);
  93. auto archive_result =
  94. Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
  95. // If the archive didn't exist, create the files inside
  96. if (archive_result.Code().description == ErrorDescription::FS_NotFormatted) {
  97. // Format the archive to create the directories
  98. Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SharedExtSaveData,
  99. FileSys::ArchiveFormatInfo(), archive_path);
  100. // Open it again to get a valid archive now that the folder exists
  101. archive_result =
  102. Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
  103. ASSERT_MSG(archive_result.Succeeded(), "Could not open the PTM SharedExtSaveData archive!");
  104. FileSys::Path gamecoin_path("/gamecoin.dat");
  105. FileSys::Mode open_mode = {};
  106. open_mode.write_flag.Assign(1);
  107. open_mode.create_flag.Assign(1);
  108. // Open the file and write the default gamecoin information
  109. auto gamecoin_result =
  110. Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode);
  111. if (gamecoin_result.Succeeded()) {
  112. auto gamecoin = gamecoin_result.MoveFrom();
  113. gamecoin->backend->Write(0, sizeof(GameCoin), true,
  114. reinterpret_cast<const u8*>(&default_game_coin));
  115. gamecoin->backend->Close();
  116. }
  117. }
  118. }
  119. void Shutdown() {}
  120. } // namespace PTM
  121. } // namespace Service