ptm.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(Service::Interface* self) {
  68. u32* cmd_buff = Kernel::GetCommandBuffer();
  69. const bool is_new_3ds = Settings::values.is_new_3ds;
  70. if (is_new_3ds) {
  71. LOG_CRITICAL(Service_PTM, "The option 'is_new_3ds' is enabled as part of the 'System' "
  72. "settings. Citra does not fully support New 3DS emulation yet!");
  73. }
  74. cmd_buff[1] = RESULT_SUCCESS.raw;
  75. cmd_buff[2] = is_new_3ds ? 1 : 0;
  76. LOG_WARNING(Service_PTM, "(STUBBED) called isNew3DS = 0x%08x", static_cast<u32>(is_new_3ds));
  77. }
  78. void Init() {
  79. AddService(new PTM_Gets);
  80. AddService(new PTM_Play);
  81. AddService(new PTM_S);
  82. AddService(new PTM_Sets);
  83. AddService(new PTM_Sysm);
  84. AddService(new PTM_U);
  85. shell_open = true;
  86. battery_is_charging = true;
  87. // Open the SharedExtSaveData archive 0xF000000B and create the gamecoin.dat file if it doesn't
  88. // exist
  89. FileSys::Path archive_path(ptm_shared_extdata_id);
  90. auto archive_result =
  91. Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
  92. // If the archive didn't exist, create the files inside
  93. if (archive_result.Code().description == ErrorDescription::FS_NotFormatted) {
  94. // Format the archive to create the directories
  95. Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SharedExtSaveData,
  96. FileSys::ArchiveFormatInfo(), archive_path);
  97. // Open it again to get a valid archive now that the folder exists
  98. archive_result =
  99. Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
  100. ASSERT_MSG(archive_result.Succeeded(), "Could not open the PTM SharedExtSaveData archive!");
  101. FileSys::Path gamecoin_path("/gamecoin.dat");
  102. Service::FS::CreateFileInArchive(*archive_result, gamecoin_path, sizeof(GameCoin));
  103. FileSys::Mode open_mode = {};
  104. open_mode.write_flag.Assign(1);
  105. // Open the file and write the default gamecoin information
  106. auto gamecoin_result =
  107. Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode);
  108. if (gamecoin_result.Succeeded()) {
  109. auto gamecoin = gamecoin_result.MoveFrom();
  110. gamecoin->backend->Write(0, sizeof(GameCoin), true,
  111. reinterpret_cast<const u8*>(&default_game_coin));
  112. gamecoin->backend->Close();
  113. }
  114. }
  115. }
  116. void Shutdown() {}
  117. } // namespace PTM
  118. } // namespace Service