ptm.cpp 4.9 KB

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