ptm.cpp 4.3 KB

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