ptm_u.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/log.h"
  5. #include "common/make_unique.h"
  6. #include "core/file_sys/archive_extsavedata.h"
  7. #include "core/hle/hle.h"
  8. #include "core/hle/service/ptm_u.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Namespace PTM_U
  11. namespace PTM_U {
  12. /**
  13. * Represents the gamecoin file structure in the SharedExtData archive
  14. * More information in 3dbrew (http://www.3dbrew.org/wiki/Extdata#Shared_Extdata_0xf000000b_gamecoin.dat)
  15. */
  16. struct GameCoin {
  17. u32 magic; ///< Magic number: 0x4F00
  18. u16 total_coins; ///< Total Play Coins
  19. u16 total_coins_on_date; ///< Total Play Coins obtained on the date stored below.
  20. u32 step_count; ///< Total step count at the time a new Play Coin was obtained.
  21. u32 last_step_count; ///< Step count for the day the last Play Coin was obtained
  22. u16 year;
  23. u8 month;
  24. u8 day;
  25. };
  26. static const GameCoin default_game_coin = { 0x4F00, 42, 0, 0, 0, 2014, 12, 29 };
  27. static std::unique_ptr<FileSys::Archive_ExtSaveData> ptm_shared_extsavedata;
  28. static const std::vector<u8> ptm_shared_extdata_id = {0, 0, 0, 0, 0x0B, 0, 0, 0xF0, 0, 0, 0, 0};
  29. /// Charge levels used by PTM functions
  30. enum class ChargeLevels : u32 {
  31. CriticalBattery = 1,
  32. LowBattery = 2,
  33. HalfFull = 3,
  34. MostlyFull = 4,
  35. CompletelyFull = 5,
  36. };
  37. static bool shell_open = true;
  38. static bool battery_is_charging = true;
  39. /**
  40. * It is unknown if GetAdapterState is the same as GetBatteryChargeState,
  41. * it is likely to just be a duplicate function of GetBatteryChargeState
  42. * that controls another part of the HW.
  43. * PTM_U::GetAdapterState service function
  44. * Outputs:
  45. * 1 : Result of function, 0 on success, otherwise error code
  46. * 2 : Output of function, 0 = not charging, 1 = charging.
  47. */
  48. static void GetAdapterState(Service::Interface* self) {
  49. u32* cmd_buff = Kernel::GetCommandBuffer();
  50. // TODO(purpasmart96): This function is only a stub,
  51. // it returns a valid result without implementing full functionality.
  52. cmd_buff[1] = 0; // No error
  53. cmd_buff[2] = battery_is_charging ? 1 : 0;
  54. LOG_WARNING(Service_PTM, "(STUBBED) called");
  55. }
  56. /*
  57. * PTM_User::GetShellState service function.
  58. * Outputs:
  59. * 1 : Result of function, 0 on success, otherwise error code
  60. * 2 : Whether the 3DS's physical shell casing is open (1) or closed (0)
  61. */
  62. static void GetShellState(Service::Interface* self) {
  63. u32* cmd_buff = Kernel::GetCommandBuffer();
  64. cmd_buff[1] = 0;
  65. cmd_buff[2] = shell_open ? 1 : 0;
  66. LOG_TRACE(Service_PTM, "PTM_U::GetShellState called");
  67. }
  68. /**
  69. * PTM_U::GetBatteryLevel service function
  70. * Outputs:
  71. * 1 : Result of function, 0 on success, otherwise error code
  72. * 2 : Battery level, 5 = completely full battery, 4 = mostly full battery,
  73. * 3 = half full battery, 2 = low battery, 1 = critical battery.
  74. */
  75. static void GetBatteryLevel(Service::Interface* self) {
  76. u32* cmd_buff = Kernel::GetCommandBuffer();
  77. // TODO(purpasmart96): This function is only a stub,
  78. // it returns a valid result without implementing full functionality.
  79. cmd_buff[1] = 0; // No error
  80. cmd_buff[2] = static_cast<u32>(ChargeLevels::CompletelyFull); // Set to a completely full battery
  81. LOG_WARNING(Service_PTM, "(STUBBED) called");
  82. }
  83. /**
  84. * PTM_U::GetBatteryChargeState service function
  85. * Outputs:
  86. * 1 : Result of function, 0 on success, otherwise error code
  87. * 2 : Output of function, 0 = not charging, 1 = charging.
  88. */
  89. static void GetBatteryChargeState(Service::Interface* self) {
  90. u32* cmd_buff = Kernel::GetCommandBuffer();
  91. // TODO(purpasmart96): This function is only a stub,
  92. // it returns a valid result without implementing full functionality.
  93. cmd_buff[1] = 0; // No error
  94. cmd_buff[2] = battery_is_charging ? 1 : 0;
  95. LOG_WARNING(Service_PTM, "(STUBBED) called");
  96. }
  97. const Interface::FunctionInfo FunctionTable[] = {
  98. {0x00010002, nullptr, "RegisterAlarmClient"},
  99. {0x00020080, nullptr, "SetRtcAlarm"},
  100. {0x00030000, nullptr, "GetRtcAlarm"},
  101. {0x00040000, nullptr, "CancelRtcAlarm"},
  102. {0x00050000, GetAdapterState, "GetAdapterState"},
  103. {0x00060000, GetShellState, "GetShellState"},
  104. {0x00070000, GetBatteryLevel, "GetBatteryLevel"},
  105. {0x00080000, GetBatteryChargeState, "GetBatteryChargeState"},
  106. {0x00090000, nullptr, "GetPedometerState"},
  107. {0x000A0042, nullptr, "GetStepHistoryEntry"},
  108. {0x000B00C2, nullptr, "GetStepHistory"},
  109. {0x000C0000, nullptr, "GetTotalStepCount"},
  110. {0x000D0040, nullptr, "SetPedometerRecordingMode"},
  111. {0x000E0000, nullptr, "GetPedometerRecordingMode"},
  112. {0x000F0084, nullptr, "GetStepHistoryAll"},
  113. };
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////
  115. // Interface class
  116. Interface::Interface() {
  117. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  118. // Create the SharedExtSaveData archive 0xF000000B and the gamecoin.dat file
  119. // TODO(Subv): In the future we should use the FS service to query this archive
  120. std::string extsavedata_directory = FileUtil::GetUserPath(D_SHAREDEXTSAVEDATA);
  121. ptm_shared_extsavedata = Common::make_unique<FileSys::Archive_ExtSaveData>(extsavedata_directory);
  122. if (!ptm_shared_extsavedata->Initialize()) {
  123. LOG_CRITICAL(Service_PTM, "Could not initialize ExtSaveData archive for the PTM:U service");
  124. return;
  125. }
  126. FileSys::Path archive_path(ptm_shared_extdata_id);
  127. ResultCode result = ptm_shared_extsavedata->Open(archive_path);
  128. // If the archive didn't exist, create the files inside
  129. if (result.description == ErrorDescription::FS_NotFormatted) {
  130. // Format the archive to clear the directories
  131. ptm_shared_extsavedata->Format(archive_path);
  132. // Open it again to get a valid archive now that the folder exists
  133. ptm_shared_extsavedata->Open(archive_path);
  134. FileSys::Path gamecoin_path("gamecoin.dat");
  135. FileSys::Mode open_mode = {};
  136. open_mode.write_flag = 1;
  137. open_mode.create_flag = 1;
  138. // Open the file and write the default gamecoin information
  139. auto gamecoin = ptm_shared_extsavedata->OpenFile(gamecoin_path, open_mode);
  140. if (gamecoin != nullptr) {
  141. gamecoin->Write(0, sizeof(GameCoin), 1, reinterpret_cast<const u8*>(&default_game_coin));
  142. gamecoin->Close();
  143. }
  144. }
  145. }
  146. } // namespace